blob: 68ebf7a78b71530207f4c2c7a36f7ea83ac5b956 [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);
Chris Lattner35e5c142006-05-05 05:51:50 +0000225 SDOperand SimplifyBinOpWithSameOpcodeHands(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000226 SDOperand SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2);
227 SDOperand SimplifySelectCC(SDOperand N0, SDOperand N1, SDOperand N2,
228 SDOperand N3, ISD::CondCode CC);
Nate Begeman452d7be2005-09-16 00:54:12 +0000229 SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N0, SDOperand N1,
Nate Begemane17daeb2005-10-05 21:43:42 +0000230 ISD::CondCode Cond, bool foldBooleans = true);
Chris Lattner6258fb22006-04-02 02:53:43 +0000231 SDOperand ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(SDNode *, MVT::ValueType);
Nate Begeman69575232005-10-20 02:15:44 +0000232 SDOperand BuildSDIV(SDNode *N);
233 SDOperand BuildUDIV(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000234public:
235 DAGCombiner(SelectionDAG &D)
Nate Begeman646d7e22005-09-02 21:18:40 +0000236 : DAG(D), TLI(D.getTargetLoweringInfo()), AfterLegalize(false) {}
Nate Begeman1d4d4142005-09-01 00:19:25 +0000237
238 /// Run - runs the dag combiner on all nodes in the work list
Nate Begeman4ebd8052005-09-01 23:24:04 +0000239 void Run(bool RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000240 };
241}
242
Chris Lattner24664722006-03-01 04:53:38 +0000243//===----------------------------------------------------------------------===//
244// TargetLowering::DAGCombinerInfo implementation
245//===----------------------------------------------------------------------===//
246
247void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) {
248 ((DAGCombiner*)DC)->AddToWorkList(N);
249}
250
251SDOperand TargetLowering::DAGCombinerInfo::
252CombineTo(SDNode *N, const std::vector<SDOperand> &To) {
253 return ((DAGCombiner*)DC)->CombineTo(N, To);
254}
255
256SDOperand TargetLowering::DAGCombinerInfo::
257CombineTo(SDNode *N, SDOperand Res) {
258 return ((DAGCombiner*)DC)->CombineTo(N, Res);
259}
260
261
262SDOperand TargetLowering::DAGCombinerInfo::
263CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1) {
264 return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1);
265}
266
267
268
269
270//===----------------------------------------------------------------------===//
271
272
Nate Begeman69575232005-10-20 02:15:44 +0000273struct ms {
274 int64_t m; // magic number
275 int64_t s; // shift amount
276};
277
278struct mu {
279 uint64_t m; // magic number
280 int64_t a; // add indicator
281 int64_t s; // shift amount
282};
283
284/// magic - calculate the magic numbers required to codegen an integer sdiv as
285/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
286/// or -1.
287static ms magic32(int32_t d) {
288 int32_t p;
289 uint32_t ad, anc, delta, q1, r1, q2, r2, t;
290 const uint32_t two31 = 0x80000000U;
291 struct ms mag;
292
293 ad = abs(d);
294 t = two31 + ((uint32_t)d >> 31);
295 anc = t - 1 - t%ad; // absolute value of nc
296 p = 31; // initialize p
297 q1 = two31/anc; // initialize q1 = 2p/abs(nc)
298 r1 = two31 - q1*anc; // initialize r1 = rem(2p,abs(nc))
299 q2 = two31/ad; // initialize q2 = 2p/abs(d)
300 r2 = two31 - q2*ad; // initialize r2 = rem(2p,abs(d))
301 do {
302 p = p + 1;
303 q1 = 2*q1; // update q1 = 2p/abs(nc)
304 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
305 if (r1 >= anc) { // must be unsigned comparison
306 q1 = q1 + 1;
307 r1 = r1 - anc;
308 }
309 q2 = 2*q2; // update q2 = 2p/abs(d)
310 r2 = 2*r2; // update r2 = rem(2p/abs(d))
311 if (r2 >= ad) { // must be unsigned comparison
312 q2 = q2 + 1;
313 r2 = r2 - ad;
314 }
315 delta = ad - r2;
316 } while (q1 < delta || (q1 == delta && r1 == 0));
317
318 mag.m = (int32_t)(q2 + 1); // make sure to sign extend
319 if (d < 0) mag.m = -mag.m; // resulting magic number
320 mag.s = p - 32; // resulting shift
321 return mag;
322}
323
324/// magicu - calculate the magic numbers required to codegen an integer udiv as
325/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
326static mu magicu32(uint32_t d) {
327 int32_t p;
328 uint32_t nc, delta, q1, r1, q2, r2;
329 struct mu magu;
330 magu.a = 0; // initialize "add" indicator
331 nc = - 1 - (-d)%d;
332 p = 31; // initialize p
333 q1 = 0x80000000/nc; // initialize q1 = 2p/nc
334 r1 = 0x80000000 - q1*nc; // initialize r1 = rem(2p,nc)
335 q2 = 0x7FFFFFFF/d; // initialize q2 = (2p-1)/d
336 r2 = 0x7FFFFFFF - q2*d; // initialize r2 = rem((2p-1),d)
337 do {
338 p = p + 1;
339 if (r1 >= nc - r1 ) {
340 q1 = 2*q1 + 1; // update q1
341 r1 = 2*r1 - nc; // update r1
342 }
343 else {
344 q1 = 2*q1; // update q1
345 r1 = 2*r1; // update r1
346 }
347 if (r2 + 1 >= d - r2) {
348 if (q2 >= 0x7FFFFFFF) magu.a = 1;
349 q2 = 2*q2 + 1; // update q2
350 r2 = 2*r2 + 1 - d; // update r2
351 }
352 else {
353 if (q2 >= 0x80000000) magu.a = 1;
354 q2 = 2*q2; // update q2
355 r2 = 2*r2 + 1; // update r2
356 }
357 delta = d - 1 - r2;
358 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
359 magu.m = q2 + 1; // resulting magic number
360 magu.s = p - 32; // resulting shift
361 return magu;
362}
363
364/// magic - calculate the magic numbers required to codegen an integer sdiv as
365/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
366/// or -1.
367static ms magic64(int64_t d) {
368 int64_t p;
369 uint64_t ad, anc, delta, q1, r1, q2, r2, t;
370 const uint64_t two63 = 9223372036854775808ULL; // 2^63
371 struct ms mag;
372
Chris Lattnerf75f2a02005-10-20 17:01:00 +0000373 ad = d >= 0 ? d : -d;
Nate Begeman69575232005-10-20 02:15:44 +0000374 t = two63 + ((uint64_t)d >> 63);
375 anc = t - 1 - t%ad; // absolute value of nc
376 p = 63; // initialize p
377 q1 = two63/anc; // initialize q1 = 2p/abs(nc)
378 r1 = two63 - q1*anc; // initialize r1 = rem(2p,abs(nc))
379 q2 = two63/ad; // initialize q2 = 2p/abs(d)
380 r2 = two63 - q2*ad; // initialize r2 = rem(2p,abs(d))
381 do {
382 p = p + 1;
383 q1 = 2*q1; // update q1 = 2p/abs(nc)
384 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
385 if (r1 >= anc) { // must be unsigned comparison
386 q1 = q1 + 1;
387 r1 = r1 - anc;
388 }
389 q2 = 2*q2; // update q2 = 2p/abs(d)
390 r2 = 2*r2; // update r2 = rem(2p/abs(d))
391 if (r2 >= ad) { // must be unsigned comparison
392 q2 = q2 + 1;
393 r2 = r2 - ad;
394 }
395 delta = ad - r2;
396 } while (q1 < delta || (q1 == delta && r1 == 0));
397
398 mag.m = q2 + 1;
399 if (d < 0) mag.m = -mag.m; // resulting magic number
400 mag.s = p - 64; // resulting shift
401 return mag;
402}
403
404/// magicu - calculate the magic numbers required to codegen an integer udiv as
405/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
406static mu magicu64(uint64_t d)
407{
408 int64_t p;
409 uint64_t nc, delta, q1, r1, q2, r2;
410 struct mu magu;
411 magu.a = 0; // initialize "add" indicator
412 nc = - 1 - (-d)%d;
413 p = 63; // initialize p
414 q1 = 0x8000000000000000ull/nc; // initialize q1 = 2p/nc
415 r1 = 0x8000000000000000ull - q1*nc; // initialize r1 = rem(2p,nc)
416 q2 = 0x7FFFFFFFFFFFFFFFull/d; // initialize q2 = (2p-1)/d
417 r2 = 0x7FFFFFFFFFFFFFFFull - q2*d; // initialize r2 = rem((2p-1),d)
418 do {
419 p = p + 1;
420 if (r1 >= nc - r1 ) {
421 q1 = 2*q1 + 1; // update q1
422 r1 = 2*r1 - nc; // update r1
423 }
424 else {
425 q1 = 2*q1; // update q1
426 r1 = 2*r1; // update r1
427 }
428 if (r2 + 1 >= d - r2) {
429 if (q2 >= 0x7FFFFFFFFFFFFFFFull) magu.a = 1;
430 q2 = 2*q2 + 1; // update q2
431 r2 = 2*r2 + 1 - d; // update r2
432 }
433 else {
434 if (q2 >= 0x8000000000000000ull) magu.a = 1;
435 q2 = 2*q2; // update q2
436 r2 = 2*r2 + 1; // update r2
437 }
438 delta = d - 1 - r2;
439 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
440 magu.m = q2 + 1; // resulting magic number
441 magu.s = p - 64; // resulting shift
442 return magu;
443}
444
Nate Begeman4ebd8052005-09-01 23:24:04 +0000445// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
446// that selects between the values 1 and 0, making it equivalent to a setcc.
Nate Begeman646d7e22005-09-02 21:18:40 +0000447// Also, set the incoming LHS, RHS, and CC references to the appropriate
448// nodes based on the type of node we are checking. This simplifies life a
449// bit for the callers.
450static bool isSetCCEquivalent(SDOperand N, SDOperand &LHS, SDOperand &RHS,
451 SDOperand &CC) {
452 if (N.getOpcode() == ISD::SETCC) {
453 LHS = N.getOperand(0);
454 RHS = N.getOperand(1);
455 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000456 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000457 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000458 if (N.getOpcode() == ISD::SELECT_CC &&
459 N.getOperand(2).getOpcode() == ISD::Constant &&
460 N.getOperand(3).getOpcode() == ISD::Constant &&
461 cast<ConstantSDNode>(N.getOperand(2))->getValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000462 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
463 LHS = N.getOperand(0);
464 RHS = N.getOperand(1);
465 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000466 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000467 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000468 return false;
469}
470
Nate Begeman99801192005-09-07 23:25:52 +0000471// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
472// one use. If this is true, it allows the users to invert the operation for
473// free when it is profitable to do so.
474static bool isOneUseSetCC(SDOperand N) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000475 SDOperand N0, N1, N2;
Nate Begeman646d7e22005-09-02 21:18:40 +0000476 if (isSetCCEquivalent(N, N0, N1, N2) && N.Val->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000477 return true;
478 return false;
479}
480
Nate Begeman452d7be2005-09-16 00:54:12 +0000481// FIXME: This should probably go in the ISD class rather than being duplicated
482// in several files.
483static bool isCommutativeBinOp(unsigned Opcode) {
484 switch (Opcode) {
485 case ISD::ADD:
486 case ISD::MUL:
487 case ISD::AND:
488 case ISD::OR:
489 case ISD::XOR: return true;
490 default: return false; // FIXME: Need commutative info for user ops!
491 }
492}
493
Nate Begemancd4d58c2006-02-03 06:46:56 +0000494SDOperand DAGCombiner::ReassociateOps(unsigned Opc, SDOperand N0, SDOperand N1){
495 MVT::ValueType VT = N0.getValueType();
496 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
497 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
498 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
499 if (isa<ConstantSDNode>(N1)) {
500 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(1), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000501 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000502 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(0));
503 } else if (N0.hasOneUse()) {
504 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(0), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000505 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000506 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(1));
507 }
508 }
509 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
510 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
511 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
512 if (isa<ConstantSDNode>(N0)) {
513 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(1), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000514 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000515 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(0));
516 } else if (N1.hasOneUse()) {
517 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(0), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000518 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000519 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(1));
520 }
521 }
522 return SDOperand();
523}
524
Nate Begeman4ebd8052005-09-01 23:24:04 +0000525void DAGCombiner::Run(bool RunningAfterLegalize) {
526 // set the instance variable, so that the various visit routines may use it.
527 AfterLegalize = RunningAfterLegalize;
528
Nate Begeman646d7e22005-09-02 21:18:40 +0000529 // Add all the dag nodes to the worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000530 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
531 E = DAG.allnodes_end(); I != E; ++I)
532 WorkList.push_back(I);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000533
Chris Lattner95038592005-10-05 06:35:28 +0000534 // Create a dummy node (which is not added to allnodes), that adds a reference
535 // to the root node, preventing it from being deleted, and tracking any
536 // changes of the root.
537 HandleSDNode Dummy(DAG.getRoot());
538
Chris Lattner24664722006-03-01 04:53:38 +0000539
540 /// DagCombineInfo - Expose the DAG combiner to the target combiner impls.
541 TargetLowering::DAGCombinerInfo
542 DagCombineInfo(DAG, !RunningAfterLegalize, this);
543
Nate Begeman1d4d4142005-09-01 00:19:25 +0000544 // while the worklist isn't empty, inspect the node on the end of it and
545 // try and combine it.
546 while (!WorkList.empty()) {
547 SDNode *N = WorkList.back();
548 WorkList.pop_back();
549
550 // If N has no uses, it is dead. Make sure to revisit all N's operands once
Chris Lattner95038592005-10-05 06:35:28 +0000551 // N is deleted from the DAG, since they too may now be dead or may have a
552 // reduced number of uses, allowing other xforms.
553 if (N->use_empty() && N != &Dummy) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000554 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
555 WorkList.push_back(N->getOperand(i).Val);
556
Nate Begeman1d4d4142005-09-01 00:19:25 +0000557 removeFromWorkList(N);
Chris Lattner95038592005-10-05 06:35:28 +0000558 DAG.DeleteNode(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000559 continue;
560 }
561
Nate Begeman83e75ec2005-09-06 04:43:02 +0000562 SDOperand RV = visit(N);
Chris Lattner24664722006-03-01 04:53:38 +0000563
564 // If nothing happened, try a target-specific DAG combine.
565 if (RV.Val == 0) {
566 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
567 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode()))
568 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
569 }
570
Nate Begeman83e75ec2005-09-06 04:43:02 +0000571 if (RV.Val) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000572 ++NodesCombined;
Nate Begeman646d7e22005-09-02 21:18:40 +0000573 // If we get back the same node we passed in, rather than a new node or
574 // zero, we know that the node must have defined multiple values and
575 // CombineTo was used. Since CombineTo takes care of the worklist
576 // mechanics for us, we have no work to do in this case.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000577 if (RV.Val != N) {
Nate Begeman2300f552005-09-07 00:15:36 +0000578 DEBUG(std::cerr << "\nReplacing "; N->dump();
579 std::cerr << "\nWith: "; RV.Val->dump();
580 std::cerr << '\n');
Chris Lattner01a22022005-10-10 22:04:48 +0000581 std::vector<SDNode*> NowDead;
582 DAG.ReplaceAllUsesWith(N, std::vector<SDOperand>(1, RV), &NowDead);
Nate Begeman646d7e22005-09-02 21:18:40 +0000583
584 // Push the new node and any users onto the worklist
Nate Begeman83e75ec2005-09-06 04:43:02 +0000585 WorkList.push_back(RV.Val);
586 AddUsersToWorkList(RV.Val);
Nate Begeman646d7e22005-09-02 21:18:40 +0000587
588 // Nodes can end up on the worklist more than once. Make sure we do
589 // not process a node that has been replaced.
590 removeFromWorkList(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000591 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
592 removeFromWorkList(NowDead[i]);
Chris Lattner5c46f742005-10-05 06:11:08 +0000593
594 // Finally, since the node is now dead, remove it from the graph.
595 DAG.DeleteNode(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000596 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000597 }
598 }
Chris Lattner95038592005-10-05 06:35:28 +0000599
600 // If the root changed (e.g. it was a dead load, update the root).
601 DAG.setRoot(Dummy.getValue());
Nate Begeman1d4d4142005-09-01 00:19:25 +0000602}
603
Nate Begeman83e75ec2005-09-06 04:43:02 +0000604SDOperand DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000605 switch(N->getOpcode()) {
606 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +0000607 case ISD::TokenFactor: return visitTokenFactor(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000608 case ISD::ADD: return visitADD(N);
609 case ISD::SUB: return visitSUB(N);
610 case ISD::MUL: return visitMUL(N);
611 case ISD::SDIV: return visitSDIV(N);
612 case ISD::UDIV: return visitUDIV(N);
613 case ISD::SREM: return visitSREM(N);
614 case ISD::UREM: return visitUREM(N);
615 case ISD::MULHU: return visitMULHU(N);
616 case ISD::MULHS: return visitMULHS(N);
617 case ISD::AND: return visitAND(N);
618 case ISD::OR: return visitOR(N);
619 case ISD::XOR: return visitXOR(N);
620 case ISD::SHL: return visitSHL(N);
621 case ISD::SRA: return visitSRA(N);
622 case ISD::SRL: return visitSRL(N);
623 case ISD::CTLZ: return visitCTLZ(N);
624 case ISD::CTTZ: return visitCTTZ(N);
625 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000626 case ISD::SELECT: return visitSELECT(N);
627 case ISD::SELECT_CC: return visitSELECT_CC(N);
628 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000629 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
630 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
631 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
632 case ISD::TRUNCATE: return visitTRUNCATE(N);
Chris Lattner94683772005-12-23 05:30:37 +0000633 case ISD::BIT_CONVERT: return visitBIT_CONVERT(N);
Chris Lattner6258fb22006-04-02 02:53:43 +0000634 case ISD::VBIT_CONVERT: return visitVBIT_CONVERT(N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000635 case ISD::FADD: return visitFADD(N);
636 case ISD::FSUB: return visitFSUB(N);
637 case ISD::FMUL: return visitFMUL(N);
638 case ISD::FDIV: return visitFDIV(N);
639 case ISD::FREM: return visitFREM(N);
Chris Lattner12d83032006-03-05 05:30:57 +0000640 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000641 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
642 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
643 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
644 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
645 case ISD::FP_ROUND: return visitFP_ROUND(N);
646 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
647 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
648 case ISD::FNEG: return visitFNEG(N);
649 case ISD::FABS: return visitFABS(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000650 case ISD::BRCOND: return visitBRCOND(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000651 case ISD::BR_CC: return visitBR_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000652 case ISD::LOAD: return visitLOAD(N);
Chris Lattner29cd7db2006-03-31 18:10:41 +0000653 case ISD::EXTLOAD:
654 case ISD::SEXTLOAD:
655 case ISD::ZEXTLOAD: return visitXEXTLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000656 case ISD::STORE: return visitSTORE(N);
Chris Lattnerca242442006-03-19 01:27:56 +0000657 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
658 case ISD::VINSERT_VECTOR_ELT: return visitVINSERT_VECTOR_ELT(N);
Chris Lattnerd7648c82006-03-28 20:28:38 +0000659 case ISD::VBUILD_VECTOR: return visitVBUILD_VECTOR(N);
Chris Lattner66445d32006-03-28 22:11:53 +0000660 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
Chris Lattnerf1d0c622006-03-31 22:16:43 +0000661 case ISD::VVECTOR_SHUFFLE: return visitVVECTOR_SHUFFLE(N);
Chris Lattneredab1b92006-04-02 03:25:57 +0000662 case ISD::VADD: return visitVBinOp(N, ISD::ADD , ISD::FADD);
663 case ISD::VSUB: return visitVBinOp(N, ISD::SUB , ISD::FSUB);
664 case ISD::VMUL: return visitVBinOp(N, ISD::MUL , ISD::FMUL);
665 case ISD::VSDIV: return visitVBinOp(N, ISD::SDIV, ISD::FDIV);
666 case ISD::VUDIV: return visitVBinOp(N, ISD::UDIV, ISD::UDIV);
667 case ISD::VAND: return visitVBinOp(N, ISD::AND , ISD::AND);
668 case ISD::VOR: return visitVBinOp(N, ISD::OR , ISD::OR);
669 case ISD::VXOR: return visitVBinOp(N, ISD::XOR , ISD::XOR);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000670 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000671 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000672}
673
Nate Begeman83e75ec2005-09-06 04:43:02 +0000674SDOperand DAGCombiner::visitTokenFactor(SDNode *N) {
Nate Begemanded49632005-10-13 03:11:28 +0000675 std::vector<SDOperand> Ops;
676 bool Changed = false;
677
Nate Begeman1d4d4142005-09-01 00:19:25 +0000678 // If the token factor has two operands and one is the entry token, replace
679 // the token factor with the other operand.
680 if (N->getNumOperands() == 2) {
681 if (N->getOperand(0).getOpcode() == ISD::EntryToken)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000682 return N->getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000683 if (N->getOperand(1).getOpcode() == ISD::EntryToken)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000684 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000685 }
Chris Lattner24edbb72005-10-13 22:10:05 +0000686
Nate Begemanded49632005-10-13 03:11:28 +0000687 // fold (tokenfactor (tokenfactor)) -> tokenfactor
688 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
689 SDOperand Op = N->getOperand(i);
690 if (Op.getOpcode() == ISD::TokenFactor && Op.hasOneUse()) {
Chris Lattnerac0f8f22006-03-13 18:37:30 +0000691 AddToWorkList(Op.Val); // Remove dead node.
Nate Begemanded49632005-10-13 03:11:28 +0000692 Changed = true;
693 for (unsigned j = 0, e = Op.getNumOperands(); j != e; ++j)
694 Ops.push_back(Op.getOperand(j));
695 } else {
696 Ops.push_back(Op);
697 }
698 }
699 if (Changed)
700 return DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000701 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000702}
703
Nate Begeman83e75ec2005-09-06 04:43:02 +0000704SDOperand DAGCombiner::visitADD(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000705 SDOperand N0 = N->getOperand(0);
706 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000707 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
708 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemanf89d78d2005-09-07 16:09:19 +0000709 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000710
711 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000712 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000713 return DAG.getNode(ISD::ADD, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000714 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000715 if (N0C && !N1C)
716 return DAG.getNode(ISD::ADD, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000717 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000718 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000719 return N0;
Chris Lattner4aafb4f2006-01-12 20:22:43 +0000720 // fold ((c1-A)+c2) -> (c1+c2)-A
721 if (N1C && N0.getOpcode() == ISD::SUB)
722 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
723 return DAG.getNode(ISD::SUB, VT,
724 DAG.getConstant(N1C->getValue()+N0C->getValue(), VT),
725 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000726 // reassociate add
727 SDOperand RADD = ReassociateOps(ISD::ADD, N0, N1);
728 if (RADD.Val != 0)
729 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000730 // fold ((0-A) + B) -> B-A
731 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
732 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000733 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000734 // fold (A + (0-B)) -> A-B
735 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
736 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000737 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +0000738 // fold (A+(B-A)) -> B
739 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000740 return N1.getOperand(0);
Chris Lattner947c2892006-03-13 06:51:27 +0000741
Evan Cheng860771d2006-03-01 01:09:54 +0000742 if (!MVT::isVector(VT) && SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +0000743 return SDOperand(N, 0);
Chris Lattner947c2892006-03-13 06:51:27 +0000744
745 // fold (a+b) -> (a|b) iff a and b share no bits.
746 if (MVT::isInteger(VT) && !MVT::isVector(VT)) {
747 uint64_t LHSZero, LHSOne;
748 uint64_t RHSZero, RHSOne;
749 uint64_t Mask = MVT::getIntVTBitMask(VT);
750 TLI.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
751 if (LHSZero) {
752 TLI.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
753
754 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
755 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
756 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
757 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
758 return DAG.getNode(ISD::OR, VT, N0, N1);
759 }
760 }
761
Nate Begeman83e75ec2005-09-06 04:43:02 +0000762 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000763}
764
Nate Begeman83e75ec2005-09-06 04:43:02 +0000765SDOperand DAGCombiner::visitSUB(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000766 SDOperand N0 = N->getOperand(0);
767 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000768 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
769 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000770 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000771
Chris Lattner854077d2005-10-17 01:07:11 +0000772 // fold (sub x, x) -> 0
773 if (N0 == N1)
774 return DAG.getConstant(0, N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000775 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000776 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000777 return DAG.getNode(ISD::SUB, VT, N0, N1);
Chris Lattner05b57432005-10-11 06:07:15 +0000778 // fold (sub x, c) -> (add x, -c)
779 if (N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000780 return DAG.getNode(ISD::ADD, VT, N0, DAG.getConstant(-N1C->getValue(), VT));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000781 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +0000782 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000783 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000784 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +0000785 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000786 return N0.getOperand(0);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000787 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000788}
789
Nate Begeman83e75ec2005-09-06 04:43:02 +0000790SDOperand DAGCombiner::visitMUL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000791 SDOperand N0 = N->getOperand(0);
792 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000793 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
794 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman223df222005-09-08 20:18:10 +0000795 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000796
797 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000798 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000799 return DAG.getNode(ISD::MUL, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000800 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000801 if (N0C && !N1C)
802 return DAG.getNode(ISD::MUL, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000803 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000804 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000805 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000806 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +0000807 if (N1C && N1C->isAllOnesValue())
Nate Begeman405e3ec2005-10-21 00:02:42 +0000808 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000809 // fold (mul x, (1 << c)) -> x << c
Nate Begeman646d7e22005-09-02 21:18:40 +0000810 if (N1C && isPowerOf2_64(N1C->getValue()))
Chris Lattner3e6099b2005-10-30 06:41:49 +0000811 return DAG.getNode(ISD::SHL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000812 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000813 TLI.getShiftAmountTy()));
Chris Lattner3e6099b2005-10-30 06:41:49 +0000814 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
815 if (N1C && isPowerOf2_64(-N1C->getSignExtended())) {
816 // FIXME: If the input is something that is easily negated (e.g. a
817 // single-use add), we should put the negate there.
818 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT),
819 DAG.getNode(ISD::SHL, VT, N0,
820 DAG.getConstant(Log2_64(-N1C->getSignExtended()),
821 TLI.getShiftAmountTy())));
822 }
Andrew Lenharth50a0d422006-04-02 21:42:45 +0000823
Chris Lattner0b1a85f2006-03-01 03:44:24 +0000824 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
825 if (N1C && N0.getOpcode() == ISD::SHL &&
826 isa<ConstantSDNode>(N0.getOperand(1))) {
827 SDOperand C3 = DAG.getNode(ISD::SHL, VT, N1, N0.getOperand(1));
Chris Lattner5750df92006-03-01 04:03:14 +0000828 AddToWorkList(C3.Val);
Chris Lattner0b1a85f2006-03-01 03:44:24 +0000829 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0), C3);
830 }
831
832 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
833 // use.
834 {
835 SDOperand Sh(0,0), Y(0,0);
836 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
837 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
838 N0.Val->hasOneUse()) {
839 Sh = N0; Y = N1;
840 } else if (N1.getOpcode() == ISD::SHL &&
841 isa<ConstantSDNode>(N1.getOperand(1)) && N1.Val->hasOneUse()) {
842 Sh = N1; Y = N0;
843 }
844 if (Sh.Val) {
845 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Sh.getOperand(0), Y);
846 return DAG.getNode(ISD::SHL, VT, Mul, Sh.getOperand(1));
847 }
848 }
Chris Lattnera1deca32006-03-04 23:33:26 +0000849 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
850 if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() &&
851 isa<ConstantSDNode>(N0.getOperand(1))) {
852 return DAG.getNode(ISD::ADD, VT,
853 DAG.getNode(ISD::MUL, VT, N0.getOperand(0), N1),
854 DAG.getNode(ISD::MUL, VT, N0.getOperand(1), N1));
855 }
Chris Lattner0b1a85f2006-03-01 03:44:24 +0000856
Nate Begemancd4d58c2006-02-03 06:46:56 +0000857 // reassociate mul
858 SDOperand RMUL = ReassociateOps(ISD::MUL, N0, N1);
859 if (RMUL.Val != 0)
860 return RMUL;
Nate Begeman83e75ec2005-09-06 04:43:02 +0000861 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000862}
863
Nate Begeman83e75ec2005-09-06 04:43:02 +0000864SDOperand DAGCombiner::visitSDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000865 SDOperand N0 = N->getOperand(0);
866 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000867 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
868 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000869 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000870
871 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000872 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000873 return DAG.getNode(ISD::SDIV, VT, N0, N1);
Nate Begeman405e3ec2005-10-21 00:02:42 +0000874 // fold (sdiv X, 1) -> X
875 if (N1C && N1C->getSignExtended() == 1LL)
876 return N0;
877 // fold (sdiv X, -1) -> 0-X
878 if (N1C && N1C->isAllOnesValue())
879 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +0000880 // If we know the sign bits of both operands are zero, strength reduce to a
881 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
882 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000883 if (TLI.MaskedValueIsZero(N1, SignBit) &&
884 TLI.MaskedValueIsZero(N0, SignBit))
Chris Lattner094c8fc2005-10-07 06:10:46 +0000885 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
Nate Begemancd6a6ed2006-02-17 07:26:20 +0000886 // fold (sdiv X, pow2) -> simple ops after legalize
Nate Begemanfb7217b2006-02-17 19:54:08 +0000887 if (N1C && N1C->getValue() && !TLI.isIntDivCheap() &&
Nate Begeman405e3ec2005-10-21 00:02:42 +0000888 (isPowerOf2_64(N1C->getSignExtended()) ||
889 isPowerOf2_64(-N1C->getSignExtended()))) {
890 // If dividing by powers of two is cheap, then don't perform the following
891 // fold.
892 if (TLI.isPow2DivCheap())
893 return SDOperand();
894 int64_t pow2 = N1C->getSignExtended();
895 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
Chris Lattner8f4880b2006-02-16 08:02:36 +0000896 unsigned lg2 = Log2_64(abs2);
897 // Splat the sign bit into the register
898 SDOperand SGN = DAG.getNode(ISD::SRA, VT, N0,
Nate Begeman405e3ec2005-10-21 00:02:42 +0000899 DAG.getConstant(MVT::getSizeInBits(VT)-1,
900 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +0000901 AddToWorkList(SGN.Val);
Chris Lattner8f4880b2006-02-16 08:02:36 +0000902 // Add (N0 < 0) ? abs2 - 1 : 0;
903 SDOperand SRL = DAG.getNode(ISD::SRL, VT, SGN,
904 DAG.getConstant(MVT::getSizeInBits(VT)-lg2,
Nate Begeman405e3ec2005-10-21 00:02:42 +0000905 TLI.getShiftAmountTy()));
Chris Lattner8f4880b2006-02-16 08:02:36 +0000906 SDOperand ADD = DAG.getNode(ISD::ADD, VT, N0, SRL);
Chris Lattner5750df92006-03-01 04:03:14 +0000907 AddToWorkList(SRL.Val);
908 AddToWorkList(ADD.Val); // Divide by pow2
Chris Lattner8f4880b2006-02-16 08:02:36 +0000909 SDOperand SRA = DAG.getNode(ISD::SRA, VT, ADD,
910 DAG.getConstant(lg2, TLI.getShiftAmountTy()));
Nate Begeman405e3ec2005-10-21 00:02:42 +0000911 // If we're dividing by a positive value, we're done. Otherwise, we must
912 // negate the result.
913 if (pow2 > 0)
914 return SRA;
Chris Lattner5750df92006-03-01 04:03:14 +0000915 AddToWorkList(SRA.Val);
Nate Begeman405e3ec2005-10-21 00:02:42 +0000916 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), SRA);
917 }
Nate Begeman69575232005-10-20 02:15:44 +0000918 // if integer divide is expensive and we satisfy the requirements, emit an
919 // alternate sequence.
Nate Begeman405e3ec2005-10-21 00:02:42 +0000920 if (N1C && (N1C->getSignExtended() < -1 || N1C->getSignExtended() > 1) &&
Chris Lattnere9936d12005-10-22 18:50:15 +0000921 !TLI.isIntDivCheap()) {
922 SDOperand Op = BuildSDIV(N);
923 if (Op.Val) return Op;
Nate Begeman69575232005-10-20 02:15:44 +0000924 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000925 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000926}
927
Nate Begeman83e75ec2005-09-06 04:43:02 +0000928SDOperand DAGCombiner::visitUDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000929 SDOperand N0 = N->getOperand(0);
930 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000931 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
932 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000933 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000934
935 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000936 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000937 return DAG.getNode(ISD::UDIV, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000938 // fold (udiv x, (1 << c)) -> x >>u c
Nate Begeman646d7e22005-09-02 21:18:40 +0000939 if (N1C && isPowerOf2_64(N1C->getValue()))
Nate Begemanfb5e4bd2006-02-05 07:20:23 +0000940 return DAG.getNode(ISD::SRL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000941 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000942 TLI.getShiftAmountTy()));
Nate Begemanfb5e4bd2006-02-05 07:20:23 +0000943 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
944 if (N1.getOpcode() == ISD::SHL) {
945 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
946 if (isPowerOf2_64(SHC->getValue())) {
947 MVT::ValueType ADDVT = N1.getOperand(1).getValueType();
Nate Begemanc031e332006-02-05 07:36:48 +0000948 SDOperand Add = DAG.getNode(ISD::ADD, ADDVT, N1.getOperand(1),
949 DAG.getConstant(Log2_64(SHC->getValue()),
950 ADDVT));
Chris Lattner5750df92006-03-01 04:03:14 +0000951 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +0000952 return DAG.getNode(ISD::SRL, VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +0000953 }
954 }
955 }
Nate Begeman69575232005-10-20 02:15:44 +0000956 // fold (udiv x, c) -> alternate
Chris Lattnere9936d12005-10-22 18:50:15 +0000957 if (N1C && N1C->getValue() && !TLI.isIntDivCheap()) {
958 SDOperand Op = BuildUDIV(N);
959 if (Op.Val) return Op;
960 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000961 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000962}
963
Nate Begeman83e75ec2005-09-06 04:43:02 +0000964SDOperand DAGCombiner::visitSREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000965 SDOperand N0 = N->getOperand(0);
966 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000967 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
968 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +0000969 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000970
971 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000972 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000973 return DAG.getNode(ISD::SREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +0000974 // If we know the sign bits of both operands are zero, strength reduce to a
975 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
976 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000977 if (TLI.MaskedValueIsZero(N1, SignBit) &&
978 TLI.MaskedValueIsZero(N0, SignBit))
Nate Begemana148d982006-01-18 22:35:16 +0000979 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000980 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000981}
982
Nate Begeman83e75ec2005-09-06 04:43:02 +0000983SDOperand DAGCombiner::visitUREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000984 SDOperand N0 = N->getOperand(0);
985 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000986 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
987 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +0000988 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000989
990 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000991 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000992 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +0000993 // fold (urem x, pow2) -> (and x, pow2-1)
994 if (N1C && !N1C->isNullValue() && isPowerOf2_64(N1C->getValue()))
Nate Begemana148d982006-01-18 22:35:16 +0000995 return DAG.getNode(ISD::AND, VT, N0, DAG.getConstant(N1C->getValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +0000996 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
997 if (N1.getOpcode() == ISD::SHL) {
998 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
999 if (isPowerOf2_64(SHC->getValue())) {
Nate Begemanbab92392006-02-05 08:07:24 +00001000 SDOperand Add = DAG.getNode(ISD::ADD, VT, N1,DAG.getConstant(~0ULL,VT));
Chris Lattner5750df92006-03-01 04:03:14 +00001001 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +00001002 return DAG.getNode(ISD::AND, VT, N0, Add);
1003 }
1004 }
1005 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001006 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001007}
1008
Nate Begeman83e75ec2005-09-06 04:43:02 +00001009SDOperand DAGCombiner::visitMULHS(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001010 SDOperand N0 = N->getOperand(0);
1011 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001012 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001013
1014 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001015 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001016 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001017 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001018 if (N1C && N1C->getValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001019 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
1020 DAG.getConstant(MVT::getSizeInBits(N0.getValueType())-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001021 TLI.getShiftAmountTy()));
1022 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001023}
1024
Nate Begeman83e75ec2005-09-06 04:43:02 +00001025SDOperand DAGCombiner::visitMULHU(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001026 SDOperand N0 = N->getOperand(0);
1027 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001028 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001029
1030 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001031 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001032 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001033 // fold (mulhu x, 1) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001034 if (N1C && N1C->getValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001035 return DAG.getConstant(0, N0.getValueType());
1036 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001037}
1038
Chris Lattner35e5c142006-05-05 05:51:50 +00001039/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
1040/// two operands of the same opcode, try to simplify it.
1041SDOperand DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
1042 SDOperand N0 = N->getOperand(0), N1 = N->getOperand(1);
1043 MVT::ValueType VT = N0.getValueType();
1044 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
1045
1046 // fold (and (zext x), (zext y)) -> (zext (and x, y))
1047 // fold (or (zext x), (zext y)) -> (zext (or x, y))
1048 // fold (xor (zext x), (zext y)) -> (zext (xor x, y))
1049 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
1050 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1051 SDOperand ORNode = DAG.getNode(N->getOpcode(),
1052 N0.getOperand(0).getValueType(),
1053 N0.getOperand(0), N1.getOperand(0));
1054 AddToWorkList(ORNode.Val);
1055 return DAG.getNode(ISD::ZERO_EXTEND, VT, ORNode);
1056 }
1057
1058 // fold (and (trunc x), (trunc y)) -> (trunc (and x, y))
1059 // fold (or (trunc x), (trunc y)) -> (trunc (or x, y))
1060 // fold (xor (trunc x), (trunc y)) -> (trunc (xor x, y))
1061 if (N0.getOpcode() == ISD::TRUNCATE &&
1062 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1063 SDOperand ORNode = DAG.getNode(N->getOpcode(),
1064 N0.getOperand(0).getValueType(),
1065 N0.getOperand(0), N1.getOperand(0));
1066 AddToWorkList(ORNode.Val);
1067 return DAG.getNode(ISD::TRUNCATE, VT, ORNode);
1068 }
1069
1070 // fold (and (shl/srl/sra x), (shl/srl/sra y)) -> (shl/srl/sra (and x, y))
1071 // fold (or (shl/srl/sra x), (shl/srl/sra y)) -> (shl/srl/sra (or x, y))
1072 // fold (xor (shl/srl/sra x), (shl/srl/sra y)) -> (shl/srl/sra (xor x, y))
1073 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
1074 N0.getOpcode() == ISD::SRA) &&
1075 N0.getOperand(1) == N1.getOperand(1)) {
1076 SDOperand ORNode = DAG.getNode(N->getOpcode(),
1077 N0.getOperand(0).getValueType(),
1078 N0.getOperand(0), N1.getOperand(0));
1079 AddToWorkList(ORNode.Val);
1080 return DAG.getNode(N0.getOpcode(), VT, ORNode, N0.getOperand(1));
1081 }
1082
1083 return SDOperand();
1084}
1085
Nate Begeman83e75ec2005-09-06 04:43:02 +00001086SDOperand DAGCombiner::visitAND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001087 SDOperand N0 = N->getOperand(0);
1088 SDOperand N1 = N->getOperand(1);
Nate Begemanfb7217b2006-02-17 19:54:08 +00001089 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001090 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1091 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001092 MVT::ValueType VT = N1.getValueType();
Nate Begeman83e75ec2005-09-06 04:43:02 +00001093 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001094
1095 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001096 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001097 return DAG.getNode(ISD::AND, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001098 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001099 if (N0C && !N1C)
1100 return DAG.getNode(ISD::AND, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001101 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001102 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001103 return N0;
1104 // if (and x, c) is known to be zero, return 0
Nate Begeman368e18d2006-02-16 21:11:51 +00001105 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001106 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +00001107 // reassociate and
1108 SDOperand RAND = ReassociateOps(ISD::AND, N0, N1);
1109 if (RAND.Val != 0)
1110 return RAND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001111 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
Nate Begeman5dc7e862005-11-02 18:42:59 +00001112 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001113 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Nate Begeman646d7e22005-09-02 21:18:40 +00001114 if ((ORI->getValue() & N1C->getValue()) == N1C->getValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001115 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +00001116 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
1117 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Chris Lattner1ec05d12006-03-01 21:47:21 +00001118 unsigned InMask = MVT::getIntVTBitMask(N0.getOperand(0).getValueType());
Chris Lattner3603cd62006-02-02 07:17:31 +00001119 if (TLI.MaskedValueIsZero(N0.getOperand(0),
Chris Lattner1ec05d12006-03-01 21:47:21 +00001120 ~N1C->getValue() & InMask)) {
1121 SDOperand Zext = DAG.getNode(ISD::ZERO_EXTEND, N0.getValueType(),
1122 N0.getOperand(0));
1123
1124 // Replace uses of the AND with uses of the Zero extend node.
1125 CombineTo(N, Zext);
1126
Chris Lattner3603cd62006-02-02 07:17:31 +00001127 // We actually want to replace all uses of the any_extend with the
1128 // zero_extend, to avoid duplicating things. This will later cause this
1129 // AND to be folded.
Chris Lattner1ec05d12006-03-01 21:47:21 +00001130 CombineTo(N0.Val, Zext);
Chris Lattnerfedced72006-04-20 23:55:59 +00001131 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner3603cd62006-02-02 07:17:31 +00001132 }
1133 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001134 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
1135 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1136 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1137 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1138
1139 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1140 MVT::isInteger(LL.getValueType())) {
1141 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
1142 if (cast<ConstantSDNode>(LR)->getValue() == 0 && Op1 == ISD::SETEQ) {
1143 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001144 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001145 return DAG.getSetCC(VT, ORNode, LR, Op1);
1146 }
1147 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
1148 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
1149 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001150 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001151 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1152 }
1153 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
1154 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
1155 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001156 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001157 return DAG.getSetCC(VT, ORNode, LR, Op1);
1158 }
1159 }
1160 // canonicalize equivalent to ll == rl
1161 if (LL == RR && LR == RL) {
1162 Op1 = ISD::getSetCCSwappedOperands(Op1);
1163 std::swap(RL, RR);
1164 }
1165 if (LL == RL && LR == RR) {
1166 bool isInteger = MVT::isInteger(LL.getValueType());
1167 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
1168 if (Result != ISD::SETCC_INVALID)
1169 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1170 }
1171 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001172
1173 // Simplify: and (op x...), (op y...) -> (op (and x, y))
1174 if (N0.getOpcode() == N1.getOpcode()) {
1175 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1176 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001177 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001178
Nate Begemande996292006-02-03 22:24:05 +00001179 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
1180 // fold (and (sra)) -> (and (srl)) when possible.
Chris Lattner6ea2dee2006-03-25 22:19:00 +00001181 if (!MVT::isVector(VT) &&
1182 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001183 return SDOperand(N, 0);
Nate Begemanded49632005-10-13 03:11:28 +00001184 // fold (zext_inreg (extload x)) -> (zextload x)
Nate Begeman5054f162005-10-14 01:12:21 +00001185 if (N0.getOpcode() == ISD::EXTLOAD) {
Nate Begemanded49632005-10-13 03:11:28 +00001186 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001187 // If we zero all the possible extended bits, then we can turn this into
1188 // a zextload if we are running before legalize or the operation is legal.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001189 if (TLI.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Chris Lattner67a44cd2005-10-13 18:16:34 +00001190 (!AfterLegalize || TLI.isOperationLegal(ISD::ZEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001191 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1192 N0.getOperand(1), N0.getOperand(2),
1193 EVT);
Chris Lattner5750df92006-03-01 04:03:14 +00001194 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001195 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001196 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001197 }
1198 }
1199 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Chris Lattner40c62d52005-10-18 06:04:22 +00001200 if (N0.getOpcode() == ISD::SEXTLOAD && N0.hasOneUse()) {
Nate Begemanded49632005-10-13 03:11:28 +00001201 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001202 // If we zero all the possible extended bits, then we can turn this into
1203 // a zextload if we are running before legalize or the operation is legal.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001204 if (TLI.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Nate Begemanbfd65a02005-10-13 18:34:58 +00001205 (!AfterLegalize || TLI.isOperationLegal(ISD::ZEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001206 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1207 N0.getOperand(1), N0.getOperand(2),
1208 EVT);
Chris Lattner5750df92006-03-01 04:03:14 +00001209 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001210 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001211 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001212 }
1213 }
Chris Lattner15045b62006-02-28 06:35:35 +00001214
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001215 // fold (and (load x), 255) -> (zextload x, i8)
1216 // fold (and (extload x, i16), 255) -> (zextload x, i8)
1217 if (N1C &&
1218 (N0.getOpcode() == ISD::LOAD || N0.getOpcode() == ISD::EXTLOAD ||
1219 N0.getOpcode() == ISD::ZEXTLOAD) &&
1220 N0.hasOneUse()) {
1221 MVT::ValueType EVT, LoadedVT;
Chris Lattner15045b62006-02-28 06:35:35 +00001222 if (N1C->getValue() == 255)
1223 EVT = MVT::i8;
1224 else if (N1C->getValue() == 65535)
1225 EVT = MVT::i16;
1226 else if (N1C->getValue() == ~0U)
1227 EVT = MVT::i32;
1228 else
1229 EVT = MVT::Other;
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001230
1231 LoadedVT = N0.getOpcode() == ISD::LOAD ? VT :
1232 cast<VTSDNode>(N0.getOperand(3))->getVT();
Chris Lattnere44be602006-04-04 17:39:18 +00001233 if (EVT != MVT::Other && LoadedVT > EVT &&
1234 (!AfterLegalize || TLI.isOperationLegal(ISD::ZEXTLOAD, EVT))) {
Chris Lattner15045b62006-02-28 06:35:35 +00001235 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
1236 // For big endian targets, we need to add an offset to the pointer to load
1237 // the correct bytes. For little endian systems, we merely need to read
1238 // fewer bytes from the same pointer.
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001239 unsigned PtrOff =
1240 (MVT::getSizeInBits(LoadedVT) - MVT::getSizeInBits(EVT)) / 8;
1241 SDOperand NewPtr = N0.getOperand(1);
1242 if (!TLI.isLittleEndian())
1243 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
1244 DAG.getConstant(PtrOff, PtrType));
Chris Lattner5750df92006-03-01 04:03:14 +00001245 AddToWorkList(NewPtr.Val);
Chris Lattner15045b62006-02-28 06:35:35 +00001246 SDOperand Load =
1247 DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0), NewPtr,
1248 N0.getOperand(2), EVT);
Chris Lattner5750df92006-03-01 04:03:14 +00001249 AddToWorkList(N);
Chris Lattner15045b62006-02-28 06:35:35 +00001250 CombineTo(N0.Val, Load, Load.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001251 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner15045b62006-02-28 06:35:35 +00001252 }
1253 }
1254
Nate Begeman83e75ec2005-09-06 04:43:02 +00001255 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001256}
1257
Nate Begeman83e75ec2005-09-06 04:43:02 +00001258SDOperand DAGCombiner::visitOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001259 SDOperand N0 = N->getOperand(0);
1260 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001261 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001262 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1263 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001264 MVT::ValueType VT = N1.getValueType();
1265 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001266
1267 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001268 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001269 return DAG.getNode(ISD::OR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001270 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001271 if (N0C && !N1C)
1272 return DAG.getNode(ISD::OR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001273 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001274 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001275 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001276 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001277 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001278 return N1;
1279 // fold (or x, c) -> c iff (x & ~c) == 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001280 if (N1C &&
1281 TLI.MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits))))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001282 return N1;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001283 // reassociate or
1284 SDOperand ROR = ReassociateOps(ISD::OR, N0, N1);
1285 if (ROR.Val != 0)
1286 return ROR;
1287 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
1288 if (N1C && N0.getOpcode() == ISD::AND && N0.Val->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00001289 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00001290 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
1291 return DAG.getNode(ISD::AND, VT, DAG.getNode(ISD::OR, VT, N0.getOperand(0),
1292 N1),
1293 DAG.getConstant(N1C->getValue() | C1->getValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00001294 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001295 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
1296 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1297 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1298 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1299
1300 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1301 MVT::isInteger(LL.getValueType())) {
1302 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
1303 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
1304 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
1305 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
1306 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001307 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001308 return DAG.getSetCC(VT, ORNode, LR, Op1);
1309 }
1310 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
1311 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
1312 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
1313 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
1314 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001315 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001316 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1317 }
1318 }
1319 // canonicalize equivalent to ll == rl
1320 if (LL == RR && LR == RL) {
1321 Op1 = ISD::getSetCCSwappedOperands(Op1);
1322 std::swap(RL, RR);
1323 }
1324 if (LL == RL && LR == RR) {
1325 bool isInteger = MVT::isInteger(LL.getValueType());
1326 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
1327 if (Result != ISD::SETCC_INVALID)
1328 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1329 }
1330 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001331
1332 // Simplify: or (op x...), (op y...) -> (op (or x, y))
1333 if (N0.getOpcode() == N1.getOpcode()) {
1334 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1335 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001336 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001337
Nate Begeman35ef9132006-01-11 21:21:00 +00001338 // canonicalize shl to left side in a shl/srl pair, to match rotate
1339 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
1340 std::swap(N0, N1);
1341 // check for rotl, rotr
1342 if (N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SRL &&
1343 N0.getOperand(0) == N1.getOperand(0) &&
Chris Lattneraf551bc2006-01-12 18:57:33 +00001344 TLI.isOperationLegal(ISD::ROTL, VT) && TLI.isTypeLegal(VT)) {
Nate Begeman35ef9132006-01-11 21:21:00 +00001345 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
1346 if (N0.getOperand(1).getOpcode() == ISD::Constant &&
1347 N1.getOperand(1).getOpcode() == ISD::Constant) {
1348 uint64_t c1val = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1349 uint64_t c2val = cast<ConstantSDNode>(N1.getOperand(1))->getValue();
1350 if ((c1val + c2val) == OpSizeInBits)
1351 return DAG.getNode(ISD::ROTL, VT, N0.getOperand(0), N0.getOperand(1));
1352 }
1353 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
1354 if (N1.getOperand(1).getOpcode() == ISD::SUB &&
1355 N0.getOperand(1) == N1.getOperand(1).getOperand(1))
1356 if (ConstantSDNode *SUBC =
1357 dyn_cast<ConstantSDNode>(N1.getOperand(1).getOperand(0)))
1358 if (SUBC->getValue() == OpSizeInBits)
1359 return DAG.getNode(ISD::ROTL, VT, N0.getOperand(0), N0.getOperand(1));
1360 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
1361 if (N0.getOperand(1).getOpcode() == ISD::SUB &&
1362 N1.getOperand(1) == N0.getOperand(1).getOperand(1))
1363 if (ConstantSDNode *SUBC =
1364 dyn_cast<ConstantSDNode>(N0.getOperand(1).getOperand(0)))
1365 if (SUBC->getValue() == OpSizeInBits) {
Chris Lattneraf551bc2006-01-12 18:57:33 +00001366 if (TLI.isOperationLegal(ISD::ROTR, VT) && TLI.isTypeLegal(VT))
Nate Begeman35ef9132006-01-11 21:21:00 +00001367 return DAG.getNode(ISD::ROTR, VT, N0.getOperand(0),
1368 N1.getOperand(1));
1369 else
1370 return DAG.getNode(ISD::ROTL, VT, N0.getOperand(0),
1371 N0.getOperand(1));
1372 }
1373 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001374 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001375}
1376
Nate Begeman83e75ec2005-09-06 04:43:02 +00001377SDOperand DAGCombiner::visitXOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001378 SDOperand N0 = N->getOperand(0);
1379 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001380 SDOperand LHS, RHS, CC;
1381 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1382 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001383 MVT::ValueType VT = N0.getValueType();
1384
1385 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001386 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001387 return DAG.getNode(ISD::XOR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001388 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001389 if (N0C && !N1C)
1390 return DAG.getNode(ISD::XOR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001391 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001392 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001393 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001394 // reassociate xor
1395 SDOperand RXOR = ReassociateOps(ISD::XOR, N0, N1);
1396 if (RXOR.Val != 0)
1397 return RXOR;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001398 // fold !(x cc y) -> (x !cc y)
Nate Begeman646d7e22005-09-02 21:18:40 +00001399 if (N1C && N1C->getValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
1400 bool isInt = MVT::isInteger(LHS.getValueType());
1401 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
1402 isInt);
1403 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001404 return DAG.getSetCC(VT, LHS, RHS, NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00001405 if (N0.getOpcode() == ISD::SELECT_CC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001406 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00001407 assert(0 && "Unhandled SetCC Equivalent!");
1408 abort();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001409 }
Nate Begeman99801192005-09-07 23:25:52 +00001410 // fold !(x or y) -> (!x and !y) iff x or y are setcc
1411 if (N1C && N1C->getValue() == 1 &&
1412 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001413 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00001414 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
1415 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001416 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1417 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00001418 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00001419 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001420 }
1421 }
Nate Begeman99801192005-09-07 23:25:52 +00001422 // fold !(x or y) -> (!x and !y) iff x or y are constants
1423 if (N1C && N1C->isAllOnesValue() &&
1424 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001425 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00001426 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
1427 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001428 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1429 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00001430 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00001431 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001432 }
1433 }
Nate Begeman223df222005-09-08 20:18:10 +00001434 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
1435 if (N1C && N0.getOpcode() == ISD::XOR) {
1436 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
1437 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
1438 if (N00C)
1439 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
1440 DAG.getConstant(N1C->getValue()^N00C->getValue(), VT));
1441 if (N01C)
1442 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
1443 DAG.getConstant(N1C->getValue()^N01C->getValue(), VT));
1444 }
1445 // fold (xor x, x) -> 0
Chris Lattner4fbdd592006-03-28 19:11:05 +00001446 if (N0 == N1) {
1447 if (!MVT::isVector(VT)) {
1448 return DAG.getConstant(0, VT);
1449 } else if (!AfterLegalize || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) {
1450 // Produce a vector of zeros.
1451 SDOperand El = DAG.getConstant(0, MVT::getVectorBaseType(VT));
1452 std::vector<SDOperand> Ops(MVT::getVectorNumElements(VT), El);
1453 return DAG.getNode(ISD::BUILD_VECTOR, VT, Ops);
1454 }
1455 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001456
1457 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
1458 if (N0.getOpcode() == N1.getOpcode()) {
1459 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1460 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001461 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001462
Chris Lattner3e104b12006-04-08 04:15:24 +00001463 // Simplify the expression using non-local knowledge.
1464 if (!MVT::isVector(VT) &&
1465 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001466 return SDOperand(N, 0);
Chris Lattner3e104b12006-04-08 04:15:24 +00001467
Nate Begeman83e75ec2005-09-06 04:43:02 +00001468 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001469}
1470
Nate Begeman83e75ec2005-09-06 04:43:02 +00001471SDOperand DAGCombiner::visitSHL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001472 SDOperand N0 = N->getOperand(0);
1473 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001474 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1475 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001476 MVT::ValueType VT = N0.getValueType();
1477 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1478
1479 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001480 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001481 return DAG.getNode(ISD::SHL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001482 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001483 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001484 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001485 // fold (shl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001486 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001487 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001488 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001489 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001490 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001491 // if (shl x, c) is known to be zero, return 0
Nate Begemanfb7217b2006-02-17 19:54:08 +00001492 if (TLI.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001493 return DAG.getConstant(0, VT);
Chris Lattner012f2412006-02-17 21:58:01 +00001494 if (SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001495 return SDOperand(N, 0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001496 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001497 if (N1C && N0.getOpcode() == ISD::SHL &&
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 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001502 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001503 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001504 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001505 }
1506 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
1507 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001508 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001509 N0.getOperand(1).getOpcode() == ISD::Constant) {
1510 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001511 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001512 SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
1513 DAG.getConstant(~0ULL << c1, VT));
1514 if (c2 > c1)
1515 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001516 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001517 else
Nate Begeman83e75ec2005-09-06 04:43:02 +00001518 return DAG.getNode(ISD::SRL, VT, Mask,
1519 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001520 }
1521 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001522 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +00001523 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001524 DAG.getConstant(~0ULL << N1C->getValue(), VT));
Chris Lattnercac70592006-03-05 19:53:55 +00001525 // fold (shl (add x, c1), c2) -> (add (shl x, c2), c1<<c2)
1526 if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() &&
1527 isa<ConstantSDNode>(N0.getOperand(1))) {
1528 return DAG.getNode(ISD::ADD, VT,
1529 DAG.getNode(ISD::SHL, VT, N0.getOperand(0), N1),
1530 DAG.getNode(ISD::SHL, VT, N0.getOperand(1), N1));
1531 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001532 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001533}
1534
Nate Begeman83e75ec2005-09-06 04:43:02 +00001535SDOperand DAGCombiner::visitSRA(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001536 SDOperand N0 = N->getOperand(0);
1537 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001538 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1539 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001540 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001541
1542 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001543 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001544 return DAG.getNode(ISD::SRA, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001545 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001546 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001547 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001548 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001549 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001550 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001551 // fold (sra x, c >= size(x)) -> undef
Nate Begemanfb7217b2006-02-17 19:54:08 +00001552 if (N1C && N1C->getValue() >= MVT::getSizeInBits(VT))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001553 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001554 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001555 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001556 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00001557 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
1558 // sext_inreg.
1559 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
1560 unsigned LowBits = MVT::getSizeInBits(VT) - (unsigned)N1C->getValue();
1561 MVT::ValueType EVT;
1562 switch (LowBits) {
1563 default: EVT = MVT::Other; break;
1564 case 1: EVT = MVT::i1; break;
1565 case 8: EVT = MVT::i8; break;
1566 case 16: EVT = MVT::i16; break;
1567 case 32: EVT = MVT::i32; break;
1568 }
1569 if (EVT > MVT::Other && TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, EVT))
1570 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
1571 DAG.getValueType(EVT));
1572 }
Chris Lattner71d9ebc2006-02-28 06:23:04 +00001573
1574 // fold (sra (sra x, c1), c2) -> (sra x, c1+c2)
1575 if (N1C && N0.getOpcode() == ISD::SRA) {
1576 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
1577 unsigned Sum = N1C->getValue() + C1->getValue();
1578 if (Sum >= MVT::getSizeInBits(VT)) Sum = MVT::getSizeInBits(VT)-1;
1579 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0),
1580 DAG.getConstant(Sum, N1C->getValueType(0)));
1581 }
1582 }
1583
Nate Begeman1d4d4142005-09-01 00:19:25 +00001584 // If the sign bit is known to be zero, switch this to a SRL.
Nate Begemanfb7217b2006-02-17 19:54:08 +00001585 if (TLI.MaskedValueIsZero(N0, MVT::getIntVTSignBit(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001586 return DAG.getNode(ISD::SRL, VT, N0, N1);
1587 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001588}
1589
Nate Begeman83e75ec2005-09-06 04:43:02 +00001590SDOperand DAGCombiner::visitSRL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001591 SDOperand N0 = N->getOperand(0);
1592 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001593 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1594 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001595 MVT::ValueType VT = N0.getValueType();
1596 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1597
1598 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001599 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001600 return DAG.getNode(ISD::SRL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001601 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001602 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001603 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001604 // fold (srl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001605 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001606 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001607 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001608 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001609 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001610 // if (srl x, c) is known to be zero, return 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001611 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001612 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001613 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001614 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001615 N0.getOperand(1).getOpcode() == ISD::Constant) {
1616 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001617 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001618 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001619 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001620 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001621 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001622 }
Chris Lattner350bec02006-04-02 06:11:11 +00001623
1624 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
1625 if (N1C && N0.getOpcode() == ISD::CTLZ &&
1626 N1C->getValue() == Log2_32(MVT::getSizeInBits(VT))) {
1627 uint64_t KnownZero, KnownOne, Mask = MVT::getIntVTBitMask(VT);
1628 TLI.ComputeMaskedBits(N0.getOperand(0), Mask, KnownZero, KnownOne);
1629
1630 // If any of the input bits are KnownOne, then the input couldn't be all
1631 // zeros, thus the result of the srl will always be zero.
1632 if (KnownOne) return DAG.getConstant(0, VT);
1633
1634 // If all of the bits input the to ctlz node are known to be zero, then
1635 // the result of the ctlz is "32" and the result of the shift is one.
1636 uint64_t UnknownBits = ~KnownZero & Mask;
1637 if (UnknownBits == 0) return DAG.getConstant(1, VT);
1638
1639 // Otherwise, check to see if there is exactly one bit input to the ctlz.
1640 if ((UnknownBits & (UnknownBits-1)) == 0) {
1641 // Okay, we know that only that the single bit specified by UnknownBits
1642 // could be set on input to the CTLZ node. If this bit is set, the SRL
1643 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
1644 // to an SRL,XOR pair, which is likely to simplify more.
1645 unsigned ShAmt = CountTrailingZeros_64(UnknownBits);
1646 SDOperand Op = N0.getOperand(0);
1647 if (ShAmt) {
1648 Op = DAG.getNode(ISD::SRL, VT, Op,
1649 DAG.getConstant(ShAmt, TLI.getShiftAmountTy()));
1650 AddToWorkList(Op.Val);
1651 }
1652 return DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(1, VT));
1653 }
1654 }
1655
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::visitCTLZ(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 (ctlz c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001665 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001666 return DAG.getNode(ISD::CTLZ, 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::visitCTTZ(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 (cttz c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001676 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001677 return DAG.getNode(ISD::CTTZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001678 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001679}
1680
Nate Begeman83e75ec2005-09-06 04:43:02 +00001681SDOperand DAGCombiner::visitCTPOP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001682 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001683 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00001684 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001685
1686 // fold (ctpop c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001687 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001688 return DAG.getNode(ISD::CTPOP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001689 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001690}
1691
Nate Begeman452d7be2005-09-16 00:54:12 +00001692SDOperand DAGCombiner::visitSELECT(SDNode *N) {
1693 SDOperand N0 = N->getOperand(0);
1694 SDOperand N1 = N->getOperand(1);
1695 SDOperand N2 = N->getOperand(2);
1696 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1697 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1698 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1699 MVT::ValueType VT = N->getValueType(0);
Nate Begeman44728a72005-09-19 22:34:01 +00001700
Nate Begeman452d7be2005-09-16 00:54:12 +00001701 // fold select C, X, X -> X
1702 if (N1 == N2)
1703 return N1;
1704 // fold select true, X, Y -> X
1705 if (N0C && !N0C->isNullValue())
1706 return N1;
1707 // fold select false, X, Y -> Y
1708 if (N0C && N0C->isNullValue())
1709 return N2;
1710 // fold select C, 1, X -> C | X
Nate Begeman44728a72005-09-19 22:34:01 +00001711 if (MVT::i1 == VT && N1C && N1C->getValue() == 1)
Nate Begeman452d7be2005-09-16 00:54:12 +00001712 return DAG.getNode(ISD::OR, VT, N0, N2);
1713 // fold select C, 0, X -> ~C & X
1714 // FIXME: this should check for C type == X type, not i1?
1715 if (MVT::i1 == VT && N1C && N1C->isNullValue()) {
1716 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00001717 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00001718 return DAG.getNode(ISD::AND, VT, XORNode, N2);
1719 }
1720 // fold select C, X, 1 -> ~C | X
Nate Begeman44728a72005-09-19 22:34:01 +00001721 if (MVT::i1 == VT && N2C && N2C->getValue() == 1) {
Nate Begeman452d7be2005-09-16 00:54:12 +00001722 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00001723 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00001724 return DAG.getNode(ISD::OR, VT, XORNode, N1);
1725 }
1726 // fold select C, X, 0 -> C & X
1727 // FIXME: this should check for C type == X type, not i1?
1728 if (MVT::i1 == VT && N2C && N2C->isNullValue())
1729 return DAG.getNode(ISD::AND, VT, N0, N1);
1730 // fold X ? X : Y --> X ? 1 : Y --> X | Y
1731 if (MVT::i1 == VT && N0 == N1)
1732 return DAG.getNode(ISD::OR, VT, N0, N2);
1733 // fold X ? Y : X --> X ? Y : 0 --> X & Y
1734 if (MVT::i1 == VT && N0 == N2)
1735 return DAG.getNode(ISD::AND, VT, N0, N1);
Chris Lattner40c62d52005-10-18 06:04:22 +00001736 // If we can fold this based on the true/false value, do so.
1737 if (SimplifySelectOps(N, N1, N2))
1738 return SDOperand();
Nate Begeman44728a72005-09-19 22:34:01 +00001739 // fold selects based on a setcc into other things, such as min/max/abs
1740 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman750ac1b2006-02-01 07:19:44 +00001741 // FIXME:
1742 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
1743 // having to say they don't support SELECT_CC on every type the DAG knows
1744 // about, since there is no way to mark an opcode illegal at all value types
1745 if (TLI.isOperationLegal(ISD::SELECT_CC, MVT::Other))
1746 return DAG.getNode(ISD::SELECT_CC, VT, N0.getOperand(0), N0.getOperand(1),
1747 N1, N2, N0.getOperand(2));
1748 else
1749 return SimplifySelect(N0, N1, N2);
Nate Begeman452d7be2005-09-16 00:54:12 +00001750 return SDOperand();
1751}
1752
1753SDOperand DAGCombiner::visitSELECT_CC(SDNode *N) {
Nate Begeman44728a72005-09-19 22:34:01 +00001754 SDOperand N0 = N->getOperand(0);
1755 SDOperand N1 = N->getOperand(1);
1756 SDOperand N2 = N->getOperand(2);
1757 SDOperand N3 = N->getOperand(3);
1758 SDOperand N4 = N->getOperand(4);
1759 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1760 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1761 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1762 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
1763
1764 // Determine if the condition we're dealing with is constant
Nate Begemane17daeb2005-10-05 21:43:42 +00001765 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
Chris Lattner91559022005-10-05 04:45:43 +00001766 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
1767
Nate Begeman44728a72005-09-19 22:34:01 +00001768 // fold select_cc lhs, rhs, x, x, cc -> x
1769 if (N2 == N3)
1770 return N2;
Chris Lattner40c62d52005-10-18 06:04:22 +00001771
1772 // If we can fold this based on the true/false value, do so.
1773 if (SimplifySelectOps(N, N2, N3))
1774 return SDOperand();
1775
Nate Begeman44728a72005-09-19 22:34:01 +00001776 // fold select_cc into other things, such as min/max/abs
1777 return SimplifySelectCC(N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00001778}
1779
1780SDOperand DAGCombiner::visitSETCC(SDNode *N) {
1781 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
1782 cast<CondCodeSDNode>(N->getOperand(2))->get());
1783}
1784
Nate Begeman83e75ec2005-09-06 04:43:02 +00001785SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001786 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001787 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001788 MVT::ValueType VT = N->getValueType(0);
1789
Nate Begeman1d4d4142005-09-01 00:19:25 +00001790 // fold (sext c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001791 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001792 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001793 // fold (sext (sext x)) -> (sext x)
1794 if (N0.getOpcode() == ISD::SIGN_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001795 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
Chris Lattnerb14ab8a2005-12-07 07:11:03 +00001796 // fold (sext (truncate x)) -> (sextinreg x) iff x size == sext size.
Chris Lattnercc2210b2005-12-07 18:02:05 +00001797 if (N0.getOpcode() == ISD::TRUNCATE && N0.getOperand(0).getValueType() == VT&&
1798 (!AfterLegalize ||
1799 TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, N0.getValueType())))
Chris Lattnerb14ab8a2005-12-07 07:11:03 +00001800 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
1801 DAG.getValueType(N0.getValueType()));
Evan Cheng110dec22005-12-14 02:19:23 +00001802 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Chris Lattnerd0f6d182005-12-15 19:02:38 +00001803 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() &&
1804 (!AfterLegalize||TLI.isOperationLegal(ISD::SEXTLOAD, N0.getValueType()))){
Nate Begeman3df4d522005-10-12 20:40:40 +00001805 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1806 N0.getOperand(1), N0.getOperand(2),
1807 N0.getValueType());
Chris Lattnerd4771842005-12-14 19:25:30 +00001808 CombineTo(N, ExtLoad);
Chris Lattnerf9884052005-10-13 21:52:31 +00001809 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1810 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001811 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begeman3df4d522005-10-12 20:40:40 +00001812 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001813
1814 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
1815 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
1816 if ((N0.getOpcode() == ISD::SEXTLOAD || N0.getOpcode() == ISD::EXTLOAD) &&
1817 N0.hasOneUse()) {
1818 SDOperand ExtLoad = DAG.getNode(ISD::SEXTLOAD, VT, N0.getOperand(0),
1819 N0.getOperand(1), N0.getOperand(2),
1820 N0.getOperand(3));
Chris Lattnerd4771842005-12-14 19:25:30 +00001821 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001822 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1823 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001824 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001825 }
1826
Nate Begeman83e75ec2005-09-06 04:43:02 +00001827 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001828}
1829
Nate Begeman83e75ec2005-09-06 04:43:02 +00001830SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001831 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001832 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001833 MVT::ValueType VT = N->getValueType(0);
1834
Nate Begeman1d4d4142005-09-01 00:19:25 +00001835 // fold (zext c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001836 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001837 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001838 // fold (zext (zext x)) -> (zext x)
1839 if (N0.getOpcode() == ISD::ZERO_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001840 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
Evan Cheng110dec22005-12-14 02:19:23 +00001841 // fold (zext (truncate x)) -> (zextinreg x) iff x size == zext size.
1842 if (N0.getOpcode() == ISD::TRUNCATE && N0.getOperand(0).getValueType() == VT&&
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001843 (!AfterLegalize || TLI.isOperationLegal(ISD::AND, N0.getValueType())))
Chris Lattner00cb95c2005-12-14 07:58:38 +00001844 return DAG.getZeroExtendInReg(N0.getOperand(0), N0.getValueType());
Evan Cheng110dec22005-12-14 02:19:23 +00001845 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Chris Lattnerd0f6d182005-12-15 19:02:38 +00001846 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() &&
1847 (!AfterLegalize||TLI.isOperationLegal(ISD::ZEXTLOAD, N0.getValueType()))){
Evan Cheng110dec22005-12-14 02:19:23 +00001848 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1849 N0.getOperand(1), N0.getOperand(2),
1850 N0.getValueType());
Chris Lattnerd4771842005-12-14 19:25:30 +00001851 CombineTo(N, ExtLoad);
Evan Cheng110dec22005-12-14 02:19:23 +00001852 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1853 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001854 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng110dec22005-12-14 02:19:23 +00001855 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001856
1857 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
1858 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
1859 if ((N0.getOpcode() == ISD::ZEXTLOAD || N0.getOpcode() == ISD::EXTLOAD) &&
1860 N0.hasOneUse()) {
1861 SDOperand ExtLoad = DAG.getNode(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1862 N0.getOperand(1), N0.getOperand(2),
1863 N0.getOperand(3));
Chris Lattnerd4771842005-12-14 19:25:30 +00001864 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001865 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1866 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001867 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001868 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001869 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001870}
1871
Nate Begeman83e75ec2005-09-06 04:43:02 +00001872SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001873 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001874 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001875 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001876 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001877 MVT::ValueType EVT = cast<VTSDNode>(N1)->getVT();
Nate Begeman07ed4172005-10-10 21:26:48 +00001878 unsigned EVTBits = MVT::getSizeInBits(EVT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001879
Nate Begeman1d4d4142005-09-01 00:19:25 +00001880 // fold (sext_in_reg c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001881 if (N0C) {
1882 SDOperand Truncate = DAG.getConstant(N0C->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001883 return DAG.getNode(ISD::SIGN_EXTEND, VT, Truncate);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001884 }
Nate Begeman646d7e22005-09-02 21:18:40 +00001885 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt1
Nate Begeman1d4d4142005-09-01 00:19:25 +00001886 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Nate Begeman216def82005-10-14 01:29:07 +00001887 cast<VTSDNode>(N0.getOperand(1))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001888 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001889 }
Nate Begeman646d7e22005-09-02 21:18:40 +00001890 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
1891 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1892 EVT < cast<VTSDNode>(N0.getOperand(1))->getVT()) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001893 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001894 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00001895 // fold (sext_in_reg (assert_sext x)) -> (assert_sext x)
1896 if (N0.getOpcode() == ISD::AssertSext &&
1897 cast<VTSDNode>(N0.getOperand(1))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001898 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001899 }
1900 // fold (sext_in_reg (sextload x)) -> (sextload x)
1901 if (N0.getOpcode() == ISD::SEXTLOAD &&
1902 cast<VTSDNode>(N0.getOperand(3))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001903 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001904 }
Nate Begeman4ebd8052005-09-01 23:24:04 +00001905 // fold (sext_in_reg (setcc x)) -> setcc x iff (setcc x) == 0 or -1
Nate Begeman1d4d4142005-09-01 00:19:25 +00001906 if (N0.getOpcode() == ISD::SETCC &&
1907 TLI.getSetCCResultContents() ==
1908 TargetLowering::ZeroOrNegativeOneSetCCResult)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001909 return N0;
Nate Begeman07ed4172005-10-10 21:26:48 +00001910 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is zero
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001911 if (TLI.MaskedValueIsZero(N0, 1ULL << (EVTBits-1)))
Nate Begemande996292006-02-03 22:24:05 +00001912 return DAG.getZeroExtendInReg(N0, EVT);
Nate Begemanded49632005-10-13 03:11:28 +00001913 // fold (sext_inreg (extload x)) -> (sextload x)
1914 if (N0.getOpcode() == ISD::EXTLOAD &&
1915 EVT == cast<VTSDNode>(N0.getOperand(3))->getVT() &&
Nate Begemanbfd65a02005-10-13 18:34:58 +00001916 (!AfterLegalize || TLI.isOperationLegal(ISD::SEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001917 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1918 N0.getOperand(1), N0.getOperand(2),
1919 EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00001920 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00001921 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001922 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001923 }
1924 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Chris Lattner40c62d52005-10-18 06:04:22 +00001925 if (N0.getOpcode() == ISD::ZEXTLOAD && N0.hasOneUse() &&
Nate Begemanded49632005-10-13 03:11:28 +00001926 EVT == cast<VTSDNode>(N0.getOperand(3))->getVT() &&
Nate Begemanbfd65a02005-10-13 18:34:58 +00001927 (!AfterLegalize || TLI.isOperationLegal(ISD::SEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001928 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1929 N0.getOperand(1), N0.getOperand(2),
1930 EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00001931 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00001932 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001933 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001934 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001935 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001936}
1937
Nate Begeman83e75ec2005-09-06 04:43:02 +00001938SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001939 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001940 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001941 MVT::ValueType VT = N->getValueType(0);
1942
1943 // noop truncate
1944 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001945 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001946 // fold (truncate c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001947 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001948 return DAG.getNode(ISD::TRUNCATE, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001949 // fold (truncate (truncate x)) -> (truncate x)
1950 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001951 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001952 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
1953 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND){
1954 if (N0.getValueType() < VT)
1955 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00001956 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001957 else if (N0.getValueType() > VT)
1958 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00001959 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001960 else
1961 // if the source and dest are the same type, we can drop both the extend
1962 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00001963 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001964 }
Nate Begeman3df4d522005-10-12 20:40:40 +00001965 // fold (truncate (load x)) -> (smaller load x)
Chris Lattner40c62d52005-10-18 06:04:22 +00001966 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse()) {
Nate Begeman3df4d522005-10-12 20:40:40 +00001967 assert(MVT::getSizeInBits(N0.getValueType()) > MVT::getSizeInBits(VT) &&
1968 "Cannot truncate to larger type!");
1969 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
Nate Begeman765784a2005-10-12 23:18:53 +00001970 // For big endian targets, we need to add an offset to the pointer to load
1971 // the correct bytes. For little endian systems, we merely need to read
1972 // fewer bytes from the same pointer.
Nate Begeman3df4d522005-10-12 20:40:40 +00001973 uint64_t PtrOff =
1974 (MVT::getSizeInBits(N0.getValueType()) - MVT::getSizeInBits(VT)) / 8;
Nate Begeman765784a2005-10-12 23:18:53 +00001975 SDOperand NewPtr = TLI.isLittleEndian() ? N0.getOperand(1) :
1976 DAG.getNode(ISD::ADD, PtrType, N0.getOperand(1),
1977 DAG.getConstant(PtrOff, PtrType));
Chris Lattner5750df92006-03-01 04:03:14 +00001978 AddToWorkList(NewPtr.Val);
Nate Begeman3df4d522005-10-12 20:40:40 +00001979 SDOperand Load = DAG.getLoad(VT, N0.getOperand(0), NewPtr,N0.getOperand(2));
Chris Lattner5750df92006-03-01 04:03:14 +00001980 AddToWorkList(N);
Chris Lattner24edbb72005-10-13 22:10:05 +00001981 CombineTo(N0.Val, Load, Load.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001982 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begeman3df4d522005-10-12 20:40:40 +00001983 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001984 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001985}
1986
Chris Lattner94683772005-12-23 05:30:37 +00001987SDOperand DAGCombiner::visitBIT_CONVERT(SDNode *N) {
1988 SDOperand N0 = N->getOperand(0);
1989 MVT::ValueType VT = N->getValueType(0);
1990
1991 // If the input is a constant, let getNode() fold it.
1992 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
1993 SDOperand Res = DAG.getNode(ISD::BIT_CONVERT, VT, N0);
1994 if (Res.Val != N) return Res;
1995 }
1996
Chris Lattnerc8547d82005-12-23 05:37:50 +00001997 if (N0.getOpcode() == ISD::BIT_CONVERT) // conv(conv(x,t1),t2) -> conv(x,t2)
1998 return DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
Chris Lattner6258fb22006-04-02 02:53:43 +00001999
Chris Lattner57104102005-12-23 05:44:41 +00002000 // fold (conv (load x)) -> (load (conv*)x)
Chris Lattnerbf40c4b2006-01-15 18:58:59 +00002001 // FIXME: These xforms need to know that the resultant load doesn't need a
2002 // higher alignment than the original!
2003 if (0 && N0.getOpcode() == ISD::LOAD && N0.hasOneUse()) {
Chris Lattner57104102005-12-23 05:44:41 +00002004 SDOperand Load = DAG.getLoad(VT, N0.getOperand(0), N0.getOperand(1),
2005 N0.getOperand(2));
Chris Lattner5750df92006-03-01 04:03:14 +00002006 AddToWorkList(N);
Chris Lattner57104102005-12-23 05:44:41 +00002007 CombineTo(N0.Val, DAG.getNode(ISD::BIT_CONVERT, N0.getValueType(), Load),
2008 Load.getValue(1));
2009 return Load;
2010 }
2011
Chris Lattner94683772005-12-23 05:30:37 +00002012 return SDOperand();
2013}
2014
Chris Lattner6258fb22006-04-02 02:53:43 +00002015SDOperand DAGCombiner::visitVBIT_CONVERT(SDNode *N) {
2016 SDOperand N0 = N->getOperand(0);
2017 MVT::ValueType VT = N->getValueType(0);
2018
2019 // If the input is a VBUILD_VECTOR with all constant elements, fold this now.
2020 // First check to see if this is all constant.
2021 if (N0.getOpcode() == ISD::VBUILD_VECTOR && N0.Val->hasOneUse() &&
2022 VT == MVT::Vector) {
2023 bool isSimple = true;
2024 for (unsigned i = 0, e = N0.getNumOperands()-2; i != e; ++i)
2025 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
2026 N0.getOperand(i).getOpcode() != ISD::Constant &&
2027 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
2028 isSimple = false;
2029 break;
2030 }
2031
Chris Lattner97c20732006-04-03 17:29:28 +00002032 MVT::ValueType DestEltVT = cast<VTSDNode>(N->getOperand(2))->getVT();
2033 if (isSimple && !MVT::isVector(DestEltVT)) {
Chris Lattner6258fb22006-04-02 02:53:43 +00002034 return ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(N0.Val, DestEltVT);
2035 }
2036 }
2037
2038 return SDOperand();
2039}
2040
2041/// ConstantFoldVBIT_CONVERTofVBUILD_VECTOR - We know that BV is a vbuild_vector
2042/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
2043/// destination element value type.
2044SDOperand DAGCombiner::
2045ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(SDNode *BV, MVT::ValueType DstEltVT) {
2046 MVT::ValueType SrcEltVT = BV->getOperand(0).getValueType();
2047
2048 // If this is already the right type, we're done.
2049 if (SrcEltVT == DstEltVT) return SDOperand(BV, 0);
2050
2051 unsigned SrcBitSize = MVT::getSizeInBits(SrcEltVT);
2052 unsigned DstBitSize = MVT::getSizeInBits(DstEltVT);
2053
2054 // If this is a conversion of N elements of one type to N elements of another
2055 // type, convert each element. This handles FP<->INT cases.
2056 if (SrcBitSize == DstBitSize) {
2057 std::vector<SDOperand> Ops;
Chris Lattner3e104b12006-04-08 04:15:24 +00002058 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00002059 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, DstEltVT, BV->getOperand(i)));
Chris Lattner3e104b12006-04-08 04:15:24 +00002060 AddToWorkList(Ops.back().Val);
2061 }
Chris Lattner6258fb22006-04-02 02:53:43 +00002062 Ops.push_back(*(BV->op_end()-2)); // Add num elements.
2063 Ops.push_back(DAG.getValueType(DstEltVT));
2064 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, Ops);
2065 }
2066
2067 // Otherwise, we're growing or shrinking the elements. To avoid having to
2068 // handle annoying details of growing/shrinking FP values, we convert them to
2069 // int first.
2070 if (MVT::isFloatingPoint(SrcEltVT)) {
2071 // Convert the input float vector to a int vector where the elements are the
2072 // same sizes.
2073 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
2074 MVT::ValueType IntVT = SrcEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
2075 BV = ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(BV, IntVT).Val;
2076 SrcEltVT = IntVT;
2077 }
2078
2079 // Now we know the input is an integer vector. If the output is a FP type,
2080 // convert to integer first, then to FP of the right size.
2081 if (MVT::isFloatingPoint(DstEltVT)) {
2082 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
2083 MVT::ValueType TmpVT = DstEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
2084 SDNode *Tmp = ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(BV, TmpVT).Val;
2085
2086 // Next, convert to FP elements of the same size.
2087 return ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(Tmp, DstEltVT);
2088 }
2089
2090 // Okay, we know the src/dst types are both integers of differing types.
2091 // Handling growing first.
2092 assert(MVT::isInteger(SrcEltVT) && MVT::isInteger(DstEltVT));
2093 if (SrcBitSize < DstBitSize) {
2094 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
2095
2096 std::vector<SDOperand> Ops;
2097 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e;
2098 i += NumInputsPerOutput) {
2099 bool isLE = TLI.isLittleEndian();
2100 uint64_t NewBits = 0;
2101 bool EltIsUndef = true;
2102 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
2103 // Shift the previously computed bits over.
2104 NewBits <<= SrcBitSize;
2105 SDOperand Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
2106 if (Op.getOpcode() == ISD::UNDEF) continue;
2107 EltIsUndef = false;
2108
2109 NewBits |= cast<ConstantSDNode>(Op)->getValue();
2110 }
2111
2112 if (EltIsUndef)
2113 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
2114 else
2115 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
2116 }
2117
2118 Ops.push_back(DAG.getConstant(Ops.size(), MVT::i32)); // Add num elements.
2119 Ops.push_back(DAG.getValueType(DstEltVT)); // Add element size.
2120 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, Ops);
2121 }
2122
2123 // Finally, this must be the case where we are shrinking elements: each input
2124 // turns into multiple outputs.
2125 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
2126 std::vector<SDOperand> Ops;
2127 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e; ++i) {
2128 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
2129 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
2130 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
2131 continue;
2132 }
2133 uint64_t OpVal = cast<ConstantSDNode>(BV->getOperand(i))->getValue();
2134
2135 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
2136 unsigned ThisVal = OpVal & ((1ULL << DstBitSize)-1);
2137 OpVal >>= DstBitSize;
2138 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
2139 }
2140
2141 // For big endian targets, swap the order of the pieces of each element.
2142 if (!TLI.isLittleEndian())
2143 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
2144 }
2145 Ops.push_back(DAG.getConstant(Ops.size(), MVT::i32)); // Add num elements.
2146 Ops.push_back(DAG.getValueType(DstEltVT)); // Add element size.
2147 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, Ops);
2148}
2149
2150
2151
Chris Lattner01b3d732005-09-28 22:28:18 +00002152SDOperand DAGCombiner::visitFADD(SDNode *N) {
2153 SDOperand N0 = N->getOperand(0);
2154 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00002155 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2156 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002157 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00002158
2159 // fold (fadd c1, c2) -> c1+c2
2160 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002161 return DAG.getNode(ISD::FADD, VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00002162 // canonicalize constant to RHS
2163 if (N0CFP && !N1CFP)
2164 return DAG.getNode(ISD::FADD, VT, N1, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00002165 // fold (A + (-B)) -> A-B
2166 if (N1.getOpcode() == ISD::FNEG)
2167 return DAG.getNode(ISD::FSUB, VT, N0, N1.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00002168 // fold ((-A) + B) -> B-A
2169 if (N0.getOpcode() == ISD::FNEG)
2170 return DAG.getNode(ISD::FSUB, VT, N1, N0.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00002171 return SDOperand();
2172}
2173
2174SDOperand DAGCombiner::visitFSUB(SDNode *N) {
2175 SDOperand N0 = N->getOperand(0);
2176 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00002177 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2178 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002179 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00002180
2181 // fold (fsub c1, c2) -> c1-c2
2182 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002183 return DAG.getNode(ISD::FSUB, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002184 // fold (A-(-B)) -> A+B
2185 if (N1.getOpcode() == ISD::FNEG)
Nate Begemana148d982006-01-18 22:35:16 +00002186 return DAG.getNode(ISD::FADD, VT, N0, N1.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00002187 return SDOperand();
2188}
2189
2190SDOperand DAGCombiner::visitFMUL(SDNode *N) {
2191 SDOperand N0 = N->getOperand(0);
2192 SDOperand N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00002193 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2194 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002195 MVT::ValueType VT = N->getValueType(0);
2196
Nate Begeman11af4ea2005-10-17 20:40:11 +00002197 // fold (fmul c1, c2) -> c1*c2
2198 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002199 return DAG.getNode(ISD::FMUL, VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00002200 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00002201 if (N0CFP && !N1CFP)
2202 return DAG.getNode(ISD::FMUL, VT, N1, N0);
Nate Begeman11af4ea2005-10-17 20:40:11 +00002203 // fold (fmul X, 2.0) -> (fadd X, X)
2204 if (N1CFP && N1CFP->isExactlyValue(+2.0))
2205 return DAG.getNode(ISD::FADD, VT, N0, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00002206 return SDOperand();
2207}
2208
2209SDOperand DAGCombiner::visitFDIV(SDNode *N) {
2210 SDOperand N0 = N->getOperand(0);
2211 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00002212 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2213 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002214 MVT::ValueType VT = N->getValueType(0);
2215
Nate Begemana148d982006-01-18 22:35:16 +00002216 // fold (fdiv c1, c2) -> c1/c2
2217 if (N0CFP && N1CFP)
2218 return DAG.getNode(ISD::FDIV, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002219 return SDOperand();
2220}
2221
2222SDOperand DAGCombiner::visitFREM(SDNode *N) {
2223 SDOperand N0 = N->getOperand(0);
2224 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00002225 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2226 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002227 MVT::ValueType VT = N->getValueType(0);
2228
Nate Begemana148d982006-01-18 22:35:16 +00002229 // fold (frem c1, c2) -> fmod(c1,c2)
2230 if (N0CFP && N1CFP)
2231 return DAG.getNode(ISD::FREM, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002232 return SDOperand();
2233}
2234
Chris Lattner12d83032006-03-05 05:30:57 +00002235SDOperand DAGCombiner::visitFCOPYSIGN(SDNode *N) {
2236 SDOperand N0 = N->getOperand(0);
2237 SDOperand N1 = N->getOperand(1);
2238 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2239 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2240 MVT::ValueType VT = N->getValueType(0);
2241
2242 if (N0CFP && N1CFP) // Constant fold
2243 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1);
2244
2245 if (N1CFP) {
2246 // copysign(x, c1) -> fabs(x) iff ispos(c1)
2247 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
2248 union {
2249 double d;
2250 int64_t i;
2251 } u;
2252 u.d = N1CFP->getValue();
2253 if (u.i >= 0)
2254 return DAG.getNode(ISD::FABS, VT, N0);
2255 else
2256 return DAG.getNode(ISD::FNEG, VT, DAG.getNode(ISD::FABS, VT, N0));
2257 }
2258
2259 // copysign(fabs(x), y) -> copysign(x, y)
2260 // copysign(fneg(x), y) -> copysign(x, y)
2261 // copysign(copysign(x,z), y) -> copysign(x, y)
2262 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
2263 N0.getOpcode() == ISD::FCOPYSIGN)
2264 return DAG.getNode(ISD::FCOPYSIGN, VT, N0.getOperand(0), N1);
2265
2266 // copysign(x, abs(y)) -> abs(x)
2267 if (N1.getOpcode() == ISD::FABS)
2268 return DAG.getNode(ISD::FABS, VT, N0);
2269
2270 // copysign(x, copysign(y,z)) -> copysign(x, z)
2271 if (N1.getOpcode() == ISD::FCOPYSIGN)
2272 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(1));
2273
2274 // copysign(x, fp_extend(y)) -> copysign(x, y)
2275 // copysign(x, fp_round(y)) -> copysign(x, y)
2276 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
2277 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(0));
2278
2279 return SDOperand();
2280}
2281
2282
Chris Lattner01b3d732005-09-28 22:28:18 +00002283
Nate Begeman83e75ec2005-09-06 04:43:02 +00002284SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002285 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002286 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00002287 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002288
2289 // fold (sint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002290 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00002291 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002292 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002293}
2294
Nate Begeman83e75ec2005-09-06 04:43:02 +00002295SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002296 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002297 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00002298 MVT::ValueType VT = N->getValueType(0);
2299
Nate Begeman1d4d4142005-09-01 00:19:25 +00002300 // fold (uint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002301 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00002302 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002303 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002304}
2305
Nate Begeman83e75ec2005-09-06 04:43:02 +00002306SDOperand DAGCombiner::visitFP_TO_SINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002307 SDOperand N0 = N->getOperand(0);
2308 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2309 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002310
2311 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00002312 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002313 return DAG.getNode(ISD::FP_TO_SINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002314 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002315}
2316
Nate Begeman83e75ec2005-09-06 04:43:02 +00002317SDOperand DAGCombiner::visitFP_TO_UINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002318 SDOperand N0 = N->getOperand(0);
2319 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2320 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002321
2322 // fold (fp_to_uint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00002323 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002324 return DAG.getNode(ISD::FP_TO_UINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002325 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002326}
2327
Nate Begeman83e75ec2005-09-06 04:43:02 +00002328SDOperand DAGCombiner::visitFP_ROUND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002329 SDOperand N0 = N->getOperand(0);
2330 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2331 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002332
2333 // fold (fp_round c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002334 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002335 return DAG.getNode(ISD::FP_ROUND, VT, N0);
Chris Lattner79dbea52006-03-13 06:26:26 +00002336
2337 // fold (fp_round (fp_extend x)) -> x
2338 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
2339 return N0.getOperand(0);
2340
2341 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
2342 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.Val->hasOneUse()) {
2343 SDOperand Tmp = DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0));
2344 AddToWorkList(Tmp.Val);
2345 return DAG.getNode(ISD::FCOPYSIGN, VT, Tmp, N0.getOperand(1));
2346 }
2347
Nate Begeman83e75ec2005-09-06 04:43:02 +00002348 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002349}
2350
Nate Begeman83e75ec2005-09-06 04:43:02 +00002351SDOperand DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002352 SDOperand N0 = N->getOperand(0);
2353 MVT::ValueType VT = N->getValueType(0);
2354 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00002355 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002356
Nate Begeman1d4d4142005-09-01 00:19:25 +00002357 // fold (fp_round_inreg c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002358 if (N0CFP) {
2359 SDOperand Round = DAG.getConstantFP(N0CFP->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002360 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002361 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00002362 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002363}
2364
Nate Begeman83e75ec2005-09-06 04:43:02 +00002365SDOperand DAGCombiner::visitFP_EXTEND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002366 SDOperand N0 = N->getOperand(0);
2367 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2368 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002369
2370 // fold (fp_extend c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002371 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002372 return DAG.getNode(ISD::FP_EXTEND, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002373 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002374}
2375
Nate Begeman83e75ec2005-09-06 04:43:02 +00002376SDOperand DAGCombiner::visitFNEG(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002377 SDOperand N0 = N->getOperand(0);
2378 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2379 MVT::ValueType VT = N->getValueType(0);
2380
2381 // fold (fneg c1) -> -c1
Nate Begeman646d7e22005-09-02 21:18:40 +00002382 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002383 return DAG.getNode(ISD::FNEG, VT, N0);
2384 // fold (fneg (sub x, y)) -> (sub y, x)
Chris Lattner12d83032006-03-05 05:30:57 +00002385 if (N0.getOpcode() == ISD::SUB)
2386 return DAG.getNode(ISD::SUB, VT, N0.getOperand(1), N0.getOperand(0));
Nate Begemana148d982006-01-18 22:35:16 +00002387 // fold (fneg (fneg x)) -> x
Chris Lattner12d83032006-03-05 05:30:57 +00002388 if (N0.getOpcode() == ISD::FNEG)
2389 return N0.getOperand(0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002390 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002391}
2392
Nate Begeman83e75ec2005-09-06 04:43:02 +00002393SDOperand DAGCombiner::visitFABS(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002394 SDOperand N0 = N->getOperand(0);
2395 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2396 MVT::ValueType VT = N->getValueType(0);
2397
Nate Begeman1d4d4142005-09-01 00:19:25 +00002398 // fold (fabs c1) -> fabs(c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00002399 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002400 return DAG.getNode(ISD::FABS, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002401 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00002402 if (N0.getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002403 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002404 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00002405 // fold (fabs (fcopysign x, y)) -> (fabs x)
2406 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
2407 return DAG.getNode(ISD::FABS, VT, N0.getOperand(0));
2408
Nate Begeman83e75ec2005-09-06 04:43:02 +00002409 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002410}
2411
Nate Begeman44728a72005-09-19 22:34:01 +00002412SDOperand DAGCombiner::visitBRCOND(SDNode *N) {
2413 SDOperand Chain = N->getOperand(0);
2414 SDOperand N1 = N->getOperand(1);
2415 SDOperand N2 = N->getOperand(2);
2416 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2417
2418 // never taken branch, fold to chain
2419 if (N1C && N1C->isNullValue())
2420 return Chain;
2421 // unconditional branch
Nate Begemane17daeb2005-10-05 21:43:42 +00002422 if (N1C && N1C->getValue() == 1)
Nate Begeman44728a72005-09-19 22:34:01 +00002423 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
Nate Begeman750ac1b2006-02-01 07:19:44 +00002424 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
2425 // on the target.
2426 if (N1.getOpcode() == ISD::SETCC &&
2427 TLI.isOperationLegal(ISD::BR_CC, MVT::Other)) {
2428 return DAG.getNode(ISD::BR_CC, MVT::Other, Chain, N1.getOperand(2),
2429 N1.getOperand(0), N1.getOperand(1), N2);
2430 }
Nate Begeman44728a72005-09-19 22:34:01 +00002431 return SDOperand();
2432}
2433
Chris Lattner3ea0b472005-10-05 06:47:48 +00002434// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
2435//
Nate Begeman44728a72005-09-19 22:34:01 +00002436SDOperand DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00002437 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
2438 SDOperand CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
2439
2440 // Use SimplifySetCC to simplify SETCC's.
Nate Begemane17daeb2005-10-05 21:43:42 +00002441 SDOperand Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false);
2442 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.Val);
2443
2444 // fold br_cc true, dest -> br dest (unconditional branch)
2445 if (SCCC && SCCC->getValue())
2446 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
2447 N->getOperand(4));
2448 // fold br_cc false, dest -> unconditional fall through
2449 if (SCCC && SCCC->isNullValue())
2450 return N->getOperand(0);
2451 // fold to a simpler setcc
2452 if (Simp.Val && Simp.getOpcode() == ISD::SETCC)
2453 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
2454 Simp.getOperand(2), Simp.getOperand(0),
2455 Simp.getOperand(1), N->getOperand(4));
Nate Begeman44728a72005-09-19 22:34:01 +00002456 return SDOperand();
2457}
2458
Chris Lattner01a22022005-10-10 22:04:48 +00002459SDOperand DAGCombiner::visitLOAD(SDNode *N) {
2460 SDOperand Chain = N->getOperand(0);
2461 SDOperand Ptr = N->getOperand(1);
2462 SDOperand SrcValue = N->getOperand(2);
Chris Lattnere4b95392006-03-31 18:06:18 +00002463
2464 // If there are no uses of the loaded value, change uses of the chain value
2465 // into uses of the chain input (i.e. delete the dead load).
2466 if (N->hasNUsesOfValue(0, 0))
2467 return CombineTo(N, DAG.getNode(ISD::UNDEF, N->getValueType(0)), Chain);
Chris Lattner01a22022005-10-10 22:04:48 +00002468
2469 // If this load is directly stored, replace the load value with the stored
2470 // value.
2471 // TODO: Handle store large -> read small portion.
2472 // TODO: Handle TRUNCSTORE/EXTLOAD
2473 if (Chain.getOpcode() == ISD::STORE && Chain.getOperand(2) == Ptr &&
2474 Chain.getOperand(1).getValueType() == N->getValueType(0))
2475 return CombineTo(N, Chain.getOperand(1), Chain);
2476
2477 return SDOperand();
2478}
2479
Chris Lattner29cd7db2006-03-31 18:10:41 +00002480/// visitXEXTLOAD - Handle EXTLOAD/ZEXTLOAD/SEXTLOAD.
2481SDOperand DAGCombiner::visitXEXTLOAD(SDNode *N) {
2482 SDOperand Chain = N->getOperand(0);
2483 SDOperand Ptr = N->getOperand(1);
2484 SDOperand SrcValue = N->getOperand(2);
2485 SDOperand EVT = N->getOperand(3);
2486
2487 // If there are no uses of the loaded value, change uses of the chain value
2488 // into uses of the chain input (i.e. delete the dead load).
2489 if (N->hasNUsesOfValue(0, 0))
2490 return CombineTo(N, DAG.getNode(ISD::UNDEF, N->getValueType(0)), Chain);
2491
2492 return SDOperand();
2493}
2494
Chris Lattner87514ca2005-10-10 22:31:19 +00002495SDOperand DAGCombiner::visitSTORE(SDNode *N) {
2496 SDOperand Chain = N->getOperand(0);
2497 SDOperand Value = N->getOperand(1);
2498 SDOperand Ptr = N->getOperand(2);
2499 SDOperand SrcValue = N->getOperand(3);
2500
2501 // If this is a store that kills a previous store, remove the previous store.
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002502 if (Chain.getOpcode() == ISD::STORE && Chain.getOperand(2) == Ptr &&
Chris Lattnerfe7f0462005-10-27 07:10:34 +00002503 Chain.Val->hasOneUse() /* Avoid introducing DAG cycles */ &&
2504 // Make sure that these stores are the same value type:
2505 // FIXME: we really care that the second store is >= size of the first.
2506 Value.getValueType() == Chain.getOperand(1).getValueType()) {
Chris Lattner87514ca2005-10-10 22:31:19 +00002507 // Create a new store of Value that replaces both stores.
2508 SDNode *PrevStore = Chain.Val;
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002509 if (PrevStore->getOperand(1) == Value) // Same value multiply stored.
2510 return Chain;
Chris Lattner87514ca2005-10-10 22:31:19 +00002511 SDOperand NewStore = DAG.getNode(ISD::STORE, MVT::Other,
2512 PrevStore->getOperand(0), Value, Ptr,
2513 SrcValue);
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002514 CombineTo(N, NewStore); // Nuke this store.
Chris Lattner87514ca2005-10-10 22:31:19 +00002515 CombineTo(PrevStore, NewStore); // Nuke the previous store.
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002516 return SDOperand(N, 0);
Chris Lattner87514ca2005-10-10 22:31:19 +00002517 }
2518
Chris Lattnerc33baaa2005-12-23 05:48:07 +00002519 // If this is a store of a bit convert, store the input value.
Chris Lattnerbf40c4b2006-01-15 18:58:59 +00002520 // FIXME: This needs to know that the resultant store does not need a
2521 // higher alignment than the original.
2522 if (0 && Value.getOpcode() == ISD::BIT_CONVERT)
Chris Lattnerc33baaa2005-12-23 05:48:07 +00002523 return DAG.getNode(ISD::STORE, MVT::Other, Chain, Value.getOperand(0),
2524 Ptr, SrcValue);
2525
Chris Lattner87514ca2005-10-10 22:31:19 +00002526 return SDOperand();
2527}
2528
Chris Lattnerca242442006-03-19 01:27:56 +00002529SDOperand DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
2530 SDOperand InVec = N->getOperand(0);
2531 SDOperand InVal = N->getOperand(1);
2532 SDOperand EltNo = N->getOperand(2);
2533
2534 // If the invec is a BUILD_VECTOR and if EltNo is a constant, build a new
2535 // vector with the inserted element.
2536 if (InVec.getOpcode() == ISD::BUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
2537 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
2538 std::vector<SDOperand> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
2539 if (Elt < Ops.size())
2540 Ops[Elt] = InVal;
2541 return DAG.getNode(ISD::BUILD_VECTOR, InVec.getValueType(), Ops);
2542 }
2543
2544 return SDOperand();
2545}
2546
2547SDOperand DAGCombiner::visitVINSERT_VECTOR_ELT(SDNode *N) {
2548 SDOperand InVec = N->getOperand(0);
2549 SDOperand InVal = N->getOperand(1);
2550 SDOperand EltNo = N->getOperand(2);
2551 SDOperand NumElts = N->getOperand(3);
2552 SDOperand EltType = N->getOperand(4);
2553
2554 // If the invec is a VBUILD_VECTOR and if EltNo is a constant, build a new
2555 // vector with the inserted element.
2556 if (InVec.getOpcode() == ISD::VBUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
2557 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
2558 std::vector<SDOperand> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
2559 if (Elt < Ops.size()-2)
2560 Ops[Elt] = InVal;
2561 return DAG.getNode(ISD::VBUILD_VECTOR, InVec.getValueType(), Ops);
2562 }
2563
2564 return SDOperand();
2565}
2566
Chris Lattnerd7648c82006-03-28 20:28:38 +00002567SDOperand DAGCombiner::visitVBUILD_VECTOR(SDNode *N) {
2568 unsigned NumInScalars = N->getNumOperands()-2;
2569 SDOperand NumElts = N->getOperand(NumInScalars);
2570 SDOperand EltType = N->getOperand(NumInScalars+1);
2571
2572 // Check to see if this is a VBUILD_VECTOR of a bunch of VEXTRACT_VECTOR_ELT
2573 // operations. If so, and if the EXTRACT_ELT vector inputs come from at most
2574 // two distinct vectors, turn this into a shuffle node.
2575 SDOperand VecIn1, VecIn2;
2576 for (unsigned i = 0; i != NumInScalars; ++i) {
2577 // Ignore undef inputs.
2578 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
2579
2580 // If this input is something other than a VEXTRACT_VECTOR_ELT with a
2581 // constant index, bail out.
2582 if (N->getOperand(i).getOpcode() != ISD::VEXTRACT_VECTOR_ELT ||
2583 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
2584 VecIn1 = VecIn2 = SDOperand(0, 0);
2585 break;
2586 }
2587
2588 // If the input vector type disagrees with the result of the vbuild_vector,
2589 // we can't make a shuffle.
2590 SDOperand ExtractedFromVec = N->getOperand(i).getOperand(0);
2591 if (*(ExtractedFromVec.Val->op_end()-2) != NumElts ||
2592 *(ExtractedFromVec.Val->op_end()-1) != EltType) {
2593 VecIn1 = VecIn2 = SDOperand(0, 0);
2594 break;
2595 }
2596
2597 // Otherwise, remember this. We allow up to two distinct input vectors.
2598 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
2599 continue;
2600
2601 if (VecIn1.Val == 0) {
2602 VecIn1 = ExtractedFromVec;
2603 } else if (VecIn2.Val == 0) {
2604 VecIn2 = ExtractedFromVec;
2605 } else {
2606 // Too many inputs.
2607 VecIn1 = VecIn2 = SDOperand(0, 0);
2608 break;
2609 }
2610 }
2611
2612 // If everything is good, we can make a shuffle operation.
2613 if (VecIn1.Val) {
2614 std::vector<SDOperand> BuildVecIndices;
2615 for (unsigned i = 0; i != NumInScalars; ++i) {
2616 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
2617 BuildVecIndices.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
2618 continue;
2619 }
2620
2621 SDOperand Extract = N->getOperand(i);
2622
2623 // If extracting from the first vector, just use the index directly.
2624 if (Extract.getOperand(0) == VecIn1) {
2625 BuildVecIndices.push_back(Extract.getOperand(1));
2626 continue;
2627 }
2628
2629 // Otherwise, use InIdx + VecSize
2630 unsigned Idx = cast<ConstantSDNode>(Extract.getOperand(1))->getValue();
2631 BuildVecIndices.push_back(DAG.getConstant(Idx+NumInScalars, MVT::i32));
2632 }
2633
2634 // Add count and size info.
2635 BuildVecIndices.push_back(NumElts);
2636 BuildVecIndices.push_back(DAG.getValueType(MVT::i32));
2637
2638 // Return the new VVECTOR_SHUFFLE node.
2639 std::vector<SDOperand> Ops;
2640 Ops.push_back(VecIn1);
Chris Lattnercef896e2006-03-28 22:19:47 +00002641 if (VecIn2.Val) {
2642 Ops.push_back(VecIn2);
2643 } else {
2644 // Use an undef vbuild_vector as input for the second operand.
2645 std::vector<SDOperand> UnOps(NumInScalars,
2646 DAG.getNode(ISD::UNDEF,
2647 cast<VTSDNode>(EltType)->getVT()));
2648 UnOps.push_back(NumElts);
2649 UnOps.push_back(EltType);
2650 Ops.push_back(DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, UnOps));
Chris Lattner3e104b12006-04-08 04:15:24 +00002651 AddToWorkList(Ops.back().Val);
Chris Lattnercef896e2006-03-28 22:19:47 +00002652 }
Chris Lattnerd7648c82006-03-28 20:28:38 +00002653 Ops.push_back(DAG.getNode(ISD::VBUILD_VECTOR,MVT::Vector, BuildVecIndices));
2654 Ops.push_back(NumElts);
2655 Ops.push_back(EltType);
2656 return DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector, Ops);
2657 }
2658
2659 return SDOperand();
2660}
2661
Chris Lattner66445d32006-03-28 22:11:53 +00002662SDOperand DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
Chris Lattnerf1d0c622006-03-31 22:16:43 +00002663 SDOperand ShufMask = N->getOperand(2);
2664 unsigned NumElts = ShufMask.getNumOperands();
2665
2666 // If the shuffle mask is an identity operation on the LHS, return the LHS.
2667 bool isIdentity = true;
2668 for (unsigned i = 0; i != NumElts; ++i) {
2669 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
2670 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
2671 isIdentity = false;
2672 break;
2673 }
2674 }
2675 if (isIdentity) return N->getOperand(0);
2676
2677 // If the shuffle mask is an identity operation on the RHS, return the RHS.
2678 isIdentity = true;
2679 for (unsigned i = 0; i != NumElts; ++i) {
2680 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
2681 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
2682 isIdentity = false;
2683 break;
2684 }
2685 }
2686 if (isIdentity) return N->getOperand(1);
2687
Chris Lattner66445d32006-03-28 22:11:53 +00002688 // If the LHS and the RHS are the same node, turn the RHS into an undef.
2689 if (N->getOperand(0) == N->getOperand(1)) {
Evan Chengc04766a2006-04-06 23:20:43 +00002690 if (N->getOperand(0).getOpcode() == ISD::UNDEF)
2691 return DAG.getNode(ISD::UNDEF, N->getValueType(0));
Chris Lattner66445d32006-03-28 22:11:53 +00002692 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
2693 // first operand.
2694 std::vector<SDOperand> MappedOps;
Chris Lattner66445d32006-03-28 22:11:53 +00002695 for (unsigned i = 0, e = ShufMask.getNumOperands(); i != e; ++i) {
Evan Chengc04766a2006-04-06 23:20:43 +00002696 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
2697 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) {
2698 MappedOps.push_back(ShufMask.getOperand(i));
2699 } else {
Chris Lattner66445d32006-03-28 22:11:53 +00002700 unsigned NewIdx =
2701 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
2702 MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32));
Chris Lattner66445d32006-03-28 22:11:53 +00002703 }
2704 }
2705 ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMask.getValueType(),
2706 MappedOps);
Chris Lattner3e104b12006-04-08 04:15:24 +00002707 AddToWorkList(ShufMask.Val);
Chris Lattner66445d32006-03-28 22:11:53 +00002708 return DAG.getNode(ISD::VECTOR_SHUFFLE, N->getValueType(0),
2709 N->getOperand(0),
2710 DAG.getNode(ISD::UNDEF, N->getValueType(0)),
2711 ShufMask);
2712 }
2713
2714 return SDOperand();
2715}
2716
Chris Lattnerf1d0c622006-03-31 22:16:43 +00002717SDOperand DAGCombiner::visitVVECTOR_SHUFFLE(SDNode *N) {
2718 SDOperand ShufMask = N->getOperand(2);
2719 unsigned NumElts = ShufMask.getNumOperands()-2;
2720
2721 // If the shuffle mask is an identity operation on the LHS, return the LHS.
2722 bool isIdentity = true;
2723 for (unsigned i = 0; i != NumElts; ++i) {
2724 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
2725 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
2726 isIdentity = false;
2727 break;
2728 }
2729 }
2730 if (isIdentity) return N->getOperand(0);
2731
2732 // If the shuffle mask is an identity operation on the RHS, return the RHS.
2733 isIdentity = true;
2734 for (unsigned i = 0; i != NumElts; ++i) {
2735 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
2736 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
2737 isIdentity = false;
2738 break;
2739 }
2740 }
2741 if (isIdentity) return N->getOperand(1);
2742
Chris Lattner17614ea2006-04-08 05:34:25 +00002743 // If the LHS and the RHS are the same node, turn the RHS into an undef.
2744 if (N->getOperand(0) == N->getOperand(1)) {
2745 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
2746 // first operand.
2747 std::vector<SDOperand> MappedOps;
2748 for (unsigned i = 0; i != NumElts; ++i) {
2749 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
2750 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) {
2751 MappedOps.push_back(ShufMask.getOperand(i));
2752 } else {
2753 unsigned NewIdx =
2754 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
2755 MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32));
2756 }
2757 }
2758 // Add the type/#elts values.
2759 MappedOps.push_back(ShufMask.getOperand(NumElts));
2760 MappedOps.push_back(ShufMask.getOperand(NumElts+1));
2761
2762 ShufMask = DAG.getNode(ISD::VBUILD_VECTOR, ShufMask.getValueType(),
2763 MappedOps);
2764 AddToWorkList(ShufMask.Val);
2765
2766 // Build the undef vector.
2767 SDOperand UDVal = DAG.getNode(ISD::UNDEF, MappedOps[0].getValueType());
2768 for (unsigned i = 0; i != NumElts; ++i)
2769 MappedOps[i] = UDVal;
2770 MappedOps[NumElts ] = *(N->getOperand(0).Val->op_end()-2);
2771 MappedOps[NumElts+1] = *(N->getOperand(0).Val->op_end()-1);
2772 UDVal = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, MappedOps);
2773
2774 return DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector,
2775 N->getOperand(0), UDVal, ShufMask,
2776 MappedOps[NumElts], MappedOps[NumElts+1]);
2777 }
2778
Chris Lattnerf1d0c622006-03-31 22:16:43 +00002779 return SDOperand();
2780}
2781
Evan Cheng44f1f092006-04-20 08:56:16 +00002782/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
2783/// a VAND to a vector_shuffle with the destination vector and a zero vector.
2784/// e.g. VAND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
2785/// vector_shuffle V, Zero, <0, 4, 2, 4>
2786SDOperand DAGCombiner::XformToShuffleWithZero(SDNode *N) {
2787 SDOperand LHS = N->getOperand(0);
2788 SDOperand RHS = N->getOperand(1);
2789 if (N->getOpcode() == ISD::VAND) {
2790 SDOperand DstVecSize = *(LHS.Val->op_end()-2);
2791 SDOperand DstVecEVT = *(LHS.Val->op_end()-1);
2792 if (RHS.getOpcode() == ISD::VBIT_CONVERT)
2793 RHS = RHS.getOperand(0);
2794 if (RHS.getOpcode() == ISD::VBUILD_VECTOR) {
2795 std::vector<SDOperand> IdxOps;
2796 unsigned NumOps = RHS.getNumOperands();
2797 unsigned NumElts = NumOps-2;
2798 MVT::ValueType EVT = cast<VTSDNode>(RHS.getOperand(NumOps-1))->getVT();
2799 for (unsigned i = 0; i != NumElts; ++i) {
2800 SDOperand Elt = RHS.getOperand(i);
2801 if (!isa<ConstantSDNode>(Elt))
2802 return SDOperand();
2803 else if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
2804 IdxOps.push_back(DAG.getConstant(i, EVT));
2805 else if (cast<ConstantSDNode>(Elt)->isNullValue())
2806 IdxOps.push_back(DAG.getConstant(NumElts, EVT));
2807 else
2808 return SDOperand();
2809 }
2810
2811 // Let's see if the target supports this vector_shuffle.
2812 if (!TLI.isVectorClearMaskLegal(IdxOps, EVT, DAG))
2813 return SDOperand();
2814
2815 // Return the new VVECTOR_SHUFFLE node.
2816 SDOperand NumEltsNode = DAG.getConstant(NumElts, MVT::i32);
2817 SDOperand EVTNode = DAG.getValueType(EVT);
2818 std::vector<SDOperand> Ops;
2819 LHS = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, LHS, NumEltsNode, EVTNode);
2820 Ops.push_back(LHS);
2821 AddToWorkList(LHS.Val);
2822 std::vector<SDOperand> ZeroOps(NumElts, DAG.getConstant(0, EVT));
2823 ZeroOps.push_back(NumEltsNode);
2824 ZeroOps.push_back(EVTNode);
2825 Ops.push_back(DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, ZeroOps));
2826 IdxOps.push_back(NumEltsNode);
2827 IdxOps.push_back(EVTNode);
2828 Ops.push_back(DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, IdxOps));
2829 Ops.push_back(NumEltsNode);
2830 Ops.push_back(EVTNode);
2831 SDOperand Result = DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector, Ops);
2832 if (NumEltsNode != DstVecSize || EVTNode != DstVecEVT) {
2833 Result = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Result,
2834 DstVecSize, DstVecEVT);
2835 }
2836 return Result;
2837 }
2838 }
2839 return SDOperand();
2840}
2841
Chris Lattneredab1b92006-04-02 03:25:57 +00002842/// visitVBinOp - Visit a binary vector operation, like VADD. IntOp indicates
2843/// the scalar operation of the vop if it is operating on an integer vector
2844/// (e.g. ADD) and FPOp indicates the FP version (e.g. FADD).
2845SDOperand DAGCombiner::visitVBinOp(SDNode *N, ISD::NodeType IntOp,
2846 ISD::NodeType FPOp) {
2847 MVT::ValueType EltType = cast<VTSDNode>(*(N->op_end()-1))->getVT();
2848 ISD::NodeType ScalarOp = MVT::isInteger(EltType) ? IntOp : FPOp;
2849 SDOperand LHS = N->getOperand(0);
2850 SDOperand RHS = N->getOperand(1);
Evan Cheng44f1f092006-04-20 08:56:16 +00002851 SDOperand Shuffle = XformToShuffleWithZero(N);
2852 if (Shuffle.Val) return Shuffle;
2853
Chris Lattneredab1b92006-04-02 03:25:57 +00002854 // If the LHS and RHS are VBUILD_VECTOR nodes, see if we can constant fold
2855 // this operation.
2856 if (LHS.getOpcode() == ISD::VBUILD_VECTOR &&
2857 RHS.getOpcode() == ISD::VBUILD_VECTOR) {
2858 std::vector<SDOperand> Ops;
2859 for (unsigned i = 0, e = LHS.getNumOperands()-2; i != e; ++i) {
2860 SDOperand LHSOp = LHS.getOperand(i);
2861 SDOperand RHSOp = RHS.getOperand(i);
2862 // If these two elements can't be folded, bail out.
2863 if ((LHSOp.getOpcode() != ISD::UNDEF &&
2864 LHSOp.getOpcode() != ISD::Constant &&
2865 LHSOp.getOpcode() != ISD::ConstantFP) ||
2866 (RHSOp.getOpcode() != ISD::UNDEF &&
2867 RHSOp.getOpcode() != ISD::Constant &&
2868 RHSOp.getOpcode() != ISD::ConstantFP))
2869 break;
2870 Ops.push_back(DAG.getNode(ScalarOp, EltType, LHSOp, RHSOp));
Chris Lattner3e104b12006-04-08 04:15:24 +00002871 AddToWorkList(Ops.back().Val);
Chris Lattneredab1b92006-04-02 03:25:57 +00002872 assert((Ops.back().getOpcode() == ISD::UNDEF ||
2873 Ops.back().getOpcode() == ISD::Constant ||
2874 Ops.back().getOpcode() == ISD::ConstantFP) &&
2875 "Scalar binop didn't fold!");
2876 }
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00002877
2878 if (Ops.size() == LHS.getNumOperands()-2) {
2879 Ops.push_back(*(LHS.Val->op_end()-2));
2880 Ops.push_back(*(LHS.Val->op_end()-1));
2881 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, Ops);
2882 }
Chris Lattneredab1b92006-04-02 03:25:57 +00002883 }
2884
2885 return SDOperand();
2886}
2887
Nate Begeman44728a72005-09-19 22:34:01 +00002888SDOperand DAGCombiner::SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2){
Nate Begemanf845b452005-10-08 00:29:44 +00002889 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
2890
2891 SDOperand SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
2892 cast<CondCodeSDNode>(N0.getOperand(2))->get());
2893 // If we got a simplified select_cc node back from SimplifySelectCC, then
2894 // break it down into a new SETCC node, and a new SELECT node, and then return
2895 // the SELECT node, since we were called with a SELECT node.
2896 if (SCC.Val) {
2897 // Check to see if we got a select_cc back (to turn into setcc/select).
2898 // Otherwise, just return whatever node we got back, like fabs.
2899 if (SCC.getOpcode() == ISD::SELECT_CC) {
2900 SDOperand SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
2901 SCC.getOperand(0), SCC.getOperand(1),
2902 SCC.getOperand(4));
Chris Lattner5750df92006-03-01 04:03:14 +00002903 AddToWorkList(SETCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00002904 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
2905 SCC.getOperand(3), SETCC);
2906 }
2907 return SCC;
2908 }
Nate Begeman44728a72005-09-19 22:34:01 +00002909 return SDOperand();
2910}
2911
Chris Lattner40c62d52005-10-18 06:04:22 +00002912/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
2913/// are the two values being selected between, see if we can simplify the
2914/// select.
2915///
2916bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDOperand LHS,
2917 SDOperand RHS) {
2918
2919 // If this is a select from two identical things, try to pull the operation
2920 // through the select.
2921 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
2922#if 0
2923 std::cerr << "SELECT: ["; LHS.Val->dump();
2924 std::cerr << "] ["; RHS.Val->dump();
2925 std::cerr << "]\n";
2926#endif
2927
2928 // If this is a load and the token chain is identical, replace the select
2929 // of two loads with a load through a select of the address to load from.
2930 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
2931 // constants have been dropped into the constant pool.
2932 if ((LHS.getOpcode() == ISD::LOAD ||
2933 LHS.getOpcode() == ISD::EXTLOAD ||
2934 LHS.getOpcode() == ISD::ZEXTLOAD ||
2935 LHS.getOpcode() == ISD::SEXTLOAD) &&
2936 // Token chains must be identical.
2937 LHS.getOperand(0) == RHS.getOperand(0) &&
2938 // If this is an EXTLOAD, the VT's must match.
2939 (LHS.getOpcode() == ISD::LOAD ||
2940 LHS.getOperand(3) == RHS.getOperand(3))) {
2941 // FIXME: this conflates two src values, discarding one. This is not
2942 // the right thing to do, but nothing uses srcvalues now. When they do,
2943 // turn SrcValue into a list of locations.
2944 SDOperand Addr;
2945 if (TheSelect->getOpcode() == ISD::SELECT)
2946 Addr = DAG.getNode(ISD::SELECT, LHS.getOperand(1).getValueType(),
2947 TheSelect->getOperand(0), LHS.getOperand(1),
2948 RHS.getOperand(1));
2949 else
2950 Addr = DAG.getNode(ISD::SELECT_CC, LHS.getOperand(1).getValueType(),
2951 TheSelect->getOperand(0),
2952 TheSelect->getOperand(1),
2953 LHS.getOperand(1), RHS.getOperand(1),
2954 TheSelect->getOperand(4));
2955
2956 SDOperand Load;
2957 if (LHS.getOpcode() == ISD::LOAD)
2958 Load = DAG.getLoad(TheSelect->getValueType(0), LHS.getOperand(0),
2959 Addr, LHS.getOperand(2));
2960 else
2961 Load = DAG.getExtLoad(LHS.getOpcode(), TheSelect->getValueType(0),
2962 LHS.getOperand(0), Addr, LHS.getOperand(2),
2963 cast<VTSDNode>(LHS.getOperand(3))->getVT());
2964 // Users of the select now use the result of the load.
2965 CombineTo(TheSelect, Load);
2966
2967 // Users of the old loads now use the new load's chain. We know the
2968 // old-load value is dead now.
2969 CombineTo(LHS.Val, Load.getValue(0), Load.getValue(1));
2970 CombineTo(RHS.Val, Load.getValue(0), Load.getValue(1));
2971 return true;
2972 }
2973 }
2974
2975 return false;
2976}
2977
Nate Begeman44728a72005-09-19 22:34:01 +00002978SDOperand DAGCombiner::SimplifySelectCC(SDOperand N0, SDOperand N1,
2979 SDOperand N2, SDOperand N3,
2980 ISD::CondCode CC) {
Nate Begemanf845b452005-10-08 00:29:44 +00002981
2982 MVT::ValueType VT = N2.getValueType();
2983 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
2984 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2985 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
2986 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
2987
2988 // Determine if the condition we're dealing with is constant
2989 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
2990 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
2991
2992 // fold select_cc true, x, y -> x
2993 if (SCCC && SCCC->getValue())
2994 return N2;
2995 // fold select_cc false, x, y -> y
2996 if (SCCC && SCCC->getValue() == 0)
2997 return N3;
2998
2999 // Check to see if we can simplify the select into an fabs node
3000 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
3001 // Allow either -0.0 or 0.0
3002 if (CFP->getValue() == 0.0) {
3003 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
3004 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
3005 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
3006 N2 == N3.getOperand(0))
3007 return DAG.getNode(ISD::FABS, VT, N0);
3008
3009 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
3010 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
3011 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
3012 N2.getOperand(0) == N3)
3013 return DAG.getNode(ISD::FABS, VT, N3);
3014 }
3015 }
3016
3017 // Check to see if we can perform the "gzip trick", transforming
3018 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
3019 if (N1C && N1C->isNullValue() && N3C && N3C->isNullValue() &&
3020 MVT::isInteger(N0.getValueType()) &&
3021 MVT::isInteger(N2.getValueType()) && CC == ISD::SETLT) {
3022 MVT::ValueType XType = N0.getValueType();
3023 MVT::ValueType AType = N2.getValueType();
3024 if (XType >= AType) {
3025 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00003026 // single-bit constant.
Nate Begemanf845b452005-10-08 00:29:44 +00003027 if (N2C && ((N2C->getValue() & (N2C->getValue()-1)) == 0)) {
3028 unsigned ShCtV = Log2_64(N2C->getValue());
3029 ShCtV = MVT::getSizeInBits(XType)-ShCtV-1;
3030 SDOperand ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
3031 SDOperand Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
Chris Lattner5750df92006-03-01 04:03:14 +00003032 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003033 if (XType > AType) {
3034 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00003035 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003036 }
3037 return DAG.getNode(ISD::AND, AType, Shift, N2);
3038 }
3039 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
3040 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3041 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00003042 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003043 if (XType > AType) {
3044 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00003045 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003046 }
3047 return DAG.getNode(ISD::AND, AType, Shift, N2);
3048 }
3049 }
Nate Begeman07ed4172005-10-10 21:26:48 +00003050
3051 // fold select C, 16, 0 -> shl C, 4
3052 if (N2C && N3C && N3C->isNullValue() && isPowerOf2_64(N2C->getValue()) &&
3053 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) {
3054 // Get a SetCC of the condition
3055 // FIXME: Should probably make sure that setcc is legal if we ever have a
3056 // target where it isn't.
Nate Begemanb0d04a72006-02-18 02:40:58 +00003057 SDOperand Temp, SCC;
Nate Begeman07ed4172005-10-10 21:26:48 +00003058 // cast from setcc result type to select result type
Nate Begemanb0d04a72006-02-18 02:40:58 +00003059 if (AfterLegalize) {
3060 SCC = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00003061 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
Nate Begemanb0d04a72006-02-18 02:40:58 +00003062 } else {
3063 SCC = DAG.getSetCC(MVT::i1, N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00003064 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00003065 }
Chris Lattner5750df92006-03-01 04:03:14 +00003066 AddToWorkList(SCC.Val);
3067 AddToWorkList(Temp.Val);
Nate Begeman07ed4172005-10-10 21:26:48 +00003068 // shl setcc result by log2 n2c
3069 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
3070 DAG.getConstant(Log2_64(N2C->getValue()),
3071 TLI.getShiftAmountTy()));
3072 }
3073
Nate Begemanf845b452005-10-08 00:29:44 +00003074 // Check to see if this is the equivalent of setcc
3075 // FIXME: Turn all of these into setcc if setcc if setcc is legal
3076 // otherwise, go ahead with the folds.
3077 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getValue() == 1ULL)) {
3078 MVT::ValueType XType = N0.getValueType();
3079 if (TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultTy())) {
3080 SDOperand Res = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
3081 if (Res.getValueType() != VT)
3082 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
3083 return Res;
3084 }
3085
3086 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
3087 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
3088 TLI.isOperationLegal(ISD::CTLZ, XType)) {
3089 SDOperand Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
3090 return DAG.getNode(ISD::SRL, XType, Ctlz,
3091 DAG.getConstant(Log2_32(MVT::getSizeInBits(XType)),
3092 TLI.getShiftAmountTy()));
3093 }
3094 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
3095 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
3096 SDOperand NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
3097 N0);
3098 SDOperand NotN0 = DAG.getNode(ISD::XOR, XType, N0,
3099 DAG.getConstant(~0ULL, XType));
3100 return DAG.getNode(ISD::SRL, XType,
3101 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
3102 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3103 TLI.getShiftAmountTy()));
3104 }
3105 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
3106 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
3107 SDOperand Sign = DAG.getNode(ISD::SRL, XType, N0,
3108 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3109 TLI.getShiftAmountTy()));
3110 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
3111 }
3112 }
3113
3114 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
3115 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
3116 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
3117 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1)) {
3118 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N2.getOperand(0))) {
3119 MVT::ValueType XType = N0.getValueType();
3120 if (SubC->isNullValue() && MVT::isInteger(XType)) {
3121 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
3122 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3123 TLI.getShiftAmountTy()));
3124 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00003125 AddToWorkList(Shift.Val);
3126 AddToWorkList(Add.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003127 return DAG.getNode(ISD::XOR, XType, Add, Shift);
3128 }
3129 }
3130 }
3131
Nate Begeman44728a72005-09-19 22:34:01 +00003132 return SDOperand();
3133}
3134
Nate Begeman452d7be2005-09-16 00:54:12 +00003135SDOperand DAGCombiner::SimplifySetCC(MVT::ValueType VT, SDOperand N0,
Nate Begemane17daeb2005-10-05 21:43:42 +00003136 SDOperand N1, ISD::CondCode Cond,
3137 bool foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00003138 // These setcc operations always fold.
3139 switch (Cond) {
3140 default: break;
3141 case ISD::SETFALSE:
3142 case ISD::SETFALSE2: return DAG.getConstant(0, VT);
3143 case ISD::SETTRUE:
3144 case ISD::SETTRUE2: return DAG.getConstant(1, VT);
3145 }
3146
3147 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
3148 uint64_t C1 = N1C->getValue();
3149 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val)) {
3150 uint64_t C0 = N0C->getValue();
3151
3152 // Sign extend the operands if required
3153 if (ISD::isSignedIntSetCC(Cond)) {
3154 C0 = N0C->getSignExtended();
3155 C1 = N1C->getSignExtended();
3156 }
3157
3158 switch (Cond) {
3159 default: assert(0 && "Unknown integer setcc!");
3160 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
3161 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
3162 case ISD::SETULT: return DAG.getConstant(C0 < C1, VT);
3163 case ISD::SETUGT: return DAG.getConstant(C0 > C1, VT);
3164 case ISD::SETULE: return DAG.getConstant(C0 <= C1, VT);
3165 case ISD::SETUGE: return DAG.getConstant(C0 >= C1, VT);
3166 case ISD::SETLT: return DAG.getConstant((int64_t)C0 < (int64_t)C1, VT);
3167 case ISD::SETGT: return DAG.getConstant((int64_t)C0 > (int64_t)C1, VT);
3168 case ISD::SETLE: return DAG.getConstant((int64_t)C0 <= (int64_t)C1, VT);
3169 case ISD::SETGE: return DAG.getConstant((int64_t)C0 >= (int64_t)C1, VT);
3170 }
3171 } else {
3172 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
3173 if (N0.getOpcode() == ISD::ZERO_EXTEND) {
3174 unsigned InSize = MVT::getSizeInBits(N0.getOperand(0).getValueType());
3175
3176 // If the comparison constant has bits in the upper part, the
3177 // zero-extended value could never match.
3178 if (C1 & (~0ULL << InSize)) {
3179 unsigned VSize = MVT::getSizeInBits(N0.getValueType());
3180 switch (Cond) {
3181 case ISD::SETUGT:
3182 case ISD::SETUGE:
3183 case ISD::SETEQ: return DAG.getConstant(0, VT);
3184 case ISD::SETULT:
3185 case ISD::SETULE:
3186 case ISD::SETNE: return DAG.getConstant(1, VT);
3187 case ISD::SETGT:
3188 case ISD::SETGE:
3189 // True if the sign bit of C1 is set.
3190 return DAG.getConstant((C1 & (1ULL << VSize)) != 0, VT);
3191 case ISD::SETLT:
3192 case ISD::SETLE:
3193 // True if the sign bit of C1 isn't set.
3194 return DAG.getConstant((C1 & (1ULL << VSize)) == 0, VT);
3195 default:
3196 break;
3197 }
3198 }
3199
3200 // Otherwise, we can perform the comparison with the low bits.
3201 switch (Cond) {
3202 case ISD::SETEQ:
3203 case ISD::SETNE:
3204 case ISD::SETUGT:
3205 case ISD::SETUGE:
3206 case ISD::SETULT:
3207 case ISD::SETULE:
3208 return DAG.getSetCC(VT, N0.getOperand(0),
3209 DAG.getConstant(C1, N0.getOperand(0).getValueType()),
3210 Cond);
3211 default:
3212 break; // todo, be more careful with signed comparisons
3213 }
3214 } else if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
3215 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
3216 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N0.getOperand(1))->getVT();
3217 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
3218 MVT::ValueType ExtDstTy = N0.getValueType();
3219 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
3220
3221 // If the extended part has any inconsistent bits, it cannot ever
3222 // compare equal. In other words, they have to be all ones or all
3223 // zeros.
3224 uint64_t ExtBits =
3225 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
3226 if ((C1 & ExtBits) != 0 && (C1 & ExtBits) != ExtBits)
3227 return DAG.getConstant(Cond == ISD::SETNE, VT);
3228
3229 SDOperand ZextOp;
3230 MVT::ValueType Op0Ty = N0.getOperand(0).getValueType();
3231 if (Op0Ty == ExtSrcTy) {
3232 ZextOp = N0.getOperand(0);
3233 } else {
3234 int64_t Imm = ~0ULL >> (64-ExtSrcTyBits);
3235 ZextOp = DAG.getNode(ISD::AND, Op0Ty, N0.getOperand(0),
3236 DAG.getConstant(Imm, Op0Ty));
3237 }
Chris Lattner5750df92006-03-01 04:03:14 +00003238 AddToWorkList(ZextOp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003239 // Otherwise, make this a use of a zext.
3240 return DAG.getSetCC(VT, ZextOp,
3241 DAG.getConstant(C1 & (~0ULL>>(64-ExtSrcTyBits)),
3242 ExtDstTy),
3243 Cond);
Chris Lattner3391bcd2006-02-08 02:13:15 +00003244 } else if ((N1C->getValue() == 0 || N1C->getValue() == 1) &&
3245 (Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
3246 (N0.getOpcode() == ISD::XOR ||
3247 (N0.getOpcode() == ISD::AND &&
3248 N0.getOperand(0).getOpcode() == ISD::XOR &&
3249 N0.getOperand(1) == N0.getOperand(0).getOperand(1))) &&
3250 isa<ConstantSDNode>(N0.getOperand(1)) &&
3251 cast<ConstantSDNode>(N0.getOperand(1))->getValue() == 1) {
3252 // If this is (X^1) == 0/1, swap the RHS and eliminate the xor. We can
3253 // only do this if the top bits are known zero.
3254 if (TLI.MaskedValueIsZero(N1,
3255 MVT::getIntVTBitMask(N0.getValueType())-1)) {
3256 // Okay, get the un-inverted input value.
3257 SDOperand Val;
3258 if (N0.getOpcode() == ISD::XOR)
3259 Val = N0.getOperand(0);
3260 else {
3261 assert(N0.getOpcode() == ISD::AND &&
3262 N0.getOperand(0).getOpcode() == ISD::XOR);
3263 // ((X^1)&1)^1 -> X & 1
3264 Val = DAG.getNode(ISD::AND, N0.getValueType(),
3265 N0.getOperand(0).getOperand(0), N0.getOperand(1));
3266 }
3267 return DAG.getSetCC(VT, Val, N1,
3268 Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ);
3269 }
Nate Begeman452d7be2005-09-16 00:54:12 +00003270 }
Chris Lattner5c46f742005-10-05 06:11:08 +00003271
Nate Begeman452d7be2005-09-16 00:54:12 +00003272 uint64_t MinVal, MaxVal;
3273 unsigned OperandBitSize = MVT::getSizeInBits(N1C->getValueType(0));
3274 if (ISD::isSignedIntSetCC(Cond)) {
3275 MinVal = 1ULL << (OperandBitSize-1);
3276 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
3277 MaxVal = ~0ULL >> (65-OperandBitSize);
3278 else
3279 MaxVal = 0;
3280 } else {
3281 MinVal = 0;
3282 MaxVal = ~0ULL >> (64-OperandBitSize);
3283 }
3284
3285 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
3286 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
3287 if (C1 == MinVal) return DAG.getConstant(1, VT); // X >= MIN --> true
3288 --C1; // X >= C0 --> X > (C0-1)
3289 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
3290 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
3291 }
3292
3293 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
3294 if (C1 == MaxVal) return DAG.getConstant(1, VT); // X <= MAX --> true
3295 ++C1; // X <= C0 --> X < (C0+1)
3296 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
3297 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
3298 }
3299
3300 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal)
3301 return DAG.getConstant(0, VT); // X < MIN --> false
3302
3303 // Canonicalize setgt X, Min --> setne X, Min
3304 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MinVal)
3305 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
Chris Lattnerc8597ca2005-10-21 21:23:25 +00003306 // Canonicalize setlt X, Max --> setne X, Max
3307 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MaxVal)
3308 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
Nate Begeman452d7be2005-09-16 00:54:12 +00003309
3310 // If we have setult X, 1, turn it into seteq X, 0
3311 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal+1)
3312 return DAG.getSetCC(VT, N0, DAG.getConstant(MinVal, N0.getValueType()),
3313 ISD::SETEQ);
3314 // If we have setugt X, Max-1, turn it into seteq X, Max
3315 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal-1)
3316 return DAG.getSetCC(VT, N0, DAG.getConstant(MaxVal, N0.getValueType()),
3317 ISD::SETEQ);
3318
3319 // If we have "setcc X, C0", check to see if we can shrink the immediate
3320 // by changing cc.
3321
3322 // SETUGT X, SINTMAX -> SETLT X, 0
3323 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
3324 C1 == (~0ULL >> (65-OperandBitSize)))
3325 return DAG.getSetCC(VT, N0, DAG.getConstant(0, N1.getValueType()),
3326 ISD::SETLT);
3327
3328 // FIXME: Implement the rest of these.
3329
3330 // Fold bit comparisons when we can.
3331 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
3332 VT == N0.getValueType() && N0.getOpcode() == ISD::AND)
3333 if (ConstantSDNode *AndRHS =
3334 dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
3335 if (Cond == ISD::SETNE && C1 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
3336 // Perform the xform if the AND RHS is a single bit.
3337 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
3338 return DAG.getNode(ISD::SRL, VT, N0,
3339 DAG.getConstant(Log2_64(AndRHS->getValue()),
3340 TLI.getShiftAmountTy()));
3341 }
3342 } else if (Cond == ISD::SETEQ && C1 == AndRHS->getValue()) {
3343 // (X & 8) == 8 --> (X & 8) >> 3
3344 // Perform the xform if C1 is a single bit.
3345 if ((C1 & (C1-1)) == 0) {
3346 return DAG.getNode(ISD::SRL, VT, N0,
3347 DAG.getConstant(Log2_64(C1),TLI.getShiftAmountTy()));
3348 }
3349 }
3350 }
3351 }
3352 } else if (isa<ConstantSDNode>(N0.Val)) {
3353 // Ensure that the constant occurs on the RHS.
3354 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
3355 }
3356
3357 if (ConstantFPSDNode *N0C = dyn_cast<ConstantFPSDNode>(N0.Val))
3358 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) {
3359 double C0 = N0C->getValue(), C1 = N1C->getValue();
3360
3361 switch (Cond) {
3362 default: break; // FIXME: Implement the rest of these!
3363 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
3364 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
3365 case ISD::SETLT: return DAG.getConstant(C0 < C1, VT);
3366 case ISD::SETGT: return DAG.getConstant(C0 > C1, VT);
3367 case ISD::SETLE: return DAG.getConstant(C0 <= C1, VT);
3368 case ISD::SETGE: return DAG.getConstant(C0 >= C1, VT);
3369 }
3370 } else {
3371 // Ensure that the constant occurs on the RHS.
3372 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
3373 }
3374
3375 if (N0 == N1) {
3376 // We can always fold X == Y for integer setcc's.
3377 if (MVT::isInteger(N0.getValueType()))
3378 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
3379 unsigned UOF = ISD::getUnorderedFlavor(Cond);
3380 if (UOF == 2) // FP operators that are undefined on NaNs.
3381 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
3382 if (UOF == unsigned(ISD::isTrueWhenEqual(Cond)))
3383 return DAG.getConstant(UOF, VT);
3384 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
3385 // if it is not already.
Chris Lattner4090aee2006-01-18 19:13:41 +00003386 ISD::CondCode NewCond = UOF == 0 ? ISD::SETO : ISD::SETUO;
Nate Begeman452d7be2005-09-16 00:54:12 +00003387 if (NewCond != Cond)
3388 return DAG.getSetCC(VT, N0, N1, NewCond);
3389 }
3390
3391 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
3392 MVT::isInteger(N0.getValueType())) {
3393 if (N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::SUB ||
3394 N0.getOpcode() == ISD::XOR) {
3395 // Simplify (X+Y) == (X+Z) --> Y == Z
3396 if (N0.getOpcode() == N1.getOpcode()) {
3397 if (N0.getOperand(0) == N1.getOperand(0))
3398 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(1), Cond);
3399 if (N0.getOperand(1) == N1.getOperand(1))
3400 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(0), Cond);
3401 if (isCommutativeBinOp(N0.getOpcode())) {
3402 // If X op Y == Y op X, try other combinations.
3403 if (N0.getOperand(0) == N1.getOperand(1))
3404 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(0), Cond);
3405 if (N0.getOperand(1) == N1.getOperand(0))
Chris Lattnera158eee2005-10-25 18:57:30 +00003406 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(1), Cond);
Nate Begeman452d7be2005-09-16 00:54:12 +00003407 }
3408 }
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003409
3410 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(N1)) {
3411 if (ConstantSDNode *LHSR = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
3412 // Turn (X+C1) == C2 --> X == C2-C1
3413 if (N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse()) {
3414 return DAG.getSetCC(VT, N0.getOperand(0),
3415 DAG.getConstant(RHSC->getValue()-LHSR->getValue(),
3416 N0.getValueType()), Cond);
3417 }
3418
3419 // Turn (X^C1) == C2 into X == C1^C2 iff X&~C1 = 0.
3420 if (N0.getOpcode() == ISD::XOR)
Chris Lattner5c46f742005-10-05 06:11:08 +00003421 // If we know that all of the inverted bits are zero, don't bother
3422 // performing the inversion.
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003423 if (TLI.MaskedValueIsZero(N0.getOperand(0), ~LHSR->getValue()))
Chris Lattner5c46f742005-10-05 06:11:08 +00003424 return DAG.getSetCC(VT, N0.getOperand(0),
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003425 DAG.getConstant(LHSR->getValue()^RHSC->getValue(),
Chris Lattner5c46f742005-10-05 06:11:08 +00003426 N0.getValueType()), Cond);
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003427 }
3428
3429 // Turn (C1-X) == C2 --> X == C1-C2
3430 if (ConstantSDNode *SUBC = dyn_cast<ConstantSDNode>(N0.getOperand(0))) {
3431 if (N0.getOpcode() == ISD::SUB && N0.Val->hasOneUse()) {
3432 return DAG.getSetCC(VT, N0.getOperand(1),
3433 DAG.getConstant(SUBC->getValue()-RHSC->getValue(),
3434 N0.getValueType()), Cond);
Chris Lattner5c46f742005-10-05 06:11:08 +00003435 }
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003436 }
3437 }
3438
Nate Begeman452d7be2005-09-16 00:54:12 +00003439 // Simplify (X+Z) == X --> Z == 0
3440 if (N0.getOperand(0) == N1)
3441 return DAG.getSetCC(VT, N0.getOperand(1),
3442 DAG.getConstant(0, N0.getValueType()), Cond);
3443 if (N0.getOperand(1) == N1) {
3444 if (isCommutativeBinOp(N0.getOpcode()))
3445 return DAG.getSetCC(VT, N0.getOperand(0),
3446 DAG.getConstant(0, N0.getValueType()), Cond);
3447 else {
3448 assert(N0.getOpcode() == ISD::SUB && "Unexpected operation!");
3449 // (Z-X) == X --> Z == X<<1
3450 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(),
3451 N1,
3452 DAG.getConstant(1,TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00003453 AddToWorkList(SH.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003454 return DAG.getSetCC(VT, N0.getOperand(0), SH, Cond);
3455 }
3456 }
3457 }
3458
3459 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
3460 N1.getOpcode() == ISD::XOR) {
3461 // Simplify X == (X+Z) --> Z == 0
3462 if (N1.getOperand(0) == N0) {
3463 return DAG.getSetCC(VT, N1.getOperand(1),
3464 DAG.getConstant(0, N1.getValueType()), Cond);
3465 } else if (N1.getOperand(1) == N0) {
3466 if (isCommutativeBinOp(N1.getOpcode())) {
3467 return DAG.getSetCC(VT, N1.getOperand(0),
3468 DAG.getConstant(0, N1.getValueType()), Cond);
3469 } else {
3470 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
3471 // X == (Z-X) --> X<<1 == Z
3472 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(), N0,
3473 DAG.getConstant(1,TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00003474 AddToWorkList(SH.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003475 return DAG.getSetCC(VT, SH, N1.getOperand(0), Cond);
3476 }
3477 }
3478 }
3479 }
3480
3481 // Fold away ALL boolean setcc's.
3482 SDOperand Temp;
Nate Begemane17daeb2005-10-05 21:43:42 +00003483 if (N0.getValueType() == MVT::i1 && foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00003484 switch (Cond) {
3485 default: assert(0 && "Unknown integer setcc!");
3486 case ISD::SETEQ: // X == Y -> (X^Y)^1
3487 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
3488 N0 = DAG.getNode(ISD::XOR, MVT::i1, Temp, DAG.getConstant(1, MVT::i1));
Chris Lattner5750df92006-03-01 04:03:14 +00003489 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003490 break;
3491 case ISD::SETNE: // X != Y --> (X^Y)
3492 N0 = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
3493 break;
3494 case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> X^1 & Y
3495 case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> X^1 & Y
3496 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
3497 N0 = DAG.getNode(ISD::AND, MVT::i1, N1, Temp);
Chris Lattner5750df92006-03-01 04:03:14 +00003498 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003499 break;
3500 case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> Y^1 & X
3501 case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> Y^1 & X
3502 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
3503 N0 = DAG.getNode(ISD::AND, MVT::i1, N0, Temp);
Chris Lattner5750df92006-03-01 04:03:14 +00003504 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003505 break;
3506 case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> X^1 | Y
3507 case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> X^1 | Y
3508 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
3509 N0 = DAG.getNode(ISD::OR, MVT::i1, N1, Temp);
Chris Lattner5750df92006-03-01 04:03:14 +00003510 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003511 break;
3512 case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> Y^1 | X
3513 case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> Y^1 | X
3514 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
3515 N0 = DAG.getNode(ISD::OR, MVT::i1, N0, Temp);
3516 break;
3517 }
3518 if (VT != MVT::i1) {
Chris Lattner5750df92006-03-01 04:03:14 +00003519 AddToWorkList(N0.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003520 // FIXME: If running after legalize, we probably can't do this.
3521 N0 = DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
3522 }
3523 return N0;
3524 }
3525
3526 // Could not fold it.
3527 return SDOperand();
3528}
3529
Nate Begeman69575232005-10-20 02:15:44 +00003530/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
3531/// return a DAG expression to select that will generate the same value by
3532/// multiplying by a magic number. See:
3533/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
3534SDOperand DAGCombiner::BuildSDIV(SDNode *N) {
3535 MVT::ValueType VT = N->getValueType(0);
Chris Lattnere9936d12005-10-22 18:50:15 +00003536
3537 // Check to see if we can do this.
3538 if (!TLI.isTypeLegal(VT) || (VT != MVT::i32 && VT != MVT::i64))
3539 return SDOperand(); // BuildSDIV only operates on i32 or i64
3540 if (!TLI.isOperationLegal(ISD::MULHS, VT))
3541 return SDOperand(); // Make sure the target supports MULHS.
Nate Begeman69575232005-10-20 02:15:44 +00003542
Nate Begemanc6a454e2005-10-20 17:45:03 +00003543 int64_t d = cast<ConstantSDNode>(N->getOperand(1))->getSignExtended();
Nate Begeman69575232005-10-20 02:15:44 +00003544 ms magics = (VT == MVT::i32) ? magic32(d) : magic64(d);
3545
3546 // Multiply the numerator (operand 0) by the magic value
3547 SDOperand Q = DAG.getNode(ISD::MULHS, VT, N->getOperand(0),
3548 DAG.getConstant(magics.m, VT));
3549 // If d > 0 and m < 0, add the numerator
3550 if (d > 0 && magics.m < 0) {
3551 Q = DAG.getNode(ISD::ADD, VT, Q, N->getOperand(0));
Chris Lattner5750df92006-03-01 04:03:14 +00003552 AddToWorkList(Q.Val);
Nate Begeman69575232005-10-20 02:15:44 +00003553 }
3554 // If d < 0 and m > 0, subtract the numerator.
3555 if (d < 0 && magics.m > 0) {
3556 Q = DAG.getNode(ISD::SUB, VT, Q, N->getOperand(0));
Chris Lattner5750df92006-03-01 04:03:14 +00003557 AddToWorkList(Q.Val);
Nate Begeman69575232005-10-20 02:15:44 +00003558 }
3559 // Shift right algebraic if shift value is nonzero
3560 if (magics.s > 0) {
3561 Q = DAG.getNode(ISD::SRA, VT, Q,
3562 DAG.getConstant(magics.s, TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00003563 AddToWorkList(Q.Val);
Nate Begeman69575232005-10-20 02:15:44 +00003564 }
3565 // Extract the sign bit and add it to the quotient
3566 SDOperand T =
Nate Begeman4d385672005-10-21 01:51:45 +00003567 DAG.getNode(ISD::SRL, VT, Q, DAG.getConstant(MVT::getSizeInBits(VT)-1,
3568 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00003569 AddToWorkList(T.Val);
Nate Begeman69575232005-10-20 02:15:44 +00003570 return DAG.getNode(ISD::ADD, VT, Q, T);
3571}
3572
3573/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
3574/// return a DAG expression to select that will generate the same value by
3575/// multiplying by a magic number. See:
3576/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
3577SDOperand DAGCombiner::BuildUDIV(SDNode *N) {
3578 MVT::ValueType VT = N->getValueType(0);
Chris Lattnere9936d12005-10-22 18:50:15 +00003579
3580 // Check to see if we can do this.
3581 if (!TLI.isTypeLegal(VT) || (VT != MVT::i32 && VT != MVT::i64))
3582 return SDOperand(); // BuildUDIV only operates on i32 or i64
3583 if (!TLI.isOperationLegal(ISD::MULHU, VT))
3584 return SDOperand(); // Make sure the target supports MULHU.
Nate Begeman69575232005-10-20 02:15:44 +00003585
3586 uint64_t d = cast<ConstantSDNode>(N->getOperand(1))->getValue();
3587 mu magics = (VT == MVT::i32) ? magicu32(d) : magicu64(d);
3588
3589 // Multiply the numerator (operand 0) by the magic value
3590 SDOperand Q = DAG.getNode(ISD::MULHU, VT, N->getOperand(0),
3591 DAG.getConstant(magics.m, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00003592 AddToWorkList(Q.Val);
Nate Begeman69575232005-10-20 02:15:44 +00003593
3594 if (magics.a == 0) {
3595 return DAG.getNode(ISD::SRL, VT, Q,
3596 DAG.getConstant(magics.s, TLI.getShiftAmountTy()));
3597 } else {
3598 SDOperand NPQ = DAG.getNode(ISD::SUB, VT, N->getOperand(0), Q);
Chris Lattner5750df92006-03-01 04:03:14 +00003599 AddToWorkList(NPQ.Val);
Nate Begeman69575232005-10-20 02:15:44 +00003600 NPQ = DAG.getNode(ISD::SRL, VT, NPQ,
3601 DAG.getConstant(1, TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00003602 AddToWorkList(NPQ.Val);
Nate Begeman69575232005-10-20 02:15:44 +00003603 NPQ = DAG.getNode(ISD::ADD, VT, NPQ, Q);
Chris Lattner5750df92006-03-01 04:03:14 +00003604 AddToWorkList(NPQ.Val);
Nate Begeman69575232005-10-20 02:15:44 +00003605 return DAG.getNode(ISD::SRL, VT, NPQ,
3606 DAG.getConstant(magics.s-1, TLI.getShiftAmountTy()));
3607 }
3608}
3609
Nate Begeman1d4d4142005-09-01 00:19:25 +00003610// SelectionDAG::Combine - This is the entry point for the file.
3611//
Nate Begeman4ebd8052005-09-01 23:24:04 +00003612void SelectionDAG::Combine(bool RunningAfterLegalize) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003613 /// run - This is the main entry point to this class.
3614 ///
Nate Begeman4ebd8052005-09-01 23:24:04 +00003615 DAGCombiner(*this).Run(RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003616}