blob: ad26719dca89dfb2dd402e220f30ed991fbe92b2 [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>
48using namespace llvm;
49
50namespace {
51 Statistic<> NodesCombined ("dagcombiner", "Number of dag nodes combined");
52
53 class DAGCombiner {
54 SelectionDAG &DAG;
55 TargetLowering &TLI;
Nate Begeman4ebd8052005-09-01 23:24:04 +000056 bool AfterLegalize;
Nate Begeman1d4d4142005-09-01 00:19:25 +000057
58 // Worklist of all of the nodes that need to be simplified.
59 std::vector<SDNode*> WorkList;
60
61 /// AddUsersToWorkList - When an instruction is simplified, add all users of
62 /// the instruction to the work lists because they might get more simplified
63 /// now.
64 ///
65 void AddUsersToWorkList(SDNode *N) {
66 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
Nate Begeman4ebd8052005-09-01 23:24:04 +000067 UI != UE; ++UI)
68 WorkList.push_back(*UI);
Nate Begeman1d4d4142005-09-01 00:19:25 +000069 }
70
71 /// removeFromWorkList - remove all instances of N from the worklist.
72 void removeFromWorkList(SDNode *N) {
73 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), N),
74 WorkList.end());
75 }
76
Chris Lattner01a22022005-10-10 22:04:48 +000077 SDOperand CombineTo(SDNode *N, const std::vector<SDOperand> &To) {
Chris Lattner87514ca2005-10-10 22:31:19 +000078 ++NodesCombined;
Chris Lattner01a22022005-10-10 22:04:48 +000079 DEBUG(std::cerr << "\nReplacing "; N->dump();
80 std::cerr << "\nWith: "; To[0].Val->dump();
81 std::cerr << " and " << To.size()-1 << " other values\n");
82 std::vector<SDNode*> NowDead;
83 DAG.ReplaceAllUsesWith(N, To, &NowDead);
84
85 // Push the new nodes and any users onto the worklist
86 for (unsigned i = 0, e = To.size(); i != e; ++i) {
87 WorkList.push_back(To[i].Val);
88 AddUsersToWorkList(To[i].Val);
89 }
90
91 // Nodes can end up on the worklist more than once. Make sure we do
92 // not process a node that has been replaced.
93 removeFromWorkList(N);
94 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
95 removeFromWorkList(NowDead[i]);
96
97 // Finally, since the node is now dead, remove it from the graph.
98 DAG.DeleteNode(N);
99 return SDOperand(N, 0);
100 }
Chris Lattner87514ca2005-10-10 22:31:19 +0000101
102 SDOperand CombineTo(SDNode *N, SDOperand Res) {
103 std::vector<SDOperand> To;
104 To.push_back(Res);
105 return CombineTo(N, To);
106 }
Chris Lattner01a22022005-10-10 22:04:48 +0000107
108 SDOperand CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1) {
109 std::vector<SDOperand> To;
110 To.push_back(Res0);
111 To.push_back(Res1);
112 return CombineTo(N, To);
113 }
114
Nate Begeman1d4d4142005-09-01 00:19:25 +0000115 /// visit - call the node-specific routine that knows how to fold each
116 /// particular type of node.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000117 SDOperand visit(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000118
119 // Visitation implementation - Implement dag node combining for different
120 // node types. The semantics are as follows:
121 // Return Value:
Nate Begeman2300f552005-09-07 00:15:36 +0000122 // SDOperand.Val == 0 - No change was made
Chris Lattner01a22022005-10-10 22:04:48 +0000123 // SDOperand.Val == N - N was replaced, is dead, and is already handled.
Nate Begeman2300f552005-09-07 00:15:36 +0000124 // otherwise - N should be replaced by the returned Operand.
Nate Begeman1d4d4142005-09-01 00:19:25 +0000125 //
Nate Begeman83e75ec2005-09-06 04:43:02 +0000126 SDOperand visitTokenFactor(SDNode *N);
127 SDOperand visitADD(SDNode *N);
128 SDOperand visitSUB(SDNode *N);
129 SDOperand visitMUL(SDNode *N);
130 SDOperand visitSDIV(SDNode *N);
131 SDOperand visitUDIV(SDNode *N);
132 SDOperand visitSREM(SDNode *N);
133 SDOperand visitUREM(SDNode *N);
134 SDOperand visitMULHU(SDNode *N);
135 SDOperand visitMULHS(SDNode *N);
136 SDOperand visitAND(SDNode *N);
137 SDOperand visitOR(SDNode *N);
138 SDOperand visitXOR(SDNode *N);
139 SDOperand visitSHL(SDNode *N);
140 SDOperand visitSRA(SDNode *N);
141 SDOperand visitSRL(SDNode *N);
142 SDOperand visitCTLZ(SDNode *N);
143 SDOperand visitCTTZ(SDNode *N);
144 SDOperand visitCTPOP(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000145 SDOperand visitSELECT(SDNode *N);
146 SDOperand visitSELECT_CC(SDNode *N);
147 SDOperand visitSETCC(SDNode *N);
Nate Begeman5054f162005-10-14 01:12:21 +0000148 SDOperand visitADD_PARTS(SDNode *N);
149 SDOperand visitSUB_PARTS(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000150 SDOperand visitSIGN_EXTEND(SDNode *N);
151 SDOperand visitZERO_EXTEND(SDNode *N);
152 SDOperand visitSIGN_EXTEND_INREG(SDNode *N);
153 SDOperand visitTRUNCATE(SDNode *N);
Chris Lattner94683772005-12-23 05:30:37 +0000154 SDOperand visitBIT_CONVERT(SDNode *N);
Nate Begeman5054f162005-10-14 01:12:21 +0000155
Chris Lattner01b3d732005-09-28 22:28:18 +0000156 SDOperand visitFADD(SDNode *N);
157 SDOperand visitFSUB(SDNode *N);
158 SDOperand visitFMUL(SDNode *N);
159 SDOperand visitFDIV(SDNode *N);
160 SDOperand visitFREM(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000161 SDOperand visitSINT_TO_FP(SDNode *N);
162 SDOperand visitUINT_TO_FP(SDNode *N);
163 SDOperand visitFP_TO_SINT(SDNode *N);
164 SDOperand visitFP_TO_UINT(SDNode *N);
165 SDOperand visitFP_ROUND(SDNode *N);
166 SDOperand visitFP_ROUND_INREG(SDNode *N);
167 SDOperand visitFP_EXTEND(SDNode *N);
168 SDOperand visitFNEG(SDNode *N);
169 SDOperand visitFABS(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000170 SDOperand visitBRCOND(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000171 SDOperand visitBRCONDTWOWAY(SDNode *N);
172 SDOperand visitBR_CC(SDNode *N);
173 SDOperand visitBRTWOWAY_CC(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000174
Chris Lattner01a22022005-10-10 22:04:48 +0000175 SDOperand visitLOAD(SDNode *N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000176 SDOperand visitSTORE(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000177
Chris Lattner40c62d52005-10-18 06:04:22 +0000178 bool SimplifySelectOps(SDNode *SELECT, SDOperand LHS, SDOperand RHS);
Nate Begeman44728a72005-09-19 22:34:01 +0000179 SDOperand SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2);
180 SDOperand SimplifySelectCC(SDOperand N0, SDOperand N1, SDOperand N2,
181 SDOperand N3, ISD::CondCode CC);
Nate Begeman452d7be2005-09-16 00:54:12 +0000182 SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N0, SDOperand N1,
Nate Begemane17daeb2005-10-05 21:43:42 +0000183 ISD::CondCode Cond, bool foldBooleans = true);
Nate Begeman69575232005-10-20 02:15:44 +0000184
185 SDOperand BuildSDIV(SDNode *N);
186 SDOperand BuildUDIV(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000187public:
188 DAGCombiner(SelectionDAG &D)
Nate Begeman646d7e22005-09-02 21:18:40 +0000189 : DAG(D), TLI(D.getTargetLoweringInfo()), AfterLegalize(false) {}
Nate Begeman1d4d4142005-09-01 00:19:25 +0000190
191 /// Run - runs the dag combiner on all nodes in the work list
Nate Begeman4ebd8052005-09-01 23:24:04 +0000192 void Run(bool RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000193 };
194}
195
Nate Begeman69575232005-10-20 02:15:44 +0000196struct ms {
197 int64_t m; // magic number
198 int64_t s; // shift amount
199};
200
201struct mu {
202 uint64_t m; // magic number
203 int64_t a; // add indicator
204 int64_t s; // shift amount
205};
206
207/// magic - calculate the magic numbers required to codegen an integer sdiv as
208/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
209/// or -1.
210static ms magic32(int32_t d) {
211 int32_t p;
212 uint32_t ad, anc, delta, q1, r1, q2, r2, t;
213 const uint32_t two31 = 0x80000000U;
214 struct ms mag;
215
216 ad = abs(d);
217 t = two31 + ((uint32_t)d >> 31);
218 anc = t - 1 - t%ad; // absolute value of nc
219 p = 31; // initialize p
220 q1 = two31/anc; // initialize q1 = 2p/abs(nc)
221 r1 = two31 - q1*anc; // initialize r1 = rem(2p,abs(nc))
222 q2 = two31/ad; // initialize q2 = 2p/abs(d)
223 r2 = two31 - q2*ad; // initialize r2 = rem(2p,abs(d))
224 do {
225 p = p + 1;
226 q1 = 2*q1; // update q1 = 2p/abs(nc)
227 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
228 if (r1 >= anc) { // must be unsigned comparison
229 q1 = q1 + 1;
230 r1 = r1 - anc;
231 }
232 q2 = 2*q2; // update q2 = 2p/abs(d)
233 r2 = 2*r2; // update r2 = rem(2p/abs(d))
234 if (r2 >= ad) { // must be unsigned comparison
235 q2 = q2 + 1;
236 r2 = r2 - ad;
237 }
238 delta = ad - r2;
239 } while (q1 < delta || (q1 == delta && r1 == 0));
240
241 mag.m = (int32_t)(q2 + 1); // make sure to sign extend
242 if (d < 0) mag.m = -mag.m; // resulting magic number
243 mag.s = p - 32; // resulting shift
244 return mag;
245}
246
247/// magicu - calculate the magic numbers required to codegen an integer udiv as
248/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
249static mu magicu32(uint32_t d) {
250 int32_t p;
251 uint32_t nc, delta, q1, r1, q2, r2;
252 struct mu magu;
253 magu.a = 0; // initialize "add" indicator
254 nc = - 1 - (-d)%d;
255 p = 31; // initialize p
256 q1 = 0x80000000/nc; // initialize q1 = 2p/nc
257 r1 = 0x80000000 - q1*nc; // initialize r1 = rem(2p,nc)
258 q2 = 0x7FFFFFFF/d; // initialize q2 = (2p-1)/d
259 r2 = 0x7FFFFFFF - q2*d; // initialize r2 = rem((2p-1),d)
260 do {
261 p = p + 1;
262 if (r1 >= nc - r1 ) {
263 q1 = 2*q1 + 1; // update q1
264 r1 = 2*r1 - nc; // update r1
265 }
266 else {
267 q1 = 2*q1; // update q1
268 r1 = 2*r1; // update r1
269 }
270 if (r2 + 1 >= d - r2) {
271 if (q2 >= 0x7FFFFFFF) magu.a = 1;
272 q2 = 2*q2 + 1; // update q2
273 r2 = 2*r2 + 1 - d; // update r2
274 }
275 else {
276 if (q2 >= 0x80000000) magu.a = 1;
277 q2 = 2*q2; // update q2
278 r2 = 2*r2 + 1; // update r2
279 }
280 delta = d - 1 - r2;
281 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
282 magu.m = q2 + 1; // resulting magic number
283 magu.s = p - 32; // resulting shift
284 return magu;
285}
286
287/// magic - calculate the magic numbers required to codegen an integer sdiv as
288/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
289/// or -1.
290static ms magic64(int64_t d) {
291 int64_t p;
292 uint64_t ad, anc, delta, q1, r1, q2, r2, t;
293 const uint64_t two63 = 9223372036854775808ULL; // 2^63
294 struct ms mag;
295
Chris Lattnerf75f2a02005-10-20 17:01:00 +0000296 ad = d >= 0 ? d : -d;
Nate Begeman69575232005-10-20 02:15:44 +0000297 t = two63 + ((uint64_t)d >> 63);
298 anc = t - 1 - t%ad; // absolute value of nc
299 p = 63; // initialize p
300 q1 = two63/anc; // initialize q1 = 2p/abs(nc)
301 r1 = two63 - q1*anc; // initialize r1 = rem(2p,abs(nc))
302 q2 = two63/ad; // initialize q2 = 2p/abs(d)
303 r2 = two63 - q2*ad; // initialize r2 = rem(2p,abs(d))
304 do {
305 p = p + 1;
306 q1 = 2*q1; // update q1 = 2p/abs(nc)
307 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
308 if (r1 >= anc) { // must be unsigned comparison
309 q1 = q1 + 1;
310 r1 = r1 - anc;
311 }
312 q2 = 2*q2; // update q2 = 2p/abs(d)
313 r2 = 2*r2; // update r2 = rem(2p/abs(d))
314 if (r2 >= ad) { // must be unsigned comparison
315 q2 = q2 + 1;
316 r2 = r2 - ad;
317 }
318 delta = ad - r2;
319 } while (q1 < delta || (q1 == delta && r1 == 0));
320
321 mag.m = q2 + 1;
322 if (d < 0) mag.m = -mag.m; // resulting magic number
323 mag.s = p - 64; // resulting shift
324 return mag;
325}
326
327/// magicu - calculate the magic numbers required to codegen an integer udiv as
328/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
329static mu magicu64(uint64_t d)
330{
331 int64_t p;
332 uint64_t nc, delta, q1, r1, q2, r2;
333 struct mu magu;
334 magu.a = 0; // initialize "add" indicator
335 nc = - 1 - (-d)%d;
336 p = 63; // initialize p
337 q1 = 0x8000000000000000ull/nc; // initialize q1 = 2p/nc
338 r1 = 0x8000000000000000ull - q1*nc; // initialize r1 = rem(2p,nc)
339 q2 = 0x7FFFFFFFFFFFFFFFull/d; // initialize q2 = (2p-1)/d
340 r2 = 0x7FFFFFFFFFFFFFFFull - q2*d; // initialize r2 = rem((2p-1),d)
341 do {
342 p = p + 1;
343 if (r1 >= nc - r1 ) {
344 q1 = 2*q1 + 1; // update q1
345 r1 = 2*r1 - nc; // update r1
346 }
347 else {
348 q1 = 2*q1; // update q1
349 r1 = 2*r1; // update r1
350 }
351 if (r2 + 1 >= d - r2) {
352 if (q2 >= 0x7FFFFFFFFFFFFFFFull) magu.a = 1;
353 q2 = 2*q2 + 1; // update q2
354 r2 = 2*r2 + 1 - d; // update r2
355 }
356 else {
357 if (q2 >= 0x8000000000000000ull) magu.a = 1;
358 q2 = 2*q2; // update q2
359 r2 = 2*r2 + 1; // update r2
360 }
361 delta = d - 1 - r2;
362 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
363 magu.m = q2 + 1; // resulting magic number
364 magu.s = p - 64; // resulting shift
365 return magu;
366}
367
Nate Begeman07ed4172005-10-10 21:26:48 +0000368/// MaskedValueIsZero - Return true if 'Op & Mask' is known to be zero. We use
369/// this predicate to simplify operations downstream. Op and Mask are known to
Nate Begeman1d4d4142005-09-01 00:19:25 +0000370/// be the same type.
371static bool MaskedValueIsZero(const SDOperand &Op, uint64_t Mask,
372 const TargetLowering &TLI) {
373 unsigned SrcBits;
374 if (Mask == 0) return true;
375
376 // If we know the result of a setcc has the top bits zero, use this info.
377 switch (Op.getOpcode()) {
Nate Begeman4ebd8052005-09-01 23:24:04 +0000378 case ISD::Constant:
379 return (cast<ConstantSDNode>(Op)->getValue() & Mask) == 0;
380 case ISD::SETCC:
381 return ((Mask & 1) == 0) &&
382 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult;
383 case ISD::ZEXTLOAD:
384 SrcBits = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(3))->getVT());
385 return (Mask & ((1ULL << SrcBits)-1)) == 0; // Returning only the zext bits.
386 case ISD::ZERO_EXTEND:
387 SrcBits = MVT::getSizeInBits(Op.getOperand(0).getValueType());
Chris Lattner7c225752005-11-02 01:47:04 +0000388 return MaskedValueIsZero(Op.getOperand(0),Mask & (~0ULL >> (64-SrcBits)),TLI);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000389 case ISD::AssertZext:
390 SrcBits = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
391 return (Mask & ((1ULL << SrcBits)-1)) == 0; // Returning only the zext bits.
392 case ISD::AND:
Chris Lattneree899e62005-10-09 22:12:36 +0000393 // If either of the operands has zero bits, the result will too.
394 if (MaskedValueIsZero(Op.getOperand(1), Mask, TLI) ||
395 MaskedValueIsZero(Op.getOperand(0), Mask, TLI))
396 return true;
Nate Begeman4ebd8052005-09-01 23:24:04 +0000397 // (X & C1) & C2 == 0 iff C1 & C2 == 0.
398 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
399 return MaskedValueIsZero(Op.getOperand(0),AndRHS->getValue() & Mask, TLI);
Chris Lattneree899e62005-10-09 22:12:36 +0000400 return false;
Nate Begeman4ebd8052005-09-01 23:24:04 +0000401 case ISD::OR:
402 case ISD::XOR:
403 return MaskedValueIsZero(Op.getOperand(0), Mask, TLI) &&
404 MaskedValueIsZero(Op.getOperand(1), Mask, TLI);
405 case ISD::SELECT:
406 return MaskedValueIsZero(Op.getOperand(1), Mask, TLI) &&
407 MaskedValueIsZero(Op.getOperand(2), Mask, TLI);
408 case ISD::SELECT_CC:
409 return MaskedValueIsZero(Op.getOperand(2), Mask, TLI) &&
410 MaskedValueIsZero(Op.getOperand(3), Mask, TLI);
411 case ISD::SRL:
412 // (ushr X, C1) & C2 == 0 iff X & (C2 << C1) == 0
413 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
414 uint64_t NewVal = Mask << ShAmt->getValue();
415 SrcBits = MVT::getSizeInBits(Op.getValueType());
416 if (SrcBits != 64) NewVal &= (1ULL << SrcBits)-1;
417 return MaskedValueIsZero(Op.getOperand(0), NewVal, TLI);
418 }
419 return false;
420 case ISD::SHL:
421 // (ushl X, C1) & C2 == 0 iff X & (C2 >> C1) == 0
422 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
423 uint64_t NewVal = Mask >> ShAmt->getValue();
424 return MaskedValueIsZero(Op.getOperand(0), NewVal, TLI);
425 }
426 return false;
Chris Lattnerbba9aa32005-10-10 16:51:40 +0000427 case ISD::ADD:
Chris Lattnerd7390752005-10-10 16:52:03 +0000428 // (add X, Y) & C == 0 iff (X&C)|(Y&C) == 0 and all bits are low bits.
Chris Lattnerbba9aa32005-10-10 16:51:40 +0000429 if ((Mask&(Mask+1)) == 0) { // All low bits
430 if (MaskedValueIsZero(Op.getOperand(0), Mask, TLI) &&
431 MaskedValueIsZero(Op.getOperand(1), Mask, TLI))
432 return true;
433 }
434 break;
Chris Lattnerc4ced262005-10-07 15:30:32 +0000435 case ISD::SUB:
436 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
437 // We know that the top bits of C-X are clear if X contains less bits
438 // than C (i.e. no wrap-around can happen). For example, 20-X is
439 // positive if we can prove that X is >= 0 and < 16.
440 unsigned Bits = MVT::getSizeInBits(CLHS->getValueType(0));
441 if ((CLHS->getValue() & (1 << (Bits-1))) == 0) { // sign bit clear
442 unsigned NLZ = CountLeadingZeros_64(CLHS->getValue()+1);
443 uint64_t MaskV = (1ULL << (63-NLZ))-1;
444 if (MaskedValueIsZero(Op.getOperand(1), ~MaskV, TLI)) {
445 // High bits are clear this value is known to be >= C.
446 unsigned NLZ2 = CountLeadingZeros_64(CLHS->getValue());
447 if ((Mask & ((1ULL << (64-NLZ2))-1)) == 0)
448 return true;
449 }
450 }
451 }
452 break;
Nate Begeman4ebd8052005-09-01 23:24:04 +0000453 case ISD::CTTZ:
454 case ISD::CTLZ:
455 case ISD::CTPOP:
456 // Bit counting instructions can not set the high bits of the result
457 // register. The max number of bits sets depends on the input.
458 return (Mask & (MVT::getSizeInBits(Op.getValueType())*2-1)) == 0;
Evan Cheng3a03ebb2005-12-21 23:05:39 +0000459 default:
460 if (Op.getOpcode() >= ISD::BUILTIN_OP_END)
461 return TLI.isMaskedValueZeroForTargetNode(Op, Mask);
462 break;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000463 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000464 return false;
465}
466
Nate Begeman4ebd8052005-09-01 23:24:04 +0000467// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
468// that selects between the values 1 and 0, making it equivalent to a setcc.
Nate Begeman646d7e22005-09-02 21:18:40 +0000469// Also, set the incoming LHS, RHS, and CC references to the appropriate
470// nodes based on the type of node we are checking. This simplifies life a
471// bit for the callers.
472static bool isSetCCEquivalent(SDOperand N, SDOperand &LHS, SDOperand &RHS,
473 SDOperand &CC) {
474 if (N.getOpcode() == ISD::SETCC) {
475 LHS = N.getOperand(0);
476 RHS = N.getOperand(1);
477 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000478 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000479 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000480 if (N.getOpcode() == ISD::SELECT_CC &&
481 N.getOperand(2).getOpcode() == ISD::Constant &&
482 N.getOperand(3).getOpcode() == ISD::Constant &&
483 cast<ConstantSDNode>(N.getOperand(2))->getValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000484 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
485 LHS = N.getOperand(0);
486 RHS = N.getOperand(1);
487 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000488 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000489 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000490 return false;
491}
492
Nate Begeman99801192005-09-07 23:25:52 +0000493// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
494// one use. If this is true, it allows the users to invert the operation for
495// free when it is profitable to do so.
496static bool isOneUseSetCC(SDOperand N) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000497 SDOperand N0, N1, N2;
Nate Begeman646d7e22005-09-02 21:18:40 +0000498 if (isSetCCEquivalent(N, N0, N1, N2) && N.Val->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000499 return true;
500 return false;
501}
502
Nate Begeman452d7be2005-09-16 00:54:12 +0000503// FIXME: This should probably go in the ISD class rather than being duplicated
504// in several files.
505static bool isCommutativeBinOp(unsigned Opcode) {
506 switch (Opcode) {
507 case ISD::ADD:
508 case ISD::MUL:
509 case ISD::AND:
510 case ISD::OR:
511 case ISD::XOR: return true;
512 default: return false; // FIXME: Need commutative info for user ops!
513 }
514}
515
Nate Begeman4ebd8052005-09-01 23:24:04 +0000516void DAGCombiner::Run(bool RunningAfterLegalize) {
517 // set the instance variable, so that the various visit routines may use it.
518 AfterLegalize = RunningAfterLegalize;
519
Nate Begeman646d7e22005-09-02 21:18:40 +0000520 // Add all the dag nodes to the worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000521 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
522 E = DAG.allnodes_end(); I != E; ++I)
523 WorkList.push_back(I);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000524
Chris Lattner95038592005-10-05 06:35:28 +0000525 // Create a dummy node (which is not added to allnodes), that adds a reference
526 // to the root node, preventing it from being deleted, and tracking any
527 // changes of the root.
528 HandleSDNode Dummy(DAG.getRoot());
529
Nate Begeman1d4d4142005-09-01 00:19:25 +0000530 // while the worklist isn't empty, inspect the node on the end of it and
531 // try and combine it.
532 while (!WorkList.empty()) {
533 SDNode *N = WorkList.back();
534 WorkList.pop_back();
535
536 // If N has no uses, it is dead. Make sure to revisit all N's operands once
Chris Lattner95038592005-10-05 06:35:28 +0000537 // N is deleted from the DAG, since they too may now be dead or may have a
538 // reduced number of uses, allowing other xforms.
539 if (N->use_empty() && N != &Dummy) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000540 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
541 WorkList.push_back(N->getOperand(i).Val);
542
Nate Begeman1d4d4142005-09-01 00:19:25 +0000543 removeFromWorkList(N);
Chris Lattner95038592005-10-05 06:35:28 +0000544 DAG.DeleteNode(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000545 continue;
546 }
547
Nate Begeman83e75ec2005-09-06 04:43:02 +0000548 SDOperand RV = visit(N);
549 if (RV.Val) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000550 ++NodesCombined;
Nate Begeman646d7e22005-09-02 21:18:40 +0000551 // If we get back the same node we passed in, rather than a new node or
552 // zero, we know that the node must have defined multiple values and
553 // CombineTo was used. Since CombineTo takes care of the worklist
554 // mechanics for us, we have no work to do in this case.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000555 if (RV.Val != N) {
Nate Begeman2300f552005-09-07 00:15:36 +0000556 DEBUG(std::cerr << "\nReplacing "; N->dump();
557 std::cerr << "\nWith: "; RV.Val->dump();
558 std::cerr << '\n');
Chris Lattner01a22022005-10-10 22:04:48 +0000559 std::vector<SDNode*> NowDead;
560 DAG.ReplaceAllUsesWith(N, std::vector<SDOperand>(1, RV), &NowDead);
Nate Begeman646d7e22005-09-02 21:18:40 +0000561
562 // Push the new node and any users onto the worklist
Nate Begeman83e75ec2005-09-06 04:43:02 +0000563 WorkList.push_back(RV.Val);
564 AddUsersToWorkList(RV.Val);
Nate Begeman646d7e22005-09-02 21:18:40 +0000565
566 // Nodes can end up on the worklist more than once. Make sure we do
567 // not process a node that has been replaced.
568 removeFromWorkList(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000569 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
570 removeFromWorkList(NowDead[i]);
Chris Lattner5c46f742005-10-05 06:11:08 +0000571
572 // Finally, since the node is now dead, remove it from the graph.
573 DAG.DeleteNode(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000574 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000575 }
576 }
Chris Lattner95038592005-10-05 06:35:28 +0000577
578 // If the root changed (e.g. it was a dead load, update the root).
579 DAG.setRoot(Dummy.getValue());
Nate Begeman1d4d4142005-09-01 00:19:25 +0000580}
581
Nate Begeman83e75ec2005-09-06 04:43:02 +0000582SDOperand DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000583 switch(N->getOpcode()) {
584 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +0000585 case ISD::TokenFactor: return visitTokenFactor(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000586 case ISD::ADD: return visitADD(N);
587 case ISD::SUB: return visitSUB(N);
588 case ISD::MUL: return visitMUL(N);
589 case ISD::SDIV: return visitSDIV(N);
590 case ISD::UDIV: return visitUDIV(N);
591 case ISD::SREM: return visitSREM(N);
592 case ISD::UREM: return visitUREM(N);
593 case ISD::MULHU: return visitMULHU(N);
594 case ISD::MULHS: return visitMULHS(N);
595 case ISD::AND: return visitAND(N);
596 case ISD::OR: return visitOR(N);
597 case ISD::XOR: return visitXOR(N);
598 case ISD::SHL: return visitSHL(N);
599 case ISD::SRA: return visitSRA(N);
600 case ISD::SRL: return visitSRL(N);
601 case ISD::CTLZ: return visitCTLZ(N);
602 case ISD::CTTZ: return visitCTTZ(N);
603 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000604 case ISD::SELECT: return visitSELECT(N);
605 case ISD::SELECT_CC: return visitSELECT_CC(N);
606 case ISD::SETCC: return visitSETCC(N);
Nate Begeman5054f162005-10-14 01:12:21 +0000607 case ISD::ADD_PARTS: return visitADD_PARTS(N);
608 case ISD::SUB_PARTS: return visitSUB_PARTS(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000609 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
610 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
611 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
612 case ISD::TRUNCATE: return visitTRUNCATE(N);
Chris Lattner94683772005-12-23 05:30:37 +0000613 case ISD::BIT_CONVERT: return visitBIT_CONVERT(N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000614 case ISD::FADD: return visitFADD(N);
615 case ISD::FSUB: return visitFSUB(N);
616 case ISD::FMUL: return visitFMUL(N);
617 case ISD::FDIV: return visitFDIV(N);
618 case ISD::FREM: return visitFREM(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000619 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
620 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
621 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
622 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
623 case ISD::FP_ROUND: return visitFP_ROUND(N);
624 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
625 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
626 case ISD::FNEG: return visitFNEG(N);
627 case ISD::FABS: return visitFABS(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000628 case ISD::BRCOND: return visitBRCOND(N);
629 case ISD::BRCONDTWOWAY: return visitBRCONDTWOWAY(N);
630 case ISD::BR_CC: return visitBR_CC(N);
631 case ISD::BRTWOWAY_CC: return visitBRTWOWAY_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000632 case ISD::LOAD: return visitLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000633 case ISD::STORE: return visitSTORE(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000634 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000635 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000636}
637
Nate Begeman83e75ec2005-09-06 04:43:02 +0000638SDOperand DAGCombiner::visitTokenFactor(SDNode *N) {
Nate Begemanded49632005-10-13 03:11:28 +0000639 std::vector<SDOperand> Ops;
640 bool Changed = false;
641
Nate Begeman1d4d4142005-09-01 00:19:25 +0000642 // If the token factor has two operands and one is the entry token, replace
643 // the token factor with the other operand.
644 if (N->getNumOperands() == 2) {
645 if (N->getOperand(0).getOpcode() == ISD::EntryToken)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000646 return N->getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000647 if (N->getOperand(1).getOpcode() == ISD::EntryToken)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000648 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000649 }
Chris Lattner24edbb72005-10-13 22:10:05 +0000650
Nate Begemanded49632005-10-13 03:11:28 +0000651 // fold (tokenfactor (tokenfactor)) -> tokenfactor
652 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
653 SDOperand Op = N->getOperand(i);
654 if (Op.getOpcode() == ISD::TokenFactor && Op.hasOneUse()) {
655 Changed = true;
656 for (unsigned j = 0, e = Op.getNumOperands(); j != e; ++j)
657 Ops.push_back(Op.getOperand(j));
658 } else {
659 Ops.push_back(Op);
660 }
661 }
662 if (Changed)
663 return DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000664 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000665}
666
Nate Begeman83e75ec2005-09-06 04:43:02 +0000667SDOperand DAGCombiner::visitADD(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000668 SDOperand N0 = N->getOperand(0);
669 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000670 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
671 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemanf89d78d2005-09-07 16:09:19 +0000672 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000673
674 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000675 if (N0C && N1C)
Nate Begemanf89d78d2005-09-07 16:09:19 +0000676 return DAG.getConstant(N0C->getValue() + N1C->getValue(), VT);
Nate Begeman99801192005-09-07 23:25:52 +0000677 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000678 if (N0C && !N1C)
679 return DAG.getNode(ISD::ADD, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000680 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000681 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000682 return N0;
Nate Begeman223df222005-09-08 20:18:10 +0000683 // fold (add (add x, c1), c2) -> (add x, c1+c2)
684 if (N1C && N0.getOpcode() == ISD::ADD) {
685 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
686 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
687 if (N00C)
688 return DAG.getNode(ISD::ADD, VT, N0.getOperand(1),
689 DAG.getConstant(N1C->getValue()+N00C->getValue(), VT));
690 if (N01C)
691 return DAG.getNode(ISD::ADD, VT, N0.getOperand(0),
692 DAG.getConstant(N1C->getValue()+N01C->getValue(), VT));
693 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000694 // fold ((0-A) + B) -> B-A
695 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
696 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000697 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000698 // fold (A + (0-B)) -> A-B
699 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
700 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000701 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +0000702 // fold (A+(B-A)) -> B
703 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000704 return N1.getOperand(0);
705 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000706}
707
Nate Begeman83e75ec2005-09-06 04:43:02 +0000708SDOperand DAGCombiner::visitSUB(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000709 SDOperand N0 = N->getOperand(0);
710 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000711 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
712 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000713
Chris Lattner854077d2005-10-17 01:07:11 +0000714 // fold (sub x, x) -> 0
715 if (N0 == N1)
716 return DAG.getConstant(0, N->getValueType(0));
717
Nate Begeman1d4d4142005-09-01 00:19:25 +0000718 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000719 if (N0C && N1C)
720 return DAG.getConstant(N0C->getValue() - N1C->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000721 N->getValueType(0));
Chris Lattner05b57432005-10-11 06:07:15 +0000722 // fold (sub x, c) -> (add x, -c)
723 if (N1C)
724 return DAG.getNode(ISD::ADD, N0.getValueType(), N0,
725 DAG.getConstant(-N1C->getValue(), N0.getValueType()));
726
Nate Begeman1d4d4142005-09-01 00:19:25 +0000727 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +0000728 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000729 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000730 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +0000731 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000732 return N0.getOperand(0);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000733 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000734}
735
Nate Begeman83e75ec2005-09-06 04:43:02 +0000736SDOperand DAGCombiner::visitMUL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000737 SDOperand N0 = N->getOperand(0);
738 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000739 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
740 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman223df222005-09-08 20:18:10 +0000741 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000742
743 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000744 if (N0C && N1C)
Chris Lattner3e6099b2005-10-30 06:41:49 +0000745 return DAG.getConstant(N0C->getValue() * N1C->getValue(), VT);
Nate Begeman99801192005-09-07 23:25:52 +0000746 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000747 if (N0C && !N1C)
748 return DAG.getNode(ISD::MUL, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000749 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000750 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000751 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000752 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +0000753 if (N1C && N1C->isAllOnesValue())
Nate Begeman405e3ec2005-10-21 00:02:42 +0000754 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000755 // fold (mul x, (1 << c)) -> x << c
Nate Begeman646d7e22005-09-02 21:18:40 +0000756 if (N1C && isPowerOf2_64(N1C->getValue()))
Chris Lattner3e6099b2005-10-30 06:41:49 +0000757 return DAG.getNode(ISD::SHL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000758 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000759 TLI.getShiftAmountTy()));
Chris Lattner3e6099b2005-10-30 06:41:49 +0000760 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
761 if (N1C && isPowerOf2_64(-N1C->getSignExtended())) {
762 // FIXME: If the input is something that is easily negated (e.g. a
763 // single-use add), we should put the negate there.
764 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT),
765 DAG.getNode(ISD::SHL, VT, N0,
766 DAG.getConstant(Log2_64(-N1C->getSignExtended()),
767 TLI.getShiftAmountTy())));
768 }
769
770
Nate Begeman223df222005-09-08 20:18:10 +0000771 // fold (mul (mul x, c1), c2) -> (mul x, c1*c2)
772 if (N1C && N0.getOpcode() == ISD::MUL) {
773 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
774 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
775 if (N00C)
776 return DAG.getNode(ISD::MUL, VT, N0.getOperand(1),
777 DAG.getConstant(N1C->getValue()*N00C->getValue(), VT));
778 if (N01C)
779 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0),
780 DAG.getConstant(N1C->getValue()*N01C->getValue(), VT));
781 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000782 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000783}
784
Nate Begeman83e75ec2005-09-06 04:43:02 +0000785SDOperand DAGCombiner::visitSDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000786 SDOperand N0 = N->getOperand(0);
787 SDOperand N1 = N->getOperand(1);
Chris Lattner094c8fc2005-10-07 06:10:46 +0000788 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +0000789 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
790 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000791
792 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000793 if (N0C && N1C && !N1C->isNullValue())
794 return DAG.getConstant(N0C->getSignExtended() / N1C->getSignExtended(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000795 N->getValueType(0));
Nate Begeman405e3ec2005-10-21 00:02:42 +0000796 // fold (sdiv X, 1) -> X
797 if (N1C && N1C->getSignExtended() == 1LL)
798 return N0;
799 // fold (sdiv X, -1) -> 0-X
800 if (N1C && N1C->isAllOnesValue())
801 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +0000802 // If we know the sign bits of both operands are zero, strength reduce to a
803 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
804 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
805 if (MaskedValueIsZero(N1, SignBit, TLI) &&
806 MaskedValueIsZero(N0, SignBit, TLI))
807 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
Nate Begeman405e3ec2005-10-21 00:02:42 +0000808 // fold (sdiv X, pow2) -> (add (sra X, log(pow2)), (srl X, sizeof(X)-1))
809 if (N1C && N1C->getValue() && !TLI.isIntDivCheap() &&
810 (isPowerOf2_64(N1C->getSignExtended()) ||
811 isPowerOf2_64(-N1C->getSignExtended()))) {
812 // If dividing by powers of two is cheap, then don't perform the following
813 // fold.
814 if (TLI.isPow2DivCheap())
815 return SDOperand();
816 int64_t pow2 = N1C->getSignExtended();
817 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
818 SDOperand SRL = DAG.getNode(ISD::SRL, VT, N0,
819 DAG.getConstant(MVT::getSizeInBits(VT)-1,
820 TLI.getShiftAmountTy()));
821 WorkList.push_back(SRL.Val);
822 SDOperand SGN = DAG.getNode(ISD::ADD, VT, N0, SRL);
823 WorkList.push_back(SGN.Val);
824 SDOperand SRA = DAG.getNode(ISD::SRA, VT, SGN,
825 DAG.getConstant(Log2_64(abs2),
826 TLI.getShiftAmountTy()));
827 // If we're dividing by a positive value, we're done. Otherwise, we must
828 // negate the result.
829 if (pow2 > 0)
830 return SRA;
831 WorkList.push_back(SRA.Val);
832 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), SRA);
833 }
Nate Begeman69575232005-10-20 02:15:44 +0000834 // if integer divide is expensive and we satisfy the requirements, emit an
835 // alternate sequence.
Nate Begeman405e3ec2005-10-21 00:02:42 +0000836 if (N1C && (N1C->getSignExtended() < -1 || N1C->getSignExtended() > 1) &&
Chris Lattnere9936d12005-10-22 18:50:15 +0000837 !TLI.isIntDivCheap()) {
838 SDOperand Op = BuildSDIV(N);
839 if (Op.Val) return Op;
Nate Begeman69575232005-10-20 02:15:44 +0000840 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000841 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000842}
843
Nate Begeman83e75ec2005-09-06 04:43:02 +0000844SDOperand DAGCombiner::visitUDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000845 SDOperand N0 = N->getOperand(0);
846 SDOperand N1 = N->getOperand(1);
Nate Begeman69575232005-10-20 02:15:44 +0000847 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +0000848 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
849 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000850
851 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000852 if (N0C && N1C && !N1C->isNullValue())
853 return DAG.getConstant(N0C->getValue() / N1C->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000854 N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000855 // fold (udiv x, (1 << c)) -> x >>u c
Nate Begeman646d7e22005-09-02 21:18:40 +0000856 if (N1C && isPowerOf2_64(N1C->getValue()))
Nate Begeman1d4d4142005-09-01 00:19:25 +0000857 return DAG.getNode(ISD::SRL, N->getValueType(0), N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000858 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000859 TLI.getShiftAmountTy()));
Nate Begeman69575232005-10-20 02:15:44 +0000860 // fold (udiv x, c) -> alternate
Chris Lattnere9936d12005-10-22 18:50:15 +0000861 if (N1C && N1C->getValue() && !TLI.isIntDivCheap()) {
862 SDOperand Op = BuildUDIV(N);
863 if (Op.Val) return Op;
864 }
865
Nate Begeman83e75ec2005-09-06 04:43:02 +0000866 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000867}
868
Nate Begeman83e75ec2005-09-06 04:43:02 +0000869SDOperand DAGCombiner::visitSREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000870 SDOperand N0 = N->getOperand(0);
871 SDOperand N1 = N->getOperand(1);
Nate Begeman07ed4172005-10-10 21:26:48 +0000872 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +0000873 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
874 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000875
876 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000877 if (N0C && N1C && !N1C->isNullValue())
878 return DAG.getConstant(N0C->getSignExtended() % N1C->getSignExtended(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000879 N->getValueType(0));
Nate Begeman07ed4172005-10-10 21:26:48 +0000880 // If we know the sign bits of both operands are zero, strength reduce to a
881 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
882 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
883 if (MaskedValueIsZero(N1, SignBit, TLI) &&
884 MaskedValueIsZero(N0, SignBit, TLI))
885 return DAG.getNode(ISD::UREM, N1.getValueType(), N0, N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000886 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000887}
888
Nate Begeman83e75ec2005-09-06 04:43:02 +0000889SDOperand DAGCombiner::visitUREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000890 SDOperand N0 = N->getOperand(0);
891 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000892 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
893 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000894
895 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000896 if (N0C && N1C && !N1C->isNullValue())
897 return DAG.getConstant(N0C->getValue() % N1C->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000898 N->getValueType(0));
Nate Begeman07ed4172005-10-10 21:26:48 +0000899 // fold (urem x, pow2) -> (and x, pow2-1)
900 if (N1C && !N1C->isNullValue() && isPowerOf2_64(N1C->getValue()))
901 return DAG.getNode(ISD::AND, N0.getValueType(), N0,
902 DAG.getConstant(N1C->getValue()-1, N1.getValueType()));
Nate Begeman83e75ec2005-09-06 04:43:02 +0000903 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000904}
905
Nate Begeman83e75ec2005-09-06 04:43:02 +0000906SDOperand DAGCombiner::visitMULHS(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000907 SDOperand N0 = N->getOperand(0);
908 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000909 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000910
911 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000912 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000913 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000914 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Nate Begeman646d7e22005-09-02 21:18:40 +0000915 if (N1C && N1C->getValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +0000916 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
917 DAG.getConstant(MVT::getSizeInBits(N0.getValueType())-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +0000918 TLI.getShiftAmountTy()));
919 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000920}
921
Nate Begeman83e75ec2005-09-06 04:43:02 +0000922SDOperand DAGCombiner::visitMULHU(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000923 SDOperand N0 = N->getOperand(0);
924 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000925 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000926
927 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000928 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000929 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000930 // fold (mulhu x, 1) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000931 if (N1C && N1C->getValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000932 return DAG.getConstant(0, N0.getValueType());
933 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000934}
935
Nate Begeman83e75ec2005-09-06 04:43:02 +0000936SDOperand DAGCombiner::visitAND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000937 SDOperand N0 = N->getOperand(0);
938 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000939 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +0000940 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
941 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000942 MVT::ValueType VT = N1.getValueType();
Nate Begeman83e75ec2005-09-06 04:43:02 +0000943 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000944
945 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000946 if (N0C && N1C)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000947 return DAG.getConstant(N0C->getValue() & N1C->getValue(), VT);
Nate Begeman99801192005-09-07 23:25:52 +0000948 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000949 if (N0C && !N1C)
950 return DAG.getNode(ISD::AND, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000951 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000952 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000953 return N0;
954 // if (and x, c) is known to be zero, return 0
955 if (N1C && MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits),TLI))
956 return DAG.getConstant(0, VT);
957 // fold (and x, c) -> x iff (x & ~c) == 0
958 if (N1C && MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits)),
959 TLI))
960 return N0;
Nate Begeman223df222005-09-08 20:18:10 +0000961 // fold (and (and x, c1), c2) -> (and x, c1^c2)
962 if (N1C && N0.getOpcode() == ISD::AND) {
963 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
964 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
965 if (N00C)
966 return DAG.getNode(ISD::AND, VT, N0.getOperand(1),
967 DAG.getConstant(N1C->getValue()&N00C->getValue(), VT));
968 if (N01C)
969 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
970 DAG.getConstant(N1C->getValue()&N01C->getValue(), VT));
971 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000972 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
Nate Begeman5dc7e862005-11-02 18:42:59 +0000973 if (N1C && N0.getOpcode() == ISD::SIGN_EXTEND_INREG) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000974 unsigned ExtendBits =
Jeff Cohen06d9b4a2005-11-12 00:59:01 +0000975 MVT::getSizeInBits(cast<VTSDNode>(N0.getOperand(1))->getVT());
976 if (ExtendBits == 64 || ((N1C->getValue() & (~0ULL << ExtendBits)) == 0))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000977 return DAG.getNode(ISD::AND, VT, N0.getOperand(0), N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000978 }
979 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
Nate Begeman5dc7e862005-11-02 18:42:59 +0000980 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +0000981 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Nate Begeman646d7e22005-09-02 21:18:40 +0000982 if ((ORI->getValue() & N1C->getValue()) == N1C->getValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000983 return N1;
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000984 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
985 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
986 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
987 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
988
989 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
990 MVT::isInteger(LL.getValueType())) {
991 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
992 if (cast<ConstantSDNode>(LR)->getValue() == 0 && Op1 == ISD::SETEQ) {
993 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
994 WorkList.push_back(ORNode.Val);
995 return DAG.getSetCC(VT, ORNode, LR, Op1);
996 }
997 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
998 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
999 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
1000 WorkList.push_back(ANDNode.Val);
1001 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1002 }
1003 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
1004 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
1005 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
1006 WorkList.push_back(ORNode.Val);
1007 return DAG.getSetCC(VT, ORNode, LR, Op1);
1008 }
1009 }
1010 // canonicalize equivalent to ll == rl
1011 if (LL == RR && LR == RL) {
1012 Op1 = ISD::getSetCCSwappedOperands(Op1);
1013 std::swap(RL, RR);
1014 }
1015 if (LL == RL && LR == RR) {
1016 bool isInteger = MVT::isInteger(LL.getValueType());
1017 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
1018 if (Result != ISD::SETCC_INVALID)
1019 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1020 }
1021 }
1022 // fold (and (zext x), (zext y)) -> (zext (and x, y))
1023 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
1024 N1.getOpcode() == ISD::ZERO_EXTEND &&
1025 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1026 SDOperand ANDNode = DAG.getNode(ISD::AND, N0.getOperand(0).getValueType(),
1027 N0.getOperand(0), N1.getOperand(0));
1028 WorkList.push_back(ANDNode.Val);
1029 return DAG.getNode(ISD::ZERO_EXTEND, VT, ANDNode);
1030 }
Nate Begeman452d7be2005-09-16 00:54:12 +00001031 // fold (and (shl/srl x), (shl/srl y)) -> (shl/srl (and x, y))
1032 if (((N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SHL) ||
1033 (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SRL)) &&
1034 N0.getOperand(1) == N1.getOperand(1)) {
1035 SDOperand ANDNode = DAG.getNode(ISD::AND, N0.getOperand(0).getValueType(),
1036 N0.getOperand(0), N1.getOperand(0));
1037 WorkList.push_back(ANDNode.Val);
1038 return DAG.getNode(N0.getOpcode(), VT, ANDNode, N0.getOperand(1));
1039 }
Chris Lattner85d63bb2005-10-15 22:18:08 +00001040 // fold (and (sra)) -> (and (srl)) when possible.
Nate Begeman5dc7e862005-11-02 18:42:59 +00001041 if (N0.getOpcode() == ISD::SRA && N0.Val->hasOneUse()) {
Chris Lattner85d63bb2005-10-15 22:18:08 +00001042 if (ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
1043 // If the RHS of the AND has zeros where the sign bits of the SRA will
1044 // land, turn the SRA into an SRL.
Chris Lattner750dbd52005-10-15 22:35:40 +00001045 if (MaskedValueIsZero(N1, (~0ULL << (OpSizeInBits-N01C->getValue())) &
Chris Lattner85d63bb2005-10-15 22:18:08 +00001046 (~0ULL>>(64-OpSizeInBits)), TLI)) {
1047 WorkList.push_back(N);
1048 CombineTo(N0.Val, DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
1049 N0.getOperand(1)));
1050 return SDOperand();
1051 }
1052 }
Nate Begeman5dc7e862005-11-02 18:42:59 +00001053 }
Nate Begemanded49632005-10-13 03:11:28 +00001054 // fold (zext_inreg (extload x)) -> (zextload x)
Nate Begeman5054f162005-10-14 01:12:21 +00001055 if (N0.getOpcode() == ISD::EXTLOAD) {
Nate Begemanded49632005-10-13 03:11:28 +00001056 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001057 // If we zero all the possible extended bits, then we can turn this into
1058 // a zextload if we are running before legalize or the operation is legal.
Nate Begeman5054f162005-10-14 01:12:21 +00001059 if (MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT), TLI) &&
Chris Lattner67a44cd2005-10-13 18:16:34 +00001060 (!AfterLegalize || TLI.isOperationLegal(ISD::ZEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001061 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1062 N0.getOperand(1), N0.getOperand(2),
1063 EVT);
Nate Begemanded49632005-10-13 03:11:28 +00001064 WorkList.push_back(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001065 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Nate Begemanded49632005-10-13 03:11:28 +00001066 return SDOperand();
1067 }
1068 }
1069 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Chris Lattner40c62d52005-10-18 06:04:22 +00001070 if (N0.getOpcode() == ISD::SEXTLOAD && N0.hasOneUse()) {
Nate Begemanded49632005-10-13 03:11:28 +00001071 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001072 // If we zero all the possible extended bits, then we can turn this into
1073 // a zextload if we are running before legalize or the operation is legal.
Nate Begeman5054f162005-10-14 01:12:21 +00001074 if (MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT), TLI) &&
Nate Begemanbfd65a02005-10-13 18:34:58 +00001075 (!AfterLegalize || TLI.isOperationLegal(ISD::ZEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001076 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1077 N0.getOperand(1), N0.getOperand(2),
1078 EVT);
Nate Begemanded49632005-10-13 03:11:28 +00001079 WorkList.push_back(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001080 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Nate Begemanded49632005-10-13 03:11:28 +00001081 return SDOperand();
1082 }
1083 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001084 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001085}
1086
Nate Begeman83e75ec2005-09-06 04:43:02 +00001087SDOperand DAGCombiner::visitOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001088 SDOperand N0 = N->getOperand(0);
1089 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001090 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001091 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1092 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001093 MVT::ValueType VT = N1.getValueType();
1094 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001095
1096 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001097 if (N0C && N1C)
1098 return DAG.getConstant(N0C->getValue() | N1C->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001099 N->getValueType(0));
Nate Begeman99801192005-09-07 23:25:52 +00001100 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001101 if (N0C && !N1C)
1102 return DAG.getNode(ISD::OR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001103 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001104 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001105 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001106 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001107 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001108 return N1;
1109 // fold (or x, c) -> c iff (x & ~c) == 0
1110 if (N1C && MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits)),
1111 TLI))
1112 return N1;
Nate Begeman223df222005-09-08 20:18:10 +00001113 // fold (or (or x, c1), c2) -> (or x, c1|c2)
1114 if (N1C && N0.getOpcode() == ISD::OR) {
1115 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
1116 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
1117 if (N00C)
1118 return DAG.getNode(ISD::OR, VT, N0.getOperand(1),
1119 DAG.getConstant(N1C->getValue()|N00C->getValue(), VT));
1120 if (N01C)
1121 return DAG.getNode(ISD::OR, VT, N0.getOperand(0),
1122 DAG.getConstant(N1C->getValue()|N01C->getValue(), VT));
Chris Lattner731d3482005-10-27 05:06:38 +00001123 } else if (N1C && N0.getOpcode() == ISD::AND && N0.Val->hasOneUse() &&
1124 isa<ConstantSDNode>(N0.getOperand(1))) {
1125 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
1126 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
1127 return DAG.getNode(ISD::AND, VT, DAG.getNode(ISD::OR, VT, N0.getOperand(0),
1128 N1),
1129 DAG.getConstant(N1C->getValue() | C1->getValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00001130 }
Chris Lattner731d3482005-10-27 05:06:38 +00001131
1132
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001133 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
1134 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1135 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1136 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1137
1138 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1139 MVT::isInteger(LL.getValueType())) {
1140 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
1141 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
1142 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
1143 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
1144 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
1145 WorkList.push_back(ORNode.Val);
1146 return DAG.getSetCC(VT, ORNode, LR, Op1);
1147 }
1148 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
1149 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
1150 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
1151 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
1152 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
1153 WorkList.push_back(ANDNode.Val);
1154 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1155 }
1156 }
1157 // canonicalize equivalent to ll == rl
1158 if (LL == RR && LR == RL) {
1159 Op1 = ISD::getSetCCSwappedOperands(Op1);
1160 std::swap(RL, RR);
1161 }
1162 if (LL == RL && LR == RR) {
1163 bool isInteger = MVT::isInteger(LL.getValueType());
1164 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
1165 if (Result != ISD::SETCC_INVALID)
1166 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1167 }
1168 }
1169 // fold (or (zext x), (zext y)) -> (zext (or x, y))
1170 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
1171 N1.getOpcode() == ISD::ZERO_EXTEND &&
1172 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1173 SDOperand ORNode = DAG.getNode(ISD::OR, N0.getOperand(0).getValueType(),
1174 N0.getOperand(0), N1.getOperand(0));
1175 WorkList.push_back(ORNode.Val);
1176 return DAG.getNode(ISD::ZERO_EXTEND, VT, ORNode);
1177 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001178 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001179}
1180
Nate Begeman83e75ec2005-09-06 04:43:02 +00001181SDOperand DAGCombiner::visitXOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001182 SDOperand N0 = N->getOperand(0);
1183 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001184 SDOperand LHS, RHS, CC;
1185 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1186 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001187 MVT::ValueType VT = N0.getValueType();
1188
1189 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001190 if (N0C && N1C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001191 return DAG.getConstant(N0C->getValue() ^ N1C->getValue(), VT);
Nate Begeman99801192005-09-07 23:25:52 +00001192 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001193 if (N0C && !N1C)
1194 return DAG.getNode(ISD::XOR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001195 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001196 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001197 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001198 // fold !(x cc y) -> (x !cc y)
Nate Begeman646d7e22005-09-02 21:18:40 +00001199 if (N1C && N1C->getValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
1200 bool isInt = MVT::isInteger(LHS.getValueType());
1201 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
1202 isInt);
1203 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001204 return DAG.getSetCC(VT, LHS, RHS, NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00001205 if (N0.getOpcode() == ISD::SELECT_CC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001206 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00001207 assert(0 && "Unhandled SetCC Equivalent!");
1208 abort();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001209 }
Nate Begeman99801192005-09-07 23:25:52 +00001210 // fold !(x or y) -> (!x and !y) iff x or y are setcc
1211 if (N1C && N1C->getValue() == 1 &&
1212 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001213 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00001214 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
1215 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001216 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1217 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Nate Begeman99801192005-09-07 23:25:52 +00001218 WorkList.push_back(LHS.Val); WorkList.push_back(RHS.Val);
1219 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001220 }
1221 }
Nate Begeman99801192005-09-07 23:25:52 +00001222 // fold !(x or y) -> (!x and !y) iff x or y are constants
1223 if (N1C && N1C->isAllOnesValue() &&
1224 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001225 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00001226 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
1227 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001228 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1229 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Nate Begeman99801192005-09-07 23:25:52 +00001230 WorkList.push_back(LHS.Val); WorkList.push_back(RHS.Val);
1231 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001232 }
1233 }
Nate Begeman223df222005-09-08 20:18:10 +00001234 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
1235 if (N1C && N0.getOpcode() == ISD::XOR) {
1236 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
1237 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
1238 if (N00C)
1239 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
1240 DAG.getConstant(N1C->getValue()^N00C->getValue(), VT));
1241 if (N01C)
1242 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
1243 DAG.getConstant(N1C->getValue()^N01C->getValue(), VT));
1244 }
1245 // fold (xor x, x) -> 0
1246 if (N0 == N1)
1247 return DAG.getConstant(0, VT);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001248 // fold (xor (zext x), (zext y)) -> (zext (xor x, y))
1249 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
1250 N1.getOpcode() == ISD::ZERO_EXTEND &&
1251 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1252 SDOperand XORNode = DAG.getNode(ISD::XOR, N0.getOperand(0).getValueType(),
1253 N0.getOperand(0), N1.getOperand(0));
1254 WorkList.push_back(XORNode.Val);
1255 return DAG.getNode(ISD::ZERO_EXTEND, VT, XORNode);
1256 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001257 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001258}
1259
Nate Begeman83e75ec2005-09-06 04:43:02 +00001260SDOperand DAGCombiner::visitSHL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001261 SDOperand N0 = N->getOperand(0);
1262 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001263 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1264 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001265 MVT::ValueType VT = N0.getValueType();
1266 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1267
1268 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001269 if (N0C && N1C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001270 return DAG.getConstant(N0C->getValue() << N1C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001271 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001272 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001273 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001274 // fold (shl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001275 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001276 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001277 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001278 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001279 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001280 // if (shl x, c) is known to be zero, return 0
Nate Begeman83e75ec2005-09-06 04:43:02 +00001281 if (N1C && MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits),TLI))
1282 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001283 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001284 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001285 N0.getOperand(1).getOpcode() == ISD::Constant) {
1286 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001287 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001288 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001289 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001290 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001291 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001292 }
1293 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
1294 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001295 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001296 N0.getOperand(1).getOpcode() == ISD::Constant) {
1297 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001298 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001299 SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
1300 DAG.getConstant(~0ULL << c1, VT));
1301 if (c2 > c1)
1302 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001303 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001304 else
Nate Begeman83e75ec2005-09-06 04:43:02 +00001305 return DAG.getNode(ISD::SRL, VT, Mask,
1306 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001307 }
1308 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001309 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +00001310 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001311 DAG.getConstant(~0ULL << N1C->getValue(), VT));
1312 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001313}
1314
Nate Begeman83e75ec2005-09-06 04:43:02 +00001315SDOperand DAGCombiner::visitSRA(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001316 SDOperand N0 = N->getOperand(0);
1317 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001318 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1319 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001320 MVT::ValueType VT = N0.getValueType();
1321 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1322
1323 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001324 if (N0C && N1C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001325 return DAG.getConstant(N0C->getSignExtended() >> N1C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001326 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001327 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001328 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001329 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001330 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001331 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001332 // fold (sra x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001333 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001334 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001335 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001336 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001337 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001338 // If the sign bit is known to be zero, switch this to a SRL.
Nate Begeman3df4d522005-10-12 20:40:40 +00001339 if (MaskedValueIsZero(N0, (1ULL << (OpSizeInBits-1)), TLI))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001340 return DAG.getNode(ISD::SRL, VT, N0, N1);
1341 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001342}
1343
Nate Begeman83e75ec2005-09-06 04:43:02 +00001344SDOperand DAGCombiner::visitSRL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001345 SDOperand N0 = N->getOperand(0);
1346 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001347 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1348 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001349 MVT::ValueType VT = N0.getValueType();
1350 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1351
1352 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001353 if (N0C && N1C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001354 return DAG.getConstant(N0C->getValue() >> N1C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001355 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001356 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001357 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001358 // fold (srl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001359 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001360 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001361 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001362 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001363 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001364 // if (srl x, c) is known to be zero, return 0
Nate Begeman83e75ec2005-09-06 04:43:02 +00001365 if (N1C && MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits),TLI))
1366 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001367 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001368 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001369 N0.getOperand(1).getOpcode() == ISD::Constant) {
1370 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001371 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001372 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001373 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001374 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001375 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001376 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001377 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001378}
1379
Nate Begeman83e75ec2005-09-06 04:43:02 +00001380SDOperand DAGCombiner::visitCTLZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001381 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001382 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001383
1384 // fold (ctlz c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001385 if (N0C)
1386 return DAG.getConstant(CountLeadingZeros_64(N0C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001387 N0.getValueType());
1388 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001389}
1390
Nate Begeman83e75ec2005-09-06 04:43:02 +00001391SDOperand DAGCombiner::visitCTTZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001392 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001393 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001394
1395 // fold (cttz c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001396 if (N0C)
1397 return DAG.getConstant(CountTrailingZeros_64(N0C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001398 N0.getValueType());
1399 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001400}
1401
Nate Begeman83e75ec2005-09-06 04:43:02 +00001402SDOperand DAGCombiner::visitCTPOP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001403 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001404 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001405
1406 // fold (ctpop c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001407 if (N0C)
1408 return DAG.getConstant(CountPopulation_64(N0C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001409 N0.getValueType());
1410 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001411}
1412
Nate Begeman452d7be2005-09-16 00:54:12 +00001413SDOperand DAGCombiner::visitSELECT(SDNode *N) {
1414 SDOperand N0 = N->getOperand(0);
1415 SDOperand N1 = N->getOperand(1);
1416 SDOperand N2 = N->getOperand(2);
1417 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1418 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1419 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1420 MVT::ValueType VT = N->getValueType(0);
Nate Begeman44728a72005-09-19 22:34:01 +00001421
Nate Begeman452d7be2005-09-16 00:54:12 +00001422 // fold select C, X, X -> X
1423 if (N1 == N2)
1424 return N1;
1425 // fold select true, X, Y -> X
1426 if (N0C && !N0C->isNullValue())
1427 return N1;
1428 // fold select false, X, Y -> Y
1429 if (N0C && N0C->isNullValue())
1430 return N2;
1431 // fold select C, 1, X -> C | X
Nate Begeman44728a72005-09-19 22:34:01 +00001432 if (MVT::i1 == VT && N1C && N1C->getValue() == 1)
Nate Begeman452d7be2005-09-16 00:54:12 +00001433 return DAG.getNode(ISD::OR, VT, N0, N2);
1434 // fold select C, 0, X -> ~C & X
1435 // FIXME: this should check for C type == X type, not i1?
1436 if (MVT::i1 == VT && N1C && N1C->isNullValue()) {
1437 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
1438 WorkList.push_back(XORNode.Val);
1439 return DAG.getNode(ISD::AND, VT, XORNode, N2);
1440 }
1441 // fold select C, X, 1 -> ~C | X
Nate Begeman44728a72005-09-19 22:34:01 +00001442 if (MVT::i1 == VT && N2C && N2C->getValue() == 1) {
Nate Begeman452d7be2005-09-16 00:54:12 +00001443 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
1444 WorkList.push_back(XORNode.Val);
1445 return DAG.getNode(ISD::OR, VT, XORNode, N1);
1446 }
1447 // fold select C, X, 0 -> C & X
1448 // FIXME: this should check for C type == X type, not i1?
1449 if (MVT::i1 == VT && N2C && N2C->isNullValue())
1450 return DAG.getNode(ISD::AND, VT, N0, N1);
1451 // fold X ? X : Y --> X ? 1 : Y --> X | Y
1452 if (MVT::i1 == VT && N0 == N1)
1453 return DAG.getNode(ISD::OR, VT, N0, N2);
1454 // fold X ? Y : X --> X ? Y : 0 --> X & Y
1455 if (MVT::i1 == VT && N0 == N2)
1456 return DAG.getNode(ISD::AND, VT, N0, N1);
Chris Lattner40c62d52005-10-18 06:04:22 +00001457
1458 // If we can fold this based on the true/false value, do so.
1459 if (SimplifySelectOps(N, N1, N2))
1460 return SDOperand();
1461
Nate Begeman44728a72005-09-19 22:34:01 +00001462 // fold selects based on a setcc into other things, such as min/max/abs
1463 if (N0.getOpcode() == ISD::SETCC)
1464 return SimplifySelect(N0, N1, N2);
Nate Begeman452d7be2005-09-16 00:54:12 +00001465 return SDOperand();
1466}
1467
1468SDOperand DAGCombiner::visitSELECT_CC(SDNode *N) {
Nate Begeman44728a72005-09-19 22:34:01 +00001469 SDOperand N0 = N->getOperand(0);
1470 SDOperand N1 = N->getOperand(1);
1471 SDOperand N2 = N->getOperand(2);
1472 SDOperand N3 = N->getOperand(3);
1473 SDOperand N4 = N->getOperand(4);
1474 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1475 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1476 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1477 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
1478
1479 // Determine if the condition we're dealing with is constant
Nate Begemane17daeb2005-10-05 21:43:42 +00001480 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
Chris Lattner91559022005-10-05 04:45:43 +00001481 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
1482
Nate Begeman44728a72005-09-19 22:34:01 +00001483 // fold select_cc lhs, rhs, x, x, cc -> x
1484 if (N2 == N3)
1485 return N2;
Chris Lattner40c62d52005-10-18 06:04:22 +00001486
1487 // If we can fold this based on the true/false value, do so.
1488 if (SimplifySelectOps(N, N2, N3))
1489 return SDOperand();
1490
Nate Begeman44728a72005-09-19 22:34:01 +00001491 // fold select_cc into other things, such as min/max/abs
1492 return SimplifySelectCC(N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00001493}
1494
1495SDOperand DAGCombiner::visitSETCC(SDNode *N) {
1496 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
1497 cast<CondCodeSDNode>(N->getOperand(2))->get());
1498}
1499
Nate Begeman5054f162005-10-14 01:12:21 +00001500SDOperand DAGCombiner::visitADD_PARTS(SDNode *N) {
1501 SDOperand LHSLo = N->getOperand(0);
1502 SDOperand RHSLo = N->getOperand(2);
1503 MVT::ValueType VT = LHSLo.getValueType();
1504
1505 // fold (a_Hi, 0) + (b_Hi, b_Lo) -> (b_Hi + a_Hi, b_Lo)
1506 if (MaskedValueIsZero(LHSLo, (1ULL << MVT::getSizeInBits(VT))-1, TLI)) {
1507 SDOperand Hi = DAG.getNode(ISD::ADD, VT, N->getOperand(1),
1508 N->getOperand(3));
1509 WorkList.push_back(Hi.Val);
1510 CombineTo(N, RHSLo, Hi);
1511 return SDOperand();
1512 }
1513 // fold (a_Hi, a_Lo) + (b_Hi, 0) -> (a_Hi + b_Hi, a_Lo)
1514 if (MaskedValueIsZero(RHSLo, (1ULL << MVT::getSizeInBits(VT))-1, TLI)) {
1515 SDOperand Hi = DAG.getNode(ISD::ADD, VT, N->getOperand(1),
1516 N->getOperand(3));
1517 WorkList.push_back(Hi.Val);
1518 CombineTo(N, LHSLo, Hi);
1519 return SDOperand();
1520 }
1521 return SDOperand();
1522}
1523
1524SDOperand DAGCombiner::visitSUB_PARTS(SDNode *N) {
1525 SDOperand LHSLo = N->getOperand(0);
1526 SDOperand RHSLo = N->getOperand(2);
1527 MVT::ValueType VT = LHSLo.getValueType();
1528
1529 // fold (a_Hi, a_Lo) - (b_Hi, 0) -> (a_Hi - b_Hi, a_Lo)
1530 if (MaskedValueIsZero(RHSLo, (1ULL << MVT::getSizeInBits(VT))-1, TLI)) {
1531 SDOperand Hi = DAG.getNode(ISD::SUB, VT, N->getOperand(1),
1532 N->getOperand(3));
1533 WorkList.push_back(Hi.Val);
1534 CombineTo(N, LHSLo, Hi);
1535 return SDOperand();
1536 }
1537 return SDOperand();
1538}
1539
Nate Begeman83e75ec2005-09-06 04:43:02 +00001540SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001541 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001542 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001543 MVT::ValueType VT = N->getValueType(0);
1544
Nate Begeman1d4d4142005-09-01 00:19:25 +00001545 // fold (sext c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001546 if (N0C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001547 return DAG.getConstant(N0C->getSignExtended(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001548 // fold (sext (sext x)) -> (sext x)
1549 if (N0.getOpcode() == ISD::SIGN_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001550 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
Chris Lattnerb14ab8a2005-12-07 07:11:03 +00001551 // fold (sext (truncate x)) -> (sextinreg x) iff x size == sext size.
Chris Lattnercc2210b2005-12-07 18:02:05 +00001552 if (N0.getOpcode() == ISD::TRUNCATE && N0.getOperand(0).getValueType() == VT&&
1553 (!AfterLegalize ||
1554 TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, N0.getValueType())))
Chris Lattnerb14ab8a2005-12-07 07:11:03 +00001555 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
1556 DAG.getValueType(N0.getValueType()));
Evan Cheng110dec22005-12-14 02:19:23 +00001557 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Chris Lattnerd0f6d182005-12-15 19:02:38 +00001558 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() &&
1559 (!AfterLegalize||TLI.isOperationLegal(ISD::SEXTLOAD, N0.getValueType()))){
Nate Begeman3df4d522005-10-12 20:40:40 +00001560 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1561 N0.getOperand(1), N0.getOperand(2),
1562 N0.getValueType());
Chris Lattnerd4771842005-12-14 19:25:30 +00001563 CombineTo(N, ExtLoad);
Chris Lattnerf9884052005-10-13 21:52:31 +00001564 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1565 ExtLoad.getValue(1));
Nate Begeman765784a2005-10-12 23:18:53 +00001566 return SDOperand();
Nate Begeman3df4d522005-10-12 20:40:40 +00001567 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001568
1569 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
1570 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
1571 if ((N0.getOpcode() == ISD::SEXTLOAD || N0.getOpcode() == ISD::EXTLOAD) &&
1572 N0.hasOneUse()) {
1573 SDOperand ExtLoad = DAG.getNode(ISD::SEXTLOAD, VT, N0.getOperand(0),
1574 N0.getOperand(1), N0.getOperand(2),
1575 N0.getOperand(3));
Chris Lattnerd4771842005-12-14 19:25:30 +00001576 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001577 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1578 ExtLoad.getValue(1));
1579 return SDOperand();
1580 }
1581
Nate Begeman83e75ec2005-09-06 04:43:02 +00001582 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001583}
1584
Nate Begeman83e75ec2005-09-06 04:43:02 +00001585SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001586 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001587 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001588 MVT::ValueType VT = N->getValueType(0);
1589
Nate Begeman1d4d4142005-09-01 00:19:25 +00001590 // fold (zext c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001591 if (N0C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001592 return DAG.getConstant(N0C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001593 // fold (zext (zext x)) -> (zext x)
1594 if (N0.getOpcode() == ISD::ZERO_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001595 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
Evan Cheng110dec22005-12-14 02:19:23 +00001596 // fold (zext (truncate x)) -> (zextinreg x) iff x size == zext size.
1597 if (N0.getOpcode() == ISD::TRUNCATE && N0.getOperand(0).getValueType() == VT&&
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001598 (!AfterLegalize || TLI.isOperationLegal(ISD::AND, N0.getValueType())))
Chris Lattner00cb95c2005-12-14 07:58:38 +00001599 return DAG.getZeroExtendInReg(N0.getOperand(0), N0.getValueType());
Evan Cheng110dec22005-12-14 02:19:23 +00001600 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Chris Lattnerd0f6d182005-12-15 19:02:38 +00001601 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() &&
1602 (!AfterLegalize||TLI.isOperationLegal(ISD::ZEXTLOAD, N0.getValueType()))){
Evan Cheng110dec22005-12-14 02:19:23 +00001603 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1604 N0.getOperand(1), N0.getOperand(2),
1605 N0.getValueType());
Chris Lattnerd4771842005-12-14 19:25:30 +00001606 CombineTo(N, ExtLoad);
Evan Cheng110dec22005-12-14 02:19:23 +00001607 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1608 ExtLoad.getValue(1));
1609 return SDOperand();
1610 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001611
1612 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
1613 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
1614 if ((N0.getOpcode() == ISD::ZEXTLOAD || N0.getOpcode() == ISD::EXTLOAD) &&
1615 N0.hasOneUse()) {
1616 SDOperand ExtLoad = DAG.getNode(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1617 N0.getOperand(1), N0.getOperand(2),
1618 N0.getOperand(3));
Chris Lattnerd4771842005-12-14 19:25:30 +00001619 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001620 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1621 ExtLoad.getValue(1));
1622 return SDOperand();
1623 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001624 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001625}
1626
Nate Begeman83e75ec2005-09-06 04:43:02 +00001627SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001628 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001629 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001630 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001631 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001632 MVT::ValueType EVT = cast<VTSDNode>(N1)->getVT();
Nate Begeman07ed4172005-10-10 21:26:48 +00001633 unsigned EVTBits = MVT::getSizeInBits(EVT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001634
Nate Begeman1d4d4142005-09-01 00:19:25 +00001635 // fold (sext_in_reg c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001636 if (N0C) {
1637 SDOperand Truncate = DAG.getConstant(N0C->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001638 return DAG.getNode(ISD::SIGN_EXTEND, VT, Truncate);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001639 }
Nate Begeman646d7e22005-09-02 21:18:40 +00001640 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt1
Nate Begeman1d4d4142005-09-01 00:19:25 +00001641 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Nate Begeman216def82005-10-14 01:29:07 +00001642 cast<VTSDNode>(N0.getOperand(1))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001643 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001644 }
Nate Begeman646d7e22005-09-02 21:18:40 +00001645 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
1646 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1647 EVT < cast<VTSDNode>(N0.getOperand(1))->getVT()) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001648 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001649 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00001650 // fold (sext_in_reg (assert_sext x)) -> (assert_sext x)
1651 if (N0.getOpcode() == ISD::AssertSext &&
1652 cast<VTSDNode>(N0.getOperand(1))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001653 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001654 }
1655 // fold (sext_in_reg (sextload x)) -> (sextload x)
1656 if (N0.getOpcode() == ISD::SEXTLOAD &&
1657 cast<VTSDNode>(N0.getOperand(3))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001658 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001659 }
Nate Begeman4ebd8052005-09-01 23:24:04 +00001660 // fold (sext_in_reg (setcc x)) -> setcc x iff (setcc x) == 0 or -1
Nate Begeman1d4d4142005-09-01 00:19:25 +00001661 if (N0.getOpcode() == ISD::SETCC &&
1662 TLI.getSetCCResultContents() ==
1663 TargetLowering::ZeroOrNegativeOneSetCCResult)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001664 return N0;
Nate Begeman07ed4172005-10-10 21:26:48 +00001665 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is zero
1666 if (MaskedValueIsZero(N0, 1ULL << (EVTBits-1), TLI))
1667 return DAG.getNode(ISD::AND, N0.getValueType(), N0,
1668 DAG.getConstant(~0ULL >> (64-EVTBits), VT));
1669 // fold (sext_in_reg (srl x)) -> sra x
1670 if (N0.getOpcode() == ISD::SRL &&
1671 N0.getOperand(1).getOpcode() == ISD::Constant &&
1672 cast<ConstantSDNode>(N0.getOperand(1))->getValue() == EVTBits) {
1673 return DAG.getNode(ISD::SRA, N0.getValueType(), N0.getOperand(0),
1674 N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001675 }
Nate Begemanded49632005-10-13 03:11:28 +00001676 // fold (sext_inreg (extload x)) -> (sextload x)
1677 if (N0.getOpcode() == ISD::EXTLOAD &&
1678 EVT == cast<VTSDNode>(N0.getOperand(3))->getVT() &&
Nate Begemanbfd65a02005-10-13 18:34:58 +00001679 (!AfterLegalize || TLI.isOperationLegal(ISD::SEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001680 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1681 N0.getOperand(1), N0.getOperand(2),
1682 EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00001683 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00001684 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Nate Begemanded49632005-10-13 03:11:28 +00001685 return SDOperand();
1686 }
1687 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Chris Lattner40c62d52005-10-18 06:04:22 +00001688 if (N0.getOpcode() == ISD::ZEXTLOAD && N0.hasOneUse() &&
Nate Begemanded49632005-10-13 03:11:28 +00001689 EVT == cast<VTSDNode>(N0.getOperand(3))->getVT() &&
Nate Begemanbfd65a02005-10-13 18:34:58 +00001690 (!AfterLegalize || TLI.isOperationLegal(ISD::SEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001691 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1692 N0.getOperand(1), N0.getOperand(2),
1693 EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00001694 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00001695 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Nate Begemanded49632005-10-13 03:11:28 +00001696 return SDOperand();
1697 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001698 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001699}
1700
Nate Begeman83e75ec2005-09-06 04:43:02 +00001701SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001702 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001703 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001704 MVT::ValueType VT = N->getValueType(0);
1705
1706 // noop truncate
1707 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001708 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001709 // fold (truncate c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001710 if (N0C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001711 return DAG.getConstant(N0C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001712 // fold (truncate (truncate x)) -> (truncate x)
1713 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001714 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001715 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
1716 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND){
1717 if (N0.getValueType() < VT)
1718 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00001719 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001720 else if (N0.getValueType() > VT)
1721 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00001722 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001723 else
1724 // if the source and dest are the same type, we can drop both the extend
1725 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00001726 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001727 }
Nate Begeman3df4d522005-10-12 20:40:40 +00001728 // fold (truncate (load x)) -> (smaller load x)
Chris Lattner40c62d52005-10-18 06:04:22 +00001729 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse()) {
Nate Begeman3df4d522005-10-12 20:40:40 +00001730 assert(MVT::getSizeInBits(N0.getValueType()) > MVT::getSizeInBits(VT) &&
1731 "Cannot truncate to larger type!");
1732 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
Nate Begeman765784a2005-10-12 23:18:53 +00001733 // For big endian targets, we need to add an offset to the pointer to load
1734 // the correct bytes. For little endian systems, we merely need to read
1735 // fewer bytes from the same pointer.
Nate Begeman3df4d522005-10-12 20:40:40 +00001736 uint64_t PtrOff =
1737 (MVT::getSizeInBits(N0.getValueType()) - MVT::getSizeInBits(VT)) / 8;
Nate Begeman765784a2005-10-12 23:18:53 +00001738 SDOperand NewPtr = TLI.isLittleEndian() ? N0.getOperand(1) :
1739 DAG.getNode(ISD::ADD, PtrType, N0.getOperand(1),
1740 DAG.getConstant(PtrOff, PtrType));
1741 WorkList.push_back(NewPtr.Val);
Nate Begeman3df4d522005-10-12 20:40:40 +00001742 SDOperand Load = DAG.getLoad(VT, N0.getOperand(0), NewPtr,N0.getOperand(2));
Nate Begeman765784a2005-10-12 23:18:53 +00001743 WorkList.push_back(N);
Chris Lattner24edbb72005-10-13 22:10:05 +00001744 CombineTo(N0.Val, Load, Load.getValue(1));
Nate Begeman765784a2005-10-12 23:18:53 +00001745 return SDOperand();
Nate Begeman3df4d522005-10-12 20:40:40 +00001746 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001747 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001748}
1749
Chris Lattner94683772005-12-23 05:30:37 +00001750SDOperand DAGCombiner::visitBIT_CONVERT(SDNode *N) {
1751 SDOperand N0 = N->getOperand(0);
1752 MVT::ValueType VT = N->getValueType(0);
1753
1754 // If the input is a constant, let getNode() fold it.
1755 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
1756 SDOperand Res = DAG.getNode(ISD::BIT_CONVERT, VT, N0);
1757 if (Res.Val != N) return Res;
1758 }
1759
1760 return SDOperand();
1761}
1762
Chris Lattner01b3d732005-09-28 22:28:18 +00001763SDOperand DAGCombiner::visitFADD(SDNode *N) {
1764 SDOperand N0 = N->getOperand(0);
1765 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00001766 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1767 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001768 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00001769
1770 // fold (fadd c1, c2) -> c1+c2
1771 if (N0CFP && N1CFP)
1772 return DAG.getConstantFP(N0CFP->getValue() + N1CFP->getValue(), VT);
1773 // canonicalize constant to RHS
1774 if (N0CFP && !N1CFP)
1775 return DAG.getNode(ISD::FADD, VT, N1, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00001776 // fold (A + (-B)) -> A-B
1777 if (N1.getOpcode() == ISD::FNEG)
1778 return DAG.getNode(ISD::FSUB, VT, N0, N1.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00001779 // fold ((-A) + B) -> B-A
1780 if (N0.getOpcode() == ISD::FNEG)
1781 return DAG.getNode(ISD::FSUB, VT, N1, N0.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00001782 return SDOperand();
1783}
1784
1785SDOperand DAGCombiner::visitFSUB(SDNode *N) {
1786 SDOperand N0 = N->getOperand(0);
1787 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00001788 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1789 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001790 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00001791
1792 // fold (fsub c1, c2) -> c1-c2
1793 if (N0CFP && N1CFP)
1794 return DAG.getConstantFP(N0CFP->getValue() - N1CFP->getValue(), VT);
Chris Lattner01b3d732005-09-28 22:28:18 +00001795 // fold (A-(-B)) -> A+B
1796 if (N1.getOpcode() == ISD::FNEG)
1797 return DAG.getNode(ISD::FADD, N0.getValueType(), N0, N1.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00001798 return SDOperand();
1799}
1800
1801SDOperand DAGCombiner::visitFMUL(SDNode *N) {
1802 SDOperand N0 = N->getOperand(0);
1803 SDOperand N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00001804 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1805 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001806 MVT::ValueType VT = N->getValueType(0);
1807
Nate Begeman11af4ea2005-10-17 20:40:11 +00001808 // fold (fmul c1, c2) -> c1*c2
1809 if (N0CFP && N1CFP)
1810 return DAG.getConstantFP(N0CFP->getValue() * N1CFP->getValue(), VT);
1811 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001812 if (N0CFP && !N1CFP)
1813 return DAG.getNode(ISD::FMUL, VT, N1, N0);
Nate Begeman11af4ea2005-10-17 20:40:11 +00001814 // fold (fmul X, 2.0) -> (fadd X, X)
1815 if (N1CFP && N1CFP->isExactlyValue(+2.0))
1816 return DAG.getNode(ISD::FADD, VT, N0, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00001817 return SDOperand();
1818}
1819
1820SDOperand DAGCombiner::visitFDIV(SDNode *N) {
1821 SDOperand N0 = N->getOperand(0);
1822 SDOperand N1 = N->getOperand(1);
1823 MVT::ValueType VT = N->getValueType(0);
1824
1825 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0))
1826 if (ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1)) {
1827 // fold floating point (fdiv c1, c2)
Nate Begeman11af4ea2005-10-17 20:40:11 +00001828 return DAG.getConstantFP(N0CFP->getValue() / N1CFP->getValue(), VT);
Chris Lattner01b3d732005-09-28 22:28:18 +00001829 }
1830 return SDOperand();
1831}
1832
1833SDOperand DAGCombiner::visitFREM(SDNode *N) {
1834 SDOperand N0 = N->getOperand(0);
1835 SDOperand N1 = N->getOperand(1);
1836 MVT::ValueType VT = N->getValueType(0);
1837
1838 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0))
1839 if (ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1)) {
1840 // fold floating point (frem c1, c2) -> fmod(c1, c2)
Nate Begeman11af4ea2005-10-17 20:40:11 +00001841 return DAG.getConstantFP(fmod(N0CFP->getValue(),N1CFP->getValue()), VT);
Chris Lattner01b3d732005-09-28 22:28:18 +00001842 }
1843 return SDOperand();
1844}
1845
1846
Nate Begeman83e75ec2005-09-06 04:43:02 +00001847SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001848 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001849 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001850
1851 // fold (sint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001852 if (N0C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001853 return DAG.getConstantFP(N0C->getSignExtended(), N->getValueType(0));
1854 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001855}
1856
Nate Begeman83e75ec2005-09-06 04:43:02 +00001857SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001858 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001859 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001860
1861 // fold (uint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001862 if (N0C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001863 return DAG.getConstantFP(N0C->getValue(), N->getValueType(0));
1864 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001865}
1866
Nate Begeman83e75ec2005-09-06 04:43:02 +00001867SDOperand DAGCombiner::visitFP_TO_SINT(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +00001868 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001869
1870 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001871 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001872 return DAG.getConstant((int64_t)N0CFP->getValue(), N->getValueType(0));
1873 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001874}
1875
Nate Begeman83e75ec2005-09-06 04:43:02 +00001876SDOperand DAGCombiner::visitFP_TO_UINT(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +00001877 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001878
1879 // fold (fp_to_uint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001880 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001881 return DAG.getConstant((uint64_t)N0CFP->getValue(), N->getValueType(0));
1882 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001883}
1884
Nate Begeman83e75ec2005-09-06 04:43:02 +00001885SDOperand DAGCombiner::visitFP_ROUND(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +00001886 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001887
1888 // fold (fp_round c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001889 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001890 return DAG.getConstantFP(N0CFP->getValue(), N->getValueType(0));
1891 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001892}
1893
Nate Begeman83e75ec2005-09-06 04:43:02 +00001894SDOperand DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001895 SDOperand N0 = N->getOperand(0);
1896 MVT::ValueType VT = N->getValueType(0);
1897 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00001898 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001899
Nate Begeman1d4d4142005-09-01 00:19:25 +00001900 // fold (fp_round_inreg c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001901 if (N0CFP) {
1902 SDOperand Round = DAG.getConstantFP(N0CFP->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001903 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001904 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001905 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001906}
1907
Nate Begeman83e75ec2005-09-06 04:43:02 +00001908SDOperand DAGCombiner::visitFP_EXTEND(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +00001909 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001910
1911 // fold (fp_extend c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001912 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001913 return DAG.getConstantFP(N0CFP->getValue(), N->getValueType(0));
1914 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001915}
1916
Nate Begeman83e75ec2005-09-06 04:43:02 +00001917SDOperand DAGCombiner::visitFNEG(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +00001918 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001919 // fold (neg c1) -> -c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001920 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001921 return DAG.getConstantFP(-N0CFP->getValue(), N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001922 // fold (neg (sub x, y)) -> (sub y, x)
1923 if (N->getOperand(0).getOpcode() == ISD::SUB)
1924 return DAG.getNode(ISD::SUB, N->getValueType(0), N->getOperand(1),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001925 N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001926 // fold (neg (neg x)) -> x
1927 if (N->getOperand(0).getOpcode() == ISD::FNEG)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001928 return N->getOperand(0).getOperand(0);
1929 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001930}
1931
Nate Begeman83e75ec2005-09-06 04:43:02 +00001932SDOperand DAGCombiner::visitFABS(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +00001933 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001934 // fold (fabs c1) -> fabs(c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001935 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001936 return DAG.getConstantFP(fabs(N0CFP->getValue()), N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001937 // fold (fabs (fabs x)) -> (fabs x)
1938 if (N->getOperand(0).getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001939 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001940 // fold (fabs (fneg x)) -> (fabs x)
1941 if (N->getOperand(0).getOpcode() == ISD::FNEG)
1942 return DAG.getNode(ISD::FABS, N->getValueType(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001943 N->getOperand(0).getOperand(0));
1944 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001945}
1946
Nate Begeman44728a72005-09-19 22:34:01 +00001947SDOperand DAGCombiner::visitBRCOND(SDNode *N) {
1948 SDOperand Chain = N->getOperand(0);
1949 SDOperand N1 = N->getOperand(1);
1950 SDOperand N2 = N->getOperand(2);
1951 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1952
1953 // never taken branch, fold to chain
1954 if (N1C && N1C->isNullValue())
1955 return Chain;
1956 // unconditional branch
Nate Begemane17daeb2005-10-05 21:43:42 +00001957 if (N1C && N1C->getValue() == 1)
Nate Begeman44728a72005-09-19 22:34:01 +00001958 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
1959 return SDOperand();
1960}
1961
1962SDOperand DAGCombiner::visitBRCONDTWOWAY(SDNode *N) {
1963 SDOperand Chain = N->getOperand(0);
1964 SDOperand N1 = N->getOperand(1);
1965 SDOperand N2 = N->getOperand(2);
1966 SDOperand N3 = N->getOperand(3);
1967 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1968
1969 // unconditional branch to true mbb
1970 if (N1C && N1C->getValue() == 1)
1971 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
1972 // unconditional branch to false mbb
1973 if (N1C && N1C->isNullValue())
1974 return DAG.getNode(ISD::BR, MVT::Other, Chain, N3);
1975 return SDOperand();
1976}
1977
Chris Lattner3ea0b472005-10-05 06:47:48 +00001978// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
1979//
Nate Begeman44728a72005-09-19 22:34:01 +00001980SDOperand DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00001981 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
1982 SDOperand CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
1983
1984 // Use SimplifySetCC to simplify SETCC's.
Nate Begemane17daeb2005-10-05 21:43:42 +00001985 SDOperand Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false);
1986 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.Val);
1987
1988 // fold br_cc true, dest -> br dest (unconditional branch)
1989 if (SCCC && SCCC->getValue())
1990 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
1991 N->getOperand(4));
1992 // fold br_cc false, dest -> unconditional fall through
1993 if (SCCC && SCCC->isNullValue())
1994 return N->getOperand(0);
1995 // fold to a simpler setcc
1996 if (Simp.Val && Simp.getOpcode() == ISD::SETCC)
1997 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
1998 Simp.getOperand(2), Simp.getOperand(0),
1999 Simp.getOperand(1), N->getOperand(4));
Nate Begeman44728a72005-09-19 22:34:01 +00002000 return SDOperand();
2001}
2002
2003SDOperand DAGCombiner::visitBRTWOWAY_CC(SDNode *N) {
Nate Begemane17daeb2005-10-05 21:43:42 +00002004 SDOperand Chain = N->getOperand(0);
2005 SDOperand CCN = N->getOperand(1);
2006 SDOperand LHS = N->getOperand(2);
2007 SDOperand RHS = N->getOperand(3);
2008 SDOperand N4 = N->getOperand(4);
2009 SDOperand N5 = N->getOperand(5);
2010
2011 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), LHS, RHS,
2012 cast<CondCodeSDNode>(CCN)->get(), false);
2013 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
2014
2015 // fold select_cc lhs, rhs, x, x, cc -> x
2016 if (N4 == N5)
2017 return DAG.getNode(ISD::BR, MVT::Other, Chain, N4);
2018 // fold select_cc true, x, y -> x
2019 if (SCCC && SCCC->getValue())
2020 return DAG.getNode(ISD::BR, MVT::Other, Chain, N4);
2021 // fold select_cc false, x, y -> y
2022 if (SCCC && SCCC->isNullValue())
2023 return DAG.getNode(ISD::BR, MVT::Other, Chain, N5);
2024 // fold to a simpler setcc
2025 if (SCC.Val && SCC.getOpcode() == ISD::SETCC)
2026 return DAG.getBR2Way_CC(Chain, SCC.getOperand(2), SCC.getOperand(0),
2027 SCC.getOperand(1), N4, N5);
Nate Begeman44728a72005-09-19 22:34:01 +00002028 return SDOperand();
2029}
2030
Chris Lattner01a22022005-10-10 22:04:48 +00002031SDOperand DAGCombiner::visitLOAD(SDNode *N) {
2032 SDOperand Chain = N->getOperand(0);
2033 SDOperand Ptr = N->getOperand(1);
2034 SDOperand SrcValue = N->getOperand(2);
2035
2036 // If this load is directly stored, replace the load value with the stored
2037 // value.
2038 // TODO: Handle store large -> read small portion.
2039 // TODO: Handle TRUNCSTORE/EXTLOAD
2040 if (Chain.getOpcode() == ISD::STORE && Chain.getOperand(2) == Ptr &&
2041 Chain.getOperand(1).getValueType() == N->getValueType(0))
2042 return CombineTo(N, Chain.getOperand(1), Chain);
2043
2044 return SDOperand();
2045}
2046
Chris Lattner87514ca2005-10-10 22:31:19 +00002047SDOperand DAGCombiner::visitSTORE(SDNode *N) {
2048 SDOperand Chain = N->getOperand(0);
2049 SDOperand Value = N->getOperand(1);
2050 SDOperand Ptr = N->getOperand(2);
2051 SDOperand SrcValue = N->getOperand(3);
2052
2053 // If this is a store that kills a previous store, remove the previous store.
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002054 if (Chain.getOpcode() == ISD::STORE && Chain.getOperand(2) == Ptr &&
Chris Lattnerfe7f0462005-10-27 07:10:34 +00002055 Chain.Val->hasOneUse() /* Avoid introducing DAG cycles */ &&
2056 // Make sure that these stores are the same value type:
2057 // FIXME: we really care that the second store is >= size of the first.
2058 Value.getValueType() == Chain.getOperand(1).getValueType()) {
Chris Lattner87514ca2005-10-10 22:31:19 +00002059 // Create a new store of Value that replaces both stores.
2060 SDNode *PrevStore = Chain.Val;
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002061 if (PrevStore->getOperand(1) == Value) // Same value multiply stored.
2062 return Chain;
Chris Lattner87514ca2005-10-10 22:31:19 +00002063 SDOperand NewStore = DAG.getNode(ISD::STORE, MVT::Other,
2064 PrevStore->getOperand(0), Value, Ptr,
2065 SrcValue);
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002066 CombineTo(N, NewStore); // Nuke this store.
Chris Lattner87514ca2005-10-10 22:31:19 +00002067 CombineTo(PrevStore, NewStore); // Nuke the previous store.
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002068 return SDOperand(N, 0);
Chris Lattner87514ca2005-10-10 22:31:19 +00002069 }
2070
2071 return SDOperand();
2072}
2073
Nate Begeman44728a72005-09-19 22:34:01 +00002074SDOperand DAGCombiner::SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2){
Nate Begemanf845b452005-10-08 00:29:44 +00002075 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
2076
2077 SDOperand SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
2078 cast<CondCodeSDNode>(N0.getOperand(2))->get());
2079 // If we got a simplified select_cc node back from SimplifySelectCC, then
2080 // break it down into a new SETCC node, and a new SELECT node, and then return
2081 // the SELECT node, since we were called with a SELECT node.
2082 if (SCC.Val) {
2083 // Check to see if we got a select_cc back (to turn into setcc/select).
2084 // Otherwise, just return whatever node we got back, like fabs.
2085 if (SCC.getOpcode() == ISD::SELECT_CC) {
2086 SDOperand SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
2087 SCC.getOperand(0), SCC.getOperand(1),
2088 SCC.getOperand(4));
2089 WorkList.push_back(SETCC.Val);
2090 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
2091 SCC.getOperand(3), SETCC);
2092 }
2093 return SCC;
2094 }
Nate Begeman44728a72005-09-19 22:34:01 +00002095 return SDOperand();
2096}
2097
Chris Lattner40c62d52005-10-18 06:04:22 +00002098/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
2099/// are the two values being selected between, see if we can simplify the
2100/// select.
2101///
2102bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDOperand LHS,
2103 SDOperand RHS) {
2104
2105 // If this is a select from two identical things, try to pull the operation
2106 // through the select.
2107 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
2108#if 0
2109 std::cerr << "SELECT: ["; LHS.Val->dump();
2110 std::cerr << "] ["; RHS.Val->dump();
2111 std::cerr << "]\n";
2112#endif
2113
2114 // If this is a load and the token chain is identical, replace the select
2115 // of two loads with a load through a select of the address to load from.
2116 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
2117 // constants have been dropped into the constant pool.
2118 if ((LHS.getOpcode() == ISD::LOAD ||
2119 LHS.getOpcode() == ISD::EXTLOAD ||
2120 LHS.getOpcode() == ISD::ZEXTLOAD ||
2121 LHS.getOpcode() == ISD::SEXTLOAD) &&
2122 // Token chains must be identical.
2123 LHS.getOperand(0) == RHS.getOperand(0) &&
2124 // If this is an EXTLOAD, the VT's must match.
2125 (LHS.getOpcode() == ISD::LOAD ||
2126 LHS.getOperand(3) == RHS.getOperand(3))) {
2127 // FIXME: this conflates two src values, discarding one. This is not
2128 // the right thing to do, but nothing uses srcvalues now. When they do,
2129 // turn SrcValue into a list of locations.
2130 SDOperand Addr;
2131 if (TheSelect->getOpcode() == ISD::SELECT)
2132 Addr = DAG.getNode(ISD::SELECT, LHS.getOperand(1).getValueType(),
2133 TheSelect->getOperand(0), LHS.getOperand(1),
2134 RHS.getOperand(1));
2135 else
2136 Addr = DAG.getNode(ISD::SELECT_CC, LHS.getOperand(1).getValueType(),
2137 TheSelect->getOperand(0),
2138 TheSelect->getOperand(1),
2139 LHS.getOperand(1), RHS.getOperand(1),
2140 TheSelect->getOperand(4));
2141
2142 SDOperand Load;
2143 if (LHS.getOpcode() == ISD::LOAD)
2144 Load = DAG.getLoad(TheSelect->getValueType(0), LHS.getOperand(0),
2145 Addr, LHS.getOperand(2));
2146 else
2147 Load = DAG.getExtLoad(LHS.getOpcode(), TheSelect->getValueType(0),
2148 LHS.getOperand(0), Addr, LHS.getOperand(2),
2149 cast<VTSDNode>(LHS.getOperand(3))->getVT());
2150 // Users of the select now use the result of the load.
2151 CombineTo(TheSelect, Load);
2152
2153 // Users of the old loads now use the new load's chain. We know the
2154 // old-load value is dead now.
2155 CombineTo(LHS.Val, Load.getValue(0), Load.getValue(1));
2156 CombineTo(RHS.Val, Load.getValue(0), Load.getValue(1));
2157 return true;
2158 }
2159 }
2160
2161 return false;
2162}
2163
Nate Begeman44728a72005-09-19 22:34:01 +00002164SDOperand DAGCombiner::SimplifySelectCC(SDOperand N0, SDOperand N1,
2165 SDOperand N2, SDOperand N3,
2166 ISD::CondCode CC) {
Nate Begemanf845b452005-10-08 00:29:44 +00002167
2168 MVT::ValueType VT = N2.getValueType();
2169 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
2170 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2171 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
2172 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
2173
2174 // Determine if the condition we're dealing with is constant
2175 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
2176 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
2177
2178 // fold select_cc true, x, y -> x
2179 if (SCCC && SCCC->getValue())
2180 return N2;
2181 // fold select_cc false, x, y -> y
2182 if (SCCC && SCCC->getValue() == 0)
2183 return N3;
2184
2185 // Check to see if we can simplify the select into an fabs node
2186 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
2187 // Allow either -0.0 or 0.0
2188 if (CFP->getValue() == 0.0) {
2189 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
2190 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
2191 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
2192 N2 == N3.getOperand(0))
2193 return DAG.getNode(ISD::FABS, VT, N0);
2194
2195 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
2196 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
2197 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
2198 N2.getOperand(0) == N3)
2199 return DAG.getNode(ISD::FABS, VT, N3);
2200 }
2201 }
2202
2203 // Check to see if we can perform the "gzip trick", transforming
2204 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
2205 if (N1C && N1C->isNullValue() && N3C && N3C->isNullValue() &&
2206 MVT::isInteger(N0.getValueType()) &&
2207 MVT::isInteger(N2.getValueType()) && CC == ISD::SETLT) {
2208 MVT::ValueType XType = N0.getValueType();
2209 MVT::ValueType AType = N2.getValueType();
2210 if (XType >= AType) {
2211 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00002212 // single-bit constant.
Nate Begemanf845b452005-10-08 00:29:44 +00002213 if (N2C && ((N2C->getValue() & (N2C->getValue()-1)) == 0)) {
2214 unsigned ShCtV = Log2_64(N2C->getValue());
2215 ShCtV = MVT::getSizeInBits(XType)-ShCtV-1;
2216 SDOperand ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
2217 SDOperand Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
2218 WorkList.push_back(Shift.Val);
2219 if (XType > AType) {
2220 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
2221 WorkList.push_back(Shift.Val);
2222 }
2223 return DAG.getNode(ISD::AND, AType, Shift, N2);
2224 }
2225 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
2226 DAG.getConstant(MVT::getSizeInBits(XType)-1,
2227 TLI.getShiftAmountTy()));
2228 WorkList.push_back(Shift.Val);
2229 if (XType > AType) {
2230 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
2231 WorkList.push_back(Shift.Val);
2232 }
2233 return DAG.getNode(ISD::AND, AType, Shift, N2);
2234 }
2235 }
Nate Begeman07ed4172005-10-10 21:26:48 +00002236
2237 // fold select C, 16, 0 -> shl C, 4
2238 if (N2C && N3C && N3C->isNullValue() && isPowerOf2_64(N2C->getValue()) &&
2239 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) {
2240 // Get a SetCC of the condition
2241 // FIXME: Should probably make sure that setcc is legal if we ever have a
2242 // target where it isn't.
2243 SDOperand Temp, SCC = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
2244 WorkList.push_back(SCC.Val);
2245 // cast from setcc result type to select result type
2246 if (AfterLegalize)
2247 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
2248 else
2249 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
2250 WorkList.push_back(Temp.Val);
2251 // shl setcc result by log2 n2c
2252 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
2253 DAG.getConstant(Log2_64(N2C->getValue()),
2254 TLI.getShiftAmountTy()));
2255 }
2256
Nate Begemanf845b452005-10-08 00:29:44 +00002257 // Check to see if this is the equivalent of setcc
2258 // FIXME: Turn all of these into setcc if setcc if setcc is legal
2259 // otherwise, go ahead with the folds.
2260 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getValue() == 1ULL)) {
2261 MVT::ValueType XType = N0.getValueType();
2262 if (TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultTy())) {
2263 SDOperand Res = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
2264 if (Res.getValueType() != VT)
2265 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
2266 return Res;
2267 }
2268
2269 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
2270 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
2271 TLI.isOperationLegal(ISD::CTLZ, XType)) {
2272 SDOperand Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
2273 return DAG.getNode(ISD::SRL, XType, Ctlz,
2274 DAG.getConstant(Log2_32(MVT::getSizeInBits(XType)),
2275 TLI.getShiftAmountTy()));
2276 }
2277 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
2278 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
2279 SDOperand NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
2280 N0);
2281 SDOperand NotN0 = DAG.getNode(ISD::XOR, XType, N0,
2282 DAG.getConstant(~0ULL, XType));
2283 return DAG.getNode(ISD::SRL, XType,
2284 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
2285 DAG.getConstant(MVT::getSizeInBits(XType)-1,
2286 TLI.getShiftAmountTy()));
2287 }
2288 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
2289 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
2290 SDOperand Sign = DAG.getNode(ISD::SRL, XType, N0,
2291 DAG.getConstant(MVT::getSizeInBits(XType)-1,
2292 TLI.getShiftAmountTy()));
2293 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
2294 }
2295 }
2296
2297 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
2298 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
2299 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
2300 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1)) {
2301 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N2.getOperand(0))) {
2302 MVT::ValueType XType = N0.getValueType();
2303 if (SubC->isNullValue() && MVT::isInteger(XType)) {
2304 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
2305 DAG.getConstant(MVT::getSizeInBits(XType)-1,
2306 TLI.getShiftAmountTy()));
2307 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
2308 WorkList.push_back(Shift.Val);
2309 WorkList.push_back(Add.Val);
2310 return DAG.getNode(ISD::XOR, XType, Add, Shift);
2311 }
2312 }
2313 }
2314
Nate Begeman44728a72005-09-19 22:34:01 +00002315 return SDOperand();
2316}
2317
Nate Begeman452d7be2005-09-16 00:54:12 +00002318SDOperand DAGCombiner::SimplifySetCC(MVT::ValueType VT, SDOperand N0,
Nate Begemane17daeb2005-10-05 21:43:42 +00002319 SDOperand N1, ISD::CondCode Cond,
2320 bool foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00002321 // These setcc operations always fold.
2322 switch (Cond) {
2323 default: break;
2324 case ISD::SETFALSE:
2325 case ISD::SETFALSE2: return DAG.getConstant(0, VT);
2326 case ISD::SETTRUE:
2327 case ISD::SETTRUE2: return DAG.getConstant(1, VT);
2328 }
2329
2330 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
2331 uint64_t C1 = N1C->getValue();
2332 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val)) {
2333 uint64_t C0 = N0C->getValue();
2334
2335 // Sign extend the operands if required
2336 if (ISD::isSignedIntSetCC(Cond)) {
2337 C0 = N0C->getSignExtended();
2338 C1 = N1C->getSignExtended();
2339 }
2340
2341 switch (Cond) {
2342 default: assert(0 && "Unknown integer setcc!");
2343 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
2344 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
2345 case ISD::SETULT: return DAG.getConstant(C0 < C1, VT);
2346 case ISD::SETUGT: return DAG.getConstant(C0 > C1, VT);
2347 case ISD::SETULE: return DAG.getConstant(C0 <= C1, VT);
2348 case ISD::SETUGE: return DAG.getConstant(C0 >= C1, VT);
2349 case ISD::SETLT: return DAG.getConstant((int64_t)C0 < (int64_t)C1, VT);
2350 case ISD::SETGT: return DAG.getConstant((int64_t)C0 > (int64_t)C1, VT);
2351 case ISD::SETLE: return DAG.getConstant((int64_t)C0 <= (int64_t)C1, VT);
2352 case ISD::SETGE: return DAG.getConstant((int64_t)C0 >= (int64_t)C1, VT);
2353 }
2354 } else {
2355 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
2356 if (N0.getOpcode() == ISD::ZERO_EXTEND) {
2357 unsigned InSize = MVT::getSizeInBits(N0.getOperand(0).getValueType());
2358
2359 // If the comparison constant has bits in the upper part, the
2360 // zero-extended value could never match.
2361 if (C1 & (~0ULL << InSize)) {
2362 unsigned VSize = MVT::getSizeInBits(N0.getValueType());
2363 switch (Cond) {
2364 case ISD::SETUGT:
2365 case ISD::SETUGE:
2366 case ISD::SETEQ: return DAG.getConstant(0, VT);
2367 case ISD::SETULT:
2368 case ISD::SETULE:
2369 case ISD::SETNE: return DAG.getConstant(1, VT);
2370 case ISD::SETGT:
2371 case ISD::SETGE:
2372 // True if the sign bit of C1 is set.
2373 return DAG.getConstant((C1 & (1ULL << VSize)) != 0, VT);
2374 case ISD::SETLT:
2375 case ISD::SETLE:
2376 // True if the sign bit of C1 isn't set.
2377 return DAG.getConstant((C1 & (1ULL << VSize)) == 0, VT);
2378 default:
2379 break;
2380 }
2381 }
2382
2383 // Otherwise, we can perform the comparison with the low bits.
2384 switch (Cond) {
2385 case ISD::SETEQ:
2386 case ISD::SETNE:
2387 case ISD::SETUGT:
2388 case ISD::SETUGE:
2389 case ISD::SETULT:
2390 case ISD::SETULE:
2391 return DAG.getSetCC(VT, N0.getOperand(0),
2392 DAG.getConstant(C1, N0.getOperand(0).getValueType()),
2393 Cond);
2394 default:
2395 break; // todo, be more careful with signed comparisons
2396 }
2397 } else if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
2398 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
2399 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N0.getOperand(1))->getVT();
2400 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
2401 MVT::ValueType ExtDstTy = N0.getValueType();
2402 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
2403
2404 // If the extended part has any inconsistent bits, it cannot ever
2405 // compare equal. In other words, they have to be all ones or all
2406 // zeros.
2407 uint64_t ExtBits =
2408 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
2409 if ((C1 & ExtBits) != 0 && (C1 & ExtBits) != ExtBits)
2410 return DAG.getConstant(Cond == ISD::SETNE, VT);
2411
2412 SDOperand ZextOp;
2413 MVT::ValueType Op0Ty = N0.getOperand(0).getValueType();
2414 if (Op0Ty == ExtSrcTy) {
2415 ZextOp = N0.getOperand(0);
2416 } else {
2417 int64_t Imm = ~0ULL >> (64-ExtSrcTyBits);
2418 ZextOp = DAG.getNode(ISD::AND, Op0Ty, N0.getOperand(0),
2419 DAG.getConstant(Imm, Op0Ty));
2420 }
2421 WorkList.push_back(ZextOp.Val);
2422 // Otherwise, make this a use of a zext.
2423 return DAG.getSetCC(VT, ZextOp,
2424 DAG.getConstant(C1 & (~0ULL>>(64-ExtSrcTyBits)),
2425 ExtDstTy),
2426 Cond);
2427 }
Chris Lattner5c46f742005-10-05 06:11:08 +00002428
Nate Begeman452d7be2005-09-16 00:54:12 +00002429 uint64_t MinVal, MaxVal;
2430 unsigned OperandBitSize = MVT::getSizeInBits(N1C->getValueType(0));
2431 if (ISD::isSignedIntSetCC(Cond)) {
2432 MinVal = 1ULL << (OperandBitSize-1);
2433 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
2434 MaxVal = ~0ULL >> (65-OperandBitSize);
2435 else
2436 MaxVal = 0;
2437 } else {
2438 MinVal = 0;
2439 MaxVal = ~0ULL >> (64-OperandBitSize);
2440 }
2441
2442 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
2443 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
2444 if (C1 == MinVal) return DAG.getConstant(1, VT); // X >= MIN --> true
2445 --C1; // X >= C0 --> X > (C0-1)
2446 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
2447 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
2448 }
2449
2450 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
2451 if (C1 == MaxVal) return DAG.getConstant(1, VT); // X <= MAX --> true
2452 ++C1; // X <= C0 --> X < (C0+1)
2453 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
2454 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
2455 }
2456
2457 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal)
2458 return DAG.getConstant(0, VT); // X < MIN --> false
2459
2460 // Canonicalize setgt X, Min --> setne X, Min
2461 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MinVal)
2462 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
Chris Lattnerc8597ca2005-10-21 21:23:25 +00002463 // Canonicalize setlt X, Max --> setne X, Max
2464 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MaxVal)
2465 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
Nate Begeman452d7be2005-09-16 00:54:12 +00002466
2467 // If we have setult X, 1, turn it into seteq X, 0
2468 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal+1)
2469 return DAG.getSetCC(VT, N0, DAG.getConstant(MinVal, N0.getValueType()),
2470 ISD::SETEQ);
2471 // If we have setugt X, Max-1, turn it into seteq X, Max
2472 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal-1)
2473 return DAG.getSetCC(VT, N0, DAG.getConstant(MaxVal, N0.getValueType()),
2474 ISD::SETEQ);
2475
2476 // If we have "setcc X, C0", check to see if we can shrink the immediate
2477 // by changing cc.
2478
2479 // SETUGT X, SINTMAX -> SETLT X, 0
2480 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
2481 C1 == (~0ULL >> (65-OperandBitSize)))
2482 return DAG.getSetCC(VT, N0, DAG.getConstant(0, N1.getValueType()),
2483 ISD::SETLT);
2484
2485 // FIXME: Implement the rest of these.
2486
2487 // Fold bit comparisons when we can.
2488 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
2489 VT == N0.getValueType() && N0.getOpcode() == ISD::AND)
2490 if (ConstantSDNode *AndRHS =
2491 dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2492 if (Cond == ISD::SETNE && C1 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
2493 // Perform the xform if the AND RHS is a single bit.
2494 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
2495 return DAG.getNode(ISD::SRL, VT, N0,
2496 DAG.getConstant(Log2_64(AndRHS->getValue()),
2497 TLI.getShiftAmountTy()));
2498 }
2499 } else if (Cond == ISD::SETEQ && C1 == AndRHS->getValue()) {
2500 // (X & 8) == 8 --> (X & 8) >> 3
2501 // Perform the xform if C1 is a single bit.
2502 if ((C1 & (C1-1)) == 0) {
2503 return DAG.getNode(ISD::SRL, VT, N0,
2504 DAG.getConstant(Log2_64(C1),TLI.getShiftAmountTy()));
2505 }
2506 }
2507 }
2508 }
2509 } else if (isa<ConstantSDNode>(N0.Val)) {
2510 // Ensure that the constant occurs on the RHS.
2511 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
2512 }
2513
2514 if (ConstantFPSDNode *N0C = dyn_cast<ConstantFPSDNode>(N0.Val))
2515 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) {
2516 double C0 = N0C->getValue(), C1 = N1C->getValue();
2517
2518 switch (Cond) {
2519 default: break; // FIXME: Implement the rest of these!
2520 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
2521 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
2522 case ISD::SETLT: return DAG.getConstant(C0 < C1, VT);
2523 case ISD::SETGT: return DAG.getConstant(C0 > C1, VT);
2524 case ISD::SETLE: return DAG.getConstant(C0 <= C1, VT);
2525 case ISD::SETGE: return DAG.getConstant(C0 >= C1, VT);
2526 }
2527 } else {
2528 // Ensure that the constant occurs on the RHS.
2529 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
2530 }
2531
2532 if (N0 == N1) {
2533 // We can always fold X == Y for integer setcc's.
2534 if (MVT::isInteger(N0.getValueType()))
2535 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
2536 unsigned UOF = ISD::getUnorderedFlavor(Cond);
2537 if (UOF == 2) // FP operators that are undefined on NaNs.
2538 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
2539 if (UOF == unsigned(ISD::isTrueWhenEqual(Cond)))
2540 return DAG.getConstant(UOF, VT);
2541 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
2542 // if it is not already.
2543 ISD::CondCode NewCond = UOF == 0 ? ISD::SETUO : ISD::SETO;
2544 if (NewCond != Cond)
2545 return DAG.getSetCC(VT, N0, N1, NewCond);
2546 }
2547
2548 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
2549 MVT::isInteger(N0.getValueType())) {
2550 if (N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::SUB ||
2551 N0.getOpcode() == ISD::XOR) {
2552 // Simplify (X+Y) == (X+Z) --> Y == Z
2553 if (N0.getOpcode() == N1.getOpcode()) {
2554 if (N0.getOperand(0) == N1.getOperand(0))
2555 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(1), Cond);
2556 if (N0.getOperand(1) == N1.getOperand(1))
2557 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(0), Cond);
2558 if (isCommutativeBinOp(N0.getOpcode())) {
2559 // If X op Y == Y op X, try other combinations.
2560 if (N0.getOperand(0) == N1.getOperand(1))
2561 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(0), Cond);
2562 if (N0.getOperand(1) == N1.getOperand(0))
Chris Lattnera158eee2005-10-25 18:57:30 +00002563 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(1), Cond);
Nate Begeman452d7be2005-09-16 00:54:12 +00002564 }
2565 }
2566
Chris Lattner5c46f742005-10-05 06:11:08 +00002567 // Turn (X^C1) == C2 into X == C1^C2 iff X&~C1 = 0. Common for condcodes.
2568 if (N0.getOpcode() == ISD::XOR)
2569 if (ConstantSDNode *XORC = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
2570 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(N1)) {
2571 // If we know that all of the inverted bits are zero, don't bother
2572 // performing the inversion.
2573 if (MaskedValueIsZero(N0.getOperand(0), ~XORC->getValue(), TLI))
2574 return DAG.getSetCC(VT, N0.getOperand(0),
2575 DAG.getConstant(XORC->getValue()^RHSC->getValue(),
2576 N0.getValueType()), Cond);
2577 }
2578
Nate Begeman452d7be2005-09-16 00:54:12 +00002579 // Simplify (X+Z) == X --> Z == 0
2580 if (N0.getOperand(0) == N1)
2581 return DAG.getSetCC(VT, N0.getOperand(1),
2582 DAG.getConstant(0, N0.getValueType()), Cond);
2583 if (N0.getOperand(1) == N1) {
2584 if (isCommutativeBinOp(N0.getOpcode()))
2585 return DAG.getSetCC(VT, N0.getOperand(0),
2586 DAG.getConstant(0, N0.getValueType()), Cond);
2587 else {
2588 assert(N0.getOpcode() == ISD::SUB && "Unexpected operation!");
2589 // (Z-X) == X --> Z == X<<1
2590 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(),
2591 N1,
2592 DAG.getConstant(1,TLI.getShiftAmountTy()));
2593 WorkList.push_back(SH.Val);
2594 return DAG.getSetCC(VT, N0.getOperand(0), SH, Cond);
2595 }
2596 }
2597 }
2598
2599 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
2600 N1.getOpcode() == ISD::XOR) {
2601 // Simplify X == (X+Z) --> Z == 0
2602 if (N1.getOperand(0) == N0) {
2603 return DAG.getSetCC(VT, N1.getOperand(1),
2604 DAG.getConstant(0, N1.getValueType()), Cond);
2605 } else if (N1.getOperand(1) == N0) {
2606 if (isCommutativeBinOp(N1.getOpcode())) {
2607 return DAG.getSetCC(VT, N1.getOperand(0),
2608 DAG.getConstant(0, N1.getValueType()), Cond);
2609 } else {
2610 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
2611 // X == (Z-X) --> X<<1 == Z
2612 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(), N0,
2613 DAG.getConstant(1,TLI.getShiftAmountTy()));
2614 WorkList.push_back(SH.Val);
2615 return DAG.getSetCC(VT, SH, N1.getOperand(0), Cond);
2616 }
2617 }
2618 }
2619 }
2620
2621 // Fold away ALL boolean setcc's.
2622 SDOperand Temp;
Nate Begemane17daeb2005-10-05 21:43:42 +00002623 if (N0.getValueType() == MVT::i1 && foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00002624 switch (Cond) {
2625 default: assert(0 && "Unknown integer setcc!");
2626 case ISD::SETEQ: // X == Y -> (X^Y)^1
2627 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
2628 N0 = DAG.getNode(ISD::XOR, MVT::i1, Temp, DAG.getConstant(1, MVT::i1));
2629 WorkList.push_back(Temp.Val);
2630 break;
2631 case ISD::SETNE: // X != Y --> (X^Y)
2632 N0 = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
2633 break;
2634 case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> X^1 & Y
2635 case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> X^1 & Y
2636 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
2637 N0 = DAG.getNode(ISD::AND, MVT::i1, N1, Temp);
2638 WorkList.push_back(Temp.Val);
2639 break;
2640 case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> Y^1 & X
2641 case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> Y^1 & X
2642 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
2643 N0 = DAG.getNode(ISD::AND, MVT::i1, N0, Temp);
2644 WorkList.push_back(Temp.Val);
2645 break;
2646 case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> X^1 | Y
2647 case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> X^1 | Y
2648 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
2649 N0 = DAG.getNode(ISD::OR, MVT::i1, N1, Temp);
2650 WorkList.push_back(Temp.Val);
2651 break;
2652 case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> Y^1 | X
2653 case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> Y^1 | X
2654 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
2655 N0 = DAG.getNode(ISD::OR, MVT::i1, N0, Temp);
2656 break;
2657 }
2658 if (VT != MVT::i1) {
2659 WorkList.push_back(N0.Val);
2660 // FIXME: If running after legalize, we probably can't do this.
2661 N0 = DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
2662 }
2663 return N0;
2664 }
2665
2666 // Could not fold it.
2667 return SDOperand();
2668}
2669
Nate Begeman69575232005-10-20 02:15:44 +00002670/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
2671/// return a DAG expression to select that will generate the same value by
2672/// multiplying by a magic number. See:
2673/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
2674SDOperand DAGCombiner::BuildSDIV(SDNode *N) {
2675 MVT::ValueType VT = N->getValueType(0);
Chris Lattnere9936d12005-10-22 18:50:15 +00002676
2677 // Check to see if we can do this.
2678 if (!TLI.isTypeLegal(VT) || (VT != MVT::i32 && VT != MVT::i64))
2679 return SDOperand(); // BuildSDIV only operates on i32 or i64
2680 if (!TLI.isOperationLegal(ISD::MULHS, VT))
2681 return SDOperand(); // Make sure the target supports MULHS.
Nate Begeman69575232005-10-20 02:15:44 +00002682
Nate Begemanc6a454e2005-10-20 17:45:03 +00002683 int64_t d = cast<ConstantSDNode>(N->getOperand(1))->getSignExtended();
Nate Begeman69575232005-10-20 02:15:44 +00002684 ms magics = (VT == MVT::i32) ? magic32(d) : magic64(d);
2685
2686 // Multiply the numerator (operand 0) by the magic value
2687 SDOperand Q = DAG.getNode(ISD::MULHS, VT, N->getOperand(0),
2688 DAG.getConstant(magics.m, VT));
2689 // If d > 0 and m < 0, add the numerator
2690 if (d > 0 && magics.m < 0) {
2691 Q = DAG.getNode(ISD::ADD, VT, Q, N->getOperand(0));
2692 WorkList.push_back(Q.Val);
2693 }
2694 // If d < 0 and m > 0, subtract the numerator.
2695 if (d < 0 && magics.m > 0) {
2696 Q = DAG.getNode(ISD::SUB, VT, Q, N->getOperand(0));
2697 WorkList.push_back(Q.Val);
2698 }
2699 // Shift right algebraic if shift value is nonzero
2700 if (magics.s > 0) {
2701 Q = DAG.getNode(ISD::SRA, VT, Q,
2702 DAG.getConstant(magics.s, TLI.getShiftAmountTy()));
2703 WorkList.push_back(Q.Val);
2704 }
2705 // Extract the sign bit and add it to the quotient
2706 SDOperand T =
Nate Begeman4d385672005-10-21 01:51:45 +00002707 DAG.getNode(ISD::SRL, VT, Q, DAG.getConstant(MVT::getSizeInBits(VT)-1,
2708 TLI.getShiftAmountTy()));
Nate Begeman69575232005-10-20 02:15:44 +00002709 WorkList.push_back(T.Val);
2710 return DAG.getNode(ISD::ADD, VT, Q, T);
2711}
2712
2713/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
2714/// return a DAG expression to select that will generate the same value by
2715/// multiplying by a magic number. See:
2716/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
2717SDOperand DAGCombiner::BuildUDIV(SDNode *N) {
2718 MVT::ValueType VT = N->getValueType(0);
Chris Lattnere9936d12005-10-22 18:50:15 +00002719
2720 // Check to see if we can do this.
2721 if (!TLI.isTypeLegal(VT) || (VT != MVT::i32 && VT != MVT::i64))
2722 return SDOperand(); // BuildUDIV only operates on i32 or i64
2723 if (!TLI.isOperationLegal(ISD::MULHU, VT))
2724 return SDOperand(); // Make sure the target supports MULHU.
Nate Begeman69575232005-10-20 02:15:44 +00002725
2726 uint64_t d = cast<ConstantSDNode>(N->getOperand(1))->getValue();
2727 mu magics = (VT == MVT::i32) ? magicu32(d) : magicu64(d);
2728
2729 // Multiply the numerator (operand 0) by the magic value
2730 SDOperand Q = DAG.getNode(ISD::MULHU, VT, N->getOperand(0),
2731 DAG.getConstant(magics.m, VT));
2732 WorkList.push_back(Q.Val);
2733
2734 if (magics.a == 0) {
2735 return DAG.getNode(ISD::SRL, VT, Q,
2736 DAG.getConstant(magics.s, TLI.getShiftAmountTy()));
2737 } else {
2738 SDOperand NPQ = DAG.getNode(ISD::SUB, VT, N->getOperand(0), Q);
2739 WorkList.push_back(NPQ.Val);
2740 NPQ = DAG.getNode(ISD::SRL, VT, NPQ,
2741 DAG.getConstant(1, TLI.getShiftAmountTy()));
2742 WorkList.push_back(NPQ.Val);
2743 NPQ = DAG.getNode(ISD::ADD, VT, NPQ, Q);
2744 WorkList.push_back(NPQ.Val);
2745 return DAG.getNode(ISD::SRL, VT, NPQ,
2746 DAG.getConstant(magics.s-1, TLI.getShiftAmountTy()));
2747 }
2748}
2749
Nate Begeman1d4d4142005-09-01 00:19:25 +00002750// SelectionDAG::Combine - This is the entry point for the file.
2751//
Nate Begeman4ebd8052005-09-01 23:24:04 +00002752void SelectionDAG::Combine(bool RunningAfterLegalize) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002753 /// run - This is the main entry point to this class.
2754 ///
Nate Begeman4ebd8052005-09-01 23:24:04 +00002755 DAGCombiner(*this).Run(RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002756}