blob: a96c59b3400b08323821d474bb8f34bf8013fcc8 [file] [log] [blame]
Nate Begeman4ebd8052005-09-01 23:24:04 +00001//===-- DAGCombiner.cpp - Implement a DAG node combiner -------------------===//
Nate Begeman1d4d4142005-09-01 00:19:25 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Nate Begeman and is distributed under the
6// University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This pass combines dag nodes to form fewer, simpler DAG nodes. It can be run
11// both before and after the DAG is legalized.
12//
13// FIXME: Missing folds
14// sdiv, udiv, srem, urem (X, const) where X is an integer can be expanded into
15// a sequence of multiplies, shifts, and adds. This should be controlled by
16// some kind of hint from the target that int div is expensive.
17// various folds of mulh[s,u] by constants such as -1, powers of 2, etc.
18//
19// FIXME: Should add a corresponding version of fold AND with
20// ZERO_EXTEND/SIGN_EXTEND by converting them to an ANY_EXTEND node which
21// we don't have yet.
22//
Nate Begeman44728a72005-09-19 22:34:01 +000023// FIXME: select C, pow2, pow2 -> something smart
24// FIXME: trunc(select X, Y, Z) -> select X, trunc(Y), trunc(Z)
Nate Begeman44728a72005-09-19 22:34:01 +000025// FIXME: Dead stores -> nuke
Chris Lattner40c62d52005-10-18 06:04:22 +000026// FIXME: shr X, (and Y,31) -> shr X, Y (TRICKY!)
Nate Begeman1d4d4142005-09-01 00:19:25 +000027// FIXME: mul (x, const) -> shifts + adds
Nate Begeman1d4d4142005-09-01 00:19:25 +000028// FIXME: undef values
Nate Begeman4ebd8052005-09-01 23:24:04 +000029// FIXME: make truncate see through SIGN_EXTEND and AND
Nate Begeman4ebd8052005-09-01 23:24:04 +000030// FIXME: (sra (sra x, c1), c2) -> (sra x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +000031// FIXME: verify that getNode can't return extends with an operand whose type
32// is >= to that of the extend.
33// FIXME: divide by zero is currently left unfolded. do we want to turn this
34// into an undef?
Nate Begemanf845b452005-10-08 00:29:44 +000035// FIXME: select ne (select cc, 1, 0), 0, true, false -> select cc, true, false
Chris Lattner01a22022005-10-10 22:04:48 +000036// FIXME: reassociate (X+C)+Y into (X+Y)+C if the inner expression has one use
Nate Begeman1d4d4142005-09-01 00:19:25 +000037//
38//===----------------------------------------------------------------------===//
39
40#define DEBUG_TYPE "dagcombine"
41#include "llvm/ADT/Statistic.h"
42#include "llvm/CodeGen/SelectionDAG.h"
Nate Begeman2300f552005-09-07 00:15:36 +000043#include "llvm/Support/Debug.h"
Nate Begeman1d4d4142005-09-01 00:19:25 +000044#include "llvm/Support/MathExtras.h"
45#include "llvm/Target/TargetLowering.h"
Chris Lattnera500fc62005-09-09 23:53:39 +000046#include <algorithm>
Nate Begeman1d4d4142005-09-01 00:19:25 +000047#include <cmath>
Chris Lattner2c2c6c62006-01-22 23:41:00 +000048#include <iostream>
Nate Begeman1d4d4142005-09-01 00:19:25 +000049using namespace llvm;
50
51namespace {
52 Statistic<> NodesCombined ("dagcombiner", "Number of dag nodes combined");
53
54 class DAGCombiner {
55 SelectionDAG &DAG;
56 TargetLowering &TLI;
Nate Begeman4ebd8052005-09-01 23:24:04 +000057 bool AfterLegalize;
Nate Begeman1d4d4142005-09-01 00:19:25 +000058
59 // Worklist of all of the nodes that need to be simplified.
60 std::vector<SDNode*> WorkList;
61
62 /// AddUsersToWorkList - When an instruction is simplified, add all users of
63 /// the instruction to the work lists because they might get more simplified
64 /// now.
65 ///
66 void AddUsersToWorkList(SDNode *N) {
67 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
Nate Begeman4ebd8052005-09-01 23:24:04 +000068 UI != UE; ++UI)
69 WorkList.push_back(*UI);
Nate Begeman1d4d4142005-09-01 00:19:25 +000070 }
71
72 /// removeFromWorkList - remove all instances of N from the worklist.
73 void removeFromWorkList(SDNode *N) {
74 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), N),
75 WorkList.end());
76 }
77
Chris Lattner01a22022005-10-10 22:04:48 +000078 SDOperand CombineTo(SDNode *N, const std::vector<SDOperand> &To) {
Chris Lattner87514ca2005-10-10 22:31:19 +000079 ++NodesCombined;
Chris Lattner01a22022005-10-10 22:04:48 +000080 DEBUG(std::cerr << "\nReplacing "; N->dump();
81 std::cerr << "\nWith: "; To[0].Val->dump();
82 std::cerr << " and " << To.size()-1 << " other values\n");
83 std::vector<SDNode*> NowDead;
84 DAG.ReplaceAllUsesWith(N, To, &NowDead);
85
86 // Push the new nodes and any users onto the worklist
87 for (unsigned i = 0, e = To.size(); i != e; ++i) {
88 WorkList.push_back(To[i].Val);
89 AddUsersToWorkList(To[i].Val);
90 }
91
92 // Nodes can end up on the worklist more than once. Make sure we do
93 // not process a node that has been replaced.
94 removeFromWorkList(N);
95 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
96 removeFromWorkList(NowDead[i]);
97
98 // Finally, since the node is now dead, remove it from the graph.
99 DAG.DeleteNode(N);
100 return SDOperand(N, 0);
101 }
Chris Lattner87514ca2005-10-10 22:31:19 +0000102
103 SDOperand CombineTo(SDNode *N, SDOperand Res) {
104 std::vector<SDOperand> To;
105 To.push_back(Res);
106 return CombineTo(N, To);
107 }
Chris Lattner01a22022005-10-10 22:04:48 +0000108
109 SDOperand CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1) {
110 std::vector<SDOperand> To;
111 To.push_back(Res0);
112 To.push_back(Res1);
113 return CombineTo(N, To);
114 }
115
Nate Begeman1d4d4142005-09-01 00:19:25 +0000116 /// visit - call the node-specific routine that knows how to fold each
117 /// particular type of node.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000118 SDOperand visit(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000119
120 // Visitation implementation - Implement dag node combining for different
121 // node types. The semantics are as follows:
122 // Return Value:
Nate Begeman2300f552005-09-07 00:15:36 +0000123 // SDOperand.Val == 0 - No change was made
Chris Lattner01a22022005-10-10 22:04:48 +0000124 // SDOperand.Val == N - N was replaced, is dead, and is already handled.
Nate Begeman2300f552005-09-07 00:15:36 +0000125 // otherwise - N should be replaced by the returned Operand.
Nate Begeman1d4d4142005-09-01 00:19:25 +0000126 //
Nate Begeman83e75ec2005-09-06 04:43:02 +0000127 SDOperand visitTokenFactor(SDNode *N);
128 SDOperand visitADD(SDNode *N);
129 SDOperand visitSUB(SDNode *N);
130 SDOperand visitMUL(SDNode *N);
131 SDOperand visitSDIV(SDNode *N);
132 SDOperand visitUDIV(SDNode *N);
133 SDOperand visitSREM(SDNode *N);
134 SDOperand visitUREM(SDNode *N);
135 SDOperand visitMULHU(SDNode *N);
136 SDOperand visitMULHS(SDNode *N);
137 SDOperand visitAND(SDNode *N);
138 SDOperand visitOR(SDNode *N);
139 SDOperand visitXOR(SDNode *N);
140 SDOperand visitSHL(SDNode *N);
141 SDOperand visitSRA(SDNode *N);
142 SDOperand visitSRL(SDNode *N);
143 SDOperand visitCTLZ(SDNode *N);
144 SDOperand visitCTTZ(SDNode *N);
145 SDOperand visitCTPOP(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000146 SDOperand visitSELECT(SDNode *N);
147 SDOperand visitSELECT_CC(SDNode *N);
148 SDOperand visitSETCC(SDNode *N);
Nate Begeman5054f162005-10-14 01:12:21 +0000149 SDOperand visitADD_PARTS(SDNode *N);
150 SDOperand visitSUB_PARTS(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000151 SDOperand visitSIGN_EXTEND(SDNode *N);
152 SDOperand visitZERO_EXTEND(SDNode *N);
153 SDOperand visitSIGN_EXTEND_INREG(SDNode *N);
154 SDOperand visitTRUNCATE(SDNode *N);
Chris Lattner94683772005-12-23 05:30:37 +0000155 SDOperand visitBIT_CONVERT(SDNode *N);
Nate Begeman5054f162005-10-14 01:12:21 +0000156
Chris Lattner01b3d732005-09-28 22:28:18 +0000157 SDOperand visitFADD(SDNode *N);
158 SDOperand visitFSUB(SDNode *N);
159 SDOperand visitFMUL(SDNode *N);
160 SDOperand visitFDIV(SDNode *N);
161 SDOperand visitFREM(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000162 SDOperand visitSINT_TO_FP(SDNode *N);
163 SDOperand visitUINT_TO_FP(SDNode *N);
164 SDOperand visitFP_TO_SINT(SDNode *N);
165 SDOperand visitFP_TO_UINT(SDNode *N);
166 SDOperand visitFP_ROUND(SDNode *N);
167 SDOperand visitFP_ROUND_INREG(SDNode *N);
168 SDOperand visitFP_EXTEND(SDNode *N);
169 SDOperand visitFNEG(SDNode *N);
170 SDOperand visitFABS(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000171 SDOperand visitBRCOND(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000172 SDOperand visitBRCONDTWOWAY(SDNode *N);
173 SDOperand visitBR_CC(SDNode *N);
174 SDOperand visitBRTWOWAY_CC(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000175
Chris Lattner01a22022005-10-10 22:04:48 +0000176 SDOperand visitLOAD(SDNode *N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000177 SDOperand visitSTORE(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000178
Jim Laskeyd6e8d412005-12-23 20:08:28 +0000179 SDOperand visitLOCATION(SDNode *N);
180 SDOperand visitDEBUGLOC(SDNode *N);
181
Chris Lattner40c62d52005-10-18 06:04:22 +0000182 bool SimplifySelectOps(SDNode *SELECT, SDOperand LHS, SDOperand RHS);
Nate Begeman44728a72005-09-19 22:34:01 +0000183 SDOperand SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2);
184 SDOperand SimplifySelectCC(SDOperand N0, SDOperand N1, SDOperand N2,
185 SDOperand N3, ISD::CondCode CC);
Nate Begeman452d7be2005-09-16 00:54:12 +0000186 SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N0, SDOperand N1,
Nate Begemane17daeb2005-10-05 21:43:42 +0000187 ISD::CondCode Cond, bool foldBooleans = true);
Nate Begeman69575232005-10-20 02:15:44 +0000188
189 SDOperand BuildSDIV(SDNode *N);
190 SDOperand BuildUDIV(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000191public:
192 DAGCombiner(SelectionDAG &D)
Nate Begeman646d7e22005-09-02 21:18:40 +0000193 : DAG(D), TLI(D.getTargetLoweringInfo()), AfterLegalize(false) {}
Nate Begeman1d4d4142005-09-01 00:19:25 +0000194
195 /// Run - runs the dag combiner on all nodes in the work list
Nate Begeman4ebd8052005-09-01 23:24:04 +0000196 void Run(bool RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000197 };
198}
199
Nate Begeman69575232005-10-20 02:15:44 +0000200struct ms {
201 int64_t m; // magic number
202 int64_t s; // shift amount
203};
204
205struct mu {
206 uint64_t m; // magic number
207 int64_t a; // add indicator
208 int64_t s; // shift amount
209};
210
211/// magic - calculate the magic numbers required to codegen an integer sdiv as
212/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
213/// or -1.
214static ms magic32(int32_t d) {
215 int32_t p;
216 uint32_t ad, anc, delta, q1, r1, q2, r2, t;
217 const uint32_t two31 = 0x80000000U;
218 struct ms mag;
219
220 ad = abs(d);
221 t = two31 + ((uint32_t)d >> 31);
222 anc = t - 1 - t%ad; // absolute value of nc
223 p = 31; // initialize p
224 q1 = two31/anc; // initialize q1 = 2p/abs(nc)
225 r1 = two31 - q1*anc; // initialize r1 = rem(2p,abs(nc))
226 q2 = two31/ad; // initialize q2 = 2p/abs(d)
227 r2 = two31 - q2*ad; // initialize r2 = rem(2p,abs(d))
228 do {
229 p = p + 1;
230 q1 = 2*q1; // update q1 = 2p/abs(nc)
231 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
232 if (r1 >= anc) { // must be unsigned comparison
233 q1 = q1 + 1;
234 r1 = r1 - anc;
235 }
236 q2 = 2*q2; // update q2 = 2p/abs(d)
237 r2 = 2*r2; // update r2 = rem(2p/abs(d))
238 if (r2 >= ad) { // must be unsigned comparison
239 q2 = q2 + 1;
240 r2 = r2 - ad;
241 }
242 delta = ad - r2;
243 } while (q1 < delta || (q1 == delta && r1 == 0));
244
245 mag.m = (int32_t)(q2 + 1); // make sure to sign extend
246 if (d < 0) mag.m = -mag.m; // resulting magic number
247 mag.s = p - 32; // resulting shift
248 return mag;
249}
250
251/// magicu - calculate the magic numbers required to codegen an integer udiv as
252/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
253static mu magicu32(uint32_t d) {
254 int32_t p;
255 uint32_t nc, delta, q1, r1, q2, r2;
256 struct mu magu;
257 magu.a = 0; // initialize "add" indicator
258 nc = - 1 - (-d)%d;
259 p = 31; // initialize p
260 q1 = 0x80000000/nc; // initialize q1 = 2p/nc
261 r1 = 0x80000000 - q1*nc; // initialize r1 = rem(2p,nc)
262 q2 = 0x7FFFFFFF/d; // initialize q2 = (2p-1)/d
263 r2 = 0x7FFFFFFF - q2*d; // initialize r2 = rem((2p-1),d)
264 do {
265 p = p + 1;
266 if (r1 >= nc - r1 ) {
267 q1 = 2*q1 + 1; // update q1
268 r1 = 2*r1 - nc; // update r1
269 }
270 else {
271 q1 = 2*q1; // update q1
272 r1 = 2*r1; // update r1
273 }
274 if (r2 + 1 >= d - r2) {
275 if (q2 >= 0x7FFFFFFF) magu.a = 1;
276 q2 = 2*q2 + 1; // update q2
277 r2 = 2*r2 + 1 - d; // update r2
278 }
279 else {
280 if (q2 >= 0x80000000) magu.a = 1;
281 q2 = 2*q2; // update q2
282 r2 = 2*r2 + 1; // update r2
283 }
284 delta = d - 1 - r2;
285 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
286 magu.m = q2 + 1; // resulting magic number
287 magu.s = p - 32; // resulting shift
288 return magu;
289}
290
291/// magic - calculate the magic numbers required to codegen an integer sdiv as
292/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
293/// or -1.
294static ms magic64(int64_t d) {
295 int64_t p;
296 uint64_t ad, anc, delta, q1, r1, q2, r2, t;
297 const uint64_t two63 = 9223372036854775808ULL; // 2^63
298 struct ms mag;
299
Chris Lattnerf75f2a02005-10-20 17:01:00 +0000300 ad = d >= 0 ? d : -d;
Nate Begeman69575232005-10-20 02:15:44 +0000301 t = two63 + ((uint64_t)d >> 63);
302 anc = t - 1 - t%ad; // absolute value of nc
303 p = 63; // initialize p
304 q1 = two63/anc; // initialize q1 = 2p/abs(nc)
305 r1 = two63 - q1*anc; // initialize r1 = rem(2p,abs(nc))
306 q2 = two63/ad; // initialize q2 = 2p/abs(d)
307 r2 = two63 - q2*ad; // initialize r2 = rem(2p,abs(d))
308 do {
309 p = p + 1;
310 q1 = 2*q1; // update q1 = 2p/abs(nc)
311 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
312 if (r1 >= anc) { // must be unsigned comparison
313 q1 = q1 + 1;
314 r1 = r1 - anc;
315 }
316 q2 = 2*q2; // update q2 = 2p/abs(d)
317 r2 = 2*r2; // update r2 = rem(2p/abs(d))
318 if (r2 >= ad) { // must be unsigned comparison
319 q2 = q2 + 1;
320 r2 = r2 - ad;
321 }
322 delta = ad - r2;
323 } while (q1 < delta || (q1 == delta && r1 == 0));
324
325 mag.m = q2 + 1;
326 if (d < 0) mag.m = -mag.m; // resulting magic number
327 mag.s = p - 64; // resulting shift
328 return mag;
329}
330
331/// magicu - calculate the magic numbers required to codegen an integer udiv as
332/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
333static mu magicu64(uint64_t d)
334{
335 int64_t p;
336 uint64_t nc, delta, q1, r1, q2, r2;
337 struct mu magu;
338 magu.a = 0; // initialize "add" indicator
339 nc = - 1 - (-d)%d;
340 p = 63; // initialize p
341 q1 = 0x8000000000000000ull/nc; // initialize q1 = 2p/nc
342 r1 = 0x8000000000000000ull - q1*nc; // initialize r1 = rem(2p,nc)
343 q2 = 0x7FFFFFFFFFFFFFFFull/d; // initialize q2 = (2p-1)/d
344 r2 = 0x7FFFFFFFFFFFFFFFull - q2*d; // initialize r2 = rem((2p-1),d)
345 do {
346 p = p + 1;
347 if (r1 >= nc - r1 ) {
348 q1 = 2*q1 + 1; // update q1
349 r1 = 2*r1 - nc; // update r1
350 }
351 else {
352 q1 = 2*q1; // update q1
353 r1 = 2*r1; // update r1
354 }
355 if (r2 + 1 >= d - r2) {
356 if (q2 >= 0x7FFFFFFFFFFFFFFFull) magu.a = 1;
357 q2 = 2*q2 + 1; // update q2
358 r2 = 2*r2 + 1 - d; // update r2
359 }
360 else {
361 if (q2 >= 0x8000000000000000ull) magu.a = 1;
362 q2 = 2*q2; // update q2
363 r2 = 2*r2 + 1; // update r2
364 }
365 delta = d - 1 - r2;
366 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
367 magu.m = q2 + 1; // resulting magic number
368 magu.s = p - 64; // resulting shift
369 return magu;
370}
371
Nate Begeman07ed4172005-10-10 21:26:48 +0000372/// MaskedValueIsZero - Return true if 'Op & Mask' is known to be zero. We use
373/// this predicate to simplify operations downstream. Op and Mask are known to
Nate Begeman1d4d4142005-09-01 00:19:25 +0000374/// be the same type.
375static bool MaskedValueIsZero(const SDOperand &Op, uint64_t Mask,
376 const TargetLowering &TLI) {
377 unsigned SrcBits;
378 if (Mask == 0) return true;
379
380 // If we know the result of a setcc has the top bits zero, use this info.
381 switch (Op.getOpcode()) {
Nate Begeman4ebd8052005-09-01 23:24:04 +0000382 case ISD::Constant:
383 return (cast<ConstantSDNode>(Op)->getValue() & Mask) == 0;
384 case ISD::SETCC:
385 return ((Mask & 1) == 0) &&
386 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult;
387 case ISD::ZEXTLOAD:
388 SrcBits = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(3))->getVT());
389 return (Mask & ((1ULL << SrcBits)-1)) == 0; // Returning only the zext bits.
390 case ISD::ZERO_EXTEND:
391 SrcBits = MVT::getSizeInBits(Op.getOperand(0).getValueType());
Chris Lattner7c225752005-11-02 01:47:04 +0000392 return MaskedValueIsZero(Op.getOperand(0),Mask & (~0ULL >> (64-SrcBits)),TLI);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000393 case ISD::AssertZext:
394 SrcBits = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
395 return (Mask & ((1ULL << SrcBits)-1)) == 0; // Returning only the zext bits.
396 case ISD::AND:
Chris Lattneree899e62005-10-09 22:12:36 +0000397 // If either of the operands has zero bits, the result will too.
398 if (MaskedValueIsZero(Op.getOperand(1), Mask, TLI) ||
399 MaskedValueIsZero(Op.getOperand(0), Mask, TLI))
400 return true;
Nate Begeman4ebd8052005-09-01 23:24:04 +0000401 // (X & C1) & C2 == 0 iff C1 & C2 == 0.
402 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
403 return MaskedValueIsZero(Op.getOperand(0),AndRHS->getValue() & Mask, TLI);
Chris Lattneree899e62005-10-09 22:12:36 +0000404 return false;
Nate Begeman4ebd8052005-09-01 23:24:04 +0000405 case ISD::OR:
406 case ISD::XOR:
407 return MaskedValueIsZero(Op.getOperand(0), Mask, TLI) &&
408 MaskedValueIsZero(Op.getOperand(1), Mask, TLI);
409 case ISD::SELECT:
410 return MaskedValueIsZero(Op.getOperand(1), Mask, TLI) &&
411 MaskedValueIsZero(Op.getOperand(2), Mask, TLI);
412 case ISD::SELECT_CC:
413 return MaskedValueIsZero(Op.getOperand(2), Mask, TLI) &&
414 MaskedValueIsZero(Op.getOperand(3), Mask, TLI);
415 case ISD::SRL:
416 // (ushr X, C1) & C2 == 0 iff X & (C2 << C1) == 0
417 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
418 uint64_t NewVal = Mask << ShAmt->getValue();
419 SrcBits = MVT::getSizeInBits(Op.getValueType());
420 if (SrcBits != 64) NewVal &= (1ULL << SrcBits)-1;
421 return MaskedValueIsZero(Op.getOperand(0), NewVal, TLI);
422 }
423 return false;
424 case ISD::SHL:
425 // (ushl X, C1) & C2 == 0 iff X & (C2 >> C1) == 0
426 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
427 uint64_t NewVal = Mask >> ShAmt->getValue();
428 return MaskedValueIsZero(Op.getOperand(0), NewVal, TLI);
429 }
430 return false;
Chris Lattnerbba9aa32005-10-10 16:51:40 +0000431 case ISD::ADD:
Chris Lattnerd7390752005-10-10 16:52:03 +0000432 // (add X, Y) & C == 0 iff (X&C)|(Y&C) == 0 and all bits are low bits.
Chris Lattnerbba9aa32005-10-10 16:51:40 +0000433 if ((Mask&(Mask+1)) == 0) { // All low bits
434 if (MaskedValueIsZero(Op.getOperand(0), Mask, TLI) &&
435 MaskedValueIsZero(Op.getOperand(1), Mask, TLI))
436 return true;
437 }
438 break;
Chris Lattnerc4ced262005-10-07 15:30:32 +0000439 case ISD::SUB:
440 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
441 // We know that the top bits of C-X are clear if X contains less bits
442 // than C (i.e. no wrap-around can happen). For example, 20-X is
443 // positive if we can prove that X is >= 0 and < 16.
444 unsigned Bits = MVT::getSizeInBits(CLHS->getValueType(0));
445 if ((CLHS->getValue() & (1 << (Bits-1))) == 0) { // sign bit clear
446 unsigned NLZ = CountLeadingZeros_64(CLHS->getValue()+1);
447 uint64_t MaskV = (1ULL << (63-NLZ))-1;
448 if (MaskedValueIsZero(Op.getOperand(1), ~MaskV, TLI)) {
449 // High bits are clear this value is known to be >= C.
450 unsigned NLZ2 = CountLeadingZeros_64(CLHS->getValue());
451 if ((Mask & ((1ULL << (64-NLZ2))-1)) == 0)
452 return true;
453 }
454 }
455 }
456 break;
Nate Begeman4ebd8052005-09-01 23:24:04 +0000457 case ISD::CTTZ:
458 case ISD::CTLZ:
459 case ISD::CTPOP:
460 // Bit counting instructions can not set the high bits of the result
461 // register. The max number of bits sets depends on the input.
462 return (Mask & (MVT::getSizeInBits(Op.getValueType())*2-1)) == 0;
Evan Cheng3a03ebb2005-12-21 23:05:39 +0000463 default:
464 if (Op.getOpcode() >= ISD::BUILTIN_OP_END)
465 return TLI.isMaskedValueZeroForTargetNode(Op, Mask);
466 break;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000467 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000468 return false;
469}
470
Nate Begeman4ebd8052005-09-01 23:24:04 +0000471// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
472// that selects between the values 1 and 0, making it equivalent to a setcc.
Nate Begeman646d7e22005-09-02 21:18:40 +0000473// Also, set the incoming LHS, RHS, and CC references to the appropriate
474// nodes based on the type of node we are checking. This simplifies life a
475// bit for the callers.
476static bool isSetCCEquivalent(SDOperand N, SDOperand &LHS, SDOperand &RHS,
477 SDOperand &CC) {
478 if (N.getOpcode() == ISD::SETCC) {
479 LHS = N.getOperand(0);
480 RHS = N.getOperand(1);
481 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000482 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000483 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000484 if (N.getOpcode() == ISD::SELECT_CC &&
485 N.getOperand(2).getOpcode() == ISD::Constant &&
486 N.getOperand(3).getOpcode() == ISD::Constant &&
487 cast<ConstantSDNode>(N.getOperand(2))->getValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000488 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
489 LHS = N.getOperand(0);
490 RHS = N.getOperand(1);
491 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000492 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000493 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000494 return false;
495}
496
Nate Begeman99801192005-09-07 23:25:52 +0000497// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
498// one use. If this is true, it allows the users to invert the operation for
499// free when it is profitable to do so.
500static bool isOneUseSetCC(SDOperand N) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000501 SDOperand N0, N1, N2;
Nate Begeman646d7e22005-09-02 21:18:40 +0000502 if (isSetCCEquivalent(N, N0, N1, N2) && N.Val->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000503 return true;
504 return false;
505}
506
Nate Begeman452d7be2005-09-16 00:54:12 +0000507// FIXME: This should probably go in the ISD class rather than being duplicated
508// in several files.
509static bool isCommutativeBinOp(unsigned Opcode) {
510 switch (Opcode) {
511 case ISD::ADD:
512 case ISD::MUL:
513 case ISD::AND:
514 case ISD::OR:
515 case ISD::XOR: return true;
516 default: return false; // FIXME: Need commutative info for user ops!
517 }
518}
519
Nate Begeman4ebd8052005-09-01 23:24:04 +0000520void DAGCombiner::Run(bool RunningAfterLegalize) {
521 // set the instance variable, so that the various visit routines may use it.
522 AfterLegalize = RunningAfterLegalize;
523
Nate Begeman646d7e22005-09-02 21:18:40 +0000524 // Add all the dag nodes to the worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000525 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
526 E = DAG.allnodes_end(); I != E; ++I)
527 WorkList.push_back(I);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000528
Chris Lattner95038592005-10-05 06:35:28 +0000529 // Create a dummy node (which is not added to allnodes), that adds a reference
530 // to the root node, preventing it from being deleted, and tracking any
531 // changes of the root.
532 HandleSDNode Dummy(DAG.getRoot());
533
Nate Begeman1d4d4142005-09-01 00:19:25 +0000534 // while the worklist isn't empty, inspect the node on the end of it and
535 // try and combine it.
536 while (!WorkList.empty()) {
537 SDNode *N = WorkList.back();
538 WorkList.pop_back();
539
540 // If N has no uses, it is dead. Make sure to revisit all N's operands once
Chris Lattner95038592005-10-05 06:35:28 +0000541 // N is deleted from the DAG, since they too may now be dead or may have a
542 // reduced number of uses, allowing other xforms.
543 if (N->use_empty() && N != &Dummy) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000544 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
545 WorkList.push_back(N->getOperand(i).Val);
546
Nate Begeman1d4d4142005-09-01 00:19:25 +0000547 removeFromWorkList(N);
Chris Lattner95038592005-10-05 06:35:28 +0000548 DAG.DeleteNode(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000549 continue;
550 }
551
Nate Begeman83e75ec2005-09-06 04:43:02 +0000552 SDOperand RV = visit(N);
553 if (RV.Val) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000554 ++NodesCombined;
Nate Begeman646d7e22005-09-02 21:18:40 +0000555 // If we get back the same node we passed in, rather than a new node or
556 // zero, we know that the node must have defined multiple values and
557 // CombineTo was used. Since CombineTo takes care of the worklist
558 // mechanics for us, we have no work to do in this case.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000559 if (RV.Val != N) {
Nate Begeman2300f552005-09-07 00:15:36 +0000560 DEBUG(std::cerr << "\nReplacing "; N->dump();
561 std::cerr << "\nWith: "; RV.Val->dump();
562 std::cerr << '\n');
Chris Lattner01a22022005-10-10 22:04:48 +0000563 std::vector<SDNode*> NowDead;
564 DAG.ReplaceAllUsesWith(N, std::vector<SDOperand>(1, RV), &NowDead);
Nate Begeman646d7e22005-09-02 21:18:40 +0000565
566 // Push the new node and any users onto the worklist
Nate Begeman83e75ec2005-09-06 04:43:02 +0000567 WorkList.push_back(RV.Val);
568 AddUsersToWorkList(RV.Val);
Nate Begeman646d7e22005-09-02 21:18:40 +0000569
570 // Nodes can end up on the worklist more than once. Make sure we do
571 // not process a node that has been replaced.
572 removeFromWorkList(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000573 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
574 removeFromWorkList(NowDead[i]);
Chris Lattner5c46f742005-10-05 06:11:08 +0000575
576 // Finally, since the node is now dead, remove it from the graph.
577 DAG.DeleteNode(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000578 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000579 }
580 }
Chris Lattner95038592005-10-05 06:35:28 +0000581
582 // If the root changed (e.g. it was a dead load, update the root).
583 DAG.setRoot(Dummy.getValue());
Nate Begeman1d4d4142005-09-01 00:19:25 +0000584}
585
Nate Begeman83e75ec2005-09-06 04:43:02 +0000586SDOperand DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000587 switch(N->getOpcode()) {
588 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +0000589 case ISD::TokenFactor: return visitTokenFactor(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000590 case ISD::ADD: return visitADD(N);
591 case ISD::SUB: return visitSUB(N);
592 case ISD::MUL: return visitMUL(N);
593 case ISD::SDIV: return visitSDIV(N);
594 case ISD::UDIV: return visitUDIV(N);
595 case ISD::SREM: return visitSREM(N);
596 case ISD::UREM: return visitUREM(N);
597 case ISD::MULHU: return visitMULHU(N);
598 case ISD::MULHS: return visitMULHS(N);
599 case ISD::AND: return visitAND(N);
600 case ISD::OR: return visitOR(N);
601 case ISD::XOR: return visitXOR(N);
602 case ISD::SHL: return visitSHL(N);
603 case ISD::SRA: return visitSRA(N);
604 case ISD::SRL: return visitSRL(N);
605 case ISD::CTLZ: return visitCTLZ(N);
606 case ISD::CTTZ: return visitCTTZ(N);
607 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000608 case ISD::SELECT: return visitSELECT(N);
609 case ISD::SELECT_CC: return visitSELECT_CC(N);
610 case ISD::SETCC: return visitSETCC(N);
Nate Begeman5054f162005-10-14 01:12:21 +0000611 case ISD::ADD_PARTS: return visitADD_PARTS(N);
612 case ISD::SUB_PARTS: return visitSUB_PARTS(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000613 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
614 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
615 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
616 case ISD::TRUNCATE: return visitTRUNCATE(N);
Chris Lattner94683772005-12-23 05:30:37 +0000617 case ISD::BIT_CONVERT: return visitBIT_CONVERT(N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000618 case ISD::FADD: return visitFADD(N);
619 case ISD::FSUB: return visitFSUB(N);
620 case ISD::FMUL: return visitFMUL(N);
621 case ISD::FDIV: return visitFDIV(N);
622 case ISD::FREM: return visitFREM(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000623 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
624 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
625 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
626 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
627 case ISD::FP_ROUND: return visitFP_ROUND(N);
628 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
629 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
630 case ISD::FNEG: return visitFNEG(N);
631 case ISD::FABS: return visitFABS(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000632 case ISD::BRCOND: return visitBRCOND(N);
633 case ISD::BRCONDTWOWAY: return visitBRCONDTWOWAY(N);
634 case ISD::BR_CC: return visitBR_CC(N);
635 case ISD::BRTWOWAY_CC: return visitBRTWOWAY_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000636 case ISD::LOAD: return visitLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000637 case ISD::STORE: return visitSTORE(N);
Jim Laskeyd6e8d412005-12-23 20:08:28 +0000638 case ISD::LOCATION: return visitLOCATION(N);
639 case ISD::DEBUG_LOC: return visitDEBUGLOC(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000640 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000641 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000642}
643
Nate Begeman83e75ec2005-09-06 04:43:02 +0000644SDOperand DAGCombiner::visitTokenFactor(SDNode *N) {
Nate Begemanded49632005-10-13 03:11:28 +0000645 std::vector<SDOperand> Ops;
646 bool Changed = false;
647
Nate Begeman1d4d4142005-09-01 00:19:25 +0000648 // If the token factor has two operands and one is the entry token, replace
649 // the token factor with the other operand.
650 if (N->getNumOperands() == 2) {
651 if (N->getOperand(0).getOpcode() == ISD::EntryToken)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000652 return N->getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000653 if (N->getOperand(1).getOpcode() == ISD::EntryToken)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000654 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000655 }
Chris Lattner24edbb72005-10-13 22:10:05 +0000656
Nate Begemanded49632005-10-13 03:11:28 +0000657 // fold (tokenfactor (tokenfactor)) -> tokenfactor
658 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
659 SDOperand Op = N->getOperand(i);
660 if (Op.getOpcode() == ISD::TokenFactor && Op.hasOneUse()) {
661 Changed = true;
662 for (unsigned j = 0, e = Op.getNumOperands(); j != e; ++j)
663 Ops.push_back(Op.getOperand(j));
664 } else {
665 Ops.push_back(Op);
666 }
667 }
668 if (Changed)
669 return DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000670 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000671}
672
Nate Begeman83e75ec2005-09-06 04:43:02 +0000673SDOperand DAGCombiner::visitADD(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000674 SDOperand N0 = N->getOperand(0);
675 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000676 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
677 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemanf89d78d2005-09-07 16:09:19 +0000678 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000679
680 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000681 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000682 return DAG.getNode(ISD::ADD, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000683 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000684 if (N0C && !N1C)
685 return DAG.getNode(ISD::ADD, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000686 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000687 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000688 return N0;
Nate Begeman223df222005-09-08 20:18:10 +0000689 // fold (add (add x, c1), c2) -> (add x, c1+c2)
690 if (N1C && N0.getOpcode() == ISD::ADD) {
691 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
692 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
693 if (N00C)
694 return DAG.getNode(ISD::ADD, VT, N0.getOperand(1),
695 DAG.getConstant(N1C->getValue()+N00C->getValue(), VT));
696 if (N01C)
697 return DAG.getNode(ISD::ADD, VT, N0.getOperand(0),
698 DAG.getConstant(N1C->getValue()+N01C->getValue(), VT));
699 }
Chris Lattner4aafb4f2006-01-12 20:22:43 +0000700
701 // fold ((c1-A)+c2) -> (c1+c2)-A
702 if (N1C && N0.getOpcode() == ISD::SUB)
703 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
704 return DAG.getNode(ISD::SUB, VT,
705 DAG.getConstant(N1C->getValue()+N0C->getValue(), VT),
706 N0.getOperand(1));
707
Nate Begeman1d4d4142005-09-01 00:19:25 +0000708 // fold ((0-A) + B) -> B-A
709 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
710 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000711 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000712 // fold (A + (0-B)) -> A-B
713 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
714 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000715 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +0000716 // fold (A+(B-A)) -> B
717 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000718 return N1.getOperand(0);
719 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000720}
721
Nate Begeman83e75ec2005-09-06 04:43:02 +0000722SDOperand DAGCombiner::visitSUB(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000723 SDOperand N0 = N->getOperand(0);
724 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000725 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
726 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000727 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000728
Chris Lattner854077d2005-10-17 01:07:11 +0000729 // fold (sub x, x) -> 0
730 if (N0 == N1)
731 return DAG.getConstant(0, N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000732 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000733 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000734 return DAG.getNode(ISD::SUB, VT, N0, N1);
Chris Lattner05b57432005-10-11 06:07:15 +0000735 // fold (sub x, c) -> (add x, -c)
736 if (N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000737 return DAG.getNode(ISD::ADD, VT, N0, DAG.getConstant(-N1C->getValue(), VT));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000738 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +0000739 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000740 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000741 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +0000742 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000743 return N0.getOperand(0);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000744 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000745}
746
Nate Begeman83e75ec2005-09-06 04:43:02 +0000747SDOperand DAGCombiner::visitMUL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000748 SDOperand N0 = N->getOperand(0);
749 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000750 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
751 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman223df222005-09-08 20:18:10 +0000752 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000753
754 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000755 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000756 return DAG.getNode(ISD::MUL, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000757 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000758 if (N0C && !N1C)
759 return DAG.getNode(ISD::MUL, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000760 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000761 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000762 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000763 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +0000764 if (N1C && N1C->isAllOnesValue())
Nate Begeman405e3ec2005-10-21 00:02:42 +0000765 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000766 // fold (mul x, (1 << c)) -> x << c
Nate Begeman646d7e22005-09-02 21:18:40 +0000767 if (N1C && isPowerOf2_64(N1C->getValue()))
Chris Lattner3e6099b2005-10-30 06:41:49 +0000768 return DAG.getNode(ISD::SHL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000769 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000770 TLI.getShiftAmountTy()));
Chris Lattner3e6099b2005-10-30 06:41:49 +0000771 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
772 if (N1C && isPowerOf2_64(-N1C->getSignExtended())) {
773 // FIXME: If the input is something that is easily negated (e.g. a
774 // single-use add), we should put the negate there.
775 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT),
776 DAG.getNode(ISD::SHL, VT, N0,
777 DAG.getConstant(Log2_64(-N1C->getSignExtended()),
778 TLI.getShiftAmountTy())));
779 }
780
781
Nate Begeman223df222005-09-08 20:18:10 +0000782 // fold (mul (mul x, c1), c2) -> (mul x, c1*c2)
783 if (N1C && N0.getOpcode() == ISD::MUL) {
784 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
785 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
786 if (N00C)
787 return DAG.getNode(ISD::MUL, VT, N0.getOperand(1),
788 DAG.getConstant(N1C->getValue()*N00C->getValue(), VT));
789 if (N01C)
790 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0),
791 DAG.getConstant(N1C->getValue()*N01C->getValue(), VT));
792 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000793 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000794}
795
Nate Begeman83e75ec2005-09-06 04:43:02 +0000796SDOperand DAGCombiner::visitSDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000797 SDOperand N0 = N->getOperand(0);
798 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000799 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
800 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000801 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000802
803 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000804 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000805 return DAG.getNode(ISD::SDIV, VT, N0, N1);
Nate Begeman405e3ec2005-10-21 00:02:42 +0000806 // fold (sdiv X, 1) -> X
807 if (N1C && N1C->getSignExtended() == 1LL)
808 return N0;
809 // fold (sdiv X, -1) -> 0-X
810 if (N1C && N1C->isAllOnesValue())
811 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +0000812 // If we know the sign bits of both operands are zero, strength reduce to a
813 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
814 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
815 if (MaskedValueIsZero(N1, SignBit, TLI) &&
816 MaskedValueIsZero(N0, SignBit, TLI))
817 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
Nate Begeman405e3ec2005-10-21 00:02:42 +0000818 // fold (sdiv X, pow2) -> (add (sra X, log(pow2)), (srl X, sizeof(X)-1))
819 if (N1C && N1C->getValue() && !TLI.isIntDivCheap() &&
820 (isPowerOf2_64(N1C->getSignExtended()) ||
821 isPowerOf2_64(-N1C->getSignExtended()))) {
822 // If dividing by powers of two is cheap, then don't perform the following
823 // fold.
824 if (TLI.isPow2DivCheap())
825 return SDOperand();
826 int64_t pow2 = N1C->getSignExtended();
827 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
828 SDOperand SRL = DAG.getNode(ISD::SRL, VT, N0,
829 DAG.getConstant(MVT::getSizeInBits(VT)-1,
830 TLI.getShiftAmountTy()));
831 WorkList.push_back(SRL.Val);
832 SDOperand SGN = DAG.getNode(ISD::ADD, VT, N0, SRL);
833 WorkList.push_back(SGN.Val);
834 SDOperand SRA = DAG.getNode(ISD::SRA, VT, SGN,
835 DAG.getConstant(Log2_64(abs2),
836 TLI.getShiftAmountTy()));
837 // If we're dividing by a positive value, we're done. Otherwise, we must
838 // negate the result.
839 if (pow2 > 0)
840 return SRA;
841 WorkList.push_back(SRA.Val);
842 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), SRA);
843 }
Nate Begeman69575232005-10-20 02:15:44 +0000844 // if integer divide is expensive and we satisfy the requirements, emit an
845 // alternate sequence.
Nate Begeman405e3ec2005-10-21 00:02:42 +0000846 if (N1C && (N1C->getSignExtended() < -1 || N1C->getSignExtended() > 1) &&
Chris Lattnere9936d12005-10-22 18:50:15 +0000847 !TLI.isIntDivCheap()) {
848 SDOperand Op = BuildSDIV(N);
849 if (Op.Val) return Op;
Nate Begeman69575232005-10-20 02:15:44 +0000850 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000851 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000852}
853
Nate Begeman83e75ec2005-09-06 04:43:02 +0000854SDOperand DAGCombiner::visitUDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000855 SDOperand N0 = N->getOperand(0);
856 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000857 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
858 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000859 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000860
861 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000862 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000863 return DAG.getNode(ISD::UDIV, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000864 // fold (udiv x, (1 << c)) -> x >>u c
Nate Begeman646d7e22005-09-02 21:18:40 +0000865 if (N1C && isPowerOf2_64(N1C->getValue()))
Nate Begeman1d4d4142005-09-01 00:19:25 +0000866 return DAG.getNode(ISD::SRL, N->getValueType(0), N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000867 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000868 TLI.getShiftAmountTy()));
Nate Begeman69575232005-10-20 02:15:44 +0000869 // fold (udiv x, c) -> alternate
Chris Lattnere9936d12005-10-22 18:50:15 +0000870 if (N1C && N1C->getValue() && !TLI.isIntDivCheap()) {
871 SDOperand Op = BuildUDIV(N);
872 if (Op.Val) return Op;
873 }
874
Nate Begeman83e75ec2005-09-06 04:43:02 +0000875 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000876}
877
Nate Begeman83e75ec2005-09-06 04:43:02 +0000878SDOperand DAGCombiner::visitSREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000879 SDOperand N0 = N->getOperand(0);
880 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000881 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
882 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +0000883 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000884
885 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000886 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000887 return DAG.getNode(ISD::SREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +0000888 // If we know the sign bits of both operands are zero, strength reduce to a
889 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
890 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
891 if (MaskedValueIsZero(N1, SignBit, TLI) &&
892 MaskedValueIsZero(N0, SignBit, TLI))
Nate Begemana148d982006-01-18 22:35:16 +0000893 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000894 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000895}
896
Nate Begeman83e75ec2005-09-06 04:43:02 +0000897SDOperand DAGCombiner::visitUREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000898 SDOperand N0 = N->getOperand(0);
899 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000900 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
901 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +0000902 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000903
904 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000905 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000906 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +0000907 // fold (urem x, pow2) -> (and x, pow2-1)
908 if (N1C && !N1C->isNullValue() && isPowerOf2_64(N1C->getValue()))
Nate Begemana148d982006-01-18 22:35:16 +0000909 return DAG.getNode(ISD::AND, VT, N0, DAG.getConstant(N1C->getValue()-1,VT));
Nate Begeman83e75ec2005-09-06 04:43:02 +0000910 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000911}
912
Nate Begeman83e75ec2005-09-06 04:43:02 +0000913SDOperand DAGCombiner::visitMULHS(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000914 SDOperand N0 = N->getOperand(0);
915 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000916 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000917
918 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000919 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000920 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000921 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Nate Begeman646d7e22005-09-02 21:18:40 +0000922 if (N1C && N1C->getValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +0000923 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
924 DAG.getConstant(MVT::getSizeInBits(N0.getValueType())-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +0000925 TLI.getShiftAmountTy()));
926 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000927}
928
Nate Begeman83e75ec2005-09-06 04:43:02 +0000929SDOperand DAGCombiner::visitMULHU(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000930 SDOperand N0 = N->getOperand(0);
931 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000932 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000933
934 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000935 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000936 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000937 // fold (mulhu x, 1) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000938 if (N1C && N1C->getValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000939 return DAG.getConstant(0, N0.getValueType());
940 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000941}
942
Nate Begeman83e75ec2005-09-06 04:43:02 +0000943SDOperand DAGCombiner::visitAND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000944 SDOperand N0 = N->getOperand(0);
945 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000946 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +0000947 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
948 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000949 MVT::ValueType VT = N1.getValueType();
Nate Begeman83e75ec2005-09-06 04:43:02 +0000950 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000951
952 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000953 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000954 return DAG.getNode(ISD::AND, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000955 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000956 if (N0C && !N1C)
957 return DAG.getNode(ISD::AND, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000958 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000959 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000960 return N0;
961 // if (and x, c) is known to be zero, return 0
962 if (N1C && MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits),TLI))
963 return DAG.getConstant(0, VT);
964 // fold (and x, c) -> x iff (x & ~c) == 0
965 if (N1C && MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits)),
966 TLI))
967 return N0;
Nate Begeman223df222005-09-08 20:18:10 +0000968 // fold (and (and x, c1), c2) -> (and x, c1^c2)
969 if (N1C && N0.getOpcode() == ISD::AND) {
970 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
971 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
972 if (N00C)
973 return DAG.getNode(ISD::AND, VT, N0.getOperand(1),
974 DAG.getConstant(N1C->getValue()&N00C->getValue(), VT));
975 if (N01C)
976 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
977 DAG.getConstant(N1C->getValue()&N01C->getValue(), VT));
978 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000979 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
Nate Begeman5dc7e862005-11-02 18:42:59 +0000980 if (N1C && N0.getOpcode() == ISD::SIGN_EXTEND_INREG) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000981 unsigned ExtendBits =
Jeff Cohen06d9b4a2005-11-12 00:59:01 +0000982 MVT::getSizeInBits(cast<VTSDNode>(N0.getOperand(1))->getVT());
983 if (ExtendBits == 64 || ((N1C->getValue() & (~0ULL << ExtendBits)) == 0))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000984 return DAG.getNode(ISD::AND, VT, N0.getOperand(0), N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000985 }
986 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
Nate Begeman5dc7e862005-11-02 18:42:59 +0000987 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +0000988 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Nate Begeman646d7e22005-09-02 21:18:40 +0000989 if ((ORI->getValue() & N1C->getValue()) == N1C->getValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000990 return N1;
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000991 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
992 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
993 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
994 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
995
996 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
997 MVT::isInteger(LL.getValueType())) {
998 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
999 if (cast<ConstantSDNode>(LR)->getValue() == 0 && Op1 == ISD::SETEQ) {
1000 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
1001 WorkList.push_back(ORNode.Val);
1002 return DAG.getSetCC(VT, ORNode, LR, Op1);
1003 }
1004 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
1005 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
1006 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
1007 WorkList.push_back(ANDNode.Val);
1008 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1009 }
1010 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
1011 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
1012 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
1013 WorkList.push_back(ORNode.Val);
1014 return DAG.getSetCC(VT, ORNode, LR, Op1);
1015 }
1016 }
1017 // canonicalize equivalent to ll == rl
1018 if (LL == RR && LR == RL) {
1019 Op1 = ISD::getSetCCSwappedOperands(Op1);
1020 std::swap(RL, RR);
1021 }
1022 if (LL == RL && LR == RR) {
1023 bool isInteger = MVT::isInteger(LL.getValueType());
1024 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
1025 if (Result != ISD::SETCC_INVALID)
1026 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1027 }
1028 }
1029 // fold (and (zext x), (zext y)) -> (zext (and x, y))
1030 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
1031 N1.getOpcode() == ISD::ZERO_EXTEND &&
1032 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1033 SDOperand ANDNode = DAG.getNode(ISD::AND, N0.getOperand(0).getValueType(),
1034 N0.getOperand(0), N1.getOperand(0));
1035 WorkList.push_back(ANDNode.Val);
1036 return DAG.getNode(ISD::ZERO_EXTEND, VT, ANDNode);
1037 }
Nate Begeman61af66e2006-01-28 01:06:30 +00001038 // fold (and (shl/srl/sra x), (shl/srl/sra y)) -> (shl/srl/sra (and x, y))
Nate Begeman452d7be2005-09-16 00:54:12 +00001039 if (((N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SHL) ||
Nate Begeman61af66e2006-01-28 01:06:30 +00001040 (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SRL) ||
1041 (N0.getOpcode() == ISD::SRA && N1.getOpcode() == ISD::SRA)) &&
Nate Begeman452d7be2005-09-16 00:54:12 +00001042 N0.getOperand(1) == N1.getOperand(1)) {
1043 SDOperand ANDNode = DAG.getNode(ISD::AND, N0.getOperand(0).getValueType(),
1044 N0.getOperand(0), N1.getOperand(0));
1045 WorkList.push_back(ANDNode.Val);
1046 return DAG.getNode(N0.getOpcode(), VT, ANDNode, N0.getOperand(1));
1047 }
Chris Lattner85d63bb2005-10-15 22:18:08 +00001048 // fold (and (sra)) -> (and (srl)) when possible.
Nate Begeman5dc7e862005-11-02 18:42:59 +00001049 if (N0.getOpcode() == ISD::SRA && N0.Val->hasOneUse()) {
Chris Lattner85d63bb2005-10-15 22:18:08 +00001050 if (ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
1051 // If the RHS of the AND has zeros where the sign bits of the SRA will
1052 // land, turn the SRA into an SRL.
Chris Lattner750dbd52005-10-15 22:35:40 +00001053 if (MaskedValueIsZero(N1, (~0ULL << (OpSizeInBits-N01C->getValue())) &
Chris Lattner85d63bb2005-10-15 22:18:08 +00001054 (~0ULL>>(64-OpSizeInBits)), TLI)) {
1055 WorkList.push_back(N);
1056 CombineTo(N0.Val, DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
1057 N0.getOperand(1)));
1058 return SDOperand();
1059 }
1060 }
Nate Begeman5dc7e862005-11-02 18:42:59 +00001061 }
Nate Begemanded49632005-10-13 03:11:28 +00001062 // fold (zext_inreg (extload x)) -> (zextload x)
Nate Begeman5054f162005-10-14 01:12:21 +00001063 if (N0.getOpcode() == ISD::EXTLOAD) {
Nate Begemanded49632005-10-13 03:11:28 +00001064 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001065 // If we zero all the possible extended bits, then we can turn this into
1066 // a zextload if we are running before legalize or the operation is legal.
Nate Begeman5054f162005-10-14 01:12:21 +00001067 if (MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT), TLI) &&
Chris Lattner67a44cd2005-10-13 18:16:34 +00001068 (!AfterLegalize || TLI.isOperationLegal(ISD::ZEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001069 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1070 N0.getOperand(1), N0.getOperand(2),
1071 EVT);
Nate Begemanded49632005-10-13 03:11:28 +00001072 WorkList.push_back(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001073 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Nate Begemanded49632005-10-13 03:11:28 +00001074 return SDOperand();
1075 }
1076 }
1077 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Chris Lattner40c62d52005-10-18 06:04:22 +00001078 if (N0.getOpcode() == ISD::SEXTLOAD && N0.hasOneUse()) {
Nate Begemanded49632005-10-13 03:11:28 +00001079 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001080 // If we zero all the possible extended bits, then we can turn this into
1081 // a zextload if we are running before legalize or the operation is legal.
Nate Begeman5054f162005-10-14 01:12:21 +00001082 if (MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT), TLI) &&
Nate Begemanbfd65a02005-10-13 18:34:58 +00001083 (!AfterLegalize || TLI.isOperationLegal(ISD::ZEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001084 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1085 N0.getOperand(1), N0.getOperand(2),
1086 EVT);
Nate Begemanded49632005-10-13 03:11:28 +00001087 WorkList.push_back(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001088 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Nate Begemanded49632005-10-13 03:11:28 +00001089 return SDOperand();
1090 }
1091 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001092 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001093}
1094
Nate Begeman83e75ec2005-09-06 04:43:02 +00001095SDOperand DAGCombiner::visitOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001096 SDOperand N0 = N->getOperand(0);
1097 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001098 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001099 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1100 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001101 MVT::ValueType VT = N1.getValueType();
1102 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001103
1104 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001105 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001106 return DAG.getNode(ISD::OR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001107 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001108 if (N0C && !N1C)
1109 return DAG.getNode(ISD::OR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001110 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001111 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001112 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001113 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001114 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001115 return N1;
1116 // fold (or x, c) -> c iff (x & ~c) == 0
1117 if (N1C && MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits)),
1118 TLI))
1119 return N1;
Nate Begeman223df222005-09-08 20:18:10 +00001120 // fold (or (or x, c1), c2) -> (or x, c1|c2)
1121 if (N1C && N0.getOpcode() == ISD::OR) {
1122 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
1123 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
1124 if (N00C)
1125 return DAG.getNode(ISD::OR, VT, N0.getOperand(1),
1126 DAG.getConstant(N1C->getValue()|N00C->getValue(), VT));
1127 if (N01C)
1128 return DAG.getNode(ISD::OR, VT, N0.getOperand(0),
1129 DAG.getConstant(N1C->getValue()|N01C->getValue(), VT));
Chris Lattner731d3482005-10-27 05:06:38 +00001130 } else if (N1C && N0.getOpcode() == ISD::AND && N0.Val->hasOneUse() &&
1131 isa<ConstantSDNode>(N0.getOperand(1))) {
1132 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
1133 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
1134 return DAG.getNode(ISD::AND, VT, DAG.getNode(ISD::OR, VT, N0.getOperand(0),
1135 N1),
1136 DAG.getConstant(N1C->getValue() | C1->getValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00001137 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001138 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
1139 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1140 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1141 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1142
1143 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1144 MVT::isInteger(LL.getValueType())) {
1145 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
1146 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
1147 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
1148 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
1149 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
1150 WorkList.push_back(ORNode.Val);
1151 return DAG.getSetCC(VT, ORNode, LR, Op1);
1152 }
1153 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
1154 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
1155 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
1156 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
1157 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
1158 WorkList.push_back(ANDNode.Val);
1159 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1160 }
1161 }
1162 // canonicalize equivalent to ll == rl
1163 if (LL == RR && LR == RL) {
1164 Op1 = ISD::getSetCCSwappedOperands(Op1);
1165 std::swap(RL, RR);
1166 }
1167 if (LL == RL && LR == RR) {
1168 bool isInteger = MVT::isInteger(LL.getValueType());
1169 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
1170 if (Result != ISD::SETCC_INVALID)
1171 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1172 }
1173 }
1174 // fold (or (zext x), (zext y)) -> (zext (or x, y))
1175 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
1176 N1.getOpcode() == ISD::ZERO_EXTEND &&
1177 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1178 SDOperand ORNode = DAG.getNode(ISD::OR, N0.getOperand(0).getValueType(),
1179 N0.getOperand(0), N1.getOperand(0));
1180 WorkList.push_back(ORNode.Val);
1181 return DAG.getNode(ISD::ZERO_EXTEND, VT, ORNode);
1182 }
Nate Begeman35ef9132006-01-11 21:21:00 +00001183 // canonicalize shl to left side in a shl/srl pair, to match rotate
1184 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
1185 std::swap(N0, N1);
1186 // check for rotl, rotr
1187 if (N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SRL &&
1188 N0.getOperand(0) == N1.getOperand(0) &&
Chris Lattneraf551bc2006-01-12 18:57:33 +00001189 TLI.isOperationLegal(ISD::ROTL, VT) && TLI.isTypeLegal(VT)) {
Nate Begeman35ef9132006-01-11 21:21:00 +00001190 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
1191 if (N0.getOperand(1).getOpcode() == ISD::Constant &&
1192 N1.getOperand(1).getOpcode() == ISD::Constant) {
1193 uint64_t c1val = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1194 uint64_t c2val = cast<ConstantSDNode>(N1.getOperand(1))->getValue();
1195 if ((c1val + c2val) == OpSizeInBits)
1196 return DAG.getNode(ISD::ROTL, VT, N0.getOperand(0), N0.getOperand(1));
1197 }
1198 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
1199 if (N1.getOperand(1).getOpcode() == ISD::SUB &&
1200 N0.getOperand(1) == N1.getOperand(1).getOperand(1))
1201 if (ConstantSDNode *SUBC =
1202 dyn_cast<ConstantSDNode>(N1.getOperand(1).getOperand(0)))
1203 if (SUBC->getValue() == OpSizeInBits)
1204 return DAG.getNode(ISD::ROTL, VT, N0.getOperand(0), N0.getOperand(1));
1205 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
1206 if (N0.getOperand(1).getOpcode() == ISD::SUB &&
1207 N1.getOperand(1) == N0.getOperand(1).getOperand(1))
1208 if (ConstantSDNode *SUBC =
1209 dyn_cast<ConstantSDNode>(N0.getOperand(1).getOperand(0)))
1210 if (SUBC->getValue() == OpSizeInBits) {
Chris Lattneraf551bc2006-01-12 18:57:33 +00001211 if (TLI.isOperationLegal(ISD::ROTR, VT) && TLI.isTypeLegal(VT))
Nate Begeman35ef9132006-01-11 21:21:00 +00001212 return DAG.getNode(ISD::ROTR, VT, N0.getOperand(0),
1213 N1.getOperand(1));
1214 else
1215 return DAG.getNode(ISD::ROTL, VT, N0.getOperand(0),
1216 N0.getOperand(1));
1217 }
1218 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001219 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001220}
1221
Nate Begeman83e75ec2005-09-06 04:43:02 +00001222SDOperand DAGCombiner::visitXOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001223 SDOperand N0 = N->getOperand(0);
1224 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001225 SDOperand LHS, RHS, CC;
1226 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1227 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001228 MVT::ValueType VT = N0.getValueType();
1229
1230 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001231 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001232 return DAG.getNode(ISD::XOR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001233 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001234 if (N0C && !N1C)
1235 return DAG.getNode(ISD::XOR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001236 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001237 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001238 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001239 // fold !(x cc y) -> (x !cc y)
Nate Begeman646d7e22005-09-02 21:18:40 +00001240 if (N1C && N1C->getValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
1241 bool isInt = MVT::isInteger(LHS.getValueType());
1242 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
1243 isInt);
1244 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001245 return DAG.getSetCC(VT, LHS, RHS, NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00001246 if (N0.getOpcode() == ISD::SELECT_CC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001247 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00001248 assert(0 && "Unhandled SetCC Equivalent!");
1249 abort();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001250 }
Nate Begeman99801192005-09-07 23:25:52 +00001251 // fold !(x or y) -> (!x and !y) iff x or y are setcc
1252 if (N1C && N1C->getValue() == 1 &&
1253 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001254 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00001255 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
1256 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001257 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1258 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Nate Begeman99801192005-09-07 23:25:52 +00001259 WorkList.push_back(LHS.Val); WorkList.push_back(RHS.Val);
1260 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001261 }
1262 }
Nate Begeman99801192005-09-07 23:25:52 +00001263 // fold !(x or y) -> (!x and !y) iff x or y are constants
1264 if (N1C && N1C->isAllOnesValue() &&
1265 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001266 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00001267 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
1268 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001269 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1270 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Nate Begeman99801192005-09-07 23:25:52 +00001271 WorkList.push_back(LHS.Val); WorkList.push_back(RHS.Val);
1272 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001273 }
1274 }
Nate Begeman223df222005-09-08 20:18:10 +00001275 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
1276 if (N1C && N0.getOpcode() == ISD::XOR) {
1277 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
1278 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
1279 if (N00C)
1280 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
1281 DAG.getConstant(N1C->getValue()^N00C->getValue(), VT));
1282 if (N01C)
1283 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
1284 DAG.getConstant(N1C->getValue()^N01C->getValue(), VT));
1285 }
1286 // fold (xor x, x) -> 0
1287 if (N0 == N1)
1288 return DAG.getConstant(0, VT);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001289 // fold (xor (zext x), (zext y)) -> (zext (xor x, y))
1290 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
1291 N1.getOpcode() == ISD::ZERO_EXTEND &&
1292 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1293 SDOperand XORNode = DAG.getNode(ISD::XOR, N0.getOperand(0).getValueType(),
1294 N0.getOperand(0), N1.getOperand(0));
1295 WorkList.push_back(XORNode.Val);
1296 return DAG.getNode(ISD::ZERO_EXTEND, VT, XORNode);
1297 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001298 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001299}
1300
Nate Begeman83e75ec2005-09-06 04:43:02 +00001301SDOperand DAGCombiner::visitSHL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001302 SDOperand N0 = N->getOperand(0);
1303 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001304 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1305 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001306 MVT::ValueType VT = N0.getValueType();
1307 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1308
1309 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001310 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001311 return DAG.getNode(ISD::SHL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001312 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001313 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001314 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001315 // fold (shl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001316 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001317 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001318 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001319 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001320 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001321 // if (shl x, c) is known to be zero, return 0
Nate Begeman83e75ec2005-09-06 04:43:02 +00001322 if (N1C && MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits),TLI))
1323 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001324 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001325 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001326 N0.getOperand(1).getOpcode() == ISD::Constant) {
1327 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001328 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001329 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001330 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001331 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001332 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001333 }
1334 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
1335 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001336 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001337 N0.getOperand(1).getOpcode() == ISD::Constant) {
1338 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001339 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001340 SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
1341 DAG.getConstant(~0ULL << c1, VT));
1342 if (c2 > c1)
1343 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001344 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001345 else
Nate Begeman83e75ec2005-09-06 04:43:02 +00001346 return DAG.getNode(ISD::SRL, VT, Mask,
1347 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001348 }
1349 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001350 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +00001351 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001352 DAG.getConstant(~0ULL << N1C->getValue(), VT));
1353 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001354}
1355
Nate Begeman83e75ec2005-09-06 04:43:02 +00001356SDOperand DAGCombiner::visitSRA(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001357 SDOperand N0 = N->getOperand(0);
1358 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001359 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1360 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001361 MVT::ValueType VT = N0.getValueType();
1362 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1363
1364 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001365 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001366 return DAG.getNode(ISD::SRA, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001367 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001368 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001369 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001370 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001371 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001372 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001373 // fold (sra x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001374 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001375 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001376 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001377 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001378 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001379 // If the sign bit is known to be zero, switch this to a SRL.
Nate Begeman3df4d522005-10-12 20:40:40 +00001380 if (MaskedValueIsZero(N0, (1ULL << (OpSizeInBits-1)), TLI))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001381 return DAG.getNode(ISD::SRL, VT, N0, N1);
1382 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001383}
1384
Nate Begeman83e75ec2005-09-06 04:43:02 +00001385SDOperand DAGCombiner::visitSRL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001386 SDOperand N0 = N->getOperand(0);
1387 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001388 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1389 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001390 MVT::ValueType VT = N0.getValueType();
1391 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1392
1393 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001394 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001395 return DAG.getNode(ISD::SRL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001396 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001397 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001398 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001399 // fold (srl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001400 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001401 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001402 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001403 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001404 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001405 // if (srl x, c) is known to be zero, return 0
Nate Begeman83e75ec2005-09-06 04:43:02 +00001406 if (N1C && MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits),TLI))
1407 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001408 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001409 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001410 N0.getOperand(1).getOpcode() == ISD::Constant) {
1411 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001412 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001413 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001414 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001415 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001416 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001417 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001418 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001419}
1420
Nate Begeman83e75ec2005-09-06 04:43:02 +00001421SDOperand DAGCombiner::visitCTLZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001422 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001423 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00001424 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001425
1426 // fold (ctlz c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001427 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001428 return DAG.getNode(ISD::CTLZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001429 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001430}
1431
Nate Begeman83e75ec2005-09-06 04:43:02 +00001432SDOperand DAGCombiner::visitCTTZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001433 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001434 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00001435 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001436
1437 // fold (cttz c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001438 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001439 return DAG.getNode(ISD::CTTZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001440 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001441}
1442
Nate Begeman83e75ec2005-09-06 04:43:02 +00001443SDOperand DAGCombiner::visitCTPOP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001444 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001445 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00001446 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001447
1448 // fold (ctpop c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001449 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001450 return DAG.getNode(ISD::CTPOP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001451 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001452}
1453
Nate Begeman452d7be2005-09-16 00:54:12 +00001454SDOperand DAGCombiner::visitSELECT(SDNode *N) {
1455 SDOperand N0 = N->getOperand(0);
1456 SDOperand N1 = N->getOperand(1);
1457 SDOperand N2 = N->getOperand(2);
1458 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1459 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1460 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1461 MVT::ValueType VT = N->getValueType(0);
Nate Begeman44728a72005-09-19 22:34:01 +00001462
Nate Begeman452d7be2005-09-16 00:54:12 +00001463 // fold select C, X, X -> X
1464 if (N1 == N2)
1465 return N1;
1466 // fold select true, X, Y -> X
1467 if (N0C && !N0C->isNullValue())
1468 return N1;
1469 // fold select false, X, Y -> Y
1470 if (N0C && N0C->isNullValue())
1471 return N2;
1472 // fold select C, 1, X -> C | X
Nate Begeman44728a72005-09-19 22:34:01 +00001473 if (MVT::i1 == VT && N1C && N1C->getValue() == 1)
Nate Begeman452d7be2005-09-16 00:54:12 +00001474 return DAG.getNode(ISD::OR, VT, N0, N2);
1475 // fold select C, 0, X -> ~C & X
1476 // FIXME: this should check for C type == X type, not i1?
1477 if (MVT::i1 == VT && N1C && N1C->isNullValue()) {
1478 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
1479 WorkList.push_back(XORNode.Val);
1480 return DAG.getNode(ISD::AND, VT, XORNode, N2);
1481 }
1482 // fold select C, X, 1 -> ~C | X
Nate Begeman44728a72005-09-19 22:34:01 +00001483 if (MVT::i1 == VT && N2C && N2C->getValue() == 1) {
Nate Begeman452d7be2005-09-16 00:54:12 +00001484 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
1485 WorkList.push_back(XORNode.Val);
1486 return DAG.getNode(ISD::OR, VT, XORNode, N1);
1487 }
1488 // fold select C, X, 0 -> C & X
1489 // FIXME: this should check for C type == X type, not i1?
1490 if (MVT::i1 == VT && N2C && N2C->isNullValue())
1491 return DAG.getNode(ISD::AND, VT, N0, N1);
1492 // fold X ? X : Y --> X ? 1 : Y --> X | Y
1493 if (MVT::i1 == VT && N0 == N1)
1494 return DAG.getNode(ISD::OR, VT, N0, N2);
1495 // fold X ? Y : X --> X ? Y : 0 --> X & Y
1496 if (MVT::i1 == VT && N0 == N2)
1497 return DAG.getNode(ISD::AND, VT, N0, N1);
Chris Lattner40c62d52005-10-18 06:04:22 +00001498
1499 // If we can fold this based on the true/false value, do so.
1500 if (SimplifySelectOps(N, N1, N2))
1501 return SDOperand();
1502
Nate Begeman44728a72005-09-19 22:34:01 +00001503 // fold selects based on a setcc into other things, such as min/max/abs
1504 if (N0.getOpcode() == ISD::SETCC)
1505 return SimplifySelect(N0, N1, N2);
Nate Begeman452d7be2005-09-16 00:54:12 +00001506 return SDOperand();
1507}
1508
1509SDOperand DAGCombiner::visitSELECT_CC(SDNode *N) {
Nate Begeman44728a72005-09-19 22:34:01 +00001510 SDOperand N0 = N->getOperand(0);
1511 SDOperand N1 = N->getOperand(1);
1512 SDOperand N2 = N->getOperand(2);
1513 SDOperand N3 = N->getOperand(3);
1514 SDOperand N4 = N->getOperand(4);
1515 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1516 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1517 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1518 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
1519
1520 // Determine if the condition we're dealing with is constant
Nate Begemane17daeb2005-10-05 21:43:42 +00001521 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
Chris Lattner91559022005-10-05 04:45:43 +00001522 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
1523
Nate Begeman44728a72005-09-19 22:34:01 +00001524 // fold select_cc lhs, rhs, x, x, cc -> x
1525 if (N2 == N3)
1526 return N2;
Chris Lattner40c62d52005-10-18 06:04:22 +00001527
1528 // If we can fold this based on the true/false value, do so.
1529 if (SimplifySelectOps(N, N2, N3))
1530 return SDOperand();
1531
Nate Begeman44728a72005-09-19 22:34:01 +00001532 // fold select_cc into other things, such as min/max/abs
1533 return SimplifySelectCC(N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00001534}
1535
1536SDOperand DAGCombiner::visitSETCC(SDNode *N) {
1537 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
1538 cast<CondCodeSDNode>(N->getOperand(2))->get());
1539}
1540
Nate Begeman5054f162005-10-14 01:12:21 +00001541SDOperand DAGCombiner::visitADD_PARTS(SDNode *N) {
1542 SDOperand LHSLo = N->getOperand(0);
1543 SDOperand RHSLo = N->getOperand(2);
1544 MVT::ValueType VT = LHSLo.getValueType();
1545
1546 // fold (a_Hi, 0) + (b_Hi, b_Lo) -> (b_Hi + a_Hi, b_Lo)
1547 if (MaskedValueIsZero(LHSLo, (1ULL << MVT::getSizeInBits(VT))-1, TLI)) {
1548 SDOperand Hi = DAG.getNode(ISD::ADD, VT, N->getOperand(1),
1549 N->getOperand(3));
1550 WorkList.push_back(Hi.Val);
1551 CombineTo(N, RHSLo, Hi);
1552 return SDOperand();
1553 }
1554 // fold (a_Hi, a_Lo) + (b_Hi, 0) -> (a_Hi + b_Hi, a_Lo)
1555 if (MaskedValueIsZero(RHSLo, (1ULL << MVT::getSizeInBits(VT))-1, TLI)) {
1556 SDOperand Hi = DAG.getNode(ISD::ADD, VT, N->getOperand(1),
1557 N->getOperand(3));
1558 WorkList.push_back(Hi.Val);
1559 CombineTo(N, LHSLo, Hi);
1560 return SDOperand();
1561 }
1562 return SDOperand();
1563}
1564
1565SDOperand DAGCombiner::visitSUB_PARTS(SDNode *N) {
1566 SDOperand LHSLo = N->getOperand(0);
1567 SDOperand RHSLo = N->getOperand(2);
1568 MVT::ValueType VT = LHSLo.getValueType();
1569
1570 // fold (a_Hi, a_Lo) - (b_Hi, 0) -> (a_Hi - b_Hi, a_Lo)
1571 if (MaskedValueIsZero(RHSLo, (1ULL << MVT::getSizeInBits(VT))-1, TLI)) {
1572 SDOperand Hi = DAG.getNode(ISD::SUB, VT, N->getOperand(1),
1573 N->getOperand(3));
1574 WorkList.push_back(Hi.Val);
1575 CombineTo(N, LHSLo, Hi);
1576 return SDOperand();
1577 }
1578 return SDOperand();
1579}
1580
Nate Begeman83e75ec2005-09-06 04:43:02 +00001581SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001582 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001583 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001584 MVT::ValueType VT = N->getValueType(0);
1585
Nate Begeman1d4d4142005-09-01 00:19:25 +00001586 // fold (sext c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001587 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001588 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001589 // fold (sext (sext x)) -> (sext x)
1590 if (N0.getOpcode() == ISD::SIGN_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001591 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
Chris Lattnerb14ab8a2005-12-07 07:11:03 +00001592 // fold (sext (truncate x)) -> (sextinreg x) iff x size == sext size.
Chris Lattnercc2210b2005-12-07 18:02:05 +00001593 if (N0.getOpcode() == ISD::TRUNCATE && N0.getOperand(0).getValueType() == VT&&
1594 (!AfterLegalize ||
1595 TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, N0.getValueType())))
Chris Lattnerb14ab8a2005-12-07 07:11:03 +00001596 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
1597 DAG.getValueType(N0.getValueType()));
Evan Cheng110dec22005-12-14 02:19:23 +00001598 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Chris Lattnerd0f6d182005-12-15 19:02:38 +00001599 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() &&
1600 (!AfterLegalize||TLI.isOperationLegal(ISD::SEXTLOAD, N0.getValueType()))){
Nate Begeman3df4d522005-10-12 20:40:40 +00001601 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1602 N0.getOperand(1), N0.getOperand(2),
1603 N0.getValueType());
Chris Lattnerd4771842005-12-14 19:25:30 +00001604 CombineTo(N, ExtLoad);
Chris Lattnerf9884052005-10-13 21:52:31 +00001605 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1606 ExtLoad.getValue(1));
Nate Begeman765784a2005-10-12 23:18:53 +00001607 return SDOperand();
Nate Begeman3df4d522005-10-12 20:40:40 +00001608 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001609
1610 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
1611 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
1612 if ((N0.getOpcode() == ISD::SEXTLOAD || N0.getOpcode() == ISD::EXTLOAD) &&
1613 N0.hasOneUse()) {
1614 SDOperand ExtLoad = DAG.getNode(ISD::SEXTLOAD, VT, N0.getOperand(0),
1615 N0.getOperand(1), N0.getOperand(2),
1616 N0.getOperand(3));
Chris Lattnerd4771842005-12-14 19:25:30 +00001617 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001618 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1619 ExtLoad.getValue(1));
1620 return SDOperand();
1621 }
1622
Nate Begeman83e75ec2005-09-06 04:43:02 +00001623 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001624}
1625
Nate Begeman83e75ec2005-09-06 04:43:02 +00001626SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001627 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001628 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001629 MVT::ValueType VT = N->getValueType(0);
1630
Nate Begeman1d4d4142005-09-01 00:19:25 +00001631 // fold (zext c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001632 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001633 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001634 // fold (zext (zext x)) -> (zext x)
1635 if (N0.getOpcode() == ISD::ZERO_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001636 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
Evan Cheng110dec22005-12-14 02:19:23 +00001637 // fold (zext (truncate x)) -> (zextinreg x) iff x size == zext size.
1638 if (N0.getOpcode() == ISD::TRUNCATE && N0.getOperand(0).getValueType() == VT&&
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001639 (!AfterLegalize || TLI.isOperationLegal(ISD::AND, N0.getValueType())))
Chris Lattner00cb95c2005-12-14 07:58:38 +00001640 return DAG.getZeroExtendInReg(N0.getOperand(0), N0.getValueType());
Evan Cheng110dec22005-12-14 02:19:23 +00001641 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Chris Lattnerd0f6d182005-12-15 19:02:38 +00001642 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() &&
1643 (!AfterLegalize||TLI.isOperationLegal(ISD::ZEXTLOAD, N0.getValueType()))){
Evan Cheng110dec22005-12-14 02:19:23 +00001644 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1645 N0.getOperand(1), N0.getOperand(2),
1646 N0.getValueType());
Chris Lattnerd4771842005-12-14 19:25:30 +00001647 CombineTo(N, ExtLoad);
Evan Cheng110dec22005-12-14 02:19:23 +00001648 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1649 ExtLoad.getValue(1));
1650 return SDOperand();
1651 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001652
1653 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
1654 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
1655 if ((N0.getOpcode() == ISD::ZEXTLOAD || N0.getOpcode() == ISD::EXTLOAD) &&
1656 N0.hasOneUse()) {
1657 SDOperand ExtLoad = DAG.getNode(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1658 N0.getOperand(1), N0.getOperand(2),
1659 N0.getOperand(3));
Chris Lattnerd4771842005-12-14 19:25:30 +00001660 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001661 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1662 ExtLoad.getValue(1));
1663 return SDOperand();
1664 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001665 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001666}
1667
Nate Begeman83e75ec2005-09-06 04:43:02 +00001668SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001669 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001670 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001671 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001672 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001673 MVT::ValueType EVT = cast<VTSDNode>(N1)->getVT();
Nate Begeman07ed4172005-10-10 21:26:48 +00001674 unsigned EVTBits = MVT::getSizeInBits(EVT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001675
Nate Begeman1d4d4142005-09-01 00:19:25 +00001676 // fold (sext_in_reg c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001677 if (N0C) {
1678 SDOperand Truncate = DAG.getConstant(N0C->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001679 return DAG.getNode(ISD::SIGN_EXTEND, VT, Truncate);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001680 }
Nate Begeman646d7e22005-09-02 21:18:40 +00001681 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt1
Nate Begeman1d4d4142005-09-01 00:19:25 +00001682 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Nate Begeman216def82005-10-14 01:29:07 +00001683 cast<VTSDNode>(N0.getOperand(1))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001684 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001685 }
Nate Begeman646d7e22005-09-02 21:18:40 +00001686 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
1687 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1688 EVT < cast<VTSDNode>(N0.getOperand(1))->getVT()) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001689 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001690 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00001691 // fold (sext_in_reg (assert_sext x)) -> (assert_sext x)
1692 if (N0.getOpcode() == ISD::AssertSext &&
1693 cast<VTSDNode>(N0.getOperand(1))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001694 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001695 }
1696 // fold (sext_in_reg (sextload x)) -> (sextload x)
1697 if (N0.getOpcode() == ISD::SEXTLOAD &&
1698 cast<VTSDNode>(N0.getOperand(3))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001699 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001700 }
Nate Begeman4ebd8052005-09-01 23:24:04 +00001701 // fold (sext_in_reg (setcc x)) -> setcc x iff (setcc x) == 0 or -1
Nate Begeman1d4d4142005-09-01 00:19:25 +00001702 if (N0.getOpcode() == ISD::SETCC &&
1703 TLI.getSetCCResultContents() ==
1704 TargetLowering::ZeroOrNegativeOneSetCCResult)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001705 return N0;
Nate Begeman07ed4172005-10-10 21:26:48 +00001706 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is zero
1707 if (MaskedValueIsZero(N0, 1ULL << (EVTBits-1), TLI))
1708 return DAG.getNode(ISD::AND, N0.getValueType(), N0,
1709 DAG.getConstant(~0ULL >> (64-EVTBits), VT));
1710 // fold (sext_in_reg (srl x)) -> sra x
1711 if (N0.getOpcode() == ISD::SRL &&
1712 N0.getOperand(1).getOpcode() == ISD::Constant &&
1713 cast<ConstantSDNode>(N0.getOperand(1))->getValue() == EVTBits) {
1714 return DAG.getNode(ISD::SRA, N0.getValueType(), N0.getOperand(0),
1715 N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001716 }
Nate Begemanded49632005-10-13 03:11:28 +00001717 // fold (sext_inreg (extload x)) -> (sextload x)
1718 if (N0.getOpcode() == ISD::EXTLOAD &&
1719 EVT == cast<VTSDNode>(N0.getOperand(3))->getVT() &&
Nate Begemanbfd65a02005-10-13 18:34:58 +00001720 (!AfterLegalize || TLI.isOperationLegal(ISD::SEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001721 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1722 N0.getOperand(1), N0.getOperand(2),
1723 EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00001724 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00001725 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Nate Begemanded49632005-10-13 03:11:28 +00001726 return SDOperand();
1727 }
1728 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Chris Lattner40c62d52005-10-18 06:04:22 +00001729 if (N0.getOpcode() == ISD::ZEXTLOAD && N0.hasOneUse() &&
Nate Begemanded49632005-10-13 03:11:28 +00001730 EVT == cast<VTSDNode>(N0.getOperand(3))->getVT() &&
Nate Begemanbfd65a02005-10-13 18:34:58 +00001731 (!AfterLegalize || TLI.isOperationLegal(ISD::SEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001732 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1733 N0.getOperand(1), N0.getOperand(2),
1734 EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00001735 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00001736 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Nate Begemanded49632005-10-13 03:11:28 +00001737 return SDOperand();
1738 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001739 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001740}
1741
Nate Begeman83e75ec2005-09-06 04:43:02 +00001742SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001743 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001744 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001745 MVT::ValueType VT = N->getValueType(0);
1746
1747 // noop truncate
1748 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001749 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001750 // fold (truncate c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001751 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001752 return DAG.getNode(ISD::TRUNCATE, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001753 // fold (truncate (truncate x)) -> (truncate x)
1754 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001755 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001756 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
1757 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND){
1758 if (N0.getValueType() < VT)
1759 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00001760 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001761 else if (N0.getValueType() > VT)
1762 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00001763 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001764 else
1765 // if the source and dest are the same type, we can drop both the extend
1766 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00001767 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001768 }
Nate Begeman3df4d522005-10-12 20:40:40 +00001769 // fold (truncate (load x)) -> (smaller load x)
Chris Lattner40c62d52005-10-18 06:04:22 +00001770 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse()) {
Nate Begeman3df4d522005-10-12 20:40:40 +00001771 assert(MVT::getSizeInBits(N0.getValueType()) > MVT::getSizeInBits(VT) &&
1772 "Cannot truncate to larger type!");
1773 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
Nate Begeman765784a2005-10-12 23:18:53 +00001774 // For big endian targets, we need to add an offset to the pointer to load
1775 // the correct bytes. For little endian systems, we merely need to read
1776 // fewer bytes from the same pointer.
Nate Begeman3df4d522005-10-12 20:40:40 +00001777 uint64_t PtrOff =
1778 (MVT::getSizeInBits(N0.getValueType()) - MVT::getSizeInBits(VT)) / 8;
Nate Begeman765784a2005-10-12 23:18:53 +00001779 SDOperand NewPtr = TLI.isLittleEndian() ? N0.getOperand(1) :
1780 DAG.getNode(ISD::ADD, PtrType, N0.getOperand(1),
1781 DAG.getConstant(PtrOff, PtrType));
1782 WorkList.push_back(NewPtr.Val);
Nate Begeman3df4d522005-10-12 20:40:40 +00001783 SDOperand Load = DAG.getLoad(VT, N0.getOperand(0), NewPtr,N0.getOperand(2));
Nate Begeman765784a2005-10-12 23:18:53 +00001784 WorkList.push_back(N);
Chris Lattner24edbb72005-10-13 22:10:05 +00001785 CombineTo(N0.Val, Load, Load.getValue(1));
Nate Begeman765784a2005-10-12 23:18:53 +00001786 return SDOperand();
Nate Begeman3df4d522005-10-12 20:40:40 +00001787 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001788 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001789}
1790
Chris Lattner94683772005-12-23 05:30:37 +00001791SDOperand DAGCombiner::visitBIT_CONVERT(SDNode *N) {
1792 SDOperand N0 = N->getOperand(0);
1793 MVT::ValueType VT = N->getValueType(0);
1794
1795 // If the input is a constant, let getNode() fold it.
1796 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
1797 SDOperand Res = DAG.getNode(ISD::BIT_CONVERT, VT, N0);
1798 if (Res.Val != N) return Res;
1799 }
1800
Chris Lattnerc8547d82005-12-23 05:37:50 +00001801 if (N0.getOpcode() == ISD::BIT_CONVERT) // conv(conv(x,t1),t2) -> conv(x,t2)
1802 return DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
1803
Chris Lattner57104102005-12-23 05:44:41 +00001804 // fold (conv (load x)) -> (load (conv*)x)
Chris Lattnerbf40c4b2006-01-15 18:58:59 +00001805 // FIXME: These xforms need to know that the resultant load doesn't need a
1806 // higher alignment than the original!
1807 if (0 && N0.getOpcode() == ISD::LOAD && N0.hasOneUse()) {
Chris Lattner57104102005-12-23 05:44:41 +00001808 SDOperand Load = DAG.getLoad(VT, N0.getOperand(0), N0.getOperand(1),
1809 N0.getOperand(2));
1810 WorkList.push_back(N);
1811 CombineTo(N0.Val, DAG.getNode(ISD::BIT_CONVERT, N0.getValueType(), Load),
1812 Load.getValue(1));
1813 return Load;
1814 }
1815
Chris Lattner94683772005-12-23 05:30:37 +00001816 return SDOperand();
1817}
1818
Chris Lattner01b3d732005-09-28 22:28:18 +00001819SDOperand DAGCombiner::visitFADD(SDNode *N) {
1820 SDOperand N0 = N->getOperand(0);
1821 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00001822 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1823 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001824 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00001825
1826 // fold (fadd c1, c2) -> c1+c2
1827 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001828 return DAG.getNode(ISD::FADD, VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00001829 // canonicalize constant to RHS
1830 if (N0CFP && !N1CFP)
1831 return DAG.getNode(ISD::FADD, VT, N1, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00001832 // fold (A + (-B)) -> A-B
1833 if (N1.getOpcode() == ISD::FNEG)
1834 return DAG.getNode(ISD::FSUB, VT, N0, N1.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00001835 // fold ((-A) + B) -> B-A
1836 if (N0.getOpcode() == ISD::FNEG)
1837 return DAG.getNode(ISD::FSUB, VT, N1, N0.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00001838 return SDOperand();
1839}
1840
1841SDOperand DAGCombiner::visitFSUB(SDNode *N) {
1842 SDOperand N0 = N->getOperand(0);
1843 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00001844 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1845 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001846 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00001847
1848 // fold (fsub c1, c2) -> c1-c2
1849 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001850 return DAG.getNode(ISD::FSUB, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001851 // fold (A-(-B)) -> A+B
1852 if (N1.getOpcode() == ISD::FNEG)
Nate Begemana148d982006-01-18 22:35:16 +00001853 return DAG.getNode(ISD::FADD, VT, N0, N1.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00001854 return SDOperand();
1855}
1856
1857SDOperand DAGCombiner::visitFMUL(SDNode *N) {
1858 SDOperand N0 = N->getOperand(0);
1859 SDOperand N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00001860 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1861 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001862 MVT::ValueType VT = N->getValueType(0);
1863
Nate Begeman11af4ea2005-10-17 20:40:11 +00001864 // fold (fmul c1, c2) -> c1*c2
1865 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001866 return DAG.getNode(ISD::FMUL, VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00001867 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001868 if (N0CFP && !N1CFP)
1869 return DAG.getNode(ISD::FMUL, VT, N1, N0);
Nate Begeman11af4ea2005-10-17 20:40:11 +00001870 // fold (fmul X, 2.0) -> (fadd X, X)
1871 if (N1CFP && N1CFP->isExactlyValue(+2.0))
1872 return DAG.getNode(ISD::FADD, VT, N0, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00001873 return SDOperand();
1874}
1875
1876SDOperand DAGCombiner::visitFDIV(SDNode *N) {
1877 SDOperand N0 = N->getOperand(0);
1878 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00001879 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1880 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001881 MVT::ValueType VT = N->getValueType(0);
1882
Nate Begemana148d982006-01-18 22:35:16 +00001883 // fold (fdiv c1, c2) -> c1/c2
1884 if (N0CFP && N1CFP)
1885 return DAG.getNode(ISD::FDIV, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001886 return SDOperand();
1887}
1888
1889SDOperand DAGCombiner::visitFREM(SDNode *N) {
1890 SDOperand N0 = N->getOperand(0);
1891 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00001892 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1893 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001894 MVT::ValueType VT = N->getValueType(0);
1895
Nate Begemana148d982006-01-18 22:35:16 +00001896 // fold (frem c1, c2) -> fmod(c1,c2)
1897 if (N0CFP && N1CFP)
1898 return DAG.getNode(ISD::FREM, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001899 return SDOperand();
1900}
1901
1902
Nate Begeman83e75ec2005-09-06 04:43:02 +00001903SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001904 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001905 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00001906 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001907
1908 // fold (sint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001909 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001910 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001911 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001912}
1913
Nate Begeman83e75ec2005-09-06 04:43:02 +00001914SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001915 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001916 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00001917 MVT::ValueType VT = N->getValueType(0);
1918
Nate Begeman1d4d4142005-09-01 00:19:25 +00001919 // fold (uint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001920 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001921 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001922 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001923}
1924
Nate Begeman83e75ec2005-09-06 04:43:02 +00001925SDOperand DAGCombiner::visitFP_TO_SINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00001926 SDOperand N0 = N->getOperand(0);
1927 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1928 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001929
1930 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001931 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001932 return DAG.getNode(ISD::FP_TO_SINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001933 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001934}
1935
Nate Begeman83e75ec2005-09-06 04:43:02 +00001936SDOperand DAGCombiner::visitFP_TO_UINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00001937 SDOperand N0 = N->getOperand(0);
1938 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1939 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001940
1941 // fold (fp_to_uint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001942 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001943 return DAG.getNode(ISD::FP_TO_UINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001944 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001945}
1946
Nate Begeman83e75ec2005-09-06 04:43:02 +00001947SDOperand DAGCombiner::visitFP_ROUND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00001948 SDOperand N0 = N->getOperand(0);
1949 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1950 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001951
1952 // fold (fp_round c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001953 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001954 return DAG.getNode(ISD::FP_ROUND, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001955 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001956}
1957
Nate Begeman83e75ec2005-09-06 04:43:02 +00001958SDOperand DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001959 SDOperand N0 = N->getOperand(0);
1960 MVT::ValueType VT = N->getValueType(0);
1961 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00001962 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001963
Nate Begeman1d4d4142005-09-01 00:19:25 +00001964 // fold (fp_round_inreg c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001965 if (N0CFP) {
1966 SDOperand Round = DAG.getConstantFP(N0CFP->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001967 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001968 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001969 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001970}
1971
Nate Begeman83e75ec2005-09-06 04:43:02 +00001972SDOperand DAGCombiner::visitFP_EXTEND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00001973 SDOperand N0 = N->getOperand(0);
1974 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1975 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001976
1977 // fold (fp_extend c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001978 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001979 return DAG.getNode(ISD::FP_EXTEND, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001980 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001981}
1982
Nate Begeman83e75ec2005-09-06 04:43:02 +00001983SDOperand DAGCombiner::visitFNEG(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00001984 SDOperand N0 = N->getOperand(0);
1985 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1986 MVT::ValueType VT = N->getValueType(0);
1987
1988 // fold (fneg c1) -> -c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001989 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001990 return DAG.getNode(ISD::FNEG, VT, N0);
1991 // fold (fneg (sub x, y)) -> (sub y, x)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001992 if (N->getOperand(0).getOpcode() == ISD::SUB)
Nate Begemana148d982006-01-18 22:35:16 +00001993 return DAG.getNode(ISD::SUB, VT, N->getOperand(1), N->getOperand(0));
1994 // fold (fneg (fneg x)) -> x
Nate Begeman1d4d4142005-09-01 00:19:25 +00001995 if (N->getOperand(0).getOpcode() == ISD::FNEG)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001996 return N->getOperand(0).getOperand(0);
1997 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001998}
1999
Nate Begeman83e75ec2005-09-06 04:43:02 +00002000SDOperand DAGCombiner::visitFABS(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002001 SDOperand N0 = N->getOperand(0);
2002 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2003 MVT::ValueType VT = N->getValueType(0);
2004
Nate Begeman1d4d4142005-09-01 00:19:25 +00002005 // fold (fabs c1) -> fabs(c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00002006 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002007 return DAG.getNode(ISD::FABS, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002008 // fold (fabs (fabs x)) -> (fabs x)
2009 if (N->getOperand(0).getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002010 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002011 // fold (fabs (fneg x)) -> (fabs x)
2012 if (N->getOperand(0).getOpcode() == ISD::FNEG)
Nate Begemana148d982006-01-18 22:35:16 +00002013 return DAG.getNode(ISD::FABS, VT, N->getOperand(0).getOperand(0));
Nate Begeman83e75ec2005-09-06 04:43:02 +00002014 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002015}
2016
Nate Begeman44728a72005-09-19 22:34:01 +00002017SDOperand DAGCombiner::visitBRCOND(SDNode *N) {
2018 SDOperand Chain = N->getOperand(0);
2019 SDOperand N1 = N->getOperand(1);
2020 SDOperand N2 = N->getOperand(2);
2021 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2022
2023 // never taken branch, fold to chain
2024 if (N1C && N1C->isNullValue())
2025 return Chain;
2026 // unconditional branch
Nate Begemane17daeb2005-10-05 21:43:42 +00002027 if (N1C && N1C->getValue() == 1)
Nate Begeman44728a72005-09-19 22:34:01 +00002028 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
2029 return SDOperand();
2030}
2031
2032SDOperand DAGCombiner::visitBRCONDTWOWAY(SDNode *N) {
2033 SDOperand Chain = N->getOperand(0);
2034 SDOperand N1 = N->getOperand(1);
2035 SDOperand N2 = N->getOperand(2);
2036 SDOperand N3 = N->getOperand(3);
2037 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2038
2039 // unconditional branch to true mbb
2040 if (N1C && N1C->getValue() == 1)
2041 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
2042 // unconditional branch to false mbb
2043 if (N1C && N1C->isNullValue())
2044 return DAG.getNode(ISD::BR, MVT::Other, Chain, N3);
2045 return SDOperand();
2046}
2047
Chris Lattner3ea0b472005-10-05 06:47:48 +00002048// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
2049//
Nate Begeman44728a72005-09-19 22:34:01 +00002050SDOperand DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00002051 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
2052 SDOperand CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
2053
2054 // Use SimplifySetCC to simplify SETCC's.
Nate Begemane17daeb2005-10-05 21:43:42 +00002055 SDOperand Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false);
2056 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.Val);
2057
2058 // fold br_cc true, dest -> br dest (unconditional branch)
2059 if (SCCC && SCCC->getValue())
2060 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
2061 N->getOperand(4));
2062 // fold br_cc false, dest -> unconditional fall through
2063 if (SCCC && SCCC->isNullValue())
2064 return N->getOperand(0);
2065 // fold to a simpler setcc
2066 if (Simp.Val && Simp.getOpcode() == ISD::SETCC)
2067 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
2068 Simp.getOperand(2), Simp.getOperand(0),
2069 Simp.getOperand(1), N->getOperand(4));
Nate Begeman44728a72005-09-19 22:34:01 +00002070 return SDOperand();
2071}
2072
2073SDOperand DAGCombiner::visitBRTWOWAY_CC(SDNode *N) {
Nate Begemane17daeb2005-10-05 21:43:42 +00002074 SDOperand Chain = N->getOperand(0);
2075 SDOperand CCN = N->getOperand(1);
2076 SDOperand LHS = N->getOperand(2);
2077 SDOperand RHS = N->getOperand(3);
2078 SDOperand N4 = N->getOperand(4);
2079 SDOperand N5 = N->getOperand(5);
2080
2081 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), LHS, RHS,
2082 cast<CondCodeSDNode>(CCN)->get(), false);
2083 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
2084
2085 // fold select_cc lhs, rhs, x, x, cc -> x
2086 if (N4 == N5)
2087 return DAG.getNode(ISD::BR, MVT::Other, Chain, N4);
2088 // fold select_cc true, x, y -> x
2089 if (SCCC && SCCC->getValue())
2090 return DAG.getNode(ISD::BR, MVT::Other, Chain, N4);
2091 // fold select_cc false, x, y -> y
2092 if (SCCC && SCCC->isNullValue())
2093 return DAG.getNode(ISD::BR, MVT::Other, Chain, N5);
2094 // fold to a simpler setcc
2095 if (SCC.Val && SCC.getOpcode() == ISD::SETCC)
2096 return DAG.getBR2Way_CC(Chain, SCC.getOperand(2), SCC.getOperand(0),
2097 SCC.getOperand(1), N4, N5);
Nate Begeman44728a72005-09-19 22:34:01 +00002098 return SDOperand();
2099}
2100
Chris Lattner01a22022005-10-10 22:04:48 +00002101SDOperand DAGCombiner::visitLOAD(SDNode *N) {
2102 SDOperand Chain = N->getOperand(0);
2103 SDOperand Ptr = N->getOperand(1);
2104 SDOperand SrcValue = N->getOperand(2);
2105
2106 // If this load is directly stored, replace the load value with the stored
2107 // value.
2108 // TODO: Handle store large -> read small portion.
2109 // TODO: Handle TRUNCSTORE/EXTLOAD
2110 if (Chain.getOpcode() == ISD::STORE && Chain.getOperand(2) == Ptr &&
2111 Chain.getOperand(1).getValueType() == N->getValueType(0))
2112 return CombineTo(N, Chain.getOperand(1), Chain);
2113
2114 return SDOperand();
2115}
2116
Chris Lattner87514ca2005-10-10 22:31:19 +00002117SDOperand DAGCombiner::visitSTORE(SDNode *N) {
2118 SDOperand Chain = N->getOperand(0);
2119 SDOperand Value = N->getOperand(1);
2120 SDOperand Ptr = N->getOperand(2);
2121 SDOperand SrcValue = N->getOperand(3);
2122
2123 // If this is a store that kills a previous store, remove the previous store.
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002124 if (Chain.getOpcode() == ISD::STORE && Chain.getOperand(2) == Ptr &&
Chris Lattnerfe7f0462005-10-27 07:10:34 +00002125 Chain.Val->hasOneUse() /* Avoid introducing DAG cycles */ &&
2126 // Make sure that these stores are the same value type:
2127 // FIXME: we really care that the second store is >= size of the first.
2128 Value.getValueType() == Chain.getOperand(1).getValueType()) {
Chris Lattner87514ca2005-10-10 22:31:19 +00002129 // Create a new store of Value that replaces both stores.
2130 SDNode *PrevStore = Chain.Val;
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002131 if (PrevStore->getOperand(1) == Value) // Same value multiply stored.
2132 return Chain;
Chris Lattner87514ca2005-10-10 22:31:19 +00002133 SDOperand NewStore = DAG.getNode(ISD::STORE, MVT::Other,
2134 PrevStore->getOperand(0), Value, Ptr,
2135 SrcValue);
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002136 CombineTo(N, NewStore); // Nuke this store.
Chris Lattner87514ca2005-10-10 22:31:19 +00002137 CombineTo(PrevStore, NewStore); // Nuke the previous store.
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002138 return SDOperand(N, 0);
Chris Lattner87514ca2005-10-10 22:31:19 +00002139 }
2140
Chris Lattnerc33baaa2005-12-23 05:48:07 +00002141 // If this is a store of a bit convert, store the input value.
Chris Lattnerbf40c4b2006-01-15 18:58:59 +00002142 // FIXME: This needs to know that the resultant store does not need a
2143 // higher alignment than the original.
2144 if (0 && Value.getOpcode() == ISD::BIT_CONVERT)
Chris Lattnerc33baaa2005-12-23 05:48:07 +00002145 return DAG.getNode(ISD::STORE, MVT::Other, Chain, Value.getOperand(0),
2146 Ptr, SrcValue);
2147
Chris Lattner87514ca2005-10-10 22:31:19 +00002148 return SDOperand();
2149}
2150
Jim Laskeyd6e8d412005-12-23 20:08:28 +00002151SDOperand DAGCombiner::visitLOCATION(SDNode *N) {
2152 SDOperand Chain = N->getOperand(0);
2153
2154 // Remove redundant locations (last one holds)
2155 if (Chain.getOpcode() == ISD::LOCATION && Chain.hasOneUse()) {
2156 return DAG.getNode(ISD::LOCATION, MVT::Other, Chain.getOperand(0),
2157 N->getOperand(1),
2158 N->getOperand(2),
2159 N->getOperand(3),
2160 N->getOperand(4));
2161 }
2162
2163 return SDOperand();
2164}
2165
2166SDOperand DAGCombiner::visitDEBUGLOC(SDNode *N) {
2167 SDOperand Chain = N->getOperand(0);
2168
2169 // Remove redundant debug locations (last one holds)
2170 if (Chain.getOpcode() == ISD::DEBUG_LOC && Chain.hasOneUse()) {
2171 return DAG.getNode(ISD::DEBUG_LOC, MVT::Other, Chain.getOperand(0),
2172 N->getOperand(1),
2173 N->getOperand(2),
Jim Laskeyabf6d172006-01-05 01:25:28 +00002174 N->getOperand(3));
Jim Laskeyd6e8d412005-12-23 20:08:28 +00002175 }
2176
2177 return SDOperand();
2178}
2179
Nate Begeman44728a72005-09-19 22:34:01 +00002180SDOperand DAGCombiner::SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2){
Nate Begemanf845b452005-10-08 00:29:44 +00002181 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
2182
2183 SDOperand SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
2184 cast<CondCodeSDNode>(N0.getOperand(2))->get());
2185 // If we got a simplified select_cc node back from SimplifySelectCC, then
2186 // break it down into a new SETCC node, and a new SELECT node, and then return
2187 // the SELECT node, since we were called with a SELECT node.
2188 if (SCC.Val) {
2189 // Check to see if we got a select_cc back (to turn into setcc/select).
2190 // Otherwise, just return whatever node we got back, like fabs.
2191 if (SCC.getOpcode() == ISD::SELECT_CC) {
2192 SDOperand SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
2193 SCC.getOperand(0), SCC.getOperand(1),
2194 SCC.getOperand(4));
2195 WorkList.push_back(SETCC.Val);
2196 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
2197 SCC.getOperand(3), SETCC);
2198 }
2199 return SCC;
2200 }
Nate Begeman44728a72005-09-19 22:34:01 +00002201 return SDOperand();
2202}
2203
Chris Lattner40c62d52005-10-18 06:04:22 +00002204/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
2205/// are the two values being selected between, see if we can simplify the
2206/// select.
2207///
2208bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDOperand LHS,
2209 SDOperand RHS) {
2210
2211 // If this is a select from two identical things, try to pull the operation
2212 // through the select.
2213 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
2214#if 0
2215 std::cerr << "SELECT: ["; LHS.Val->dump();
2216 std::cerr << "] ["; RHS.Val->dump();
2217 std::cerr << "]\n";
2218#endif
2219
2220 // If this is a load and the token chain is identical, replace the select
2221 // of two loads with a load through a select of the address to load from.
2222 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
2223 // constants have been dropped into the constant pool.
2224 if ((LHS.getOpcode() == ISD::LOAD ||
2225 LHS.getOpcode() == ISD::EXTLOAD ||
2226 LHS.getOpcode() == ISD::ZEXTLOAD ||
2227 LHS.getOpcode() == ISD::SEXTLOAD) &&
2228 // Token chains must be identical.
2229 LHS.getOperand(0) == RHS.getOperand(0) &&
2230 // If this is an EXTLOAD, the VT's must match.
2231 (LHS.getOpcode() == ISD::LOAD ||
2232 LHS.getOperand(3) == RHS.getOperand(3))) {
2233 // FIXME: this conflates two src values, discarding one. This is not
2234 // the right thing to do, but nothing uses srcvalues now. When they do,
2235 // turn SrcValue into a list of locations.
2236 SDOperand Addr;
2237 if (TheSelect->getOpcode() == ISD::SELECT)
2238 Addr = DAG.getNode(ISD::SELECT, LHS.getOperand(1).getValueType(),
2239 TheSelect->getOperand(0), LHS.getOperand(1),
2240 RHS.getOperand(1));
2241 else
2242 Addr = DAG.getNode(ISD::SELECT_CC, LHS.getOperand(1).getValueType(),
2243 TheSelect->getOperand(0),
2244 TheSelect->getOperand(1),
2245 LHS.getOperand(1), RHS.getOperand(1),
2246 TheSelect->getOperand(4));
2247
2248 SDOperand Load;
2249 if (LHS.getOpcode() == ISD::LOAD)
2250 Load = DAG.getLoad(TheSelect->getValueType(0), LHS.getOperand(0),
2251 Addr, LHS.getOperand(2));
2252 else
2253 Load = DAG.getExtLoad(LHS.getOpcode(), TheSelect->getValueType(0),
2254 LHS.getOperand(0), Addr, LHS.getOperand(2),
2255 cast<VTSDNode>(LHS.getOperand(3))->getVT());
2256 // Users of the select now use the result of the load.
2257 CombineTo(TheSelect, Load);
2258
2259 // Users of the old loads now use the new load's chain. We know the
2260 // old-load value is dead now.
2261 CombineTo(LHS.Val, Load.getValue(0), Load.getValue(1));
2262 CombineTo(RHS.Val, Load.getValue(0), Load.getValue(1));
2263 return true;
2264 }
2265 }
2266
2267 return false;
2268}
2269
Nate Begeman44728a72005-09-19 22:34:01 +00002270SDOperand DAGCombiner::SimplifySelectCC(SDOperand N0, SDOperand N1,
2271 SDOperand N2, SDOperand N3,
2272 ISD::CondCode CC) {
Nate Begemanf845b452005-10-08 00:29:44 +00002273
2274 MVT::ValueType VT = N2.getValueType();
2275 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
2276 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2277 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
2278 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
2279
2280 // Determine if the condition we're dealing with is constant
2281 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
2282 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
2283
2284 // fold select_cc true, x, y -> x
2285 if (SCCC && SCCC->getValue())
2286 return N2;
2287 // fold select_cc false, x, y -> y
2288 if (SCCC && SCCC->getValue() == 0)
2289 return N3;
2290
2291 // Check to see if we can simplify the select into an fabs node
2292 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
2293 // Allow either -0.0 or 0.0
2294 if (CFP->getValue() == 0.0) {
2295 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
2296 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
2297 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
2298 N2 == N3.getOperand(0))
2299 return DAG.getNode(ISD::FABS, VT, N0);
2300
2301 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
2302 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
2303 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
2304 N2.getOperand(0) == N3)
2305 return DAG.getNode(ISD::FABS, VT, N3);
2306 }
2307 }
2308
2309 // Check to see if we can perform the "gzip trick", transforming
2310 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
2311 if (N1C && N1C->isNullValue() && N3C && N3C->isNullValue() &&
2312 MVT::isInteger(N0.getValueType()) &&
2313 MVT::isInteger(N2.getValueType()) && CC == ISD::SETLT) {
2314 MVT::ValueType XType = N0.getValueType();
2315 MVT::ValueType AType = N2.getValueType();
2316 if (XType >= AType) {
2317 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00002318 // single-bit constant.
Nate Begemanf845b452005-10-08 00:29:44 +00002319 if (N2C && ((N2C->getValue() & (N2C->getValue()-1)) == 0)) {
2320 unsigned ShCtV = Log2_64(N2C->getValue());
2321 ShCtV = MVT::getSizeInBits(XType)-ShCtV-1;
2322 SDOperand ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
2323 SDOperand Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
2324 WorkList.push_back(Shift.Val);
2325 if (XType > AType) {
2326 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
2327 WorkList.push_back(Shift.Val);
2328 }
2329 return DAG.getNode(ISD::AND, AType, Shift, N2);
2330 }
2331 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
2332 DAG.getConstant(MVT::getSizeInBits(XType)-1,
2333 TLI.getShiftAmountTy()));
2334 WorkList.push_back(Shift.Val);
2335 if (XType > AType) {
2336 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
2337 WorkList.push_back(Shift.Val);
2338 }
2339 return DAG.getNode(ISD::AND, AType, Shift, N2);
2340 }
2341 }
Nate Begeman07ed4172005-10-10 21:26:48 +00002342
2343 // fold select C, 16, 0 -> shl C, 4
2344 if (N2C && N3C && N3C->isNullValue() && isPowerOf2_64(N2C->getValue()) &&
2345 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) {
2346 // Get a SetCC of the condition
2347 // FIXME: Should probably make sure that setcc is legal if we ever have a
2348 // target where it isn't.
2349 SDOperand Temp, SCC = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
2350 WorkList.push_back(SCC.Val);
2351 // cast from setcc result type to select result type
2352 if (AfterLegalize)
2353 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
2354 else
2355 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
2356 WorkList.push_back(Temp.Val);
2357 // shl setcc result by log2 n2c
2358 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
2359 DAG.getConstant(Log2_64(N2C->getValue()),
2360 TLI.getShiftAmountTy()));
2361 }
2362
Nate Begemanf845b452005-10-08 00:29:44 +00002363 // Check to see if this is the equivalent of setcc
2364 // FIXME: Turn all of these into setcc if setcc if setcc is legal
2365 // otherwise, go ahead with the folds.
2366 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getValue() == 1ULL)) {
2367 MVT::ValueType XType = N0.getValueType();
2368 if (TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultTy())) {
2369 SDOperand Res = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
2370 if (Res.getValueType() != VT)
2371 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
2372 return Res;
2373 }
2374
2375 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
2376 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
2377 TLI.isOperationLegal(ISD::CTLZ, XType)) {
2378 SDOperand Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
2379 return DAG.getNode(ISD::SRL, XType, Ctlz,
2380 DAG.getConstant(Log2_32(MVT::getSizeInBits(XType)),
2381 TLI.getShiftAmountTy()));
2382 }
2383 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
2384 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
2385 SDOperand NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
2386 N0);
2387 SDOperand NotN0 = DAG.getNode(ISD::XOR, XType, N0,
2388 DAG.getConstant(~0ULL, XType));
2389 return DAG.getNode(ISD::SRL, XType,
2390 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
2391 DAG.getConstant(MVT::getSizeInBits(XType)-1,
2392 TLI.getShiftAmountTy()));
2393 }
2394 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
2395 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
2396 SDOperand Sign = DAG.getNode(ISD::SRL, XType, N0,
2397 DAG.getConstant(MVT::getSizeInBits(XType)-1,
2398 TLI.getShiftAmountTy()));
2399 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
2400 }
2401 }
2402
2403 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
2404 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
2405 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
2406 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1)) {
2407 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N2.getOperand(0))) {
2408 MVT::ValueType XType = N0.getValueType();
2409 if (SubC->isNullValue() && MVT::isInteger(XType)) {
2410 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
2411 DAG.getConstant(MVT::getSizeInBits(XType)-1,
2412 TLI.getShiftAmountTy()));
2413 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
2414 WorkList.push_back(Shift.Val);
2415 WorkList.push_back(Add.Val);
2416 return DAG.getNode(ISD::XOR, XType, Add, Shift);
2417 }
2418 }
2419 }
2420
Nate Begeman44728a72005-09-19 22:34:01 +00002421 return SDOperand();
2422}
2423
Nate Begeman452d7be2005-09-16 00:54:12 +00002424SDOperand DAGCombiner::SimplifySetCC(MVT::ValueType VT, SDOperand N0,
Nate Begemane17daeb2005-10-05 21:43:42 +00002425 SDOperand N1, ISD::CondCode Cond,
2426 bool foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00002427 // These setcc operations always fold.
2428 switch (Cond) {
2429 default: break;
2430 case ISD::SETFALSE:
2431 case ISD::SETFALSE2: return DAG.getConstant(0, VT);
2432 case ISD::SETTRUE:
2433 case ISD::SETTRUE2: return DAG.getConstant(1, VT);
2434 }
2435
2436 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
2437 uint64_t C1 = N1C->getValue();
2438 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val)) {
2439 uint64_t C0 = N0C->getValue();
2440
2441 // Sign extend the operands if required
2442 if (ISD::isSignedIntSetCC(Cond)) {
2443 C0 = N0C->getSignExtended();
2444 C1 = N1C->getSignExtended();
2445 }
2446
2447 switch (Cond) {
2448 default: assert(0 && "Unknown integer setcc!");
2449 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
2450 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
2451 case ISD::SETULT: return DAG.getConstant(C0 < C1, VT);
2452 case ISD::SETUGT: return DAG.getConstant(C0 > C1, VT);
2453 case ISD::SETULE: return DAG.getConstant(C0 <= C1, VT);
2454 case ISD::SETUGE: return DAG.getConstant(C0 >= C1, VT);
2455 case ISD::SETLT: return DAG.getConstant((int64_t)C0 < (int64_t)C1, VT);
2456 case ISD::SETGT: return DAG.getConstant((int64_t)C0 > (int64_t)C1, VT);
2457 case ISD::SETLE: return DAG.getConstant((int64_t)C0 <= (int64_t)C1, VT);
2458 case ISD::SETGE: return DAG.getConstant((int64_t)C0 >= (int64_t)C1, VT);
2459 }
2460 } else {
2461 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
2462 if (N0.getOpcode() == ISD::ZERO_EXTEND) {
2463 unsigned InSize = MVT::getSizeInBits(N0.getOperand(0).getValueType());
2464
2465 // If the comparison constant has bits in the upper part, the
2466 // zero-extended value could never match.
2467 if (C1 & (~0ULL << InSize)) {
2468 unsigned VSize = MVT::getSizeInBits(N0.getValueType());
2469 switch (Cond) {
2470 case ISD::SETUGT:
2471 case ISD::SETUGE:
2472 case ISD::SETEQ: return DAG.getConstant(0, VT);
2473 case ISD::SETULT:
2474 case ISD::SETULE:
2475 case ISD::SETNE: return DAG.getConstant(1, VT);
2476 case ISD::SETGT:
2477 case ISD::SETGE:
2478 // True if the sign bit of C1 is set.
2479 return DAG.getConstant((C1 & (1ULL << VSize)) != 0, VT);
2480 case ISD::SETLT:
2481 case ISD::SETLE:
2482 // True if the sign bit of C1 isn't set.
2483 return DAG.getConstant((C1 & (1ULL << VSize)) == 0, VT);
2484 default:
2485 break;
2486 }
2487 }
2488
2489 // Otherwise, we can perform the comparison with the low bits.
2490 switch (Cond) {
2491 case ISD::SETEQ:
2492 case ISD::SETNE:
2493 case ISD::SETUGT:
2494 case ISD::SETUGE:
2495 case ISD::SETULT:
2496 case ISD::SETULE:
2497 return DAG.getSetCC(VT, N0.getOperand(0),
2498 DAG.getConstant(C1, N0.getOperand(0).getValueType()),
2499 Cond);
2500 default:
2501 break; // todo, be more careful with signed comparisons
2502 }
2503 } else if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
2504 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
2505 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N0.getOperand(1))->getVT();
2506 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
2507 MVT::ValueType ExtDstTy = N0.getValueType();
2508 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
2509
2510 // If the extended part has any inconsistent bits, it cannot ever
2511 // compare equal. In other words, they have to be all ones or all
2512 // zeros.
2513 uint64_t ExtBits =
2514 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
2515 if ((C1 & ExtBits) != 0 && (C1 & ExtBits) != ExtBits)
2516 return DAG.getConstant(Cond == ISD::SETNE, VT);
2517
2518 SDOperand ZextOp;
2519 MVT::ValueType Op0Ty = N0.getOperand(0).getValueType();
2520 if (Op0Ty == ExtSrcTy) {
2521 ZextOp = N0.getOperand(0);
2522 } else {
2523 int64_t Imm = ~0ULL >> (64-ExtSrcTyBits);
2524 ZextOp = DAG.getNode(ISD::AND, Op0Ty, N0.getOperand(0),
2525 DAG.getConstant(Imm, Op0Ty));
2526 }
2527 WorkList.push_back(ZextOp.Val);
2528 // Otherwise, make this a use of a zext.
2529 return DAG.getSetCC(VT, ZextOp,
2530 DAG.getConstant(C1 & (~0ULL>>(64-ExtSrcTyBits)),
2531 ExtDstTy),
2532 Cond);
2533 }
Chris Lattner5c46f742005-10-05 06:11:08 +00002534
Nate Begeman452d7be2005-09-16 00:54:12 +00002535 uint64_t MinVal, MaxVal;
2536 unsigned OperandBitSize = MVT::getSizeInBits(N1C->getValueType(0));
2537 if (ISD::isSignedIntSetCC(Cond)) {
2538 MinVal = 1ULL << (OperandBitSize-1);
2539 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
2540 MaxVal = ~0ULL >> (65-OperandBitSize);
2541 else
2542 MaxVal = 0;
2543 } else {
2544 MinVal = 0;
2545 MaxVal = ~0ULL >> (64-OperandBitSize);
2546 }
2547
2548 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
2549 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
2550 if (C1 == MinVal) return DAG.getConstant(1, VT); // X >= MIN --> true
2551 --C1; // X >= C0 --> X > (C0-1)
2552 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
2553 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
2554 }
2555
2556 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
2557 if (C1 == MaxVal) return DAG.getConstant(1, VT); // X <= MAX --> true
2558 ++C1; // X <= C0 --> X < (C0+1)
2559 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
2560 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
2561 }
2562
2563 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal)
2564 return DAG.getConstant(0, VT); // X < MIN --> false
2565
2566 // Canonicalize setgt X, Min --> setne X, Min
2567 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MinVal)
2568 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
Chris Lattnerc8597ca2005-10-21 21:23:25 +00002569 // Canonicalize setlt X, Max --> setne X, Max
2570 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MaxVal)
2571 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
Nate Begeman452d7be2005-09-16 00:54:12 +00002572
2573 // If we have setult X, 1, turn it into seteq X, 0
2574 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal+1)
2575 return DAG.getSetCC(VT, N0, DAG.getConstant(MinVal, N0.getValueType()),
2576 ISD::SETEQ);
2577 // If we have setugt X, Max-1, turn it into seteq X, Max
2578 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal-1)
2579 return DAG.getSetCC(VT, N0, DAG.getConstant(MaxVal, N0.getValueType()),
2580 ISD::SETEQ);
2581
2582 // If we have "setcc X, C0", check to see if we can shrink the immediate
2583 // by changing cc.
2584
2585 // SETUGT X, SINTMAX -> SETLT X, 0
2586 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
2587 C1 == (~0ULL >> (65-OperandBitSize)))
2588 return DAG.getSetCC(VT, N0, DAG.getConstant(0, N1.getValueType()),
2589 ISD::SETLT);
2590
2591 // FIXME: Implement the rest of these.
2592
2593 // Fold bit comparisons when we can.
2594 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
2595 VT == N0.getValueType() && N0.getOpcode() == ISD::AND)
2596 if (ConstantSDNode *AndRHS =
2597 dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2598 if (Cond == ISD::SETNE && C1 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
2599 // Perform the xform if the AND RHS is a single bit.
2600 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
2601 return DAG.getNode(ISD::SRL, VT, N0,
2602 DAG.getConstant(Log2_64(AndRHS->getValue()),
2603 TLI.getShiftAmountTy()));
2604 }
2605 } else if (Cond == ISD::SETEQ && C1 == AndRHS->getValue()) {
2606 // (X & 8) == 8 --> (X & 8) >> 3
2607 // Perform the xform if C1 is a single bit.
2608 if ((C1 & (C1-1)) == 0) {
2609 return DAG.getNode(ISD::SRL, VT, N0,
2610 DAG.getConstant(Log2_64(C1),TLI.getShiftAmountTy()));
2611 }
2612 }
2613 }
2614 }
2615 } else if (isa<ConstantSDNode>(N0.Val)) {
2616 // Ensure that the constant occurs on the RHS.
2617 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
2618 }
2619
2620 if (ConstantFPSDNode *N0C = dyn_cast<ConstantFPSDNode>(N0.Val))
2621 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) {
2622 double C0 = N0C->getValue(), C1 = N1C->getValue();
2623
2624 switch (Cond) {
2625 default: break; // FIXME: Implement the rest of these!
2626 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
2627 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
2628 case ISD::SETLT: return DAG.getConstant(C0 < C1, VT);
2629 case ISD::SETGT: return DAG.getConstant(C0 > C1, VT);
2630 case ISD::SETLE: return DAG.getConstant(C0 <= C1, VT);
2631 case ISD::SETGE: return DAG.getConstant(C0 >= C1, VT);
2632 }
2633 } else {
2634 // Ensure that the constant occurs on the RHS.
2635 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
2636 }
2637
2638 if (N0 == N1) {
2639 // We can always fold X == Y for integer setcc's.
2640 if (MVT::isInteger(N0.getValueType()))
2641 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
2642 unsigned UOF = ISD::getUnorderedFlavor(Cond);
2643 if (UOF == 2) // FP operators that are undefined on NaNs.
2644 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
2645 if (UOF == unsigned(ISD::isTrueWhenEqual(Cond)))
2646 return DAG.getConstant(UOF, VT);
2647 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
2648 // if it is not already.
Chris Lattner4090aee2006-01-18 19:13:41 +00002649 ISD::CondCode NewCond = UOF == 0 ? ISD::SETO : ISD::SETUO;
Nate Begeman452d7be2005-09-16 00:54:12 +00002650 if (NewCond != Cond)
2651 return DAG.getSetCC(VT, N0, N1, NewCond);
2652 }
2653
2654 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
2655 MVT::isInteger(N0.getValueType())) {
2656 if (N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::SUB ||
2657 N0.getOpcode() == ISD::XOR) {
2658 // Simplify (X+Y) == (X+Z) --> Y == Z
2659 if (N0.getOpcode() == N1.getOpcode()) {
2660 if (N0.getOperand(0) == N1.getOperand(0))
2661 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(1), Cond);
2662 if (N0.getOperand(1) == N1.getOperand(1))
2663 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(0), Cond);
2664 if (isCommutativeBinOp(N0.getOpcode())) {
2665 // If X op Y == Y op X, try other combinations.
2666 if (N0.getOperand(0) == N1.getOperand(1))
2667 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(0), Cond);
2668 if (N0.getOperand(1) == N1.getOperand(0))
Chris Lattnera158eee2005-10-25 18:57:30 +00002669 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(1), Cond);
Nate Begeman452d7be2005-09-16 00:54:12 +00002670 }
2671 }
2672
Chris Lattner5c46f742005-10-05 06:11:08 +00002673 // Turn (X^C1) == C2 into X == C1^C2 iff X&~C1 = 0. Common for condcodes.
2674 if (N0.getOpcode() == ISD::XOR)
2675 if (ConstantSDNode *XORC = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
2676 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(N1)) {
2677 // If we know that all of the inverted bits are zero, don't bother
2678 // performing the inversion.
2679 if (MaskedValueIsZero(N0.getOperand(0), ~XORC->getValue(), TLI))
2680 return DAG.getSetCC(VT, N0.getOperand(0),
2681 DAG.getConstant(XORC->getValue()^RHSC->getValue(),
2682 N0.getValueType()), Cond);
2683 }
2684
Nate Begeman452d7be2005-09-16 00:54:12 +00002685 // Simplify (X+Z) == X --> Z == 0
2686 if (N0.getOperand(0) == N1)
2687 return DAG.getSetCC(VT, N0.getOperand(1),
2688 DAG.getConstant(0, N0.getValueType()), Cond);
2689 if (N0.getOperand(1) == N1) {
2690 if (isCommutativeBinOp(N0.getOpcode()))
2691 return DAG.getSetCC(VT, N0.getOperand(0),
2692 DAG.getConstant(0, N0.getValueType()), Cond);
2693 else {
2694 assert(N0.getOpcode() == ISD::SUB && "Unexpected operation!");
2695 // (Z-X) == X --> Z == X<<1
2696 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(),
2697 N1,
2698 DAG.getConstant(1,TLI.getShiftAmountTy()));
2699 WorkList.push_back(SH.Val);
2700 return DAG.getSetCC(VT, N0.getOperand(0), SH, Cond);
2701 }
2702 }
2703 }
2704
2705 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
2706 N1.getOpcode() == ISD::XOR) {
2707 // Simplify X == (X+Z) --> Z == 0
2708 if (N1.getOperand(0) == N0) {
2709 return DAG.getSetCC(VT, N1.getOperand(1),
2710 DAG.getConstant(0, N1.getValueType()), Cond);
2711 } else if (N1.getOperand(1) == N0) {
2712 if (isCommutativeBinOp(N1.getOpcode())) {
2713 return DAG.getSetCC(VT, N1.getOperand(0),
2714 DAG.getConstant(0, N1.getValueType()), Cond);
2715 } else {
2716 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
2717 // X == (Z-X) --> X<<1 == Z
2718 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(), N0,
2719 DAG.getConstant(1,TLI.getShiftAmountTy()));
2720 WorkList.push_back(SH.Val);
2721 return DAG.getSetCC(VT, SH, N1.getOperand(0), Cond);
2722 }
2723 }
2724 }
2725 }
2726
2727 // Fold away ALL boolean setcc's.
2728 SDOperand Temp;
Nate Begemane17daeb2005-10-05 21:43:42 +00002729 if (N0.getValueType() == MVT::i1 && foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00002730 switch (Cond) {
2731 default: assert(0 && "Unknown integer setcc!");
2732 case ISD::SETEQ: // X == Y -> (X^Y)^1
2733 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
2734 N0 = DAG.getNode(ISD::XOR, MVT::i1, Temp, DAG.getConstant(1, MVT::i1));
2735 WorkList.push_back(Temp.Val);
2736 break;
2737 case ISD::SETNE: // X != Y --> (X^Y)
2738 N0 = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
2739 break;
2740 case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> X^1 & Y
2741 case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> X^1 & Y
2742 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
2743 N0 = DAG.getNode(ISD::AND, MVT::i1, N1, Temp);
2744 WorkList.push_back(Temp.Val);
2745 break;
2746 case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> Y^1 & X
2747 case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> Y^1 & X
2748 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
2749 N0 = DAG.getNode(ISD::AND, MVT::i1, N0, Temp);
2750 WorkList.push_back(Temp.Val);
2751 break;
2752 case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> X^1 | Y
2753 case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> X^1 | Y
2754 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
2755 N0 = DAG.getNode(ISD::OR, MVT::i1, N1, Temp);
2756 WorkList.push_back(Temp.Val);
2757 break;
2758 case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> Y^1 | X
2759 case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> Y^1 | X
2760 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
2761 N0 = DAG.getNode(ISD::OR, MVT::i1, N0, Temp);
2762 break;
2763 }
2764 if (VT != MVT::i1) {
2765 WorkList.push_back(N0.Val);
2766 // FIXME: If running after legalize, we probably can't do this.
2767 N0 = DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
2768 }
2769 return N0;
2770 }
2771
2772 // Could not fold it.
2773 return SDOperand();
2774}
2775
Nate Begeman69575232005-10-20 02:15:44 +00002776/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
2777/// return a DAG expression to select that will generate the same value by
2778/// multiplying by a magic number. See:
2779/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
2780SDOperand DAGCombiner::BuildSDIV(SDNode *N) {
2781 MVT::ValueType VT = N->getValueType(0);
Chris Lattnere9936d12005-10-22 18:50:15 +00002782
2783 // Check to see if we can do this.
2784 if (!TLI.isTypeLegal(VT) || (VT != MVT::i32 && VT != MVT::i64))
2785 return SDOperand(); // BuildSDIV only operates on i32 or i64
2786 if (!TLI.isOperationLegal(ISD::MULHS, VT))
2787 return SDOperand(); // Make sure the target supports MULHS.
Nate Begeman69575232005-10-20 02:15:44 +00002788
Nate Begemanc6a454e2005-10-20 17:45:03 +00002789 int64_t d = cast<ConstantSDNode>(N->getOperand(1))->getSignExtended();
Nate Begeman69575232005-10-20 02:15:44 +00002790 ms magics = (VT == MVT::i32) ? magic32(d) : magic64(d);
2791
2792 // Multiply the numerator (operand 0) by the magic value
2793 SDOperand Q = DAG.getNode(ISD::MULHS, VT, N->getOperand(0),
2794 DAG.getConstant(magics.m, VT));
2795 // If d > 0 and m < 0, add the numerator
2796 if (d > 0 && magics.m < 0) {
2797 Q = DAG.getNode(ISD::ADD, VT, Q, N->getOperand(0));
2798 WorkList.push_back(Q.Val);
2799 }
2800 // If d < 0 and m > 0, subtract the numerator.
2801 if (d < 0 && magics.m > 0) {
2802 Q = DAG.getNode(ISD::SUB, VT, Q, N->getOperand(0));
2803 WorkList.push_back(Q.Val);
2804 }
2805 // Shift right algebraic if shift value is nonzero
2806 if (magics.s > 0) {
2807 Q = DAG.getNode(ISD::SRA, VT, Q,
2808 DAG.getConstant(magics.s, TLI.getShiftAmountTy()));
2809 WorkList.push_back(Q.Val);
2810 }
2811 // Extract the sign bit and add it to the quotient
2812 SDOperand T =
Nate Begeman4d385672005-10-21 01:51:45 +00002813 DAG.getNode(ISD::SRL, VT, Q, DAG.getConstant(MVT::getSizeInBits(VT)-1,
2814 TLI.getShiftAmountTy()));
Nate Begeman69575232005-10-20 02:15:44 +00002815 WorkList.push_back(T.Val);
2816 return DAG.getNode(ISD::ADD, VT, Q, T);
2817}
2818
2819/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
2820/// return a DAG expression to select that will generate the same value by
2821/// multiplying by a magic number. See:
2822/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
2823SDOperand DAGCombiner::BuildUDIV(SDNode *N) {
2824 MVT::ValueType VT = N->getValueType(0);
Chris Lattnere9936d12005-10-22 18:50:15 +00002825
2826 // Check to see if we can do this.
2827 if (!TLI.isTypeLegal(VT) || (VT != MVT::i32 && VT != MVT::i64))
2828 return SDOperand(); // BuildUDIV only operates on i32 or i64
2829 if (!TLI.isOperationLegal(ISD::MULHU, VT))
2830 return SDOperand(); // Make sure the target supports MULHU.
Nate Begeman69575232005-10-20 02:15:44 +00002831
2832 uint64_t d = cast<ConstantSDNode>(N->getOperand(1))->getValue();
2833 mu magics = (VT == MVT::i32) ? magicu32(d) : magicu64(d);
2834
2835 // Multiply the numerator (operand 0) by the magic value
2836 SDOperand Q = DAG.getNode(ISD::MULHU, VT, N->getOperand(0),
2837 DAG.getConstant(magics.m, VT));
2838 WorkList.push_back(Q.Val);
2839
2840 if (magics.a == 0) {
2841 return DAG.getNode(ISD::SRL, VT, Q,
2842 DAG.getConstant(magics.s, TLI.getShiftAmountTy()));
2843 } else {
2844 SDOperand NPQ = DAG.getNode(ISD::SUB, VT, N->getOperand(0), Q);
2845 WorkList.push_back(NPQ.Val);
2846 NPQ = DAG.getNode(ISD::SRL, VT, NPQ,
2847 DAG.getConstant(1, TLI.getShiftAmountTy()));
2848 WorkList.push_back(NPQ.Val);
2849 NPQ = DAG.getNode(ISD::ADD, VT, NPQ, Q);
2850 WorkList.push_back(NPQ.Val);
2851 return DAG.getNode(ISD::SRL, VT, NPQ,
2852 DAG.getConstant(magics.s-1, TLI.getShiftAmountTy()));
2853 }
2854}
2855
Nate Begeman1d4d4142005-09-01 00:19:25 +00002856// SelectionDAG::Combine - This is the entry point for the file.
2857//
Nate Begeman4ebd8052005-09-01 23:24:04 +00002858void SelectionDAG::Combine(bool RunningAfterLegalize) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002859 /// run - This is the main entry point to this class.
2860 ///
Nate Begeman4ebd8052005-09-01 23:24:04 +00002861 DAGCombiner(*this).Run(RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002862}