blob: 3124da575aa04c2d121d874291dab706dfec97b3 [file] [log] [blame]
Nate Begeman4ebd8052005-09-01 23:24:04 +00001//===-- DAGCombiner.cpp - Implement a DAG node combiner -------------------===//
Nate Begeman1d4d4142005-09-01 00:19:25 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Nate Begeman and is distributed under the
6// University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This pass combines dag nodes to form fewer, simpler DAG nodes. It can be run
11// both before and after the DAG is legalized.
12//
13// FIXME: Missing folds
14// sdiv, udiv, srem, urem (X, const) where X is an integer can be expanded into
15// a sequence of multiplies, shifts, and adds. This should be controlled by
16// some kind of hint from the target that int div is expensive.
17// various folds of mulh[s,u] by constants such as -1, powers of 2, etc.
18//
19// FIXME: Should add a corresponding version of fold AND with
20// ZERO_EXTEND/SIGN_EXTEND by converting them to an ANY_EXTEND node which
21// we don't have yet.
22//
Nate Begeman44728a72005-09-19 22:34:01 +000023// FIXME: select C, pow2, pow2 -> something smart
24// FIXME: trunc(select X, Y, Z) -> select X, trunc(Y), trunc(Z)
Nate Begeman44728a72005-09-19 22:34:01 +000025// FIXME: Dead stores -> nuke
Chris Lattner40c62d52005-10-18 06:04:22 +000026// FIXME: shr X, (and Y,31) -> shr X, Y (TRICKY!)
Nate Begeman1d4d4142005-09-01 00:19:25 +000027// FIXME: mul (x, const) -> shifts + adds
Nate Begeman1d4d4142005-09-01 00:19:25 +000028// FIXME: undef values
Nate Begeman4ebd8052005-09-01 23:24:04 +000029// FIXME: make truncate see through SIGN_EXTEND and AND
Nate Begeman4ebd8052005-09-01 23:24:04 +000030// FIXME: (sra (sra x, c1), c2) -> (sra x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +000031// FIXME: verify that getNode can't return extends with an operand whose type
32// is >= to that of the extend.
33// FIXME: divide by zero is currently left unfolded. do we want to turn this
34// into an undef?
Nate Begemanf845b452005-10-08 00:29:44 +000035// FIXME: select ne (select cc, 1, 0), 0, true, false -> select cc, true, false
Nate Begeman1d4d4142005-09-01 00:19:25 +000036//
37//===----------------------------------------------------------------------===//
38
39#define DEBUG_TYPE "dagcombine"
40#include "llvm/ADT/Statistic.h"
41#include "llvm/CodeGen/SelectionDAG.h"
Nate Begeman2300f552005-09-07 00:15:36 +000042#include "llvm/Support/Debug.h"
Nate Begeman1d4d4142005-09-01 00:19:25 +000043#include "llvm/Support/MathExtras.h"
44#include "llvm/Target/TargetLowering.h"
Chris Lattnera500fc62005-09-09 23:53:39 +000045#include <algorithm>
Nate Begeman1d4d4142005-09-01 00:19:25 +000046#include <cmath>
Chris Lattner2c2c6c62006-01-22 23:41:00 +000047#include <iostream>
Nate Begeman1d4d4142005-09-01 00:19:25 +000048using namespace llvm;
49
50namespace {
51 Statistic<> NodesCombined ("dagcombiner", "Number of dag nodes combined");
52
53 class DAGCombiner {
54 SelectionDAG &DAG;
55 TargetLowering &TLI;
Nate Begeman4ebd8052005-09-01 23:24:04 +000056 bool AfterLegalize;
Nate Begeman1d4d4142005-09-01 00:19:25 +000057
58 // Worklist of all of the nodes that need to be simplified.
59 std::vector<SDNode*> WorkList;
60
61 /// AddUsersToWorkList - When an instruction is simplified, add all users of
62 /// the instruction to the work lists because they might get more simplified
63 /// now.
64 ///
65 void AddUsersToWorkList(SDNode *N) {
66 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
Nate Begeman4ebd8052005-09-01 23:24:04 +000067 UI != UE; ++UI)
68 WorkList.push_back(*UI);
Nate Begeman1d4d4142005-09-01 00:19:25 +000069 }
70
71 /// removeFromWorkList - remove all instances of N from the worklist.
72 void removeFromWorkList(SDNode *N) {
73 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), N),
74 WorkList.end());
75 }
76
Chris Lattner01a22022005-10-10 22:04:48 +000077 SDOperand CombineTo(SDNode *N, const std::vector<SDOperand> &To) {
Chris Lattner87514ca2005-10-10 22:31:19 +000078 ++NodesCombined;
Chris Lattner01a22022005-10-10 22:04:48 +000079 DEBUG(std::cerr << "\nReplacing "; N->dump();
80 std::cerr << "\nWith: "; To[0].Val->dump();
81 std::cerr << " and " << To.size()-1 << " other values\n");
82 std::vector<SDNode*> NowDead;
83 DAG.ReplaceAllUsesWith(N, To, &NowDead);
84
85 // Push the new nodes and any users onto the worklist
86 for (unsigned i = 0, e = To.size(); i != e; ++i) {
87 WorkList.push_back(To[i].Val);
88 AddUsersToWorkList(To[i].Val);
89 }
90
91 // Nodes can end up on the worklist more than once. Make sure we do
92 // not process a node that has been replaced.
93 removeFromWorkList(N);
94 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
95 removeFromWorkList(NowDead[i]);
96
97 // Finally, since the node is now dead, remove it from the graph.
98 DAG.DeleteNode(N);
99 return SDOperand(N, 0);
100 }
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
Jim Laskeyd6e8d412005-12-23 20:08:28 +0000178 SDOperand visitLOCATION(SDNode *N);
179 SDOperand visitDEBUGLOC(SDNode *N);
180
Nate Begemancd4d58c2006-02-03 06:46:56 +0000181 SDOperand ReassociateOps(unsigned Opc, SDOperand LHS, SDOperand RHS);
182
Chris Lattner40c62d52005-10-18 06:04:22 +0000183 bool SimplifySelectOps(SDNode *SELECT, SDOperand LHS, SDOperand RHS);
Nate Begeman44728a72005-09-19 22:34:01 +0000184 SDOperand SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2);
185 SDOperand SimplifySelectCC(SDOperand N0, SDOperand N1, SDOperand N2,
186 SDOperand N3, ISD::CondCode CC);
Nate Begeman452d7be2005-09-16 00:54:12 +0000187 SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N0, SDOperand N1,
Nate Begemane17daeb2005-10-05 21:43:42 +0000188 ISD::CondCode Cond, bool foldBooleans = true);
Nate Begeman69575232005-10-20 02:15:44 +0000189
190 SDOperand BuildSDIV(SDNode *N);
191 SDOperand BuildUDIV(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000192public:
193 DAGCombiner(SelectionDAG &D)
Nate Begeman646d7e22005-09-02 21:18:40 +0000194 : DAG(D), TLI(D.getTargetLoweringInfo()), AfterLegalize(false) {}
Nate Begeman1d4d4142005-09-01 00:19:25 +0000195
196 /// Run - runs the dag combiner on all nodes in the work list
Nate Begeman4ebd8052005-09-01 23:24:04 +0000197 void Run(bool RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000198 };
199}
200
Nate Begeman69575232005-10-20 02:15:44 +0000201struct ms {
202 int64_t m; // magic number
203 int64_t s; // shift amount
204};
205
206struct mu {
207 uint64_t m; // magic number
208 int64_t a; // add indicator
209 int64_t s; // shift amount
210};
211
212/// magic - calculate the magic numbers required to codegen an integer sdiv as
213/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
214/// or -1.
215static ms magic32(int32_t d) {
216 int32_t p;
217 uint32_t ad, anc, delta, q1, r1, q2, r2, t;
218 const uint32_t two31 = 0x80000000U;
219 struct ms mag;
220
221 ad = abs(d);
222 t = two31 + ((uint32_t)d >> 31);
223 anc = t - 1 - t%ad; // absolute value of nc
224 p = 31; // initialize p
225 q1 = two31/anc; // initialize q1 = 2p/abs(nc)
226 r1 = two31 - q1*anc; // initialize r1 = rem(2p,abs(nc))
227 q2 = two31/ad; // initialize q2 = 2p/abs(d)
228 r2 = two31 - q2*ad; // initialize r2 = rem(2p,abs(d))
229 do {
230 p = p + 1;
231 q1 = 2*q1; // update q1 = 2p/abs(nc)
232 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
233 if (r1 >= anc) { // must be unsigned comparison
234 q1 = q1 + 1;
235 r1 = r1 - anc;
236 }
237 q2 = 2*q2; // update q2 = 2p/abs(d)
238 r2 = 2*r2; // update r2 = rem(2p/abs(d))
239 if (r2 >= ad) { // must be unsigned comparison
240 q2 = q2 + 1;
241 r2 = r2 - ad;
242 }
243 delta = ad - r2;
244 } while (q1 < delta || (q1 == delta && r1 == 0));
245
246 mag.m = (int32_t)(q2 + 1); // make sure to sign extend
247 if (d < 0) mag.m = -mag.m; // resulting magic number
248 mag.s = p - 32; // resulting shift
249 return mag;
250}
251
252/// magicu - calculate the magic numbers required to codegen an integer udiv as
253/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
254static mu magicu32(uint32_t d) {
255 int32_t p;
256 uint32_t nc, delta, q1, r1, q2, r2;
257 struct mu magu;
258 magu.a = 0; // initialize "add" indicator
259 nc = - 1 - (-d)%d;
260 p = 31; // initialize p
261 q1 = 0x80000000/nc; // initialize q1 = 2p/nc
262 r1 = 0x80000000 - q1*nc; // initialize r1 = rem(2p,nc)
263 q2 = 0x7FFFFFFF/d; // initialize q2 = (2p-1)/d
264 r2 = 0x7FFFFFFF - q2*d; // initialize r2 = rem((2p-1),d)
265 do {
266 p = p + 1;
267 if (r1 >= nc - r1 ) {
268 q1 = 2*q1 + 1; // update q1
269 r1 = 2*r1 - nc; // update r1
270 }
271 else {
272 q1 = 2*q1; // update q1
273 r1 = 2*r1; // update r1
274 }
275 if (r2 + 1 >= d - r2) {
276 if (q2 >= 0x7FFFFFFF) magu.a = 1;
277 q2 = 2*q2 + 1; // update q2
278 r2 = 2*r2 + 1 - d; // update r2
279 }
280 else {
281 if (q2 >= 0x80000000) magu.a = 1;
282 q2 = 2*q2; // update q2
283 r2 = 2*r2 + 1; // update r2
284 }
285 delta = d - 1 - r2;
286 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
287 magu.m = q2 + 1; // resulting magic number
288 magu.s = p - 32; // resulting shift
289 return magu;
290}
291
292/// magic - calculate the magic numbers required to codegen an integer sdiv as
293/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
294/// or -1.
295static ms magic64(int64_t d) {
296 int64_t p;
297 uint64_t ad, anc, delta, q1, r1, q2, r2, t;
298 const uint64_t two63 = 9223372036854775808ULL; // 2^63
299 struct ms mag;
300
Chris Lattnerf75f2a02005-10-20 17:01:00 +0000301 ad = d >= 0 ? d : -d;
Nate Begeman69575232005-10-20 02:15:44 +0000302 t = two63 + ((uint64_t)d >> 63);
303 anc = t - 1 - t%ad; // absolute value of nc
304 p = 63; // initialize p
305 q1 = two63/anc; // initialize q1 = 2p/abs(nc)
306 r1 = two63 - q1*anc; // initialize r1 = rem(2p,abs(nc))
307 q2 = two63/ad; // initialize q2 = 2p/abs(d)
308 r2 = two63 - q2*ad; // initialize r2 = rem(2p,abs(d))
309 do {
310 p = p + 1;
311 q1 = 2*q1; // update q1 = 2p/abs(nc)
312 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
313 if (r1 >= anc) { // must be unsigned comparison
314 q1 = q1 + 1;
315 r1 = r1 - anc;
316 }
317 q2 = 2*q2; // update q2 = 2p/abs(d)
318 r2 = 2*r2; // update r2 = rem(2p/abs(d))
319 if (r2 >= ad) { // must be unsigned comparison
320 q2 = q2 + 1;
321 r2 = r2 - ad;
322 }
323 delta = ad - r2;
324 } while (q1 < delta || (q1 == delta && r1 == 0));
325
326 mag.m = q2 + 1;
327 if (d < 0) mag.m = -mag.m; // resulting magic number
328 mag.s = p - 64; // resulting shift
329 return mag;
330}
331
332/// magicu - calculate the magic numbers required to codegen an integer udiv as
333/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
334static mu magicu64(uint64_t d)
335{
336 int64_t p;
337 uint64_t nc, delta, q1, r1, q2, r2;
338 struct mu magu;
339 magu.a = 0; // initialize "add" indicator
340 nc = - 1 - (-d)%d;
341 p = 63; // initialize p
342 q1 = 0x8000000000000000ull/nc; // initialize q1 = 2p/nc
343 r1 = 0x8000000000000000ull - q1*nc; // initialize r1 = rem(2p,nc)
344 q2 = 0x7FFFFFFFFFFFFFFFull/d; // initialize q2 = (2p-1)/d
345 r2 = 0x7FFFFFFFFFFFFFFFull - q2*d; // initialize r2 = rem((2p-1),d)
346 do {
347 p = p + 1;
348 if (r1 >= nc - r1 ) {
349 q1 = 2*q1 + 1; // update q1
350 r1 = 2*r1 - nc; // update r1
351 }
352 else {
353 q1 = 2*q1; // update q1
354 r1 = 2*r1; // update r1
355 }
356 if (r2 + 1 >= d - r2) {
357 if (q2 >= 0x7FFFFFFFFFFFFFFFull) magu.a = 1;
358 q2 = 2*q2 + 1; // update q2
359 r2 = 2*r2 + 1 - d; // update r2
360 }
361 else {
362 if (q2 >= 0x8000000000000000ull) magu.a = 1;
363 q2 = 2*q2; // update q2
364 r2 = 2*r2 + 1; // update r2
365 }
366 delta = d - 1 - r2;
367 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
368 magu.m = q2 + 1; // resulting magic number
369 magu.s = p - 64; // resulting shift
370 return magu;
371}
372
Nate Begeman4ebd8052005-09-01 23:24:04 +0000373// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
374// that selects between the values 1 and 0, making it equivalent to a setcc.
Nate Begeman646d7e22005-09-02 21:18:40 +0000375// Also, set the incoming LHS, RHS, and CC references to the appropriate
376// nodes based on the type of node we are checking. This simplifies life a
377// bit for the callers.
378static bool isSetCCEquivalent(SDOperand N, SDOperand &LHS, SDOperand &RHS,
379 SDOperand &CC) {
380 if (N.getOpcode() == ISD::SETCC) {
381 LHS = N.getOperand(0);
382 RHS = N.getOperand(1);
383 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000384 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000385 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000386 if (N.getOpcode() == ISD::SELECT_CC &&
387 N.getOperand(2).getOpcode() == ISD::Constant &&
388 N.getOperand(3).getOpcode() == ISD::Constant &&
389 cast<ConstantSDNode>(N.getOperand(2))->getValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000390 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
391 LHS = N.getOperand(0);
392 RHS = N.getOperand(1);
393 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000394 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000395 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000396 return false;
397}
398
Nate Begeman99801192005-09-07 23:25:52 +0000399// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
400// one use. If this is true, it allows the users to invert the operation for
401// free when it is profitable to do so.
402static bool isOneUseSetCC(SDOperand N) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000403 SDOperand N0, N1, N2;
Nate Begeman646d7e22005-09-02 21:18:40 +0000404 if (isSetCCEquivalent(N, N0, N1, N2) && N.Val->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000405 return true;
406 return false;
407}
408
Nate Begeman452d7be2005-09-16 00:54:12 +0000409// FIXME: This should probably go in the ISD class rather than being duplicated
410// in several files.
411static bool isCommutativeBinOp(unsigned Opcode) {
412 switch (Opcode) {
413 case ISD::ADD:
414 case ISD::MUL:
415 case ISD::AND:
416 case ISD::OR:
417 case ISD::XOR: return true;
418 default: return false; // FIXME: Need commutative info for user ops!
419 }
420}
421
Nate Begemancd4d58c2006-02-03 06:46:56 +0000422SDOperand DAGCombiner::ReassociateOps(unsigned Opc, SDOperand N0, SDOperand N1){
423 MVT::ValueType VT = N0.getValueType();
424 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
425 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
426 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
427 if (isa<ConstantSDNode>(N1)) {
428 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(1), N1);
429 WorkList.push_back(OpNode.Val);
430 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(0));
431 } else if (N0.hasOneUse()) {
432 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(0), N1);
433 WorkList.push_back(OpNode.Val);
434 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(1));
435 }
436 }
437 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
438 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
439 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
440 if (isa<ConstantSDNode>(N0)) {
441 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(1), N0);
442 WorkList.push_back(OpNode.Val);
443 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(0));
444 } else if (N1.hasOneUse()) {
445 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(0), N0);
446 WorkList.push_back(OpNode.Val);
447 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(1));
448 }
449 }
450 return SDOperand();
451}
452
Nate Begeman4ebd8052005-09-01 23:24:04 +0000453void DAGCombiner::Run(bool RunningAfterLegalize) {
454 // set the instance variable, so that the various visit routines may use it.
455 AfterLegalize = RunningAfterLegalize;
456
Nate Begeman646d7e22005-09-02 21:18:40 +0000457 // Add all the dag nodes to the worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000458 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
459 E = DAG.allnodes_end(); I != E; ++I)
460 WorkList.push_back(I);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000461
Chris Lattner95038592005-10-05 06:35:28 +0000462 // Create a dummy node (which is not added to allnodes), that adds a reference
463 // to the root node, preventing it from being deleted, and tracking any
464 // changes of the root.
465 HandleSDNode Dummy(DAG.getRoot());
466
Nate Begeman1d4d4142005-09-01 00:19:25 +0000467 // while the worklist isn't empty, inspect the node on the end of it and
468 // try and combine it.
469 while (!WorkList.empty()) {
470 SDNode *N = WorkList.back();
471 WorkList.pop_back();
472
473 // If N has no uses, it is dead. Make sure to revisit all N's operands once
Chris Lattner95038592005-10-05 06:35:28 +0000474 // N is deleted from the DAG, since they too may now be dead or may have a
475 // reduced number of uses, allowing other xforms.
476 if (N->use_empty() && N != &Dummy) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000477 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
478 WorkList.push_back(N->getOperand(i).Val);
479
Nate Begeman1d4d4142005-09-01 00:19:25 +0000480 removeFromWorkList(N);
Chris Lattner95038592005-10-05 06:35:28 +0000481 DAG.DeleteNode(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000482 continue;
483 }
484
Nate Begeman83e75ec2005-09-06 04:43:02 +0000485 SDOperand RV = visit(N);
486 if (RV.Val) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000487 ++NodesCombined;
Nate Begeman646d7e22005-09-02 21:18:40 +0000488 // If we get back the same node we passed in, rather than a new node or
489 // zero, we know that the node must have defined multiple values and
490 // CombineTo was used. Since CombineTo takes care of the worklist
491 // mechanics for us, we have no work to do in this case.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000492 if (RV.Val != N) {
Nate Begeman2300f552005-09-07 00:15:36 +0000493 DEBUG(std::cerr << "\nReplacing "; N->dump();
494 std::cerr << "\nWith: "; RV.Val->dump();
495 std::cerr << '\n');
Chris Lattner01a22022005-10-10 22:04:48 +0000496 std::vector<SDNode*> NowDead;
497 DAG.ReplaceAllUsesWith(N, std::vector<SDOperand>(1, RV), &NowDead);
Nate Begeman646d7e22005-09-02 21:18:40 +0000498
499 // Push the new node and any users onto the worklist
Nate Begeman83e75ec2005-09-06 04:43:02 +0000500 WorkList.push_back(RV.Val);
501 AddUsersToWorkList(RV.Val);
Nate Begeman646d7e22005-09-02 21:18:40 +0000502
503 // Nodes can end up on the worklist more than once. Make sure we do
504 // not process a node that has been replaced.
505 removeFromWorkList(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000506 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
507 removeFromWorkList(NowDead[i]);
Chris Lattner5c46f742005-10-05 06:11:08 +0000508
509 // Finally, since the node is now dead, remove it from the graph.
510 DAG.DeleteNode(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000511 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000512 }
513 }
Chris Lattner95038592005-10-05 06:35:28 +0000514
515 // If the root changed (e.g. it was a dead load, update the root).
516 DAG.setRoot(Dummy.getValue());
Nate Begeman1d4d4142005-09-01 00:19:25 +0000517}
518
Nate Begeman83e75ec2005-09-06 04:43:02 +0000519SDOperand DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000520 switch(N->getOpcode()) {
521 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +0000522 case ISD::TokenFactor: return visitTokenFactor(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000523 case ISD::ADD: return visitADD(N);
524 case ISD::SUB: return visitSUB(N);
525 case ISD::MUL: return visitMUL(N);
526 case ISD::SDIV: return visitSDIV(N);
527 case ISD::UDIV: return visitUDIV(N);
528 case ISD::SREM: return visitSREM(N);
529 case ISD::UREM: return visitUREM(N);
530 case ISD::MULHU: return visitMULHU(N);
531 case ISD::MULHS: return visitMULHS(N);
532 case ISD::AND: return visitAND(N);
533 case ISD::OR: return visitOR(N);
534 case ISD::XOR: return visitXOR(N);
535 case ISD::SHL: return visitSHL(N);
536 case ISD::SRA: return visitSRA(N);
537 case ISD::SRL: return visitSRL(N);
538 case ISD::CTLZ: return visitCTLZ(N);
539 case ISD::CTTZ: return visitCTTZ(N);
540 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000541 case ISD::SELECT: return visitSELECT(N);
542 case ISD::SELECT_CC: return visitSELECT_CC(N);
543 case ISD::SETCC: return visitSETCC(N);
Nate Begeman5054f162005-10-14 01:12:21 +0000544 case ISD::ADD_PARTS: return visitADD_PARTS(N);
545 case ISD::SUB_PARTS: return visitSUB_PARTS(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000546 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
547 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
548 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
549 case ISD::TRUNCATE: return visitTRUNCATE(N);
Chris Lattner94683772005-12-23 05:30:37 +0000550 case ISD::BIT_CONVERT: return visitBIT_CONVERT(N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000551 case ISD::FADD: return visitFADD(N);
552 case ISD::FSUB: return visitFSUB(N);
553 case ISD::FMUL: return visitFMUL(N);
554 case ISD::FDIV: return visitFDIV(N);
555 case ISD::FREM: return visitFREM(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000556 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
557 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
558 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
559 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
560 case ISD::FP_ROUND: return visitFP_ROUND(N);
561 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
562 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
563 case ISD::FNEG: return visitFNEG(N);
564 case ISD::FABS: return visitFABS(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000565 case ISD::BRCOND: return visitBRCOND(N);
566 case ISD::BRCONDTWOWAY: return visitBRCONDTWOWAY(N);
567 case ISD::BR_CC: return visitBR_CC(N);
568 case ISD::BRTWOWAY_CC: return visitBRTWOWAY_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000569 case ISD::LOAD: return visitLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000570 case ISD::STORE: return visitSTORE(N);
Jim Laskeyd6e8d412005-12-23 20:08:28 +0000571 case ISD::LOCATION: return visitLOCATION(N);
572 case ISD::DEBUG_LOC: return visitDEBUGLOC(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000573 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000574 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000575}
576
Nate Begeman83e75ec2005-09-06 04:43:02 +0000577SDOperand DAGCombiner::visitTokenFactor(SDNode *N) {
Nate Begemanded49632005-10-13 03:11:28 +0000578 std::vector<SDOperand> Ops;
579 bool Changed = false;
580
Nate Begeman1d4d4142005-09-01 00:19:25 +0000581 // If the token factor has two operands and one is the entry token, replace
582 // the token factor with the other operand.
583 if (N->getNumOperands() == 2) {
584 if (N->getOperand(0).getOpcode() == ISD::EntryToken)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000585 return N->getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000586 if (N->getOperand(1).getOpcode() == ISD::EntryToken)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000587 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000588 }
Chris Lattner24edbb72005-10-13 22:10:05 +0000589
Nate Begemanded49632005-10-13 03:11:28 +0000590 // fold (tokenfactor (tokenfactor)) -> tokenfactor
591 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
592 SDOperand Op = N->getOperand(i);
593 if (Op.getOpcode() == ISD::TokenFactor && Op.hasOneUse()) {
594 Changed = true;
595 for (unsigned j = 0, e = Op.getNumOperands(); j != e; ++j)
596 Ops.push_back(Op.getOperand(j));
597 } else {
598 Ops.push_back(Op);
599 }
600 }
601 if (Changed)
602 return DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000603 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000604}
605
Nate Begeman83e75ec2005-09-06 04:43:02 +0000606SDOperand DAGCombiner::visitADD(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000607 SDOperand N0 = N->getOperand(0);
608 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000609 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
610 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemanf89d78d2005-09-07 16:09:19 +0000611 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000612
613 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000614 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000615 return DAG.getNode(ISD::ADD, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000616 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000617 if (N0C && !N1C)
618 return DAG.getNode(ISD::ADD, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000619 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000620 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000621 return N0;
Chris Lattner4aafb4f2006-01-12 20:22:43 +0000622 // fold ((c1-A)+c2) -> (c1+c2)-A
623 if (N1C && N0.getOpcode() == ISD::SUB)
624 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
625 return DAG.getNode(ISD::SUB, VT,
626 DAG.getConstant(N1C->getValue()+N0C->getValue(), VT),
627 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000628 // reassociate add
629 SDOperand RADD = ReassociateOps(ISD::ADD, N0, N1);
630 if (RADD.Val != 0)
631 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000632 // fold ((0-A) + B) -> B-A
633 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
634 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000635 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000636 // fold (A + (0-B)) -> A-B
637 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
638 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000639 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +0000640 // fold (A+(B-A)) -> B
641 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000642 return N1.getOperand(0);
643 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000644}
645
Nate Begeman83e75ec2005-09-06 04:43:02 +0000646SDOperand DAGCombiner::visitSUB(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000647 SDOperand N0 = N->getOperand(0);
648 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000649 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
650 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000651 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000652
Chris Lattner854077d2005-10-17 01:07:11 +0000653 // fold (sub x, x) -> 0
654 if (N0 == N1)
655 return DAG.getConstant(0, N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000656 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000657 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000658 return DAG.getNode(ISD::SUB, VT, N0, N1);
Chris Lattner05b57432005-10-11 06:07:15 +0000659 // fold (sub x, c) -> (add x, -c)
660 if (N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000661 return DAG.getNode(ISD::ADD, VT, N0, DAG.getConstant(-N1C->getValue(), VT));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000662 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +0000663 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000664 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000665 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +0000666 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000667 return N0.getOperand(0);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000668 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000669}
670
Nate Begeman83e75ec2005-09-06 04:43:02 +0000671SDOperand DAGCombiner::visitMUL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000672 SDOperand N0 = N->getOperand(0);
673 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000674 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
675 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman223df222005-09-08 20:18:10 +0000676 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000677
678 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000679 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000680 return DAG.getNode(ISD::MUL, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000681 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000682 if (N0C && !N1C)
683 return DAG.getNode(ISD::MUL, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000684 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000685 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000686 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000687 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +0000688 if (N1C && N1C->isAllOnesValue())
Nate Begeman405e3ec2005-10-21 00:02:42 +0000689 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000690 // fold (mul x, (1 << c)) -> x << c
Nate Begeman646d7e22005-09-02 21:18:40 +0000691 if (N1C && isPowerOf2_64(N1C->getValue()))
Chris Lattner3e6099b2005-10-30 06:41:49 +0000692 return DAG.getNode(ISD::SHL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000693 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000694 TLI.getShiftAmountTy()));
Chris Lattner3e6099b2005-10-30 06:41:49 +0000695 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
696 if (N1C && isPowerOf2_64(-N1C->getSignExtended())) {
697 // FIXME: If the input is something that is easily negated (e.g. a
698 // single-use add), we should put the negate there.
699 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT),
700 DAG.getNode(ISD::SHL, VT, N0,
701 DAG.getConstant(Log2_64(-N1C->getSignExtended()),
702 TLI.getShiftAmountTy())));
703 }
Nate Begemancd4d58c2006-02-03 06:46:56 +0000704 // reassociate mul
705 SDOperand RMUL = ReassociateOps(ISD::MUL, N0, N1);
706 if (RMUL.Val != 0)
707 return RMUL;
Nate Begeman83e75ec2005-09-06 04:43:02 +0000708 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000709}
710
Nate Begeman83e75ec2005-09-06 04:43:02 +0000711SDOperand DAGCombiner::visitSDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000712 SDOperand N0 = N->getOperand(0);
713 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000714 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
715 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000716 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000717
718 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000719 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000720 return DAG.getNode(ISD::SDIV, VT, N0, N1);
Nate Begeman405e3ec2005-10-21 00:02:42 +0000721 // fold (sdiv X, 1) -> X
722 if (N1C && N1C->getSignExtended() == 1LL)
723 return N0;
724 // fold (sdiv X, -1) -> 0-X
725 if (N1C && N1C->isAllOnesValue())
726 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +0000727 // If we know the sign bits of both operands are zero, strength reduce to a
728 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
729 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000730 if (TLI.MaskedValueIsZero(N1, SignBit) &&
731 TLI.MaskedValueIsZero(N0, SignBit))
Chris Lattner094c8fc2005-10-07 06:10:46 +0000732 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
Nate Begeman405e3ec2005-10-21 00:02:42 +0000733 // fold (sdiv X, pow2) -> (add (sra X, log(pow2)), (srl X, sizeof(X)-1))
734 if (N1C && N1C->getValue() && !TLI.isIntDivCheap() &&
735 (isPowerOf2_64(N1C->getSignExtended()) ||
736 isPowerOf2_64(-N1C->getSignExtended()))) {
737 // If dividing by powers of two is cheap, then don't perform the following
738 // fold.
739 if (TLI.isPow2DivCheap())
740 return SDOperand();
741 int64_t pow2 = N1C->getSignExtended();
742 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
743 SDOperand SRL = DAG.getNode(ISD::SRL, VT, N0,
744 DAG.getConstant(MVT::getSizeInBits(VT)-1,
745 TLI.getShiftAmountTy()));
746 WorkList.push_back(SRL.Val);
747 SDOperand SGN = DAG.getNode(ISD::ADD, VT, N0, SRL);
748 WorkList.push_back(SGN.Val);
749 SDOperand SRA = DAG.getNode(ISD::SRA, VT, SGN,
750 DAG.getConstant(Log2_64(abs2),
751 TLI.getShiftAmountTy()));
752 // If we're dividing by a positive value, we're done. Otherwise, we must
753 // negate the result.
754 if (pow2 > 0)
755 return SRA;
756 WorkList.push_back(SRA.Val);
757 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), SRA);
758 }
Nate Begeman69575232005-10-20 02:15:44 +0000759 // if integer divide is expensive and we satisfy the requirements, emit an
760 // alternate sequence.
Nate Begeman405e3ec2005-10-21 00:02:42 +0000761 if (N1C && (N1C->getSignExtended() < -1 || N1C->getSignExtended() > 1) &&
Chris Lattnere9936d12005-10-22 18:50:15 +0000762 !TLI.isIntDivCheap()) {
763 SDOperand Op = BuildSDIV(N);
764 if (Op.Val) return Op;
Nate Begeman69575232005-10-20 02:15:44 +0000765 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000766 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000767}
768
Nate Begeman83e75ec2005-09-06 04:43:02 +0000769SDOperand DAGCombiner::visitUDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000770 SDOperand N0 = N->getOperand(0);
771 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000772 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
773 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000774 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000775
776 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000777 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000778 return DAG.getNode(ISD::UDIV, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000779 // fold (udiv x, (1 << c)) -> x >>u c
Nate Begeman646d7e22005-09-02 21:18:40 +0000780 if (N1C && isPowerOf2_64(N1C->getValue()))
Nate Begeman1d4d4142005-09-01 00:19:25 +0000781 return DAG.getNode(ISD::SRL, N->getValueType(0), N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000782 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000783 TLI.getShiftAmountTy()));
Nate Begeman69575232005-10-20 02:15:44 +0000784 // fold (udiv x, c) -> alternate
Chris Lattnere9936d12005-10-22 18:50:15 +0000785 if (N1C && N1C->getValue() && !TLI.isIntDivCheap()) {
786 SDOperand Op = BuildUDIV(N);
787 if (Op.Val) return Op;
788 }
789
Nate Begeman83e75ec2005-09-06 04:43:02 +0000790 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000791}
792
Nate Begeman83e75ec2005-09-06 04:43:02 +0000793SDOperand DAGCombiner::visitSREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000794 SDOperand N0 = N->getOperand(0);
795 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000796 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
797 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +0000798 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000799
800 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000801 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000802 return DAG.getNode(ISD::SREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +0000803 // If we know the sign bits of both operands are zero, strength reduce to a
804 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
805 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000806 if (TLI.MaskedValueIsZero(N1, SignBit) &&
807 TLI.MaskedValueIsZero(N0, SignBit))
Nate Begemana148d982006-01-18 22:35:16 +0000808 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000809 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000810}
811
Nate Begeman83e75ec2005-09-06 04:43:02 +0000812SDOperand DAGCombiner::visitUREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000813 SDOperand N0 = N->getOperand(0);
814 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000815 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
816 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +0000817 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000818
819 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000820 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000821 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +0000822 // fold (urem x, pow2) -> (and x, pow2-1)
823 if (N1C && !N1C->isNullValue() && isPowerOf2_64(N1C->getValue()))
Nate Begemana148d982006-01-18 22:35:16 +0000824 return DAG.getNode(ISD::AND, VT, N0, DAG.getConstant(N1C->getValue()-1,VT));
Nate Begeman83e75ec2005-09-06 04:43:02 +0000825 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000826}
827
Nate Begeman83e75ec2005-09-06 04:43:02 +0000828SDOperand DAGCombiner::visitMULHS(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000829 SDOperand N0 = N->getOperand(0);
830 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000831 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000832
833 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000834 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000835 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000836 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Nate Begeman646d7e22005-09-02 21:18:40 +0000837 if (N1C && N1C->getValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +0000838 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
839 DAG.getConstant(MVT::getSizeInBits(N0.getValueType())-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +0000840 TLI.getShiftAmountTy()));
841 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000842}
843
Nate Begeman83e75ec2005-09-06 04:43:02 +0000844SDOperand DAGCombiner::visitMULHU(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000845 SDOperand N0 = N->getOperand(0);
846 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000847 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000848
849 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000850 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000851 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000852 // fold (mulhu x, 1) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000853 if (N1C && N1C->getValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000854 return DAG.getConstant(0, N0.getValueType());
855 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000856}
857
Nate Begeman83e75ec2005-09-06 04:43:02 +0000858SDOperand DAGCombiner::visitAND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000859 SDOperand N0 = N->getOperand(0);
860 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000861 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +0000862 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
863 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000864 MVT::ValueType VT = N1.getValueType();
Nate Begeman83e75ec2005-09-06 04:43:02 +0000865 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000866
867 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000868 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000869 return DAG.getNode(ISD::AND, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000870 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000871 if (N0C && !N1C)
872 return DAG.getNode(ISD::AND, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000873 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000874 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000875 return N0;
876 // if (and x, c) is known to be zero, return 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000877 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000878 return DAG.getConstant(0, VT);
879 // fold (and x, c) -> x iff (x & ~c) == 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000880 if (N1C &&
881 TLI.MaskedValueIsZero(N0, ~N1C->getValue() & (~0ULL>>(64-OpSizeInBits))))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000882 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +0000883 // reassociate and
884 SDOperand RAND = ReassociateOps(ISD::AND, N0, N1);
885 if (RAND.Val != 0)
886 return RAND;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000887 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
Nate Begeman5dc7e862005-11-02 18:42:59 +0000888 if (N1C && N0.getOpcode() == ISD::SIGN_EXTEND_INREG) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000889 unsigned ExtendBits =
Jeff Cohen06d9b4a2005-11-12 00:59:01 +0000890 MVT::getSizeInBits(cast<VTSDNode>(N0.getOperand(1))->getVT());
891 if (ExtendBits == 64 || ((N1C->getValue() & (~0ULL << ExtendBits)) == 0))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000892 return DAG.getNode(ISD::AND, VT, N0.getOperand(0), N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000893 }
894 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
Nate Begeman5dc7e862005-11-02 18:42:59 +0000895 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +0000896 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Nate Begeman646d7e22005-09-02 21:18:40 +0000897 if ((ORI->getValue() & N1C->getValue()) == N1C->getValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000898 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +0000899 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
900 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
901 unsigned InBits = MVT::getSizeInBits(N0.getOperand(0).getValueType());
902 if (TLI.MaskedValueIsZero(N0.getOperand(0),
903 ~N1C->getValue() & ((1ULL << InBits)-1))) {
904 // We actually want to replace all uses of the any_extend with the
905 // zero_extend, to avoid duplicating things. This will later cause this
906 // AND to be folded.
907 CombineTo(N0.Val, DAG.getNode(ISD::ZERO_EXTEND, N0.getValueType(),
908 N0.getOperand(0)));
909 return SDOperand();
910 }
911 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000912 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
913 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
914 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
915 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
916
917 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
918 MVT::isInteger(LL.getValueType())) {
919 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
920 if (cast<ConstantSDNode>(LR)->getValue() == 0 && Op1 == ISD::SETEQ) {
921 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
922 WorkList.push_back(ORNode.Val);
923 return DAG.getSetCC(VT, ORNode, LR, Op1);
924 }
925 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
926 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
927 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
928 WorkList.push_back(ANDNode.Val);
929 return DAG.getSetCC(VT, ANDNode, LR, Op1);
930 }
931 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
932 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
933 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
934 WorkList.push_back(ORNode.Val);
935 return DAG.getSetCC(VT, ORNode, LR, Op1);
936 }
937 }
938 // canonicalize equivalent to ll == rl
939 if (LL == RR && LR == RL) {
940 Op1 = ISD::getSetCCSwappedOperands(Op1);
941 std::swap(RL, RR);
942 }
943 if (LL == RL && LR == RR) {
944 bool isInteger = MVT::isInteger(LL.getValueType());
945 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
946 if (Result != ISD::SETCC_INVALID)
947 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
948 }
949 }
950 // fold (and (zext x), (zext y)) -> (zext (and x, y))
951 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
952 N1.getOpcode() == ISD::ZERO_EXTEND &&
953 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
954 SDOperand ANDNode = DAG.getNode(ISD::AND, N0.getOperand(0).getValueType(),
955 N0.getOperand(0), N1.getOperand(0));
956 WorkList.push_back(ANDNode.Val);
957 return DAG.getNode(ISD::ZERO_EXTEND, VT, ANDNode);
958 }
Nate Begeman61af66e2006-01-28 01:06:30 +0000959 // fold (and (shl/srl/sra x), (shl/srl/sra y)) -> (shl/srl/sra (and x, y))
Nate Begeman452d7be2005-09-16 00:54:12 +0000960 if (((N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SHL) ||
Nate Begeman61af66e2006-01-28 01:06:30 +0000961 (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SRL) ||
962 (N0.getOpcode() == ISD::SRA && N1.getOpcode() == ISD::SRA)) &&
Nate Begeman452d7be2005-09-16 00:54:12 +0000963 N0.getOperand(1) == N1.getOperand(1)) {
964 SDOperand ANDNode = DAG.getNode(ISD::AND, N0.getOperand(0).getValueType(),
965 N0.getOperand(0), N1.getOperand(0));
966 WorkList.push_back(ANDNode.Val);
967 return DAG.getNode(N0.getOpcode(), VT, ANDNode, N0.getOperand(1));
968 }
Chris Lattner85d63bb2005-10-15 22:18:08 +0000969 // fold (and (sra)) -> (and (srl)) when possible.
Nate Begeman5dc7e862005-11-02 18:42:59 +0000970 if (N0.getOpcode() == ISD::SRA && N0.Val->hasOneUse()) {
Chris Lattner85d63bb2005-10-15 22:18:08 +0000971 if (ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
972 // If the RHS of the AND has zeros where the sign bits of the SRA will
973 // land, turn the SRA into an SRL.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000974 if (TLI.MaskedValueIsZero(N1, (~0ULL << (OpSizeInBits-N01C->getValue())) &
975 (~0ULL>>(64-OpSizeInBits)))) {
Chris Lattner85d63bb2005-10-15 22:18:08 +0000976 WorkList.push_back(N);
977 CombineTo(N0.Val, DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
978 N0.getOperand(1)));
979 return SDOperand();
980 }
981 }
Nate Begeman5dc7e862005-11-02 18:42:59 +0000982 }
Nate Begemanded49632005-10-13 03:11:28 +0000983 // fold (zext_inreg (extload x)) -> (zextload x)
Nate Begeman5054f162005-10-14 01:12:21 +0000984 if (N0.getOpcode() == ISD::EXTLOAD) {
Nate Begemanded49632005-10-13 03:11:28 +0000985 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +0000986 // If we zero all the possible extended bits, then we can turn this into
987 // a zextload if we are running before legalize or the operation is legal.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000988 if (TLI.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Chris Lattner67a44cd2005-10-13 18:16:34 +0000989 (!AfterLegalize || TLI.isOperationLegal(ISD::ZEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +0000990 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
991 N0.getOperand(1), N0.getOperand(2),
992 EVT);
Nate Begemanded49632005-10-13 03:11:28 +0000993 WorkList.push_back(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +0000994 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Nate Begemanded49632005-10-13 03:11:28 +0000995 return SDOperand();
996 }
997 }
998 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Chris Lattner40c62d52005-10-18 06:04:22 +0000999 if (N0.getOpcode() == ISD::SEXTLOAD && N0.hasOneUse()) {
Nate Begemanded49632005-10-13 03:11:28 +00001000 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001001 // If we zero all the possible extended bits, then we can turn this into
1002 // a zextload if we are running before legalize or the operation is legal.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001003 if (TLI.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Nate Begemanbfd65a02005-10-13 18:34:58 +00001004 (!AfterLegalize || TLI.isOperationLegal(ISD::ZEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001005 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1006 N0.getOperand(1), N0.getOperand(2),
1007 EVT);
Nate Begemanded49632005-10-13 03:11:28 +00001008 WorkList.push_back(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001009 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Nate Begemanded49632005-10-13 03:11:28 +00001010 return SDOperand();
1011 }
1012 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001013 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001014}
1015
Nate Begeman83e75ec2005-09-06 04:43:02 +00001016SDOperand DAGCombiner::visitOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001017 SDOperand N0 = N->getOperand(0);
1018 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001019 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001020 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1021 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001022 MVT::ValueType VT = N1.getValueType();
1023 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001024
1025 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001026 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001027 return DAG.getNode(ISD::OR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001028 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001029 if (N0C && !N1C)
1030 return DAG.getNode(ISD::OR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001031 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001032 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001033 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001034 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001035 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001036 return N1;
1037 // fold (or x, c) -> c iff (x & ~c) == 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001038 if (N1C &&
1039 TLI.MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits))))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001040 return N1;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001041 // reassociate or
1042 SDOperand ROR = ReassociateOps(ISD::OR, N0, N1);
1043 if (ROR.Val != 0)
1044 return ROR;
1045 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
1046 if (N1C && N0.getOpcode() == ISD::AND && N0.Val->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00001047 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00001048 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
1049 return DAG.getNode(ISD::AND, VT, DAG.getNode(ISD::OR, VT, N0.getOperand(0),
1050 N1),
1051 DAG.getConstant(N1C->getValue() | C1->getValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00001052 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001053 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
1054 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1055 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1056 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1057
1058 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1059 MVT::isInteger(LL.getValueType())) {
1060 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
1061 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
1062 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
1063 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
1064 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
1065 WorkList.push_back(ORNode.Val);
1066 return DAG.getSetCC(VT, ORNode, LR, Op1);
1067 }
1068 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
1069 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
1070 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
1071 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
1072 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
1073 WorkList.push_back(ANDNode.Val);
1074 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1075 }
1076 }
1077 // canonicalize equivalent to ll == rl
1078 if (LL == RR && LR == RL) {
1079 Op1 = ISD::getSetCCSwappedOperands(Op1);
1080 std::swap(RL, RR);
1081 }
1082 if (LL == RL && LR == RR) {
1083 bool isInteger = MVT::isInteger(LL.getValueType());
1084 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
1085 if (Result != ISD::SETCC_INVALID)
1086 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1087 }
1088 }
1089 // fold (or (zext x), (zext y)) -> (zext (or x, y))
1090 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
1091 N1.getOpcode() == ISD::ZERO_EXTEND &&
1092 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1093 SDOperand ORNode = DAG.getNode(ISD::OR, N0.getOperand(0).getValueType(),
1094 N0.getOperand(0), N1.getOperand(0));
1095 WorkList.push_back(ORNode.Val);
1096 return DAG.getNode(ISD::ZERO_EXTEND, VT, ORNode);
1097 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00001098 // fold (or (shl/srl/sra x), (shl/srl/sra y)) -> (shl/srl/sra (or x, y))
1099 if (((N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SHL) ||
1100 (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SRL) ||
1101 (N0.getOpcode() == ISD::SRA && N1.getOpcode() == ISD::SRA)) &&
1102 N0.getOperand(1) == N1.getOperand(1)) {
1103 SDOperand ORNode = DAG.getNode(ISD::OR, N0.getOperand(0).getValueType(),
1104 N0.getOperand(0), N1.getOperand(0));
1105 WorkList.push_back(ORNode.Val);
1106 return DAG.getNode(N0.getOpcode(), VT, ORNode, N0.getOperand(1));
1107 }
Nate Begeman35ef9132006-01-11 21:21:00 +00001108 // canonicalize shl to left side in a shl/srl pair, to match rotate
1109 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
1110 std::swap(N0, N1);
1111 // check for rotl, rotr
1112 if (N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SRL &&
1113 N0.getOperand(0) == N1.getOperand(0) &&
Chris Lattneraf551bc2006-01-12 18:57:33 +00001114 TLI.isOperationLegal(ISD::ROTL, VT) && TLI.isTypeLegal(VT)) {
Nate Begeman35ef9132006-01-11 21:21:00 +00001115 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
1116 if (N0.getOperand(1).getOpcode() == ISD::Constant &&
1117 N1.getOperand(1).getOpcode() == ISD::Constant) {
1118 uint64_t c1val = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1119 uint64_t c2val = cast<ConstantSDNode>(N1.getOperand(1))->getValue();
1120 if ((c1val + c2val) == OpSizeInBits)
1121 return DAG.getNode(ISD::ROTL, VT, N0.getOperand(0), N0.getOperand(1));
1122 }
1123 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
1124 if (N1.getOperand(1).getOpcode() == ISD::SUB &&
1125 N0.getOperand(1) == N1.getOperand(1).getOperand(1))
1126 if (ConstantSDNode *SUBC =
1127 dyn_cast<ConstantSDNode>(N1.getOperand(1).getOperand(0)))
1128 if (SUBC->getValue() == OpSizeInBits)
1129 return DAG.getNode(ISD::ROTL, VT, N0.getOperand(0), N0.getOperand(1));
1130 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
1131 if (N0.getOperand(1).getOpcode() == ISD::SUB &&
1132 N1.getOperand(1) == N0.getOperand(1).getOperand(1))
1133 if (ConstantSDNode *SUBC =
1134 dyn_cast<ConstantSDNode>(N0.getOperand(1).getOperand(0)))
1135 if (SUBC->getValue() == OpSizeInBits) {
Chris Lattneraf551bc2006-01-12 18:57:33 +00001136 if (TLI.isOperationLegal(ISD::ROTR, VT) && TLI.isTypeLegal(VT))
Nate Begeman35ef9132006-01-11 21:21:00 +00001137 return DAG.getNode(ISD::ROTR, VT, N0.getOperand(0),
1138 N1.getOperand(1));
1139 else
1140 return DAG.getNode(ISD::ROTL, VT, N0.getOperand(0),
1141 N0.getOperand(1));
1142 }
1143 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001144 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001145}
1146
Nate Begeman83e75ec2005-09-06 04:43:02 +00001147SDOperand DAGCombiner::visitXOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001148 SDOperand N0 = N->getOperand(0);
1149 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001150 SDOperand LHS, RHS, CC;
1151 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1152 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001153 MVT::ValueType VT = N0.getValueType();
1154
1155 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001156 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001157 return DAG.getNode(ISD::XOR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001158 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001159 if (N0C && !N1C)
1160 return DAG.getNode(ISD::XOR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001161 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001162 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001163 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001164 // reassociate xor
1165 SDOperand RXOR = ReassociateOps(ISD::XOR, N0, N1);
1166 if (RXOR.Val != 0)
1167 return RXOR;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001168 // fold !(x cc y) -> (x !cc y)
Nate Begeman646d7e22005-09-02 21:18:40 +00001169 if (N1C && N1C->getValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
1170 bool isInt = MVT::isInteger(LHS.getValueType());
1171 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
1172 isInt);
1173 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001174 return DAG.getSetCC(VT, LHS, RHS, NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00001175 if (N0.getOpcode() == ISD::SELECT_CC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001176 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00001177 assert(0 && "Unhandled SetCC Equivalent!");
1178 abort();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001179 }
Nate Begeman99801192005-09-07 23:25:52 +00001180 // fold !(x or y) -> (!x and !y) iff x or y are setcc
1181 if (N1C && N1C->getValue() == 1 &&
1182 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001183 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00001184 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
1185 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001186 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1187 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Nate Begeman99801192005-09-07 23:25:52 +00001188 WorkList.push_back(LHS.Val); WorkList.push_back(RHS.Val);
1189 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001190 }
1191 }
Nate Begeman99801192005-09-07 23:25:52 +00001192 // fold !(x or y) -> (!x and !y) iff x or y are constants
1193 if (N1C && N1C->isAllOnesValue() &&
1194 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001195 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00001196 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
1197 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001198 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1199 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Nate Begeman99801192005-09-07 23:25:52 +00001200 WorkList.push_back(LHS.Val); WorkList.push_back(RHS.Val);
1201 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001202 }
1203 }
Nate Begeman223df222005-09-08 20:18:10 +00001204 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
1205 if (N1C && N0.getOpcode() == ISD::XOR) {
1206 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
1207 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
1208 if (N00C)
1209 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
1210 DAG.getConstant(N1C->getValue()^N00C->getValue(), VT));
1211 if (N01C)
1212 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
1213 DAG.getConstant(N1C->getValue()^N01C->getValue(), VT));
1214 }
1215 // fold (xor x, x) -> 0
1216 if (N0 == N1)
1217 return DAG.getConstant(0, VT);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001218 // fold (xor (zext x), (zext y)) -> (zext (xor x, y))
1219 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
1220 N1.getOpcode() == ISD::ZERO_EXTEND &&
1221 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1222 SDOperand XORNode = DAG.getNode(ISD::XOR, N0.getOperand(0).getValueType(),
1223 N0.getOperand(0), N1.getOperand(0));
1224 WorkList.push_back(XORNode.Val);
1225 return DAG.getNode(ISD::ZERO_EXTEND, VT, XORNode);
1226 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00001227 // fold (xor (shl/srl/sra x), (shl/srl/sra y)) -> (shl/srl/sra (xor x, y))
1228 if (((N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SHL) ||
1229 (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SRL) ||
1230 (N0.getOpcode() == ISD::SRA && N1.getOpcode() == ISD::SRA)) &&
1231 N0.getOperand(1) == N1.getOperand(1)) {
1232 SDOperand XORNode = DAG.getNode(ISD::XOR, N0.getOperand(0).getValueType(),
1233 N0.getOperand(0), N1.getOperand(0));
1234 WorkList.push_back(XORNode.Val);
1235 return DAG.getNode(N0.getOpcode(), VT, XORNode, N0.getOperand(1));
1236 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001237 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001238}
1239
Nate Begeman83e75ec2005-09-06 04:43:02 +00001240SDOperand DAGCombiner::visitSHL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001241 SDOperand N0 = N->getOperand(0);
1242 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001243 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1244 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001245 MVT::ValueType VT = N0.getValueType();
1246 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1247
1248 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001249 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001250 return DAG.getNode(ISD::SHL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001251 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001252 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001253 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001254 // fold (shl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001255 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001256 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001257 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001258 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001259 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001260 // if (shl x, c) is known to be zero, return 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001261 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001262 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001263 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001264 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001265 N0.getOperand(1).getOpcode() == ISD::Constant) {
1266 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001267 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001268 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001269 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001270 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001271 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001272 }
1273 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
1274 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001275 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001276 N0.getOperand(1).getOpcode() == ISD::Constant) {
1277 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001278 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001279 SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
1280 DAG.getConstant(~0ULL << c1, VT));
1281 if (c2 > c1)
1282 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001283 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001284 else
Nate Begeman83e75ec2005-09-06 04:43:02 +00001285 return DAG.getNode(ISD::SRL, VT, Mask,
1286 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001287 }
1288 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001289 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +00001290 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001291 DAG.getConstant(~0ULL << N1C->getValue(), VT));
1292 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001293}
1294
Nate Begeman83e75ec2005-09-06 04:43:02 +00001295SDOperand DAGCombiner::visitSRA(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001296 SDOperand N0 = N->getOperand(0);
1297 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001298 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1299 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001300 MVT::ValueType VT = N0.getValueType();
1301 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1302
1303 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001304 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001305 return DAG.getNode(ISD::SRA, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001306 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001307 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001308 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001309 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001310 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001311 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001312 // fold (sra x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001313 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001314 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001315 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001316 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001317 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001318 // If the sign bit is known to be zero, switch this to a SRL.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001319 if (TLI.MaskedValueIsZero(N0, (1ULL << (OpSizeInBits-1))))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001320 return DAG.getNode(ISD::SRL, VT, N0, N1);
1321 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001322}
1323
Nate Begeman83e75ec2005-09-06 04:43:02 +00001324SDOperand DAGCombiner::visitSRL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001325 SDOperand N0 = N->getOperand(0);
1326 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001327 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1328 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001329 MVT::ValueType VT = N0.getValueType();
1330 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1331
1332 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001333 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001334 return DAG.getNode(ISD::SRL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001335 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001336 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001337 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001338 // fold (srl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001339 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001340 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001341 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001342 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001343 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001344 // if (srl x, c) is known to be zero, return 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001345 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001346 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001347 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001348 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001349 N0.getOperand(1).getOpcode() == ISD::Constant) {
1350 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001351 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001352 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001353 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001354 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001355 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001356 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001357 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001358}
1359
Nate Begeman83e75ec2005-09-06 04:43:02 +00001360SDOperand DAGCombiner::visitCTLZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001361 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001362 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00001363 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001364
1365 // fold (ctlz c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001366 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001367 return DAG.getNode(ISD::CTLZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001368 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001369}
1370
Nate Begeman83e75ec2005-09-06 04:43:02 +00001371SDOperand DAGCombiner::visitCTTZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001372 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001373 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00001374 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001375
1376 // fold (cttz c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001377 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001378 return DAG.getNode(ISD::CTTZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001379 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001380}
1381
Nate Begeman83e75ec2005-09-06 04:43:02 +00001382SDOperand DAGCombiner::visitCTPOP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001383 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001384 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00001385 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001386
1387 // fold (ctpop c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001388 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001389 return DAG.getNode(ISD::CTPOP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001390 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001391}
1392
Nate Begeman452d7be2005-09-16 00:54:12 +00001393SDOperand DAGCombiner::visitSELECT(SDNode *N) {
1394 SDOperand N0 = N->getOperand(0);
1395 SDOperand N1 = N->getOperand(1);
1396 SDOperand N2 = N->getOperand(2);
1397 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1398 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1399 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1400 MVT::ValueType VT = N->getValueType(0);
Nate Begeman44728a72005-09-19 22:34:01 +00001401
Nate Begeman452d7be2005-09-16 00:54:12 +00001402 // fold select C, X, X -> X
1403 if (N1 == N2)
1404 return N1;
1405 // fold select true, X, Y -> X
1406 if (N0C && !N0C->isNullValue())
1407 return N1;
1408 // fold select false, X, Y -> Y
1409 if (N0C && N0C->isNullValue())
1410 return N2;
1411 // fold select C, 1, X -> C | X
Nate Begeman44728a72005-09-19 22:34:01 +00001412 if (MVT::i1 == VT && N1C && N1C->getValue() == 1)
Nate Begeman452d7be2005-09-16 00:54:12 +00001413 return DAG.getNode(ISD::OR, VT, N0, N2);
1414 // fold select C, 0, X -> ~C & X
1415 // FIXME: this should check for C type == X type, not i1?
1416 if (MVT::i1 == VT && N1C && N1C->isNullValue()) {
1417 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
1418 WorkList.push_back(XORNode.Val);
1419 return DAG.getNode(ISD::AND, VT, XORNode, N2);
1420 }
1421 // fold select C, X, 1 -> ~C | X
Nate Begeman44728a72005-09-19 22:34:01 +00001422 if (MVT::i1 == VT && N2C && N2C->getValue() == 1) {
Nate Begeman452d7be2005-09-16 00:54:12 +00001423 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
1424 WorkList.push_back(XORNode.Val);
1425 return DAG.getNode(ISD::OR, VT, XORNode, N1);
1426 }
1427 // fold select C, X, 0 -> C & X
1428 // FIXME: this should check for C type == X type, not i1?
1429 if (MVT::i1 == VT && N2C && N2C->isNullValue())
1430 return DAG.getNode(ISD::AND, VT, N0, N1);
1431 // fold X ? X : Y --> X ? 1 : Y --> X | Y
1432 if (MVT::i1 == VT && N0 == N1)
1433 return DAG.getNode(ISD::OR, VT, N0, N2);
1434 // fold X ? Y : X --> X ? Y : 0 --> X & Y
1435 if (MVT::i1 == VT && N0 == N2)
1436 return DAG.getNode(ISD::AND, VT, N0, N1);
Chris Lattner40c62d52005-10-18 06:04:22 +00001437 // If we can fold this based on the true/false value, do so.
1438 if (SimplifySelectOps(N, N1, N2))
1439 return SDOperand();
Nate Begeman44728a72005-09-19 22:34:01 +00001440 // fold selects based on a setcc into other things, such as min/max/abs
1441 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman750ac1b2006-02-01 07:19:44 +00001442 // FIXME:
1443 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
1444 // having to say they don't support SELECT_CC on every type the DAG knows
1445 // about, since there is no way to mark an opcode illegal at all value types
1446 if (TLI.isOperationLegal(ISD::SELECT_CC, MVT::Other))
1447 return DAG.getNode(ISD::SELECT_CC, VT, N0.getOperand(0), N0.getOperand(1),
1448 N1, N2, N0.getOperand(2));
1449 else
1450 return SimplifySelect(N0, N1, N2);
Nate Begeman452d7be2005-09-16 00:54:12 +00001451 return SDOperand();
1452}
1453
1454SDOperand DAGCombiner::visitSELECT_CC(SDNode *N) {
Nate Begeman44728a72005-09-19 22:34:01 +00001455 SDOperand N0 = N->getOperand(0);
1456 SDOperand N1 = N->getOperand(1);
1457 SDOperand N2 = N->getOperand(2);
1458 SDOperand N3 = N->getOperand(3);
1459 SDOperand N4 = N->getOperand(4);
1460 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1461 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1462 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1463 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
1464
1465 // Determine if the condition we're dealing with is constant
Nate Begemane17daeb2005-10-05 21:43:42 +00001466 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
Chris Lattner91559022005-10-05 04:45:43 +00001467 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
1468
Nate Begeman44728a72005-09-19 22:34:01 +00001469 // fold select_cc lhs, rhs, x, x, cc -> x
1470 if (N2 == N3)
1471 return N2;
Chris Lattner40c62d52005-10-18 06:04:22 +00001472
1473 // If we can fold this based on the true/false value, do so.
1474 if (SimplifySelectOps(N, N2, N3))
1475 return SDOperand();
1476
Nate Begeman44728a72005-09-19 22:34:01 +00001477 // fold select_cc into other things, such as min/max/abs
1478 return SimplifySelectCC(N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00001479}
1480
1481SDOperand DAGCombiner::visitSETCC(SDNode *N) {
1482 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
1483 cast<CondCodeSDNode>(N->getOperand(2))->get());
1484}
1485
Nate Begeman5054f162005-10-14 01:12:21 +00001486SDOperand DAGCombiner::visitADD_PARTS(SDNode *N) {
1487 SDOperand LHSLo = N->getOperand(0);
1488 SDOperand RHSLo = N->getOperand(2);
1489 MVT::ValueType VT = LHSLo.getValueType();
1490
1491 // fold (a_Hi, 0) + (b_Hi, b_Lo) -> (b_Hi + a_Hi, b_Lo)
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001492 if (TLI.MaskedValueIsZero(LHSLo, (1ULL << MVT::getSizeInBits(VT))-1)) {
Nate Begeman5054f162005-10-14 01:12:21 +00001493 SDOperand Hi = DAG.getNode(ISD::ADD, VT, N->getOperand(1),
1494 N->getOperand(3));
1495 WorkList.push_back(Hi.Val);
1496 CombineTo(N, RHSLo, Hi);
1497 return SDOperand();
1498 }
1499 // fold (a_Hi, a_Lo) + (b_Hi, 0) -> (a_Hi + b_Hi, a_Lo)
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001500 if (TLI.MaskedValueIsZero(RHSLo, (1ULL << MVT::getSizeInBits(VT))-1)) {
Nate Begeman5054f162005-10-14 01:12:21 +00001501 SDOperand Hi = DAG.getNode(ISD::ADD, VT, N->getOperand(1),
1502 N->getOperand(3));
1503 WorkList.push_back(Hi.Val);
1504 CombineTo(N, LHSLo, Hi);
1505 return SDOperand();
1506 }
1507 return SDOperand();
1508}
1509
1510SDOperand DAGCombiner::visitSUB_PARTS(SDNode *N) {
1511 SDOperand LHSLo = N->getOperand(0);
1512 SDOperand RHSLo = N->getOperand(2);
1513 MVT::ValueType VT = LHSLo.getValueType();
1514
1515 // fold (a_Hi, a_Lo) - (b_Hi, 0) -> (a_Hi - b_Hi, a_Lo)
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001516 if (TLI.MaskedValueIsZero(RHSLo, (1ULL << MVT::getSizeInBits(VT))-1)) {
Nate Begeman5054f162005-10-14 01:12:21 +00001517 SDOperand Hi = DAG.getNode(ISD::SUB, VT, N->getOperand(1),
1518 N->getOperand(3));
1519 WorkList.push_back(Hi.Val);
1520 CombineTo(N, LHSLo, Hi);
1521 return SDOperand();
1522 }
1523 return SDOperand();
1524}
1525
Nate Begeman83e75ec2005-09-06 04:43:02 +00001526SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001527 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001528 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001529 MVT::ValueType VT = N->getValueType(0);
1530
Nate Begeman1d4d4142005-09-01 00:19:25 +00001531 // fold (sext c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001532 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001533 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001534 // fold (sext (sext x)) -> (sext x)
1535 if (N0.getOpcode() == ISD::SIGN_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001536 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
Chris Lattnerb14ab8a2005-12-07 07:11:03 +00001537 // fold (sext (truncate x)) -> (sextinreg x) iff x size == sext size.
Chris Lattnercc2210b2005-12-07 18:02:05 +00001538 if (N0.getOpcode() == ISD::TRUNCATE && N0.getOperand(0).getValueType() == VT&&
1539 (!AfterLegalize ||
1540 TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, N0.getValueType())))
Chris Lattnerb14ab8a2005-12-07 07:11:03 +00001541 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
1542 DAG.getValueType(N0.getValueType()));
Evan Cheng110dec22005-12-14 02:19:23 +00001543 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Chris Lattnerd0f6d182005-12-15 19:02:38 +00001544 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() &&
1545 (!AfterLegalize||TLI.isOperationLegal(ISD::SEXTLOAD, N0.getValueType()))){
Nate Begeman3df4d522005-10-12 20:40:40 +00001546 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1547 N0.getOperand(1), N0.getOperand(2),
1548 N0.getValueType());
Chris Lattnerd4771842005-12-14 19:25:30 +00001549 CombineTo(N, ExtLoad);
Chris Lattnerf9884052005-10-13 21:52:31 +00001550 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1551 ExtLoad.getValue(1));
Nate Begeman765784a2005-10-12 23:18:53 +00001552 return SDOperand();
Nate Begeman3df4d522005-10-12 20:40:40 +00001553 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001554
1555 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
1556 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
1557 if ((N0.getOpcode() == ISD::SEXTLOAD || N0.getOpcode() == ISD::EXTLOAD) &&
1558 N0.hasOneUse()) {
1559 SDOperand ExtLoad = DAG.getNode(ISD::SEXTLOAD, VT, N0.getOperand(0),
1560 N0.getOperand(1), N0.getOperand(2),
1561 N0.getOperand(3));
Chris Lattnerd4771842005-12-14 19:25:30 +00001562 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001563 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1564 ExtLoad.getValue(1));
1565 return SDOperand();
1566 }
1567
Nate Begeman83e75ec2005-09-06 04:43:02 +00001568 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001569}
1570
Nate Begeman83e75ec2005-09-06 04:43:02 +00001571SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001572 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001573 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001574 MVT::ValueType VT = N->getValueType(0);
1575
Nate Begeman1d4d4142005-09-01 00:19:25 +00001576 // fold (zext c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001577 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001578 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001579 // fold (zext (zext x)) -> (zext x)
1580 if (N0.getOpcode() == ISD::ZERO_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001581 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
Evan Cheng110dec22005-12-14 02:19:23 +00001582 // fold (zext (truncate x)) -> (zextinreg x) iff x size == zext size.
1583 if (N0.getOpcode() == ISD::TRUNCATE && N0.getOperand(0).getValueType() == VT&&
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001584 (!AfterLegalize || TLI.isOperationLegal(ISD::AND, N0.getValueType())))
Chris Lattner00cb95c2005-12-14 07:58:38 +00001585 return DAG.getZeroExtendInReg(N0.getOperand(0), N0.getValueType());
Evan Cheng110dec22005-12-14 02:19:23 +00001586 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Chris Lattnerd0f6d182005-12-15 19:02:38 +00001587 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() &&
1588 (!AfterLegalize||TLI.isOperationLegal(ISD::ZEXTLOAD, N0.getValueType()))){
Evan Cheng110dec22005-12-14 02:19:23 +00001589 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1590 N0.getOperand(1), N0.getOperand(2),
1591 N0.getValueType());
Chris Lattnerd4771842005-12-14 19:25:30 +00001592 CombineTo(N, ExtLoad);
Evan Cheng110dec22005-12-14 02:19:23 +00001593 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1594 ExtLoad.getValue(1));
1595 return SDOperand();
1596 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001597
1598 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
1599 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
1600 if ((N0.getOpcode() == ISD::ZEXTLOAD || N0.getOpcode() == ISD::EXTLOAD) &&
1601 N0.hasOneUse()) {
1602 SDOperand ExtLoad = DAG.getNode(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1603 N0.getOperand(1), N0.getOperand(2),
1604 N0.getOperand(3));
Chris Lattnerd4771842005-12-14 19:25:30 +00001605 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001606 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1607 ExtLoad.getValue(1));
1608 return SDOperand();
1609 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001610 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001611}
1612
Nate Begeman83e75ec2005-09-06 04:43:02 +00001613SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001614 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001615 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001616 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001617 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001618 MVT::ValueType EVT = cast<VTSDNode>(N1)->getVT();
Nate Begeman07ed4172005-10-10 21:26:48 +00001619 unsigned EVTBits = MVT::getSizeInBits(EVT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001620
Nate Begeman1d4d4142005-09-01 00:19:25 +00001621 // fold (sext_in_reg c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001622 if (N0C) {
1623 SDOperand Truncate = DAG.getConstant(N0C->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001624 return DAG.getNode(ISD::SIGN_EXTEND, VT, Truncate);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001625 }
Nate Begeman646d7e22005-09-02 21:18:40 +00001626 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt1
Nate Begeman1d4d4142005-09-01 00:19:25 +00001627 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Nate Begeman216def82005-10-14 01:29:07 +00001628 cast<VTSDNode>(N0.getOperand(1))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001629 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001630 }
Nate Begeman646d7e22005-09-02 21:18:40 +00001631 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
1632 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1633 EVT < cast<VTSDNode>(N0.getOperand(1))->getVT()) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001634 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001635 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00001636 // fold (sext_in_reg (assert_sext x)) -> (assert_sext x)
1637 if (N0.getOpcode() == ISD::AssertSext &&
1638 cast<VTSDNode>(N0.getOperand(1))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001639 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001640 }
1641 // fold (sext_in_reg (sextload x)) -> (sextload x)
1642 if (N0.getOpcode() == ISD::SEXTLOAD &&
1643 cast<VTSDNode>(N0.getOperand(3))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001644 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001645 }
Nate Begeman4ebd8052005-09-01 23:24:04 +00001646 // fold (sext_in_reg (setcc x)) -> setcc x iff (setcc x) == 0 or -1
Nate Begeman1d4d4142005-09-01 00:19:25 +00001647 if (N0.getOpcode() == ISD::SETCC &&
1648 TLI.getSetCCResultContents() ==
1649 TargetLowering::ZeroOrNegativeOneSetCCResult)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001650 return N0;
Nate Begeman07ed4172005-10-10 21:26:48 +00001651 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is zero
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001652 if (TLI.MaskedValueIsZero(N0, 1ULL << (EVTBits-1)))
Nate Begeman07ed4172005-10-10 21:26:48 +00001653 return DAG.getNode(ISD::AND, N0.getValueType(), N0,
1654 DAG.getConstant(~0ULL >> (64-EVTBits), VT));
1655 // fold (sext_in_reg (srl x)) -> sra x
1656 if (N0.getOpcode() == ISD::SRL &&
1657 N0.getOperand(1).getOpcode() == ISD::Constant &&
1658 cast<ConstantSDNode>(N0.getOperand(1))->getValue() == EVTBits) {
1659 return DAG.getNode(ISD::SRA, N0.getValueType(), N0.getOperand(0),
1660 N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001661 }
Nate Begemanded49632005-10-13 03:11:28 +00001662 // fold (sext_inreg (extload x)) -> (sextload x)
1663 if (N0.getOpcode() == ISD::EXTLOAD &&
1664 EVT == cast<VTSDNode>(N0.getOperand(3))->getVT() &&
Nate Begemanbfd65a02005-10-13 18:34:58 +00001665 (!AfterLegalize || TLI.isOperationLegal(ISD::SEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001666 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1667 N0.getOperand(1), N0.getOperand(2),
1668 EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00001669 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00001670 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Nate Begemanded49632005-10-13 03:11:28 +00001671 return SDOperand();
1672 }
1673 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Chris Lattner40c62d52005-10-18 06:04:22 +00001674 if (N0.getOpcode() == ISD::ZEXTLOAD && N0.hasOneUse() &&
Nate Begemanded49632005-10-13 03:11:28 +00001675 EVT == cast<VTSDNode>(N0.getOperand(3))->getVT() &&
Nate Begemanbfd65a02005-10-13 18:34:58 +00001676 (!AfterLegalize || TLI.isOperationLegal(ISD::SEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001677 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1678 N0.getOperand(1), N0.getOperand(2),
1679 EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00001680 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00001681 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Nate Begemanded49632005-10-13 03:11:28 +00001682 return SDOperand();
1683 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001684 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001685}
1686
Nate Begeman83e75ec2005-09-06 04:43:02 +00001687SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001688 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001689 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001690 MVT::ValueType VT = N->getValueType(0);
1691
1692 // noop truncate
1693 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001694 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001695 // fold (truncate c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001696 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001697 return DAG.getNode(ISD::TRUNCATE, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001698 // fold (truncate (truncate x)) -> (truncate x)
1699 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001700 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001701 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
1702 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND){
1703 if (N0.getValueType() < VT)
1704 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00001705 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001706 else if (N0.getValueType() > VT)
1707 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00001708 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001709 else
1710 // if the source and dest are the same type, we can drop both the extend
1711 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00001712 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001713 }
Nate Begeman3df4d522005-10-12 20:40:40 +00001714 // fold (truncate (load x)) -> (smaller load x)
Chris Lattner40c62d52005-10-18 06:04:22 +00001715 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse()) {
Nate Begeman3df4d522005-10-12 20:40:40 +00001716 assert(MVT::getSizeInBits(N0.getValueType()) > MVT::getSizeInBits(VT) &&
1717 "Cannot truncate to larger type!");
1718 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
Nate Begeman765784a2005-10-12 23:18:53 +00001719 // For big endian targets, we need to add an offset to the pointer to load
1720 // the correct bytes. For little endian systems, we merely need to read
1721 // fewer bytes from the same pointer.
Nate Begeman3df4d522005-10-12 20:40:40 +00001722 uint64_t PtrOff =
1723 (MVT::getSizeInBits(N0.getValueType()) - MVT::getSizeInBits(VT)) / 8;
Nate Begeman765784a2005-10-12 23:18:53 +00001724 SDOperand NewPtr = TLI.isLittleEndian() ? N0.getOperand(1) :
1725 DAG.getNode(ISD::ADD, PtrType, N0.getOperand(1),
1726 DAG.getConstant(PtrOff, PtrType));
1727 WorkList.push_back(NewPtr.Val);
Nate Begeman3df4d522005-10-12 20:40:40 +00001728 SDOperand Load = DAG.getLoad(VT, N0.getOperand(0), NewPtr,N0.getOperand(2));
Nate Begeman765784a2005-10-12 23:18:53 +00001729 WorkList.push_back(N);
Chris Lattner24edbb72005-10-13 22:10:05 +00001730 CombineTo(N0.Val, Load, Load.getValue(1));
Nate Begeman765784a2005-10-12 23:18:53 +00001731 return SDOperand();
Nate Begeman3df4d522005-10-12 20:40:40 +00001732 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001733 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001734}
1735
Chris Lattner94683772005-12-23 05:30:37 +00001736SDOperand DAGCombiner::visitBIT_CONVERT(SDNode *N) {
1737 SDOperand N0 = N->getOperand(0);
1738 MVT::ValueType VT = N->getValueType(0);
1739
1740 // If the input is a constant, let getNode() fold it.
1741 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
1742 SDOperand Res = DAG.getNode(ISD::BIT_CONVERT, VT, N0);
1743 if (Res.Val != N) return Res;
1744 }
1745
Chris Lattnerc8547d82005-12-23 05:37:50 +00001746 if (N0.getOpcode() == ISD::BIT_CONVERT) // conv(conv(x,t1),t2) -> conv(x,t2)
1747 return DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
1748
Chris Lattner57104102005-12-23 05:44:41 +00001749 // fold (conv (load x)) -> (load (conv*)x)
Chris Lattnerbf40c4b2006-01-15 18:58:59 +00001750 // FIXME: These xforms need to know that the resultant load doesn't need a
1751 // higher alignment than the original!
1752 if (0 && N0.getOpcode() == ISD::LOAD && N0.hasOneUse()) {
Chris Lattner57104102005-12-23 05:44:41 +00001753 SDOperand Load = DAG.getLoad(VT, N0.getOperand(0), N0.getOperand(1),
1754 N0.getOperand(2));
1755 WorkList.push_back(N);
1756 CombineTo(N0.Val, DAG.getNode(ISD::BIT_CONVERT, N0.getValueType(), Load),
1757 Load.getValue(1));
1758 return Load;
1759 }
1760
Chris Lattner94683772005-12-23 05:30:37 +00001761 return SDOperand();
1762}
1763
Chris Lattner01b3d732005-09-28 22:28:18 +00001764SDOperand DAGCombiner::visitFADD(SDNode *N) {
1765 SDOperand N0 = N->getOperand(0);
1766 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00001767 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1768 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001769 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00001770
1771 // fold (fadd c1, c2) -> c1+c2
1772 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001773 return DAG.getNode(ISD::FADD, VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00001774 // canonicalize constant to RHS
1775 if (N0CFP && !N1CFP)
1776 return DAG.getNode(ISD::FADD, VT, N1, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00001777 // fold (A + (-B)) -> A-B
1778 if (N1.getOpcode() == ISD::FNEG)
1779 return DAG.getNode(ISD::FSUB, VT, N0, N1.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00001780 // fold ((-A) + B) -> B-A
1781 if (N0.getOpcode() == ISD::FNEG)
1782 return DAG.getNode(ISD::FSUB, VT, N1, N0.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00001783 return SDOperand();
1784}
1785
1786SDOperand DAGCombiner::visitFSUB(SDNode *N) {
1787 SDOperand N0 = N->getOperand(0);
1788 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00001789 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1790 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001791 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00001792
1793 // fold (fsub c1, c2) -> c1-c2
1794 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001795 return DAG.getNode(ISD::FSUB, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001796 // fold (A-(-B)) -> A+B
1797 if (N1.getOpcode() == ISD::FNEG)
Nate Begemana148d982006-01-18 22:35:16 +00001798 return DAG.getNode(ISD::FADD, VT, N0, N1.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00001799 return SDOperand();
1800}
1801
1802SDOperand DAGCombiner::visitFMUL(SDNode *N) {
1803 SDOperand N0 = N->getOperand(0);
1804 SDOperand N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00001805 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1806 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001807 MVT::ValueType VT = N->getValueType(0);
1808
Nate Begeman11af4ea2005-10-17 20:40:11 +00001809 // fold (fmul c1, c2) -> c1*c2
1810 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001811 return DAG.getNode(ISD::FMUL, VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00001812 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001813 if (N0CFP && !N1CFP)
1814 return DAG.getNode(ISD::FMUL, VT, N1, N0);
Nate Begeman11af4ea2005-10-17 20:40:11 +00001815 // fold (fmul X, 2.0) -> (fadd X, X)
1816 if (N1CFP && N1CFP->isExactlyValue(+2.0))
1817 return DAG.getNode(ISD::FADD, VT, N0, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00001818 return SDOperand();
1819}
1820
1821SDOperand DAGCombiner::visitFDIV(SDNode *N) {
1822 SDOperand N0 = N->getOperand(0);
1823 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00001824 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1825 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001826 MVT::ValueType VT = N->getValueType(0);
1827
Nate Begemana148d982006-01-18 22:35:16 +00001828 // fold (fdiv c1, c2) -> c1/c2
1829 if (N0CFP && N1CFP)
1830 return DAG.getNode(ISD::FDIV, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001831 return SDOperand();
1832}
1833
1834SDOperand DAGCombiner::visitFREM(SDNode *N) {
1835 SDOperand N0 = N->getOperand(0);
1836 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00001837 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1838 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001839 MVT::ValueType VT = N->getValueType(0);
1840
Nate Begemana148d982006-01-18 22:35:16 +00001841 // fold (frem c1, c2) -> fmod(c1,c2)
1842 if (N0CFP && N1CFP)
1843 return DAG.getNode(ISD::FREM, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001844 return SDOperand();
1845}
1846
1847
Nate Begeman83e75ec2005-09-06 04:43:02 +00001848SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001849 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001850 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00001851 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001852
1853 // fold (sint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001854 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001855 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001856 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001857}
1858
Nate Begeman83e75ec2005-09-06 04:43:02 +00001859SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001860 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001861 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00001862 MVT::ValueType VT = N->getValueType(0);
1863
Nate Begeman1d4d4142005-09-01 00:19:25 +00001864 // fold (uint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001865 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001866 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001867 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001868}
1869
Nate Begeman83e75ec2005-09-06 04:43:02 +00001870SDOperand DAGCombiner::visitFP_TO_SINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00001871 SDOperand N0 = N->getOperand(0);
1872 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1873 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001874
1875 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001876 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001877 return DAG.getNode(ISD::FP_TO_SINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001878 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001879}
1880
Nate Begeman83e75ec2005-09-06 04:43:02 +00001881SDOperand DAGCombiner::visitFP_TO_UINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00001882 SDOperand N0 = N->getOperand(0);
1883 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1884 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001885
1886 // fold (fp_to_uint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001887 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001888 return DAG.getNode(ISD::FP_TO_UINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001889 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001890}
1891
Nate Begeman83e75ec2005-09-06 04:43:02 +00001892SDOperand DAGCombiner::visitFP_ROUND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00001893 SDOperand N0 = N->getOperand(0);
1894 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1895 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001896
1897 // fold (fp_round c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001898 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001899 return DAG.getNode(ISD::FP_ROUND, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001900 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001901}
1902
Nate Begeman83e75ec2005-09-06 04:43:02 +00001903SDOperand DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001904 SDOperand N0 = N->getOperand(0);
1905 MVT::ValueType VT = N->getValueType(0);
1906 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00001907 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001908
Nate Begeman1d4d4142005-09-01 00:19:25 +00001909 // fold (fp_round_inreg c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001910 if (N0CFP) {
1911 SDOperand Round = DAG.getConstantFP(N0CFP->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001912 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001913 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001914 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001915}
1916
Nate Begeman83e75ec2005-09-06 04:43:02 +00001917SDOperand DAGCombiner::visitFP_EXTEND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00001918 SDOperand N0 = N->getOperand(0);
1919 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1920 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001921
1922 // fold (fp_extend c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001923 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001924 return DAG.getNode(ISD::FP_EXTEND, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001925 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001926}
1927
Nate Begeman83e75ec2005-09-06 04:43:02 +00001928SDOperand DAGCombiner::visitFNEG(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00001929 SDOperand N0 = N->getOperand(0);
1930 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1931 MVT::ValueType VT = N->getValueType(0);
1932
1933 // fold (fneg c1) -> -c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001934 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001935 return DAG.getNode(ISD::FNEG, VT, N0);
1936 // fold (fneg (sub x, y)) -> (sub y, x)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001937 if (N->getOperand(0).getOpcode() == ISD::SUB)
Nate Begemana148d982006-01-18 22:35:16 +00001938 return DAG.getNode(ISD::SUB, VT, N->getOperand(1), N->getOperand(0));
1939 // fold (fneg (fneg x)) -> x
Nate Begeman1d4d4142005-09-01 00:19:25 +00001940 if (N->getOperand(0).getOpcode() == ISD::FNEG)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001941 return N->getOperand(0).getOperand(0);
1942 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001943}
1944
Nate Begeman83e75ec2005-09-06 04:43:02 +00001945SDOperand DAGCombiner::visitFABS(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00001946 SDOperand N0 = N->getOperand(0);
1947 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1948 MVT::ValueType VT = N->getValueType(0);
1949
Nate Begeman1d4d4142005-09-01 00:19:25 +00001950 // fold (fabs c1) -> fabs(c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001951 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001952 return DAG.getNode(ISD::FABS, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001953 // fold (fabs (fabs x)) -> (fabs x)
1954 if (N->getOperand(0).getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001955 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001956 // fold (fabs (fneg x)) -> (fabs x)
1957 if (N->getOperand(0).getOpcode() == ISD::FNEG)
Nate Begemana148d982006-01-18 22:35:16 +00001958 return DAG.getNode(ISD::FABS, VT, N->getOperand(0).getOperand(0));
Nate Begeman83e75ec2005-09-06 04:43:02 +00001959 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001960}
1961
Nate Begeman44728a72005-09-19 22:34:01 +00001962SDOperand DAGCombiner::visitBRCOND(SDNode *N) {
1963 SDOperand Chain = N->getOperand(0);
1964 SDOperand N1 = N->getOperand(1);
1965 SDOperand N2 = N->getOperand(2);
1966 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1967
1968 // never taken branch, fold to chain
1969 if (N1C && N1C->isNullValue())
1970 return Chain;
1971 // unconditional branch
Nate Begemane17daeb2005-10-05 21:43:42 +00001972 if (N1C && N1C->getValue() == 1)
Nate Begeman44728a72005-09-19 22:34:01 +00001973 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
Nate Begeman750ac1b2006-02-01 07:19:44 +00001974 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
1975 // on the target.
1976 if (N1.getOpcode() == ISD::SETCC &&
1977 TLI.isOperationLegal(ISD::BR_CC, MVT::Other)) {
1978 return DAG.getNode(ISD::BR_CC, MVT::Other, Chain, N1.getOperand(2),
1979 N1.getOperand(0), N1.getOperand(1), N2);
1980 }
Nate Begeman44728a72005-09-19 22:34:01 +00001981 return SDOperand();
1982}
1983
1984SDOperand DAGCombiner::visitBRCONDTWOWAY(SDNode *N) {
1985 SDOperand Chain = N->getOperand(0);
1986 SDOperand N1 = N->getOperand(1);
1987 SDOperand N2 = N->getOperand(2);
1988 SDOperand N3 = N->getOperand(3);
1989 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1990
1991 // unconditional branch to true mbb
1992 if (N1C && N1C->getValue() == 1)
1993 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
1994 // unconditional branch to false mbb
1995 if (N1C && N1C->isNullValue())
1996 return DAG.getNode(ISD::BR, MVT::Other, Chain, N3);
Nate Begeman750ac1b2006-02-01 07:19:44 +00001997 // fold a brcondtwoway with a setcc condition into a BRTWOWAY_CC node if
1998 // BRTWOWAY_CC is legal on the target.
1999 if (N1.getOpcode() == ISD::SETCC &&
2000 TLI.isOperationLegal(ISD::BRTWOWAY_CC, MVT::Other)) {
2001 std::vector<SDOperand> Ops;
2002 Ops.push_back(Chain);
2003 Ops.push_back(N1.getOperand(2));
2004 Ops.push_back(N1.getOperand(0));
2005 Ops.push_back(N1.getOperand(1));
2006 Ops.push_back(N2);
2007 Ops.push_back(N3);
2008 return DAG.getNode(ISD::BRTWOWAY_CC, MVT::Other, Ops);
2009 }
Nate Begeman44728a72005-09-19 22:34:01 +00002010 return SDOperand();
2011}
2012
Chris Lattner3ea0b472005-10-05 06:47:48 +00002013// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
2014//
Nate Begeman44728a72005-09-19 22:34:01 +00002015SDOperand DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00002016 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
2017 SDOperand CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
2018
2019 // Use SimplifySetCC to simplify SETCC's.
Nate Begemane17daeb2005-10-05 21:43:42 +00002020 SDOperand Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false);
2021 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.Val);
2022
2023 // fold br_cc true, dest -> br dest (unconditional branch)
2024 if (SCCC && SCCC->getValue())
2025 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
2026 N->getOperand(4));
2027 // fold br_cc false, dest -> unconditional fall through
2028 if (SCCC && SCCC->isNullValue())
2029 return N->getOperand(0);
2030 // fold to a simpler setcc
2031 if (Simp.Val && Simp.getOpcode() == ISD::SETCC)
2032 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
2033 Simp.getOperand(2), Simp.getOperand(0),
2034 Simp.getOperand(1), N->getOperand(4));
Nate Begeman44728a72005-09-19 22:34:01 +00002035 return SDOperand();
2036}
2037
2038SDOperand DAGCombiner::visitBRTWOWAY_CC(SDNode *N) {
Nate Begemane17daeb2005-10-05 21:43:42 +00002039 SDOperand Chain = N->getOperand(0);
2040 SDOperand CCN = N->getOperand(1);
2041 SDOperand LHS = N->getOperand(2);
2042 SDOperand RHS = N->getOperand(3);
2043 SDOperand N4 = N->getOperand(4);
2044 SDOperand N5 = N->getOperand(5);
2045
2046 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), LHS, RHS,
2047 cast<CondCodeSDNode>(CCN)->get(), false);
2048 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
2049
2050 // fold select_cc lhs, rhs, x, x, cc -> x
2051 if (N4 == N5)
2052 return DAG.getNode(ISD::BR, MVT::Other, Chain, N4);
2053 // fold select_cc true, x, y -> x
2054 if (SCCC && SCCC->getValue())
2055 return DAG.getNode(ISD::BR, MVT::Other, Chain, N4);
2056 // fold select_cc false, x, y -> y
2057 if (SCCC && SCCC->isNullValue())
2058 return DAG.getNode(ISD::BR, MVT::Other, Chain, N5);
2059 // fold to a simpler setcc
Chris Lattner03d5e872006-01-29 06:00:45 +00002060 if (SCC.Val && SCC.getOpcode() == ISD::SETCC) {
2061 std::vector<SDOperand> Ops;
2062 Ops.push_back(Chain);
2063 Ops.push_back(SCC.getOperand(2));
2064 Ops.push_back(SCC.getOperand(0));
2065 Ops.push_back(SCC.getOperand(1));
2066 Ops.push_back(N4);
2067 Ops.push_back(N5);
2068 return DAG.getNode(ISD::BRTWOWAY_CC, MVT::Other, Ops);
2069 }
Nate Begeman44728a72005-09-19 22:34:01 +00002070 return SDOperand();
2071}
2072
Chris Lattner01a22022005-10-10 22:04:48 +00002073SDOperand DAGCombiner::visitLOAD(SDNode *N) {
2074 SDOperand Chain = N->getOperand(0);
2075 SDOperand Ptr = N->getOperand(1);
2076 SDOperand SrcValue = N->getOperand(2);
2077
2078 // If this load is directly stored, replace the load value with the stored
2079 // value.
2080 // TODO: Handle store large -> read small portion.
2081 // TODO: Handle TRUNCSTORE/EXTLOAD
2082 if (Chain.getOpcode() == ISD::STORE && Chain.getOperand(2) == Ptr &&
2083 Chain.getOperand(1).getValueType() == N->getValueType(0))
2084 return CombineTo(N, Chain.getOperand(1), Chain);
2085
2086 return SDOperand();
2087}
2088
Chris Lattner87514ca2005-10-10 22:31:19 +00002089SDOperand DAGCombiner::visitSTORE(SDNode *N) {
2090 SDOperand Chain = N->getOperand(0);
2091 SDOperand Value = N->getOperand(1);
2092 SDOperand Ptr = N->getOperand(2);
2093 SDOperand SrcValue = N->getOperand(3);
2094
2095 // If this is a store that kills a previous store, remove the previous store.
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002096 if (Chain.getOpcode() == ISD::STORE && Chain.getOperand(2) == Ptr &&
Chris Lattnerfe7f0462005-10-27 07:10:34 +00002097 Chain.Val->hasOneUse() /* Avoid introducing DAG cycles */ &&
2098 // Make sure that these stores are the same value type:
2099 // FIXME: we really care that the second store is >= size of the first.
2100 Value.getValueType() == Chain.getOperand(1).getValueType()) {
Chris Lattner87514ca2005-10-10 22:31:19 +00002101 // Create a new store of Value that replaces both stores.
2102 SDNode *PrevStore = Chain.Val;
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002103 if (PrevStore->getOperand(1) == Value) // Same value multiply stored.
2104 return Chain;
Chris Lattner87514ca2005-10-10 22:31:19 +00002105 SDOperand NewStore = DAG.getNode(ISD::STORE, MVT::Other,
2106 PrevStore->getOperand(0), Value, Ptr,
2107 SrcValue);
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002108 CombineTo(N, NewStore); // Nuke this store.
Chris Lattner87514ca2005-10-10 22:31:19 +00002109 CombineTo(PrevStore, NewStore); // Nuke the previous store.
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002110 return SDOperand(N, 0);
Chris Lattner87514ca2005-10-10 22:31:19 +00002111 }
2112
Chris Lattnerc33baaa2005-12-23 05:48:07 +00002113 // If this is a store of a bit convert, store the input value.
Chris Lattnerbf40c4b2006-01-15 18:58:59 +00002114 // FIXME: This needs to know that the resultant store does not need a
2115 // higher alignment than the original.
2116 if (0 && Value.getOpcode() == ISD::BIT_CONVERT)
Chris Lattnerc33baaa2005-12-23 05:48:07 +00002117 return DAG.getNode(ISD::STORE, MVT::Other, Chain, Value.getOperand(0),
2118 Ptr, SrcValue);
2119
Chris Lattner87514ca2005-10-10 22:31:19 +00002120 return SDOperand();
2121}
2122
Jim Laskeyd6e8d412005-12-23 20:08:28 +00002123SDOperand DAGCombiner::visitLOCATION(SDNode *N) {
2124 SDOperand Chain = N->getOperand(0);
2125
2126 // Remove redundant locations (last one holds)
2127 if (Chain.getOpcode() == ISD::LOCATION && Chain.hasOneUse()) {
2128 return DAG.getNode(ISD::LOCATION, MVT::Other, Chain.getOperand(0),
2129 N->getOperand(1),
2130 N->getOperand(2),
2131 N->getOperand(3),
2132 N->getOperand(4));
2133 }
2134
2135 return SDOperand();
2136}
2137
2138SDOperand DAGCombiner::visitDEBUGLOC(SDNode *N) {
2139 SDOperand Chain = N->getOperand(0);
2140
2141 // Remove redundant debug locations (last one holds)
2142 if (Chain.getOpcode() == ISD::DEBUG_LOC && Chain.hasOneUse()) {
2143 return DAG.getNode(ISD::DEBUG_LOC, MVT::Other, Chain.getOperand(0),
2144 N->getOperand(1),
2145 N->getOperand(2),
Jim Laskeyabf6d172006-01-05 01:25:28 +00002146 N->getOperand(3));
Jim Laskeyd6e8d412005-12-23 20:08:28 +00002147 }
2148
2149 return SDOperand();
2150}
2151
Nate Begeman44728a72005-09-19 22:34:01 +00002152SDOperand DAGCombiner::SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2){
Nate Begemanf845b452005-10-08 00:29:44 +00002153 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
2154
2155 SDOperand SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
2156 cast<CondCodeSDNode>(N0.getOperand(2))->get());
2157 // If we got a simplified select_cc node back from SimplifySelectCC, then
2158 // break it down into a new SETCC node, and a new SELECT node, and then return
2159 // the SELECT node, since we were called with a SELECT node.
2160 if (SCC.Val) {
2161 // Check to see if we got a select_cc back (to turn into setcc/select).
2162 // Otherwise, just return whatever node we got back, like fabs.
2163 if (SCC.getOpcode() == ISD::SELECT_CC) {
2164 SDOperand SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
2165 SCC.getOperand(0), SCC.getOperand(1),
2166 SCC.getOperand(4));
2167 WorkList.push_back(SETCC.Val);
2168 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
2169 SCC.getOperand(3), SETCC);
2170 }
2171 return SCC;
2172 }
Nate Begeman44728a72005-09-19 22:34:01 +00002173 return SDOperand();
2174}
2175
Chris Lattner40c62d52005-10-18 06:04:22 +00002176/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
2177/// are the two values being selected between, see if we can simplify the
2178/// select.
2179///
2180bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDOperand LHS,
2181 SDOperand RHS) {
2182
2183 // If this is a select from two identical things, try to pull the operation
2184 // through the select.
2185 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
2186#if 0
2187 std::cerr << "SELECT: ["; LHS.Val->dump();
2188 std::cerr << "] ["; RHS.Val->dump();
2189 std::cerr << "]\n";
2190#endif
2191
2192 // If this is a load and the token chain is identical, replace the select
2193 // of two loads with a load through a select of the address to load from.
2194 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
2195 // constants have been dropped into the constant pool.
2196 if ((LHS.getOpcode() == ISD::LOAD ||
2197 LHS.getOpcode() == ISD::EXTLOAD ||
2198 LHS.getOpcode() == ISD::ZEXTLOAD ||
2199 LHS.getOpcode() == ISD::SEXTLOAD) &&
2200 // Token chains must be identical.
2201 LHS.getOperand(0) == RHS.getOperand(0) &&
2202 // If this is an EXTLOAD, the VT's must match.
2203 (LHS.getOpcode() == ISD::LOAD ||
2204 LHS.getOperand(3) == RHS.getOperand(3))) {
2205 // FIXME: this conflates two src values, discarding one. This is not
2206 // the right thing to do, but nothing uses srcvalues now. When they do,
2207 // turn SrcValue into a list of locations.
2208 SDOperand Addr;
2209 if (TheSelect->getOpcode() == ISD::SELECT)
2210 Addr = DAG.getNode(ISD::SELECT, LHS.getOperand(1).getValueType(),
2211 TheSelect->getOperand(0), LHS.getOperand(1),
2212 RHS.getOperand(1));
2213 else
2214 Addr = DAG.getNode(ISD::SELECT_CC, LHS.getOperand(1).getValueType(),
2215 TheSelect->getOperand(0),
2216 TheSelect->getOperand(1),
2217 LHS.getOperand(1), RHS.getOperand(1),
2218 TheSelect->getOperand(4));
2219
2220 SDOperand Load;
2221 if (LHS.getOpcode() == ISD::LOAD)
2222 Load = DAG.getLoad(TheSelect->getValueType(0), LHS.getOperand(0),
2223 Addr, LHS.getOperand(2));
2224 else
2225 Load = DAG.getExtLoad(LHS.getOpcode(), TheSelect->getValueType(0),
2226 LHS.getOperand(0), Addr, LHS.getOperand(2),
2227 cast<VTSDNode>(LHS.getOperand(3))->getVT());
2228 // Users of the select now use the result of the load.
2229 CombineTo(TheSelect, Load);
2230
2231 // Users of the old loads now use the new load's chain. We know the
2232 // old-load value is dead now.
2233 CombineTo(LHS.Val, Load.getValue(0), Load.getValue(1));
2234 CombineTo(RHS.Val, Load.getValue(0), Load.getValue(1));
2235 return true;
2236 }
2237 }
2238
2239 return false;
2240}
2241
Nate Begeman44728a72005-09-19 22:34:01 +00002242SDOperand DAGCombiner::SimplifySelectCC(SDOperand N0, SDOperand N1,
2243 SDOperand N2, SDOperand N3,
2244 ISD::CondCode CC) {
Nate Begemanf845b452005-10-08 00:29:44 +00002245
2246 MVT::ValueType VT = N2.getValueType();
2247 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
2248 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2249 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
2250 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
2251
2252 // Determine if the condition we're dealing with is constant
2253 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
2254 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
2255
2256 // fold select_cc true, x, y -> x
2257 if (SCCC && SCCC->getValue())
2258 return N2;
2259 // fold select_cc false, x, y -> y
2260 if (SCCC && SCCC->getValue() == 0)
2261 return N3;
2262
2263 // Check to see if we can simplify the select into an fabs node
2264 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
2265 // Allow either -0.0 or 0.0
2266 if (CFP->getValue() == 0.0) {
2267 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
2268 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
2269 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
2270 N2 == N3.getOperand(0))
2271 return DAG.getNode(ISD::FABS, VT, N0);
2272
2273 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
2274 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
2275 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
2276 N2.getOperand(0) == N3)
2277 return DAG.getNode(ISD::FABS, VT, N3);
2278 }
2279 }
2280
2281 // Check to see if we can perform the "gzip trick", transforming
2282 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
2283 if (N1C && N1C->isNullValue() && N3C && N3C->isNullValue() &&
2284 MVT::isInteger(N0.getValueType()) &&
2285 MVT::isInteger(N2.getValueType()) && CC == ISD::SETLT) {
2286 MVT::ValueType XType = N0.getValueType();
2287 MVT::ValueType AType = N2.getValueType();
2288 if (XType >= AType) {
2289 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00002290 // single-bit constant.
Nate Begemanf845b452005-10-08 00:29:44 +00002291 if (N2C && ((N2C->getValue() & (N2C->getValue()-1)) == 0)) {
2292 unsigned ShCtV = Log2_64(N2C->getValue());
2293 ShCtV = MVT::getSizeInBits(XType)-ShCtV-1;
2294 SDOperand ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
2295 SDOperand Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
2296 WorkList.push_back(Shift.Val);
2297 if (XType > AType) {
2298 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
2299 WorkList.push_back(Shift.Val);
2300 }
2301 return DAG.getNode(ISD::AND, AType, Shift, N2);
2302 }
2303 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
2304 DAG.getConstant(MVT::getSizeInBits(XType)-1,
2305 TLI.getShiftAmountTy()));
2306 WorkList.push_back(Shift.Val);
2307 if (XType > AType) {
2308 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
2309 WorkList.push_back(Shift.Val);
2310 }
2311 return DAG.getNode(ISD::AND, AType, Shift, N2);
2312 }
2313 }
Nate Begeman07ed4172005-10-10 21:26:48 +00002314
2315 // fold select C, 16, 0 -> shl C, 4
2316 if (N2C && N3C && N3C->isNullValue() && isPowerOf2_64(N2C->getValue()) &&
2317 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) {
2318 // Get a SetCC of the condition
2319 // FIXME: Should probably make sure that setcc is legal if we ever have a
2320 // target where it isn't.
2321 SDOperand Temp, SCC = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
2322 WorkList.push_back(SCC.Val);
2323 // cast from setcc result type to select result type
2324 if (AfterLegalize)
2325 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
2326 else
2327 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
2328 WorkList.push_back(Temp.Val);
2329 // shl setcc result by log2 n2c
2330 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
2331 DAG.getConstant(Log2_64(N2C->getValue()),
2332 TLI.getShiftAmountTy()));
2333 }
2334
Nate Begemanf845b452005-10-08 00:29:44 +00002335 // Check to see if this is the equivalent of setcc
2336 // FIXME: Turn all of these into setcc if setcc if setcc is legal
2337 // otherwise, go ahead with the folds.
2338 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getValue() == 1ULL)) {
2339 MVT::ValueType XType = N0.getValueType();
2340 if (TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultTy())) {
2341 SDOperand Res = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
2342 if (Res.getValueType() != VT)
2343 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
2344 return Res;
2345 }
2346
2347 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
2348 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
2349 TLI.isOperationLegal(ISD::CTLZ, XType)) {
2350 SDOperand Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
2351 return DAG.getNode(ISD::SRL, XType, Ctlz,
2352 DAG.getConstant(Log2_32(MVT::getSizeInBits(XType)),
2353 TLI.getShiftAmountTy()));
2354 }
2355 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
2356 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
2357 SDOperand NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
2358 N0);
2359 SDOperand NotN0 = DAG.getNode(ISD::XOR, XType, N0,
2360 DAG.getConstant(~0ULL, XType));
2361 return DAG.getNode(ISD::SRL, XType,
2362 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
2363 DAG.getConstant(MVT::getSizeInBits(XType)-1,
2364 TLI.getShiftAmountTy()));
2365 }
2366 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
2367 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
2368 SDOperand Sign = DAG.getNode(ISD::SRL, XType, N0,
2369 DAG.getConstant(MVT::getSizeInBits(XType)-1,
2370 TLI.getShiftAmountTy()));
2371 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
2372 }
2373 }
2374
2375 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
2376 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
2377 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
2378 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1)) {
2379 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N2.getOperand(0))) {
2380 MVT::ValueType XType = N0.getValueType();
2381 if (SubC->isNullValue() && MVT::isInteger(XType)) {
2382 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
2383 DAG.getConstant(MVT::getSizeInBits(XType)-1,
2384 TLI.getShiftAmountTy()));
2385 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
2386 WorkList.push_back(Shift.Val);
2387 WorkList.push_back(Add.Val);
2388 return DAG.getNode(ISD::XOR, XType, Add, Shift);
2389 }
2390 }
2391 }
2392
Nate Begeman44728a72005-09-19 22:34:01 +00002393 return SDOperand();
2394}
2395
Nate Begeman452d7be2005-09-16 00:54:12 +00002396SDOperand DAGCombiner::SimplifySetCC(MVT::ValueType VT, SDOperand N0,
Nate Begemane17daeb2005-10-05 21:43:42 +00002397 SDOperand N1, ISD::CondCode Cond,
2398 bool foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00002399 // These setcc operations always fold.
2400 switch (Cond) {
2401 default: break;
2402 case ISD::SETFALSE:
2403 case ISD::SETFALSE2: return DAG.getConstant(0, VT);
2404 case ISD::SETTRUE:
2405 case ISD::SETTRUE2: return DAG.getConstant(1, VT);
2406 }
2407
2408 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
2409 uint64_t C1 = N1C->getValue();
2410 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val)) {
2411 uint64_t C0 = N0C->getValue();
2412
2413 // Sign extend the operands if required
2414 if (ISD::isSignedIntSetCC(Cond)) {
2415 C0 = N0C->getSignExtended();
2416 C1 = N1C->getSignExtended();
2417 }
2418
2419 switch (Cond) {
2420 default: assert(0 && "Unknown integer setcc!");
2421 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
2422 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
2423 case ISD::SETULT: return DAG.getConstant(C0 < C1, VT);
2424 case ISD::SETUGT: return DAG.getConstant(C0 > C1, VT);
2425 case ISD::SETULE: return DAG.getConstant(C0 <= C1, VT);
2426 case ISD::SETUGE: return DAG.getConstant(C0 >= C1, VT);
2427 case ISD::SETLT: return DAG.getConstant((int64_t)C0 < (int64_t)C1, VT);
2428 case ISD::SETGT: return DAG.getConstant((int64_t)C0 > (int64_t)C1, VT);
2429 case ISD::SETLE: return DAG.getConstant((int64_t)C0 <= (int64_t)C1, VT);
2430 case ISD::SETGE: return DAG.getConstant((int64_t)C0 >= (int64_t)C1, VT);
2431 }
2432 } else {
2433 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
2434 if (N0.getOpcode() == ISD::ZERO_EXTEND) {
2435 unsigned InSize = MVT::getSizeInBits(N0.getOperand(0).getValueType());
2436
2437 // If the comparison constant has bits in the upper part, the
2438 // zero-extended value could never match.
2439 if (C1 & (~0ULL << InSize)) {
2440 unsigned VSize = MVT::getSizeInBits(N0.getValueType());
2441 switch (Cond) {
2442 case ISD::SETUGT:
2443 case ISD::SETUGE:
2444 case ISD::SETEQ: return DAG.getConstant(0, VT);
2445 case ISD::SETULT:
2446 case ISD::SETULE:
2447 case ISD::SETNE: return DAG.getConstant(1, VT);
2448 case ISD::SETGT:
2449 case ISD::SETGE:
2450 // True if the sign bit of C1 is set.
2451 return DAG.getConstant((C1 & (1ULL << VSize)) != 0, VT);
2452 case ISD::SETLT:
2453 case ISD::SETLE:
2454 // True if the sign bit of C1 isn't set.
2455 return DAG.getConstant((C1 & (1ULL << VSize)) == 0, VT);
2456 default:
2457 break;
2458 }
2459 }
2460
2461 // Otherwise, we can perform the comparison with the low bits.
2462 switch (Cond) {
2463 case ISD::SETEQ:
2464 case ISD::SETNE:
2465 case ISD::SETUGT:
2466 case ISD::SETUGE:
2467 case ISD::SETULT:
2468 case ISD::SETULE:
2469 return DAG.getSetCC(VT, N0.getOperand(0),
2470 DAG.getConstant(C1, N0.getOperand(0).getValueType()),
2471 Cond);
2472 default:
2473 break; // todo, be more careful with signed comparisons
2474 }
2475 } else if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
2476 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
2477 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N0.getOperand(1))->getVT();
2478 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
2479 MVT::ValueType ExtDstTy = N0.getValueType();
2480 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
2481
2482 // If the extended part has any inconsistent bits, it cannot ever
2483 // compare equal. In other words, they have to be all ones or all
2484 // zeros.
2485 uint64_t ExtBits =
2486 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
2487 if ((C1 & ExtBits) != 0 && (C1 & ExtBits) != ExtBits)
2488 return DAG.getConstant(Cond == ISD::SETNE, VT);
2489
2490 SDOperand ZextOp;
2491 MVT::ValueType Op0Ty = N0.getOperand(0).getValueType();
2492 if (Op0Ty == ExtSrcTy) {
2493 ZextOp = N0.getOperand(0);
2494 } else {
2495 int64_t Imm = ~0ULL >> (64-ExtSrcTyBits);
2496 ZextOp = DAG.getNode(ISD::AND, Op0Ty, N0.getOperand(0),
2497 DAG.getConstant(Imm, Op0Ty));
2498 }
2499 WorkList.push_back(ZextOp.Val);
2500 // Otherwise, make this a use of a zext.
2501 return DAG.getSetCC(VT, ZextOp,
2502 DAG.getConstant(C1 & (~0ULL>>(64-ExtSrcTyBits)),
2503 ExtDstTy),
2504 Cond);
2505 }
Chris Lattner5c46f742005-10-05 06:11:08 +00002506
Nate Begeman452d7be2005-09-16 00:54:12 +00002507 uint64_t MinVal, MaxVal;
2508 unsigned OperandBitSize = MVT::getSizeInBits(N1C->getValueType(0));
2509 if (ISD::isSignedIntSetCC(Cond)) {
2510 MinVal = 1ULL << (OperandBitSize-1);
2511 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
2512 MaxVal = ~0ULL >> (65-OperandBitSize);
2513 else
2514 MaxVal = 0;
2515 } else {
2516 MinVal = 0;
2517 MaxVal = ~0ULL >> (64-OperandBitSize);
2518 }
2519
2520 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
2521 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
2522 if (C1 == MinVal) return DAG.getConstant(1, VT); // X >= MIN --> true
2523 --C1; // X >= C0 --> X > (C0-1)
2524 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
2525 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
2526 }
2527
2528 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
2529 if (C1 == MaxVal) return DAG.getConstant(1, VT); // X <= MAX --> true
2530 ++C1; // X <= C0 --> X < (C0+1)
2531 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
2532 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
2533 }
2534
2535 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal)
2536 return DAG.getConstant(0, VT); // X < MIN --> false
2537
2538 // Canonicalize setgt X, Min --> setne X, Min
2539 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MinVal)
2540 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
Chris Lattnerc8597ca2005-10-21 21:23:25 +00002541 // Canonicalize setlt X, Max --> setne X, Max
2542 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MaxVal)
2543 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
Nate Begeman452d7be2005-09-16 00:54:12 +00002544
2545 // If we have setult X, 1, turn it into seteq X, 0
2546 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal+1)
2547 return DAG.getSetCC(VT, N0, DAG.getConstant(MinVal, N0.getValueType()),
2548 ISD::SETEQ);
2549 // If we have setugt X, Max-1, turn it into seteq X, Max
2550 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal-1)
2551 return DAG.getSetCC(VT, N0, DAG.getConstant(MaxVal, N0.getValueType()),
2552 ISD::SETEQ);
2553
2554 // If we have "setcc X, C0", check to see if we can shrink the immediate
2555 // by changing cc.
2556
2557 // SETUGT X, SINTMAX -> SETLT X, 0
2558 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
2559 C1 == (~0ULL >> (65-OperandBitSize)))
2560 return DAG.getSetCC(VT, N0, DAG.getConstant(0, N1.getValueType()),
2561 ISD::SETLT);
2562
2563 // FIXME: Implement the rest of these.
2564
2565 // Fold bit comparisons when we can.
2566 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
2567 VT == N0.getValueType() && N0.getOpcode() == ISD::AND)
2568 if (ConstantSDNode *AndRHS =
2569 dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2570 if (Cond == ISD::SETNE && C1 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
2571 // Perform the xform if the AND RHS is a single bit.
2572 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
2573 return DAG.getNode(ISD::SRL, VT, N0,
2574 DAG.getConstant(Log2_64(AndRHS->getValue()),
2575 TLI.getShiftAmountTy()));
2576 }
2577 } else if (Cond == ISD::SETEQ && C1 == AndRHS->getValue()) {
2578 // (X & 8) == 8 --> (X & 8) >> 3
2579 // Perform the xform if C1 is a single bit.
2580 if ((C1 & (C1-1)) == 0) {
2581 return DAG.getNode(ISD::SRL, VT, N0,
2582 DAG.getConstant(Log2_64(C1),TLI.getShiftAmountTy()));
2583 }
2584 }
2585 }
2586 }
2587 } else if (isa<ConstantSDNode>(N0.Val)) {
2588 // Ensure that the constant occurs on the RHS.
2589 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
2590 }
2591
2592 if (ConstantFPSDNode *N0C = dyn_cast<ConstantFPSDNode>(N0.Val))
2593 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) {
2594 double C0 = N0C->getValue(), C1 = N1C->getValue();
2595
2596 switch (Cond) {
2597 default: break; // FIXME: Implement the rest of these!
2598 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
2599 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
2600 case ISD::SETLT: return DAG.getConstant(C0 < C1, VT);
2601 case ISD::SETGT: return DAG.getConstant(C0 > C1, VT);
2602 case ISD::SETLE: return DAG.getConstant(C0 <= C1, VT);
2603 case ISD::SETGE: return DAG.getConstant(C0 >= C1, VT);
2604 }
2605 } else {
2606 // Ensure that the constant occurs on the RHS.
2607 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
2608 }
2609
2610 if (N0 == N1) {
2611 // We can always fold X == Y for integer setcc's.
2612 if (MVT::isInteger(N0.getValueType()))
2613 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
2614 unsigned UOF = ISD::getUnorderedFlavor(Cond);
2615 if (UOF == 2) // FP operators that are undefined on NaNs.
2616 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
2617 if (UOF == unsigned(ISD::isTrueWhenEqual(Cond)))
2618 return DAG.getConstant(UOF, VT);
2619 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
2620 // if it is not already.
Chris Lattner4090aee2006-01-18 19:13:41 +00002621 ISD::CondCode NewCond = UOF == 0 ? ISD::SETO : ISD::SETUO;
Nate Begeman452d7be2005-09-16 00:54:12 +00002622 if (NewCond != Cond)
2623 return DAG.getSetCC(VT, N0, N1, NewCond);
2624 }
2625
2626 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
2627 MVT::isInteger(N0.getValueType())) {
2628 if (N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::SUB ||
2629 N0.getOpcode() == ISD::XOR) {
2630 // Simplify (X+Y) == (X+Z) --> Y == Z
2631 if (N0.getOpcode() == N1.getOpcode()) {
2632 if (N0.getOperand(0) == N1.getOperand(0))
2633 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(1), Cond);
2634 if (N0.getOperand(1) == N1.getOperand(1))
2635 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(0), Cond);
2636 if (isCommutativeBinOp(N0.getOpcode())) {
2637 // If X op Y == Y op X, try other combinations.
2638 if (N0.getOperand(0) == N1.getOperand(1))
2639 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(0), Cond);
2640 if (N0.getOperand(1) == N1.getOperand(0))
Chris Lattnera158eee2005-10-25 18:57:30 +00002641 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(1), Cond);
Nate Begeman452d7be2005-09-16 00:54:12 +00002642 }
2643 }
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00002644
2645 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(N1)) {
2646 if (ConstantSDNode *LHSR = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2647 // Turn (X+C1) == C2 --> X == C2-C1
2648 if (N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse()) {
2649 return DAG.getSetCC(VT, N0.getOperand(0),
2650 DAG.getConstant(RHSC->getValue()-LHSR->getValue(),
2651 N0.getValueType()), Cond);
2652 }
2653
2654 // Turn (X^C1) == C2 into X == C1^C2 iff X&~C1 = 0.
2655 if (N0.getOpcode() == ISD::XOR)
Chris Lattner5c46f742005-10-05 06:11:08 +00002656 // If we know that all of the inverted bits are zero, don't bother
2657 // performing the inversion.
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00002658 if (TLI.MaskedValueIsZero(N0.getOperand(0), ~LHSR->getValue()))
Chris Lattner5c46f742005-10-05 06:11:08 +00002659 return DAG.getSetCC(VT, N0.getOperand(0),
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00002660 DAG.getConstant(LHSR->getValue()^RHSC->getValue(),
Chris Lattner5c46f742005-10-05 06:11:08 +00002661 N0.getValueType()), Cond);
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00002662 }
2663
2664 // Turn (C1-X) == C2 --> X == C1-C2
2665 if (ConstantSDNode *SUBC = dyn_cast<ConstantSDNode>(N0.getOperand(0))) {
2666 if (N0.getOpcode() == ISD::SUB && N0.Val->hasOneUse()) {
2667 return DAG.getSetCC(VT, N0.getOperand(1),
2668 DAG.getConstant(SUBC->getValue()-RHSC->getValue(),
2669 N0.getValueType()), Cond);
Chris Lattner5c46f742005-10-05 06:11:08 +00002670 }
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00002671 }
2672 }
2673
Nate Begeman452d7be2005-09-16 00:54:12 +00002674 // Simplify (X+Z) == X --> Z == 0
2675 if (N0.getOperand(0) == N1)
2676 return DAG.getSetCC(VT, N0.getOperand(1),
2677 DAG.getConstant(0, N0.getValueType()), Cond);
2678 if (N0.getOperand(1) == N1) {
2679 if (isCommutativeBinOp(N0.getOpcode()))
2680 return DAG.getSetCC(VT, N0.getOperand(0),
2681 DAG.getConstant(0, N0.getValueType()), Cond);
2682 else {
2683 assert(N0.getOpcode() == ISD::SUB && "Unexpected operation!");
2684 // (Z-X) == X --> Z == X<<1
2685 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(),
2686 N1,
2687 DAG.getConstant(1,TLI.getShiftAmountTy()));
2688 WorkList.push_back(SH.Val);
2689 return DAG.getSetCC(VT, N0.getOperand(0), SH, Cond);
2690 }
2691 }
2692 }
2693
2694 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
2695 N1.getOpcode() == ISD::XOR) {
2696 // Simplify X == (X+Z) --> Z == 0
2697 if (N1.getOperand(0) == N0) {
2698 return DAG.getSetCC(VT, N1.getOperand(1),
2699 DAG.getConstant(0, N1.getValueType()), Cond);
2700 } else if (N1.getOperand(1) == N0) {
2701 if (isCommutativeBinOp(N1.getOpcode())) {
2702 return DAG.getSetCC(VT, N1.getOperand(0),
2703 DAG.getConstant(0, N1.getValueType()), Cond);
2704 } else {
2705 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
2706 // X == (Z-X) --> X<<1 == Z
2707 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(), N0,
2708 DAG.getConstant(1,TLI.getShiftAmountTy()));
2709 WorkList.push_back(SH.Val);
2710 return DAG.getSetCC(VT, SH, N1.getOperand(0), Cond);
2711 }
2712 }
2713 }
2714 }
2715
2716 // Fold away ALL boolean setcc's.
2717 SDOperand Temp;
Nate Begemane17daeb2005-10-05 21:43:42 +00002718 if (N0.getValueType() == MVT::i1 && foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00002719 switch (Cond) {
2720 default: assert(0 && "Unknown integer setcc!");
2721 case ISD::SETEQ: // X == Y -> (X^Y)^1
2722 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
2723 N0 = DAG.getNode(ISD::XOR, MVT::i1, Temp, DAG.getConstant(1, MVT::i1));
2724 WorkList.push_back(Temp.Val);
2725 break;
2726 case ISD::SETNE: // X != Y --> (X^Y)
2727 N0 = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
2728 break;
2729 case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> X^1 & Y
2730 case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> X^1 & Y
2731 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
2732 N0 = DAG.getNode(ISD::AND, MVT::i1, N1, Temp);
2733 WorkList.push_back(Temp.Val);
2734 break;
2735 case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> Y^1 & X
2736 case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> Y^1 & X
2737 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
2738 N0 = DAG.getNode(ISD::AND, MVT::i1, N0, Temp);
2739 WorkList.push_back(Temp.Val);
2740 break;
2741 case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> X^1 | Y
2742 case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> X^1 | Y
2743 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
2744 N0 = DAG.getNode(ISD::OR, MVT::i1, N1, Temp);
2745 WorkList.push_back(Temp.Val);
2746 break;
2747 case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> Y^1 | X
2748 case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> Y^1 | X
2749 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
2750 N0 = DAG.getNode(ISD::OR, MVT::i1, N0, Temp);
2751 break;
2752 }
2753 if (VT != MVT::i1) {
2754 WorkList.push_back(N0.Val);
2755 // FIXME: If running after legalize, we probably can't do this.
2756 N0 = DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
2757 }
2758 return N0;
2759 }
2760
2761 // Could not fold it.
2762 return SDOperand();
2763}
2764
Nate Begeman69575232005-10-20 02:15:44 +00002765/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
2766/// return a DAG expression to select that will generate the same value by
2767/// multiplying by a magic number. See:
2768/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
2769SDOperand DAGCombiner::BuildSDIV(SDNode *N) {
2770 MVT::ValueType VT = N->getValueType(0);
Chris Lattnere9936d12005-10-22 18:50:15 +00002771
2772 // Check to see if we can do this.
2773 if (!TLI.isTypeLegal(VT) || (VT != MVT::i32 && VT != MVT::i64))
2774 return SDOperand(); // BuildSDIV only operates on i32 or i64
2775 if (!TLI.isOperationLegal(ISD::MULHS, VT))
2776 return SDOperand(); // Make sure the target supports MULHS.
Nate Begeman69575232005-10-20 02:15:44 +00002777
Nate Begemanc6a454e2005-10-20 17:45:03 +00002778 int64_t d = cast<ConstantSDNode>(N->getOperand(1))->getSignExtended();
Nate Begeman69575232005-10-20 02:15:44 +00002779 ms magics = (VT == MVT::i32) ? magic32(d) : magic64(d);
2780
2781 // Multiply the numerator (operand 0) by the magic value
2782 SDOperand Q = DAG.getNode(ISD::MULHS, VT, N->getOperand(0),
2783 DAG.getConstant(magics.m, VT));
2784 // If d > 0 and m < 0, add the numerator
2785 if (d > 0 && magics.m < 0) {
2786 Q = DAG.getNode(ISD::ADD, VT, Q, N->getOperand(0));
2787 WorkList.push_back(Q.Val);
2788 }
2789 // If d < 0 and m > 0, subtract the numerator.
2790 if (d < 0 && magics.m > 0) {
2791 Q = DAG.getNode(ISD::SUB, VT, Q, N->getOperand(0));
2792 WorkList.push_back(Q.Val);
2793 }
2794 // Shift right algebraic if shift value is nonzero
2795 if (magics.s > 0) {
2796 Q = DAG.getNode(ISD::SRA, VT, Q,
2797 DAG.getConstant(magics.s, TLI.getShiftAmountTy()));
2798 WorkList.push_back(Q.Val);
2799 }
2800 // Extract the sign bit and add it to the quotient
2801 SDOperand T =
Nate Begeman4d385672005-10-21 01:51:45 +00002802 DAG.getNode(ISD::SRL, VT, Q, DAG.getConstant(MVT::getSizeInBits(VT)-1,
2803 TLI.getShiftAmountTy()));
Nate Begeman69575232005-10-20 02:15:44 +00002804 WorkList.push_back(T.Val);
2805 return DAG.getNode(ISD::ADD, VT, Q, T);
2806}
2807
2808/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
2809/// return a DAG expression to select that will generate the same value by
2810/// multiplying by a magic number. See:
2811/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
2812SDOperand DAGCombiner::BuildUDIV(SDNode *N) {
2813 MVT::ValueType VT = N->getValueType(0);
Chris Lattnere9936d12005-10-22 18:50:15 +00002814
2815 // Check to see if we can do this.
2816 if (!TLI.isTypeLegal(VT) || (VT != MVT::i32 && VT != MVT::i64))
2817 return SDOperand(); // BuildUDIV only operates on i32 or i64
2818 if (!TLI.isOperationLegal(ISD::MULHU, VT))
2819 return SDOperand(); // Make sure the target supports MULHU.
Nate Begeman69575232005-10-20 02:15:44 +00002820
2821 uint64_t d = cast<ConstantSDNode>(N->getOperand(1))->getValue();
2822 mu magics = (VT == MVT::i32) ? magicu32(d) : magicu64(d);
2823
2824 // Multiply the numerator (operand 0) by the magic value
2825 SDOperand Q = DAG.getNode(ISD::MULHU, VT, N->getOperand(0),
2826 DAG.getConstant(magics.m, VT));
2827 WorkList.push_back(Q.Val);
2828
2829 if (magics.a == 0) {
2830 return DAG.getNode(ISD::SRL, VT, Q,
2831 DAG.getConstant(magics.s, TLI.getShiftAmountTy()));
2832 } else {
2833 SDOperand NPQ = DAG.getNode(ISD::SUB, VT, N->getOperand(0), Q);
2834 WorkList.push_back(NPQ.Val);
2835 NPQ = DAG.getNode(ISD::SRL, VT, NPQ,
2836 DAG.getConstant(1, TLI.getShiftAmountTy()));
2837 WorkList.push_back(NPQ.Val);
2838 NPQ = DAG.getNode(ISD::ADD, VT, NPQ, Q);
2839 WorkList.push_back(NPQ.Val);
2840 return DAG.getNode(ISD::SRL, VT, NPQ,
2841 DAG.getConstant(magics.s-1, TLI.getShiftAmountTy()));
2842 }
2843}
2844
Nate Begeman1d4d4142005-09-01 00:19:25 +00002845// SelectionDAG::Combine - This is the entry point for the file.
2846//
Nate Begeman4ebd8052005-09-01 23:24:04 +00002847void SelectionDAG::Combine(bool RunningAfterLegalize) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002848 /// run - This is the main entry point to this class.
2849 ///
Nate Begeman4ebd8052005-09-01 23:24:04 +00002850 DAGCombiner(*this).Run(RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002851}