blob: dd1ce00a3adcc2808b6df41094ed1db7c3a64ae7 [file] [log] [blame]
Nate Begeman4ebd8052005-09-01 23:24:04 +00001//===-- DAGCombiner.cpp - Implement a DAG node combiner -------------------===//
Nate Begeman1d4d4142005-09-01 00:19:25 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Nate Begeman and is distributed under the
6// University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This pass combines dag nodes to form fewer, simpler DAG nodes. It can be run
11// both before and after the DAG is legalized.
12//
13// FIXME: Missing folds
14// sdiv, udiv, srem, urem (X, const) where X is an integer can be expanded into
15// a sequence of multiplies, shifts, and adds. This should be controlled by
16// some kind of hint from the target that int div is expensive.
17// various folds of mulh[s,u] by constants such as -1, powers of 2, etc.
18//
Nate Begeman44728a72005-09-19 22:34:01 +000019// FIXME: select C, pow2, pow2 -> something smart
20// FIXME: trunc(select X, Y, Z) -> select X, trunc(Y), trunc(Z)
Nate Begeman44728a72005-09-19 22:34:01 +000021// FIXME: Dead stores -> nuke
Chris Lattner40c62d52005-10-18 06:04:22 +000022// FIXME: shr X, (and Y,31) -> shr X, Y (TRICKY!)
Nate Begeman1d4d4142005-09-01 00:19:25 +000023// FIXME: mul (x, const) -> shifts + adds
Nate Begeman1d4d4142005-09-01 00:19:25 +000024// FIXME: undef values
Nate Begeman646d7e22005-09-02 21:18:40 +000025// FIXME: divide by zero is currently left unfolded. do we want to turn this
26// into an undef?
Nate Begemanf845b452005-10-08 00:29:44 +000027// FIXME: select ne (select cc, 1, 0), 0, true, false -> select cc, true, false
Nate Begeman1d4d4142005-09-01 00:19:25 +000028//
29//===----------------------------------------------------------------------===//
30
31#define DEBUG_TYPE "dagcombine"
32#include "llvm/ADT/Statistic.h"
33#include "llvm/CodeGen/SelectionDAG.h"
Nate Begeman2300f552005-09-07 00:15:36 +000034#include "llvm/Support/Debug.h"
Nate Begeman1d4d4142005-09-01 00:19:25 +000035#include "llvm/Support/MathExtras.h"
36#include "llvm/Target/TargetLowering.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000037#include "llvm/Support/Compiler.h"
Jim Laskeyd1aed7a2006-09-21 16:28:59 +000038#include "llvm/Support/CommandLine.h"
Chris Lattnera500fc62005-09-09 23:53:39 +000039#include <algorithm>
Nate Begeman1d4d4142005-09-01 00:19:25 +000040#include <cmath>
Chris Lattner2c2c6c62006-01-22 23:41:00 +000041#include <iostream>
Jim Laskey279f0532006-09-25 16:29:54 +000042#include <algorithm>
Nate Begeman1d4d4142005-09-01 00:19:25 +000043using namespace llvm;
44
45namespace {
Andrew Lenharthae6153f2006-07-20 17:43:27 +000046 static Statistic<> NodesCombined ("dagcombiner",
47 "Number of dag nodes combined");
Jim Laskeyd1aed7a2006-09-21 16:28:59 +000048
Jim Laskey71382342006-10-07 23:37:56 +000049 static cl::opt<bool>
50 CombinerAA("combiner-alias-analysis", cl::Hidden,
51 cl::desc("Turn on alias analysis turning testing"));
Jim Laskey6ff23e52006-10-04 16:53:27 +000052
Jim Laskeybc588b82006-10-05 15:07:25 +000053//------------------------------ DAGCombiner ---------------------------------//
54
Jim Laskey71382342006-10-07 23:37:56 +000055 class VISIBILITY_HIDDEN DAGCombiner {
Nate Begeman1d4d4142005-09-01 00:19:25 +000056 SelectionDAG &DAG;
57 TargetLowering &TLI;
Nate Begeman4ebd8052005-09-01 23:24:04 +000058 bool AfterLegalize;
Nate Begeman1d4d4142005-09-01 00:19:25 +000059
60 // Worklist of all of the nodes that need to be simplified.
61 std::vector<SDNode*> WorkList;
62
63 /// AddUsersToWorkList - When an instruction is simplified, add all users of
64 /// the instruction to the work lists because they might get more simplified
65 /// now.
66 ///
67 void AddUsersToWorkList(SDNode *N) {
68 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
Nate Begeman4ebd8052005-09-01 23:24:04 +000069 UI != UE; ++UI)
Jim Laskey6ff23e52006-10-04 16:53:27 +000070 AddToWorkList(*UI);
Nate Begeman1d4d4142005-09-01 00:19:25 +000071 }
72
73 /// removeFromWorkList - remove all instances of N from the worklist.
Chris Lattner5750df92006-03-01 04:03:14 +000074 ///
Nate Begeman1d4d4142005-09-01 00:19:25 +000075 void removeFromWorkList(SDNode *N) {
76 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), N),
77 WorkList.end());
78 }
79
Chris Lattner24664722006-03-01 04:53:38 +000080 public:
Jim Laskey6ff23e52006-10-04 16:53:27 +000081 /// AddToWorkList - Add to the work list making sure it's instance is at the
82 /// the back (next to be processed.)
Chris Lattner5750df92006-03-01 04:03:14 +000083 void AddToWorkList(SDNode *N) {
Jim Laskey6ff23e52006-10-04 16:53:27 +000084 removeFromWorkList(N);
Chris Lattner5750df92006-03-01 04:03:14 +000085 WorkList.push_back(N);
86 }
Jim Laskey6ff23e52006-10-04 16:53:27 +000087
Chris Lattner3577e382006-08-11 17:56:38 +000088 SDOperand CombineTo(SDNode *N, const SDOperand *To, unsigned NumTo) {
89 assert(N->getNumValues() == NumTo && "Broken CombineTo call!");
Chris Lattner87514ca2005-10-10 22:31:19 +000090 ++NodesCombined;
Jim Laskey6ff23e52006-10-04 16:53:27 +000091 DEBUG(std::cerr << "\nReplacing.1 "; N->dump();
Evan Cheng60e8c712006-05-09 06:55:15 +000092 std::cerr << "\nWith: "; To[0].Val->dump(&DAG);
Chris Lattner3577e382006-08-11 17:56:38 +000093 std::cerr << " and " << NumTo-1 << " other values\n");
Chris Lattner01a22022005-10-10 22:04:48 +000094 std::vector<SDNode*> NowDead;
Chris Lattner3577e382006-08-11 17:56:38 +000095 DAG.ReplaceAllUsesWith(N, To, &NowDead);
Chris Lattner01a22022005-10-10 22:04:48 +000096
97 // Push the new nodes and any users onto the worklist
Chris Lattner3577e382006-08-11 17:56:38 +000098 for (unsigned i = 0, e = NumTo; i != e; ++i) {
Jim Laskey6ff23e52006-10-04 16:53:27 +000099 AddToWorkList(To[i].Val);
Chris Lattner01a22022005-10-10 22:04:48 +0000100 AddUsersToWorkList(To[i].Val);
101 }
102
Jim Laskey6ff23e52006-10-04 16:53:27 +0000103 // Nodes can be reintroduced into the worklist. Make sure we do not
104 // process a node that has been replaced.
Chris Lattner01a22022005-10-10 22:04:48 +0000105 removeFromWorkList(N);
106 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
107 removeFromWorkList(NowDead[i]);
108
109 // Finally, since the node is now dead, remove it from the graph.
110 DAG.DeleteNode(N);
111 return SDOperand(N, 0);
112 }
Nate Begeman368e18d2006-02-16 21:11:51 +0000113
Chris Lattner24664722006-03-01 04:53:38 +0000114 SDOperand CombineTo(SDNode *N, SDOperand Res) {
Chris Lattner3577e382006-08-11 17:56:38 +0000115 return CombineTo(N, &Res, 1);
Chris Lattner24664722006-03-01 04:53:38 +0000116 }
117
118 SDOperand CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1) {
Chris Lattner3577e382006-08-11 17:56:38 +0000119 SDOperand To[] = { Res0, Res1 };
120 return CombineTo(N, To, 2);
Chris Lattner24664722006-03-01 04:53:38 +0000121 }
122 private:
123
Chris Lattner012f2412006-02-17 21:58:01 +0000124 /// SimplifyDemandedBits - Check the specified integer node value to see if
Chris Lattnerb2742f42006-03-01 19:55:35 +0000125 /// it can be simplified or if things it uses can be simplified by bit
Chris Lattner012f2412006-02-17 21:58:01 +0000126 /// propagation. If so, return true.
127 bool SimplifyDemandedBits(SDOperand Op) {
Nate Begeman368e18d2006-02-16 21:11:51 +0000128 TargetLowering::TargetLoweringOpt TLO(DAG);
129 uint64_t KnownZero, KnownOne;
Chris Lattner012f2412006-02-17 21:58:01 +0000130 uint64_t Demanded = MVT::getIntVTBitMask(Op.getValueType());
131 if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO))
132 return false;
133
134 // Revisit the node.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000135 AddToWorkList(Op.Val);
Chris Lattner012f2412006-02-17 21:58:01 +0000136
137 // Replace the old value with the new one.
138 ++NodesCombined;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000139 DEBUG(std::cerr << "\nReplacing.2 "; TLO.Old.Val->dump();
Jim Laskey279f0532006-09-25 16:29:54 +0000140 std::cerr << "\nWith: "; TLO.New.Val->dump(&DAG);
141 std::cerr << '\n');
Chris Lattner012f2412006-02-17 21:58:01 +0000142
143 std::vector<SDNode*> NowDead;
144 DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New, NowDead);
145
Chris Lattner7d20d392006-02-20 06:51:04 +0000146 // Push the new node and any (possibly new) users onto the worklist.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000147 AddToWorkList(TLO.New.Val);
Chris Lattner012f2412006-02-17 21:58:01 +0000148 AddUsersToWorkList(TLO.New.Val);
149
150 // Nodes can end up on the worklist more than once. Make sure we do
151 // not process a node that has been replaced.
Chris Lattner012f2412006-02-17 21:58:01 +0000152 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
153 removeFromWorkList(NowDead[i]);
154
Chris Lattner7d20d392006-02-20 06:51:04 +0000155 // Finally, if the node is now dead, remove it from the graph. The node
156 // may not be dead if the replacement process recursively simplified to
157 // something else needing this node.
158 if (TLO.Old.Val->use_empty()) {
159 removeFromWorkList(TLO.Old.Val);
160 DAG.DeleteNode(TLO.Old.Val);
161 }
Chris Lattner012f2412006-02-17 21:58:01 +0000162 return true;
Nate Begeman368e18d2006-02-16 21:11:51 +0000163 }
Chris Lattner87514ca2005-10-10 22:31:19 +0000164
Nate Begeman1d4d4142005-09-01 00:19:25 +0000165 /// visit - call the node-specific routine that knows how to fold each
166 /// particular type of node.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000167 SDOperand visit(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000168
169 // Visitation implementation - Implement dag node combining for different
170 // node types. The semantics are as follows:
171 // Return Value:
Nate Begeman2300f552005-09-07 00:15:36 +0000172 // SDOperand.Val == 0 - No change was made
Chris Lattner01a22022005-10-10 22:04:48 +0000173 // SDOperand.Val == N - N was replaced, is dead, and is already handled.
Nate Begeman2300f552005-09-07 00:15:36 +0000174 // otherwise - N should be replaced by the returned Operand.
Nate Begeman1d4d4142005-09-01 00:19:25 +0000175 //
Nate Begeman83e75ec2005-09-06 04:43:02 +0000176 SDOperand visitTokenFactor(SDNode *N);
177 SDOperand visitADD(SDNode *N);
178 SDOperand visitSUB(SDNode *N);
179 SDOperand visitMUL(SDNode *N);
180 SDOperand visitSDIV(SDNode *N);
181 SDOperand visitUDIV(SDNode *N);
182 SDOperand visitSREM(SDNode *N);
183 SDOperand visitUREM(SDNode *N);
184 SDOperand visitMULHU(SDNode *N);
185 SDOperand visitMULHS(SDNode *N);
186 SDOperand visitAND(SDNode *N);
187 SDOperand visitOR(SDNode *N);
188 SDOperand visitXOR(SDNode *N);
Chris Lattneredab1b92006-04-02 03:25:57 +0000189 SDOperand visitVBinOp(SDNode *N, ISD::NodeType IntOp, ISD::NodeType FPOp);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000190 SDOperand visitSHL(SDNode *N);
191 SDOperand visitSRA(SDNode *N);
192 SDOperand visitSRL(SDNode *N);
193 SDOperand visitCTLZ(SDNode *N);
194 SDOperand visitCTTZ(SDNode *N);
195 SDOperand visitCTPOP(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000196 SDOperand visitSELECT(SDNode *N);
197 SDOperand visitSELECT_CC(SDNode *N);
198 SDOperand visitSETCC(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000199 SDOperand visitSIGN_EXTEND(SDNode *N);
200 SDOperand visitZERO_EXTEND(SDNode *N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000201 SDOperand visitANY_EXTEND(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000202 SDOperand visitSIGN_EXTEND_INREG(SDNode *N);
203 SDOperand visitTRUNCATE(SDNode *N);
Chris Lattner94683772005-12-23 05:30:37 +0000204 SDOperand visitBIT_CONVERT(SDNode *N);
Chris Lattner6258fb22006-04-02 02:53:43 +0000205 SDOperand visitVBIT_CONVERT(SDNode *N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000206 SDOperand visitFADD(SDNode *N);
207 SDOperand visitFSUB(SDNode *N);
208 SDOperand visitFMUL(SDNode *N);
209 SDOperand visitFDIV(SDNode *N);
210 SDOperand visitFREM(SDNode *N);
Chris Lattner12d83032006-03-05 05:30:57 +0000211 SDOperand visitFCOPYSIGN(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000212 SDOperand visitSINT_TO_FP(SDNode *N);
213 SDOperand visitUINT_TO_FP(SDNode *N);
214 SDOperand visitFP_TO_SINT(SDNode *N);
215 SDOperand visitFP_TO_UINT(SDNode *N);
216 SDOperand visitFP_ROUND(SDNode *N);
217 SDOperand visitFP_ROUND_INREG(SDNode *N);
218 SDOperand visitFP_EXTEND(SDNode *N);
219 SDOperand visitFNEG(SDNode *N);
220 SDOperand visitFABS(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000221 SDOperand visitBRCOND(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000222 SDOperand visitBR_CC(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000223 SDOperand visitLOAD(SDNode *N);
Evan Chengc5484282006-10-04 00:56:09 +0000224 SDOperand visitLOADX(SDNode *N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000225 SDOperand visitSTORE(SDNode *N);
Chris Lattnerca242442006-03-19 01:27:56 +0000226 SDOperand visitINSERT_VECTOR_ELT(SDNode *N);
227 SDOperand visitVINSERT_VECTOR_ELT(SDNode *N);
Chris Lattnerd7648c82006-03-28 20:28:38 +0000228 SDOperand visitVBUILD_VECTOR(SDNode *N);
Chris Lattner66445d32006-03-28 22:11:53 +0000229 SDOperand visitVECTOR_SHUFFLE(SDNode *N);
Chris Lattnerf1d0c622006-03-31 22:16:43 +0000230 SDOperand visitVVECTOR_SHUFFLE(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000231
Evan Cheng44f1f092006-04-20 08:56:16 +0000232 SDOperand XformToShuffleWithZero(SDNode *N);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000233 SDOperand ReassociateOps(unsigned Opc, SDOperand LHS, SDOperand RHS);
234
Chris Lattner40c62d52005-10-18 06:04:22 +0000235 bool SimplifySelectOps(SDNode *SELECT, SDOperand LHS, SDOperand RHS);
Chris Lattner35e5c142006-05-05 05:51:50 +0000236 SDOperand SimplifyBinOpWithSameOpcodeHands(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000237 SDOperand SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2);
238 SDOperand SimplifySelectCC(SDOperand N0, SDOperand N1, SDOperand N2,
239 SDOperand N3, ISD::CondCode CC);
Nate Begeman452d7be2005-09-16 00:54:12 +0000240 SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N0, SDOperand N1,
Nate Begemane17daeb2005-10-05 21:43:42 +0000241 ISD::CondCode Cond, bool foldBooleans = true);
Chris Lattner6258fb22006-04-02 02:53:43 +0000242 SDOperand ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(SDNode *, MVT::ValueType);
Nate Begeman69575232005-10-20 02:15:44 +0000243 SDOperand BuildSDIV(SDNode *N);
Chris Lattner516b9622006-09-14 20:50:57 +0000244 SDOperand BuildUDIV(SDNode *N);
245 SDNode *MatchRotate(SDOperand LHS, SDOperand RHS);
Jim Laskey279f0532006-09-25 16:29:54 +0000246
Jim Laskey6ff23e52006-10-04 16:53:27 +0000247 /// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
248 /// looking for aliasing nodes and adding them to the Aliases vector.
Jim Laskeybc588b82006-10-05 15:07:25 +0000249 void GatherAllAliases(SDNode *N, SDOperand OriginalChain,
Jim Laskey6ff23e52006-10-04 16:53:27 +0000250 SmallVector<SDOperand, 8> &Aliases);
251
Jim Laskey279f0532006-09-25 16:29:54 +0000252 /// FindBetterChain - Walk up chain skipping non-aliasing memory nodes,
Jim Laskey6ff23e52006-10-04 16:53:27 +0000253 /// looking for a better chain (aliasing node.)
Jim Laskey279f0532006-09-25 16:29:54 +0000254 SDOperand FindBetterChain(SDNode *N, SDOperand Chain);
255
Nate Begeman1d4d4142005-09-01 00:19:25 +0000256public:
257 DAGCombiner(SelectionDAG &D)
Nate Begeman646d7e22005-09-02 21:18:40 +0000258 : DAG(D), TLI(D.getTargetLoweringInfo()), AfterLegalize(false) {}
Nate Begeman1d4d4142005-09-01 00:19:25 +0000259
260 /// Run - runs the dag combiner on all nodes in the work list
Nate Begeman4ebd8052005-09-01 23:24:04 +0000261 void Run(bool RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000262 };
263}
264
Chris Lattner24664722006-03-01 04:53:38 +0000265//===----------------------------------------------------------------------===//
266// TargetLowering::DAGCombinerInfo implementation
267//===----------------------------------------------------------------------===//
268
269void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) {
270 ((DAGCombiner*)DC)->AddToWorkList(N);
271}
272
273SDOperand TargetLowering::DAGCombinerInfo::
274CombineTo(SDNode *N, const std::vector<SDOperand> &To) {
Chris Lattner3577e382006-08-11 17:56:38 +0000275 return ((DAGCombiner*)DC)->CombineTo(N, &To[0], To.size());
Chris Lattner24664722006-03-01 04:53:38 +0000276}
277
278SDOperand TargetLowering::DAGCombinerInfo::
279CombineTo(SDNode *N, SDOperand Res) {
280 return ((DAGCombiner*)DC)->CombineTo(N, Res);
281}
282
283
284SDOperand TargetLowering::DAGCombinerInfo::
285CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1) {
286 return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1);
287}
288
289
290
291
292//===----------------------------------------------------------------------===//
293
294
Nate Begeman4ebd8052005-09-01 23:24:04 +0000295// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
296// that selects between the values 1 and 0, making it equivalent to a setcc.
Nate Begeman646d7e22005-09-02 21:18:40 +0000297// Also, set the incoming LHS, RHS, and CC references to the appropriate
298// nodes based on the type of node we are checking. This simplifies life a
299// bit for the callers.
300static bool isSetCCEquivalent(SDOperand N, SDOperand &LHS, SDOperand &RHS,
301 SDOperand &CC) {
302 if (N.getOpcode() == ISD::SETCC) {
303 LHS = N.getOperand(0);
304 RHS = N.getOperand(1);
305 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000306 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000307 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000308 if (N.getOpcode() == ISD::SELECT_CC &&
309 N.getOperand(2).getOpcode() == ISD::Constant &&
310 N.getOperand(3).getOpcode() == ISD::Constant &&
311 cast<ConstantSDNode>(N.getOperand(2))->getValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000312 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
313 LHS = N.getOperand(0);
314 RHS = N.getOperand(1);
315 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000316 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000317 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000318 return false;
319}
320
Nate Begeman99801192005-09-07 23:25:52 +0000321// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
322// one use. If this is true, it allows the users to invert the operation for
323// free when it is profitable to do so.
324static bool isOneUseSetCC(SDOperand N) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000325 SDOperand N0, N1, N2;
Nate Begeman646d7e22005-09-02 21:18:40 +0000326 if (isSetCCEquivalent(N, N0, N1, N2) && N.Val->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000327 return true;
328 return false;
329}
330
Nate Begemancd4d58c2006-02-03 06:46:56 +0000331SDOperand DAGCombiner::ReassociateOps(unsigned Opc, SDOperand N0, SDOperand N1){
332 MVT::ValueType VT = N0.getValueType();
333 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
334 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
335 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
336 if (isa<ConstantSDNode>(N1)) {
337 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(1), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000338 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000339 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(0));
340 } else if (N0.hasOneUse()) {
341 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(0), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000342 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000343 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(1));
344 }
345 }
346 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
347 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
348 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
349 if (isa<ConstantSDNode>(N0)) {
350 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(1), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000351 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000352 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(0));
353 } else if (N1.hasOneUse()) {
354 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(0), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000355 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000356 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(1));
357 }
358 }
359 return SDOperand();
360}
361
Nate Begeman4ebd8052005-09-01 23:24:04 +0000362void DAGCombiner::Run(bool RunningAfterLegalize) {
363 // set the instance variable, so that the various visit routines may use it.
364 AfterLegalize = RunningAfterLegalize;
365
Nate Begeman646d7e22005-09-02 21:18:40 +0000366 // Add all the dag nodes to the worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000367 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
368 E = DAG.allnodes_end(); I != E; ++I)
369 WorkList.push_back(I);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000370
Chris Lattner95038592005-10-05 06:35:28 +0000371 // Create a dummy node (which is not added to allnodes), that adds a reference
372 // to the root node, preventing it from being deleted, and tracking any
373 // changes of the root.
374 HandleSDNode Dummy(DAG.getRoot());
375
Chris Lattner24664722006-03-01 04:53:38 +0000376
377 /// DagCombineInfo - Expose the DAG combiner to the target combiner impls.
378 TargetLowering::DAGCombinerInfo
379 DagCombineInfo(DAG, !RunningAfterLegalize, this);
Jim Laskey6ff23e52006-10-04 16:53:27 +0000380
Nate Begeman1d4d4142005-09-01 00:19:25 +0000381 // while the worklist isn't empty, inspect the node on the end of it and
382 // try and combine it.
383 while (!WorkList.empty()) {
384 SDNode *N = WorkList.back();
385 WorkList.pop_back();
386
387 // If N has no uses, it is dead. Make sure to revisit all N's operands once
Chris Lattner95038592005-10-05 06:35:28 +0000388 // N is deleted from the DAG, since they too may now be dead or may have a
389 // reduced number of uses, allowing other xforms.
390 if (N->use_empty() && N != &Dummy) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000391 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Jim Laskey6ff23e52006-10-04 16:53:27 +0000392 AddToWorkList(N->getOperand(i).Val);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000393
Chris Lattner95038592005-10-05 06:35:28 +0000394 DAG.DeleteNode(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000395 continue;
396 }
397
Nate Begeman83e75ec2005-09-06 04:43:02 +0000398 SDOperand RV = visit(N);
Chris Lattner24664722006-03-01 04:53:38 +0000399
400 // If nothing happened, try a target-specific DAG combine.
401 if (RV.Val == 0) {
Chris Lattner729c6d12006-05-27 00:43:02 +0000402 assert(N->getOpcode() != ISD::DELETED_NODE &&
403 "Node was deleted but visit returned NULL!");
Chris Lattner24664722006-03-01 04:53:38 +0000404 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
405 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode()))
406 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
407 }
408
Nate Begeman83e75ec2005-09-06 04:43:02 +0000409 if (RV.Val) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000410 ++NodesCombined;
Nate Begeman646d7e22005-09-02 21:18:40 +0000411 // If we get back the same node we passed in, rather than a new node or
412 // zero, we know that the node must have defined multiple values and
413 // CombineTo was used. Since CombineTo takes care of the worklist
414 // mechanics for us, we have no work to do in this case.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000415 if (RV.Val != N) {
Chris Lattner729c6d12006-05-27 00:43:02 +0000416 assert(N->getOpcode() != ISD::DELETED_NODE &&
417 RV.Val->getOpcode() != ISD::DELETED_NODE &&
418 "Node was deleted but visit returned new node!");
419
Jim Laskey6ff23e52006-10-04 16:53:27 +0000420 DEBUG(std::cerr << "\nReplacing.3 "; N->dump();
Evan Cheng60e8c712006-05-09 06:55:15 +0000421 std::cerr << "\nWith: "; RV.Val->dump(&DAG);
Nate Begeman2300f552005-09-07 00:15:36 +0000422 std::cerr << '\n');
Chris Lattner01a22022005-10-10 22:04:48 +0000423 std::vector<SDNode*> NowDead;
Evan Cheng2adffa12006-09-21 19:04:05 +0000424 if (N->getNumValues() == RV.Val->getNumValues())
425 DAG.ReplaceAllUsesWith(N, RV.Val, &NowDead);
426 else {
427 assert(N->getValueType(0) == RV.getValueType() && "Type mismatch");
428 SDOperand OpV = RV;
429 DAG.ReplaceAllUsesWith(N, &OpV, &NowDead);
430 }
Nate Begeman646d7e22005-09-02 21:18:40 +0000431
432 // Push the new node and any users onto the worklist
Jim Laskey6ff23e52006-10-04 16:53:27 +0000433 AddToWorkList(RV.Val);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000434 AddUsersToWorkList(RV.Val);
Nate Begeman646d7e22005-09-02 21:18:40 +0000435
Jim Laskey6ff23e52006-10-04 16:53:27 +0000436 // Nodes can be reintroduced into the worklist. Make sure we do not
437 // process a node that has been replaced.
Nate Begeman646d7e22005-09-02 21:18:40 +0000438 removeFromWorkList(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000439 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
440 removeFromWorkList(NowDead[i]);
Chris Lattner5c46f742005-10-05 06:11:08 +0000441
442 // Finally, since the node is now dead, remove it from the graph.
443 DAG.DeleteNode(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000444 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000445 }
446 }
Chris Lattner95038592005-10-05 06:35:28 +0000447
448 // If the root changed (e.g. it was a dead load, update the root).
449 DAG.setRoot(Dummy.getValue());
Nate Begeman1d4d4142005-09-01 00:19:25 +0000450}
451
Nate Begeman83e75ec2005-09-06 04:43:02 +0000452SDOperand DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000453 switch(N->getOpcode()) {
454 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +0000455 case ISD::TokenFactor: return visitTokenFactor(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000456 case ISD::ADD: return visitADD(N);
457 case ISD::SUB: return visitSUB(N);
458 case ISD::MUL: return visitMUL(N);
459 case ISD::SDIV: return visitSDIV(N);
460 case ISD::UDIV: return visitUDIV(N);
461 case ISD::SREM: return visitSREM(N);
462 case ISD::UREM: return visitUREM(N);
463 case ISD::MULHU: return visitMULHU(N);
464 case ISD::MULHS: return visitMULHS(N);
465 case ISD::AND: return visitAND(N);
466 case ISD::OR: return visitOR(N);
467 case ISD::XOR: return visitXOR(N);
468 case ISD::SHL: return visitSHL(N);
469 case ISD::SRA: return visitSRA(N);
470 case ISD::SRL: return visitSRL(N);
471 case ISD::CTLZ: return visitCTLZ(N);
472 case ISD::CTTZ: return visitCTTZ(N);
473 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000474 case ISD::SELECT: return visitSELECT(N);
475 case ISD::SELECT_CC: return visitSELECT_CC(N);
476 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000477 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
478 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000479 case ISD::ANY_EXTEND: return visitANY_EXTEND(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000480 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
481 case ISD::TRUNCATE: return visitTRUNCATE(N);
Chris Lattner94683772005-12-23 05:30:37 +0000482 case ISD::BIT_CONVERT: return visitBIT_CONVERT(N);
Chris Lattner6258fb22006-04-02 02:53:43 +0000483 case ISD::VBIT_CONVERT: return visitVBIT_CONVERT(N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000484 case ISD::FADD: return visitFADD(N);
485 case ISD::FSUB: return visitFSUB(N);
486 case ISD::FMUL: return visitFMUL(N);
487 case ISD::FDIV: return visitFDIV(N);
488 case ISD::FREM: return visitFREM(N);
Chris Lattner12d83032006-03-05 05:30:57 +0000489 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000490 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
491 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
492 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
493 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
494 case ISD::FP_ROUND: return visitFP_ROUND(N);
495 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
496 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
497 case ISD::FNEG: return visitFNEG(N);
498 case ISD::FABS: return visitFABS(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000499 case ISD::BRCOND: return visitBRCOND(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000500 case ISD::BR_CC: return visitBR_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000501 case ISD::LOAD: return visitLOAD(N);
Evan Chengc5484282006-10-04 00:56:09 +0000502 case ISD::LOADX: return visitLOADX(N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000503 case ISD::STORE: return visitSTORE(N);
Chris Lattnerca242442006-03-19 01:27:56 +0000504 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
505 case ISD::VINSERT_VECTOR_ELT: return visitVINSERT_VECTOR_ELT(N);
Chris Lattnerd7648c82006-03-28 20:28:38 +0000506 case ISD::VBUILD_VECTOR: return visitVBUILD_VECTOR(N);
Chris Lattner66445d32006-03-28 22:11:53 +0000507 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
Chris Lattnerf1d0c622006-03-31 22:16:43 +0000508 case ISD::VVECTOR_SHUFFLE: return visitVVECTOR_SHUFFLE(N);
Chris Lattneredab1b92006-04-02 03:25:57 +0000509 case ISD::VADD: return visitVBinOp(N, ISD::ADD , ISD::FADD);
510 case ISD::VSUB: return visitVBinOp(N, ISD::SUB , ISD::FSUB);
511 case ISD::VMUL: return visitVBinOp(N, ISD::MUL , ISD::FMUL);
512 case ISD::VSDIV: return visitVBinOp(N, ISD::SDIV, ISD::FDIV);
513 case ISD::VUDIV: return visitVBinOp(N, ISD::UDIV, ISD::UDIV);
514 case ISD::VAND: return visitVBinOp(N, ISD::AND , ISD::AND);
515 case ISD::VOR: return visitVBinOp(N, ISD::OR , ISD::OR);
516 case ISD::VXOR: return visitVBinOp(N, ISD::XOR , ISD::XOR);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000517 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000518 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000519}
520
Nate Begeman83e75ec2005-09-06 04:43:02 +0000521SDOperand DAGCombiner::visitTokenFactor(SDNode *N) {
Jim Laskey6ff23e52006-10-04 16:53:27 +0000522 SmallVector<SDNode *, 8> TFs; // List of token factors to visit.
Jim Laskey279f0532006-09-25 16:29:54 +0000523 SmallVector<SDOperand, 8> Ops; // Ops for replacing token factor.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000524 bool Changed = false; // If we should replace this token factor.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000525
526 // Start out with this token factor.
Jim Laskey279f0532006-09-25 16:29:54 +0000527 TFs.push_back(N);
Jim Laskey279f0532006-09-25 16:29:54 +0000528
Jim Laskey71382342006-10-07 23:37:56 +0000529 // Iterate through token factors. The TFs grows when new token factors are
Jim Laskeybc588b82006-10-05 15:07:25 +0000530 // encountered.
531 for (unsigned i = 0; i < TFs.size(); ++i) {
532 SDNode *TF = TFs[i];
533
Jim Laskey6ff23e52006-10-04 16:53:27 +0000534 // Check each of the operands.
535 for (unsigned i = 0, ie = TF->getNumOperands(); i != ie; ++i) {
536 SDOperand Op = TF->getOperand(i);
Jim Laskey279f0532006-09-25 16:29:54 +0000537
Jim Laskey6ff23e52006-10-04 16:53:27 +0000538 switch (Op.getOpcode()) {
539 case ISD::EntryToken:
Jim Laskeybc588b82006-10-05 15:07:25 +0000540 // Entry tokens don't need to be added to the list. They are
541 // rededundant.
542 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000543 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000544
Jim Laskey6ff23e52006-10-04 16:53:27 +0000545 case ISD::TokenFactor:
Jim Laskeybc588b82006-10-05 15:07:25 +0000546 if ((CombinerAA || Op.hasOneUse()) &&
547 std::find(TFs.begin(), TFs.end(), Op.Val) == TFs.end()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +0000548 // Queue up for processing.
549 TFs.push_back(Op.Val);
550 // Clean up in case the token factor is removed.
551 AddToWorkList(Op.Val);
552 Changed = true;
553 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000554 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000555 // Fall thru
556
557 default:
Jim Laskeybc588b82006-10-05 15:07:25 +0000558 // Only add if not there prior.
559 if (std::find(Ops.begin(), Ops.end(), Op) == Ops.end())
560 Ops.push_back(Op);
Jim Laskey6ff23e52006-10-04 16:53:27 +0000561 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000562 }
563 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000564 }
565
566 SDOperand Result;
567
568 // If we've change things around then replace token factor.
569 if (Changed) {
570 if (Ops.size() == 0) {
571 // The entry token is the only possible outcome.
572 Result = DAG.getEntryNode();
573 } else {
574 // New and improved token factor.
575 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0], Ops.size());
Nate Begemanded49632005-10-13 03:11:28 +0000576 }
577 }
Jim Laskey279f0532006-09-25 16:29:54 +0000578
Jim Laskey6ff23e52006-10-04 16:53:27 +0000579 return Result;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000580}
581
Nate Begeman83e75ec2005-09-06 04:43:02 +0000582SDOperand DAGCombiner::visitADD(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000583 SDOperand N0 = N->getOperand(0);
584 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000585 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
586 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemanf89d78d2005-09-07 16:09:19 +0000587 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000588
589 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000590 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000591 return DAG.getNode(ISD::ADD, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000592 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000593 if (N0C && !N1C)
594 return DAG.getNode(ISD::ADD, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000595 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000596 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000597 return N0;
Chris Lattner4aafb4f2006-01-12 20:22:43 +0000598 // fold ((c1-A)+c2) -> (c1+c2)-A
599 if (N1C && N0.getOpcode() == ISD::SUB)
600 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
601 return DAG.getNode(ISD::SUB, VT,
602 DAG.getConstant(N1C->getValue()+N0C->getValue(), VT),
603 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000604 // reassociate add
605 SDOperand RADD = ReassociateOps(ISD::ADD, N0, N1);
606 if (RADD.Val != 0)
607 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000608 // fold ((0-A) + B) -> B-A
609 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
610 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000611 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000612 // fold (A + (0-B)) -> A-B
613 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
614 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000615 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +0000616 // fold (A+(B-A)) -> B
617 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000618 return N1.getOperand(0);
Chris Lattner947c2892006-03-13 06:51:27 +0000619
Evan Cheng860771d2006-03-01 01:09:54 +0000620 if (!MVT::isVector(VT) && SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +0000621 return SDOperand(N, 0);
Chris Lattner947c2892006-03-13 06:51:27 +0000622
623 // fold (a+b) -> (a|b) iff a and b share no bits.
624 if (MVT::isInteger(VT) && !MVT::isVector(VT)) {
625 uint64_t LHSZero, LHSOne;
626 uint64_t RHSZero, RHSOne;
627 uint64_t Mask = MVT::getIntVTBitMask(VT);
628 TLI.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
629 if (LHSZero) {
630 TLI.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
631
632 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
633 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
634 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
635 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
636 return DAG.getNode(ISD::OR, VT, N0, N1);
637 }
638 }
639
Nate Begeman83e75ec2005-09-06 04:43:02 +0000640 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000641}
642
Nate Begeman83e75ec2005-09-06 04:43:02 +0000643SDOperand DAGCombiner::visitSUB(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000644 SDOperand N0 = N->getOperand(0);
645 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000646 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
647 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000648 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000649
Chris Lattner854077d2005-10-17 01:07:11 +0000650 // fold (sub x, x) -> 0
651 if (N0 == N1)
652 return DAG.getConstant(0, N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000653 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000654 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000655 return DAG.getNode(ISD::SUB, VT, N0, N1);
Chris Lattner05b57432005-10-11 06:07:15 +0000656 // fold (sub x, c) -> (add x, -c)
657 if (N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000658 return DAG.getNode(ISD::ADD, VT, N0, DAG.getConstant(-N1C->getValue(), VT));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000659 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +0000660 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000661 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000662 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +0000663 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000664 return N0.getOperand(0);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000665 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000666}
667
Nate Begeman83e75ec2005-09-06 04:43:02 +0000668SDOperand DAGCombiner::visitMUL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000669 SDOperand N0 = N->getOperand(0);
670 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000671 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
672 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman223df222005-09-08 20:18:10 +0000673 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000674
675 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000676 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000677 return DAG.getNode(ISD::MUL, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000678 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000679 if (N0C && !N1C)
680 return DAG.getNode(ISD::MUL, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000681 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000682 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000683 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000684 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +0000685 if (N1C && N1C->isAllOnesValue())
Nate Begeman405e3ec2005-10-21 00:02:42 +0000686 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000687 // fold (mul x, (1 << c)) -> x << c
Nate Begeman646d7e22005-09-02 21:18:40 +0000688 if (N1C && isPowerOf2_64(N1C->getValue()))
Chris Lattner3e6099b2005-10-30 06:41:49 +0000689 return DAG.getNode(ISD::SHL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000690 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000691 TLI.getShiftAmountTy()));
Chris Lattner3e6099b2005-10-30 06:41:49 +0000692 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
693 if (N1C && isPowerOf2_64(-N1C->getSignExtended())) {
694 // FIXME: If the input is something that is easily negated (e.g. a
695 // single-use add), we should put the negate there.
696 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT),
697 DAG.getNode(ISD::SHL, VT, N0,
698 DAG.getConstant(Log2_64(-N1C->getSignExtended()),
699 TLI.getShiftAmountTy())));
700 }
Andrew Lenharth50a0d422006-04-02 21:42:45 +0000701
Chris Lattner0b1a85f2006-03-01 03:44:24 +0000702 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
703 if (N1C && N0.getOpcode() == ISD::SHL &&
704 isa<ConstantSDNode>(N0.getOperand(1))) {
705 SDOperand C3 = DAG.getNode(ISD::SHL, VT, N1, N0.getOperand(1));
Chris Lattner5750df92006-03-01 04:03:14 +0000706 AddToWorkList(C3.Val);
Chris Lattner0b1a85f2006-03-01 03:44:24 +0000707 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0), C3);
708 }
709
710 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
711 // use.
712 {
713 SDOperand Sh(0,0), Y(0,0);
714 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
715 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
716 N0.Val->hasOneUse()) {
717 Sh = N0; Y = N1;
718 } else if (N1.getOpcode() == ISD::SHL &&
719 isa<ConstantSDNode>(N1.getOperand(1)) && N1.Val->hasOneUse()) {
720 Sh = N1; Y = N0;
721 }
722 if (Sh.Val) {
723 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Sh.getOperand(0), Y);
724 return DAG.getNode(ISD::SHL, VT, Mul, Sh.getOperand(1));
725 }
726 }
Chris Lattnera1deca32006-03-04 23:33:26 +0000727 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
728 if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() &&
729 isa<ConstantSDNode>(N0.getOperand(1))) {
730 return DAG.getNode(ISD::ADD, VT,
731 DAG.getNode(ISD::MUL, VT, N0.getOperand(0), N1),
732 DAG.getNode(ISD::MUL, VT, N0.getOperand(1), N1));
733 }
Chris Lattner0b1a85f2006-03-01 03:44:24 +0000734
Nate Begemancd4d58c2006-02-03 06:46:56 +0000735 // reassociate mul
736 SDOperand RMUL = ReassociateOps(ISD::MUL, N0, N1);
737 if (RMUL.Val != 0)
738 return RMUL;
Nate Begeman83e75ec2005-09-06 04:43:02 +0000739 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000740}
741
Nate Begeman83e75ec2005-09-06 04:43:02 +0000742SDOperand DAGCombiner::visitSDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000743 SDOperand N0 = N->getOperand(0);
744 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000745 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
746 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000747 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000748
749 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000750 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000751 return DAG.getNode(ISD::SDIV, VT, N0, N1);
Nate Begeman405e3ec2005-10-21 00:02:42 +0000752 // fold (sdiv X, 1) -> X
753 if (N1C && N1C->getSignExtended() == 1LL)
754 return N0;
755 // fold (sdiv X, -1) -> 0-X
756 if (N1C && N1C->isAllOnesValue())
757 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +0000758 // If we know the sign bits of both operands are zero, strength reduce to a
759 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
760 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000761 if (TLI.MaskedValueIsZero(N1, SignBit) &&
762 TLI.MaskedValueIsZero(N0, SignBit))
Chris Lattner094c8fc2005-10-07 06:10:46 +0000763 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
Nate Begemancd6a6ed2006-02-17 07:26:20 +0000764 // fold (sdiv X, pow2) -> simple ops after legalize
Nate Begemanfb7217b2006-02-17 19:54:08 +0000765 if (N1C && N1C->getValue() && !TLI.isIntDivCheap() &&
Nate Begeman405e3ec2005-10-21 00:02:42 +0000766 (isPowerOf2_64(N1C->getSignExtended()) ||
767 isPowerOf2_64(-N1C->getSignExtended()))) {
768 // If dividing by powers of two is cheap, then don't perform the following
769 // fold.
770 if (TLI.isPow2DivCheap())
771 return SDOperand();
772 int64_t pow2 = N1C->getSignExtended();
773 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
Chris Lattner8f4880b2006-02-16 08:02:36 +0000774 unsigned lg2 = Log2_64(abs2);
775 // Splat the sign bit into the register
776 SDOperand SGN = DAG.getNode(ISD::SRA, VT, N0,
Nate Begeman405e3ec2005-10-21 00:02:42 +0000777 DAG.getConstant(MVT::getSizeInBits(VT)-1,
778 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +0000779 AddToWorkList(SGN.Val);
Chris Lattner8f4880b2006-02-16 08:02:36 +0000780 // Add (N0 < 0) ? abs2 - 1 : 0;
781 SDOperand SRL = DAG.getNode(ISD::SRL, VT, SGN,
782 DAG.getConstant(MVT::getSizeInBits(VT)-lg2,
Nate Begeman405e3ec2005-10-21 00:02:42 +0000783 TLI.getShiftAmountTy()));
Chris Lattner8f4880b2006-02-16 08:02:36 +0000784 SDOperand ADD = DAG.getNode(ISD::ADD, VT, N0, SRL);
Chris Lattner5750df92006-03-01 04:03:14 +0000785 AddToWorkList(SRL.Val);
786 AddToWorkList(ADD.Val); // Divide by pow2
Chris Lattner8f4880b2006-02-16 08:02:36 +0000787 SDOperand SRA = DAG.getNode(ISD::SRA, VT, ADD,
788 DAG.getConstant(lg2, TLI.getShiftAmountTy()));
Nate Begeman405e3ec2005-10-21 00:02:42 +0000789 // If we're dividing by a positive value, we're done. Otherwise, we must
790 // negate the result.
791 if (pow2 > 0)
792 return SRA;
Chris Lattner5750df92006-03-01 04:03:14 +0000793 AddToWorkList(SRA.Val);
Nate Begeman405e3ec2005-10-21 00:02:42 +0000794 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), SRA);
795 }
Nate Begeman69575232005-10-20 02:15:44 +0000796 // if integer divide is expensive and we satisfy the requirements, emit an
797 // alternate sequence.
Nate Begeman405e3ec2005-10-21 00:02:42 +0000798 if (N1C && (N1C->getSignExtended() < -1 || N1C->getSignExtended() > 1) &&
Chris Lattnere9936d12005-10-22 18:50:15 +0000799 !TLI.isIntDivCheap()) {
800 SDOperand Op = BuildSDIV(N);
801 if (Op.Val) return Op;
Nate Begeman69575232005-10-20 02:15:44 +0000802 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000803 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000804}
805
Nate Begeman83e75ec2005-09-06 04:43:02 +0000806SDOperand DAGCombiner::visitUDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000807 SDOperand N0 = N->getOperand(0);
808 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000809 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
810 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000811 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000812
813 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000814 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000815 return DAG.getNode(ISD::UDIV, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000816 // fold (udiv x, (1 << c)) -> x >>u c
Nate Begeman646d7e22005-09-02 21:18:40 +0000817 if (N1C && isPowerOf2_64(N1C->getValue()))
Nate Begemanfb5e4bd2006-02-05 07:20:23 +0000818 return DAG.getNode(ISD::SRL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000819 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000820 TLI.getShiftAmountTy()));
Nate Begemanfb5e4bd2006-02-05 07:20:23 +0000821 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
822 if (N1.getOpcode() == ISD::SHL) {
823 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
824 if (isPowerOf2_64(SHC->getValue())) {
825 MVT::ValueType ADDVT = N1.getOperand(1).getValueType();
Nate Begemanc031e332006-02-05 07:36:48 +0000826 SDOperand Add = DAG.getNode(ISD::ADD, ADDVT, N1.getOperand(1),
827 DAG.getConstant(Log2_64(SHC->getValue()),
828 ADDVT));
Chris Lattner5750df92006-03-01 04:03:14 +0000829 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +0000830 return DAG.getNode(ISD::SRL, VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +0000831 }
832 }
833 }
Nate Begeman69575232005-10-20 02:15:44 +0000834 // fold (udiv x, c) -> alternate
Chris Lattnere9936d12005-10-22 18:50:15 +0000835 if (N1C && N1C->getValue() && !TLI.isIntDivCheap()) {
836 SDOperand Op = BuildUDIV(N);
837 if (Op.Val) return Op;
838 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000839 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000840}
841
Nate Begeman83e75ec2005-09-06 04:43:02 +0000842SDOperand DAGCombiner::visitSREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000843 SDOperand N0 = N->getOperand(0);
844 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000845 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
846 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +0000847 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000848
849 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000850 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000851 return DAG.getNode(ISD::SREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +0000852 // If we know the sign bits of both operands are zero, strength reduce to a
853 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
854 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000855 if (TLI.MaskedValueIsZero(N1, SignBit) &&
856 TLI.MaskedValueIsZero(N0, SignBit))
Nate Begemana148d982006-01-18 22:35:16 +0000857 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000858 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000859}
860
Nate Begeman83e75ec2005-09-06 04:43:02 +0000861SDOperand DAGCombiner::visitUREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000862 SDOperand N0 = N->getOperand(0);
863 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000864 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
865 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +0000866 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000867
868 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000869 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000870 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +0000871 // fold (urem x, pow2) -> (and x, pow2-1)
872 if (N1C && !N1C->isNullValue() && isPowerOf2_64(N1C->getValue()))
Nate Begemana148d982006-01-18 22:35:16 +0000873 return DAG.getNode(ISD::AND, VT, N0, DAG.getConstant(N1C->getValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +0000874 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
875 if (N1.getOpcode() == ISD::SHL) {
876 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
877 if (isPowerOf2_64(SHC->getValue())) {
Nate Begemanbab92392006-02-05 08:07:24 +0000878 SDOperand Add = DAG.getNode(ISD::ADD, VT, N1,DAG.getConstant(~0ULL,VT));
Chris Lattner5750df92006-03-01 04:03:14 +0000879 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +0000880 return DAG.getNode(ISD::AND, VT, N0, Add);
881 }
882 }
883 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000884 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000885}
886
Nate Begeman83e75ec2005-09-06 04:43:02 +0000887SDOperand DAGCombiner::visitMULHS(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000888 SDOperand N0 = N->getOperand(0);
889 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000890 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000891
892 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000893 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000894 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000895 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Nate Begeman646d7e22005-09-02 21:18:40 +0000896 if (N1C && N1C->getValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +0000897 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
898 DAG.getConstant(MVT::getSizeInBits(N0.getValueType())-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +0000899 TLI.getShiftAmountTy()));
900 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000901}
902
Nate Begeman83e75ec2005-09-06 04:43:02 +0000903SDOperand DAGCombiner::visitMULHU(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000904 SDOperand N0 = N->getOperand(0);
905 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000906 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000907
908 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000909 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000910 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000911 // fold (mulhu x, 1) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000912 if (N1C && N1C->getValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000913 return DAG.getConstant(0, N0.getValueType());
914 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000915}
916
Chris Lattner35e5c142006-05-05 05:51:50 +0000917/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
918/// two operands of the same opcode, try to simplify it.
919SDOperand DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
920 SDOperand N0 = N->getOperand(0), N1 = N->getOperand(1);
921 MVT::ValueType VT = N0.getValueType();
922 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
923
Chris Lattner540121f2006-05-05 06:31:05 +0000924 // For each of OP in AND/OR/XOR:
925 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
926 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
927 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
Chris Lattner0d8dae72006-05-05 06:32:04 +0000928 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y))
Chris Lattner540121f2006-05-05 06:31:05 +0000929 if ((N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND||
Chris Lattner0d8dae72006-05-05 06:32:04 +0000930 N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::TRUNCATE) &&
Chris Lattner35e5c142006-05-05 05:51:50 +0000931 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
932 SDOperand ORNode = DAG.getNode(N->getOpcode(),
933 N0.getOperand(0).getValueType(),
934 N0.getOperand(0), N1.getOperand(0));
935 AddToWorkList(ORNode.Val);
Chris Lattner540121f2006-05-05 06:31:05 +0000936 return DAG.getNode(N0.getOpcode(), VT, ORNode);
Chris Lattner35e5c142006-05-05 05:51:50 +0000937 }
938
Chris Lattnera3dc3f62006-05-05 06:10:43 +0000939 // For each of OP in SHL/SRL/SRA/AND...
940 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
941 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
942 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
Chris Lattner35e5c142006-05-05 05:51:50 +0000943 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
Chris Lattnera3dc3f62006-05-05 06:10:43 +0000944 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
Chris Lattner35e5c142006-05-05 05:51:50 +0000945 N0.getOperand(1) == N1.getOperand(1)) {
946 SDOperand ORNode = DAG.getNode(N->getOpcode(),
947 N0.getOperand(0).getValueType(),
948 N0.getOperand(0), N1.getOperand(0));
949 AddToWorkList(ORNode.Val);
950 return DAG.getNode(N0.getOpcode(), VT, ORNode, N0.getOperand(1));
951 }
952
953 return SDOperand();
954}
955
Nate Begeman83e75ec2005-09-06 04:43:02 +0000956SDOperand DAGCombiner::visitAND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000957 SDOperand N0 = N->getOperand(0);
958 SDOperand N1 = N->getOperand(1);
Nate Begemanfb7217b2006-02-17 19:54:08 +0000959 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +0000960 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
961 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000962 MVT::ValueType VT = N1.getValueType();
Nate Begeman83e75ec2005-09-06 04:43:02 +0000963 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000964
965 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000966 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000967 return DAG.getNode(ISD::AND, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000968 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000969 if (N0C && !N1C)
970 return DAG.getNode(ISD::AND, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000971 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000972 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000973 return N0;
974 // if (and x, c) is known to be zero, return 0
Nate Begeman368e18d2006-02-16 21:11:51 +0000975 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000976 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000977 // reassociate and
978 SDOperand RAND = ReassociateOps(ISD::AND, N0, N1);
979 if (RAND.Val != 0)
980 return RAND;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000981 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
Nate Begeman5dc7e862005-11-02 18:42:59 +0000982 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +0000983 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Nate Begeman646d7e22005-09-02 21:18:40 +0000984 if ((ORI->getValue() & N1C->getValue()) == N1C->getValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000985 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +0000986 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
987 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Chris Lattner1ec05d12006-03-01 21:47:21 +0000988 unsigned InMask = MVT::getIntVTBitMask(N0.getOperand(0).getValueType());
Chris Lattner3603cd62006-02-02 07:17:31 +0000989 if (TLI.MaskedValueIsZero(N0.getOperand(0),
Chris Lattner1ec05d12006-03-01 21:47:21 +0000990 ~N1C->getValue() & InMask)) {
991 SDOperand Zext = DAG.getNode(ISD::ZERO_EXTEND, N0.getValueType(),
992 N0.getOperand(0));
993
994 // Replace uses of the AND with uses of the Zero extend node.
995 CombineTo(N, Zext);
996
Chris Lattner3603cd62006-02-02 07:17:31 +0000997 // We actually want to replace all uses of the any_extend with the
998 // zero_extend, to avoid duplicating things. This will later cause this
999 // AND to be folded.
Chris Lattner1ec05d12006-03-01 21:47:21 +00001000 CombineTo(N0.Val, Zext);
Chris Lattnerfedced72006-04-20 23:55:59 +00001001 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner3603cd62006-02-02 07:17:31 +00001002 }
1003 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001004 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
1005 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1006 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1007 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1008
1009 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1010 MVT::isInteger(LL.getValueType())) {
1011 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
1012 if (cast<ConstantSDNode>(LR)->getValue() == 0 && Op1 == ISD::SETEQ) {
1013 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001014 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001015 return DAG.getSetCC(VT, ORNode, LR, Op1);
1016 }
1017 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
1018 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
1019 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001020 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001021 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1022 }
1023 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
1024 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
1025 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001026 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001027 return DAG.getSetCC(VT, ORNode, LR, Op1);
1028 }
1029 }
1030 // canonicalize equivalent to ll == rl
1031 if (LL == RR && LR == RL) {
1032 Op1 = ISD::getSetCCSwappedOperands(Op1);
1033 std::swap(RL, RR);
1034 }
1035 if (LL == RL && LR == RR) {
1036 bool isInteger = MVT::isInteger(LL.getValueType());
1037 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
1038 if (Result != ISD::SETCC_INVALID)
1039 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1040 }
1041 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001042
1043 // Simplify: and (op x...), (op y...) -> (op (and x, y))
1044 if (N0.getOpcode() == N1.getOpcode()) {
1045 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1046 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001047 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001048
Nate Begemande996292006-02-03 22:24:05 +00001049 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
1050 // fold (and (sra)) -> (and (srl)) when possible.
Chris Lattner6ea2dee2006-03-25 22:19:00 +00001051 if (!MVT::isVector(VT) &&
1052 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001053 return SDOperand(N, 0);
Nate Begemanded49632005-10-13 03:11:28 +00001054 // fold (zext_inreg (extload x)) -> (zextload x)
Evan Chengc5484282006-10-04 00:56:09 +00001055 if (ISD::isEXTLoad(N0.Val)) {
Nate Begemanded49632005-10-13 03:11:28 +00001056 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001057 // If we zero all the possible extended bits, then we can turn this into
1058 // a zextload if we are running before legalize or the operation is legal.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001059 if (TLI.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Evan Chengc5484282006-10-04 00:56:09 +00001060 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001061 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1062 N0.getOperand(1), N0.getOperand(2),
1063 EVT);
Chris Lattner5750df92006-03-01 04:03:14 +00001064 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001065 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001066 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001067 }
1068 }
1069 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Evan Chengc5484282006-10-04 00:56:09 +00001070 if (ISD::isSEXTLoad(N0.Val) && N0.hasOneUse()) {
Nate Begemanded49632005-10-13 03:11:28 +00001071 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001072 // If we zero all the possible extended bits, then we can turn this into
1073 // a zextload if we are running before legalize or the operation is legal.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001074 if (TLI.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Evan Chengc5484282006-10-04 00:56:09 +00001075 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001076 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1077 N0.getOperand(1), N0.getOperand(2),
1078 EVT);
Chris Lattner5750df92006-03-01 04:03:14 +00001079 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001080 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001081 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001082 }
1083 }
Chris Lattner15045b62006-02-28 06:35:35 +00001084
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001085 // fold (and (load x), 255) -> (zextload x, i8)
1086 // fold (and (extload x, i16), 255) -> (zextload x, i8)
1087 if (N1C &&
Evan Chengc5484282006-10-04 00:56:09 +00001088 (N0.getOpcode() == ISD::LOAD || ISD::isEXTLoad(N0.Val) ||
1089 ISD::isZEXTLoad(N0.Val)) &&
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001090 N0.hasOneUse()) {
1091 MVT::ValueType EVT, LoadedVT;
Chris Lattner15045b62006-02-28 06:35:35 +00001092 if (N1C->getValue() == 255)
1093 EVT = MVT::i8;
1094 else if (N1C->getValue() == 65535)
1095 EVT = MVT::i16;
1096 else if (N1C->getValue() == ~0U)
1097 EVT = MVT::i32;
1098 else
1099 EVT = MVT::Other;
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001100
1101 LoadedVT = N0.getOpcode() == ISD::LOAD ? VT :
1102 cast<VTSDNode>(N0.getOperand(3))->getVT();
Chris Lattnere44be602006-04-04 17:39:18 +00001103 if (EVT != MVT::Other && LoadedVT > EVT &&
Evan Chengc5484282006-10-04 00:56:09 +00001104 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Chris Lattner15045b62006-02-28 06:35:35 +00001105 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
1106 // For big endian targets, we need to add an offset to the pointer to load
1107 // the correct bytes. For little endian systems, we merely need to read
1108 // fewer bytes from the same pointer.
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001109 unsigned PtrOff =
1110 (MVT::getSizeInBits(LoadedVT) - MVT::getSizeInBits(EVT)) / 8;
1111 SDOperand NewPtr = N0.getOperand(1);
1112 if (!TLI.isLittleEndian())
1113 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
1114 DAG.getConstant(PtrOff, PtrType));
Chris Lattner5750df92006-03-01 04:03:14 +00001115 AddToWorkList(NewPtr.Val);
Chris Lattner15045b62006-02-28 06:35:35 +00001116 SDOperand Load =
1117 DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0), NewPtr,
1118 N0.getOperand(2), EVT);
Chris Lattner5750df92006-03-01 04:03:14 +00001119 AddToWorkList(N);
Chris Lattner15045b62006-02-28 06:35:35 +00001120 CombineTo(N0.Val, Load, Load.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001121 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner15045b62006-02-28 06:35:35 +00001122 }
1123 }
1124
Nate Begeman83e75ec2005-09-06 04:43:02 +00001125 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001126}
1127
Nate Begeman83e75ec2005-09-06 04:43:02 +00001128SDOperand DAGCombiner::visitOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001129 SDOperand N0 = N->getOperand(0);
1130 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001131 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001132 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1133 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001134 MVT::ValueType VT = N1.getValueType();
1135 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001136
1137 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001138 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001139 return DAG.getNode(ISD::OR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001140 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001141 if (N0C && !N1C)
1142 return DAG.getNode(ISD::OR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001143 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001144 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001145 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001146 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001147 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001148 return N1;
1149 // fold (or x, c) -> c iff (x & ~c) == 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001150 if (N1C &&
1151 TLI.MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits))))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001152 return N1;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001153 // reassociate or
1154 SDOperand ROR = ReassociateOps(ISD::OR, N0, N1);
1155 if (ROR.Val != 0)
1156 return ROR;
1157 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
1158 if (N1C && N0.getOpcode() == ISD::AND && N0.Val->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00001159 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00001160 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
1161 return DAG.getNode(ISD::AND, VT, DAG.getNode(ISD::OR, VT, N0.getOperand(0),
1162 N1),
1163 DAG.getConstant(N1C->getValue() | C1->getValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00001164 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001165 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
1166 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1167 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1168 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1169
1170 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1171 MVT::isInteger(LL.getValueType())) {
1172 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
1173 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
1174 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
1175 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
1176 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001177 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001178 return DAG.getSetCC(VT, ORNode, LR, Op1);
1179 }
1180 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
1181 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
1182 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
1183 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
1184 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001185 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001186 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1187 }
1188 }
1189 // canonicalize equivalent to ll == rl
1190 if (LL == RR && LR == RL) {
1191 Op1 = ISD::getSetCCSwappedOperands(Op1);
1192 std::swap(RL, RR);
1193 }
1194 if (LL == RL && LR == RR) {
1195 bool isInteger = MVT::isInteger(LL.getValueType());
1196 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
1197 if (Result != ISD::SETCC_INVALID)
1198 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1199 }
1200 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001201
1202 // Simplify: or (op x...), (op y...) -> (op (or x, y))
1203 if (N0.getOpcode() == N1.getOpcode()) {
1204 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1205 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001206 }
Chris Lattner516b9622006-09-14 20:50:57 +00001207
Chris Lattner1ec72732006-09-14 21:11:37 +00001208 // (X & C1) | (Y & C2) -> (X|Y) & C3 if possible.
1209 if (N0.getOpcode() == ISD::AND &&
1210 N1.getOpcode() == ISD::AND &&
1211 N0.getOperand(1).getOpcode() == ISD::Constant &&
1212 N1.getOperand(1).getOpcode() == ISD::Constant &&
1213 // Don't increase # computations.
1214 (N0.Val->hasOneUse() || N1.Val->hasOneUse())) {
1215 // We can only do this xform if we know that bits from X that are set in C2
1216 // but not in C1 are already zero. Likewise for Y.
1217 uint64_t LHSMask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1218 uint64_t RHSMask = cast<ConstantSDNode>(N1.getOperand(1))->getValue();
1219
1220 if (TLI.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) &&
1221 TLI.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) {
1222 SDOperand X =DAG.getNode(ISD::OR, VT, N0.getOperand(0), N1.getOperand(0));
1223 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(LHSMask|RHSMask, VT));
1224 }
1225 }
1226
1227
Chris Lattner516b9622006-09-14 20:50:57 +00001228 // See if this is some rotate idiom.
1229 if (SDNode *Rot = MatchRotate(N0, N1))
1230 return SDOperand(Rot, 0);
Chris Lattner35e5c142006-05-05 05:51:50 +00001231
Nate Begeman83e75ec2005-09-06 04:43:02 +00001232 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001233}
1234
Chris Lattner516b9622006-09-14 20:50:57 +00001235
1236/// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present.
1237static bool MatchRotateHalf(SDOperand Op, SDOperand &Shift, SDOperand &Mask) {
1238 if (Op.getOpcode() == ISD::AND) {
1239 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1240 Mask = Op.getOperand(1);
1241 Op = Op.getOperand(0);
1242 } else {
1243 return false;
1244 }
1245 }
1246
1247 if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) {
1248 Shift = Op;
1249 return true;
1250 }
1251 return false;
1252}
1253
1254
1255// MatchRotate - Handle an 'or' of two operands. If this is one of the many
1256// idioms for rotate, and if the target supports rotation instructions, generate
1257// a rot[lr].
1258SDNode *DAGCombiner::MatchRotate(SDOperand LHS, SDOperand RHS) {
1259 // Must be a legal type. Expanded an promoted things won't work with rotates.
1260 MVT::ValueType VT = LHS.getValueType();
1261 if (!TLI.isTypeLegal(VT)) return 0;
1262
1263 // The target must have at least one rotate flavor.
1264 bool HasROTL = TLI.isOperationLegal(ISD::ROTL, VT);
1265 bool HasROTR = TLI.isOperationLegal(ISD::ROTR, VT);
1266 if (!HasROTL && !HasROTR) return 0;
1267
1268 // Match "(X shl/srl V1) & V2" where V2 may not be present.
1269 SDOperand LHSShift; // The shift.
1270 SDOperand LHSMask; // AND value if any.
1271 if (!MatchRotateHalf(LHS, LHSShift, LHSMask))
1272 return 0; // Not part of a rotate.
1273
1274 SDOperand RHSShift; // The shift.
1275 SDOperand RHSMask; // AND value if any.
1276 if (!MatchRotateHalf(RHS, RHSShift, RHSMask))
1277 return 0; // Not part of a rotate.
1278
1279 if (LHSShift.getOperand(0) != RHSShift.getOperand(0))
1280 return 0; // Not shifting the same value.
1281
1282 if (LHSShift.getOpcode() == RHSShift.getOpcode())
1283 return 0; // Shifts must disagree.
1284
1285 // Canonicalize shl to left side in a shl/srl pair.
1286 if (RHSShift.getOpcode() == ISD::SHL) {
1287 std::swap(LHS, RHS);
1288 std::swap(LHSShift, RHSShift);
1289 std::swap(LHSMask , RHSMask );
1290 }
1291
1292 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1293
1294 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
1295 // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2)
1296 if (LHSShift.getOperand(1).getOpcode() == ISD::Constant &&
1297 RHSShift.getOperand(1).getOpcode() == ISD::Constant) {
1298 uint64_t LShVal = cast<ConstantSDNode>(LHSShift.getOperand(1))->getValue();
1299 uint64_t RShVal = cast<ConstantSDNode>(RHSShift.getOperand(1))->getValue();
1300 if ((LShVal + RShVal) != OpSizeInBits)
1301 return 0;
1302
1303 SDOperand Rot;
1304 if (HasROTL)
1305 Rot = DAG.getNode(ISD::ROTL, VT, LHSShift.getOperand(0),
1306 LHSShift.getOperand(1));
1307 else
1308 Rot = DAG.getNode(ISD::ROTR, VT, LHSShift.getOperand(0),
1309 RHSShift.getOperand(1));
1310
1311 // If there is an AND of either shifted operand, apply it to the result.
1312 if (LHSMask.Val || RHSMask.Val) {
1313 uint64_t Mask = MVT::getIntVTBitMask(VT);
1314
1315 if (LHSMask.Val) {
1316 uint64_t RHSBits = (1ULL << LShVal)-1;
1317 Mask &= cast<ConstantSDNode>(LHSMask)->getValue() | RHSBits;
1318 }
1319 if (RHSMask.Val) {
1320 uint64_t LHSBits = ~((1ULL << (OpSizeInBits-RShVal))-1);
1321 Mask &= cast<ConstantSDNode>(RHSMask)->getValue() | LHSBits;
1322 }
1323
1324 Rot = DAG.getNode(ISD::AND, VT, Rot, DAG.getConstant(Mask, VT));
1325 }
1326
1327 return Rot.Val;
1328 }
1329
1330 // If there is a mask here, and we have a variable shift, we can't be sure
1331 // that we're masking out the right stuff.
1332 if (LHSMask.Val || RHSMask.Val)
1333 return 0;
1334
1335 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
1336 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotr x, (sub 32, y))
1337 if (RHSShift.getOperand(1).getOpcode() == ISD::SUB &&
1338 LHSShift.getOperand(1) == RHSShift.getOperand(1).getOperand(1)) {
1339 if (ConstantSDNode *SUBC =
1340 dyn_cast<ConstantSDNode>(RHSShift.getOperand(1).getOperand(0))) {
1341 if (SUBC->getValue() == OpSizeInBits)
1342 if (HasROTL)
1343 return DAG.getNode(ISD::ROTL, VT, LHSShift.getOperand(0),
1344 LHSShift.getOperand(1)).Val;
1345 else
1346 return DAG.getNode(ISD::ROTR, VT, LHSShift.getOperand(0),
1347 LHSShift.getOperand(1)).Val;
1348 }
1349 }
1350
1351 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
1352 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotl x, (sub 32, y))
1353 if (LHSShift.getOperand(1).getOpcode() == ISD::SUB &&
1354 RHSShift.getOperand(1) == LHSShift.getOperand(1).getOperand(1)) {
1355 if (ConstantSDNode *SUBC =
1356 dyn_cast<ConstantSDNode>(LHSShift.getOperand(1).getOperand(0))) {
1357 if (SUBC->getValue() == OpSizeInBits)
1358 if (HasROTL)
1359 return DAG.getNode(ISD::ROTL, VT, LHSShift.getOperand(0),
1360 LHSShift.getOperand(1)).Val;
1361 else
1362 return DAG.getNode(ISD::ROTR, VT, LHSShift.getOperand(0),
1363 RHSShift.getOperand(1)).Val;
1364 }
1365 }
1366
1367 return 0;
1368}
1369
1370
Nate Begeman83e75ec2005-09-06 04:43:02 +00001371SDOperand DAGCombiner::visitXOR(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 SDOperand LHS, RHS, CC;
1375 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1376 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001377 MVT::ValueType VT = N0.getValueType();
1378
1379 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001380 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001381 return DAG.getNode(ISD::XOR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001382 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001383 if (N0C && !N1C)
1384 return DAG.getNode(ISD::XOR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001385 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001386 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001387 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001388 // reassociate xor
1389 SDOperand RXOR = ReassociateOps(ISD::XOR, N0, N1);
1390 if (RXOR.Val != 0)
1391 return RXOR;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001392 // fold !(x cc y) -> (x !cc y)
Nate Begeman646d7e22005-09-02 21:18:40 +00001393 if (N1C && N1C->getValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
1394 bool isInt = MVT::isInteger(LHS.getValueType());
1395 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
1396 isInt);
1397 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001398 return DAG.getSetCC(VT, LHS, RHS, NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00001399 if (N0.getOpcode() == ISD::SELECT_CC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001400 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00001401 assert(0 && "Unhandled SetCC Equivalent!");
1402 abort();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001403 }
Nate Begeman99801192005-09-07 23:25:52 +00001404 // fold !(x or y) -> (!x and !y) iff x or y are setcc
1405 if (N1C && N1C->getValue() == 1 &&
1406 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001407 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00001408 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
1409 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001410 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1411 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00001412 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00001413 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001414 }
1415 }
Nate Begeman99801192005-09-07 23:25:52 +00001416 // fold !(x or y) -> (!x and !y) iff x or y are constants
1417 if (N1C && N1C->isAllOnesValue() &&
1418 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001419 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00001420 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
1421 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001422 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1423 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00001424 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00001425 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001426 }
1427 }
Nate Begeman223df222005-09-08 20:18:10 +00001428 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
1429 if (N1C && N0.getOpcode() == ISD::XOR) {
1430 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
1431 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
1432 if (N00C)
1433 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
1434 DAG.getConstant(N1C->getValue()^N00C->getValue(), VT));
1435 if (N01C)
1436 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
1437 DAG.getConstant(N1C->getValue()^N01C->getValue(), VT));
1438 }
1439 // fold (xor x, x) -> 0
Chris Lattner4fbdd592006-03-28 19:11:05 +00001440 if (N0 == N1) {
1441 if (!MVT::isVector(VT)) {
1442 return DAG.getConstant(0, VT);
1443 } else if (!AfterLegalize || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) {
1444 // Produce a vector of zeros.
1445 SDOperand El = DAG.getConstant(0, MVT::getVectorBaseType(VT));
1446 std::vector<SDOperand> Ops(MVT::getVectorNumElements(VT), El);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001447 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4fbdd592006-03-28 19:11:05 +00001448 }
1449 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001450
1451 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
1452 if (N0.getOpcode() == N1.getOpcode()) {
1453 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1454 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001455 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001456
Chris Lattner3e104b12006-04-08 04:15:24 +00001457 // Simplify the expression using non-local knowledge.
1458 if (!MVT::isVector(VT) &&
1459 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001460 return SDOperand(N, 0);
Chris Lattner3e104b12006-04-08 04:15:24 +00001461
Nate Begeman83e75ec2005-09-06 04:43:02 +00001462 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001463}
1464
Nate Begeman83e75ec2005-09-06 04:43:02 +00001465SDOperand DAGCombiner::visitSHL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001466 SDOperand N0 = N->getOperand(0);
1467 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001468 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1469 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001470 MVT::ValueType VT = N0.getValueType();
1471 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1472
1473 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001474 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001475 return DAG.getNode(ISD::SHL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001476 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001477 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001478 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001479 // fold (shl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001480 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001481 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001482 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001483 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001484 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001485 // if (shl x, c) is known to be zero, return 0
Nate Begemanfb7217b2006-02-17 19:54:08 +00001486 if (TLI.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001487 return DAG.getConstant(0, VT);
Chris Lattner012f2412006-02-17 21:58:01 +00001488 if (SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001489 return SDOperand(N, 0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001490 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001491 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001492 N0.getOperand(1).getOpcode() == ISD::Constant) {
1493 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001494 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001495 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001496 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001497 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001498 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001499 }
1500 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
1501 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001502 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001503 N0.getOperand(1).getOpcode() == ISD::Constant) {
1504 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001505 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001506 SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
1507 DAG.getConstant(~0ULL << c1, VT));
1508 if (c2 > c1)
1509 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001510 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001511 else
Nate Begeman83e75ec2005-09-06 04:43:02 +00001512 return DAG.getNode(ISD::SRL, VT, Mask,
1513 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001514 }
1515 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001516 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +00001517 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001518 DAG.getConstant(~0ULL << N1C->getValue(), VT));
Chris Lattnercac70592006-03-05 19:53:55 +00001519 // fold (shl (add x, c1), c2) -> (add (shl x, c2), c1<<c2)
1520 if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() &&
1521 isa<ConstantSDNode>(N0.getOperand(1))) {
1522 return DAG.getNode(ISD::ADD, VT,
1523 DAG.getNode(ISD::SHL, VT, N0.getOperand(0), N1),
1524 DAG.getNode(ISD::SHL, VT, N0.getOperand(1), N1));
1525 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001526 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001527}
1528
Nate Begeman83e75ec2005-09-06 04:43:02 +00001529SDOperand DAGCombiner::visitSRA(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001530 SDOperand N0 = N->getOperand(0);
1531 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001532 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1533 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001534 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001535
1536 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001537 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001538 return DAG.getNode(ISD::SRA, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001539 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001540 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001541 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001542 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001543 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001544 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001545 // fold (sra x, c >= size(x)) -> undef
Nate Begemanfb7217b2006-02-17 19:54:08 +00001546 if (N1C && N1C->getValue() >= MVT::getSizeInBits(VT))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001547 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001548 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001549 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001550 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00001551 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
1552 // sext_inreg.
1553 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
1554 unsigned LowBits = MVT::getSizeInBits(VT) - (unsigned)N1C->getValue();
1555 MVT::ValueType EVT;
1556 switch (LowBits) {
1557 default: EVT = MVT::Other; break;
1558 case 1: EVT = MVT::i1; break;
1559 case 8: EVT = MVT::i8; break;
1560 case 16: EVT = MVT::i16; break;
1561 case 32: EVT = MVT::i32; break;
1562 }
1563 if (EVT > MVT::Other && TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, EVT))
1564 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
1565 DAG.getValueType(EVT));
1566 }
Chris Lattner71d9ebc2006-02-28 06:23:04 +00001567
1568 // fold (sra (sra x, c1), c2) -> (sra x, c1+c2)
1569 if (N1C && N0.getOpcode() == ISD::SRA) {
1570 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
1571 unsigned Sum = N1C->getValue() + C1->getValue();
1572 if (Sum >= MVT::getSizeInBits(VT)) Sum = MVT::getSizeInBits(VT)-1;
1573 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0),
1574 DAG.getConstant(Sum, N1C->getValueType(0)));
1575 }
1576 }
1577
Chris Lattnera8504462006-05-08 20:51:54 +00001578 // Simplify, based on bits shifted out of the LHS.
1579 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
1580 return SDOperand(N, 0);
1581
1582
Nate Begeman1d4d4142005-09-01 00:19:25 +00001583 // If the sign bit is known to be zero, switch this to a SRL.
Nate Begemanfb7217b2006-02-17 19:54:08 +00001584 if (TLI.MaskedValueIsZero(N0, MVT::getIntVTSignBit(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001585 return DAG.getNode(ISD::SRL, VT, N0, N1);
1586 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001587}
1588
Nate Begeman83e75ec2005-09-06 04:43:02 +00001589SDOperand DAGCombiner::visitSRL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001590 SDOperand N0 = N->getOperand(0);
1591 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001592 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1593 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001594 MVT::ValueType VT = N0.getValueType();
1595 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1596
1597 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001598 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001599 return DAG.getNode(ISD::SRL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001600 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001601 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001602 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001603 // fold (srl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001604 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001605 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001606 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001607 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001608 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001609 // if (srl x, c) is known to be zero, return 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001610 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001611 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001612 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001613 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001614 N0.getOperand(1).getOpcode() == ISD::Constant) {
1615 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001616 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001617 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001618 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001619 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001620 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001621 }
Chris Lattner350bec02006-04-02 06:11:11 +00001622
Chris Lattner06afe072006-05-05 22:53:17 +00001623 // fold (srl (anyextend x), c) -> (anyextend (srl x, c))
1624 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
1625 // Shifting in all undef bits?
1626 MVT::ValueType SmallVT = N0.getOperand(0).getValueType();
1627 if (N1C->getValue() >= MVT::getSizeInBits(SmallVT))
1628 return DAG.getNode(ISD::UNDEF, VT);
1629
1630 SDOperand SmallShift = DAG.getNode(ISD::SRL, SmallVT, N0.getOperand(0), N1);
1631 AddToWorkList(SmallShift.Val);
1632 return DAG.getNode(ISD::ANY_EXTEND, VT, SmallShift);
1633 }
1634
Chris Lattner350bec02006-04-02 06:11:11 +00001635 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
1636 if (N1C && N0.getOpcode() == ISD::CTLZ &&
1637 N1C->getValue() == Log2_32(MVT::getSizeInBits(VT))) {
1638 uint64_t KnownZero, KnownOne, Mask = MVT::getIntVTBitMask(VT);
1639 TLI.ComputeMaskedBits(N0.getOperand(0), Mask, KnownZero, KnownOne);
1640
1641 // If any of the input bits are KnownOne, then the input couldn't be all
1642 // zeros, thus the result of the srl will always be zero.
1643 if (KnownOne) return DAG.getConstant(0, VT);
1644
1645 // If all of the bits input the to ctlz node are known to be zero, then
1646 // the result of the ctlz is "32" and the result of the shift is one.
1647 uint64_t UnknownBits = ~KnownZero & Mask;
1648 if (UnknownBits == 0) return DAG.getConstant(1, VT);
1649
1650 // Otherwise, check to see if there is exactly one bit input to the ctlz.
1651 if ((UnknownBits & (UnknownBits-1)) == 0) {
1652 // Okay, we know that only that the single bit specified by UnknownBits
1653 // could be set on input to the CTLZ node. If this bit is set, the SRL
1654 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
1655 // to an SRL,XOR pair, which is likely to simplify more.
1656 unsigned ShAmt = CountTrailingZeros_64(UnknownBits);
1657 SDOperand Op = N0.getOperand(0);
1658 if (ShAmt) {
1659 Op = DAG.getNode(ISD::SRL, VT, Op,
1660 DAG.getConstant(ShAmt, TLI.getShiftAmountTy()));
1661 AddToWorkList(Op.Val);
1662 }
1663 return DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(1, VT));
1664 }
1665 }
1666
Nate Begeman83e75ec2005-09-06 04:43:02 +00001667 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001668}
1669
Nate Begeman83e75ec2005-09-06 04:43:02 +00001670SDOperand DAGCombiner::visitCTLZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001671 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00001672 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001673
1674 // fold (ctlz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00001675 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00001676 return DAG.getNode(ISD::CTLZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001677 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001678}
1679
Nate Begeman83e75ec2005-09-06 04:43:02 +00001680SDOperand DAGCombiner::visitCTTZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001681 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00001682 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001683
1684 // fold (cttz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00001685 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00001686 return DAG.getNode(ISD::CTTZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001687 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001688}
1689
Nate Begeman83e75ec2005-09-06 04:43:02 +00001690SDOperand DAGCombiner::visitCTPOP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001691 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00001692 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001693
1694 // fold (ctpop c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00001695 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00001696 return DAG.getNode(ISD::CTPOP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001697 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001698}
1699
Nate Begeman452d7be2005-09-16 00:54:12 +00001700SDOperand DAGCombiner::visitSELECT(SDNode *N) {
1701 SDOperand N0 = N->getOperand(0);
1702 SDOperand N1 = N->getOperand(1);
1703 SDOperand N2 = N->getOperand(2);
1704 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1705 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1706 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1707 MVT::ValueType VT = N->getValueType(0);
Nate Begeman44728a72005-09-19 22:34:01 +00001708
Nate Begeman452d7be2005-09-16 00:54:12 +00001709 // fold select C, X, X -> X
1710 if (N1 == N2)
1711 return N1;
1712 // fold select true, X, Y -> X
1713 if (N0C && !N0C->isNullValue())
1714 return N1;
1715 // fold select false, X, Y -> Y
1716 if (N0C && N0C->isNullValue())
1717 return N2;
1718 // fold select C, 1, X -> C | X
Nate Begeman44728a72005-09-19 22:34:01 +00001719 if (MVT::i1 == VT && N1C && N1C->getValue() == 1)
Nate Begeman452d7be2005-09-16 00:54:12 +00001720 return DAG.getNode(ISD::OR, VT, N0, N2);
1721 // fold select C, 0, X -> ~C & X
1722 // FIXME: this should check for C type == X type, not i1?
1723 if (MVT::i1 == VT && N1C && N1C->isNullValue()) {
1724 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00001725 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00001726 return DAG.getNode(ISD::AND, VT, XORNode, N2);
1727 }
1728 // fold select C, X, 1 -> ~C | X
Nate Begeman44728a72005-09-19 22:34:01 +00001729 if (MVT::i1 == VT && N2C && N2C->getValue() == 1) {
Nate Begeman452d7be2005-09-16 00:54:12 +00001730 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00001731 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00001732 return DAG.getNode(ISD::OR, VT, XORNode, N1);
1733 }
1734 // fold select C, X, 0 -> C & X
1735 // FIXME: this should check for C type == X type, not i1?
1736 if (MVT::i1 == VT && N2C && N2C->isNullValue())
1737 return DAG.getNode(ISD::AND, VT, N0, N1);
1738 // fold X ? X : Y --> X ? 1 : Y --> X | Y
1739 if (MVT::i1 == VT && N0 == N1)
1740 return DAG.getNode(ISD::OR, VT, N0, N2);
1741 // fold X ? Y : X --> X ? Y : 0 --> X & Y
1742 if (MVT::i1 == VT && N0 == N2)
1743 return DAG.getNode(ISD::AND, VT, N0, N1);
Chris Lattner729c6d12006-05-27 00:43:02 +00001744
Chris Lattner40c62d52005-10-18 06:04:22 +00001745 // If we can fold this based on the true/false value, do so.
1746 if (SimplifySelectOps(N, N1, N2))
Chris Lattner729c6d12006-05-27 00:43:02 +00001747 return SDOperand(N, 0); // Don't revisit N.
1748
Nate Begeman44728a72005-09-19 22:34:01 +00001749 // fold selects based on a setcc into other things, such as min/max/abs
1750 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman750ac1b2006-02-01 07:19:44 +00001751 // FIXME:
1752 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
1753 // having to say they don't support SELECT_CC on every type the DAG knows
1754 // about, since there is no way to mark an opcode illegal at all value types
1755 if (TLI.isOperationLegal(ISD::SELECT_CC, MVT::Other))
1756 return DAG.getNode(ISD::SELECT_CC, VT, N0.getOperand(0), N0.getOperand(1),
1757 N1, N2, N0.getOperand(2));
1758 else
1759 return SimplifySelect(N0, N1, N2);
Nate Begeman452d7be2005-09-16 00:54:12 +00001760 return SDOperand();
1761}
1762
1763SDOperand DAGCombiner::visitSELECT_CC(SDNode *N) {
Nate Begeman44728a72005-09-19 22:34:01 +00001764 SDOperand N0 = N->getOperand(0);
1765 SDOperand N1 = N->getOperand(1);
1766 SDOperand N2 = N->getOperand(2);
1767 SDOperand N3 = N->getOperand(3);
1768 SDOperand N4 = N->getOperand(4);
1769 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1770 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1771 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1772 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
1773
Nate Begeman44728a72005-09-19 22:34:01 +00001774 // fold select_cc lhs, rhs, x, x, cc -> x
1775 if (N2 == N3)
1776 return N2;
Chris Lattner40c62d52005-10-18 06:04:22 +00001777
Chris Lattner5f42a242006-09-20 06:19:26 +00001778 // Determine if the condition we're dealing with is constant
1779 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
1780
1781 if (ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val)) {
1782 if (SCCC->getValue())
1783 return N2; // cond always true -> true val
1784 else
1785 return N3; // cond always false -> false val
1786 }
1787
1788 // Fold to a simpler select_cc
1789 if (SCC.Val && SCC.getOpcode() == ISD::SETCC)
1790 return DAG.getNode(ISD::SELECT_CC, N2.getValueType(),
1791 SCC.getOperand(0), SCC.getOperand(1), N2, N3,
1792 SCC.getOperand(2));
1793
Chris Lattner40c62d52005-10-18 06:04:22 +00001794 // If we can fold this based on the true/false value, do so.
1795 if (SimplifySelectOps(N, N2, N3))
Chris Lattner729c6d12006-05-27 00:43:02 +00001796 return SDOperand(N, 0); // Don't revisit N.
Chris Lattner40c62d52005-10-18 06:04:22 +00001797
Nate Begeman44728a72005-09-19 22:34:01 +00001798 // fold select_cc into other things, such as min/max/abs
1799 return SimplifySelectCC(N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00001800}
1801
1802SDOperand DAGCombiner::visitSETCC(SDNode *N) {
1803 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
1804 cast<CondCodeSDNode>(N->getOperand(2))->get());
1805}
1806
Nate Begeman83e75ec2005-09-06 04:43:02 +00001807SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001808 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001809 MVT::ValueType VT = N->getValueType(0);
1810
Nate Begeman1d4d4142005-09-01 00:19:25 +00001811 // fold (sext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00001812 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00001813 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0);
Chris Lattner310b5782006-05-06 23:06:26 +00001814
Nate Begeman1d4d4142005-09-01 00:19:25 +00001815 // fold (sext (sext x)) -> (sext x)
Chris Lattner310b5782006-05-06 23:06:26 +00001816 // fold (sext (aext x)) -> (sext x)
1817 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001818 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
Chris Lattner310b5782006-05-06 23:06:26 +00001819
Chris Lattner6007b842006-09-21 06:00:20 +00001820 // fold (sext (truncate x)) -> (sextinreg x).
1821 if (N0.getOpcode() == ISD::TRUNCATE &&
Chris Lattnerbf370872006-09-21 06:17:39 +00001822 (!AfterLegalize || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG,
1823 N0.getValueType()))) {
Chris Lattner6007b842006-09-21 06:00:20 +00001824 SDOperand Op = N0.getOperand(0);
1825 if (Op.getValueType() < VT) {
1826 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
1827 } else if (Op.getValueType() > VT) {
1828 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
1829 }
1830 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, Op,
Chris Lattnerb14ab8a2005-12-07 07:11:03 +00001831 DAG.getValueType(N0.getValueType()));
Chris Lattner6007b842006-09-21 06:00:20 +00001832 }
Chris Lattner310b5782006-05-06 23:06:26 +00001833
Evan Cheng110dec22005-12-14 02:19:23 +00001834 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Chris Lattnerd0f6d182005-12-15 19:02:38 +00001835 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00001836 (!AfterLegalize||TLI.isLoadXLegal(ISD::SEXTLOAD, N0.getValueType()))){
Nate Begeman3df4d522005-10-12 20:40:40 +00001837 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1838 N0.getOperand(1), N0.getOperand(2),
1839 N0.getValueType());
Chris Lattnerd4771842005-12-14 19:25:30 +00001840 CombineTo(N, ExtLoad);
Chris Lattnerf9884052005-10-13 21:52:31 +00001841 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1842 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001843 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begeman3df4d522005-10-12 20:40:40 +00001844 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001845
1846 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
1847 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
Evan Chengc5484282006-10-04 00:56:09 +00001848 if ((ISD::isSEXTLoad(N0.Val) || ISD::isEXTLoad(N0.Val)) && N0.hasOneUse()) {
Nate Begeman5c742682006-05-08 01:35:01 +00001849 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
1850 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1851 N0.getOperand(1), N0.getOperand(2), EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00001852 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001853 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1854 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001855 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001856 }
1857
Nate Begeman83e75ec2005-09-06 04:43:02 +00001858 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001859}
1860
Nate Begeman83e75ec2005-09-06 04:43:02 +00001861SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001862 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001863 MVT::ValueType VT = N->getValueType(0);
1864
Nate Begeman1d4d4142005-09-01 00:19:25 +00001865 // fold (zext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00001866 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00001867 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001868 // fold (zext (zext x)) -> (zext x)
Chris Lattner310b5782006-05-06 23:06:26 +00001869 // fold (zext (aext x)) -> (zext x)
1870 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001871 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
Chris Lattner6007b842006-09-21 06:00:20 +00001872
1873 // fold (zext (truncate x)) -> (and x, mask)
1874 if (N0.getOpcode() == ISD::TRUNCATE &&
1875 (!AfterLegalize || TLI.isOperationLegal(ISD::AND, VT))) {
1876 SDOperand Op = N0.getOperand(0);
1877 if (Op.getValueType() < VT) {
1878 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
1879 } else if (Op.getValueType() > VT) {
1880 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
1881 }
1882 return DAG.getZeroExtendInReg(Op, N0.getValueType());
1883 }
1884
Chris Lattner111c2282006-09-21 06:14:31 +00001885 // fold (zext (and (trunc x), cst)) -> (and x, cst).
1886 if (N0.getOpcode() == ISD::AND &&
1887 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
1888 N0.getOperand(1).getOpcode() == ISD::Constant) {
1889 SDOperand X = N0.getOperand(0).getOperand(0);
1890 if (X.getValueType() < VT) {
1891 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
1892 } else if (X.getValueType() > VT) {
1893 X = DAG.getNode(ISD::TRUNCATE, VT, X);
1894 }
1895 uint64_t Mask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1896 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
1897 }
1898
Evan Cheng110dec22005-12-14 02:19:23 +00001899 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Chris Lattnerd0f6d182005-12-15 19:02:38 +00001900 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00001901 (!AfterLegalize||TLI.isLoadXLegal(ISD::ZEXTLOAD, N0.getValueType()))) {
Evan Cheng110dec22005-12-14 02:19:23 +00001902 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1903 N0.getOperand(1), N0.getOperand(2),
1904 N0.getValueType());
Chris Lattnerd4771842005-12-14 19:25:30 +00001905 CombineTo(N, ExtLoad);
Evan Cheng110dec22005-12-14 02:19:23 +00001906 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1907 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001908 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng110dec22005-12-14 02:19:23 +00001909 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001910
1911 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
1912 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
Evan Chengc5484282006-10-04 00:56:09 +00001913 if ((ISD::isZEXTLoad(N0.Val) || ISD::isEXTLoad(N0.Val)) && N0.hasOneUse()) {
Nate Begeman5c742682006-05-08 01:35:01 +00001914 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
1915 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1916 N0.getOperand(1), N0.getOperand(2), EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00001917 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001918 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1919 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001920 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001921 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001922 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001923}
1924
Chris Lattner5ffc0662006-05-05 05:58:59 +00001925SDOperand DAGCombiner::visitANY_EXTEND(SDNode *N) {
1926 SDOperand N0 = N->getOperand(0);
Chris Lattner5ffc0662006-05-05 05:58:59 +00001927 MVT::ValueType VT = N->getValueType(0);
1928
1929 // fold (aext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00001930 if (isa<ConstantSDNode>(N0))
Chris Lattner5ffc0662006-05-05 05:58:59 +00001931 return DAG.getNode(ISD::ANY_EXTEND, VT, N0);
1932 // fold (aext (aext x)) -> (aext x)
1933 // fold (aext (zext x)) -> (zext x)
1934 // fold (aext (sext x)) -> (sext x)
1935 if (N0.getOpcode() == ISD::ANY_EXTEND ||
1936 N0.getOpcode() == ISD::ZERO_EXTEND ||
1937 N0.getOpcode() == ISD::SIGN_EXTEND)
1938 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
1939
Chris Lattner84750582006-09-20 06:29:17 +00001940 // fold (aext (truncate x))
1941 if (N0.getOpcode() == ISD::TRUNCATE) {
1942 SDOperand TruncOp = N0.getOperand(0);
1943 if (TruncOp.getValueType() == VT)
1944 return TruncOp; // x iff x size == zext size.
1945 if (TruncOp.getValueType() > VT)
1946 return DAG.getNode(ISD::TRUNCATE, VT, TruncOp);
1947 return DAG.getNode(ISD::ANY_EXTEND, VT, TruncOp);
1948 }
Chris Lattner0e4b9222006-09-21 06:40:43 +00001949
1950 // fold (aext (and (trunc x), cst)) -> (and x, cst).
1951 if (N0.getOpcode() == ISD::AND &&
1952 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
1953 N0.getOperand(1).getOpcode() == ISD::Constant) {
1954 SDOperand X = N0.getOperand(0).getOperand(0);
1955 if (X.getValueType() < VT) {
1956 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
1957 } else if (X.getValueType() > VT) {
1958 X = DAG.getNode(ISD::TRUNCATE, VT, X);
1959 }
1960 uint64_t Mask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1961 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
1962 }
1963
Chris Lattner5ffc0662006-05-05 05:58:59 +00001964 // fold (aext (load x)) -> (aext (truncate (extload x)))
1965 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00001966 (!AfterLegalize||TLI.isLoadXLegal(ISD::EXTLOAD, N0.getValueType()))) {
Chris Lattner5ffc0662006-05-05 05:58:59 +00001967 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, N0.getOperand(0),
1968 N0.getOperand(1), N0.getOperand(2),
1969 N0.getValueType());
1970 CombineTo(N, ExtLoad);
1971 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1972 ExtLoad.getValue(1));
1973 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1974 }
1975
1976 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
1977 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
1978 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
Evan Chengc5484282006-10-04 00:56:09 +00001979 if (N0.getOpcode() == ISD::LOADX && N0.hasOneUse()) {
Nate Begeman5c742682006-05-08 01:35:01 +00001980 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
Evan Chengc5484282006-10-04 00:56:09 +00001981 unsigned LType = N0.getConstantOperandVal(4);
1982 SDOperand ExtLoad = DAG.getExtLoad((ISD::LoadExtType)LType, VT,
1983 N0.getOperand(0), N0.getOperand(1),
1984 N0.getOperand(2), EVT);
Chris Lattner5ffc0662006-05-05 05:58:59 +00001985 CombineTo(N, ExtLoad);
1986 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1987 ExtLoad.getValue(1));
1988 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1989 }
1990 return SDOperand();
1991}
1992
1993
Nate Begeman83e75ec2005-09-06 04:43:02 +00001994SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001995 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001996 SDOperand N1 = N->getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001997 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001998 MVT::ValueType EVT = cast<VTSDNode>(N1)->getVT();
Nate Begeman07ed4172005-10-10 21:26:48 +00001999 unsigned EVTBits = MVT::getSizeInBits(EVT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002000
Nate Begeman1d4d4142005-09-01 00:19:25 +00002001 // fold (sext_in_reg c1) -> c1
Chris Lattnereaeda562006-05-08 20:59:41 +00002002 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
Chris Lattner310b5782006-05-06 23:06:26 +00002003 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0, N1);
Chris Lattneree4ea922006-05-06 09:30:03 +00002004
Chris Lattner541a24f2006-05-06 22:43:44 +00002005 // If the input is already sign extended, just drop the extension.
Chris Lattneree4ea922006-05-06 09:30:03 +00002006 if (TLI.ComputeNumSignBits(N0) >= MVT::getSizeInBits(VT)-EVTBits+1)
2007 return N0;
2008
Nate Begeman646d7e22005-09-02 21:18:40 +00002009 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
2010 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
2011 EVT < cast<VTSDNode>(N0.getOperand(1))->getVT()) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00002012 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002013 }
Chris Lattner4b37e872006-05-08 21:18:59 +00002014
Nate Begeman07ed4172005-10-10 21:26:48 +00002015 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is zero
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00002016 if (TLI.MaskedValueIsZero(N0, 1ULL << (EVTBits-1)))
Nate Begemande996292006-02-03 22:24:05 +00002017 return DAG.getZeroExtendInReg(N0, EVT);
Chris Lattner4b37e872006-05-08 21:18:59 +00002018
2019 // fold (sext_in_reg (srl X, 24), i8) -> sra X, 24
2020 // fold (sext_in_reg (srl X, 23), i8) -> sra X, 23 iff possible.
2021 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
2022 if (N0.getOpcode() == ISD::SRL) {
2023 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
2024 if (ShAmt->getValue()+EVTBits <= MVT::getSizeInBits(VT)) {
2025 // We can turn this into an SRA iff the input to the SRL is already sign
2026 // extended enough.
2027 unsigned InSignBits = TLI.ComputeNumSignBits(N0.getOperand(0));
2028 if (MVT::getSizeInBits(VT)-(ShAmt->getValue()+EVTBits) < InSignBits)
2029 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0), N0.getOperand(1));
2030 }
2031 }
2032
Nate Begemanded49632005-10-13 03:11:28 +00002033 // fold (sext_inreg (extload x)) -> (sextload x)
Evan Chengc5484282006-10-04 00:56:09 +00002034 if (ISD::isEXTLoad(N0.Val) &&
Nate Begemanded49632005-10-13 03:11:28 +00002035 EVT == cast<VTSDNode>(N0.getOperand(3))->getVT() &&
Evan Chengc5484282006-10-04 00:56:09 +00002036 (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00002037 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
2038 N0.getOperand(1), N0.getOperand(2),
2039 EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00002040 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00002041 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002042 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00002043 }
2044 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Evan Chengc5484282006-10-04 00:56:09 +00002045 if (ISD::isZEXTLoad(N0.Val) && N0.hasOneUse() &&
Nate Begemanded49632005-10-13 03:11:28 +00002046 EVT == cast<VTSDNode>(N0.getOperand(3))->getVT() &&
Evan Chengc5484282006-10-04 00:56:09 +00002047 (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00002048 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
2049 N0.getOperand(1), N0.getOperand(2),
2050 EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00002051 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00002052 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002053 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00002054 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00002055 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002056}
2057
Nate Begeman83e75ec2005-09-06 04:43:02 +00002058SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002059 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002060 MVT::ValueType VT = N->getValueType(0);
2061
2062 // noop truncate
2063 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002064 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002065 // fold (truncate c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00002066 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002067 return DAG.getNode(ISD::TRUNCATE, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002068 // fold (truncate (truncate x)) -> (truncate x)
2069 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002070 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002071 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
Chris Lattnerb72773b2006-05-05 22:56:26 +00002072 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND||
2073 N0.getOpcode() == ISD::ANY_EXTEND) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002074 if (N0.getValueType() < VT)
2075 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00002076 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002077 else if (N0.getValueType() > VT)
2078 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00002079 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002080 else
2081 // if the source and dest are the same type, we can drop both the extend
2082 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00002083 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002084 }
Nate Begeman3df4d522005-10-12 20:40:40 +00002085 // fold (truncate (load x)) -> (smaller load x)
Chris Lattner40c62d52005-10-18 06:04:22 +00002086 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse()) {
Nate Begeman3df4d522005-10-12 20:40:40 +00002087 assert(MVT::getSizeInBits(N0.getValueType()) > MVT::getSizeInBits(VT) &&
2088 "Cannot truncate to larger type!");
2089 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
Nate Begeman765784a2005-10-12 23:18:53 +00002090 // For big endian targets, we need to add an offset to the pointer to load
2091 // the correct bytes. For little endian systems, we merely need to read
2092 // fewer bytes from the same pointer.
Nate Begeman3df4d522005-10-12 20:40:40 +00002093 uint64_t PtrOff =
2094 (MVT::getSizeInBits(N0.getValueType()) - MVT::getSizeInBits(VT)) / 8;
Nate Begeman765784a2005-10-12 23:18:53 +00002095 SDOperand NewPtr = TLI.isLittleEndian() ? N0.getOperand(1) :
2096 DAG.getNode(ISD::ADD, PtrType, N0.getOperand(1),
2097 DAG.getConstant(PtrOff, PtrType));
Chris Lattner5750df92006-03-01 04:03:14 +00002098 AddToWorkList(NewPtr.Val);
Nate Begeman3df4d522005-10-12 20:40:40 +00002099 SDOperand Load = DAG.getLoad(VT, N0.getOperand(0), NewPtr,N0.getOperand(2));
Chris Lattner5750df92006-03-01 04:03:14 +00002100 AddToWorkList(N);
Chris Lattner24edbb72005-10-13 22:10:05 +00002101 CombineTo(N0.Val, Load, Load.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002102 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begeman3df4d522005-10-12 20:40:40 +00002103 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00002104 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002105}
2106
Chris Lattner94683772005-12-23 05:30:37 +00002107SDOperand DAGCombiner::visitBIT_CONVERT(SDNode *N) {
2108 SDOperand N0 = N->getOperand(0);
2109 MVT::ValueType VT = N->getValueType(0);
2110
2111 // If the input is a constant, let getNode() fold it.
2112 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
2113 SDOperand Res = DAG.getNode(ISD::BIT_CONVERT, VT, N0);
2114 if (Res.Val != N) return Res;
2115 }
2116
Chris Lattnerc8547d82005-12-23 05:37:50 +00002117 if (N0.getOpcode() == ISD::BIT_CONVERT) // conv(conv(x,t1),t2) -> conv(x,t2)
2118 return DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
Chris Lattner6258fb22006-04-02 02:53:43 +00002119
Chris Lattner57104102005-12-23 05:44:41 +00002120 // fold (conv (load x)) -> (load (conv*)x)
Chris Lattnerbf40c4b2006-01-15 18:58:59 +00002121 // FIXME: These xforms need to know that the resultant load doesn't need a
2122 // higher alignment than the original!
2123 if (0 && N0.getOpcode() == ISD::LOAD && N0.hasOneUse()) {
Chris Lattner57104102005-12-23 05:44:41 +00002124 SDOperand Load = DAG.getLoad(VT, N0.getOperand(0), N0.getOperand(1),
2125 N0.getOperand(2));
Chris Lattner5750df92006-03-01 04:03:14 +00002126 AddToWorkList(N);
Chris Lattner57104102005-12-23 05:44:41 +00002127 CombineTo(N0.Val, DAG.getNode(ISD::BIT_CONVERT, N0.getValueType(), Load),
2128 Load.getValue(1));
2129 return Load;
2130 }
2131
Chris Lattner94683772005-12-23 05:30:37 +00002132 return SDOperand();
2133}
2134
Chris Lattner6258fb22006-04-02 02:53:43 +00002135SDOperand DAGCombiner::visitVBIT_CONVERT(SDNode *N) {
2136 SDOperand N0 = N->getOperand(0);
2137 MVT::ValueType VT = N->getValueType(0);
2138
2139 // If the input is a VBUILD_VECTOR with all constant elements, fold this now.
2140 // First check to see if this is all constant.
2141 if (N0.getOpcode() == ISD::VBUILD_VECTOR && N0.Val->hasOneUse() &&
2142 VT == MVT::Vector) {
2143 bool isSimple = true;
2144 for (unsigned i = 0, e = N0.getNumOperands()-2; i != e; ++i)
2145 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
2146 N0.getOperand(i).getOpcode() != ISD::Constant &&
2147 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
2148 isSimple = false;
2149 break;
2150 }
2151
Chris Lattner97c20732006-04-03 17:29:28 +00002152 MVT::ValueType DestEltVT = cast<VTSDNode>(N->getOperand(2))->getVT();
2153 if (isSimple && !MVT::isVector(DestEltVT)) {
Chris Lattner6258fb22006-04-02 02:53:43 +00002154 return ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(N0.Val, DestEltVT);
2155 }
2156 }
2157
2158 return SDOperand();
2159}
2160
2161/// ConstantFoldVBIT_CONVERTofVBUILD_VECTOR - We know that BV is a vbuild_vector
2162/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
2163/// destination element value type.
2164SDOperand DAGCombiner::
2165ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(SDNode *BV, MVT::ValueType DstEltVT) {
2166 MVT::ValueType SrcEltVT = BV->getOperand(0).getValueType();
2167
2168 // If this is already the right type, we're done.
2169 if (SrcEltVT == DstEltVT) return SDOperand(BV, 0);
2170
2171 unsigned SrcBitSize = MVT::getSizeInBits(SrcEltVT);
2172 unsigned DstBitSize = MVT::getSizeInBits(DstEltVT);
2173
2174 // If this is a conversion of N elements of one type to N elements of another
2175 // type, convert each element. This handles FP<->INT cases.
2176 if (SrcBitSize == DstBitSize) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002177 SmallVector<SDOperand, 8> Ops;
Chris Lattner3e104b12006-04-08 04:15:24 +00002178 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00002179 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, DstEltVT, BV->getOperand(i)));
Chris Lattner3e104b12006-04-08 04:15:24 +00002180 AddToWorkList(Ops.back().Val);
2181 }
Chris Lattner6258fb22006-04-02 02:53:43 +00002182 Ops.push_back(*(BV->op_end()-2)); // Add num elements.
2183 Ops.push_back(DAG.getValueType(DstEltVT));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002184 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00002185 }
2186
2187 // Otherwise, we're growing or shrinking the elements. To avoid having to
2188 // handle annoying details of growing/shrinking FP values, we convert them to
2189 // int first.
2190 if (MVT::isFloatingPoint(SrcEltVT)) {
2191 // Convert the input float vector to a int vector where the elements are the
2192 // same sizes.
2193 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
2194 MVT::ValueType IntVT = SrcEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
2195 BV = ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(BV, IntVT).Val;
2196 SrcEltVT = IntVT;
2197 }
2198
2199 // Now we know the input is an integer vector. If the output is a FP type,
2200 // convert to integer first, then to FP of the right size.
2201 if (MVT::isFloatingPoint(DstEltVT)) {
2202 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
2203 MVT::ValueType TmpVT = DstEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
2204 SDNode *Tmp = ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(BV, TmpVT).Val;
2205
2206 // Next, convert to FP elements of the same size.
2207 return ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(Tmp, DstEltVT);
2208 }
2209
2210 // Okay, we know the src/dst types are both integers of differing types.
2211 // Handling growing first.
2212 assert(MVT::isInteger(SrcEltVT) && MVT::isInteger(DstEltVT));
2213 if (SrcBitSize < DstBitSize) {
2214 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
2215
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002216 SmallVector<SDOperand, 8> Ops;
Chris Lattner6258fb22006-04-02 02:53:43 +00002217 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e;
2218 i += NumInputsPerOutput) {
2219 bool isLE = TLI.isLittleEndian();
2220 uint64_t NewBits = 0;
2221 bool EltIsUndef = true;
2222 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
2223 // Shift the previously computed bits over.
2224 NewBits <<= SrcBitSize;
2225 SDOperand Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
2226 if (Op.getOpcode() == ISD::UNDEF) continue;
2227 EltIsUndef = false;
2228
2229 NewBits |= cast<ConstantSDNode>(Op)->getValue();
2230 }
2231
2232 if (EltIsUndef)
2233 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
2234 else
2235 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
2236 }
2237
2238 Ops.push_back(DAG.getConstant(Ops.size(), MVT::i32)); // Add num elements.
2239 Ops.push_back(DAG.getValueType(DstEltVT)); // Add element size.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002240 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00002241 }
2242
2243 // Finally, this must be the case where we are shrinking elements: each input
2244 // turns into multiple outputs.
2245 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002246 SmallVector<SDOperand, 8> Ops;
Chris Lattner6258fb22006-04-02 02:53:43 +00002247 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e; ++i) {
2248 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
2249 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
2250 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
2251 continue;
2252 }
2253 uint64_t OpVal = cast<ConstantSDNode>(BV->getOperand(i))->getValue();
2254
2255 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
2256 unsigned ThisVal = OpVal & ((1ULL << DstBitSize)-1);
2257 OpVal >>= DstBitSize;
2258 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
2259 }
2260
2261 // For big endian targets, swap the order of the pieces of each element.
2262 if (!TLI.isLittleEndian())
2263 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
2264 }
2265 Ops.push_back(DAG.getConstant(Ops.size(), MVT::i32)); // Add num elements.
2266 Ops.push_back(DAG.getValueType(DstEltVT)); // Add element size.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002267 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00002268}
2269
2270
2271
Chris Lattner01b3d732005-09-28 22:28:18 +00002272SDOperand DAGCombiner::visitFADD(SDNode *N) {
2273 SDOperand N0 = N->getOperand(0);
2274 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00002275 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2276 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002277 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00002278
2279 // fold (fadd c1, c2) -> c1+c2
2280 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002281 return DAG.getNode(ISD::FADD, VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00002282 // canonicalize constant to RHS
2283 if (N0CFP && !N1CFP)
2284 return DAG.getNode(ISD::FADD, VT, N1, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00002285 // fold (A + (-B)) -> A-B
2286 if (N1.getOpcode() == ISD::FNEG)
2287 return DAG.getNode(ISD::FSUB, VT, N0, N1.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00002288 // fold ((-A) + B) -> B-A
2289 if (N0.getOpcode() == ISD::FNEG)
2290 return DAG.getNode(ISD::FSUB, VT, N1, N0.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00002291 return SDOperand();
2292}
2293
2294SDOperand DAGCombiner::visitFSUB(SDNode *N) {
2295 SDOperand N0 = N->getOperand(0);
2296 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00002297 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2298 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002299 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00002300
2301 // fold (fsub c1, c2) -> c1-c2
2302 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002303 return DAG.getNode(ISD::FSUB, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002304 // fold (A-(-B)) -> A+B
2305 if (N1.getOpcode() == ISD::FNEG)
Nate Begemana148d982006-01-18 22:35:16 +00002306 return DAG.getNode(ISD::FADD, VT, N0, N1.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00002307 return SDOperand();
2308}
2309
2310SDOperand DAGCombiner::visitFMUL(SDNode *N) {
2311 SDOperand N0 = N->getOperand(0);
2312 SDOperand N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00002313 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2314 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002315 MVT::ValueType VT = N->getValueType(0);
2316
Nate Begeman11af4ea2005-10-17 20:40:11 +00002317 // fold (fmul c1, c2) -> c1*c2
2318 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002319 return DAG.getNode(ISD::FMUL, VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00002320 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00002321 if (N0CFP && !N1CFP)
2322 return DAG.getNode(ISD::FMUL, VT, N1, N0);
Nate Begeman11af4ea2005-10-17 20:40:11 +00002323 // fold (fmul X, 2.0) -> (fadd X, X)
2324 if (N1CFP && N1CFP->isExactlyValue(+2.0))
2325 return DAG.getNode(ISD::FADD, VT, N0, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00002326 return SDOperand();
2327}
2328
2329SDOperand DAGCombiner::visitFDIV(SDNode *N) {
2330 SDOperand N0 = N->getOperand(0);
2331 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00002332 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2333 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002334 MVT::ValueType VT = N->getValueType(0);
2335
Nate Begemana148d982006-01-18 22:35:16 +00002336 // fold (fdiv c1, c2) -> c1/c2
2337 if (N0CFP && N1CFP)
2338 return DAG.getNode(ISD::FDIV, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002339 return SDOperand();
2340}
2341
2342SDOperand DAGCombiner::visitFREM(SDNode *N) {
2343 SDOperand N0 = N->getOperand(0);
2344 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00002345 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2346 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002347 MVT::ValueType VT = N->getValueType(0);
2348
Nate Begemana148d982006-01-18 22:35:16 +00002349 // fold (frem c1, c2) -> fmod(c1,c2)
2350 if (N0CFP && N1CFP)
2351 return DAG.getNode(ISD::FREM, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002352 return SDOperand();
2353}
2354
Chris Lattner12d83032006-03-05 05:30:57 +00002355SDOperand DAGCombiner::visitFCOPYSIGN(SDNode *N) {
2356 SDOperand N0 = N->getOperand(0);
2357 SDOperand N1 = N->getOperand(1);
2358 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2359 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2360 MVT::ValueType VT = N->getValueType(0);
2361
2362 if (N0CFP && N1CFP) // Constant fold
2363 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1);
2364
2365 if (N1CFP) {
2366 // copysign(x, c1) -> fabs(x) iff ispos(c1)
2367 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
2368 union {
2369 double d;
2370 int64_t i;
2371 } u;
2372 u.d = N1CFP->getValue();
2373 if (u.i >= 0)
2374 return DAG.getNode(ISD::FABS, VT, N0);
2375 else
2376 return DAG.getNode(ISD::FNEG, VT, DAG.getNode(ISD::FABS, VT, N0));
2377 }
2378
2379 // copysign(fabs(x), y) -> copysign(x, y)
2380 // copysign(fneg(x), y) -> copysign(x, y)
2381 // copysign(copysign(x,z), y) -> copysign(x, y)
2382 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
2383 N0.getOpcode() == ISD::FCOPYSIGN)
2384 return DAG.getNode(ISD::FCOPYSIGN, VT, N0.getOperand(0), N1);
2385
2386 // copysign(x, abs(y)) -> abs(x)
2387 if (N1.getOpcode() == ISD::FABS)
2388 return DAG.getNode(ISD::FABS, VT, N0);
2389
2390 // copysign(x, copysign(y,z)) -> copysign(x, z)
2391 if (N1.getOpcode() == ISD::FCOPYSIGN)
2392 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(1));
2393
2394 // copysign(x, fp_extend(y)) -> copysign(x, y)
2395 // copysign(x, fp_round(y)) -> copysign(x, y)
2396 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
2397 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(0));
2398
2399 return SDOperand();
2400}
2401
2402
Chris Lattner01b3d732005-09-28 22:28:18 +00002403
Nate Begeman83e75ec2005-09-06 04:43:02 +00002404SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002405 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002406 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00002407 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002408
2409 // fold (sint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002410 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00002411 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002412 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002413}
2414
Nate Begeman83e75ec2005-09-06 04:43:02 +00002415SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002416 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002417 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00002418 MVT::ValueType VT = N->getValueType(0);
2419
Nate Begeman1d4d4142005-09-01 00:19:25 +00002420 // fold (uint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002421 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00002422 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002423 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002424}
2425
Nate Begeman83e75ec2005-09-06 04:43:02 +00002426SDOperand DAGCombiner::visitFP_TO_SINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002427 SDOperand N0 = N->getOperand(0);
2428 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2429 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002430
2431 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00002432 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002433 return DAG.getNode(ISD::FP_TO_SINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002434 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002435}
2436
Nate Begeman83e75ec2005-09-06 04:43:02 +00002437SDOperand DAGCombiner::visitFP_TO_UINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002438 SDOperand N0 = N->getOperand(0);
2439 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2440 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002441
2442 // fold (fp_to_uint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00002443 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002444 return DAG.getNode(ISD::FP_TO_UINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002445 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002446}
2447
Nate Begeman83e75ec2005-09-06 04:43:02 +00002448SDOperand DAGCombiner::visitFP_ROUND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002449 SDOperand N0 = N->getOperand(0);
2450 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2451 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002452
2453 // fold (fp_round c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002454 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002455 return DAG.getNode(ISD::FP_ROUND, VT, N0);
Chris Lattner79dbea52006-03-13 06:26:26 +00002456
2457 // fold (fp_round (fp_extend x)) -> x
2458 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
2459 return N0.getOperand(0);
2460
2461 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
2462 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.Val->hasOneUse()) {
2463 SDOperand Tmp = DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0));
2464 AddToWorkList(Tmp.Val);
2465 return DAG.getNode(ISD::FCOPYSIGN, VT, Tmp, N0.getOperand(1));
2466 }
2467
Nate Begeman83e75ec2005-09-06 04:43:02 +00002468 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002469}
2470
Nate Begeman83e75ec2005-09-06 04:43:02 +00002471SDOperand DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002472 SDOperand N0 = N->getOperand(0);
2473 MVT::ValueType VT = N->getValueType(0);
2474 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00002475 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002476
Nate Begeman1d4d4142005-09-01 00:19:25 +00002477 // fold (fp_round_inreg c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002478 if (N0CFP) {
2479 SDOperand Round = DAG.getConstantFP(N0CFP->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002480 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002481 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00002482 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002483}
2484
Nate Begeman83e75ec2005-09-06 04:43:02 +00002485SDOperand DAGCombiner::visitFP_EXTEND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002486 SDOperand N0 = N->getOperand(0);
2487 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2488 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002489
2490 // fold (fp_extend c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002491 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002492 return DAG.getNode(ISD::FP_EXTEND, VT, N0);
Chris Lattnere564dbb2006-05-05 21:34:35 +00002493
2494 // fold (fpext (load x)) -> (fpext (fpround (extload x)))
2495 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00002496 (!AfterLegalize||TLI.isLoadXLegal(ISD::EXTLOAD, N0.getValueType()))) {
Chris Lattnere564dbb2006-05-05 21:34:35 +00002497 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, N0.getOperand(0),
2498 N0.getOperand(1), N0.getOperand(2),
2499 N0.getValueType());
2500 CombineTo(N, ExtLoad);
2501 CombineTo(N0.Val, DAG.getNode(ISD::FP_ROUND, N0.getValueType(), ExtLoad),
2502 ExtLoad.getValue(1));
2503 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2504 }
2505
2506
Nate Begeman83e75ec2005-09-06 04:43:02 +00002507 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002508}
2509
Nate Begeman83e75ec2005-09-06 04:43:02 +00002510SDOperand DAGCombiner::visitFNEG(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002511 SDOperand N0 = N->getOperand(0);
2512 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2513 MVT::ValueType VT = N->getValueType(0);
2514
2515 // fold (fneg c1) -> -c1
Nate Begeman646d7e22005-09-02 21:18:40 +00002516 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002517 return DAG.getNode(ISD::FNEG, VT, N0);
2518 // fold (fneg (sub x, y)) -> (sub y, x)
Chris Lattner12d83032006-03-05 05:30:57 +00002519 if (N0.getOpcode() == ISD::SUB)
2520 return DAG.getNode(ISD::SUB, VT, N0.getOperand(1), N0.getOperand(0));
Nate Begemana148d982006-01-18 22:35:16 +00002521 // fold (fneg (fneg x)) -> x
Chris Lattner12d83032006-03-05 05:30:57 +00002522 if (N0.getOpcode() == ISD::FNEG)
2523 return N0.getOperand(0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002524 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002525}
2526
Nate Begeman83e75ec2005-09-06 04:43:02 +00002527SDOperand DAGCombiner::visitFABS(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002528 SDOperand N0 = N->getOperand(0);
2529 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2530 MVT::ValueType VT = N->getValueType(0);
2531
Nate Begeman1d4d4142005-09-01 00:19:25 +00002532 // fold (fabs c1) -> fabs(c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00002533 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002534 return DAG.getNode(ISD::FABS, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002535 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00002536 if (N0.getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002537 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002538 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00002539 // fold (fabs (fcopysign x, y)) -> (fabs x)
2540 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
2541 return DAG.getNode(ISD::FABS, VT, N0.getOperand(0));
2542
Nate Begeman83e75ec2005-09-06 04:43:02 +00002543 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002544}
2545
Nate Begeman44728a72005-09-19 22:34:01 +00002546SDOperand DAGCombiner::visitBRCOND(SDNode *N) {
2547 SDOperand Chain = N->getOperand(0);
2548 SDOperand N1 = N->getOperand(1);
2549 SDOperand N2 = N->getOperand(2);
2550 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2551
2552 // never taken branch, fold to chain
2553 if (N1C && N1C->isNullValue())
2554 return Chain;
2555 // unconditional branch
Nate Begemane17daeb2005-10-05 21:43:42 +00002556 if (N1C && N1C->getValue() == 1)
Nate Begeman44728a72005-09-19 22:34:01 +00002557 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
Nate Begeman750ac1b2006-02-01 07:19:44 +00002558 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
2559 // on the target.
2560 if (N1.getOpcode() == ISD::SETCC &&
2561 TLI.isOperationLegal(ISD::BR_CC, MVT::Other)) {
2562 return DAG.getNode(ISD::BR_CC, MVT::Other, Chain, N1.getOperand(2),
2563 N1.getOperand(0), N1.getOperand(1), N2);
2564 }
Nate Begeman44728a72005-09-19 22:34:01 +00002565 return SDOperand();
2566}
2567
Chris Lattner3ea0b472005-10-05 06:47:48 +00002568// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
2569//
Nate Begeman44728a72005-09-19 22:34:01 +00002570SDOperand DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00002571 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
2572 SDOperand CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
2573
2574 // Use SimplifySetCC to simplify SETCC's.
Nate Begemane17daeb2005-10-05 21:43:42 +00002575 SDOperand Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false);
2576 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.Val);
2577
2578 // fold br_cc true, dest -> br dest (unconditional branch)
2579 if (SCCC && SCCC->getValue())
2580 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
2581 N->getOperand(4));
2582 // fold br_cc false, dest -> unconditional fall through
2583 if (SCCC && SCCC->isNullValue())
2584 return N->getOperand(0);
2585 // fold to a simpler setcc
2586 if (Simp.Val && Simp.getOpcode() == ISD::SETCC)
2587 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
2588 Simp.getOperand(2), Simp.getOperand(0),
2589 Simp.getOperand(1), N->getOperand(4));
Nate Begeman44728a72005-09-19 22:34:01 +00002590 return SDOperand();
2591}
2592
Chris Lattner01a22022005-10-10 22:04:48 +00002593SDOperand DAGCombiner::visitLOAD(SDNode *N) {
2594 SDOperand Chain = N->getOperand(0);
2595 SDOperand Ptr = N->getOperand(1);
2596 SDOperand SrcValue = N->getOperand(2);
Jim Laskey6ff23e52006-10-04 16:53:27 +00002597
Chris Lattnere4b95392006-03-31 18:06:18 +00002598 // If there are no uses of the loaded value, change uses of the chain value
2599 // into uses of the chain input (i.e. delete the dead load).
2600 if (N->hasNUsesOfValue(0, 0))
2601 return CombineTo(N, DAG.getNode(ISD::UNDEF, N->getValueType(0)), Chain);
Chris Lattner01a22022005-10-10 22:04:48 +00002602
2603 // If this load is directly stored, replace the load value with the stored
2604 // value.
2605 // TODO: Handle store large -> read small portion.
2606 // TODO: Handle TRUNCSTORE/EXTLOAD
2607 if (Chain.getOpcode() == ISD::STORE && Chain.getOperand(2) == Ptr &&
2608 Chain.getOperand(1).getValueType() == N->getValueType(0))
2609 return CombineTo(N, Chain.getOperand(1), Chain);
Jim Laskey6ff23e52006-10-04 16:53:27 +00002610
Jim Laskeybb151852006-09-26 17:44:58 +00002611 if (CombinerAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00002612 // Walk up chain skipping non-aliasing memory nodes.
2613 SDOperand BetterChain = FindBetterChain(N, Chain);
2614
Jim Laskey6ff23e52006-10-04 16:53:27 +00002615 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00002616 if (Chain != BetterChain) {
2617 // Replace the chain to void dependency.
2618 SDOperand ReplLoad = DAG.getLoad(N->getValueType(0), BetterChain, Ptr,
2619 SrcValue);
2620
Jim Laskey6ff23e52006-10-04 16:53:27 +00002621 // Create token factor to keep old chain connected.
Jim Laskey288af5e2006-09-25 19:32:58 +00002622 SDOperand Token = DAG.getNode(ISD::TokenFactor, MVT::Other,
2623 Chain, ReplLoad.getValue(1));
Jim Laskey6ff23e52006-10-04 16:53:27 +00002624
2625 // Replace uses with load result and token factor.
2626 return CombineTo(N, ReplLoad.getValue(0), Token);
Jim Laskey279f0532006-09-25 16:29:54 +00002627 }
2628 }
2629
Chris Lattner01a22022005-10-10 22:04:48 +00002630 return SDOperand();
2631}
2632
Evan Chengc5484282006-10-04 00:56:09 +00002633/// visitLOADX - Handle EXTLOAD/ZEXTLOAD/SEXTLOAD.
2634SDOperand DAGCombiner::visitLOADX(SDNode *N) {
Chris Lattner29cd7db2006-03-31 18:10:41 +00002635 SDOperand Chain = N->getOperand(0);
2636 SDOperand Ptr = N->getOperand(1);
2637 SDOperand SrcValue = N->getOperand(2);
2638 SDOperand EVT = N->getOperand(3);
2639
2640 // If there are no uses of the loaded value, change uses of the chain value
2641 // into uses of the chain input (i.e. delete the dead load).
2642 if (N->hasNUsesOfValue(0, 0))
2643 return CombineTo(N, DAG.getNode(ISD::UNDEF, N->getValueType(0)), Chain);
2644
2645 return SDOperand();
2646}
2647
Chris Lattner87514ca2005-10-10 22:31:19 +00002648SDOperand DAGCombiner::visitSTORE(SDNode *N) {
2649 SDOperand Chain = N->getOperand(0);
2650 SDOperand Value = N->getOperand(1);
2651 SDOperand Ptr = N->getOperand(2);
2652 SDOperand SrcValue = N->getOperand(3);
2653
2654 // If this is a store that kills a previous store, remove the previous store.
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002655 if (Chain.getOpcode() == ISD::STORE && Chain.getOperand(2) == Ptr &&
Chris Lattnerfe7f0462005-10-27 07:10:34 +00002656 Chain.Val->hasOneUse() /* Avoid introducing DAG cycles */ &&
2657 // Make sure that these stores are the same value type:
2658 // FIXME: we really care that the second store is >= size of the first.
2659 Value.getValueType() == Chain.getOperand(1).getValueType()) {
Chris Lattner87514ca2005-10-10 22:31:19 +00002660 // Create a new store of Value that replaces both stores.
2661 SDNode *PrevStore = Chain.Val;
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002662 if (PrevStore->getOperand(1) == Value) // Same value multiply stored.
2663 return Chain;
Evan Cheng786225a2006-10-05 23:01:46 +00002664 SDOperand NewStore = DAG.getStore(PrevStore->getOperand(0), Value, Ptr,
2665 SrcValue);
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002666 CombineTo(N, NewStore); // Nuke this store.
Chris Lattner87514ca2005-10-10 22:31:19 +00002667 CombineTo(PrevStore, NewStore); // Nuke the previous store.
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002668 return SDOperand(N, 0);
Chris Lattner87514ca2005-10-10 22:31:19 +00002669 }
2670
Chris Lattnerc33baaa2005-12-23 05:48:07 +00002671 // If this is a store of a bit convert, store the input value.
Chris Lattnerbf40c4b2006-01-15 18:58:59 +00002672 // FIXME: This needs to know that the resultant store does not need a
2673 // higher alignment than the original.
Jim Laskey14fbcbf2006-09-25 21:11:32 +00002674 if (0 && Value.getOpcode() == ISD::BIT_CONVERT) {
Evan Cheng786225a2006-10-05 23:01:46 +00002675 return DAG.getStore(Chain, Value.getOperand(0), Ptr, SrcValue);
Jim Laskey279f0532006-09-25 16:29:54 +00002676 }
2677
2678 if (CombinerAA) {
Jim Laskey288af5e2006-09-25 19:32:58 +00002679 // If the store ptr is a frame index and the frame index has a use of one
2680 // and this is a return block, then the store is redundant.
2681 if (Ptr.hasOneUse() && isa<FrameIndexSDNode>(Ptr) &&
2682 DAG.getRoot().getOpcode() == ISD::RET) {
2683 return Chain;
2684 }
2685
Jim Laskey279f0532006-09-25 16:29:54 +00002686 // Walk up chain skipping non-aliasing memory nodes.
2687 SDOperand BetterChain = FindBetterChain(N, Chain);
2688
Jim Laskey6ff23e52006-10-04 16:53:27 +00002689 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00002690 if (Chain != BetterChain) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00002691 // Replace the chain to avoid dependency.
Evan Cheng786225a2006-10-05 23:01:46 +00002692 SDOperand ReplStore = DAG.getStore(BetterChain, Value, Ptr, SrcValue);
Jim Laskey279f0532006-09-25 16:29:54 +00002693 // Create token to keep both nodes around.
Jim Laskey6ff23e52006-10-04 16:53:27 +00002694 return DAG.getNode(ISD::TokenFactor, MVT::Other, Chain, ReplStore);
Jim Laskey279f0532006-09-25 16:29:54 +00002695 }
Jim Laskeyd1aed7a2006-09-21 16:28:59 +00002696 }
Chris Lattnerc33baaa2005-12-23 05:48:07 +00002697
Chris Lattner87514ca2005-10-10 22:31:19 +00002698 return SDOperand();
2699}
2700
Chris Lattnerca242442006-03-19 01:27:56 +00002701SDOperand DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
2702 SDOperand InVec = N->getOperand(0);
2703 SDOperand InVal = N->getOperand(1);
2704 SDOperand EltNo = N->getOperand(2);
2705
2706 // If the invec is a BUILD_VECTOR and if EltNo is a constant, build a new
2707 // vector with the inserted element.
2708 if (InVec.getOpcode() == ISD::BUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
2709 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002710 SmallVector<SDOperand, 8> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
Chris Lattnerca242442006-03-19 01:27:56 +00002711 if (Elt < Ops.size())
2712 Ops[Elt] = InVal;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002713 return DAG.getNode(ISD::BUILD_VECTOR, InVec.getValueType(),
2714 &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00002715 }
2716
2717 return SDOperand();
2718}
2719
2720SDOperand DAGCombiner::visitVINSERT_VECTOR_ELT(SDNode *N) {
2721 SDOperand InVec = N->getOperand(0);
2722 SDOperand InVal = N->getOperand(1);
2723 SDOperand EltNo = N->getOperand(2);
2724 SDOperand NumElts = N->getOperand(3);
2725 SDOperand EltType = N->getOperand(4);
2726
2727 // If the invec is a VBUILD_VECTOR and if EltNo is a constant, build a new
2728 // vector with the inserted element.
2729 if (InVec.getOpcode() == ISD::VBUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
2730 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002731 SmallVector<SDOperand, 8> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
Chris Lattnerca242442006-03-19 01:27:56 +00002732 if (Elt < Ops.size()-2)
2733 Ops[Elt] = InVal;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002734 return DAG.getNode(ISD::VBUILD_VECTOR, InVec.getValueType(),
2735 &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00002736 }
2737
2738 return SDOperand();
2739}
2740
Chris Lattnerd7648c82006-03-28 20:28:38 +00002741SDOperand DAGCombiner::visitVBUILD_VECTOR(SDNode *N) {
2742 unsigned NumInScalars = N->getNumOperands()-2;
2743 SDOperand NumElts = N->getOperand(NumInScalars);
2744 SDOperand EltType = N->getOperand(NumInScalars+1);
2745
2746 // Check to see if this is a VBUILD_VECTOR of a bunch of VEXTRACT_VECTOR_ELT
2747 // operations. If so, and if the EXTRACT_ELT vector inputs come from at most
2748 // two distinct vectors, turn this into a shuffle node.
2749 SDOperand VecIn1, VecIn2;
2750 for (unsigned i = 0; i != NumInScalars; ++i) {
2751 // Ignore undef inputs.
2752 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
2753
2754 // If this input is something other than a VEXTRACT_VECTOR_ELT with a
2755 // constant index, bail out.
2756 if (N->getOperand(i).getOpcode() != ISD::VEXTRACT_VECTOR_ELT ||
2757 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
2758 VecIn1 = VecIn2 = SDOperand(0, 0);
2759 break;
2760 }
2761
2762 // If the input vector type disagrees with the result of the vbuild_vector,
2763 // we can't make a shuffle.
2764 SDOperand ExtractedFromVec = N->getOperand(i).getOperand(0);
2765 if (*(ExtractedFromVec.Val->op_end()-2) != NumElts ||
2766 *(ExtractedFromVec.Val->op_end()-1) != EltType) {
2767 VecIn1 = VecIn2 = SDOperand(0, 0);
2768 break;
2769 }
2770
2771 // Otherwise, remember this. We allow up to two distinct input vectors.
2772 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
2773 continue;
2774
2775 if (VecIn1.Val == 0) {
2776 VecIn1 = ExtractedFromVec;
2777 } else if (VecIn2.Val == 0) {
2778 VecIn2 = ExtractedFromVec;
2779 } else {
2780 // Too many inputs.
2781 VecIn1 = VecIn2 = SDOperand(0, 0);
2782 break;
2783 }
2784 }
2785
2786 // If everything is good, we can make a shuffle operation.
2787 if (VecIn1.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002788 SmallVector<SDOperand, 8> BuildVecIndices;
Chris Lattnerd7648c82006-03-28 20:28:38 +00002789 for (unsigned i = 0; i != NumInScalars; ++i) {
2790 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
2791 BuildVecIndices.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
2792 continue;
2793 }
2794
2795 SDOperand Extract = N->getOperand(i);
2796
2797 // If extracting from the first vector, just use the index directly.
2798 if (Extract.getOperand(0) == VecIn1) {
2799 BuildVecIndices.push_back(Extract.getOperand(1));
2800 continue;
2801 }
2802
2803 // Otherwise, use InIdx + VecSize
2804 unsigned Idx = cast<ConstantSDNode>(Extract.getOperand(1))->getValue();
2805 BuildVecIndices.push_back(DAG.getConstant(Idx+NumInScalars, MVT::i32));
2806 }
2807
2808 // Add count and size info.
2809 BuildVecIndices.push_back(NumElts);
2810 BuildVecIndices.push_back(DAG.getValueType(MVT::i32));
2811
2812 // Return the new VVECTOR_SHUFFLE node.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002813 SDOperand Ops[5];
2814 Ops[0] = VecIn1;
Chris Lattnercef896e2006-03-28 22:19:47 +00002815 if (VecIn2.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002816 Ops[1] = VecIn2;
Chris Lattnercef896e2006-03-28 22:19:47 +00002817 } else {
2818 // Use an undef vbuild_vector as input for the second operand.
2819 std::vector<SDOperand> UnOps(NumInScalars,
2820 DAG.getNode(ISD::UNDEF,
2821 cast<VTSDNode>(EltType)->getVT()));
2822 UnOps.push_back(NumElts);
2823 UnOps.push_back(EltType);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002824 Ops[1] = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
2825 &UnOps[0], UnOps.size());
2826 AddToWorkList(Ops[1].Val);
Chris Lattnercef896e2006-03-28 22:19:47 +00002827 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002828 Ops[2] = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
2829 &BuildVecIndices[0], BuildVecIndices.size());
2830 Ops[3] = NumElts;
2831 Ops[4] = EltType;
2832 return DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector, Ops, 5);
Chris Lattnerd7648c82006-03-28 20:28:38 +00002833 }
2834
2835 return SDOperand();
2836}
2837
Chris Lattner66445d32006-03-28 22:11:53 +00002838SDOperand DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
Chris Lattnerf1d0c622006-03-31 22:16:43 +00002839 SDOperand ShufMask = N->getOperand(2);
2840 unsigned NumElts = ShufMask.getNumOperands();
2841
2842 // If the shuffle mask is an identity operation on the LHS, return the LHS.
2843 bool isIdentity = true;
2844 for (unsigned i = 0; i != NumElts; ++i) {
2845 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
2846 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
2847 isIdentity = false;
2848 break;
2849 }
2850 }
2851 if (isIdentity) return N->getOperand(0);
2852
2853 // If the shuffle mask is an identity operation on the RHS, return the RHS.
2854 isIdentity = true;
2855 for (unsigned i = 0; i != NumElts; ++i) {
2856 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
2857 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
2858 isIdentity = false;
2859 break;
2860 }
2861 }
2862 if (isIdentity) return N->getOperand(1);
Evan Chenge7bec0d2006-07-20 22:44:41 +00002863
2864 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
2865 // needed at all.
2866 bool isUnary = true;
Evan Cheng917ec982006-07-21 08:25:53 +00002867 bool isSplat = true;
Evan Chenge7bec0d2006-07-20 22:44:41 +00002868 int VecNum = -1;
Reid Spencer9160a6a2006-07-25 20:44:41 +00002869 unsigned BaseIdx = 0;
Evan Chenge7bec0d2006-07-20 22:44:41 +00002870 for (unsigned i = 0; i != NumElts; ++i)
2871 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
2872 unsigned Idx = cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue();
2873 int V = (Idx < NumElts) ? 0 : 1;
Evan Cheng917ec982006-07-21 08:25:53 +00002874 if (VecNum == -1) {
Evan Chenge7bec0d2006-07-20 22:44:41 +00002875 VecNum = V;
Evan Cheng917ec982006-07-21 08:25:53 +00002876 BaseIdx = Idx;
2877 } else {
2878 if (BaseIdx != Idx)
2879 isSplat = false;
2880 if (VecNum != V) {
2881 isUnary = false;
2882 break;
2883 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00002884 }
2885 }
2886
2887 SDOperand N0 = N->getOperand(0);
2888 SDOperand N1 = N->getOperand(1);
2889 // Normalize unary shuffle so the RHS is undef.
2890 if (isUnary && VecNum == 1)
2891 std::swap(N0, N1);
2892
Evan Cheng917ec982006-07-21 08:25:53 +00002893 // If it is a splat, check if the argument vector is a build_vector with
2894 // all scalar elements the same.
2895 if (isSplat) {
2896 SDNode *V = N0.Val;
2897 if (V->getOpcode() == ISD::BIT_CONVERT)
2898 V = V->getOperand(0).Val;
2899 if (V->getOpcode() == ISD::BUILD_VECTOR) {
2900 unsigned NumElems = V->getNumOperands()-2;
2901 if (NumElems > BaseIdx) {
2902 SDOperand Base;
2903 bool AllSame = true;
2904 for (unsigned i = 0; i != NumElems; ++i) {
2905 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
2906 Base = V->getOperand(i);
2907 break;
2908 }
2909 }
2910 // Splat of <u, u, u, u>, return <u, u, u, u>
2911 if (!Base.Val)
2912 return N0;
2913 for (unsigned i = 0; i != NumElems; ++i) {
2914 if (V->getOperand(i).getOpcode() != ISD::UNDEF &&
2915 V->getOperand(i) != Base) {
2916 AllSame = false;
2917 break;
2918 }
2919 }
2920 // Splat of <x, x, x, x>, return <x, x, x, x>
2921 if (AllSame)
2922 return N0;
2923 }
2924 }
2925 }
2926
Evan Chenge7bec0d2006-07-20 22:44:41 +00002927 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
2928 // into an undef.
2929 if (isUnary || N0 == N1) {
2930 if (N0.getOpcode() == ISD::UNDEF)
Evan Chengc04766a2006-04-06 23:20:43 +00002931 return DAG.getNode(ISD::UNDEF, N->getValueType(0));
Chris Lattner66445d32006-03-28 22:11:53 +00002932 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
2933 // first operand.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002934 SmallVector<SDOperand, 8> MappedOps;
Chris Lattner66445d32006-03-28 22:11:53 +00002935 for (unsigned i = 0, e = ShufMask.getNumOperands(); i != e; ++i) {
Evan Chengc04766a2006-04-06 23:20:43 +00002936 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
2937 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) {
2938 MappedOps.push_back(ShufMask.getOperand(i));
2939 } else {
Chris Lattner66445d32006-03-28 22:11:53 +00002940 unsigned NewIdx =
2941 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
2942 MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32));
Chris Lattner66445d32006-03-28 22:11:53 +00002943 }
2944 }
2945 ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMask.getValueType(),
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002946 &MappedOps[0], MappedOps.size());
Chris Lattner3e104b12006-04-08 04:15:24 +00002947 AddToWorkList(ShufMask.Val);
Chris Lattner66445d32006-03-28 22:11:53 +00002948 return DAG.getNode(ISD::VECTOR_SHUFFLE, N->getValueType(0),
Evan Chenge7bec0d2006-07-20 22:44:41 +00002949 N0,
Chris Lattner66445d32006-03-28 22:11:53 +00002950 DAG.getNode(ISD::UNDEF, N->getValueType(0)),
2951 ShufMask);
2952 }
2953
2954 return SDOperand();
2955}
2956
Chris Lattnerf1d0c622006-03-31 22:16:43 +00002957SDOperand DAGCombiner::visitVVECTOR_SHUFFLE(SDNode *N) {
2958 SDOperand ShufMask = N->getOperand(2);
2959 unsigned NumElts = ShufMask.getNumOperands()-2;
2960
2961 // If the shuffle mask is an identity operation on the LHS, return the LHS.
2962 bool isIdentity = true;
2963 for (unsigned i = 0; i != NumElts; ++i) {
2964 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
2965 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
2966 isIdentity = false;
2967 break;
2968 }
2969 }
2970 if (isIdentity) return N->getOperand(0);
2971
2972 // If the shuffle mask is an identity operation on the RHS, return the RHS.
2973 isIdentity = true;
2974 for (unsigned i = 0; i != NumElts; ++i) {
2975 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
2976 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
2977 isIdentity = false;
2978 break;
2979 }
2980 }
2981 if (isIdentity) return N->getOperand(1);
2982
Evan Chenge7bec0d2006-07-20 22:44:41 +00002983 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
2984 // needed at all.
2985 bool isUnary = true;
Evan Cheng917ec982006-07-21 08:25:53 +00002986 bool isSplat = true;
Evan Chenge7bec0d2006-07-20 22:44:41 +00002987 int VecNum = -1;
Reid Spencer9160a6a2006-07-25 20:44:41 +00002988 unsigned BaseIdx = 0;
Evan Chenge7bec0d2006-07-20 22:44:41 +00002989 for (unsigned i = 0; i != NumElts; ++i)
2990 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
2991 unsigned Idx = cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue();
2992 int V = (Idx < NumElts) ? 0 : 1;
Evan Cheng917ec982006-07-21 08:25:53 +00002993 if (VecNum == -1) {
Evan Chenge7bec0d2006-07-20 22:44:41 +00002994 VecNum = V;
Evan Cheng917ec982006-07-21 08:25:53 +00002995 BaseIdx = Idx;
2996 } else {
2997 if (BaseIdx != Idx)
2998 isSplat = false;
2999 if (VecNum != V) {
3000 isUnary = false;
3001 break;
3002 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00003003 }
3004 }
3005
3006 SDOperand N0 = N->getOperand(0);
3007 SDOperand N1 = N->getOperand(1);
3008 // Normalize unary shuffle so the RHS is undef.
3009 if (isUnary && VecNum == 1)
3010 std::swap(N0, N1);
3011
Evan Cheng917ec982006-07-21 08:25:53 +00003012 // If it is a splat, check if the argument vector is a build_vector with
3013 // all scalar elements the same.
3014 if (isSplat) {
3015 SDNode *V = N0.Val;
3016 if (V->getOpcode() == ISD::VBIT_CONVERT)
3017 V = V->getOperand(0).Val;
3018 if (V->getOpcode() == ISD::VBUILD_VECTOR) {
3019 unsigned NumElems = V->getNumOperands()-2;
3020 if (NumElems > BaseIdx) {
3021 SDOperand Base;
3022 bool AllSame = true;
3023 for (unsigned i = 0; i != NumElems; ++i) {
3024 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
3025 Base = V->getOperand(i);
3026 break;
3027 }
3028 }
3029 // Splat of <u, u, u, u>, return <u, u, u, u>
3030 if (!Base.Val)
3031 return N0;
3032 for (unsigned i = 0; i != NumElems; ++i) {
3033 if (V->getOperand(i).getOpcode() != ISD::UNDEF &&
3034 V->getOperand(i) != Base) {
3035 AllSame = false;
3036 break;
3037 }
3038 }
3039 // Splat of <x, x, x, x>, return <x, x, x, x>
3040 if (AllSame)
3041 return N0;
3042 }
3043 }
3044 }
3045
Evan Chenge7bec0d2006-07-20 22:44:41 +00003046 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
3047 // into an undef.
3048 if (isUnary || N0 == N1) {
Chris Lattner17614ea2006-04-08 05:34:25 +00003049 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
3050 // first operand.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003051 SmallVector<SDOperand, 8> MappedOps;
Chris Lattner17614ea2006-04-08 05:34:25 +00003052 for (unsigned i = 0; i != NumElts; ++i) {
3053 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
3054 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) {
3055 MappedOps.push_back(ShufMask.getOperand(i));
3056 } else {
3057 unsigned NewIdx =
3058 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
3059 MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32));
3060 }
3061 }
3062 // Add the type/#elts values.
3063 MappedOps.push_back(ShufMask.getOperand(NumElts));
3064 MappedOps.push_back(ShufMask.getOperand(NumElts+1));
3065
3066 ShufMask = DAG.getNode(ISD::VBUILD_VECTOR, ShufMask.getValueType(),
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003067 &MappedOps[0], MappedOps.size());
Chris Lattner17614ea2006-04-08 05:34:25 +00003068 AddToWorkList(ShufMask.Val);
3069
3070 // Build the undef vector.
3071 SDOperand UDVal = DAG.getNode(ISD::UNDEF, MappedOps[0].getValueType());
3072 for (unsigned i = 0; i != NumElts; ++i)
3073 MappedOps[i] = UDVal;
Evan Chenge7bec0d2006-07-20 22:44:41 +00003074 MappedOps[NumElts ] = *(N0.Val->op_end()-2);
3075 MappedOps[NumElts+1] = *(N0.Val->op_end()-1);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003076 UDVal = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
3077 &MappedOps[0], MappedOps.size());
Chris Lattner17614ea2006-04-08 05:34:25 +00003078
3079 return DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector,
Evan Chenge7bec0d2006-07-20 22:44:41 +00003080 N0, UDVal, ShufMask,
Chris Lattner17614ea2006-04-08 05:34:25 +00003081 MappedOps[NumElts], MappedOps[NumElts+1]);
3082 }
3083
Chris Lattnerf1d0c622006-03-31 22:16:43 +00003084 return SDOperand();
3085}
3086
Evan Cheng44f1f092006-04-20 08:56:16 +00003087/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
3088/// a VAND to a vector_shuffle with the destination vector and a zero vector.
3089/// e.g. VAND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
3090/// vector_shuffle V, Zero, <0, 4, 2, 4>
3091SDOperand DAGCombiner::XformToShuffleWithZero(SDNode *N) {
3092 SDOperand LHS = N->getOperand(0);
3093 SDOperand RHS = N->getOperand(1);
3094 if (N->getOpcode() == ISD::VAND) {
3095 SDOperand DstVecSize = *(LHS.Val->op_end()-2);
3096 SDOperand DstVecEVT = *(LHS.Val->op_end()-1);
3097 if (RHS.getOpcode() == ISD::VBIT_CONVERT)
3098 RHS = RHS.getOperand(0);
3099 if (RHS.getOpcode() == ISD::VBUILD_VECTOR) {
3100 std::vector<SDOperand> IdxOps;
3101 unsigned NumOps = RHS.getNumOperands();
3102 unsigned NumElts = NumOps-2;
3103 MVT::ValueType EVT = cast<VTSDNode>(RHS.getOperand(NumOps-1))->getVT();
3104 for (unsigned i = 0; i != NumElts; ++i) {
3105 SDOperand Elt = RHS.getOperand(i);
3106 if (!isa<ConstantSDNode>(Elt))
3107 return SDOperand();
3108 else if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
3109 IdxOps.push_back(DAG.getConstant(i, EVT));
3110 else if (cast<ConstantSDNode>(Elt)->isNullValue())
3111 IdxOps.push_back(DAG.getConstant(NumElts, EVT));
3112 else
3113 return SDOperand();
3114 }
3115
3116 // Let's see if the target supports this vector_shuffle.
3117 if (!TLI.isVectorClearMaskLegal(IdxOps, EVT, DAG))
3118 return SDOperand();
3119
3120 // Return the new VVECTOR_SHUFFLE node.
3121 SDOperand NumEltsNode = DAG.getConstant(NumElts, MVT::i32);
3122 SDOperand EVTNode = DAG.getValueType(EVT);
3123 std::vector<SDOperand> Ops;
Chris Lattner516b9622006-09-14 20:50:57 +00003124 LHS = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, LHS, NumEltsNode,
3125 EVTNode);
Evan Cheng44f1f092006-04-20 08:56:16 +00003126 Ops.push_back(LHS);
3127 AddToWorkList(LHS.Val);
3128 std::vector<SDOperand> ZeroOps(NumElts, DAG.getConstant(0, EVT));
3129 ZeroOps.push_back(NumEltsNode);
3130 ZeroOps.push_back(EVTNode);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003131 Ops.push_back(DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
3132 &ZeroOps[0], ZeroOps.size()));
Evan Cheng44f1f092006-04-20 08:56:16 +00003133 IdxOps.push_back(NumEltsNode);
3134 IdxOps.push_back(EVTNode);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003135 Ops.push_back(DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
3136 &IdxOps[0], IdxOps.size()));
Evan Cheng44f1f092006-04-20 08:56:16 +00003137 Ops.push_back(NumEltsNode);
3138 Ops.push_back(EVTNode);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003139 SDOperand Result = DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector,
3140 &Ops[0], Ops.size());
Evan Cheng44f1f092006-04-20 08:56:16 +00003141 if (NumEltsNode != DstVecSize || EVTNode != DstVecEVT) {
3142 Result = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Result,
3143 DstVecSize, DstVecEVT);
3144 }
3145 return Result;
3146 }
3147 }
3148 return SDOperand();
3149}
3150
Chris Lattneredab1b92006-04-02 03:25:57 +00003151/// visitVBinOp - Visit a binary vector operation, like VADD. IntOp indicates
3152/// the scalar operation of the vop if it is operating on an integer vector
3153/// (e.g. ADD) and FPOp indicates the FP version (e.g. FADD).
3154SDOperand DAGCombiner::visitVBinOp(SDNode *N, ISD::NodeType IntOp,
3155 ISD::NodeType FPOp) {
3156 MVT::ValueType EltType = cast<VTSDNode>(*(N->op_end()-1))->getVT();
3157 ISD::NodeType ScalarOp = MVT::isInteger(EltType) ? IntOp : FPOp;
3158 SDOperand LHS = N->getOperand(0);
3159 SDOperand RHS = N->getOperand(1);
Evan Cheng44f1f092006-04-20 08:56:16 +00003160 SDOperand Shuffle = XformToShuffleWithZero(N);
3161 if (Shuffle.Val) return Shuffle;
3162
Chris Lattneredab1b92006-04-02 03:25:57 +00003163 // If the LHS and RHS are VBUILD_VECTOR nodes, see if we can constant fold
3164 // this operation.
3165 if (LHS.getOpcode() == ISD::VBUILD_VECTOR &&
3166 RHS.getOpcode() == ISD::VBUILD_VECTOR) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003167 SmallVector<SDOperand, 8> Ops;
Chris Lattneredab1b92006-04-02 03:25:57 +00003168 for (unsigned i = 0, e = LHS.getNumOperands()-2; i != e; ++i) {
3169 SDOperand LHSOp = LHS.getOperand(i);
3170 SDOperand RHSOp = RHS.getOperand(i);
3171 // If these two elements can't be folded, bail out.
3172 if ((LHSOp.getOpcode() != ISD::UNDEF &&
3173 LHSOp.getOpcode() != ISD::Constant &&
3174 LHSOp.getOpcode() != ISD::ConstantFP) ||
3175 (RHSOp.getOpcode() != ISD::UNDEF &&
3176 RHSOp.getOpcode() != ISD::Constant &&
3177 RHSOp.getOpcode() != ISD::ConstantFP))
3178 break;
Evan Cheng7b336a82006-05-31 06:08:35 +00003179 // Can't fold divide by zero.
3180 if (N->getOpcode() == ISD::VSDIV || N->getOpcode() == ISD::VUDIV) {
3181 if ((RHSOp.getOpcode() == ISD::Constant &&
3182 cast<ConstantSDNode>(RHSOp.Val)->isNullValue()) ||
3183 (RHSOp.getOpcode() == ISD::ConstantFP &&
3184 !cast<ConstantFPSDNode>(RHSOp.Val)->getValue()))
3185 break;
3186 }
Chris Lattneredab1b92006-04-02 03:25:57 +00003187 Ops.push_back(DAG.getNode(ScalarOp, EltType, LHSOp, RHSOp));
Chris Lattner3e104b12006-04-08 04:15:24 +00003188 AddToWorkList(Ops.back().Val);
Chris Lattneredab1b92006-04-02 03:25:57 +00003189 assert((Ops.back().getOpcode() == ISD::UNDEF ||
3190 Ops.back().getOpcode() == ISD::Constant ||
3191 Ops.back().getOpcode() == ISD::ConstantFP) &&
3192 "Scalar binop didn't fold!");
3193 }
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00003194
3195 if (Ops.size() == LHS.getNumOperands()-2) {
3196 Ops.push_back(*(LHS.Val->op_end()-2));
3197 Ops.push_back(*(LHS.Val->op_end()-1));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003198 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00003199 }
Chris Lattneredab1b92006-04-02 03:25:57 +00003200 }
3201
3202 return SDOperand();
3203}
3204
Nate Begeman44728a72005-09-19 22:34:01 +00003205SDOperand DAGCombiner::SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2){
Nate Begemanf845b452005-10-08 00:29:44 +00003206 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
3207
3208 SDOperand SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
3209 cast<CondCodeSDNode>(N0.getOperand(2))->get());
3210 // If we got a simplified select_cc node back from SimplifySelectCC, then
3211 // break it down into a new SETCC node, and a new SELECT node, and then return
3212 // the SELECT node, since we were called with a SELECT node.
3213 if (SCC.Val) {
3214 // Check to see if we got a select_cc back (to turn into setcc/select).
3215 // Otherwise, just return whatever node we got back, like fabs.
3216 if (SCC.getOpcode() == ISD::SELECT_CC) {
3217 SDOperand SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
3218 SCC.getOperand(0), SCC.getOperand(1),
3219 SCC.getOperand(4));
Chris Lattner5750df92006-03-01 04:03:14 +00003220 AddToWorkList(SETCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003221 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
3222 SCC.getOperand(3), SETCC);
3223 }
3224 return SCC;
3225 }
Nate Begeman44728a72005-09-19 22:34:01 +00003226 return SDOperand();
3227}
3228
Chris Lattner40c62d52005-10-18 06:04:22 +00003229/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
3230/// are the two values being selected between, see if we can simplify the
Chris Lattner729c6d12006-05-27 00:43:02 +00003231/// select. Callers of this should assume that TheSelect is deleted if this
3232/// returns true. As such, they should return the appropriate thing (e.g. the
3233/// node) back to the top-level of the DAG combiner loop to avoid it being
3234/// looked at.
Chris Lattner40c62d52005-10-18 06:04:22 +00003235///
3236bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDOperand LHS,
3237 SDOperand RHS) {
3238
3239 // If this is a select from two identical things, try to pull the operation
3240 // through the select.
3241 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
Chris Lattner40c62d52005-10-18 06:04:22 +00003242 // If this is a load and the token chain is identical, replace the select
3243 // of two loads with a load through a select of the address to load from.
3244 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
3245 // constants have been dropped into the constant pool.
3246 if ((LHS.getOpcode() == ISD::LOAD ||
Evan Chengc5484282006-10-04 00:56:09 +00003247 LHS.getOpcode() == ISD::LOADX ) &&
Chris Lattner40c62d52005-10-18 06:04:22 +00003248 // Token chains must be identical.
3249 LHS.getOperand(0) == RHS.getOperand(0) &&
3250 // If this is an EXTLOAD, the VT's must match.
3251 (LHS.getOpcode() == ISD::LOAD ||
3252 LHS.getOperand(3) == RHS.getOperand(3))) {
3253 // FIXME: this conflates two src values, discarding one. This is not
3254 // the right thing to do, but nothing uses srcvalues now. When they do,
3255 // turn SrcValue into a list of locations.
3256 SDOperand Addr;
3257 if (TheSelect->getOpcode() == ISD::SELECT)
3258 Addr = DAG.getNode(ISD::SELECT, LHS.getOperand(1).getValueType(),
3259 TheSelect->getOperand(0), LHS.getOperand(1),
3260 RHS.getOperand(1));
3261 else
3262 Addr = DAG.getNode(ISD::SELECT_CC, LHS.getOperand(1).getValueType(),
3263 TheSelect->getOperand(0),
3264 TheSelect->getOperand(1),
3265 LHS.getOperand(1), RHS.getOperand(1),
3266 TheSelect->getOperand(4));
3267
3268 SDOperand Load;
3269 if (LHS.getOpcode() == ISD::LOAD)
3270 Load = DAG.getLoad(TheSelect->getValueType(0), LHS.getOperand(0),
3271 Addr, LHS.getOperand(2));
Evan Chengc5484282006-10-04 00:56:09 +00003272 else {
3273 unsigned LType = LHS.getConstantOperandVal(4);
3274 Load = DAG.getExtLoad((ISD::LoadExtType)LType,
3275 TheSelect->getValueType(0),
Chris Lattner40c62d52005-10-18 06:04:22 +00003276 LHS.getOperand(0), Addr, LHS.getOperand(2),
3277 cast<VTSDNode>(LHS.getOperand(3))->getVT());
Evan Chengc5484282006-10-04 00:56:09 +00003278 }
Chris Lattner40c62d52005-10-18 06:04:22 +00003279 // Users of the select now use the result of the load.
3280 CombineTo(TheSelect, Load);
3281
3282 // Users of the old loads now use the new load's chain. We know the
3283 // old-load value is dead now.
3284 CombineTo(LHS.Val, Load.getValue(0), Load.getValue(1));
3285 CombineTo(RHS.Val, Load.getValue(0), Load.getValue(1));
3286 return true;
3287 }
3288 }
3289
3290 return false;
3291}
3292
Nate Begeman44728a72005-09-19 22:34:01 +00003293SDOperand DAGCombiner::SimplifySelectCC(SDOperand N0, SDOperand N1,
3294 SDOperand N2, SDOperand N3,
3295 ISD::CondCode CC) {
Nate Begemanf845b452005-10-08 00:29:44 +00003296
3297 MVT::ValueType VT = N2.getValueType();
Nate Begemanf845b452005-10-08 00:29:44 +00003298 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
3299 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
3300 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
3301
3302 // Determine if the condition we're dealing with is constant
3303 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
3304 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
3305
3306 // fold select_cc true, x, y -> x
3307 if (SCCC && SCCC->getValue())
3308 return N2;
3309 // fold select_cc false, x, y -> y
3310 if (SCCC && SCCC->getValue() == 0)
3311 return N3;
3312
3313 // Check to see if we can simplify the select into an fabs node
3314 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
3315 // Allow either -0.0 or 0.0
3316 if (CFP->getValue() == 0.0) {
3317 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
3318 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
3319 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
3320 N2 == N3.getOperand(0))
3321 return DAG.getNode(ISD::FABS, VT, N0);
3322
3323 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
3324 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
3325 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
3326 N2.getOperand(0) == N3)
3327 return DAG.getNode(ISD::FABS, VT, N3);
3328 }
3329 }
3330
3331 // Check to see if we can perform the "gzip trick", transforming
3332 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
Chris Lattnere3152e52006-09-20 06:41:35 +00003333 if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT &&
Nate Begemanf845b452005-10-08 00:29:44 +00003334 MVT::isInteger(N0.getValueType()) &&
Chris Lattnere3152e52006-09-20 06:41:35 +00003335 MVT::isInteger(N2.getValueType()) &&
3336 (N1C->isNullValue() || // (a < 0) ? b : 0
3337 (N1C->getValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0
Nate Begemanf845b452005-10-08 00:29:44 +00003338 MVT::ValueType XType = N0.getValueType();
3339 MVT::ValueType AType = N2.getValueType();
3340 if (XType >= AType) {
3341 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00003342 // single-bit constant.
Nate Begemanf845b452005-10-08 00:29:44 +00003343 if (N2C && ((N2C->getValue() & (N2C->getValue()-1)) == 0)) {
3344 unsigned ShCtV = Log2_64(N2C->getValue());
3345 ShCtV = MVT::getSizeInBits(XType)-ShCtV-1;
3346 SDOperand ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
3347 SDOperand Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
Chris Lattner5750df92006-03-01 04:03:14 +00003348 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003349 if (XType > AType) {
3350 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00003351 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003352 }
3353 return DAG.getNode(ISD::AND, AType, Shift, N2);
3354 }
3355 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
3356 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3357 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00003358 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003359 if (XType > AType) {
3360 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00003361 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003362 }
3363 return DAG.getNode(ISD::AND, AType, Shift, N2);
3364 }
3365 }
Nate Begeman07ed4172005-10-10 21:26:48 +00003366
3367 // fold select C, 16, 0 -> shl C, 4
3368 if (N2C && N3C && N3C->isNullValue() && isPowerOf2_64(N2C->getValue()) &&
3369 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) {
3370 // Get a SetCC of the condition
3371 // FIXME: Should probably make sure that setcc is legal if we ever have a
3372 // target where it isn't.
Nate Begemanb0d04a72006-02-18 02:40:58 +00003373 SDOperand Temp, SCC;
Nate Begeman07ed4172005-10-10 21:26:48 +00003374 // cast from setcc result type to select result type
Nate Begemanb0d04a72006-02-18 02:40:58 +00003375 if (AfterLegalize) {
3376 SCC = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00003377 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
Nate Begemanb0d04a72006-02-18 02:40:58 +00003378 } else {
3379 SCC = DAG.getSetCC(MVT::i1, N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00003380 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00003381 }
Chris Lattner5750df92006-03-01 04:03:14 +00003382 AddToWorkList(SCC.Val);
3383 AddToWorkList(Temp.Val);
Nate Begeman07ed4172005-10-10 21:26:48 +00003384 // shl setcc result by log2 n2c
3385 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
3386 DAG.getConstant(Log2_64(N2C->getValue()),
3387 TLI.getShiftAmountTy()));
3388 }
3389
Nate Begemanf845b452005-10-08 00:29:44 +00003390 // Check to see if this is the equivalent of setcc
3391 // FIXME: Turn all of these into setcc if setcc if setcc is legal
3392 // otherwise, go ahead with the folds.
3393 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getValue() == 1ULL)) {
3394 MVT::ValueType XType = N0.getValueType();
3395 if (TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultTy())) {
3396 SDOperand Res = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
3397 if (Res.getValueType() != VT)
3398 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
3399 return Res;
3400 }
3401
3402 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
3403 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
3404 TLI.isOperationLegal(ISD::CTLZ, XType)) {
3405 SDOperand Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
3406 return DAG.getNode(ISD::SRL, XType, Ctlz,
3407 DAG.getConstant(Log2_32(MVT::getSizeInBits(XType)),
3408 TLI.getShiftAmountTy()));
3409 }
3410 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
3411 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
3412 SDOperand NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
3413 N0);
3414 SDOperand NotN0 = DAG.getNode(ISD::XOR, XType, N0,
3415 DAG.getConstant(~0ULL, XType));
3416 return DAG.getNode(ISD::SRL, XType,
3417 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
3418 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3419 TLI.getShiftAmountTy()));
3420 }
3421 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
3422 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
3423 SDOperand Sign = DAG.getNode(ISD::SRL, XType, N0,
3424 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3425 TLI.getShiftAmountTy()));
3426 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
3427 }
3428 }
3429
3430 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
3431 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
3432 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
3433 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1)) {
3434 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N2.getOperand(0))) {
3435 MVT::ValueType XType = N0.getValueType();
3436 if (SubC->isNullValue() && MVT::isInteger(XType)) {
3437 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
3438 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3439 TLI.getShiftAmountTy()));
3440 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00003441 AddToWorkList(Shift.Val);
3442 AddToWorkList(Add.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003443 return DAG.getNode(ISD::XOR, XType, Add, Shift);
3444 }
3445 }
3446 }
3447
Nate Begeman44728a72005-09-19 22:34:01 +00003448 return SDOperand();
3449}
3450
Nate Begeman452d7be2005-09-16 00:54:12 +00003451SDOperand DAGCombiner::SimplifySetCC(MVT::ValueType VT, SDOperand N0,
Nate Begemane17daeb2005-10-05 21:43:42 +00003452 SDOperand N1, ISD::CondCode Cond,
3453 bool foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00003454 // These setcc operations always fold.
3455 switch (Cond) {
3456 default: break;
3457 case ISD::SETFALSE:
3458 case ISD::SETFALSE2: return DAG.getConstant(0, VT);
3459 case ISD::SETTRUE:
3460 case ISD::SETTRUE2: return DAG.getConstant(1, VT);
3461 }
3462
3463 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
3464 uint64_t C1 = N1C->getValue();
3465 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val)) {
3466 uint64_t C0 = N0C->getValue();
3467
3468 // Sign extend the operands if required
3469 if (ISD::isSignedIntSetCC(Cond)) {
3470 C0 = N0C->getSignExtended();
3471 C1 = N1C->getSignExtended();
3472 }
3473
3474 switch (Cond) {
3475 default: assert(0 && "Unknown integer setcc!");
3476 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
3477 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
3478 case ISD::SETULT: return DAG.getConstant(C0 < C1, VT);
3479 case ISD::SETUGT: return DAG.getConstant(C0 > C1, VT);
3480 case ISD::SETULE: return DAG.getConstant(C0 <= C1, VT);
3481 case ISD::SETUGE: return DAG.getConstant(C0 >= C1, VT);
3482 case ISD::SETLT: return DAG.getConstant((int64_t)C0 < (int64_t)C1, VT);
3483 case ISD::SETGT: return DAG.getConstant((int64_t)C0 > (int64_t)C1, VT);
3484 case ISD::SETLE: return DAG.getConstant((int64_t)C0 <= (int64_t)C1, VT);
3485 case ISD::SETGE: return DAG.getConstant((int64_t)C0 >= (int64_t)C1, VT);
3486 }
3487 } else {
Chris Lattner5f42a242006-09-20 06:19:26 +00003488 // If the LHS is '(srl (ctlz x), 5)', the RHS is 0/1, and this is an
3489 // equality comparison, then we're just comparing whether X itself is
3490 // zero.
3491 if (N0.getOpcode() == ISD::SRL && (C1 == 0 || C1 == 1) &&
3492 N0.getOperand(0).getOpcode() == ISD::CTLZ &&
3493 N0.getOperand(1).getOpcode() == ISD::Constant) {
3494 unsigned ShAmt = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
3495 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
3496 ShAmt == Log2_32(MVT::getSizeInBits(N0.getValueType()))) {
3497 if ((C1 == 0) == (Cond == ISD::SETEQ)) {
3498 // (srl (ctlz x), 5) == 0 -> X != 0
3499 // (srl (ctlz x), 5) != 1 -> X != 0
3500 Cond = ISD::SETNE;
3501 } else {
3502 // (srl (ctlz x), 5) != 0 -> X == 0
3503 // (srl (ctlz x), 5) == 1 -> X == 0
3504 Cond = ISD::SETEQ;
3505 }
3506 SDOperand Zero = DAG.getConstant(0, N0.getValueType());
3507 return DAG.getSetCC(VT, N0.getOperand(0).getOperand(0),
3508 Zero, Cond);
3509 }
3510 }
3511
Nate Begeman452d7be2005-09-16 00:54:12 +00003512 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
3513 if (N0.getOpcode() == ISD::ZERO_EXTEND) {
3514 unsigned InSize = MVT::getSizeInBits(N0.getOperand(0).getValueType());
3515
3516 // If the comparison constant has bits in the upper part, the
3517 // zero-extended value could never match.
3518 if (C1 & (~0ULL << InSize)) {
3519 unsigned VSize = MVT::getSizeInBits(N0.getValueType());
3520 switch (Cond) {
3521 case ISD::SETUGT:
3522 case ISD::SETUGE:
3523 case ISD::SETEQ: return DAG.getConstant(0, VT);
3524 case ISD::SETULT:
3525 case ISD::SETULE:
3526 case ISD::SETNE: return DAG.getConstant(1, VT);
3527 case ISD::SETGT:
3528 case ISD::SETGE:
3529 // True if the sign bit of C1 is set.
3530 return DAG.getConstant((C1 & (1ULL << VSize)) != 0, VT);
3531 case ISD::SETLT:
3532 case ISD::SETLE:
3533 // True if the sign bit of C1 isn't set.
3534 return DAG.getConstant((C1 & (1ULL << VSize)) == 0, VT);
3535 default:
3536 break;
3537 }
3538 }
3539
3540 // Otherwise, we can perform the comparison with the low bits.
3541 switch (Cond) {
3542 case ISD::SETEQ:
3543 case ISD::SETNE:
3544 case ISD::SETUGT:
3545 case ISD::SETUGE:
3546 case ISD::SETULT:
3547 case ISD::SETULE:
3548 return DAG.getSetCC(VT, N0.getOperand(0),
3549 DAG.getConstant(C1, N0.getOperand(0).getValueType()),
3550 Cond);
3551 default:
3552 break; // todo, be more careful with signed comparisons
3553 }
3554 } else if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
3555 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
3556 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N0.getOperand(1))->getVT();
3557 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
3558 MVT::ValueType ExtDstTy = N0.getValueType();
3559 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
3560
3561 // If the extended part has any inconsistent bits, it cannot ever
3562 // compare equal. In other words, they have to be all ones or all
3563 // zeros.
3564 uint64_t ExtBits =
3565 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
3566 if ((C1 & ExtBits) != 0 && (C1 & ExtBits) != ExtBits)
3567 return DAG.getConstant(Cond == ISD::SETNE, VT);
3568
3569 SDOperand ZextOp;
3570 MVT::ValueType Op0Ty = N0.getOperand(0).getValueType();
3571 if (Op0Ty == ExtSrcTy) {
3572 ZextOp = N0.getOperand(0);
3573 } else {
3574 int64_t Imm = ~0ULL >> (64-ExtSrcTyBits);
3575 ZextOp = DAG.getNode(ISD::AND, Op0Ty, N0.getOperand(0),
3576 DAG.getConstant(Imm, Op0Ty));
3577 }
Chris Lattner5750df92006-03-01 04:03:14 +00003578 AddToWorkList(ZextOp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003579 // Otherwise, make this a use of a zext.
3580 return DAG.getSetCC(VT, ZextOp,
3581 DAG.getConstant(C1 & (~0ULL>>(64-ExtSrcTyBits)),
3582 ExtDstTy),
3583 Cond);
Chris Lattner3391bcd2006-02-08 02:13:15 +00003584 } else if ((N1C->getValue() == 0 || N1C->getValue() == 1) &&
3585 (Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
3586 (N0.getOpcode() == ISD::XOR ||
3587 (N0.getOpcode() == ISD::AND &&
3588 N0.getOperand(0).getOpcode() == ISD::XOR &&
3589 N0.getOperand(1) == N0.getOperand(0).getOperand(1))) &&
3590 isa<ConstantSDNode>(N0.getOperand(1)) &&
3591 cast<ConstantSDNode>(N0.getOperand(1))->getValue() == 1) {
3592 // If this is (X^1) == 0/1, swap the RHS and eliminate the xor. We can
3593 // only do this if the top bits are known zero.
3594 if (TLI.MaskedValueIsZero(N1,
3595 MVT::getIntVTBitMask(N0.getValueType())-1)) {
3596 // Okay, get the un-inverted input value.
3597 SDOperand Val;
3598 if (N0.getOpcode() == ISD::XOR)
3599 Val = N0.getOperand(0);
3600 else {
3601 assert(N0.getOpcode() == ISD::AND &&
3602 N0.getOperand(0).getOpcode() == ISD::XOR);
3603 // ((X^1)&1)^1 -> X & 1
3604 Val = DAG.getNode(ISD::AND, N0.getValueType(),
3605 N0.getOperand(0).getOperand(0), N0.getOperand(1));
3606 }
3607 return DAG.getSetCC(VT, Val, N1,
3608 Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ);
3609 }
Nate Begeman452d7be2005-09-16 00:54:12 +00003610 }
Chris Lattner5c46f742005-10-05 06:11:08 +00003611
Nate Begeman452d7be2005-09-16 00:54:12 +00003612 uint64_t MinVal, MaxVal;
3613 unsigned OperandBitSize = MVT::getSizeInBits(N1C->getValueType(0));
3614 if (ISD::isSignedIntSetCC(Cond)) {
3615 MinVal = 1ULL << (OperandBitSize-1);
3616 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
3617 MaxVal = ~0ULL >> (65-OperandBitSize);
3618 else
3619 MaxVal = 0;
3620 } else {
3621 MinVal = 0;
3622 MaxVal = ~0ULL >> (64-OperandBitSize);
3623 }
3624
3625 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
3626 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
3627 if (C1 == MinVal) return DAG.getConstant(1, VT); // X >= MIN --> true
3628 --C1; // X >= C0 --> X > (C0-1)
3629 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
3630 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
3631 }
3632
3633 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
3634 if (C1 == MaxVal) return DAG.getConstant(1, VT); // X <= MAX --> true
3635 ++C1; // X <= C0 --> X < (C0+1)
3636 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
3637 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
3638 }
3639
3640 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal)
3641 return DAG.getConstant(0, VT); // X < MIN --> false
3642
3643 // Canonicalize setgt X, Min --> setne X, Min
3644 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MinVal)
3645 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
Chris Lattnerc8597ca2005-10-21 21:23:25 +00003646 // Canonicalize setlt X, Max --> setne X, Max
3647 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MaxVal)
3648 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
Nate Begeman452d7be2005-09-16 00:54:12 +00003649
3650 // If we have setult X, 1, turn it into seteq X, 0
3651 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal+1)
3652 return DAG.getSetCC(VT, N0, DAG.getConstant(MinVal, N0.getValueType()),
3653 ISD::SETEQ);
3654 // If we have setugt X, Max-1, turn it into seteq X, Max
3655 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal-1)
3656 return DAG.getSetCC(VT, N0, DAG.getConstant(MaxVal, N0.getValueType()),
3657 ISD::SETEQ);
3658
3659 // If we have "setcc X, C0", check to see if we can shrink the immediate
3660 // by changing cc.
3661
3662 // SETUGT X, SINTMAX -> SETLT X, 0
3663 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
3664 C1 == (~0ULL >> (65-OperandBitSize)))
3665 return DAG.getSetCC(VT, N0, DAG.getConstant(0, N1.getValueType()),
3666 ISD::SETLT);
3667
3668 // FIXME: Implement the rest of these.
3669
3670 // Fold bit comparisons when we can.
3671 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
3672 VT == N0.getValueType() && N0.getOpcode() == ISD::AND)
3673 if (ConstantSDNode *AndRHS =
3674 dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
3675 if (Cond == ISD::SETNE && C1 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
3676 // Perform the xform if the AND RHS is a single bit.
3677 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
3678 return DAG.getNode(ISD::SRL, VT, N0,
3679 DAG.getConstant(Log2_64(AndRHS->getValue()),
3680 TLI.getShiftAmountTy()));
3681 }
3682 } else if (Cond == ISD::SETEQ && C1 == AndRHS->getValue()) {
3683 // (X & 8) == 8 --> (X & 8) >> 3
3684 // Perform the xform if C1 is a single bit.
3685 if ((C1 & (C1-1)) == 0) {
3686 return DAG.getNode(ISD::SRL, VT, N0,
Chris Lattner729c6d12006-05-27 00:43:02 +00003687 DAG.getConstant(Log2_64(C1),TLI.getShiftAmountTy()));
Nate Begeman452d7be2005-09-16 00:54:12 +00003688 }
3689 }
3690 }
3691 }
3692 } else if (isa<ConstantSDNode>(N0.Val)) {
3693 // Ensure that the constant occurs on the RHS.
3694 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
3695 }
3696
3697 if (ConstantFPSDNode *N0C = dyn_cast<ConstantFPSDNode>(N0.Val))
3698 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) {
3699 double C0 = N0C->getValue(), C1 = N1C->getValue();
3700
3701 switch (Cond) {
3702 default: break; // FIXME: Implement the rest of these!
3703 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
3704 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
3705 case ISD::SETLT: return DAG.getConstant(C0 < C1, VT);
3706 case ISD::SETGT: return DAG.getConstant(C0 > C1, VT);
3707 case ISD::SETLE: return DAG.getConstant(C0 <= C1, VT);
3708 case ISD::SETGE: return DAG.getConstant(C0 >= C1, VT);
3709 }
3710 } else {
3711 // Ensure that the constant occurs on the RHS.
3712 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
3713 }
3714
3715 if (N0 == N1) {
3716 // We can always fold X == Y for integer setcc's.
3717 if (MVT::isInteger(N0.getValueType()))
3718 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
3719 unsigned UOF = ISD::getUnorderedFlavor(Cond);
3720 if (UOF == 2) // FP operators that are undefined on NaNs.
3721 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
3722 if (UOF == unsigned(ISD::isTrueWhenEqual(Cond)))
3723 return DAG.getConstant(UOF, VT);
3724 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
3725 // if it is not already.
Chris Lattner4090aee2006-01-18 19:13:41 +00003726 ISD::CondCode NewCond = UOF == 0 ? ISD::SETO : ISD::SETUO;
Nate Begeman452d7be2005-09-16 00:54:12 +00003727 if (NewCond != Cond)
3728 return DAG.getSetCC(VT, N0, N1, NewCond);
3729 }
3730
3731 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
3732 MVT::isInteger(N0.getValueType())) {
3733 if (N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::SUB ||
3734 N0.getOpcode() == ISD::XOR) {
3735 // Simplify (X+Y) == (X+Z) --> Y == Z
3736 if (N0.getOpcode() == N1.getOpcode()) {
3737 if (N0.getOperand(0) == N1.getOperand(0))
3738 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(1), Cond);
3739 if (N0.getOperand(1) == N1.getOperand(1))
3740 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(0), Cond);
Evan Cheng1efba0e2006-08-29 06:42:35 +00003741 if (DAG.isCommutativeBinOp(N0.getOpcode())) {
Nate Begeman452d7be2005-09-16 00:54:12 +00003742 // If X op Y == Y op X, try other combinations.
3743 if (N0.getOperand(0) == N1.getOperand(1))
3744 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(0), Cond);
3745 if (N0.getOperand(1) == N1.getOperand(0))
Chris Lattnera158eee2005-10-25 18:57:30 +00003746 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(1), Cond);
Nate Begeman452d7be2005-09-16 00:54:12 +00003747 }
3748 }
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003749
3750 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(N1)) {
3751 if (ConstantSDNode *LHSR = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
3752 // Turn (X+C1) == C2 --> X == C2-C1
3753 if (N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse()) {
3754 return DAG.getSetCC(VT, N0.getOperand(0),
3755 DAG.getConstant(RHSC->getValue()-LHSR->getValue(),
3756 N0.getValueType()), Cond);
3757 }
3758
3759 // Turn (X^C1) == C2 into X == C1^C2 iff X&~C1 = 0.
3760 if (N0.getOpcode() == ISD::XOR)
Chris Lattner5c46f742005-10-05 06:11:08 +00003761 // If we know that all of the inverted bits are zero, don't bother
3762 // performing the inversion.
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003763 if (TLI.MaskedValueIsZero(N0.getOperand(0), ~LHSR->getValue()))
Chris Lattner5c46f742005-10-05 06:11:08 +00003764 return DAG.getSetCC(VT, N0.getOperand(0),
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003765 DAG.getConstant(LHSR->getValue()^RHSC->getValue(),
Chris Lattner5c46f742005-10-05 06:11:08 +00003766 N0.getValueType()), Cond);
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003767 }
3768
3769 // Turn (C1-X) == C2 --> X == C1-C2
3770 if (ConstantSDNode *SUBC = dyn_cast<ConstantSDNode>(N0.getOperand(0))) {
3771 if (N0.getOpcode() == ISD::SUB && N0.Val->hasOneUse()) {
3772 return DAG.getSetCC(VT, N0.getOperand(1),
3773 DAG.getConstant(SUBC->getValue()-RHSC->getValue(),
3774 N0.getValueType()), Cond);
Chris Lattner5c46f742005-10-05 06:11:08 +00003775 }
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003776 }
3777 }
3778
Nate Begeman452d7be2005-09-16 00:54:12 +00003779 // Simplify (X+Z) == X --> Z == 0
3780 if (N0.getOperand(0) == N1)
3781 return DAG.getSetCC(VT, N0.getOperand(1),
3782 DAG.getConstant(0, N0.getValueType()), Cond);
3783 if (N0.getOperand(1) == N1) {
Evan Cheng1efba0e2006-08-29 06:42:35 +00003784 if (DAG.isCommutativeBinOp(N0.getOpcode()))
Nate Begeman452d7be2005-09-16 00:54:12 +00003785 return DAG.getSetCC(VT, N0.getOperand(0),
3786 DAG.getConstant(0, N0.getValueType()), Cond);
3787 else {
3788 assert(N0.getOpcode() == ISD::SUB && "Unexpected operation!");
3789 // (Z-X) == X --> Z == X<<1
3790 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(),
3791 N1,
3792 DAG.getConstant(1,TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00003793 AddToWorkList(SH.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003794 return DAG.getSetCC(VT, N0.getOperand(0), SH, Cond);
3795 }
3796 }
3797 }
3798
3799 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
3800 N1.getOpcode() == ISD::XOR) {
3801 // Simplify X == (X+Z) --> Z == 0
3802 if (N1.getOperand(0) == N0) {
3803 return DAG.getSetCC(VT, N1.getOperand(1),
3804 DAG.getConstant(0, N1.getValueType()), Cond);
3805 } else if (N1.getOperand(1) == N0) {
Evan Cheng1efba0e2006-08-29 06:42:35 +00003806 if (DAG.isCommutativeBinOp(N1.getOpcode())) {
Nate Begeman452d7be2005-09-16 00:54:12 +00003807 return DAG.getSetCC(VT, N1.getOperand(0),
3808 DAG.getConstant(0, N1.getValueType()), Cond);
3809 } else {
3810 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
3811 // X == (Z-X) --> X<<1 == Z
3812 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(), N0,
3813 DAG.getConstant(1,TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00003814 AddToWorkList(SH.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003815 return DAG.getSetCC(VT, SH, N1.getOperand(0), Cond);
3816 }
3817 }
3818 }
3819 }
3820
3821 // Fold away ALL boolean setcc's.
3822 SDOperand Temp;
Nate Begemane17daeb2005-10-05 21:43:42 +00003823 if (N0.getValueType() == MVT::i1 && foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00003824 switch (Cond) {
3825 default: assert(0 && "Unknown integer setcc!");
3826 case ISD::SETEQ: // X == Y -> (X^Y)^1
3827 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
3828 N0 = DAG.getNode(ISD::XOR, MVT::i1, Temp, DAG.getConstant(1, MVT::i1));
Chris Lattner5750df92006-03-01 04:03:14 +00003829 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003830 break;
3831 case ISD::SETNE: // X != Y --> (X^Y)
3832 N0 = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
3833 break;
3834 case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> X^1 & Y
3835 case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> X^1 & Y
3836 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
3837 N0 = DAG.getNode(ISD::AND, MVT::i1, N1, Temp);
Chris Lattner5750df92006-03-01 04:03:14 +00003838 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003839 break;
3840 case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> Y^1 & X
3841 case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> Y^1 & X
3842 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
3843 N0 = DAG.getNode(ISD::AND, MVT::i1, N0, Temp);
Chris Lattner5750df92006-03-01 04:03:14 +00003844 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003845 break;
3846 case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> X^1 | Y
3847 case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> X^1 | Y
3848 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
3849 N0 = DAG.getNode(ISD::OR, MVT::i1, N1, Temp);
Chris Lattner5750df92006-03-01 04:03:14 +00003850 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003851 break;
3852 case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> Y^1 | X
3853 case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> Y^1 | X
3854 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
3855 N0 = DAG.getNode(ISD::OR, MVT::i1, N0, Temp);
3856 break;
3857 }
3858 if (VT != MVT::i1) {
Chris Lattner5750df92006-03-01 04:03:14 +00003859 AddToWorkList(N0.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003860 // FIXME: If running after legalize, we probably can't do this.
3861 N0 = DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
3862 }
3863 return N0;
3864 }
3865
3866 // Could not fold it.
3867 return SDOperand();
3868}
3869
Nate Begeman69575232005-10-20 02:15:44 +00003870/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
3871/// return a DAG expression to select that will generate the same value by
3872/// multiplying by a magic number. See:
3873/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
3874SDOperand DAGCombiner::BuildSDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00003875 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00003876 SDOperand S = TLI.BuildSDIV(N, DAG, &Built);
3877
Andrew Lenharth232c9102006-06-12 16:07:18 +00003878 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00003879 ii != ee; ++ii)
3880 AddToWorkList(*ii);
3881 return S;
Nate Begeman69575232005-10-20 02:15:44 +00003882}
3883
3884/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
3885/// return a DAG expression to select that will generate the same value by
3886/// multiplying by a magic number. See:
3887/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
3888SDOperand DAGCombiner::BuildUDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00003889 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00003890 SDOperand S = TLI.BuildUDIV(N, DAG, &Built);
Nate Begeman69575232005-10-20 02:15:44 +00003891
Andrew Lenharth232c9102006-06-12 16:07:18 +00003892 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00003893 ii != ee; ++ii)
3894 AddToWorkList(*ii);
3895 return S;
Nate Begeman69575232005-10-20 02:15:44 +00003896}
3897
Jim Laskey71382342006-10-07 23:37:56 +00003898/// FindBaseOffset - Return true if base is known not to alias with anything
3899/// but itself. Provides base object and offset as results.
3900static bool FindBaseOffset(SDOperand Ptr, SDOperand &Base, int64_t &Offset) {
3901 // Assume it is a primitive operation.
3902 Base = Ptr; Offset = 0;
3903
3904 // If it's an adding a simple constant then integrate the offset.
3905 if (Base.getOpcode() == ISD::ADD) {
3906 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Base.getOperand(1))) {
3907 Base = Base.getOperand(0);
3908 Offset += C->getValue();
3909 }
3910 }
3911
3912 // If it's any of the following then it can't alias with anything but itself.
3913 return isa<FrameIndexSDNode>(Base) ||
3914 isa<ConstantPoolSDNode>(Base) ||
3915 isa<GlobalAddressSDNode>(Base);
3916}
3917
3918/// isAlias - Return true if there is any possibility that the two addresses
3919/// overlap.
3920static bool isAlias(SDOperand Ptr1, int64_t Size1, SDOperand SrcValue1,
3921 SDOperand Ptr2, int64_t Size2, SDOperand SrcValue2) {
3922 // If they are the same then they must be aliases.
3923 if (Ptr1 == Ptr2) return true;
3924
3925 // Gather base node and offset information.
3926 SDOperand Base1, Base2;
3927 int64_t Offset1, Offset2;
3928 bool KnownBase1 = FindBaseOffset(Ptr1, Base1, Offset1);
3929 bool KnownBase2 = FindBaseOffset(Ptr2, Base2, Offset2);
3930
3931 // If they have a same base address then...
3932 if (Base1 == Base2) {
3933 // Check to see if the addresses overlap.
3934 return!((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
3935 }
3936
3937 // Otherwise they alias if either is unknown.
3938 return !KnownBase1 || !KnownBase2;
3939}
3940
3941/// FindAliasInfo - Extracts the relevant alias information from the memory
3942/// node. Returns true if the operand was a load.
3943static bool FindAliasInfo(SDNode *N,
3944 SDOperand &Ptr, int64_t &Size, SDOperand &SrcValue) {
3945 switch (N->getOpcode()) {
3946 case ISD::LOAD:
3947 Ptr = N->getOperand(1);
3948 Size = MVT::getSizeInBits(N->getValueType(0)) >> 3;
3949 SrcValue = N->getOperand(2);
3950 return true;
3951 case ISD::STORE:
3952 Ptr = N->getOperand(2);
3953 Size = MVT::getSizeInBits(N->getOperand(1).getValueType()) >> 3;
3954 SrcValue = N->getOperand(3);
3955 break;
3956 default:
3957 assert(0 && "FindAliasInfo expected a memory operand");
3958 break;
3959 }
3960
3961 return false;
3962}
3963
Jim Laskey6ff23e52006-10-04 16:53:27 +00003964/// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
3965/// looking for aliasing nodes and adding them to the Aliases vector.
Jim Laskeybc588b82006-10-05 15:07:25 +00003966void DAGCombiner::GatherAllAliases(SDNode *N, SDOperand OriginalChain,
Jim Laskey6ff23e52006-10-04 16:53:27 +00003967 SmallVector<SDOperand, 8> &Aliases) {
Jim Laskeybc588b82006-10-05 15:07:25 +00003968 SmallVector<SDOperand, 8> Chains; // List of chains to visit.
Jim Laskey6ff23e52006-10-04 16:53:27 +00003969 std::set<SDNode *> Visited; // Visited node set.
3970
Jim Laskey279f0532006-09-25 16:29:54 +00003971 // Get alias information for node.
3972 SDOperand Ptr;
3973 int64_t Size;
3974 SDOperand SrcValue;
Jim Laskeybc588b82006-10-05 15:07:25 +00003975 bool IsLoad = FindAliasInfo(N, Ptr, Size, SrcValue);
Jim Laskey279f0532006-09-25 16:29:54 +00003976
Jim Laskey6ff23e52006-10-04 16:53:27 +00003977 // Starting off.
Jim Laskeybc588b82006-10-05 15:07:25 +00003978 Chains.push_back(OriginalChain);
Jim Laskey6ff23e52006-10-04 16:53:27 +00003979
Jim Laskeybc588b82006-10-05 15:07:25 +00003980 // Look at each chain and determine if it is an alias. If so, add it to the
3981 // aliases list. If not, then continue up the chain looking for the next
3982 // candidate.
3983 while (!Chains.empty()) {
3984 SDOperand Chain = Chains.back();
3985 Chains.pop_back();
Jim Laskey6ff23e52006-10-04 16:53:27 +00003986
Jim Laskeybc588b82006-10-05 15:07:25 +00003987 // Don't bother if we've been before.
3988 if (Visited.find(Chain.Val) != Visited.end()) continue;
3989 Visited.insert(Chain.Val);
3990
3991 switch (Chain.getOpcode()) {
3992 case ISD::EntryToken:
3993 // Entry token is ideal chain operand, but handled in FindBetterChain.
3994 break;
Jim Laskey6ff23e52006-10-04 16:53:27 +00003995
Jim Laskeybc588b82006-10-05 15:07:25 +00003996 case ISD::LOAD:
3997 case ISD::STORE: {
3998 // Get alias information for Chain.
3999 SDOperand OpPtr;
4000 int64_t OpSize;
4001 SDOperand OpSrcValue;
4002 bool IsOpLoad = FindAliasInfo(Chain.Val, OpPtr, OpSize, OpSrcValue);
4003
4004 // If chain is alias then stop here.
4005 if (!(IsLoad && IsOpLoad) &&
4006 isAlias(Ptr, Size, SrcValue, OpPtr, OpSize, OpSrcValue)) {
4007 Aliases.push_back(Chain);
4008 } else {
4009 // Look further up the chain.
4010 Chains.push_back(Chain.getOperand(0));
4011 // Clean up old chain.
4012 AddToWorkList(Chain.Val);
Jim Laskey279f0532006-09-25 16:29:54 +00004013 }
Jim Laskeybc588b82006-10-05 15:07:25 +00004014 break;
4015 }
4016
4017 case ISD::TokenFactor:
4018 // We have to check each of the operands of the token factor, so we queue
4019 // then up. Adding the operands to the queue (stack) in reverse order
4020 // maintains the original order and increases the likelihood that getNode
4021 // will find a matching token factor (CSE.)
4022 for (unsigned n = Chain.getNumOperands(); n;)
4023 Chains.push_back(Chain.getOperand(--n));
4024 // Eliminate the token factor if we can.
4025 AddToWorkList(Chain.Val);
4026 break;
4027
4028 default:
4029 // For all other instructions we will just have to take what we can get.
4030 Aliases.push_back(Chain);
4031 break;
Jim Laskey279f0532006-09-25 16:29:54 +00004032 }
4033 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00004034}
4035
4036/// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, looking
4037/// for a better chain (aliasing node.)
4038SDOperand DAGCombiner::FindBetterChain(SDNode *N, SDOperand OldChain) {
4039 SmallVector<SDOperand, 8> Aliases; // Ops for replacing token factor.
Jim Laskey279f0532006-09-25 16:29:54 +00004040
Jim Laskey6ff23e52006-10-04 16:53:27 +00004041 // Accumulate all the aliases to this node.
4042 GatherAllAliases(N, OldChain, Aliases);
4043
4044 if (Aliases.size() == 0) {
4045 // If no operands then chain to entry token.
4046 return DAG.getEntryNode();
4047 } else if (Aliases.size() == 1) {
4048 // If a single operand then chain to it. We don't need to revisit it.
4049 return Aliases[0];
4050 }
4051
4052 // Construct a custom tailored token factor.
4053 SDOperand NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
4054 &Aliases[0], Aliases.size());
4055
4056 // Make sure the old chain gets cleaned up.
4057 if (NewChain != OldChain) AddToWorkList(OldChain.Val);
4058
4059 return NewChain;
Jim Laskey279f0532006-09-25 16:29:54 +00004060}
4061
Nate Begeman1d4d4142005-09-01 00:19:25 +00004062// SelectionDAG::Combine - This is the entry point for the file.
4063//
Nate Begeman4ebd8052005-09-01 23:24:04 +00004064void SelectionDAG::Combine(bool RunningAfterLegalize) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00004065 /// run - This is the main entry point to this class.
4066 ///
Nate Begeman4ebd8052005-09-01 23:24:04 +00004067 DAGCombiner(*this).Run(RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004068}