blob: 2d914f00815ca33f3cccb66a2eb388e7a5bde46f [file] [log] [blame]
Nate Begeman4ebd8052005-09-01 23:24:04 +00001//===-- DAGCombiner.cpp - Implement a DAG node combiner -------------------===//
Nate Begeman1d4d4142005-09-01 00:19:25 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Nate Begeman and is distributed under the
6// University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This pass combines dag nodes to form fewer, simpler DAG nodes. It can be run
11// both before and after the DAG is legalized.
12//
13// FIXME: Missing folds
14// sdiv, udiv, srem, urem (X, const) where X is an integer can be expanded into
15// a sequence of multiplies, shifts, and adds. This should be controlled by
16// some kind of hint from the target that int div is expensive.
17// various folds of mulh[s,u] by constants such as -1, powers of 2, etc.
18//
Nate Begeman44728a72005-09-19 22:34:01 +000019// FIXME: select C, pow2, pow2 -> something smart
20// FIXME: trunc(select X, Y, Z) -> select X, trunc(Y), trunc(Z)
Nate Begeman44728a72005-09-19 22:34:01 +000021// FIXME: Dead stores -> nuke
Chris Lattner40c62d52005-10-18 06:04:22 +000022// FIXME: shr X, (and Y,31) -> shr X, Y (TRICKY!)
Nate Begeman1d4d4142005-09-01 00:19:25 +000023// FIXME: mul (x, const) -> shifts + adds
Nate Begeman1d4d4142005-09-01 00:19:25 +000024// FIXME: undef values
Nate Begeman4ebd8052005-09-01 23:24:04 +000025// FIXME: make truncate see through SIGN_EXTEND and AND
Nate Begeman646d7e22005-09-02 21:18:40 +000026// FIXME: divide by zero is currently left unfolded. do we want to turn this
27// into an undef?
Nate Begemanf845b452005-10-08 00:29:44 +000028// FIXME: select ne (select cc, 1, 0), 0, true, false -> select cc, true, false
Nate Begeman1d4d4142005-09-01 00:19:25 +000029//
30//===----------------------------------------------------------------------===//
31
32#define DEBUG_TYPE "dagcombine"
33#include "llvm/ADT/Statistic.h"
34#include "llvm/CodeGen/SelectionDAG.h"
Nate Begeman2300f552005-09-07 00:15:36 +000035#include "llvm/Support/Debug.h"
Nate Begeman1d4d4142005-09-01 00:19:25 +000036#include "llvm/Support/MathExtras.h"
37#include "llvm/Target/TargetLowering.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000038#include "llvm/Support/Compiler.h"
Chris Lattnera500fc62005-09-09 23:53:39 +000039#include <algorithm>
Nate Begeman1d4d4142005-09-01 00:19:25 +000040#include <cmath>
Chris Lattner2c2c6c62006-01-22 23:41:00 +000041#include <iostream>
Nate Begeman1d4d4142005-09-01 00:19:25 +000042using namespace llvm;
43
44namespace {
Andrew Lenharthae6153f2006-07-20 17:43:27 +000045 static Statistic<> NodesCombined ("dagcombiner",
46 "Number of dag nodes combined");
Nate Begeman1d4d4142005-09-01 00:19:25 +000047
Chris Lattner360e8202006-06-28 21:58:30 +000048 class VISIBILITY_HIDDEN DAGCombiner {
Nate Begeman1d4d4142005-09-01 00:19:25 +000049 SelectionDAG &DAG;
50 TargetLowering &TLI;
Nate Begeman4ebd8052005-09-01 23:24:04 +000051 bool AfterLegalize;
Nate Begeman1d4d4142005-09-01 00:19:25 +000052
53 // Worklist of all of the nodes that need to be simplified.
54 std::vector<SDNode*> WorkList;
55
56 /// AddUsersToWorkList - When an instruction is simplified, add all users of
57 /// the instruction to the work lists because they might get more simplified
58 /// now.
59 ///
60 void AddUsersToWorkList(SDNode *N) {
61 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
Nate Begeman4ebd8052005-09-01 23:24:04 +000062 UI != UE; ++UI)
63 WorkList.push_back(*UI);
Nate Begeman1d4d4142005-09-01 00:19:25 +000064 }
65
66 /// removeFromWorkList - remove all instances of N from the worklist.
Chris Lattner5750df92006-03-01 04:03:14 +000067 ///
Nate Begeman1d4d4142005-09-01 00:19:25 +000068 void removeFromWorkList(SDNode *N) {
69 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), N),
70 WorkList.end());
71 }
72
Chris Lattner24664722006-03-01 04:53:38 +000073 public:
Chris Lattner5750df92006-03-01 04:03:14 +000074 void AddToWorkList(SDNode *N) {
75 WorkList.push_back(N);
76 }
77
Chris Lattner3577e382006-08-11 17:56:38 +000078 SDOperand CombineTo(SDNode *N, const SDOperand *To, unsigned NumTo) {
79 assert(N->getNumValues() == NumTo && "Broken CombineTo call!");
Chris Lattner87514ca2005-10-10 22:31:19 +000080 ++NodesCombined;
Chris Lattner01a22022005-10-10 22:04:48 +000081 DEBUG(std::cerr << "\nReplacing "; N->dump();
Evan Cheng60e8c712006-05-09 06:55:15 +000082 std::cerr << "\nWith: "; To[0].Val->dump(&DAG);
Chris Lattner3577e382006-08-11 17:56:38 +000083 std::cerr << " and " << NumTo-1 << " other values\n");
Chris Lattner01a22022005-10-10 22:04:48 +000084 std::vector<SDNode*> NowDead;
Chris Lattner3577e382006-08-11 17:56:38 +000085 DAG.ReplaceAllUsesWith(N, To, &NowDead);
Chris Lattner01a22022005-10-10 22:04:48 +000086
87 // Push the new nodes and any users onto the worklist
Chris Lattner3577e382006-08-11 17:56:38 +000088 for (unsigned i = 0, e = NumTo; i != e; ++i) {
Chris Lattner01a22022005-10-10 22:04:48 +000089 WorkList.push_back(To[i].Val);
90 AddUsersToWorkList(To[i].Val);
91 }
92
93 // Nodes can end up on the worklist more than once. Make sure we do
94 // not process a node that has been replaced.
95 removeFromWorkList(N);
96 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
97 removeFromWorkList(NowDead[i]);
98
99 // Finally, since the node is now dead, remove it from the graph.
100 DAG.DeleteNode(N);
101 return SDOperand(N, 0);
102 }
Nate Begeman368e18d2006-02-16 21:11:51 +0000103
Chris Lattner24664722006-03-01 04:53:38 +0000104 SDOperand CombineTo(SDNode *N, SDOperand Res) {
Chris Lattner3577e382006-08-11 17:56:38 +0000105 return CombineTo(N, &Res, 1);
Chris Lattner24664722006-03-01 04:53:38 +0000106 }
107
108 SDOperand CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1) {
Chris Lattner3577e382006-08-11 17:56:38 +0000109 SDOperand To[] = { Res0, Res1 };
110 return CombineTo(N, To, 2);
Chris Lattner24664722006-03-01 04:53:38 +0000111 }
112 private:
113
Chris Lattner012f2412006-02-17 21:58:01 +0000114 /// SimplifyDemandedBits - Check the specified integer node value to see if
Chris Lattnerb2742f42006-03-01 19:55:35 +0000115 /// it can be simplified or if things it uses can be simplified by bit
Chris Lattner012f2412006-02-17 21:58:01 +0000116 /// propagation. If so, return true.
117 bool SimplifyDemandedBits(SDOperand Op) {
Nate Begeman368e18d2006-02-16 21:11:51 +0000118 TargetLowering::TargetLoweringOpt TLO(DAG);
119 uint64_t KnownZero, KnownOne;
Chris Lattner012f2412006-02-17 21:58:01 +0000120 uint64_t Demanded = MVT::getIntVTBitMask(Op.getValueType());
121 if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO))
122 return false;
123
124 // Revisit the node.
125 WorkList.push_back(Op.Val);
126
127 // Replace the old value with the new one.
128 ++NodesCombined;
129 DEBUG(std::cerr << "\nReplacing "; TLO.Old.Val->dump();
Evan Cheng60e8c712006-05-09 06:55:15 +0000130 std::cerr << "\nWith: "; TLO.New.Val->dump(&DAG));
Chris Lattner012f2412006-02-17 21:58:01 +0000131
132 std::vector<SDNode*> NowDead;
133 DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New, NowDead);
134
Chris Lattner7d20d392006-02-20 06:51:04 +0000135 // Push the new node and any (possibly new) users onto the worklist.
Chris Lattner012f2412006-02-17 21:58:01 +0000136 WorkList.push_back(TLO.New.Val);
137 AddUsersToWorkList(TLO.New.Val);
138
139 // Nodes can end up on the worklist more than once. Make sure we do
140 // not process a node that has been replaced.
Chris Lattner012f2412006-02-17 21:58:01 +0000141 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
142 removeFromWorkList(NowDead[i]);
143
Chris Lattner7d20d392006-02-20 06:51:04 +0000144 // Finally, if the node is now dead, remove it from the graph. The node
145 // may not be dead if the replacement process recursively simplified to
146 // something else needing this node.
147 if (TLO.Old.Val->use_empty()) {
148 removeFromWorkList(TLO.Old.Val);
149 DAG.DeleteNode(TLO.Old.Val);
150 }
Chris Lattner012f2412006-02-17 21:58:01 +0000151 return true;
Nate Begeman368e18d2006-02-16 21:11:51 +0000152 }
Chris Lattner87514ca2005-10-10 22:31:19 +0000153
Nate Begeman1d4d4142005-09-01 00:19:25 +0000154 /// visit - call the node-specific routine that knows how to fold each
155 /// particular type of node.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000156 SDOperand visit(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000157
158 // Visitation implementation - Implement dag node combining for different
159 // node types. The semantics are as follows:
160 // Return Value:
Nate Begeman2300f552005-09-07 00:15:36 +0000161 // SDOperand.Val == 0 - No change was made
Chris Lattner01a22022005-10-10 22:04:48 +0000162 // SDOperand.Val == N - N was replaced, is dead, and is already handled.
Nate Begeman2300f552005-09-07 00:15:36 +0000163 // otherwise - N should be replaced by the returned Operand.
Nate Begeman1d4d4142005-09-01 00:19:25 +0000164 //
Nate Begeman83e75ec2005-09-06 04:43:02 +0000165 SDOperand visitTokenFactor(SDNode *N);
166 SDOperand visitADD(SDNode *N);
167 SDOperand visitSUB(SDNode *N);
168 SDOperand visitMUL(SDNode *N);
169 SDOperand visitSDIV(SDNode *N);
170 SDOperand visitUDIV(SDNode *N);
171 SDOperand visitSREM(SDNode *N);
172 SDOperand visitUREM(SDNode *N);
173 SDOperand visitMULHU(SDNode *N);
174 SDOperand visitMULHS(SDNode *N);
175 SDOperand visitAND(SDNode *N);
176 SDOperand visitOR(SDNode *N);
177 SDOperand visitXOR(SDNode *N);
Chris Lattneredab1b92006-04-02 03:25:57 +0000178 SDOperand visitVBinOp(SDNode *N, ISD::NodeType IntOp, ISD::NodeType FPOp);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000179 SDOperand visitSHL(SDNode *N);
180 SDOperand visitSRA(SDNode *N);
181 SDOperand visitSRL(SDNode *N);
182 SDOperand visitCTLZ(SDNode *N);
183 SDOperand visitCTTZ(SDNode *N);
184 SDOperand visitCTPOP(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000185 SDOperand visitSELECT(SDNode *N);
186 SDOperand visitSELECT_CC(SDNode *N);
187 SDOperand visitSETCC(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000188 SDOperand visitSIGN_EXTEND(SDNode *N);
189 SDOperand visitZERO_EXTEND(SDNode *N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000190 SDOperand visitANY_EXTEND(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000191 SDOperand visitSIGN_EXTEND_INREG(SDNode *N);
192 SDOperand visitTRUNCATE(SDNode *N);
Chris Lattner94683772005-12-23 05:30:37 +0000193 SDOperand visitBIT_CONVERT(SDNode *N);
Chris Lattner6258fb22006-04-02 02:53:43 +0000194 SDOperand visitVBIT_CONVERT(SDNode *N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000195 SDOperand visitFADD(SDNode *N);
196 SDOperand visitFSUB(SDNode *N);
197 SDOperand visitFMUL(SDNode *N);
198 SDOperand visitFDIV(SDNode *N);
199 SDOperand visitFREM(SDNode *N);
Chris Lattner12d83032006-03-05 05:30:57 +0000200 SDOperand visitFCOPYSIGN(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000201 SDOperand visitSINT_TO_FP(SDNode *N);
202 SDOperand visitUINT_TO_FP(SDNode *N);
203 SDOperand visitFP_TO_SINT(SDNode *N);
204 SDOperand visitFP_TO_UINT(SDNode *N);
205 SDOperand visitFP_ROUND(SDNode *N);
206 SDOperand visitFP_ROUND_INREG(SDNode *N);
207 SDOperand visitFP_EXTEND(SDNode *N);
208 SDOperand visitFNEG(SDNode *N);
209 SDOperand visitFABS(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000210 SDOperand visitBRCOND(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000211 SDOperand visitBR_CC(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000212 SDOperand visitLOAD(SDNode *N);
Chris Lattner29cd7db2006-03-31 18:10:41 +0000213 SDOperand visitXEXTLOAD(SDNode *N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000214 SDOperand visitSTORE(SDNode *N);
Chris Lattnerca242442006-03-19 01:27:56 +0000215 SDOperand visitINSERT_VECTOR_ELT(SDNode *N);
216 SDOperand visitVINSERT_VECTOR_ELT(SDNode *N);
Chris Lattnerd7648c82006-03-28 20:28:38 +0000217 SDOperand visitVBUILD_VECTOR(SDNode *N);
Chris Lattner66445d32006-03-28 22:11:53 +0000218 SDOperand visitVECTOR_SHUFFLE(SDNode *N);
Chris Lattnerf1d0c622006-03-31 22:16:43 +0000219 SDOperand visitVVECTOR_SHUFFLE(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000220
Evan Cheng44f1f092006-04-20 08:56:16 +0000221 SDOperand XformToShuffleWithZero(SDNode *N);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000222 SDOperand ReassociateOps(unsigned Opc, SDOperand LHS, SDOperand RHS);
223
Chris Lattner40c62d52005-10-18 06:04:22 +0000224 bool SimplifySelectOps(SDNode *SELECT, SDOperand LHS, SDOperand RHS);
Chris Lattner35e5c142006-05-05 05:51:50 +0000225 SDOperand SimplifyBinOpWithSameOpcodeHands(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000226 SDOperand SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2);
227 SDOperand SimplifySelectCC(SDOperand N0, SDOperand N1, SDOperand N2,
228 SDOperand N3, ISD::CondCode CC);
Nate Begeman452d7be2005-09-16 00:54:12 +0000229 SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N0, SDOperand N1,
Nate Begemane17daeb2005-10-05 21:43:42 +0000230 ISD::CondCode Cond, bool foldBooleans = true);
Chris Lattner6258fb22006-04-02 02:53:43 +0000231 SDOperand ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(SDNode *, MVT::ValueType);
Nate Begeman69575232005-10-20 02:15:44 +0000232 SDOperand BuildSDIV(SDNode *N);
233 SDOperand BuildUDIV(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000234public:
235 DAGCombiner(SelectionDAG &D)
Nate Begeman646d7e22005-09-02 21:18:40 +0000236 : DAG(D), TLI(D.getTargetLoweringInfo()), AfterLegalize(false) {}
Nate Begeman1d4d4142005-09-01 00:19:25 +0000237
238 /// Run - runs the dag combiner on all nodes in the work list
Nate Begeman4ebd8052005-09-01 23:24:04 +0000239 void Run(bool RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000240 };
241}
242
Chris Lattner24664722006-03-01 04:53:38 +0000243//===----------------------------------------------------------------------===//
244// TargetLowering::DAGCombinerInfo implementation
245//===----------------------------------------------------------------------===//
246
247void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) {
248 ((DAGCombiner*)DC)->AddToWorkList(N);
249}
250
251SDOperand TargetLowering::DAGCombinerInfo::
252CombineTo(SDNode *N, const std::vector<SDOperand> &To) {
Chris Lattner3577e382006-08-11 17:56:38 +0000253 return ((DAGCombiner*)DC)->CombineTo(N, &To[0], To.size());
Chris Lattner24664722006-03-01 04:53:38 +0000254}
255
256SDOperand TargetLowering::DAGCombinerInfo::
257CombineTo(SDNode *N, SDOperand Res) {
258 return ((DAGCombiner*)DC)->CombineTo(N, Res);
259}
260
261
262SDOperand TargetLowering::DAGCombinerInfo::
263CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1) {
264 return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1);
265}
266
267
268
269
270//===----------------------------------------------------------------------===//
271
272
Nate Begeman4ebd8052005-09-01 23:24:04 +0000273// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
274// that selects between the values 1 and 0, making it equivalent to a setcc.
Nate Begeman646d7e22005-09-02 21:18:40 +0000275// Also, set the incoming LHS, RHS, and CC references to the appropriate
276// nodes based on the type of node we are checking. This simplifies life a
277// bit for the callers.
278static bool isSetCCEquivalent(SDOperand N, SDOperand &LHS, SDOperand &RHS,
279 SDOperand &CC) {
280 if (N.getOpcode() == ISD::SETCC) {
281 LHS = N.getOperand(0);
282 RHS = N.getOperand(1);
283 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000284 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000285 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000286 if (N.getOpcode() == ISD::SELECT_CC &&
287 N.getOperand(2).getOpcode() == ISD::Constant &&
288 N.getOperand(3).getOpcode() == ISD::Constant &&
289 cast<ConstantSDNode>(N.getOperand(2))->getValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000290 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
291 LHS = N.getOperand(0);
292 RHS = N.getOperand(1);
293 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000294 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000295 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000296 return false;
297}
298
Nate Begeman99801192005-09-07 23:25:52 +0000299// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
300// one use. If this is true, it allows the users to invert the operation for
301// free when it is profitable to do so.
302static bool isOneUseSetCC(SDOperand N) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000303 SDOperand N0, N1, N2;
Nate Begeman646d7e22005-09-02 21:18:40 +0000304 if (isSetCCEquivalent(N, N0, N1, N2) && N.Val->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000305 return true;
306 return false;
307}
308
Nate Begemancd4d58c2006-02-03 06:46:56 +0000309SDOperand DAGCombiner::ReassociateOps(unsigned Opc, SDOperand N0, SDOperand N1){
310 MVT::ValueType VT = N0.getValueType();
311 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
312 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
313 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
314 if (isa<ConstantSDNode>(N1)) {
315 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(1), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000316 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000317 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(0));
318 } else if (N0.hasOneUse()) {
319 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(0), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000320 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000321 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(1));
322 }
323 }
324 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
325 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
326 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
327 if (isa<ConstantSDNode>(N0)) {
328 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(1), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000329 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000330 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(0));
331 } else if (N1.hasOneUse()) {
332 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(0), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000333 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000334 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(1));
335 }
336 }
337 return SDOperand();
338}
339
Nate Begeman4ebd8052005-09-01 23:24:04 +0000340void DAGCombiner::Run(bool RunningAfterLegalize) {
341 // set the instance variable, so that the various visit routines may use it.
342 AfterLegalize = RunningAfterLegalize;
343
Nate Begeman646d7e22005-09-02 21:18:40 +0000344 // Add all the dag nodes to the worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000345 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
346 E = DAG.allnodes_end(); I != E; ++I)
347 WorkList.push_back(I);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000348
Chris Lattner95038592005-10-05 06:35:28 +0000349 // Create a dummy node (which is not added to allnodes), that adds a reference
350 // to the root node, preventing it from being deleted, and tracking any
351 // changes of the root.
352 HandleSDNode Dummy(DAG.getRoot());
353
Chris Lattner24664722006-03-01 04:53:38 +0000354
355 /// DagCombineInfo - Expose the DAG combiner to the target combiner impls.
356 TargetLowering::DAGCombinerInfo
357 DagCombineInfo(DAG, !RunningAfterLegalize, this);
358
Nate Begeman1d4d4142005-09-01 00:19:25 +0000359 // while the worklist isn't empty, inspect the node on the end of it and
360 // try and combine it.
361 while (!WorkList.empty()) {
362 SDNode *N = WorkList.back();
363 WorkList.pop_back();
364
365 // If N has no uses, it is dead. Make sure to revisit all N's operands once
Chris Lattner95038592005-10-05 06:35:28 +0000366 // N is deleted from the DAG, since they too may now be dead or may have a
367 // reduced number of uses, allowing other xforms.
368 if (N->use_empty() && N != &Dummy) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000369 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
370 WorkList.push_back(N->getOperand(i).Val);
371
Nate Begeman1d4d4142005-09-01 00:19:25 +0000372 removeFromWorkList(N);
Chris Lattner95038592005-10-05 06:35:28 +0000373 DAG.DeleteNode(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000374 continue;
375 }
376
Nate Begeman83e75ec2005-09-06 04:43:02 +0000377 SDOperand RV = visit(N);
Chris Lattner24664722006-03-01 04:53:38 +0000378
379 // If nothing happened, try a target-specific DAG combine.
380 if (RV.Val == 0) {
Chris Lattner729c6d12006-05-27 00:43:02 +0000381 assert(N->getOpcode() != ISD::DELETED_NODE &&
382 "Node was deleted but visit returned NULL!");
Chris Lattner24664722006-03-01 04:53:38 +0000383 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
384 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode()))
385 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
386 }
387
Nate Begeman83e75ec2005-09-06 04:43:02 +0000388 if (RV.Val) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000389 ++NodesCombined;
Nate Begeman646d7e22005-09-02 21:18:40 +0000390 // If we get back the same node we passed in, rather than a new node or
391 // zero, we know that the node must have defined multiple values and
392 // CombineTo was used. Since CombineTo takes care of the worklist
393 // mechanics for us, we have no work to do in this case.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000394 if (RV.Val != N) {
Chris Lattner729c6d12006-05-27 00:43:02 +0000395 assert(N->getOpcode() != ISD::DELETED_NODE &&
396 RV.Val->getOpcode() != ISD::DELETED_NODE &&
397 "Node was deleted but visit returned new node!");
398
Nate Begeman2300f552005-09-07 00:15:36 +0000399 DEBUG(std::cerr << "\nReplacing "; N->dump();
Evan Cheng60e8c712006-05-09 06:55:15 +0000400 std::cerr << "\nWith: "; RV.Val->dump(&DAG);
Nate Begeman2300f552005-09-07 00:15:36 +0000401 std::cerr << '\n');
Chris Lattner01a22022005-10-10 22:04:48 +0000402 std::vector<SDNode*> NowDead;
Chris Lattnerb9ea4a32006-08-11 17:46:28 +0000403 SDOperand OpV = RV;
404 DAG.ReplaceAllUsesWith(N, &OpV, &NowDead);
Nate Begeman646d7e22005-09-02 21:18:40 +0000405
406 // Push the new node and any users onto the worklist
Nate Begeman83e75ec2005-09-06 04:43:02 +0000407 WorkList.push_back(RV.Val);
408 AddUsersToWorkList(RV.Val);
Nate Begeman646d7e22005-09-02 21:18:40 +0000409
410 // Nodes can end up on the worklist more than once. Make sure we do
411 // not process a node that has been replaced.
412 removeFromWorkList(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000413 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
414 removeFromWorkList(NowDead[i]);
Chris Lattner5c46f742005-10-05 06:11:08 +0000415
416 // Finally, since the node is now dead, remove it from the graph.
417 DAG.DeleteNode(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000418 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000419 }
420 }
Chris Lattner95038592005-10-05 06:35:28 +0000421
422 // If the root changed (e.g. it was a dead load, update the root).
423 DAG.setRoot(Dummy.getValue());
Nate Begeman1d4d4142005-09-01 00:19:25 +0000424}
425
Nate Begeman83e75ec2005-09-06 04:43:02 +0000426SDOperand DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000427 switch(N->getOpcode()) {
428 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +0000429 case ISD::TokenFactor: return visitTokenFactor(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000430 case ISD::ADD: return visitADD(N);
431 case ISD::SUB: return visitSUB(N);
432 case ISD::MUL: return visitMUL(N);
433 case ISD::SDIV: return visitSDIV(N);
434 case ISD::UDIV: return visitUDIV(N);
435 case ISD::SREM: return visitSREM(N);
436 case ISD::UREM: return visitUREM(N);
437 case ISD::MULHU: return visitMULHU(N);
438 case ISD::MULHS: return visitMULHS(N);
439 case ISD::AND: return visitAND(N);
440 case ISD::OR: return visitOR(N);
441 case ISD::XOR: return visitXOR(N);
442 case ISD::SHL: return visitSHL(N);
443 case ISD::SRA: return visitSRA(N);
444 case ISD::SRL: return visitSRL(N);
445 case ISD::CTLZ: return visitCTLZ(N);
446 case ISD::CTTZ: return visitCTTZ(N);
447 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000448 case ISD::SELECT: return visitSELECT(N);
449 case ISD::SELECT_CC: return visitSELECT_CC(N);
450 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000451 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
452 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000453 case ISD::ANY_EXTEND: return visitANY_EXTEND(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000454 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
455 case ISD::TRUNCATE: return visitTRUNCATE(N);
Chris Lattner94683772005-12-23 05:30:37 +0000456 case ISD::BIT_CONVERT: return visitBIT_CONVERT(N);
Chris Lattner6258fb22006-04-02 02:53:43 +0000457 case ISD::VBIT_CONVERT: return visitVBIT_CONVERT(N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000458 case ISD::FADD: return visitFADD(N);
459 case ISD::FSUB: return visitFSUB(N);
460 case ISD::FMUL: return visitFMUL(N);
461 case ISD::FDIV: return visitFDIV(N);
462 case ISD::FREM: return visitFREM(N);
Chris Lattner12d83032006-03-05 05:30:57 +0000463 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000464 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
465 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
466 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
467 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
468 case ISD::FP_ROUND: return visitFP_ROUND(N);
469 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
470 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
471 case ISD::FNEG: return visitFNEG(N);
472 case ISD::FABS: return visitFABS(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000473 case ISD::BRCOND: return visitBRCOND(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000474 case ISD::BR_CC: return visitBR_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000475 case ISD::LOAD: return visitLOAD(N);
Chris Lattner29cd7db2006-03-31 18:10:41 +0000476 case ISD::EXTLOAD:
477 case ISD::SEXTLOAD:
478 case ISD::ZEXTLOAD: return visitXEXTLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000479 case ISD::STORE: return visitSTORE(N);
Chris Lattnerca242442006-03-19 01:27:56 +0000480 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
481 case ISD::VINSERT_VECTOR_ELT: return visitVINSERT_VECTOR_ELT(N);
Chris Lattnerd7648c82006-03-28 20:28:38 +0000482 case ISD::VBUILD_VECTOR: return visitVBUILD_VECTOR(N);
Chris Lattner66445d32006-03-28 22:11:53 +0000483 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
Chris Lattnerf1d0c622006-03-31 22:16:43 +0000484 case ISD::VVECTOR_SHUFFLE: return visitVVECTOR_SHUFFLE(N);
Chris Lattneredab1b92006-04-02 03:25:57 +0000485 case ISD::VADD: return visitVBinOp(N, ISD::ADD , ISD::FADD);
486 case ISD::VSUB: return visitVBinOp(N, ISD::SUB , ISD::FSUB);
487 case ISD::VMUL: return visitVBinOp(N, ISD::MUL , ISD::FMUL);
488 case ISD::VSDIV: return visitVBinOp(N, ISD::SDIV, ISD::FDIV);
489 case ISD::VUDIV: return visitVBinOp(N, ISD::UDIV, ISD::UDIV);
490 case ISD::VAND: return visitVBinOp(N, ISD::AND , ISD::AND);
491 case ISD::VOR: return visitVBinOp(N, ISD::OR , ISD::OR);
492 case ISD::VXOR: return visitVBinOp(N, ISD::XOR , ISD::XOR);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000493 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000494 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000495}
496
Nate Begeman83e75ec2005-09-06 04:43:02 +0000497SDOperand DAGCombiner::visitTokenFactor(SDNode *N) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000498 SmallVector<SDOperand, 8> Ops;
Nate Begemanded49632005-10-13 03:11:28 +0000499 bool Changed = false;
500
Nate Begeman1d4d4142005-09-01 00:19:25 +0000501 // If the token factor has two operands and one is the entry token, replace
502 // the token factor with the other operand.
503 if (N->getNumOperands() == 2) {
Chris Lattner21a57dc2006-05-12 05:01:37 +0000504 if (N->getOperand(0).getOpcode() == ISD::EntryToken ||
505 N->getOperand(0) == N->getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000506 return N->getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000507 if (N->getOperand(1).getOpcode() == ISD::EntryToken)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000508 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000509 }
Chris Lattner24edbb72005-10-13 22:10:05 +0000510
Nate Begemanded49632005-10-13 03:11:28 +0000511 // fold (tokenfactor (tokenfactor)) -> tokenfactor
512 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
513 SDOperand Op = N->getOperand(i);
514 if (Op.getOpcode() == ISD::TokenFactor && Op.hasOneUse()) {
Chris Lattnerac0f8f22006-03-13 18:37:30 +0000515 AddToWorkList(Op.Val); // Remove dead node.
Nate Begemanded49632005-10-13 03:11:28 +0000516 Changed = true;
517 for (unsigned j = 0, e = Op.getNumOperands(); j != e; ++j)
518 Ops.push_back(Op.getOperand(j));
Chris Lattner21a57dc2006-05-12 05:01:37 +0000519 } else if (i == 0 || N->getOperand(i) != N->getOperand(i-1)) {
Nate Begemanded49632005-10-13 03:11:28 +0000520 Ops.push_back(Op);
Chris Lattner21a57dc2006-05-12 05:01:37 +0000521 } else {
522 // Deleted an operand that was the same as the last one.
523 Changed = true;
Nate Begemanded49632005-10-13 03:11:28 +0000524 }
525 }
526 if (Changed)
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000527 return DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0], Ops.size());
Nate Begeman83e75ec2005-09-06 04:43:02 +0000528 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000529}
530
Nate Begeman83e75ec2005-09-06 04:43:02 +0000531SDOperand DAGCombiner::visitADD(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000532 SDOperand N0 = N->getOperand(0);
533 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000534 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
535 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemanf89d78d2005-09-07 16:09:19 +0000536 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000537
538 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000539 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000540 return DAG.getNode(ISD::ADD, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000541 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000542 if (N0C && !N1C)
543 return DAG.getNode(ISD::ADD, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000544 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000545 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000546 return N0;
Chris Lattner4aafb4f2006-01-12 20:22:43 +0000547 // fold ((c1-A)+c2) -> (c1+c2)-A
548 if (N1C && N0.getOpcode() == ISD::SUB)
549 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
550 return DAG.getNode(ISD::SUB, VT,
551 DAG.getConstant(N1C->getValue()+N0C->getValue(), VT),
552 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000553 // reassociate add
554 SDOperand RADD = ReassociateOps(ISD::ADD, N0, N1);
555 if (RADD.Val != 0)
556 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000557 // fold ((0-A) + B) -> B-A
558 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
559 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000560 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000561 // fold (A + (0-B)) -> A-B
562 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
563 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000564 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +0000565 // fold (A+(B-A)) -> B
566 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000567 return N1.getOperand(0);
Chris Lattner947c2892006-03-13 06:51:27 +0000568
Evan Cheng860771d2006-03-01 01:09:54 +0000569 if (!MVT::isVector(VT) && SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +0000570 return SDOperand(N, 0);
Chris Lattner947c2892006-03-13 06:51:27 +0000571
572 // fold (a+b) -> (a|b) iff a and b share no bits.
573 if (MVT::isInteger(VT) && !MVT::isVector(VT)) {
574 uint64_t LHSZero, LHSOne;
575 uint64_t RHSZero, RHSOne;
576 uint64_t Mask = MVT::getIntVTBitMask(VT);
577 TLI.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
578 if (LHSZero) {
579 TLI.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
580
581 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
582 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
583 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
584 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
585 return DAG.getNode(ISD::OR, VT, N0, N1);
586 }
587 }
588
Nate Begeman83e75ec2005-09-06 04:43:02 +0000589 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000590}
591
Nate Begeman83e75ec2005-09-06 04:43:02 +0000592SDOperand DAGCombiner::visitSUB(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000593 SDOperand N0 = N->getOperand(0);
594 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000595 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
596 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000597 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000598
Chris Lattner854077d2005-10-17 01:07:11 +0000599 // fold (sub x, x) -> 0
600 if (N0 == N1)
601 return DAG.getConstant(0, N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000602 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000603 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000604 return DAG.getNode(ISD::SUB, VT, N0, N1);
Chris Lattner05b57432005-10-11 06:07:15 +0000605 // fold (sub x, c) -> (add x, -c)
606 if (N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000607 return DAG.getNode(ISD::ADD, VT, N0, DAG.getConstant(-N1C->getValue(), VT));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000608 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +0000609 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000610 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000611 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +0000612 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000613 return N0.getOperand(0);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000614 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000615}
616
Nate Begeman83e75ec2005-09-06 04:43:02 +0000617SDOperand DAGCombiner::visitMUL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000618 SDOperand N0 = N->getOperand(0);
619 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000620 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
621 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman223df222005-09-08 20:18:10 +0000622 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000623
624 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000625 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000626 return DAG.getNode(ISD::MUL, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000627 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000628 if (N0C && !N1C)
629 return DAG.getNode(ISD::MUL, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000630 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000631 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000632 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000633 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +0000634 if (N1C && N1C->isAllOnesValue())
Nate Begeman405e3ec2005-10-21 00:02:42 +0000635 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000636 // fold (mul x, (1 << c)) -> x << c
Nate Begeman646d7e22005-09-02 21:18:40 +0000637 if (N1C && isPowerOf2_64(N1C->getValue()))
Chris Lattner3e6099b2005-10-30 06:41:49 +0000638 return DAG.getNode(ISD::SHL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000639 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000640 TLI.getShiftAmountTy()));
Chris Lattner3e6099b2005-10-30 06:41:49 +0000641 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
642 if (N1C && isPowerOf2_64(-N1C->getSignExtended())) {
643 // FIXME: If the input is something that is easily negated (e.g. a
644 // single-use add), we should put the negate there.
645 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT),
646 DAG.getNode(ISD::SHL, VT, N0,
647 DAG.getConstant(Log2_64(-N1C->getSignExtended()),
648 TLI.getShiftAmountTy())));
649 }
Andrew Lenharth50a0d422006-04-02 21:42:45 +0000650
Chris Lattner0b1a85f2006-03-01 03:44:24 +0000651 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
652 if (N1C && N0.getOpcode() == ISD::SHL &&
653 isa<ConstantSDNode>(N0.getOperand(1))) {
654 SDOperand C3 = DAG.getNode(ISD::SHL, VT, N1, N0.getOperand(1));
Chris Lattner5750df92006-03-01 04:03:14 +0000655 AddToWorkList(C3.Val);
Chris Lattner0b1a85f2006-03-01 03:44:24 +0000656 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0), C3);
657 }
658
659 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
660 // use.
661 {
662 SDOperand Sh(0,0), Y(0,0);
663 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
664 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
665 N0.Val->hasOneUse()) {
666 Sh = N0; Y = N1;
667 } else if (N1.getOpcode() == ISD::SHL &&
668 isa<ConstantSDNode>(N1.getOperand(1)) && N1.Val->hasOneUse()) {
669 Sh = N1; Y = N0;
670 }
671 if (Sh.Val) {
672 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Sh.getOperand(0), Y);
673 return DAG.getNode(ISD::SHL, VT, Mul, Sh.getOperand(1));
674 }
675 }
Chris Lattnera1deca32006-03-04 23:33:26 +0000676 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
677 if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() &&
678 isa<ConstantSDNode>(N0.getOperand(1))) {
679 return DAG.getNode(ISD::ADD, VT,
680 DAG.getNode(ISD::MUL, VT, N0.getOperand(0), N1),
681 DAG.getNode(ISD::MUL, VT, N0.getOperand(1), N1));
682 }
Chris Lattner0b1a85f2006-03-01 03:44:24 +0000683
Nate Begemancd4d58c2006-02-03 06:46:56 +0000684 // reassociate mul
685 SDOperand RMUL = ReassociateOps(ISD::MUL, N0, N1);
686 if (RMUL.Val != 0)
687 return RMUL;
Nate Begeman83e75ec2005-09-06 04:43:02 +0000688 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000689}
690
Nate Begeman83e75ec2005-09-06 04:43:02 +0000691SDOperand DAGCombiner::visitSDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000692 SDOperand N0 = N->getOperand(0);
693 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000694 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
695 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000696 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000697
698 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000699 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000700 return DAG.getNode(ISD::SDIV, VT, N0, N1);
Nate Begeman405e3ec2005-10-21 00:02:42 +0000701 // fold (sdiv X, 1) -> X
702 if (N1C && N1C->getSignExtended() == 1LL)
703 return N0;
704 // fold (sdiv X, -1) -> 0-X
705 if (N1C && N1C->isAllOnesValue())
706 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +0000707 // If we know the sign bits of both operands are zero, strength reduce to a
708 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
709 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000710 if (TLI.MaskedValueIsZero(N1, SignBit) &&
711 TLI.MaskedValueIsZero(N0, SignBit))
Chris Lattner094c8fc2005-10-07 06:10:46 +0000712 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
Nate Begemancd6a6ed2006-02-17 07:26:20 +0000713 // fold (sdiv X, pow2) -> simple ops after legalize
Nate Begemanfb7217b2006-02-17 19:54:08 +0000714 if (N1C && N1C->getValue() && !TLI.isIntDivCheap() &&
Nate Begeman405e3ec2005-10-21 00:02:42 +0000715 (isPowerOf2_64(N1C->getSignExtended()) ||
716 isPowerOf2_64(-N1C->getSignExtended()))) {
717 // If dividing by powers of two is cheap, then don't perform the following
718 // fold.
719 if (TLI.isPow2DivCheap())
720 return SDOperand();
721 int64_t pow2 = N1C->getSignExtended();
722 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
Chris Lattner8f4880b2006-02-16 08:02:36 +0000723 unsigned lg2 = Log2_64(abs2);
724 // Splat the sign bit into the register
725 SDOperand SGN = DAG.getNode(ISD::SRA, VT, N0,
Nate Begeman405e3ec2005-10-21 00:02:42 +0000726 DAG.getConstant(MVT::getSizeInBits(VT)-1,
727 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +0000728 AddToWorkList(SGN.Val);
Chris Lattner8f4880b2006-02-16 08:02:36 +0000729 // Add (N0 < 0) ? abs2 - 1 : 0;
730 SDOperand SRL = DAG.getNode(ISD::SRL, VT, SGN,
731 DAG.getConstant(MVT::getSizeInBits(VT)-lg2,
Nate Begeman405e3ec2005-10-21 00:02:42 +0000732 TLI.getShiftAmountTy()));
Chris Lattner8f4880b2006-02-16 08:02:36 +0000733 SDOperand ADD = DAG.getNode(ISD::ADD, VT, N0, SRL);
Chris Lattner5750df92006-03-01 04:03:14 +0000734 AddToWorkList(SRL.Val);
735 AddToWorkList(ADD.Val); // Divide by pow2
Chris Lattner8f4880b2006-02-16 08:02:36 +0000736 SDOperand SRA = DAG.getNode(ISD::SRA, VT, ADD,
737 DAG.getConstant(lg2, TLI.getShiftAmountTy()));
Nate Begeman405e3ec2005-10-21 00:02:42 +0000738 // If we're dividing by a positive value, we're done. Otherwise, we must
739 // negate the result.
740 if (pow2 > 0)
741 return SRA;
Chris Lattner5750df92006-03-01 04:03:14 +0000742 AddToWorkList(SRA.Val);
Nate Begeman405e3ec2005-10-21 00:02:42 +0000743 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), SRA);
744 }
Nate Begeman69575232005-10-20 02:15:44 +0000745 // if integer divide is expensive and we satisfy the requirements, emit an
746 // alternate sequence.
Nate Begeman405e3ec2005-10-21 00:02:42 +0000747 if (N1C && (N1C->getSignExtended() < -1 || N1C->getSignExtended() > 1) &&
Chris Lattnere9936d12005-10-22 18:50:15 +0000748 !TLI.isIntDivCheap()) {
749 SDOperand Op = BuildSDIV(N);
750 if (Op.Val) return Op;
Nate Begeman69575232005-10-20 02:15:44 +0000751 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000752 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000753}
754
Nate Begeman83e75ec2005-09-06 04:43:02 +0000755SDOperand DAGCombiner::visitUDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000756 SDOperand N0 = N->getOperand(0);
757 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000758 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
759 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000760 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000761
762 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000763 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000764 return DAG.getNode(ISD::UDIV, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000765 // fold (udiv x, (1 << c)) -> x >>u c
Nate Begeman646d7e22005-09-02 21:18:40 +0000766 if (N1C && isPowerOf2_64(N1C->getValue()))
Nate Begemanfb5e4bd2006-02-05 07:20:23 +0000767 return DAG.getNode(ISD::SRL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000768 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000769 TLI.getShiftAmountTy()));
Nate Begemanfb5e4bd2006-02-05 07:20:23 +0000770 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
771 if (N1.getOpcode() == ISD::SHL) {
772 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
773 if (isPowerOf2_64(SHC->getValue())) {
774 MVT::ValueType ADDVT = N1.getOperand(1).getValueType();
Nate Begemanc031e332006-02-05 07:36:48 +0000775 SDOperand Add = DAG.getNode(ISD::ADD, ADDVT, N1.getOperand(1),
776 DAG.getConstant(Log2_64(SHC->getValue()),
777 ADDVT));
Chris Lattner5750df92006-03-01 04:03:14 +0000778 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +0000779 return DAG.getNode(ISD::SRL, VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +0000780 }
781 }
782 }
Nate Begeman69575232005-10-20 02:15:44 +0000783 // fold (udiv x, c) -> alternate
Chris Lattnere9936d12005-10-22 18:50:15 +0000784 if (N1C && N1C->getValue() && !TLI.isIntDivCheap()) {
785 SDOperand Op = BuildUDIV(N);
786 if (Op.Val) return Op;
787 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000788 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000789}
790
Nate Begeman83e75ec2005-09-06 04:43:02 +0000791SDOperand DAGCombiner::visitSREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000792 SDOperand N0 = N->getOperand(0);
793 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000794 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
795 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +0000796 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000797
798 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000799 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000800 return DAG.getNode(ISD::SREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +0000801 // If we know the sign bits of both operands are zero, strength reduce to a
802 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
803 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000804 if (TLI.MaskedValueIsZero(N1, SignBit) &&
805 TLI.MaskedValueIsZero(N0, SignBit))
Nate Begemana148d982006-01-18 22:35:16 +0000806 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000807 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000808}
809
Nate Begeman83e75ec2005-09-06 04:43:02 +0000810SDOperand DAGCombiner::visitUREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000811 SDOperand N0 = N->getOperand(0);
812 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000813 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
814 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +0000815 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000816
817 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000818 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000819 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +0000820 // fold (urem x, pow2) -> (and x, pow2-1)
821 if (N1C && !N1C->isNullValue() && isPowerOf2_64(N1C->getValue()))
Nate Begemana148d982006-01-18 22:35:16 +0000822 return DAG.getNode(ISD::AND, VT, N0, DAG.getConstant(N1C->getValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +0000823 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
824 if (N1.getOpcode() == ISD::SHL) {
825 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
826 if (isPowerOf2_64(SHC->getValue())) {
Nate Begemanbab92392006-02-05 08:07:24 +0000827 SDOperand Add = DAG.getNode(ISD::ADD, VT, N1,DAG.getConstant(~0ULL,VT));
Chris Lattner5750df92006-03-01 04:03:14 +0000828 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +0000829 return DAG.getNode(ISD::AND, VT, N0, Add);
830 }
831 }
832 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000833 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000834}
835
Nate Begeman83e75ec2005-09-06 04:43:02 +0000836SDOperand DAGCombiner::visitMULHS(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000837 SDOperand N0 = N->getOperand(0);
838 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000839 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000840
841 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000842 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000843 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000844 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Nate Begeman646d7e22005-09-02 21:18:40 +0000845 if (N1C && N1C->getValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +0000846 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
847 DAG.getConstant(MVT::getSizeInBits(N0.getValueType())-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +0000848 TLI.getShiftAmountTy()));
849 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000850}
851
Nate Begeman83e75ec2005-09-06 04:43:02 +0000852SDOperand DAGCombiner::visitMULHU(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000853 SDOperand N0 = N->getOperand(0);
854 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000855 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000856
857 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000858 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000859 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000860 // fold (mulhu x, 1) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000861 if (N1C && N1C->getValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000862 return DAG.getConstant(0, N0.getValueType());
863 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000864}
865
Chris Lattner35e5c142006-05-05 05:51:50 +0000866/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
867/// two operands of the same opcode, try to simplify it.
868SDOperand DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
869 SDOperand N0 = N->getOperand(0), N1 = N->getOperand(1);
870 MVT::ValueType VT = N0.getValueType();
871 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
872
Chris Lattner540121f2006-05-05 06:31:05 +0000873 // For each of OP in AND/OR/XOR:
874 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
875 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
876 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
Chris Lattner0d8dae72006-05-05 06:32:04 +0000877 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y))
Chris Lattner540121f2006-05-05 06:31:05 +0000878 if ((N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND||
Chris Lattner0d8dae72006-05-05 06:32:04 +0000879 N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::TRUNCATE) &&
Chris Lattner35e5c142006-05-05 05:51:50 +0000880 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
881 SDOperand ORNode = DAG.getNode(N->getOpcode(),
882 N0.getOperand(0).getValueType(),
883 N0.getOperand(0), N1.getOperand(0));
884 AddToWorkList(ORNode.Val);
Chris Lattner540121f2006-05-05 06:31:05 +0000885 return DAG.getNode(N0.getOpcode(), VT, ORNode);
Chris Lattner35e5c142006-05-05 05:51:50 +0000886 }
887
Chris Lattnera3dc3f62006-05-05 06:10:43 +0000888 // For each of OP in SHL/SRL/SRA/AND...
889 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
890 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
891 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
Chris Lattner35e5c142006-05-05 05:51:50 +0000892 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
Chris Lattnera3dc3f62006-05-05 06:10:43 +0000893 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
Chris Lattner35e5c142006-05-05 05:51:50 +0000894 N0.getOperand(1) == N1.getOperand(1)) {
895 SDOperand ORNode = DAG.getNode(N->getOpcode(),
896 N0.getOperand(0).getValueType(),
897 N0.getOperand(0), N1.getOperand(0));
898 AddToWorkList(ORNode.Val);
899 return DAG.getNode(N0.getOpcode(), VT, ORNode, N0.getOperand(1));
900 }
901
902 return SDOperand();
903}
904
Nate Begeman83e75ec2005-09-06 04:43:02 +0000905SDOperand DAGCombiner::visitAND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000906 SDOperand N0 = N->getOperand(0);
907 SDOperand N1 = N->getOperand(1);
Nate Begemanfb7217b2006-02-17 19:54:08 +0000908 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +0000909 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
910 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000911 MVT::ValueType VT = N1.getValueType();
Nate Begeman83e75ec2005-09-06 04:43:02 +0000912 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000913
914 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000915 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000916 return DAG.getNode(ISD::AND, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000917 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000918 if (N0C && !N1C)
919 return DAG.getNode(ISD::AND, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000920 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000921 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000922 return N0;
923 // if (and x, c) is known to be zero, return 0
Nate Begeman368e18d2006-02-16 21:11:51 +0000924 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000925 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000926 // reassociate and
927 SDOperand RAND = ReassociateOps(ISD::AND, N0, N1);
928 if (RAND.Val != 0)
929 return RAND;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000930 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
Nate Begeman5dc7e862005-11-02 18:42:59 +0000931 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +0000932 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Nate Begeman646d7e22005-09-02 21:18:40 +0000933 if ((ORI->getValue() & N1C->getValue()) == N1C->getValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000934 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +0000935 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
936 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Chris Lattner1ec05d12006-03-01 21:47:21 +0000937 unsigned InMask = MVT::getIntVTBitMask(N0.getOperand(0).getValueType());
Chris Lattner3603cd62006-02-02 07:17:31 +0000938 if (TLI.MaskedValueIsZero(N0.getOperand(0),
Chris Lattner1ec05d12006-03-01 21:47:21 +0000939 ~N1C->getValue() & InMask)) {
940 SDOperand Zext = DAG.getNode(ISD::ZERO_EXTEND, N0.getValueType(),
941 N0.getOperand(0));
942
943 // Replace uses of the AND with uses of the Zero extend node.
944 CombineTo(N, Zext);
945
Chris Lattner3603cd62006-02-02 07:17:31 +0000946 // We actually want to replace all uses of the any_extend with the
947 // zero_extend, to avoid duplicating things. This will later cause this
948 // AND to be folded.
Chris Lattner1ec05d12006-03-01 21:47:21 +0000949 CombineTo(N0.Val, Zext);
Chris Lattnerfedced72006-04-20 23:55:59 +0000950 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner3603cd62006-02-02 07:17:31 +0000951 }
952 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000953 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
954 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
955 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
956 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
957
958 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
959 MVT::isInteger(LL.getValueType())) {
960 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
961 if (cast<ConstantSDNode>(LR)->getValue() == 0 && Op1 == ISD::SETEQ) {
962 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +0000963 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000964 return DAG.getSetCC(VT, ORNode, LR, Op1);
965 }
966 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
967 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
968 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +0000969 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000970 return DAG.getSetCC(VT, ANDNode, LR, Op1);
971 }
972 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
973 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
974 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +0000975 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000976 return DAG.getSetCC(VT, ORNode, LR, Op1);
977 }
978 }
979 // canonicalize equivalent to ll == rl
980 if (LL == RR && LR == RL) {
981 Op1 = ISD::getSetCCSwappedOperands(Op1);
982 std::swap(RL, RR);
983 }
984 if (LL == RL && LR == RR) {
985 bool isInteger = MVT::isInteger(LL.getValueType());
986 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
987 if (Result != ISD::SETCC_INVALID)
988 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
989 }
990 }
Chris Lattner35e5c142006-05-05 05:51:50 +0000991
992 // Simplify: and (op x...), (op y...) -> (op (and x, y))
993 if (N0.getOpcode() == N1.getOpcode()) {
994 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
995 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000996 }
Chris Lattner35e5c142006-05-05 05:51:50 +0000997
Nate Begemande996292006-02-03 22:24:05 +0000998 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
999 // fold (and (sra)) -> (and (srl)) when possible.
Chris Lattner6ea2dee2006-03-25 22:19:00 +00001000 if (!MVT::isVector(VT) &&
1001 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001002 return SDOperand(N, 0);
Nate Begemanded49632005-10-13 03:11:28 +00001003 // fold (zext_inreg (extload x)) -> (zextload x)
Nate Begeman5054f162005-10-14 01:12:21 +00001004 if (N0.getOpcode() == ISD::EXTLOAD) {
Nate Begemanded49632005-10-13 03:11:28 +00001005 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001006 // If we zero all the possible extended bits, then we can turn this into
1007 // a zextload if we are running before legalize or the operation is legal.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001008 if (TLI.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Chris Lattner67a44cd2005-10-13 18:16:34 +00001009 (!AfterLegalize || TLI.isOperationLegal(ISD::ZEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001010 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1011 N0.getOperand(1), N0.getOperand(2),
1012 EVT);
Chris Lattner5750df92006-03-01 04:03:14 +00001013 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001014 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001015 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001016 }
1017 }
1018 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Chris Lattner40c62d52005-10-18 06:04:22 +00001019 if (N0.getOpcode() == ISD::SEXTLOAD && N0.hasOneUse()) {
Nate Begemanded49632005-10-13 03:11:28 +00001020 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001021 // If we zero all the possible extended bits, then we can turn this into
1022 // a zextload if we are running before legalize or the operation is legal.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001023 if (TLI.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Nate Begemanbfd65a02005-10-13 18:34:58 +00001024 (!AfterLegalize || TLI.isOperationLegal(ISD::ZEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001025 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1026 N0.getOperand(1), N0.getOperand(2),
1027 EVT);
Chris Lattner5750df92006-03-01 04:03:14 +00001028 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001029 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001030 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001031 }
1032 }
Chris Lattner15045b62006-02-28 06:35:35 +00001033
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001034 // fold (and (load x), 255) -> (zextload x, i8)
1035 // fold (and (extload x, i16), 255) -> (zextload x, i8)
1036 if (N1C &&
1037 (N0.getOpcode() == ISD::LOAD || N0.getOpcode() == ISD::EXTLOAD ||
1038 N0.getOpcode() == ISD::ZEXTLOAD) &&
1039 N0.hasOneUse()) {
1040 MVT::ValueType EVT, LoadedVT;
Chris Lattner15045b62006-02-28 06:35:35 +00001041 if (N1C->getValue() == 255)
1042 EVT = MVT::i8;
1043 else if (N1C->getValue() == 65535)
1044 EVT = MVT::i16;
1045 else if (N1C->getValue() == ~0U)
1046 EVT = MVT::i32;
1047 else
1048 EVT = MVT::Other;
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001049
1050 LoadedVT = N0.getOpcode() == ISD::LOAD ? VT :
1051 cast<VTSDNode>(N0.getOperand(3))->getVT();
Chris Lattnere44be602006-04-04 17:39:18 +00001052 if (EVT != MVT::Other && LoadedVT > EVT &&
1053 (!AfterLegalize || TLI.isOperationLegal(ISD::ZEXTLOAD, EVT))) {
Chris Lattner15045b62006-02-28 06:35:35 +00001054 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
1055 // For big endian targets, we need to add an offset to the pointer to load
1056 // the correct bytes. For little endian systems, we merely need to read
1057 // fewer bytes from the same pointer.
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001058 unsigned PtrOff =
1059 (MVT::getSizeInBits(LoadedVT) - MVT::getSizeInBits(EVT)) / 8;
1060 SDOperand NewPtr = N0.getOperand(1);
1061 if (!TLI.isLittleEndian())
1062 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
1063 DAG.getConstant(PtrOff, PtrType));
Chris Lattner5750df92006-03-01 04:03:14 +00001064 AddToWorkList(NewPtr.Val);
Chris Lattner15045b62006-02-28 06:35:35 +00001065 SDOperand Load =
1066 DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0), NewPtr,
1067 N0.getOperand(2), EVT);
Chris Lattner5750df92006-03-01 04:03:14 +00001068 AddToWorkList(N);
Chris Lattner15045b62006-02-28 06:35:35 +00001069 CombineTo(N0.Val, Load, Load.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001070 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner15045b62006-02-28 06:35:35 +00001071 }
1072 }
1073
Nate Begeman83e75ec2005-09-06 04:43:02 +00001074 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001075}
1076
Nate Begeman83e75ec2005-09-06 04:43:02 +00001077SDOperand DAGCombiner::visitOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001078 SDOperand N0 = N->getOperand(0);
1079 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001080 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001081 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1082 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001083 MVT::ValueType VT = N1.getValueType();
1084 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001085
1086 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001087 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001088 return DAG.getNode(ISD::OR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001089 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001090 if (N0C && !N1C)
1091 return DAG.getNode(ISD::OR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001092 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001093 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001094 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001095 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001096 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001097 return N1;
1098 // fold (or x, c) -> c iff (x & ~c) == 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001099 if (N1C &&
1100 TLI.MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits))))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001101 return N1;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001102 // reassociate or
1103 SDOperand ROR = ReassociateOps(ISD::OR, N0, N1);
1104 if (ROR.Val != 0)
1105 return ROR;
1106 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
1107 if (N1C && N0.getOpcode() == ISD::AND && N0.Val->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00001108 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00001109 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
1110 return DAG.getNode(ISD::AND, VT, DAG.getNode(ISD::OR, VT, N0.getOperand(0),
1111 N1),
1112 DAG.getConstant(N1C->getValue() | C1->getValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00001113 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001114 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
1115 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1116 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1117 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1118
1119 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1120 MVT::isInteger(LL.getValueType())) {
1121 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
1122 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
1123 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
1124 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
1125 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001126 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001127 return DAG.getSetCC(VT, ORNode, LR, Op1);
1128 }
1129 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
1130 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
1131 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
1132 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
1133 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001134 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001135 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1136 }
1137 }
1138 // canonicalize equivalent to ll == rl
1139 if (LL == RR && LR == RL) {
1140 Op1 = ISD::getSetCCSwappedOperands(Op1);
1141 std::swap(RL, RR);
1142 }
1143 if (LL == RL && LR == RR) {
1144 bool isInteger = MVT::isInteger(LL.getValueType());
1145 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
1146 if (Result != ISD::SETCC_INVALID)
1147 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1148 }
1149 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001150
1151 // Simplify: or (op x...), (op y...) -> (op (or x, y))
1152 if (N0.getOpcode() == N1.getOpcode()) {
1153 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1154 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001155 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001156
Nate Begeman35ef9132006-01-11 21:21:00 +00001157 // canonicalize shl to left side in a shl/srl pair, to match rotate
1158 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
1159 std::swap(N0, N1);
1160 // check for rotl, rotr
1161 if (N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SRL &&
1162 N0.getOperand(0) == N1.getOperand(0) &&
Evan Chengdfcfacb2006-08-31 07:41:12 +00001163 TLI.isTypeLegal(VT)) {
1164 bool HasROTL = TLI.isOperationLegal(ISD::ROTL, VT);
1165 bool HasROTR = TLI.isOperationLegal(ISD::ROTR, VT);
1166 if (HasROTL || HasROTR) {
1167 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
1168 // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2)
1169 if (N0.getOperand(1).getOpcode() == ISD::Constant &&
1170 N1.getOperand(1).getOpcode() == ISD::Constant) {
1171 uint64_t c1val = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1172 uint64_t c2val = cast<ConstantSDNode>(N1.getOperand(1))->getValue();
1173 if ((c1val + c2val) == OpSizeInBits)
1174 if (HasROTL)
Nate Begeman35ef9132006-01-11 21:21:00 +00001175 return DAG.getNode(ISD::ROTL, VT, N0.getOperand(0),
1176 N0.getOperand(1));
Evan Chengdfcfacb2006-08-31 07:41:12 +00001177 else
1178 return DAG.getNode(ISD::ROTR, VT, N0.getOperand(0),
1179 N1.getOperand(1));
1180
1181 }
1182 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
1183 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotr x, (sub 32, y))
1184 if (N1.getOperand(1).getOpcode() == ISD::SUB &&
1185 N0.getOperand(1) == N1.getOperand(1).getOperand(1))
1186 if (ConstantSDNode *SUBC =
1187 dyn_cast<ConstantSDNode>(N1.getOperand(1).getOperand(0)))
1188 if (SUBC->getValue() == OpSizeInBits)
1189 if (HasROTL)
1190 return DAG.getNode(ISD::ROTL, VT, N0.getOperand(0),
1191 N0.getOperand(1));
1192 else
1193 return DAG.getNode(ISD::ROTR, VT, N0.getOperand(0),
1194 N1.getOperand(1));
1195 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
1196 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotl x, (sub 32, y))
1197 if (N0.getOperand(1).getOpcode() == ISD::SUB &&
1198 N1.getOperand(1) == N0.getOperand(1).getOperand(1))
1199 if (ConstantSDNode *SUBC =
1200 dyn_cast<ConstantSDNode>(N0.getOperand(1).getOperand(0)))
1201 if (SUBC->getValue() == OpSizeInBits)
1202 if (HasROTR)
1203 return DAG.getNode(ISD::ROTR, VT, N0.getOperand(0),
1204 N1.getOperand(1));
1205 else
1206 return DAG.getNode(ISD::ROTL, VT, N0.getOperand(0),
1207 N0.getOperand(1));
1208 }
Nate Begeman35ef9132006-01-11 21:21:00 +00001209 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001210 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001211}
1212
Nate Begeman83e75ec2005-09-06 04:43:02 +00001213SDOperand DAGCombiner::visitXOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001214 SDOperand N0 = N->getOperand(0);
1215 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001216 SDOperand LHS, RHS, CC;
1217 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1218 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001219 MVT::ValueType VT = N0.getValueType();
1220
1221 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001222 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001223 return DAG.getNode(ISD::XOR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001224 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001225 if (N0C && !N1C)
1226 return DAG.getNode(ISD::XOR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001227 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001228 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001229 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001230 // reassociate xor
1231 SDOperand RXOR = ReassociateOps(ISD::XOR, N0, N1);
1232 if (RXOR.Val != 0)
1233 return RXOR;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001234 // fold !(x cc y) -> (x !cc y)
Nate Begeman646d7e22005-09-02 21:18:40 +00001235 if (N1C && N1C->getValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
1236 bool isInt = MVT::isInteger(LHS.getValueType());
1237 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
1238 isInt);
1239 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001240 return DAG.getSetCC(VT, LHS, RHS, NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00001241 if (N0.getOpcode() == ISD::SELECT_CC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001242 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00001243 assert(0 && "Unhandled SetCC Equivalent!");
1244 abort();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001245 }
Nate Begeman99801192005-09-07 23:25:52 +00001246 // fold !(x or y) -> (!x and !y) iff x or y are setcc
1247 if (N1C && N1C->getValue() == 1 &&
1248 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001249 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00001250 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
1251 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001252 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1253 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00001254 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00001255 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001256 }
1257 }
Nate Begeman99801192005-09-07 23:25:52 +00001258 // fold !(x or y) -> (!x and !y) iff x or y are constants
1259 if (N1C && N1C->isAllOnesValue() &&
1260 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001261 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00001262 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
1263 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001264 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1265 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00001266 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00001267 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001268 }
1269 }
Nate Begeman223df222005-09-08 20:18:10 +00001270 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
1271 if (N1C && N0.getOpcode() == ISD::XOR) {
1272 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
1273 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
1274 if (N00C)
1275 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
1276 DAG.getConstant(N1C->getValue()^N00C->getValue(), VT));
1277 if (N01C)
1278 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
1279 DAG.getConstant(N1C->getValue()^N01C->getValue(), VT));
1280 }
1281 // fold (xor x, x) -> 0
Chris Lattner4fbdd592006-03-28 19:11:05 +00001282 if (N0 == N1) {
1283 if (!MVT::isVector(VT)) {
1284 return DAG.getConstant(0, VT);
1285 } else if (!AfterLegalize || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) {
1286 // Produce a vector of zeros.
1287 SDOperand El = DAG.getConstant(0, MVT::getVectorBaseType(VT));
1288 std::vector<SDOperand> Ops(MVT::getVectorNumElements(VT), El);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001289 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4fbdd592006-03-28 19:11:05 +00001290 }
1291 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001292
1293 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
1294 if (N0.getOpcode() == N1.getOpcode()) {
1295 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1296 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001297 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001298
Chris Lattner3e104b12006-04-08 04:15:24 +00001299 // Simplify the expression using non-local knowledge.
1300 if (!MVT::isVector(VT) &&
1301 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001302 return SDOperand(N, 0);
Chris Lattner3e104b12006-04-08 04:15:24 +00001303
Nate Begeman83e75ec2005-09-06 04:43:02 +00001304 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001305}
1306
Nate Begeman83e75ec2005-09-06 04:43:02 +00001307SDOperand DAGCombiner::visitSHL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001308 SDOperand N0 = N->getOperand(0);
1309 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001310 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1311 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001312 MVT::ValueType VT = N0.getValueType();
1313 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1314
1315 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001316 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001317 return DAG.getNode(ISD::SHL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001318 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001319 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001320 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001321 // fold (shl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001322 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001323 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001324 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001325 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001326 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001327 // if (shl x, c) is known to be zero, return 0
Nate Begemanfb7217b2006-02-17 19:54:08 +00001328 if (TLI.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001329 return DAG.getConstant(0, VT);
Chris Lattner012f2412006-02-17 21:58:01 +00001330 if (SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001331 return SDOperand(N, 0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001332 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001333 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001334 N0.getOperand(1).getOpcode() == ISD::Constant) {
1335 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001336 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001337 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001338 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001339 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001340 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001341 }
1342 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
1343 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001344 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001345 N0.getOperand(1).getOpcode() == ISD::Constant) {
1346 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001347 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001348 SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
1349 DAG.getConstant(~0ULL << c1, VT));
1350 if (c2 > c1)
1351 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001352 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001353 else
Nate Begeman83e75ec2005-09-06 04:43:02 +00001354 return DAG.getNode(ISD::SRL, VT, Mask,
1355 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001356 }
1357 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001358 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +00001359 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001360 DAG.getConstant(~0ULL << N1C->getValue(), VT));
Chris Lattnercac70592006-03-05 19:53:55 +00001361 // fold (shl (add x, c1), c2) -> (add (shl x, c2), c1<<c2)
1362 if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() &&
1363 isa<ConstantSDNode>(N0.getOperand(1))) {
1364 return DAG.getNode(ISD::ADD, VT,
1365 DAG.getNode(ISD::SHL, VT, N0.getOperand(0), N1),
1366 DAG.getNode(ISD::SHL, VT, N0.getOperand(1), N1));
1367 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001368 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001369}
1370
Nate Begeman83e75ec2005-09-06 04:43:02 +00001371SDOperand DAGCombiner::visitSRA(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001372 SDOperand N0 = N->getOperand(0);
1373 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001374 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1375 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001376 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001377
1378 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001379 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001380 return DAG.getNode(ISD::SRA, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001381 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001382 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001383 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001384 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001385 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001386 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001387 // fold (sra x, c >= size(x)) -> undef
Nate Begemanfb7217b2006-02-17 19:54:08 +00001388 if (N1C && N1C->getValue() >= MVT::getSizeInBits(VT))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001389 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001390 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001391 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001392 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00001393 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
1394 // sext_inreg.
1395 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
1396 unsigned LowBits = MVT::getSizeInBits(VT) - (unsigned)N1C->getValue();
1397 MVT::ValueType EVT;
1398 switch (LowBits) {
1399 default: EVT = MVT::Other; break;
1400 case 1: EVT = MVT::i1; break;
1401 case 8: EVT = MVT::i8; break;
1402 case 16: EVT = MVT::i16; break;
1403 case 32: EVT = MVT::i32; break;
1404 }
1405 if (EVT > MVT::Other && TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, EVT))
1406 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
1407 DAG.getValueType(EVT));
1408 }
Chris Lattner71d9ebc2006-02-28 06:23:04 +00001409
1410 // fold (sra (sra x, c1), c2) -> (sra x, c1+c2)
1411 if (N1C && N0.getOpcode() == ISD::SRA) {
1412 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
1413 unsigned Sum = N1C->getValue() + C1->getValue();
1414 if (Sum >= MVT::getSizeInBits(VT)) Sum = MVT::getSizeInBits(VT)-1;
1415 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0),
1416 DAG.getConstant(Sum, N1C->getValueType(0)));
1417 }
1418 }
1419
Chris Lattnera8504462006-05-08 20:51:54 +00001420 // Simplify, based on bits shifted out of the LHS.
1421 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
1422 return SDOperand(N, 0);
1423
1424
Nate Begeman1d4d4142005-09-01 00:19:25 +00001425 // If the sign bit is known to be zero, switch this to a SRL.
Nate Begemanfb7217b2006-02-17 19:54:08 +00001426 if (TLI.MaskedValueIsZero(N0, MVT::getIntVTSignBit(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001427 return DAG.getNode(ISD::SRL, VT, N0, N1);
1428 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001429}
1430
Nate Begeman83e75ec2005-09-06 04:43:02 +00001431SDOperand DAGCombiner::visitSRL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001432 SDOperand N0 = N->getOperand(0);
1433 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001434 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1435 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001436 MVT::ValueType VT = N0.getValueType();
1437 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1438
1439 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001440 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001441 return DAG.getNode(ISD::SRL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001442 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001443 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001444 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001445 // fold (srl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001446 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001447 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001448 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001449 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001450 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001451 // if (srl x, c) is known to be zero, return 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001452 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001453 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001454 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001455 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001456 N0.getOperand(1).getOpcode() == ISD::Constant) {
1457 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001458 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001459 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001460 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001461 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001462 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001463 }
Chris Lattner350bec02006-04-02 06:11:11 +00001464
Chris Lattner06afe072006-05-05 22:53:17 +00001465 // fold (srl (anyextend x), c) -> (anyextend (srl x, c))
1466 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
1467 // Shifting in all undef bits?
1468 MVT::ValueType SmallVT = N0.getOperand(0).getValueType();
1469 if (N1C->getValue() >= MVT::getSizeInBits(SmallVT))
1470 return DAG.getNode(ISD::UNDEF, VT);
1471
1472 SDOperand SmallShift = DAG.getNode(ISD::SRL, SmallVT, N0.getOperand(0), N1);
1473 AddToWorkList(SmallShift.Val);
1474 return DAG.getNode(ISD::ANY_EXTEND, VT, SmallShift);
1475 }
1476
Chris Lattner350bec02006-04-02 06:11:11 +00001477 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
1478 if (N1C && N0.getOpcode() == ISD::CTLZ &&
1479 N1C->getValue() == Log2_32(MVT::getSizeInBits(VT))) {
1480 uint64_t KnownZero, KnownOne, Mask = MVT::getIntVTBitMask(VT);
1481 TLI.ComputeMaskedBits(N0.getOperand(0), Mask, KnownZero, KnownOne);
1482
1483 // If any of the input bits are KnownOne, then the input couldn't be all
1484 // zeros, thus the result of the srl will always be zero.
1485 if (KnownOne) return DAG.getConstant(0, VT);
1486
1487 // If all of the bits input the to ctlz node are known to be zero, then
1488 // the result of the ctlz is "32" and the result of the shift is one.
1489 uint64_t UnknownBits = ~KnownZero & Mask;
1490 if (UnknownBits == 0) return DAG.getConstant(1, VT);
1491
1492 // Otherwise, check to see if there is exactly one bit input to the ctlz.
1493 if ((UnknownBits & (UnknownBits-1)) == 0) {
1494 // Okay, we know that only that the single bit specified by UnknownBits
1495 // could be set on input to the CTLZ node. If this bit is set, the SRL
1496 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
1497 // to an SRL,XOR pair, which is likely to simplify more.
1498 unsigned ShAmt = CountTrailingZeros_64(UnknownBits);
1499 SDOperand Op = N0.getOperand(0);
1500 if (ShAmt) {
1501 Op = DAG.getNode(ISD::SRL, VT, Op,
1502 DAG.getConstant(ShAmt, TLI.getShiftAmountTy()));
1503 AddToWorkList(Op.Val);
1504 }
1505 return DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(1, VT));
1506 }
1507 }
1508
Nate Begeman83e75ec2005-09-06 04:43:02 +00001509 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001510}
1511
Nate Begeman83e75ec2005-09-06 04:43:02 +00001512SDOperand DAGCombiner::visitCTLZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001513 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00001514 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001515
1516 // fold (ctlz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00001517 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00001518 return DAG.getNode(ISD::CTLZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001519 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001520}
1521
Nate Begeman83e75ec2005-09-06 04:43:02 +00001522SDOperand DAGCombiner::visitCTTZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001523 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00001524 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001525
1526 // fold (cttz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00001527 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00001528 return DAG.getNode(ISD::CTTZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001529 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001530}
1531
Nate Begeman83e75ec2005-09-06 04:43:02 +00001532SDOperand DAGCombiner::visitCTPOP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001533 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00001534 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001535
1536 // fold (ctpop c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00001537 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00001538 return DAG.getNode(ISD::CTPOP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001539 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001540}
1541
Nate Begeman452d7be2005-09-16 00:54:12 +00001542SDOperand DAGCombiner::visitSELECT(SDNode *N) {
1543 SDOperand N0 = N->getOperand(0);
1544 SDOperand N1 = N->getOperand(1);
1545 SDOperand N2 = N->getOperand(2);
1546 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1547 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1548 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1549 MVT::ValueType VT = N->getValueType(0);
Nate Begeman44728a72005-09-19 22:34:01 +00001550
Nate Begeman452d7be2005-09-16 00:54:12 +00001551 // fold select C, X, X -> X
1552 if (N1 == N2)
1553 return N1;
1554 // fold select true, X, Y -> X
1555 if (N0C && !N0C->isNullValue())
1556 return N1;
1557 // fold select false, X, Y -> Y
1558 if (N0C && N0C->isNullValue())
1559 return N2;
1560 // fold select C, 1, X -> C | X
Nate Begeman44728a72005-09-19 22:34:01 +00001561 if (MVT::i1 == VT && N1C && N1C->getValue() == 1)
Nate Begeman452d7be2005-09-16 00:54:12 +00001562 return DAG.getNode(ISD::OR, VT, N0, N2);
1563 // fold select C, 0, X -> ~C & X
1564 // FIXME: this should check for C type == X type, not i1?
1565 if (MVT::i1 == VT && N1C && N1C->isNullValue()) {
1566 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00001567 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00001568 return DAG.getNode(ISD::AND, VT, XORNode, N2);
1569 }
1570 // fold select C, X, 1 -> ~C | X
Nate Begeman44728a72005-09-19 22:34:01 +00001571 if (MVT::i1 == VT && N2C && N2C->getValue() == 1) {
Nate Begeman452d7be2005-09-16 00:54:12 +00001572 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00001573 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00001574 return DAG.getNode(ISD::OR, VT, XORNode, N1);
1575 }
1576 // fold select C, X, 0 -> C & X
1577 // FIXME: this should check for C type == X type, not i1?
1578 if (MVT::i1 == VT && N2C && N2C->isNullValue())
1579 return DAG.getNode(ISD::AND, VT, N0, N1);
1580 // fold X ? X : Y --> X ? 1 : Y --> X | Y
1581 if (MVT::i1 == VT && N0 == N1)
1582 return DAG.getNode(ISD::OR, VT, N0, N2);
1583 // fold X ? Y : X --> X ? Y : 0 --> X & Y
1584 if (MVT::i1 == VT && N0 == N2)
1585 return DAG.getNode(ISD::AND, VT, N0, N1);
Chris Lattner729c6d12006-05-27 00:43:02 +00001586
Chris Lattner40c62d52005-10-18 06:04:22 +00001587 // If we can fold this based on the true/false value, do so.
1588 if (SimplifySelectOps(N, N1, N2))
Chris Lattner729c6d12006-05-27 00:43:02 +00001589 return SDOperand(N, 0); // Don't revisit N.
1590
Nate Begeman44728a72005-09-19 22:34:01 +00001591 // fold selects based on a setcc into other things, such as min/max/abs
1592 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman750ac1b2006-02-01 07:19:44 +00001593 // FIXME:
1594 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
1595 // having to say they don't support SELECT_CC on every type the DAG knows
1596 // about, since there is no way to mark an opcode illegal at all value types
1597 if (TLI.isOperationLegal(ISD::SELECT_CC, MVT::Other))
1598 return DAG.getNode(ISD::SELECT_CC, VT, N0.getOperand(0), N0.getOperand(1),
1599 N1, N2, N0.getOperand(2));
1600 else
1601 return SimplifySelect(N0, N1, N2);
Nate Begeman452d7be2005-09-16 00:54:12 +00001602 return SDOperand();
1603}
1604
1605SDOperand DAGCombiner::visitSELECT_CC(SDNode *N) {
Nate Begeman44728a72005-09-19 22:34:01 +00001606 SDOperand N0 = N->getOperand(0);
1607 SDOperand N1 = N->getOperand(1);
1608 SDOperand N2 = N->getOperand(2);
1609 SDOperand N3 = N->getOperand(3);
1610 SDOperand N4 = N->getOperand(4);
1611 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1612 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1613 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1614 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
1615
1616 // Determine if the condition we're dealing with is constant
Nate Begemane17daeb2005-10-05 21:43:42 +00001617 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
Chris Lattner5eed34d2006-05-12 17:57:54 +00001618 //ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
Chris Lattner91559022005-10-05 04:45:43 +00001619
Nate Begeman44728a72005-09-19 22:34:01 +00001620 // fold select_cc lhs, rhs, x, x, cc -> x
1621 if (N2 == N3)
1622 return N2;
Chris Lattner40c62d52005-10-18 06:04:22 +00001623
1624 // If we can fold this based on the true/false value, do so.
1625 if (SimplifySelectOps(N, N2, N3))
Chris Lattner729c6d12006-05-27 00:43:02 +00001626 return SDOperand(N, 0); // Don't revisit N.
Chris Lattner40c62d52005-10-18 06:04:22 +00001627
Nate Begeman44728a72005-09-19 22:34:01 +00001628 // fold select_cc into other things, such as min/max/abs
1629 return SimplifySelectCC(N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00001630}
1631
1632SDOperand DAGCombiner::visitSETCC(SDNode *N) {
1633 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
1634 cast<CondCodeSDNode>(N->getOperand(2))->get());
1635}
1636
Nate Begeman83e75ec2005-09-06 04:43:02 +00001637SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001638 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001639 MVT::ValueType VT = N->getValueType(0);
1640
Nate Begeman1d4d4142005-09-01 00:19:25 +00001641 // fold (sext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00001642 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00001643 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0);
Chris Lattner310b5782006-05-06 23:06:26 +00001644
Nate Begeman1d4d4142005-09-01 00:19:25 +00001645 // fold (sext (sext x)) -> (sext x)
Chris Lattner310b5782006-05-06 23:06:26 +00001646 // fold (sext (aext x)) -> (sext x)
1647 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001648 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
Chris Lattner310b5782006-05-06 23:06:26 +00001649
Chris Lattnerb14ab8a2005-12-07 07:11:03 +00001650 // fold (sext (truncate x)) -> (sextinreg x) iff x size == sext size.
Chris Lattnercc2210b2005-12-07 18:02:05 +00001651 if (N0.getOpcode() == ISD::TRUNCATE && N0.getOperand(0).getValueType() == VT&&
1652 (!AfterLegalize ||
1653 TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, N0.getValueType())))
Chris Lattnerb14ab8a2005-12-07 07:11:03 +00001654 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
1655 DAG.getValueType(N0.getValueType()));
Chris Lattner310b5782006-05-06 23:06:26 +00001656
Evan Cheng110dec22005-12-14 02:19:23 +00001657 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Chris Lattnerd0f6d182005-12-15 19:02:38 +00001658 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() &&
1659 (!AfterLegalize||TLI.isOperationLegal(ISD::SEXTLOAD, N0.getValueType()))){
Nate Begeman3df4d522005-10-12 20:40:40 +00001660 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1661 N0.getOperand(1), N0.getOperand(2),
1662 N0.getValueType());
Chris Lattnerd4771842005-12-14 19:25:30 +00001663 CombineTo(N, ExtLoad);
Chris Lattnerf9884052005-10-13 21:52:31 +00001664 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1665 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001666 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begeman3df4d522005-10-12 20:40:40 +00001667 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001668
1669 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
1670 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
1671 if ((N0.getOpcode() == ISD::SEXTLOAD || N0.getOpcode() == ISD::EXTLOAD) &&
1672 N0.hasOneUse()) {
Nate Begeman5c742682006-05-08 01:35:01 +00001673 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
1674 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1675 N0.getOperand(1), N0.getOperand(2), EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00001676 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001677 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1678 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001679 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001680 }
1681
Nate Begeman83e75ec2005-09-06 04:43:02 +00001682 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001683}
1684
Nate Begeman83e75ec2005-09-06 04:43:02 +00001685SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001686 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001687 MVT::ValueType VT = N->getValueType(0);
1688
Nate Begeman1d4d4142005-09-01 00:19:25 +00001689 // fold (zext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00001690 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00001691 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001692 // fold (zext (zext x)) -> (zext x)
Chris Lattner310b5782006-05-06 23:06:26 +00001693 // fold (zext (aext x)) -> (zext x)
1694 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001695 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
Evan Cheng110dec22005-12-14 02:19:23 +00001696 // fold (zext (truncate x)) -> (zextinreg x) iff x size == zext size.
1697 if (N0.getOpcode() == ISD::TRUNCATE && N0.getOperand(0).getValueType() == VT&&
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001698 (!AfterLegalize || TLI.isOperationLegal(ISD::AND, N0.getValueType())))
Chris Lattner00cb95c2005-12-14 07:58:38 +00001699 return DAG.getZeroExtendInReg(N0.getOperand(0), N0.getValueType());
Evan Cheng110dec22005-12-14 02:19:23 +00001700 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Chris Lattnerd0f6d182005-12-15 19:02:38 +00001701 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() &&
1702 (!AfterLegalize||TLI.isOperationLegal(ISD::ZEXTLOAD, N0.getValueType()))){
Evan Cheng110dec22005-12-14 02:19:23 +00001703 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1704 N0.getOperand(1), N0.getOperand(2),
1705 N0.getValueType());
Chris Lattnerd4771842005-12-14 19:25:30 +00001706 CombineTo(N, ExtLoad);
Evan Cheng110dec22005-12-14 02:19:23 +00001707 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1708 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001709 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng110dec22005-12-14 02:19:23 +00001710 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001711
1712 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
1713 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
1714 if ((N0.getOpcode() == ISD::ZEXTLOAD || N0.getOpcode() == ISD::EXTLOAD) &&
1715 N0.hasOneUse()) {
Nate Begeman5c742682006-05-08 01:35:01 +00001716 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
1717 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1718 N0.getOperand(1), N0.getOperand(2), EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00001719 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001720 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1721 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001722 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001723 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001724 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001725}
1726
Chris Lattner5ffc0662006-05-05 05:58:59 +00001727SDOperand DAGCombiner::visitANY_EXTEND(SDNode *N) {
1728 SDOperand N0 = N->getOperand(0);
Chris Lattner5ffc0662006-05-05 05:58:59 +00001729 MVT::ValueType VT = N->getValueType(0);
1730
1731 // fold (aext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00001732 if (isa<ConstantSDNode>(N0))
Chris Lattner5ffc0662006-05-05 05:58:59 +00001733 return DAG.getNode(ISD::ANY_EXTEND, VT, N0);
1734 // fold (aext (aext x)) -> (aext x)
1735 // fold (aext (zext x)) -> (zext x)
1736 // fold (aext (sext x)) -> (sext x)
1737 if (N0.getOpcode() == ISD::ANY_EXTEND ||
1738 N0.getOpcode() == ISD::ZERO_EXTEND ||
1739 N0.getOpcode() == ISD::SIGN_EXTEND)
1740 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
1741
1742 // fold (aext (truncate x)) -> x iff x size == zext size.
1743 if (N0.getOpcode() == ISD::TRUNCATE && N0.getOperand(0).getValueType() == VT)
1744 return N0.getOperand(0);
1745 // fold (aext (load x)) -> (aext (truncate (extload x)))
1746 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() &&
1747 (!AfterLegalize||TLI.isOperationLegal(ISD::EXTLOAD, N0.getValueType()))) {
1748 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, N0.getOperand(0),
1749 N0.getOperand(1), N0.getOperand(2),
1750 N0.getValueType());
1751 CombineTo(N, ExtLoad);
1752 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1753 ExtLoad.getValue(1));
1754 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1755 }
1756
1757 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
1758 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
1759 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
1760 if ((N0.getOpcode() == ISD::ZEXTLOAD || N0.getOpcode() == ISD::EXTLOAD ||
1761 N0.getOpcode() == ISD::SEXTLOAD) &&
1762 N0.hasOneUse()) {
Nate Begeman5c742682006-05-08 01:35:01 +00001763 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
1764 SDOperand ExtLoad = DAG.getExtLoad(N0.getOpcode(), VT, N0.getOperand(0),
1765 N0.getOperand(1), N0.getOperand(2), EVT);
Chris Lattner5ffc0662006-05-05 05:58:59 +00001766 CombineTo(N, ExtLoad);
1767 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1768 ExtLoad.getValue(1));
1769 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1770 }
1771 return SDOperand();
1772}
1773
1774
Nate Begeman83e75ec2005-09-06 04:43:02 +00001775SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001776 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001777 SDOperand N1 = N->getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001778 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001779 MVT::ValueType EVT = cast<VTSDNode>(N1)->getVT();
Nate Begeman07ed4172005-10-10 21:26:48 +00001780 unsigned EVTBits = MVT::getSizeInBits(EVT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001781
Nate Begeman1d4d4142005-09-01 00:19:25 +00001782 // fold (sext_in_reg c1) -> c1
Chris Lattnereaeda562006-05-08 20:59:41 +00001783 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
Chris Lattner310b5782006-05-06 23:06:26 +00001784 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0, N1);
Chris Lattneree4ea922006-05-06 09:30:03 +00001785
Chris Lattner541a24f2006-05-06 22:43:44 +00001786 // If the input is already sign extended, just drop the extension.
Chris Lattneree4ea922006-05-06 09:30:03 +00001787 if (TLI.ComputeNumSignBits(N0) >= MVT::getSizeInBits(VT)-EVTBits+1)
1788 return N0;
1789
Nate Begeman646d7e22005-09-02 21:18:40 +00001790 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
1791 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1792 EVT < cast<VTSDNode>(N0.getOperand(1))->getVT()) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001793 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001794 }
Chris Lattner4b37e872006-05-08 21:18:59 +00001795
Nate Begeman07ed4172005-10-10 21:26:48 +00001796 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is zero
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001797 if (TLI.MaskedValueIsZero(N0, 1ULL << (EVTBits-1)))
Nate Begemande996292006-02-03 22:24:05 +00001798 return DAG.getZeroExtendInReg(N0, EVT);
Chris Lattner4b37e872006-05-08 21:18:59 +00001799
1800 // fold (sext_in_reg (srl X, 24), i8) -> sra X, 24
1801 // fold (sext_in_reg (srl X, 23), i8) -> sra X, 23 iff possible.
1802 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
1803 if (N0.getOpcode() == ISD::SRL) {
1804 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
1805 if (ShAmt->getValue()+EVTBits <= MVT::getSizeInBits(VT)) {
1806 // We can turn this into an SRA iff the input to the SRL is already sign
1807 // extended enough.
1808 unsigned InSignBits = TLI.ComputeNumSignBits(N0.getOperand(0));
1809 if (MVT::getSizeInBits(VT)-(ShAmt->getValue()+EVTBits) < InSignBits)
1810 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0), N0.getOperand(1));
1811 }
1812 }
1813
Nate Begemanded49632005-10-13 03:11:28 +00001814 // fold (sext_inreg (extload x)) -> (sextload x)
1815 if (N0.getOpcode() == ISD::EXTLOAD &&
1816 EVT == cast<VTSDNode>(N0.getOperand(3))->getVT() &&
Nate Begemanbfd65a02005-10-13 18:34:58 +00001817 (!AfterLegalize || TLI.isOperationLegal(ISD::SEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001818 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1819 N0.getOperand(1), N0.getOperand(2),
1820 EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00001821 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00001822 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001823 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001824 }
1825 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Chris Lattner40c62d52005-10-18 06:04:22 +00001826 if (N0.getOpcode() == ISD::ZEXTLOAD && N0.hasOneUse() &&
Nate Begemanded49632005-10-13 03:11:28 +00001827 EVT == cast<VTSDNode>(N0.getOperand(3))->getVT() &&
Nate Begemanbfd65a02005-10-13 18:34:58 +00001828 (!AfterLegalize || TLI.isOperationLegal(ISD::SEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001829 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1830 N0.getOperand(1), N0.getOperand(2),
1831 EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00001832 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00001833 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001834 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001835 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001836 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001837}
1838
Nate Begeman83e75ec2005-09-06 04:43:02 +00001839SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001840 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001841 MVT::ValueType VT = N->getValueType(0);
1842
1843 // noop truncate
1844 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001845 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001846 // fold (truncate c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00001847 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00001848 return DAG.getNode(ISD::TRUNCATE, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001849 // fold (truncate (truncate x)) -> (truncate x)
1850 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001851 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001852 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
Chris Lattnerb72773b2006-05-05 22:56:26 +00001853 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND||
1854 N0.getOpcode() == ISD::ANY_EXTEND) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001855 if (N0.getValueType() < VT)
1856 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00001857 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001858 else if (N0.getValueType() > VT)
1859 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00001860 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001861 else
1862 // if the source and dest are the same type, we can drop both the extend
1863 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00001864 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001865 }
Nate Begeman3df4d522005-10-12 20:40:40 +00001866 // fold (truncate (load x)) -> (smaller load x)
Chris Lattner40c62d52005-10-18 06:04:22 +00001867 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse()) {
Nate Begeman3df4d522005-10-12 20:40:40 +00001868 assert(MVT::getSizeInBits(N0.getValueType()) > MVT::getSizeInBits(VT) &&
1869 "Cannot truncate to larger type!");
1870 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
Nate Begeman765784a2005-10-12 23:18:53 +00001871 // For big endian targets, we need to add an offset to the pointer to load
1872 // the correct bytes. For little endian systems, we merely need to read
1873 // fewer bytes from the same pointer.
Nate Begeman3df4d522005-10-12 20:40:40 +00001874 uint64_t PtrOff =
1875 (MVT::getSizeInBits(N0.getValueType()) - MVT::getSizeInBits(VT)) / 8;
Nate Begeman765784a2005-10-12 23:18:53 +00001876 SDOperand NewPtr = TLI.isLittleEndian() ? N0.getOperand(1) :
1877 DAG.getNode(ISD::ADD, PtrType, N0.getOperand(1),
1878 DAG.getConstant(PtrOff, PtrType));
Chris Lattner5750df92006-03-01 04:03:14 +00001879 AddToWorkList(NewPtr.Val);
Nate Begeman3df4d522005-10-12 20:40:40 +00001880 SDOperand Load = DAG.getLoad(VT, N0.getOperand(0), NewPtr,N0.getOperand(2));
Chris Lattner5750df92006-03-01 04:03:14 +00001881 AddToWorkList(N);
Chris Lattner24edbb72005-10-13 22:10:05 +00001882 CombineTo(N0.Val, Load, Load.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001883 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begeman3df4d522005-10-12 20:40:40 +00001884 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001885 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001886}
1887
Chris Lattner94683772005-12-23 05:30:37 +00001888SDOperand DAGCombiner::visitBIT_CONVERT(SDNode *N) {
1889 SDOperand N0 = N->getOperand(0);
1890 MVT::ValueType VT = N->getValueType(0);
1891
1892 // If the input is a constant, let getNode() fold it.
1893 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
1894 SDOperand Res = DAG.getNode(ISD::BIT_CONVERT, VT, N0);
1895 if (Res.Val != N) return Res;
1896 }
1897
Chris Lattnerc8547d82005-12-23 05:37:50 +00001898 if (N0.getOpcode() == ISD::BIT_CONVERT) // conv(conv(x,t1),t2) -> conv(x,t2)
1899 return DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
Chris Lattner6258fb22006-04-02 02:53:43 +00001900
Chris Lattner57104102005-12-23 05:44:41 +00001901 // fold (conv (load x)) -> (load (conv*)x)
Chris Lattnerbf40c4b2006-01-15 18:58:59 +00001902 // FIXME: These xforms need to know that the resultant load doesn't need a
1903 // higher alignment than the original!
1904 if (0 && N0.getOpcode() == ISD::LOAD && N0.hasOneUse()) {
Chris Lattner57104102005-12-23 05:44:41 +00001905 SDOperand Load = DAG.getLoad(VT, N0.getOperand(0), N0.getOperand(1),
1906 N0.getOperand(2));
Chris Lattner5750df92006-03-01 04:03:14 +00001907 AddToWorkList(N);
Chris Lattner57104102005-12-23 05:44:41 +00001908 CombineTo(N0.Val, DAG.getNode(ISD::BIT_CONVERT, N0.getValueType(), Load),
1909 Load.getValue(1));
1910 return Load;
1911 }
1912
Chris Lattner94683772005-12-23 05:30:37 +00001913 return SDOperand();
1914}
1915
Chris Lattner6258fb22006-04-02 02:53:43 +00001916SDOperand DAGCombiner::visitVBIT_CONVERT(SDNode *N) {
1917 SDOperand N0 = N->getOperand(0);
1918 MVT::ValueType VT = N->getValueType(0);
1919
1920 // If the input is a VBUILD_VECTOR with all constant elements, fold this now.
1921 // First check to see if this is all constant.
1922 if (N0.getOpcode() == ISD::VBUILD_VECTOR && N0.Val->hasOneUse() &&
1923 VT == MVT::Vector) {
1924 bool isSimple = true;
1925 for (unsigned i = 0, e = N0.getNumOperands()-2; i != e; ++i)
1926 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
1927 N0.getOperand(i).getOpcode() != ISD::Constant &&
1928 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
1929 isSimple = false;
1930 break;
1931 }
1932
Chris Lattner97c20732006-04-03 17:29:28 +00001933 MVT::ValueType DestEltVT = cast<VTSDNode>(N->getOperand(2))->getVT();
1934 if (isSimple && !MVT::isVector(DestEltVT)) {
Chris Lattner6258fb22006-04-02 02:53:43 +00001935 return ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(N0.Val, DestEltVT);
1936 }
1937 }
1938
1939 return SDOperand();
1940}
1941
1942/// ConstantFoldVBIT_CONVERTofVBUILD_VECTOR - We know that BV is a vbuild_vector
1943/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
1944/// destination element value type.
1945SDOperand DAGCombiner::
1946ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(SDNode *BV, MVT::ValueType DstEltVT) {
1947 MVT::ValueType SrcEltVT = BV->getOperand(0).getValueType();
1948
1949 // If this is already the right type, we're done.
1950 if (SrcEltVT == DstEltVT) return SDOperand(BV, 0);
1951
1952 unsigned SrcBitSize = MVT::getSizeInBits(SrcEltVT);
1953 unsigned DstBitSize = MVT::getSizeInBits(DstEltVT);
1954
1955 // If this is a conversion of N elements of one type to N elements of another
1956 // type, convert each element. This handles FP<->INT cases.
1957 if (SrcBitSize == DstBitSize) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001958 SmallVector<SDOperand, 8> Ops;
Chris Lattner3e104b12006-04-08 04:15:24 +00001959 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00001960 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, DstEltVT, BV->getOperand(i)));
Chris Lattner3e104b12006-04-08 04:15:24 +00001961 AddToWorkList(Ops.back().Val);
1962 }
Chris Lattner6258fb22006-04-02 02:53:43 +00001963 Ops.push_back(*(BV->op_end()-2)); // Add num elements.
1964 Ops.push_back(DAG.getValueType(DstEltVT));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001965 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00001966 }
1967
1968 // Otherwise, we're growing or shrinking the elements. To avoid having to
1969 // handle annoying details of growing/shrinking FP values, we convert them to
1970 // int first.
1971 if (MVT::isFloatingPoint(SrcEltVT)) {
1972 // Convert the input float vector to a int vector where the elements are the
1973 // same sizes.
1974 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
1975 MVT::ValueType IntVT = SrcEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
1976 BV = ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(BV, IntVT).Val;
1977 SrcEltVT = IntVT;
1978 }
1979
1980 // Now we know the input is an integer vector. If the output is a FP type,
1981 // convert to integer first, then to FP of the right size.
1982 if (MVT::isFloatingPoint(DstEltVT)) {
1983 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
1984 MVT::ValueType TmpVT = DstEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
1985 SDNode *Tmp = ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(BV, TmpVT).Val;
1986
1987 // Next, convert to FP elements of the same size.
1988 return ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(Tmp, DstEltVT);
1989 }
1990
1991 // Okay, we know the src/dst types are both integers of differing types.
1992 // Handling growing first.
1993 assert(MVT::isInteger(SrcEltVT) && MVT::isInteger(DstEltVT));
1994 if (SrcBitSize < DstBitSize) {
1995 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
1996
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001997 SmallVector<SDOperand, 8> Ops;
Chris Lattner6258fb22006-04-02 02:53:43 +00001998 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e;
1999 i += NumInputsPerOutput) {
2000 bool isLE = TLI.isLittleEndian();
2001 uint64_t NewBits = 0;
2002 bool EltIsUndef = true;
2003 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
2004 // Shift the previously computed bits over.
2005 NewBits <<= SrcBitSize;
2006 SDOperand Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
2007 if (Op.getOpcode() == ISD::UNDEF) continue;
2008 EltIsUndef = false;
2009
2010 NewBits |= cast<ConstantSDNode>(Op)->getValue();
2011 }
2012
2013 if (EltIsUndef)
2014 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
2015 else
2016 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
2017 }
2018
2019 Ops.push_back(DAG.getConstant(Ops.size(), MVT::i32)); // Add num elements.
2020 Ops.push_back(DAG.getValueType(DstEltVT)); // Add element size.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002021 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00002022 }
2023
2024 // Finally, this must be the case where we are shrinking elements: each input
2025 // turns into multiple outputs.
2026 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002027 SmallVector<SDOperand, 8> Ops;
Chris Lattner6258fb22006-04-02 02:53:43 +00002028 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e; ++i) {
2029 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
2030 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
2031 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
2032 continue;
2033 }
2034 uint64_t OpVal = cast<ConstantSDNode>(BV->getOperand(i))->getValue();
2035
2036 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
2037 unsigned ThisVal = OpVal & ((1ULL << DstBitSize)-1);
2038 OpVal >>= DstBitSize;
2039 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
2040 }
2041
2042 // For big endian targets, swap the order of the pieces of each element.
2043 if (!TLI.isLittleEndian())
2044 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
2045 }
2046 Ops.push_back(DAG.getConstant(Ops.size(), MVT::i32)); // Add num elements.
2047 Ops.push_back(DAG.getValueType(DstEltVT)); // Add element size.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002048 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00002049}
2050
2051
2052
Chris Lattner01b3d732005-09-28 22:28:18 +00002053SDOperand DAGCombiner::visitFADD(SDNode *N) {
2054 SDOperand N0 = N->getOperand(0);
2055 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00002056 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2057 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002058 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00002059
2060 // fold (fadd c1, c2) -> c1+c2
2061 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002062 return DAG.getNode(ISD::FADD, VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00002063 // canonicalize constant to RHS
2064 if (N0CFP && !N1CFP)
2065 return DAG.getNode(ISD::FADD, VT, N1, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00002066 // fold (A + (-B)) -> A-B
2067 if (N1.getOpcode() == ISD::FNEG)
2068 return DAG.getNode(ISD::FSUB, VT, N0, N1.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00002069 // fold ((-A) + B) -> B-A
2070 if (N0.getOpcode() == ISD::FNEG)
2071 return DAG.getNode(ISD::FSUB, VT, N1, N0.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00002072 return SDOperand();
2073}
2074
2075SDOperand DAGCombiner::visitFSUB(SDNode *N) {
2076 SDOperand N0 = N->getOperand(0);
2077 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00002078 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2079 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002080 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00002081
2082 // fold (fsub c1, c2) -> c1-c2
2083 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002084 return DAG.getNode(ISD::FSUB, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002085 // fold (A-(-B)) -> A+B
2086 if (N1.getOpcode() == ISD::FNEG)
Nate Begemana148d982006-01-18 22:35:16 +00002087 return DAG.getNode(ISD::FADD, VT, N0, N1.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00002088 return SDOperand();
2089}
2090
2091SDOperand DAGCombiner::visitFMUL(SDNode *N) {
2092 SDOperand N0 = N->getOperand(0);
2093 SDOperand N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00002094 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2095 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002096 MVT::ValueType VT = N->getValueType(0);
2097
Nate Begeman11af4ea2005-10-17 20:40:11 +00002098 // fold (fmul c1, c2) -> c1*c2
2099 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002100 return DAG.getNode(ISD::FMUL, VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00002101 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00002102 if (N0CFP && !N1CFP)
2103 return DAG.getNode(ISD::FMUL, VT, N1, N0);
Nate Begeman11af4ea2005-10-17 20:40:11 +00002104 // fold (fmul X, 2.0) -> (fadd X, X)
2105 if (N1CFP && N1CFP->isExactlyValue(+2.0))
2106 return DAG.getNode(ISD::FADD, VT, N0, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00002107 return SDOperand();
2108}
2109
2110SDOperand DAGCombiner::visitFDIV(SDNode *N) {
2111 SDOperand N0 = N->getOperand(0);
2112 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00002113 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2114 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002115 MVT::ValueType VT = N->getValueType(0);
2116
Nate Begemana148d982006-01-18 22:35:16 +00002117 // fold (fdiv c1, c2) -> c1/c2
2118 if (N0CFP && N1CFP)
2119 return DAG.getNode(ISD::FDIV, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002120 return SDOperand();
2121}
2122
2123SDOperand DAGCombiner::visitFREM(SDNode *N) {
2124 SDOperand N0 = N->getOperand(0);
2125 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00002126 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2127 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002128 MVT::ValueType VT = N->getValueType(0);
2129
Nate Begemana148d982006-01-18 22:35:16 +00002130 // fold (frem c1, c2) -> fmod(c1,c2)
2131 if (N0CFP && N1CFP)
2132 return DAG.getNode(ISD::FREM, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002133 return SDOperand();
2134}
2135
Chris Lattner12d83032006-03-05 05:30:57 +00002136SDOperand DAGCombiner::visitFCOPYSIGN(SDNode *N) {
2137 SDOperand N0 = N->getOperand(0);
2138 SDOperand N1 = N->getOperand(1);
2139 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2140 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2141 MVT::ValueType VT = N->getValueType(0);
2142
2143 if (N0CFP && N1CFP) // Constant fold
2144 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1);
2145
2146 if (N1CFP) {
2147 // copysign(x, c1) -> fabs(x) iff ispos(c1)
2148 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
2149 union {
2150 double d;
2151 int64_t i;
2152 } u;
2153 u.d = N1CFP->getValue();
2154 if (u.i >= 0)
2155 return DAG.getNode(ISD::FABS, VT, N0);
2156 else
2157 return DAG.getNode(ISD::FNEG, VT, DAG.getNode(ISD::FABS, VT, N0));
2158 }
2159
2160 // copysign(fabs(x), y) -> copysign(x, y)
2161 // copysign(fneg(x), y) -> copysign(x, y)
2162 // copysign(copysign(x,z), y) -> copysign(x, y)
2163 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
2164 N0.getOpcode() == ISD::FCOPYSIGN)
2165 return DAG.getNode(ISD::FCOPYSIGN, VT, N0.getOperand(0), N1);
2166
2167 // copysign(x, abs(y)) -> abs(x)
2168 if (N1.getOpcode() == ISD::FABS)
2169 return DAG.getNode(ISD::FABS, VT, N0);
2170
2171 // copysign(x, copysign(y,z)) -> copysign(x, z)
2172 if (N1.getOpcode() == ISD::FCOPYSIGN)
2173 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(1));
2174
2175 // copysign(x, fp_extend(y)) -> copysign(x, y)
2176 // copysign(x, fp_round(y)) -> copysign(x, y)
2177 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
2178 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(0));
2179
2180 return SDOperand();
2181}
2182
2183
Chris Lattner01b3d732005-09-28 22:28:18 +00002184
Nate Begeman83e75ec2005-09-06 04:43:02 +00002185SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002186 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002187 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00002188 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002189
2190 // fold (sint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002191 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00002192 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002193 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002194}
2195
Nate Begeman83e75ec2005-09-06 04:43:02 +00002196SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002197 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002198 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00002199 MVT::ValueType VT = N->getValueType(0);
2200
Nate Begeman1d4d4142005-09-01 00:19:25 +00002201 // fold (uint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002202 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00002203 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002204 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002205}
2206
Nate Begeman83e75ec2005-09-06 04:43:02 +00002207SDOperand DAGCombiner::visitFP_TO_SINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002208 SDOperand N0 = N->getOperand(0);
2209 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2210 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002211
2212 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00002213 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002214 return DAG.getNode(ISD::FP_TO_SINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002215 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002216}
2217
Nate Begeman83e75ec2005-09-06 04:43:02 +00002218SDOperand DAGCombiner::visitFP_TO_UINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002219 SDOperand N0 = N->getOperand(0);
2220 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2221 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002222
2223 // fold (fp_to_uint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00002224 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002225 return DAG.getNode(ISD::FP_TO_UINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002226 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002227}
2228
Nate Begeman83e75ec2005-09-06 04:43:02 +00002229SDOperand DAGCombiner::visitFP_ROUND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002230 SDOperand N0 = N->getOperand(0);
2231 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2232 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002233
2234 // fold (fp_round c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002235 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002236 return DAG.getNode(ISD::FP_ROUND, VT, N0);
Chris Lattner79dbea52006-03-13 06:26:26 +00002237
2238 // fold (fp_round (fp_extend x)) -> x
2239 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
2240 return N0.getOperand(0);
2241
2242 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
2243 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.Val->hasOneUse()) {
2244 SDOperand Tmp = DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0));
2245 AddToWorkList(Tmp.Val);
2246 return DAG.getNode(ISD::FCOPYSIGN, VT, Tmp, N0.getOperand(1));
2247 }
2248
Nate Begeman83e75ec2005-09-06 04:43:02 +00002249 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002250}
2251
Nate Begeman83e75ec2005-09-06 04:43:02 +00002252SDOperand DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002253 SDOperand N0 = N->getOperand(0);
2254 MVT::ValueType VT = N->getValueType(0);
2255 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00002256 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002257
Nate Begeman1d4d4142005-09-01 00:19:25 +00002258 // fold (fp_round_inreg c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002259 if (N0CFP) {
2260 SDOperand Round = DAG.getConstantFP(N0CFP->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002261 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002262 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00002263 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002264}
2265
Nate Begeman83e75ec2005-09-06 04:43:02 +00002266SDOperand DAGCombiner::visitFP_EXTEND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002267 SDOperand N0 = N->getOperand(0);
2268 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2269 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002270
2271 // fold (fp_extend c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002272 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002273 return DAG.getNode(ISD::FP_EXTEND, VT, N0);
Chris Lattnere564dbb2006-05-05 21:34:35 +00002274
2275 // fold (fpext (load x)) -> (fpext (fpround (extload x)))
2276 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() &&
2277 (!AfterLegalize||TLI.isOperationLegal(ISD::EXTLOAD, N0.getValueType()))) {
2278 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, N0.getOperand(0),
2279 N0.getOperand(1), N0.getOperand(2),
2280 N0.getValueType());
2281 CombineTo(N, ExtLoad);
2282 CombineTo(N0.Val, DAG.getNode(ISD::FP_ROUND, N0.getValueType(), ExtLoad),
2283 ExtLoad.getValue(1));
2284 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2285 }
2286
2287
Nate Begeman83e75ec2005-09-06 04:43:02 +00002288 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002289}
2290
Nate Begeman83e75ec2005-09-06 04:43:02 +00002291SDOperand DAGCombiner::visitFNEG(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002292 SDOperand N0 = N->getOperand(0);
2293 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2294 MVT::ValueType VT = N->getValueType(0);
2295
2296 // fold (fneg c1) -> -c1
Nate Begeman646d7e22005-09-02 21:18:40 +00002297 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002298 return DAG.getNode(ISD::FNEG, VT, N0);
2299 // fold (fneg (sub x, y)) -> (sub y, x)
Chris Lattner12d83032006-03-05 05:30:57 +00002300 if (N0.getOpcode() == ISD::SUB)
2301 return DAG.getNode(ISD::SUB, VT, N0.getOperand(1), N0.getOperand(0));
Nate Begemana148d982006-01-18 22:35:16 +00002302 // fold (fneg (fneg x)) -> x
Chris Lattner12d83032006-03-05 05:30:57 +00002303 if (N0.getOpcode() == ISD::FNEG)
2304 return N0.getOperand(0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002305 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002306}
2307
Nate Begeman83e75ec2005-09-06 04:43:02 +00002308SDOperand DAGCombiner::visitFABS(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002309 SDOperand N0 = N->getOperand(0);
2310 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2311 MVT::ValueType VT = N->getValueType(0);
2312
Nate Begeman1d4d4142005-09-01 00:19:25 +00002313 // fold (fabs c1) -> fabs(c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00002314 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002315 return DAG.getNode(ISD::FABS, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002316 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00002317 if (N0.getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002318 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002319 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00002320 // fold (fabs (fcopysign x, y)) -> (fabs x)
2321 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
2322 return DAG.getNode(ISD::FABS, VT, N0.getOperand(0));
2323
Nate Begeman83e75ec2005-09-06 04:43:02 +00002324 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002325}
2326
Nate Begeman44728a72005-09-19 22:34:01 +00002327SDOperand DAGCombiner::visitBRCOND(SDNode *N) {
2328 SDOperand Chain = N->getOperand(0);
2329 SDOperand N1 = N->getOperand(1);
2330 SDOperand N2 = N->getOperand(2);
2331 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2332
2333 // never taken branch, fold to chain
2334 if (N1C && N1C->isNullValue())
2335 return Chain;
2336 // unconditional branch
Nate Begemane17daeb2005-10-05 21:43:42 +00002337 if (N1C && N1C->getValue() == 1)
Nate Begeman44728a72005-09-19 22:34:01 +00002338 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
Nate Begeman750ac1b2006-02-01 07:19:44 +00002339 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
2340 // on the target.
2341 if (N1.getOpcode() == ISD::SETCC &&
2342 TLI.isOperationLegal(ISD::BR_CC, MVT::Other)) {
2343 return DAG.getNode(ISD::BR_CC, MVT::Other, Chain, N1.getOperand(2),
2344 N1.getOperand(0), N1.getOperand(1), N2);
2345 }
Nate Begeman44728a72005-09-19 22:34:01 +00002346 return SDOperand();
2347}
2348
Chris Lattner3ea0b472005-10-05 06:47:48 +00002349// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
2350//
Nate Begeman44728a72005-09-19 22:34:01 +00002351SDOperand DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00002352 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
2353 SDOperand CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
2354
2355 // Use SimplifySetCC to simplify SETCC's.
Nate Begemane17daeb2005-10-05 21:43:42 +00002356 SDOperand Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false);
2357 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.Val);
2358
2359 // fold br_cc true, dest -> br dest (unconditional branch)
2360 if (SCCC && SCCC->getValue())
2361 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
2362 N->getOperand(4));
2363 // fold br_cc false, dest -> unconditional fall through
2364 if (SCCC && SCCC->isNullValue())
2365 return N->getOperand(0);
2366 // fold to a simpler setcc
2367 if (Simp.Val && Simp.getOpcode() == ISD::SETCC)
2368 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
2369 Simp.getOperand(2), Simp.getOperand(0),
2370 Simp.getOperand(1), N->getOperand(4));
Nate Begeman44728a72005-09-19 22:34:01 +00002371 return SDOperand();
2372}
2373
Chris Lattner01a22022005-10-10 22:04:48 +00002374SDOperand DAGCombiner::visitLOAD(SDNode *N) {
2375 SDOperand Chain = N->getOperand(0);
2376 SDOperand Ptr = N->getOperand(1);
2377 SDOperand SrcValue = N->getOperand(2);
Chris Lattnere4b95392006-03-31 18:06:18 +00002378
2379 // If there are no uses of the loaded value, change uses of the chain value
2380 // into uses of the chain input (i.e. delete the dead load).
2381 if (N->hasNUsesOfValue(0, 0))
2382 return CombineTo(N, DAG.getNode(ISD::UNDEF, N->getValueType(0)), Chain);
Chris Lattner01a22022005-10-10 22:04:48 +00002383
2384 // If this load is directly stored, replace the load value with the stored
2385 // value.
2386 // TODO: Handle store large -> read small portion.
2387 // TODO: Handle TRUNCSTORE/EXTLOAD
2388 if (Chain.getOpcode() == ISD::STORE && Chain.getOperand(2) == Ptr &&
2389 Chain.getOperand(1).getValueType() == N->getValueType(0))
2390 return CombineTo(N, Chain.getOperand(1), Chain);
2391
2392 return SDOperand();
2393}
2394
Chris Lattner29cd7db2006-03-31 18:10:41 +00002395/// visitXEXTLOAD - Handle EXTLOAD/ZEXTLOAD/SEXTLOAD.
2396SDOperand DAGCombiner::visitXEXTLOAD(SDNode *N) {
2397 SDOperand Chain = N->getOperand(0);
2398 SDOperand Ptr = N->getOperand(1);
2399 SDOperand SrcValue = N->getOperand(2);
2400 SDOperand EVT = N->getOperand(3);
2401
2402 // If there are no uses of the loaded value, change uses of the chain value
2403 // into uses of the chain input (i.e. delete the dead load).
2404 if (N->hasNUsesOfValue(0, 0))
2405 return CombineTo(N, DAG.getNode(ISD::UNDEF, N->getValueType(0)), Chain);
2406
2407 return SDOperand();
2408}
2409
Chris Lattner87514ca2005-10-10 22:31:19 +00002410SDOperand DAGCombiner::visitSTORE(SDNode *N) {
2411 SDOperand Chain = N->getOperand(0);
2412 SDOperand Value = N->getOperand(1);
2413 SDOperand Ptr = N->getOperand(2);
2414 SDOperand SrcValue = N->getOperand(3);
2415
2416 // If this is a store that kills a previous store, remove the previous store.
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002417 if (Chain.getOpcode() == ISD::STORE && Chain.getOperand(2) == Ptr &&
Chris Lattnerfe7f0462005-10-27 07:10:34 +00002418 Chain.Val->hasOneUse() /* Avoid introducing DAG cycles */ &&
2419 // Make sure that these stores are the same value type:
2420 // FIXME: we really care that the second store is >= size of the first.
2421 Value.getValueType() == Chain.getOperand(1).getValueType()) {
Chris Lattner87514ca2005-10-10 22:31:19 +00002422 // Create a new store of Value that replaces both stores.
2423 SDNode *PrevStore = Chain.Val;
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002424 if (PrevStore->getOperand(1) == Value) // Same value multiply stored.
2425 return Chain;
Chris Lattner87514ca2005-10-10 22:31:19 +00002426 SDOperand NewStore = DAG.getNode(ISD::STORE, MVT::Other,
2427 PrevStore->getOperand(0), Value, Ptr,
2428 SrcValue);
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002429 CombineTo(N, NewStore); // Nuke this store.
Chris Lattner87514ca2005-10-10 22:31:19 +00002430 CombineTo(PrevStore, NewStore); // Nuke the previous store.
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002431 return SDOperand(N, 0);
Chris Lattner87514ca2005-10-10 22:31:19 +00002432 }
2433
Chris Lattnerc33baaa2005-12-23 05:48:07 +00002434 // If this is a store of a bit convert, store the input value.
Chris Lattnerbf40c4b2006-01-15 18:58:59 +00002435 // FIXME: This needs to know that the resultant store does not need a
2436 // higher alignment than the original.
2437 if (0 && Value.getOpcode() == ISD::BIT_CONVERT)
Chris Lattnerc33baaa2005-12-23 05:48:07 +00002438 return DAG.getNode(ISD::STORE, MVT::Other, Chain, Value.getOperand(0),
2439 Ptr, SrcValue);
2440
Chris Lattner87514ca2005-10-10 22:31:19 +00002441 return SDOperand();
2442}
2443
Chris Lattnerca242442006-03-19 01:27:56 +00002444SDOperand DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
2445 SDOperand InVec = N->getOperand(0);
2446 SDOperand InVal = N->getOperand(1);
2447 SDOperand EltNo = N->getOperand(2);
2448
2449 // If the invec is a BUILD_VECTOR and if EltNo is a constant, build a new
2450 // vector with the inserted element.
2451 if (InVec.getOpcode() == ISD::BUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
2452 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002453 SmallVector<SDOperand, 8> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
Chris Lattnerca242442006-03-19 01:27:56 +00002454 if (Elt < Ops.size())
2455 Ops[Elt] = InVal;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002456 return DAG.getNode(ISD::BUILD_VECTOR, InVec.getValueType(),
2457 &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00002458 }
2459
2460 return SDOperand();
2461}
2462
2463SDOperand DAGCombiner::visitVINSERT_VECTOR_ELT(SDNode *N) {
2464 SDOperand InVec = N->getOperand(0);
2465 SDOperand InVal = N->getOperand(1);
2466 SDOperand EltNo = N->getOperand(2);
2467 SDOperand NumElts = N->getOperand(3);
2468 SDOperand EltType = N->getOperand(4);
2469
2470 // If the invec is a VBUILD_VECTOR and if EltNo is a constant, build a new
2471 // vector with the inserted element.
2472 if (InVec.getOpcode() == ISD::VBUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
2473 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002474 SmallVector<SDOperand, 8> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
Chris Lattnerca242442006-03-19 01:27:56 +00002475 if (Elt < Ops.size()-2)
2476 Ops[Elt] = InVal;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002477 return DAG.getNode(ISD::VBUILD_VECTOR, InVec.getValueType(),
2478 &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00002479 }
2480
2481 return SDOperand();
2482}
2483
Chris Lattnerd7648c82006-03-28 20:28:38 +00002484SDOperand DAGCombiner::visitVBUILD_VECTOR(SDNode *N) {
2485 unsigned NumInScalars = N->getNumOperands()-2;
2486 SDOperand NumElts = N->getOperand(NumInScalars);
2487 SDOperand EltType = N->getOperand(NumInScalars+1);
2488
2489 // Check to see if this is a VBUILD_VECTOR of a bunch of VEXTRACT_VECTOR_ELT
2490 // operations. If so, and if the EXTRACT_ELT vector inputs come from at most
2491 // two distinct vectors, turn this into a shuffle node.
2492 SDOperand VecIn1, VecIn2;
2493 for (unsigned i = 0; i != NumInScalars; ++i) {
2494 // Ignore undef inputs.
2495 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
2496
2497 // If this input is something other than a VEXTRACT_VECTOR_ELT with a
2498 // constant index, bail out.
2499 if (N->getOperand(i).getOpcode() != ISD::VEXTRACT_VECTOR_ELT ||
2500 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
2501 VecIn1 = VecIn2 = SDOperand(0, 0);
2502 break;
2503 }
2504
2505 // If the input vector type disagrees with the result of the vbuild_vector,
2506 // we can't make a shuffle.
2507 SDOperand ExtractedFromVec = N->getOperand(i).getOperand(0);
2508 if (*(ExtractedFromVec.Val->op_end()-2) != NumElts ||
2509 *(ExtractedFromVec.Val->op_end()-1) != EltType) {
2510 VecIn1 = VecIn2 = SDOperand(0, 0);
2511 break;
2512 }
2513
2514 // Otherwise, remember this. We allow up to two distinct input vectors.
2515 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
2516 continue;
2517
2518 if (VecIn1.Val == 0) {
2519 VecIn1 = ExtractedFromVec;
2520 } else if (VecIn2.Val == 0) {
2521 VecIn2 = ExtractedFromVec;
2522 } else {
2523 // Too many inputs.
2524 VecIn1 = VecIn2 = SDOperand(0, 0);
2525 break;
2526 }
2527 }
2528
2529 // If everything is good, we can make a shuffle operation.
2530 if (VecIn1.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002531 SmallVector<SDOperand, 8> BuildVecIndices;
Chris Lattnerd7648c82006-03-28 20:28:38 +00002532 for (unsigned i = 0; i != NumInScalars; ++i) {
2533 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
2534 BuildVecIndices.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
2535 continue;
2536 }
2537
2538 SDOperand Extract = N->getOperand(i);
2539
2540 // If extracting from the first vector, just use the index directly.
2541 if (Extract.getOperand(0) == VecIn1) {
2542 BuildVecIndices.push_back(Extract.getOperand(1));
2543 continue;
2544 }
2545
2546 // Otherwise, use InIdx + VecSize
2547 unsigned Idx = cast<ConstantSDNode>(Extract.getOperand(1))->getValue();
2548 BuildVecIndices.push_back(DAG.getConstant(Idx+NumInScalars, MVT::i32));
2549 }
2550
2551 // Add count and size info.
2552 BuildVecIndices.push_back(NumElts);
2553 BuildVecIndices.push_back(DAG.getValueType(MVT::i32));
2554
2555 // Return the new VVECTOR_SHUFFLE node.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002556 SDOperand Ops[5];
2557 Ops[0] = VecIn1;
Chris Lattnercef896e2006-03-28 22:19:47 +00002558 if (VecIn2.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002559 Ops[1] = VecIn2;
Chris Lattnercef896e2006-03-28 22:19:47 +00002560 } else {
2561 // Use an undef vbuild_vector as input for the second operand.
2562 std::vector<SDOperand> UnOps(NumInScalars,
2563 DAG.getNode(ISD::UNDEF,
2564 cast<VTSDNode>(EltType)->getVT()));
2565 UnOps.push_back(NumElts);
2566 UnOps.push_back(EltType);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002567 Ops[1] = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
2568 &UnOps[0], UnOps.size());
2569 AddToWorkList(Ops[1].Val);
Chris Lattnercef896e2006-03-28 22:19:47 +00002570 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002571 Ops[2] = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
2572 &BuildVecIndices[0], BuildVecIndices.size());
2573 Ops[3] = NumElts;
2574 Ops[4] = EltType;
2575 return DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector, Ops, 5);
Chris Lattnerd7648c82006-03-28 20:28:38 +00002576 }
2577
2578 return SDOperand();
2579}
2580
Chris Lattner66445d32006-03-28 22:11:53 +00002581SDOperand DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
Chris Lattnerf1d0c622006-03-31 22:16:43 +00002582 SDOperand ShufMask = N->getOperand(2);
2583 unsigned NumElts = ShufMask.getNumOperands();
2584
2585 // If the shuffle mask is an identity operation on the LHS, return the LHS.
2586 bool isIdentity = true;
2587 for (unsigned i = 0; i != NumElts; ++i) {
2588 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
2589 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
2590 isIdentity = false;
2591 break;
2592 }
2593 }
2594 if (isIdentity) return N->getOperand(0);
2595
2596 // If the shuffle mask is an identity operation on the RHS, return the RHS.
2597 isIdentity = true;
2598 for (unsigned i = 0; i != NumElts; ++i) {
2599 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
2600 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
2601 isIdentity = false;
2602 break;
2603 }
2604 }
2605 if (isIdentity) return N->getOperand(1);
Evan Chenge7bec0d2006-07-20 22:44:41 +00002606
2607 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
2608 // needed at all.
2609 bool isUnary = true;
Evan Cheng917ec982006-07-21 08:25:53 +00002610 bool isSplat = true;
Evan Chenge7bec0d2006-07-20 22:44:41 +00002611 int VecNum = -1;
Reid Spencer9160a6a2006-07-25 20:44:41 +00002612 unsigned BaseIdx = 0;
Evan Chenge7bec0d2006-07-20 22:44:41 +00002613 for (unsigned i = 0; i != NumElts; ++i)
2614 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
2615 unsigned Idx = cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue();
2616 int V = (Idx < NumElts) ? 0 : 1;
Evan Cheng917ec982006-07-21 08:25:53 +00002617 if (VecNum == -1) {
Evan Chenge7bec0d2006-07-20 22:44:41 +00002618 VecNum = V;
Evan Cheng917ec982006-07-21 08:25:53 +00002619 BaseIdx = Idx;
2620 } else {
2621 if (BaseIdx != Idx)
2622 isSplat = false;
2623 if (VecNum != V) {
2624 isUnary = false;
2625 break;
2626 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00002627 }
2628 }
2629
2630 SDOperand N0 = N->getOperand(0);
2631 SDOperand N1 = N->getOperand(1);
2632 // Normalize unary shuffle so the RHS is undef.
2633 if (isUnary && VecNum == 1)
2634 std::swap(N0, N1);
2635
Evan Cheng917ec982006-07-21 08:25:53 +00002636 // If it is a splat, check if the argument vector is a build_vector with
2637 // all scalar elements the same.
2638 if (isSplat) {
2639 SDNode *V = N0.Val;
2640 if (V->getOpcode() == ISD::BIT_CONVERT)
2641 V = V->getOperand(0).Val;
2642 if (V->getOpcode() == ISD::BUILD_VECTOR) {
2643 unsigned NumElems = V->getNumOperands()-2;
2644 if (NumElems > BaseIdx) {
2645 SDOperand Base;
2646 bool AllSame = true;
2647 for (unsigned i = 0; i != NumElems; ++i) {
2648 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
2649 Base = V->getOperand(i);
2650 break;
2651 }
2652 }
2653 // Splat of <u, u, u, u>, return <u, u, u, u>
2654 if (!Base.Val)
2655 return N0;
2656 for (unsigned i = 0; i != NumElems; ++i) {
2657 if (V->getOperand(i).getOpcode() != ISD::UNDEF &&
2658 V->getOperand(i) != Base) {
2659 AllSame = false;
2660 break;
2661 }
2662 }
2663 // Splat of <x, x, x, x>, return <x, x, x, x>
2664 if (AllSame)
2665 return N0;
2666 }
2667 }
2668 }
2669
Evan Chenge7bec0d2006-07-20 22:44:41 +00002670 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
2671 // into an undef.
2672 if (isUnary || N0 == N1) {
2673 if (N0.getOpcode() == ISD::UNDEF)
Evan Chengc04766a2006-04-06 23:20:43 +00002674 return DAG.getNode(ISD::UNDEF, N->getValueType(0));
Chris Lattner66445d32006-03-28 22:11:53 +00002675 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
2676 // first operand.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002677 SmallVector<SDOperand, 8> MappedOps;
Chris Lattner66445d32006-03-28 22:11:53 +00002678 for (unsigned i = 0, e = ShufMask.getNumOperands(); i != e; ++i) {
Evan Chengc04766a2006-04-06 23:20:43 +00002679 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
2680 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) {
2681 MappedOps.push_back(ShufMask.getOperand(i));
2682 } else {
Chris Lattner66445d32006-03-28 22:11:53 +00002683 unsigned NewIdx =
2684 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
2685 MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32));
Chris Lattner66445d32006-03-28 22:11:53 +00002686 }
2687 }
2688 ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMask.getValueType(),
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002689 &MappedOps[0], MappedOps.size());
Chris Lattner3e104b12006-04-08 04:15:24 +00002690 AddToWorkList(ShufMask.Val);
Chris Lattner66445d32006-03-28 22:11:53 +00002691 return DAG.getNode(ISD::VECTOR_SHUFFLE, N->getValueType(0),
Evan Chenge7bec0d2006-07-20 22:44:41 +00002692 N0,
Chris Lattner66445d32006-03-28 22:11:53 +00002693 DAG.getNode(ISD::UNDEF, N->getValueType(0)),
2694 ShufMask);
2695 }
2696
2697 return SDOperand();
2698}
2699
Chris Lattnerf1d0c622006-03-31 22:16:43 +00002700SDOperand DAGCombiner::visitVVECTOR_SHUFFLE(SDNode *N) {
2701 SDOperand ShufMask = N->getOperand(2);
2702 unsigned NumElts = ShufMask.getNumOperands()-2;
2703
2704 // If the shuffle mask is an identity operation on the LHS, return the LHS.
2705 bool isIdentity = true;
2706 for (unsigned i = 0; i != NumElts; ++i) {
2707 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
2708 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
2709 isIdentity = false;
2710 break;
2711 }
2712 }
2713 if (isIdentity) return N->getOperand(0);
2714
2715 // If the shuffle mask is an identity operation on the RHS, return the RHS.
2716 isIdentity = true;
2717 for (unsigned i = 0; i != NumElts; ++i) {
2718 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
2719 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
2720 isIdentity = false;
2721 break;
2722 }
2723 }
2724 if (isIdentity) return N->getOperand(1);
2725
Evan Chenge7bec0d2006-07-20 22:44:41 +00002726 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
2727 // needed at all.
2728 bool isUnary = true;
Evan Cheng917ec982006-07-21 08:25:53 +00002729 bool isSplat = true;
Evan Chenge7bec0d2006-07-20 22:44:41 +00002730 int VecNum = -1;
Reid Spencer9160a6a2006-07-25 20:44:41 +00002731 unsigned BaseIdx = 0;
Evan Chenge7bec0d2006-07-20 22:44:41 +00002732 for (unsigned i = 0; i != NumElts; ++i)
2733 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
2734 unsigned Idx = cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue();
2735 int V = (Idx < NumElts) ? 0 : 1;
Evan Cheng917ec982006-07-21 08:25:53 +00002736 if (VecNum == -1) {
Evan Chenge7bec0d2006-07-20 22:44:41 +00002737 VecNum = V;
Evan Cheng917ec982006-07-21 08:25:53 +00002738 BaseIdx = Idx;
2739 } else {
2740 if (BaseIdx != Idx)
2741 isSplat = false;
2742 if (VecNum != V) {
2743 isUnary = false;
2744 break;
2745 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00002746 }
2747 }
2748
2749 SDOperand N0 = N->getOperand(0);
2750 SDOperand N1 = N->getOperand(1);
2751 // Normalize unary shuffle so the RHS is undef.
2752 if (isUnary && VecNum == 1)
2753 std::swap(N0, N1);
2754
Evan Cheng917ec982006-07-21 08:25:53 +00002755 // If it is a splat, check if the argument vector is a build_vector with
2756 // all scalar elements the same.
2757 if (isSplat) {
2758 SDNode *V = N0.Val;
2759 if (V->getOpcode() == ISD::VBIT_CONVERT)
2760 V = V->getOperand(0).Val;
2761 if (V->getOpcode() == ISD::VBUILD_VECTOR) {
2762 unsigned NumElems = V->getNumOperands()-2;
2763 if (NumElems > BaseIdx) {
2764 SDOperand Base;
2765 bool AllSame = true;
2766 for (unsigned i = 0; i != NumElems; ++i) {
2767 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
2768 Base = V->getOperand(i);
2769 break;
2770 }
2771 }
2772 // Splat of <u, u, u, u>, return <u, u, u, u>
2773 if (!Base.Val)
2774 return N0;
2775 for (unsigned i = 0; i != NumElems; ++i) {
2776 if (V->getOperand(i).getOpcode() != ISD::UNDEF &&
2777 V->getOperand(i) != Base) {
2778 AllSame = false;
2779 break;
2780 }
2781 }
2782 // Splat of <x, x, x, x>, return <x, x, x, x>
2783 if (AllSame)
2784 return N0;
2785 }
2786 }
2787 }
2788
Evan Chenge7bec0d2006-07-20 22:44:41 +00002789 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
2790 // into an undef.
2791 if (isUnary || N0 == N1) {
Chris Lattner17614ea2006-04-08 05:34:25 +00002792 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
2793 // first operand.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002794 SmallVector<SDOperand, 8> MappedOps;
Chris Lattner17614ea2006-04-08 05:34:25 +00002795 for (unsigned i = 0; i != NumElts; ++i) {
2796 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
2797 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) {
2798 MappedOps.push_back(ShufMask.getOperand(i));
2799 } else {
2800 unsigned NewIdx =
2801 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
2802 MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32));
2803 }
2804 }
2805 // Add the type/#elts values.
2806 MappedOps.push_back(ShufMask.getOperand(NumElts));
2807 MappedOps.push_back(ShufMask.getOperand(NumElts+1));
2808
2809 ShufMask = DAG.getNode(ISD::VBUILD_VECTOR, ShufMask.getValueType(),
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002810 &MappedOps[0], MappedOps.size());
Chris Lattner17614ea2006-04-08 05:34:25 +00002811 AddToWorkList(ShufMask.Val);
2812
2813 // Build the undef vector.
2814 SDOperand UDVal = DAG.getNode(ISD::UNDEF, MappedOps[0].getValueType());
2815 for (unsigned i = 0; i != NumElts; ++i)
2816 MappedOps[i] = UDVal;
Evan Chenge7bec0d2006-07-20 22:44:41 +00002817 MappedOps[NumElts ] = *(N0.Val->op_end()-2);
2818 MappedOps[NumElts+1] = *(N0.Val->op_end()-1);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002819 UDVal = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
2820 &MappedOps[0], MappedOps.size());
Chris Lattner17614ea2006-04-08 05:34:25 +00002821
2822 return DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector,
Evan Chenge7bec0d2006-07-20 22:44:41 +00002823 N0, UDVal, ShufMask,
Chris Lattner17614ea2006-04-08 05:34:25 +00002824 MappedOps[NumElts], MappedOps[NumElts+1]);
2825 }
2826
Chris Lattnerf1d0c622006-03-31 22:16:43 +00002827 return SDOperand();
2828}
2829
Evan Cheng44f1f092006-04-20 08:56:16 +00002830/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
2831/// a VAND to a vector_shuffle with the destination vector and a zero vector.
2832/// e.g. VAND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
2833/// vector_shuffle V, Zero, <0, 4, 2, 4>
2834SDOperand DAGCombiner::XformToShuffleWithZero(SDNode *N) {
2835 SDOperand LHS = N->getOperand(0);
2836 SDOperand RHS = N->getOperand(1);
2837 if (N->getOpcode() == ISD::VAND) {
2838 SDOperand DstVecSize = *(LHS.Val->op_end()-2);
2839 SDOperand DstVecEVT = *(LHS.Val->op_end()-1);
2840 if (RHS.getOpcode() == ISD::VBIT_CONVERT)
2841 RHS = RHS.getOperand(0);
2842 if (RHS.getOpcode() == ISD::VBUILD_VECTOR) {
2843 std::vector<SDOperand> IdxOps;
2844 unsigned NumOps = RHS.getNumOperands();
2845 unsigned NumElts = NumOps-2;
2846 MVT::ValueType EVT = cast<VTSDNode>(RHS.getOperand(NumOps-1))->getVT();
2847 for (unsigned i = 0; i != NumElts; ++i) {
2848 SDOperand Elt = RHS.getOperand(i);
2849 if (!isa<ConstantSDNode>(Elt))
2850 return SDOperand();
2851 else if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
2852 IdxOps.push_back(DAG.getConstant(i, EVT));
2853 else if (cast<ConstantSDNode>(Elt)->isNullValue())
2854 IdxOps.push_back(DAG.getConstant(NumElts, EVT));
2855 else
2856 return SDOperand();
2857 }
2858
2859 // Let's see if the target supports this vector_shuffle.
2860 if (!TLI.isVectorClearMaskLegal(IdxOps, EVT, DAG))
2861 return SDOperand();
2862
2863 // Return the new VVECTOR_SHUFFLE node.
2864 SDOperand NumEltsNode = DAG.getConstant(NumElts, MVT::i32);
2865 SDOperand EVTNode = DAG.getValueType(EVT);
2866 std::vector<SDOperand> Ops;
2867 LHS = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, LHS, NumEltsNode, EVTNode);
2868 Ops.push_back(LHS);
2869 AddToWorkList(LHS.Val);
2870 std::vector<SDOperand> ZeroOps(NumElts, DAG.getConstant(0, EVT));
2871 ZeroOps.push_back(NumEltsNode);
2872 ZeroOps.push_back(EVTNode);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002873 Ops.push_back(DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
2874 &ZeroOps[0], ZeroOps.size()));
Evan Cheng44f1f092006-04-20 08:56:16 +00002875 IdxOps.push_back(NumEltsNode);
2876 IdxOps.push_back(EVTNode);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002877 Ops.push_back(DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
2878 &IdxOps[0], IdxOps.size()));
Evan Cheng44f1f092006-04-20 08:56:16 +00002879 Ops.push_back(NumEltsNode);
2880 Ops.push_back(EVTNode);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002881 SDOperand Result = DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector,
2882 &Ops[0], Ops.size());
Evan Cheng44f1f092006-04-20 08:56:16 +00002883 if (NumEltsNode != DstVecSize || EVTNode != DstVecEVT) {
2884 Result = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Result,
2885 DstVecSize, DstVecEVT);
2886 }
2887 return Result;
2888 }
2889 }
2890 return SDOperand();
2891}
2892
Chris Lattneredab1b92006-04-02 03:25:57 +00002893/// visitVBinOp - Visit a binary vector operation, like VADD. IntOp indicates
2894/// the scalar operation of the vop if it is operating on an integer vector
2895/// (e.g. ADD) and FPOp indicates the FP version (e.g. FADD).
2896SDOperand DAGCombiner::visitVBinOp(SDNode *N, ISD::NodeType IntOp,
2897 ISD::NodeType FPOp) {
2898 MVT::ValueType EltType = cast<VTSDNode>(*(N->op_end()-1))->getVT();
2899 ISD::NodeType ScalarOp = MVT::isInteger(EltType) ? IntOp : FPOp;
2900 SDOperand LHS = N->getOperand(0);
2901 SDOperand RHS = N->getOperand(1);
Evan Cheng44f1f092006-04-20 08:56:16 +00002902 SDOperand Shuffle = XformToShuffleWithZero(N);
2903 if (Shuffle.Val) return Shuffle;
2904
Chris Lattneredab1b92006-04-02 03:25:57 +00002905 // If the LHS and RHS are VBUILD_VECTOR nodes, see if we can constant fold
2906 // this operation.
2907 if (LHS.getOpcode() == ISD::VBUILD_VECTOR &&
2908 RHS.getOpcode() == ISD::VBUILD_VECTOR) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002909 SmallVector<SDOperand, 8> Ops;
Chris Lattneredab1b92006-04-02 03:25:57 +00002910 for (unsigned i = 0, e = LHS.getNumOperands()-2; i != e; ++i) {
2911 SDOperand LHSOp = LHS.getOperand(i);
2912 SDOperand RHSOp = RHS.getOperand(i);
2913 // If these two elements can't be folded, bail out.
2914 if ((LHSOp.getOpcode() != ISD::UNDEF &&
2915 LHSOp.getOpcode() != ISD::Constant &&
2916 LHSOp.getOpcode() != ISD::ConstantFP) ||
2917 (RHSOp.getOpcode() != ISD::UNDEF &&
2918 RHSOp.getOpcode() != ISD::Constant &&
2919 RHSOp.getOpcode() != ISD::ConstantFP))
2920 break;
Evan Cheng7b336a82006-05-31 06:08:35 +00002921 // Can't fold divide by zero.
2922 if (N->getOpcode() == ISD::VSDIV || N->getOpcode() == ISD::VUDIV) {
2923 if ((RHSOp.getOpcode() == ISD::Constant &&
2924 cast<ConstantSDNode>(RHSOp.Val)->isNullValue()) ||
2925 (RHSOp.getOpcode() == ISD::ConstantFP &&
2926 !cast<ConstantFPSDNode>(RHSOp.Val)->getValue()))
2927 break;
2928 }
Chris Lattneredab1b92006-04-02 03:25:57 +00002929 Ops.push_back(DAG.getNode(ScalarOp, EltType, LHSOp, RHSOp));
Chris Lattner3e104b12006-04-08 04:15:24 +00002930 AddToWorkList(Ops.back().Val);
Chris Lattneredab1b92006-04-02 03:25:57 +00002931 assert((Ops.back().getOpcode() == ISD::UNDEF ||
2932 Ops.back().getOpcode() == ISD::Constant ||
2933 Ops.back().getOpcode() == ISD::ConstantFP) &&
2934 "Scalar binop didn't fold!");
2935 }
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00002936
2937 if (Ops.size() == LHS.getNumOperands()-2) {
2938 Ops.push_back(*(LHS.Val->op_end()-2));
2939 Ops.push_back(*(LHS.Val->op_end()-1));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002940 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00002941 }
Chris Lattneredab1b92006-04-02 03:25:57 +00002942 }
2943
2944 return SDOperand();
2945}
2946
Nate Begeman44728a72005-09-19 22:34:01 +00002947SDOperand DAGCombiner::SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2){
Nate Begemanf845b452005-10-08 00:29:44 +00002948 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
2949
2950 SDOperand SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
2951 cast<CondCodeSDNode>(N0.getOperand(2))->get());
2952 // If we got a simplified select_cc node back from SimplifySelectCC, then
2953 // break it down into a new SETCC node, and a new SELECT node, and then return
2954 // the SELECT node, since we were called with a SELECT node.
2955 if (SCC.Val) {
2956 // Check to see if we got a select_cc back (to turn into setcc/select).
2957 // Otherwise, just return whatever node we got back, like fabs.
2958 if (SCC.getOpcode() == ISD::SELECT_CC) {
2959 SDOperand SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
2960 SCC.getOperand(0), SCC.getOperand(1),
2961 SCC.getOperand(4));
Chris Lattner5750df92006-03-01 04:03:14 +00002962 AddToWorkList(SETCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00002963 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
2964 SCC.getOperand(3), SETCC);
2965 }
2966 return SCC;
2967 }
Nate Begeman44728a72005-09-19 22:34:01 +00002968 return SDOperand();
2969}
2970
Chris Lattner40c62d52005-10-18 06:04:22 +00002971/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
2972/// are the two values being selected between, see if we can simplify the
Chris Lattner729c6d12006-05-27 00:43:02 +00002973/// select. Callers of this should assume that TheSelect is deleted if this
2974/// returns true. As such, they should return the appropriate thing (e.g. the
2975/// node) back to the top-level of the DAG combiner loop to avoid it being
2976/// looked at.
Chris Lattner40c62d52005-10-18 06:04:22 +00002977///
2978bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDOperand LHS,
2979 SDOperand RHS) {
2980
2981 // If this is a select from two identical things, try to pull the operation
2982 // through the select.
2983 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
2984#if 0
2985 std::cerr << "SELECT: ["; LHS.Val->dump();
2986 std::cerr << "] ["; RHS.Val->dump();
2987 std::cerr << "]\n";
2988#endif
2989
2990 // If this is a load and the token chain is identical, replace the select
2991 // of two loads with a load through a select of the address to load from.
2992 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
2993 // constants have been dropped into the constant pool.
2994 if ((LHS.getOpcode() == ISD::LOAD ||
2995 LHS.getOpcode() == ISD::EXTLOAD ||
2996 LHS.getOpcode() == ISD::ZEXTLOAD ||
2997 LHS.getOpcode() == ISD::SEXTLOAD) &&
2998 // Token chains must be identical.
2999 LHS.getOperand(0) == RHS.getOperand(0) &&
3000 // If this is an EXTLOAD, the VT's must match.
3001 (LHS.getOpcode() == ISD::LOAD ||
3002 LHS.getOperand(3) == RHS.getOperand(3))) {
3003 // FIXME: this conflates two src values, discarding one. This is not
3004 // the right thing to do, but nothing uses srcvalues now. When they do,
3005 // turn SrcValue into a list of locations.
3006 SDOperand Addr;
3007 if (TheSelect->getOpcode() == ISD::SELECT)
3008 Addr = DAG.getNode(ISD::SELECT, LHS.getOperand(1).getValueType(),
3009 TheSelect->getOperand(0), LHS.getOperand(1),
3010 RHS.getOperand(1));
3011 else
3012 Addr = DAG.getNode(ISD::SELECT_CC, LHS.getOperand(1).getValueType(),
3013 TheSelect->getOperand(0),
3014 TheSelect->getOperand(1),
3015 LHS.getOperand(1), RHS.getOperand(1),
3016 TheSelect->getOperand(4));
3017
3018 SDOperand Load;
3019 if (LHS.getOpcode() == ISD::LOAD)
3020 Load = DAG.getLoad(TheSelect->getValueType(0), LHS.getOperand(0),
3021 Addr, LHS.getOperand(2));
3022 else
3023 Load = DAG.getExtLoad(LHS.getOpcode(), TheSelect->getValueType(0),
3024 LHS.getOperand(0), Addr, LHS.getOperand(2),
3025 cast<VTSDNode>(LHS.getOperand(3))->getVT());
3026 // Users of the select now use the result of the load.
3027 CombineTo(TheSelect, Load);
3028
3029 // Users of the old loads now use the new load's chain. We know the
3030 // old-load value is dead now.
3031 CombineTo(LHS.Val, Load.getValue(0), Load.getValue(1));
3032 CombineTo(RHS.Val, Load.getValue(0), Load.getValue(1));
3033 return true;
3034 }
3035 }
3036
3037 return false;
3038}
3039
Nate Begeman44728a72005-09-19 22:34:01 +00003040SDOperand DAGCombiner::SimplifySelectCC(SDOperand N0, SDOperand N1,
3041 SDOperand N2, SDOperand N3,
3042 ISD::CondCode CC) {
Nate Begemanf845b452005-10-08 00:29:44 +00003043
3044 MVT::ValueType VT = N2.getValueType();
Chris Lattner5eed34d2006-05-12 17:57:54 +00003045 //ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003046 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
3047 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
3048 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
3049
3050 // Determine if the condition we're dealing with is constant
3051 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
3052 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
3053
3054 // fold select_cc true, x, y -> x
3055 if (SCCC && SCCC->getValue())
3056 return N2;
3057 // fold select_cc false, x, y -> y
3058 if (SCCC && SCCC->getValue() == 0)
3059 return N3;
3060
3061 // Check to see if we can simplify the select into an fabs node
3062 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
3063 // Allow either -0.0 or 0.0
3064 if (CFP->getValue() == 0.0) {
3065 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
3066 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
3067 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
3068 N2 == N3.getOperand(0))
3069 return DAG.getNode(ISD::FABS, VT, N0);
3070
3071 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
3072 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
3073 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
3074 N2.getOperand(0) == N3)
3075 return DAG.getNode(ISD::FABS, VT, N3);
3076 }
3077 }
3078
3079 // Check to see if we can perform the "gzip trick", transforming
3080 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
3081 if (N1C && N1C->isNullValue() && N3C && N3C->isNullValue() &&
3082 MVT::isInteger(N0.getValueType()) &&
3083 MVT::isInteger(N2.getValueType()) && CC == ISD::SETLT) {
3084 MVT::ValueType XType = N0.getValueType();
3085 MVT::ValueType AType = N2.getValueType();
3086 if (XType >= AType) {
3087 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00003088 // single-bit constant.
Nate Begemanf845b452005-10-08 00:29:44 +00003089 if (N2C && ((N2C->getValue() & (N2C->getValue()-1)) == 0)) {
3090 unsigned ShCtV = Log2_64(N2C->getValue());
3091 ShCtV = MVT::getSizeInBits(XType)-ShCtV-1;
3092 SDOperand ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
3093 SDOperand Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
Chris Lattner5750df92006-03-01 04:03:14 +00003094 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003095 if (XType > AType) {
3096 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00003097 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003098 }
3099 return DAG.getNode(ISD::AND, AType, Shift, N2);
3100 }
3101 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
3102 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3103 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00003104 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003105 if (XType > AType) {
3106 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00003107 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003108 }
3109 return DAG.getNode(ISD::AND, AType, Shift, N2);
3110 }
3111 }
Nate Begeman07ed4172005-10-10 21:26:48 +00003112
3113 // fold select C, 16, 0 -> shl C, 4
3114 if (N2C && N3C && N3C->isNullValue() && isPowerOf2_64(N2C->getValue()) &&
3115 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) {
3116 // Get a SetCC of the condition
3117 // FIXME: Should probably make sure that setcc is legal if we ever have a
3118 // target where it isn't.
Nate Begemanb0d04a72006-02-18 02:40:58 +00003119 SDOperand Temp, SCC;
Nate Begeman07ed4172005-10-10 21:26:48 +00003120 // cast from setcc result type to select result type
Nate Begemanb0d04a72006-02-18 02:40:58 +00003121 if (AfterLegalize) {
3122 SCC = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00003123 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
Nate Begemanb0d04a72006-02-18 02:40:58 +00003124 } else {
3125 SCC = DAG.getSetCC(MVT::i1, N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00003126 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00003127 }
Chris Lattner5750df92006-03-01 04:03:14 +00003128 AddToWorkList(SCC.Val);
3129 AddToWorkList(Temp.Val);
Nate Begeman07ed4172005-10-10 21:26:48 +00003130 // shl setcc result by log2 n2c
3131 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
3132 DAG.getConstant(Log2_64(N2C->getValue()),
3133 TLI.getShiftAmountTy()));
3134 }
3135
Nate Begemanf845b452005-10-08 00:29:44 +00003136 // Check to see if this is the equivalent of setcc
3137 // FIXME: Turn all of these into setcc if setcc if setcc is legal
3138 // otherwise, go ahead with the folds.
3139 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getValue() == 1ULL)) {
3140 MVT::ValueType XType = N0.getValueType();
3141 if (TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultTy())) {
3142 SDOperand Res = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
3143 if (Res.getValueType() != VT)
3144 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
3145 return Res;
3146 }
3147
3148 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
3149 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
3150 TLI.isOperationLegal(ISD::CTLZ, XType)) {
3151 SDOperand Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
3152 return DAG.getNode(ISD::SRL, XType, Ctlz,
3153 DAG.getConstant(Log2_32(MVT::getSizeInBits(XType)),
3154 TLI.getShiftAmountTy()));
3155 }
3156 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
3157 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
3158 SDOperand NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
3159 N0);
3160 SDOperand NotN0 = DAG.getNode(ISD::XOR, XType, N0,
3161 DAG.getConstant(~0ULL, XType));
3162 return DAG.getNode(ISD::SRL, XType,
3163 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
3164 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3165 TLI.getShiftAmountTy()));
3166 }
3167 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
3168 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
3169 SDOperand Sign = DAG.getNode(ISD::SRL, XType, N0,
3170 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3171 TLI.getShiftAmountTy()));
3172 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
3173 }
3174 }
3175
3176 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
3177 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
3178 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
3179 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1)) {
3180 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N2.getOperand(0))) {
3181 MVT::ValueType XType = N0.getValueType();
3182 if (SubC->isNullValue() && MVT::isInteger(XType)) {
3183 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
3184 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3185 TLI.getShiftAmountTy()));
3186 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00003187 AddToWorkList(Shift.Val);
3188 AddToWorkList(Add.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003189 return DAG.getNode(ISD::XOR, XType, Add, Shift);
3190 }
3191 }
3192 }
3193
Nate Begeman44728a72005-09-19 22:34:01 +00003194 return SDOperand();
3195}
3196
Nate Begeman452d7be2005-09-16 00:54:12 +00003197SDOperand DAGCombiner::SimplifySetCC(MVT::ValueType VT, SDOperand N0,
Nate Begemane17daeb2005-10-05 21:43:42 +00003198 SDOperand N1, ISD::CondCode Cond,
3199 bool foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00003200 // These setcc operations always fold.
3201 switch (Cond) {
3202 default: break;
3203 case ISD::SETFALSE:
3204 case ISD::SETFALSE2: return DAG.getConstant(0, VT);
3205 case ISD::SETTRUE:
3206 case ISD::SETTRUE2: return DAG.getConstant(1, VT);
3207 }
3208
3209 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
3210 uint64_t C1 = N1C->getValue();
3211 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val)) {
3212 uint64_t C0 = N0C->getValue();
3213
3214 // Sign extend the operands if required
3215 if (ISD::isSignedIntSetCC(Cond)) {
3216 C0 = N0C->getSignExtended();
3217 C1 = N1C->getSignExtended();
3218 }
3219
3220 switch (Cond) {
3221 default: assert(0 && "Unknown integer setcc!");
3222 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
3223 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
3224 case ISD::SETULT: return DAG.getConstant(C0 < C1, VT);
3225 case ISD::SETUGT: return DAG.getConstant(C0 > C1, VT);
3226 case ISD::SETULE: return DAG.getConstant(C0 <= C1, VT);
3227 case ISD::SETUGE: return DAG.getConstant(C0 >= C1, VT);
3228 case ISD::SETLT: return DAG.getConstant((int64_t)C0 < (int64_t)C1, VT);
3229 case ISD::SETGT: return DAG.getConstant((int64_t)C0 > (int64_t)C1, VT);
3230 case ISD::SETLE: return DAG.getConstant((int64_t)C0 <= (int64_t)C1, VT);
3231 case ISD::SETGE: return DAG.getConstant((int64_t)C0 >= (int64_t)C1, VT);
3232 }
3233 } else {
3234 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
3235 if (N0.getOpcode() == ISD::ZERO_EXTEND) {
3236 unsigned InSize = MVT::getSizeInBits(N0.getOperand(0).getValueType());
3237
3238 // If the comparison constant has bits in the upper part, the
3239 // zero-extended value could never match.
3240 if (C1 & (~0ULL << InSize)) {
3241 unsigned VSize = MVT::getSizeInBits(N0.getValueType());
3242 switch (Cond) {
3243 case ISD::SETUGT:
3244 case ISD::SETUGE:
3245 case ISD::SETEQ: return DAG.getConstant(0, VT);
3246 case ISD::SETULT:
3247 case ISD::SETULE:
3248 case ISD::SETNE: return DAG.getConstant(1, VT);
3249 case ISD::SETGT:
3250 case ISD::SETGE:
3251 // True if the sign bit of C1 is set.
3252 return DAG.getConstant((C1 & (1ULL << VSize)) != 0, VT);
3253 case ISD::SETLT:
3254 case ISD::SETLE:
3255 // True if the sign bit of C1 isn't set.
3256 return DAG.getConstant((C1 & (1ULL << VSize)) == 0, VT);
3257 default:
3258 break;
3259 }
3260 }
3261
3262 // Otherwise, we can perform the comparison with the low bits.
3263 switch (Cond) {
3264 case ISD::SETEQ:
3265 case ISD::SETNE:
3266 case ISD::SETUGT:
3267 case ISD::SETUGE:
3268 case ISD::SETULT:
3269 case ISD::SETULE:
3270 return DAG.getSetCC(VT, N0.getOperand(0),
3271 DAG.getConstant(C1, N0.getOperand(0).getValueType()),
3272 Cond);
3273 default:
3274 break; // todo, be more careful with signed comparisons
3275 }
3276 } else if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
3277 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
3278 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N0.getOperand(1))->getVT();
3279 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
3280 MVT::ValueType ExtDstTy = N0.getValueType();
3281 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
3282
3283 // If the extended part has any inconsistent bits, it cannot ever
3284 // compare equal. In other words, they have to be all ones or all
3285 // zeros.
3286 uint64_t ExtBits =
3287 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
3288 if ((C1 & ExtBits) != 0 && (C1 & ExtBits) != ExtBits)
3289 return DAG.getConstant(Cond == ISD::SETNE, VT);
3290
3291 SDOperand ZextOp;
3292 MVT::ValueType Op0Ty = N0.getOperand(0).getValueType();
3293 if (Op0Ty == ExtSrcTy) {
3294 ZextOp = N0.getOperand(0);
3295 } else {
3296 int64_t Imm = ~0ULL >> (64-ExtSrcTyBits);
3297 ZextOp = DAG.getNode(ISD::AND, Op0Ty, N0.getOperand(0),
3298 DAG.getConstant(Imm, Op0Ty));
3299 }
Chris Lattner5750df92006-03-01 04:03:14 +00003300 AddToWorkList(ZextOp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003301 // Otherwise, make this a use of a zext.
3302 return DAG.getSetCC(VT, ZextOp,
3303 DAG.getConstant(C1 & (~0ULL>>(64-ExtSrcTyBits)),
3304 ExtDstTy),
3305 Cond);
Chris Lattner3391bcd2006-02-08 02:13:15 +00003306 } else if ((N1C->getValue() == 0 || N1C->getValue() == 1) &&
3307 (Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
3308 (N0.getOpcode() == ISD::XOR ||
3309 (N0.getOpcode() == ISD::AND &&
3310 N0.getOperand(0).getOpcode() == ISD::XOR &&
3311 N0.getOperand(1) == N0.getOperand(0).getOperand(1))) &&
3312 isa<ConstantSDNode>(N0.getOperand(1)) &&
3313 cast<ConstantSDNode>(N0.getOperand(1))->getValue() == 1) {
3314 // If this is (X^1) == 0/1, swap the RHS and eliminate the xor. We can
3315 // only do this if the top bits are known zero.
3316 if (TLI.MaskedValueIsZero(N1,
3317 MVT::getIntVTBitMask(N0.getValueType())-1)) {
3318 // Okay, get the un-inverted input value.
3319 SDOperand Val;
3320 if (N0.getOpcode() == ISD::XOR)
3321 Val = N0.getOperand(0);
3322 else {
3323 assert(N0.getOpcode() == ISD::AND &&
3324 N0.getOperand(0).getOpcode() == ISD::XOR);
3325 // ((X^1)&1)^1 -> X & 1
3326 Val = DAG.getNode(ISD::AND, N0.getValueType(),
3327 N0.getOperand(0).getOperand(0), N0.getOperand(1));
3328 }
3329 return DAG.getSetCC(VT, Val, N1,
3330 Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ);
3331 }
Nate Begeman452d7be2005-09-16 00:54:12 +00003332 }
Chris Lattner5c46f742005-10-05 06:11:08 +00003333
Nate Begeman452d7be2005-09-16 00:54:12 +00003334 uint64_t MinVal, MaxVal;
3335 unsigned OperandBitSize = MVT::getSizeInBits(N1C->getValueType(0));
3336 if (ISD::isSignedIntSetCC(Cond)) {
3337 MinVal = 1ULL << (OperandBitSize-1);
3338 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
3339 MaxVal = ~0ULL >> (65-OperandBitSize);
3340 else
3341 MaxVal = 0;
3342 } else {
3343 MinVal = 0;
3344 MaxVal = ~0ULL >> (64-OperandBitSize);
3345 }
3346
3347 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
3348 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
3349 if (C1 == MinVal) return DAG.getConstant(1, VT); // X >= MIN --> true
3350 --C1; // X >= C0 --> X > (C0-1)
3351 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
3352 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
3353 }
3354
3355 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
3356 if (C1 == MaxVal) return DAG.getConstant(1, VT); // X <= MAX --> true
3357 ++C1; // X <= C0 --> X < (C0+1)
3358 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
3359 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
3360 }
3361
3362 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal)
3363 return DAG.getConstant(0, VT); // X < MIN --> false
3364
3365 // Canonicalize setgt X, Min --> setne X, Min
3366 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MinVal)
3367 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
Chris Lattnerc8597ca2005-10-21 21:23:25 +00003368 // Canonicalize setlt X, Max --> setne X, Max
3369 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MaxVal)
3370 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
Nate Begeman452d7be2005-09-16 00:54:12 +00003371
3372 // If we have setult X, 1, turn it into seteq X, 0
3373 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal+1)
3374 return DAG.getSetCC(VT, N0, DAG.getConstant(MinVal, N0.getValueType()),
3375 ISD::SETEQ);
3376 // If we have setugt X, Max-1, turn it into seteq X, Max
3377 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal-1)
3378 return DAG.getSetCC(VT, N0, DAG.getConstant(MaxVal, N0.getValueType()),
3379 ISD::SETEQ);
3380
3381 // If we have "setcc X, C0", check to see if we can shrink the immediate
3382 // by changing cc.
3383
3384 // SETUGT X, SINTMAX -> SETLT X, 0
3385 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
3386 C1 == (~0ULL >> (65-OperandBitSize)))
3387 return DAG.getSetCC(VT, N0, DAG.getConstant(0, N1.getValueType()),
3388 ISD::SETLT);
3389
3390 // FIXME: Implement the rest of these.
3391
3392 // Fold bit comparisons when we can.
3393 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
3394 VT == N0.getValueType() && N0.getOpcode() == ISD::AND)
3395 if (ConstantSDNode *AndRHS =
3396 dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
3397 if (Cond == ISD::SETNE && C1 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
3398 // Perform the xform if the AND RHS is a single bit.
3399 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
3400 return DAG.getNode(ISD::SRL, VT, N0,
3401 DAG.getConstant(Log2_64(AndRHS->getValue()),
3402 TLI.getShiftAmountTy()));
3403 }
3404 } else if (Cond == ISD::SETEQ && C1 == AndRHS->getValue()) {
3405 // (X & 8) == 8 --> (X & 8) >> 3
3406 // Perform the xform if C1 is a single bit.
3407 if ((C1 & (C1-1)) == 0) {
3408 return DAG.getNode(ISD::SRL, VT, N0,
Chris Lattner729c6d12006-05-27 00:43:02 +00003409 DAG.getConstant(Log2_64(C1),TLI.getShiftAmountTy()));
Nate Begeman452d7be2005-09-16 00:54:12 +00003410 }
3411 }
3412 }
3413 }
3414 } else if (isa<ConstantSDNode>(N0.Val)) {
3415 // Ensure that the constant occurs on the RHS.
3416 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
3417 }
3418
3419 if (ConstantFPSDNode *N0C = dyn_cast<ConstantFPSDNode>(N0.Val))
3420 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) {
3421 double C0 = N0C->getValue(), C1 = N1C->getValue();
3422
3423 switch (Cond) {
3424 default: break; // FIXME: Implement the rest of these!
3425 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
3426 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
3427 case ISD::SETLT: return DAG.getConstant(C0 < C1, VT);
3428 case ISD::SETGT: return DAG.getConstant(C0 > C1, VT);
3429 case ISD::SETLE: return DAG.getConstant(C0 <= C1, VT);
3430 case ISD::SETGE: return DAG.getConstant(C0 >= C1, VT);
3431 }
3432 } else {
3433 // Ensure that the constant occurs on the RHS.
3434 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
3435 }
3436
3437 if (N0 == N1) {
3438 // We can always fold X == Y for integer setcc's.
3439 if (MVT::isInteger(N0.getValueType()))
3440 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
3441 unsigned UOF = ISD::getUnorderedFlavor(Cond);
3442 if (UOF == 2) // FP operators that are undefined on NaNs.
3443 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
3444 if (UOF == unsigned(ISD::isTrueWhenEqual(Cond)))
3445 return DAG.getConstant(UOF, VT);
3446 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
3447 // if it is not already.
Chris Lattner4090aee2006-01-18 19:13:41 +00003448 ISD::CondCode NewCond = UOF == 0 ? ISD::SETO : ISD::SETUO;
Nate Begeman452d7be2005-09-16 00:54:12 +00003449 if (NewCond != Cond)
3450 return DAG.getSetCC(VT, N0, N1, NewCond);
3451 }
3452
3453 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
3454 MVT::isInteger(N0.getValueType())) {
3455 if (N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::SUB ||
3456 N0.getOpcode() == ISD::XOR) {
3457 // Simplify (X+Y) == (X+Z) --> Y == Z
3458 if (N0.getOpcode() == N1.getOpcode()) {
3459 if (N0.getOperand(0) == N1.getOperand(0))
3460 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(1), Cond);
3461 if (N0.getOperand(1) == N1.getOperand(1))
3462 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(0), Cond);
Evan Cheng1efba0e2006-08-29 06:42:35 +00003463 if (DAG.isCommutativeBinOp(N0.getOpcode())) {
Nate Begeman452d7be2005-09-16 00:54:12 +00003464 // If X op Y == Y op X, try other combinations.
3465 if (N0.getOperand(0) == N1.getOperand(1))
3466 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(0), Cond);
3467 if (N0.getOperand(1) == N1.getOperand(0))
Chris Lattnera158eee2005-10-25 18:57:30 +00003468 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(1), Cond);
Nate Begeman452d7be2005-09-16 00:54:12 +00003469 }
3470 }
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003471
3472 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(N1)) {
3473 if (ConstantSDNode *LHSR = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
3474 // Turn (X+C1) == C2 --> X == C2-C1
3475 if (N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse()) {
3476 return DAG.getSetCC(VT, N0.getOperand(0),
3477 DAG.getConstant(RHSC->getValue()-LHSR->getValue(),
3478 N0.getValueType()), Cond);
3479 }
3480
3481 // Turn (X^C1) == C2 into X == C1^C2 iff X&~C1 = 0.
3482 if (N0.getOpcode() == ISD::XOR)
Chris Lattner5c46f742005-10-05 06:11:08 +00003483 // If we know that all of the inverted bits are zero, don't bother
3484 // performing the inversion.
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003485 if (TLI.MaskedValueIsZero(N0.getOperand(0), ~LHSR->getValue()))
Chris Lattner5c46f742005-10-05 06:11:08 +00003486 return DAG.getSetCC(VT, N0.getOperand(0),
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003487 DAG.getConstant(LHSR->getValue()^RHSC->getValue(),
Chris Lattner5c46f742005-10-05 06:11:08 +00003488 N0.getValueType()), Cond);
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003489 }
3490
3491 // Turn (C1-X) == C2 --> X == C1-C2
3492 if (ConstantSDNode *SUBC = dyn_cast<ConstantSDNode>(N0.getOperand(0))) {
3493 if (N0.getOpcode() == ISD::SUB && N0.Val->hasOneUse()) {
3494 return DAG.getSetCC(VT, N0.getOperand(1),
3495 DAG.getConstant(SUBC->getValue()-RHSC->getValue(),
3496 N0.getValueType()), Cond);
Chris Lattner5c46f742005-10-05 06:11:08 +00003497 }
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003498 }
3499 }
3500
Nate Begeman452d7be2005-09-16 00:54:12 +00003501 // Simplify (X+Z) == X --> Z == 0
3502 if (N0.getOperand(0) == N1)
3503 return DAG.getSetCC(VT, N0.getOperand(1),
3504 DAG.getConstant(0, N0.getValueType()), Cond);
3505 if (N0.getOperand(1) == N1) {
Evan Cheng1efba0e2006-08-29 06:42:35 +00003506 if (DAG.isCommutativeBinOp(N0.getOpcode()))
Nate Begeman452d7be2005-09-16 00:54:12 +00003507 return DAG.getSetCC(VT, N0.getOperand(0),
3508 DAG.getConstant(0, N0.getValueType()), Cond);
3509 else {
3510 assert(N0.getOpcode() == ISD::SUB && "Unexpected operation!");
3511 // (Z-X) == X --> Z == X<<1
3512 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(),
3513 N1,
3514 DAG.getConstant(1,TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00003515 AddToWorkList(SH.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003516 return DAG.getSetCC(VT, N0.getOperand(0), SH, Cond);
3517 }
3518 }
3519 }
3520
3521 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
3522 N1.getOpcode() == ISD::XOR) {
3523 // Simplify X == (X+Z) --> Z == 0
3524 if (N1.getOperand(0) == N0) {
3525 return DAG.getSetCC(VT, N1.getOperand(1),
3526 DAG.getConstant(0, N1.getValueType()), Cond);
3527 } else if (N1.getOperand(1) == N0) {
Evan Cheng1efba0e2006-08-29 06:42:35 +00003528 if (DAG.isCommutativeBinOp(N1.getOpcode())) {
Nate Begeman452d7be2005-09-16 00:54:12 +00003529 return DAG.getSetCC(VT, N1.getOperand(0),
3530 DAG.getConstant(0, N1.getValueType()), Cond);
3531 } else {
3532 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
3533 // X == (Z-X) --> X<<1 == Z
3534 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(), N0,
3535 DAG.getConstant(1,TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00003536 AddToWorkList(SH.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003537 return DAG.getSetCC(VT, SH, N1.getOperand(0), Cond);
3538 }
3539 }
3540 }
3541 }
3542
3543 // Fold away ALL boolean setcc's.
3544 SDOperand Temp;
Nate Begemane17daeb2005-10-05 21:43:42 +00003545 if (N0.getValueType() == MVT::i1 && foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00003546 switch (Cond) {
3547 default: assert(0 && "Unknown integer setcc!");
3548 case ISD::SETEQ: // X == Y -> (X^Y)^1
3549 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
3550 N0 = DAG.getNode(ISD::XOR, MVT::i1, Temp, DAG.getConstant(1, MVT::i1));
Chris Lattner5750df92006-03-01 04:03:14 +00003551 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003552 break;
3553 case ISD::SETNE: // X != Y --> (X^Y)
3554 N0 = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
3555 break;
3556 case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> X^1 & Y
3557 case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> X^1 & Y
3558 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
3559 N0 = DAG.getNode(ISD::AND, MVT::i1, N1, Temp);
Chris Lattner5750df92006-03-01 04:03:14 +00003560 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003561 break;
3562 case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> Y^1 & X
3563 case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> Y^1 & X
3564 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
3565 N0 = DAG.getNode(ISD::AND, MVT::i1, N0, Temp);
Chris Lattner5750df92006-03-01 04:03:14 +00003566 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003567 break;
3568 case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> X^1 | Y
3569 case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> X^1 | Y
3570 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
3571 N0 = DAG.getNode(ISD::OR, MVT::i1, N1, Temp);
Chris Lattner5750df92006-03-01 04:03:14 +00003572 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003573 break;
3574 case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> Y^1 | X
3575 case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> Y^1 | X
3576 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
3577 N0 = DAG.getNode(ISD::OR, MVT::i1, N0, Temp);
3578 break;
3579 }
3580 if (VT != MVT::i1) {
Chris Lattner5750df92006-03-01 04:03:14 +00003581 AddToWorkList(N0.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003582 // FIXME: If running after legalize, we probably can't do this.
3583 N0 = DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
3584 }
3585 return N0;
3586 }
3587
3588 // Could not fold it.
3589 return SDOperand();
3590}
3591
Nate Begeman69575232005-10-20 02:15:44 +00003592/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
3593/// return a DAG expression to select that will generate the same value by
3594/// multiplying by a magic number. See:
3595/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
3596SDOperand DAGCombiner::BuildSDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00003597 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00003598 SDOperand S = TLI.BuildSDIV(N, DAG, &Built);
3599
Andrew Lenharth232c9102006-06-12 16:07:18 +00003600 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00003601 ii != ee; ++ii)
3602 AddToWorkList(*ii);
3603 return S;
Nate Begeman69575232005-10-20 02:15:44 +00003604}
3605
3606/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
3607/// return a DAG expression to select that will generate the same value by
3608/// multiplying by a magic number. See:
3609/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
3610SDOperand DAGCombiner::BuildUDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00003611 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00003612 SDOperand S = TLI.BuildUDIV(N, DAG, &Built);
Nate Begeman69575232005-10-20 02:15:44 +00003613
Andrew Lenharth232c9102006-06-12 16:07:18 +00003614 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00003615 ii != ee; ++ii)
3616 AddToWorkList(*ii);
3617 return S;
Nate Begeman69575232005-10-20 02:15:44 +00003618}
3619
Nate Begeman1d4d4142005-09-01 00:19:25 +00003620// SelectionDAG::Combine - This is the entry point for the file.
3621//
Nate Begeman4ebd8052005-09-01 23:24:04 +00003622void SelectionDAG::Combine(bool RunningAfterLegalize) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003623 /// run - This is the main entry point to this class.
3624 ///
Nate Begeman4ebd8052005-09-01 23:24:04 +00003625 DAGCombiner(*this).Run(RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003626}