blob: aa325b3ba405993c2f8c7eacdde4abd62c68d081 [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 Lattner360e8202006-06-28 21:58:30 +000038#include "llvm/Support/Visibility.h"
Chris Lattnera500fc62005-09-09 23:53:39 +000039#include <algorithm>
Nate Begeman1d4d4142005-09-01 00:19:25 +000040#include <cmath>
Chris Lattner2c2c6c62006-01-22 23:41:00 +000041#include <iostream>
Nate Begeman1d4d4142005-09-01 00:19:25 +000042using namespace llvm;
43
44namespace {
Andrew Lenharthae6153f2006-07-20 17:43:27 +000045 static Statistic<> NodesCombined ("dagcombiner",
46 "Number of dag nodes combined");
Nate Begeman1d4d4142005-09-01 00:19:25 +000047
Chris Lattner360e8202006-06-28 21:58:30 +000048 class VISIBILITY_HIDDEN DAGCombiner {
Nate Begeman1d4d4142005-09-01 00:19:25 +000049 SelectionDAG &DAG;
50 TargetLowering &TLI;
Nate Begeman4ebd8052005-09-01 23:24:04 +000051 bool AfterLegalize;
Nate Begeman1d4d4142005-09-01 00:19:25 +000052
53 // Worklist of all of the nodes that need to be simplified.
54 std::vector<SDNode*> WorkList;
55
56 /// AddUsersToWorkList - When an instruction is simplified, add all users of
57 /// the instruction to the work lists because they might get more simplified
58 /// now.
59 ///
60 void AddUsersToWorkList(SDNode *N) {
61 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
Nate Begeman4ebd8052005-09-01 23:24:04 +000062 UI != UE; ++UI)
63 WorkList.push_back(*UI);
Nate Begeman1d4d4142005-09-01 00:19:25 +000064 }
65
66 /// removeFromWorkList - remove all instances of N from the worklist.
Chris Lattner5750df92006-03-01 04:03:14 +000067 ///
Nate Begeman1d4d4142005-09-01 00:19:25 +000068 void removeFromWorkList(SDNode *N) {
69 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), N),
70 WorkList.end());
71 }
72
Chris Lattner24664722006-03-01 04:53:38 +000073 public:
Chris Lattner5750df92006-03-01 04:03:14 +000074 void AddToWorkList(SDNode *N) {
75 WorkList.push_back(N);
76 }
77
Chris Lattner01a22022005-10-10 22:04:48 +000078 SDOperand CombineTo(SDNode *N, const std::vector<SDOperand> &To) {
Chris Lattner87514ca2005-10-10 22:31:19 +000079 ++NodesCombined;
Chris Lattner01a22022005-10-10 22:04:48 +000080 DEBUG(std::cerr << "\nReplacing "; N->dump();
Evan Cheng60e8c712006-05-09 06:55:15 +000081 std::cerr << "\nWith: "; To[0].Val->dump(&DAG);
Chris Lattner01a22022005-10-10 22:04:48 +000082 std::cerr << " and " << To.size()-1 << " other values\n");
83 std::vector<SDNode*> NowDead;
84 DAG.ReplaceAllUsesWith(N, To, &NowDead);
85
86 // Push the new nodes and any users onto the worklist
87 for (unsigned i = 0, e = To.size(); i != e; ++i) {
88 WorkList.push_back(To[i].Val);
89 AddUsersToWorkList(To[i].Val);
90 }
91
92 // Nodes can end up on the worklist more than once. Make sure we do
93 // not process a node that has been replaced.
94 removeFromWorkList(N);
95 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
96 removeFromWorkList(NowDead[i]);
97
98 // Finally, since the node is now dead, remove it from the graph.
99 DAG.DeleteNode(N);
100 return SDOperand(N, 0);
101 }
Nate Begeman368e18d2006-02-16 21:11:51 +0000102
Chris Lattner24664722006-03-01 04:53:38 +0000103 SDOperand CombineTo(SDNode *N, SDOperand Res) {
104 std::vector<SDOperand> To;
105 To.push_back(Res);
106 return CombineTo(N, To);
107 }
108
109 SDOperand CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1) {
110 std::vector<SDOperand> To;
111 To.push_back(Res0);
112 To.push_back(Res1);
113 return CombineTo(N, To);
114 }
115 private:
116
Chris Lattner012f2412006-02-17 21:58:01 +0000117 /// SimplifyDemandedBits - Check the specified integer node value to see if
Chris Lattnerb2742f42006-03-01 19:55:35 +0000118 /// it can be simplified or if things it uses can be simplified by bit
Chris Lattner012f2412006-02-17 21:58:01 +0000119 /// propagation. If so, return true.
120 bool SimplifyDemandedBits(SDOperand Op) {
Nate Begeman368e18d2006-02-16 21:11:51 +0000121 TargetLowering::TargetLoweringOpt TLO(DAG);
122 uint64_t KnownZero, KnownOne;
Chris Lattner012f2412006-02-17 21:58:01 +0000123 uint64_t Demanded = MVT::getIntVTBitMask(Op.getValueType());
124 if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO))
125 return false;
126
127 // Revisit the node.
128 WorkList.push_back(Op.Val);
129
130 // Replace the old value with the new one.
131 ++NodesCombined;
132 DEBUG(std::cerr << "\nReplacing "; TLO.Old.Val->dump();
Evan Cheng60e8c712006-05-09 06:55:15 +0000133 std::cerr << "\nWith: "; TLO.New.Val->dump(&DAG));
Chris Lattner012f2412006-02-17 21:58:01 +0000134
135 std::vector<SDNode*> NowDead;
136 DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New, NowDead);
137
Chris Lattner7d20d392006-02-20 06:51:04 +0000138 // Push the new node and any (possibly new) users onto the worklist.
Chris Lattner012f2412006-02-17 21:58:01 +0000139 WorkList.push_back(TLO.New.Val);
140 AddUsersToWorkList(TLO.New.Val);
141
142 // Nodes can end up on the worklist more than once. Make sure we do
143 // not process a node that has been replaced.
Chris Lattner012f2412006-02-17 21:58:01 +0000144 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
145 removeFromWorkList(NowDead[i]);
146
Chris Lattner7d20d392006-02-20 06:51:04 +0000147 // Finally, if the node is now dead, remove it from the graph. The node
148 // may not be dead if the replacement process recursively simplified to
149 // something else needing this node.
150 if (TLO.Old.Val->use_empty()) {
151 removeFromWorkList(TLO.Old.Val);
152 DAG.DeleteNode(TLO.Old.Val);
153 }
Chris Lattner012f2412006-02-17 21:58:01 +0000154 return true;
Nate Begeman368e18d2006-02-16 21:11:51 +0000155 }
Chris Lattner87514ca2005-10-10 22:31:19 +0000156
Nate Begeman1d4d4142005-09-01 00:19:25 +0000157 /// visit - call the node-specific routine that knows how to fold each
158 /// particular type of node.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000159 SDOperand visit(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000160
161 // Visitation implementation - Implement dag node combining for different
162 // node types. The semantics are as follows:
163 // Return Value:
Nate Begeman2300f552005-09-07 00:15:36 +0000164 // SDOperand.Val == 0 - No change was made
Chris Lattner01a22022005-10-10 22:04:48 +0000165 // SDOperand.Val == N - N was replaced, is dead, and is already handled.
Nate Begeman2300f552005-09-07 00:15:36 +0000166 // otherwise - N should be replaced by the returned Operand.
Nate Begeman1d4d4142005-09-01 00:19:25 +0000167 //
Nate Begeman83e75ec2005-09-06 04:43:02 +0000168 SDOperand visitTokenFactor(SDNode *N);
169 SDOperand visitADD(SDNode *N);
170 SDOperand visitSUB(SDNode *N);
171 SDOperand visitMUL(SDNode *N);
172 SDOperand visitSDIV(SDNode *N);
173 SDOperand visitUDIV(SDNode *N);
174 SDOperand visitSREM(SDNode *N);
175 SDOperand visitUREM(SDNode *N);
176 SDOperand visitMULHU(SDNode *N);
177 SDOperand visitMULHS(SDNode *N);
178 SDOperand visitAND(SDNode *N);
179 SDOperand visitOR(SDNode *N);
180 SDOperand visitXOR(SDNode *N);
Chris Lattneredab1b92006-04-02 03:25:57 +0000181 SDOperand visitVBinOp(SDNode *N, ISD::NodeType IntOp, ISD::NodeType FPOp);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000182 SDOperand visitSHL(SDNode *N);
183 SDOperand visitSRA(SDNode *N);
184 SDOperand visitSRL(SDNode *N);
185 SDOperand visitCTLZ(SDNode *N);
186 SDOperand visitCTTZ(SDNode *N);
187 SDOperand visitCTPOP(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000188 SDOperand visitSELECT(SDNode *N);
189 SDOperand visitSELECT_CC(SDNode *N);
190 SDOperand visitSETCC(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000191 SDOperand visitSIGN_EXTEND(SDNode *N);
192 SDOperand visitZERO_EXTEND(SDNode *N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000193 SDOperand visitANY_EXTEND(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000194 SDOperand visitSIGN_EXTEND_INREG(SDNode *N);
195 SDOperand visitTRUNCATE(SDNode *N);
Chris Lattner94683772005-12-23 05:30:37 +0000196 SDOperand visitBIT_CONVERT(SDNode *N);
Chris Lattner6258fb22006-04-02 02:53:43 +0000197 SDOperand visitVBIT_CONVERT(SDNode *N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000198 SDOperand visitFADD(SDNode *N);
199 SDOperand visitFSUB(SDNode *N);
200 SDOperand visitFMUL(SDNode *N);
201 SDOperand visitFDIV(SDNode *N);
202 SDOperand visitFREM(SDNode *N);
Chris Lattner12d83032006-03-05 05:30:57 +0000203 SDOperand visitFCOPYSIGN(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000204 SDOperand visitSINT_TO_FP(SDNode *N);
205 SDOperand visitUINT_TO_FP(SDNode *N);
206 SDOperand visitFP_TO_SINT(SDNode *N);
207 SDOperand visitFP_TO_UINT(SDNode *N);
208 SDOperand visitFP_ROUND(SDNode *N);
209 SDOperand visitFP_ROUND_INREG(SDNode *N);
210 SDOperand visitFP_EXTEND(SDNode *N);
211 SDOperand visitFNEG(SDNode *N);
212 SDOperand visitFABS(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000213 SDOperand visitBRCOND(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000214 SDOperand visitBR_CC(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000215 SDOperand visitLOAD(SDNode *N);
Chris Lattner29cd7db2006-03-31 18:10:41 +0000216 SDOperand visitXEXTLOAD(SDNode *N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000217 SDOperand visitSTORE(SDNode *N);
Chris Lattnerca242442006-03-19 01:27:56 +0000218 SDOperand visitINSERT_VECTOR_ELT(SDNode *N);
219 SDOperand visitVINSERT_VECTOR_ELT(SDNode *N);
Chris Lattnerd7648c82006-03-28 20:28:38 +0000220 SDOperand visitVBUILD_VECTOR(SDNode *N);
Chris Lattner66445d32006-03-28 22:11:53 +0000221 SDOperand visitVECTOR_SHUFFLE(SDNode *N);
Chris Lattnerf1d0c622006-03-31 22:16:43 +0000222 SDOperand visitVVECTOR_SHUFFLE(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000223
Evan Cheng44f1f092006-04-20 08:56:16 +0000224 SDOperand XformToShuffleWithZero(SDNode *N);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000225 SDOperand ReassociateOps(unsigned Opc, SDOperand LHS, SDOperand RHS);
226
Chris Lattner40c62d52005-10-18 06:04:22 +0000227 bool SimplifySelectOps(SDNode *SELECT, SDOperand LHS, SDOperand RHS);
Chris Lattner35e5c142006-05-05 05:51:50 +0000228 SDOperand SimplifyBinOpWithSameOpcodeHands(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000229 SDOperand SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2);
230 SDOperand SimplifySelectCC(SDOperand N0, SDOperand N1, SDOperand N2,
231 SDOperand N3, ISD::CondCode CC);
Nate Begeman452d7be2005-09-16 00:54:12 +0000232 SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N0, SDOperand N1,
Nate Begemane17daeb2005-10-05 21:43:42 +0000233 ISD::CondCode Cond, bool foldBooleans = true);
Chris Lattner6258fb22006-04-02 02:53:43 +0000234 SDOperand ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(SDNode *, MVT::ValueType);
Nate Begeman69575232005-10-20 02:15:44 +0000235 SDOperand BuildSDIV(SDNode *N);
236 SDOperand BuildUDIV(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000237public:
238 DAGCombiner(SelectionDAG &D)
Nate Begeman646d7e22005-09-02 21:18:40 +0000239 : DAG(D), TLI(D.getTargetLoweringInfo()), AfterLegalize(false) {}
Nate Begeman1d4d4142005-09-01 00:19:25 +0000240
241 /// Run - runs the dag combiner on all nodes in the work list
Nate Begeman4ebd8052005-09-01 23:24:04 +0000242 void Run(bool RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000243 };
244}
245
Chris Lattner24664722006-03-01 04:53:38 +0000246//===----------------------------------------------------------------------===//
247// TargetLowering::DAGCombinerInfo implementation
248//===----------------------------------------------------------------------===//
249
250void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) {
251 ((DAGCombiner*)DC)->AddToWorkList(N);
252}
253
254SDOperand TargetLowering::DAGCombinerInfo::
255CombineTo(SDNode *N, const std::vector<SDOperand> &To) {
256 return ((DAGCombiner*)DC)->CombineTo(N, To);
257}
258
259SDOperand TargetLowering::DAGCombinerInfo::
260CombineTo(SDNode *N, SDOperand Res) {
261 return ((DAGCombiner*)DC)->CombineTo(N, Res);
262}
263
264
265SDOperand TargetLowering::DAGCombinerInfo::
266CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1) {
267 return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1);
268}
269
270
271
272
273//===----------------------------------------------------------------------===//
274
275
Nate Begeman4ebd8052005-09-01 23:24:04 +0000276// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
277// that selects between the values 1 and 0, making it equivalent to a setcc.
Nate Begeman646d7e22005-09-02 21:18:40 +0000278// Also, set the incoming LHS, RHS, and CC references to the appropriate
279// nodes based on the type of node we are checking. This simplifies life a
280// bit for the callers.
281static bool isSetCCEquivalent(SDOperand N, SDOperand &LHS, SDOperand &RHS,
282 SDOperand &CC) {
283 if (N.getOpcode() == ISD::SETCC) {
284 LHS = N.getOperand(0);
285 RHS = N.getOperand(1);
286 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000287 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000288 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000289 if (N.getOpcode() == ISD::SELECT_CC &&
290 N.getOperand(2).getOpcode() == ISD::Constant &&
291 N.getOperand(3).getOpcode() == ISD::Constant &&
292 cast<ConstantSDNode>(N.getOperand(2))->getValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000293 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
294 LHS = N.getOperand(0);
295 RHS = N.getOperand(1);
296 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000297 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000298 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000299 return false;
300}
301
Nate Begeman99801192005-09-07 23:25:52 +0000302// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
303// one use. If this is true, it allows the users to invert the operation for
304// free when it is profitable to do so.
305static bool isOneUseSetCC(SDOperand N) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000306 SDOperand N0, N1, N2;
Nate Begeman646d7e22005-09-02 21:18:40 +0000307 if (isSetCCEquivalent(N, N0, N1, N2) && N.Val->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000308 return true;
309 return false;
310}
311
Nate Begeman452d7be2005-09-16 00:54:12 +0000312// FIXME: This should probably go in the ISD class rather than being duplicated
313// in several files.
314static bool isCommutativeBinOp(unsigned Opcode) {
315 switch (Opcode) {
316 case ISD::ADD:
317 case ISD::MUL:
318 case ISD::AND:
319 case ISD::OR:
320 case ISD::XOR: return true;
321 default: return false; // FIXME: Need commutative info for user ops!
322 }
323}
324
Nate Begemancd4d58c2006-02-03 06:46:56 +0000325SDOperand DAGCombiner::ReassociateOps(unsigned Opc, SDOperand N0, SDOperand N1){
326 MVT::ValueType VT = N0.getValueType();
327 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
328 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
329 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
330 if (isa<ConstantSDNode>(N1)) {
331 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(1), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000332 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000333 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(0));
334 } else if (N0.hasOneUse()) {
335 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(0), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000336 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000337 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(1));
338 }
339 }
340 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
341 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
342 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
343 if (isa<ConstantSDNode>(N0)) {
344 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(1), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000345 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000346 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(0));
347 } else if (N1.hasOneUse()) {
348 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(0), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000349 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000350 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(1));
351 }
352 }
353 return SDOperand();
354}
355
Nate Begeman4ebd8052005-09-01 23:24:04 +0000356void DAGCombiner::Run(bool RunningAfterLegalize) {
357 // set the instance variable, so that the various visit routines may use it.
358 AfterLegalize = RunningAfterLegalize;
359
Nate Begeman646d7e22005-09-02 21:18:40 +0000360 // Add all the dag nodes to the worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000361 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
362 E = DAG.allnodes_end(); I != E; ++I)
363 WorkList.push_back(I);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000364
Chris Lattner95038592005-10-05 06:35:28 +0000365 // Create a dummy node (which is not added to allnodes), that adds a reference
366 // to the root node, preventing it from being deleted, and tracking any
367 // changes of the root.
368 HandleSDNode Dummy(DAG.getRoot());
369
Chris Lattner24664722006-03-01 04:53:38 +0000370
371 /// DagCombineInfo - Expose the DAG combiner to the target combiner impls.
372 TargetLowering::DAGCombinerInfo
373 DagCombineInfo(DAG, !RunningAfterLegalize, this);
374
Nate Begeman1d4d4142005-09-01 00:19:25 +0000375 // while the worklist isn't empty, inspect the node on the end of it and
376 // try and combine it.
377 while (!WorkList.empty()) {
378 SDNode *N = WorkList.back();
379 WorkList.pop_back();
380
381 // If N has no uses, it is dead. Make sure to revisit all N's operands once
Chris Lattner95038592005-10-05 06:35:28 +0000382 // N is deleted from the DAG, since they too may now be dead or may have a
383 // reduced number of uses, allowing other xforms.
384 if (N->use_empty() && N != &Dummy) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000385 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
386 WorkList.push_back(N->getOperand(i).Val);
387
Nate Begeman1d4d4142005-09-01 00:19:25 +0000388 removeFromWorkList(N);
Chris Lattner95038592005-10-05 06:35:28 +0000389 DAG.DeleteNode(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000390 continue;
391 }
392
Nate Begeman83e75ec2005-09-06 04:43:02 +0000393 SDOperand RV = visit(N);
Chris Lattner24664722006-03-01 04:53:38 +0000394
395 // If nothing happened, try a target-specific DAG combine.
396 if (RV.Val == 0) {
Chris Lattner729c6d12006-05-27 00:43:02 +0000397 assert(N->getOpcode() != ISD::DELETED_NODE &&
398 "Node was deleted but visit returned NULL!");
Chris Lattner24664722006-03-01 04:53:38 +0000399 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
400 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode()))
401 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
402 }
403
Nate Begeman83e75ec2005-09-06 04:43:02 +0000404 if (RV.Val) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000405 ++NodesCombined;
Nate Begeman646d7e22005-09-02 21:18:40 +0000406 // If we get back the same node we passed in, rather than a new node or
407 // zero, we know that the node must have defined multiple values and
408 // CombineTo was used. Since CombineTo takes care of the worklist
409 // mechanics for us, we have no work to do in this case.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000410 if (RV.Val != N) {
Chris Lattner729c6d12006-05-27 00:43:02 +0000411 assert(N->getOpcode() != ISD::DELETED_NODE &&
412 RV.Val->getOpcode() != ISD::DELETED_NODE &&
413 "Node was deleted but visit returned new node!");
414
Nate Begeman2300f552005-09-07 00:15:36 +0000415 DEBUG(std::cerr << "\nReplacing "; N->dump();
Evan Cheng60e8c712006-05-09 06:55:15 +0000416 std::cerr << "\nWith: "; RV.Val->dump(&DAG);
Nate Begeman2300f552005-09-07 00:15:36 +0000417 std::cerr << '\n');
Chris Lattner01a22022005-10-10 22:04:48 +0000418 std::vector<SDNode*> NowDead;
419 DAG.ReplaceAllUsesWith(N, std::vector<SDOperand>(1, RV), &NowDead);
Nate Begeman646d7e22005-09-02 21:18:40 +0000420
421 // Push the new node and any users onto the worklist
Nate Begeman83e75ec2005-09-06 04:43:02 +0000422 WorkList.push_back(RV.Val);
423 AddUsersToWorkList(RV.Val);
Nate Begeman646d7e22005-09-02 21:18:40 +0000424
425 // Nodes can end up on the worklist more than once. Make sure we do
426 // not process a node that has been replaced.
427 removeFromWorkList(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000428 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
429 removeFromWorkList(NowDead[i]);
Chris Lattner5c46f742005-10-05 06:11:08 +0000430
431 // Finally, since the node is now dead, remove it from the graph.
432 DAG.DeleteNode(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000433 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000434 }
435 }
Chris Lattner95038592005-10-05 06:35:28 +0000436
437 // If the root changed (e.g. it was a dead load, update the root).
438 DAG.setRoot(Dummy.getValue());
Nate Begeman1d4d4142005-09-01 00:19:25 +0000439}
440
Nate Begeman83e75ec2005-09-06 04:43:02 +0000441SDOperand DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000442 switch(N->getOpcode()) {
443 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +0000444 case ISD::TokenFactor: return visitTokenFactor(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000445 case ISD::ADD: return visitADD(N);
446 case ISD::SUB: return visitSUB(N);
447 case ISD::MUL: return visitMUL(N);
448 case ISD::SDIV: return visitSDIV(N);
449 case ISD::UDIV: return visitUDIV(N);
450 case ISD::SREM: return visitSREM(N);
451 case ISD::UREM: return visitUREM(N);
452 case ISD::MULHU: return visitMULHU(N);
453 case ISD::MULHS: return visitMULHS(N);
454 case ISD::AND: return visitAND(N);
455 case ISD::OR: return visitOR(N);
456 case ISD::XOR: return visitXOR(N);
457 case ISD::SHL: return visitSHL(N);
458 case ISD::SRA: return visitSRA(N);
459 case ISD::SRL: return visitSRL(N);
460 case ISD::CTLZ: return visitCTLZ(N);
461 case ISD::CTTZ: return visitCTTZ(N);
462 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000463 case ISD::SELECT: return visitSELECT(N);
464 case ISD::SELECT_CC: return visitSELECT_CC(N);
465 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000466 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
467 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000468 case ISD::ANY_EXTEND: return visitANY_EXTEND(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000469 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
470 case ISD::TRUNCATE: return visitTRUNCATE(N);
Chris Lattner94683772005-12-23 05:30:37 +0000471 case ISD::BIT_CONVERT: return visitBIT_CONVERT(N);
Chris Lattner6258fb22006-04-02 02:53:43 +0000472 case ISD::VBIT_CONVERT: return visitVBIT_CONVERT(N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000473 case ISD::FADD: return visitFADD(N);
474 case ISD::FSUB: return visitFSUB(N);
475 case ISD::FMUL: return visitFMUL(N);
476 case ISD::FDIV: return visitFDIV(N);
477 case ISD::FREM: return visitFREM(N);
Chris Lattner12d83032006-03-05 05:30:57 +0000478 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000479 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
480 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
481 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
482 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
483 case ISD::FP_ROUND: return visitFP_ROUND(N);
484 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
485 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
486 case ISD::FNEG: return visitFNEG(N);
487 case ISD::FABS: return visitFABS(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000488 case ISD::BRCOND: return visitBRCOND(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000489 case ISD::BR_CC: return visitBR_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000490 case ISD::LOAD: return visitLOAD(N);
Chris Lattner29cd7db2006-03-31 18:10:41 +0000491 case ISD::EXTLOAD:
492 case ISD::SEXTLOAD:
493 case ISD::ZEXTLOAD: return visitXEXTLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000494 case ISD::STORE: return visitSTORE(N);
Chris Lattnerca242442006-03-19 01:27:56 +0000495 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
496 case ISD::VINSERT_VECTOR_ELT: return visitVINSERT_VECTOR_ELT(N);
Chris Lattnerd7648c82006-03-28 20:28:38 +0000497 case ISD::VBUILD_VECTOR: return visitVBUILD_VECTOR(N);
Chris Lattner66445d32006-03-28 22:11:53 +0000498 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
Chris Lattnerf1d0c622006-03-31 22:16:43 +0000499 case ISD::VVECTOR_SHUFFLE: return visitVVECTOR_SHUFFLE(N);
Chris Lattneredab1b92006-04-02 03:25:57 +0000500 case ISD::VADD: return visitVBinOp(N, ISD::ADD , ISD::FADD);
501 case ISD::VSUB: return visitVBinOp(N, ISD::SUB , ISD::FSUB);
502 case ISD::VMUL: return visitVBinOp(N, ISD::MUL , ISD::FMUL);
503 case ISD::VSDIV: return visitVBinOp(N, ISD::SDIV, ISD::FDIV);
504 case ISD::VUDIV: return visitVBinOp(N, ISD::UDIV, ISD::UDIV);
505 case ISD::VAND: return visitVBinOp(N, ISD::AND , ISD::AND);
506 case ISD::VOR: return visitVBinOp(N, ISD::OR , ISD::OR);
507 case ISD::VXOR: return visitVBinOp(N, ISD::XOR , ISD::XOR);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000508 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000509 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000510}
511
Nate Begeman83e75ec2005-09-06 04:43:02 +0000512SDOperand DAGCombiner::visitTokenFactor(SDNode *N) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000513 SmallVector<SDOperand, 8> Ops;
Nate Begemanded49632005-10-13 03:11:28 +0000514 bool Changed = false;
515
Nate Begeman1d4d4142005-09-01 00:19:25 +0000516 // If the token factor has two operands and one is the entry token, replace
517 // the token factor with the other operand.
518 if (N->getNumOperands() == 2) {
Chris Lattner21a57dc2006-05-12 05:01:37 +0000519 if (N->getOperand(0).getOpcode() == ISD::EntryToken ||
520 N->getOperand(0) == N->getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000521 return N->getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000522 if (N->getOperand(1).getOpcode() == ISD::EntryToken)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000523 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000524 }
Chris Lattner24edbb72005-10-13 22:10:05 +0000525
Nate Begemanded49632005-10-13 03:11:28 +0000526 // fold (tokenfactor (tokenfactor)) -> tokenfactor
527 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
528 SDOperand Op = N->getOperand(i);
529 if (Op.getOpcode() == ISD::TokenFactor && Op.hasOneUse()) {
Chris Lattnerac0f8f22006-03-13 18:37:30 +0000530 AddToWorkList(Op.Val); // Remove dead node.
Nate Begemanded49632005-10-13 03:11:28 +0000531 Changed = true;
532 for (unsigned j = 0, e = Op.getNumOperands(); j != e; ++j)
533 Ops.push_back(Op.getOperand(j));
Chris Lattner21a57dc2006-05-12 05:01:37 +0000534 } else if (i == 0 || N->getOperand(i) != N->getOperand(i-1)) {
Nate Begemanded49632005-10-13 03:11:28 +0000535 Ops.push_back(Op);
Chris Lattner21a57dc2006-05-12 05:01:37 +0000536 } else {
537 // Deleted an operand that was the same as the last one.
538 Changed = true;
Nate Begemanded49632005-10-13 03:11:28 +0000539 }
540 }
541 if (Changed)
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000542 return DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0], Ops.size());
Nate Begeman83e75ec2005-09-06 04:43:02 +0000543 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000544}
545
Nate Begeman83e75ec2005-09-06 04:43:02 +0000546SDOperand DAGCombiner::visitADD(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000547 SDOperand N0 = N->getOperand(0);
548 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000549 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
550 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemanf89d78d2005-09-07 16:09:19 +0000551 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000552
553 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000554 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000555 return DAG.getNode(ISD::ADD, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000556 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000557 if (N0C && !N1C)
558 return DAG.getNode(ISD::ADD, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000559 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000560 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000561 return N0;
Chris Lattner4aafb4f2006-01-12 20:22:43 +0000562 // fold ((c1-A)+c2) -> (c1+c2)-A
563 if (N1C && N0.getOpcode() == ISD::SUB)
564 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
565 return DAG.getNode(ISD::SUB, VT,
566 DAG.getConstant(N1C->getValue()+N0C->getValue(), VT),
567 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000568 // reassociate add
569 SDOperand RADD = ReassociateOps(ISD::ADD, N0, N1);
570 if (RADD.Val != 0)
571 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000572 // fold ((0-A) + B) -> B-A
573 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
574 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000575 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000576 // fold (A + (0-B)) -> A-B
577 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
578 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000579 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +0000580 // fold (A+(B-A)) -> B
581 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000582 return N1.getOperand(0);
Chris Lattner947c2892006-03-13 06:51:27 +0000583
Evan Cheng860771d2006-03-01 01:09:54 +0000584 if (!MVT::isVector(VT) && SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +0000585 return SDOperand(N, 0);
Chris Lattner947c2892006-03-13 06:51:27 +0000586
587 // fold (a+b) -> (a|b) iff a and b share no bits.
588 if (MVT::isInteger(VT) && !MVT::isVector(VT)) {
589 uint64_t LHSZero, LHSOne;
590 uint64_t RHSZero, RHSOne;
591 uint64_t Mask = MVT::getIntVTBitMask(VT);
592 TLI.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
593 if (LHSZero) {
594 TLI.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
595
596 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
597 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
598 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
599 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
600 return DAG.getNode(ISD::OR, VT, N0, N1);
601 }
602 }
603
Nate Begeman83e75ec2005-09-06 04:43:02 +0000604 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000605}
606
Nate Begeman83e75ec2005-09-06 04:43:02 +0000607SDOperand DAGCombiner::visitSUB(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000608 SDOperand N0 = N->getOperand(0);
609 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000610 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
611 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000612 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000613
Chris Lattner854077d2005-10-17 01:07:11 +0000614 // fold (sub x, x) -> 0
615 if (N0 == N1)
616 return DAG.getConstant(0, N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000617 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000618 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000619 return DAG.getNode(ISD::SUB, VT, N0, N1);
Chris Lattner05b57432005-10-11 06:07:15 +0000620 // fold (sub x, c) -> (add x, -c)
621 if (N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000622 return DAG.getNode(ISD::ADD, VT, N0, DAG.getConstant(-N1C->getValue(), VT));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000623 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +0000624 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000625 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000626 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +0000627 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000628 return N0.getOperand(0);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000629 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000630}
631
Nate Begeman83e75ec2005-09-06 04:43:02 +0000632SDOperand DAGCombiner::visitMUL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000633 SDOperand N0 = N->getOperand(0);
634 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000635 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
636 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman223df222005-09-08 20:18:10 +0000637 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000638
639 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000640 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000641 return DAG.getNode(ISD::MUL, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000642 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000643 if (N0C && !N1C)
644 return DAG.getNode(ISD::MUL, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000645 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000646 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000647 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000648 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +0000649 if (N1C && N1C->isAllOnesValue())
Nate Begeman405e3ec2005-10-21 00:02:42 +0000650 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000651 // fold (mul x, (1 << c)) -> x << c
Nate Begeman646d7e22005-09-02 21:18:40 +0000652 if (N1C && isPowerOf2_64(N1C->getValue()))
Chris Lattner3e6099b2005-10-30 06:41:49 +0000653 return DAG.getNode(ISD::SHL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000654 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000655 TLI.getShiftAmountTy()));
Chris Lattner3e6099b2005-10-30 06:41:49 +0000656 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
657 if (N1C && isPowerOf2_64(-N1C->getSignExtended())) {
658 // FIXME: If the input is something that is easily negated (e.g. a
659 // single-use add), we should put the negate there.
660 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT),
661 DAG.getNode(ISD::SHL, VT, N0,
662 DAG.getConstant(Log2_64(-N1C->getSignExtended()),
663 TLI.getShiftAmountTy())));
664 }
Andrew Lenharth50a0d422006-04-02 21:42:45 +0000665
Chris Lattner0b1a85f2006-03-01 03:44:24 +0000666 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
667 if (N1C && N0.getOpcode() == ISD::SHL &&
668 isa<ConstantSDNode>(N0.getOperand(1))) {
669 SDOperand C3 = DAG.getNode(ISD::SHL, VT, N1, N0.getOperand(1));
Chris Lattner5750df92006-03-01 04:03:14 +0000670 AddToWorkList(C3.Val);
Chris Lattner0b1a85f2006-03-01 03:44:24 +0000671 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0), C3);
672 }
673
674 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
675 // use.
676 {
677 SDOperand Sh(0,0), Y(0,0);
678 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
679 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
680 N0.Val->hasOneUse()) {
681 Sh = N0; Y = N1;
682 } else if (N1.getOpcode() == ISD::SHL &&
683 isa<ConstantSDNode>(N1.getOperand(1)) && N1.Val->hasOneUse()) {
684 Sh = N1; Y = N0;
685 }
686 if (Sh.Val) {
687 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Sh.getOperand(0), Y);
688 return DAG.getNode(ISD::SHL, VT, Mul, Sh.getOperand(1));
689 }
690 }
Chris Lattnera1deca32006-03-04 23:33:26 +0000691 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
692 if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() &&
693 isa<ConstantSDNode>(N0.getOperand(1))) {
694 return DAG.getNode(ISD::ADD, VT,
695 DAG.getNode(ISD::MUL, VT, N0.getOperand(0), N1),
696 DAG.getNode(ISD::MUL, VT, N0.getOperand(1), N1));
697 }
Chris Lattner0b1a85f2006-03-01 03:44:24 +0000698
Nate Begemancd4d58c2006-02-03 06:46:56 +0000699 // reassociate mul
700 SDOperand RMUL = ReassociateOps(ISD::MUL, N0, N1);
701 if (RMUL.Val != 0)
702 return RMUL;
Nate Begeman83e75ec2005-09-06 04:43:02 +0000703 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000704}
705
Nate Begeman83e75ec2005-09-06 04:43:02 +0000706SDOperand DAGCombiner::visitSDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000707 SDOperand N0 = N->getOperand(0);
708 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000709 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
710 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000711 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000712
713 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000714 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000715 return DAG.getNode(ISD::SDIV, VT, N0, N1);
Nate Begeman405e3ec2005-10-21 00:02:42 +0000716 // fold (sdiv X, 1) -> X
717 if (N1C && N1C->getSignExtended() == 1LL)
718 return N0;
719 // fold (sdiv X, -1) -> 0-X
720 if (N1C && N1C->isAllOnesValue())
721 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +0000722 // If we know the sign bits of both operands are zero, strength reduce to a
723 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
724 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000725 if (TLI.MaskedValueIsZero(N1, SignBit) &&
726 TLI.MaskedValueIsZero(N0, SignBit))
Chris Lattner094c8fc2005-10-07 06:10:46 +0000727 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
Nate Begemancd6a6ed2006-02-17 07:26:20 +0000728 // fold (sdiv X, pow2) -> simple ops after legalize
Nate Begemanfb7217b2006-02-17 19:54:08 +0000729 if (N1C && N1C->getValue() && !TLI.isIntDivCheap() &&
Nate Begeman405e3ec2005-10-21 00:02:42 +0000730 (isPowerOf2_64(N1C->getSignExtended()) ||
731 isPowerOf2_64(-N1C->getSignExtended()))) {
732 // If dividing by powers of two is cheap, then don't perform the following
733 // fold.
734 if (TLI.isPow2DivCheap())
735 return SDOperand();
736 int64_t pow2 = N1C->getSignExtended();
737 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
Chris Lattner8f4880b2006-02-16 08:02:36 +0000738 unsigned lg2 = Log2_64(abs2);
739 // Splat the sign bit into the register
740 SDOperand SGN = DAG.getNode(ISD::SRA, VT, N0,
Nate Begeman405e3ec2005-10-21 00:02:42 +0000741 DAG.getConstant(MVT::getSizeInBits(VT)-1,
742 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +0000743 AddToWorkList(SGN.Val);
Chris Lattner8f4880b2006-02-16 08:02:36 +0000744 // Add (N0 < 0) ? abs2 - 1 : 0;
745 SDOperand SRL = DAG.getNode(ISD::SRL, VT, SGN,
746 DAG.getConstant(MVT::getSizeInBits(VT)-lg2,
Nate Begeman405e3ec2005-10-21 00:02:42 +0000747 TLI.getShiftAmountTy()));
Chris Lattner8f4880b2006-02-16 08:02:36 +0000748 SDOperand ADD = DAG.getNode(ISD::ADD, VT, N0, SRL);
Chris Lattner5750df92006-03-01 04:03:14 +0000749 AddToWorkList(SRL.Val);
750 AddToWorkList(ADD.Val); // Divide by pow2
Chris Lattner8f4880b2006-02-16 08:02:36 +0000751 SDOperand SRA = DAG.getNode(ISD::SRA, VT, ADD,
752 DAG.getConstant(lg2, TLI.getShiftAmountTy()));
Nate Begeman405e3ec2005-10-21 00:02:42 +0000753 // If we're dividing by a positive value, we're done. Otherwise, we must
754 // negate the result.
755 if (pow2 > 0)
756 return SRA;
Chris Lattner5750df92006-03-01 04:03:14 +0000757 AddToWorkList(SRA.Val);
Nate Begeman405e3ec2005-10-21 00:02:42 +0000758 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), SRA);
759 }
Nate Begeman69575232005-10-20 02:15:44 +0000760 // if integer divide is expensive and we satisfy the requirements, emit an
761 // alternate sequence.
Nate Begeman405e3ec2005-10-21 00:02:42 +0000762 if (N1C && (N1C->getSignExtended() < -1 || N1C->getSignExtended() > 1) &&
Chris Lattnere9936d12005-10-22 18:50:15 +0000763 !TLI.isIntDivCheap()) {
764 SDOperand Op = BuildSDIV(N);
765 if (Op.Val) return Op;
Nate Begeman69575232005-10-20 02:15:44 +0000766 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000767 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000768}
769
Nate Begeman83e75ec2005-09-06 04:43:02 +0000770SDOperand DAGCombiner::visitUDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000771 SDOperand N0 = N->getOperand(0);
772 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000773 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
774 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000775 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000776
777 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000778 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000779 return DAG.getNode(ISD::UDIV, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000780 // fold (udiv x, (1 << c)) -> x >>u c
Nate Begeman646d7e22005-09-02 21:18:40 +0000781 if (N1C && isPowerOf2_64(N1C->getValue()))
Nate Begemanfb5e4bd2006-02-05 07:20:23 +0000782 return DAG.getNode(ISD::SRL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000783 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000784 TLI.getShiftAmountTy()));
Nate Begemanfb5e4bd2006-02-05 07:20:23 +0000785 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
786 if (N1.getOpcode() == ISD::SHL) {
787 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
788 if (isPowerOf2_64(SHC->getValue())) {
789 MVT::ValueType ADDVT = N1.getOperand(1).getValueType();
Nate Begemanc031e332006-02-05 07:36:48 +0000790 SDOperand Add = DAG.getNode(ISD::ADD, ADDVT, N1.getOperand(1),
791 DAG.getConstant(Log2_64(SHC->getValue()),
792 ADDVT));
Chris Lattner5750df92006-03-01 04:03:14 +0000793 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +0000794 return DAG.getNode(ISD::SRL, VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +0000795 }
796 }
797 }
Nate Begeman69575232005-10-20 02:15:44 +0000798 // fold (udiv x, c) -> alternate
Chris Lattnere9936d12005-10-22 18:50:15 +0000799 if (N1C && N1C->getValue() && !TLI.isIntDivCheap()) {
800 SDOperand Op = BuildUDIV(N);
801 if (Op.Val) return Op;
802 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000803 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000804}
805
Nate Begeman83e75ec2005-09-06 04:43:02 +0000806SDOperand DAGCombiner::visitSREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000807 SDOperand N0 = N->getOperand(0);
808 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000809 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
810 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +0000811 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000812
813 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000814 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000815 return DAG.getNode(ISD::SREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +0000816 // If we know the sign bits of both operands are zero, strength reduce to a
817 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
818 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000819 if (TLI.MaskedValueIsZero(N1, SignBit) &&
820 TLI.MaskedValueIsZero(N0, SignBit))
Nate Begemana148d982006-01-18 22:35:16 +0000821 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000822 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000823}
824
Nate Begeman83e75ec2005-09-06 04:43:02 +0000825SDOperand DAGCombiner::visitUREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000826 SDOperand N0 = N->getOperand(0);
827 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000828 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
829 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +0000830 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000831
832 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000833 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000834 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +0000835 // fold (urem x, pow2) -> (and x, pow2-1)
836 if (N1C && !N1C->isNullValue() && isPowerOf2_64(N1C->getValue()))
Nate Begemana148d982006-01-18 22:35:16 +0000837 return DAG.getNode(ISD::AND, VT, N0, DAG.getConstant(N1C->getValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +0000838 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
839 if (N1.getOpcode() == ISD::SHL) {
840 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
841 if (isPowerOf2_64(SHC->getValue())) {
Nate Begemanbab92392006-02-05 08:07:24 +0000842 SDOperand Add = DAG.getNode(ISD::ADD, VT, N1,DAG.getConstant(~0ULL,VT));
Chris Lattner5750df92006-03-01 04:03:14 +0000843 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +0000844 return DAG.getNode(ISD::AND, VT, N0, Add);
845 }
846 }
847 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000848 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000849}
850
Nate Begeman83e75ec2005-09-06 04:43:02 +0000851SDOperand DAGCombiner::visitMULHS(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000852 SDOperand N0 = N->getOperand(0);
853 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000854 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000855
856 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000857 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000858 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000859 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Nate Begeman646d7e22005-09-02 21:18:40 +0000860 if (N1C && N1C->getValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +0000861 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
862 DAG.getConstant(MVT::getSizeInBits(N0.getValueType())-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +0000863 TLI.getShiftAmountTy()));
864 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000865}
866
Nate Begeman83e75ec2005-09-06 04:43:02 +0000867SDOperand DAGCombiner::visitMULHU(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000868 SDOperand N0 = N->getOperand(0);
869 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000870 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000871
872 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000873 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000874 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000875 // fold (mulhu x, 1) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000876 if (N1C && N1C->getValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000877 return DAG.getConstant(0, N0.getValueType());
878 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000879}
880
Chris Lattner35e5c142006-05-05 05:51:50 +0000881/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
882/// two operands of the same opcode, try to simplify it.
883SDOperand DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
884 SDOperand N0 = N->getOperand(0), N1 = N->getOperand(1);
885 MVT::ValueType VT = N0.getValueType();
886 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
887
Chris Lattner540121f2006-05-05 06:31:05 +0000888 // For each of OP in AND/OR/XOR:
889 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
890 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
891 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
Chris Lattner0d8dae72006-05-05 06:32:04 +0000892 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y))
Chris Lattner540121f2006-05-05 06:31:05 +0000893 if ((N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND||
Chris Lattner0d8dae72006-05-05 06:32:04 +0000894 N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::TRUNCATE) &&
Chris Lattner35e5c142006-05-05 05:51:50 +0000895 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
896 SDOperand ORNode = DAG.getNode(N->getOpcode(),
897 N0.getOperand(0).getValueType(),
898 N0.getOperand(0), N1.getOperand(0));
899 AddToWorkList(ORNode.Val);
Chris Lattner540121f2006-05-05 06:31:05 +0000900 return DAG.getNode(N0.getOpcode(), VT, ORNode);
Chris Lattner35e5c142006-05-05 05:51:50 +0000901 }
902
Chris Lattnera3dc3f62006-05-05 06:10:43 +0000903 // For each of OP in SHL/SRL/SRA/AND...
904 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
905 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
906 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
Chris Lattner35e5c142006-05-05 05:51:50 +0000907 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
Chris Lattnera3dc3f62006-05-05 06:10:43 +0000908 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
Chris Lattner35e5c142006-05-05 05:51:50 +0000909 N0.getOperand(1) == N1.getOperand(1)) {
910 SDOperand ORNode = DAG.getNode(N->getOpcode(),
911 N0.getOperand(0).getValueType(),
912 N0.getOperand(0), N1.getOperand(0));
913 AddToWorkList(ORNode.Val);
914 return DAG.getNode(N0.getOpcode(), VT, ORNode, N0.getOperand(1));
915 }
916
917 return SDOperand();
918}
919
Nate Begeman83e75ec2005-09-06 04:43:02 +0000920SDOperand DAGCombiner::visitAND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000921 SDOperand N0 = N->getOperand(0);
922 SDOperand N1 = N->getOperand(1);
Nate Begemanfb7217b2006-02-17 19:54:08 +0000923 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +0000924 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
925 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000926 MVT::ValueType VT = N1.getValueType();
Nate Begeman83e75ec2005-09-06 04:43:02 +0000927 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000928
929 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000930 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000931 return DAG.getNode(ISD::AND, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000932 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000933 if (N0C && !N1C)
934 return DAG.getNode(ISD::AND, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000935 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000936 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000937 return N0;
938 // if (and x, c) is known to be zero, return 0
Nate Begeman368e18d2006-02-16 21:11:51 +0000939 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000940 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000941 // reassociate and
942 SDOperand RAND = ReassociateOps(ISD::AND, N0, N1);
943 if (RAND.Val != 0)
944 return RAND;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000945 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
Nate Begeman5dc7e862005-11-02 18:42:59 +0000946 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +0000947 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Nate Begeman646d7e22005-09-02 21:18:40 +0000948 if ((ORI->getValue() & N1C->getValue()) == N1C->getValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000949 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +0000950 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
951 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Chris Lattner1ec05d12006-03-01 21:47:21 +0000952 unsigned InMask = MVT::getIntVTBitMask(N0.getOperand(0).getValueType());
Chris Lattner3603cd62006-02-02 07:17:31 +0000953 if (TLI.MaskedValueIsZero(N0.getOperand(0),
Chris Lattner1ec05d12006-03-01 21:47:21 +0000954 ~N1C->getValue() & InMask)) {
955 SDOperand Zext = DAG.getNode(ISD::ZERO_EXTEND, N0.getValueType(),
956 N0.getOperand(0));
957
958 // Replace uses of the AND with uses of the Zero extend node.
959 CombineTo(N, Zext);
960
Chris Lattner3603cd62006-02-02 07:17:31 +0000961 // We actually want to replace all uses of the any_extend with the
962 // zero_extend, to avoid duplicating things. This will later cause this
963 // AND to be folded.
Chris Lattner1ec05d12006-03-01 21:47:21 +0000964 CombineTo(N0.Val, Zext);
Chris Lattnerfedced72006-04-20 23:55:59 +0000965 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner3603cd62006-02-02 07:17:31 +0000966 }
967 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000968 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
969 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
970 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
971 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
972
973 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
974 MVT::isInteger(LL.getValueType())) {
975 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
976 if (cast<ConstantSDNode>(LR)->getValue() == 0 && Op1 == ISD::SETEQ) {
977 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +0000978 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000979 return DAG.getSetCC(VT, ORNode, LR, Op1);
980 }
981 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
982 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
983 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +0000984 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000985 return DAG.getSetCC(VT, ANDNode, LR, Op1);
986 }
987 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
988 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
989 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +0000990 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000991 return DAG.getSetCC(VT, ORNode, LR, Op1);
992 }
993 }
994 // canonicalize equivalent to ll == rl
995 if (LL == RR && LR == RL) {
996 Op1 = ISD::getSetCCSwappedOperands(Op1);
997 std::swap(RL, RR);
998 }
999 if (LL == RL && LR == RR) {
1000 bool isInteger = MVT::isInteger(LL.getValueType());
1001 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
1002 if (Result != ISD::SETCC_INVALID)
1003 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1004 }
1005 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001006
1007 // Simplify: and (op x...), (op y...) -> (op (and x, y))
1008 if (N0.getOpcode() == N1.getOpcode()) {
1009 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1010 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001011 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001012
Nate Begemande996292006-02-03 22:24:05 +00001013 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
1014 // fold (and (sra)) -> (and (srl)) when possible.
Chris Lattner6ea2dee2006-03-25 22:19:00 +00001015 if (!MVT::isVector(VT) &&
1016 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001017 return SDOperand(N, 0);
Nate Begemanded49632005-10-13 03:11:28 +00001018 // fold (zext_inreg (extload x)) -> (zextload x)
Nate Begeman5054f162005-10-14 01:12:21 +00001019 if (N0.getOpcode() == ISD::EXTLOAD) {
Nate Begemanded49632005-10-13 03:11:28 +00001020 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001021 // If we zero all the possible extended bits, then we can turn this into
1022 // a zextload if we are running before legalize or the operation is legal.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001023 if (TLI.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Chris Lattner67a44cd2005-10-13 18:16:34 +00001024 (!AfterLegalize || TLI.isOperationLegal(ISD::ZEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001025 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1026 N0.getOperand(1), N0.getOperand(2),
1027 EVT);
Chris Lattner5750df92006-03-01 04:03:14 +00001028 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001029 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001030 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001031 }
1032 }
1033 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Chris Lattner40c62d52005-10-18 06:04:22 +00001034 if (N0.getOpcode() == ISD::SEXTLOAD && N0.hasOneUse()) {
Nate Begemanded49632005-10-13 03:11:28 +00001035 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001036 // If we zero all the possible extended bits, then we can turn this into
1037 // a zextload if we are running before legalize or the operation is legal.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001038 if (TLI.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Nate Begemanbfd65a02005-10-13 18:34:58 +00001039 (!AfterLegalize || TLI.isOperationLegal(ISD::ZEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001040 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1041 N0.getOperand(1), N0.getOperand(2),
1042 EVT);
Chris Lattner5750df92006-03-01 04:03:14 +00001043 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001044 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001045 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001046 }
1047 }
Chris Lattner15045b62006-02-28 06:35:35 +00001048
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001049 // fold (and (load x), 255) -> (zextload x, i8)
1050 // fold (and (extload x, i16), 255) -> (zextload x, i8)
1051 if (N1C &&
1052 (N0.getOpcode() == ISD::LOAD || N0.getOpcode() == ISD::EXTLOAD ||
1053 N0.getOpcode() == ISD::ZEXTLOAD) &&
1054 N0.hasOneUse()) {
1055 MVT::ValueType EVT, LoadedVT;
Chris Lattner15045b62006-02-28 06:35:35 +00001056 if (N1C->getValue() == 255)
1057 EVT = MVT::i8;
1058 else if (N1C->getValue() == 65535)
1059 EVT = MVT::i16;
1060 else if (N1C->getValue() == ~0U)
1061 EVT = MVT::i32;
1062 else
1063 EVT = MVT::Other;
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001064
1065 LoadedVT = N0.getOpcode() == ISD::LOAD ? VT :
1066 cast<VTSDNode>(N0.getOperand(3))->getVT();
Chris Lattnere44be602006-04-04 17:39:18 +00001067 if (EVT != MVT::Other && LoadedVT > EVT &&
1068 (!AfterLegalize || TLI.isOperationLegal(ISD::ZEXTLOAD, EVT))) {
Chris Lattner15045b62006-02-28 06:35:35 +00001069 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
1070 // For big endian targets, we need to add an offset to the pointer to load
1071 // the correct bytes. For little endian systems, we merely need to read
1072 // fewer bytes from the same pointer.
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001073 unsigned PtrOff =
1074 (MVT::getSizeInBits(LoadedVT) - MVT::getSizeInBits(EVT)) / 8;
1075 SDOperand NewPtr = N0.getOperand(1);
1076 if (!TLI.isLittleEndian())
1077 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
1078 DAG.getConstant(PtrOff, PtrType));
Chris Lattner5750df92006-03-01 04:03:14 +00001079 AddToWorkList(NewPtr.Val);
Chris Lattner15045b62006-02-28 06:35:35 +00001080 SDOperand Load =
1081 DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0), NewPtr,
1082 N0.getOperand(2), EVT);
Chris Lattner5750df92006-03-01 04:03:14 +00001083 AddToWorkList(N);
Chris Lattner15045b62006-02-28 06:35:35 +00001084 CombineTo(N0.Val, Load, Load.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001085 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner15045b62006-02-28 06:35:35 +00001086 }
1087 }
1088
Nate Begeman83e75ec2005-09-06 04:43:02 +00001089 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001090}
1091
Nate Begeman83e75ec2005-09-06 04:43:02 +00001092SDOperand DAGCombiner::visitOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001093 SDOperand N0 = N->getOperand(0);
1094 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001095 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001096 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1097 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001098 MVT::ValueType VT = N1.getValueType();
1099 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001100
1101 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001102 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001103 return DAG.getNode(ISD::OR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001104 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001105 if (N0C && !N1C)
1106 return DAG.getNode(ISD::OR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001107 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001108 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001109 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001110 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001111 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001112 return N1;
1113 // fold (or x, c) -> c iff (x & ~c) == 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001114 if (N1C &&
1115 TLI.MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits))))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001116 return N1;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001117 // reassociate or
1118 SDOperand ROR = ReassociateOps(ISD::OR, N0, N1);
1119 if (ROR.Val != 0)
1120 return ROR;
1121 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
1122 if (N1C && N0.getOpcode() == ISD::AND && N0.Val->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00001123 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00001124 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
1125 return DAG.getNode(ISD::AND, VT, DAG.getNode(ISD::OR, VT, N0.getOperand(0),
1126 N1),
1127 DAG.getConstant(N1C->getValue() | C1->getValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00001128 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001129 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
1130 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1131 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1132 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1133
1134 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1135 MVT::isInteger(LL.getValueType())) {
1136 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
1137 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
1138 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
1139 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
1140 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001141 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001142 return DAG.getSetCC(VT, ORNode, LR, Op1);
1143 }
1144 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
1145 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
1146 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
1147 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
1148 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001149 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001150 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1151 }
1152 }
1153 // canonicalize equivalent to ll == rl
1154 if (LL == RR && LR == RL) {
1155 Op1 = ISD::getSetCCSwappedOperands(Op1);
1156 std::swap(RL, RR);
1157 }
1158 if (LL == RL && LR == RR) {
1159 bool isInteger = MVT::isInteger(LL.getValueType());
1160 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
1161 if (Result != ISD::SETCC_INVALID)
1162 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1163 }
1164 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001165
1166 // Simplify: or (op x...), (op y...) -> (op (or x, y))
1167 if (N0.getOpcode() == N1.getOpcode()) {
1168 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1169 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001170 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001171
Nate Begeman35ef9132006-01-11 21:21:00 +00001172 // canonicalize shl to left side in a shl/srl pair, to match rotate
1173 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
1174 std::swap(N0, N1);
1175 // check for rotl, rotr
1176 if (N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SRL &&
1177 N0.getOperand(0) == N1.getOperand(0) &&
Chris Lattneraf551bc2006-01-12 18:57:33 +00001178 TLI.isOperationLegal(ISD::ROTL, VT) && TLI.isTypeLegal(VT)) {
Nate Begeman35ef9132006-01-11 21:21:00 +00001179 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
1180 if (N0.getOperand(1).getOpcode() == ISD::Constant &&
1181 N1.getOperand(1).getOpcode() == ISD::Constant) {
1182 uint64_t c1val = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1183 uint64_t c2val = cast<ConstantSDNode>(N1.getOperand(1))->getValue();
1184 if ((c1val + c2val) == OpSizeInBits)
1185 return DAG.getNode(ISD::ROTL, VT, N0.getOperand(0), N0.getOperand(1));
1186 }
1187 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
1188 if (N1.getOperand(1).getOpcode() == ISD::SUB &&
1189 N0.getOperand(1) == N1.getOperand(1).getOperand(1))
1190 if (ConstantSDNode *SUBC =
1191 dyn_cast<ConstantSDNode>(N1.getOperand(1).getOperand(0)))
1192 if (SUBC->getValue() == OpSizeInBits)
1193 return DAG.getNode(ISD::ROTL, VT, N0.getOperand(0), N0.getOperand(1));
1194 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
1195 if (N0.getOperand(1).getOpcode() == ISD::SUB &&
1196 N1.getOperand(1) == N0.getOperand(1).getOperand(1))
1197 if (ConstantSDNode *SUBC =
1198 dyn_cast<ConstantSDNode>(N0.getOperand(1).getOperand(0)))
1199 if (SUBC->getValue() == OpSizeInBits) {
Chris Lattneraf551bc2006-01-12 18:57:33 +00001200 if (TLI.isOperationLegal(ISD::ROTR, VT) && TLI.isTypeLegal(VT))
Nate Begeman35ef9132006-01-11 21:21:00 +00001201 return DAG.getNode(ISD::ROTR, VT, N0.getOperand(0),
1202 N1.getOperand(1));
1203 else
1204 return DAG.getNode(ISD::ROTL, VT, N0.getOperand(0),
1205 N0.getOperand(1));
1206 }
1207 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001208 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001209}
1210
Nate Begeman83e75ec2005-09-06 04:43:02 +00001211SDOperand DAGCombiner::visitXOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001212 SDOperand N0 = N->getOperand(0);
1213 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001214 SDOperand LHS, RHS, CC;
1215 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1216 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001217 MVT::ValueType VT = N0.getValueType();
1218
1219 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001220 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001221 return DAG.getNode(ISD::XOR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001222 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001223 if (N0C && !N1C)
1224 return DAG.getNode(ISD::XOR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001225 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001226 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001227 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001228 // reassociate xor
1229 SDOperand RXOR = ReassociateOps(ISD::XOR, N0, N1);
1230 if (RXOR.Val != 0)
1231 return RXOR;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001232 // fold !(x cc y) -> (x !cc y)
Nate Begeman646d7e22005-09-02 21:18:40 +00001233 if (N1C && N1C->getValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
1234 bool isInt = MVT::isInteger(LHS.getValueType());
1235 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
1236 isInt);
1237 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001238 return DAG.getSetCC(VT, LHS, RHS, NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00001239 if (N0.getOpcode() == ISD::SELECT_CC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001240 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00001241 assert(0 && "Unhandled SetCC Equivalent!");
1242 abort();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001243 }
Nate Begeman99801192005-09-07 23:25:52 +00001244 // fold !(x or y) -> (!x and !y) iff x or y are setcc
1245 if (N1C && N1C->getValue() == 1 &&
1246 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001247 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00001248 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
1249 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001250 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1251 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00001252 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00001253 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001254 }
1255 }
Nate Begeman99801192005-09-07 23:25:52 +00001256 // fold !(x or y) -> (!x and !y) iff x or y are constants
1257 if (N1C && N1C->isAllOnesValue() &&
1258 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001259 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00001260 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
1261 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001262 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1263 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00001264 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00001265 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001266 }
1267 }
Nate Begeman223df222005-09-08 20:18:10 +00001268 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
1269 if (N1C && N0.getOpcode() == ISD::XOR) {
1270 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
1271 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
1272 if (N00C)
1273 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
1274 DAG.getConstant(N1C->getValue()^N00C->getValue(), VT));
1275 if (N01C)
1276 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
1277 DAG.getConstant(N1C->getValue()^N01C->getValue(), VT));
1278 }
1279 // fold (xor x, x) -> 0
Chris Lattner4fbdd592006-03-28 19:11:05 +00001280 if (N0 == N1) {
1281 if (!MVT::isVector(VT)) {
1282 return DAG.getConstant(0, VT);
1283 } else if (!AfterLegalize || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) {
1284 // Produce a vector of zeros.
1285 SDOperand El = DAG.getConstant(0, MVT::getVectorBaseType(VT));
1286 std::vector<SDOperand> Ops(MVT::getVectorNumElements(VT), El);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001287 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4fbdd592006-03-28 19:11:05 +00001288 }
1289 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001290
1291 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
1292 if (N0.getOpcode() == N1.getOpcode()) {
1293 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1294 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001295 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001296
Chris Lattner3e104b12006-04-08 04:15:24 +00001297 // Simplify the expression using non-local knowledge.
1298 if (!MVT::isVector(VT) &&
1299 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001300 return SDOperand(N, 0);
Chris Lattner3e104b12006-04-08 04:15:24 +00001301
Nate Begeman83e75ec2005-09-06 04:43:02 +00001302 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001303}
1304
Nate Begeman83e75ec2005-09-06 04:43:02 +00001305SDOperand DAGCombiner::visitSHL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001306 SDOperand N0 = N->getOperand(0);
1307 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001308 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1309 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001310 MVT::ValueType VT = N0.getValueType();
1311 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1312
1313 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001314 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001315 return DAG.getNode(ISD::SHL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001316 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001317 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001318 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001319 // fold (shl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001320 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001321 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001322 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001323 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001324 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001325 // if (shl x, c) is known to be zero, return 0
Nate Begemanfb7217b2006-02-17 19:54:08 +00001326 if (TLI.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001327 return DAG.getConstant(0, VT);
Chris Lattner012f2412006-02-17 21:58:01 +00001328 if (SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001329 return SDOperand(N, 0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001330 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001331 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001332 N0.getOperand(1).getOpcode() == ISD::Constant) {
1333 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001334 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001335 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001336 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001337 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001338 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001339 }
1340 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
1341 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001342 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001343 N0.getOperand(1).getOpcode() == ISD::Constant) {
1344 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001345 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001346 SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
1347 DAG.getConstant(~0ULL << c1, VT));
1348 if (c2 > c1)
1349 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001350 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001351 else
Nate Begeman83e75ec2005-09-06 04:43:02 +00001352 return DAG.getNode(ISD::SRL, VT, Mask,
1353 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001354 }
1355 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001356 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +00001357 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001358 DAG.getConstant(~0ULL << N1C->getValue(), VT));
Chris Lattnercac70592006-03-05 19:53:55 +00001359 // fold (shl (add x, c1), c2) -> (add (shl x, c2), c1<<c2)
1360 if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() &&
1361 isa<ConstantSDNode>(N0.getOperand(1))) {
1362 return DAG.getNode(ISD::ADD, VT,
1363 DAG.getNode(ISD::SHL, VT, N0.getOperand(0), N1),
1364 DAG.getNode(ISD::SHL, VT, N0.getOperand(1), N1));
1365 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001366 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001367}
1368
Nate Begeman83e75ec2005-09-06 04:43:02 +00001369SDOperand DAGCombiner::visitSRA(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001370 SDOperand N0 = N->getOperand(0);
1371 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001372 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1373 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001374 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001375
1376 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001377 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001378 return DAG.getNode(ISD::SRA, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001379 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001380 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001381 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001382 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001383 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001384 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001385 // fold (sra x, c >= size(x)) -> undef
Nate Begemanfb7217b2006-02-17 19:54:08 +00001386 if (N1C && N1C->getValue() >= MVT::getSizeInBits(VT))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001387 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001388 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001389 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001390 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00001391 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
1392 // sext_inreg.
1393 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
1394 unsigned LowBits = MVT::getSizeInBits(VT) - (unsigned)N1C->getValue();
1395 MVT::ValueType EVT;
1396 switch (LowBits) {
1397 default: EVT = MVT::Other; break;
1398 case 1: EVT = MVT::i1; break;
1399 case 8: EVT = MVT::i8; break;
1400 case 16: EVT = MVT::i16; break;
1401 case 32: EVT = MVT::i32; break;
1402 }
1403 if (EVT > MVT::Other && TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, EVT))
1404 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
1405 DAG.getValueType(EVT));
1406 }
Chris Lattner71d9ebc2006-02-28 06:23:04 +00001407
1408 // fold (sra (sra x, c1), c2) -> (sra x, c1+c2)
1409 if (N1C && N0.getOpcode() == ISD::SRA) {
1410 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
1411 unsigned Sum = N1C->getValue() + C1->getValue();
1412 if (Sum >= MVT::getSizeInBits(VT)) Sum = MVT::getSizeInBits(VT)-1;
1413 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0),
1414 DAG.getConstant(Sum, N1C->getValueType(0)));
1415 }
1416 }
1417
Chris Lattnera8504462006-05-08 20:51:54 +00001418 // Simplify, based on bits shifted out of the LHS.
1419 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
1420 return SDOperand(N, 0);
1421
1422
Nate Begeman1d4d4142005-09-01 00:19:25 +00001423 // If the sign bit is known to be zero, switch this to a SRL.
Nate Begemanfb7217b2006-02-17 19:54:08 +00001424 if (TLI.MaskedValueIsZero(N0, MVT::getIntVTSignBit(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001425 return DAG.getNode(ISD::SRL, VT, N0, N1);
1426 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001427}
1428
Nate Begeman83e75ec2005-09-06 04:43:02 +00001429SDOperand DAGCombiner::visitSRL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001430 SDOperand N0 = N->getOperand(0);
1431 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001432 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1433 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001434 MVT::ValueType VT = N0.getValueType();
1435 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1436
1437 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001438 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001439 return DAG.getNode(ISD::SRL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001440 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001441 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001442 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001443 // fold (srl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001444 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001445 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001446 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001447 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001448 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001449 // if (srl x, c) is known to be zero, return 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001450 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001451 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001452 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001453 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001454 N0.getOperand(1).getOpcode() == ISD::Constant) {
1455 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001456 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001457 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001458 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001459 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001460 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001461 }
Chris Lattner350bec02006-04-02 06:11:11 +00001462
Chris Lattner06afe072006-05-05 22:53:17 +00001463 // fold (srl (anyextend x), c) -> (anyextend (srl x, c))
1464 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
1465 // Shifting in all undef bits?
1466 MVT::ValueType SmallVT = N0.getOperand(0).getValueType();
1467 if (N1C->getValue() >= MVT::getSizeInBits(SmallVT))
1468 return DAG.getNode(ISD::UNDEF, VT);
1469
1470 SDOperand SmallShift = DAG.getNode(ISD::SRL, SmallVT, N0.getOperand(0), N1);
1471 AddToWorkList(SmallShift.Val);
1472 return DAG.getNode(ISD::ANY_EXTEND, VT, SmallShift);
1473 }
1474
Chris Lattner350bec02006-04-02 06:11:11 +00001475 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
1476 if (N1C && N0.getOpcode() == ISD::CTLZ &&
1477 N1C->getValue() == Log2_32(MVT::getSizeInBits(VT))) {
1478 uint64_t KnownZero, KnownOne, Mask = MVT::getIntVTBitMask(VT);
1479 TLI.ComputeMaskedBits(N0.getOperand(0), Mask, KnownZero, KnownOne);
1480
1481 // If any of the input bits are KnownOne, then the input couldn't be all
1482 // zeros, thus the result of the srl will always be zero.
1483 if (KnownOne) return DAG.getConstant(0, VT);
1484
1485 // If all of the bits input the to ctlz node are known to be zero, then
1486 // the result of the ctlz is "32" and the result of the shift is one.
1487 uint64_t UnknownBits = ~KnownZero & Mask;
1488 if (UnknownBits == 0) return DAG.getConstant(1, VT);
1489
1490 // Otherwise, check to see if there is exactly one bit input to the ctlz.
1491 if ((UnknownBits & (UnknownBits-1)) == 0) {
1492 // Okay, we know that only that the single bit specified by UnknownBits
1493 // could be set on input to the CTLZ node. If this bit is set, the SRL
1494 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
1495 // to an SRL,XOR pair, which is likely to simplify more.
1496 unsigned ShAmt = CountTrailingZeros_64(UnknownBits);
1497 SDOperand Op = N0.getOperand(0);
1498 if (ShAmt) {
1499 Op = DAG.getNode(ISD::SRL, VT, Op,
1500 DAG.getConstant(ShAmt, TLI.getShiftAmountTy()));
1501 AddToWorkList(Op.Val);
1502 }
1503 return DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(1, VT));
1504 }
1505 }
1506
Nate Begeman83e75ec2005-09-06 04:43:02 +00001507 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001508}
1509
Nate Begeman83e75ec2005-09-06 04:43:02 +00001510SDOperand DAGCombiner::visitCTLZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001511 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00001512 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001513
1514 // fold (ctlz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00001515 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00001516 return DAG.getNode(ISD::CTLZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001517 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001518}
1519
Nate Begeman83e75ec2005-09-06 04:43:02 +00001520SDOperand DAGCombiner::visitCTTZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001521 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00001522 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001523
1524 // fold (cttz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00001525 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00001526 return DAG.getNode(ISD::CTTZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001527 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001528}
1529
Nate Begeman83e75ec2005-09-06 04:43:02 +00001530SDOperand DAGCombiner::visitCTPOP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001531 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00001532 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001533
1534 // fold (ctpop c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00001535 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00001536 return DAG.getNode(ISD::CTPOP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001537 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001538}
1539
Nate Begeman452d7be2005-09-16 00:54:12 +00001540SDOperand DAGCombiner::visitSELECT(SDNode *N) {
1541 SDOperand N0 = N->getOperand(0);
1542 SDOperand N1 = N->getOperand(1);
1543 SDOperand N2 = N->getOperand(2);
1544 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1545 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1546 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1547 MVT::ValueType VT = N->getValueType(0);
Nate Begeman44728a72005-09-19 22:34:01 +00001548
Nate Begeman452d7be2005-09-16 00:54:12 +00001549 // fold select C, X, X -> X
1550 if (N1 == N2)
1551 return N1;
1552 // fold select true, X, Y -> X
1553 if (N0C && !N0C->isNullValue())
1554 return N1;
1555 // fold select false, X, Y -> Y
1556 if (N0C && N0C->isNullValue())
1557 return N2;
1558 // fold select C, 1, X -> C | X
Nate Begeman44728a72005-09-19 22:34:01 +00001559 if (MVT::i1 == VT && N1C && N1C->getValue() == 1)
Nate Begeman452d7be2005-09-16 00:54:12 +00001560 return DAG.getNode(ISD::OR, VT, N0, N2);
1561 // fold select C, 0, X -> ~C & X
1562 // FIXME: this should check for C type == X type, not i1?
1563 if (MVT::i1 == VT && N1C && N1C->isNullValue()) {
1564 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00001565 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00001566 return DAG.getNode(ISD::AND, VT, XORNode, N2);
1567 }
1568 // fold select C, X, 1 -> ~C | X
Nate Begeman44728a72005-09-19 22:34:01 +00001569 if (MVT::i1 == VT && N2C && N2C->getValue() == 1) {
Nate Begeman452d7be2005-09-16 00:54:12 +00001570 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00001571 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00001572 return DAG.getNode(ISD::OR, VT, XORNode, N1);
1573 }
1574 // fold select C, X, 0 -> C & X
1575 // FIXME: this should check for C type == X type, not i1?
1576 if (MVT::i1 == VT && N2C && N2C->isNullValue())
1577 return DAG.getNode(ISD::AND, VT, N0, N1);
1578 // fold X ? X : Y --> X ? 1 : Y --> X | Y
1579 if (MVT::i1 == VT && N0 == N1)
1580 return DAG.getNode(ISD::OR, VT, N0, N2);
1581 // fold X ? Y : X --> X ? Y : 0 --> X & Y
1582 if (MVT::i1 == VT && N0 == N2)
1583 return DAG.getNode(ISD::AND, VT, N0, N1);
Chris Lattner729c6d12006-05-27 00:43:02 +00001584
Chris Lattner40c62d52005-10-18 06:04:22 +00001585 // If we can fold this based on the true/false value, do so.
1586 if (SimplifySelectOps(N, N1, N2))
Chris Lattner729c6d12006-05-27 00:43:02 +00001587 return SDOperand(N, 0); // Don't revisit N.
1588
Nate Begeman44728a72005-09-19 22:34:01 +00001589 // fold selects based on a setcc into other things, such as min/max/abs
1590 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman750ac1b2006-02-01 07:19:44 +00001591 // FIXME:
1592 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
1593 // having to say they don't support SELECT_CC on every type the DAG knows
1594 // about, since there is no way to mark an opcode illegal at all value types
1595 if (TLI.isOperationLegal(ISD::SELECT_CC, MVT::Other))
1596 return DAG.getNode(ISD::SELECT_CC, VT, N0.getOperand(0), N0.getOperand(1),
1597 N1, N2, N0.getOperand(2));
1598 else
1599 return SimplifySelect(N0, N1, N2);
Nate Begeman452d7be2005-09-16 00:54:12 +00001600 return SDOperand();
1601}
1602
1603SDOperand DAGCombiner::visitSELECT_CC(SDNode *N) {
Nate Begeman44728a72005-09-19 22:34:01 +00001604 SDOperand N0 = N->getOperand(0);
1605 SDOperand N1 = N->getOperand(1);
1606 SDOperand N2 = N->getOperand(2);
1607 SDOperand N3 = N->getOperand(3);
1608 SDOperand N4 = N->getOperand(4);
1609 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1610 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1611 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1612 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
1613
1614 // Determine if the condition we're dealing with is constant
Nate Begemane17daeb2005-10-05 21:43:42 +00001615 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
Chris Lattner5eed34d2006-05-12 17:57:54 +00001616 //ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
Chris Lattner91559022005-10-05 04:45:43 +00001617
Nate Begeman44728a72005-09-19 22:34:01 +00001618 // fold select_cc lhs, rhs, x, x, cc -> x
1619 if (N2 == N3)
1620 return N2;
Chris Lattner40c62d52005-10-18 06:04:22 +00001621
1622 // If we can fold this based on the true/false value, do so.
1623 if (SimplifySelectOps(N, N2, N3))
Chris Lattner729c6d12006-05-27 00:43:02 +00001624 return SDOperand(N, 0); // Don't revisit N.
Chris Lattner40c62d52005-10-18 06:04:22 +00001625
Nate Begeman44728a72005-09-19 22:34:01 +00001626 // fold select_cc into other things, such as min/max/abs
1627 return SimplifySelectCC(N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00001628}
1629
1630SDOperand DAGCombiner::visitSETCC(SDNode *N) {
1631 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
1632 cast<CondCodeSDNode>(N->getOperand(2))->get());
1633}
1634
Nate Begeman83e75ec2005-09-06 04:43:02 +00001635SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001636 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001637 MVT::ValueType VT = N->getValueType(0);
1638
Nate Begeman1d4d4142005-09-01 00:19:25 +00001639 // fold (sext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00001640 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00001641 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0);
Chris Lattner310b5782006-05-06 23:06:26 +00001642
Nate Begeman1d4d4142005-09-01 00:19:25 +00001643 // fold (sext (sext x)) -> (sext x)
Chris Lattner310b5782006-05-06 23:06:26 +00001644 // fold (sext (aext x)) -> (sext x)
1645 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001646 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
Chris Lattner310b5782006-05-06 23:06:26 +00001647
Chris Lattnerb14ab8a2005-12-07 07:11:03 +00001648 // fold (sext (truncate x)) -> (sextinreg x) iff x size == sext size.
Chris Lattnercc2210b2005-12-07 18:02:05 +00001649 if (N0.getOpcode() == ISD::TRUNCATE && N0.getOperand(0).getValueType() == VT&&
1650 (!AfterLegalize ||
1651 TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, N0.getValueType())))
Chris Lattnerb14ab8a2005-12-07 07:11:03 +00001652 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
1653 DAG.getValueType(N0.getValueType()));
Chris Lattner310b5782006-05-06 23:06:26 +00001654
Evan Cheng110dec22005-12-14 02:19:23 +00001655 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Chris Lattnerd0f6d182005-12-15 19:02:38 +00001656 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() &&
1657 (!AfterLegalize||TLI.isOperationLegal(ISD::SEXTLOAD, N0.getValueType()))){
Nate Begeman3df4d522005-10-12 20:40:40 +00001658 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1659 N0.getOperand(1), N0.getOperand(2),
1660 N0.getValueType());
Chris Lattnerd4771842005-12-14 19:25:30 +00001661 CombineTo(N, ExtLoad);
Chris Lattnerf9884052005-10-13 21:52:31 +00001662 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1663 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001664 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begeman3df4d522005-10-12 20:40:40 +00001665 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001666
1667 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
1668 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
1669 if ((N0.getOpcode() == ISD::SEXTLOAD || N0.getOpcode() == ISD::EXTLOAD) &&
1670 N0.hasOneUse()) {
Nate Begeman5c742682006-05-08 01:35:01 +00001671 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
1672 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1673 N0.getOperand(1), N0.getOperand(2), EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00001674 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001675 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1676 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001677 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001678 }
1679
Nate Begeman83e75ec2005-09-06 04:43:02 +00001680 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001681}
1682
Nate Begeman83e75ec2005-09-06 04:43:02 +00001683SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001684 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001685 MVT::ValueType VT = N->getValueType(0);
1686
Nate Begeman1d4d4142005-09-01 00:19:25 +00001687 // fold (zext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00001688 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00001689 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001690 // fold (zext (zext x)) -> (zext x)
Chris Lattner310b5782006-05-06 23:06:26 +00001691 // fold (zext (aext x)) -> (zext x)
1692 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001693 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
Evan Cheng110dec22005-12-14 02:19:23 +00001694 // fold (zext (truncate x)) -> (zextinreg x) iff x size == zext size.
1695 if (N0.getOpcode() == ISD::TRUNCATE && N0.getOperand(0).getValueType() == VT&&
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001696 (!AfterLegalize || TLI.isOperationLegal(ISD::AND, N0.getValueType())))
Chris Lattner00cb95c2005-12-14 07:58:38 +00001697 return DAG.getZeroExtendInReg(N0.getOperand(0), N0.getValueType());
Evan Cheng110dec22005-12-14 02:19:23 +00001698 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Chris Lattnerd0f6d182005-12-15 19:02:38 +00001699 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() &&
1700 (!AfterLegalize||TLI.isOperationLegal(ISD::ZEXTLOAD, N0.getValueType()))){
Evan Cheng110dec22005-12-14 02:19:23 +00001701 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1702 N0.getOperand(1), N0.getOperand(2),
1703 N0.getValueType());
Chris Lattnerd4771842005-12-14 19:25:30 +00001704 CombineTo(N, ExtLoad);
Evan Cheng110dec22005-12-14 02:19:23 +00001705 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1706 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001707 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng110dec22005-12-14 02:19:23 +00001708 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001709
1710 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
1711 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
1712 if ((N0.getOpcode() == ISD::ZEXTLOAD || N0.getOpcode() == ISD::EXTLOAD) &&
1713 N0.hasOneUse()) {
Nate Begeman5c742682006-05-08 01:35:01 +00001714 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
1715 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1716 N0.getOperand(1), N0.getOperand(2), EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00001717 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001718 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1719 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001720 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001721 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001722 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001723}
1724
Chris Lattner5ffc0662006-05-05 05:58:59 +00001725SDOperand DAGCombiner::visitANY_EXTEND(SDNode *N) {
1726 SDOperand N0 = N->getOperand(0);
Chris Lattner5ffc0662006-05-05 05:58:59 +00001727 MVT::ValueType VT = N->getValueType(0);
1728
1729 // fold (aext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00001730 if (isa<ConstantSDNode>(N0))
Chris Lattner5ffc0662006-05-05 05:58:59 +00001731 return DAG.getNode(ISD::ANY_EXTEND, VT, N0);
1732 // fold (aext (aext x)) -> (aext x)
1733 // fold (aext (zext x)) -> (zext x)
1734 // fold (aext (sext x)) -> (sext x)
1735 if (N0.getOpcode() == ISD::ANY_EXTEND ||
1736 N0.getOpcode() == ISD::ZERO_EXTEND ||
1737 N0.getOpcode() == ISD::SIGN_EXTEND)
1738 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
1739
1740 // fold (aext (truncate x)) -> x iff x size == zext size.
1741 if (N0.getOpcode() == ISD::TRUNCATE && N0.getOperand(0).getValueType() == VT)
1742 return N0.getOperand(0);
1743 // fold (aext (load x)) -> (aext (truncate (extload x)))
1744 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() &&
1745 (!AfterLegalize||TLI.isOperationLegal(ISD::EXTLOAD, N0.getValueType()))) {
1746 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, N0.getOperand(0),
1747 N0.getOperand(1), N0.getOperand(2),
1748 N0.getValueType());
1749 CombineTo(N, ExtLoad);
1750 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1751 ExtLoad.getValue(1));
1752 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1753 }
1754
1755 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
1756 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
1757 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
1758 if ((N0.getOpcode() == ISD::ZEXTLOAD || N0.getOpcode() == ISD::EXTLOAD ||
1759 N0.getOpcode() == ISD::SEXTLOAD) &&
1760 N0.hasOneUse()) {
Nate Begeman5c742682006-05-08 01:35:01 +00001761 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
1762 SDOperand ExtLoad = DAG.getExtLoad(N0.getOpcode(), VT, N0.getOperand(0),
1763 N0.getOperand(1), N0.getOperand(2), EVT);
Chris Lattner5ffc0662006-05-05 05:58:59 +00001764 CombineTo(N, ExtLoad);
1765 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1766 ExtLoad.getValue(1));
1767 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1768 }
1769 return SDOperand();
1770}
1771
1772
Nate Begeman83e75ec2005-09-06 04:43:02 +00001773SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001774 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001775 SDOperand N1 = N->getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001776 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001777 MVT::ValueType EVT = cast<VTSDNode>(N1)->getVT();
Nate Begeman07ed4172005-10-10 21:26:48 +00001778 unsigned EVTBits = MVT::getSizeInBits(EVT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001779
Nate Begeman1d4d4142005-09-01 00:19:25 +00001780 // fold (sext_in_reg c1) -> c1
Chris Lattnereaeda562006-05-08 20:59:41 +00001781 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
Chris Lattner310b5782006-05-06 23:06:26 +00001782 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0, N1);
Chris Lattneree4ea922006-05-06 09:30:03 +00001783
Chris Lattner541a24f2006-05-06 22:43:44 +00001784 // If the input is already sign extended, just drop the extension.
Chris Lattneree4ea922006-05-06 09:30:03 +00001785 if (TLI.ComputeNumSignBits(N0) >= MVT::getSizeInBits(VT)-EVTBits+1)
1786 return N0;
1787
Nate Begeman646d7e22005-09-02 21:18:40 +00001788 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
1789 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1790 EVT < cast<VTSDNode>(N0.getOperand(1))->getVT()) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001791 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001792 }
Chris Lattner4b37e872006-05-08 21:18:59 +00001793
Nate Begeman07ed4172005-10-10 21:26:48 +00001794 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is zero
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001795 if (TLI.MaskedValueIsZero(N0, 1ULL << (EVTBits-1)))
Nate Begemande996292006-02-03 22:24:05 +00001796 return DAG.getZeroExtendInReg(N0, EVT);
Chris Lattner4b37e872006-05-08 21:18:59 +00001797
1798 // fold (sext_in_reg (srl X, 24), i8) -> sra X, 24
1799 // fold (sext_in_reg (srl X, 23), i8) -> sra X, 23 iff possible.
1800 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
1801 if (N0.getOpcode() == ISD::SRL) {
1802 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
1803 if (ShAmt->getValue()+EVTBits <= MVT::getSizeInBits(VT)) {
1804 // We can turn this into an SRA iff the input to the SRL is already sign
1805 // extended enough.
1806 unsigned InSignBits = TLI.ComputeNumSignBits(N0.getOperand(0));
1807 if (MVT::getSizeInBits(VT)-(ShAmt->getValue()+EVTBits) < InSignBits)
1808 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0), N0.getOperand(1));
1809 }
1810 }
1811
Nate Begemanded49632005-10-13 03:11:28 +00001812 // fold (sext_inreg (extload x)) -> (sextload x)
1813 if (N0.getOpcode() == ISD::EXTLOAD &&
1814 EVT == cast<VTSDNode>(N0.getOperand(3))->getVT() &&
Nate Begemanbfd65a02005-10-13 18:34:58 +00001815 (!AfterLegalize || TLI.isOperationLegal(ISD::SEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001816 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1817 N0.getOperand(1), N0.getOperand(2),
1818 EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00001819 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00001820 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001821 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001822 }
1823 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Chris Lattner40c62d52005-10-18 06:04:22 +00001824 if (N0.getOpcode() == ISD::ZEXTLOAD && N0.hasOneUse() &&
Nate Begemanded49632005-10-13 03:11:28 +00001825 EVT == cast<VTSDNode>(N0.getOperand(3))->getVT() &&
Nate Begemanbfd65a02005-10-13 18:34:58 +00001826 (!AfterLegalize || TLI.isOperationLegal(ISD::SEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001827 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1828 N0.getOperand(1), N0.getOperand(2),
1829 EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00001830 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00001831 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001832 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001833 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001834 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001835}
1836
Nate Begeman83e75ec2005-09-06 04:43:02 +00001837SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001838 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001839 MVT::ValueType VT = N->getValueType(0);
1840
1841 // noop truncate
1842 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001843 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001844 // fold (truncate c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00001845 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00001846 return DAG.getNode(ISD::TRUNCATE, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001847 // fold (truncate (truncate x)) -> (truncate x)
1848 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001849 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001850 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
Chris Lattnerb72773b2006-05-05 22:56:26 +00001851 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND||
1852 N0.getOpcode() == ISD::ANY_EXTEND) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001853 if (N0.getValueType() < VT)
1854 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00001855 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001856 else if (N0.getValueType() > VT)
1857 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00001858 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001859 else
1860 // if the source and dest are the same type, we can drop both the extend
1861 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00001862 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001863 }
Nate Begeman3df4d522005-10-12 20:40:40 +00001864 // fold (truncate (load x)) -> (smaller load x)
Chris Lattner40c62d52005-10-18 06:04:22 +00001865 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse()) {
Nate Begeman3df4d522005-10-12 20:40:40 +00001866 assert(MVT::getSizeInBits(N0.getValueType()) > MVT::getSizeInBits(VT) &&
1867 "Cannot truncate to larger type!");
1868 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
Nate Begeman765784a2005-10-12 23:18:53 +00001869 // For big endian targets, we need to add an offset to the pointer to load
1870 // the correct bytes. For little endian systems, we merely need to read
1871 // fewer bytes from the same pointer.
Nate Begeman3df4d522005-10-12 20:40:40 +00001872 uint64_t PtrOff =
1873 (MVT::getSizeInBits(N0.getValueType()) - MVT::getSizeInBits(VT)) / 8;
Nate Begeman765784a2005-10-12 23:18:53 +00001874 SDOperand NewPtr = TLI.isLittleEndian() ? N0.getOperand(1) :
1875 DAG.getNode(ISD::ADD, PtrType, N0.getOperand(1),
1876 DAG.getConstant(PtrOff, PtrType));
Chris Lattner5750df92006-03-01 04:03:14 +00001877 AddToWorkList(NewPtr.Val);
Nate Begeman3df4d522005-10-12 20:40:40 +00001878 SDOperand Load = DAG.getLoad(VT, N0.getOperand(0), NewPtr,N0.getOperand(2));
Chris Lattner5750df92006-03-01 04:03:14 +00001879 AddToWorkList(N);
Chris Lattner24edbb72005-10-13 22:10:05 +00001880 CombineTo(N0.Val, Load, Load.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001881 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begeman3df4d522005-10-12 20:40:40 +00001882 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001883 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001884}
1885
Chris Lattner94683772005-12-23 05:30:37 +00001886SDOperand DAGCombiner::visitBIT_CONVERT(SDNode *N) {
1887 SDOperand N0 = N->getOperand(0);
1888 MVT::ValueType VT = N->getValueType(0);
1889
1890 // If the input is a constant, let getNode() fold it.
1891 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
1892 SDOperand Res = DAG.getNode(ISD::BIT_CONVERT, VT, N0);
1893 if (Res.Val != N) return Res;
1894 }
1895
Chris Lattnerc8547d82005-12-23 05:37:50 +00001896 if (N0.getOpcode() == ISD::BIT_CONVERT) // conv(conv(x,t1),t2) -> conv(x,t2)
1897 return DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
Chris Lattner6258fb22006-04-02 02:53:43 +00001898
Chris Lattner57104102005-12-23 05:44:41 +00001899 // fold (conv (load x)) -> (load (conv*)x)
Chris Lattnerbf40c4b2006-01-15 18:58:59 +00001900 // FIXME: These xforms need to know that the resultant load doesn't need a
1901 // higher alignment than the original!
1902 if (0 && N0.getOpcode() == ISD::LOAD && N0.hasOneUse()) {
Chris Lattner57104102005-12-23 05:44:41 +00001903 SDOperand Load = DAG.getLoad(VT, N0.getOperand(0), N0.getOperand(1),
1904 N0.getOperand(2));
Chris Lattner5750df92006-03-01 04:03:14 +00001905 AddToWorkList(N);
Chris Lattner57104102005-12-23 05:44:41 +00001906 CombineTo(N0.Val, DAG.getNode(ISD::BIT_CONVERT, N0.getValueType(), Load),
1907 Load.getValue(1));
1908 return Load;
1909 }
1910
Chris Lattner94683772005-12-23 05:30:37 +00001911 return SDOperand();
1912}
1913
Chris Lattner6258fb22006-04-02 02:53:43 +00001914SDOperand DAGCombiner::visitVBIT_CONVERT(SDNode *N) {
1915 SDOperand N0 = N->getOperand(0);
1916 MVT::ValueType VT = N->getValueType(0);
1917
1918 // If the input is a VBUILD_VECTOR with all constant elements, fold this now.
1919 // First check to see if this is all constant.
1920 if (N0.getOpcode() == ISD::VBUILD_VECTOR && N0.Val->hasOneUse() &&
1921 VT == MVT::Vector) {
1922 bool isSimple = true;
1923 for (unsigned i = 0, e = N0.getNumOperands()-2; i != e; ++i)
1924 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
1925 N0.getOperand(i).getOpcode() != ISD::Constant &&
1926 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
1927 isSimple = false;
1928 break;
1929 }
1930
Chris Lattner97c20732006-04-03 17:29:28 +00001931 MVT::ValueType DestEltVT = cast<VTSDNode>(N->getOperand(2))->getVT();
1932 if (isSimple && !MVT::isVector(DestEltVT)) {
Chris Lattner6258fb22006-04-02 02:53:43 +00001933 return ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(N0.Val, DestEltVT);
1934 }
1935 }
1936
1937 return SDOperand();
1938}
1939
1940/// ConstantFoldVBIT_CONVERTofVBUILD_VECTOR - We know that BV is a vbuild_vector
1941/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
1942/// destination element value type.
1943SDOperand DAGCombiner::
1944ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(SDNode *BV, MVT::ValueType DstEltVT) {
1945 MVT::ValueType SrcEltVT = BV->getOperand(0).getValueType();
1946
1947 // If this is already the right type, we're done.
1948 if (SrcEltVT == DstEltVT) return SDOperand(BV, 0);
1949
1950 unsigned SrcBitSize = MVT::getSizeInBits(SrcEltVT);
1951 unsigned DstBitSize = MVT::getSizeInBits(DstEltVT);
1952
1953 // If this is a conversion of N elements of one type to N elements of another
1954 // type, convert each element. This handles FP<->INT cases.
1955 if (SrcBitSize == DstBitSize) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001956 SmallVector<SDOperand, 8> Ops;
Chris Lattner3e104b12006-04-08 04:15:24 +00001957 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00001958 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, DstEltVT, BV->getOperand(i)));
Chris Lattner3e104b12006-04-08 04:15:24 +00001959 AddToWorkList(Ops.back().Val);
1960 }
Chris Lattner6258fb22006-04-02 02:53:43 +00001961 Ops.push_back(*(BV->op_end()-2)); // Add num elements.
1962 Ops.push_back(DAG.getValueType(DstEltVT));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001963 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00001964 }
1965
1966 // Otherwise, we're growing or shrinking the elements. To avoid having to
1967 // handle annoying details of growing/shrinking FP values, we convert them to
1968 // int first.
1969 if (MVT::isFloatingPoint(SrcEltVT)) {
1970 // Convert the input float vector to a int vector where the elements are the
1971 // same sizes.
1972 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
1973 MVT::ValueType IntVT = SrcEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
1974 BV = ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(BV, IntVT).Val;
1975 SrcEltVT = IntVT;
1976 }
1977
1978 // Now we know the input is an integer vector. If the output is a FP type,
1979 // convert to integer first, then to FP of the right size.
1980 if (MVT::isFloatingPoint(DstEltVT)) {
1981 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
1982 MVT::ValueType TmpVT = DstEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
1983 SDNode *Tmp = ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(BV, TmpVT).Val;
1984
1985 // Next, convert to FP elements of the same size.
1986 return ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(Tmp, DstEltVT);
1987 }
1988
1989 // Okay, we know the src/dst types are both integers of differing types.
1990 // Handling growing first.
1991 assert(MVT::isInteger(SrcEltVT) && MVT::isInteger(DstEltVT));
1992 if (SrcBitSize < DstBitSize) {
1993 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
1994
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001995 SmallVector<SDOperand, 8> Ops;
Chris Lattner6258fb22006-04-02 02:53:43 +00001996 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e;
1997 i += NumInputsPerOutput) {
1998 bool isLE = TLI.isLittleEndian();
1999 uint64_t NewBits = 0;
2000 bool EltIsUndef = true;
2001 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
2002 // Shift the previously computed bits over.
2003 NewBits <<= SrcBitSize;
2004 SDOperand Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
2005 if (Op.getOpcode() == ISD::UNDEF) continue;
2006 EltIsUndef = false;
2007
2008 NewBits |= cast<ConstantSDNode>(Op)->getValue();
2009 }
2010
2011 if (EltIsUndef)
2012 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
2013 else
2014 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
2015 }
2016
2017 Ops.push_back(DAG.getConstant(Ops.size(), MVT::i32)); // Add num elements.
2018 Ops.push_back(DAG.getValueType(DstEltVT)); // Add element size.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002019 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00002020 }
2021
2022 // Finally, this must be the case where we are shrinking elements: each input
2023 // turns into multiple outputs.
2024 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002025 SmallVector<SDOperand, 8> Ops;
Chris Lattner6258fb22006-04-02 02:53:43 +00002026 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e; ++i) {
2027 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
2028 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
2029 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
2030 continue;
2031 }
2032 uint64_t OpVal = cast<ConstantSDNode>(BV->getOperand(i))->getValue();
2033
2034 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
2035 unsigned ThisVal = OpVal & ((1ULL << DstBitSize)-1);
2036 OpVal >>= DstBitSize;
2037 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
2038 }
2039
2040 // For big endian targets, swap the order of the pieces of each element.
2041 if (!TLI.isLittleEndian())
2042 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
2043 }
2044 Ops.push_back(DAG.getConstant(Ops.size(), MVT::i32)); // Add num elements.
2045 Ops.push_back(DAG.getValueType(DstEltVT)); // Add element size.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002046 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00002047}
2048
2049
2050
Chris Lattner01b3d732005-09-28 22:28:18 +00002051SDOperand DAGCombiner::visitFADD(SDNode *N) {
2052 SDOperand N0 = N->getOperand(0);
2053 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00002054 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2055 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002056 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00002057
2058 // fold (fadd c1, c2) -> c1+c2
2059 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002060 return DAG.getNode(ISD::FADD, VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00002061 // canonicalize constant to RHS
2062 if (N0CFP && !N1CFP)
2063 return DAG.getNode(ISD::FADD, VT, N1, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00002064 // fold (A + (-B)) -> A-B
2065 if (N1.getOpcode() == ISD::FNEG)
2066 return DAG.getNode(ISD::FSUB, VT, N0, N1.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00002067 // fold ((-A) + B) -> B-A
2068 if (N0.getOpcode() == ISD::FNEG)
2069 return DAG.getNode(ISD::FSUB, VT, N1, N0.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00002070 return SDOperand();
2071}
2072
2073SDOperand DAGCombiner::visitFSUB(SDNode *N) {
2074 SDOperand N0 = N->getOperand(0);
2075 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00002076 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2077 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002078 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00002079
2080 // fold (fsub c1, c2) -> c1-c2
2081 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002082 return DAG.getNode(ISD::FSUB, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002083 // fold (A-(-B)) -> A+B
2084 if (N1.getOpcode() == ISD::FNEG)
Nate Begemana148d982006-01-18 22:35:16 +00002085 return DAG.getNode(ISD::FADD, VT, N0, N1.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00002086 return SDOperand();
2087}
2088
2089SDOperand DAGCombiner::visitFMUL(SDNode *N) {
2090 SDOperand N0 = N->getOperand(0);
2091 SDOperand N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00002092 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2093 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002094 MVT::ValueType VT = N->getValueType(0);
2095
Nate Begeman11af4ea2005-10-17 20:40:11 +00002096 // fold (fmul c1, c2) -> c1*c2
2097 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002098 return DAG.getNode(ISD::FMUL, VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00002099 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00002100 if (N0CFP && !N1CFP)
2101 return DAG.getNode(ISD::FMUL, VT, N1, N0);
Nate Begeman11af4ea2005-10-17 20:40:11 +00002102 // fold (fmul X, 2.0) -> (fadd X, X)
2103 if (N1CFP && N1CFP->isExactlyValue(+2.0))
2104 return DAG.getNode(ISD::FADD, VT, N0, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00002105 return SDOperand();
2106}
2107
2108SDOperand DAGCombiner::visitFDIV(SDNode *N) {
2109 SDOperand N0 = N->getOperand(0);
2110 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00002111 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2112 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002113 MVT::ValueType VT = N->getValueType(0);
2114
Nate Begemana148d982006-01-18 22:35:16 +00002115 // fold (fdiv c1, c2) -> c1/c2
2116 if (N0CFP && N1CFP)
2117 return DAG.getNode(ISD::FDIV, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002118 return SDOperand();
2119}
2120
2121SDOperand DAGCombiner::visitFREM(SDNode *N) {
2122 SDOperand N0 = N->getOperand(0);
2123 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00002124 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2125 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002126 MVT::ValueType VT = N->getValueType(0);
2127
Nate Begemana148d982006-01-18 22:35:16 +00002128 // fold (frem c1, c2) -> fmod(c1,c2)
2129 if (N0CFP && N1CFP)
2130 return DAG.getNode(ISD::FREM, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002131 return SDOperand();
2132}
2133
Chris Lattner12d83032006-03-05 05:30:57 +00002134SDOperand DAGCombiner::visitFCOPYSIGN(SDNode *N) {
2135 SDOperand N0 = N->getOperand(0);
2136 SDOperand N1 = N->getOperand(1);
2137 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2138 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2139 MVT::ValueType VT = N->getValueType(0);
2140
2141 if (N0CFP && N1CFP) // Constant fold
2142 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1);
2143
2144 if (N1CFP) {
2145 // copysign(x, c1) -> fabs(x) iff ispos(c1)
2146 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
2147 union {
2148 double d;
2149 int64_t i;
2150 } u;
2151 u.d = N1CFP->getValue();
2152 if (u.i >= 0)
2153 return DAG.getNode(ISD::FABS, VT, N0);
2154 else
2155 return DAG.getNode(ISD::FNEG, VT, DAG.getNode(ISD::FABS, VT, N0));
2156 }
2157
2158 // copysign(fabs(x), y) -> copysign(x, y)
2159 // copysign(fneg(x), y) -> copysign(x, y)
2160 // copysign(copysign(x,z), y) -> copysign(x, y)
2161 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
2162 N0.getOpcode() == ISD::FCOPYSIGN)
2163 return DAG.getNode(ISD::FCOPYSIGN, VT, N0.getOperand(0), N1);
2164
2165 // copysign(x, abs(y)) -> abs(x)
2166 if (N1.getOpcode() == ISD::FABS)
2167 return DAG.getNode(ISD::FABS, VT, N0);
2168
2169 // copysign(x, copysign(y,z)) -> copysign(x, z)
2170 if (N1.getOpcode() == ISD::FCOPYSIGN)
2171 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(1));
2172
2173 // copysign(x, fp_extend(y)) -> copysign(x, y)
2174 // copysign(x, fp_round(y)) -> copysign(x, y)
2175 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
2176 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(0));
2177
2178 return SDOperand();
2179}
2180
2181
Chris Lattner01b3d732005-09-28 22:28:18 +00002182
Nate Begeman83e75ec2005-09-06 04:43:02 +00002183SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002184 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002185 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00002186 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002187
2188 // fold (sint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002189 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00002190 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002191 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002192}
2193
Nate Begeman83e75ec2005-09-06 04:43:02 +00002194SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002195 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002196 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00002197 MVT::ValueType VT = N->getValueType(0);
2198
Nate Begeman1d4d4142005-09-01 00:19:25 +00002199 // fold (uint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002200 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00002201 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002202 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002203}
2204
Nate Begeman83e75ec2005-09-06 04:43:02 +00002205SDOperand DAGCombiner::visitFP_TO_SINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002206 SDOperand N0 = N->getOperand(0);
2207 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2208 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002209
2210 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00002211 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002212 return DAG.getNode(ISD::FP_TO_SINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002213 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002214}
2215
Nate Begeman83e75ec2005-09-06 04:43:02 +00002216SDOperand DAGCombiner::visitFP_TO_UINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002217 SDOperand N0 = N->getOperand(0);
2218 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2219 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002220
2221 // fold (fp_to_uint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00002222 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002223 return DAG.getNode(ISD::FP_TO_UINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002224 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002225}
2226
Nate Begeman83e75ec2005-09-06 04:43:02 +00002227SDOperand DAGCombiner::visitFP_ROUND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002228 SDOperand N0 = N->getOperand(0);
2229 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2230 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002231
2232 // fold (fp_round c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002233 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002234 return DAG.getNode(ISD::FP_ROUND, VT, N0);
Chris Lattner79dbea52006-03-13 06:26:26 +00002235
2236 // fold (fp_round (fp_extend x)) -> x
2237 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
2238 return N0.getOperand(0);
2239
2240 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
2241 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.Val->hasOneUse()) {
2242 SDOperand Tmp = DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0));
2243 AddToWorkList(Tmp.Val);
2244 return DAG.getNode(ISD::FCOPYSIGN, VT, Tmp, N0.getOperand(1));
2245 }
2246
Nate Begeman83e75ec2005-09-06 04:43:02 +00002247 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002248}
2249
Nate Begeman83e75ec2005-09-06 04:43:02 +00002250SDOperand DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002251 SDOperand N0 = N->getOperand(0);
2252 MVT::ValueType VT = N->getValueType(0);
2253 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00002254 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002255
Nate Begeman1d4d4142005-09-01 00:19:25 +00002256 // fold (fp_round_inreg c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002257 if (N0CFP) {
2258 SDOperand Round = DAG.getConstantFP(N0CFP->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002259 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002260 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00002261 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002262}
2263
Nate Begeman83e75ec2005-09-06 04:43:02 +00002264SDOperand DAGCombiner::visitFP_EXTEND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002265 SDOperand N0 = N->getOperand(0);
2266 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2267 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002268
2269 // fold (fp_extend c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002270 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002271 return DAG.getNode(ISD::FP_EXTEND, VT, N0);
Chris Lattnere564dbb2006-05-05 21:34:35 +00002272
2273 // fold (fpext (load x)) -> (fpext (fpround (extload x)))
2274 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() &&
2275 (!AfterLegalize||TLI.isOperationLegal(ISD::EXTLOAD, N0.getValueType()))) {
2276 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, N0.getOperand(0),
2277 N0.getOperand(1), N0.getOperand(2),
2278 N0.getValueType());
2279 CombineTo(N, ExtLoad);
2280 CombineTo(N0.Val, DAG.getNode(ISD::FP_ROUND, N0.getValueType(), ExtLoad),
2281 ExtLoad.getValue(1));
2282 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2283 }
2284
2285
Nate Begeman83e75ec2005-09-06 04:43:02 +00002286 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002287}
2288
Nate Begeman83e75ec2005-09-06 04:43:02 +00002289SDOperand DAGCombiner::visitFNEG(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002290 SDOperand N0 = N->getOperand(0);
2291 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2292 MVT::ValueType VT = N->getValueType(0);
2293
2294 // fold (fneg c1) -> -c1
Nate Begeman646d7e22005-09-02 21:18:40 +00002295 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002296 return DAG.getNode(ISD::FNEG, VT, N0);
2297 // fold (fneg (sub x, y)) -> (sub y, x)
Chris Lattner12d83032006-03-05 05:30:57 +00002298 if (N0.getOpcode() == ISD::SUB)
2299 return DAG.getNode(ISD::SUB, VT, N0.getOperand(1), N0.getOperand(0));
Nate Begemana148d982006-01-18 22:35:16 +00002300 // fold (fneg (fneg x)) -> x
Chris Lattner12d83032006-03-05 05:30:57 +00002301 if (N0.getOpcode() == ISD::FNEG)
2302 return N0.getOperand(0);
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::visitFABS(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);
2310
Nate Begeman1d4d4142005-09-01 00:19:25 +00002311 // fold (fabs c1) -> fabs(c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00002312 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002313 return DAG.getNode(ISD::FABS, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002314 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00002315 if (N0.getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002316 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002317 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00002318 // fold (fabs (fcopysign x, y)) -> (fabs x)
2319 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
2320 return DAG.getNode(ISD::FABS, VT, N0.getOperand(0));
2321
Nate Begeman83e75ec2005-09-06 04:43:02 +00002322 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002323}
2324
Nate Begeman44728a72005-09-19 22:34:01 +00002325SDOperand DAGCombiner::visitBRCOND(SDNode *N) {
2326 SDOperand Chain = N->getOperand(0);
2327 SDOperand N1 = N->getOperand(1);
2328 SDOperand N2 = N->getOperand(2);
2329 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2330
2331 // never taken branch, fold to chain
2332 if (N1C && N1C->isNullValue())
2333 return Chain;
2334 // unconditional branch
Nate Begemane17daeb2005-10-05 21:43:42 +00002335 if (N1C && N1C->getValue() == 1)
Nate Begeman44728a72005-09-19 22:34:01 +00002336 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
Nate Begeman750ac1b2006-02-01 07:19:44 +00002337 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
2338 // on the target.
2339 if (N1.getOpcode() == ISD::SETCC &&
2340 TLI.isOperationLegal(ISD::BR_CC, MVT::Other)) {
2341 return DAG.getNode(ISD::BR_CC, MVT::Other, Chain, N1.getOperand(2),
2342 N1.getOperand(0), N1.getOperand(1), N2);
2343 }
Nate Begeman44728a72005-09-19 22:34:01 +00002344 return SDOperand();
2345}
2346
Chris Lattner3ea0b472005-10-05 06:47:48 +00002347// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
2348//
Nate Begeman44728a72005-09-19 22:34:01 +00002349SDOperand DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00002350 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
2351 SDOperand CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
2352
2353 // Use SimplifySetCC to simplify SETCC's.
Nate Begemane17daeb2005-10-05 21:43:42 +00002354 SDOperand Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false);
2355 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.Val);
2356
2357 // fold br_cc true, dest -> br dest (unconditional branch)
2358 if (SCCC && SCCC->getValue())
2359 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
2360 N->getOperand(4));
2361 // fold br_cc false, dest -> unconditional fall through
2362 if (SCCC && SCCC->isNullValue())
2363 return N->getOperand(0);
2364 // fold to a simpler setcc
2365 if (Simp.Val && Simp.getOpcode() == ISD::SETCC)
2366 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
2367 Simp.getOperand(2), Simp.getOperand(0),
2368 Simp.getOperand(1), N->getOperand(4));
Nate Begeman44728a72005-09-19 22:34:01 +00002369 return SDOperand();
2370}
2371
Chris Lattner01a22022005-10-10 22:04:48 +00002372SDOperand DAGCombiner::visitLOAD(SDNode *N) {
2373 SDOperand Chain = N->getOperand(0);
2374 SDOperand Ptr = N->getOperand(1);
2375 SDOperand SrcValue = N->getOperand(2);
Chris Lattnere4b95392006-03-31 18:06:18 +00002376
2377 // If there are no uses of the loaded value, change uses of the chain value
2378 // into uses of the chain input (i.e. delete the dead load).
2379 if (N->hasNUsesOfValue(0, 0))
2380 return CombineTo(N, DAG.getNode(ISD::UNDEF, N->getValueType(0)), Chain);
Chris Lattner01a22022005-10-10 22:04:48 +00002381
2382 // If this load is directly stored, replace the load value with the stored
2383 // value.
2384 // TODO: Handle store large -> read small portion.
2385 // TODO: Handle TRUNCSTORE/EXTLOAD
2386 if (Chain.getOpcode() == ISD::STORE && Chain.getOperand(2) == Ptr &&
2387 Chain.getOperand(1).getValueType() == N->getValueType(0))
2388 return CombineTo(N, Chain.getOperand(1), Chain);
2389
2390 return SDOperand();
2391}
2392
Chris Lattner29cd7db2006-03-31 18:10:41 +00002393/// visitXEXTLOAD - Handle EXTLOAD/ZEXTLOAD/SEXTLOAD.
2394SDOperand DAGCombiner::visitXEXTLOAD(SDNode *N) {
2395 SDOperand Chain = N->getOperand(0);
2396 SDOperand Ptr = N->getOperand(1);
2397 SDOperand SrcValue = N->getOperand(2);
2398 SDOperand EVT = N->getOperand(3);
2399
2400 // If there are no uses of the loaded value, change uses of the chain value
2401 // into uses of the chain input (i.e. delete the dead load).
2402 if (N->hasNUsesOfValue(0, 0))
2403 return CombineTo(N, DAG.getNode(ISD::UNDEF, N->getValueType(0)), Chain);
2404
2405 return SDOperand();
2406}
2407
Chris Lattner87514ca2005-10-10 22:31:19 +00002408SDOperand DAGCombiner::visitSTORE(SDNode *N) {
2409 SDOperand Chain = N->getOperand(0);
2410 SDOperand Value = N->getOperand(1);
2411 SDOperand Ptr = N->getOperand(2);
2412 SDOperand SrcValue = N->getOperand(3);
2413
2414 // If this is a store that kills a previous store, remove the previous store.
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002415 if (Chain.getOpcode() == ISD::STORE && Chain.getOperand(2) == Ptr &&
Chris Lattnerfe7f0462005-10-27 07:10:34 +00002416 Chain.Val->hasOneUse() /* Avoid introducing DAG cycles */ &&
2417 // Make sure that these stores are the same value type:
2418 // FIXME: we really care that the second store is >= size of the first.
2419 Value.getValueType() == Chain.getOperand(1).getValueType()) {
Chris Lattner87514ca2005-10-10 22:31:19 +00002420 // Create a new store of Value that replaces both stores.
2421 SDNode *PrevStore = Chain.Val;
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002422 if (PrevStore->getOperand(1) == Value) // Same value multiply stored.
2423 return Chain;
Chris Lattner87514ca2005-10-10 22:31:19 +00002424 SDOperand NewStore = DAG.getNode(ISD::STORE, MVT::Other,
2425 PrevStore->getOperand(0), Value, Ptr,
2426 SrcValue);
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002427 CombineTo(N, NewStore); // Nuke this store.
Chris Lattner87514ca2005-10-10 22:31:19 +00002428 CombineTo(PrevStore, NewStore); // Nuke the previous store.
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002429 return SDOperand(N, 0);
Chris Lattner87514ca2005-10-10 22:31:19 +00002430 }
2431
Chris Lattnerc33baaa2005-12-23 05:48:07 +00002432 // If this is a store of a bit convert, store the input value.
Chris Lattnerbf40c4b2006-01-15 18:58:59 +00002433 // FIXME: This needs to know that the resultant store does not need a
2434 // higher alignment than the original.
2435 if (0 && Value.getOpcode() == ISD::BIT_CONVERT)
Chris Lattnerc33baaa2005-12-23 05:48:07 +00002436 return DAG.getNode(ISD::STORE, MVT::Other, Chain, Value.getOperand(0),
2437 Ptr, SrcValue);
2438
Chris Lattner87514ca2005-10-10 22:31:19 +00002439 return SDOperand();
2440}
2441
Chris Lattnerca242442006-03-19 01:27:56 +00002442SDOperand DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
2443 SDOperand InVec = N->getOperand(0);
2444 SDOperand InVal = N->getOperand(1);
2445 SDOperand EltNo = N->getOperand(2);
2446
2447 // If the invec is a BUILD_VECTOR and if EltNo is a constant, build a new
2448 // vector with the inserted element.
2449 if (InVec.getOpcode() == ISD::BUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
2450 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002451 SmallVector<SDOperand, 8> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
Chris Lattnerca242442006-03-19 01:27:56 +00002452 if (Elt < Ops.size())
2453 Ops[Elt] = InVal;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002454 return DAG.getNode(ISD::BUILD_VECTOR, InVec.getValueType(),
2455 &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00002456 }
2457
2458 return SDOperand();
2459}
2460
2461SDOperand DAGCombiner::visitVINSERT_VECTOR_ELT(SDNode *N) {
2462 SDOperand InVec = N->getOperand(0);
2463 SDOperand InVal = N->getOperand(1);
2464 SDOperand EltNo = N->getOperand(2);
2465 SDOperand NumElts = N->getOperand(3);
2466 SDOperand EltType = N->getOperand(4);
2467
2468 // If the invec is a VBUILD_VECTOR and if EltNo is a constant, build a new
2469 // vector with the inserted element.
2470 if (InVec.getOpcode() == ISD::VBUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
2471 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002472 SmallVector<SDOperand, 8> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
Chris Lattnerca242442006-03-19 01:27:56 +00002473 if (Elt < Ops.size()-2)
2474 Ops[Elt] = InVal;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002475 return DAG.getNode(ISD::VBUILD_VECTOR, InVec.getValueType(),
2476 &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00002477 }
2478
2479 return SDOperand();
2480}
2481
Chris Lattnerd7648c82006-03-28 20:28:38 +00002482SDOperand DAGCombiner::visitVBUILD_VECTOR(SDNode *N) {
2483 unsigned NumInScalars = N->getNumOperands()-2;
2484 SDOperand NumElts = N->getOperand(NumInScalars);
2485 SDOperand EltType = N->getOperand(NumInScalars+1);
2486
2487 // Check to see if this is a VBUILD_VECTOR of a bunch of VEXTRACT_VECTOR_ELT
2488 // operations. If so, and if the EXTRACT_ELT vector inputs come from at most
2489 // two distinct vectors, turn this into a shuffle node.
2490 SDOperand VecIn1, VecIn2;
2491 for (unsigned i = 0; i != NumInScalars; ++i) {
2492 // Ignore undef inputs.
2493 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
2494
2495 // If this input is something other than a VEXTRACT_VECTOR_ELT with a
2496 // constant index, bail out.
2497 if (N->getOperand(i).getOpcode() != ISD::VEXTRACT_VECTOR_ELT ||
2498 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
2499 VecIn1 = VecIn2 = SDOperand(0, 0);
2500 break;
2501 }
2502
2503 // If the input vector type disagrees with the result of the vbuild_vector,
2504 // we can't make a shuffle.
2505 SDOperand ExtractedFromVec = N->getOperand(i).getOperand(0);
2506 if (*(ExtractedFromVec.Val->op_end()-2) != NumElts ||
2507 *(ExtractedFromVec.Val->op_end()-1) != EltType) {
2508 VecIn1 = VecIn2 = SDOperand(0, 0);
2509 break;
2510 }
2511
2512 // Otherwise, remember this. We allow up to two distinct input vectors.
2513 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
2514 continue;
2515
2516 if (VecIn1.Val == 0) {
2517 VecIn1 = ExtractedFromVec;
2518 } else if (VecIn2.Val == 0) {
2519 VecIn2 = ExtractedFromVec;
2520 } else {
2521 // Too many inputs.
2522 VecIn1 = VecIn2 = SDOperand(0, 0);
2523 break;
2524 }
2525 }
2526
2527 // If everything is good, we can make a shuffle operation.
2528 if (VecIn1.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002529 SmallVector<SDOperand, 8> BuildVecIndices;
Chris Lattnerd7648c82006-03-28 20:28:38 +00002530 for (unsigned i = 0; i != NumInScalars; ++i) {
2531 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
2532 BuildVecIndices.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
2533 continue;
2534 }
2535
2536 SDOperand Extract = N->getOperand(i);
2537
2538 // If extracting from the first vector, just use the index directly.
2539 if (Extract.getOperand(0) == VecIn1) {
2540 BuildVecIndices.push_back(Extract.getOperand(1));
2541 continue;
2542 }
2543
2544 // Otherwise, use InIdx + VecSize
2545 unsigned Idx = cast<ConstantSDNode>(Extract.getOperand(1))->getValue();
2546 BuildVecIndices.push_back(DAG.getConstant(Idx+NumInScalars, MVT::i32));
2547 }
2548
2549 // Add count and size info.
2550 BuildVecIndices.push_back(NumElts);
2551 BuildVecIndices.push_back(DAG.getValueType(MVT::i32));
2552
2553 // Return the new VVECTOR_SHUFFLE node.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002554 SDOperand Ops[5];
2555 Ops[0] = VecIn1;
Chris Lattnercef896e2006-03-28 22:19:47 +00002556 if (VecIn2.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002557 Ops[1] = VecIn2;
Chris Lattnercef896e2006-03-28 22:19:47 +00002558 } else {
2559 // Use an undef vbuild_vector as input for the second operand.
2560 std::vector<SDOperand> UnOps(NumInScalars,
2561 DAG.getNode(ISD::UNDEF,
2562 cast<VTSDNode>(EltType)->getVT()));
2563 UnOps.push_back(NumElts);
2564 UnOps.push_back(EltType);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002565 Ops[1] = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
2566 &UnOps[0], UnOps.size());
2567 AddToWorkList(Ops[1].Val);
Chris Lattnercef896e2006-03-28 22:19:47 +00002568 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002569 Ops[2] = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
2570 &BuildVecIndices[0], BuildVecIndices.size());
2571 Ops[3] = NumElts;
2572 Ops[4] = EltType;
2573 return DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector, Ops, 5);
Chris Lattnerd7648c82006-03-28 20:28:38 +00002574 }
2575
2576 return SDOperand();
2577}
2578
Chris Lattner66445d32006-03-28 22:11:53 +00002579SDOperand DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
Chris Lattnerf1d0c622006-03-31 22:16:43 +00002580 SDOperand ShufMask = N->getOperand(2);
2581 unsigned NumElts = ShufMask.getNumOperands();
2582
2583 // If the shuffle mask is an identity operation on the LHS, return the LHS.
2584 bool isIdentity = true;
2585 for (unsigned i = 0; i != NumElts; ++i) {
2586 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
2587 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
2588 isIdentity = false;
2589 break;
2590 }
2591 }
2592 if (isIdentity) return N->getOperand(0);
2593
2594 // If the shuffle mask is an identity operation on the RHS, return the RHS.
2595 isIdentity = true;
2596 for (unsigned i = 0; i != NumElts; ++i) {
2597 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
2598 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
2599 isIdentity = false;
2600 break;
2601 }
2602 }
2603 if (isIdentity) return N->getOperand(1);
Evan Chenge7bec0d2006-07-20 22:44:41 +00002604
2605 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
2606 // needed at all.
2607 bool isUnary = true;
Evan Cheng917ec982006-07-21 08:25:53 +00002608 bool isSplat = true;
Evan Chenge7bec0d2006-07-20 22:44:41 +00002609 int VecNum = -1;
Reid Spencer9160a6a2006-07-25 20:44:41 +00002610 unsigned BaseIdx = 0;
Evan Chenge7bec0d2006-07-20 22:44:41 +00002611 for (unsigned i = 0; i != NumElts; ++i)
2612 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
2613 unsigned Idx = cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue();
2614 int V = (Idx < NumElts) ? 0 : 1;
Evan Cheng917ec982006-07-21 08:25:53 +00002615 if (VecNum == -1) {
Evan Chenge7bec0d2006-07-20 22:44:41 +00002616 VecNum = V;
Evan Cheng917ec982006-07-21 08:25:53 +00002617 BaseIdx = Idx;
2618 } else {
2619 if (BaseIdx != Idx)
2620 isSplat = false;
2621 if (VecNum != V) {
2622 isUnary = false;
2623 break;
2624 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00002625 }
2626 }
2627
2628 SDOperand N0 = N->getOperand(0);
2629 SDOperand N1 = N->getOperand(1);
2630 // Normalize unary shuffle so the RHS is undef.
2631 if (isUnary && VecNum == 1)
2632 std::swap(N0, N1);
2633
Evan Cheng917ec982006-07-21 08:25:53 +00002634 // If it is a splat, check if the argument vector is a build_vector with
2635 // all scalar elements the same.
2636 if (isSplat) {
2637 SDNode *V = N0.Val;
2638 if (V->getOpcode() == ISD::BIT_CONVERT)
2639 V = V->getOperand(0).Val;
2640 if (V->getOpcode() == ISD::BUILD_VECTOR) {
2641 unsigned NumElems = V->getNumOperands()-2;
2642 if (NumElems > BaseIdx) {
2643 SDOperand Base;
2644 bool AllSame = true;
2645 for (unsigned i = 0; i != NumElems; ++i) {
2646 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
2647 Base = V->getOperand(i);
2648 break;
2649 }
2650 }
2651 // Splat of <u, u, u, u>, return <u, u, u, u>
2652 if (!Base.Val)
2653 return N0;
2654 for (unsigned i = 0; i != NumElems; ++i) {
2655 if (V->getOperand(i).getOpcode() != ISD::UNDEF &&
2656 V->getOperand(i) != Base) {
2657 AllSame = false;
2658 break;
2659 }
2660 }
2661 // Splat of <x, x, x, x>, return <x, x, x, x>
2662 if (AllSame)
2663 return N0;
2664 }
2665 }
2666 }
2667
Evan Chenge7bec0d2006-07-20 22:44:41 +00002668 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
2669 // into an undef.
2670 if (isUnary || N0 == N1) {
2671 if (N0.getOpcode() == ISD::UNDEF)
Evan Chengc04766a2006-04-06 23:20:43 +00002672 return DAG.getNode(ISD::UNDEF, N->getValueType(0));
Chris Lattner66445d32006-03-28 22:11:53 +00002673 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
2674 // first operand.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002675 SmallVector<SDOperand, 8> MappedOps;
Chris Lattner66445d32006-03-28 22:11:53 +00002676 for (unsigned i = 0, e = ShufMask.getNumOperands(); i != e; ++i) {
Evan Chengc04766a2006-04-06 23:20:43 +00002677 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
2678 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) {
2679 MappedOps.push_back(ShufMask.getOperand(i));
2680 } else {
Chris Lattner66445d32006-03-28 22:11:53 +00002681 unsigned NewIdx =
2682 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
2683 MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32));
Chris Lattner66445d32006-03-28 22:11:53 +00002684 }
2685 }
2686 ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMask.getValueType(),
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002687 &MappedOps[0], MappedOps.size());
Chris Lattner3e104b12006-04-08 04:15:24 +00002688 AddToWorkList(ShufMask.Val);
Chris Lattner66445d32006-03-28 22:11:53 +00002689 return DAG.getNode(ISD::VECTOR_SHUFFLE, N->getValueType(0),
Evan Chenge7bec0d2006-07-20 22:44:41 +00002690 N0,
Chris Lattner66445d32006-03-28 22:11:53 +00002691 DAG.getNode(ISD::UNDEF, N->getValueType(0)),
2692 ShufMask);
2693 }
2694
2695 return SDOperand();
2696}
2697
Chris Lattnerf1d0c622006-03-31 22:16:43 +00002698SDOperand DAGCombiner::visitVVECTOR_SHUFFLE(SDNode *N) {
2699 SDOperand ShufMask = N->getOperand(2);
2700 unsigned NumElts = ShufMask.getNumOperands()-2;
2701
2702 // If the shuffle mask is an identity operation on the LHS, return the LHS.
2703 bool isIdentity = true;
2704 for (unsigned i = 0; i != NumElts; ++i) {
2705 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
2706 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
2707 isIdentity = false;
2708 break;
2709 }
2710 }
2711 if (isIdentity) return N->getOperand(0);
2712
2713 // If the shuffle mask is an identity operation on the RHS, return the RHS.
2714 isIdentity = true;
2715 for (unsigned i = 0; i != NumElts; ++i) {
2716 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
2717 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
2718 isIdentity = false;
2719 break;
2720 }
2721 }
2722 if (isIdentity) return N->getOperand(1);
2723
Evan Chenge7bec0d2006-07-20 22:44:41 +00002724 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
2725 // needed at all.
2726 bool isUnary = true;
Evan Cheng917ec982006-07-21 08:25:53 +00002727 bool isSplat = true;
Evan Chenge7bec0d2006-07-20 22:44:41 +00002728 int VecNum = -1;
Reid Spencer9160a6a2006-07-25 20:44:41 +00002729 unsigned BaseIdx = 0;
Evan Chenge7bec0d2006-07-20 22:44:41 +00002730 for (unsigned i = 0; i != NumElts; ++i)
2731 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
2732 unsigned Idx = cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue();
2733 int V = (Idx < NumElts) ? 0 : 1;
Evan Cheng917ec982006-07-21 08:25:53 +00002734 if (VecNum == -1) {
Evan Chenge7bec0d2006-07-20 22:44:41 +00002735 VecNum = V;
Evan Cheng917ec982006-07-21 08:25:53 +00002736 BaseIdx = Idx;
2737 } else {
2738 if (BaseIdx != Idx)
2739 isSplat = false;
2740 if (VecNum != V) {
2741 isUnary = false;
2742 break;
2743 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00002744 }
2745 }
2746
2747 SDOperand N0 = N->getOperand(0);
2748 SDOperand N1 = N->getOperand(1);
2749 // Normalize unary shuffle so the RHS is undef.
2750 if (isUnary && VecNum == 1)
2751 std::swap(N0, N1);
2752
Evan Cheng917ec982006-07-21 08:25:53 +00002753 // If it is a splat, check if the argument vector is a build_vector with
2754 // all scalar elements the same.
2755 if (isSplat) {
2756 SDNode *V = N0.Val;
2757 if (V->getOpcode() == ISD::VBIT_CONVERT)
2758 V = V->getOperand(0).Val;
2759 if (V->getOpcode() == ISD::VBUILD_VECTOR) {
2760 unsigned NumElems = V->getNumOperands()-2;
2761 if (NumElems > BaseIdx) {
2762 SDOperand Base;
2763 bool AllSame = true;
2764 for (unsigned i = 0; i != NumElems; ++i) {
2765 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
2766 Base = V->getOperand(i);
2767 break;
2768 }
2769 }
2770 // Splat of <u, u, u, u>, return <u, u, u, u>
2771 if (!Base.Val)
2772 return N0;
2773 for (unsigned i = 0; i != NumElems; ++i) {
2774 if (V->getOperand(i).getOpcode() != ISD::UNDEF &&
2775 V->getOperand(i) != Base) {
2776 AllSame = false;
2777 break;
2778 }
2779 }
2780 // Splat of <x, x, x, x>, return <x, x, x, x>
2781 if (AllSame)
2782 return N0;
2783 }
2784 }
2785 }
2786
Evan Chenge7bec0d2006-07-20 22:44:41 +00002787 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
2788 // into an undef.
2789 if (isUnary || N0 == N1) {
Chris Lattner17614ea2006-04-08 05:34:25 +00002790 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
2791 // first operand.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002792 SmallVector<SDOperand, 8> MappedOps;
Chris Lattner17614ea2006-04-08 05:34:25 +00002793 for (unsigned i = 0; i != NumElts; ++i) {
2794 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
2795 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) {
2796 MappedOps.push_back(ShufMask.getOperand(i));
2797 } else {
2798 unsigned NewIdx =
2799 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
2800 MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32));
2801 }
2802 }
2803 // Add the type/#elts values.
2804 MappedOps.push_back(ShufMask.getOperand(NumElts));
2805 MappedOps.push_back(ShufMask.getOperand(NumElts+1));
2806
2807 ShufMask = DAG.getNode(ISD::VBUILD_VECTOR, ShufMask.getValueType(),
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002808 &MappedOps[0], MappedOps.size());
Chris Lattner17614ea2006-04-08 05:34:25 +00002809 AddToWorkList(ShufMask.Val);
2810
2811 // Build the undef vector.
2812 SDOperand UDVal = DAG.getNode(ISD::UNDEF, MappedOps[0].getValueType());
2813 for (unsigned i = 0; i != NumElts; ++i)
2814 MappedOps[i] = UDVal;
Evan Chenge7bec0d2006-07-20 22:44:41 +00002815 MappedOps[NumElts ] = *(N0.Val->op_end()-2);
2816 MappedOps[NumElts+1] = *(N0.Val->op_end()-1);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002817 UDVal = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
2818 &MappedOps[0], MappedOps.size());
Chris Lattner17614ea2006-04-08 05:34:25 +00002819
2820 return DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector,
Evan Chenge7bec0d2006-07-20 22:44:41 +00002821 N0, UDVal, ShufMask,
Chris Lattner17614ea2006-04-08 05:34:25 +00002822 MappedOps[NumElts], MappedOps[NumElts+1]);
2823 }
2824
Chris Lattnerf1d0c622006-03-31 22:16:43 +00002825 return SDOperand();
2826}
2827
Evan Cheng44f1f092006-04-20 08:56:16 +00002828/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
2829/// a VAND to a vector_shuffle with the destination vector and a zero vector.
2830/// e.g. VAND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
2831/// vector_shuffle V, Zero, <0, 4, 2, 4>
2832SDOperand DAGCombiner::XformToShuffleWithZero(SDNode *N) {
2833 SDOperand LHS = N->getOperand(0);
2834 SDOperand RHS = N->getOperand(1);
2835 if (N->getOpcode() == ISD::VAND) {
2836 SDOperand DstVecSize = *(LHS.Val->op_end()-2);
2837 SDOperand DstVecEVT = *(LHS.Val->op_end()-1);
2838 if (RHS.getOpcode() == ISD::VBIT_CONVERT)
2839 RHS = RHS.getOperand(0);
2840 if (RHS.getOpcode() == ISD::VBUILD_VECTOR) {
2841 std::vector<SDOperand> IdxOps;
2842 unsigned NumOps = RHS.getNumOperands();
2843 unsigned NumElts = NumOps-2;
2844 MVT::ValueType EVT = cast<VTSDNode>(RHS.getOperand(NumOps-1))->getVT();
2845 for (unsigned i = 0; i != NumElts; ++i) {
2846 SDOperand Elt = RHS.getOperand(i);
2847 if (!isa<ConstantSDNode>(Elt))
2848 return SDOperand();
2849 else if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
2850 IdxOps.push_back(DAG.getConstant(i, EVT));
2851 else if (cast<ConstantSDNode>(Elt)->isNullValue())
2852 IdxOps.push_back(DAG.getConstant(NumElts, EVT));
2853 else
2854 return SDOperand();
2855 }
2856
2857 // Let's see if the target supports this vector_shuffle.
2858 if (!TLI.isVectorClearMaskLegal(IdxOps, EVT, DAG))
2859 return SDOperand();
2860
2861 // Return the new VVECTOR_SHUFFLE node.
2862 SDOperand NumEltsNode = DAG.getConstant(NumElts, MVT::i32);
2863 SDOperand EVTNode = DAG.getValueType(EVT);
2864 std::vector<SDOperand> Ops;
2865 LHS = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, LHS, NumEltsNode, EVTNode);
2866 Ops.push_back(LHS);
2867 AddToWorkList(LHS.Val);
2868 std::vector<SDOperand> ZeroOps(NumElts, DAG.getConstant(0, EVT));
2869 ZeroOps.push_back(NumEltsNode);
2870 ZeroOps.push_back(EVTNode);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002871 Ops.push_back(DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
2872 &ZeroOps[0], ZeroOps.size()));
Evan Cheng44f1f092006-04-20 08:56:16 +00002873 IdxOps.push_back(NumEltsNode);
2874 IdxOps.push_back(EVTNode);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002875 Ops.push_back(DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
2876 &IdxOps[0], IdxOps.size()));
Evan Cheng44f1f092006-04-20 08:56:16 +00002877 Ops.push_back(NumEltsNode);
2878 Ops.push_back(EVTNode);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002879 SDOperand Result = DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector,
2880 &Ops[0], Ops.size());
Evan Cheng44f1f092006-04-20 08:56:16 +00002881 if (NumEltsNode != DstVecSize || EVTNode != DstVecEVT) {
2882 Result = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Result,
2883 DstVecSize, DstVecEVT);
2884 }
2885 return Result;
2886 }
2887 }
2888 return SDOperand();
2889}
2890
Chris Lattneredab1b92006-04-02 03:25:57 +00002891/// visitVBinOp - Visit a binary vector operation, like VADD. IntOp indicates
2892/// the scalar operation of the vop if it is operating on an integer vector
2893/// (e.g. ADD) and FPOp indicates the FP version (e.g. FADD).
2894SDOperand DAGCombiner::visitVBinOp(SDNode *N, ISD::NodeType IntOp,
2895 ISD::NodeType FPOp) {
2896 MVT::ValueType EltType = cast<VTSDNode>(*(N->op_end()-1))->getVT();
2897 ISD::NodeType ScalarOp = MVT::isInteger(EltType) ? IntOp : FPOp;
2898 SDOperand LHS = N->getOperand(0);
2899 SDOperand RHS = N->getOperand(1);
Evan Cheng44f1f092006-04-20 08:56:16 +00002900 SDOperand Shuffle = XformToShuffleWithZero(N);
2901 if (Shuffle.Val) return Shuffle;
2902
Chris Lattneredab1b92006-04-02 03:25:57 +00002903 // If the LHS and RHS are VBUILD_VECTOR nodes, see if we can constant fold
2904 // this operation.
2905 if (LHS.getOpcode() == ISD::VBUILD_VECTOR &&
2906 RHS.getOpcode() == ISD::VBUILD_VECTOR) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002907 SmallVector<SDOperand, 8> Ops;
Chris Lattneredab1b92006-04-02 03:25:57 +00002908 for (unsigned i = 0, e = LHS.getNumOperands()-2; i != e; ++i) {
2909 SDOperand LHSOp = LHS.getOperand(i);
2910 SDOperand RHSOp = RHS.getOperand(i);
2911 // If these two elements can't be folded, bail out.
2912 if ((LHSOp.getOpcode() != ISD::UNDEF &&
2913 LHSOp.getOpcode() != ISD::Constant &&
2914 LHSOp.getOpcode() != ISD::ConstantFP) ||
2915 (RHSOp.getOpcode() != ISD::UNDEF &&
2916 RHSOp.getOpcode() != ISD::Constant &&
2917 RHSOp.getOpcode() != ISD::ConstantFP))
2918 break;
Evan Cheng7b336a82006-05-31 06:08:35 +00002919 // Can't fold divide by zero.
2920 if (N->getOpcode() == ISD::VSDIV || N->getOpcode() == ISD::VUDIV) {
2921 if ((RHSOp.getOpcode() == ISD::Constant &&
2922 cast<ConstantSDNode>(RHSOp.Val)->isNullValue()) ||
2923 (RHSOp.getOpcode() == ISD::ConstantFP &&
2924 !cast<ConstantFPSDNode>(RHSOp.Val)->getValue()))
2925 break;
2926 }
Chris Lattneredab1b92006-04-02 03:25:57 +00002927 Ops.push_back(DAG.getNode(ScalarOp, EltType, LHSOp, RHSOp));
Chris Lattner3e104b12006-04-08 04:15:24 +00002928 AddToWorkList(Ops.back().Val);
Chris Lattneredab1b92006-04-02 03:25:57 +00002929 assert((Ops.back().getOpcode() == ISD::UNDEF ||
2930 Ops.back().getOpcode() == ISD::Constant ||
2931 Ops.back().getOpcode() == ISD::ConstantFP) &&
2932 "Scalar binop didn't fold!");
2933 }
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00002934
2935 if (Ops.size() == LHS.getNumOperands()-2) {
2936 Ops.push_back(*(LHS.Val->op_end()-2));
2937 Ops.push_back(*(LHS.Val->op_end()-1));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002938 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00002939 }
Chris Lattneredab1b92006-04-02 03:25:57 +00002940 }
2941
2942 return SDOperand();
2943}
2944
Nate Begeman44728a72005-09-19 22:34:01 +00002945SDOperand DAGCombiner::SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2){
Nate Begemanf845b452005-10-08 00:29:44 +00002946 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
2947
2948 SDOperand SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
2949 cast<CondCodeSDNode>(N0.getOperand(2))->get());
2950 // If we got a simplified select_cc node back from SimplifySelectCC, then
2951 // break it down into a new SETCC node, and a new SELECT node, and then return
2952 // the SELECT node, since we were called with a SELECT node.
2953 if (SCC.Val) {
2954 // Check to see if we got a select_cc back (to turn into setcc/select).
2955 // Otherwise, just return whatever node we got back, like fabs.
2956 if (SCC.getOpcode() == ISD::SELECT_CC) {
2957 SDOperand SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
2958 SCC.getOperand(0), SCC.getOperand(1),
2959 SCC.getOperand(4));
Chris Lattner5750df92006-03-01 04:03:14 +00002960 AddToWorkList(SETCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00002961 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
2962 SCC.getOperand(3), SETCC);
2963 }
2964 return SCC;
2965 }
Nate Begeman44728a72005-09-19 22:34:01 +00002966 return SDOperand();
2967}
2968
Chris Lattner40c62d52005-10-18 06:04:22 +00002969/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
2970/// are the two values being selected between, see if we can simplify the
Chris Lattner729c6d12006-05-27 00:43:02 +00002971/// select. Callers of this should assume that TheSelect is deleted if this
2972/// returns true. As such, they should return the appropriate thing (e.g. the
2973/// node) back to the top-level of the DAG combiner loop to avoid it being
2974/// looked at.
Chris Lattner40c62d52005-10-18 06:04:22 +00002975///
2976bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDOperand LHS,
2977 SDOperand RHS) {
2978
2979 // If this is a select from two identical things, try to pull the operation
2980 // through the select.
2981 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
2982#if 0
2983 std::cerr << "SELECT: ["; LHS.Val->dump();
2984 std::cerr << "] ["; RHS.Val->dump();
2985 std::cerr << "]\n";
2986#endif
2987
2988 // If this is a load and the token chain is identical, replace the select
2989 // of two loads with a load through a select of the address to load from.
2990 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
2991 // constants have been dropped into the constant pool.
2992 if ((LHS.getOpcode() == ISD::LOAD ||
2993 LHS.getOpcode() == ISD::EXTLOAD ||
2994 LHS.getOpcode() == ISD::ZEXTLOAD ||
2995 LHS.getOpcode() == ISD::SEXTLOAD) &&
2996 // Token chains must be identical.
2997 LHS.getOperand(0) == RHS.getOperand(0) &&
2998 // If this is an EXTLOAD, the VT's must match.
2999 (LHS.getOpcode() == ISD::LOAD ||
3000 LHS.getOperand(3) == RHS.getOperand(3))) {
3001 // FIXME: this conflates two src values, discarding one. This is not
3002 // the right thing to do, but nothing uses srcvalues now. When they do,
3003 // turn SrcValue into a list of locations.
3004 SDOperand Addr;
3005 if (TheSelect->getOpcode() == ISD::SELECT)
3006 Addr = DAG.getNode(ISD::SELECT, LHS.getOperand(1).getValueType(),
3007 TheSelect->getOperand(0), LHS.getOperand(1),
3008 RHS.getOperand(1));
3009 else
3010 Addr = DAG.getNode(ISD::SELECT_CC, LHS.getOperand(1).getValueType(),
3011 TheSelect->getOperand(0),
3012 TheSelect->getOperand(1),
3013 LHS.getOperand(1), RHS.getOperand(1),
3014 TheSelect->getOperand(4));
3015
3016 SDOperand Load;
3017 if (LHS.getOpcode() == ISD::LOAD)
3018 Load = DAG.getLoad(TheSelect->getValueType(0), LHS.getOperand(0),
3019 Addr, LHS.getOperand(2));
3020 else
3021 Load = DAG.getExtLoad(LHS.getOpcode(), TheSelect->getValueType(0),
3022 LHS.getOperand(0), Addr, LHS.getOperand(2),
3023 cast<VTSDNode>(LHS.getOperand(3))->getVT());
3024 // Users of the select now use the result of the load.
3025 CombineTo(TheSelect, Load);
3026
3027 // Users of the old loads now use the new load's chain. We know the
3028 // old-load value is dead now.
3029 CombineTo(LHS.Val, Load.getValue(0), Load.getValue(1));
3030 CombineTo(RHS.Val, Load.getValue(0), Load.getValue(1));
3031 return true;
3032 }
3033 }
3034
3035 return false;
3036}
3037
Nate Begeman44728a72005-09-19 22:34:01 +00003038SDOperand DAGCombiner::SimplifySelectCC(SDOperand N0, SDOperand N1,
3039 SDOperand N2, SDOperand N3,
3040 ISD::CondCode CC) {
Nate Begemanf845b452005-10-08 00:29:44 +00003041
3042 MVT::ValueType VT = N2.getValueType();
Chris Lattner5eed34d2006-05-12 17:57:54 +00003043 //ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003044 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
3045 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
3046 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
3047
3048 // Determine if the condition we're dealing with is constant
3049 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
3050 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
3051
3052 // fold select_cc true, x, y -> x
3053 if (SCCC && SCCC->getValue())
3054 return N2;
3055 // fold select_cc false, x, y -> y
3056 if (SCCC && SCCC->getValue() == 0)
3057 return N3;
3058
3059 // Check to see if we can simplify the select into an fabs node
3060 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
3061 // Allow either -0.0 or 0.0
3062 if (CFP->getValue() == 0.0) {
3063 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
3064 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
3065 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
3066 N2 == N3.getOperand(0))
3067 return DAG.getNode(ISD::FABS, VT, N0);
3068
3069 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
3070 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
3071 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
3072 N2.getOperand(0) == N3)
3073 return DAG.getNode(ISD::FABS, VT, N3);
3074 }
3075 }
3076
3077 // Check to see if we can perform the "gzip trick", transforming
3078 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
3079 if (N1C && N1C->isNullValue() && N3C && N3C->isNullValue() &&
3080 MVT::isInteger(N0.getValueType()) &&
3081 MVT::isInteger(N2.getValueType()) && CC == ISD::SETLT) {
3082 MVT::ValueType XType = N0.getValueType();
3083 MVT::ValueType AType = N2.getValueType();
3084 if (XType >= AType) {
3085 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00003086 // single-bit constant.
Nate Begemanf845b452005-10-08 00:29:44 +00003087 if (N2C && ((N2C->getValue() & (N2C->getValue()-1)) == 0)) {
3088 unsigned ShCtV = Log2_64(N2C->getValue());
3089 ShCtV = MVT::getSizeInBits(XType)-ShCtV-1;
3090 SDOperand ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
3091 SDOperand Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
Chris Lattner5750df92006-03-01 04:03:14 +00003092 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003093 if (XType > AType) {
3094 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00003095 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003096 }
3097 return DAG.getNode(ISD::AND, AType, Shift, N2);
3098 }
3099 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
3100 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3101 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00003102 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003103 if (XType > AType) {
3104 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00003105 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003106 }
3107 return DAG.getNode(ISD::AND, AType, Shift, N2);
3108 }
3109 }
Nate Begeman07ed4172005-10-10 21:26:48 +00003110
3111 // fold select C, 16, 0 -> shl C, 4
3112 if (N2C && N3C && N3C->isNullValue() && isPowerOf2_64(N2C->getValue()) &&
3113 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) {
3114 // Get a SetCC of the condition
3115 // FIXME: Should probably make sure that setcc is legal if we ever have a
3116 // target where it isn't.
Nate Begemanb0d04a72006-02-18 02:40:58 +00003117 SDOperand Temp, SCC;
Nate Begeman07ed4172005-10-10 21:26:48 +00003118 // cast from setcc result type to select result type
Nate Begemanb0d04a72006-02-18 02:40:58 +00003119 if (AfterLegalize) {
3120 SCC = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00003121 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
Nate Begemanb0d04a72006-02-18 02:40:58 +00003122 } else {
3123 SCC = DAG.getSetCC(MVT::i1, N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00003124 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00003125 }
Chris Lattner5750df92006-03-01 04:03:14 +00003126 AddToWorkList(SCC.Val);
3127 AddToWorkList(Temp.Val);
Nate Begeman07ed4172005-10-10 21:26:48 +00003128 // shl setcc result by log2 n2c
3129 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
3130 DAG.getConstant(Log2_64(N2C->getValue()),
3131 TLI.getShiftAmountTy()));
3132 }
3133
Nate Begemanf845b452005-10-08 00:29:44 +00003134 // Check to see if this is the equivalent of setcc
3135 // FIXME: Turn all of these into setcc if setcc if setcc is legal
3136 // otherwise, go ahead with the folds.
3137 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getValue() == 1ULL)) {
3138 MVT::ValueType XType = N0.getValueType();
3139 if (TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultTy())) {
3140 SDOperand Res = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
3141 if (Res.getValueType() != VT)
3142 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
3143 return Res;
3144 }
3145
3146 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
3147 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
3148 TLI.isOperationLegal(ISD::CTLZ, XType)) {
3149 SDOperand Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
3150 return DAG.getNode(ISD::SRL, XType, Ctlz,
3151 DAG.getConstant(Log2_32(MVT::getSizeInBits(XType)),
3152 TLI.getShiftAmountTy()));
3153 }
3154 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
3155 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
3156 SDOperand NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
3157 N0);
3158 SDOperand NotN0 = DAG.getNode(ISD::XOR, XType, N0,
3159 DAG.getConstant(~0ULL, XType));
3160 return DAG.getNode(ISD::SRL, XType,
3161 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
3162 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3163 TLI.getShiftAmountTy()));
3164 }
3165 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
3166 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
3167 SDOperand Sign = DAG.getNode(ISD::SRL, XType, N0,
3168 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3169 TLI.getShiftAmountTy()));
3170 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
3171 }
3172 }
3173
3174 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
3175 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
3176 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
3177 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1)) {
3178 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N2.getOperand(0))) {
3179 MVT::ValueType XType = N0.getValueType();
3180 if (SubC->isNullValue() && MVT::isInteger(XType)) {
3181 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
3182 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3183 TLI.getShiftAmountTy()));
3184 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00003185 AddToWorkList(Shift.Val);
3186 AddToWorkList(Add.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003187 return DAG.getNode(ISD::XOR, XType, Add, Shift);
3188 }
3189 }
3190 }
3191
Nate Begeman44728a72005-09-19 22:34:01 +00003192 return SDOperand();
3193}
3194
Nate Begeman452d7be2005-09-16 00:54:12 +00003195SDOperand DAGCombiner::SimplifySetCC(MVT::ValueType VT, SDOperand N0,
Nate Begemane17daeb2005-10-05 21:43:42 +00003196 SDOperand N1, ISD::CondCode Cond,
3197 bool foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00003198 // These setcc operations always fold.
3199 switch (Cond) {
3200 default: break;
3201 case ISD::SETFALSE:
3202 case ISD::SETFALSE2: return DAG.getConstant(0, VT);
3203 case ISD::SETTRUE:
3204 case ISD::SETTRUE2: return DAG.getConstant(1, VT);
3205 }
3206
3207 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
3208 uint64_t C1 = N1C->getValue();
3209 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val)) {
3210 uint64_t C0 = N0C->getValue();
3211
3212 // Sign extend the operands if required
3213 if (ISD::isSignedIntSetCC(Cond)) {
3214 C0 = N0C->getSignExtended();
3215 C1 = N1C->getSignExtended();
3216 }
3217
3218 switch (Cond) {
3219 default: assert(0 && "Unknown integer setcc!");
3220 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
3221 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
3222 case ISD::SETULT: return DAG.getConstant(C0 < C1, VT);
3223 case ISD::SETUGT: return DAG.getConstant(C0 > C1, VT);
3224 case ISD::SETULE: return DAG.getConstant(C0 <= C1, VT);
3225 case ISD::SETUGE: return DAG.getConstant(C0 >= C1, VT);
3226 case ISD::SETLT: return DAG.getConstant((int64_t)C0 < (int64_t)C1, VT);
3227 case ISD::SETGT: return DAG.getConstant((int64_t)C0 > (int64_t)C1, VT);
3228 case ISD::SETLE: return DAG.getConstant((int64_t)C0 <= (int64_t)C1, VT);
3229 case ISD::SETGE: return DAG.getConstant((int64_t)C0 >= (int64_t)C1, VT);
3230 }
3231 } else {
3232 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
3233 if (N0.getOpcode() == ISD::ZERO_EXTEND) {
3234 unsigned InSize = MVT::getSizeInBits(N0.getOperand(0).getValueType());
3235
3236 // If the comparison constant has bits in the upper part, the
3237 // zero-extended value could never match.
3238 if (C1 & (~0ULL << InSize)) {
3239 unsigned VSize = MVT::getSizeInBits(N0.getValueType());
3240 switch (Cond) {
3241 case ISD::SETUGT:
3242 case ISD::SETUGE:
3243 case ISD::SETEQ: return DAG.getConstant(0, VT);
3244 case ISD::SETULT:
3245 case ISD::SETULE:
3246 case ISD::SETNE: return DAG.getConstant(1, VT);
3247 case ISD::SETGT:
3248 case ISD::SETGE:
3249 // True if the sign bit of C1 is set.
3250 return DAG.getConstant((C1 & (1ULL << VSize)) != 0, VT);
3251 case ISD::SETLT:
3252 case ISD::SETLE:
3253 // True if the sign bit of C1 isn't set.
3254 return DAG.getConstant((C1 & (1ULL << VSize)) == 0, VT);
3255 default:
3256 break;
3257 }
3258 }
3259
3260 // Otherwise, we can perform the comparison with the low bits.
3261 switch (Cond) {
3262 case ISD::SETEQ:
3263 case ISD::SETNE:
3264 case ISD::SETUGT:
3265 case ISD::SETUGE:
3266 case ISD::SETULT:
3267 case ISD::SETULE:
3268 return DAG.getSetCC(VT, N0.getOperand(0),
3269 DAG.getConstant(C1, N0.getOperand(0).getValueType()),
3270 Cond);
3271 default:
3272 break; // todo, be more careful with signed comparisons
3273 }
3274 } else if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
3275 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
3276 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N0.getOperand(1))->getVT();
3277 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
3278 MVT::ValueType ExtDstTy = N0.getValueType();
3279 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
3280
3281 // If the extended part has any inconsistent bits, it cannot ever
3282 // compare equal. In other words, they have to be all ones or all
3283 // zeros.
3284 uint64_t ExtBits =
3285 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
3286 if ((C1 & ExtBits) != 0 && (C1 & ExtBits) != ExtBits)
3287 return DAG.getConstant(Cond == ISD::SETNE, VT);
3288
3289 SDOperand ZextOp;
3290 MVT::ValueType Op0Ty = N0.getOperand(0).getValueType();
3291 if (Op0Ty == ExtSrcTy) {
3292 ZextOp = N0.getOperand(0);
3293 } else {
3294 int64_t Imm = ~0ULL >> (64-ExtSrcTyBits);
3295 ZextOp = DAG.getNode(ISD::AND, Op0Ty, N0.getOperand(0),
3296 DAG.getConstant(Imm, Op0Ty));
3297 }
Chris Lattner5750df92006-03-01 04:03:14 +00003298 AddToWorkList(ZextOp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003299 // Otherwise, make this a use of a zext.
3300 return DAG.getSetCC(VT, ZextOp,
3301 DAG.getConstant(C1 & (~0ULL>>(64-ExtSrcTyBits)),
3302 ExtDstTy),
3303 Cond);
Chris Lattner3391bcd2006-02-08 02:13:15 +00003304 } else if ((N1C->getValue() == 0 || N1C->getValue() == 1) &&
3305 (Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
3306 (N0.getOpcode() == ISD::XOR ||
3307 (N0.getOpcode() == ISD::AND &&
3308 N0.getOperand(0).getOpcode() == ISD::XOR &&
3309 N0.getOperand(1) == N0.getOperand(0).getOperand(1))) &&
3310 isa<ConstantSDNode>(N0.getOperand(1)) &&
3311 cast<ConstantSDNode>(N0.getOperand(1))->getValue() == 1) {
3312 // If this is (X^1) == 0/1, swap the RHS and eliminate the xor. We can
3313 // only do this if the top bits are known zero.
3314 if (TLI.MaskedValueIsZero(N1,
3315 MVT::getIntVTBitMask(N0.getValueType())-1)) {
3316 // Okay, get the un-inverted input value.
3317 SDOperand Val;
3318 if (N0.getOpcode() == ISD::XOR)
3319 Val = N0.getOperand(0);
3320 else {
3321 assert(N0.getOpcode() == ISD::AND &&
3322 N0.getOperand(0).getOpcode() == ISD::XOR);
3323 // ((X^1)&1)^1 -> X & 1
3324 Val = DAG.getNode(ISD::AND, N0.getValueType(),
3325 N0.getOperand(0).getOperand(0), N0.getOperand(1));
3326 }
3327 return DAG.getSetCC(VT, Val, N1,
3328 Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ);
3329 }
Nate Begeman452d7be2005-09-16 00:54:12 +00003330 }
Chris Lattner5c46f742005-10-05 06:11:08 +00003331
Nate Begeman452d7be2005-09-16 00:54:12 +00003332 uint64_t MinVal, MaxVal;
3333 unsigned OperandBitSize = MVT::getSizeInBits(N1C->getValueType(0));
3334 if (ISD::isSignedIntSetCC(Cond)) {
3335 MinVal = 1ULL << (OperandBitSize-1);
3336 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
3337 MaxVal = ~0ULL >> (65-OperandBitSize);
3338 else
3339 MaxVal = 0;
3340 } else {
3341 MinVal = 0;
3342 MaxVal = ~0ULL >> (64-OperandBitSize);
3343 }
3344
3345 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
3346 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
3347 if (C1 == MinVal) return DAG.getConstant(1, VT); // X >= MIN --> true
3348 --C1; // X >= C0 --> X > (C0-1)
3349 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
3350 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
3351 }
3352
3353 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
3354 if (C1 == MaxVal) return DAG.getConstant(1, VT); // X <= MAX --> true
3355 ++C1; // X <= C0 --> X < (C0+1)
3356 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
3357 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
3358 }
3359
3360 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal)
3361 return DAG.getConstant(0, VT); // X < MIN --> false
3362
3363 // Canonicalize setgt X, Min --> setne X, Min
3364 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MinVal)
3365 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
Chris Lattnerc8597ca2005-10-21 21:23:25 +00003366 // Canonicalize setlt X, Max --> setne X, Max
3367 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MaxVal)
3368 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
Nate Begeman452d7be2005-09-16 00:54:12 +00003369
3370 // If we have setult X, 1, turn it into seteq X, 0
3371 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal+1)
3372 return DAG.getSetCC(VT, N0, DAG.getConstant(MinVal, N0.getValueType()),
3373 ISD::SETEQ);
3374 // If we have setugt X, Max-1, turn it into seteq X, Max
3375 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal-1)
3376 return DAG.getSetCC(VT, N0, DAG.getConstant(MaxVal, N0.getValueType()),
3377 ISD::SETEQ);
3378
3379 // If we have "setcc X, C0", check to see if we can shrink the immediate
3380 // by changing cc.
3381
3382 // SETUGT X, SINTMAX -> SETLT X, 0
3383 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
3384 C1 == (~0ULL >> (65-OperandBitSize)))
3385 return DAG.getSetCC(VT, N0, DAG.getConstant(0, N1.getValueType()),
3386 ISD::SETLT);
3387
3388 // FIXME: Implement the rest of these.
3389
3390 // Fold bit comparisons when we can.
3391 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
3392 VT == N0.getValueType() && N0.getOpcode() == ISD::AND)
3393 if (ConstantSDNode *AndRHS =
3394 dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
3395 if (Cond == ISD::SETNE && C1 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
3396 // Perform the xform if the AND RHS is a single bit.
3397 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
3398 return DAG.getNode(ISD::SRL, VT, N0,
3399 DAG.getConstant(Log2_64(AndRHS->getValue()),
3400 TLI.getShiftAmountTy()));
3401 }
3402 } else if (Cond == ISD::SETEQ && C1 == AndRHS->getValue()) {
3403 // (X & 8) == 8 --> (X & 8) >> 3
3404 // Perform the xform if C1 is a single bit.
3405 if ((C1 & (C1-1)) == 0) {
3406 return DAG.getNode(ISD::SRL, VT, N0,
Chris Lattner729c6d12006-05-27 00:43:02 +00003407 DAG.getConstant(Log2_64(C1),TLI.getShiftAmountTy()));
Nate Begeman452d7be2005-09-16 00:54:12 +00003408 }
3409 }
3410 }
3411 }
3412 } else if (isa<ConstantSDNode>(N0.Val)) {
3413 // Ensure that the constant occurs on the RHS.
3414 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
3415 }
3416
3417 if (ConstantFPSDNode *N0C = dyn_cast<ConstantFPSDNode>(N0.Val))
3418 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) {
3419 double C0 = N0C->getValue(), C1 = N1C->getValue();
3420
3421 switch (Cond) {
3422 default: break; // FIXME: Implement the rest of these!
3423 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
3424 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
3425 case ISD::SETLT: return DAG.getConstant(C0 < C1, VT);
3426 case ISD::SETGT: return DAG.getConstant(C0 > C1, VT);
3427 case ISD::SETLE: return DAG.getConstant(C0 <= C1, VT);
3428 case ISD::SETGE: return DAG.getConstant(C0 >= C1, VT);
3429 }
3430 } else {
3431 // Ensure that the constant occurs on the RHS.
3432 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
3433 }
3434
3435 if (N0 == N1) {
3436 // We can always fold X == Y for integer setcc's.
3437 if (MVT::isInteger(N0.getValueType()))
3438 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
3439 unsigned UOF = ISD::getUnorderedFlavor(Cond);
3440 if (UOF == 2) // FP operators that are undefined on NaNs.
3441 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
3442 if (UOF == unsigned(ISD::isTrueWhenEqual(Cond)))
3443 return DAG.getConstant(UOF, VT);
3444 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
3445 // if it is not already.
Chris Lattner4090aee2006-01-18 19:13:41 +00003446 ISD::CondCode NewCond = UOF == 0 ? ISD::SETO : ISD::SETUO;
Nate Begeman452d7be2005-09-16 00:54:12 +00003447 if (NewCond != Cond)
3448 return DAG.getSetCC(VT, N0, N1, NewCond);
3449 }
3450
3451 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
3452 MVT::isInteger(N0.getValueType())) {
3453 if (N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::SUB ||
3454 N0.getOpcode() == ISD::XOR) {
3455 // Simplify (X+Y) == (X+Z) --> Y == Z
3456 if (N0.getOpcode() == N1.getOpcode()) {
3457 if (N0.getOperand(0) == N1.getOperand(0))
3458 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(1), Cond);
3459 if (N0.getOperand(1) == N1.getOperand(1))
3460 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(0), Cond);
3461 if (isCommutativeBinOp(N0.getOpcode())) {
3462 // If X op Y == Y op X, try other combinations.
3463 if (N0.getOperand(0) == N1.getOperand(1))
3464 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(0), Cond);
3465 if (N0.getOperand(1) == N1.getOperand(0))
Chris Lattnera158eee2005-10-25 18:57:30 +00003466 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(1), Cond);
Nate Begeman452d7be2005-09-16 00:54:12 +00003467 }
3468 }
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003469
3470 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(N1)) {
3471 if (ConstantSDNode *LHSR = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
3472 // Turn (X+C1) == C2 --> X == C2-C1
3473 if (N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse()) {
3474 return DAG.getSetCC(VT, N0.getOperand(0),
3475 DAG.getConstant(RHSC->getValue()-LHSR->getValue(),
3476 N0.getValueType()), Cond);
3477 }
3478
3479 // Turn (X^C1) == C2 into X == C1^C2 iff X&~C1 = 0.
3480 if (N0.getOpcode() == ISD::XOR)
Chris Lattner5c46f742005-10-05 06:11:08 +00003481 // If we know that all of the inverted bits are zero, don't bother
3482 // performing the inversion.
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003483 if (TLI.MaskedValueIsZero(N0.getOperand(0), ~LHSR->getValue()))
Chris Lattner5c46f742005-10-05 06:11:08 +00003484 return DAG.getSetCC(VT, N0.getOperand(0),
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003485 DAG.getConstant(LHSR->getValue()^RHSC->getValue(),
Chris Lattner5c46f742005-10-05 06:11:08 +00003486 N0.getValueType()), Cond);
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003487 }
3488
3489 // Turn (C1-X) == C2 --> X == C1-C2
3490 if (ConstantSDNode *SUBC = dyn_cast<ConstantSDNode>(N0.getOperand(0))) {
3491 if (N0.getOpcode() == ISD::SUB && N0.Val->hasOneUse()) {
3492 return DAG.getSetCC(VT, N0.getOperand(1),
3493 DAG.getConstant(SUBC->getValue()-RHSC->getValue(),
3494 N0.getValueType()), Cond);
Chris Lattner5c46f742005-10-05 06:11:08 +00003495 }
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003496 }
3497 }
3498
Nate Begeman452d7be2005-09-16 00:54:12 +00003499 // Simplify (X+Z) == X --> Z == 0
3500 if (N0.getOperand(0) == N1)
3501 return DAG.getSetCC(VT, N0.getOperand(1),
3502 DAG.getConstant(0, N0.getValueType()), Cond);
3503 if (N0.getOperand(1) == N1) {
3504 if (isCommutativeBinOp(N0.getOpcode()))
3505 return DAG.getSetCC(VT, N0.getOperand(0),
3506 DAG.getConstant(0, N0.getValueType()), Cond);
3507 else {
3508 assert(N0.getOpcode() == ISD::SUB && "Unexpected operation!");
3509 // (Z-X) == X --> Z == X<<1
3510 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(),
3511 N1,
3512 DAG.getConstant(1,TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00003513 AddToWorkList(SH.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003514 return DAG.getSetCC(VT, N0.getOperand(0), SH, Cond);
3515 }
3516 }
3517 }
3518
3519 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
3520 N1.getOpcode() == ISD::XOR) {
3521 // Simplify X == (X+Z) --> Z == 0
3522 if (N1.getOperand(0) == N0) {
3523 return DAG.getSetCC(VT, N1.getOperand(1),
3524 DAG.getConstant(0, N1.getValueType()), Cond);
3525 } else if (N1.getOperand(1) == N0) {
3526 if (isCommutativeBinOp(N1.getOpcode())) {
3527 return DAG.getSetCC(VT, N1.getOperand(0),
3528 DAG.getConstant(0, N1.getValueType()), Cond);
3529 } else {
3530 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
3531 // X == (Z-X) --> X<<1 == Z
3532 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(), N0,
3533 DAG.getConstant(1,TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00003534 AddToWorkList(SH.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003535 return DAG.getSetCC(VT, SH, N1.getOperand(0), Cond);
3536 }
3537 }
3538 }
3539 }
3540
3541 // Fold away ALL boolean setcc's.
3542 SDOperand Temp;
Nate Begemane17daeb2005-10-05 21:43:42 +00003543 if (N0.getValueType() == MVT::i1 && foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00003544 switch (Cond) {
3545 default: assert(0 && "Unknown integer setcc!");
3546 case ISD::SETEQ: // X == Y -> (X^Y)^1
3547 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
3548 N0 = DAG.getNode(ISD::XOR, MVT::i1, Temp, DAG.getConstant(1, MVT::i1));
Chris Lattner5750df92006-03-01 04:03:14 +00003549 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003550 break;
3551 case ISD::SETNE: // X != Y --> (X^Y)
3552 N0 = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
3553 break;
3554 case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> X^1 & Y
3555 case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> X^1 & Y
3556 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
3557 N0 = DAG.getNode(ISD::AND, MVT::i1, N1, Temp);
Chris Lattner5750df92006-03-01 04:03:14 +00003558 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003559 break;
3560 case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> Y^1 & X
3561 case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> Y^1 & X
3562 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
3563 N0 = DAG.getNode(ISD::AND, MVT::i1, N0, Temp);
Chris Lattner5750df92006-03-01 04:03:14 +00003564 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003565 break;
3566 case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> X^1 | Y
3567 case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> X^1 | Y
3568 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
3569 N0 = DAG.getNode(ISD::OR, MVT::i1, N1, Temp);
Chris Lattner5750df92006-03-01 04:03:14 +00003570 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003571 break;
3572 case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> Y^1 | X
3573 case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> Y^1 | X
3574 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
3575 N0 = DAG.getNode(ISD::OR, MVT::i1, N0, Temp);
3576 break;
3577 }
3578 if (VT != MVT::i1) {
Chris Lattner5750df92006-03-01 04:03:14 +00003579 AddToWorkList(N0.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003580 // FIXME: If running after legalize, we probably can't do this.
3581 N0 = DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
3582 }
3583 return N0;
3584 }
3585
3586 // Could not fold it.
3587 return SDOperand();
3588}
3589
Nate Begeman69575232005-10-20 02:15:44 +00003590/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
3591/// return a DAG expression to select that will generate the same value by
3592/// multiplying by a magic number. See:
3593/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
3594SDOperand DAGCombiner::BuildSDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00003595 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00003596 SDOperand S = TLI.BuildSDIV(N, DAG, &Built);
3597
Andrew Lenharth232c9102006-06-12 16:07:18 +00003598 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00003599 ii != ee; ++ii)
3600 AddToWorkList(*ii);
3601 return S;
Nate Begeman69575232005-10-20 02:15:44 +00003602}
3603
3604/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
3605/// return a DAG expression to select that will generate the same value by
3606/// multiplying by a magic number. See:
3607/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
3608SDOperand DAGCombiner::BuildUDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00003609 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00003610 SDOperand S = TLI.BuildUDIV(N, DAG, &Built);
Nate Begeman69575232005-10-20 02:15:44 +00003611
Andrew Lenharth232c9102006-06-12 16:07:18 +00003612 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00003613 ii != ee; ++ii)
3614 AddToWorkList(*ii);
3615 return S;
Nate Begeman69575232005-10-20 02:15:44 +00003616}
3617
Nate Begeman1d4d4142005-09-01 00:19:25 +00003618// SelectionDAG::Combine - This is the entry point for the file.
3619//
Nate Begeman4ebd8052005-09-01 23:24:04 +00003620void SelectionDAG::Combine(bool RunningAfterLegalize) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003621 /// run - This is the main entry point to this class.
3622 ///
Nate Begeman4ebd8052005-09-01 23:24:04 +00003623 DAGCombiner(*this).Run(RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003624}