blob: 4d5dcd780e383c82d2e3134eae4206a548292b5a [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 }
Nate Begeman368e18d2006-02-16 21:11:51 +0000101
102 bool DemandedBitsAreZero(SDOperand Op, uint64_t DemandedMask,
103 SDOperand &Old, SDOperand &New) const {
104 TargetLowering::TargetLoweringOpt TLO(DAG);
105 uint64_t KnownZero, KnownOne;
106 if (TLI.SimplifyDemandedBits(Op, DemandedMask, KnownZero, KnownOne, TLO)){
107 Old = TLO.Old;
108 New = TLO.New;
109 return true;
110 }
111 return false;
112 }
Chris Lattner87514ca2005-10-10 22:31:19 +0000113
114 SDOperand CombineTo(SDNode *N, SDOperand Res) {
115 std::vector<SDOperand> To;
116 To.push_back(Res);
117 return CombineTo(N, To);
118 }
Chris Lattner01a22022005-10-10 22:04:48 +0000119
120 SDOperand CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1) {
121 std::vector<SDOperand> To;
122 To.push_back(Res0);
123 To.push_back(Res1);
124 return CombineTo(N, To);
125 }
126
Nate Begeman1d4d4142005-09-01 00:19:25 +0000127 /// visit - call the node-specific routine that knows how to fold each
128 /// particular type of node.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000129 SDOperand visit(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000130
131 // Visitation implementation - Implement dag node combining for different
132 // node types. The semantics are as follows:
133 // Return Value:
Nate Begeman2300f552005-09-07 00:15:36 +0000134 // SDOperand.Val == 0 - No change was made
Chris Lattner01a22022005-10-10 22:04:48 +0000135 // SDOperand.Val == N - N was replaced, is dead, and is already handled.
Nate Begeman2300f552005-09-07 00:15:36 +0000136 // otherwise - N should be replaced by the returned Operand.
Nate Begeman1d4d4142005-09-01 00:19:25 +0000137 //
Nate Begeman83e75ec2005-09-06 04:43:02 +0000138 SDOperand visitTokenFactor(SDNode *N);
139 SDOperand visitADD(SDNode *N);
140 SDOperand visitSUB(SDNode *N);
141 SDOperand visitMUL(SDNode *N);
142 SDOperand visitSDIV(SDNode *N);
143 SDOperand visitUDIV(SDNode *N);
144 SDOperand visitSREM(SDNode *N);
145 SDOperand visitUREM(SDNode *N);
146 SDOperand visitMULHU(SDNode *N);
147 SDOperand visitMULHS(SDNode *N);
148 SDOperand visitAND(SDNode *N);
149 SDOperand visitOR(SDNode *N);
150 SDOperand visitXOR(SDNode *N);
151 SDOperand visitSHL(SDNode *N);
152 SDOperand visitSRA(SDNode *N);
153 SDOperand visitSRL(SDNode *N);
154 SDOperand visitCTLZ(SDNode *N);
155 SDOperand visitCTTZ(SDNode *N);
156 SDOperand visitCTPOP(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000157 SDOperand visitSELECT(SDNode *N);
158 SDOperand visitSELECT_CC(SDNode *N);
159 SDOperand visitSETCC(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000160 SDOperand visitSIGN_EXTEND(SDNode *N);
161 SDOperand visitZERO_EXTEND(SDNode *N);
162 SDOperand visitSIGN_EXTEND_INREG(SDNode *N);
163 SDOperand visitTRUNCATE(SDNode *N);
Chris Lattner94683772005-12-23 05:30:37 +0000164 SDOperand visitBIT_CONVERT(SDNode *N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000165 SDOperand visitFADD(SDNode *N);
166 SDOperand visitFSUB(SDNode *N);
167 SDOperand visitFMUL(SDNode *N);
168 SDOperand visitFDIV(SDNode *N);
169 SDOperand visitFREM(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000170 SDOperand visitSINT_TO_FP(SDNode *N);
171 SDOperand visitUINT_TO_FP(SDNode *N);
172 SDOperand visitFP_TO_SINT(SDNode *N);
173 SDOperand visitFP_TO_UINT(SDNode *N);
174 SDOperand visitFP_ROUND(SDNode *N);
175 SDOperand visitFP_ROUND_INREG(SDNode *N);
176 SDOperand visitFP_EXTEND(SDNode *N);
177 SDOperand visitFNEG(SDNode *N);
178 SDOperand visitFABS(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000179 SDOperand visitBRCOND(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000180 SDOperand visitBRCONDTWOWAY(SDNode *N);
181 SDOperand visitBR_CC(SDNode *N);
182 SDOperand visitBRTWOWAY_CC(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000183 SDOperand visitLOAD(SDNode *N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000184 SDOperand visitSTORE(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000185
Nate Begemancd4d58c2006-02-03 06:46:56 +0000186 SDOperand ReassociateOps(unsigned Opc, SDOperand LHS, SDOperand RHS);
187
Chris Lattner40c62d52005-10-18 06:04:22 +0000188 bool SimplifySelectOps(SDNode *SELECT, SDOperand LHS, SDOperand RHS);
Nate Begeman44728a72005-09-19 22:34:01 +0000189 SDOperand SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2);
190 SDOperand SimplifySelectCC(SDOperand N0, SDOperand N1, SDOperand N2,
191 SDOperand N3, ISD::CondCode CC);
Nate Begeman452d7be2005-09-16 00:54:12 +0000192 SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N0, SDOperand N1,
Nate Begemane17daeb2005-10-05 21:43:42 +0000193 ISD::CondCode Cond, bool foldBooleans = true);
Nate Begeman69575232005-10-20 02:15:44 +0000194
195 SDOperand BuildSDIV(SDNode *N);
196 SDOperand BuildUDIV(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000197public:
198 DAGCombiner(SelectionDAG &D)
Nate Begeman646d7e22005-09-02 21:18:40 +0000199 : DAG(D), TLI(D.getTargetLoweringInfo()), AfterLegalize(false) {}
Nate Begeman1d4d4142005-09-01 00:19:25 +0000200
201 /// Run - runs the dag combiner on all nodes in the work list
Nate Begeman4ebd8052005-09-01 23:24:04 +0000202 void Run(bool RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000203 };
204}
205
Nate Begeman69575232005-10-20 02:15:44 +0000206struct ms {
207 int64_t m; // magic number
208 int64_t s; // shift amount
209};
210
211struct mu {
212 uint64_t m; // magic number
213 int64_t a; // add indicator
214 int64_t s; // shift amount
215};
216
217/// magic - calculate the magic numbers required to codegen an integer sdiv as
218/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
219/// or -1.
220static ms magic32(int32_t d) {
221 int32_t p;
222 uint32_t ad, anc, delta, q1, r1, q2, r2, t;
223 const uint32_t two31 = 0x80000000U;
224 struct ms mag;
225
226 ad = abs(d);
227 t = two31 + ((uint32_t)d >> 31);
228 anc = t - 1 - t%ad; // absolute value of nc
229 p = 31; // initialize p
230 q1 = two31/anc; // initialize q1 = 2p/abs(nc)
231 r1 = two31 - q1*anc; // initialize r1 = rem(2p,abs(nc))
232 q2 = two31/ad; // initialize q2 = 2p/abs(d)
233 r2 = two31 - q2*ad; // initialize r2 = rem(2p,abs(d))
234 do {
235 p = p + 1;
236 q1 = 2*q1; // update q1 = 2p/abs(nc)
237 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
238 if (r1 >= anc) { // must be unsigned comparison
239 q1 = q1 + 1;
240 r1 = r1 - anc;
241 }
242 q2 = 2*q2; // update q2 = 2p/abs(d)
243 r2 = 2*r2; // update r2 = rem(2p/abs(d))
244 if (r2 >= ad) { // must be unsigned comparison
245 q2 = q2 + 1;
246 r2 = r2 - ad;
247 }
248 delta = ad - r2;
249 } while (q1 < delta || (q1 == delta && r1 == 0));
250
251 mag.m = (int32_t)(q2 + 1); // make sure to sign extend
252 if (d < 0) mag.m = -mag.m; // resulting magic number
253 mag.s = p - 32; // resulting shift
254 return mag;
255}
256
257/// magicu - calculate the magic numbers required to codegen an integer udiv as
258/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
259static mu magicu32(uint32_t d) {
260 int32_t p;
261 uint32_t nc, delta, q1, r1, q2, r2;
262 struct mu magu;
263 magu.a = 0; // initialize "add" indicator
264 nc = - 1 - (-d)%d;
265 p = 31; // initialize p
266 q1 = 0x80000000/nc; // initialize q1 = 2p/nc
267 r1 = 0x80000000 - q1*nc; // initialize r1 = rem(2p,nc)
268 q2 = 0x7FFFFFFF/d; // initialize q2 = (2p-1)/d
269 r2 = 0x7FFFFFFF - q2*d; // initialize r2 = rem((2p-1),d)
270 do {
271 p = p + 1;
272 if (r1 >= nc - r1 ) {
273 q1 = 2*q1 + 1; // update q1
274 r1 = 2*r1 - nc; // update r1
275 }
276 else {
277 q1 = 2*q1; // update q1
278 r1 = 2*r1; // update r1
279 }
280 if (r2 + 1 >= d - r2) {
281 if (q2 >= 0x7FFFFFFF) magu.a = 1;
282 q2 = 2*q2 + 1; // update q2
283 r2 = 2*r2 + 1 - d; // update r2
284 }
285 else {
286 if (q2 >= 0x80000000) magu.a = 1;
287 q2 = 2*q2; // update q2
288 r2 = 2*r2 + 1; // update r2
289 }
290 delta = d - 1 - r2;
291 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
292 magu.m = q2 + 1; // resulting magic number
293 magu.s = p - 32; // resulting shift
294 return magu;
295}
296
297/// magic - calculate the magic numbers required to codegen an integer sdiv as
298/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
299/// or -1.
300static ms magic64(int64_t d) {
301 int64_t p;
302 uint64_t ad, anc, delta, q1, r1, q2, r2, t;
303 const uint64_t two63 = 9223372036854775808ULL; // 2^63
304 struct ms mag;
305
Chris Lattnerf75f2a02005-10-20 17:01:00 +0000306 ad = d >= 0 ? d : -d;
Nate Begeman69575232005-10-20 02:15:44 +0000307 t = two63 + ((uint64_t)d >> 63);
308 anc = t - 1 - t%ad; // absolute value of nc
309 p = 63; // initialize p
310 q1 = two63/anc; // initialize q1 = 2p/abs(nc)
311 r1 = two63 - q1*anc; // initialize r1 = rem(2p,abs(nc))
312 q2 = two63/ad; // initialize q2 = 2p/abs(d)
313 r2 = two63 - q2*ad; // initialize r2 = rem(2p,abs(d))
314 do {
315 p = p + 1;
316 q1 = 2*q1; // update q1 = 2p/abs(nc)
317 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
318 if (r1 >= anc) { // must be unsigned comparison
319 q1 = q1 + 1;
320 r1 = r1 - anc;
321 }
322 q2 = 2*q2; // update q2 = 2p/abs(d)
323 r2 = 2*r2; // update r2 = rem(2p/abs(d))
324 if (r2 >= ad) { // must be unsigned comparison
325 q2 = q2 + 1;
326 r2 = r2 - ad;
327 }
328 delta = ad - r2;
329 } while (q1 < delta || (q1 == delta && r1 == 0));
330
331 mag.m = q2 + 1;
332 if (d < 0) mag.m = -mag.m; // resulting magic number
333 mag.s = p - 64; // resulting shift
334 return mag;
335}
336
337/// magicu - calculate the magic numbers required to codegen an integer udiv as
338/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
339static mu magicu64(uint64_t d)
340{
341 int64_t p;
342 uint64_t nc, delta, q1, r1, q2, r2;
343 struct mu magu;
344 magu.a = 0; // initialize "add" indicator
345 nc = - 1 - (-d)%d;
346 p = 63; // initialize p
347 q1 = 0x8000000000000000ull/nc; // initialize q1 = 2p/nc
348 r1 = 0x8000000000000000ull - q1*nc; // initialize r1 = rem(2p,nc)
349 q2 = 0x7FFFFFFFFFFFFFFFull/d; // initialize q2 = (2p-1)/d
350 r2 = 0x7FFFFFFFFFFFFFFFull - q2*d; // initialize r2 = rem((2p-1),d)
351 do {
352 p = p + 1;
353 if (r1 >= nc - r1 ) {
354 q1 = 2*q1 + 1; // update q1
355 r1 = 2*r1 - nc; // update r1
356 }
357 else {
358 q1 = 2*q1; // update q1
359 r1 = 2*r1; // update r1
360 }
361 if (r2 + 1 >= d - r2) {
362 if (q2 >= 0x7FFFFFFFFFFFFFFFull) magu.a = 1;
363 q2 = 2*q2 + 1; // update q2
364 r2 = 2*r2 + 1 - d; // update r2
365 }
366 else {
367 if (q2 >= 0x8000000000000000ull) magu.a = 1;
368 q2 = 2*q2; // update q2
369 r2 = 2*r2 + 1; // update r2
370 }
371 delta = d - 1 - r2;
372 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
373 magu.m = q2 + 1; // resulting magic number
374 magu.s = p - 64; // resulting shift
375 return magu;
376}
377
Nate Begeman4ebd8052005-09-01 23:24:04 +0000378// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
379// that selects between the values 1 and 0, making it equivalent to a setcc.
Nate Begeman646d7e22005-09-02 21:18:40 +0000380// Also, set the incoming LHS, RHS, and CC references to the appropriate
381// nodes based on the type of node we are checking. This simplifies life a
382// bit for the callers.
383static bool isSetCCEquivalent(SDOperand N, SDOperand &LHS, SDOperand &RHS,
384 SDOperand &CC) {
385 if (N.getOpcode() == ISD::SETCC) {
386 LHS = N.getOperand(0);
387 RHS = N.getOperand(1);
388 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000389 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000390 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000391 if (N.getOpcode() == ISD::SELECT_CC &&
392 N.getOperand(2).getOpcode() == ISD::Constant &&
393 N.getOperand(3).getOpcode() == ISD::Constant &&
394 cast<ConstantSDNode>(N.getOperand(2))->getValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000395 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
396 LHS = N.getOperand(0);
397 RHS = N.getOperand(1);
398 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000399 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000400 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000401 return false;
402}
403
Nate Begeman99801192005-09-07 23:25:52 +0000404// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
405// one use. If this is true, it allows the users to invert the operation for
406// free when it is profitable to do so.
407static bool isOneUseSetCC(SDOperand N) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000408 SDOperand N0, N1, N2;
Nate Begeman646d7e22005-09-02 21:18:40 +0000409 if (isSetCCEquivalent(N, N0, N1, N2) && N.Val->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000410 return true;
411 return false;
412}
413
Nate Begeman452d7be2005-09-16 00:54:12 +0000414// FIXME: This should probably go in the ISD class rather than being duplicated
415// in several files.
416static bool isCommutativeBinOp(unsigned Opcode) {
417 switch (Opcode) {
418 case ISD::ADD:
419 case ISD::MUL:
420 case ISD::AND:
421 case ISD::OR:
422 case ISD::XOR: return true;
423 default: return false; // FIXME: Need commutative info for user ops!
424 }
425}
426
Nate Begemancd4d58c2006-02-03 06:46:56 +0000427SDOperand DAGCombiner::ReassociateOps(unsigned Opc, SDOperand N0, SDOperand N1){
428 MVT::ValueType VT = N0.getValueType();
429 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
430 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
431 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
432 if (isa<ConstantSDNode>(N1)) {
433 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(1), N1);
434 WorkList.push_back(OpNode.Val);
435 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(0));
436 } else if (N0.hasOneUse()) {
437 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(0), N1);
438 WorkList.push_back(OpNode.Val);
439 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(1));
440 }
441 }
442 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
443 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
444 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
445 if (isa<ConstantSDNode>(N0)) {
446 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(1), N0);
447 WorkList.push_back(OpNode.Val);
448 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(0));
449 } else if (N1.hasOneUse()) {
450 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(0), N0);
451 WorkList.push_back(OpNode.Val);
452 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(1));
453 }
454 }
455 return SDOperand();
456}
457
Nate Begeman4ebd8052005-09-01 23:24:04 +0000458void DAGCombiner::Run(bool RunningAfterLegalize) {
459 // set the instance variable, so that the various visit routines may use it.
460 AfterLegalize = RunningAfterLegalize;
461
Nate Begeman646d7e22005-09-02 21:18:40 +0000462 // Add all the dag nodes to the worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000463 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
464 E = DAG.allnodes_end(); I != E; ++I)
465 WorkList.push_back(I);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000466
Chris Lattner95038592005-10-05 06:35:28 +0000467 // Create a dummy node (which is not added to allnodes), that adds a reference
468 // to the root node, preventing it from being deleted, and tracking any
469 // changes of the root.
470 HandleSDNode Dummy(DAG.getRoot());
471
Nate Begeman1d4d4142005-09-01 00:19:25 +0000472 // while the worklist isn't empty, inspect the node on the end of it and
473 // try and combine it.
474 while (!WorkList.empty()) {
475 SDNode *N = WorkList.back();
476 WorkList.pop_back();
477
478 // If N has no uses, it is dead. Make sure to revisit all N's operands once
Chris Lattner95038592005-10-05 06:35:28 +0000479 // N is deleted from the DAG, since they too may now be dead or may have a
480 // reduced number of uses, allowing other xforms.
481 if (N->use_empty() && N != &Dummy) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000482 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
483 WorkList.push_back(N->getOperand(i).Val);
484
Nate Begeman1d4d4142005-09-01 00:19:25 +0000485 removeFromWorkList(N);
Chris Lattner95038592005-10-05 06:35:28 +0000486 DAG.DeleteNode(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000487 continue;
488 }
489
Nate Begeman83e75ec2005-09-06 04:43:02 +0000490 SDOperand RV = visit(N);
491 if (RV.Val) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000492 ++NodesCombined;
Nate Begeman646d7e22005-09-02 21:18:40 +0000493 // If we get back the same node we passed in, rather than a new node or
494 // zero, we know that the node must have defined multiple values and
495 // CombineTo was used. Since CombineTo takes care of the worklist
496 // mechanics for us, we have no work to do in this case.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000497 if (RV.Val != N) {
Nate Begeman2300f552005-09-07 00:15:36 +0000498 DEBUG(std::cerr << "\nReplacing "; N->dump();
499 std::cerr << "\nWith: "; RV.Val->dump();
500 std::cerr << '\n');
Chris Lattner01a22022005-10-10 22:04:48 +0000501 std::vector<SDNode*> NowDead;
502 DAG.ReplaceAllUsesWith(N, std::vector<SDOperand>(1, RV), &NowDead);
Nate Begeman646d7e22005-09-02 21:18:40 +0000503
504 // Push the new node and any users onto the worklist
Nate Begeman83e75ec2005-09-06 04:43:02 +0000505 WorkList.push_back(RV.Val);
506 AddUsersToWorkList(RV.Val);
Nate Begeman646d7e22005-09-02 21:18:40 +0000507
508 // Nodes can end up on the worklist more than once. Make sure we do
509 // not process a node that has been replaced.
510 removeFromWorkList(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000511 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
512 removeFromWorkList(NowDead[i]);
Chris Lattner5c46f742005-10-05 06:11:08 +0000513
514 // Finally, since the node is now dead, remove it from the graph.
515 DAG.DeleteNode(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000516 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000517 }
518 }
Chris Lattner95038592005-10-05 06:35:28 +0000519
520 // If the root changed (e.g. it was a dead load, update the root).
521 DAG.setRoot(Dummy.getValue());
Nate Begeman1d4d4142005-09-01 00:19:25 +0000522}
523
Nate Begeman83e75ec2005-09-06 04:43:02 +0000524SDOperand DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000525 switch(N->getOpcode()) {
526 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +0000527 case ISD::TokenFactor: return visitTokenFactor(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000528 case ISD::ADD: return visitADD(N);
529 case ISD::SUB: return visitSUB(N);
530 case ISD::MUL: return visitMUL(N);
531 case ISD::SDIV: return visitSDIV(N);
532 case ISD::UDIV: return visitUDIV(N);
533 case ISD::SREM: return visitSREM(N);
534 case ISD::UREM: return visitUREM(N);
535 case ISD::MULHU: return visitMULHU(N);
536 case ISD::MULHS: return visitMULHS(N);
537 case ISD::AND: return visitAND(N);
538 case ISD::OR: return visitOR(N);
539 case ISD::XOR: return visitXOR(N);
540 case ISD::SHL: return visitSHL(N);
541 case ISD::SRA: return visitSRA(N);
542 case ISD::SRL: return visitSRL(N);
543 case ISD::CTLZ: return visitCTLZ(N);
544 case ISD::CTTZ: return visitCTTZ(N);
545 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000546 case ISD::SELECT: return visitSELECT(N);
547 case ISD::SELECT_CC: return visitSELECT_CC(N);
548 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000549 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
550 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
551 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
552 case ISD::TRUNCATE: return visitTRUNCATE(N);
Chris Lattner94683772005-12-23 05:30:37 +0000553 case ISD::BIT_CONVERT: return visitBIT_CONVERT(N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000554 case ISD::FADD: return visitFADD(N);
555 case ISD::FSUB: return visitFSUB(N);
556 case ISD::FMUL: return visitFMUL(N);
557 case ISD::FDIV: return visitFDIV(N);
558 case ISD::FREM: return visitFREM(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000559 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
560 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
561 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
562 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
563 case ISD::FP_ROUND: return visitFP_ROUND(N);
564 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
565 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
566 case ISD::FNEG: return visitFNEG(N);
567 case ISD::FABS: return visitFABS(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000568 case ISD::BRCOND: return visitBRCOND(N);
569 case ISD::BRCONDTWOWAY: return visitBRCONDTWOWAY(N);
570 case ISD::BR_CC: return visitBR_CC(N);
571 case ISD::BRTWOWAY_CC: return visitBRTWOWAY_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000572 case ISD::LOAD: return visitLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000573 case ISD::STORE: return visitSTORE(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000574 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000575 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000576}
577
Nate Begeman83e75ec2005-09-06 04:43:02 +0000578SDOperand DAGCombiner::visitTokenFactor(SDNode *N) {
Nate Begemanded49632005-10-13 03:11:28 +0000579 std::vector<SDOperand> Ops;
580 bool Changed = false;
581
Nate Begeman1d4d4142005-09-01 00:19:25 +0000582 // If the token factor has two operands and one is the entry token, replace
583 // the token factor with the other operand.
584 if (N->getNumOperands() == 2) {
585 if (N->getOperand(0).getOpcode() == ISD::EntryToken)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000586 return N->getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000587 if (N->getOperand(1).getOpcode() == ISD::EntryToken)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000588 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000589 }
Chris Lattner24edbb72005-10-13 22:10:05 +0000590
Nate Begemanded49632005-10-13 03:11:28 +0000591 // fold (tokenfactor (tokenfactor)) -> tokenfactor
592 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
593 SDOperand Op = N->getOperand(i);
594 if (Op.getOpcode() == ISD::TokenFactor && Op.hasOneUse()) {
595 Changed = true;
596 for (unsigned j = 0, e = Op.getNumOperands(); j != e; ++j)
597 Ops.push_back(Op.getOperand(j));
598 } else {
599 Ops.push_back(Op);
600 }
601 }
602 if (Changed)
603 return DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000604 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000605}
606
Nate Begeman83e75ec2005-09-06 04:43:02 +0000607SDOperand DAGCombiner::visitADD(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000608 SDOperand N0 = N->getOperand(0);
609 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000610 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
611 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemanf89d78d2005-09-07 16:09:19 +0000612 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000613
614 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000615 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000616 return DAG.getNode(ISD::ADD, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000617 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000618 if (N0C && !N1C)
619 return DAG.getNode(ISD::ADD, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000620 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000621 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000622 return N0;
Chris Lattner4aafb4f2006-01-12 20:22:43 +0000623 // fold ((c1-A)+c2) -> (c1+c2)-A
624 if (N1C && N0.getOpcode() == ISD::SUB)
625 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
626 return DAG.getNode(ISD::SUB, VT,
627 DAG.getConstant(N1C->getValue()+N0C->getValue(), VT),
628 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000629 // reassociate add
630 SDOperand RADD = ReassociateOps(ISD::ADD, N0, N1);
631 if (RADD.Val != 0)
632 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000633 // fold ((0-A) + B) -> B-A
634 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
635 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000636 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000637 // fold (A + (0-B)) -> A-B
638 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
639 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000640 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +0000641 // fold (A+(B-A)) -> B
642 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000643 return N1.getOperand(0);
644 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000645}
646
Nate Begeman83e75ec2005-09-06 04:43:02 +0000647SDOperand DAGCombiner::visitSUB(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000648 SDOperand N0 = N->getOperand(0);
649 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000650 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
651 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000652 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000653
Chris Lattner854077d2005-10-17 01:07:11 +0000654 // fold (sub x, x) -> 0
655 if (N0 == N1)
656 return DAG.getConstant(0, N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000657 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000658 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000659 return DAG.getNode(ISD::SUB, VT, N0, N1);
Chris Lattner05b57432005-10-11 06:07:15 +0000660 // fold (sub x, c) -> (add x, -c)
661 if (N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000662 return DAG.getNode(ISD::ADD, VT, N0, DAG.getConstant(-N1C->getValue(), VT));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000663 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +0000664 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000665 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000666 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +0000667 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000668 return N0.getOperand(0);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000669 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000670}
671
Nate Begeman83e75ec2005-09-06 04:43:02 +0000672SDOperand DAGCombiner::visitMUL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000673 SDOperand N0 = N->getOperand(0);
674 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000675 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
676 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman223df222005-09-08 20:18:10 +0000677 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000678
679 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000680 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000681 return DAG.getNode(ISD::MUL, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000682 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000683 if (N0C && !N1C)
684 return DAG.getNode(ISD::MUL, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000685 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000686 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000687 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000688 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +0000689 if (N1C && N1C->isAllOnesValue())
Nate Begeman405e3ec2005-10-21 00:02:42 +0000690 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000691 // fold (mul x, (1 << c)) -> x << c
Nate Begeman646d7e22005-09-02 21:18:40 +0000692 if (N1C && isPowerOf2_64(N1C->getValue()))
Chris Lattner3e6099b2005-10-30 06:41:49 +0000693 return DAG.getNode(ISD::SHL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000694 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000695 TLI.getShiftAmountTy()));
Chris Lattner3e6099b2005-10-30 06:41:49 +0000696 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
697 if (N1C && isPowerOf2_64(-N1C->getSignExtended())) {
698 // FIXME: If the input is something that is easily negated (e.g. a
699 // single-use add), we should put the negate there.
700 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT),
701 DAG.getNode(ISD::SHL, VT, N0,
702 DAG.getConstant(Log2_64(-N1C->getSignExtended()),
703 TLI.getShiftAmountTy())));
704 }
Nate Begemancd4d58c2006-02-03 06:46:56 +0000705 // reassociate mul
706 SDOperand RMUL = ReassociateOps(ISD::MUL, N0, N1);
707 if (RMUL.Val != 0)
708 return RMUL;
Nate Begeman83e75ec2005-09-06 04:43:02 +0000709 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000710}
711
Nate Begeman83e75ec2005-09-06 04:43:02 +0000712SDOperand DAGCombiner::visitSDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000713 SDOperand N0 = N->getOperand(0);
714 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000715 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
716 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000717 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000718
719 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000720 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000721 return DAG.getNode(ISD::SDIV, VT, N0, N1);
Nate Begeman405e3ec2005-10-21 00:02:42 +0000722 // fold (sdiv X, 1) -> X
723 if (N1C && N1C->getSignExtended() == 1LL)
724 return N0;
725 // fold (sdiv X, -1) -> 0-X
726 if (N1C && N1C->isAllOnesValue())
727 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +0000728 // If we know the sign bits of both operands are zero, strength reduce to a
729 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
730 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000731 if (TLI.MaskedValueIsZero(N1, SignBit) &&
732 TLI.MaskedValueIsZero(N0, SignBit))
Chris Lattner094c8fc2005-10-07 06:10:46 +0000733 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
Nate Begemancd6a6ed2006-02-17 07:26:20 +0000734 // fold (sdiv X, pow2) -> simple ops after legalize
735 if (N1C && N1C->getValue() && !TLI.isIntDivCheap() && AfterLegalize &&
Nate Begeman405e3ec2005-10-21 00:02:42 +0000736 (isPowerOf2_64(N1C->getSignExtended()) ||
737 isPowerOf2_64(-N1C->getSignExtended()))) {
738 // If dividing by powers of two is cheap, then don't perform the following
739 // fold.
740 if (TLI.isPow2DivCheap())
741 return SDOperand();
742 int64_t pow2 = N1C->getSignExtended();
743 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
Chris Lattner8f4880b2006-02-16 08:02:36 +0000744 unsigned lg2 = Log2_64(abs2);
745 // Splat the sign bit into the register
746 SDOperand SGN = DAG.getNode(ISD::SRA, VT, N0,
Nate Begeman405e3ec2005-10-21 00:02:42 +0000747 DAG.getConstant(MVT::getSizeInBits(VT)-1,
748 TLI.getShiftAmountTy()));
Nate Begeman405e3ec2005-10-21 00:02:42 +0000749 WorkList.push_back(SGN.Val);
Chris Lattner8f4880b2006-02-16 08:02:36 +0000750 // Add (N0 < 0) ? abs2 - 1 : 0;
751 SDOperand SRL = DAG.getNode(ISD::SRL, VT, SGN,
752 DAG.getConstant(MVT::getSizeInBits(VT)-lg2,
Nate Begeman405e3ec2005-10-21 00:02:42 +0000753 TLI.getShiftAmountTy()));
Chris Lattner8f4880b2006-02-16 08:02:36 +0000754 SDOperand ADD = DAG.getNode(ISD::ADD, VT, N0, SRL);
755 WorkList.push_back(SRL.Val);
756 WorkList.push_back(ADD.Val); // Divide by pow2
757 SDOperand SRA = DAG.getNode(ISD::SRA, VT, ADD,
758 DAG.getConstant(lg2, TLI.getShiftAmountTy()));
Nate Begeman405e3ec2005-10-21 00:02:42 +0000759 // If we're dividing by a positive value, we're done. Otherwise, we must
760 // negate the result.
761 if (pow2 > 0)
762 return SRA;
763 WorkList.push_back(SRA.Val);
764 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), SRA);
765 }
Nate Begeman69575232005-10-20 02:15:44 +0000766 // if integer divide is expensive and we satisfy the requirements, emit an
767 // alternate sequence.
Nate Begeman405e3ec2005-10-21 00:02:42 +0000768 if (N1C && (N1C->getSignExtended() < -1 || N1C->getSignExtended() > 1) &&
Chris Lattnere9936d12005-10-22 18:50:15 +0000769 !TLI.isIntDivCheap()) {
770 SDOperand Op = BuildSDIV(N);
771 if (Op.Val) return Op;
Nate Begeman69575232005-10-20 02:15:44 +0000772 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000773 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000774}
775
Nate Begeman83e75ec2005-09-06 04:43:02 +0000776SDOperand DAGCombiner::visitUDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000777 SDOperand N0 = N->getOperand(0);
778 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000779 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
780 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000781 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000782
783 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000784 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000785 return DAG.getNode(ISD::UDIV, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000786 // fold (udiv x, (1 << c)) -> x >>u c
Nate Begeman646d7e22005-09-02 21:18:40 +0000787 if (N1C && isPowerOf2_64(N1C->getValue()))
Nate Begemanfb5e4bd2006-02-05 07:20:23 +0000788 return DAG.getNode(ISD::SRL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000789 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000790 TLI.getShiftAmountTy()));
Nate Begemanfb5e4bd2006-02-05 07:20:23 +0000791 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
792 if (N1.getOpcode() == ISD::SHL) {
793 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
794 if (isPowerOf2_64(SHC->getValue())) {
795 MVT::ValueType ADDVT = N1.getOperand(1).getValueType();
Nate Begemanc031e332006-02-05 07:36:48 +0000796 SDOperand Add = DAG.getNode(ISD::ADD, ADDVT, N1.getOperand(1),
797 DAG.getConstant(Log2_64(SHC->getValue()),
798 ADDVT));
799 WorkList.push_back(Add.Val);
800 return DAG.getNode(ISD::SRL, VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +0000801 }
802 }
803 }
Nate Begeman69575232005-10-20 02:15:44 +0000804 // fold (udiv x, c) -> alternate
Chris Lattnere9936d12005-10-22 18:50:15 +0000805 if (N1C && N1C->getValue() && !TLI.isIntDivCheap()) {
806 SDOperand Op = BuildUDIV(N);
807 if (Op.Val) return Op;
808 }
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::visitSREM(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 (srem 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::SREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +0000822 // If we know the sign bits of both operands are zero, strength reduce to a
823 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
824 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000825 if (TLI.MaskedValueIsZero(N1, SignBit) &&
826 TLI.MaskedValueIsZero(N0, SignBit))
Nate Begemana148d982006-01-18 22:35:16 +0000827 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000828 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000829}
830
Nate Begeman83e75ec2005-09-06 04:43:02 +0000831SDOperand DAGCombiner::visitUREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000832 SDOperand N0 = N->getOperand(0);
833 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000834 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
835 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +0000836 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000837
838 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000839 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000840 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +0000841 // fold (urem x, pow2) -> (and x, pow2-1)
842 if (N1C && !N1C->isNullValue() && isPowerOf2_64(N1C->getValue()))
Nate Begemana148d982006-01-18 22:35:16 +0000843 return DAG.getNode(ISD::AND, VT, N0, DAG.getConstant(N1C->getValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +0000844 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
845 if (N1.getOpcode() == ISD::SHL) {
846 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
847 if (isPowerOf2_64(SHC->getValue())) {
Nate Begemanbab92392006-02-05 08:07:24 +0000848 SDOperand Add = DAG.getNode(ISD::ADD, VT, N1,DAG.getConstant(~0ULL,VT));
Nate Begemanc031e332006-02-05 07:36:48 +0000849 WorkList.push_back(Add.Val);
850 return DAG.getNode(ISD::AND, VT, N0, Add);
851 }
852 }
853 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000854 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000855}
856
Nate Begeman83e75ec2005-09-06 04:43:02 +0000857SDOperand DAGCombiner::visitMULHS(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000858 SDOperand N0 = N->getOperand(0);
859 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000860 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000861
862 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000863 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000864 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000865 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Nate Begeman646d7e22005-09-02 21:18:40 +0000866 if (N1C && N1C->getValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +0000867 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
868 DAG.getConstant(MVT::getSizeInBits(N0.getValueType())-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +0000869 TLI.getShiftAmountTy()));
870 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000871}
872
Nate Begeman83e75ec2005-09-06 04:43:02 +0000873SDOperand DAGCombiner::visitMULHU(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000874 SDOperand N0 = N->getOperand(0);
875 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000876 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000877
878 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000879 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000880 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000881 // fold (mulhu x, 1) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000882 if (N1C && N1C->getValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000883 return DAG.getConstant(0, N0.getValueType());
884 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000885}
886
Nate Begeman83e75ec2005-09-06 04:43:02 +0000887SDOperand DAGCombiner::visitAND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000888 SDOperand N0 = N->getOperand(0);
889 SDOperand N1 = N->getOperand(1);
Nate Begemande996292006-02-03 22:24:05 +0000890 SDOperand LL, LR, RL, RR, CC0, CC1, Old, New;
Nate Begeman646d7e22005-09-02 21:18:40 +0000891 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
892 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000893 MVT::ValueType VT = N1.getValueType();
Nate Begeman83e75ec2005-09-06 04:43:02 +0000894 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000895
896 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000897 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000898 return DAG.getNode(ISD::AND, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000899 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000900 if (N0C && !N1C)
901 return DAG.getNode(ISD::AND, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000902 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000903 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000904 return N0;
905 // if (and x, c) is known to be zero, return 0
Nate Begeman368e18d2006-02-16 21:11:51 +0000906 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000907 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000908 // reassociate and
909 SDOperand RAND = ReassociateOps(ISD::AND, N0, N1);
910 if (RAND.Val != 0)
911 return RAND;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000912 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
Nate Begeman5dc7e862005-11-02 18:42:59 +0000913 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +0000914 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Nate Begeman646d7e22005-09-02 21:18:40 +0000915 if ((ORI->getValue() & N1C->getValue()) == N1C->getValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000916 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +0000917 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
918 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
919 unsigned InBits = MVT::getSizeInBits(N0.getOperand(0).getValueType());
920 if (TLI.MaskedValueIsZero(N0.getOperand(0),
921 ~N1C->getValue() & ((1ULL << InBits)-1))) {
922 // We actually want to replace all uses of the any_extend with the
923 // zero_extend, to avoid duplicating things. This will later cause this
924 // AND to be folded.
925 CombineTo(N0.Val, DAG.getNode(ISD::ZERO_EXTEND, N0.getValueType(),
926 N0.getOperand(0)));
927 return SDOperand();
928 }
929 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000930 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
931 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
932 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
933 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
934
935 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
936 MVT::isInteger(LL.getValueType())) {
937 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
938 if (cast<ConstantSDNode>(LR)->getValue() == 0 && Op1 == ISD::SETEQ) {
939 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
940 WorkList.push_back(ORNode.Val);
941 return DAG.getSetCC(VT, ORNode, LR, Op1);
942 }
943 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
944 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
945 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
946 WorkList.push_back(ANDNode.Val);
947 return DAG.getSetCC(VT, ANDNode, LR, Op1);
948 }
949 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
950 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
951 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
952 WorkList.push_back(ORNode.Val);
953 return DAG.getSetCC(VT, ORNode, LR, Op1);
954 }
955 }
956 // canonicalize equivalent to ll == rl
957 if (LL == RR && LR == RL) {
958 Op1 = ISD::getSetCCSwappedOperands(Op1);
959 std::swap(RL, RR);
960 }
961 if (LL == RL && LR == RR) {
962 bool isInteger = MVT::isInteger(LL.getValueType());
963 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
964 if (Result != ISD::SETCC_INVALID)
965 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
966 }
967 }
968 // fold (and (zext x), (zext y)) -> (zext (and x, y))
969 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
970 N1.getOpcode() == ISD::ZERO_EXTEND &&
971 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
972 SDOperand ANDNode = DAG.getNode(ISD::AND, N0.getOperand(0).getValueType(),
973 N0.getOperand(0), N1.getOperand(0));
974 WorkList.push_back(ANDNode.Val);
975 return DAG.getNode(ISD::ZERO_EXTEND, VT, ANDNode);
976 }
Nate Begeman61af66e2006-01-28 01:06:30 +0000977 // fold (and (shl/srl/sra x), (shl/srl/sra y)) -> (shl/srl/sra (and x, y))
Nate Begeman452d7be2005-09-16 00:54:12 +0000978 if (((N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SHL) ||
Nate Begeman61af66e2006-01-28 01:06:30 +0000979 (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SRL) ||
980 (N0.getOpcode() == ISD::SRA && N1.getOpcode() == ISD::SRA)) &&
Nate Begeman452d7be2005-09-16 00:54:12 +0000981 N0.getOperand(1) == N1.getOperand(1)) {
982 SDOperand ANDNode = DAG.getNode(ISD::AND, N0.getOperand(0).getValueType(),
983 N0.getOperand(0), N1.getOperand(0));
984 WorkList.push_back(ANDNode.Val);
985 return DAG.getNode(N0.getOpcode(), VT, ANDNode, N0.getOperand(1));
986 }
Nate Begemande996292006-02-03 22:24:05 +0000987 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
988 // fold (and (sra)) -> (and (srl)) when possible.
Nate Begeman368e18d2006-02-16 21:11:51 +0000989 if (DemandedBitsAreZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT), Old,
990 New)) {
Nate Begemande996292006-02-03 22:24:05 +0000991 WorkList.push_back(N);
992 CombineTo(Old.Val, New);
993 return SDOperand();
994 }
Nate Begemanded49632005-10-13 03:11:28 +0000995 // fold (zext_inreg (extload x)) -> (zextload x)
Nate Begeman5054f162005-10-14 01:12:21 +0000996 if (N0.getOpcode() == ISD::EXTLOAD) {
Nate Begemanded49632005-10-13 03:11:28 +0000997 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +0000998 // If we zero all the possible extended bits, then we can turn this into
999 // a zextload if we are running before legalize or the operation is legal.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001000 if (TLI.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Chris Lattner67a44cd2005-10-13 18:16:34 +00001001 (!AfterLegalize || TLI.isOperationLegal(ISD::ZEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001002 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1003 N0.getOperand(1), N0.getOperand(2),
1004 EVT);
Nate Begemanded49632005-10-13 03:11:28 +00001005 WorkList.push_back(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001006 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Nate Begemanded49632005-10-13 03:11:28 +00001007 return SDOperand();
1008 }
1009 }
1010 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Chris Lattner40c62d52005-10-18 06:04:22 +00001011 if (N0.getOpcode() == ISD::SEXTLOAD && N0.hasOneUse()) {
Nate Begemanded49632005-10-13 03:11:28 +00001012 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001013 // If we zero all the possible extended bits, then we can turn this into
1014 // a zextload if we are running before legalize or the operation is legal.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001015 if (TLI.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Nate Begemanbfd65a02005-10-13 18:34:58 +00001016 (!AfterLegalize || TLI.isOperationLegal(ISD::ZEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001017 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1018 N0.getOperand(1), N0.getOperand(2),
1019 EVT);
Nate Begemanded49632005-10-13 03:11:28 +00001020 WorkList.push_back(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001021 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Nate Begemanded49632005-10-13 03:11:28 +00001022 return SDOperand();
1023 }
1024 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001025 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001026}
1027
Nate Begeman83e75ec2005-09-06 04:43:02 +00001028SDOperand DAGCombiner::visitOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001029 SDOperand N0 = N->getOperand(0);
1030 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001031 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001032 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1033 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001034 MVT::ValueType VT = N1.getValueType();
1035 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001036
1037 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001038 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001039 return DAG.getNode(ISD::OR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001040 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001041 if (N0C && !N1C)
1042 return DAG.getNode(ISD::OR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001043 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001044 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001045 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001046 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001047 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001048 return N1;
1049 // fold (or x, c) -> c iff (x & ~c) == 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001050 if (N1C &&
1051 TLI.MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits))))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001052 return N1;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001053 // reassociate or
1054 SDOperand ROR = ReassociateOps(ISD::OR, N0, N1);
1055 if (ROR.Val != 0)
1056 return ROR;
1057 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
1058 if (N1C && N0.getOpcode() == ISD::AND && N0.Val->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00001059 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00001060 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
1061 return DAG.getNode(ISD::AND, VT, DAG.getNode(ISD::OR, VT, N0.getOperand(0),
1062 N1),
1063 DAG.getConstant(N1C->getValue() | C1->getValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00001064 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001065 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
1066 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1067 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1068 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1069
1070 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1071 MVT::isInteger(LL.getValueType())) {
1072 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
1073 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
1074 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
1075 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
1076 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
1077 WorkList.push_back(ORNode.Val);
1078 return DAG.getSetCC(VT, ORNode, LR, Op1);
1079 }
1080 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
1081 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
1082 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
1083 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
1084 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
1085 WorkList.push_back(ANDNode.Val);
1086 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1087 }
1088 }
1089 // canonicalize equivalent to ll == rl
1090 if (LL == RR && LR == RL) {
1091 Op1 = ISD::getSetCCSwappedOperands(Op1);
1092 std::swap(RL, RR);
1093 }
1094 if (LL == RL && LR == RR) {
1095 bool isInteger = MVT::isInteger(LL.getValueType());
1096 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
1097 if (Result != ISD::SETCC_INVALID)
1098 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1099 }
1100 }
1101 // fold (or (zext x), (zext y)) -> (zext (or x, y))
1102 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
1103 N1.getOpcode() == ISD::ZERO_EXTEND &&
1104 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1105 SDOperand ORNode = DAG.getNode(ISD::OR, N0.getOperand(0).getValueType(),
1106 N0.getOperand(0), N1.getOperand(0));
1107 WorkList.push_back(ORNode.Val);
1108 return DAG.getNode(ISD::ZERO_EXTEND, VT, ORNode);
1109 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00001110 // fold (or (shl/srl/sra x), (shl/srl/sra y)) -> (shl/srl/sra (or x, y))
1111 if (((N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SHL) ||
1112 (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SRL) ||
1113 (N0.getOpcode() == ISD::SRA && N1.getOpcode() == ISD::SRA)) &&
1114 N0.getOperand(1) == N1.getOperand(1)) {
1115 SDOperand ORNode = DAG.getNode(ISD::OR, N0.getOperand(0).getValueType(),
1116 N0.getOperand(0), N1.getOperand(0));
1117 WorkList.push_back(ORNode.Val);
1118 return DAG.getNode(N0.getOpcode(), VT, ORNode, N0.getOperand(1));
1119 }
Nate Begeman35ef9132006-01-11 21:21:00 +00001120 // canonicalize shl to left side in a shl/srl pair, to match rotate
1121 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
1122 std::swap(N0, N1);
1123 // check for rotl, rotr
1124 if (N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SRL &&
1125 N0.getOperand(0) == N1.getOperand(0) &&
Chris Lattneraf551bc2006-01-12 18:57:33 +00001126 TLI.isOperationLegal(ISD::ROTL, VT) && TLI.isTypeLegal(VT)) {
Nate Begeman35ef9132006-01-11 21:21:00 +00001127 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
1128 if (N0.getOperand(1).getOpcode() == ISD::Constant &&
1129 N1.getOperand(1).getOpcode() == ISD::Constant) {
1130 uint64_t c1val = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1131 uint64_t c2val = cast<ConstantSDNode>(N1.getOperand(1))->getValue();
1132 if ((c1val + c2val) == OpSizeInBits)
1133 return DAG.getNode(ISD::ROTL, VT, N0.getOperand(0), N0.getOperand(1));
1134 }
1135 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
1136 if (N1.getOperand(1).getOpcode() == ISD::SUB &&
1137 N0.getOperand(1) == N1.getOperand(1).getOperand(1))
1138 if (ConstantSDNode *SUBC =
1139 dyn_cast<ConstantSDNode>(N1.getOperand(1).getOperand(0)))
1140 if (SUBC->getValue() == OpSizeInBits)
1141 return DAG.getNode(ISD::ROTL, VT, N0.getOperand(0), N0.getOperand(1));
1142 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
1143 if (N0.getOperand(1).getOpcode() == ISD::SUB &&
1144 N1.getOperand(1) == N0.getOperand(1).getOperand(1))
1145 if (ConstantSDNode *SUBC =
1146 dyn_cast<ConstantSDNode>(N0.getOperand(1).getOperand(0)))
1147 if (SUBC->getValue() == OpSizeInBits) {
Chris Lattneraf551bc2006-01-12 18:57:33 +00001148 if (TLI.isOperationLegal(ISD::ROTR, VT) && TLI.isTypeLegal(VT))
Nate Begeman35ef9132006-01-11 21:21:00 +00001149 return DAG.getNode(ISD::ROTR, VT, N0.getOperand(0),
1150 N1.getOperand(1));
1151 else
1152 return DAG.getNode(ISD::ROTL, VT, N0.getOperand(0),
1153 N0.getOperand(1));
1154 }
1155 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001156 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001157}
1158
Nate Begeman83e75ec2005-09-06 04:43:02 +00001159SDOperand DAGCombiner::visitXOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001160 SDOperand N0 = N->getOperand(0);
1161 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001162 SDOperand LHS, RHS, CC;
1163 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1164 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001165 MVT::ValueType VT = N0.getValueType();
1166
1167 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001168 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001169 return DAG.getNode(ISD::XOR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001170 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001171 if (N0C && !N1C)
1172 return DAG.getNode(ISD::XOR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001173 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001174 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001175 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001176 // reassociate xor
1177 SDOperand RXOR = ReassociateOps(ISD::XOR, N0, N1);
1178 if (RXOR.Val != 0)
1179 return RXOR;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001180 // fold !(x cc y) -> (x !cc y)
Nate Begeman646d7e22005-09-02 21:18:40 +00001181 if (N1C && N1C->getValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
1182 bool isInt = MVT::isInteger(LHS.getValueType());
1183 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
1184 isInt);
1185 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001186 return DAG.getSetCC(VT, LHS, RHS, NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00001187 if (N0.getOpcode() == ISD::SELECT_CC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001188 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00001189 assert(0 && "Unhandled SetCC Equivalent!");
1190 abort();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001191 }
Nate Begeman99801192005-09-07 23:25:52 +00001192 // fold !(x or y) -> (!x and !y) iff x or y are setcc
1193 if (N1C && N1C->getValue() == 1 &&
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 (isOneUseSetCC(RHS) || isOneUseSetCC(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 Begeman99801192005-09-07 23:25:52 +00001204 // fold !(x or y) -> (!x and !y) iff x or y are constants
1205 if (N1C && N1C->isAllOnesValue() &&
1206 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001207 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00001208 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
1209 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001210 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1211 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Nate Begeman99801192005-09-07 23:25:52 +00001212 WorkList.push_back(LHS.Val); WorkList.push_back(RHS.Val);
1213 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001214 }
1215 }
Nate Begeman223df222005-09-08 20:18:10 +00001216 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
1217 if (N1C && N0.getOpcode() == ISD::XOR) {
1218 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
1219 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
1220 if (N00C)
1221 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
1222 DAG.getConstant(N1C->getValue()^N00C->getValue(), VT));
1223 if (N01C)
1224 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
1225 DAG.getConstant(N1C->getValue()^N01C->getValue(), VT));
1226 }
1227 // fold (xor x, x) -> 0
1228 if (N0 == N1)
1229 return DAG.getConstant(0, VT);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001230 // fold (xor (zext x), (zext y)) -> (zext (xor x, y))
1231 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
1232 N1.getOpcode() == ISD::ZERO_EXTEND &&
1233 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1234 SDOperand XORNode = DAG.getNode(ISD::XOR, N0.getOperand(0).getValueType(),
1235 N0.getOperand(0), N1.getOperand(0));
1236 WorkList.push_back(XORNode.Val);
1237 return DAG.getNode(ISD::ZERO_EXTEND, VT, XORNode);
1238 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00001239 // fold (xor (shl/srl/sra x), (shl/srl/sra y)) -> (shl/srl/sra (xor x, y))
1240 if (((N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SHL) ||
1241 (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SRL) ||
1242 (N0.getOpcode() == ISD::SRA && N1.getOpcode() == ISD::SRA)) &&
1243 N0.getOperand(1) == N1.getOperand(1)) {
1244 SDOperand XORNode = DAG.getNode(ISD::XOR, N0.getOperand(0).getValueType(),
1245 N0.getOperand(0), N1.getOperand(0));
1246 WorkList.push_back(XORNode.Val);
1247 return DAG.getNode(N0.getOpcode(), VT, XORNode, N0.getOperand(1));
1248 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001249 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001250}
1251
Nate Begeman83e75ec2005-09-06 04:43:02 +00001252SDOperand DAGCombiner::visitSHL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001253 SDOperand N0 = N->getOperand(0);
1254 SDOperand N1 = N->getOperand(1);
Nate Begemande996292006-02-03 22:24:05 +00001255 SDOperand Old = SDOperand();
1256 SDOperand New = SDOperand();
Nate Begeman646d7e22005-09-02 21:18:40 +00001257 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1258 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001259 MVT::ValueType VT = N0.getValueType();
1260 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1261
1262 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001263 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001264 return DAG.getNode(ISD::SHL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001265 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001266 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001267 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001268 // fold (shl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001269 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001270 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001271 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001272 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001273 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001274 // if (shl x, c) is known to be zero, return 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001275 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001276 return DAG.getConstant(0, VT);
Nate Begeman368e18d2006-02-16 21:11:51 +00001277 if (N1C && DemandedBitsAreZero(SDOperand(N,0), ~0ULL >> (64-OpSizeInBits),
1278 Old, New)) {
Nate Begemande996292006-02-03 22:24:05 +00001279 WorkList.push_back(N);
1280 CombineTo(Old.Val, New);
1281 return SDOperand();
1282 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00001283 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001284 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001285 N0.getOperand(1).getOpcode() == ISD::Constant) {
1286 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001287 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001288 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001289 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001290 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001291 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001292 }
1293 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
1294 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001295 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001296 N0.getOperand(1).getOpcode() == ISD::Constant) {
1297 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001298 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001299 SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
1300 DAG.getConstant(~0ULL << c1, VT));
1301 if (c2 > c1)
1302 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001303 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001304 else
Nate Begeman83e75ec2005-09-06 04:43:02 +00001305 return DAG.getNode(ISD::SRL, VT, Mask,
1306 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001307 }
1308 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001309 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +00001310 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001311 DAG.getConstant(~0ULL << N1C->getValue(), VT));
1312 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001313}
1314
Nate Begeman83e75ec2005-09-06 04:43:02 +00001315SDOperand DAGCombiner::visitSRA(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001316 SDOperand N0 = N->getOperand(0);
1317 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001318 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1319 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001320 MVT::ValueType VT = N0.getValueType();
1321 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1322
1323 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001324 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001325 return DAG.getNode(ISD::SRA, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001326 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001327 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001328 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001329 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001330 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001331 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001332 // fold (sra x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001333 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001334 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001335 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001336 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001337 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001338 // If the sign bit is known to be zero, switch this to a SRL.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001339 if (TLI.MaskedValueIsZero(N0, (1ULL << (OpSizeInBits-1))))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001340 return DAG.getNode(ISD::SRL, VT, N0, N1);
1341 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001342}
1343
Nate Begeman83e75ec2005-09-06 04:43:02 +00001344SDOperand DAGCombiner::visitSRL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001345 SDOperand N0 = N->getOperand(0);
1346 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001347 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1348 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001349 MVT::ValueType VT = N0.getValueType();
1350 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1351
1352 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001353 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001354 return DAG.getNode(ISD::SRL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001355 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001356 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001357 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001358 // fold (srl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001359 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001360 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001361 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001362 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001363 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001364 // if (srl x, c) is known to be zero, return 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001365 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001366 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001367 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001368 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001369 N0.getOperand(1).getOpcode() == ISD::Constant) {
1370 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001371 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001372 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001373 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001374 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001375 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001376 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001377 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001378}
1379
Nate Begeman83e75ec2005-09-06 04:43:02 +00001380SDOperand DAGCombiner::visitCTLZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001381 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001382 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00001383 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001384
1385 // fold (ctlz c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001386 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001387 return DAG.getNode(ISD::CTLZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001388 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001389}
1390
Nate Begeman83e75ec2005-09-06 04:43:02 +00001391SDOperand DAGCombiner::visitCTTZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001392 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001393 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00001394 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001395
1396 // fold (cttz c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001397 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001398 return DAG.getNode(ISD::CTTZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001399 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001400}
1401
Nate Begeman83e75ec2005-09-06 04:43:02 +00001402SDOperand DAGCombiner::visitCTPOP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001403 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001404 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00001405 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001406
1407 // fold (ctpop c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001408 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001409 return DAG.getNode(ISD::CTPOP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001410 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001411}
1412
Nate Begeman452d7be2005-09-16 00:54:12 +00001413SDOperand DAGCombiner::visitSELECT(SDNode *N) {
1414 SDOperand N0 = N->getOperand(0);
1415 SDOperand N1 = N->getOperand(1);
1416 SDOperand N2 = N->getOperand(2);
1417 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1418 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1419 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1420 MVT::ValueType VT = N->getValueType(0);
Nate Begeman44728a72005-09-19 22:34:01 +00001421
Nate Begeman452d7be2005-09-16 00:54:12 +00001422 // fold select C, X, X -> X
1423 if (N1 == N2)
1424 return N1;
1425 // fold select true, X, Y -> X
1426 if (N0C && !N0C->isNullValue())
1427 return N1;
1428 // fold select false, X, Y -> Y
1429 if (N0C && N0C->isNullValue())
1430 return N2;
1431 // fold select C, 1, X -> C | X
Nate Begeman44728a72005-09-19 22:34:01 +00001432 if (MVT::i1 == VT && N1C && N1C->getValue() == 1)
Nate Begeman452d7be2005-09-16 00:54:12 +00001433 return DAG.getNode(ISD::OR, VT, N0, N2);
1434 // fold select C, 0, X -> ~C & X
1435 // FIXME: this should check for C type == X type, not i1?
1436 if (MVT::i1 == VT && N1C && N1C->isNullValue()) {
1437 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
1438 WorkList.push_back(XORNode.Val);
1439 return DAG.getNode(ISD::AND, VT, XORNode, N2);
1440 }
1441 // fold select C, X, 1 -> ~C | X
Nate Begeman44728a72005-09-19 22:34:01 +00001442 if (MVT::i1 == VT && N2C && N2C->getValue() == 1) {
Nate Begeman452d7be2005-09-16 00:54:12 +00001443 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
1444 WorkList.push_back(XORNode.Val);
1445 return DAG.getNode(ISD::OR, VT, XORNode, N1);
1446 }
1447 // fold select C, X, 0 -> C & X
1448 // FIXME: this should check for C type == X type, not i1?
1449 if (MVT::i1 == VT && N2C && N2C->isNullValue())
1450 return DAG.getNode(ISD::AND, VT, N0, N1);
1451 // fold X ? X : Y --> X ? 1 : Y --> X | Y
1452 if (MVT::i1 == VT && N0 == N1)
1453 return DAG.getNode(ISD::OR, VT, N0, N2);
1454 // fold X ? Y : X --> X ? Y : 0 --> X & Y
1455 if (MVT::i1 == VT && N0 == N2)
1456 return DAG.getNode(ISD::AND, VT, N0, N1);
Chris Lattner40c62d52005-10-18 06:04:22 +00001457 // If we can fold this based on the true/false value, do so.
1458 if (SimplifySelectOps(N, N1, N2))
1459 return SDOperand();
Nate Begeman44728a72005-09-19 22:34:01 +00001460 // fold selects based on a setcc into other things, such as min/max/abs
1461 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman750ac1b2006-02-01 07:19:44 +00001462 // FIXME:
1463 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
1464 // having to say they don't support SELECT_CC on every type the DAG knows
1465 // about, since there is no way to mark an opcode illegal at all value types
1466 if (TLI.isOperationLegal(ISD::SELECT_CC, MVT::Other))
1467 return DAG.getNode(ISD::SELECT_CC, VT, N0.getOperand(0), N0.getOperand(1),
1468 N1, N2, N0.getOperand(2));
1469 else
1470 return SimplifySelect(N0, N1, N2);
Nate Begeman452d7be2005-09-16 00:54:12 +00001471 return SDOperand();
1472}
1473
1474SDOperand DAGCombiner::visitSELECT_CC(SDNode *N) {
Nate Begeman44728a72005-09-19 22:34:01 +00001475 SDOperand N0 = N->getOperand(0);
1476 SDOperand N1 = N->getOperand(1);
1477 SDOperand N2 = N->getOperand(2);
1478 SDOperand N3 = N->getOperand(3);
1479 SDOperand N4 = N->getOperand(4);
1480 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1481 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1482 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1483 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
1484
1485 // Determine if the condition we're dealing with is constant
Nate Begemane17daeb2005-10-05 21:43:42 +00001486 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
Chris Lattner91559022005-10-05 04:45:43 +00001487 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
1488
Nate Begeman44728a72005-09-19 22:34:01 +00001489 // fold select_cc lhs, rhs, x, x, cc -> x
1490 if (N2 == N3)
1491 return N2;
Chris Lattner40c62d52005-10-18 06:04:22 +00001492
1493 // If we can fold this based on the true/false value, do so.
1494 if (SimplifySelectOps(N, N2, N3))
1495 return SDOperand();
1496
Nate Begeman44728a72005-09-19 22:34:01 +00001497 // fold select_cc into other things, such as min/max/abs
1498 return SimplifySelectCC(N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00001499}
1500
1501SDOperand DAGCombiner::visitSETCC(SDNode *N) {
1502 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
1503 cast<CondCodeSDNode>(N->getOperand(2))->get());
1504}
1505
Nate Begeman83e75ec2005-09-06 04:43:02 +00001506SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001507 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001508 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001509 MVT::ValueType VT = N->getValueType(0);
1510
Nate Begeman1d4d4142005-09-01 00:19:25 +00001511 // fold (sext c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001512 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001513 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001514 // fold (sext (sext x)) -> (sext x)
1515 if (N0.getOpcode() == ISD::SIGN_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001516 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
Chris Lattnerb14ab8a2005-12-07 07:11:03 +00001517 // fold (sext (truncate x)) -> (sextinreg x) iff x size == sext size.
Chris Lattnercc2210b2005-12-07 18:02:05 +00001518 if (N0.getOpcode() == ISD::TRUNCATE && N0.getOperand(0).getValueType() == VT&&
1519 (!AfterLegalize ||
1520 TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, N0.getValueType())))
Chris Lattnerb14ab8a2005-12-07 07:11:03 +00001521 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
1522 DAG.getValueType(N0.getValueType()));
Evan Cheng110dec22005-12-14 02:19:23 +00001523 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Chris Lattnerd0f6d182005-12-15 19:02:38 +00001524 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() &&
1525 (!AfterLegalize||TLI.isOperationLegal(ISD::SEXTLOAD, N0.getValueType()))){
Nate Begeman3df4d522005-10-12 20:40:40 +00001526 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1527 N0.getOperand(1), N0.getOperand(2),
1528 N0.getValueType());
Chris Lattnerd4771842005-12-14 19:25:30 +00001529 CombineTo(N, ExtLoad);
Chris Lattnerf9884052005-10-13 21:52:31 +00001530 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1531 ExtLoad.getValue(1));
Nate Begeman765784a2005-10-12 23:18:53 +00001532 return SDOperand();
Nate Begeman3df4d522005-10-12 20:40:40 +00001533 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001534
1535 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
1536 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
1537 if ((N0.getOpcode() == ISD::SEXTLOAD || N0.getOpcode() == ISD::EXTLOAD) &&
1538 N0.hasOneUse()) {
1539 SDOperand ExtLoad = DAG.getNode(ISD::SEXTLOAD, VT, N0.getOperand(0),
1540 N0.getOperand(1), N0.getOperand(2),
1541 N0.getOperand(3));
Chris Lattnerd4771842005-12-14 19:25:30 +00001542 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001543 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1544 ExtLoad.getValue(1));
1545 return SDOperand();
1546 }
1547
Nate Begeman83e75ec2005-09-06 04:43:02 +00001548 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001549}
1550
Nate Begeman83e75ec2005-09-06 04:43:02 +00001551SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001552 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001553 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001554 MVT::ValueType VT = N->getValueType(0);
1555
Nate Begeman1d4d4142005-09-01 00:19:25 +00001556 // fold (zext c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001557 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001558 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001559 // fold (zext (zext x)) -> (zext x)
1560 if (N0.getOpcode() == ISD::ZERO_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001561 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
Evan Cheng110dec22005-12-14 02:19:23 +00001562 // fold (zext (truncate x)) -> (zextinreg x) iff x size == zext size.
1563 if (N0.getOpcode() == ISD::TRUNCATE && N0.getOperand(0).getValueType() == VT&&
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001564 (!AfterLegalize || TLI.isOperationLegal(ISD::AND, N0.getValueType())))
Chris Lattner00cb95c2005-12-14 07:58:38 +00001565 return DAG.getZeroExtendInReg(N0.getOperand(0), N0.getValueType());
Evan Cheng110dec22005-12-14 02:19:23 +00001566 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Chris Lattnerd0f6d182005-12-15 19:02:38 +00001567 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() &&
1568 (!AfterLegalize||TLI.isOperationLegal(ISD::ZEXTLOAD, N0.getValueType()))){
Evan Cheng110dec22005-12-14 02:19:23 +00001569 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1570 N0.getOperand(1), N0.getOperand(2),
1571 N0.getValueType());
Chris Lattnerd4771842005-12-14 19:25:30 +00001572 CombineTo(N, ExtLoad);
Evan Cheng110dec22005-12-14 02:19:23 +00001573 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1574 ExtLoad.getValue(1));
1575 return SDOperand();
1576 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001577
1578 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
1579 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
1580 if ((N0.getOpcode() == ISD::ZEXTLOAD || N0.getOpcode() == ISD::EXTLOAD) &&
1581 N0.hasOneUse()) {
1582 SDOperand ExtLoad = DAG.getNode(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1583 N0.getOperand(1), N0.getOperand(2),
1584 N0.getOperand(3));
Chris Lattnerd4771842005-12-14 19:25:30 +00001585 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001586 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1587 ExtLoad.getValue(1));
1588 return SDOperand();
1589 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001590 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001591}
1592
Nate Begeman83e75ec2005-09-06 04:43:02 +00001593SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001594 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001595 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001596 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001597 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001598 MVT::ValueType EVT = cast<VTSDNode>(N1)->getVT();
Nate Begeman07ed4172005-10-10 21:26:48 +00001599 unsigned EVTBits = MVT::getSizeInBits(EVT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001600
Nate Begeman1d4d4142005-09-01 00:19:25 +00001601 // fold (sext_in_reg c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001602 if (N0C) {
1603 SDOperand Truncate = DAG.getConstant(N0C->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001604 return DAG.getNode(ISD::SIGN_EXTEND, VT, Truncate);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001605 }
Nate Begeman646d7e22005-09-02 21:18:40 +00001606 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt1
Nate Begeman1d4d4142005-09-01 00:19:25 +00001607 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Nate Begeman216def82005-10-14 01:29:07 +00001608 cast<VTSDNode>(N0.getOperand(1))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001609 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001610 }
Nate Begeman646d7e22005-09-02 21:18:40 +00001611 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
1612 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1613 EVT < cast<VTSDNode>(N0.getOperand(1))->getVT()) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001614 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001615 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00001616 // fold (sext_in_reg (assert_sext x)) -> (assert_sext x)
1617 if (N0.getOpcode() == ISD::AssertSext &&
1618 cast<VTSDNode>(N0.getOperand(1))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001619 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001620 }
1621 // fold (sext_in_reg (sextload x)) -> (sextload x)
1622 if (N0.getOpcode() == ISD::SEXTLOAD &&
1623 cast<VTSDNode>(N0.getOperand(3))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001624 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001625 }
Nate Begeman4ebd8052005-09-01 23:24:04 +00001626 // fold (sext_in_reg (setcc x)) -> setcc x iff (setcc x) == 0 or -1
Nate Begeman1d4d4142005-09-01 00:19:25 +00001627 if (N0.getOpcode() == ISD::SETCC &&
1628 TLI.getSetCCResultContents() ==
1629 TargetLowering::ZeroOrNegativeOneSetCCResult)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001630 return N0;
Nate Begeman07ed4172005-10-10 21:26:48 +00001631 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is zero
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001632 if (TLI.MaskedValueIsZero(N0, 1ULL << (EVTBits-1)))
Nate Begemande996292006-02-03 22:24:05 +00001633 return DAG.getZeroExtendInReg(N0, EVT);
Nate Begeman07ed4172005-10-10 21:26:48 +00001634 // fold (sext_in_reg (srl x)) -> sra x
1635 if (N0.getOpcode() == ISD::SRL &&
1636 N0.getOperand(1).getOpcode() == ISD::Constant &&
1637 cast<ConstantSDNode>(N0.getOperand(1))->getValue() == EVTBits) {
1638 return DAG.getNode(ISD::SRA, N0.getValueType(), N0.getOperand(0),
1639 N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001640 }
Nate Begemanded49632005-10-13 03:11:28 +00001641 // fold (sext_inreg (extload x)) -> (sextload x)
1642 if (N0.getOpcode() == ISD::EXTLOAD &&
1643 EVT == cast<VTSDNode>(N0.getOperand(3))->getVT() &&
Nate Begemanbfd65a02005-10-13 18:34:58 +00001644 (!AfterLegalize || TLI.isOperationLegal(ISD::SEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001645 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1646 N0.getOperand(1), N0.getOperand(2),
1647 EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00001648 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00001649 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Nate Begemanded49632005-10-13 03:11:28 +00001650 return SDOperand();
1651 }
1652 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Chris Lattner40c62d52005-10-18 06:04:22 +00001653 if (N0.getOpcode() == ISD::ZEXTLOAD && N0.hasOneUse() &&
Nate Begemanded49632005-10-13 03:11:28 +00001654 EVT == cast<VTSDNode>(N0.getOperand(3))->getVT() &&
Nate Begemanbfd65a02005-10-13 18:34:58 +00001655 (!AfterLegalize || TLI.isOperationLegal(ISD::SEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001656 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1657 N0.getOperand(1), N0.getOperand(2),
1658 EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00001659 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00001660 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Nate Begemanded49632005-10-13 03:11:28 +00001661 return SDOperand();
1662 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001663 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001664}
1665
Nate Begeman83e75ec2005-09-06 04:43:02 +00001666SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001667 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001668 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001669 MVT::ValueType VT = N->getValueType(0);
1670
1671 // noop truncate
1672 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001673 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001674 // fold (truncate c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001675 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001676 return DAG.getNode(ISD::TRUNCATE, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001677 // fold (truncate (truncate x)) -> (truncate x)
1678 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001679 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001680 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
1681 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND){
1682 if (N0.getValueType() < VT)
1683 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00001684 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001685 else if (N0.getValueType() > VT)
1686 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00001687 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001688 else
1689 // if the source and dest are the same type, we can drop both the extend
1690 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00001691 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001692 }
Nate Begeman3df4d522005-10-12 20:40:40 +00001693 // fold (truncate (load x)) -> (smaller load x)
Chris Lattner40c62d52005-10-18 06:04:22 +00001694 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse()) {
Nate Begeman3df4d522005-10-12 20:40:40 +00001695 assert(MVT::getSizeInBits(N0.getValueType()) > MVT::getSizeInBits(VT) &&
1696 "Cannot truncate to larger type!");
1697 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
Nate Begeman765784a2005-10-12 23:18:53 +00001698 // For big endian targets, we need to add an offset to the pointer to load
1699 // the correct bytes. For little endian systems, we merely need to read
1700 // fewer bytes from the same pointer.
Nate Begeman3df4d522005-10-12 20:40:40 +00001701 uint64_t PtrOff =
1702 (MVT::getSizeInBits(N0.getValueType()) - MVT::getSizeInBits(VT)) / 8;
Nate Begeman765784a2005-10-12 23:18:53 +00001703 SDOperand NewPtr = TLI.isLittleEndian() ? N0.getOperand(1) :
1704 DAG.getNode(ISD::ADD, PtrType, N0.getOperand(1),
1705 DAG.getConstant(PtrOff, PtrType));
1706 WorkList.push_back(NewPtr.Val);
Nate Begeman3df4d522005-10-12 20:40:40 +00001707 SDOperand Load = DAG.getLoad(VT, N0.getOperand(0), NewPtr,N0.getOperand(2));
Nate Begeman765784a2005-10-12 23:18:53 +00001708 WorkList.push_back(N);
Chris Lattner24edbb72005-10-13 22:10:05 +00001709 CombineTo(N0.Val, Load, Load.getValue(1));
Nate Begeman765784a2005-10-12 23:18:53 +00001710 return SDOperand();
Nate Begeman3df4d522005-10-12 20:40:40 +00001711 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001712 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001713}
1714
Chris Lattner94683772005-12-23 05:30:37 +00001715SDOperand DAGCombiner::visitBIT_CONVERT(SDNode *N) {
1716 SDOperand N0 = N->getOperand(0);
1717 MVT::ValueType VT = N->getValueType(0);
1718
1719 // If the input is a constant, let getNode() fold it.
1720 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
1721 SDOperand Res = DAG.getNode(ISD::BIT_CONVERT, VT, N0);
1722 if (Res.Val != N) return Res;
1723 }
1724
Chris Lattnerc8547d82005-12-23 05:37:50 +00001725 if (N0.getOpcode() == ISD::BIT_CONVERT) // conv(conv(x,t1),t2) -> conv(x,t2)
1726 return DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
1727
Chris Lattner57104102005-12-23 05:44:41 +00001728 // fold (conv (load x)) -> (load (conv*)x)
Chris Lattnerbf40c4b2006-01-15 18:58:59 +00001729 // FIXME: These xforms need to know that the resultant load doesn't need a
1730 // higher alignment than the original!
1731 if (0 && N0.getOpcode() == ISD::LOAD && N0.hasOneUse()) {
Chris Lattner57104102005-12-23 05:44:41 +00001732 SDOperand Load = DAG.getLoad(VT, N0.getOperand(0), N0.getOperand(1),
1733 N0.getOperand(2));
1734 WorkList.push_back(N);
1735 CombineTo(N0.Val, DAG.getNode(ISD::BIT_CONVERT, N0.getValueType(), Load),
1736 Load.getValue(1));
1737 return Load;
1738 }
1739
Chris Lattner94683772005-12-23 05:30:37 +00001740 return SDOperand();
1741}
1742
Chris Lattner01b3d732005-09-28 22:28:18 +00001743SDOperand DAGCombiner::visitFADD(SDNode *N) {
1744 SDOperand N0 = N->getOperand(0);
1745 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00001746 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1747 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001748 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00001749
1750 // fold (fadd c1, c2) -> c1+c2
1751 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001752 return DAG.getNode(ISD::FADD, VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00001753 // canonicalize constant to RHS
1754 if (N0CFP && !N1CFP)
1755 return DAG.getNode(ISD::FADD, VT, N1, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00001756 // fold (A + (-B)) -> A-B
1757 if (N1.getOpcode() == ISD::FNEG)
1758 return DAG.getNode(ISD::FSUB, VT, N0, N1.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00001759 // fold ((-A) + B) -> B-A
1760 if (N0.getOpcode() == ISD::FNEG)
1761 return DAG.getNode(ISD::FSUB, VT, N1, N0.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00001762 return SDOperand();
1763}
1764
1765SDOperand DAGCombiner::visitFSUB(SDNode *N) {
1766 SDOperand N0 = N->getOperand(0);
1767 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00001768 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1769 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001770 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00001771
1772 // fold (fsub c1, c2) -> c1-c2
1773 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001774 return DAG.getNode(ISD::FSUB, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001775 // fold (A-(-B)) -> A+B
1776 if (N1.getOpcode() == ISD::FNEG)
Nate Begemana148d982006-01-18 22:35:16 +00001777 return DAG.getNode(ISD::FADD, VT, N0, N1.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00001778 return SDOperand();
1779}
1780
1781SDOperand DAGCombiner::visitFMUL(SDNode *N) {
1782 SDOperand N0 = N->getOperand(0);
1783 SDOperand N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00001784 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1785 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001786 MVT::ValueType VT = N->getValueType(0);
1787
Nate Begeman11af4ea2005-10-17 20:40:11 +00001788 // fold (fmul c1, c2) -> c1*c2
1789 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001790 return DAG.getNode(ISD::FMUL, VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00001791 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001792 if (N0CFP && !N1CFP)
1793 return DAG.getNode(ISD::FMUL, VT, N1, N0);
Nate Begeman11af4ea2005-10-17 20:40:11 +00001794 // fold (fmul X, 2.0) -> (fadd X, X)
1795 if (N1CFP && N1CFP->isExactlyValue(+2.0))
1796 return DAG.getNode(ISD::FADD, VT, N0, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00001797 return SDOperand();
1798}
1799
1800SDOperand DAGCombiner::visitFDIV(SDNode *N) {
1801 SDOperand N0 = N->getOperand(0);
1802 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00001803 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1804 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001805 MVT::ValueType VT = N->getValueType(0);
1806
Nate Begemana148d982006-01-18 22:35:16 +00001807 // fold (fdiv c1, c2) -> c1/c2
1808 if (N0CFP && N1CFP)
1809 return DAG.getNode(ISD::FDIV, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001810 return SDOperand();
1811}
1812
1813SDOperand DAGCombiner::visitFREM(SDNode *N) {
1814 SDOperand N0 = N->getOperand(0);
1815 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00001816 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1817 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001818 MVT::ValueType VT = N->getValueType(0);
1819
Nate Begemana148d982006-01-18 22:35:16 +00001820 // fold (frem c1, c2) -> fmod(c1,c2)
1821 if (N0CFP && N1CFP)
1822 return DAG.getNode(ISD::FREM, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001823 return SDOperand();
1824}
1825
1826
Nate Begeman83e75ec2005-09-06 04:43:02 +00001827SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001828 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001829 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00001830 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001831
1832 // fold (sint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001833 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001834 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001835 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001836}
1837
Nate Begeman83e75ec2005-09-06 04:43:02 +00001838SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001839 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001840 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00001841 MVT::ValueType VT = N->getValueType(0);
1842
Nate Begeman1d4d4142005-09-01 00:19:25 +00001843 // fold (uint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001844 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001845 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001846 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001847}
1848
Nate Begeman83e75ec2005-09-06 04:43:02 +00001849SDOperand DAGCombiner::visitFP_TO_SINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00001850 SDOperand N0 = N->getOperand(0);
1851 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1852 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001853
1854 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001855 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001856 return DAG.getNode(ISD::FP_TO_SINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001857 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001858}
1859
Nate Begeman83e75ec2005-09-06 04:43:02 +00001860SDOperand DAGCombiner::visitFP_TO_UINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00001861 SDOperand N0 = N->getOperand(0);
1862 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1863 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001864
1865 // fold (fp_to_uint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001866 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001867 return DAG.getNode(ISD::FP_TO_UINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001868 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001869}
1870
Nate Begeman83e75ec2005-09-06 04:43:02 +00001871SDOperand DAGCombiner::visitFP_ROUND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00001872 SDOperand N0 = N->getOperand(0);
1873 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1874 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001875
1876 // fold (fp_round c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001877 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001878 return DAG.getNode(ISD::FP_ROUND, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001879 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001880}
1881
Nate Begeman83e75ec2005-09-06 04:43:02 +00001882SDOperand DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001883 SDOperand N0 = N->getOperand(0);
1884 MVT::ValueType VT = N->getValueType(0);
1885 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00001886 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001887
Nate Begeman1d4d4142005-09-01 00:19:25 +00001888 // fold (fp_round_inreg c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001889 if (N0CFP) {
1890 SDOperand Round = DAG.getConstantFP(N0CFP->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001891 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001892 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001893 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001894}
1895
Nate Begeman83e75ec2005-09-06 04:43:02 +00001896SDOperand DAGCombiner::visitFP_EXTEND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00001897 SDOperand N0 = N->getOperand(0);
1898 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1899 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001900
1901 // fold (fp_extend c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001902 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001903 return DAG.getNode(ISD::FP_EXTEND, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001904 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001905}
1906
Nate Begeman83e75ec2005-09-06 04:43:02 +00001907SDOperand DAGCombiner::visitFNEG(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00001908 SDOperand N0 = N->getOperand(0);
1909 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1910 MVT::ValueType VT = N->getValueType(0);
1911
1912 // fold (fneg c1) -> -c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001913 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001914 return DAG.getNode(ISD::FNEG, VT, N0);
1915 // fold (fneg (sub x, y)) -> (sub y, x)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001916 if (N->getOperand(0).getOpcode() == ISD::SUB)
Nate Begemana148d982006-01-18 22:35:16 +00001917 return DAG.getNode(ISD::SUB, VT, N->getOperand(1), N->getOperand(0));
1918 // fold (fneg (fneg x)) -> x
Nate Begeman1d4d4142005-09-01 00:19:25 +00001919 if (N->getOperand(0).getOpcode() == ISD::FNEG)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001920 return N->getOperand(0).getOperand(0);
1921 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001922}
1923
Nate Begeman83e75ec2005-09-06 04:43:02 +00001924SDOperand DAGCombiner::visitFABS(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00001925 SDOperand N0 = N->getOperand(0);
1926 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1927 MVT::ValueType VT = N->getValueType(0);
1928
Nate Begeman1d4d4142005-09-01 00:19:25 +00001929 // fold (fabs c1) -> fabs(c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001930 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001931 return DAG.getNode(ISD::FABS, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001932 // fold (fabs (fabs x)) -> (fabs x)
1933 if (N->getOperand(0).getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001934 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001935 // fold (fabs (fneg x)) -> (fabs x)
1936 if (N->getOperand(0).getOpcode() == ISD::FNEG)
Nate Begemana148d982006-01-18 22:35:16 +00001937 return DAG.getNode(ISD::FABS, VT, N->getOperand(0).getOperand(0));
Nate Begeman83e75ec2005-09-06 04:43:02 +00001938 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001939}
1940
Nate Begeman44728a72005-09-19 22:34:01 +00001941SDOperand DAGCombiner::visitBRCOND(SDNode *N) {
1942 SDOperand Chain = N->getOperand(0);
1943 SDOperand N1 = N->getOperand(1);
1944 SDOperand N2 = N->getOperand(2);
1945 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1946
1947 // never taken branch, fold to chain
1948 if (N1C && N1C->isNullValue())
1949 return Chain;
1950 // unconditional branch
Nate Begemane17daeb2005-10-05 21:43:42 +00001951 if (N1C && N1C->getValue() == 1)
Nate Begeman44728a72005-09-19 22:34:01 +00001952 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
Nate Begeman750ac1b2006-02-01 07:19:44 +00001953 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
1954 // on the target.
1955 if (N1.getOpcode() == ISD::SETCC &&
1956 TLI.isOperationLegal(ISD::BR_CC, MVT::Other)) {
1957 return DAG.getNode(ISD::BR_CC, MVT::Other, Chain, N1.getOperand(2),
1958 N1.getOperand(0), N1.getOperand(1), N2);
1959 }
Nate Begeman44728a72005-09-19 22:34:01 +00001960 return SDOperand();
1961}
1962
1963SDOperand DAGCombiner::visitBRCONDTWOWAY(SDNode *N) {
1964 SDOperand Chain = N->getOperand(0);
1965 SDOperand N1 = N->getOperand(1);
1966 SDOperand N2 = N->getOperand(2);
1967 SDOperand N3 = N->getOperand(3);
1968 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1969
1970 // unconditional branch to true mbb
1971 if (N1C && N1C->getValue() == 1)
1972 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
1973 // unconditional branch to false mbb
1974 if (N1C && N1C->isNullValue())
1975 return DAG.getNode(ISD::BR, MVT::Other, Chain, N3);
Nate Begeman750ac1b2006-02-01 07:19:44 +00001976 // fold a brcondtwoway with a setcc condition into a BRTWOWAY_CC node if
1977 // BRTWOWAY_CC is legal on the target.
1978 if (N1.getOpcode() == ISD::SETCC &&
1979 TLI.isOperationLegal(ISD::BRTWOWAY_CC, MVT::Other)) {
1980 std::vector<SDOperand> Ops;
1981 Ops.push_back(Chain);
1982 Ops.push_back(N1.getOperand(2));
1983 Ops.push_back(N1.getOperand(0));
1984 Ops.push_back(N1.getOperand(1));
1985 Ops.push_back(N2);
1986 Ops.push_back(N3);
1987 return DAG.getNode(ISD::BRTWOWAY_CC, MVT::Other, Ops);
1988 }
Nate Begeman44728a72005-09-19 22:34:01 +00001989 return SDOperand();
1990}
1991
Chris Lattner3ea0b472005-10-05 06:47:48 +00001992// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
1993//
Nate Begeman44728a72005-09-19 22:34:01 +00001994SDOperand DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00001995 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
1996 SDOperand CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
1997
1998 // Use SimplifySetCC to simplify SETCC's.
Nate Begemane17daeb2005-10-05 21:43:42 +00001999 SDOperand Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false);
2000 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.Val);
2001
2002 // fold br_cc true, dest -> br dest (unconditional branch)
2003 if (SCCC && SCCC->getValue())
2004 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
2005 N->getOperand(4));
2006 // fold br_cc false, dest -> unconditional fall through
2007 if (SCCC && SCCC->isNullValue())
2008 return N->getOperand(0);
2009 // fold to a simpler setcc
2010 if (Simp.Val && Simp.getOpcode() == ISD::SETCC)
2011 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
2012 Simp.getOperand(2), Simp.getOperand(0),
2013 Simp.getOperand(1), N->getOperand(4));
Nate Begeman44728a72005-09-19 22:34:01 +00002014 return SDOperand();
2015}
2016
2017SDOperand DAGCombiner::visitBRTWOWAY_CC(SDNode *N) {
Nate Begemane17daeb2005-10-05 21:43:42 +00002018 SDOperand Chain = N->getOperand(0);
2019 SDOperand CCN = N->getOperand(1);
2020 SDOperand LHS = N->getOperand(2);
2021 SDOperand RHS = N->getOperand(3);
2022 SDOperand N4 = N->getOperand(4);
2023 SDOperand N5 = N->getOperand(5);
2024
2025 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), LHS, RHS,
2026 cast<CondCodeSDNode>(CCN)->get(), false);
2027 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
2028
2029 // fold select_cc lhs, rhs, x, x, cc -> x
2030 if (N4 == N5)
2031 return DAG.getNode(ISD::BR, MVT::Other, Chain, N4);
2032 // fold select_cc true, x, y -> x
2033 if (SCCC && SCCC->getValue())
2034 return DAG.getNode(ISD::BR, MVT::Other, Chain, N4);
2035 // fold select_cc false, x, y -> y
2036 if (SCCC && SCCC->isNullValue())
2037 return DAG.getNode(ISD::BR, MVT::Other, Chain, N5);
2038 // fold to a simpler setcc
Chris Lattner03d5e872006-01-29 06:00:45 +00002039 if (SCC.Val && SCC.getOpcode() == ISD::SETCC) {
2040 std::vector<SDOperand> Ops;
2041 Ops.push_back(Chain);
2042 Ops.push_back(SCC.getOperand(2));
2043 Ops.push_back(SCC.getOperand(0));
2044 Ops.push_back(SCC.getOperand(1));
2045 Ops.push_back(N4);
2046 Ops.push_back(N5);
2047 return DAG.getNode(ISD::BRTWOWAY_CC, MVT::Other, Ops);
2048 }
Nate Begeman44728a72005-09-19 22:34:01 +00002049 return SDOperand();
2050}
2051
Chris Lattner01a22022005-10-10 22:04:48 +00002052SDOperand DAGCombiner::visitLOAD(SDNode *N) {
2053 SDOperand Chain = N->getOperand(0);
2054 SDOperand Ptr = N->getOperand(1);
2055 SDOperand SrcValue = N->getOperand(2);
2056
2057 // If this load is directly stored, replace the load value with the stored
2058 // value.
2059 // TODO: Handle store large -> read small portion.
2060 // TODO: Handle TRUNCSTORE/EXTLOAD
2061 if (Chain.getOpcode() == ISD::STORE && Chain.getOperand(2) == Ptr &&
2062 Chain.getOperand(1).getValueType() == N->getValueType(0))
2063 return CombineTo(N, Chain.getOperand(1), Chain);
2064
2065 return SDOperand();
2066}
2067
Chris Lattner87514ca2005-10-10 22:31:19 +00002068SDOperand DAGCombiner::visitSTORE(SDNode *N) {
2069 SDOperand Chain = N->getOperand(0);
2070 SDOperand Value = N->getOperand(1);
2071 SDOperand Ptr = N->getOperand(2);
2072 SDOperand SrcValue = N->getOperand(3);
2073
2074 // If this is a store that kills a previous store, remove the previous store.
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002075 if (Chain.getOpcode() == ISD::STORE && Chain.getOperand(2) == Ptr &&
Chris Lattnerfe7f0462005-10-27 07:10:34 +00002076 Chain.Val->hasOneUse() /* Avoid introducing DAG cycles */ &&
2077 // Make sure that these stores are the same value type:
2078 // FIXME: we really care that the second store is >= size of the first.
2079 Value.getValueType() == Chain.getOperand(1).getValueType()) {
Chris Lattner87514ca2005-10-10 22:31:19 +00002080 // Create a new store of Value that replaces both stores.
2081 SDNode *PrevStore = Chain.Val;
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002082 if (PrevStore->getOperand(1) == Value) // Same value multiply stored.
2083 return Chain;
Chris Lattner87514ca2005-10-10 22:31:19 +00002084 SDOperand NewStore = DAG.getNode(ISD::STORE, MVT::Other,
2085 PrevStore->getOperand(0), Value, Ptr,
2086 SrcValue);
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002087 CombineTo(N, NewStore); // Nuke this store.
Chris Lattner87514ca2005-10-10 22:31:19 +00002088 CombineTo(PrevStore, NewStore); // Nuke the previous store.
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002089 return SDOperand(N, 0);
Chris Lattner87514ca2005-10-10 22:31:19 +00002090 }
2091
Chris Lattnerc33baaa2005-12-23 05:48:07 +00002092 // If this is a store of a bit convert, store the input value.
Chris Lattnerbf40c4b2006-01-15 18:58:59 +00002093 // FIXME: This needs to know that the resultant store does not need a
2094 // higher alignment than the original.
2095 if (0 && Value.getOpcode() == ISD::BIT_CONVERT)
Chris Lattnerc33baaa2005-12-23 05:48:07 +00002096 return DAG.getNode(ISD::STORE, MVT::Other, Chain, Value.getOperand(0),
2097 Ptr, SrcValue);
2098
Chris Lattner87514ca2005-10-10 22:31:19 +00002099 return SDOperand();
2100}
2101
Nate Begeman44728a72005-09-19 22:34:01 +00002102SDOperand DAGCombiner::SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2){
Nate Begemanf845b452005-10-08 00:29:44 +00002103 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
2104
2105 SDOperand SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
2106 cast<CondCodeSDNode>(N0.getOperand(2))->get());
2107 // If we got a simplified select_cc node back from SimplifySelectCC, then
2108 // break it down into a new SETCC node, and a new SELECT node, and then return
2109 // the SELECT node, since we were called with a SELECT node.
2110 if (SCC.Val) {
2111 // Check to see if we got a select_cc back (to turn into setcc/select).
2112 // Otherwise, just return whatever node we got back, like fabs.
2113 if (SCC.getOpcode() == ISD::SELECT_CC) {
2114 SDOperand SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
2115 SCC.getOperand(0), SCC.getOperand(1),
2116 SCC.getOperand(4));
2117 WorkList.push_back(SETCC.Val);
2118 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
2119 SCC.getOperand(3), SETCC);
2120 }
2121 return SCC;
2122 }
Nate Begeman44728a72005-09-19 22:34:01 +00002123 return SDOperand();
2124}
2125
Chris Lattner40c62d52005-10-18 06:04:22 +00002126/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
2127/// are the two values being selected between, see if we can simplify the
2128/// select.
2129///
2130bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDOperand LHS,
2131 SDOperand RHS) {
2132
2133 // If this is a select from two identical things, try to pull the operation
2134 // through the select.
2135 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
2136#if 0
2137 std::cerr << "SELECT: ["; LHS.Val->dump();
2138 std::cerr << "] ["; RHS.Val->dump();
2139 std::cerr << "]\n";
2140#endif
2141
2142 // If this is a load and the token chain is identical, replace the select
2143 // of two loads with a load through a select of the address to load from.
2144 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
2145 // constants have been dropped into the constant pool.
2146 if ((LHS.getOpcode() == ISD::LOAD ||
2147 LHS.getOpcode() == ISD::EXTLOAD ||
2148 LHS.getOpcode() == ISD::ZEXTLOAD ||
2149 LHS.getOpcode() == ISD::SEXTLOAD) &&
2150 // Token chains must be identical.
2151 LHS.getOperand(0) == RHS.getOperand(0) &&
2152 // If this is an EXTLOAD, the VT's must match.
2153 (LHS.getOpcode() == ISD::LOAD ||
2154 LHS.getOperand(3) == RHS.getOperand(3))) {
2155 // FIXME: this conflates two src values, discarding one. This is not
2156 // the right thing to do, but nothing uses srcvalues now. When they do,
2157 // turn SrcValue into a list of locations.
2158 SDOperand Addr;
2159 if (TheSelect->getOpcode() == ISD::SELECT)
2160 Addr = DAG.getNode(ISD::SELECT, LHS.getOperand(1).getValueType(),
2161 TheSelect->getOperand(0), LHS.getOperand(1),
2162 RHS.getOperand(1));
2163 else
2164 Addr = DAG.getNode(ISD::SELECT_CC, LHS.getOperand(1).getValueType(),
2165 TheSelect->getOperand(0),
2166 TheSelect->getOperand(1),
2167 LHS.getOperand(1), RHS.getOperand(1),
2168 TheSelect->getOperand(4));
2169
2170 SDOperand Load;
2171 if (LHS.getOpcode() == ISD::LOAD)
2172 Load = DAG.getLoad(TheSelect->getValueType(0), LHS.getOperand(0),
2173 Addr, LHS.getOperand(2));
2174 else
2175 Load = DAG.getExtLoad(LHS.getOpcode(), TheSelect->getValueType(0),
2176 LHS.getOperand(0), Addr, LHS.getOperand(2),
2177 cast<VTSDNode>(LHS.getOperand(3))->getVT());
2178 // Users of the select now use the result of the load.
2179 CombineTo(TheSelect, Load);
2180
2181 // Users of the old loads now use the new load's chain. We know the
2182 // old-load value is dead now.
2183 CombineTo(LHS.Val, Load.getValue(0), Load.getValue(1));
2184 CombineTo(RHS.Val, Load.getValue(0), Load.getValue(1));
2185 return true;
2186 }
2187 }
2188
2189 return false;
2190}
2191
Nate Begeman44728a72005-09-19 22:34:01 +00002192SDOperand DAGCombiner::SimplifySelectCC(SDOperand N0, SDOperand N1,
2193 SDOperand N2, SDOperand N3,
2194 ISD::CondCode CC) {
Nate Begemanf845b452005-10-08 00:29:44 +00002195
2196 MVT::ValueType VT = N2.getValueType();
2197 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
2198 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2199 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
2200 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
2201
2202 // Determine if the condition we're dealing with is constant
2203 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
2204 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
2205
2206 // fold select_cc true, x, y -> x
2207 if (SCCC && SCCC->getValue())
2208 return N2;
2209 // fold select_cc false, x, y -> y
2210 if (SCCC && SCCC->getValue() == 0)
2211 return N3;
2212
2213 // Check to see if we can simplify the select into an fabs node
2214 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
2215 // Allow either -0.0 or 0.0
2216 if (CFP->getValue() == 0.0) {
2217 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
2218 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
2219 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
2220 N2 == N3.getOperand(0))
2221 return DAG.getNode(ISD::FABS, VT, N0);
2222
2223 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
2224 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
2225 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
2226 N2.getOperand(0) == N3)
2227 return DAG.getNode(ISD::FABS, VT, N3);
2228 }
2229 }
2230
2231 // Check to see if we can perform the "gzip trick", transforming
2232 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
2233 if (N1C && N1C->isNullValue() && N3C && N3C->isNullValue() &&
2234 MVT::isInteger(N0.getValueType()) &&
2235 MVT::isInteger(N2.getValueType()) && CC == ISD::SETLT) {
2236 MVT::ValueType XType = N0.getValueType();
2237 MVT::ValueType AType = N2.getValueType();
2238 if (XType >= AType) {
2239 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00002240 // single-bit constant.
Nate Begemanf845b452005-10-08 00:29:44 +00002241 if (N2C && ((N2C->getValue() & (N2C->getValue()-1)) == 0)) {
2242 unsigned ShCtV = Log2_64(N2C->getValue());
2243 ShCtV = MVT::getSizeInBits(XType)-ShCtV-1;
2244 SDOperand ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
2245 SDOperand Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
2246 WorkList.push_back(Shift.Val);
2247 if (XType > AType) {
2248 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
2249 WorkList.push_back(Shift.Val);
2250 }
2251 return DAG.getNode(ISD::AND, AType, Shift, N2);
2252 }
2253 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
2254 DAG.getConstant(MVT::getSizeInBits(XType)-1,
2255 TLI.getShiftAmountTy()));
2256 WorkList.push_back(Shift.Val);
2257 if (XType > AType) {
2258 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
2259 WorkList.push_back(Shift.Val);
2260 }
2261 return DAG.getNode(ISD::AND, AType, Shift, N2);
2262 }
2263 }
Nate Begeman07ed4172005-10-10 21:26:48 +00002264
2265 // fold select C, 16, 0 -> shl C, 4
2266 if (N2C && N3C && N3C->isNullValue() && isPowerOf2_64(N2C->getValue()) &&
2267 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) {
2268 // Get a SetCC of the condition
2269 // FIXME: Should probably make sure that setcc is legal if we ever have a
2270 // target where it isn't.
2271 SDOperand Temp, SCC = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
2272 WorkList.push_back(SCC.Val);
2273 // cast from setcc result type to select result type
2274 if (AfterLegalize)
2275 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
2276 else
2277 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
2278 WorkList.push_back(Temp.Val);
2279 // shl setcc result by log2 n2c
2280 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
2281 DAG.getConstant(Log2_64(N2C->getValue()),
2282 TLI.getShiftAmountTy()));
2283 }
2284
Nate Begemanf845b452005-10-08 00:29:44 +00002285 // Check to see if this is the equivalent of setcc
2286 // FIXME: Turn all of these into setcc if setcc if setcc is legal
2287 // otherwise, go ahead with the folds.
2288 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getValue() == 1ULL)) {
2289 MVT::ValueType XType = N0.getValueType();
2290 if (TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultTy())) {
2291 SDOperand Res = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
2292 if (Res.getValueType() != VT)
2293 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
2294 return Res;
2295 }
2296
2297 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
2298 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
2299 TLI.isOperationLegal(ISD::CTLZ, XType)) {
2300 SDOperand Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
2301 return DAG.getNode(ISD::SRL, XType, Ctlz,
2302 DAG.getConstant(Log2_32(MVT::getSizeInBits(XType)),
2303 TLI.getShiftAmountTy()));
2304 }
2305 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
2306 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
2307 SDOperand NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
2308 N0);
2309 SDOperand NotN0 = DAG.getNode(ISD::XOR, XType, N0,
2310 DAG.getConstant(~0ULL, XType));
2311 return DAG.getNode(ISD::SRL, XType,
2312 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
2313 DAG.getConstant(MVT::getSizeInBits(XType)-1,
2314 TLI.getShiftAmountTy()));
2315 }
2316 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
2317 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
2318 SDOperand Sign = DAG.getNode(ISD::SRL, XType, N0,
2319 DAG.getConstant(MVT::getSizeInBits(XType)-1,
2320 TLI.getShiftAmountTy()));
2321 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
2322 }
2323 }
2324
2325 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
2326 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
2327 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
2328 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1)) {
2329 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N2.getOperand(0))) {
2330 MVT::ValueType XType = N0.getValueType();
2331 if (SubC->isNullValue() && MVT::isInteger(XType)) {
2332 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
2333 DAG.getConstant(MVT::getSizeInBits(XType)-1,
2334 TLI.getShiftAmountTy()));
2335 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
2336 WorkList.push_back(Shift.Val);
2337 WorkList.push_back(Add.Val);
2338 return DAG.getNode(ISD::XOR, XType, Add, Shift);
2339 }
2340 }
2341 }
2342
Nate Begeman44728a72005-09-19 22:34:01 +00002343 return SDOperand();
2344}
2345
Nate Begeman452d7be2005-09-16 00:54:12 +00002346SDOperand DAGCombiner::SimplifySetCC(MVT::ValueType VT, SDOperand N0,
Nate Begemane17daeb2005-10-05 21:43:42 +00002347 SDOperand N1, ISD::CondCode Cond,
2348 bool foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00002349 // These setcc operations always fold.
2350 switch (Cond) {
2351 default: break;
2352 case ISD::SETFALSE:
2353 case ISD::SETFALSE2: return DAG.getConstant(0, VT);
2354 case ISD::SETTRUE:
2355 case ISD::SETTRUE2: return DAG.getConstant(1, VT);
2356 }
2357
2358 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
2359 uint64_t C1 = N1C->getValue();
2360 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val)) {
2361 uint64_t C0 = N0C->getValue();
2362
2363 // Sign extend the operands if required
2364 if (ISD::isSignedIntSetCC(Cond)) {
2365 C0 = N0C->getSignExtended();
2366 C1 = N1C->getSignExtended();
2367 }
2368
2369 switch (Cond) {
2370 default: assert(0 && "Unknown integer setcc!");
2371 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
2372 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
2373 case ISD::SETULT: return DAG.getConstant(C0 < C1, VT);
2374 case ISD::SETUGT: return DAG.getConstant(C0 > C1, VT);
2375 case ISD::SETULE: return DAG.getConstant(C0 <= C1, VT);
2376 case ISD::SETUGE: return DAG.getConstant(C0 >= C1, VT);
2377 case ISD::SETLT: return DAG.getConstant((int64_t)C0 < (int64_t)C1, VT);
2378 case ISD::SETGT: return DAG.getConstant((int64_t)C0 > (int64_t)C1, VT);
2379 case ISD::SETLE: return DAG.getConstant((int64_t)C0 <= (int64_t)C1, VT);
2380 case ISD::SETGE: return DAG.getConstant((int64_t)C0 >= (int64_t)C1, VT);
2381 }
2382 } else {
2383 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
2384 if (N0.getOpcode() == ISD::ZERO_EXTEND) {
2385 unsigned InSize = MVT::getSizeInBits(N0.getOperand(0).getValueType());
2386
2387 // If the comparison constant has bits in the upper part, the
2388 // zero-extended value could never match.
2389 if (C1 & (~0ULL << InSize)) {
2390 unsigned VSize = MVT::getSizeInBits(N0.getValueType());
2391 switch (Cond) {
2392 case ISD::SETUGT:
2393 case ISD::SETUGE:
2394 case ISD::SETEQ: return DAG.getConstant(0, VT);
2395 case ISD::SETULT:
2396 case ISD::SETULE:
2397 case ISD::SETNE: return DAG.getConstant(1, VT);
2398 case ISD::SETGT:
2399 case ISD::SETGE:
2400 // True if the sign bit of C1 is set.
2401 return DAG.getConstant((C1 & (1ULL << VSize)) != 0, VT);
2402 case ISD::SETLT:
2403 case ISD::SETLE:
2404 // True if the sign bit of C1 isn't set.
2405 return DAG.getConstant((C1 & (1ULL << VSize)) == 0, VT);
2406 default:
2407 break;
2408 }
2409 }
2410
2411 // Otherwise, we can perform the comparison with the low bits.
2412 switch (Cond) {
2413 case ISD::SETEQ:
2414 case ISD::SETNE:
2415 case ISD::SETUGT:
2416 case ISD::SETUGE:
2417 case ISD::SETULT:
2418 case ISD::SETULE:
2419 return DAG.getSetCC(VT, N0.getOperand(0),
2420 DAG.getConstant(C1, N0.getOperand(0).getValueType()),
2421 Cond);
2422 default:
2423 break; // todo, be more careful with signed comparisons
2424 }
2425 } else if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
2426 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
2427 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N0.getOperand(1))->getVT();
2428 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
2429 MVT::ValueType ExtDstTy = N0.getValueType();
2430 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
2431
2432 // If the extended part has any inconsistent bits, it cannot ever
2433 // compare equal. In other words, they have to be all ones or all
2434 // zeros.
2435 uint64_t ExtBits =
2436 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
2437 if ((C1 & ExtBits) != 0 && (C1 & ExtBits) != ExtBits)
2438 return DAG.getConstant(Cond == ISD::SETNE, VT);
2439
2440 SDOperand ZextOp;
2441 MVT::ValueType Op0Ty = N0.getOperand(0).getValueType();
2442 if (Op0Ty == ExtSrcTy) {
2443 ZextOp = N0.getOperand(0);
2444 } else {
2445 int64_t Imm = ~0ULL >> (64-ExtSrcTyBits);
2446 ZextOp = DAG.getNode(ISD::AND, Op0Ty, N0.getOperand(0),
2447 DAG.getConstant(Imm, Op0Ty));
2448 }
2449 WorkList.push_back(ZextOp.Val);
2450 // Otherwise, make this a use of a zext.
2451 return DAG.getSetCC(VT, ZextOp,
2452 DAG.getConstant(C1 & (~0ULL>>(64-ExtSrcTyBits)),
2453 ExtDstTy),
2454 Cond);
Chris Lattner3391bcd2006-02-08 02:13:15 +00002455 } else if ((N1C->getValue() == 0 || N1C->getValue() == 1) &&
2456 (Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
2457 (N0.getOpcode() == ISD::XOR ||
2458 (N0.getOpcode() == ISD::AND &&
2459 N0.getOperand(0).getOpcode() == ISD::XOR &&
2460 N0.getOperand(1) == N0.getOperand(0).getOperand(1))) &&
2461 isa<ConstantSDNode>(N0.getOperand(1)) &&
2462 cast<ConstantSDNode>(N0.getOperand(1))->getValue() == 1) {
2463 // If this is (X^1) == 0/1, swap the RHS and eliminate the xor. We can
2464 // only do this if the top bits are known zero.
2465 if (TLI.MaskedValueIsZero(N1,
2466 MVT::getIntVTBitMask(N0.getValueType())-1)) {
2467 // Okay, get the un-inverted input value.
2468 SDOperand Val;
2469 if (N0.getOpcode() == ISD::XOR)
2470 Val = N0.getOperand(0);
2471 else {
2472 assert(N0.getOpcode() == ISD::AND &&
2473 N0.getOperand(0).getOpcode() == ISD::XOR);
2474 // ((X^1)&1)^1 -> X & 1
2475 Val = DAG.getNode(ISD::AND, N0.getValueType(),
2476 N0.getOperand(0).getOperand(0), N0.getOperand(1));
2477 }
2478 return DAG.getSetCC(VT, Val, N1,
2479 Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ);
2480 }
Nate Begeman452d7be2005-09-16 00:54:12 +00002481 }
Chris Lattner5c46f742005-10-05 06:11:08 +00002482
Nate Begeman452d7be2005-09-16 00:54:12 +00002483 uint64_t MinVal, MaxVal;
2484 unsigned OperandBitSize = MVT::getSizeInBits(N1C->getValueType(0));
2485 if (ISD::isSignedIntSetCC(Cond)) {
2486 MinVal = 1ULL << (OperandBitSize-1);
2487 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
2488 MaxVal = ~0ULL >> (65-OperandBitSize);
2489 else
2490 MaxVal = 0;
2491 } else {
2492 MinVal = 0;
2493 MaxVal = ~0ULL >> (64-OperandBitSize);
2494 }
2495
2496 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
2497 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
2498 if (C1 == MinVal) return DAG.getConstant(1, VT); // X >= MIN --> true
2499 --C1; // X >= C0 --> X > (C0-1)
2500 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
2501 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
2502 }
2503
2504 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
2505 if (C1 == MaxVal) return DAG.getConstant(1, VT); // X <= MAX --> true
2506 ++C1; // X <= C0 --> X < (C0+1)
2507 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
2508 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
2509 }
2510
2511 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal)
2512 return DAG.getConstant(0, VT); // X < MIN --> false
2513
2514 // Canonicalize setgt X, Min --> setne X, Min
2515 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MinVal)
2516 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
Chris Lattnerc8597ca2005-10-21 21:23:25 +00002517 // Canonicalize setlt X, Max --> setne X, Max
2518 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MaxVal)
2519 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
Nate Begeman452d7be2005-09-16 00:54:12 +00002520
2521 // If we have setult X, 1, turn it into seteq X, 0
2522 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal+1)
2523 return DAG.getSetCC(VT, N0, DAG.getConstant(MinVal, N0.getValueType()),
2524 ISD::SETEQ);
2525 // If we have setugt X, Max-1, turn it into seteq X, Max
2526 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal-1)
2527 return DAG.getSetCC(VT, N0, DAG.getConstant(MaxVal, N0.getValueType()),
2528 ISD::SETEQ);
2529
2530 // If we have "setcc X, C0", check to see if we can shrink the immediate
2531 // by changing cc.
2532
2533 // SETUGT X, SINTMAX -> SETLT X, 0
2534 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
2535 C1 == (~0ULL >> (65-OperandBitSize)))
2536 return DAG.getSetCC(VT, N0, DAG.getConstant(0, N1.getValueType()),
2537 ISD::SETLT);
2538
2539 // FIXME: Implement the rest of these.
2540
2541 // Fold bit comparisons when we can.
2542 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
2543 VT == N0.getValueType() && N0.getOpcode() == ISD::AND)
2544 if (ConstantSDNode *AndRHS =
2545 dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2546 if (Cond == ISD::SETNE && C1 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
2547 // Perform the xform if the AND RHS is a single bit.
2548 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
2549 return DAG.getNode(ISD::SRL, VT, N0,
2550 DAG.getConstant(Log2_64(AndRHS->getValue()),
2551 TLI.getShiftAmountTy()));
2552 }
2553 } else if (Cond == ISD::SETEQ && C1 == AndRHS->getValue()) {
2554 // (X & 8) == 8 --> (X & 8) >> 3
2555 // Perform the xform if C1 is a single bit.
2556 if ((C1 & (C1-1)) == 0) {
2557 return DAG.getNode(ISD::SRL, VT, N0,
2558 DAG.getConstant(Log2_64(C1),TLI.getShiftAmountTy()));
2559 }
2560 }
2561 }
2562 }
2563 } else if (isa<ConstantSDNode>(N0.Val)) {
2564 // Ensure that the constant occurs on the RHS.
2565 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
2566 }
2567
2568 if (ConstantFPSDNode *N0C = dyn_cast<ConstantFPSDNode>(N0.Val))
2569 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) {
2570 double C0 = N0C->getValue(), C1 = N1C->getValue();
2571
2572 switch (Cond) {
2573 default: break; // FIXME: Implement the rest of these!
2574 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
2575 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
2576 case ISD::SETLT: return DAG.getConstant(C0 < C1, VT);
2577 case ISD::SETGT: return DAG.getConstant(C0 > C1, VT);
2578 case ISD::SETLE: return DAG.getConstant(C0 <= C1, VT);
2579 case ISD::SETGE: return DAG.getConstant(C0 >= C1, VT);
2580 }
2581 } else {
2582 // Ensure that the constant occurs on the RHS.
2583 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
2584 }
2585
2586 if (N0 == N1) {
2587 // We can always fold X == Y for integer setcc's.
2588 if (MVT::isInteger(N0.getValueType()))
2589 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
2590 unsigned UOF = ISD::getUnorderedFlavor(Cond);
2591 if (UOF == 2) // FP operators that are undefined on NaNs.
2592 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
2593 if (UOF == unsigned(ISD::isTrueWhenEqual(Cond)))
2594 return DAG.getConstant(UOF, VT);
2595 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
2596 // if it is not already.
Chris Lattner4090aee2006-01-18 19:13:41 +00002597 ISD::CondCode NewCond = UOF == 0 ? ISD::SETO : ISD::SETUO;
Nate Begeman452d7be2005-09-16 00:54:12 +00002598 if (NewCond != Cond)
2599 return DAG.getSetCC(VT, N0, N1, NewCond);
2600 }
2601
2602 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
2603 MVT::isInteger(N0.getValueType())) {
2604 if (N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::SUB ||
2605 N0.getOpcode() == ISD::XOR) {
2606 // Simplify (X+Y) == (X+Z) --> Y == Z
2607 if (N0.getOpcode() == N1.getOpcode()) {
2608 if (N0.getOperand(0) == N1.getOperand(0))
2609 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(1), Cond);
2610 if (N0.getOperand(1) == N1.getOperand(1))
2611 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(0), Cond);
2612 if (isCommutativeBinOp(N0.getOpcode())) {
2613 // If X op Y == Y op X, try other combinations.
2614 if (N0.getOperand(0) == N1.getOperand(1))
2615 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(0), Cond);
2616 if (N0.getOperand(1) == N1.getOperand(0))
Chris Lattnera158eee2005-10-25 18:57:30 +00002617 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(1), Cond);
Nate Begeman452d7be2005-09-16 00:54:12 +00002618 }
2619 }
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00002620
2621 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(N1)) {
2622 if (ConstantSDNode *LHSR = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2623 // Turn (X+C1) == C2 --> X == C2-C1
2624 if (N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse()) {
2625 return DAG.getSetCC(VT, N0.getOperand(0),
2626 DAG.getConstant(RHSC->getValue()-LHSR->getValue(),
2627 N0.getValueType()), Cond);
2628 }
2629
2630 // Turn (X^C1) == C2 into X == C1^C2 iff X&~C1 = 0.
2631 if (N0.getOpcode() == ISD::XOR)
Chris Lattner5c46f742005-10-05 06:11:08 +00002632 // If we know that all of the inverted bits are zero, don't bother
2633 // performing the inversion.
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00002634 if (TLI.MaskedValueIsZero(N0.getOperand(0), ~LHSR->getValue()))
Chris Lattner5c46f742005-10-05 06:11:08 +00002635 return DAG.getSetCC(VT, N0.getOperand(0),
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00002636 DAG.getConstant(LHSR->getValue()^RHSC->getValue(),
Chris Lattner5c46f742005-10-05 06:11:08 +00002637 N0.getValueType()), Cond);
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00002638 }
2639
2640 // Turn (C1-X) == C2 --> X == C1-C2
2641 if (ConstantSDNode *SUBC = dyn_cast<ConstantSDNode>(N0.getOperand(0))) {
2642 if (N0.getOpcode() == ISD::SUB && N0.Val->hasOneUse()) {
2643 return DAG.getSetCC(VT, N0.getOperand(1),
2644 DAG.getConstant(SUBC->getValue()-RHSC->getValue(),
2645 N0.getValueType()), Cond);
Chris Lattner5c46f742005-10-05 06:11:08 +00002646 }
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00002647 }
2648 }
2649
Nate Begeman452d7be2005-09-16 00:54:12 +00002650 // Simplify (X+Z) == X --> Z == 0
2651 if (N0.getOperand(0) == N1)
2652 return DAG.getSetCC(VT, N0.getOperand(1),
2653 DAG.getConstant(0, N0.getValueType()), Cond);
2654 if (N0.getOperand(1) == N1) {
2655 if (isCommutativeBinOp(N0.getOpcode()))
2656 return DAG.getSetCC(VT, N0.getOperand(0),
2657 DAG.getConstant(0, N0.getValueType()), Cond);
2658 else {
2659 assert(N0.getOpcode() == ISD::SUB && "Unexpected operation!");
2660 // (Z-X) == X --> Z == X<<1
2661 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(),
2662 N1,
2663 DAG.getConstant(1,TLI.getShiftAmountTy()));
2664 WorkList.push_back(SH.Val);
2665 return DAG.getSetCC(VT, N0.getOperand(0), SH, Cond);
2666 }
2667 }
2668 }
2669
2670 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
2671 N1.getOpcode() == ISD::XOR) {
2672 // Simplify X == (X+Z) --> Z == 0
2673 if (N1.getOperand(0) == N0) {
2674 return DAG.getSetCC(VT, N1.getOperand(1),
2675 DAG.getConstant(0, N1.getValueType()), Cond);
2676 } else if (N1.getOperand(1) == N0) {
2677 if (isCommutativeBinOp(N1.getOpcode())) {
2678 return DAG.getSetCC(VT, N1.getOperand(0),
2679 DAG.getConstant(0, N1.getValueType()), Cond);
2680 } else {
2681 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
2682 // X == (Z-X) --> X<<1 == Z
2683 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(), N0,
2684 DAG.getConstant(1,TLI.getShiftAmountTy()));
2685 WorkList.push_back(SH.Val);
2686 return DAG.getSetCC(VT, SH, N1.getOperand(0), Cond);
2687 }
2688 }
2689 }
2690 }
2691
2692 // Fold away ALL boolean setcc's.
2693 SDOperand Temp;
Nate Begemane17daeb2005-10-05 21:43:42 +00002694 if (N0.getValueType() == MVT::i1 && foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00002695 switch (Cond) {
2696 default: assert(0 && "Unknown integer setcc!");
2697 case ISD::SETEQ: // X == Y -> (X^Y)^1
2698 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
2699 N0 = DAG.getNode(ISD::XOR, MVT::i1, Temp, DAG.getConstant(1, MVT::i1));
2700 WorkList.push_back(Temp.Val);
2701 break;
2702 case ISD::SETNE: // X != Y --> (X^Y)
2703 N0 = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
2704 break;
2705 case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> X^1 & Y
2706 case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> X^1 & Y
2707 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
2708 N0 = DAG.getNode(ISD::AND, MVT::i1, N1, Temp);
2709 WorkList.push_back(Temp.Val);
2710 break;
2711 case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> Y^1 & X
2712 case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> Y^1 & X
2713 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
2714 N0 = DAG.getNode(ISD::AND, MVT::i1, N0, Temp);
2715 WorkList.push_back(Temp.Val);
2716 break;
2717 case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> X^1 | Y
2718 case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> X^1 | Y
2719 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
2720 N0 = DAG.getNode(ISD::OR, MVT::i1, N1, Temp);
2721 WorkList.push_back(Temp.Val);
2722 break;
2723 case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> Y^1 | X
2724 case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> Y^1 | X
2725 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
2726 N0 = DAG.getNode(ISD::OR, MVT::i1, N0, Temp);
2727 break;
2728 }
2729 if (VT != MVT::i1) {
2730 WorkList.push_back(N0.Val);
2731 // FIXME: If running after legalize, we probably can't do this.
2732 N0 = DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
2733 }
2734 return N0;
2735 }
2736
2737 // Could not fold it.
2738 return SDOperand();
2739}
2740
Nate Begeman69575232005-10-20 02:15:44 +00002741/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
2742/// return a DAG expression to select that will generate the same value by
2743/// multiplying by a magic number. See:
2744/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
2745SDOperand DAGCombiner::BuildSDIV(SDNode *N) {
2746 MVT::ValueType VT = N->getValueType(0);
Chris Lattnere9936d12005-10-22 18:50:15 +00002747
2748 // Check to see if we can do this.
2749 if (!TLI.isTypeLegal(VT) || (VT != MVT::i32 && VT != MVT::i64))
2750 return SDOperand(); // BuildSDIV only operates on i32 or i64
2751 if (!TLI.isOperationLegal(ISD::MULHS, VT))
2752 return SDOperand(); // Make sure the target supports MULHS.
Nate Begeman69575232005-10-20 02:15:44 +00002753
Nate Begemanc6a454e2005-10-20 17:45:03 +00002754 int64_t d = cast<ConstantSDNode>(N->getOperand(1))->getSignExtended();
Nate Begeman69575232005-10-20 02:15:44 +00002755 ms magics = (VT == MVT::i32) ? magic32(d) : magic64(d);
2756
2757 // Multiply the numerator (operand 0) by the magic value
2758 SDOperand Q = DAG.getNode(ISD::MULHS, VT, N->getOperand(0),
2759 DAG.getConstant(magics.m, VT));
2760 // If d > 0 and m < 0, add the numerator
2761 if (d > 0 && magics.m < 0) {
2762 Q = DAG.getNode(ISD::ADD, VT, Q, N->getOperand(0));
2763 WorkList.push_back(Q.Val);
2764 }
2765 // If d < 0 and m > 0, subtract the numerator.
2766 if (d < 0 && magics.m > 0) {
2767 Q = DAG.getNode(ISD::SUB, VT, Q, N->getOperand(0));
2768 WorkList.push_back(Q.Val);
2769 }
2770 // Shift right algebraic if shift value is nonzero
2771 if (magics.s > 0) {
2772 Q = DAG.getNode(ISD::SRA, VT, Q,
2773 DAG.getConstant(magics.s, TLI.getShiftAmountTy()));
2774 WorkList.push_back(Q.Val);
2775 }
2776 // Extract the sign bit and add it to the quotient
2777 SDOperand T =
Nate Begeman4d385672005-10-21 01:51:45 +00002778 DAG.getNode(ISD::SRL, VT, Q, DAG.getConstant(MVT::getSizeInBits(VT)-1,
2779 TLI.getShiftAmountTy()));
Nate Begeman69575232005-10-20 02:15:44 +00002780 WorkList.push_back(T.Val);
2781 return DAG.getNode(ISD::ADD, VT, Q, T);
2782}
2783
2784/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
2785/// return a DAG expression to select that will generate the same value by
2786/// multiplying by a magic number. See:
2787/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
2788SDOperand DAGCombiner::BuildUDIV(SDNode *N) {
2789 MVT::ValueType VT = N->getValueType(0);
Chris Lattnere9936d12005-10-22 18:50:15 +00002790
2791 // Check to see if we can do this.
2792 if (!TLI.isTypeLegal(VT) || (VT != MVT::i32 && VT != MVT::i64))
2793 return SDOperand(); // BuildUDIV only operates on i32 or i64
2794 if (!TLI.isOperationLegal(ISD::MULHU, VT))
2795 return SDOperand(); // Make sure the target supports MULHU.
Nate Begeman69575232005-10-20 02:15:44 +00002796
2797 uint64_t d = cast<ConstantSDNode>(N->getOperand(1))->getValue();
2798 mu magics = (VT == MVT::i32) ? magicu32(d) : magicu64(d);
2799
2800 // Multiply the numerator (operand 0) by the magic value
2801 SDOperand Q = DAG.getNode(ISD::MULHU, VT, N->getOperand(0),
2802 DAG.getConstant(magics.m, VT));
2803 WorkList.push_back(Q.Val);
2804
2805 if (magics.a == 0) {
2806 return DAG.getNode(ISD::SRL, VT, Q,
2807 DAG.getConstant(magics.s, TLI.getShiftAmountTy()));
2808 } else {
2809 SDOperand NPQ = DAG.getNode(ISD::SUB, VT, N->getOperand(0), Q);
2810 WorkList.push_back(NPQ.Val);
2811 NPQ = DAG.getNode(ISD::SRL, VT, NPQ,
2812 DAG.getConstant(1, TLI.getShiftAmountTy()));
2813 WorkList.push_back(NPQ.Val);
2814 NPQ = DAG.getNode(ISD::ADD, VT, NPQ, Q);
2815 WorkList.push_back(NPQ.Val);
2816 return DAG.getNode(ISD::SRL, VT, NPQ,
2817 DAG.getConstant(magics.s-1, TLI.getShiftAmountTy()));
2818 }
2819}
2820
Nate Begeman1d4d4142005-09-01 00:19:25 +00002821// SelectionDAG::Combine - This is the entry point for the file.
2822//
Nate Begeman4ebd8052005-09-01 23:24:04 +00002823void SelectionDAG::Combine(bool RunningAfterLegalize) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002824 /// run - This is the main entry point to this class.
2825 ///
Nate Begeman4ebd8052005-09-01 23:24:04 +00002826 DAGCombiner(*this).Run(RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002827}