blob: 3e53ae0ed3fd0d0cf510e277ae1ceac60e786124 [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 Begeman646d7e22005-09-02 21:18:40 +000025// FIXME: divide by zero is currently left unfolded. do we want to turn this
26// into an undef?
Nate Begemanf845b452005-10-08 00:29:44 +000027// FIXME: select ne (select cc, 1, 0), 0, true, false -> select cc, true, false
Nate Begeman1d4d4142005-09-01 00:19:25 +000028//
29//===----------------------------------------------------------------------===//
30
31#define DEBUG_TYPE "dagcombine"
32#include "llvm/ADT/Statistic.h"
33#include "llvm/CodeGen/SelectionDAG.h"
Nate Begeman2300f552005-09-07 00:15:36 +000034#include "llvm/Support/Debug.h"
Nate Begeman1d4d4142005-09-01 00:19:25 +000035#include "llvm/Support/MathExtras.h"
36#include "llvm/Target/TargetLowering.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000037#include "llvm/Support/Compiler.h"
Jim Laskeyd1aed7a2006-09-21 16:28:59 +000038#include "llvm/Support/CommandLine.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");
Jim Laskeyd1aed7a2006-09-21 16:28:59 +000047
Nate Begeman1d4d4142005-09-01 00:19:25 +000048
Jim Laskeyd1aed7a2006-09-21 16:28:59 +000049static cl::opt<bool>
50 CombinerAA("combiner-alias-analysis", cl::Hidden,
51 cl::desc("Turn on alias analysis turning testing"));
Jim Laskeyd1aed7a2006-09-21 16:28:59 +000052
53class VISIBILITY_HIDDEN DAGCombiner {
Nate Begeman1d4d4142005-09-01 00:19:25 +000054 SelectionDAG &DAG;
55 TargetLowering &TLI;
Nate Begeman4ebd8052005-09-01 23:24:04 +000056 bool AfterLegalize;
Nate Begeman1d4d4142005-09-01 00:19:25 +000057
58 // Worklist of all of the nodes that need to be simplified.
59 std::vector<SDNode*> WorkList;
60
61 /// AddUsersToWorkList - When an instruction is simplified, add all users of
62 /// the instruction to the work lists because they might get more simplified
63 /// now.
64 ///
65 void AddUsersToWorkList(SDNode *N) {
66 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
Nate Begeman4ebd8052005-09-01 23:24:04 +000067 UI != UE; ++UI)
68 WorkList.push_back(*UI);
Nate Begeman1d4d4142005-09-01 00:19:25 +000069 }
70
71 /// removeFromWorkList - remove all instances of N from the worklist.
Chris Lattner5750df92006-03-01 04:03:14 +000072 ///
Nate Begeman1d4d4142005-09-01 00:19:25 +000073 void removeFromWorkList(SDNode *N) {
74 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), N),
75 WorkList.end());
76 }
77
Chris Lattner24664722006-03-01 04:53:38 +000078 public:
Chris Lattner5750df92006-03-01 04:03:14 +000079 void AddToWorkList(SDNode *N) {
80 WorkList.push_back(N);
81 }
82
Chris Lattner3577e382006-08-11 17:56:38 +000083 SDOperand CombineTo(SDNode *N, const SDOperand *To, unsigned NumTo) {
84 assert(N->getNumValues() == NumTo && "Broken CombineTo call!");
Chris Lattner87514ca2005-10-10 22:31:19 +000085 ++NodesCombined;
Chris Lattner01a22022005-10-10 22:04:48 +000086 DEBUG(std::cerr << "\nReplacing "; N->dump();
Evan Cheng60e8c712006-05-09 06:55:15 +000087 std::cerr << "\nWith: "; To[0].Val->dump(&DAG);
Chris Lattner3577e382006-08-11 17:56:38 +000088 std::cerr << " and " << NumTo-1 << " other values\n");
Chris Lattner01a22022005-10-10 22:04:48 +000089 std::vector<SDNode*> NowDead;
Chris Lattner3577e382006-08-11 17:56:38 +000090 DAG.ReplaceAllUsesWith(N, To, &NowDead);
Chris Lattner01a22022005-10-10 22:04:48 +000091
92 // Push the new nodes and any users onto the worklist
Chris Lattner3577e382006-08-11 17:56:38 +000093 for (unsigned i = 0, e = NumTo; i != e; ++i) {
Chris Lattner01a22022005-10-10 22:04:48 +000094 WorkList.push_back(To[i].Val);
95 AddUsersToWorkList(To[i].Val);
96 }
97
98 // Nodes can end up on the worklist more than once. Make sure we do
99 // not process a node that has been replaced.
100 removeFromWorkList(N);
101 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
102 removeFromWorkList(NowDead[i]);
103
104 // Finally, since the node is now dead, remove it from the graph.
105 DAG.DeleteNode(N);
106 return SDOperand(N, 0);
107 }
Nate Begeman368e18d2006-02-16 21:11:51 +0000108
Chris Lattner24664722006-03-01 04:53:38 +0000109 SDOperand CombineTo(SDNode *N, SDOperand Res) {
Chris Lattner3577e382006-08-11 17:56:38 +0000110 return CombineTo(N, &Res, 1);
Chris Lattner24664722006-03-01 04:53:38 +0000111 }
112
113 SDOperand CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1) {
Chris Lattner3577e382006-08-11 17:56:38 +0000114 SDOperand To[] = { Res0, Res1 };
115 return CombineTo(N, To, 2);
Chris Lattner24664722006-03-01 04:53:38 +0000116 }
117 private:
118
Chris Lattner012f2412006-02-17 21:58:01 +0000119 /// SimplifyDemandedBits - Check the specified integer node value to see if
Chris Lattnerb2742f42006-03-01 19:55:35 +0000120 /// it can be simplified or if things it uses can be simplified by bit
Chris Lattner012f2412006-02-17 21:58:01 +0000121 /// propagation. If so, return true.
122 bool SimplifyDemandedBits(SDOperand Op) {
Nate Begeman368e18d2006-02-16 21:11:51 +0000123 TargetLowering::TargetLoweringOpt TLO(DAG);
124 uint64_t KnownZero, KnownOne;
Chris Lattner012f2412006-02-17 21:58:01 +0000125 uint64_t Demanded = MVT::getIntVTBitMask(Op.getValueType());
126 if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO))
127 return false;
128
129 // Revisit the node.
130 WorkList.push_back(Op.Val);
131
132 // Replace the old value with the new one.
133 ++NodesCombined;
134 DEBUG(std::cerr << "\nReplacing "; TLO.Old.Val->dump();
Evan Cheng60e8c712006-05-09 06:55:15 +0000135 std::cerr << "\nWith: "; TLO.New.Val->dump(&DAG));
Chris Lattner012f2412006-02-17 21:58:01 +0000136
137 std::vector<SDNode*> NowDead;
138 DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New, NowDead);
139
Chris Lattner7d20d392006-02-20 06:51:04 +0000140 // Push the new node and any (possibly new) users onto the worklist.
Chris Lattner012f2412006-02-17 21:58:01 +0000141 WorkList.push_back(TLO.New.Val);
142 AddUsersToWorkList(TLO.New.Val);
143
144 // Nodes can end up on the worklist more than once. Make sure we do
145 // not process a node that has been replaced.
Chris Lattner012f2412006-02-17 21:58:01 +0000146 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
147 removeFromWorkList(NowDead[i]);
148
Chris Lattner7d20d392006-02-20 06:51:04 +0000149 // Finally, if the node is now dead, remove it from the graph. The node
150 // may not be dead if the replacement process recursively simplified to
151 // something else needing this node.
152 if (TLO.Old.Val->use_empty()) {
153 removeFromWorkList(TLO.Old.Val);
154 DAG.DeleteNode(TLO.Old.Val);
155 }
Chris Lattner012f2412006-02-17 21:58:01 +0000156 return true;
Nate Begeman368e18d2006-02-16 21:11:51 +0000157 }
Chris Lattner87514ca2005-10-10 22:31:19 +0000158
Nate Begeman1d4d4142005-09-01 00:19:25 +0000159 /// visit - call the node-specific routine that knows how to fold each
160 /// particular type of node.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000161 SDOperand visit(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000162
163 // Visitation implementation - Implement dag node combining for different
164 // node types. The semantics are as follows:
165 // Return Value:
Nate Begeman2300f552005-09-07 00:15:36 +0000166 // SDOperand.Val == 0 - No change was made
Chris Lattner01a22022005-10-10 22:04:48 +0000167 // SDOperand.Val == N - N was replaced, is dead, and is already handled.
Nate Begeman2300f552005-09-07 00:15:36 +0000168 // otherwise - N should be replaced by the returned Operand.
Nate Begeman1d4d4142005-09-01 00:19:25 +0000169 //
Nate Begeman83e75ec2005-09-06 04:43:02 +0000170 SDOperand visitTokenFactor(SDNode *N);
171 SDOperand visitADD(SDNode *N);
172 SDOperand visitSUB(SDNode *N);
173 SDOperand visitMUL(SDNode *N);
174 SDOperand visitSDIV(SDNode *N);
175 SDOperand visitUDIV(SDNode *N);
176 SDOperand visitSREM(SDNode *N);
177 SDOperand visitUREM(SDNode *N);
178 SDOperand visitMULHU(SDNode *N);
179 SDOperand visitMULHS(SDNode *N);
180 SDOperand visitAND(SDNode *N);
181 SDOperand visitOR(SDNode *N);
182 SDOperand visitXOR(SDNode *N);
Chris Lattneredab1b92006-04-02 03:25:57 +0000183 SDOperand visitVBinOp(SDNode *N, ISD::NodeType IntOp, ISD::NodeType FPOp);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000184 SDOperand visitSHL(SDNode *N);
185 SDOperand visitSRA(SDNode *N);
186 SDOperand visitSRL(SDNode *N);
187 SDOperand visitCTLZ(SDNode *N);
188 SDOperand visitCTTZ(SDNode *N);
189 SDOperand visitCTPOP(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000190 SDOperand visitSELECT(SDNode *N);
191 SDOperand visitSELECT_CC(SDNode *N);
192 SDOperand visitSETCC(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000193 SDOperand visitSIGN_EXTEND(SDNode *N);
194 SDOperand visitZERO_EXTEND(SDNode *N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000195 SDOperand visitANY_EXTEND(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000196 SDOperand visitSIGN_EXTEND_INREG(SDNode *N);
197 SDOperand visitTRUNCATE(SDNode *N);
Chris Lattner94683772005-12-23 05:30:37 +0000198 SDOperand visitBIT_CONVERT(SDNode *N);
Chris Lattner6258fb22006-04-02 02:53:43 +0000199 SDOperand visitVBIT_CONVERT(SDNode *N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000200 SDOperand visitFADD(SDNode *N);
201 SDOperand visitFSUB(SDNode *N);
202 SDOperand visitFMUL(SDNode *N);
203 SDOperand visitFDIV(SDNode *N);
204 SDOperand visitFREM(SDNode *N);
Chris Lattner12d83032006-03-05 05:30:57 +0000205 SDOperand visitFCOPYSIGN(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000206 SDOperand visitSINT_TO_FP(SDNode *N);
207 SDOperand visitUINT_TO_FP(SDNode *N);
208 SDOperand visitFP_TO_SINT(SDNode *N);
209 SDOperand visitFP_TO_UINT(SDNode *N);
210 SDOperand visitFP_ROUND(SDNode *N);
211 SDOperand visitFP_ROUND_INREG(SDNode *N);
212 SDOperand visitFP_EXTEND(SDNode *N);
213 SDOperand visitFNEG(SDNode *N);
214 SDOperand visitFABS(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000215 SDOperand visitBRCOND(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000216 SDOperand visitBR_CC(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000217 SDOperand visitLOAD(SDNode *N);
Chris Lattner29cd7db2006-03-31 18:10:41 +0000218 SDOperand visitXEXTLOAD(SDNode *N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000219 SDOperand visitSTORE(SDNode *N);
Chris Lattnerca242442006-03-19 01:27:56 +0000220 SDOperand visitINSERT_VECTOR_ELT(SDNode *N);
221 SDOperand visitVINSERT_VECTOR_ELT(SDNode *N);
Chris Lattnerd7648c82006-03-28 20:28:38 +0000222 SDOperand visitVBUILD_VECTOR(SDNode *N);
Chris Lattner66445d32006-03-28 22:11:53 +0000223 SDOperand visitVECTOR_SHUFFLE(SDNode *N);
Chris Lattnerf1d0c622006-03-31 22:16:43 +0000224 SDOperand visitVVECTOR_SHUFFLE(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000225
Evan Cheng44f1f092006-04-20 08:56:16 +0000226 SDOperand XformToShuffleWithZero(SDNode *N);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000227 SDOperand ReassociateOps(unsigned Opc, SDOperand LHS, SDOperand RHS);
228
Chris Lattner40c62d52005-10-18 06:04:22 +0000229 bool SimplifySelectOps(SDNode *SELECT, SDOperand LHS, SDOperand RHS);
Chris Lattner35e5c142006-05-05 05:51:50 +0000230 SDOperand SimplifyBinOpWithSameOpcodeHands(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000231 SDOperand SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2);
232 SDOperand SimplifySelectCC(SDOperand N0, SDOperand N1, SDOperand N2,
233 SDOperand N3, ISD::CondCode CC);
Nate Begeman452d7be2005-09-16 00:54:12 +0000234 SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N0, SDOperand N1,
Nate Begemane17daeb2005-10-05 21:43:42 +0000235 ISD::CondCode Cond, bool foldBooleans = true);
Chris Lattner6258fb22006-04-02 02:53:43 +0000236 SDOperand ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(SDNode *, MVT::ValueType);
Nate Begeman69575232005-10-20 02:15:44 +0000237 SDOperand BuildSDIV(SDNode *N);
Chris Lattner516b9622006-09-14 20:50:57 +0000238 SDOperand BuildUDIV(SDNode *N);
239 SDNode *MatchRotate(SDOperand LHS, SDOperand RHS);
Jim Laskeyd1aed7a2006-09-21 16:28:59 +0000240 bool isNotAlias(SDOperand Ptr1, SDOperand Ptr2);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000241public:
242 DAGCombiner(SelectionDAG &D)
Nate Begeman646d7e22005-09-02 21:18:40 +0000243 : DAG(D), TLI(D.getTargetLoweringInfo()), AfterLegalize(false) {}
Nate Begeman1d4d4142005-09-01 00:19:25 +0000244
245 /// Run - runs the dag combiner on all nodes in the work list
Nate Begeman4ebd8052005-09-01 23:24:04 +0000246 void Run(bool RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000247 };
248}
249
Chris Lattner24664722006-03-01 04:53:38 +0000250//===----------------------------------------------------------------------===//
251// TargetLowering::DAGCombinerInfo implementation
252//===----------------------------------------------------------------------===//
253
254void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) {
255 ((DAGCombiner*)DC)->AddToWorkList(N);
256}
257
258SDOperand TargetLowering::DAGCombinerInfo::
259CombineTo(SDNode *N, const std::vector<SDOperand> &To) {
Chris Lattner3577e382006-08-11 17:56:38 +0000260 return ((DAGCombiner*)DC)->CombineTo(N, &To[0], To.size());
Chris Lattner24664722006-03-01 04:53:38 +0000261}
262
263SDOperand TargetLowering::DAGCombinerInfo::
264CombineTo(SDNode *N, SDOperand Res) {
265 return ((DAGCombiner*)DC)->CombineTo(N, Res);
266}
267
268
269SDOperand TargetLowering::DAGCombinerInfo::
270CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1) {
271 return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1);
272}
273
274
275
276
277//===----------------------------------------------------------------------===//
278
279
Nate Begeman4ebd8052005-09-01 23:24:04 +0000280// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
281// that selects between the values 1 and 0, making it equivalent to a setcc.
Nate Begeman646d7e22005-09-02 21:18:40 +0000282// Also, set the incoming LHS, RHS, and CC references to the appropriate
283// nodes based on the type of node we are checking. This simplifies life a
284// bit for the callers.
285static bool isSetCCEquivalent(SDOperand N, SDOperand &LHS, SDOperand &RHS,
286 SDOperand &CC) {
287 if (N.getOpcode() == ISD::SETCC) {
288 LHS = N.getOperand(0);
289 RHS = N.getOperand(1);
290 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000291 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000292 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000293 if (N.getOpcode() == ISD::SELECT_CC &&
294 N.getOperand(2).getOpcode() == ISD::Constant &&
295 N.getOperand(3).getOpcode() == ISD::Constant &&
296 cast<ConstantSDNode>(N.getOperand(2))->getValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000297 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
298 LHS = N.getOperand(0);
299 RHS = N.getOperand(1);
300 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000301 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000302 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000303 return false;
304}
305
Nate Begeman99801192005-09-07 23:25:52 +0000306// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
307// one use. If this is true, it allows the users to invert the operation for
308// free when it is profitable to do so.
309static bool isOneUseSetCC(SDOperand N) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000310 SDOperand N0, N1, N2;
Nate Begeman646d7e22005-09-02 21:18:40 +0000311 if (isSetCCEquivalent(N, N0, N1, N2) && N.Val->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000312 return true;
313 return false;
314}
315
Nate Begemancd4d58c2006-02-03 06:46:56 +0000316SDOperand DAGCombiner::ReassociateOps(unsigned Opc, SDOperand N0, SDOperand N1){
317 MVT::ValueType VT = N0.getValueType();
318 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
319 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
320 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
321 if (isa<ConstantSDNode>(N1)) {
322 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(1), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000323 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000324 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(0));
325 } else if (N0.hasOneUse()) {
326 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(0), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000327 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000328 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(1));
329 }
330 }
331 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
332 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
333 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
334 if (isa<ConstantSDNode>(N0)) {
335 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(1), N0);
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, N1.getOperand(0));
338 } else if (N1.hasOneUse()) {
339 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(0), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000340 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000341 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(1));
342 }
343 }
344 return SDOperand();
345}
346
Nate Begeman4ebd8052005-09-01 23:24:04 +0000347void DAGCombiner::Run(bool RunningAfterLegalize) {
348 // set the instance variable, so that the various visit routines may use it.
349 AfterLegalize = RunningAfterLegalize;
350
Nate Begeman646d7e22005-09-02 21:18:40 +0000351 // Add all the dag nodes to the worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000352 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
353 E = DAG.allnodes_end(); I != E; ++I)
354 WorkList.push_back(I);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000355
Chris Lattner95038592005-10-05 06:35:28 +0000356 // Create a dummy node (which is not added to allnodes), that adds a reference
357 // to the root node, preventing it from being deleted, and tracking any
358 // changes of the root.
359 HandleSDNode Dummy(DAG.getRoot());
360
Chris Lattner24664722006-03-01 04:53:38 +0000361
362 /// DagCombineInfo - Expose the DAG combiner to the target combiner impls.
363 TargetLowering::DAGCombinerInfo
364 DagCombineInfo(DAG, !RunningAfterLegalize, this);
365
Nate Begeman1d4d4142005-09-01 00:19:25 +0000366 // while the worklist isn't empty, inspect the node on the end of it and
367 // try and combine it.
368 while (!WorkList.empty()) {
369 SDNode *N = WorkList.back();
370 WorkList.pop_back();
371
372 // If N has no uses, it is dead. Make sure to revisit all N's operands once
Chris Lattner95038592005-10-05 06:35:28 +0000373 // N is deleted from the DAG, since they too may now be dead or may have a
374 // reduced number of uses, allowing other xforms.
375 if (N->use_empty() && N != &Dummy) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000376 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
377 WorkList.push_back(N->getOperand(i).Val);
378
Nate Begeman1d4d4142005-09-01 00:19:25 +0000379 removeFromWorkList(N);
Chris Lattner95038592005-10-05 06:35:28 +0000380 DAG.DeleteNode(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000381 continue;
382 }
383
Nate Begeman83e75ec2005-09-06 04:43:02 +0000384 SDOperand RV = visit(N);
Chris Lattner24664722006-03-01 04:53:38 +0000385
386 // If nothing happened, try a target-specific DAG combine.
387 if (RV.Val == 0) {
Chris Lattner729c6d12006-05-27 00:43:02 +0000388 assert(N->getOpcode() != ISD::DELETED_NODE &&
389 "Node was deleted but visit returned NULL!");
Chris Lattner24664722006-03-01 04:53:38 +0000390 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
391 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode()))
392 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
393 }
394
Nate Begeman83e75ec2005-09-06 04:43:02 +0000395 if (RV.Val) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000396 ++NodesCombined;
Nate Begeman646d7e22005-09-02 21:18:40 +0000397 // If we get back the same node we passed in, rather than a new node or
398 // zero, we know that the node must have defined multiple values and
399 // CombineTo was used. Since CombineTo takes care of the worklist
400 // mechanics for us, we have no work to do in this case.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000401 if (RV.Val != N) {
Chris Lattner729c6d12006-05-27 00:43:02 +0000402 assert(N->getOpcode() != ISD::DELETED_NODE &&
403 RV.Val->getOpcode() != ISD::DELETED_NODE &&
404 "Node was deleted but visit returned new node!");
405
Nate Begeman2300f552005-09-07 00:15:36 +0000406 DEBUG(std::cerr << "\nReplacing "; N->dump();
Evan Cheng60e8c712006-05-09 06:55:15 +0000407 std::cerr << "\nWith: "; RV.Val->dump(&DAG);
Nate Begeman2300f552005-09-07 00:15:36 +0000408 std::cerr << '\n');
Chris Lattner01a22022005-10-10 22:04:48 +0000409 std::vector<SDNode*> NowDead;
Chris Lattnerb9ea4a32006-08-11 17:46:28 +0000410 SDOperand OpV = RV;
411 DAG.ReplaceAllUsesWith(N, &OpV, &NowDead);
Nate Begeman646d7e22005-09-02 21:18:40 +0000412
413 // Push the new node and any users onto the worklist
Nate Begeman83e75ec2005-09-06 04:43:02 +0000414 WorkList.push_back(RV.Val);
415 AddUsersToWorkList(RV.Val);
Nate Begeman646d7e22005-09-02 21:18:40 +0000416
417 // Nodes can end up on the worklist more than once. Make sure we do
418 // not process a node that has been replaced.
419 removeFromWorkList(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000420 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
421 removeFromWorkList(NowDead[i]);
Chris Lattner5c46f742005-10-05 06:11:08 +0000422
423 // Finally, since the node is now dead, remove it from the graph.
424 DAG.DeleteNode(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000425 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000426 }
427 }
Chris Lattner95038592005-10-05 06:35:28 +0000428
429 // If the root changed (e.g. it was a dead load, update the root).
430 DAG.setRoot(Dummy.getValue());
Nate Begeman1d4d4142005-09-01 00:19:25 +0000431}
432
Nate Begeman83e75ec2005-09-06 04:43:02 +0000433SDOperand DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000434 switch(N->getOpcode()) {
435 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +0000436 case ISD::TokenFactor: return visitTokenFactor(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000437 case ISD::ADD: return visitADD(N);
438 case ISD::SUB: return visitSUB(N);
439 case ISD::MUL: return visitMUL(N);
440 case ISD::SDIV: return visitSDIV(N);
441 case ISD::UDIV: return visitUDIV(N);
442 case ISD::SREM: return visitSREM(N);
443 case ISD::UREM: return visitUREM(N);
444 case ISD::MULHU: return visitMULHU(N);
445 case ISD::MULHS: return visitMULHS(N);
446 case ISD::AND: return visitAND(N);
447 case ISD::OR: return visitOR(N);
448 case ISD::XOR: return visitXOR(N);
449 case ISD::SHL: return visitSHL(N);
450 case ISD::SRA: return visitSRA(N);
451 case ISD::SRL: return visitSRL(N);
452 case ISD::CTLZ: return visitCTLZ(N);
453 case ISD::CTTZ: return visitCTTZ(N);
454 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000455 case ISD::SELECT: return visitSELECT(N);
456 case ISD::SELECT_CC: return visitSELECT_CC(N);
457 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000458 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
459 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000460 case ISD::ANY_EXTEND: return visitANY_EXTEND(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000461 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
462 case ISD::TRUNCATE: return visitTRUNCATE(N);
Chris Lattner94683772005-12-23 05:30:37 +0000463 case ISD::BIT_CONVERT: return visitBIT_CONVERT(N);
Chris Lattner6258fb22006-04-02 02:53:43 +0000464 case ISD::VBIT_CONVERT: return visitVBIT_CONVERT(N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000465 case ISD::FADD: return visitFADD(N);
466 case ISD::FSUB: return visitFSUB(N);
467 case ISD::FMUL: return visitFMUL(N);
468 case ISD::FDIV: return visitFDIV(N);
469 case ISD::FREM: return visitFREM(N);
Chris Lattner12d83032006-03-05 05:30:57 +0000470 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000471 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
472 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
473 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
474 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
475 case ISD::FP_ROUND: return visitFP_ROUND(N);
476 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
477 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
478 case ISD::FNEG: return visitFNEG(N);
479 case ISD::FABS: return visitFABS(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000480 case ISD::BRCOND: return visitBRCOND(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000481 case ISD::BR_CC: return visitBR_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000482 case ISD::LOAD: return visitLOAD(N);
Chris Lattner29cd7db2006-03-31 18:10:41 +0000483 case ISD::EXTLOAD:
484 case ISD::SEXTLOAD:
485 case ISD::ZEXTLOAD: return visitXEXTLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000486 case ISD::STORE: return visitSTORE(N);
Chris Lattnerca242442006-03-19 01:27:56 +0000487 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
488 case ISD::VINSERT_VECTOR_ELT: return visitVINSERT_VECTOR_ELT(N);
Chris Lattnerd7648c82006-03-28 20:28:38 +0000489 case ISD::VBUILD_VECTOR: return visitVBUILD_VECTOR(N);
Chris Lattner66445d32006-03-28 22:11:53 +0000490 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
Chris Lattnerf1d0c622006-03-31 22:16:43 +0000491 case ISD::VVECTOR_SHUFFLE: return visitVVECTOR_SHUFFLE(N);
Chris Lattneredab1b92006-04-02 03:25:57 +0000492 case ISD::VADD: return visitVBinOp(N, ISD::ADD , ISD::FADD);
493 case ISD::VSUB: return visitVBinOp(N, ISD::SUB , ISD::FSUB);
494 case ISD::VMUL: return visitVBinOp(N, ISD::MUL , ISD::FMUL);
495 case ISD::VSDIV: return visitVBinOp(N, ISD::SDIV, ISD::FDIV);
496 case ISD::VUDIV: return visitVBinOp(N, ISD::UDIV, ISD::UDIV);
497 case ISD::VAND: return visitVBinOp(N, ISD::AND , ISD::AND);
498 case ISD::VOR: return visitVBinOp(N, ISD::OR , ISD::OR);
499 case ISD::VXOR: return visitVBinOp(N, ISD::XOR , ISD::XOR);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000500 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000501 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000502}
503
Nate Begeman83e75ec2005-09-06 04:43:02 +0000504SDOperand DAGCombiner::visitTokenFactor(SDNode *N) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000505 SmallVector<SDOperand, 8> Ops;
Nate Begemanded49632005-10-13 03:11:28 +0000506 bool Changed = false;
507
Nate Begeman1d4d4142005-09-01 00:19:25 +0000508 // If the token factor has two operands and one is the entry token, replace
509 // the token factor with the other operand.
510 if (N->getNumOperands() == 2) {
Chris Lattner21a57dc2006-05-12 05:01:37 +0000511 if (N->getOperand(0).getOpcode() == ISD::EntryToken ||
512 N->getOperand(0) == N->getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000513 return N->getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000514 if (N->getOperand(1).getOpcode() == ISD::EntryToken)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000515 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000516 }
Chris Lattner24edbb72005-10-13 22:10:05 +0000517
Nate Begemanded49632005-10-13 03:11:28 +0000518 // fold (tokenfactor (tokenfactor)) -> tokenfactor
519 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
520 SDOperand Op = N->getOperand(i);
521 if (Op.getOpcode() == ISD::TokenFactor && Op.hasOneUse()) {
Chris Lattnerac0f8f22006-03-13 18:37:30 +0000522 AddToWorkList(Op.Val); // Remove dead node.
Nate Begemanded49632005-10-13 03:11:28 +0000523 Changed = true;
524 for (unsigned j = 0, e = Op.getNumOperands(); j != e; ++j)
525 Ops.push_back(Op.getOperand(j));
Chris Lattner21a57dc2006-05-12 05:01:37 +0000526 } else if (i == 0 || N->getOperand(i) != N->getOperand(i-1)) {
Nate Begemanded49632005-10-13 03:11:28 +0000527 Ops.push_back(Op);
Chris Lattner21a57dc2006-05-12 05:01:37 +0000528 } else {
529 // Deleted an operand that was the same as the last one.
530 Changed = true;
Nate Begemanded49632005-10-13 03:11:28 +0000531 }
532 }
533 if (Changed)
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000534 return DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0], Ops.size());
Nate Begeman83e75ec2005-09-06 04:43:02 +0000535 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000536}
537
Nate Begeman83e75ec2005-09-06 04:43:02 +0000538SDOperand DAGCombiner::visitADD(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000539 SDOperand N0 = N->getOperand(0);
540 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000541 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
542 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemanf89d78d2005-09-07 16:09:19 +0000543 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000544
545 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000546 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000547 return DAG.getNode(ISD::ADD, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000548 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000549 if (N0C && !N1C)
550 return DAG.getNode(ISD::ADD, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000551 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000552 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000553 return N0;
Chris Lattner4aafb4f2006-01-12 20:22:43 +0000554 // fold ((c1-A)+c2) -> (c1+c2)-A
555 if (N1C && N0.getOpcode() == ISD::SUB)
556 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
557 return DAG.getNode(ISD::SUB, VT,
558 DAG.getConstant(N1C->getValue()+N0C->getValue(), VT),
559 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000560 // reassociate add
561 SDOperand RADD = ReassociateOps(ISD::ADD, N0, N1);
562 if (RADD.Val != 0)
563 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000564 // fold ((0-A) + B) -> B-A
565 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
566 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000567 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000568 // fold (A + (0-B)) -> A-B
569 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
570 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000571 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +0000572 // fold (A+(B-A)) -> B
573 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000574 return N1.getOperand(0);
Chris Lattner947c2892006-03-13 06:51:27 +0000575
Evan Cheng860771d2006-03-01 01:09:54 +0000576 if (!MVT::isVector(VT) && SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +0000577 return SDOperand(N, 0);
Chris Lattner947c2892006-03-13 06:51:27 +0000578
579 // fold (a+b) -> (a|b) iff a and b share no bits.
580 if (MVT::isInteger(VT) && !MVT::isVector(VT)) {
581 uint64_t LHSZero, LHSOne;
582 uint64_t RHSZero, RHSOne;
583 uint64_t Mask = MVT::getIntVTBitMask(VT);
584 TLI.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
585 if (LHSZero) {
586 TLI.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
587
588 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
589 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
590 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
591 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
592 return DAG.getNode(ISD::OR, VT, N0, N1);
593 }
594 }
595
Nate Begeman83e75ec2005-09-06 04:43:02 +0000596 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000597}
598
Nate Begeman83e75ec2005-09-06 04:43:02 +0000599SDOperand DAGCombiner::visitSUB(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000600 SDOperand N0 = N->getOperand(0);
601 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000602 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
603 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000604 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000605
Chris Lattner854077d2005-10-17 01:07:11 +0000606 // fold (sub x, x) -> 0
607 if (N0 == N1)
608 return DAG.getConstant(0, N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000609 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000610 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000611 return DAG.getNode(ISD::SUB, VT, N0, N1);
Chris Lattner05b57432005-10-11 06:07:15 +0000612 // fold (sub x, c) -> (add x, -c)
613 if (N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000614 return DAG.getNode(ISD::ADD, VT, N0, DAG.getConstant(-N1C->getValue(), VT));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000615 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +0000616 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000617 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000618 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +0000619 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000620 return N0.getOperand(0);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000621 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000622}
623
Nate Begeman83e75ec2005-09-06 04:43:02 +0000624SDOperand DAGCombiner::visitMUL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000625 SDOperand N0 = N->getOperand(0);
626 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000627 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
628 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman223df222005-09-08 20:18:10 +0000629 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000630
631 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000632 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000633 return DAG.getNode(ISD::MUL, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000634 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000635 if (N0C && !N1C)
636 return DAG.getNode(ISD::MUL, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000637 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000638 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000639 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000640 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +0000641 if (N1C && N1C->isAllOnesValue())
Nate Begeman405e3ec2005-10-21 00:02:42 +0000642 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000643 // fold (mul x, (1 << c)) -> x << c
Nate Begeman646d7e22005-09-02 21:18:40 +0000644 if (N1C && isPowerOf2_64(N1C->getValue()))
Chris Lattner3e6099b2005-10-30 06:41:49 +0000645 return DAG.getNode(ISD::SHL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000646 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000647 TLI.getShiftAmountTy()));
Chris Lattner3e6099b2005-10-30 06:41:49 +0000648 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
649 if (N1C && isPowerOf2_64(-N1C->getSignExtended())) {
650 // FIXME: If the input is something that is easily negated (e.g. a
651 // single-use add), we should put the negate there.
652 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT),
653 DAG.getNode(ISD::SHL, VT, N0,
654 DAG.getConstant(Log2_64(-N1C->getSignExtended()),
655 TLI.getShiftAmountTy())));
656 }
Andrew Lenharth50a0d422006-04-02 21:42:45 +0000657
Chris Lattner0b1a85f2006-03-01 03:44:24 +0000658 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
659 if (N1C && N0.getOpcode() == ISD::SHL &&
660 isa<ConstantSDNode>(N0.getOperand(1))) {
661 SDOperand C3 = DAG.getNode(ISD::SHL, VT, N1, N0.getOperand(1));
Chris Lattner5750df92006-03-01 04:03:14 +0000662 AddToWorkList(C3.Val);
Chris Lattner0b1a85f2006-03-01 03:44:24 +0000663 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0), C3);
664 }
665
666 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
667 // use.
668 {
669 SDOperand Sh(0,0), Y(0,0);
670 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
671 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
672 N0.Val->hasOneUse()) {
673 Sh = N0; Y = N1;
674 } else if (N1.getOpcode() == ISD::SHL &&
675 isa<ConstantSDNode>(N1.getOperand(1)) && N1.Val->hasOneUse()) {
676 Sh = N1; Y = N0;
677 }
678 if (Sh.Val) {
679 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Sh.getOperand(0), Y);
680 return DAG.getNode(ISD::SHL, VT, Mul, Sh.getOperand(1));
681 }
682 }
Chris Lattnera1deca32006-03-04 23:33:26 +0000683 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
684 if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() &&
685 isa<ConstantSDNode>(N0.getOperand(1))) {
686 return DAG.getNode(ISD::ADD, VT,
687 DAG.getNode(ISD::MUL, VT, N0.getOperand(0), N1),
688 DAG.getNode(ISD::MUL, VT, N0.getOperand(1), N1));
689 }
Chris Lattner0b1a85f2006-03-01 03:44:24 +0000690
Nate Begemancd4d58c2006-02-03 06:46:56 +0000691 // reassociate mul
692 SDOperand RMUL = ReassociateOps(ISD::MUL, N0, N1);
693 if (RMUL.Val != 0)
694 return RMUL;
Nate Begeman83e75ec2005-09-06 04:43:02 +0000695 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000696}
697
Nate Begeman83e75ec2005-09-06 04:43:02 +0000698SDOperand DAGCombiner::visitSDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000699 SDOperand N0 = N->getOperand(0);
700 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000701 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
702 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000703 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000704
705 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000706 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000707 return DAG.getNode(ISD::SDIV, VT, N0, N1);
Nate Begeman405e3ec2005-10-21 00:02:42 +0000708 // fold (sdiv X, 1) -> X
709 if (N1C && N1C->getSignExtended() == 1LL)
710 return N0;
711 // fold (sdiv X, -1) -> 0-X
712 if (N1C && N1C->isAllOnesValue())
713 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +0000714 // If we know the sign bits of both operands are zero, strength reduce to a
715 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
716 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000717 if (TLI.MaskedValueIsZero(N1, SignBit) &&
718 TLI.MaskedValueIsZero(N0, SignBit))
Chris Lattner094c8fc2005-10-07 06:10:46 +0000719 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
Nate Begemancd6a6ed2006-02-17 07:26:20 +0000720 // fold (sdiv X, pow2) -> simple ops after legalize
Nate Begemanfb7217b2006-02-17 19:54:08 +0000721 if (N1C && N1C->getValue() && !TLI.isIntDivCheap() &&
Nate Begeman405e3ec2005-10-21 00:02:42 +0000722 (isPowerOf2_64(N1C->getSignExtended()) ||
723 isPowerOf2_64(-N1C->getSignExtended()))) {
724 // If dividing by powers of two is cheap, then don't perform the following
725 // fold.
726 if (TLI.isPow2DivCheap())
727 return SDOperand();
728 int64_t pow2 = N1C->getSignExtended();
729 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
Chris Lattner8f4880b2006-02-16 08:02:36 +0000730 unsigned lg2 = Log2_64(abs2);
731 // Splat the sign bit into the register
732 SDOperand SGN = DAG.getNode(ISD::SRA, VT, N0,
Nate Begeman405e3ec2005-10-21 00:02:42 +0000733 DAG.getConstant(MVT::getSizeInBits(VT)-1,
734 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +0000735 AddToWorkList(SGN.Val);
Chris Lattner8f4880b2006-02-16 08:02:36 +0000736 // Add (N0 < 0) ? abs2 - 1 : 0;
737 SDOperand SRL = DAG.getNode(ISD::SRL, VT, SGN,
738 DAG.getConstant(MVT::getSizeInBits(VT)-lg2,
Nate Begeman405e3ec2005-10-21 00:02:42 +0000739 TLI.getShiftAmountTy()));
Chris Lattner8f4880b2006-02-16 08:02:36 +0000740 SDOperand ADD = DAG.getNode(ISD::ADD, VT, N0, SRL);
Chris Lattner5750df92006-03-01 04:03:14 +0000741 AddToWorkList(SRL.Val);
742 AddToWorkList(ADD.Val); // Divide by pow2
Chris Lattner8f4880b2006-02-16 08:02:36 +0000743 SDOperand SRA = DAG.getNode(ISD::SRA, VT, ADD,
744 DAG.getConstant(lg2, TLI.getShiftAmountTy()));
Nate Begeman405e3ec2005-10-21 00:02:42 +0000745 // If we're dividing by a positive value, we're done. Otherwise, we must
746 // negate the result.
747 if (pow2 > 0)
748 return SRA;
Chris Lattner5750df92006-03-01 04:03:14 +0000749 AddToWorkList(SRA.Val);
Nate Begeman405e3ec2005-10-21 00:02:42 +0000750 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), SRA);
751 }
Nate Begeman69575232005-10-20 02:15:44 +0000752 // if integer divide is expensive and we satisfy the requirements, emit an
753 // alternate sequence.
Nate Begeman405e3ec2005-10-21 00:02:42 +0000754 if (N1C && (N1C->getSignExtended() < -1 || N1C->getSignExtended() > 1) &&
Chris Lattnere9936d12005-10-22 18:50:15 +0000755 !TLI.isIntDivCheap()) {
756 SDOperand Op = BuildSDIV(N);
757 if (Op.Val) return Op;
Nate Begeman69575232005-10-20 02:15:44 +0000758 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000759 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000760}
761
Nate Begeman83e75ec2005-09-06 04:43:02 +0000762SDOperand DAGCombiner::visitUDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000763 SDOperand N0 = N->getOperand(0);
764 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000765 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
766 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000767 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000768
769 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000770 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000771 return DAG.getNode(ISD::UDIV, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000772 // fold (udiv x, (1 << c)) -> x >>u c
Nate Begeman646d7e22005-09-02 21:18:40 +0000773 if (N1C && isPowerOf2_64(N1C->getValue()))
Nate Begemanfb5e4bd2006-02-05 07:20:23 +0000774 return DAG.getNode(ISD::SRL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000775 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000776 TLI.getShiftAmountTy()));
Nate Begemanfb5e4bd2006-02-05 07:20:23 +0000777 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
778 if (N1.getOpcode() == ISD::SHL) {
779 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
780 if (isPowerOf2_64(SHC->getValue())) {
781 MVT::ValueType ADDVT = N1.getOperand(1).getValueType();
Nate Begemanc031e332006-02-05 07:36:48 +0000782 SDOperand Add = DAG.getNode(ISD::ADD, ADDVT, N1.getOperand(1),
783 DAG.getConstant(Log2_64(SHC->getValue()),
784 ADDVT));
Chris Lattner5750df92006-03-01 04:03:14 +0000785 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +0000786 return DAG.getNode(ISD::SRL, VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +0000787 }
788 }
789 }
Nate Begeman69575232005-10-20 02:15:44 +0000790 // fold (udiv x, c) -> alternate
Chris Lattnere9936d12005-10-22 18:50:15 +0000791 if (N1C && N1C->getValue() && !TLI.isIntDivCheap()) {
792 SDOperand Op = BuildUDIV(N);
793 if (Op.Val) return Op;
794 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000795 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000796}
797
Nate Begeman83e75ec2005-09-06 04:43:02 +0000798SDOperand DAGCombiner::visitSREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000799 SDOperand N0 = N->getOperand(0);
800 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000801 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
802 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +0000803 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000804
805 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000806 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000807 return DAG.getNode(ISD::SREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +0000808 // If we know the sign bits of both operands are zero, strength reduce to a
809 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
810 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000811 if (TLI.MaskedValueIsZero(N1, SignBit) &&
812 TLI.MaskedValueIsZero(N0, SignBit))
Nate Begemana148d982006-01-18 22:35:16 +0000813 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000814 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000815}
816
Nate Begeman83e75ec2005-09-06 04:43:02 +0000817SDOperand DAGCombiner::visitUREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000818 SDOperand N0 = N->getOperand(0);
819 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000820 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
821 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +0000822 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000823
824 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000825 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000826 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +0000827 // fold (urem x, pow2) -> (and x, pow2-1)
828 if (N1C && !N1C->isNullValue() && isPowerOf2_64(N1C->getValue()))
Nate Begemana148d982006-01-18 22:35:16 +0000829 return DAG.getNode(ISD::AND, VT, N0, DAG.getConstant(N1C->getValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +0000830 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
831 if (N1.getOpcode() == ISD::SHL) {
832 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
833 if (isPowerOf2_64(SHC->getValue())) {
Nate Begemanbab92392006-02-05 08:07:24 +0000834 SDOperand Add = DAG.getNode(ISD::ADD, VT, N1,DAG.getConstant(~0ULL,VT));
Chris Lattner5750df92006-03-01 04:03:14 +0000835 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +0000836 return DAG.getNode(ISD::AND, VT, N0, Add);
837 }
838 }
839 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000840 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000841}
842
Nate Begeman83e75ec2005-09-06 04:43:02 +0000843SDOperand DAGCombiner::visitMULHS(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000844 SDOperand N0 = N->getOperand(0);
845 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000846 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000847
848 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000849 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000850 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000851 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Nate Begeman646d7e22005-09-02 21:18:40 +0000852 if (N1C && N1C->getValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +0000853 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
854 DAG.getConstant(MVT::getSizeInBits(N0.getValueType())-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +0000855 TLI.getShiftAmountTy()));
856 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000857}
858
Nate Begeman83e75ec2005-09-06 04:43:02 +0000859SDOperand DAGCombiner::visitMULHU(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000860 SDOperand N0 = N->getOperand(0);
861 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000862 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000863
864 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000865 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000866 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000867 // fold (mulhu x, 1) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000868 if (N1C && N1C->getValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000869 return DAG.getConstant(0, N0.getValueType());
870 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000871}
872
Chris Lattner35e5c142006-05-05 05:51:50 +0000873/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
874/// two operands of the same opcode, try to simplify it.
875SDOperand DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
876 SDOperand N0 = N->getOperand(0), N1 = N->getOperand(1);
877 MVT::ValueType VT = N0.getValueType();
878 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
879
Chris Lattner540121f2006-05-05 06:31:05 +0000880 // For each of OP in AND/OR/XOR:
881 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
882 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
883 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
Chris Lattner0d8dae72006-05-05 06:32:04 +0000884 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y))
Chris Lattner540121f2006-05-05 06:31:05 +0000885 if ((N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND||
Chris Lattner0d8dae72006-05-05 06:32:04 +0000886 N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::TRUNCATE) &&
Chris Lattner35e5c142006-05-05 05:51:50 +0000887 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
888 SDOperand ORNode = DAG.getNode(N->getOpcode(),
889 N0.getOperand(0).getValueType(),
890 N0.getOperand(0), N1.getOperand(0));
891 AddToWorkList(ORNode.Val);
Chris Lattner540121f2006-05-05 06:31:05 +0000892 return DAG.getNode(N0.getOpcode(), VT, ORNode);
Chris Lattner35e5c142006-05-05 05:51:50 +0000893 }
894
Chris Lattnera3dc3f62006-05-05 06:10:43 +0000895 // For each of OP in SHL/SRL/SRA/AND...
896 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
897 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
898 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
Chris Lattner35e5c142006-05-05 05:51:50 +0000899 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
Chris Lattnera3dc3f62006-05-05 06:10:43 +0000900 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
Chris Lattner35e5c142006-05-05 05:51:50 +0000901 N0.getOperand(1) == N1.getOperand(1)) {
902 SDOperand ORNode = DAG.getNode(N->getOpcode(),
903 N0.getOperand(0).getValueType(),
904 N0.getOperand(0), N1.getOperand(0));
905 AddToWorkList(ORNode.Val);
906 return DAG.getNode(N0.getOpcode(), VT, ORNode, N0.getOperand(1));
907 }
908
909 return SDOperand();
910}
911
Nate Begeman83e75ec2005-09-06 04:43:02 +0000912SDOperand DAGCombiner::visitAND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000913 SDOperand N0 = N->getOperand(0);
914 SDOperand N1 = N->getOperand(1);
Nate Begemanfb7217b2006-02-17 19:54:08 +0000915 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +0000916 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
917 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000918 MVT::ValueType VT = N1.getValueType();
Nate Begeman83e75ec2005-09-06 04:43:02 +0000919 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000920
921 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000922 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000923 return DAG.getNode(ISD::AND, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000924 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000925 if (N0C && !N1C)
926 return DAG.getNode(ISD::AND, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000927 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000928 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000929 return N0;
930 // if (and x, c) is known to be zero, return 0
Nate Begeman368e18d2006-02-16 21:11:51 +0000931 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000932 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000933 // reassociate and
934 SDOperand RAND = ReassociateOps(ISD::AND, N0, N1);
935 if (RAND.Val != 0)
936 return RAND;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000937 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
Nate Begeman5dc7e862005-11-02 18:42:59 +0000938 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +0000939 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Nate Begeman646d7e22005-09-02 21:18:40 +0000940 if ((ORI->getValue() & N1C->getValue()) == N1C->getValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000941 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +0000942 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
943 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Chris Lattner1ec05d12006-03-01 21:47:21 +0000944 unsigned InMask = MVT::getIntVTBitMask(N0.getOperand(0).getValueType());
Chris Lattner3603cd62006-02-02 07:17:31 +0000945 if (TLI.MaskedValueIsZero(N0.getOperand(0),
Chris Lattner1ec05d12006-03-01 21:47:21 +0000946 ~N1C->getValue() & InMask)) {
947 SDOperand Zext = DAG.getNode(ISD::ZERO_EXTEND, N0.getValueType(),
948 N0.getOperand(0));
949
950 // Replace uses of the AND with uses of the Zero extend node.
951 CombineTo(N, Zext);
952
Chris Lattner3603cd62006-02-02 07:17:31 +0000953 // We actually want to replace all uses of the any_extend with the
954 // zero_extend, to avoid duplicating things. This will later cause this
955 // AND to be folded.
Chris Lattner1ec05d12006-03-01 21:47:21 +0000956 CombineTo(N0.Val, Zext);
Chris Lattnerfedced72006-04-20 23:55:59 +0000957 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner3603cd62006-02-02 07:17:31 +0000958 }
959 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000960 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
961 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
962 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
963 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
964
965 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
966 MVT::isInteger(LL.getValueType())) {
967 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
968 if (cast<ConstantSDNode>(LR)->getValue() == 0 && Op1 == ISD::SETEQ) {
969 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +0000970 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000971 return DAG.getSetCC(VT, ORNode, LR, Op1);
972 }
973 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
974 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
975 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +0000976 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000977 return DAG.getSetCC(VT, ANDNode, LR, Op1);
978 }
979 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
980 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
981 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +0000982 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000983 return DAG.getSetCC(VT, ORNode, LR, Op1);
984 }
985 }
986 // canonicalize equivalent to ll == rl
987 if (LL == RR && LR == RL) {
988 Op1 = ISD::getSetCCSwappedOperands(Op1);
989 std::swap(RL, RR);
990 }
991 if (LL == RL && LR == RR) {
992 bool isInteger = MVT::isInteger(LL.getValueType());
993 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
994 if (Result != ISD::SETCC_INVALID)
995 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
996 }
997 }
Chris Lattner35e5c142006-05-05 05:51:50 +0000998
999 // Simplify: and (op x...), (op y...) -> (op (and x, y))
1000 if (N0.getOpcode() == N1.getOpcode()) {
1001 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1002 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001003 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001004
Nate Begemande996292006-02-03 22:24:05 +00001005 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
1006 // fold (and (sra)) -> (and (srl)) when possible.
Chris Lattner6ea2dee2006-03-25 22:19:00 +00001007 if (!MVT::isVector(VT) &&
1008 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001009 return SDOperand(N, 0);
Nate Begemanded49632005-10-13 03:11:28 +00001010 // fold (zext_inreg (extload x)) -> (zextload x)
Nate Begeman5054f162005-10-14 01:12:21 +00001011 if (N0.getOpcode() == ISD::EXTLOAD) {
Nate Begemanded49632005-10-13 03:11:28 +00001012 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001013 // If we zero all the possible extended bits, then we can turn this into
1014 // a zextload if we are running before legalize or the operation is legal.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001015 if (TLI.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Chris Lattner67a44cd2005-10-13 18:16:34 +00001016 (!AfterLegalize || TLI.isOperationLegal(ISD::ZEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001017 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1018 N0.getOperand(1), N0.getOperand(2),
1019 EVT);
Chris Lattner5750df92006-03-01 04:03:14 +00001020 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001021 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001022 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001023 }
1024 }
1025 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Chris Lattner40c62d52005-10-18 06:04:22 +00001026 if (N0.getOpcode() == ISD::SEXTLOAD && N0.hasOneUse()) {
Nate Begemanded49632005-10-13 03:11:28 +00001027 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001028 // If we zero all the possible extended bits, then we can turn this into
1029 // a zextload if we are running before legalize or the operation is legal.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001030 if (TLI.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Nate Begemanbfd65a02005-10-13 18:34:58 +00001031 (!AfterLegalize || TLI.isOperationLegal(ISD::ZEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001032 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1033 N0.getOperand(1), N0.getOperand(2),
1034 EVT);
Chris Lattner5750df92006-03-01 04:03:14 +00001035 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001036 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001037 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001038 }
1039 }
Chris Lattner15045b62006-02-28 06:35:35 +00001040
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001041 // fold (and (load x), 255) -> (zextload x, i8)
1042 // fold (and (extload x, i16), 255) -> (zextload x, i8)
1043 if (N1C &&
1044 (N0.getOpcode() == ISD::LOAD || N0.getOpcode() == ISD::EXTLOAD ||
1045 N0.getOpcode() == ISD::ZEXTLOAD) &&
1046 N0.hasOneUse()) {
1047 MVT::ValueType EVT, LoadedVT;
Chris Lattner15045b62006-02-28 06:35:35 +00001048 if (N1C->getValue() == 255)
1049 EVT = MVT::i8;
1050 else if (N1C->getValue() == 65535)
1051 EVT = MVT::i16;
1052 else if (N1C->getValue() == ~0U)
1053 EVT = MVT::i32;
1054 else
1055 EVT = MVT::Other;
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001056
1057 LoadedVT = N0.getOpcode() == ISD::LOAD ? VT :
1058 cast<VTSDNode>(N0.getOperand(3))->getVT();
Chris Lattnere44be602006-04-04 17:39:18 +00001059 if (EVT != MVT::Other && LoadedVT > EVT &&
1060 (!AfterLegalize || TLI.isOperationLegal(ISD::ZEXTLOAD, EVT))) {
Chris Lattner15045b62006-02-28 06:35:35 +00001061 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
1062 // For big endian targets, we need to add an offset to the pointer to load
1063 // the correct bytes. For little endian systems, we merely need to read
1064 // fewer bytes from the same pointer.
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001065 unsigned PtrOff =
1066 (MVT::getSizeInBits(LoadedVT) - MVT::getSizeInBits(EVT)) / 8;
1067 SDOperand NewPtr = N0.getOperand(1);
1068 if (!TLI.isLittleEndian())
1069 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
1070 DAG.getConstant(PtrOff, PtrType));
Chris Lattner5750df92006-03-01 04:03:14 +00001071 AddToWorkList(NewPtr.Val);
Chris Lattner15045b62006-02-28 06:35:35 +00001072 SDOperand Load =
1073 DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0), NewPtr,
1074 N0.getOperand(2), EVT);
Chris Lattner5750df92006-03-01 04:03:14 +00001075 AddToWorkList(N);
Chris Lattner15045b62006-02-28 06:35:35 +00001076 CombineTo(N0.Val, Load, Load.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001077 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner15045b62006-02-28 06:35:35 +00001078 }
1079 }
1080
Nate Begeman83e75ec2005-09-06 04:43:02 +00001081 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001082}
1083
Nate Begeman83e75ec2005-09-06 04:43:02 +00001084SDOperand DAGCombiner::visitOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001085 SDOperand N0 = N->getOperand(0);
1086 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001087 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001088 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1089 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001090 MVT::ValueType VT = N1.getValueType();
1091 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001092
1093 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001094 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001095 return DAG.getNode(ISD::OR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001096 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001097 if (N0C && !N1C)
1098 return DAG.getNode(ISD::OR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001099 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001100 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001101 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001102 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001103 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001104 return N1;
1105 // fold (or x, c) -> c iff (x & ~c) == 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001106 if (N1C &&
1107 TLI.MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits))))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001108 return N1;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001109 // reassociate or
1110 SDOperand ROR = ReassociateOps(ISD::OR, N0, N1);
1111 if (ROR.Val != 0)
1112 return ROR;
1113 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
1114 if (N1C && N0.getOpcode() == ISD::AND && N0.Val->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00001115 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00001116 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
1117 return DAG.getNode(ISD::AND, VT, DAG.getNode(ISD::OR, VT, N0.getOperand(0),
1118 N1),
1119 DAG.getConstant(N1C->getValue() | C1->getValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00001120 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001121 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
1122 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1123 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1124 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1125
1126 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1127 MVT::isInteger(LL.getValueType())) {
1128 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
1129 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
1130 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
1131 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
1132 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001133 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001134 return DAG.getSetCC(VT, ORNode, LR, Op1);
1135 }
1136 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
1137 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
1138 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
1139 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
1140 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001141 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001142 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1143 }
1144 }
1145 // canonicalize equivalent to ll == rl
1146 if (LL == RR && LR == RL) {
1147 Op1 = ISD::getSetCCSwappedOperands(Op1);
1148 std::swap(RL, RR);
1149 }
1150 if (LL == RL && LR == RR) {
1151 bool isInteger = MVT::isInteger(LL.getValueType());
1152 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
1153 if (Result != ISD::SETCC_INVALID)
1154 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1155 }
1156 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001157
1158 // Simplify: or (op x...), (op y...) -> (op (or x, y))
1159 if (N0.getOpcode() == N1.getOpcode()) {
1160 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1161 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001162 }
Chris Lattner516b9622006-09-14 20:50:57 +00001163
Chris Lattner1ec72732006-09-14 21:11:37 +00001164 // (X & C1) | (Y & C2) -> (X|Y) & C3 if possible.
1165 if (N0.getOpcode() == ISD::AND &&
1166 N1.getOpcode() == ISD::AND &&
1167 N0.getOperand(1).getOpcode() == ISD::Constant &&
1168 N1.getOperand(1).getOpcode() == ISD::Constant &&
1169 // Don't increase # computations.
1170 (N0.Val->hasOneUse() || N1.Val->hasOneUse())) {
1171 // We can only do this xform if we know that bits from X that are set in C2
1172 // but not in C1 are already zero. Likewise for Y.
1173 uint64_t LHSMask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1174 uint64_t RHSMask = cast<ConstantSDNode>(N1.getOperand(1))->getValue();
1175
1176 if (TLI.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) &&
1177 TLI.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) {
1178 SDOperand X =DAG.getNode(ISD::OR, VT, N0.getOperand(0), N1.getOperand(0));
1179 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(LHSMask|RHSMask, VT));
1180 }
1181 }
1182
1183
Chris Lattner516b9622006-09-14 20:50:57 +00001184 // See if this is some rotate idiom.
1185 if (SDNode *Rot = MatchRotate(N0, N1))
1186 return SDOperand(Rot, 0);
Chris Lattner35e5c142006-05-05 05:51:50 +00001187
Nate Begeman83e75ec2005-09-06 04:43:02 +00001188 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001189}
1190
Chris Lattner516b9622006-09-14 20:50:57 +00001191
1192/// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present.
1193static bool MatchRotateHalf(SDOperand Op, SDOperand &Shift, SDOperand &Mask) {
1194 if (Op.getOpcode() == ISD::AND) {
1195 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1196 Mask = Op.getOperand(1);
1197 Op = Op.getOperand(0);
1198 } else {
1199 return false;
1200 }
1201 }
1202
1203 if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) {
1204 Shift = Op;
1205 return true;
1206 }
1207 return false;
1208}
1209
1210
1211// MatchRotate - Handle an 'or' of two operands. If this is one of the many
1212// idioms for rotate, and if the target supports rotation instructions, generate
1213// a rot[lr].
1214SDNode *DAGCombiner::MatchRotate(SDOperand LHS, SDOperand RHS) {
1215 // Must be a legal type. Expanded an promoted things won't work with rotates.
1216 MVT::ValueType VT = LHS.getValueType();
1217 if (!TLI.isTypeLegal(VT)) return 0;
1218
1219 // The target must have at least one rotate flavor.
1220 bool HasROTL = TLI.isOperationLegal(ISD::ROTL, VT);
1221 bool HasROTR = TLI.isOperationLegal(ISD::ROTR, VT);
1222 if (!HasROTL && !HasROTR) return 0;
1223
1224 // Match "(X shl/srl V1) & V2" where V2 may not be present.
1225 SDOperand LHSShift; // The shift.
1226 SDOperand LHSMask; // AND value if any.
1227 if (!MatchRotateHalf(LHS, LHSShift, LHSMask))
1228 return 0; // Not part of a rotate.
1229
1230 SDOperand RHSShift; // The shift.
1231 SDOperand RHSMask; // AND value if any.
1232 if (!MatchRotateHalf(RHS, RHSShift, RHSMask))
1233 return 0; // Not part of a rotate.
1234
1235 if (LHSShift.getOperand(0) != RHSShift.getOperand(0))
1236 return 0; // Not shifting the same value.
1237
1238 if (LHSShift.getOpcode() == RHSShift.getOpcode())
1239 return 0; // Shifts must disagree.
1240
1241 // Canonicalize shl to left side in a shl/srl pair.
1242 if (RHSShift.getOpcode() == ISD::SHL) {
1243 std::swap(LHS, RHS);
1244 std::swap(LHSShift, RHSShift);
1245 std::swap(LHSMask , RHSMask );
1246 }
1247
1248 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1249
1250 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
1251 // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2)
1252 if (LHSShift.getOperand(1).getOpcode() == ISD::Constant &&
1253 RHSShift.getOperand(1).getOpcode() == ISD::Constant) {
1254 uint64_t LShVal = cast<ConstantSDNode>(LHSShift.getOperand(1))->getValue();
1255 uint64_t RShVal = cast<ConstantSDNode>(RHSShift.getOperand(1))->getValue();
1256 if ((LShVal + RShVal) != OpSizeInBits)
1257 return 0;
1258
1259 SDOperand Rot;
1260 if (HasROTL)
1261 Rot = DAG.getNode(ISD::ROTL, VT, LHSShift.getOperand(0),
1262 LHSShift.getOperand(1));
1263 else
1264 Rot = DAG.getNode(ISD::ROTR, VT, LHSShift.getOperand(0),
1265 RHSShift.getOperand(1));
1266
1267 // If there is an AND of either shifted operand, apply it to the result.
1268 if (LHSMask.Val || RHSMask.Val) {
1269 uint64_t Mask = MVT::getIntVTBitMask(VT);
1270
1271 if (LHSMask.Val) {
1272 uint64_t RHSBits = (1ULL << LShVal)-1;
1273 Mask &= cast<ConstantSDNode>(LHSMask)->getValue() | RHSBits;
1274 }
1275 if (RHSMask.Val) {
1276 uint64_t LHSBits = ~((1ULL << (OpSizeInBits-RShVal))-1);
1277 Mask &= cast<ConstantSDNode>(RHSMask)->getValue() | LHSBits;
1278 }
1279
1280 Rot = DAG.getNode(ISD::AND, VT, Rot, DAG.getConstant(Mask, VT));
1281 }
1282
1283 return Rot.Val;
1284 }
1285
1286 // If there is a mask here, and we have a variable shift, we can't be sure
1287 // that we're masking out the right stuff.
1288 if (LHSMask.Val || RHSMask.Val)
1289 return 0;
1290
1291 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
1292 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotr x, (sub 32, y))
1293 if (RHSShift.getOperand(1).getOpcode() == ISD::SUB &&
1294 LHSShift.getOperand(1) == RHSShift.getOperand(1).getOperand(1)) {
1295 if (ConstantSDNode *SUBC =
1296 dyn_cast<ConstantSDNode>(RHSShift.getOperand(1).getOperand(0))) {
1297 if (SUBC->getValue() == OpSizeInBits)
1298 if (HasROTL)
1299 return DAG.getNode(ISD::ROTL, VT, LHSShift.getOperand(0),
1300 LHSShift.getOperand(1)).Val;
1301 else
1302 return DAG.getNode(ISD::ROTR, VT, LHSShift.getOperand(0),
1303 LHSShift.getOperand(1)).Val;
1304 }
1305 }
1306
1307 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
1308 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotl x, (sub 32, y))
1309 if (LHSShift.getOperand(1).getOpcode() == ISD::SUB &&
1310 RHSShift.getOperand(1) == LHSShift.getOperand(1).getOperand(1)) {
1311 if (ConstantSDNode *SUBC =
1312 dyn_cast<ConstantSDNode>(LHSShift.getOperand(1).getOperand(0))) {
1313 if (SUBC->getValue() == OpSizeInBits)
1314 if (HasROTL)
1315 return DAG.getNode(ISD::ROTL, VT, LHSShift.getOperand(0),
1316 LHSShift.getOperand(1)).Val;
1317 else
1318 return DAG.getNode(ISD::ROTR, VT, LHSShift.getOperand(0),
1319 RHSShift.getOperand(1)).Val;
1320 }
1321 }
1322
1323 return 0;
1324}
1325
1326
Nate Begeman83e75ec2005-09-06 04:43:02 +00001327SDOperand DAGCombiner::visitXOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001328 SDOperand N0 = N->getOperand(0);
1329 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001330 SDOperand LHS, RHS, CC;
1331 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1332 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001333 MVT::ValueType VT = N0.getValueType();
1334
1335 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001336 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001337 return DAG.getNode(ISD::XOR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001338 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001339 if (N0C && !N1C)
1340 return DAG.getNode(ISD::XOR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001341 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001342 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001343 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001344 // reassociate xor
1345 SDOperand RXOR = ReassociateOps(ISD::XOR, N0, N1);
1346 if (RXOR.Val != 0)
1347 return RXOR;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001348 // fold !(x cc y) -> (x !cc y)
Nate Begeman646d7e22005-09-02 21:18:40 +00001349 if (N1C && N1C->getValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
1350 bool isInt = MVT::isInteger(LHS.getValueType());
1351 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
1352 isInt);
1353 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001354 return DAG.getSetCC(VT, LHS, RHS, NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00001355 if (N0.getOpcode() == ISD::SELECT_CC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001356 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00001357 assert(0 && "Unhandled SetCC Equivalent!");
1358 abort();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001359 }
Nate Begeman99801192005-09-07 23:25:52 +00001360 // fold !(x or y) -> (!x and !y) iff x or y are setcc
1361 if (N1C && N1C->getValue() == 1 &&
1362 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001363 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00001364 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
1365 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001366 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1367 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00001368 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00001369 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001370 }
1371 }
Nate Begeman99801192005-09-07 23:25:52 +00001372 // fold !(x or y) -> (!x and !y) iff x or y are constants
1373 if (N1C && N1C->isAllOnesValue() &&
1374 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001375 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00001376 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
1377 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001378 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1379 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00001380 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00001381 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001382 }
1383 }
Nate Begeman223df222005-09-08 20:18:10 +00001384 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
1385 if (N1C && N0.getOpcode() == ISD::XOR) {
1386 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
1387 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
1388 if (N00C)
1389 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
1390 DAG.getConstant(N1C->getValue()^N00C->getValue(), VT));
1391 if (N01C)
1392 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
1393 DAG.getConstant(N1C->getValue()^N01C->getValue(), VT));
1394 }
1395 // fold (xor x, x) -> 0
Chris Lattner4fbdd592006-03-28 19:11:05 +00001396 if (N0 == N1) {
1397 if (!MVT::isVector(VT)) {
1398 return DAG.getConstant(0, VT);
1399 } else if (!AfterLegalize || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) {
1400 // Produce a vector of zeros.
1401 SDOperand El = DAG.getConstant(0, MVT::getVectorBaseType(VT));
1402 std::vector<SDOperand> Ops(MVT::getVectorNumElements(VT), El);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001403 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4fbdd592006-03-28 19:11:05 +00001404 }
1405 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001406
1407 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
1408 if (N0.getOpcode() == N1.getOpcode()) {
1409 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1410 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001411 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001412
Chris Lattner3e104b12006-04-08 04:15:24 +00001413 // Simplify the expression using non-local knowledge.
1414 if (!MVT::isVector(VT) &&
1415 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001416 return SDOperand(N, 0);
Chris Lattner3e104b12006-04-08 04:15:24 +00001417
Nate Begeman83e75ec2005-09-06 04:43:02 +00001418 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001419}
1420
Nate Begeman83e75ec2005-09-06 04:43:02 +00001421SDOperand DAGCombiner::visitSHL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001422 SDOperand N0 = N->getOperand(0);
1423 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001424 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1425 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001426 MVT::ValueType VT = N0.getValueType();
1427 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1428
1429 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001430 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001431 return DAG.getNode(ISD::SHL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001432 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001433 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001434 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001435 // fold (shl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001436 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001437 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001438 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001439 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001440 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001441 // if (shl x, c) is known to be zero, return 0
Nate Begemanfb7217b2006-02-17 19:54:08 +00001442 if (TLI.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001443 return DAG.getConstant(0, VT);
Chris Lattner012f2412006-02-17 21:58:01 +00001444 if (SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001445 return SDOperand(N, 0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001446 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001447 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001448 N0.getOperand(1).getOpcode() == ISD::Constant) {
1449 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001450 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001451 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001452 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001453 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001454 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001455 }
1456 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
1457 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001458 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001459 N0.getOperand(1).getOpcode() == ISD::Constant) {
1460 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001461 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001462 SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
1463 DAG.getConstant(~0ULL << c1, VT));
1464 if (c2 > c1)
1465 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001466 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001467 else
Nate Begeman83e75ec2005-09-06 04:43:02 +00001468 return DAG.getNode(ISD::SRL, VT, Mask,
1469 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001470 }
1471 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001472 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +00001473 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001474 DAG.getConstant(~0ULL << N1C->getValue(), VT));
Chris Lattnercac70592006-03-05 19:53:55 +00001475 // fold (shl (add x, c1), c2) -> (add (shl x, c2), c1<<c2)
1476 if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() &&
1477 isa<ConstantSDNode>(N0.getOperand(1))) {
1478 return DAG.getNode(ISD::ADD, VT,
1479 DAG.getNode(ISD::SHL, VT, N0.getOperand(0), N1),
1480 DAG.getNode(ISD::SHL, VT, N0.getOperand(1), N1));
1481 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001482 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001483}
1484
Nate Begeman83e75ec2005-09-06 04:43:02 +00001485SDOperand DAGCombiner::visitSRA(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001486 SDOperand N0 = N->getOperand(0);
1487 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001488 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1489 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001490 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001491
1492 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001493 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001494 return DAG.getNode(ISD::SRA, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001495 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001496 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001497 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001498 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001499 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001500 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001501 // fold (sra x, c >= size(x)) -> undef
Nate Begemanfb7217b2006-02-17 19:54:08 +00001502 if (N1C && N1C->getValue() >= MVT::getSizeInBits(VT))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001503 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001504 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001505 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001506 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00001507 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
1508 // sext_inreg.
1509 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
1510 unsigned LowBits = MVT::getSizeInBits(VT) - (unsigned)N1C->getValue();
1511 MVT::ValueType EVT;
1512 switch (LowBits) {
1513 default: EVT = MVT::Other; break;
1514 case 1: EVT = MVT::i1; break;
1515 case 8: EVT = MVT::i8; break;
1516 case 16: EVT = MVT::i16; break;
1517 case 32: EVT = MVT::i32; break;
1518 }
1519 if (EVT > MVT::Other && TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, EVT))
1520 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
1521 DAG.getValueType(EVT));
1522 }
Chris Lattner71d9ebc2006-02-28 06:23:04 +00001523
1524 // fold (sra (sra x, c1), c2) -> (sra x, c1+c2)
1525 if (N1C && N0.getOpcode() == ISD::SRA) {
1526 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
1527 unsigned Sum = N1C->getValue() + C1->getValue();
1528 if (Sum >= MVT::getSizeInBits(VT)) Sum = MVT::getSizeInBits(VT)-1;
1529 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0),
1530 DAG.getConstant(Sum, N1C->getValueType(0)));
1531 }
1532 }
1533
Chris Lattnera8504462006-05-08 20:51:54 +00001534 // Simplify, based on bits shifted out of the LHS.
1535 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
1536 return SDOperand(N, 0);
1537
1538
Nate Begeman1d4d4142005-09-01 00:19:25 +00001539 // If the sign bit is known to be zero, switch this to a SRL.
Nate Begemanfb7217b2006-02-17 19:54:08 +00001540 if (TLI.MaskedValueIsZero(N0, MVT::getIntVTSignBit(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001541 return DAG.getNode(ISD::SRL, VT, N0, N1);
1542 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001543}
1544
Nate Begeman83e75ec2005-09-06 04:43:02 +00001545SDOperand DAGCombiner::visitSRL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001546 SDOperand N0 = N->getOperand(0);
1547 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001548 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1549 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001550 MVT::ValueType VT = N0.getValueType();
1551 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1552
1553 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001554 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001555 return DAG.getNode(ISD::SRL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001556 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001557 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001558 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001559 // fold (srl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001560 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001561 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001562 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001563 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001564 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001565 // if (srl x, c) is known to be zero, return 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001566 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001567 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001568 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001569 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001570 N0.getOperand(1).getOpcode() == ISD::Constant) {
1571 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001572 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001573 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001574 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001575 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001576 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001577 }
Chris Lattner350bec02006-04-02 06:11:11 +00001578
Chris Lattner06afe072006-05-05 22:53:17 +00001579 // fold (srl (anyextend x), c) -> (anyextend (srl x, c))
1580 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
1581 // Shifting in all undef bits?
1582 MVT::ValueType SmallVT = N0.getOperand(0).getValueType();
1583 if (N1C->getValue() >= MVT::getSizeInBits(SmallVT))
1584 return DAG.getNode(ISD::UNDEF, VT);
1585
1586 SDOperand SmallShift = DAG.getNode(ISD::SRL, SmallVT, N0.getOperand(0), N1);
1587 AddToWorkList(SmallShift.Val);
1588 return DAG.getNode(ISD::ANY_EXTEND, VT, SmallShift);
1589 }
1590
Chris Lattner350bec02006-04-02 06:11:11 +00001591 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
1592 if (N1C && N0.getOpcode() == ISD::CTLZ &&
1593 N1C->getValue() == Log2_32(MVT::getSizeInBits(VT))) {
1594 uint64_t KnownZero, KnownOne, Mask = MVT::getIntVTBitMask(VT);
1595 TLI.ComputeMaskedBits(N0.getOperand(0), Mask, KnownZero, KnownOne);
1596
1597 // If any of the input bits are KnownOne, then the input couldn't be all
1598 // zeros, thus the result of the srl will always be zero.
1599 if (KnownOne) return DAG.getConstant(0, VT);
1600
1601 // If all of the bits input the to ctlz node are known to be zero, then
1602 // the result of the ctlz is "32" and the result of the shift is one.
1603 uint64_t UnknownBits = ~KnownZero & Mask;
1604 if (UnknownBits == 0) return DAG.getConstant(1, VT);
1605
1606 // Otherwise, check to see if there is exactly one bit input to the ctlz.
1607 if ((UnknownBits & (UnknownBits-1)) == 0) {
1608 // Okay, we know that only that the single bit specified by UnknownBits
1609 // could be set on input to the CTLZ node. If this bit is set, the SRL
1610 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
1611 // to an SRL,XOR pair, which is likely to simplify more.
1612 unsigned ShAmt = CountTrailingZeros_64(UnknownBits);
1613 SDOperand Op = N0.getOperand(0);
1614 if (ShAmt) {
1615 Op = DAG.getNode(ISD::SRL, VT, Op,
1616 DAG.getConstant(ShAmt, TLI.getShiftAmountTy()));
1617 AddToWorkList(Op.Val);
1618 }
1619 return DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(1, VT));
1620 }
1621 }
1622
Nate Begeman83e75ec2005-09-06 04:43:02 +00001623 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001624}
1625
Nate Begeman83e75ec2005-09-06 04:43:02 +00001626SDOperand DAGCombiner::visitCTLZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001627 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00001628 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001629
1630 // fold (ctlz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00001631 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00001632 return DAG.getNode(ISD::CTLZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001633 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001634}
1635
Nate Begeman83e75ec2005-09-06 04:43:02 +00001636SDOperand DAGCombiner::visitCTTZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001637 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00001638 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001639
1640 // fold (cttz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00001641 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00001642 return DAG.getNode(ISD::CTTZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001643 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001644}
1645
Nate Begeman83e75ec2005-09-06 04:43:02 +00001646SDOperand DAGCombiner::visitCTPOP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001647 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00001648 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001649
1650 // fold (ctpop c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00001651 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00001652 return DAG.getNode(ISD::CTPOP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001653 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001654}
1655
Nate Begeman452d7be2005-09-16 00:54:12 +00001656SDOperand DAGCombiner::visitSELECT(SDNode *N) {
1657 SDOperand N0 = N->getOperand(0);
1658 SDOperand N1 = N->getOperand(1);
1659 SDOperand N2 = N->getOperand(2);
1660 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1661 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1662 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1663 MVT::ValueType VT = N->getValueType(0);
Nate Begeman44728a72005-09-19 22:34:01 +00001664
Nate Begeman452d7be2005-09-16 00:54:12 +00001665 // fold select C, X, X -> X
1666 if (N1 == N2)
1667 return N1;
1668 // fold select true, X, Y -> X
1669 if (N0C && !N0C->isNullValue())
1670 return N1;
1671 // fold select false, X, Y -> Y
1672 if (N0C && N0C->isNullValue())
1673 return N2;
1674 // fold select C, 1, X -> C | X
Nate Begeman44728a72005-09-19 22:34:01 +00001675 if (MVT::i1 == VT && N1C && N1C->getValue() == 1)
Nate Begeman452d7be2005-09-16 00:54:12 +00001676 return DAG.getNode(ISD::OR, VT, N0, N2);
1677 // fold select C, 0, X -> ~C & X
1678 // FIXME: this should check for C type == X type, not i1?
1679 if (MVT::i1 == VT && N1C && N1C->isNullValue()) {
1680 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00001681 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00001682 return DAG.getNode(ISD::AND, VT, XORNode, N2);
1683 }
1684 // fold select C, X, 1 -> ~C | X
Nate Begeman44728a72005-09-19 22:34:01 +00001685 if (MVT::i1 == VT && N2C && N2C->getValue() == 1) {
Nate Begeman452d7be2005-09-16 00:54:12 +00001686 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00001687 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00001688 return DAG.getNode(ISD::OR, VT, XORNode, N1);
1689 }
1690 // fold select C, X, 0 -> C & X
1691 // FIXME: this should check for C type == X type, not i1?
1692 if (MVT::i1 == VT && N2C && N2C->isNullValue())
1693 return DAG.getNode(ISD::AND, VT, N0, N1);
1694 // fold X ? X : Y --> X ? 1 : Y --> X | Y
1695 if (MVT::i1 == VT && N0 == N1)
1696 return DAG.getNode(ISD::OR, VT, N0, N2);
1697 // fold X ? Y : X --> X ? Y : 0 --> X & Y
1698 if (MVT::i1 == VT && N0 == N2)
1699 return DAG.getNode(ISD::AND, VT, N0, N1);
Chris Lattner729c6d12006-05-27 00:43:02 +00001700
Chris Lattner40c62d52005-10-18 06:04:22 +00001701 // If we can fold this based on the true/false value, do so.
1702 if (SimplifySelectOps(N, N1, N2))
Chris Lattner729c6d12006-05-27 00:43:02 +00001703 return SDOperand(N, 0); // Don't revisit N.
1704
Nate Begeman44728a72005-09-19 22:34:01 +00001705 // fold selects based on a setcc into other things, such as min/max/abs
1706 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman750ac1b2006-02-01 07:19:44 +00001707 // FIXME:
1708 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
1709 // having to say they don't support SELECT_CC on every type the DAG knows
1710 // about, since there is no way to mark an opcode illegal at all value types
1711 if (TLI.isOperationLegal(ISD::SELECT_CC, MVT::Other))
1712 return DAG.getNode(ISD::SELECT_CC, VT, N0.getOperand(0), N0.getOperand(1),
1713 N1, N2, N0.getOperand(2));
1714 else
1715 return SimplifySelect(N0, N1, N2);
Nate Begeman452d7be2005-09-16 00:54:12 +00001716 return SDOperand();
1717}
1718
1719SDOperand DAGCombiner::visitSELECT_CC(SDNode *N) {
Nate Begeman44728a72005-09-19 22:34:01 +00001720 SDOperand N0 = N->getOperand(0);
1721 SDOperand N1 = N->getOperand(1);
1722 SDOperand N2 = N->getOperand(2);
1723 SDOperand N3 = N->getOperand(3);
1724 SDOperand N4 = N->getOperand(4);
1725 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1726 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1727 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1728 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
1729
Nate Begeman44728a72005-09-19 22:34:01 +00001730 // fold select_cc lhs, rhs, x, x, cc -> x
1731 if (N2 == N3)
1732 return N2;
Chris Lattner40c62d52005-10-18 06:04:22 +00001733
Chris Lattner5f42a242006-09-20 06:19:26 +00001734 // Determine if the condition we're dealing with is constant
1735 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
1736
1737 if (ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val)) {
1738 if (SCCC->getValue())
1739 return N2; // cond always true -> true val
1740 else
1741 return N3; // cond always false -> false val
1742 }
1743
1744 // Fold to a simpler select_cc
1745 if (SCC.Val && SCC.getOpcode() == ISD::SETCC)
1746 return DAG.getNode(ISD::SELECT_CC, N2.getValueType(),
1747 SCC.getOperand(0), SCC.getOperand(1), N2, N3,
1748 SCC.getOperand(2));
1749
Chris Lattner40c62d52005-10-18 06:04:22 +00001750 // If we can fold this based on the true/false value, do so.
1751 if (SimplifySelectOps(N, N2, N3))
Chris Lattner729c6d12006-05-27 00:43:02 +00001752 return SDOperand(N, 0); // Don't revisit N.
Chris Lattner40c62d52005-10-18 06:04:22 +00001753
Nate Begeman44728a72005-09-19 22:34:01 +00001754 // fold select_cc into other things, such as min/max/abs
1755 return SimplifySelectCC(N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00001756}
1757
1758SDOperand DAGCombiner::visitSETCC(SDNode *N) {
1759 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
1760 cast<CondCodeSDNode>(N->getOperand(2))->get());
1761}
1762
Nate Begeman83e75ec2005-09-06 04:43:02 +00001763SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001764 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001765 MVT::ValueType VT = N->getValueType(0);
1766
Nate Begeman1d4d4142005-09-01 00:19:25 +00001767 // fold (sext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00001768 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00001769 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0);
Chris Lattner310b5782006-05-06 23:06:26 +00001770
Nate Begeman1d4d4142005-09-01 00:19:25 +00001771 // fold (sext (sext x)) -> (sext x)
Chris Lattner310b5782006-05-06 23:06:26 +00001772 // fold (sext (aext x)) -> (sext x)
1773 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001774 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
Chris Lattner310b5782006-05-06 23:06:26 +00001775
Chris Lattner6007b842006-09-21 06:00:20 +00001776 // fold (sext (truncate x)) -> (sextinreg x).
1777 if (N0.getOpcode() == ISD::TRUNCATE &&
Chris Lattnerbf370872006-09-21 06:17:39 +00001778 (!AfterLegalize || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG,
1779 N0.getValueType()))) {
Chris Lattner6007b842006-09-21 06:00:20 +00001780 SDOperand Op = N0.getOperand(0);
1781 if (Op.getValueType() < VT) {
1782 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
1783 } else if (Op.getValueType() > VT) {
1784 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
1785 }
1786 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, Op,
Chris Lattnerb14ab8a2005-12-07 07:11:03 +00001787 DAG.getValueType(N0.getValueType()));
Chris Lattner6007b842006-09-21 06:00:20 +00001788 }
Chris Lattner310b5782006-05-06 23:06:26 +00001789
Evan Cheng110dec22005-12-14 02:19:23 +00001790 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Chris Lattnerd0f6d182005-12-15 19:02:38 +00001791 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() &&
1792 (!AfterLegalize||TLI.isOperationLegal(ISD::SEXTLOAD, N0.getValueType()))){
Nate Begeman3df4d522005-10-12 20:40:40 +00001793 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1794 N0.getOperand(1), N0.getOperand(2),
1795 N0.getValueType());
Chris Lattnerd4771842005-12-14 19:25:30 +00001796 CombineTo(N, ExtLoad);
Chris Lattnerf9884052005-10-13 21:52:31 +00001797 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1798 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001799 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begeman3df4d522005-10-12 20:40:40 +00001800 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001801
1802 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
1803 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
1804 if ((N0.getOpcode() == ISD::SEXTLOAD || N0.getOpcode() == ISD::EXTLOAD) &&
1805 N0.hasOneUse()) {
Nate Begeman5c742682006-05-08 01:35:01 +00001806 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
1807 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1808 N0.getOperand(1), N0.getOperand(2), EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00001809 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001810 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1811 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001812 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001813 }
1814
Nate Begeman83e75ec2005-09-06 04:43:02 +00001815 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001816}
1817
Nate Begeman83e75ec2005-09-06 04:43:02 +00001818SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001819 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001820 MVT::ValueType VT = N->getValueType(0);
1821
Nate Begeman1d4d4142005-09-01 00:19:25 +00001822 // fold (zext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00001823 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00001824 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001825 // fold (zext (zext x)) -> (zext x)
Chris Lattner310b5782006-05-06 23:06:26 +00001826 // fold (zext (aext x)) -> (zext x)
1827 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001828 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
Chris Lattner6007b842006-09-21 06:00:20 +00001829
1830 // fold (zext (truncate x)) -> (and x, mask)
1831 if (N0.getOpcode() == ISD::TRUNCATE &&
1832 (!AfterLegalize || TLI.isOperationLegal(ISD::AND, VT))) {
1833 SDOperand Op = N0.getOperand(0);
1834 if (Op.getValueType() < VT) {
1835 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
1836 } else if (Op.getValueType() > VT) {
1837 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
1838 }
1839 return DAG.getZeroExtendInReg(Op, N0.getValueType());
1840 }
1841
Chris Lattner111c2282006-09-21 06:14:31 +00001842 // fold (zext (and (trunc x), cst)) -> (and x, cst).
1843 if (N0.getOpcode() == ISD::AND &&
1844 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
1845 N0.getOperand(1).getOpcode() == ISD::Constant) {
1846 SDOperand X = N0.getOperand(0).getOperand(0);
1847 if (X.getValueType() < VT) {
1848 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
1849 } else if (X.getValueType() > VT) {
1850 X = DAG.getNode(ISD::TRUNCATE, VT, X);
1851 }
1852 uint64_t Mask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1853 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
1854 }
1855
Evan Cheng110dec22005-12-14 02:19:23 +00001856 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Chris Lattnerd0f6d182005-12-15 19:02:38 +00001857 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() &&
1858 (!AfterLegalize||TLI.isOperationLegal(ISD::ZEXTLOAD, N0.getValueType()))){
Evan Cheng110dec22005-12-14 02:19:23 +00001859 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1860 N0.getOperand(1), N0.getOperand(2),
1861 N0.getValueType());
Chris Lattnerd4771842005-12-14 19:25:30 +00001862 CombineTo(N, ExtLoad);
Evan Cheng110dec22005-12-14 02:19:23 +00001863 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1864 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001865 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng110dec22005-12-14 02:19:23 +00001866 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001867
1868 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
1869 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
1870 if ((N0.getOpcode() == ISD::ZEXTLOAD || N0.getOpcode() == ISD::EXTLOAD) &&
1871 N0.hasOneUse()) {
Nate Begeman5c742682006-05-08 01:35:01 +00001872 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
1873 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1874 N0.getOperand(1), N0.getOperand(2), EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00001875 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001876 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1877 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001878 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001879 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001880 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001881}
1882
Chris Lattner5ffc0662006-05-05 05:58:59 +00001883SDOperand DAGCombiner::visitANY_EXTEND(SDNode *N) {
1884 SDOperand N0 = N->getOperand(0);
Chris Lattner5ffc0662006-05-05 05:58:59 +00001885 MVT::ValueType VT = N->getValueType(0);
1886
1887 // fold (aext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00001888 if (isa<ConstantSDNode>(N0))
Chris Lattner5ffc0662006-05-05 05:58:59 +00001889 return DAG.getNode(ISD::ANY_EXTEND, VT, N0);
1890 // fold (aext (aext x)) -> (aext x)
1891 // fold (aext (zext x)) -> (zext x)
1892 // fold (aext (sext x)) -> (sext x)
1893 if (N0.getOpcode() == ISD::ANY_EXTEND ||
1894 N0.getOpcode() == ISD::ZERO_EXTEND ||
1895 N0.getOpcode() == ISD::SIGN_EXTEND)
1896 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
1897
Chris Lattner84750582006-09-20 06:29:17 +00001898 // fold (aext (truncate x))
1899 if (N0.getOpcode() == ISD::TRUNCATE) {
1900 SDOperand TruncOp = N0.getOperand(0);
1901 if (TruncOp.getValueType() == VT)
1902 return TruncOp; // x iff x size == zext size.
1903 if (TruncOp.getValueType() > VT)
1904 return DAG.getNode(ISD::TRUNCATE, VT, TruncOp);
1905 return DAG.getNode(ISD::ANY_EXTEND, VT, TruncOp);
1906 }
Chris Lattner0e4b9222006-09-21 06:40:43 +00001907
1908 // fold (aext (and (trunc x), cst)) -> (and x, cst).
1909 if (N0.getOpcode() == ISD::AND &&
1910 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
1911 N0.getOperand(1).getOpcode() == ISD::Constant) {
1912 SDOperand X = N0.getOperand(0).getOperand(0);
1913 if (X.getValueType() < VT) {
1914 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
1915 } else if (X.getValueType() > VT) {
1916 X = DAG.getNode(ISD::TRUNCATE, VT, X);
1917 }
1918 uint64_t Mask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1919 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
1920 }
1921
Chris Lattner5ffc0662006-05-05 05:58:59 +00001922 // fold (aext (load x)) -> (aext (truncate (extload x)))
1923 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() &&
1924 (!AfterLegalize||TLI.isOperationLegal(ISD::EXTLOAD, N0.getValueType()))) {
1925 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, N0.getOperand(0),
1926 N0.getOperand(1), N0.getOperand(2),
1927 N0.getValueType());
1928 CombineTo(N, ExtLoad);
1929 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1930 ExtLoad.getValue(1));
1931 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1932 }
1933
1934 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
1935 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
1936 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
1937 if ((N0.getOpcode() == ISD::ZEXTLOAD || N0.getOpcode() == ISD::EXTLOAD ||
1938 N0.getOpcode() == ISD::SEXTLOAD) &&
1939 N0.hasOneUse()) {
Nate Begeman5c742682006-05-08 01:35:01 +00001940 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
1941 SDOperand ExtLoad = DAG.getExtLoad(N0.getOpcode(), VT, N0.getOperand(0),
1942 N0.getOperand(1), N0.getOperand(2), EVT);
Chris Lattner5ffc0662006-05-05 05:58:59 +00001943 CombineTo(N, ExtLoad);
1944 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1945 ExtLoad.getValue(1));
1946 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1947 }
1948 return SDOperand();
1949}
1950
1951
Nate Begeman83e75ec2005-09-06 04:43:02 +00001952SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001953 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001954 SDOperand N1 = N->getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001955 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001956 MVT::ValueType EVT = cast<VTSDNode>(N1)->getVT();
Nate Begeman07ed4172005-10-10 21:26:48 +00001957 unsigned EVTBits = MVT::getSizeInBits(EVT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001958
Nate Begeman1d4d4142005-09-01 00:19:25 +00001959 // fold (sext_in_reg c1) -> c1
Chris Lattnereaeda562006-05-08 20:59:41 +00001960 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
Chris Lattner310b5782006-05-06 23:06:26 +00001961 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0, N1);
Chris Lattneree4ea922006-05-06 09:30:03 +00001962
Chris Lattner541a24f2006-05-06 22:43:44 +00001963 // If the input is already sign extended, just drop the extension.
Chris Lattneree4ea922006-05-06 09:30:03 +00001964 if (TLI.ComputeNumSignBits(N0) >= MVT::getSizeInBits(VT)-EVTBits+1)
1965 return N0;
1966
Nate Begeman646d7e22005-09-02 21:18:40 +00001967 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
1968 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1969 EVT < cast<VTSDNode>(N0.getOperand(1))->getVT()) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001970 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001971 }
Chris Lattner4b37e872006-05-08 21:18:59 +00001972
Nate Begeman07ed4172005-10-10 21:26:48 +00001973 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is zero
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001974 if (TLI.MaskedValueIsZero(N0, 1ULL << (EVTBits-1)))
Nate Begemande996292006-02-03 22:24:05 +00001975 return DAG.getZeroExtendInReg(N0, EVT);
Chris Lattner4b37e872006-05-08 21:18:59 +00001976
1977 // fold (sext_in_reg (srl X, 24), i8) -> sra X, 24
1978 // fold (sext_in_reg (srl X, 23), i8) -> sra X, 23 iff possible.
1979 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
1980 if (N0.getOpcode() == ISD::SRL) {
1981 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
1982 if (ShAmt->getValue()+EVTBits <= MVT::getSizeInBits(VT)) {
1983 // We can turn this into an SRA iff the input to the SRL is already sign
1984 // extended enough.
1985 unsigned InSignBits = TLI.ComputeNumSignBits(N0.getOperand(0));
1986 if (MVT::getSizeInBits(VT)-(ShAmt->getValue()+EVTBits) < InSignBits)
1987 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0), N0.getOperand(1));
1988 }
1989 }
1990
Nate Begemanded49632005-10-13 03:11:28 +00001991 // fold (sext_inreg (extload x)) -> (sextload x)
1992 if (N0.getOpcode() == ISD::EXTLOAD &&
1993 EVT == cast<VTSDNode>(N0.getOperand(3))->getVT() &&
Nate Begemanbfd65a02005-10-13 18:34:58 +00001994 (!AfterLegalize || TLI.isOperationLegal(ISD::SEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001995 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1996 N0.getOperand(1), N0.getOperand(2),
1997 EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00001998 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00001999 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002000 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00002001 }
2002 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Chris Lattner40c62d52005-10-18 06:04:22 +00002003 if (N0.getOpcode() == ISD::ZEXTLOAD && N0.hasOneUse() &&
Nate Begemanded49632005-10-13 03:11:28 +00002004 EVT == cast<VTSDNode>(N0.getOperand(3))->getVT() &&
Nate Begemanbfd65a02005-10-13 18:34:58 +00002005 (!AfterLegalize || TLI.isOperationLegal(ISD::SEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00002006 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
2007 N0.getOperand(1), N0.getOperand(2),
2008 EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00002009 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00002010 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002011 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00002012 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00002013 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002014}
2015
Nate Begeman83e75ec2005-09-06 04:43:02 +00002016SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002017 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002018 MVT::ValueType VT = N->getValueType(0);
2019
2020 // noop truncate
2021 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002022 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002023 // fold (truncate c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00002024 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002025 return DAG.getNode(ISD::TRUNCATE, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002026 // fold (truncate (truncate x)) -> (truncate x)
2027 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002028 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002029 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
Chris Lattnerb72773b2006-05-05 22:56:26 +00002030 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND||
2031 N0.getOpcode() == ISD::ANY_EXTEND) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002032 if (N0.getValueType() < VT)
2033 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00002034 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002035 else if (N0.getValueType() > VT)
2036 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00002037 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002038 else
2039 // if the source and dest are the same type, we can drop both the extend
2040 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00002041 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002042 }
Nate Begeman3df4d522005-10-12 20:40:40 +00002043 // fold (truncate (load x)) -> (smaller load x)
Chris Lattner40c62d52005-10-18 06:04:22 +00002044 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse()) {
Nate Begeman3df4d522005-10-12 20:40:40 +00002045 assert(MVT::getSizeInBits(N0.getValueType()) > MVT::getSizeInBits(VT) &&
2046 "Cannot truncate to larger type!");
2047 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
Nate Begeman765784a2005-10-12 23:18:53 +00002048 // For big endian targets, we need to add an offset to the pointer to load
2049 // the correct bytes. For little endian systems, we merely need to read
2050 // fewer bytes from the same pointer.
Nate Begeman3df4d522005-10-12 20:40:40 +00002051 uint64_t PtrOff =
2052 (MVT::getSizeInBits(N0.getValueType()) - MVT::getSizeInBits(VT)) / 8;
Nate Begeman765784a2005-10-12 23:18:53 +00002053 SDOperand NewPtr = TLI.isLittleEndian() ? N0.getOperand(1) :
2054 DAG.getNode(ISD::ADD, PtrType, N0.getOperand(1),
2055 DAG.getConstant(PtrOff, PtrType));
Chris Lattner5750df92006-03-01 04:03:14 +00002056 AddToWorkList(NewPtr.Val);
Nate Begeman3df4d522005-10-12 20:40:40 +00002057 SDOperand Load = DAG.getLoad(VT, N0.getOperand(0), NewPtr,N0.getOperand(2));
Chris Lattner5750df92006-03-01 04:03:14 +00002058 AddToWorkList(N);
Chris Lattner24edbb72005-10-13 22:10:05 +00002059 CombineTo(N0.Val, Load, Load.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002060 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begeman3df4d522005-10-12 20:40:40 +00002061 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00002062 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002063}
2064
Chris Lattner94683772005-12-23 05:30:37 +00002065SDOperand DAGCombiner::visitBIT_CONVERT(SDNode *N) {
2066 SDOperand N0 = N->getOperand(0);
2067 MVT::ValueType VT = N->getValueType(0);
2068
2069 // If the input is a constant, let getNode() fold it.
2070 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
2071 SDOperand Res = DAG.getNode(ISD::BIT_CONVERT, VT, N0);
2072 if (Res.Val != N) return Res;
2073 }
2074
Chris Lattnerc8547d82005-12-23 05:37:50 +00002075 if (N0.getOpcode() == ISD::BIT_CONVERT) // conv(conv(x,t1),t2) -> conv(x,t2)
2076 return DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
Chris Lattner6258fb22006-04-02 02:53:43 +00002077
Chris Lattner57104102005-12-23 05:44:41 +00002078 // fold (conv (load x)) -> (load (conv*)x)
Chris Lattnerbf40c4b2006-01-15 18:58:59 +00002079 // FIXME: These xforms need to know that the resultant load doesn't need a
2080 // higher alignment than the original!
2081 if (0 && N0.getOpcode() == ISD::LOAD && N0.hasOneUse()) {
Chris Lattner57104102005-12-23 05:44:41 +00002082 SDOperand Load = DAG.getLoad(VT, N0.getOperand(0), N0.getOperand(1),
2083 N0.getOperand(2));
Chris Lattner5750df92006-03-01 04:03:14 +00002084 AddToWorkList(N);
Chris Lattner57104102005-12-23 05:44:41 +00002085 CombineTo(N0.Val, DAG.getNode(ISD::BIT_CONVERT, N0.getValueType(), Load),
2086 Load.getValue(1));
2087 return Load;
2088 }
2089
Chris Lattner94683772005-12-23 05:30:37 +00002090 return SDOperand();
2091}
2092
Chris Lattner6258fb22006-04-02 02:53:43 +00002093SDOperand DAGCombiner::visitVBIT_CONVERT(SDNode *N) {
2094 SDOperand N0 = N->getOperand(0);
2095 MVT::ValueType VT = N->getValueType(0);
2096
2097 // If the input is a VBUILD_VECTOR with all constant elements, fold this now.
2098 // First check to see if this is all constant.
2099 if (N0.getOpcode() == ISD::VBUILD_VECTOR && N0.Val->hasOneUse() &&
2100 VT == MVT::Vector) {
2101 bool isSimple = true;
2102 for (unsigned i = 0, e = N0.getNumOperands()-2; i != e; ++i)
2103 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
2104 N0.getOperand(i).getOpcode() != ISD::Constant &&
2105 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
2106 isSimple = false;
2107 break;
2108 }
2109
Chris Lattner97c20732006-04-03 17:29:28 +00002110 MVT::ValueType DestEltVT = cast<VTSDNode>(N->getOperand(2))->getVT();
2111 if (isSimple && !MVT::isVector(DestEltVT)) {
Chris Lattner6258fb22006-04-02 02:53:43 +00002112 return ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(N0.Val, DestEltVT);
2113 }
2114 }
2115
2116 return SDOperand();
2117}
2118
2119/// ConstantFoldVBIT_CONVERTofVBUILD_VECTOR - We know that BV is a vbuild_vector
2120/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
2121/// destination element value type.
2122SDOperand DAGCombiner::
2123ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(SDNode *BV, MVT::ValueType DstEltVT) {
2124 MVT::ValueType SrcEltVT = BV->getOperand(0).getValueType();
2125
2126 // If this is already the right type, we're done.
2127 if (SrcEltVT == DstEltVT) return SDOperand(BV, 0);
2128
2129 unsigned SrcBitSize = MVT::getSizeInBits(SrcEltVT);
2130 unsigned DstBitSize = MVT::getSizeInBits(DstEltVT);
2131
2132 // If this is a conversion of N elements of one type to N elements of another
2133 // type, convert each element. This handles FP<->INT cases.
2134 if (SrcBitSize == DstBitSize) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002135 SmallVector<SDOperand, 8> Ops;
Chris Lattner3e104b12006-04-08 04:15:24 +00002136 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00002137 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, DstEltVT, BV->getOperand(i)));
Chris Lattner3e104b12006-04-08 04:15:24 +00002138 AddToWorkList(Ops.back().Val);
2139 }
Chris Lattner6258fb22006-04-02 02:53:43 +00002140 Ops.push_back(*(BV->op_end()-2)); // Add num elements.
2141 Ops.push_back(DAG.getValueType(DstEltVT));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002142 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00002143 }
2144
2145 // Otherwise, we're growing or shrinking the elements. To avoid having to
2146 // handle annoying details of growing/shrinking FP values, we convert them to
2147 // int first.
2148 if (MVT::isFloatingPoint(SrcEltVT)) {
2149 // Convert the input float vector to a int vector where the elements are the
2150 // same sizes.
2151 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
2152 MVT::ValueType IntVT = SrcEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
2153 BV = ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(BV, IntVT).Val;
2154 SrcEltVT = IntVT;
2155 }
2156
2157 // Now we know the input is an integer vector. If the output is a FP type,
2158 // convert to integer first, then to FP of the right size.
2159 if (MVT::isFloatingPoint(DstEltVT)) {
2160 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
2161 MVT::ValueType TmpVT = DstEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
2162 SDNode *Tmp = ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(BV, TmpVT).Val;
2163
2164 // Next, convert to FP elements of the same size.
2165 return ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(Tmp, DstEltVT);
2166 }
2167
2168 // Okay, we know the src/dst types are both integers of differing types.
2169 // Handling growing first.
2170 assert(MVT::isInteger(SrcEltVT) && MVT::isInteger(DstEltVT));
2171 if (SrcBitSize < DstBitSize) {
2172 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
2173
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002174 SmallVector<SDOperand, 8> Ops;
Chris Lattner6258fb22006-04-02 02:53:43 +00002175 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e;
2176 i += NumInputsPerOutput) {
2177 bool isLE = TLI.isLittleEndian();
2178 uint64_t NewBits = 0;
2179 bool EltIsUndef = true;
2180 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
2181 // Shift the previously computed bits over.
2182 NewBits <<= SrcBitSize;
2183 SDOperand Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
2184 if (Op.getOpcode() == ISD::UNDEF) continue;
2185 EltIsUndef = false;
2186
2187 NewBits |= cast<ConstantSDNode>(Op)->getValue();
2188 }
2189
2190 if (EltIsUndef)
2191 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
2192 else
2193 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
2194 }
2195
2196 Ops.push_back(DAG.getConstant(Ops.size(), MVT::i32)); // Add num elements.
2197 Ops.push_back(DAG.getValueType(DstEltVT)); // Add element size.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002198 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00002199 }
2200
2201 // Finally, this must be the case where we are shrinking elements: each input
2202 // turns into multiple outputs.
2203 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002204 SmallVector<SDOperand, 8> Ops;
Chris Lattner6258fb22006-04-02 02:53:43 +00002205 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e; ++i) {
2206 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
2207 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
2208 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
2209 continue;
2210 }
2211 uint64_t OpVal = cast<ConstantSDNode>(BV->getOperand(i))->getValue();
2212
2213 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
2214 unsigned ThisVal = OpVal & ((1ULL << DstBitSize)-1);
2215 OpVal >>= DstBitSize;
2216 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
2217 }
2218
2219 // For big endian targets, swap the order of the pieces of each element.
2220 if (!TLI.isLittleEndian())
2221 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
2222 }
2223 Ops.push_back(DAG.getConstant(Ops.size(), MVT::i32)); // Add num elements.
2224 Ops.push_back(DAG.getValueType(DstEltVT)); // Add element size.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002225 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00002226}
2227
2228
2229
Chris Lattner01b3d732005-09-28 22:28:18 +00002230SDOperand DAGCombiner::visitFADD(SDNode *N) {
2231 SDOperand N0 = N->getOperand(0);
2232 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00002233 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2234 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002235 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00002236
2237 // fold (fadd c1, c2) -> c1+c2
2238 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002239 return DAG.getNode(ISD::FADD, VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00002240 // canonicalize constant to RHS
2241 if (N0CFP && !N1CFP)
2242 return DAG.getNode(ISD::FADD, VT, N1, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00002243 // fold (A + (-B)) -> A-B
2244 if (N1.getOpcode() == ISD::FNEG)
2245 return DAG.getNode(ISD::FSUB, VT, N0, N1.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00002246 // fold ((-A) + B) -> B-A
2247 if (N0.getOpcode() == ISD::FNEG)
2248 return DAG.getNode(ISD::FSUB, VT, N1, N0.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00002249 return SDOperand();
2250}
2251
2252SDOperand DAGCombiner::visitFSUB(SDNode *N) {
2253 SDOperand N0 = N->getOperand(0);
2254 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00002255 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2256 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002257 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00002258
2259 // fold (fsub c1, c2) -> c1-c2
2260 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002261 return DAG.getNode(ISD::FSUB, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002262 // fold (A-(-B)) -> A+B
2263 if (N1.getOpcode() == ISD::FNEG)
Nate Begemana148d982006-01-18 22:35:16 +00002264 return DAG.getNode(ISD::FADD, VT, N0, N1.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00002265 return SDOperand();
2266}
2267
2268SDOperand DAGCombiner::visitFMUL(SDNode *N) {
2269 SDOperand N0 = N->getOperand(0);
2270 SDOperand N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00002271 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2272 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002273 MVT::ValueType VT = N->getValueType(0);
2274
Nate Begeman11af4ea2005-10-17 20:40:11 +00002275 // fold (fmul c1, c2) -> c1*c2
2276 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002277 return DAG.getNode(ISD::FMUL, VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00002278 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00002279 if (N0CFP && !N1CFP)
2280 return DAG.getNode(ISD::FMUL, VT, N1, N0);
Nate Begeman11af4ea2005-10-17 20:40:11 +00002281 // fold (fmul X, 2.0) -> (fadd X, X)
2282 if (N1CFP && N1CFP->isExactlyValue(+2.0))
2283 return DAG.getNode(ISD::FADD, VT, N0, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00002284 return SDOperand();
2285}
2286
2287SDOperand DAGCombiner::visitFDIV(SDNode *N) {
2288 SDOperand N0 = N->getOperand(0);
2289 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00002290 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2291 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002292 MVT::ValueType VT = N->getValueType(0);
2293
Nate Begemana148d982006-01-18 22:35:16 +00002294 // fold (fdiv c1, c2) -> c1/c2
2295 if (N0CFP && N1CFP)
2296 return DAG.getNode(ISD::FDIV, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002297 return SDOperand();
2298}
2299
2300SDOperand DAGCombiner::visitFREM(SDNode *N) {
2301 SDOperand N0 = N->getOperand(0);
2302 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00002303 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2304 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002305 MVT::ValueType VT = N->getValueType(0);
2306
Nate Begemana148d982006-01-18 22:35:16 +00002307 // fold (frem c1, c2) -> fmod(c1,c2)
2308 if (N0CFP && N1CFP)
2309 return DAG.getNode(ISD::FREM, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002310 return SDOperand();
2311}
2312
Chris Lattner12d83032006-03-05 05:30:57 +00002313SDOperand DAGCombiner::visitFCOPYSIGN(SDNode *N) {
2314 SDOperand N0 = N->getOperand(0);
2315 SDOperand N1 = N->getOperand(1);
2316 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2317 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2318 MVT::ValueType VT = N->getValueType(0);
2319
2320 if (N0CFP && N1CFP) // Constant fold
2321 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1);
2322
2323 if (N1CFP) {
2324 // copysign(x, c1) -> fabs(x) iff ispos(c1)
2325 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
2326 union {
2327 double d;
2328 int64_t i;
2329 } u;
2330 u.d = N1CFP->getValue();
2331 if (u.i >= 0)
2332 return DAG.getNode(ISD::FABS, VT, N0);
2333 else
2334 return DAG.getNode(ISD::FNEG, VT, DAG.getNode(ISD::FABS, VT, N0));
2335 }
2336
2337 // copysign(fabs(x), y) -> copysign(x, y)
2338 // copysign(fneg(x), y) -> copysign(x, y)
2339 // copysign(copysign(x,z), y) -> copysign(x, y)
2340 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
2341 N0.getOpcode() == ISD::FCOPYSIGN)
2342 return DAG.getNode(ISD::FCOPYSIGN, VT, N0.getOperand(0), N1);
2343
2344 // copysign(x, abs(y)) -> abs(x)
2345 if (N1.getOpcode() == ISD::FABS)
2346 return DAG.getNode(ISD::FABS, VT, N0);
2347
2348 // copysign(x, copysign(y,z)) -> copysign(x, z)
2349 if (N1.getOpcode() == ISD::FCOPYSIGN)
2350 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(1));
2351
2352 // copysign(x, fp_extend(y)) -> copysign(x, y)
2353 // copysign(x, fp_round(y)) -> copysign(x, y)
2354 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
2355 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(0));
2356
2357 return SDOperand();
2358}
2359
2360
Chris Lattner01b3d732005-09-28 22:28:18 +00002361
Nate Begeman83e75ec2005-09-06 04:43:02 +00002362SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002363 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002364 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00002365 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002366
2367 // fold (sint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002368 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00002369 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002370 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002371}
2372
Nate Begeman83e75ec2005-09-06 04:43:02 +00002373SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002374 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002375 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00002376 MVT::ValueType VT = N->getValueType(0);
2377
Nate Begeman1d4d4142005-09-01 00:19:25 +00002378 // fold (uint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002379 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00002380 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002381 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002382}
2383
Nate Begeman83e75ec2005-09-06 04:43:02 +00002384SDOperand DAGCombiner::visitFP_TO_SINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002385 SDOperand N0 = N->getOperand(0);
2386 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2387 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002388
2389 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00002390 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002391 return DAG.getNode(ISD::FP_TO_SINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002392 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002393}
2394
Nate Begeman83e75ec2005-09-06 04:43:02 +00002395SDOperand DAGCombiner::visitFP_TO_UINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002396 SDOperand N0 = N->getOperand(0);
2397 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2398 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002399
2400 // fold (fp_to_uint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00002401 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002402 return DAG.getNode(ISD::FP_TO_UINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002403 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002404}
2405
Nate Begeman83e75ec2005-09-06 04:43:02 +00002406SDOperand DAGCombiner::visitFP_ROUND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002407 SDOperand N0 = N->getOperand(0);
2408 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2409 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002410
2411 // fold (fp_round c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002412 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002413 return DAG.getNode(ISD::FP_ROUND, VT, N0);
Chris Lattner79dbea52006-03-13 06:26:26 +00002414
2415 // fold (fp_round (fp_extend x)) -> x
2416 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
2417 return N0.getOperand(0);
2418
2419 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
2420 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.Val->hasOneUse()) {
2421 SDOperand Tmp = DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0));
2422 AddToWorkList(Tmp.Val);
2423 return DAG.getNode(ISD::FCOPYSIGN, VT, Tmp, N0.getOperand(1));
2424 }
2425
Nate Begeman83e75ec2005-09-06 04:43:02 +00002426 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002427}
2428
Nate Begeman83e75ec2005-09-06 04:43:02 +00002429SDOperand DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002430 SDOperand N0 = N->getOperand(0);
2431 MVT::ValueType VT = N->getValueType(0);
2432 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00002433 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002434
Nate Begeman1d4d4142005-09-01 00:19:25 +00002435 // fold (fp_round_inreg c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002436 if (N0CFP) {
2437 SDOperand Round = DAG.getConstantFP(N0CFP->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002438 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002439 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00002440 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002441}
2442
Nate Begeman83e75ec2005-09-06 04:43:02 +00002443SDOperand DAGCombiner::visitFP_EXTEND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002444 SDOperand N0 = N->getOperand(0);
2445 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2446 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002447
2448 // fold (fp_extend c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002449 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002450 return DAG.getNode(ISD::FP_EXTEND, VT, N0);
Chris Lattnere564dbb2006-05-05 21:34:35 +00002451
2452 // fold (fpext (load x)) -> (fpext (fpround (extload x)))
2453 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() &&
2454 (!AfterLegalize||TLI.isOperationLegal(ISD::EXTLOAD, N0.getValueType()))) {
2455 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, N0.getOperand(0),
2456 N0.getOperand(1), N0.getOperand(2),
2457 N0.getValueType());
2458 CombineTo(N, ExtLoad);
2459 CombineTo(N0.Val, DAG.getNode(ISD::FP_ROUND, N0.getValueType(), ExtLoad),
2460 ExtLoad.getValue(1));
2461 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2462 }
2463
2464
Nate Begeman83e75ec2005-09-06 04:43:02 +00002465 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002466}
2467
Nate Begeman83e75ec2005-09-06 04:43:02 +00002468SDOperand DAGCombiner::visitFNEG(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002469 SDOperand N0 = N->getOperand(0);
2470 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2471 MVT::ValueType VT = N->getValueType(0);
2472
2473 // fold (fneg c1) -> -c1
Nate Begeman646d7e22005-09-02 21:18:40 +00002474 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002475 return DAG.getNode(ISD::FNEG, VT, N0);
2476 // fold (fneg (sub x, y)) -> (sub y, x)
Chris Lattner12d83032006-03-05 05:30:57 +00002477 if (N0.getOpcode() == ISD::SUB)
2478 return DAG.getNode(ISD::SUB, VT, N0.getOperand(1), N0.getOperand(0));
Nate Begemana148d982006-01-18 22:35:16 +00002479 // fold (fneg (fneg x)) -> x
Chris Lattner12d83032006-03-05 05:30:57 +00002480 if (N0.getOpcode() == ISD::FNEG)
2481 return N0.getOperand(0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002482 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002483}
2484
Nate Begeman83e75ec2005-09-06 04:43:02 +00002485SDOperand DAGCombiner::visitFABS(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002486 SDOperand N0 = N->getOperand(0);
2487 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2488 MVT::ValueType VT = N->getValueType(0);
2489
Nate Begeman1d4d4142005-09-01 00:19:25 +00002490 // fold (fabs c1) -> fabs(c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00002491 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002492 return DAG.getNode(ISD::FABS, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002493 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00002494 if (N0.getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002495 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002496 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00002497 // fold (fabs (fcopysign x, y)) -> (fabs x)
2498 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
2499 return DAG.getNode(ISD::FABS, VT, N0.getOperand(0));
2500
Nate Begeman83e75ec2005-09-06 04:43:02 +00002501 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002502}
2503
Nate Begeman44728a72005-09-19 22:34:01 +00002504SDOperand DAGCombiner::visitBRCOND(SDNode *N) {
2505 SDOperand Chain = N->getOperand(0);
2506 SDOperand N1 = N->getOperand(1);
2507 SDOperand N2 = N->getOperand(2);
2508 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2509
2510 // never taken branch, fold to chain
2511 if (N1C && N1C->isNullValue())
2512 return Chain;
2513 // unconditional branch
Nate Begemane17daeb2005-10-05 21:43:42 +00002514 if (N1C && N1C->getValue() == 1)
Nate Begeman44728a72005-09-19 22:34:01 +00002515 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
Nate Begeman750ac1b2006-02-01 07:19:44 +00002516 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
2517 // on the target.
2518 if (N1.getOpcode() == ISD::SETCC &&
2519 TLI.isOperationLegal(ISD::BR_CC, MVT::Other)) {
2520 return DAG.getNode(ISD::BR_CC, MVT::Other, Chain, N1.getOperand(2),
2521 N1.getOperand(0), N1.getOperand(1), N2);
2522 }
Nate Begeman44728a72005-09-19 22:34:01 +00002523 return SDOperand();
2524}
2525
Chris Lattner3ea0b472005-10-05 06:47:48 +00002526// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
2527//
Nate Begeman44728a72005-09-19 22:34:01 +00002528SDOperand DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00002529 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
2530 SDOperand CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
2531
2532 // Use SimplifySetCC to simplify SETCC's.
Nate Begemane17daeb2005-10-05 21:43:42 +00002533 SDOperand Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false);
2534 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.Val);
2535
2536 // fold br_cc true, dest -> br dest (unconditional branch)
2537 if (SCCC && SCCC->getValue())
2538 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
2539 N->getOperand(4));
2540 // fold br_cc false, dest -> unconditional fall through
2541 if (SCCC && SCCC->isNullValue())
2542 return N->getOperand(0);
2543 // fold to a simpler setcc
2544 if (Simp.Val && Simp.getOpcode() == ISD::SETCC)
2545 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
2546 Simp.getOperand(2), Simp.getOperand(0),
2547 Simp.getOperand(1), N->getOperand(4));
Nate Begeman44728a72005-09-19 22:34:01 +00002548 return SDOperand();
2549}
2550
Chris Lattner01a22022005-10-10 22:04:48 +00002551SDOperand DAGCombiner::visitLOAD(SDNode *N) {
2552 SDOperand Chain = N->getOperand(0);
2553 SDOperand Ptr = N->getOperand(1);
2554 SDOperand SrcValue = N->getOperand(2);
Chris Lattnere4b95392006-03-31 18:06:18 +00002555
2556 // If there are no uses of the loaded value, change uses of the chain value
2557 // into uses of the chain input (i.e. delete the dead load).
2558 if (N->hasNUsesOfValue(0, 0))
2559 return CombineTo(N, DAG.getNode(ISD::UNDEF, N->getValueType(0)), Chain);
Chris Lattner01a22022005-10-10 22:04:48 +00002560
2561 // If this load is directly stored, replace the load value with the stored
2562 // value.
2563 // TODO: Handle store large -> read small portion.
2564 // TODO: Handle TRUNCSTORE/EXTLOAD
2565 if (Chain.getOpcode() == ISD::STORE && Chain.getOperand(2) == Ptr &&
2566 Chain.getOperand(1).getValueType() == N->getValueType(0))
2567 return CombineTo(N, Chain.getOperand(1), Chain);
2568
2569 return SDOperand();
2570}
2571
Chris Lattner29cd7db2006-03-31 18:10:41 +00002572/// visitXEXTLOAD - Handle EXTLOAD/ZEXTLOAD/SEXTLOAD.
2573SDOperand DAGCombiner::visitXEXTLOAD(SDNode *N) {
2574 SDOperand Chain = N->getOperand(0);
2575 SDOperand Ptr = N->getOperand(1);
2576 SDOperand SrcValue = N->getOperand(2);
2577 SDOperand EVT = N->getOperand(3);
2578
2579 // If there are no uses of the loaded value, change uses of the chain value
2580 // into uses of the chain input (i.e. delete the dead load).
2581 if (N->hasNUsesOfValue(0, 0))
2582 return CombineTo(N, DAG.getNode(ISD::UNDEF, N->getValueType(0)), Chain);
2583
2584 return SDOperand();
2585}
2586
Jim Laskeyd1aed7a2006-09-21 16:28:59 +00002587/// isNotAlias - Return true if we have definitive knowlege that the two
2588/// addresses don't overlap.
2589bool DAGCombiner::isNotAlias(SDOperand Ptr1, SDOperand Ptr2) {
2590 // Mind the flag.
2591 if (!CombinerAA) return false;
2592
Jim Laskey516b0ea2006-09-21 17:35:47 +00002593 // If they are the same then they must be aliases.
Jim Laskeyd1aed7a2006-09-21 16:28:59 +00002594 if (Ptr1 == Ptr2) return false;
2595
Jim Laskey516b0ea2006-09-21 17:35:47 +00002596 // If both operands are frame values (not the same location from above test)
Jim Laskeyd1aed7a2006-09-21 16:28:59 +00002597 // then they can't alias.
2598 FrameIndexSDNode *FI1 = dyn_cast<FrameIndexSDNode>(Ptr1);
2599 FrameIndexSDNode *FI2 = dyn_cast<FrameIndexSDNode>(Ptr2);
Jim Laskey516b0ea2006-09-21 17:35:47 +00002600 if (FI1 && FI2) {
Jim Laskeyd1aed7a2006-09-21 16:28:59 +00002601 return true;
2602 }
2603
2604 // Otherwise we don't know and have to play it safe.
2605 return false;
2606}
2607
Chris Lattner87514ca2005-10-10 22:31:19 +00002608SDOperand DAGCombiner::visitSTORE(SDNode *N) {
2609 SDOperand Chain = N->getOperand(0);
2610 SDOperand Value = N->getOperand(1);
2611 SDOperand Ptr = N->getOperand(2);
2612 SDOperand SrcValue = N->getOperand(3);
2613
2614 // If this is a store that kills a previous store, remove the previous store.
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002615 if (Chain.getOpcode() == ISD::STORE && Chain.getOperand(2) == Ptr &&
Chris Lattnerfe7f0462005-10-27 07:10:34 +00002616 Chain.Val->hasOneUse() /* Avoid introducing DAG cycles */ &&
2617 // Make sure that these stores are the same value type:
2618 // FIXME: we really care that the second store is >= size of the first.
2619 Value.getValueType() == Chain.getOperand(1).getValueType()) {
Chris Lattner87514ca2005-10-10 22:31:19 +00002620 // Create a new store of Value that replaces both stores.
2621 SDNode *PrevStore = Chain.Val;
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002622 if (PrevStore->getOperand(1) == Value) // Same value multiply stored.
2623 return Chain;
Chris Lattner87514ca2005-10-10 22:31:19 +00002624 SDOperand NewStore = DAG.getNode(ISD::STORE, MVT::Other,
2625 PrevStore->getOperand(0), Value, Ptr,
2626 SrcValue);
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002627 CombineTo(N, NewStore); // Nuke this store.
Chris Lattner87514ca2005-10-10 22:31:19 +00002628 CombineTo(PrevStore, NewStore); // Nuke the previous store.
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002629 return SDOperand(N, 0);
Chris Lattner87514ca2005-10-10 22:31:19 +00002630 }
2631
Chris Lattnerc33baaa2005-12-23 05:48:07 +00002632 // If this is a store of a bit convert, store the input value.
Chris Lattnerbf40c4b2006-01-15 18:58:59 +00002633 // FIXME: This needs to know that the resultant store does not need a
2634 // higher alignment than the original.
2635 if (0 && Value.getOpcode() == ISD::BIT_CONVERT)
Chris Lattnerc33baaa2005-12-23 05:48:07 +00002636 return DAG.getNode(ISD::STORE, MVT::Other, Chain, Value.getOperand(0),
2637 Ptr, SrcValue);
Jim Laskeyd1aed7a2006-09-21 16:28:59 +00002638
2639 // If the previous store is not an alias then break artificial chain.
2640 if (Chain.getOpcode() == ISD::STORE && isNotAlias(Ptr, Chain.getOperand(2))) {
2641 // Replace the chain to void dependency.
2642 SDNode *PrevStore = Chain.Val;
2643 SDOperand ReplStore = DAG.getNode(ISD::STORE, MVT::Other,
2644 PrevStore->getOperand(0), Value, Ptr,
2645 SrcValue);
2646 // Create token to keep both stores around.
2647 SDOperand Token = DAG.getNode(ISD::TokenFactor, MVT::Other,
2648 Chain, ReplStore);
2649 // Replace uses with token.
Jim Laskey516b0ea2006-09-21 17:35:47 +00002650 return Token;
Jim Laskeyd1aed7a2006-09-21 16:28:59 +00002651 }
Chris Lattnerc33baaa2005-12-23 05:48:07 +00002652
Chris Lattner87514ca2005-10-10 22:31:19 +00002653 return SDOperand();
2654}
2655
Chris Lattnerca242442006-03-19 01:27:56 +00002656SDOperand DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
2657 SDOperand InVec = N->getOperand(0);
2658 SDOperand InVal = N->getOperand(1);
2659 SDOperand EltNo = N->getOperand(2);
2660
2661 // If the invec is a BUILD_VECTOR and if EltNo is a constant, build a new
2662 // vector with the inserted element.
2663 if (InVec.getOpcode() == ISD::BUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
2664 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002665 SmallVector<SDOperand, 8> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
Chris Lattnerca242442006-03-19 01:27:56 +00002666 if (Elt < Ops.size())
2667 Ops[Elt] = InVal;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002668 return DAG.getNode(ISD::BUILD_VECTOR, InVec.getValueType(),
2669 &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00002670 }
2671
2672 return SDOperand();
2673}
2674
2675SDOperand DAGCombiner::visitVINSERT_VECTOR_ELT(SDNode *N) {
2676 SDOperand InVec = N->getOperand(0);
2677 SDOperand InVal = N->getOperand(1);
2678 SDOperand EltNo = N->getOperand(2);
2679 SDOperand NumElts = N->getOperand(3);
2680 SDOperand EltType = N->getOperand(4);
2681
2682 // If the invec is a VBUILD_VECTOR and if EltNo is a constant, build a new
2683 // vector with the inserted element.
2684 if (InVec.getOpcode() == ISD::VBUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
2685 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002686 SmallVector<SDOperand, 8> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
Chris Lattnerca242442006-03-19 01:27:56 +00002687 if (Elt < Ops.size()-2)
2688 Ops[Elt] = InVal;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002689 return DAG.getNode(ISD::VBUILD_VECTOR, InVec.getValueType(),
2690 &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00002691 }
2692
2693 return SDOperand();
2694}
2695
Chris Lattnerd7648c82006-03-28 20:28:38 +00002696SDOperand DAGCombiner::visitVBUILD_VECTOR(SDNode *N) {
2697 unsigned NumInScalars = N->getNumOperands()-2;
2698 SDOperand NumElts = N->getOperand(NumInScalars);
2699 SDOperand EltType = N->getOperand(NumInScalars+1);
2700
2701 // Check to see if this is a VBUILD_VECTOR of a bunch of VEXTRACT_VECTOR_ELT
2702 // operations. If so, and if the EXTRACT_ELT vector inputs come from at most
2703 // two distinct vectors, turn this into a shuffle node.
2704 SDOperand VecIn1, VecIn2;
2705 for (unsigned i = 0; i != NumInScalars; ++i) {
2706 // Ignore undef inputs.
2707 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
2708
2709 // If this input is something other than a VEXTRACT_VECTOR_ELT with a
2710 // constant index, bail out.
2711 if (N->getOperand(i).getOpcode() != ISD::VEXTRACT_VECTOR_ELT ||
2712 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
2713 VecIn1 = VecIn2 = SDOperand(0, 0);
2714 break;
2715 }
2716
2717 // If the input vector type disagrees with the result of the vbuild_vector,
2718 // we can't make a shuffle.
2719 SDOperand ExtractedFromVec = N->getOperand(i).getOperand(0);
2720 if (*(ExtractedFromVec.Val->op_end()-2) != NumElts ||
2721 *(ExtractedFromVec.Val->op_end()-1) != EltType) {
2722 VecIn1 = VecIn2 = SDOperand(0, 0);
2723 break;
2724 }
2725
2726 // Otherwise, remember this. We allow up to two distinct input vectors.
2727 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
2728 continue;
2729
2730 if (VecIn1.Val == 0) {
2731 VecIn1 = ExtractedFromVec;
2732 } else if (VecIn2.Val == 0) {
2733 VecIn2 = ExtractedFromVec;
2734 } else {
2735 // Too many inputs.
2736 VecIn1 = VecIn2 = SDOperand(0, 0);
2737 break;
2738 }
2739 }
2740
2741 // If everything is good, we can make a shuffle operation.
2742 if (VecIn1.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002743 SmallVector<SDOperand, 8> BuildVecIndices;
Chris Lattnerd7648c82006-03-28 20:28:38 +00002744 for (unsigned i = 0; i != NumInScalars; ++i) {
2745 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
2746 BuildVecIndices.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
2747 continue;
2748 }
2749
2750 SDOperand Extract = N->getOperand(i);
2751
2752 // If extracting from the first vector, just use the index directly.
2753 if (Extract.getOperand(0) == VecIn1) {
2754 BuildVecIndices.push_back(Extract.getOperand(1));
2755 continue;
2756 }
2757
2758 // Otherwise, use InIdx + VecSize
2759 unsigned Idx = cast<ConstantSDNode>(Extract.getOperand(1))->getValue();
2760 BuildVecIndices.push_back(DAG.getConstant(Idx+NumInScalars, MVT::i32));
2761 }
2762
2763 // Add count and size info.
2764 BuildVecIndices.push_back(NumElts);
2765 BuildVecIndices.push_back(DAG.getValueType(MVT::i32));
2766
2767 // Return the new VVECTOR_SHUFFLE node.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002768 SDOperand Ops[5];
2769 Ops[0] = VecIn1;
Chris Lattnercef896e2006-03-28 22:19:47 +00002770 if (VecIn2.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002771 Ops[1] = VecIn2;
Chris Lattnercef896e2006-03-28 22:19:47 +00002772 } else {
2773 // Use an undef vbuild_vector as input for the second operand.
2774 std::vector<SDOperand> UnOps(NumInScalars,
2775 DAG.getNode(ISD::UNDEF,
2776 cast<VTSDNode>(EltType)->getVT()));
2777 UnOps.push_back(NumElts);
2778 UnOps.push_back(EltType);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002779 Ops[1] = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
2780 &UnOps[0], UnOps.size());
2781 AddToWorkList(Ops[1].Val);
Chris Lattnercef896e2006-03-28 22:19:47 +00002782 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002783 Ops[2] = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
2784 &BuildVecIndices[0], BuildVecIndices.size());
2785 Ops[3] = NumElts;
2786 Ops[4] = EltType;
2787 return DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector, Ops, 5);
Chris Lattnerd7648c82006-03-28 20:28:38 +00002788 }
2789
2790 return SDOperand();
2791}
2792
Chris Lattner66445d32006-03-28 22:11:53 +00002793SDOperand DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
Chris Lattnerf1d0c622006-03-31 22:16:43 +00002794 SDOperand ShufMask = N->getOperand(2);
2795 unsigned NumElts = ShufMask.getNumOperands();
2796
2797 // If the shuffle mask is an identity operation on the LHS, return the LHS.
2798 bool isIdentity = true;
2799 for (unsigned i = 0; i != NumElts; ++i) {
2800 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
2801 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
2802 isIdentity = false;
2803 break;
2804 }
2805 }
2806 if (isIdentity) return N->getOperand(0);
2807
2808 // If the shuffle mask is an identity operation on the RHS, return the RHS.
2809 isIdentity = true;
2810 for (unsigned i = 0; i != NumElts; ++i) {
2811 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
2812 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
2813 isIdentity = false;
2814 break;
2815 }
2816 }
2817 if (isIdentity) return N->getOperand(1);
Evan Chenge7bec0d2006-07-20 22:44:41 +00002818
2819 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
2820 // needed at all.
2821 bool isUnary = true;
Evan Cheng917ec982006-07-21 08:25:53 +00002822 bool isSplat = true;
Evan Chenge7bec0d2006-07-20 22:44:41 +00002823 int VecNum = -1;
Reid Spencer9160a6a2006-07-25 20:44:41 +00002824 unsigned BaseIdx = 0;
Evan Chenge7bec0d2006-07-20 22:44:41 +00002825 for (unsigned i = 0; i != NumElts; ++i)
2826 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
2827 unsigned Idx = cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue();
2828 int V = (Idx < NumElts) ? 0 : 1;
Evan Cheng917ec982006-07-21 08:25:53 +00002829 if (VecNum == -1) {
Evan Chenge7bec0d2006-07-20 22:44:41 +00002830 VecNum = V;
Evan Cheng917ec982006-07-21 08:25:53 +00002831 BaseIdx = Idx;
2832 } else {
2833 if (BaseIdx != Idx)
2834 isSplat = false;
2835 if (VecNum != V) {
2836 isUnary = false;
2837 break;
2838 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00002839 }
2840 }
2841
2842 SDOperand N0 = N->getOperand(0);
2843 SDOperand N1 = N->getOperand(1);
2844 // Normalize unary shuffle so the RHS is undef.
2845 if (isUnary && VecNum == 1)
2846 std::swap(N0, N1);
2847
Evan Cheng917ec982006-07-21 08:25:53 +00002848 // If it is a splat, check if the argument vector is a build_vector with
2849 // all scalar elements the same.
2850 if (isSplat) {
2851 SDNode *V = N0.Val;
2852 if (V->getOpcode() == ISD::BIT_CONVERT)
2853 V = V->getOperand(0).Val;
2854 if (V->getOpcode() == ISD::BUILD_VECTOR) {
2855 unsigned NumElems = V->getNumOperands()-2;
2856 if (NumElems > BaseIdx) {
2857 SDOperand Base;
2858 bool AllSame = true;
2859 for (unsigned i = 0; i != NumElems; ++i) {
2860 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
2861 Base = V->getOperand(i);
2862 break;
2863 }
2864 }
2865 // Splat of <u, u, u, u>, return <u, u, u, u>
2866 if (!Base.Val)
2867 return N0;
2868 for (unsigned i = 0; i != NumElems; ++i) {
2869 if (V->getOperand(i).getOpcode() != ISD::UNDEF &&
2870 V->getOperand(i) != Base) {
2871 AllSame = false;
2872 break;
2873 }
2874 }
2875 // Splat of <x, x, x, x>, return <x, x, x, x>
2876 if (AllSame)
2877 return N0;
2878 }
2879 }
2880 }
2881
Evan Chenge7bec0d2006-07-20 22:44:41 +00002882 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
2883 // into an undef.
2884 if (isUnary || N0 == N1) {
2885 if (N0.getOpcode() == ISD::UNDEF)
Evan Chengc04766a2006-04-06 23:20:43 +00002886 return DAG.getNode(ISD::UNDEF, N->getValueType(0));
Chris Lattner66445d32006-03-28 22:11:53 +00002887 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
2888 // first operand.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002889 SmallVector<SDOperand, 8> MappedOps;
Chris Lattner66445d32006-03-28 22:11:53 +00002890 for (unsigned i = 0, e = ShufMask.getNumOperands(); i != e; ++i) {
Evan Chengc04766a2006-04-06 23:20:43 +00002891 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
2892 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) {
2893 MappedOps.push_back(ShufMask.getOperand(i));
2894 } else {
Chris Lattner66445d32006-03-28 22:11:53 +00002895 unsigned NewIdx =
2896 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
2897 MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32));
Chris Lattner66445d32006-03-28 22:11:53 +00002898 }
2899 }
2900 ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMask.getValueType(),
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002901 &MappedOps[0], MappedOps.size());
Chris Lattner3e104b12006-04-08 04:15:24 +00002902 AddToWorkList(ShufMask.Val);
Chris Lattner66445d32006-03-28 22:11:53 +00002903 return DAG.getNode(ISD::VECTOR_SHUFFLE, N->getValueType(0),
Evan Chenge7bec0d2006-07-20 22:44:41 +00002904 N0,
Chris Lattner66445d32006-03-28 22:11:53 +00002905 DAG.getNode(ISD::UNDEF, N->getValueType(0)),
2906 ShufMask);
2907 }
2908
2909 return SDOperand();
2910}
2911
Chris Lattnerf1d0c622006-03-31 22:16:43 +00002912SDOperand DAGCombiner::visitVVECTOR_SHUFFLE(SDNode *N) {
2913 SDOperand ShufMask = N->getOperand(2);
2914 unsigned NumElts = ShufMask.getNumOperands()-2;
2915
2916 // If the shuffle mask is an identity operation on the LHS, return the LHS.
2917 bool isIdentity = true;
2918 for (unsigned i = 0; i != NumElts; ++i) {
2919 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
2920 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
2921 isIdentity = false;
2922 break;
2923 }
2924 }
2925 if (isIdentity) return N->getOperand(0);
2926
2927 // If the shuffle mask is an identity operation on the RHS, return the RHS.
2928 isIdentity = true;
2929 for (unsigned i = 0; i != NumElts; ++i) {
2930 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
2931 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
2932 isIdentity = false;
2933 break;
2934 }
2935 }
2936 if (isIdentity) return N->getOperand(1);
2937
Evan Chenge7bec0d2006-07-20 22:44:41 +00002938 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
2939 // needed at all.
2940 bool isUnary = true;
Evan Cheng917ec982006-07-21 08:25:53 +00002941 bool isSplat = true;
Evan Chenge7bec0d2006-07-20 22:44:41 +00002942 int VecNum = -1;
Reid Spencer9160a6a2006-07-25 20:44:41 +00002943 unsigned BaseIdx = 0;
Evan Chenge7bec0d2006-07-20 22:44:41 +00002944 for (unsigned i = 0; i != NumElts; ++i)
2945 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
2946 unsigned Idx = cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue();
2947 int V = (Idx < NumElts) ? 0 : 1;
Evan Cheng917ec982006-07-21 08:25:53 +00002948 if (VecNum == -1) {
Evan Chenge7bec0d2006-07-20 22:44:41 +00002949 VecNum = V;
Evan Cheng917ec982006-07-21 08:25:53 +00002950 BaseIdx = Idx;
2951 } else {
2952 if (BaseIdx != Idx)
2953 isSplat = false;
2954 if (VecNum != V) {
2955 isUnary = false;
2956 break;
2957 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00002958 }
2959 }
2960
2961 SDOperand N0 = N->getOperand(0);
2962 SDOperand N1 = N->getOperand(1);
2963 // Normalize unary shuffle so the RHS is undef.
2964 if (isUnary && VecNum == 1)
2965 std::swap(N0, N1);
2966
Evan Cheng917ec982006-07-21 08:25:53 +00002967 // If it is a splat, check if the argument vector is a build_vector with
2968 // all scalar elements the same.
2969 if (isSplat) {
2970 SDNode *V = N0.Val;
2971 if (V->getOpcode() == ISD::VBIT_CONVERT)
2972 V = V->getOperand(0).Val;
2973 if (V->getOpcode() == ISD::VBUILD_VECTOR) {
2974 unsigned NumElems = V->getNumOperands()-2;
2975 if (NumElems > BaseIdx) {
2976 SDOperand Base;
2977 bool AllSame = true;
2978 for (unsigned i = 0; i != NumElems; ++i) {
2979 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
2980 Base = V->getOperand(i);
2981 break;
2982 }
2983 }
2984 // Splat of <u, u, u, u>, return <u, u, u, u>
2985 if (!Base.Val)
2986 return N0;
2987 for (unsigned i = 0; i != NumElems; ++i) {
2988 if (V->getOperand(i).getOpcode() != ISD::UNDEF &&
2989 V->getOperand(i) != Base) {
2990 AllSame = false;
2991 break;
2992 }
2993 }
2994 // Splat of <x, x, x, x>, return <x, x, x, x>
2995 if (AllSame)
2996 return N0;
2997 }
2998 }
2999 }
3000
Evan Chenge7bec0d2006-07-20 22:44:41 +00003001 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
3002 // into an undef.
3003 if (isUnary || N0 == N1) {
Chris Lattner17614ea2006-04-08 05:34:25 +00003004 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
3005 // first operand.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003006 SmallVector<SDOperand, 8> MappedOps;
Chris Lattner17614ea2006-04-08 05:34:25 +00003007 for (unsigned i = 0; i != NumElts; ++i) {
3008 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
3009 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) {
3010 MappedOps.push_back(ShufMask.getOperand(i));
3011 } else {
3012 unsigned NewIdx =
3013 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
3014 MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32));
3015 }
3016 }
3017 // Add the type/#elts values.
3018 MappedOps.push_back(ShufMask.getOperand(NumElts));
3019 MappedOps.push_back(ShufMask.getOperand(NumElts+1));
3020
3021 ShufMask = DAG.getNode(ISD::VBUILD_VECTOR, ShufMask.getValueType(),
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003022 &MappedOps[0], MappedOps.size());
Chris Lattner17614ea2006-04-08 05:34:25 +00003023 AddToWorkList(ShufMask.Val);
3024
3025 // Build the undef vector.
3026 SDOperand UDVal = DAG.getNode(ISD::UNDEF, MappedOps[0].getValueType());
3027 for (unsigned i = 0; i != NumElts; ++i)
3028 MappedOps[i] = UDVal;
Evan Chenge7bec0d2006-07-20 22:44:41 +00003029 MappedOps[NumElts ] = *(N0.Val->op_end()-2);
3030 MappedOps[NumElts+1] = *(N0.Val->op_end()-1);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003031 UDVal = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
3032 &MappedOps[0], MappedOps.size());
Chris Lattner17614ea2006-04-08 05:34:25 +00003033
3034 return DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector,
Evan Chenge7bec0d2006-07-20 22:44:41 +00003035 N0, UDVal, ShufMask,
Chris Lattner17614ea2006-04-08 05:34:25 +00003036 MappedOps[NumElts], MappedOps[NumElts+1]);
3037 }
3038
Chris Lattnerf1d0c622006-03-31 22:16:43 +00003039 return SDOperand();
3040}
3041
Evan Cheng44f1f092006-04-20 08:56:16 +00003042/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
3043/// a VAND to a vector_shuffle with the destination vector and a zero vector.
3044/// e.g. VAND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
3045/// vector_shuffle V, Zero, <0, 4, 2, 4>
3046SDOperand DAGCombiner::XformToShuffleWithZero(SDNode *N) {
3047 SDOperand LHS = N->getOperand(0);
3048 SDOperand RHS = N->getOperand(1);
3049 if (N->getOpcode() == ISD::VAND) {
3050 SDOperand DstVecSize = *(LHS.Val->op_end()-2);
3051 SDOperand DstVecEVT = *(LHS.Val->op_end()-1);
3052 if (RHS.getOpcode() == ISD::VBIT_CONVERT)
3053 RHS = RHS.getOperand(0);
3054 if (RHS.getOpcode() == ISD::VBUILD_VECTOR) {
3055 std::vector<SDOperand> IdxOps;
3056 unsigned NumOps = RHS.getNumOperands();
3057 unsigned NumElts = NumOps-2;
3058 MVT::ValueType EVT = cast<VTSDNode>(RHS.getOperand(NumOps-1))->getVT();
3059 for (unsigned i = 0; i != NumElts; ++i) {
3060 SDOperand Elt = RHS.getOperand(i);
3061 if (!isa<ConstantSDNode>(Elt))
3062 return SDOperand();
3063 else if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
3064 IdxOps.push_back(DAG.getConstant(i, EVT));
3065 else if (cast<ConstantSDNode>(Elt)->isNullValue())
3066 IdxOps.push_back(DAG.getConstant(NumElts, EVT));
3067 else
3068 return SDOperand();
3069 }
3070
3071 // Let's see if the target supports this vector_shuffle.
3072 if (!TLI.isVectorClearMaskLegal(IdxOps, EVT, DAG))
3073 return SDOperand();
3074
3075 // Return the new VVECTOR_SHUFFLE node.
3076 SDOperand NumEltsNode = DAG.getConstant(NumElts, MVT::i32);
3077 SDOperand EVTNode = DAG.getValueType(EVT);
3078 std::vector<SDOperand> Ops;
Chris Lattner516b9622006-09-14 20:50:57 +00003079 LHS = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, LHS, NumEltsNode,
3080 EVTNode);
Evan Cheng44f1f092006-04-20 08:56:16 +00003081 Ops.push_back(LHS);
3082 AddToWorkList(LHS.Val);
3083 std::vector<SDOperand> ZeroOps(NumElts, DAG.getConstant(0, EVT));
3084 ZeroOps.push_back(NumEltsNode);
3085 ZeroOps.push_back(EVTNode);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003086 Ops.push_back(DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
3087 &ZeroOps[0], ZeroOps.size()));
Evan Cheng44f1f092006-04-20 08:56:16 +00003088 IdxOps.push_back(NumEltsNode);
3089 IdxOps.push_back(EVTNode);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003090 Ops.push_back(DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
3091 &IdxOps[0], IdxOps.size()));
Evan Cheng44f1f092006-04-20 08:56:16 +00003092 Ops.push_back(NumEltsNode);
3093 Ops.push_back(EVTNode);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003094 SDOperand Result = DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector,
3095 &Ops[0], Ops.size());
Evan Cheng44f1f092006-04-20 08:56:16 +00003096 if (NumEltsNode != DstVecSize || EVTNode != DstVecEVT) {
3097 Result = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Result,
3098 DstVecSize, DstVecEVT);
3099 }
3100 return Result;
3101 }
3102 }
3103 return SDOperand();
3104}
3105
Chris Lattneredab1b92006-04-02 03:25:57 +00003106/// visitVBinOp - Visit a binary vector operation, like VADD. IntOp indicates
3107/// the scalar operation of the vop if it is operating on an integer vector
3108/// (e.g. ADD) and FPOp indicates the FP version (e.g. FADD).
3109SDOperand DAGCombiner::visitVBinOp(SDNode *N, ISD::NodeType IntOp,
3110 ISD::NodeType FPOp) {
3111 MVT::ValueType EltType = cast<VTSDNode>(*(N->op_end()-1))->getVT();
3112 ISD::NodeType ScalarOp = MVT::isInteger(EltType) ? IntOp : FPOp;
3113 SDOperand LHS = N->getOperand(0);
3114 SDOperand RHS = N->getOperand(1);
Evan Cheng44f1f092006-04-20 08:56:16 +00003115 SDOperand Shuffle = XformToShuffleWithZero(N);
3116 if (Shuffle.Val) return Shuffle;
3117
Chris Lattneredab1b92006-04-02 03:25:57 +00003118 // If the LHS and RHS are VBUILD_VECTOR nodes, see if we can constant fold
3119 // this operation.
3120 if (LHS.getOpcode() == ISD::VBUILD_VECTOR &&
3121 RHS.getOpcode() == ISD::VBUILD_VECTOR) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003122 SmallVector<SDOperand, 8> Ops;
Chris Lattneredab1b92006-04-02 03:25:57 +00003123 for (unsigned i = 0, e = LHS.getNumOperands()-2; i != e; ++i) {
3124 SDOperand LHSOp = LHS.getOperand(i);
3125 SDOperand RHSOp = RHS.getOperand(i);
3126 // If these two elements can't be folded, bail out.
3127 if ((LHSOp.getOpcode() != ISD::UNDEF &&
3128 LHSOp.getOpcode() != ISD::Constant &&
3129 LHSOp.getOpcode() != ISD::ConstantFP) ||
3130 (RHSOp.getOpcode() != ISD::UNDEF &&
3131 RHSOp.getOpcode() != ISD::Constant &&
3132 RHSOp.getOpcode() != ISD::ConstantFP))
3133 break;
Evan Cheng7b336a82006-05-31 06:08:35 +00003134 // Can't fold divide by zero.
3135 if (N->getOpcode() == ISD::VSDIV || N->getOpcode() == ISD::VUDIV) {
3136 if ((RHSOp.getOpcode() == ISD::Constant &&
3137 cast<ConstantSDNode>(RHSOp.Val)->isNullValue()) ||
3138 (RHSOp.getOpcode() == ISD::ConstantFP &&
3139 !cast<ConstantFPSDNode>(RHSOp.Val)->getValue()))
3140 break;
3141 }
Chris Lattneredab1b92006-04-02 03:25:57 +00003142 Ops.push_back(DAG.getNode(ScalarOp, EltType, LHSOp, RHSOp));
Chris Lattner3e104b12006-04-08 04:15:24 +00003143 AddToWorkList(Ops.back().Val);
Chris Lattneredab1b92006-04-02 03:25:57 +00003144 assert((Ops.back().getOpcode() == ISD::UNDEF ||
3145 Ops.back().getOpcode() == ISD::Constant ||
3146 Ops.back().getOpcode() == ISD::ConstantFP) &&
3147 "Scalar binop didn't fold!");
3148 }
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00003149
3150 if (Ops.size() == LHS.getNumOperands()-2) {
3151 Ops.push_back(*(LHS.Val->op_end()-2));
3152 Ops.push_back(*(LHS.Val->op_end()-1));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003153 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00003154 }
Chris Lattneredab1b92006-04-02 03:25:57 +00003155 }
3156
3157 return SDOperand();
3158}
3159
Nate Begeman44728a72005-09-19 22:34:01 +00003160SDOperand DAGCombiner::SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2){
Nate Begemanf845b452005-10-08 00:29:44 +00003161 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
3162
3163 SDOperand SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
3164 cast<CondCodeSDNode>(N0.getOperand(2))->get());
3165 // If we got a simplified select_cc node back from SimplifySelectCC, then
3166 // break it down into a new SETCC node, and a new SELECT node, and then return
3167 // the SELECT node, since we were called with a SELECT node.
3168 if (SCC.Val) {
3169 // Check to see if we got a select_cc back (to turn into setcc/select).
3170 // Otherwise, just return whatever node we got back, like fabs.
3171 if (SCC.getOpcode() == ISD::SELECT_CC) {
3172 SDOperand SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
3173 SCC.getOperand(0), SCC.getOperand(1),
3174 SCC.getOperand(4));
Chris Lattner5750df92006-03-01 04:03:14 +00003175 AddToWorkList(SETCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003176 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
3177 SCC.getOperand(3), SETCC);
3178 }
3179 return SCC;
3180 }
Nate Begeman44728a72005-09-19 22:34:01 +00003181 return SDOperand();
3182}
3183
Chris Lattner40c62d52005-10-18 06:04:22 +00003184/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
3185/// are the two values being selected between, see if we can simplify the
Chris Lattner729c6d12006-05-27 00:43:02 +00003186/// select. Callers of this should assume that TheSelect is deleted if this
3187/// returns true. As such, they should return the appropriate thing (e.g. the
3188/// node) back to the top-level of the DAG combiner loop to avoid it being
3189/// looked at.
Chris Lattner40c62d52005-10-18 06:04:22 +00003190///
3191bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDOperand LHS,
3192 SDOperand RHS) {
3193
3194 // If this is a select from two identical things, try to pull the operation
3195 // through the select.
3196 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
3197#if 0
3198 std::cerr << "SELECT: ["; LHS.Val->dump();
3199 std::cerr << "] ["; RHS.Val->dump();
3200 std::cerr << "]\n";
3201#endif
3202
3203 // If this is a load and the token chain is identical, replace the select
3204 // of two loads with a load through a select of the address to load from.
3205 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
3206 // constants have been dropped into the constant pool.
3207 if ((LHS.getOpcode() == ISD::LOAD ||
3208 LHS.getOpcode() == ISD::EXTLOAD ||
3209 LHS.getOpcode() == ISD::ZEXTLOAD ||
3210 LHS.getOpcode() == ISD::SEXTLOAD) &&
3211 // Token chains must be identical.
3212 LHS.getOperand(0) == RHS.getOperand(0) &&
3213 // If this is an EXTLOAD, the VT's must match.
3214 (LHS.getOpcode() == ISD::LOAD ||
3215 LHS.getOperand(3) == RHS.getOperand(3))) {
3216 // FIXME: this conflates two src values, discarding one. This is not
3217 // the right thing to do, but nothing uses srcvalues now. When they do,
3218 // turn SrcValue into a list of locations.
3219 SDOperand Addr;
3220 if (TheSelect->getOpcode() == ISD::SELECT)
3221 Addr = DAG.getNode(ISD::SELECT, LHS.getOperand(1).getValueType(),
3222 TheSelect->getOperand(0), LHS.getOperand(1),
3223 RHS.getOperand(1));
3224 else
3225 Addr = DAG.getNode(ISD::SELECT_CC, LHS.getOperand(1).getValueType(),
3226 TheSelect->getOperand(0),
3227 TheSelect->getOperand(1),
3228 LHS.getOperand(1), RHS.getOperand(1),
3229 TheSelect->getOperand(4));
3230
3231 SDOperand Load;
3232 if (LHS.getOpcode() == ISD::LOAD)
3233 Load = DAG.getLoad(TheSelect->getValueType(0), LHS.getOperand(0),
3234 Addr, LHS.getOperand(2));
3235 else
3236 Load = DAG.getExtLoad(LHS.getOpcode(), TheSelect->getValueType(0),
3237 LHS.getOperand(0), Addr, LHS.getOperand(2),
3238 cast<VTSDNode>(LHS.getOperand(3))->getVT());
3239 // Users of the select now use the result of the load.
3240 CombineTo(TheSelect, Load);
3241
3242 // Users of the old loads now use the new load's chain. We know the
3243 // old-load value is dead now.
3244 CombineTo(LHS.Val, Load.getValue(0), Load.getValue(1));
3245 CombineTo(RHS.Val, Load.getValue(0), Load.getValue(1));
3246 return true;
3247 }
3248 }
3249
3250 return false;
3251}
3252
Nate Begeman44728a72005-09-19 22:34:01 +00003253SDOperand DAGCombiner::SimplifySelectCC(SDOperand N0, SDOperand N1,
3254 SDOperand N2, SDOperand N3,
3255 ISD::CondCode CC) {
Nate Begemanf845b452005-10-08 00:29:44 +00003256
3257 MVT::ValueType VT = N2.getValueType();
Nate Begemanf845b452005-10-08 00:29:44 +00003258 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
3259 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
3260 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
3261
3262 // Determine if the condition we're dealing with is constant
3263 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
3264 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
3265
3266 // fold select_cc true, x, y -> x
3267 if (SCCC && SCCC->getValue())
3268 return N2;
3269 // fold select_cc false, x, y -> y
3270 if (SCCC && SCCC->getValue() == 0)
3271 return N3;
3272
3273 // Check to see if we can simplify the select into an fabs node
3274 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
3275 // Allow either -0.0 or 0.0
3276 if (CFP->getValue() == 0.0) {
3277 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
3278 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
3279 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
3280 N2 == N3.getOperand(0))
3281 return DAG.getNode(ISD::FABS, VT, N0);
3282
3283 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
3284 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
3285 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
3286 N2.getOperand(0) == N3)
3287 return DAG.getNode(ISD::FABS, VT, N3);
3288 }
3289 }
3290
3291 // Check to see if we can perform the "gzip trick", transforming
3292 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
Chris Lattnere3152e52006-09-20 06:41:35 +00003293 if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT &&
Nate Begemanf845b452005-10-08 00:29:44 +00003294 MVT::isInteger(N0.getValueType()) &&
Chris Lattnere3152e52006-09-20 06:41:35 +00003295 MVT::isInteger(N2.getValueType()) &&
3296 (N1C->isNullValue() || // (a < 0) ? b : 0
3297 (N1C->getValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0
Nate Begemanf845b452005-10-08 00:29:44 +00003298 MVT::ValueType XType = N0.getValueType();
3299 MVT::ValueType AType = N2.getValueType();
3300 if (XType >= AType) {
3301 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00003302 // single-bit constant.
Nate Begemanf845b452005-10-08 00:29:44 +00003303 if (N2C && ((N2C->getValue() & (N2C->getValue()-1)) == 0)) {
3304 unsigned ShCtV = Log2_64(N2C->getValue());
3305 ShCtV = MVT::getSizeInBits(XType)-ShCtV-1;
3306 SDOperand ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
3307 SDOperand Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
Chris Lattner5750df92006-03-01 04:03:14 +00003308 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003309 if (XType > AType) {
3310 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00003311 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003312 }
3313 return DAG.getNode(ISD::AND, AType, Shift, N2);
3314 }
3315 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
3316 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3317 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00003318 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003319 if (XType > AType) {
3320 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00003321 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003322 }
3323 return DAG.getNode(ISD::AND, AType, Shift, N2);
3324 }
3325 }
Nate Begeman07ed4172005-10-10 21:26:48 +00003326
3327 // fold select C, 16, 0 -> shl C, 4
3328 if (N2C && N3C && N3C->isNullValue() && isPowerOf2_64(N2C->getValue()) &&
3329 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) {
3330 // Get a SetCC of the condition
3331 // FIXME: Should probably make sure that setcc is legal if we ever have a
3332 // target where it isn't.
Nate Begemanb0d04a72006-02-18 02:40:58 +00003333 SDOperand Temp, SCC;
Nate Begeman07ed4172005-10-10 21:26:48 +00003334 // cast from setcc result type to select result type
Nate Begemanb0d04a72006-02-18 02:40:58 +00003335 if (AfterLegalize) {
3336 SCC = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00003337 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
Nate Begemanb0d04a72006-02-18 02:40:58 +00003338 } else {
3339 SCC = DAG.getSetCC(MVT::i1, N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00003340 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00003341 }
Chris Lattner5750df92006-03-01 04:03:14 +00003342 AddToWorkList(SCC.Val);
3343 AddToWorkList(Temp.Val);
Nate Begeman07ed4172005-10-10 21:26:48 +00003344 // shl setcc result by log2 n2c
3345 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
3346 DAG.getConstant(Log2_64(N2C->getValue()),
3347 TLI.getShiftAmountTy()));
3348 }
3349
Nate Begemanf845b452005-10-08 00:29:44 +00003350 // Check to see if this is the equivalent of setcc
3351 // FIXME: Turn all of these into setcc if setcc if setcc is legal
3352 // otherwise, go ahead with the folds.
3353 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getValue() == 1ULL)) {
3354 MVT::ValueType XType = N0.getValueType();
3355 if (TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultTy())) {
3356 SDOperand Res = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
3357 if (Res.getValueType() != VT)
3358 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
3359 return Res;
3360 }
3361
3362 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
3363 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
3364 TLI.isOperationLegal(ISD::CTLZ, XType)) {
3365 SDOperand Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
3366 return DAG.getNode(ISD::SRL, XType, Ctlz,
3367 DAG.getConstant(Log2_32(MVT::getSizeInBits(XType)),
3368 TLI.getShiftAmountTy()));
3369 }
3370 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
3371 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
3372 SDOperand NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
3373 N0);
3374 SDOperand NotN0 = DAG.getNode(ISD::XOR, XType, N0,
3375 DAG.getConstant(~0ULL, XType));
3376 return DAG.getNode(ISD::SRL, XType,
3377 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
3378 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3379 TLI.getShiftAmountTy()));
3380 }
3381 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
3382 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
3383 SDOperand Sign = DAG.getNode(ISD::SRL, XType, N0,
3384 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3385 TLI.getShiftAmountTy()));
3386 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
3387 }
3388 }
3389
3390 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
3391 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
3392 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
3393 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1)) {
3394 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N2.getOperand(0))) {
3395 MVT::ValueType XType = N0.getValueType();
3396 if (SubC->isNullValue() && MVT::isInteger(XType)) {
3397 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
3398 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3399 TLI.getShiftAmountTy()));
3400 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00003401 AddToWorkList(Shift.Val);
3402 AddToWorkList(Add.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003403 return DAG.getNode(ISD::XOR, XType, Add, Shift);
3404 }
3405 }
3406 }
3407
Nate Begeman44728a72005-09-19 22:34:01 +00003408 return SDOperand();
3409}
3410
Nate Begeman452d7be2005-09-16 00:54:12 +00003411SDOperand DAGCombiner::SimplifySetCC(MVT::ValueType VT, SDOperand N0,
Nate Begemane17daeb2005-10-05 21:43:42 +00003412 SDOperand N1, ISD::CondCode Cond,
3413 bool foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00003414 // These setcc operations always fold.
3415 switch (Cond) {
3416 default: break;
3417 case ISD::SETFALSE:
3418 case ISD::SETFALSE2: return DAG.getConstant(0, VT);
3419 case ISD::SETTRUE:
3420 case ISD::SETTRUE2: return DAG.getConstant(1, VT);
3421 }
3422
3423 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
3424 uint64_t C1 = N1C->getValue();
3425 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val)) {
3426 uint64_t C0 = N0C->getValue();
3427
3428 // Sign extend the operands if required
3429 if (ISD::isSignedIntSetCC(Cond)) {
3430 C0 = N0C->getSignExtended();
3431 C1 = N1C->getSignExtended();
3432 }
3433
3434 switch (Cond) {
3435 default: assert(0 && "Unknown integer setcc!");
3436 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
3437 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
3438 case ISD::SETULT: return DAG.getConstant(C0 < C1, VT);
3439 case ISD::SETUGT: return DAG.getConstant(C0 > C1, VT);
3440 case ISD::SETULE: return DAG.getConstant(C0 <= C1, VT);
3441 case ISD::SETUGE: return DAG.getConstant(C0 >= C1, VT);
3442 case ISD::SETLT: return DAG.getConstant((int64_t)C0 < (int64_t)C1, VT);
3443 case ISD::SETGT: return DAG.getConstant((int64_t)C0 > (int64_t)C1, VT);
3444 case ISD::SETLE: return DAG.getConstant((int64_t)C0 <= (int64_t)C1, VT);
3445 case ISD::SETGE: return DAG.getConstant((int64_t)C0 >= (int64_t)C1, VT);
3446 }
3447 } else {
Chris Lattner5f42a242006-09-20 06:19:26 +00003448 // If the LHS is '(srl (ctlz x), 5)', the RHS is 0/1, and this is an
3449 // equality comparison, then we're just comparing whether X itself is
3450 // zero.
3451 if (N0.getOpcode() == ISD::SRL && (C1 == 0 || C1 == 1) &&
3452 N0.getOperand(0).getOpcode() == ISD::CTLZ &&
3453 N0.getOperand(1).getOpcode() == ISD::Constant) {
3454 unsigned ShAmt = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
3455 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
3456 ShAmt == Log2_32(MVT::getSizeInBits(N0.getValueType()))) {
3457 if ((C1 == 0) == (Cond == ISD::SETEQ)) {
3458 // (srl (ctlz x), 5) == 0 -> X != 0
3459 // (srl (ctlz x), 5) != 1 -> X != 0
3460 Cond = ISD::SETNE;
3461 } else {
3462 // (srl (ctlz x), 5) != 0 -> X == 0
3463 // (srl (ctlz x), 5) == 1 -> X == 0
3464 Cond = ISD::SETEQ;
3465 }
3466 SDOperand Zero = DAG.getConstant(0, N0.getValueType());
3467 return DAG.getSetCC(VT, N0.getOperand(0).getOperand(0),
3468 Zero, Cond);
3469 }
3470 }
3471
Nate Begeman452d7be2005-09-16 00:54:12 +00003472 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
3473 if (N0.getOpcode() == ISD::ZERO_EXTEND) {
3474 unsigned InSize = MVT::getSizeInBits(N0.getOperand(0).getValueType());
3475
3476 // If the comparison constant has bits in the upper part, the
3477 // zero-extended value could never match.
3478 if (C1 & (~0ULL << InSize)) {
3479 unsigned VSize = MVT::getSizeInBits(N0.getValueType());
3480 switch (Cond) {
3481 case ISD::SETUGT:
3482 case ISD::SETUGE:
3483 case ISD::SETEQ: return DAG.getConstant(0, VT);
3484 case ISD::SETULT:
3485 case ISD::SETULE:
3486 case ISD::SETNE: return DAG.getConstant(1, VT);
3487 case ISD::SETGT:
3488 case ISD::SETGE:
3489 // True if the sign bit of C1 is set.
3490 return DAG.getConstant((C1 & (1ULL << VSize)) != 0, VT);
3491 case ISD::SETLT:
3492 case ISD::SETLE:
3493 // True if the sign bit of C1 isn't set.
3494 return DAG.getConstant((C1 & (1ULL << VSize)) == 0, VT);
3495 default:
3496 break;
3497 }
3498 }
3499
3500 // Otherwise, we can perform the comparison with the low bits.
3501 switch (Cond) {
3502 case ISD::SETEQ:
3503 case ISD::SETNE:
3504 case ISD::SETUGT:
3505 case ISD::SETUGE:
3506 case ISD::SETULT:
3507 case ISD::SETULE:
3508 return DAG.getSetCC(VT, N0.getOperand(0),
3509 DAG.getConstant(C1, N0.getOperand(0).getValueType()),
3510 Cond);
3511 default:
3512 break; // todo, be more careful with signed comparisons
3513 }
3514 } else if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
3515 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
3516 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N0.getOperand(1))->getVT();
3517 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
3518 MVT::ValueType ExtDstTy = N0.getValueType();
3519 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
3520
3521 // If the extended part has any inconsistent bits, it cannot ever
3522 // compare equal. In other words, they have to be all ones or all
3523 // zeros.
3524 uint64_t ExtBits =
3525 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
3526 if ((C1 & ExtBits) != 0 && (C1 & ExtBits) != ExtBits)
3527 return DAG.getConstant(Cond == ISD::SETNE, VT);
3528
3529 SDOperand ZextOp;
3530 MVT::ValueType Op0Ty = N0.getOperand(0).getValueType();
3531 if (Op0Ty == ExtSrcTy) {
3532 ZextOp = N0.getOperand(0);
3533 } else {
3534 int64_t Imm = ~0ULL >> (64-ExtSrcTyBits);
3535 ZextOp = DAG.getNode(ISD::AND, Op0Ty, N0.getOperand(0),
3536 DAG.getConstant(Imm, Op0Ty));
3537 }
Chris Lattner5750df92006-03-01 04:03:14 +00003538 AddToWorkList(ZextOp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003539 // Otherwise, make this a use of a zext.
3540 return DAG.getSetCC(VT, ZextOp,
3541 DAG.getConstant(C1 & (~0ULL>>(64-ExtSrcTyBits)),
3542 ExtDstTy),
3543 Cond);
Chris Lattner3391bcd2006-02-08 02:13:15 +00003544 } else if ((N1C->getValue() == 0 || N1C->getValue() == 1) &&
3545 (Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
3546 (N0.getOpcode() == ISD::XOR ||
3547 (N0.getOpcode() == ISD::AND &&
3548 N0.getOperand(0).getOpcode() == ISD::XOR &&
3549 N0.getOperand(1) == N0.getOperand(0).getOperand(1))) &&
3550 isa<ConstantSDNode>(N0.getOperand(1)) &&
3551 cast<ConstantSDNode>(N0.getOperand(1))->getValue() == 1) {
3552 // If this is (X^1) == 0/1, swap the RHS and eliminate the xor. We can
3553 // only do this if the top bits are known zero.
3554 if (TLI.MaskedValueIsZero(N1,
3555 MVT::getIntVTBitMask(N0.getValueType())-1)) {
3556 // Okay, get the un-inverted input value.
3557 SDOperand Val;
3558 if (N0.getOpcode() == ISD::XOR)
3559 Val = N0.getOperand(0);
3560 else {
3561 assert(N0.getOpcode() == ISD::AND &&
3562 N0.getOperand(0).getOpcode() == ISD::XOR);
3563 // ((X^1)&1)^1 -> X & 1
3564 Val = DAG.getNode(ISD::AND, N0.getValueType(),
3565 N0.getOperand(0).getOperand(0), N0.getOperand(1));
3566 }
3567 return DAG.getSetCC(VT, Val, N1,
3568 Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ);
3569 }
Nate Begeman452d7be2005-09-16 00:54:12 +00003570 }
Chris Lattner5c46f742005-10-05 06:11:08 +00003571
Nate Begeman452d7be2005-09-16 00:54:12 +00003572 uint64_t MinVal, MaxVal;
3573 unsigned OperandBitSize = MVT::getSizeInBits(N1C->getValueType(0));
3574 if (ISD::isSignedIntSetCC(Cond)) {
3575 MinVal = 1ULL << (OperandBitSize-1);
3576 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
3577 MaxVal = ~0ULL >> (65-OperandBitSize);
3578 else
3579 MaxVal = 0;
3580 } else {
3581 MinVal = 0;
3582 MaxVal = ~0ULL >> (64-OperandBitSize);
3583 }
3584
3585 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
3586 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
3587 if (C1 == MinVal) return DAG.getConstant(1, VT); // X >= MIN --> true
3588 --C1; // X >= C0 --> X > (C0-1)
3589 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
3590 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
3591 }
3592
3593 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
3594 if (C1 == MaxVal) return DAG.getConstant(1, VT); // X <= MAX --> true
3595 ++C1; // X <= C0 --> X < (C0+1)
3596 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
3597 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
3598 }
3599
3600 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal)
3601 return DAG.getConstant(0, VT); // X < MIN --> false
3602
3603 // Canonicalize setgt X, Min --> setne X, Min
3604 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MinVal)
3605 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
Chris Lattnerc8597ca2005-10-21 21:23:25 +00003606 // Canonicalize setlt X, Max --> setne X, Max
3607 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MaxVal)
3608 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
Nate Begeman452d7be2005-09-16 00:54:12 +00003609
3610 // If we have setult X, 1, turn it into seteq X, 0
3611 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal+1)
3612 return DAG.getSetCC(VT, N0, DAG.getConstant(MinVal, N0.getValueType()),
3613 ISD::SETEQ);
3614 // If we have setugt X, Max-1, turn it into seteq X, Max
3615 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal-1)
3616 return DAG.getSetCC(VT, N0, DAG.getConstant(MaxVal, N0.getValueType()),
3617 ISD::SETEQ);
3618
3619 // If we have "setcc X, C0", check to see if we can shrink the immediate
3620 // by changing cc.
3621
3622 // SETUGT X, SINTMAX -> SETLT X, 0
3623 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
3624 C1 == (~0ULL >> (65-OperandBitSize)))
3625 return DAG.getSetCC(VT, N0, DAG.getConstant(0, N1.getValueType()),
3626 ISD::SETLT);
3627
3628 // FIXME: Implement the rest of these.
3629
3630 // Fold bit comparisons when we can.
3631 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
3632 VT == N0.getValueType() && N0.getOpcode() == ISD::AND)
3633 if (ConstantSDNode *AndRHS =
3634 dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
3635 if (Cond == ISD::SETNE && C1 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
3636 // Perform the xform if the AND RHS is a single bit.
3637 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
3638 return DAG.getNode(ISD::SRL, VT, N0,
3639 DAG.getConstant(Log2_64(AndRHS->getValue()),
3640 TLI.getShiftAmountTy()));
3641 }
3642 } else if (Cond == ISD::SETEQ && C1 == AndRHS->getValue()) {
3643 // (X & 8) == 8 --> (X & 8) >> 3
3644 // Perform the xform if C1 is a single bit.
3645 if ((C1 & (C1-1)) == 0) {
3646 return DAG.getNode(ISD::SRL, VT, N0,
Chris Lattner729c6d12006-05-27 00:43:02 +00003647 DAG.getConstant(Log2_64(C1),TLI.getShiftAmountTy()));
Nate Begeman452d7be2005-09-16 00:54:12 +00003648 }
3649 }
3650 }
3651 }
3652 } else if (isa<ConstantSDNode>(N0.Val)) {
3653 // Ensure that the constant occurs on the RHS.
3654 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
3655 }
3656
3657 if (ConstantFPSDNode *N0C = dyn_cast<ConstantFPSDNode>(N0.Val))
3658 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) {
3659 double C0 = N0C->getValue(), C1 = N1C->getValue();
3660
3661 switch (Cond) {
3662 default: break; // FIXME: Implement the rest of these!
3663 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
3664 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
3665 case ISD::SETLT: return DAG.getConstant(C0 < C1, VT);
3666 case ISD::SETGT: return DAG.getConstant(C0 > C1, VT);
3667 case ISD::SETLE: return DAG.getConstant(C0 <= C1, VT);
3668 case ISD::SETGE: return DAG.getConstant(C0 >= C1, VT);
3669 }
3670 } else {
3671 // Ensure that the constant occurs on the RHS.
3672 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
3673 }
3674
3675 if (N0 == N1) {
3676 // We can always fold X == Y for integer setcc's.
3677 if (MVT::isInteger(N0.getValueType()))
3678 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
3679 unsigned UOF = ISD::getUnorderedFlavor(Cond);
3680 if (UOF == 2) // FP operators that are undefined on NaNs.
3681 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
3682 if (UOF == unsigned(ISD::isTrueWhenEqual(Cond)))
3683 return DAG.getConstant(UOF, VT);
3684 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
3685 // if it is not already.
Chris Lattner4090aee2006-01-18 19:13:41 +00003686 ISD::CondCode NewCond = UOF == 0 ? ISD::SETO : ISD::SETUO;
Nate Begeman452d7be2005-09-16 00:54:12 +00003687 if (NewCond != Cond)
3688 return DAG.getSetCC(VT, N0, N1, NewCond);
3689 }
3690
3691 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
3692 MVT::isInteger(N0.getValueType())) {
3693 if (N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::SUB ||
3694 N0.getOpcode() == ISD::XOR) {
3695 // Simplify (X+Y) == (X+Z) --> Y == Z
3696 if (N0.getOpcode() == N1.getOpcode()) {
3697 if (N0.getOperand(0) == N1.getOperand(0))
3698 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(1), Cond);
3699 if (N0.getOperand(1) == N1.getOperand(1))
3700 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(0), Cond);
Evan Cheng1efba0e2006-08-29 06:42:35 +00003701 if (DAG.isCommutativeBinOp(N0.getOpcode())) {
Nate Begeman452d7be2005-09-16 00:54:12 +00003702 // If X op Y == Y op X, try other combinations.
3703 if (N0.getOperand(0) == N1.getOperand(1))
3704 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(0), Cond);
3705 if (N0.getOperand(1) == N1.getOperand(0))
Chris Lattnera158eee2005-10-25 18:57:30 +00003706 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(1), Cond);
Nate Begeman452d7be2005-09-16 00:54:12 +00003707 }
3708 }
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003709
3710 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(N1)) {
3711 if (ConstantSDNode *LHSR = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
3712 // Turn (X+C1) == C2 --> X == C2-C1
3713 if (N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse()) {
3714 return DAG.getSetCC(VT, N0.getOperand(0),
3715 DAG.getConstant(RHSC->getValue()-LHSR->getValue(),
3716 N0.getValueType()), Cond);
3717 }
3718
3719 // Turn (X^C1) == C2 into X == C1^C2 iff X&~C1 = 0.
3720 if (N0.getOpcode() == ISD::XOR)
Chris Lattner5c46f742005-10-05 06:11:08 +00003721 // If we know that all of the inverted bits are zero, don't bother
3722 // performing the inversion.
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003723 if (TLI.MaskedValueIsZero(N0.getOperand(0), ~LHSR->getValue()))
Chris Lattner5c46f742005-10-05 06:11:08 +00003724 return DAG.getSetCC(VT, N0.getOperand(0),
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003725 DAG.getConstant(LHSR->getValue()^RHSC->getValue(),
Chris Lattner5c46f742005-10-05 06:11:08 +00003726 N0.getValueType()), Cond);
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003727 }
3728
3729 // Turn (C1-X) == C2 --> X == C1-C2
3730 if (ConstantSDNode *SUBC = dyn_cast<ConstantSDNode>(N0.getOperand(0))) {
3731 if (N0.getOpcode() == ISD::SUB && N0.Val->hasOneUse()) {
3732 return DAG.getSetCC(VT, N0.getOperand(1),
3733 DAG.getConstant(SUBC->getValue()-RHSC->getValue(),
3734 N0.getValueType()), Cond);
Chris Lattner5c46f742005-10-05 06:11:08 +00003735 }
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003736 }
3737 }
3738
Nate Begeman452d7be2005-09-16 00:54:12 +00003739 // Simplify (X+Z) == X --> Z == 0
3740 if (N0.getOperand(0) == N1)
3741 return DAG.getSetCC(VT, N0.getOperand(1),
3742 DAG.getConstant(0, N0.getValueType()), Cond);
3743 if (N0.getOperand(1) == N1) {
Evan Cheng1efba0e2006-08-29 06:42:35 +00003744 if (DAG.isCommutativeBinOp(N0.getOpcode()))
Nate Begeman452d7be2005-09-16 00:54:12 +00003745 return DAG.getSetCC(VT, N0.getOperand(0),
3746 DAG.getConstant(0, N0.getValueType()), Cond);
3747 else {
3748 assert(N0.getOpcode() == ISD::SUB && "Unexpected operation!");
3749 // (Z-X) == X --> Z == X<<1
3750 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(),
3751 N1,
3752 DAG.getConstant(1,TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00003753 AddToWorkList(SH.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003754 return DAG.getSetCC(VT, N0.getOperand(0), SH, Cond);
3755 }
3756 }
3757 }
3758
3759 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
3760 N1.getOpcode() == ISD::XOR) {
3761 // Simplify X == (X+Z) --> Z == 0
3762 if (N1.getOperand(0) == N0) {
3763 return DAG.getSetCC(VT, N1.getOperand(1),
3764 DAG.getConstant(0, N1.getValueType()), Cond);
3765 } else if (N1.getOperand(1) == N0) {
Evan Cheng1efba0e2006-08-29 06:42:35 +00003766 if (DAG.isCommutativeBinOp(N1.getOpcode())) {
Nate Begeman452d7be2005-09-16 00:54:12 +00003767 return DAG.getSetCC(VT, N1.getOperand(0),
3768 DAG.getConstant(0, N1.getValueType()), Cond);
3769 } else {
3770 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
3771 // X == (Z-X) --> X<<1 == Z
3772 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(), N0,
3773 DAG.getConstant(1,TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00003774 AddToWorkList(SH.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003775 return DAG.getSetCC(VT, SH, N1.getOperand(0), Cond);
3776 }
3777 }
3778 }
3779 }
3780
3781 // Fold away ALL boolean setcc's.
3782 SDOperand Temp;
Nate Begemane17daeb2005-10-05 21:43:42 +00003783 if (N0.getValueType() == MVT::i1 && foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00003784 switch (Cond) {
3785 default: assert(0 && "Unknown integer setcc!");
3786 case ISD::SETEQ: // X == Y -> (X^Y)^1
3787 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
3788 N0 = DAG.getNode(ISD::XOR, MVT::i1, Temp, DAG.getConstant(1, MVT::i1));
Chris Lattner5750df92006-03-01 04:03:14 +00003789 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003790 break;
3791 case ISD::SETNE: // X != Y --> (X^Y)
3792 N0 = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
3793 break;
3794 case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> X^1 & Y
3795 case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> X^1 & Y
3796 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
3797 N0 = DAG.getNode(ISD::AND, MVT::i1, N1, Temp);
Chris Lattner5750df92006-03-01 04:03:14 +00003798 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003799 break;
3800 case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> Y^1 & X
3801 case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> Y^1 & X
3802 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
3803 N0 = DAG.getNode(ISD::AND, MVT::i1, N0, Temp);
Chris Lattner5750df92006-03-01 04:03:14 +00003804 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003805 break;
3806 case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> X^1 | Y
3807 case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> X^1 | Y
3808 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
3809 N0 = DAG.getNode(ISD::OR, MVT::i1, N1, Temp);
Chris Lattner5750df92006-03-01 04:03:14 +00003810 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003811 break;
3812 case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> Y^1 | X
3813 case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> Y^1 | X
3814 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
3815 N0 = DAG.getNode(ISD::OR, MVT::i1, N0, Temp);
3816 break;
3817 }
3818 if (VT != MVT::i1) {
Chris Lattner5750df92006-03-01 04:03:14 +00003819 AddToWorkList(N0.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003820 // FIXME: If running after legalize, we probably can't do this.
3821 N0 = DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
3822 }
3823 return N0;
3824 }
3825
3826 // Could not fold it.
3827 return SDOperand();
3828}
3829
Nate Begeman69575232005-10-20 02:15:44 +00003830/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
3831/// return a DAG expression to select that will generate the same value by
3832/// multiplying by a magic number. See:
3833/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
3834SDOperand DAGCombiner::BuildSDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00003835 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00003836 SDOperand S = TLI.BuildSDIV(N, DAG, &Built);
3837
Andrew Lenharth232c9102006-06-12 16:07:18 +00003838 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00003839 ii != ee; ++ii)
3840 AddToWorkList(*ii);
3841 return S;
Nate Begeman69575232005-10-20 02:15:44 +00003842}
3843
3844/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
3845/// return a DAG expression to select that will generate the same value by
3846/// multiplying by a magic number. See:
3847/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
3848SDOperand DAGCombiner::BuildUDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00003849 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00003850 SDOperand S = TLI.BuildUDIV(N, DAG, &Built);
Nate Begeman69575232005-10-20 02:15:44 +00003851
Andrew Lenharth232c9102006-06-12 16:07:18 +00003852 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00003853 ii != ee; ++ii)
3854 AddToWorkList(*ii);
3855 return S;
Nate Begeman69575232005-10-20 02:15:44 +00003856}
3857
Nate Begeman1d4d4142005-09-01 00:19:25 +00003858// SelectionDAG::Combine - This is the entry point for the file.
3859//
Nate Begeman4ebd8052005-09-01 23:24:04 +00003860void SelectionDAG::Combine(bool RunningAfterLegalize) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003861 /// run - This is the main entry point to this class.
3862 ///
Nate Begeman4ebd8052005-09-01 23:24:04 +00003863 DAGCombiner(*this).Run(RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003864}