blob: 7019f8f646a4ead061bc57925fd42d2554323ab4 [file] [log] [blame]
Nate Begeman4ebd8052005-09-01 23:24:04 +00001//===-- DAGCombiner.cpp - Implement a DAG node combiner -------------------===//
Nate Begeman1d4d4142005-09-01 00:19:25 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Nate Begeman and is distributed under the
6// University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This pass combines dag nodes to form fewer, simpler DAG nodes. It can be run
11// both before and after the DAG is legalized.
12//
13// FIXME: Missing folds
14// sdiv, udiv, srem, urem (X, const) where X is an integer can be expanded into
15// a sequence of multiplies, shifts, and adds. This should be controlled by
16// some kind of hint from the target that int div is expensive.
17// various folds of mulh[s,u] by constants such as -1, powers of 2, etc.
18//
Nate Begeman44728a72005-09-19 22:34:01 +000019// FIXME: select C, pow2, pow2 -> something smart
20// FIXME: trunc(select X, Y, Z) -> select X, trunc(Y), trunc(Z)
Nate Begeman44728a72005-09-19 22:34:01 +000021// FIXME: Dead stores -> nuke
Chris Lattner40c62d52005-10-18 06:04:22 +000022// FIXME: shr X, (and Y,31) -> shr X, Y (TRICKY!)
Nate Begeman1d4d4142005-09-01 00:19:25 +000023// FIXME: mul (x, const) -> shifts + adds
Nate Begeman1d4d4142005-09-01 00:19:25 +000024// FIXME: undef values
Nate Begeman4ebd8052005-09-01 23:24:04 +000025// FIXME: make truncate see through SIGN_EXTEND and AND
Nate Begeman646d7e22005-09-02 21:18:40 +000026// FIXME: divide by zero is currently left unfolded. do we want to turn this
27// into an undef?
Nate Begemanf845b452005-10-08 00:29:44 +000028// FIXME: select ne (select cc, 1, 0), 0, true, false -> select cc, true, false
Nate Begeman1d4d4142005-09-01 00:19:25 +000029//
30//===----------------------------------------------------------------------===//
31
32#define DEBUG_TYPE "dagcombine"
33#include "llvm/ADT/Statistic.h"
34#include "llvm/CodeGen/SelectionDAG.h"
Nate Begeman2300f552005-09-07 00:15:36 +000035#include "llvm/Support/Debug.h"
Nate Begeman1d4d4142005-09-01 00:19:25 +000036#include "llvm/Support/MathExtras.h"
37#include "llvm/Target/TargetLowering.h"
Chris Lattnera500fc62005-09-09 23:53:39 +000038#include <algorithm>
Nate Begeman1d4d4142005-09-01 00:19:25 +000039#include <cmath>
Chris Lattner2c2c6c62006-01-22 23:41:00 +000040#include <iostream>
Nate Begeman1d4d4142005-09-01 00:19:25 +000041using namespace llvm;
42
43namespace {
44 Statistic<> NodesCombined ("dagcombiner", "Number of dag nodes combined");
45
46 class DAGCombiner {
47 SelectionDAG &DAG;
48 TargetLowering &TLI;
Nate Begeman4ebd8052005-09-01 23:24:04 +000049 bool AfterLegalize;
Nate Begeman1d4d4142005-09-01 00:19:25 +000050
51 // Worklist of all of the nodes that need to be simplified.
52 std::vector<SDNode*> WorkList;
53
54 /// AddUsersToWorkList - When an instruction is simplified, add all users of
55 /// the instruction to the work lists because they might get more simplified
56 /// now.
57 ///
58 void AddUsersToWorkList(SDNode *N) {
59 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
Nate Begeman4ebd8052005-09-01 23:24:04 +000060 UI != UE; ++UI)
61 WorkList.push_back(*UI);
Nate Begeman1d4d4142005-09-01 00:19:25 +000062 }
63
64 /// removeFromWorkList - remove all instances of N from the worklist.
Chris Lattner5750df92006-03-01 04:03:14 +000065 ///
Nate Begeman1d4d4142005-09-01 00:19:25 +000066 void removeFromWorkList(SDNode *N) {
67 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), N),
68 WorkList.end());
69 }
70
Chris Lattner24664722006-03-01 04:53:38 +000071 public:
Chris Lattner5750df92006-03-01 04:03:14 +000072 void AddToWorkList(SDNode *N) {
73 WorkList.push_back(N);
74 }
75
Chris Lattner01a22022005-10-10 22:04:48 +000076 SDOperand CombineTo(SDNode *N, const std::vector<SDOperand> &To) {
Chris Lattner87514ca2005-10-10 22:31:19 +000077 ++NodesCombined;
Chris Lattner01a22022005-10-10 22:04:48 +000078 DEBUG(std::cerr << "\nReplacing "; N->dump();
79 std::cerr << "\nWith: "; To[0].Val->dump();
80 std::cerr << " and " << To.size()-1 << " other values\n");
81 std::vector<SDNode*> NowDead;
82 DAG.ReplaceAllUsesWith(N, To, &NowDead);
83
84 // Push the new nodes and any users onto the worklist
85 for (unsigned i = 0, e = To.size(); i != e; ++i) {
86 WorkList.push_back(To[i].Val);
87 AddUsersToWorkList(To[i].Val);
88 }
89
90 // Nodes can end up on the worklist more than once. Make sure we do
91 // not process a node that has been replaced.
92 removeFromWorkList(N);
93 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
94 removeFromWorkList(NowDead[i]);
95
96 // Finally, since the node is now dead, remove it from the graph.
97 DAG.DeleteNode(N);
98 return SDOperand(N, 0);
99 }
Nate Begeman368e18d2006-02-16 21:11:51 +0000100
Chris Lattner24664722006-03-01 04:53:38 +0000101 SDOperand CombineTo(SDNode *N, SDOperand Res) {
102 std::vector<SDOperand> To;
103 To.push_back(Res);
104 return CombineTo(N, To);
105 }
106
107 SDOperand CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1) {
108 std::vector<SDOperand> To;
109 To.push_back(Res0);
110 To.push_back(Res1);
111 return CombineTo(N, To);
112 }
113 private:
114
Chris Lattner012f2412006-02-17 21:58:01 +0000115 /// SimplifyDemandedBits - Check the specified integer node value to see if
Chris Lattnerb2742f42006-03-01 19:55:35 +0000116 /// it can be simplified or if things it uses can be simplified by bit
Chris Lattner012f2412006-02-17 21:58:01 +0000117 /// propagation. If so, return true.
118 bool SimplifyDemandedBits(SDOperand Op) {
Nate Begeman368e18d2006-02-16 21:11:51 +0000119 TargetLowering::TargetLoweringOpt TLO(DAG);
120 uint64_t KnownZero, KnownOne;
Chris Lattner012f2412006-02-17 21:58:01 +0000121 uint64_t Demanded = MVT::getIntVTBitMask(Op.getValueType());
122 if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO))
123 return false;
124
125 // Revisit the node.
126 WorkList.push_back(Op.Val);
127
128 // Replace the old value with the new one.
129 ++NodesCombined;
130 DEBUG(std::cerr << "\nReplacing "; TLO.Old.Val->dump();
131 std::cerr << "\nWith: "; TLO.New.Val->dump());
132
133 std::vector<SDNode*> NowDead;
134 DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New, NowDead);
135
Chris Lattner7d20d392006-02-20 06:51:04 +0000136 // Push the new node and any (possibly new) users onto the worklist.
Chris Lattner012f2412006-02-17 21:58:01 +0000137 WorkList.push_back(TLO.New.Val);
138 AddUsersToWorkList(TLO.New.Val);
139
140 // Nodes can end up on the worklist more than once. Make sure we do
141 // not process a node that has been replaced.
Chris Lattner012f2412006-02-17 21:58:01 +0000142 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
143 removeFromWorkList(NowDead[i]);
144
Chris Lattner7d20d392006-02-20 06:51:04 +0000145 // Finally, if the node is now dead, remove it from the graph. The node
146 // may not be dead if the replacement process recursively simplified to
147 // something else needing this node.
148 if (TLO.Old.Val->use_empty()) {
149 removeFromWorkList(TLO.Old.Val);
150 DAG.DeleteNode(TLO.Old.Val);
151 }
Chris Lattner012f2412006-02-17 21:58:01 +0000152 return true;
Nate Begeman368e18d2006-02-16 21:11:51 +0000153 }
Chris Lattner87514ca2005-10-10 22:31:19 +0000154
Nate Begeman1d4d4142005-09-01 00:19:25 +0000155 /// visit - call the node-specific routine that knows how to fold each
156 /// particular type of node.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000157 SDOperand visit(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000158
159 // Visitation implementation - Implement dag node combining for different
160 // node types. The semantics are as follows:
161 // Return Value:
Nate Begeman2300f552005-09-07 00:15:36 +0000162 // SDOperand.Val == 0 - No change was made
Chris Lattner01a22022005-10-10 22:04:48 +0000163 // SDOperand.Val == N - N was replaced, is dead, and is already handled.
Nate Begeman2300f552005-09-07 00:15:36 +0000164 // otherwise - N should be replaced by the returned Operand.
Nate Begeman1d4d4142005-09-01 00:19:25 +0000165 //
Nate Begeman83e75ec2005-09-06 04:43:02 +0000166 SDOperand visitTokenFactor(SDNode *N);
167 SDOperand visitADD(SDNode *N);
168 SDOperand visitSUB(SDNode *N);
169 SDOperand visitMUL(SDNode *N);
170 SDOperand visitSDIV(SDNode *N);
171 SDOperand visitUDIV(SDNode *N);
172 SDOperand visitSREM(SDNode *N);
173 SDOperand visitUREM(SDNode *N);
174 SDOperand visitMULHU(SDNode *N);
175 SDOperand visitMULHS(SDNode *N);
176 SDOperand visitAND(SDNode *N);
177 SDOperand visitOR(SDNode *N);
178 SDOperand visitXOR(SDNode *N);
Chris Lattneredab1b92006-04-02 03:25:57 +0000179 SDOperand visitVBinOp(SDNode *N, ISD::NodeType IntOp, ISD::NodeType FPOp);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000180 SDOperand visitSHL(SDNode *N);
181 SDOperand visitSRA(SDNode *N);
182 SDOperand visitSRL(SDNode *N);
183 SDOperand visitCTLZ(SDNode *N);
184 SDOperand visitCTTZ(SDNode *N);
185 SDOperand visitCTPOP(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000186 SDOperand visitSELECT(SDNode *N);
187 SDOperand visitSELECT_CC(SDNode *N);
188 SDOperand visitSETCC(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000189 SDOperand visitSIGN_EXTEND(SDNode *N);
190 SDOperand visitZERO_EXTEND(SDNode *N);
191 SDOperand visitSIGN_EXTEND_INREG(SDNode *N);
192 SDOperand visitTRUNCATE(SDNode *N);
Chris Lattner94683772005-12-23 05:30:37 +0000193 SDOperand visitBIT_CONVERT(SDNode *N);
Chris Lattner6258fb22006-04-02 02:53:43 +0000194 SDOperand visitVBIT_CONVERT(SDNode *N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000195 SDOperand visitFADD(SDNode *N);
196 SDOperand visitFSUB(SDNode *N);
197 SDOperand visitFMUL(SDNode *N);
198 SDOperand visitFDIV(SDNode *N);
199 SDOperand visitFREM(SDNode *N);
Chris Lattner12d83032006-03-05 05:30:57 +0000200 SDOperand visitFCOPYSIGN(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000201 SDOperand visitSINT_TO_FP(SDNode *N);
202 SDOperand visitUINT_TO_FP(SDNode *N);
203 SDOperand visitFP_TO_SINT(SDNode *N);
204 SDOperand visitFP_TO_UINT(SDNode *N);
205 SDOperand visitFP_ROUND(SDNode *N);
206 SDOperand visitFP_ROUND_INREG(SDNode *N);
207 SDOperand visitFP_EXTEND(SDNode *N);
208 SDOperand visitFNEG(SDNode *N);
209 SDOperand visitFABS(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000210 SDOperand visitBRCOND(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000211 SDOperand visitBR_CC(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000212 SDOperand visitLOAD(SDNode *N);
Chris Lattner29cd7db2006-03-31 18:10:41 +0000213 SDOperand visitXEXTLOAD(SDNode *N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000214 SDOperand visitSTORE(SDNode *N);
Chris Lattnerca242442006-03-19 01:27:56 +0000215 SDOperand visitINSERT_VECTOR_ELT(SDNode *N);
216 SDOperand visitVINSERT_VECTOR_ELT(SDNode *N);
Chris Lattnerd7648c82006-03-28 20:28:38 +0000217 SDOperand visitVBUILD_VECTOR(SDNode *N);
Chris Lattner66445d32006-03-28 22:11:53 +0000218 SDOperand visitVECTOR_SHUFFLE(SDNode *N);
Chris Lattnerf1d0c622006-03-31 22:16:43 +0000219 SDOperand visitVVECTOR_SHUFFLE(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000220
Evan Cheng44f1f092006-04-20 08:56:16 +0000221 SDOperand XformToShuffleWithZero(SDNode *N);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000222 SDOperand ReassociateOps(unsigned Opc, SDOperand LHS, SDOperand RHS);
223
Chris Lattner40c62d52005-10-18 06:04:22 +0000224 bool SimplifySelectOps(SDNode *SELECT, SDOperand LHS, SDOperand RHS);
Nate Begeman44728a72005-09-19 22:34:01 +0000225 SDOperand SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2);
226 SDOperand SimplifySelectCC(SDOperand N0, SDOperand N1, SDOperand N2,
227 SDOperand N3, ISD::CondCode CC);
Nate Begeman452d7be2005-09-16 00:54:12 +0000228 SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N0, SDOperand N1,
Nate Begemane17daeb2005-10-05 21:43:42 +0000229 ISD::CondCode Cond, bool foldBooleans = true);
Chris Lattner6258fb22006-04-02 02:53:43 +0000230 SDOperand ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(SDNode *, MVT::ValueType);
Nate Begeman69575232005-10-20 02:15:44 +0000231 SDOperand BuildSDIV(SDNode *N);
232 SDOperand BuildUDIV(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000233public:
234 DAGCombiner(SelectionDAG &D)
Nate Begeman646d7e22005-09-02 21:18:40 +0000235 : DAG(D), TLI(D.getTargetLoweringInfo()), AfterLegalize(false) {}
Nate Begeman1d4d4142005-09-01 00:19:25 +0000236
237 /// Run - runs the dag combiner on all nodes in the work list
Nate Begeman4ebd8052005-09-01 23:24:04 +0000238 void Run(bool RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000239 };
240}
241
Chris Lattner24664722006-03-01 04:53:38 +0000242//===----------------------------------------------------------------------===//
243// TargetLowering::DAGCombinerInfo implementation
244//===----------------------------------------------------------------------===//
245
246void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) {
247 ((DAGCombiner*)DC)->AddToWorkList(N);
248}
249
250SDOperand TargetLowering::DAGCombinerInfo::
251CombineTo(SDNode *N, const std::vector<SDOperand> &To) {
252 return ((DAGCombiner*)DC)->CombineTo(N, To);
253}
254
255SDOperand TargetLowering::DAGCombinerInfo::
256CombineTo(SDNode *N, SDOperand Res) {
257 return ((DAGCombiner*)DC)->CombineTo(N, Res);
258}
259
260
261SDOperand TargetLowering::DAGCombinerInfo::
262CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1) {
263 return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1);
264}
265
266
267
268
269//===----------------------------------------------------------------------===//
270
271
Nate Begeman69575232005-10-20 02:15:44 +0000272struct ms {
273 int64_t m; // magic number
274 int64_t s; // shift amount
275};
276
277struct mu {
278 uint64_t m; // magic number
279 int64_t a; // add indicator
280 int64_t s; // shift amount
281};
282
283/// magic - calculate the magic numbers required to codegen an integer sdiv as
284/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
285/// or -1.
286static ms magic32(int32_t d) {
287 int32_t p;
288 uint32_t ad, anc, delta, q1, r1, q2, r2, t;
289 const uint32_t two31 = 0x80000000U;
290 struct ms mag;
291
292 ad = abs(d);
293 t = two31 + ((uint32_t)d >> 31);
294 anc = t - 1 - t%ad; // absolute value of nc
295 p = 31; // initialize p
296 q1 = two31/anc; // initialize q1 = 2p/abs(nc)
297 r1 = two31 - q1*anc; // initialize r1 = rem(2p,abs(nc))
298 q2 = two31/ad; // initialize q2 = 2p/abs(d)
299 r2 = two31 - q2*ad; // initialize r2 = rem(2p,abs(d))
300 do {
301 p = p + 1;
302 q1 = 2*q1; // update q1 = 2p/abs(nc)
303 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
304 if (r1 >= anc) { // must be unsigned comparison
305 q1 = q1 + 1;
306 r1 = r1 - anc;
307 }
308 q2 = 2*q2; // update q2 = 2p/abs(d)
309 r2 = 2*r2; // update r2 = rem(2p/abs(d))
310 if (r2 >= ad) { // must be unsigned comparison
311 q2 = q2 + 1;
312 r2 = r2 - ad;
313 }
314 delta = ad - r2;
315 } while (q1 < delta || (q1 == delta && r1 == 0));
316
317 mag.m = (int32_t)(q2 + 1); // make sure to sign extend
318 if (d < 0) mag.m = -mag.m; // resulting magic number
319 mag.s = p - 32; // resulting shift
320 return mag;
321}
322
323/// magicu - calculate the magic numbers required to codegen an integer udiv as
324/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
325static mu magicu32(uint32_t d) {
326 int32_t p;
327 uint32_t nc, delta, q1, r1, q2, r2;
328 struct mu magu;
329 magu.a = 0; // initialize "add" indicator
330 nc = - 1 - (-d)%d;
331 p = 31; // initialize p
332 q1 = 0x80000000/nc; // initialize q1 = 2p/nc
333 r1 = 0x80000000 - q1*nc; // initialize r1 = rem(2p,nc)
334 q2 = 0x7FFFFFFF/d; // initialize q2 = (2p-1)/d
335 r2 = 0x7FFFFFFF - q2*d; // initialize r2 = rem((2p-1),d)
336 do {
337 p = p + 1;
338 if (r1 >= nc - r1 ) {
339 q1 = 2*q1 + 1; // update q1
340 r1 = 2*r1 - nc; // update r1
341 }
342 else {
343 q1 = 2*q1; // update q1
344 r1 = 2*r1; // update r1
345 }
346 if (r2 + 1 >= d - r2) {
347 if (q2 >= 0x7FFFFFFF) magu.a = 1;
348 q2 = 2*q2 + 1; // update q2
349 r2 = 2*r2 + 1 - d; // update r2
350 }
351 else {
352 if (q2 >= 0x80000000) magu.a = 1;
353 q2 = 2*q2; // update q2
354 r2 = 2*r2 + 1; // update r2
355 }
356 delta = d - 1 - r2;
357 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
358 magu.m = q2 + 1; // resulting magic number
359 magu.s = p - 32; // resulting shift
360 return magu;
361}
362
363/// magic - calculate the magic numbers required to codegen an integer sdiv as
364/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
365/// or -1.
366static ms magic64(int64_t d) {
367 int64_t p;
368 uint64_t ad, anc, delta, q1, r1, q2, r2, t;
369 const uint64_t two63 = 9223372036854775808ULL; // 2^63
370 struct ms mag;
371
Chris Lattnerf75f2a02005-10-20 17:01:00 +0000372 ad = d >= 0 ? d : -d;
Nate Begeman69575232005-10-20 02:15:44 +0000373 t = two63 + ((uint64_t)d >> 63);
374 anc = t - 1 - t%ad; // absolute value of nc
375 p = 63; // initialize p
376 q1 = two63/anc; // initialize q1 = 2p/abs(nc)
377 r1 = two63 - q1*anc; // initialize r1 = rem(2p,abs(nc))
378 q2 = two63/ad; // initialize q2 = 2p/abs(d)
379 r2 = two63 - q2*ad; // initialize r2 = rem(2p,abs(d))
380 do {
381 p = p + 1;
382 q1 = 2*q1; // update q1 = 2p/abs(nc)
383 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
384 if (r1 >= anc) { // must be unsigned comparison
385 q1 = q1 + 1;
386 r1 = r1 - anc;
387 }
388 q2 = 2*q2; // update q2 = 2p/abs(d)
389 r2 = 2*r2; // update r2 = rem(2p/abs(d))
390 if (r2 >= ad) { // must be unsigned comparison
391 q2 = q2 + 1;
392 r2 = r2 - ad;
393 }
394 delta = ad - r2;
395 } while (q1 < delta || (q1 == delta && r1 == 0));
396
397 mag.m = q2 + 1;
398 if (d < 0) mag.m = -mag.m; // resulting magic number
399 mag.s = p - 64; // resulting shift
400 return mag;
401}
402
403/// magicu - calculate the magic numbers required to codegen an integer udiv as
404/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
405static mu magicu64(uint64_t d)
406{
407 int64_t p;
408 uint64_t nc, delta, q1, r1, q2, r2;
409 struct mu magu;
410 magu.a = 0; // initialize "add" indicator
411 nc = - 1 - (-d)%d;
412 p = 63; // initialize p
413 q1 = 0x8000000000000000ull/nc; // initialize q1 = 2p/nc
414 r1 = 0x8000000000000000ull - q1*nc; // initialize r1 = rem(2p,nc)
415 q2 = 0x7FFFFFFFFFFFFFFFull/d; // initialize q2 = (2p-1)/d
416 r2 = 0x7FFFFFFFFFFFFFFFull - q2*d; // initialize r2 = rem((2p-1),d)
417 do {
418 p = p + 1;
419 if (r1 >= nc - r1 ) {
420 q1 = 2*q1 + 1; // update q1
421 r1 = 2*r1 - nc; // update r1
422 }
423 else {
424 q1 = 2*q1; // update q1
425 r1 = 2*r1; // update r1
426 }
427 if (r2 + 1 >= d - r2) {
428 if (q2 >= 0x7FFFFFFFFFFFFFFFull) magu.a = 1;
429 q2 = 2*q2 + 1; // update q2
430 r2 = 2*r2 + 1 - d; // update r2
431 }
432 else {
433 if (q2 >= 0x8000000000000000ull) magu.a = 1;
434 q2 = 2*q2; // update q2
435 r2 = 2*r2 + 1; // update r2
436 }
437 delta = d - 1 - r2;
438 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
439 magu.m = q2 + 1; // resulting magic number
440 magu.s = p - 64; // resulting shift
441 return magu;
442}
443
Nate Begeman4ebd8052005-09-01 23:24:04 +0000444// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
445// that selects between the values 1 and 0, making it equivalent to a setcc.
Nate Begeman646d7e22005-09-02 21:18:40 +0000446// Also, set the incoming LHS, RHS, and CC references to the appropriate
447// nodes based on the type of node we are checking. This simplifies life a
448// bit for the callers.
449static bool isSetCCEquivalent(SDOperand N, SDOperand &LHS, SDOperand &RHS,
450 SDOperand &CC) {
451 if (N.getOpcode() == ISD::SETCC) {
452 LHS = N.getOperand(0);
453 RHS = N.getOperand(1);
454 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000455 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000456 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000457 if (N.getOpcode() == ISD::SELECT_CC &&
458 N.getOperand(2).getOpcode() == ISD::Constant &&
459 N.getOperand(3).getOpcode() == ISD::Constant &&
460 cast<ConstantSDNode>(N.getOperand(2))->getValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000461 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
462 LHS = N.getOperand(0);
463 RHS = N.getOperand(1);
464 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000465 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000466 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000467 return false;
468}
469
Nate Begeman99801192005-09-07 23:25:52 +0000470// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
471// one use. If this is true, it allows the users to invert the operation for
472// free when it is profitable to do so.
473static bool isOneUseSetCC(SDOperand N) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000474 SDOperand N0, N1, N2;
Nate Begeman646d7e22005-09-02 21:18:40 +0000475 if (isSetCCEquivalent(N, N0, N1, N2) && N.Val->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000476 return true;
477 return false;
478}
479
Nate Begeman452d7be2005-09-16 00:54:12 +0000480// FIXME: This should probably go in the ISD class rather than being duplicated
481// in several files.
482static bool isCommutativeBinOp(unsigned Opcode) {
483 switch (Opcode) {
484 case ISD::ADD:
485 case ISD::MUL:
486 case ISD::AND:
487 case ISD::OR:
488 case ISD::XOR: return true;
489 default: return false; // FIXME: Need commutative info for user ops!
490 }
491}
492
Nate Begemancd4d58c2006-02-03 06:46:56 +0000493SDOperand DAGCombiner::ReassociateOps(unsigned Opc, SDOperand N0, SDOperand N1){
494 MVT::ValueType VT = N0.getValueType();
495 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
496 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
497 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
498 if (isa<ConstantSDNode>(N1)) {
499 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(1), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000500 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000501 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(0));
502 } else if (N0.hasOneUse()) {
503 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(0), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000504 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000505 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(1));
506 }
507 }
508 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
509 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
510 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
511 if (isa<ConstantSDNode>(N0)) {
512 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(1), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000513 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000514 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(0));
515 } else if (N1.hasOneUse()) {
516 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(0), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000517 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000518 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(1));
519 }
520 }
521 return SDOperand();
522}
523
Nate Begeman4ebd8052005-09-01 23:24:04 +0000524void DAGCombiner::Run(bool RunningAfterLegalize) {
525 // set the instance variable, so that the various visit routines may use it.
526 AfterLegalize = RunningAfterLegalize;
527
Nate Begeman646d7e22005-09-02 21:18:40 +0000528 // Add all the dag nodes to the worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000529 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
530 E = DAG.allnodes_end(); I != E; ++I)
531 WorkList.push_back(I);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000532
Chris Lattner95038592005-10-05 06:35:28 +0000533 // Create a dummy node (which is not added to allnodes), that adds a reference
534 // to the root node, preventing it from being deleted, and tracking any
535 // changes of the root.
536 HandleSDNode Dummy(DAG.getRoot());
537
Chris Lattner24664722006-03-01 04:53:38 +0000538
539 /// DagCombineInfo - Expose the DAG combiner to the target combiner impls.
540 TargetLowering::DAGCombinerInfo
541 DagCombineInfo(DAG, !RunningAfterLegalize, this);
542
Nate Begeman1d4d4142005-09-01 00:19:25 +0000543 // while the worklist isn't empty, inspect the node on the end of it and
544 // try and combine it.
545 while (!WorkList.empty()) {
546 SDNode *N = WorkList.back();
547 WorkList.pop_back();
548
549 // If N has no uses, it is dead. Make sure to revisit all N's operands once
Chris Lattner95038592005-10-05 06:35:28 +0000550 // N is deleted from the DAG, since they too may now be dead or may have a
551 // reduced number of uses, allowing other xforms.
552 if (N->use_empty() && N != &Dummy) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000553 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
554 WorkList.push_back(N->getOperand(i).Val);
555
Nate Begeman1d4d4142005-09-01 00:19:25 +0000556 removeFromWorkList(N);
Chris Lattner95038592005-10-05 06:35:28 +0000557 DAG.DeleteNode(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000558 continue;
559 }
560
Nate Begeman83e75ec2005-09-06 04:43:02 +0000561 SDOperand RV = visit(N);
Chris Lattner24664722006-03-01 04:53:38 +0000562
563 // If nothing happened, try a target-specific DAG combine.
564 if (RV.Val == 0) {
565 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
566 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode()))
567 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
568 }
569
Nate Begeman83e75ec2005-09-06 04:43:02 +0000570 if (RV.Val) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000571 ++NodesCombined;
Nate Begeman646d7e22005-09-02 21:18:40 +0000572 // If we get back the same node we passed in, rather than a new node or
573 // zero, we know that the node must have defined multiple values and
574 // CombineTo was used. Since CombineTo takes care of the worklist
575 // mechanics for us, we have no work to do in this case.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000576 if (RV.Val != N) {
Nate Begeman2300f552005-09-07 00:15:36 +0000577 DEBUG(std::cerr << "\nReplacing "; N->dump();
578 std::cerr << "\nWith: "; RV.Val->dump();
579 std::cerr << '\n');
Chris Lattner01a22022005-10-10 22:04:48 +0000580 std::vector<SDNode*> NowDead;
581 DAG.ReplaceAllUsesWith(N, std::vector<SDOperand>(1, RV), &NowDead);
Nate Begeman646d7e22005-09-02 21:18:40 +0000582
583 // Push the new node and any users onto the worklist
Nate Begeman83e75ec2005-09-06 04:43:02 +0000584 WorkList.push_back(RV.Val);
585 AddUsersToWorkList(RV.Val);
Nate Begeman646d7e22005-09-02 21:18:40 +0000586
587 // Nodes can end up on the worklist more than once. Make sure we do
588 // not process a node that has been replaced.
589 removeFromWorkList(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000590 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
591 removeFromWorkList(NowDead[i]);
Chris Lattner5c46f742005-10-05 06:11:08 +0000592
593 // Finally, since the node is now dead, remove it from the graph.
594 DAG.DeleteNode(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000595 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000596 }
597 }
Chris Lattner95038592005-10-05 06:35:28 +0000598
599 // If the root changed (e.g. it was a dead load, update the root).
600 DAG.setRoot(Dummy.getValue());
Nate Begeman1d4d4142005-09-01 00:19:25 +0000601}
602
Nate Begeman83e75ec2005-09-06 04:43:02 +0000603SDOperand DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000604 switch(N->getOpcode()) {
605 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +0000606 case ISD::TokenFactor: return visitTokenFactor(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000607 case ISD::ADD: return visitADD(N);
608 case ISD::SUB: return visitSUB(N);
609 case ISD::MUL: return visitMUL(N);
610 case ISD::SDIV: return visitSDIV(N);
611 case ISD::UDIV: return visitUDIV(N);
612 case ISD::SREM: return visitSREM(N);
613 case ISD::UREM: return visitUREM(N);
614 case ISD::MULHU: return visitMULHU(N);
615 case ISD::MULHS: return visitMULHS(N);
616 case ISD::AND: return visitAND(N);
617 case ISD::OR: return visitOR(N);
618 case ISD::XOR: return visitXOR(N);
619 case ISD::SHL: return visitSHL(N);
620 case ISD::SRA: return visitSRA(N);
621 case ISD::SRL: return visitSRL(N);
622 case ISD::CTLZ: return visitCTLZ(N);
623 case ISD::CTTZ: return visitCTTZ(N);
624 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000625 case ISD::SELECT: return visitSELECT(N);
626 case ISD::SELECT_CC: return visitSELECT_CC(N);
627 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000628 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
629 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
630 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
631 case ISD::TRUNCATE: return visitTRUNCATE(N);
Chris Lattner94683772005-12-23 05:30:37 +0000632 case ISD::BIT_CONVERT: return visitBIT_CONVERT(N);
Chris Lattner6258fb22006-04-02 02:53:43 +0000633 case ISD::VBIT_CONVERT: return visitVBIT_CONVERT(N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000634 case ISD::FADD: return visitFADD(N);
635 case ISD::FSUB: return visitFSUB(N);
636 case ISD::FMUL: return visitFMUL(N);
637 case ISD::FDIV: return visitFDIV(N);
638 case ISD::FREM: return visitFREM(N);
Chris Lattner12d83032006-03-05 05:30:57 +0000639 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000640 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
641 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
642 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
643 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
644 case ISD::FP_ROUND: return visitFP_ROUND(N);
645 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
646 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
647 case ISD::FNEG: return visitFNEG(N);
648 case ISD::FABS: return visitFABS(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000649 case ISD::BRCOND: return visitBRCOND(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000650 case ISD::BR_CC: return visitBR_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000651 case ISD::LOAD: return visitLOAD(N);
Chris Lattner29cd7db2006-03-31 18:10:41 +0000652 case ISD::EXTLOAD:
653 case ISD::SEXTLOAD:
654 case ISD::ZEXTLOAD: return visitXEXTLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000655 case ISD::STORE: return visitSTORE(N);
Chris Lattnerca242442006-03-19 01:27:56 +0000656 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
657 case ISD::VINSERT_VECTOR_ELT: return visitVINSERT_VECTOR_ELT(N);
Chris Lattnerd7648c82006-03-28 20:28:38 +0000658 case ISD::VBUILD_VECTOR: return visitVBUILD_VECTOR(N);
Chris Lattner66445d32006-03-28 22:11:53 +0000659 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
Chris Lattnerf1d0c622006-03-31 22:16:43 +0000660 case ISD::VVECTOR_SHUFFLE: return visitVVECTOR_SHUFFLE(N);
Chris Lattneredab1b92006-04-02 03:25:57 +0000661 case ISD::VADD: return visitVBinOp(N, ISD::ADD , ISD::FADD);
662 case ISD::VSUB: return visitVBinOp(N, ISD::SUB , ISD::FSUB);
663 case ISD::VMUL: return visitVBinOp(N, ISD::MUL , ISD::FMUL);
664 case ISD::VSDIV: return visitVBinOp(N, ISD::SDIV, ISD::FDIV);
665 case ISD::VUDIV: return visitVBinOp(N, ISD::UDIV, ISD::UDIV);
666 case ISD::VAND: return visitVBinOp(N, ISD::AND , ISD::AND);
667 case ISD::VOR: return visitVBinOp(N, ISD::OR , ISD::OR);
668 case ISD::VXOR: return visitVBinOp(N, ISD::XOR , ISD::XOR);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000669 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000670 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000671}
672
Nate Begeman83e75ec2005-09-06 04:43:02 +0000673SDOperand DAGCombiner::visitTokenFactor(SDNode *N) {
Nate Begemanded49632005-10-13 03:11:28 +0000674 std::vector<SDOperand> Ops;
675 bool Changed = false;
676
Nate Begeman1d4d4142005-09-01 00:19:25 +0000677 // If the token factor has two operands and one is the entry token, replace
678 // the token factor with the other operand.
679 if (N->getNumOperands() == 2) {
680 if (N->getOperand(0).getOpcode() == ISD::EntryToken)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000681 return N->getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000682 if (N->getOperand(1).getOpcode() == ISD::EntryToken)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000683 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000684 }
Chris Lattner24edbb72005-10-13 22:10:05 +0000685
Nate Begemanded49632005-10-13 03:11:28 +0000686 // fold (tokenfactor (tokenfactor)) -> tokenfactor
687 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
688 SDOperand Op = N->getOperand(i);
689 if (Op.getOpcode() == ISD::TokenFactor && Op.hasOneUse()) {
Chris Lattnerac0f8f22006-03-13 18:37:30 +0000690 AddToWorkList(Op.Val); // Remove dead node.
Nate Begemanded49632005-10-13 03:11:28 +0000691 Changed = true;
692 for (unsigned j = 0, e = Op.getNumOperands(); j != e; ++j)
693 Ops.push_back(Op.getOperand(j));
694 } else {
695 Ops.push_back(Op);
696 }
697 }
698 if (Changed)
699 return DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000700 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000701}
702
Nate Begeman83e75ec2005-09-06 04:43:02 +0000703SDOperand DAGCombiner::visitADD(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000704 SDOperand N0 = N->getOperand(0);
705 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000706 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
707 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemanf89d78d2005-09-07 16:09:19 +0000708 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000709
710 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000711 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000712 return DAG.getNode(ISD::ADD, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000713 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000714 if (N0C && !N1C)
715 return DAG.getNode(ISD::ADD, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000716 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000717 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000718 return N0;
Chris Lattner4aafb4f2006-01-12 20:22:43 +0000719 // fold ((c1-A)+c2) -> (c1+c2)-A
720 if (N1C && N0.getOpcode() == ISD::SUB)
721 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
722 return DAG.getNode(ISD::SUB, VT,
723 DAG.getConstant(N1C->getValue()+N0C->getValue(), VT),
724 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000725 // reassociate add
726 SDOperand RADD = ReassociateOps(ISD::ADD, N0, N1);
727 if (RADD.Val != 0)
728 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000729 // fold ((0-A) + B) -> B-A
730 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
731 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000732 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000733 // fold (A + (0-B)) -> A-B
734 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
735 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000736 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +0000737 // fold (A+(B-A)) -> B
738 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000739 return N1.getOperand(0);
Chris Lattner947c2892006-03-13 06:51:27 +0000740
Evan Cheng860771d2006-03-01 01:09:54 +0000741 if (!MVT::isVector(VT) && SimplifyDemandedBits(SDOperand(N, 0)))
Nate Begemanb0d04a72006-02-18 02:40:58 +0000742 return SDOperand();
Chris Lattner947c2892006-03-13 06:51:27 +0000743
744 // fold (a+b) -> (a|b) iff a and b share no bits.
745 if (MVT::isInteger(VT) && !MVT::isVector(VT)) {
746 uint64_t LHSZero, LHSOne;
747 uint64_t RHSZero, RHSOne;
748 uint64_t Mask = MVT::getIntVTBitMask(VT);
749 TLI.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
750 if (LHSZero) {
751 TLI.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
752
753 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
754 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
755 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
756 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
757 return DAG.getNode(ISD::OR, VT, N0, N1);
758 }
759 }
760
Nate Begeman83e75ec2005-09-06 04:43:02 +0000761 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000762}
763
Nate Begeman83e75ec2005-09-06 04:43:02 +0000764SDOperand DAGCombiner::visitSUB(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000765 SDOperand N0 = N->getOperand(0);
766 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000767 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
768 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000769 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000770
Chris Lattner854077d2005-10-17 01:07:11 +0000771 // fold (sub x, x) -> 0
772 if (N0 == N1)
773 return DAG.getConstant(0, N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000774 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000775 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000776 return DAG.getNode(ISD::SUB, VT, N0, N1);
Chris Lattner05b57432005-10-11 06:07:15 +0000777 // fold (sub x, c) -> (add x, -c)
778 if (N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000779 return DAG.getNode(ISD::ADD, VT, N0, DAG.getConstant(-N1C->getValue(), VT));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000780 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +0000781 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000782 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000783 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +0000784 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000785 return N0.getOperand(0);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000786 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000787}
788
Nate Begeman83e75ec2005-09-06 04:43:02 +0000789SDOperand DAGCombiner::visitMUL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000790 SDOperand N0 = N->getOperand(0);
791 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000792 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
793 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman223df222005-09-08 20:18:10 +0000794 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000795
796 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000797 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000798 return DAG.getNode(ISD::MUL, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000799 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000800 if (N0C && !N1C)
801 return DAG.getNode(ISD::MUL, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000802 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000803 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000804 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000805 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +0000806 if (N1C && N1C->isAllOnesValue())
Nate Begeman405e3ec2005-10-21 00:02:42 +0000807 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000808 // fold (mul x, (1 << c)) -> x << c
Nate Begeman646d7e22005-09-02 21:18:40 +0000809 if (N1C && isPowerOf2_64(N1C->getValue()))
Chris Lattner3e6099b2005-10-30 06:41:49 +0000810 return DAG.getNode(ISD::SHL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000811 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000812 TLI.getShiftAmountTy()));
Chris Lattner3e6099b2005-10-30 06:41:49 +0000813 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
814 if (N1C && isPowerOf2_64(-N1C->getSignExtended())) {
815 // FIXME: If the input is something that is easily negated (e.g. a
816 // single-use add), we should put the negate there.
817 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT),
818 DAG.getNode(ISD::SHL, VT, N0,
819 DAG.getConstant(Log2_64(-N1C->getSignExtended()),
820 TLI.getShiftAmountTy())));
821 }
Andrew Lenharth50a0d422006-04-02 21:42:45 +0000822
Chris Lattner0b1a85f2006-03-01 03:44:24 +0000823 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
824 if (N1C && N0.getOpcode() == ISD::SHL &&
825 isa<ConstantSDNode>(N0.getOperand(1))) {
826 SDOperand C3 = DAG.getNode(ISD::SHL, VT, N1, N0.getOperand(1));
Chris Lattner5750df92006-03-01 04:03:14 +0000827 AddToWorkList(C3.Val);
Chris Lattner0b1a85f2006-03-01 03:44:24 +0000828 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0), C3);
829 }
830
831 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
832 // use.
833 {
834 SDOperand Sh(0,0), Y(0,0);
835 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
836 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
837 N0.Val->hasOneUse()) {
838 Sh = N0; Y = N1;
839 } else if (N1.getOpcode() == ISD::SHL &&
840 isa<ConstantSDNode>(N1.getOperand(1)) && N1.Val->hasOneUse()) {
841 Sh = N1; Y = N0;
842 }
843 if (Sh.Val) {
844 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Sh.getOperand(0), Y);
845 return DAG.getNode(ISD::SHL, VT, Mul, Sh.getOperand(1));
846 }
847 }
Chris Lattnera1deca32006-03-04 23:33:26 +0000848 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
849 if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() &&
850 isa<ConstantSDNode>(N0.getOperand(1))) {
851 return DAG.getNode(ISD::ADD, VT,
852 DAG.getNode(ISD::MUL, VT, N0.getOperand(0), N1),
853 DAG.getNode(ISD::MUL, VT, N0.getOperand(1), N1));
854 }
Chris Lattner0b1a85f2006-03-01 03:44:24 +0000855
Nate Begemancd4d58c2006-02-03 06:46:56 +0000856 // reassociate mul
857 SDOperand RMUL = ReassociateOps(ISD::MUL, N0, N1);
858 if (RMUL.Val != 0)
859 return RMUL;
Nate Begeman83e75ec2005-09-06 04:43:02 +0000860 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000861}
862
Nate Begeman83e75ec2005-09-06 04:43:02 +0000863SDOperand DAGCombiner::visitSDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000864 SDOperand N0 = N->getOperand(0);
865 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000866 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
867 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000868 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000869
870 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000871 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000872 return DAG.getNode(ISD::SDIV, VT, N0, N1);
Nate Begeman405e3ec2005-10-21 00:02:42 +0000873 // fold (sdiv X, 1) -> X
874 if (N1C && N1C->getSignExtended() == 1LL)
875 return N0;
876 // fold (sdiv X, -1) -> 0-X
877 if (N1C && N1C->isAllOnesValue())
878 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +0000879 // If we know the sign bits of both operands are zero, strength reduce to a
880 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
881 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000882 if (TLI.MaskedValueIsZero(N1, SignBit) &&
883 TLI.MaskedValueIsZero(N0, SignBit))
Chris Lattner094c8fc2005-10-07 06:10:46 +0000884 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
Nate Begemancd6a6ed2006-02-17 07:26:20 +0000885 // fold (sdiv X, pow2) -> simple ops after legalize
Nate Begemanfb7217b2006-02-17 19:54:08 +0000886 if (N1C && N1C->getValue() && !TLI.isIntDivCheap() &&
Nate Begeman405e3ec2005-10-21 00:02:42 +0000887 (isPowerOf2_64(N1C->getSignExtended()) ||
888 isPowerOf2_64(-N1C->getSignExtended()))) {
889 // If dividing by powers of two is cheap, then don't perform the following
890 // fold.
891 if (TLI.isPow2DivCheap())
892 return SDOperand();
893 int64_t pow2 = N1C->getSignExtended();
894 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
Chris Lattner8f4880b2006-02-16 08:02:36 +0000895 unsigned lg2 = Log2_64(abs2);
896 // Splat the sign bit into the register
897 SDOperand SGN = DAG.getNode(ISD::SRA, VT, N0,
Nate Begeman405e3ec2005-10-21 00:02:42 +0000898 DAG.getConstant(MVT::getSizeInBits(VT)-1,
899 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +0000900 AddToWorkList(SGN.Val);
Chris Lattner8f4880b2006-02-16 08:02:36 +0000901 // Add (N0 < 0) ? abs2 - 1 : 0;
902 SDOperand SRL = DAG.getNode(ISD::SRL, VT, SGN,
903 DAG.getConstant(MVT::getSizeInBits(VT)-lg2,
Nate Begeman405e3ec2005-10-21 00:02:42 +0000904 TLI.getShiftAmountTy()));
Chris Lattner8f4880b2006-02-16 08:02:36 +0000905 SDOperand ADD = DAG.getNode(ISD::ADD, VT, N0, SRL);
Chris Lattner5750df92006-03-01 04:03:14 +0000906 AddToWorkList(SRL.Val);
907 AddToWorkList(ADD.Val); // Divide by pow2
Chris Lattner8f4880b2006-02-16 08:02:36 +0000908 SDOperand SRA = DAG.getNode(ISD::SRA, VT, ADD,
909 DAG.getConstant(lg2, TLI.getShiftAmountTy()));
Nate Begeman405e3ec2005-10-21 00:02:42 +0000910 // If we're dividing by a positive value, we're done. Otherwise, we must
911 // negate the result.
912 if (pow2 > 0)
913 return SRA;
Chris Lattner5750df92006-03-01 04:03:14 +0000914 AddToWorkList(SRA.Val);
Nate Begeman405e3ec2005-10-21 00:02:42 +0000915 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), SRA);
916 }
Nate Begeman69575232005-10-20 02:15:44 +0000917 // if integer divide is expensive and we satisfy the requirements, emit an
918 // alternate sequence.
Nate Begeman405e3ec2005-10-21 00:02:42 +0000919 if (N1C && (N1C->getSignExtended() < -1 || N1C->getSignExtended() > 1) &&
Chris Lattnere9936d12005-10-22 18:50:15 +0000920 !TLI.isIntDivCheap()) {
921 SDOperand Op = BuildSDIV(N);
922 if (Op.Val) return Op;
Nate Begeman69575232005-10-20 02:15:44 +0000923 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000924 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000925}
926
Nate Begeman83e75ec2005-09-06 04:43:02 +0000927SDOperand DAGCombiner::visitUDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000928 SDOperand N0 = N->getOperand(0);
929 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000930 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
931 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000932 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000933
934 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000935 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000936 return DAG.getNode(ISD::UDIV, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000937 // fold (udiv x, (1 << c)) -> x >>u c
Nate Begeman646d7e22005-09-02 21:18:40 +0000938 if (N1C && isPowerOf2_64(N1C->getValue()))
Nate Begemanfb5e4bd2006-02-05 07:20:23 +0000939 return DAG.getNode(ISD::SRL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000940 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000941 TLI.getShiftAmountTy()));
Nate Begemanfb5e4bd2006-02-05 07:20:23 +0000942 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
943 if (N1.getOpcode() == ISD::SHL) {
944 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
945 if (isPowerOf2_64(SHC->getValue())) {
946 MVT::ValueType ADDVT = N1.getOperand(1).getValueType();
Nate Begemanc031e332006-02-05 07:36:48 +0000947 SDOperand Add = DAG.getNode(ISD::ADD, ADDVT, N1.getOperand(1),
948 DAG.getConstant(Log2_64(SHC->getValue()),
949 ADDVT));
Chris Lattner5750df92006-03-01 04:03:14 +0000950 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +0000951 return DAG.getNode(ISD::SRL, VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +0000952 }
953 }
954 }
Nate Begeman69575232005-10-20 02:15:44 +0000955 // fold (udiv x, c) -> alternate
Chris Lattnere9936d12005-10-22 18:50:15 +0000956 if (N1C && N1C->getValue() && !TLI.isIntDivCheap()) {
957 SDOperand Op = BuildUDIV(N);
958 if (Op.Val) return Op;
959 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000960 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000961}
962
Nate Begeman83e75ec2005-09-06 04:43:02 +0000963SDOperand DAGCombiner::visitSREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000964 SDOperand N0 = N->getOperand(0);
965 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000966 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
967 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +0000968 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000969
970 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000971 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000972 return DAG.getNode(ISD::SREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +0000973 // If we know the sign bits of both operands are zero, strength reduce to a
974 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
975 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000976 if (TLI.MaskedValueIsZero(N1, SignBit) &&
977 TLI.MaskedValueIsZero(N0, SignBit))
Nate Begemana148d982006-01-18 22:35:16 +0000978 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000979 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000980}
981
Nate Begeman83e75ec2005-09-06 04:43:02 +0000982SDOperand DAGCombiner::visitUREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000983 SDOperand N0 = N->getOperand(0);
984 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000985 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
986 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +0000987 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000988
989 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000990 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000991 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +0000992 // fold (urem x, pow2) -> (and x, pow2-1)
993 if (N1C && !N1C->isNullValue() && isPowerOf2_64(N1C->getValue()))
Nate Begemana148d982006-01-18 22:35:16 +0000994 return DAG.getNode(ISD::AND, VT, N0, DAG.getConstant(N1C->getValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +0000995 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
996 if (N1.getOpcode() == ISD::SHL) {
997 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
998 if (isPowerOf2_64(SHC->getValue())) {
Nate Begemanbab92392006-02-05 08:07:24 +0000999 SDOperand Add = DAG.getNode(ISD::ADD, VT, N1,DAG.getConstant(~0ULL,VT));
Chris Lattner5750df92006-03-01 04:03:14 +00001000 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +00001001 return DAG.getNode(ISD::AND, VT, N0, Add);
1002 }
1003 }
1004 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001005 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001006}
1007
Nate Begeman83e75ec2005-09-06 04:43:02 +00001008SDOperand DAGCombiner::visitMULHS(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001009 SDOperand N0 = N->getOperand(0);
1010 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001011 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001012
1013 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001014 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001015 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001016 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001017 if (N1C && N1C->getValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001018 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
1019 DAG.getConstant(MVT::getSizeInBits(N0.getValueType())-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001020 TLI.getShiftAmountTy()));
1021 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001022}
1023
Nate Begeman83e75ec2005-09-06 04:43:02 +00001024SDOperand DAGCombiner::visitMULHU(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001025 SDOperand N0 = N->getOperand(0);
1026 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001027 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001028
1029 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001030 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001031 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001032 // fold (mulhu x, 1) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001033 if (N1C && N1C->getValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001034 return DAG.getConstant(0, N0.getValueType());
1035 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001036}
1037
Nate Begeman83e75ec2005-09-06 04:43:02 +00001038SDOperand DAGCombiner::visitAND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001039 SDOperand N0 = N->getOperand(0);
1040 SDOperand N1 = N->getOperand(1);
Nate Begemanfb7217b2006-02-17 19:54:08 +00001041 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001042 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1043 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001044 MVT::ValueType VT = N1.getValueType();
Nate Begeman83e75ec2005-09-06 04:43:02 +00001045 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001046
1047 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001048 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001049 return DAG.getNode(ISD::AND, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001050 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001051 if (N0C && !N1C)
1052 return DAG.getNode(ISD::AND, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001053 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001054 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001055 return N0;
1056 // if (and x, c) is known to be zero, return 0
Nate Begeman368e18d2006-02-16 21:11:51 +00001057 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001058 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +00001059 // reassociate and
1060 SDOperand RAND = ReassociateOps(ISD::AND, N0, N1);
1061 if (RAND.Val != 0)
1062 return RAND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001063 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
Nate Begeman5dc7e862005-11-02 18:42:59 +00001064 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001065 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Nate Begeman646d7e22005-09-02 21:18:40 +00001066 if ((ORI->getValue() & N1C->getValue()) == N1C->getValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001067 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +00001068 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
1069 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Chris Lattner1ec05d12006-03-01 21:47:21 +00001070 unsigned InMask = MVT::getIntVTBitMask(N0.getOperand(0).getValueType());
Chris Lattner3603cd62006-02-02 07:17:31 +00001071 if (TLI.MaskedValueIsZero(N0.getOperand(0),
Chris Lattner1ec05d12006-03-01 21:47:21 +00001072 ~N1C->getValue() & InMask)) {
1073 SDOperand Zext = DAG.getNode(ISD::ZERO_EXTEND, N0.getValueType(),
1074 N0.getOperand(0));
1075
1076 // Replace uses of the AND with uses of the Zero extend node.
1077 CombineTo(N, Zext);
1078
Chris Lattner3603cd62006-02-02 07:17:31 +00001079 // We actually want to replace all uses of the any_extend with the
1080 // zero_extend, to avoid duplicating things. This will later cause this
1081 // AND to be folded.
Chris Lattner1ec05d12006-03-01 21:47:21 +00001082 CombineTo(N0.Val, Zext);
Chris Lattnerfedced72006-04-20 23:55:59 +00001083 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner3603cd62006-02-02 07:17:31 +00001084 }
1085 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001086 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
1087 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1088 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1089 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1090
1091 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1092 MVT::isInteger(LL.getValueType())) {
1093 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
1094 if (cast<ConstantSDNode>(LR)->getValue() == 0 && Op1 == ISD::SETEQ) {
1095 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001096 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001097 return DAG.getSetCC(VT, ORNode, LR, Op1);
1098 }
1099 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
1100 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
1101 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001102 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001103 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1104 }
1105 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
1106 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
1107 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001108 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001109 return DAG.getSetCC(VT, ORNode, LR, Op1);
1110 }
1111 }
1112 // canonicalize equivalent to ll == rl
1113 if (LL == RR && LR == RL) {
1114 Op1 = ISD::getSetCCSwappedOperands(Op1);
1115 std::swap(RL, RR);
1116 }
1117 if (LL == RL && LR == RR) {
1118 bool isInteger = MVT::isInteger(LL.getValueType());
1119 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
1120 if (Result != ISD::SETCC_INVALID)
1121 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1122 }
1123 }
1124 // fold (and (zext x), (zext y)) -> (zext (and x, y))
1125 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
1126 N1.getOpcode() == ISD::ZERO_EXTEND &&
1127 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1128 SDOperand ANDNode = DAG.getNode(ISD::AND, N0.getOperand(0).getValueType(),
1129 N0.getOperand(0), N1.getOperand(0));
Chris Lattner5750df92006-03-01 04:03:14 +00001130 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001131 return DAG.getNode(ISD::ZERO_EXTEND, VT, ANDNode);
1132 }
Nate Begeman61af66e2006-01-28 01:06:30 +00001133 // fold (and (shl/srl/sra x), (shl/srl/sra y)) -> (shl/srl/sra (and x, y))
Nate Begeman452d7be2005-09-16 00:54:12 +00001134 if (((N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SHL) ||
Nate Begeman61af66e2006-01-28 01:06:30 +00001135 (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SRL) ||
1136 (N0.getOpcode() == ISD::SRA && N1.getOpcode() == ISD::SRA)) &&
Nate Begeman452d7be2005-09-16 00:54:12 +00001137 N0.getOperand(1) == N1.getOperand(1)) {
1138 SDOperand ANDNode = DAG.getNode(ISD::AND, N0.getOperand(0).getValueType(),
1139 N0.getOperand(0), N1.getOperand(0));
Chris Lattner5750df92006-03-01 04:03:14 +00001140 AddToWorkList(ANDNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00001141 return DAG.getNode(N0.getOpcode(), VT, ANDNode, N0.getOperand(1));
1142 }
Nate Begemande996292006-02-03 22:24:05 +00001143 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
1144 // fold (and (sra)) -> (and (srl)) when possible.
Chris Lattner6ea2dee2006-03-25 22:19:00 +00001145 if (!MVT::isVector(VT) &&
1146 SimplifyDemandedBits(SDOperand(N, 0)))
Nate Begemande996292006-02-03 22:24:05 +00001147 return SDOperand();
Nate Begemanded49632005-10-13 03:11:28 +00001148 // fold (zext_inreg (extload x)) -> (zextload x)
Nate Begeman5054f162005-10-14 01:12:21 +00001149 if (N0.getOpcode() == ISD::EXTLOAD) {
Nate Begemanded49632005-10-13 03:11:28 +00001150 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001151 // If we zero all the possible extended bits, then we can turn this into
1152 // a zextload if we are running before legalize or the operation is legal.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001153 if (TLI.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Chris Lattner67a44cd2005-10-13 18:16:34 +00001154 (!AfterLegalize || TLI.isOperationLegal(ISD::ZEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001155 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1156 N0.getOperand(1), N0.getOperand(2),
1157 EVT);
Chris Lattner5750df92006-03-01 04:03:14 +00001158 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001159 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001160 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001161 }
1162 }
1163 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Chris Lattner40c62d52005-10-18 06:04:22 +00001164 if (N0.getOpcode() == ISD::SEXTLOAD && N0.hasOneUse()) {
Nate Begemanded49632005-10-13 03:11:28 +00001165 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001166 // If we zero all the possible extended bits, then we can turn this into
1167 // a zextload if we are running before legalize or the operation is legal.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001168 if (TLI.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Nate Begemanbfd65a02005-10-13 18:34:58 +00001169 (!AfterLegalize || TLI.isOperationLegal(ISD::ZEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001170 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1171 N0.getOperand(1), N0.getOperand(2),
1172 EVT);
Chris Lattner5750df92006-03-01 04:03:14 +00001173 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001174 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001175 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001176 }
1177 }
Chris Lattner15045b62006-02-28 06:35:35 +00001178
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001179 // fold (and (load x), 255) -> (zextload x, i8)
1180 // fold (and (extload x, i16), 255) -> (zextload x, i8)
1181 if (N1C &&
1182 (N0.getOpcode() == ISD::LOAD || N0.getOpcode() == ISD::EXTLOAD ||
1183 N0.getOpcode() == ISD::ZEXTLOAD) &&
1184 N0.hasOneUse()) {
1185 MVT::ValueType EVT, LoadedVT;
Chris Lattner15045b62006-02-28 06:35:35 +00001186 if (N1C->getValue() == 255)
1187 EVT = MVT::i8;
1188 else if (N1C->getValue() == 65535)
1189 EVT = MVT::i16;
1190 else if (N1C->getValue() == ~0U)
1191 EVT = MVT::i32;
1192 else
1193 EVT = MVT::Other;
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001194
1195 LoadedVT = N0.getOpcode() == ISD::LOAD ? VT :
1196 cast<VTSDNode>(N0.getOperand(3))->getVT();
Chris Lattnere44be602006-04-04 17:39:18 +00001197 if (EVT != MVT::Other && LoadedVT > EVT &&
1198 (!AfterLegalize || TLI.isOperationLegal(ISD::ZEXTLOAD, EVT))) {
Chris Lattner15045b62006-02-28 06:35:35 +00001199 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
1200 // For big endian targets, we need to add an offset to the pointer to load
1201 // the correct bytes. For little endian systems, we merely need to read
1202 // fewer bytes from the same pointer.
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001203 unsigned PtrOff =
1204 (MVT::getSizeInBits(LoadedVT) - MVT::getSizeInBits(EVT)) / 8;
1205 SDOperand NewPtr = N0.getOperand(1);
1206 if (!TLI.isLittleEndian())
1207 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
1208 DAG.getConstant(PtrOff, PtrType));
Chris Lattner5750df92006-03-01 04:03:14 +00001209 AddToWorkList(NewPtr.Val);
Chris Lattner15045b62006-02-28 06:35:35 +00001210 SDOperand Load =
1211 DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0), NewPtr,
1212 N0.getOperand(2), EVT);
Chris Lattner5750df92006-03-01 04:03:14 +00001213 AddToWorkList(N);
Chris Lattner15045b62006-02-28 06:35:35 +00001214 CombineTo(N0.Val, Load, Load.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001215 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner15045b62006-02-28 06:35:35 +00001216 }
1217 }
1218
Nate Begeman83e75ec2005-09-06 04:43:02 +00001219 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001220}
1221
Nate Begeman83e75ec2005-09-06 04:43:02 +00001222SDOperand DAGCombiner::visitOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001223 SDOperand N0 = N->getOperand(0);
1224 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001225 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001226 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1227 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001228 MVT::ValueType VT = N1.getValueType();
1229 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001230
1231 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001232 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001233 return DAG.getNode(ISD::OR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001234 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001235 if (N0C && !N1C)
1236 return DAG.getNode(ISD::OR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001237 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001238 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001239 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001240 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001241 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001242 return N1;
1243 // fold (or x, c) -> c iff (x & ~c) == 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001244 if (N1C &&
1245 TLI.MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits))))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001246 return N1;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001247 // reassociate or
1248 SDOperand ROR = ReassociateOps(ISD::OR, N0, N1);
1249 if (ROR.Val != 0)
1250 return ROR;
1251 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
1252 if (N1C && N0.getOpcode() == ISD::AND && N0.Val->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00001253 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00001254 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
1255 return DAG.getNode(ISD::AND, VT, DAG.getNode(ISD::OR, VT, N0.getOperand(0),
1256 N1),
1257 DAG.getConstant(N1C->getValue() | C1->getValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00001258 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001259 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
1260 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1261 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1262 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1263
1264 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1265 MVT::isInteger(LL.getValueType())) {
1266 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
1267 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
1268 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
1269 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
1270 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001271 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001272 return DAG.getSetCC(VT, ORNode, LR, Op1);
1273 }
1274 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
1275 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
1276 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
1277 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
1278 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001279 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001280 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1281 }
1282 }
1283 // canonicalize equivalent to ll == rl
1284 if (LL == RR && LR == RL) {
1285 Op1 = ISD::getSetCCSwappedOperands(Op1);
1286 std::swap(RL, RR);
1287 }
1288 if (LL == RL && LR == RR) {
1289 bool isInteger = MVT::isInteger(LL.getValueType());
1290 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
1291 if (Result != ISD::SETCC_INVALID)
1292 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1293 }
1294 }
1295 // fold (or (zext x), (zext y)) -> (zext (or x, y))
1296 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
1297 N1.getOpcode() == ISD::ZERO_EXTEND &&
1298 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1299 SDOperand ORNode = DAG.getNode(ISD::OR, N0.getOperand(0).getValueType(),
1300 N0.getOperand(0), N1.getOperand(0));
Chris Lattner5750df92006-03-01 04:03:14 +00001301 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001302 return DAG.getNode(ISD::ZERO_EXTEND, VT, ORNode);
1303 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00001304 // fold (or (shl/srl/sra x), (shl/srl/sra y)) -> (shl/srl/sra (or x, y))
1305 if (((N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SHL) ||
1306 (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SRL) ||
1307 (N0.getOpcode() == ISD::SRA && N1.getOpcode() == ISD::SRA)) &&
1308 N0.getOperand(1) == N1.getOperand(1)) {
1309 SDOperand ORNode = DAG.getNode(ISD::OR, N0.getOperand(0).getValueType(),
1310 N0.getOperand(0), N1.getOperand(0));
Chris Lattner5750df92006-03-01 04:03:14 +00001311 AddToWorkList(ORNode.Val);
Nate Begeman750ac1b2006-02-01 07:19:44 +00001312 return DAG.getNode(N0.getOpcode(), VT, ORNode, N0.getOperand(1));
1313 }
Nate Begeman35ef9132006-01-11 21:21:00 +00001314 // canonicalize shl to left side in a shl/srl pair, to match rotate
1315 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
1316 std::swap(N0, N1);
1317 // check for rotl, rotr
1318 if (N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SRL &&
1319 N0.getOperand(0) == N1.getOperand(0) &&
Chris Lattneraf551bc2006-01-12 18:57:33 +00001320 TLI.isOperationLegal(ISD::ROTL, VT) && TLI.isTypeLegal(VT)) {
Nate Begeman35ef9132006-01-11 21:21:00 +00001321 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
1322 if (N0.getOperand(1).getOpcode() == ISD::Constant &&
1323 N1.getOperand(1).getOpcode() == ISD::Constant) {
1324 uint64_t c1val = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1325 uint64_t c2val = cast<ConstantSDNode>(N1.getOperand(1))->getValue();
1326 if ((c1val + c2val) == OpSizeInBits)
1327 return DAG.getNode(ISD::ROTL, VT, N0.getOperand(0), N0.getOperand(1));
1328 }
1329 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
1330 if (N1.getOperand(1).getOpcode() == ISD::SUB &&
1331 N0.getOperand(1) == N1.getOperand(1).getOperand(1))
1332 if (ConstantSDNode *SUBC =
1333 dyn_cast<ConstantSDNode>(N1.getOperand(1).getOperand(0)))
1334 if (SUBC->getValue() == OpSizeInBits)
1335 return DAG.getNode(ISD::ROTL, VT, N0.getOperand(0), N0.getOperand(1));
1336 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
1337 if (N0.getOperand(1).getOpcode() == ISD::SUB &&
1338 N1.getOperand(1) == N0.getOperand(1).getOperand(1))
1339 if (ConstantSDNode *SUBC =
1340 dyn_cast<ConstantSDNode>(N0.getOperand(1).getOperand(0)))
1341 if (SUBC->getValue() == OpSizeInBits) {
Chris Lattneraf551bc2006-01-12 18:57:33 +00001342 if (TLI.isOperationLegal(ISD::ROTR, VT) && TLI.isTypeLegal(VT))
Nate Begeman35ef9132006-01-11 21:21:00 +00001343 return DAG.getNode(ISD::ROTR, VT, N0.getOperand(0),
1344 N1.getOperand(1));
1345 else
1346 return DAG.getNode(ISD::ROTL, VT, N0.getOperand(0),
1347 N0.getOperand(1));
1348 }
1349 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001350 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001351}
1352
Nate Begeman83e75ec2005-09-06 04:43:02 +00001353SDOperand DAGCombiner::visitXOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001354 SDOperand N0 = N->getOperand(0);
1355 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001356 SDOperand LHS, RHS, CC;
1357 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1358 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001359 MVT::ValueType VT = N0.getValueType();
1360
1361 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001362 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001363 return DAG.getNode(ISD::XOR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001364 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001365 if (N0C && !N1C)
1366 return DAG.getNode(ISD::XOR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001367 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001368 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001369 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001370 // reassociate xor
1371 SDOperand RXOR = ReassociateOps(ISD::XOR, N0, N1);
1372 if (RXOR.Val != 0)
1373 return RXOR;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001374 // fold !(x cc y) -> (x !cc y)
Nate Begeman646d7e22005-09-02 21:18:40 +00001375 if (N1C && N1C->getValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
1376 bool isInt = MVT::isInteger(LHS.getValueType());
1377 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
1378 isInt);
1379 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001380 return DAG.getSetCC(VT, LHS, RHS, NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00001381 if (N0.getOpcode() == ISD::SELECT_CC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001382 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00001383 assert(0 && "Unhandled SetCC Equivalent!");
1384 abort();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001385 }
Nate Begeman99801192005-09-07 23:25:52 +00001386 // fold !(x or y) -> (!x and !y) iff x or y are setcc
1387 if (N1C && N1C->getValue() == 1 &&
1388 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001389 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00001390 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
1391 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001392 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1393 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00001394 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00001395 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001396 }
1397 }
Nate Begeman99801192005-09-07 23:25:52 +00001398 // fold !(x or y) -> (!x and !y) iff x or y are constants
1399 if (N1C && N1C->isAllOnesValue() &&
1400 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001401 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00001402 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
1403 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001404 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1405 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00001406 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00001407 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001408 }
1409 }
Nate Begeman223df222005-09-08 20:18:10 +00001410 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
1411 if (N1C && N0.getOpcode() == ISD::XOR) {
1412 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
1413 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
1414 if (N00C)
1415 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
1416 DAG.getConstant(N1C->getValue()^N00C->getValue(), VT));
1417 if (N01C)
1418 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
1419 DAG.getConstant(N1C->getValue()^N01C->getValue(), VT));
1420 }
1421 // fold (xor x, x) -> 0
Chris Lattner4fbdd592006-03-28 19:11:05 +00001422 if (N0 == N1) {
1423 if (!MVT::isVector(VT)) {
1424 return DAG.getConstant(0, VT);
1425 } else if (!AfterLegalize || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) {
1426 // Produce a vector of zeros.
1427 SDOperand El = DAG.getConstant(0, MVT::getVectorBaseType(VT));
1428 std::vector<SDOperand> Ops(MVT::getVectorNumElements(VT), El);
1429 return DAG.getNode(ISD::BUILD_VECTOR, VT, Ops);
1430 }
1431 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001432 // fold (xor (zext x), (zext y)) -> (zext (xor x, y))
1433 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
1434 N1.getOpcode() == ISD::ZERO_EXTEND &&
1435 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1436 SDOperand XORNode = DAG.getNode(ISD::XOR, N0.getOperand(0).getValueType(),
1437 N0.getOperand(0), N1.getOperand(0));
Chris Lattner5750df92006-03-01 04:03:14 +00001438 AddToWorkList(XORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001439 return DAG.getNode(ISD::ZERO_EXTEND, VT, XORNode);
1440 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00001441 // fold (xor (shl/srl/sra x), (shl/srl/sra y)) -> (shl/srl/sra (xor x, y))
1442 if (((N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SHL) ||
1443 (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SRL) ||
1444 (N0.getOpcode() == ISD::SRA && N1.getOpcode() == ISD::SRA)) &&
1445 N0.getOperand(1) == N1.getOperand(1)) {
1446 SDOperand XORNode = DAG.getNode(ISD::XOR, N0.getOperand(0).getValueType(),
1447 N0.getOperand(0), N1.getOperand(0));
Chris Lattner5750df92006-03-01 04:03:14 +00001448 AddToWorkList(XORNode.Val);
Nate Begeman750ac1b2006-02-01 07:19:44 +00001449 return DAG.getNode(N0.getOpcode(), VT, XORNode, N0.getOperand(1));
1450 }
Chris Lattner3e104b12006-04-08 04:15:24 +00001451
1452 // Simplify the expression using non-local knowledge.
1453 if (!MVT::isVector(VT) &&
1454 SimplifyDemandedBits(SDOperand(N, 0)))
1455 return SDOperand();
1456
Nate Begeman83e75ec2005-09-06 04:43:02 +00001457 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001458}
1459
Nate Begeman83e75ec2005-09-06 04:43:02 +00001460SDOperand DAGCombiner::visitSHL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001461 SDOperand N0 = N->getOperand(0);
1462 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001463 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1464 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001465 MVT::ValueType VT = N0.getValueType();
1466 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1467
1468 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001469 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001470 return DAG.getNode(ISD::SHL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001471 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001472 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001473 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001474 // fold (shl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001475 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001476 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001477 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001478 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001479 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001480 // if (shl x, c) is known to be zero, return 0
Nate Begemanfb7217b2006-02-17 19:54:08 +00001481 if (TLI.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001482 return DAG.getConstant(0, VT);
Chris Lattner012f2412006-02-17 21:58:01 +00001483 if (SimplifyDemandedBits(SDOperand(N, 0)))
Nate Begemande996292006-02-03 22:24:05 +00001484 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001485 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001486 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001487 N0.getOperand(1).getOpcode() == ISD::Constant) {
1488 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001489 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001490 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001491 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001492 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001493 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001494 }
1495 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
1496 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001497 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001498 N0.getOperand(1).getOpcode() == ISD::Constant) {
1499 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001500 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001501 SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
1502 DAG.getConstant(~0ULL << c1, VT));
1503 if (c2 > c1)
1504 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001505 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001506 else
Nate Begeman83e75ec2005-09-06 04:43:02 +00001507 return DAG.getNode(ISD::SRL, VT, Mask,
1508 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001509 }
1510 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001511 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +00001512 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001513 DAG.getConstant(~0ULL << N1C->getValue(), VT));
Chris Lattnercac70592006-03-05 19:53:55 +00001514 // fold (shl (add x, c1), c2) -> (add (shl x, c2), c1<<c2)
1515 if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() &&
1516 isa<ConstantSDNode>(N0.getOperand(1))) {
1517 return DAG.getNode(ISD::ADD, VT,
1518 DAG.getNode(ISD::SHL, VT, N0.getOperand(0), N1),
1519 DAG.getNode(ISD::SHL, VT, N0.getOperand(1), N1));
1520 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001521 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001522}
1523
Nate Begeman83e75ec2005-09-06 04:43:02 +00001524SDOperand DAGCombiner::visitSRA(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001525 SDOperand N0 = N->getOperand(0);
1526 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001527 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1528 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001529 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001530
1531 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001532 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001533 return DAG.getNode(ISD::SRA, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001534 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001535 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001536 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001537 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001538 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001539 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001540 // fold (sra x, c >= size(x)) -> undef
Nate Begemanfb7217b2006-02-17 19:54:08 +00001541 if (N1C && N1C->getValue() >= MVT::getSizeInBits(VT))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001542 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001543 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001544 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001545 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00001546 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
1547 // sext_inreg.
1548 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
1549 unsigned LowBits = MVT::getSizeInBits(VT) - (unsigned)N1C->getValue();
1550 MVT::ValueType EVT;
1551 switch (LowBits) {
1552 default: EVT = MVT::Other; break;
1553 case 1: EVT = MVT::i1; break;
1554 case 8: EVT = MVT::i8; break;
1555 case 16: EVT = MVT::i16; break;
1556 case 32: EVT = MVT::i32; break;
1557 }
1558 if (EVT > MVT::Other && TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, EVT))
1559 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
1560 DAG.getValueType(EVT));
1561 }
Chris Lattner71d9ebc2006-02-28 06:23:04 +00001562
1563 // fold (sra (sra x, c1), c2) -> (sra x, c1+c2)
1564 if (N1C && N0.getOpcode() == ISD::SRA) {
1565 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
1566 unsigned Sum = N1C->getValue() + C1->getValue();
1567 if (Sum >= MVT::getSizeInBits(VT)) Sum = MVT::getSizeInBits(VT)-1;
1568 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0),
1569 DAG.getConstant(Sum, N1C->getValueType(0)));
1570 }
1571 }
1572
Nate Begeman1d4d4142005-09-01 00:19:25 +00001573 // If the sign bit is known to be zero, switch this to a SRL.
Nate Begemanfb7217b2006-02-17 19:54:08 +00001574 if (TLI.MaskedValueIsZero(N0, MVT::getIntVTSignBit(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001575 return DAG.getNode(ISD::SRL, VT, N0, N1);
1576 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001577}
1578
Nate Begeman83e75ec2005-09-06 04:43:02 +00001579SDOperand DAGCombiner::visitSRL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001580 SDOperand N0 = N->getOperand(0);
1581 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001582 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1583 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001584 MVT::ValueType VT = N0.getValueType();
1585 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1586
1587 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001588 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001589 return DAG.getNode(ISD::SRL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001590 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001591 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001592 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001593 // fold (srl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001594 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001595 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001596 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001597 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001598 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001599 // if (srl x, c) is known to be zero, return 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001600 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001601 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001602 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001603 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001604 N0.getOperand(1).getOpcode() == ISD::Constant) {
1605 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001606 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001607 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001608 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001609 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001610 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001611 }
Chris Lattner350bec02006-04-02 06:11:11 +00001612
1613 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
1614 if (N1C && N0.getOpcode() == ISD::CTLZ &&
1615 N1C->getValue() == Log2_32(MVT::getSizeInBits(VT))) {
1616 uint64_t KnownZero, KnownOne, Mask = MVT::getIntVTBitMask(VT);
1617 TLI.ComputeMaskedBits(N0.getOperand(0), Mask, KnownZero, KnownOne);
1618
1619 // If any of the input bits are KnownOne, then the input couldn't be all
1620 // zeros, thus the result of the srl will always be zero.
1621 if (KnownOne) return DAG.getConstant(0, VT);
1622
1623 // If all of the bits input the to ctlz node are known to be zero, then
1624 // the result of the ctlz is "32" and the result of the shift is one.
1625 uint64_t UnknownBits = ~KnownZero & Mask;
1626 if (UnknownBits == 0) return DAG.getConstant(1, VT);
1627
1628 // Otherwise, check to see if there is exactly one bit input to the ctlz.
1629 if ((UnknownBits & (UnknownBits-1)) == 0) {
1630 // Okay, we know that only that the single bit specified by UnknownBits
1631 // could be set on input to the CTLZ node. If this bit is set, the SRL
1632 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
1633 // to an SRL,XOR pair, which is likely to simplify more.
1634 unsigned ShAmt = CountTrailingZeros_64(UnknownBits);
1635 SDOperand Op = N0.getOperand(0);
1636 if (ShAmt) {
1637 Op = DAG.getNode(ISD::SRL, VT, Op,
1638 DAG.getConstant(ShAmt, TLI.getShiftAmountTy()));
1639 AddToWorkList(Op.Val);
1640 }
1641 return DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(1, VT));
1642 }
1643 }
1644
Nate Begeman83e75ec2005-09-06 04:43:02 +00001645 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001646}
1647
Nate Begeman83e75ec2005-09-06 04:43:02 +00001648SDOperand DAGCombiner::visitCTLZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001649 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001650 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00001651 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001652
1653 // fold (ctlz c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001654 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001655 return DAG.getNode(ISD::CTLZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001656 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001657}
1658
Nate Begeman83e75ec2005-09-06 04:43:02 +00001659SDOperand DAGCombiner::visitCTTZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001660 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001661 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00001662 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001663
1664 // fold (cttz c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001665 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001666 return DAG.getNode(ISD::CTTZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001667 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001668}
1669
Nate Begeman83e75ec2005-09-06 04:43:02 +00001670SDOperand DAGCombiner::visitCTPOP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001671 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001672 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00001673 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001674
1675 // fold (ctpop c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001676 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001677 return DAG.getNode(ISD::CTPOP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001678 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001679}
1680
Nate Begeman452d7be2005-09-16 00:54:12 +00001681SDOperand DAGCombiner::visitSELECT(SDNode *N) {
1682 SDOperand N0 = N->getOperand(0);
1683 SDOperand N1 = N->getOperand(1);
1684 SDOperand N2 = N->getOperand(2);
1685 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1686 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1687 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1688 MVT::ValueType VT = N->getValueType(0);
Nate Begeman44728a72005-09-19 22:34:01 +00001689
Nate Begeman452d7be2005-09-16 00:54:12 +00001690 // fold select C, X, X -> X
1691 if (N1 == N2)
1692 return N1;
1693 // fold select true, X, Y -> X
1694 if (N0C && !N0C->isNullValue())
1695 return N1;
1696 // fold select false, X, Y -> Y
1697 if (N0C && N0C->isNullValue())
1698 return N2;
1699 // fold select C, 1, X -> C | X
Nate Begeman44728a72005-09-19 22:34:01 +00001700 if (MVT::i1 == VT && N1C && N1C->getValue() == 1)
Nate Begeman452d7be2005-09-16 00:54:12 +00001701 return DAG.getNode(ISD::OR, VT, N0, N2);
1702 // fold select C, 0, X -> ~C & X
1703 // FIXME: this should check for C type == X type, not i1?
1704 if (MVT::i1 == VT && N1C && N1C->isNullValue()) {
1705 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00001706 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00001707 return DAG.getNode(ISD::AND, VT, XORNode, N2);
1708 }
1709 // fold select C, X, 1 -> ~C | X
Nate Begeman44728a72005-09-19 22:34:01 +00001710 if (MVT::i1 == VT && N2C && N2C->getValue() == 1) {
Nate Begeman452d7be2005-09-16 00:54:12 +00001711 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00001712 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00001713 return DAG.getNode(ISD::OR, VT, XORNode, N1);
1714 }
1715 // fold select C, X, 0 -> C & X
1716 // FIXME: this should check for C type == X type, not i1?
1717 if (MVT::i1 == VT && N2C && N2C->isNullValue())
1718 return DAG.getNode(ISD::AND, VT, N0, N1);
1719 // fold X ? X : Y --> X ? 1 : Y --> X | Y
1720 if (MVT::i1 == VT && N0 == N1)
1721 return DAG.getNode(ISD::OR, VT, N0, N2);
1722 // fold X ? Y : X --> X ? Y : 0 --> X & Y
1723 if (MVT::i1 == VT && N0 == N2)
1724 return DAG.getNode(ISD::AND, VT, N0, N1);
Chris Lattner40c62d52005-10-18 06:04:22 +00001725 // If we can fold this based on the true/false value, do so.
1726 if (SimplifySelectOps(N, N1, N2))
1727 return SDOperand();
Nate Begeman44728a72005-09-19 22:34:01 +00001728 // fold selects based on a setcc into other things, such as min/max/abs
1729 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman750ac1b2006-02-01 07:19:44 +00001730 // FIXME:
1731 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
1732 // having to say they don't support SELECT_CC on every type the DAG knows
1733 // about, since there is no way to mark an opcode illegal at all value types
1734 if (TLI.isOperationLegal(ISD::SELECT_CC, MVT::Other))
1735 return DAG.getNode(ISD::SELECT_CC, VT, N0.getOperand(0), N0.getOperand(1),
1736 N1, N2, N0.getOperand(2));
1737 else
1738 return SimplifySelect(N0, N1, N2);
Nate Begeman452d7be2005-09-16 00:54:12 +00001739 return SDOperand();
1740}
1741
1742SDOperand DAGCombiner::visitSELECT_CC(SDNode *N) {
Nate Begeman44728a72005-09-19 22:34:01 +00001743 SDOperand N0 = N->getOperand(0);
1744 SDOperand N1 = N->getOperand(1);
1745 SDOperand N2 = N->getOperand(2);
1746 SDOperand N3 = N->getOperand(3);
1747 SDOperand N4 = N->getOperand(4);
1748 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1749 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1750 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1751 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
1752
1753 // Determine if the condition we're dealing with is constant
Nate Begemane17daeb2005-10-05 21:43:42 +00001754 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
Chris Lattner91559022005-10-05 04:45:43 +00001755 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
1756
Nate Begeman44728a72005-09-19 22:34:01 +00001757 // fold select_cc lhs, rhs, x, x, cc -> x
1758 if (N2 == N3)
1759 return N2;
Chris Lattner40c62d52005-10-18 06:04:22 +00001760
1761 // If we can fold this based on the true/false value, do so.
1762 if (SimplifySelectOps(N, N2, N3))
1763 return SDOperand();
1764
Nate Begeman44728a72005-09-19 22:34:01 +00001765 // fold select_cc into other things, such as min/max/abs
1766 return SimplifySelectCC(N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00001767}
1768
1769SDOperand DAGCombiner::visitSETCC(SDNode *N) {
1770 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
1771 cast<CondCodeSDNode>(N->getOperand(2))->get());
1772}
1773
Nate Begeman83e75ec2005-09-06 04:43:02 +00001774SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001775 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001776 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001777 MVT::ValueType VT = N->getValueType(0);
1778
Nate Begeman1d4d4142005-09-01 00:19:25 +00001779 // fold (sext c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001780 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001781 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001782 // fold (sext (sext x)) -> (sext x)
1783 if (N0.getOpcode() == ISD::SIGN_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001784 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
Chris Lattnerb14ab8a2005-12-07 07:11:03 +00001785 // fold (sext (truncate x)) -> (sextinreg x) iff x size == sext size.
Chris Lattnercc2210b2005-12-07 18:02:05 +00001786 if (N0.getOpcode() == ISD::TRUNCATE && N0.getOperand(0).getValueType() == VT&&
1787 (!AfterLegalize ||
1788 TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, N0.getValueType())))
Chris Lattnerb14ab8a2005-12-07 07:11:03 +00001789 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
1790 DAG.getValueType(N0.getValueType()));
Evan Cheng110dec22005-12-14 02:19:23 +00001791 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Chris Lattnerd0f6d182005-12-15 19:02:38 +00001792 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() &&
1793 (!AfterLegalize||TLI.isOperationLegal(ISD::SEXTLOAD, N0.getValueType()))){
Nate Begeman3df4d522005-10-12 20:40:40 +00001794 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1795 N0.getOperand(1), N0.getOperand(2),
1796 N0.getValueType());
Chris Lattnerd4771842005-12-14 19:25:30 +00001797 CombineTo(N, ExtLoad);
Chris Lattnerf9884052005-10-13 21:52:31 +00001798 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1799 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001800 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begeman3df4d522005-10-12 20:40:40 +00001801 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001802
1803 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
1804 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
1805 if ((N0.getOpcode() == ISD::SEXTLOAD || N0.getOpcode() == ISD::EXTLOAD) &&
1806 N0.hasOneUse()) {
1807 SDOperand ExtLoad = DAG.getNode(ISD::SEXTLOAD, VT, N0.getOperand(0),
1808 N0.getOperand(1), N0.getOperand(2),
1809 N0.getOperand(3));
Chris Lattnerd4771842005-12-14 19:25:30 +00001810 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001811 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1812 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001813 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001814 }
1815
Nate Begeman83e75ec2005-09-06 04:43:02 +00001816 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001817}
1818
Nate Begeman83e75ec2005-09-06 04:43:02 +00001819SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001820 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001821 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001822 MVT::ValueType VT = N->getValueType(0);
1823
Nate Begeman1d4d4142005-09-01 00:19:25 +00001824 // fold (zext c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001825 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001826 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001827 // fold (zext (zext x)) -> (zext x)
1828 if (N0.getOpcode() == ISD::ZERO_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001829 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
Evan Cheng110dec22005-12-14 02:19:23 +00001830 // fold (zext (truncate x)) -> (zextinreg x) iff x size == zext size.
1831 if (N0.getOpcode() == ISD::TRUNCATE && N0.getOperand(0).getValueType() == VT&&
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001832 (!AfterLegalize || TLI.isOperationLegal(ISD::AND, N0.getValueType())))
Chris Lattner00cb95c2005-12-14 07:58:38 +00001833 return DAG.getZeroExtendInReg(N0.getOperand(0), N0.getValueType());
Evan Cheng110dec22005-12-14 02:19:23 +00001834 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Chris Lattnerd0f6d182005-12-15 19:02:38 +00001835 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() &&
1836 (!AfterLegalize||TLI.isOperationLegal(ISD::ZEXTLOAD, N0.getValueType()))){
Evan Cheng110dec22005-12-14 02:19:23 +00001837 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1838 N0.getOperand(1), N0.getOperand(2),
1839 N0.getValueType());
Chris Lattnerd4771842005-12-14 19:25:30 +00001840 CombineTo(N, ExtLoad);
Evan Cheng110dec22005-12-14 02:19:23 +00001841 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1842 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001843 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng110dec22005-12-14 02:19:23 +00001844 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001845
1846 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
1847 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
1848 if ((N0.getOpcode() == ISD::ZEXTLOAD || N0.getOpcode() == ISD::EXTLOAD) &&
1849 N0.hasOneUse()) {
1850 SDOperand ExtLoad = DAG.getNode(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1851 N0.getOperand(1), N0.getOperand(2),
1852 N0.getOperand(3));
Chris Lattnerd4771842005-12-14 19:25:30 +00001853 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001854 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1855 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001856 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001857 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001858 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001859}
1860
Nate Begeman83e75ec2005-09-06 04:43:02 +00001861SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001862 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001863 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001864 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001865 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001866 MVT::ValueType EVT = cast<VTSDNode>(N1)->getVT();
Nate Begeman07ed4172005-10-10 21:26:48 +00001867 unsigned EVTBits = MVT::getSizeInBits(EVT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001868
Nate Begeman1d4d4142005-09-01 00:19:25 +00001869 // fold (sext_in_reg c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001870 if (N0C) {
1871 SDOperand Truncate = DAG.getConstant(N0C->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001872 return DAG.getNode(ISD::SIGN_EXTEND, VT, Truncate);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001873 }
Nate Begeman646d7e22005-09-02 21:18:40 +00001874 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt1
Nate Begeman1d4d4142005-09-01 00:19:25 +00001875 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Nate Begeman216def82005-10-14 01:29:07 +00001876 cast<VTSDNode>(N0.getOperand(1))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001877 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001878 }
Nate Begeman646d7e22005-09-02 21:18:40 +00001879 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
1880 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1881 EVT < cast<VTSDNode>(N0.getOperand(1))->getVT()) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001882 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001883 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00001884 // fold (sext_in_reg (assert_sext x)) -> (assert_sext x)
1885 if (N0.getOpcode() == ISD::AssertSext &&
1886 cast<VTSDNode>(N0.getOperand(1))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001887 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001888 }
1889 // fold (sext_in_reg (sextload x)) -> (sextload x)
1890 if (N0.getOpcode() == ISD::SEXTLOAD &&
1891 cast<VTSDNode>(N0.getOperand(3))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001892 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001893 }
Nate Begeman4ebd8052005-09-01 23:24:04 +00001894 // fold (sext_in_reg (setcc x)) -> setcc x iff (setcc x) == 0 or -1
Nate Begeman1d4d4142005-09-01 00:19:25 +00001895 if (N0.getOpcode() == ISD::SETCC &&
1896 TLI.getSetCCResultContents() ==
1897 TargetLowering::ZeroOrNegativeOneSetCCResult)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001898 return N0;
Nate Begeman07ed4172005-10-10 21:26:48 +00001899 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is zero
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001900 if (TLI.MaskedValueIsZero(N0, 1ULL << (EVTBits-1)))
Nate Begemande996292006-02-03 22:24:05 +00001901 return DAG.getZeroExtendInReg(N0, EVT);
Nate Begeman07ed4172005-10-10 21:26:48 +00001902 // fold (sext_in_reg (srl x)) -> sra x
1903 if (N0.getOpcode() == ISD::SRL &&
1904 N0.getOperand(1).getOpcode() == ISD::Constant &&
1905 cast<ConstantSDNode>(N0.getOperand(1))->getValue() == EVTBits) {
1906 return DAG.getNode(ISD::SRA, N0.getValueType(), N0.getOperand(0),
1907 N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001908 }
Nate Begemanded49632005-10-13 03:11:28 +00001909 // fold (sext_inreg (extload x)) -> (sextload x)
1910 if (N0.getOpcode() == ISD::EXTLOAD &&
1911 EVT == cast<VTSDNode>(N0.getOperand(3))->getVT() &&
Nate Begemanbfd65a02005-10-13 18:34:58 +00001912 (!AfterLegalize || TLI.isOperationLegal(ISD::SEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001913 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1914 N0.getOperand(1), N0.getOperand(2),
1915 EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00001916 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00001917 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001918 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001919 }
1920 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Chris Lattner40c62d52005-10-18 06:04:22 +00001921 if (N0.getOpcode() == ISD::ZEXTLOAD && N0.hasOneUse() &&
Nate Begemanded49632005-10-13 03:11:28 +00001922 EVT == cast<VTSDNode>(N0.getOperand(3))->getVT() &&
Nate Begemanbfd65a02005-10-13 18:34:58 +00001923 (!AfterLegalize || TLI.isOperationLegal(ISD::SEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001924 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1925 N0.getOperand(1), N0.getOperand(2),
1926 EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00001927 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00001928 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001929 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001930 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001931 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001932}
1933
Nate Begeman83e75ec2005-09-06 04:43:02 +00001934SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001935 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001936 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001937 MVT::ValueType VT = N->getValueType(0);
1938
1939 // noop truncate
1940 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001941 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001942 // fold (truncate c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001943 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001944 return DAG.getNode(ISD::TRUNCATE, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001945 // fold (truncate (truncate x)) -> (truncate x)
1946 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001947 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001948 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
1949 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND){
1950 if (N0.getValueType() < VT)
1951 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00001952 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001953 else if (N0.getValueType() > VT)
1954 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00001955 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001956 else
1957 // if the source and dest are the same type, we can drop both the extend
1958 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00001959 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001960 }
Nate Begeman3df4d522005-10-12 20:40:40 +00001961 // fold (truncate (load x)) -> (smaller load x)
Chris Lattner40c62d52005-10-18 06:04:22 +00001962 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse()) {
Nate Begeman3df4d522005-10-12 20:40:40 +00001963 assert(MVT::getSizeInBits(N0.getValueType()) > MVT::getSizeInBits(VT) &&
1964 "Cannot truncate to larger type!");
1965 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
Nate Begeman765784a2005-10-12 23:18:53 +00001966 // For big endian targets, we need to add an offset to the pointer to load
1967 // the correct bytes. For little endian systems, we merely need to read
1968 // fewer bytes from the same pointer.
Nate Begeman3df4d522005-10-12 20:40:40 +00001969 uint64_t PtrOff =
1970 (MVT::getSizeInBits(N0.getValueType()) - MVT::getSizeInBits(VT)) / 8;
Nate Begeman765784a2005-10-12 23:18:53 +00001971 SDOperand NewPtr = TLI.isLittleEndian() ? N0.getOperand(1) :
1972 DAG.getNode(ISD::ADD, PtrType, N0.getOperand(1),
1973 DAG.getConstant(PtrOff, PtrType));
Chris Lattner5750df92006-03-01 04:03:14 +00001974 AddToWorkList(NewPtr.Val);
Nate Begeman3df4d522005-10-12 20:40:40 +00001975 SDOperand Load = DAG.getLoad(VT, N0.getOperand(0), NewPtr,N0.getOperand(2));
Chris Lattner5750df92006-03-01 04:03:14 +00001976 AddToWorkList(N);
Chris Lattner24edbb72005-10-13 22:10:05 +00001977 CombineTo(N0.Val, Load, Load.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001978 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begeman3df4d522005-10-12 20:40:40 +00001979 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001980 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001981}
1982
Chris Lattner94683772005-12-23 05:30:37 +00001983SDOperand DAGCombiner::visitBIT_CONVERT(SDNode *N) {
1984 SDOperand N0 = N->getOperand(0);
1985 MVT::ValueType VT = N->getValueType(0);
1986
1987 // If the input is a constant, let getNode() fold it.
1988 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
1989 SDOperand Res = DAG.getNode(ISD::BIT_CONVERT, VT, N0);
1990 if (Res.Val != N) return Res;
1991 }
1992
Chris Lattnerc8547d82005-12-23 05:37:50 +00001993 if (N0.getOpcode() == ISD::BIT_CONVERT) // conv(conv(x,t1),t2) -> conv(x,t2)
1994 return DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
Chris Lattner6258fb22006-04-02 02:53:43 +00001995
Chris Lattner57104102005-12-23 05:44:41 +00001996 // fold (conv (load x)) -> (load (conv*)x)
Chris Lattnerbf40c4b2006-01-15 18:58:59 +00001997 // FIXME: These xforms need to know that the resultant load doesn't need a
1998 // higher alignment than the original!
1999 if (0 && N0.getOpcode() == ISD::LOAD && N0.hasOneUse()) {
Chris Lattner57104102005-12-23 05:44:41 +00002000 SDOperand Load = DAG.getLoad(VT, N0.getOperand(0), N0.getOperand(1),
2001 N0.getOperand(2));
Chris Lattner5750df92006-03-01 04:03:14 +00002002 AddToWorkList(N);
Chris Lattner57104102005-12-23 05:44:41 +00002003 CombineTo(N0.Val, DAG.getNode(ISD::BIT_CONVERT, N0.getValueType(), Load),
2004 Load.getValue(1));
2005 return Load;
2006 }
2007
Chris Lattner94683772005-12-23 05:30:37 +00002008 return SDOperand();
2009}
2010
Chris Lattner6258fb22006-04-02 02:53:43 +00002011SDOperand DAGCombiner::visitVBIT_CONVERT(SDNode *N) {
2012 SDOperand N0 = N->getOperand(0);
2013 MVT::ValueType VT = N->getValueType(0);
2014
2015 // If the input is a VBUILD_VECTOR with all constant elements, fold this now.
2016 // First check to see if this is all constant.
2017 if (N0.getOpcode() == ISD::VBUILD_VECTOR && N0.Val->hasOneUse() &&
2018 VT == MVT::Vector) {
2019 bool isSimple = true;
2020 for (unsigned i = 0, e = N0.getNumOperands()-2; i != e; ++i)
2021 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
2022 N0.getOperand(i).getOpcode() != ISD::Constant &&
2023 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
2024 isSimple = false;
2025 break;
2026 }
2027
Chris Lattner97c20732006-04-03 17:29:28 +00002028 MVT::ValueType DestEltVT = cast<VTSDNode>(N->getOperand(2))->getVT();
2029 if (isSimple && !MVT::isVector(DestEltVT)) {
Chris Lattner6258fb22006-04-02 02:53:43 +00002030 return ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(N0.Val, DestEltVT);
2031 }
2032 }
2033
2034 return SDOperand();
2035}
2036
2037/// ConstantFoldVBIT_CONVERTofVBUILD_VECTOR - We know that BV is a vbuild_vector
2038/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
2039/// destination element value type.
2040SDOperand DAGCombiner::
2041ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(SDNode *BV, MVT::ValueType DstEltVT) {
2042 MVT::ValueType SrcEltVT = BV->getOperand(0).getValueType();
2043
2044 // If this is already the right type, we're done.
2045 if (SrcEltVT == DstEltVT) return SDOperand(BV, 0);
2046
2047 unsigned SrcBitSize = MVT::getSizeInBits(SrcEltVT);
2048 unsigned DstBitSize = MVT::getSizeInBits(DstEltVT);
2049
2050 // If this is a conversion of N elements of one type to N elements of another
2051 // type, convert each element. This handles FP<->INT cases.
2052 if (SrcBitSize == DstBitSize) {
2053 std::vector<SDOperand> Ops;
Chris Lattner3e104b12006-04-08 04:15:24 +00002054 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00002055 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, DstEltVT, BV->getOperand(i)));
Chris Lattner3e104b12006-04-08 04:15:24 +00002056 AddToWorkList(Ops.back().Val);
2057 }
Chris Lattner6258fb22006-04-02 02:53:43 +00002058 Ops.push_back(*(BV->op_end()-2)); // Add num elements.
2059 Ops.push_back(DAG.getValueType(DstEltVT));
2060 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, Ops);
2061 }
2062
2063 // Otherwise, we're growing or shrinking the elements. To avoid having to
2064 // handle annoying details of growing/shrinking FP values, we convert them to
2065 // int first.
2066 if (MVT::isFloatingPoint(SrcEltVT)) {
2067 // Convert the input float vector to a int vector where the elements are the
2068 // same sizes.
2069 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
2070 MVT::ValueType IntVT = SrcEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
2071 BV = ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(BV, IntVT).Val;
2072 SrcEltVT = IntVT;
2073 }
2074
2075 // Now we know the input is an integer vector. If the output is a FP type,
2076 // convert to integer first, then to FP of the right size.
2077 if (MVT::isFloatingPoint(DstEltVT)) {
2078 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
2079 MVT::ValueType TmpVT = DstEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
2080 SDNode *Tmp = ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(BV, TmpVT).Val;
2081
2082 // Next, convert to FP elements of the same size.
2083 return ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(Tmp, DstEltVT);
2084 }
2085
2086 // Okay, we know the src/dst types are both integers of differing types.
2087 // Handling growing first.
2088 assert(MVT::isInteger(SrcEltVT) && MVT::isInteger(DstEltVT));
2089 if (SrcBitSize < DstBitSize) {
2090 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
2091
2092 std::vector<SDOperand> Ops;
2093 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e;
2094 i += NumInputsPerOutput) {
2095 bool isLE = TLI.isLittleEndian();
2096 uint64_t NewBits = 0;
2097 bool EltIsUndef = true;
2098 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
2099 // Shift the previously computed bits over.
2100 NewBits <<= SrcBitSize;
2101 SDOperand Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
2102 if (Op.getOpcode() == ISD::UNDEF) continue;
2103 EltIsUndef = false;
2104
2105 NewBits |= cast<ConstantSDNode>(Op)->getValue();
2106 }
2107
2108 if (EltIsUndef)
2109 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
2110 else
2111 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
2112 }
2113
2114 Ops.push_back(DAG.getConstant(Ops.size(), MVT::i32)); // Add num elements.
2115 Ops.push_back(DAG.getValueType(DstEltVT)); // Add element size.
2116 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, Ops);
2117 }
2118
2119 // Finally, this must be the case where we are shrinking elements: each input
2120 // turns into multiple outputs.
2121 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
2122 std::vector<SDOperand> Ops;
2123 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e; ++i) {
2124 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
2125 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
2126 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
2127 continue;
2128 }
2129 uint64_t OpVal = cast<ConstantSDNode>(BV->getOperand(i))->getValue();
2130
2131 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
2132 unsigned ThisVal = OpVal & ((1ULL << DstBitSize)-1);
2133 OpVal >>= DstBitSize;
2134 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
2135 }
2136
2137 // For big endian targets, swap the order of the pieces of each element.
2138 if (!TLI.isLittleEndian())
2139 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
2140 }
2141 Ops.push_back(DAG.getConstant(Ops.size(), MVT::i32)); // Add num elements.
2142 Ops.push_back(DAG.getValueType(DstEltVT)); // Add element size.
2143 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, Ops);
2144}
2145
2146
2147
Chris Lattner01b3d732005-09-28 22:28:18 +00002148SDOperand DAGCombiner::visitFADD(SDNode *N) {
2149 SDOperand N0 = N->getOperand(0);
2150 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00002151 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2152 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002153 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00002154
2155 // fold (fadd c1, c2) -> c1+c2
2156 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002157 return DAG.getNode(ISD::FADD, VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00002158 // canonicalize constant to RHS
2159 if (N0CFP && !N1CFP)
2160 return DAG.getNode(ISD::FADD, VT, N1, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00002161 // fold (A + (-B)) -> A-B
2162 if (N1.getOpcode() == ISD::FNEG)
2163 return DAG.getNode(ISD::FSUB, VT, N0, N1.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00002164 // fold ((-A) + B) -> B-A
2165 if (N0.getOpcode() == ISD::FNEG)
2166 return DAG.getNode(ISD::FSUB, VT, N1, N0.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00002167 return SDOperand();
2168}
2169
2170SDOperand DAGCombiner::visitFSUB(SDNode *N) {
2171 SDOperand N0 = N->getOperand(0);
2172 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00002173 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2174 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002175 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00002176
2177 // fold (fsub c1, c2) -> c1-c2
2178 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002179 return DAG.getNode(ISD::FSUB, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002180 // fold (A-(-B)) -> A+B
2181 if (N1.getOpcode() == ISD::FNEG)
Nate Begemana148d982006-01-18 22:35:16 +00002182 return DAG.getNode(ISD::FADD, VT, N0, N1.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00002183 return SDOperand();
2184}
2185
2186SDOperand DAGCombiner::visitFMUL(SDNode *N) {
2187 SDOperand N0 = N->getOperand(0);
2188 SDOperand N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00002189 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2190 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002191 MVT::ValueType VT = N->getValueType(0);
2192
Nate Begeman11af4ea2005-10-17 20:40:11 +00002193 // fold (fmul c1, c2) -> c1*c2
2194 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002195 return DAG.getNode(ISD::FMUL, VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00002196 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00002197 if (N0CFP && !N1CFP)
2198 return DAG.getNode(ISD::FMUL, VT, N1, N0);
Nate Begeman11af4ea2005-10-17 20:40:11 +00002199 // fold (fmul X, 2.0) -> (fadd X, X)
2200 if (N1CFP && N1CFP->isExactlyValue(+2.0))
2201 return DAG.getNode(ISD::FADD, VT, N0, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00002202 return SDOperand();
2203}
2204
2205SDOperand DAGCombiner::visitFDIV(SDNode *N) {
2206 SDOperand N0 = N->getOperand(0);
2207 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00002208 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2209 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002210 MVT::ValueType VT = N->getValueType(0);
2211
Nate Begemana148d982006-01-18 22:35:16 +00002212 // fold (fdiv c1, c2) -> c1/c2
2213 if (N0CFP && N1CFP)
2214 return DAG.getNode(ISD::FDIV, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002215 return SDOperand();
2216}
2217
2218SDOperand DAGCombiner::visitFREM(SDNode *N) {
2219 SDOperand N0 = N->getOperand(0);
2220 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00002221 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2222 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002223 MVT::ValueType VT = N->getValueType(0);
2224
Nate Begemana148d982006-01-18 22:35:16 +00002225 // fold (frem c1, c2) -> fmod(c1,c2)
2226 if (N0CFP && N1CFP)
2227 return DAG.getNode(ISD::FREM, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002228 return SDOperand();
2229}
2230
Chris Lattner12d83032006-03-05 05:30:57 +00002231SDOperand DAGCombiner::visitFCOPYSIGN(SDNode *N) {
2232 SDOperand N0 = N->getOperand(0);
2233 SDOperand N1 = N->getOperand(1);
2234 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2235 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2236 MVT::ValueType VT = N->getValueType(0);
2237
2238 if (N0CFP && N1CFP) // Constant fold
2239 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1);
2240
2241 if (N1CFP) {
2242 // copysign(x, c1) -> fabs(x) iff ispos(c1)
2243 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
2244 union {
2245 double d;
2246 int64_t i;
2247 } u;
2248 u.d = N1CFP->getValue();
2249 if (u.i >= 0)
2250 return DAG.getNode(ISD::FABS, VT, N0);
2251 else
2252 return DAG.getNode(ISD::FNEG, VT, DAG.getNode(ISD::FABS, VT, N0));
2253 }
2254
2255 // copysign(fabs(x), y) -> copysign(x, y)
2256 // copysign(fneg(x), y) -> copysign(x, y)
2257 // copysign(copysign(x,z), y) -> copysign(x, y)
2258 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
2259 N0.getOpcode() == ISD::FCOPYSIGN)
2260 return DAG.getNode(ISD::FCOPYSIGN, VT, N0.getOperand(0), N1);
2261
2262 // copysign(x, abs(y)) -> abs(x)
2263 if (N1.getOpcode() == ISD::FABS)
2264 return DAG.getNode(ISD::FABS, VT, N0);
2265
2266 // copysign(x, copysign(y,z)) -> copysign(x, z)
2267 if (N1.getOpcode() == ISD::FCOPYSIGN)
2268 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(1));
2269
2270 // copysign(x, fp_extend(y)) -> copysign(x, y)
2271 // copysign(x, fp_round(y)) -> copysign(x, y)
2272 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
2273 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(0));
2274
2275 return SDOperand();
2276}
2277
2278
Chris Lattner01b3d732005-09-28 22:28:18 +00002279
Nate Begeman83e75ec2005-09-06 04:43:02 +00002280SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002281 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002282 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00002283 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002284
2285 // fold (sint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002286 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00002287 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002288 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002289}
2290
Nate Begeman83e75ec2005-09-06 04:43:02 +00002291SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002292 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002293 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00002294 MVT::ValueType VT = N->getValueType(0);
2295
Nate Begeman1d4d4142005-09-01 00:19:25 +00002296 // fold (uint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002297 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00002298 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002299 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002300}
2301
Nate Begeman83e75ec2005-09-06 04:43:02 +00002302SDOperand DAGCombiner::visitFP_TO_SINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002303 SDOperand N0 = N->getOperand(0);
2304 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2305 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002306
2307 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00002308 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002309 return DAG.getNode(ISD::FP_TO_SINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002310 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002311}
2312
Nate Begeman83e75ec2005-09-06 04:43:02 +00002313SDOperand DAGCombiner::visitFP_TO_UINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002314 SDOperand N0 = N->getOperand(0);
2315 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2316 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002317
2318 // fold (fp_to_uint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00002319 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002320 return DAG.getNode(ISD::FP_TO_UINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002321 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002322}
2323
Nate Begeman83e75ec2005-09-06 04:43:02 +00002324SDOperand DAGCombiner::visitFP_ROUND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002325 SDOperand N0 = N->getOperand(0);
2326 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2327 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002328
2329 // fold (fp_round c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002330 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002331 return DAG.getNode(ISD::FP_ROUND, VT, N0);
Chris Lattner79dbea52006-03-13 06:26:26 +00002332
2333 // fold (fp_round (fp_extend x)) -> x
2334 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
2335 return N0.getOperand(0);
2336
2337 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
2338 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.Val->hasOneUse()) {
2339 SDOperand Tmp = DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0));
2340 AddToWorkList(Tmp.Val);
2341 return DAG.getNode(ISD::FCOPYSIGN, VT, Tmp, N0.getOperand(1));
2342 }
2343
Nate Begeman83e75ec2005-09-06 04:43:02 +00002344 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002345}
2346
Nate Begeman83e75ec2005-09-06 04:43:02 +00002347SDOperand DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002348 SDOperand N0 = N->getOperand(0);
2349 MVT::ValueType VT = N->getValueType(0);
2350 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00002351 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002352
Nate Begeman1d4d4142005-09-01 00:19:25 +00002353 // fold (fp_round_inreg c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002354 if (N0CFP) {
2355 SDOperand Round = DAG.getConstantFP(N0CFP->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002356 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002357 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00002358 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002359}
2360
Nate Begeman83e75ec2005-09-06 04:43:02 +00002361SDOperand DAGCombiner::visitFP_EXTEND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002362 SDOperand N0 = N->getOperand(0);
2363 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2364 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002365
2366 // fold (fp_extend c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002367 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002368 return DAG.getNode(ISD::FP_EXTEND, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002369 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002370}
2371
Nate Begeman83e75ec2005-09-06 04:43:02 +00002372SDOperand DAGCombiner::visitFNEG(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002373 SDOperand N0 = N->getOperand(0);
2374 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2375 MVT::ValueType VT = N->getValueType(0);
2376
2377 // fold (fneg c1) -> -c1
Nate Begeman646d7e22005-09-02 21:18:40 +00002378 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002379 return DAG.getNode(ISD::FNEG, VT, N0);
2380 // fold (fneg (sub x, y)) -> (sub y, x)
Chris Lattner12d83032006-03-05 05:30:57 +00002381 if (N0.getOpcode() == ISD::SUB)
2382 return DAG.getNode(ISD::SUB, VT, N0.getOperand(1), N0.getOperand(0));
Nate Begemana148d982006-01-18 22:35:16 +00002383 // fold (fneg (fneg x)) -> x
Chris Lattner12d83032006-03-05 05:30:57 +00002384 if (N0.getOpcode() == ISD::FNEG)
2385 return N0.getOperand(0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002386 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002387}
2388
Nate Begeman83e75ec2005-09-06 04:43:02 +00002389SDOperand DAGCombiner::visitFABS(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002390 SDOperand N0 = N->getOperand(0);
2391 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2392 MVT::ValueType VT = N->getValueType(0);
2393
Nate Begeman1d4d4142005-09-01 00:19:25 +00002394 // fold (fabs c1) -> fabs(c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00002395 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002396 return DAG.getNode(ISD::FABS, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002397 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00002398 if (N0.getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002399 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002400 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00002401 // fold (fabs (fcopysign x, y)) -> (fabs x)
2402 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
2403 return DAG.getNode(ISD::FABS, VT, N0.getOperand(0));
2404
Nate Begeman83e75ec2005-09-06 04:43:02 +00002405 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002406}
2407
Nate Begeman44728a72005-09-19 22:34:01 +00002408SDOperand DAGCombiner::visitBRCOND(SDNode *N) {
2409 SDOperand Chain = N->getOperand(0);
2410 SDOperand N1 = N->getOperand(1);
2411 SDOperand N2 = N->getOperand(2);
2412 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2413
2414 // never taken branch, fold to chain
2415 if (N1C && N1C->isNullValue())
2416 return Chain;
2417 // unconditional branch
Nate Begemane17daeb2005-10-05 21:43:42 +00002418 if (N1C && N1C->getValue() == 1)
Nate Begeman44728a72005-09-19 22:34:01 +00002419 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
Nate Begeman750ac1b2006-02-01 07:19:44 +00002420 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
2421 // on the target.
2422 if (N1.getOpcode() == ISD::SETCC &&
2423 TLI.isOperationLegal(ISD::BR_CC, MVT::Other)) {
2424 return DAG.getNode(ISD::BR_CC, MVT::Other, Chain, N1.getOperand(2),
2425 N1.getOperand(0), N1.getOperand(1), N2);
2426 }
Nate Begeman44728a72005-09-19 22:34:01 +00002427 return SDOperand();
2428}
2429
Chris Lattner3ea0b472005-10-05 06:47:48 +00002430// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
2431//
Nate Begeman44728a72005-09-19 22:34:01 +00002432SDOperand DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00002433 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
2434 SDOperand CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
2435
2436 // Use SimplifySetCC to simplify SETCC's.
Nate Begemane17daeb2005-10-05 21:43:42 +00002437 SDOperand Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false);
2438 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.Val);
2439
2440 // fold br_cc true, dest -> br dest (unconditional branch)
2441 if (SCCC && SCCC->getValue())
2442 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
2443 N->getOperand(4));
2444 // fold br_cc false, dest -> unconditional fall through
2445 if (SCCC && SCCC->isNullValue())
2446 return N->getOperand(0);
2447 // fold to a simpler setcc
2448 if (Simp.Val && Simp.getOpcode() == ISD::SETCC)
2449 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
2450 Simp.getOperand(2), Simp.getOperand(0),
2451 Simp.getOperand(1), N->getOperand(4));
Nate Begeman44728a72005-09-19 22:34:01 +00002452 return SDOperand();
2453}
2454
Chris Lattner01a22022005-10-10 22:04:48 +00002455SDOperand DAGCombiner::visitLOAD(SDNode *N) {
2456 SDOperand Chain = N->getOperand(0);
2457 SDOperand Ptr = N->getOperand(1);
2458 SDOperand SrcValue = N->getOperand(2);
Chris Lattnere4b95392006-03-31 18:06:18 +00002459
2460 // If there are no uses of the loaded value, change uses of the chain value
2461 // into uses of the chain input (i.e. delete the dead load).
2462 if (N->hasNUsesOfValue(0, 0))
2463 return CombineTo(N, DAG.getNode(ISD::UNDEF, N->getValueType(0)), Chain);
Chris Lattner01a22022005-10-10 22:04:48 +00002464
2465 // If this load is directly stored, replace the load value with the stored
2466 // value.
2467 // TODO: Handle store large -> read small portion.
2468 // TODO: Handle TRUNCSTORE/EXTLOAD
2469 if (Chain.getOpcode() == ISD::STORE && Chain.getOperand(2) == Ptr &&
2470 Chain.getOperand(1).getValueType() == N->getValueType(0))
2471 return CombineTo(N, Chain.getOperand(1), Chain);
2472
2473 return SDOperand();
2474}
2475
Chris Lattner29cd7db2006-03-31 18:10:41 +00002476/// visitXEXTLOAD - Handle EXTLOAD/ZEXTLOAD/SEXTLOAD.
2477SDOperand DAGCombiner::visitXEXTLOAD(SDNode *N) {
2478 SDOperand Chain = N->getOperand(0);
2479 SDOperand Ptr = N->getOperand(1);
2480 SDOperand SrcValue = N->getOperand(2);
2481 SDOperand EVT = N->getOperand(3);
2482
2483 // If there are no uses of the loaded value, change uses of the chain value
2484 // into uses of the chain input (i.e. delete the dead load).
2485 if (N->hasNUsesOfValue(0, 0))
2486 return CombineTo(N, DAG.getNode(ISD::UNDEF, N->getValueType(0)), Chain);
2487
2488 return SDOperand();
2489}
2490
Chris Lattner87514ca2005-10-10 22:31:19 +00002491SDOperand DAGCombiner::visitSTORE(SDNode *N) {
2492 SDOperand Chain = N->getOperand(0);
2493 SDOperand Value = N->getOperand(1);
2494 SDOperand Ptr = N->getOperand(2);
2495 SDOperand SrcValue = N->getOperand(3);
2496
2497 // If this is a store that kills a previous store, remove the previous store.
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002498 if (Chain.getOpcode() == ISD::STORE && Chain.getOperand(2) == Ptr &&
Chris Lattnerfe7f0462005-10-27 07:10:34 +00002499 Chain.Val->hasOneUse() /* Avoid introducing DAG cycles */ &&
2500 // Make sure that these stores are the same value type:
2501 // FIXME: we really care that the second store is >= size of the first.
2502 Value.getValueType() == Chain.getOperand(1).getValueType()) {
Chris Lattner87514ca2005-10-10 22:31:19 +00002503 // Create a new store of Value that replaces both stores.
2504 SDNode *PrevStore = Chain.Val;
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002505 if (PrevStore->getOperand(1) == Value) // Same value multiply stored.
2506 return Chain;
Chris Lattner87514ca2005-10-10 22:31:19 +00002507 SDOperand NewStore = DAG.getNode(ISD::STORE, MVT::Other,
2508 PrevStore->getOperand(0), Value, Ptr,
2509 SrcValue);
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002510 CombineTo(N, NewStore); // Nuke this store.
Chris Lattner87514ca2005-10-10 22:31:19 +00002511 CombineTo(PrevStore, NewStore); // Nuke the previous store.
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002512 return SDOperand(N, 0);
Chris Lattner87514ca2005-10-10 22:31:19 +00002513 }
2514
Chris Lattnerc33baaa2005-12-23 05:48:07 +00002515 // If this is a store of a bit convert, store the input value.
Chris Lattnerbf40c4b2006-01-15 18:58:59 +00002516 // FIXME: This needs to know that the resultant store does not need a
2517 // higher alignment than the original.
2518 if (0 && Value.getOpcode() == ISD::BIT_CONVERT)
Chris Lattnerc33baaa2005-12-23 05:48:07 +00002519 return DAG.getNode(ISD::STORE, MVT::Other, Chain, Value.getOperand(0),
2520 Ptr, SrcValue);
2521
Chris Lattner87514ca2005-10-10 22:31:19 +00002522 return SDOperand();
2523}
2524
Chris Lattnerca242442006-03-19 01:27:56 +00002525SDOperand DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
2526 SDOperand InVec = N->getOperand(0);
2527 SDOperand InVal = N->getOperand(1);
2528 SDOperand EltNo = N->getOperand(2);
2529
2530 // If the invec is a BUILD_VECTOR and if EltNo is a constant, build a new
2531 // vector with the inserted element.
2532 if (InVec.getOpcode() == ISD::BUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
2533 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
2534 std::vector<SDOperand> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
2535 if (Elt < Ops.size())
2536 Ops[Elt] = InVal;
2537 return DAG.getNode(ISD::BUILD_VECTOR, InVec.getValueType(), Ops);
2538 }
2539
2540 return SDOperand();
2541}
2542
2543SDOperand DAGCombiner::visitVINSERT_VECTOR_ELT(SDNode *N) {
2544 SDOperand InVec = N->getOperand(0);
2545 SDOperand InVal = N->getOperand(1);
2546 SDOperand EltNo = N->getOperand(2);
2547 SDOperand NumElts = N->getOperand(3);
2548 SDOperand EltType = N->getOperand(4);
2549
2550 // If the invec is a VBUILD_VECTOR and if EltNo is a constant, build a new
2551 // vector with the inserted element.
2552 if (InVec.getOpcode() == ISD::VBUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
2553 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
2554 std::vector<SDOperand> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
2555 if (Elt < Ops.size()-2)
2556 Ops[Elt] = InVal;
2557 return DAG.getNode(ISD::VBUILD_VECTOR, InVec.getValueType(), Ops);
2558 }
2559
2560 return SDOperand();
2561}
2562
Chris Lattnerd7648c82006-03-28 20:28:38 +00002563SDOperand DAGCombiner::visitVBUILD_VECTOR(SDNode *N) {
2564 unsigned NumInScalars = N->getNumOperands()-2;
2565 SDOperand NumElts = N->getOperand(NumInScalars);
2566 SDOperand EltType = N->getOperand(NumInScalars+1);
2567
2568 // Check to see if this is a VBUILD_VECTOR of a bunch of VEXTRACT_VECTOR_ELT
2569 // operations. If so, and if the EXTRACT_ELT vector inputs come from at most
2570 // two distinct vectors, turn this into a shuffle node.
2571 SDOperand VecIn1, VecIn2;
2572 for (unsigned i = 0; i != NumInScalars; ++i) {
2573 // Ignore undef inputs.
2574 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
2575
2576 // If this input is something other than a VEXTRACT_VECTOR_ELT with a
2577 // constant index, bail out.
2578 if (N->getOperand(i).getOpcode() != ISD::VEXTRACT_VECTOR_ELT ||
2579 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
2580 VecIn1 = VecIn2 = SDOperand(0, 0);
2581 break;
2582 }
2583
2584 // If the input vector type disagrees with the result of the vbuild_vector,
2585 // we can't make a shuffle.
2586 SDOperand ExtractedFromVec = N->getOperand(i).getOperand(0);
2587 if (*(ExtractedFromVec.Val->op_end()-2) != NumElts ||
2588 *(ExtractedFromVec.Val->op_end()-1) != EltType) {
2589 VecIn1 = VecIn2 = SDOperand(0, 0);
2590 break;
2591 }
2592
2593 // Otherwise, remember this. We allow up to two distinct input vectors.
2594 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
2595 continue;
2596
2597 if (VecIn1.Val == 0) {
2598 VecIn1 = ExtractedFromVec;
2599 } else if (VecIn2.Val == 0) {
2600 VecIn2 = ExtractedFromVec;
2601 } else {
2602 // Too many inputs.
2603 VecIn1 = VecIn2 = SDOperand(0, 0);
2604 break;
2605 }
2606 }
2607
2608 // If everything is good, we can make a shuffle operation.
2609 if (VecIn1.Val) {
2610 std::vector<SDOperand> BuildVecIndices;
2611 for (unsigned i = 0; i != NumInScalars; ++i) {
2612 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
2613 BuildVecIndices.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
2614 continue;
2615 }
2616
2617 SDOperand Extract = N->getOperand(i);
2618
2619 // If extracting from the first vector, just use the index directly.
2620 if (Extract.getOperand(0) == VecIn1) {
2621 BuildVecIndices.push_back(Extract.getOperand(1));
2622 continue;
2623 }
2624
2625 // Otherwise, use InIdx + VecSize
2626 unsigned Idx = cast<ConstantSDNode>(Extract.getOperand(1))->getValue();
2627 BuildVecIndices.push_back(DAG.getConstant(Idx+NumInScalars, MVT::i32));
2628 }
2629
2630 // Add count and size info.
2631 BuildVecIndices.push_back(NumElts);
2632 BuildVecIndices.push_back(DAG.getValueType(MVT::i32));
2633
2634 // Return the new VVECTOR_SHUFFLE node.
2635 std::vector<SDOperand> Ops;
2636 Ops.push_back(VecIn1);
Chris Lattnercef896e2006-03-28 22:19:47 +00002637 if (VecIn2.Val) {
2638 Ops.push_back(VecIn2);
2639 } else {
2640 // Use an undef vbuild_vector as input for the second operand.
2641 std::vector<SDOperand> UnOps(NumInScalars,
2642 DAG.getNode(ISD::UNDEF,
2643 cast<VTSDNode>(EltType)->getVT()));
2644 UnOps.push_back(NumElts);
2645 UnOps.push_back(EltType);
2646 Ops.push_back(DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, UnOps));
Chris Lattner3e104b12006-04-08 04:15:24 +00002647 AddToWorkList(Ops.back().Val);
Chris Lattnercef896e2006-03-28 22:19:47 +00002648 }
Chris Lattnerd7648c82006-03-28 20:28:38 +00002649 Ops.push_back(DAG.getNode(ISD::VBUILD_VECTOR,MVT::Vector, BuildVecIndices));
2650 Ops.push_back(NumElts);
2651 Ops.push_back(EltType);
2652 return DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector, Ops);
2653 }
2654
2655 return SDOperand();
2656}
2657
Chris Lattner66445d32006-03-28 22:11:53 +00002658SDOperand DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
Chris Lattnerf1d0c622006-03-31 22:16:43 +00002659 SDOperand ShufMask = N->getOperand(2);
2660 unsigned NumElts = ShufMask.getNumOperands();
2661
2662 // If the shuffle mask is an identity operation on the LHS, return the LHS.
2663 bool isIdentity = true;
2664 for (unsigned i = 0; i != NumElts; ++i) {
2665 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
2666 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
2667 isIdentity = false;
2668 break;
2669 }
2670 }
2671 if (isIdentity) return N->getOperand(0);
2672
2673 // If the shuffle mask is an identity operation on the RHS, return the RHS.
2674 isIdentity = true;
2675 for (unsigned i = 0; i != NumElts; ++i) {
2676 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
2677 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
2678 isIdentity = false;
2679 break;
2680 }
2681 }
2682 if (isIdentity) return N->getOperand(1);
2683
Chris Lattner66445d32006-03-28 22:11:53 +00002684 // If the LHS and the RHS are the same node, turn the RHS into an undef.
2685 if (N->getOperand(0) == N->getOperand(1)) {
Evan Chengc04766a2006-04-06 23:20:43 +00002686 if (N->getOperand(0).getOpcode() == ISD::UNDEF)
2687 return DAG.getNode(ISD::UNDEF, N->getValueType(0));
Chris Lattner66445d32006-03-28 22:11:53 +00002688 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
2689 // first operand.
2690 std::vector<SDOperand> MappedOps;
Chris Lattner66445d32006-03-28 22:11:53 +00002691 for (unsigned i = 0, e = ShufMask.getNumOperands(); i != e; ++i) {
Evan Chengc04766a2006-04-06 23:20:43 +00002692 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
2693 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) {
2694 MappedOps.push_back(ShufMask.getOperand(i));
2695 } else {
Chris Lattner66445d32006-03-28 22:11:53 +00002696 unsigned NewIdx =
2697 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
2698 MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32));
Chris Lattner66445d32006-03-28 22:11:53 +00002699 }
2700 }
2701 ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMask.getValueType(),
2702 MappedOps);
Chris Lattner3e104b12006-04-08 04:15:24 +00002703 AddToWorkList(ShufMask.Val);
Chris Lattner66445d32006-03-28 22:11:53 +00002704 return DAG.getNode(ISD::VECTOR_SHUFFLE, N->getValueType(0),
2705 N->getOperand(0),
2706 DAG.getNode(ISD::UNDEF, N->getValueType(0)),
2707 ShufMask);
2708 }
2709
2710 return SDOperand();
2711}
2712
Chris Lattnerf1d0c622006-03-31 22:16:43 +00002713SDOperand DAGCombiner::visitVVECTOR_SHUFFLE(SDNode *N) {
2714 SDOperand ShufMask = N->getOperand(2);
2715 unsigned NumElts = ShufMask.getNumOperands()-2;
2716
2717 // If the shuffle mask is an identity operation on the LHS, return the LHS.
2718 bool isIdentity = true;
2719 for (unsigned i = 0; i != NumElts; ++i) {
2720 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
2721 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
2722 isIdentity = false;
2723 break;
2724 }
2725 }
2726 if (isIdentity) return N->getOperand(0);
2727
2728 // If the shuffle mask is an identity operation on the RHS, return the RHS.
2729 isIdentity = true;
2730 for (unsigned i = 0; i != NumElts; ++i) {
2731 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
2732 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
2733 isIdentity = false;
2734 break;
2735 }
2736 }
2737 if (isIdentity) return N->getOperand(1);
2738
Chris Lattner17614ea2006-04-08 05:34:25 +00002739 // If the LHS and the RHS are the same node, turn the RHS into an undef.
2740 if (N->getOperand(0) == N->getOperand(1)) {
2741 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
2742 // first operand.
2743 std::vector<SDOperand> MappedOps;
2744 for (unsigned i = 0; i != NumElts; ++i) {
2745 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
2746 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) {
2747 MappedOps.push_back(ShufMask.getOperand(i));
2748 } else {
2749 unsigned NewIdx =
2750 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
2751 MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32));
2752 }
2753 }
2754 // Add the type/#elts values.
2755 MappedOps.push_back(ShufMask.getOperand(NumElts));
2756 MappedOps.push_back(ShufMask.getOperand(NumElts+1));
2757
2758 ShufMask = DAG.getNode(ISD::VBUILD_VECTOR, ShufMask.getValueType(),
2759 MappedOps);
2760 AddToWorkList(ShufMask.Val);
2761
2762 // Build the undef vector.
2763 SDOperand UDVal = DAG.getNode(ISD::UNDEF, MappedOps[0].getValueType());
2764 for (unsigned i = 0; i != NumElts; ++i)
2765 MappedOps[i] = UDVal;
2766 MappedOps[NumElts ] = *(N->getOperand(0).Val->op_end()-2);
2767 MappedOps[NumElts+1] = *(N->getOperand(0).Val->op_end()-1);
2768 UDVal = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, MappedOps);
2769
2770 return DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector,
2771 N->getOperand(0), UDVal, ShufMask,
2772 MappedOps[NumElts], MappedOps[NumElts+1]);
2773 }
2774
Chris Lattnerf1d0c622006-03-31 22:16:43 +00002775 return SDOperand();
2776}
2777
Evan Cheng44f1f092006-04-20 08:56:16 +00002778/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
2779/// a VAND to a vector_shuffle with the destination vector and a zero vector.
2780/// e.g. VAND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
2781/// vector_shuffle V, Zero, <0, 4, 2, 4>
2782SDOperand DAGCombiner::XformToShuffleWithZero(SDNode *N) {
2783 SDOperand LHS = N->getOperand(0);
2784 SDOperand RHS = N->getOperand(1);
2785 if (N->getOpcode() == ISD::VAND) {
2786 SDOperand DstVecSize = *(LHS.Val->op_end()-2);
2787 SDOperand DstVecEVT = *(LHS.Val->op_end()-1);
2788 if (RHS.getOpcode() == ISD::VBIT_CONVERT)
2789 RHS = RHS.getOperand(0);
2790 if (RHS.getOpcode() == ISD::VBUILD_VECTOR) {
2791 std::vector<SDOperand> IdxOps;
2792 unsigned NumOps = RHS.getNumOperands();
2793 unsigned NumElts = NumOps-2;
2794 MVT::ValueType EVT = cast<VTSDNode>(RHS.getOperand(NumOps-1))->getVT();
2795 for (unsigned i = 0; i != NumElts; ++i) {
2796 SDOperand Elt = RHS.getOperand(i);
2797 if (!isa<ConstantSDNode>(Elt))
2798 return SDOperand();
2799 else if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
2800 IdxOps.push_back(DAG.getConstant(i, EVT));
2801 else if (cast<ConstantSDNode>(Elt)->isNullValue())
2802 IdxOps.push_back(DAG.getConstant(NumElts, EVT));
2803 else
2804 return SDOperand();
2805 }
2806
2807 // Let's see if the target supports this vector_shuffle.
2808 if (!TLI.isVectorClearMaskLegal(IdxOps, EVT, DAG))
2809 return SDOperand();
2810
2811 // Return the new VVECTOR_SHUFFLE node.
2812 SDOperand NumEltsNode = DAG.getConstant(NumElts, MVT::i32);
2813 SDOperand EVTNode = DAG.getValueType(EVT);
2814 std::vector<SDOperand> Ops;
2815 LHS = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, LHS, NumEltsNode, EVTNode);
2816 Ops.push_back(LHS);
2817 AddToWorkList(LHS.Val);
2818 std::vector<SDOperand> ZeroOps(NumElts, DAG.getConstant(0, EVT));
2819 ZeroOps.push_back(NumEltsNode);
2820 ZeroOps.push_back(EVTNode);
2821 Ops.push_back(DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, ZeroOps));
2822 IdxOps.push_back(NumEltsNode);
2823 IdxOps.push_back(EVTNode);
2824 Ops.push_back(DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, IdxOps));
2825 Ops.push_back(NumEltsNode);
2826 Ops.push_back(EVTNode);
2827 SDOperand Result = DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector, Ops);
2828 if (NumEltsNode != DstVecSize || EVTNode != DstVecEVT) {
2829 Result = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Result,
2830 DstVecSize, DstVecEVT);
2831 }
2832 return Result;
2833 }
2834 }
2835 return SDOperand();
2836}
2837
Chris Lattneredab1b92006-04-02 03:25:57 +00002838/// visitVBinOp - Visit a binary vector operation, like VADD. IntOp indicates
2839/// the scalar operation of the vop if it is operating on an integer vector
2840/// (e.g. ADD) and FPOp indicates the FP version (e.g. FADD).
2841SDOperand DAGCombiner::visitVBinOp(SDNode *N, ISD::NodeType IntOp,
2842 ISD::NodeType FPOp) {
2843 MVT::ValueType EltType = cast<VTSDNode>(*(N->op_end()-1))->getVT();
2844 ISD::NodeType ScalarOp = MVT::isInteger(EltType) ? IntOp : FPOp;
2845 SDOperand LHS = N->getOperand(0);
2846 SDOperand RHS = N->getOperand(1);
Evan Cheng44f1f092006-04-20 08:56:16 +00002847 SDOperand Shuffle = XformToShuffleWithZero(N);
2848 if (Shuffle.Val) return Shuffle;
2849
Chris Lattneredab1b92006-04-02 03:25:57 +00002850 // If the LHS and RHS are VBUILD_VECTOR nodes, see if we can constant fold
2851 // this operation.
2852 if (LHS.getOpcode() == ISD::VBUILD_VECTOR &&
2853 RHS.getOpcode() == ISD::VBUILD_VECTOR) {
2854 std::vector<SDOperand> Ops;
2855 for (unsigned i = 0, e = LHS.getNumOperands()-2; i != e; ++i) {
2856 SDOperand LHSOp = LHS.getOperand(i);
2857 SDOperand RHSOp = RHS.getOperand(i);
2858 // If these two elements can't be folded, bail out.
2859 if ((LHSOp.getOpcode() != ISD::UNDEF &&
2860 LHSOp.getOpcode() != ISD::Constant &&
2861 LHSOp.getOpcode() != ISD::ConstantFP) ||
2862 (RHSOp.getOpcode() != ISD::UNDEF &&
2863 RHSOp.getOpcode() != ISD::Constant &&
2864 RHSOp.getOpcode() != ISD::ConstantFP))
2865 break;
2866 Ops.push_back(DAG.getNode(ScalarOp, EltType, LHSOp, RHSOp));
Chris Lattner3e104b12006-04-08 04:15:24 +00002867 AddToWorkList(Ops.back().Val);
Chris Lattneredab1b92006-04-02 03:25:57 +00002868 assert((Ops.back().getOpcode() == ISD::UNDEF ||
2869 Ops.back().getOpcode() == ISD::Constant ||
2870 Ops.back().getOpcode() == ISD::ConstantFP) &&
2871 "Scalar binop didn't fold!");
2872 }
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00002873
2874 if (Ops.size() == LHS.getNumOperands()-2) {
2875 Ops.push_back(*(LHS.Val->op_end()-2));
2876 Ops.push_back(*(LHS.Val->op_end()-1));
2877 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, Ops);
2878 }
Chris Lattneredab1b92006-04-02 03:25:57 +00002879 }
2880
2881 return SDOperand();
2882}
2883
Nate Begeman44728a72005-09-19 22:34:01 +00002884SDOperand DAGCombiner::SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2){
Nate Begemanf845b452005-10-08 00:29:44 +00002885 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
2886
2887 SDOperand SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
2888 cast<CondCodeSDNode>(N0.getOperand(2))->get());
2889 // If we got a simplified select_cc node back from SimplifySelectCC, then
2890 // break it down into a new SETCC node, and a new SELECT node, and then return
2891 // the SELECT node, since we were called with a SELECT node.
2892 if (SCC.Val) {
2893 // Check to see if we got a select_cc back (to turn into setcc/select).
2894 // Otherwise, just return whatever node we got back, like fabs.
2895 if (SCC.getOpcode() == ISD::SELECT_CC) {
2896 SDOperand SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
2897 SCC.getOperand(0), SCC.getOperand(1),
2898 SCC.getOperand(4));
Chris Lattner5750df92006-03-01 04:03:14 +00002899 AddToWorkList(SETCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00002900 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
2901 SCC.getOperand(3), SETCC);
2902 }
2903 return SCC;
2904 }
Nate Begeman44728a72005-09-19 22:34:01 +00002905 return SDOperand();
2906}
2907
Chris Lattner40c62d52005-10-18 06:04:22 +00002908/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
2909/// are the two values being selected between, see if we can simplify the
2910/// select.
2911///
2912bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDOperand LHS,
2913 SDOperand RHS) {
2914
2915 // If this is a select from two identical things, try to pull the operation
2916 // through the select.
2917 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
2918#if 0
2919 std::cerr << "SELECT: ["; LHS.Val->dump();
2920 std::cerr << "] ["; RHS.Val->dump();
2921 std::cerr << "]\n";
2922#endif
2923
2924 // If this is a load and the token chain is identical, replace the select
2925 // of two loads with a load through a select of the address to load from.
2926 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
2927 // constants have been dropped into the constant pool.
2928 if ((LHS.getOpcode() == ISD::LOAD ||
2929 LHS.getOpcode() == ISD::EXTLOAD ||
2930 LHS.getOpcode() == ISD::ZEXTLOAD ||
2931 LHS.getOpcode() == ISD::SEXTLOAD) &&
2932 // Token chains must be identical.
2933 LHS.getOperand(0) == RHS.getOperand(0) &&
2934 // If this is an EXTLOAD, the VT's must match.
2935 (LHS.getOpcode() == ISD::LOAD ||
2936 LHS.getOperand(3) == RHS.getOperand(3))) {
2937 // FIXME: this conflates two src values, discarding one. This is not
2938 // the right thing to do, but nothing uses srcvalues now. When they do,
2939 // turn SrcValue into a list of locations.
2940 SDOperand Addr;
2941 if (TheSelect->getOpcode() == ISD::SELECT)
2942 Addr = DAG.getNode(ISD::SELECT, LHS.getOperand(1).getValueType(),
2943 TheSelect->getOperand(0), LHS.getOperand(1),
2944 RHS.getOperand(1));
2945 else
2946 Addr = DAG.getNode(ISD::SELECT_CC, LHS.getOperand(1).getValueType(),
2947 TheSelect->getOperand(0),
2948 TheSelect->getOperand(1),
2949 LHS.getOperand(1), RHS.getOperand(1),
2950 TheSelect->getOperand(4));
2951
2952 SDOperand Load;
2953 if (LHS.getOpcode() == ISD::LOAD)
2954 Load = DAG.getLoad(TheSelect->getValueType(0), LHS.getOperand(0),
2955 Addr, LHS.getOperand(2));
2956 else
2957 Load = DAG.getExtLoad(LHS.getOpcode(), TheSelect->getValueType(0),
2958 LHS.getOperand(0), Addr, LHS.getOperand(2),
2959 cast<VTSDNode>(LHS.getOperand(3))->getVT());
2960 // Users of the select now use the result of the load.
2961 CombineTo(TheSelect, Load);
2962
2963 // Users of the old loads now use the new load's chain. We know the
2964 // old-load value is dead now.
2965 CombineTo(LHS.Val, Load.getValue(0), Load.getValue(1));
2966 CombineTo(RHS.Val, Load.getValue(0), Load.getValue(1));
2967 return true;
2968 }
2969 }
2970
2971 return false;
2972}
2973
Nate Begeman44728a72005-09-19 22:34:01 +00002974SDOperand DAGCombiner::SimplifySelectCC(SDOperand N0, SDOperand N1,
2975 SDOperand N2, SDOperand N3,
2976 ISD::CondCode CC) {
Nate Begemanf845b452005-10-08 00:29:44 +00002977
2978 MVT::ValueType VT = N2.getValueType();
2979 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
2980 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2981 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
2982 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
2983
2984 // Determine if the condition we're dealing with is constant
2985 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
2986 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
2987
2988 // fold select_cc true, x, y -> x
2989 if (SCCC && SCCC->getValue())
2990 return N2;
2991 // fold select_cc false, x, y -> y
2992 if (SCCC && SCCC->getValue() == 0)
2993 return N3;
2994
2995 // Check to see if we can simplify the select into an fabs node
2996 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
2997 // Allow either -0.0 or 0.0
2998 if (CFP->getValue() == 0.0) {
2999 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
3000 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
3001 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
3002 N2 == N3.getOperand(0))
3003 return DAG.getNode(ISD::FABS, VT, N0);
3004
3005 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
3006 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
3007 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
3008 N2.getOperand(0) == N3)
3009 return DAG.getNode(ISD::FABS, VT, N3);
3010 }
3011 }
3012
3013 // Check to see if we can perform the "gzip trick", transforming
3014 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
3015 if (N1C && N1C->isNullValue() && N3C && N3C->isNullValue() &&
3016 MVT::isInteger(N0.getValueType()) &&
3017 MVT::isInteger(N2.getValueType()) && CC == ISD::SETLT) {
3018 MVT::ValueType XType = N0.getValueType();
3019 MVT::ValueType AType = N2.getValueType();
3020 if (XType >= AType) {
3021 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00003022 // single-bit constant.
Nate Begemanf845b452005-10-08 00:29:44 +00003023 if (N2C && ((N2C->getValue() & (N2C->getValue()-1)) == 0)) {
3024 unsigned ShCtV = Log2_64(N2C->getValue());
3025 ShCtV = MVT::getSizeInBits(XType)-ShCtV-1;
3026 SDOperand ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
3027 SDOperand Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
Chris Lattner5750df92006-03-01 04:03:14 +00003028 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003029 if (XType > AType) {
3030 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00003031 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003032 }
3033 return DAG.getNode(ISD::AND, AType, Shift, N2);
3034 }
3035 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
3036 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3037 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00003038 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003039 if (XType > AType) {
3040 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00003041 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003042 }
3043 return DAG.getNode(ISD::AND, AType, Shift, N2);
3044 }
3045 }
Nate Begeman07ed4172005-10-10 21:26:48 +00003046
3047 // fold select C, 16, 0 -> shl C, 4
3048 if (N2C && N3C && N3C->isNullValue() && isPowerOf2_64(N2C->getValue()) &&
3049 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) {
3050 // Get a SetCC of the condition
3051 // FIXME: Should probably make sure that setcc is legal if we ever have a
3052 // target where it isn't.
Nate Begemanb0d04a72006-02-18 02:40:58 +00003053 SDOperand Temp, SCC;
Nate Begeman07ed4172005-10-10 21:26:48 +00003054 // cast from setcc result type to select result type
Nate Begemanb0d04a72006-02-18 02:40:58 +00003055 if (AfterLegalize) {
3056 SCC = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00003057 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
Nate Begemanb0d04a72006-02-18 02:40:58 +00003058 } else {
3059 SCC = DAG.getSetCC(MVT::i1, N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00003060 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00003061 }
Chris Lattner5750df92006-03-01 04:03:14 +00003062 AddToWorkList(SCC.Val);
3063 AddToWorkList(Temp.Val);
Nate Begeman07ed4172005-10-10 21:26:48 +00003064 // shl setcc result by log2 n2c
3065 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
3066 DAG.getConstant(Log2_64(N2C->getValue()),
3067 TLI.getShiftAmountTy()));
3068 }
3069
Nate Begemanf845b452005-10-08 00:29:44 +00003070 // Check to see if this is the equivalent of setcc
3071 // FIXME: Turn all of these into setcc if setcc if setcc is legal
3072 // otherwise, go ahead with the folds.
3073 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getValue() == 1ULL)) {
3074 MVT::ValueType XType = N0.getValueType();
3075 if (TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultTy())) {
3076 SDOperand Res = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
3077 if (Res.getValueType() != VT)
3078 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
3079 return Res;
3080 }
3081
3082 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
3083 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
3084 TLI.isOperationLegal(ISD::CTLZ, XType)) {
3085 SDOperand Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
3086 return DAG.getNode(ISD::SRL, XType, Ctlz,
3087 DAG.getConstant(Log2_32(MVT::getSizeInBits(XType)),
3088 TLI.getShiftAmountTy()));
3089 }
3090 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
3091 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
3092 SDOperand NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
3093 N0);
3094 SDOperand NotN0 = DAG.getNode(ISD::XOR, XType, N0,
3095 DAG.getConstant(~0ULL, XType));
3096 return DAG.getNode(ISD::SRL, XType,
3097 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
3098 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3099 TLI.getShiftAmountTy()));
3100 }
3101 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
3102 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
3103 SDOperand Sign = DAG.getNode(ISD::SRL, XType, N0,
3104 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3105 TLI.getShiftAmountTy()));
3106 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
3107 }
3108 }
3109
3110 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
3111 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
3112 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
3113 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1)) {
3114 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N2.getOperand(0))) {
3115 MVT::ValueType XType = N0.getValueType();
3116 if (SubC->isNullValue() && MVT::isInteger(XType)) {
3117 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
3118 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3119 TLI.getShiftAmountTy()));
3120 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00003121 AddToWorkList(Shift.Val);
3122 AddToWorkList(Add.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003123 return DAG.getNode(ISD::XOR, XType, Add, Shift);
3124 }
3125 }
3126 }
3127
Nate Begeman44728a72005-09-19 22:34:01 +00003128 return SDOperand();
3129}
3130
Nate Begeman452d7be2005-09-16 00:54:12 +00003131SDOperand DAGCombiner::SimplifySetCC(MVT::ValueType VT, SDOperand N0,
Nate Begemane17daeb2005-10-05 21:43:42 +00003132 SDOperand N1, ISD::CondCode Cond,
3133 bool foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00003134 // These setcc operations always fold.
3135 switch (Cond) {
3136 default: break;
3137 case ISD::SETFALSE:
3138 case ISD::SETFALSE2: return DAG.getConstant(0, VT);
3139 case ISD::SETTRUE:
3140 case ISD::SETTRUE2: return DAG.getConstant(1, VT);
3141 }
3142
3143 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
3144 uint64_t C1 = N1C->getValue();
3145 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val)) {
3146 uint64_t C0 = N0C->getValue();
3147
3148 // Sign extend the operands if required
3149 if (ISD::isSignedIntSetCC(Cond)) {
3150 C0 = N0C->getSignExtended();
3151 C1 = N1C->getSignExtended();
3152 }
3153
3154 switch (Cond) {
3155 default: assert(0 && "Unknown integer setcc!");
3156 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
3157 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
3158 case ISD::SETULT: return DAG.getConstant(C0 < C1, VT);
3159 case ISD::SETUGT: return DAG.getConstant(C0 > C1, VT);
3160 case ISD::SETULE: return DAG.getConstant(C0 <= C1, VT);
3161 case ISD::SETUGE: return DAG.getConstant(C0 >= C1, VT);
3162 case ISD::SETLT: return DAG.getConstant((int64_t)C0 < (int64_t)C1, VT);
3163 case ISD::SETGT: return DAG.getConstant((int64_t)C0 > (int64_t)C1, VT);
3164 case ISD::SETLE: return DAG.getConstant((int64_t)C0 <= (int64_t)C1, VT);
3165 case ISD::SETGE: return DAG.getConstant((int64_t)C0 >= (int64_t)C1, VT);
3166 }
3167 } else {
3168 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
3169 if (N0.getOpcode() == ISD::ZERO_EXTEND) {
3170 unsigned InSize = MVT::getSizeInBits(N0.getOperand(0).getValueType());
3171
3172 // If the comparison constant has bits in the upper part, the
3173 // zero-extended value could never match.
3174 if (C1 & (~0ULL << InSize)) {
3175 unsigned VSize = MVT::getSizeInBits(N0.getValueType());
3176 switch (Cond) {
3177 case ISD::SETUGT:
3178 case ISD::SETUGE:
3179 case ISD::SETEQ: return DAG.getConstant(0, VT);
3180 case ISD::SETULT:
3181 case ISD::SETULE:
3182 case ISD::SETNE: return DAG.getConstant(1, VT);
3183 case ISD::SETGT:
3184 case ISD::SETGE:
3185 // True if the sign bit of C1 is set.
3186 return DAG.getConstant((C1 & (1ULL << VSize)) != 0, VT);
3187 case ISD::SETLT:
3188 case ISD::SETLE:
3189 // True if the sign bit of C1 isn't set.
3190 return DAG.getConstant((C1 & (1ULL << VSize)) == 0, VT);
3191 default:
3192 break;
3193 }
3194 }
3195
3196 // Otherwise, we can perform the comparison with the low bits.
3197 switch (Cond) {
3198 case ISD::SETEQ:
3199 case ISD::SETNE:
3200 case ISD::SETUGT:
3201 case ISD::SETUGE:
3202 case ISD::SETULT:
3203 case ISD::SETULE:
3204 return DAG.getSetCC(VT, N0.getOperand(0),
3205 DAG.getConstant(C1, N0.getOperand(0).getValueType()),
3206 Cond);
3207 default:
3208 break; // todo, be more careful with signed comparisons
3209 }
3210 } else if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
3211 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
3212 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N0.getOperand(1))->getVT();
3213 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
3214 MVT::ValueType ExtDstTy = N0.getValueType();
3215 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
3216
3217 // If the extended part has any inconsistent bits, it cannot ever
3218 // compare equal. In other words, they have to be all ones or all
3219 // zeros.
3220 uint64_t ExtBits =
3221 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
3222 if ((C1 & ExtBits) != 0 && (C1 & ExtBits) != ExtBits)
3223 return DAG.getConstant(Cond == ISD::SETNE, VT);
3224
3225 SDOperand ZextOp;
3226 MVT::ValueType Op0Ty = N0.getOperand(0).getValueType();
3227 if (Op0Ty == ExtSrcTy) {
3228 ZextOp = N0.getOperand(0);
3229 } else {
3230 int64_t Imm = ~0ULL >> (64-ExtSrcTyBits);
3231 ZextOp = DAG.getNode(ISD::AND, Op0Ty, N0.getOperand(0),
3232 DAG.getConstant(Imm, Op0Ty));
3233 }
Chris Lattner5750df92006-03-01 04:03:14 +00003234 AddToWorkList(ZextOp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003235 // Otherwise, make this a use of a zext.
3236 return DAG.getSetCC(VT, ZextOp,
3237 DAG.getConstant(C1 & (~0ULL>>(64-ExtSrcTyBits)),
3238 ExtDstTy),
3239 Cond);
Chris Lattner3391bcd2006-02-08 02:13:15 +00003240 } else if ((N1C->getValue() == 0 || N1C->getValue() == 1) &&
3241 (Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
3242 (N0.getOpcode() == ISD::XOR ||
3243 (N0.getOpcode() == ISD::AND &&
3244 N0.getOperand(0).getOpcode() == ISD::XOR &&
3245 N0.getOperand(1) == N0.getOperand(0).getOperand(1))) &&
3246 isa<ConstantSDNode>(N0.getOperand(1)) &&
3247 cast<ConstantSDNode>(N0.getOperand(1))->getValue() == 1) {
3248 // If this is (X^1) == 0/1, swap the RHS and eliminate the xor. We can
3249 // only do this if the top bits are known zero.
3250 if (TLI.MaskedValueIsZero(N1,
3251 MVT::getIntVTBitMask(N0.getValueType())-1)) {
3252 // Okay, get the un-inverted input value.
3253 SDOperand Val;
3254 if (N0.getOpcode() == ISD::XOR)
3255 Val = N0.getOperand(0);
3256 else {
3257 assert(N0.getOpcode() == ISD::AND &&
3258 N0.getOperand(0).getOpcode() == ISD::XOR);
3259 // ((X^1)&1)^1 -> X & 1
3260 Val = DAG.getNode(ISD::AND, N0.getValueType(),
3261 N0.getOperand(0).getOperand(0), N0.getOperand(1));
3262 }
3263 return DAG.getSetCC(VT, Val, N1,
3264 Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ);
3265 }
Nate Begeman452d7be2005-09-16 00:54:12 +00003266 }
Chris Lattner5c46f742005-10-05 06:11:08 +00003267
Nate Begeman452d7be2005-09-16 00:54:12 +00003268 uint64_t MinVal, MaxVal;
3269 unsigned OperandBitSize = MVT::getSizeInBits(N1C->getValueType(0));
3270 if (ISD::isSignedIntSetCC(Cond)) {
3271 MinVal = 1ULL << (OperandBitSize-1);
3272 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
3273 MaxVal = ~0ULL >> (65-OperandBitSize);
3274 else
3275 MaxVal = 0;
3276 } else {
3277 MinVal = 0;
3278 MaxVal = ~0ULL >> (64-OperandBitSize);
3279 }
3280
3281 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
3282 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
3283 if (C1 == MinVal) return DAG.getConstant(1, VT); // X >= MIN --> true
3284 --C1; // X >= C0 --> X > (C0-1)
3285 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
3286 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
3287 }
3288
3289 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
3290 if (C1 == MaxVal) return DAG.getConstant(1, VT); // X <= MAX --> true
3291 ++C1; // X <= C0 --> X < (C0+1)
3292 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
3293 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
3294 }
3295
3296 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal)
3297 return DAG.getConstant(0, VT); // X < MIN --> false
3298
3299 // Canonicalize setgt X, Min --> setne X, Min
3300 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MinVal)
3301 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
Chris Lattnerc8597ca2005-10-21 21:23:25 +00003302 // Canonicalize setlt X, Max --> setne X, Max
3303 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MaxVal)
3304 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
Nate Begeman452d7be2005-09-16 00:54:12 +00003305
3306 // If we have setult X, 1, turn it into seteq X, 0
3307 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal+1)
3308 return DAG.getSetCC(VT, N0, DAG.getConstant(MinVal, N0.getValueType()),
3309 ISD::SETEQ);
3310 // If we have setugt X, Max-1, turn it into seteq X, Max
3311 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal-1)
3312 return DAG.getSetCC(VT, N0, DAG.getConstant(MaxVal, N0.getValueType()),
3313 ISD::SETEQ);
3314
3315 // If we have "setcc X, C0", check to see if we can shrink the immediate
3316 // by changing cc.
3317
3318 // SETUGT X, SINTMAX -> SETLT X, 0
3319 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
3320 C1 == (~0ULL >> (65-OperandBitSize)))
3321 return DAG.getSetCC(VT, N0, DAG.getConstant(0, N1.getValueType()),
3322 ISD::SETLT);
3323
3324 // FIXME: Implement the rest of these.
3325
3326 // Fold bit comparisons when we can.
3327 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
3328 VT == N0.getValueType() && N0.getOpcode() == ISD::AND)
3329 if (ConstantSDNode *AndRHS =
3330 dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
3331 if (Cond == ISD::SETNE && C1 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
3332 // Perform the xform if the AND RHS is a single bit.
3333 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
3334 return DAG.getNode(ISD::SRL, VT, N0,
3335 DAG.getConstant(Log2_64(AndRHS->getValue()),
3336 TLI.getShiftAmountTy()));
3337 }
3338 } else if (Cond == ISD::SETEQ && C1 == AndRHS->getValue()) {
3339 // (X & 8) == 8 --> (X & 8) >> 3
3340 // Perform the xform if C1 is a single bit.
3341 if ((C1 & (C1-1)) == 0) {
3342 return DAG.getNode(ISD::SRL, VT, N0,
3343 DAG.getConstant(Log2_64(C1),TLI.getShiftAmountTy()));
3344 }
3345 }
3346 }
3347 }
3348 } else if (isa<ConstantSDNode>(N0.Val)) {
3349 // Ensure that the constant occurs on the RHS.
3350 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
3351 }
3352
3353 if (ConstantFPSDNode *N0C = dyn_cast<ConstantFPSDNode>(N0.Val))
3354 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) {
3355 double C0 = N0C->getValue(), C1 = N1C->getValue();
3356
3357 switch (Cond) {
3358 default: break; // FIXME: Implement the rest of these!
3359 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
3360 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
3361 case ISD::SETLT: return DAG.getConstant(C0 < C1, VT);
3362 case ISD::SETGT: return DAG.getConstant(C0 > C1, VT);
3363 case ISD::SETLE: return DAG.getConstant(C0 <= C1, VT);
3364 case ISD::SETGE: return DAG.getConstant(C0 >= C1, VT);
3365 }
3366 } else {
3367 // Ensure that the constant occurs on the RHS.
3368 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
3369 }
3370
3371 if (N0 == N1) {
3372 // We can always fold X == Y for integer setcc's.
3373 if (MVT::isInteger(N0.getValueType()))
3374 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
3375 unsigned UOF = ISD::getUnorderedFlavor(Cond);
3376 if (UOF == 2) // FP operators that are undefined on NaNs.
3377 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
3378 if (UOF == unsigned(ISD::isTrueWhenEqual(Cond)))
3379 return DAG.getConstant(UOF, VT);
3380 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
3381 // if it is not already.
Chris Lattner4090aee2006-01-18 19:13:41 +00003382 ISD::CondCode NewCond = UOF == 0 ? ISD::SETO : ISD::SETUO;
Nate Begeman452d7be2005-09-16 00:54:12 +00003383 if (NewCond != Cond)
3384 return DAG.getSetCC(VT, N0, N1, NewCond);
3385 }
3386
3387 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
3388 MVT::isInteger(N0.getValueType())) {
3389 if (N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::SUB ||
3390 N0.getOpcode() == ISD::XOR) {
3391 // Simplify (X+Y) == (X+Z) --> Y == Z
3392 if (N0.getOpcode() == N1.getOpcode()) {
3393 if (N0.getOperand(0) == N1.getOperand(0))
3394 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(1), Cond);
3395 if (N0.getOperand(1) == N1.getOperand(1))
3396 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(0), Cond);
3397 if (isCommutativeBinOp(N0.getOpcode())) {
3398 // If X op Y == Y op X, try other combinations.
3399 if (N0.getOperand(0) == N1.getOperand(1))
3400 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(0), Cond);
3401 if (N0.getOperand(1) == N1.getOperand(0))
Chris Lattnera158eee2005-10-25 18:57:30 +00003402 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(1), Cond);
Nate Begeman452d7be2005-09-16 00:54:12 +00003403 }
3404 }
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003405
3406 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(N1)) {
3407 if (ConstantSDNode *LHSR = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
3408 // Turn (X+C1) == C2 --> X == C2-C1
3409 if (N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse()) {
3410 return DAG.getSetCC(VT, N0.getOperand(0),
3411 DAG.getConstant(RHSC->getValue()-LHSR->getValue(),
3412 N0.getValueType()), Cond);
3413 }
3414
3415 // Turn (X^C1) == C2 into X == C1^C2 iff X&~C1 = 0.
3416 if (N0.getOpcode() == ISD::XOR)
Chris Lattner5c46f742005-10-05 06:11:08 +00003417 // If we know that all of the inverted bits are zero, don't bother
3418 // performing the inversion.
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003419 if (TLI.MaskedValueIsZero(N0.getOperand(0), ~LHSR->getValue()))
Chris Lattner5c46f742005-10-05 06:11:08 +00003420 return DAG.getSetCC(VT, N0.getOperand(0),
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003421 DAG.getConstant(LHSR->getValue()^RHSC->getValue(),
Chris Lattner5c46f742005-10-05 06:11:08 +00003422 N0.getValueType()), Cond);
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003423 }
3424
3425 // Turn (C1-X) == C2 --> X == C1-C2
3426 if (ConstantSDNode *SUBC = dyn_cast<ConstantSDNode>(N0.getOperand(0))) {
3427 if (N0.getOpcode() == ISD::SUB && N0.Val->hasOneUse()) {
3428 return DAG.getSetCC(VT, N0.getOperand(1),
3429 DAG.getConstant(SUBC->getValue()-RHSC->getValue(),
3430 N0.getValueType()), Cond);
Chris Lattner5c46f742005-10-05 06:11:08 +00003431 }
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003432 }
3433 }
3434
Nate Begeman452d7be2005-09-16 00:54:12 +00003435 // Simplify (X+Z) == X --> Z == 0
3436 if (N0.getOperand(0) == N1)
3437 return DAG.getSetCC(VT, N0.getOperand(1),
3438 DAG.getConstant(0, N0.getValueType()), Cond);
3439 if (N0.getOperand(1) == N1) {
3440 if (isCommutativeBinOp(N0.getOpcode()))
3441 return DAG.getSetCC(VT, N0.getOperand(0),
3442 DAG.getConstant(0, N0.getValueType()), Cond);
3443 else {
3444 assert(N0.getOpcode() == ISD::SUB && "Unexpected operation!");
3445 // (Z-X) == X --> Z == X<<1
3446 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(),
3447 N1,
3448 DAG.getConstant(1,TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00003449 AddToWorkList(SH.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003450 return DAG.getSetCC(VT, N0.getOperand(0), SH, Cond);
3451 }
3452 }
3453 }
3454
3455 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
3456 N1.getOpcode() == ISD::XOR) {
3457 // Simplify X == (X+Z) --> Z == 0
3458 if (N1.getOperand(0) == N0) {
3459 return DAG.getSetCC(VT, N1.getOperand(1),
3460 DAG.getConstant(0, N1.getValueType()), Cond);
3461 } else if (N1.getOperand(1) == N0) {
3462 if (isCommutativeBinOp(N1.getOpcode())) {
3463 return DAG.getSetCC(VT, N1.getOperand(0),
3464 DAG.getConstant(0, N1.getValueType()), Cond);
3465 } else {
3466 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
3467 // X == (Z-X) --> X<<1 == Z
3468 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(), N0,
3469 DAG.getConstant(1,TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00003470 AddToWorkList(SH.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003471 return DAG.getSetCC(VT, SH, N1.getOperand(0), Cond);
3472 }
3473 }
3474 }
3475 }
3476
3477 // Fold away ALL boolean setcc's.
3478 SDOperand Temp;
Nate Begemane17daeb2005-10-05 21:43:42 +00003479 if (N0.getValueType() == MVT::i1 && foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00003480 switch (Cond) {
3481 default: assert(0 && "Unknown integer setcc!");
3482 case ISD::SETEQ: // X == Y -> (X^Y)^1
3483 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
3484 N0 = DAG.getNode(ISD::XOR, MVT::i1, Temp, DAG.getConstant(1, MVT::i1));
Chris Lattner5750df92006-03-01 04:03:14 +00003485 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003486 break;
3487 case ISD::SETNE: // X != Y --> (X^Y)
3488 N0 = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
3489 break;
3490 case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> X^1 & Y
3491 case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> X^1 & Y
3492 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
3493 N0 = DAG.getNode(ISD::AND, MVT::i1, N1, Temp);
Chris Lattner5750df92006-03-01 04:03:14 +00003494 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003495 break;
3496 case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> Y^1 & X
3497 case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> Y^1 & X
3498 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
3499 N0 = DAG.getNode(ISD::AND, MVT::i1, N0, Temp);
Chris Lattner5750df92006-03-01 04:03:14 +00003500 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003501 break;
3502 case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> X^1 | Y
3503 case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> X^1 | Y
3504 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
3505 N0 = DAG.getNode(ISD::OR, MVT::i1, N1, Temp);
Chris Lattner5750df92006-03-01 04:03:14 +00003506 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003507 break;
3508 case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> Y^1 | X
3509 case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> Y^1 | X
3510 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
3511 N0 = DAG.getNode(ISD::OR, MVT::i1, N0, Temp);
3512 break;
3513 }
3514 if (VT != MVT::i1) {
Chris Lattner5750df92006-03-01 04:03:14 +00003515 AddToWorkList(N0.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003516 // FIXME: If running after legalize, we probably can't do this.
3517 N0 = DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
3518 }
3519 return N0;
3520 }
3521
3522 // Could not fold it.
3523 return SDOperand();
3524}
3525
Nate Begeman69575232005-10-20 02:15:44 +00003526/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
3527/// return a DAG expression to select that will generate the same value by
3528/// multiplying by a magic number. See:
3529/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
3530SDOperand DAGCombiner::BuildSDIV(SDNode *N) {
3531 MVT::ValueType VT = N->getValueType(0);
Chris Lattnere9936d12005-10-22 18:50:15 +00003532
3533 // Check to see if we can do this.
3534 if (!TLI.isTypeLegal(VT) || (VT != MVT::i32 && VT != MVT::i64))
3535 return SDOperand(); // BuildSDIV only operates on i32 or i64
3536 if (!TLI.isOperationLegal(ISD::MULHS, VT))
3537 return SDOperand(); // Make sure the target supports MULHS.
Nate Begeman69575232005-10-20 02:15:44 +00003538
Nate Begemanc6a454e2005-10-20 17:45:03 +00003539 int64_t d = cast<ConstantSDNode>(N->getOperand(1))->getSignExtended();
Nate Begeman69575232005-10-20 02:15:44 +00003540 ms magics = (VT == MVT::i32) ? magic32(d) : magic64(d);
3541
3542 // Multiply the numerator (operand 0) by the magic value
3543 SDOperand Q = DAG.getNode(ISD::MULHS, VT, N->getOperand(0),
3544 DAG.getConstant(magics.m, VT));
3545 // If d > 0 and m < 0, add the numerator
3546 if (d > 0 && magics.m < 0) {
3547 Q = DAG.getNode(ISD::ADD, VT, Q, N->getOperand(0));
Chris Lattner5750df92006-03-01 04:03:14 +00003548 AddToWorkList(Q.Val);
Nate Begeman69575232005-10-20 02:15:44 +00003549 }
3550 // If d < 0 and m > 0, subtract the numerator.
3551 if (d < 0 && magics.m > 0) {
3552 Q = DAG.getNode(ISD::SUB, VT, Q, N->getOperand(0));
Chris Lattner5750df92006-03-01 04:03:14 +00003553 AddToWorkList(Q.Val);
Nate Begeman69575232005-10-20 02:15:44 +00003554 }
3555 // Shift right algebraic if shift value is nonzero
3556 if (magics.s > 0) {
3557 Q = DAG.getNode(ISD::SRA, VT, Q,
3558 DAG.getConstant(magics.s, TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00003559 AddToWorkList(Q.Val);
Nate Begeman69575232005-10-20 02:15:44 +00003560 }
3561 // Extract the sign bit and add it to the quotient
3562 SDOperand T =
Nate Begeman4d385672005-10-21 01:51:45 +00003563 DAG.getNode(ISD::SRL, VT, Q, DAG.getConstant(MVT::getSizeInBits(VT)-1,
3564 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00003565 AddToWorkList(T.Val);
Nate Begeman69575232005-10-20 02:15:44 +00003566 return DAG.getNode(ISD::ADD, VT, Q, T);
3567}
3568
3569/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
3570/// return a DAG expression to select that will generate the same value by
3571/// multiplying by a magic number. See:
3572/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
3573SDOperand DAGCombiner::BuildUDIV(SDNode *N) {
3574 MVT::ValueType VT = N->getValueType(0);
Chris Lattnere9936d12005-10-22 18:50:15 +00003575
3576 // Check to see if we can do this.
3577 if (!TLI.isTypeLegal(VT) || (VT != MVT::i32 && VT != MVT::i64))
3578 return SDOperand(); // BuildUDIV only operates on i32 or i64
3579 if (!TLI.isOperationLegal(ISD::MULHU, VT))
3580 return SDOperand(); // Make sure the target supports MULHU.
Nate Begeman69575232005-10-20 02:15:44 +00003581
3582 uint64_t d = cast<ConstantSDNode>(N->getOperand(1))->getValue();
3583 mu magics = (VT == MVT::i32) ? magicu32(d) : magicu64(d);
3584
3585 // Multiply the numerator (operand 0) by the magic value
3586 SDOperand Q = DAG.getNode(ISD::MULHU, VT, N->getOperand(0),
3587 DAG.getConstant(magics.m, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00003588 AddToWorkList(Q.Val);
Nate Begeman69575232005-10-20 02:15:44 +00003589
3590 if (magics.a == 0) {
3591 return DAG.getNode(ISD::SRL, VT, Q,
3592 DAG.getConstant(magics.s, TLI.getShiftAmountTy()));
3593 } else {
3594 SDOperand NPQ = DAG.getNode(ISD::SUB, VT, N->getOperand(0), Q);
Chris Lattner5750df92006-03-01 04:03:14 +00003595 AddToWorkList(NPQ.Val);
Nate Begeman69575232005-10-20 02:15:44 +00003596 NPQ = DAG.getNode(ISD::SRL, VT, NPQ,
3597 DAG.getConstant(1, TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00003598 AddToWorkList(NPQ.Val);
Nate Begeman69575232005-10-20 02:15:44 +00003599 NPQ = DAG.getNode(ISD::ADD, VT, NPQ, Q);
Chris Lattner5750df92006-03-01 04:03:14 +00003600 AddToWorkList(NPQ.Val);
Nate Begeman69575232005-10-20 02:15:44 +00003601 return DAG.getNode(ISD::SRL, VT, NPQ,
3602 DAG.getConstant(magics.s-1, TLI.getShiftAmountTy()));
3603 }
3604}
3605
Nate Begeman1d4d4142005-09-01 00:19:25 +00003606// SelectionDAG::Combine - This is the entry point for the file.
3607//
Nate Begeman4ebd8052005-09-01 23:24:04 +00003608void SelectionDAG::Combine(bool RunningAfterLegalize) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003609 /// run - This is the main entry point to this class.
3610 ///
Nate Begeman4ebd8052005-09-01 23:24:04 +00003611 DAGCombiner(*this).Run(RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003612}