blob: 9fdbe266ec2fd2cf4dd5473a44672590aaf523d3 [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"
Jim Laskeyc7c3f112006-10-16 20:52:31 +000033#include "llvm/Analysis/AliasAnalysis.h"
Nate Begeman1d4d4142005-09-01 00:19:25 +000034#include "llvm/CodeGen/SelectionDAG.h"
Nate Begeman2300f552005-09-07 00:15:36 +000035#include "llvm/Support/Debug.h"
Nate Begeman1d4d4142005-09-01 00:19:25 +000036#include "llvm/Support/MathExtras.h"
37#include "llvm/Target/TargetLowering.h"
Chris Lattnerddae4bd2007-01-08 23:04:05 +000038#include "llvm/Target/TargetOptions.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000039#include "llvm/Support/Compiler.h"
Jim Laskeyd1aed7a2006-09-21 16:28:59 +000040#include "llvm/Support/CommandLine.h"
Chris Lattnera500fc62005-09-09 23:53:39 +000041#include <algorithm>
Nate Begeman1d4d4142005-09-01 00:19:25 +000042using namespace llvm;
43
Chris Lattnercd3245a2006-12-19 22:41:21 +000044STATISTIC(NodesCombined , "Number of dag nodes combined");
45STATISTIC(PreIndexedNodes , "Number of pre-indexed nodes created");
46STATISTIC(PostIndexedNodes, "Number of post-indexed nodes created");
47
Nate Begeman1d4d4142005-09-01 00:19:25 +000048namespace {
Chris Lattner938ab022007-01-16 04:55:25 +000049#ifndef NDEBUG
50 static cl::opt<bool>
51 ViewDAGCombine1("view-dag-combine1-dags", cl::Hidden,
52 cl::desc("Pop up a window to show dags before the first "
53 "dag combine pass"));
54 static cl::opt<bool>
55 ViewDAGCombine2("view-dag-combine2-dags", cl::Hidden,
56 cl::desc("Pop up a window to show dags before the second "
57 "dag combine pass"));
58#else
59 static const bool ViewDAGCombine1 = false;
60 static const bool ViewDAGCombine2 = false;
61#endif
62
Jim Laskey71382342006-10-07 23:37:56 +000063 static cl::opt<bool>
64 CombinerAA("combiner-alias-analysis", cl::Hidden,
Jim Laskey26f7fa72006-10-17 19:33:52 +000065 cl::desc("Turn on alias analysis during testing"));
Jim Laskey3ad175b2006-10-12 15:22:24 +000066
Jim Laskey07a27092006-10-18 19:08:31 +000067 static cl::opt<bool>
68 CombinerGlobalAA("combiner-global-alias-analysis", cl::Hidden,
69 cl::desc("Include global information in alias analysis"));
70
Jim Laskeybc588b82006-10-05 15:07:25 +000071//------------------------------ DAGCombiner ---------------------------------//
72
Jim Laskey71382342006-10-07 23:37:56 +000073 class VISIBILITY_HIDDEN DAGCombiner {
Nate Begeman1d4d4142005-09-01 00:19:25 +000074 SelectionDAG &DAG;
75 TargetLowering &TLI;
Nate Begeman4ebd8052005-09-01 23:24:04 +000076 bool AfterLegalize;
Nate Begeman1d4d4142005-09-01 00:19:25 +000077
78 // Worklist of all of the nodes that need to be simplified.
79 std::vector<SDNode*> WorkList;
80
Jim Laskeyc7c3f112006-10-16 20:52:31 +000081 // AA - Used for DAG load/store alias analysis.
82 AliasAnalysis &AA;
83
Nate Begeman1d4d4142005-09-01 00:19:25 +000084 /// AddUsersToWorkList - When an instruction is simplified, add all users of
85 /// the instruction to the work lists because they might get more simplified
86 /// now.
87 ///
88 void AddUsersToWorkList(SDNode *N) {
89 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
Nate Begeman4ebd8052005-09-01 23:24:04 +000090 UI != UE; ++UI)
Jim Laskey6ff23e52006-10-04 16:53:27 +000091 AddToWorkList(*UI);
Nate Begeman1d4d4142005-09-01 00:19:25 +000092 }
93
94 /// removeFromWorkList - remove all instances of N from the worklist.
Chris Lattner5750df92006-03-01 04:03:14 +000095 ///
Nate Begeman1d4d4142005-09-01 00:19:25 +000096 void removeFromWorkList(SDNode *N) {
97 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), N),
98 WorkList.end());
99 }
100
Chris Lattner24664722006-03-01 04:53:38 +0000101 public:
Jim Laskey6ff23e52006-10-04 16:53:27 +0000102 /// AddToWorkList - Add to the work list making sure it's instance is at the
103 /// the back (next to be processed.)
Chris Lattner5750df92006-03-01 04:03:14 +0000104 void AddToWorkList(SDNode *N) {
Jim Laskey6ff23e52006-10-04 16:53:27 +0000105 removeFromWorkList(N);
Chris Lattner5750df92006-03-01 04:03:14 +0000106 WorkList.push_back(N);
107 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000108
Jim Laskey274062c2006-10-13 23:32:28 +0000109 SDOperand CombineTo(SDNode *N, const SDOperand *To, unsigned NumTo,
110 bool AddTo = true) {
Chris Lattner3577e382006-08-11 17:56:38 +0000111 assert(N->getNumValues() == NumTo && "Broken CombineTo call!");
Chris Lattner87514ca2005-10-10 22:31:19 +0000112 ++NodesCombined;
Bill Wendling832171c2006-12-07 20:04:42 +0000113 DOUT << "\nReplacing.1 "; DEBUG(N->dump());
114 DOUT << "\nWith: "; DEBUG(To[0].Val->dump(&DAG));
115 DOUT << " and " << NumTo-1 << " other values\n";
Chris Lattner01a22022005-10-10 22:04:48 +0000116 std::vector<SDNode*> NowDead;
Chris Lattner3577e382006-08-11 17:56:38 +0000117 DAG.ReplaceAllUsesWith(N, To, &NowDead);
Chris Lattner01a22022005-10-10 22:04:48 +0000118
Jim Laskey274062c2006-10-13 23:32:28 +0000119 if (AddTo) {
120 // Push the new nodes and any users onto the worklist
121 for (unsigned i = 0, e = NumTo; i != e; ++i) {
122 AddToWorkList(To[i].Val);
123 AddUsersToWorkList(To[i].Val);
124 }
Chris Lattner01a22022005-10-10 22:04:48 +0000125 }
126
Jim Laskey6ff23e52006-10-04 16:53:27 +0000127 // Nodes can be reintroduced into the worklist. Make sure we do not
128 // process a node that has been replaced.
Chris Lattner01a22022005-10-10 22:04:48 +0000129 removeFromWorkList(N);
130 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
131 removeFromWorkList(NowDead[i]);
132
133 // Finally, since the node is now dead, remove it from the graph.
134 DAG.DeleteNode(N);
135 return SDOperand(N, 0);
136 }
Nate Begeman368e18d2006-02-16 21:11:51 +0000137
Jim Laskey274062c2006-10-13 23:32:28 +0000138 SDOperand CombineTo(SDNode *N, SDOperand Res, bool AddTo = true) {
139 return CombineTo(N, &Res, 1, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000140 }
141
Jim Laskey274062c2006-10-13 23:32:28 +0000142 SDOperand CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1,
143 bool AddTo = true) {
Chris Lattner3577e382006-08-11 17:56:38 +0000144 SDOperand To[] = { Res0, Res1 };
Jim Laskey274062c2006-10-13 23:32:28 +0000145 return CombineTo(N, To, 2, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000146 }
147 private:
148
Chris Lattner012f2412006-02-17 21:58:01 +0000149 /// SimplifyDemandedBits - Check the specified integer node value to see if
Chris Lattnerb2742f42006-03-01 19:55:35 +0000150 /// it can be simplified or if things it uses can be simplified by bit
Chris Lattner012f2412006-02-17 21:58:01 +0000151 /// propagation. If so, return true.
152 bool SimplifyDemandedBits(SDOperand Op) {
Nate Begeman368e18d2006-02-16 21:11:51 +0000153 TargetLowering::TargetLoweringOpt TLO(DAG);
154 uint64_t KnownZero, KnownOne;
Chris Lattner012f2412006-02-17 21:58:01 +0000155 uint64_t Demanded = MVT::getIntVTBitMask(Op.getValueType());
156 if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO))
157 return false;
158
159 // Revisit the node.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000160 AddToWorkList(Op.Val);
Chris Lattner012f2412006-02-17 21:58:01 +0000161
162 // Replace the old value with the new one.
163 ++NodesCombined;
Bill Wendling832171c2006-12-07 20:04:42 +0000164 DOUT << "\nReplacing.2 "; DEBUG(TLO.Old.Val->dump());
165 DOUT << "\nWith: "; DEBUG(TLO.New.Val->dump(&DAG));
166 DOUT << '\n';
Chris Lattner012f2412006-02-17 21:58:01 +0000167
168 std::vector<SDNode*> NowDead;
169 DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New, NowDead);
170
Chris Lattner7d20d392006-02-20 06:51:04 +0000171 // Push the new node and any (possibly new) users onto the worklist.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000172 AddToWorkList(TLO.New.Val);
Chris Lattner012f2412006-02-17 21:58:01 +0000173 AddUsersToWorkList(TLO.New.Val);
174
175 // Nodes can end up on the worklist more than once. Make sure we do
176 // not process a node that has been replaced.
Chris Lattner012f2412006-02-17 21:58:01 +0000177 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
178 removeFromWorkList(NowDead[i]);
179
Chris Lattner7d20d392006-02-20 06:51:04 +0000180 // Finally, if the node is now dead, remove it from the graph. The node
181 // may not be dead if the replacement process recursively simplified to
182 // something else needing this node.
183 if (TLO.Old.Val->use_empty()) {
184 removeFromWorkList(TLO.Old.Val);
185 DAG.DeleteNode(TLO.Old.Val);
186 }
Chris Lattner012f2412006-02-17 21:58:01 +0000187 return true;
Nate Begeman368e18d2006-02-16 21:11:51 +0000188 }
Chris Lattner87514ca2005-10-10 22:31:19 +0000189
Chris Lattner448f2192006-11-11 00:39:41 +0000190 bool CombineToPreIndexedLoadStore(SDNode *N);
191 bool CombineToPostIndexedLoadStore(SDNode *N);
192
193
Nate Begeman1d4d4142005-09-01 00:19:25 +0000194 /// visit - call the node-specific routine that knows how to fold each
195 /// particular type of node.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000196 SDOperand visit(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000197
198 // Visitation implementation - Implement dag node combining for different
199 // node types. The semantics are as follows:
200 // Return Value:
Nate Begeman2300f552005-09-07 00:15:36 +0000201 // SDOperand.Val == 0 - No change was made
Chris Lattner01a22022005-10-10 22:04:48 +0000202 // SDOperand.Val == N - N was replaced, is dead, and is already handled.
Nate Begeman2300f552005-09-07 00:15:36 +0000203 // otherwise - N should be replaced by the returned Operand.
Nate Begeman1d4d4142005-09-01 00:19:25 +0000204 //
Nate Begeman83e75ec2005-09-06 04:43:02 +0000205 SDOperand visitTokenFactor(SDNode *N);
206 SDOperand visitADD(SDNode *N);
207 SDOperand visitSUB(SDNode *N);
Chris Lattner91153682007-03-04 20:03:15 +0000208 SDOperand visitADDC(SDNode *N);
209 SDOperand visitADDE(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000210 SDOperand visitMUL(SDNode *N);
211 SDOperand visitSDIV(SDNode *N);
212 SDOperand visitUDIV(SDNode *N);
213 SDOperand visitSREM(SDNode *N);
214 SDOperand visitUREM(SDNode *N);
215 SDOperand visitMULHU(SDNode *N);
216 SDOperand visitMULHS(SDNode *N);
217 SDOperand visitAND(SDNode *N);
218 SDOperand visitOR(SDNode *N);
219 SDOperand visitXOR(SDNode *N);
Chris Lattneredab1b92006-04-02 03:25:57 +0000220 SDOperand visitVBinOp(SDNode *N, ISD::NodeType IntOp, ISD::NodeType FPOp);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000221 SDOperand visitSHL(SDNode *N);
222 SDOperand visitSRA(SDNode *N);
223 SDOperand visitSRL(SDNode *N);
224 SDOperand visitCTLZ(SDNode *N);
225 SDOperand visitCTTZ(SDNode *N);
226 SDOperand visitCTPOP(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000227 SDOperand visitSELECT(SDNode *N);
228 SDOperand visitSELECT_CC(SDNode *N);
229 SDOperand visitSETCC(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000230 SDOperand visitSIGN_EXTEND(SDNode *N);
231 SDOperand visitZERO_EXTEND(SDNode *N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000232 SDOperand visitANY_EXTEND(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000233 SDOperand visitSIGN_EXTEND_INREG(SDNode *N);
234 SDOperand visitTRUNCATE(SDNode *N);
Chris Lattner94683772005-12-23 05:30:37 +0000235 SDOperand visitBIT_CONVERT(SDNode *N);
Chris Lattner6258fb22006-04-02 02:53:43 +0000236 SDOperand visitVBIT_CONVERT(SDNode *N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000237 SDOperand visitFADD(SDNode *N);
238 SDOperand visitFSUB(SDNode *N);
239 SDOperand visitFMUL(SDNode *N);
240 SDOperand visitFDIV(SDNode *N);
241 SDOperand visitFREM(SDNode *N);
Chris Lattner12d83032006-03-05 05:30:57 +0000242 SDOperand visitFCOPYSIGN(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000243 SDOperand visitSINT_TO_FP(SDNode *N);
244 SDOperand visitUINT_TO_FP(SDNode *N);
245 SDOperand visitFP_TO_SINT(SDNode *N);
246 SDOperand visitFP_TO_UINT(SDNode *N);
247 SDOperand visitFP_ROUND(SDNode *N);
248 SDOperand visitFP_ROUND_INREG(SDNode *N);
249 SDOperand visitFP_EXTEND(SDNode *N);
250 SDOperand visitFNEG(SDNode *N);
251 SDOperand visitFABS(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000252 SDOperand visitBRCOND(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000253 SDOperand visitBR_CC(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000254 SDOperand visitLOAD(SDNode *N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000255 SDOperand visitSTORE(SDNode *N);
Chris Lattnerca242442006-03-19 01:27:56 +0000256 SDOperand visitINSERT_VECTOR_ELT(SDNode *N);
257 SDOperand visitVINSERT_VECTOR_ELT(SDNode *N);
Chris Lattnerd7648c82006-03-28 20:28:38 +0000258 SDOperand visitVBUILD_VECTOR(SDNode *N);
Chris Lattner66445d32006-03-28 22:11:53 +0000259 SDOperand visitVECTOR_SHUFFLE(SDNode *N);
Chris Lattnerf1d0c622006-03-31 22:16:43 +0000260 SDOperand visitVVECTOR_SHUFFLE(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000261
Evan Cheng44f1f092006-04-20 08:56:16 +0000262 SDOperand XformToShuffleWithZero(SDNode *N);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000263 SDOperand ReassociateOps(unsigned Opc, SDOperand LHS, SDOperand RHS);
264
Chris Lattner40c62d52005-10-18 06:04:22 +0000265 bool SimplifySelectOps(SDNode *SELECT, SDOperand LHS, SDOperand RHS);
Chris Lattner35e5c142006-05-05 05:51:50 +0000266 SDOperand SimplifyBinOpWithSameOpcodeHands(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000267 SDOperand SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2);
268 SDOperand SimplifySelectCC(SDOperand N0, SDOperand N1, SDOperand N2,
269 SDOperand N3, ISD::CondCode CC);
Nate Begeman452d7be2005-09-16 00:54:12 +0000270 SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N0, SDOperand N1,
Nate Begemane17daeb2005-10-05 21:43:42 +0000271 ISD::CondCode Cond, bool foldBooleans = true);
Chris Lattner6258fb22006-04-02 02:53:43 +0000272 SDOperand ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(SDNode *, MVT::ValueType);
Nate Begeman69575232005-10-20 02:15:44 +0000273 SDOperand BuildSDIV(SDNode *N);
Chris Lattner516b9622006-09-14 20:50:57 +0000274 SDOperand BuildUDIV(SDNode *N);
275 SDNode *MatchRotate(SDOperand LHS, SDOperand RHS);
Jim Laskey279f0532006-09-25 16:29:54 +0000276
Jim Laskey6ff23e52006-10-04 16:53:27 +0000277 /// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
278 /// looking for aliasing nodes and adding them to the Aliases vector.
Jim Laskeybc588b82006-10-05 15:07:25 +0000279 void GatherAllAliases(SDNode *N, SDOperand OriginalChain,
Jim Laskey6ff23e52006-10-04 16:53:27 +0000280 SmallVector<SDOperand, 8> &Aliases);
281
Jim Laskey096c22e2006-10-18 12:29:57 +0000282 /// isAlias - Return true if there is any possibility that the two addresses
283 /// overlap.
284 bool isAlias(SDOperand Ptr1, int64_t Size1,
285 const Value *SrcValue1, int SrcValueOffset1,
286 SDOperand Ptr2, int64_t Size2,
Jeff Cohend41b30d2006-11-05 19:31:28 +0000287 const Value *SrcValue2, int SrcValueOffset2);
Jim Laskey096c22e2006-10-18 12:29:57 +0000288
Jim Laskey7ca56af2006-10-11 13:47:09 +0000289 /// FindAliasInfo - Extracts the relevant alias information from the memory
290 /// node. Returns true if the operand was a load.
291 bool FindAliasInfo(SDNode *N,
Jim Laskey096c22e2006-10-18 12:29:57 +0000292 SDOperand &Ptr, int64_t &Size,
293 const Value *&SrcValue, int &SrcValueOffset);
Jim Laskey7ca56af2006-10-11 13:47:09 +0000294
Jim Laskey279f0532006-09-25 16:29:54 +0000295 /// FindBetterChain - Walk up chain skipping non-aliasing memory nodes,
Jim Laskey6ff23e52006-10-04 16:53:27 +0000296 /// looking for a better chain (aliasing node.)
Jim Laskey279f0532006-09-25 16:29:54 +0000297 SDOperand FindBetterChain(SDNode *N, SDOperand Chain);
298
Nate Begeman1d4d4142005-09-01 00:19:25 +0000299public:
Jim Laskeyc7c3f112006-10-16 20:52:31 +0000300 DAGCombiner(SelectionDAG &D, AliasAnalysis &A)
301 : DAG(D),
302 TLI(D.getTargetLoweringInfo()),
303 AfterLegalize(false),
304 AA(A) {}
Nate Begeman1d4d4142005-09-01 00:19:25 +0000305
306 /// Run - runs the dag combiner on all nodes in the work list
Nate Begeman4ebd8052005-09-01 23:24:04 +0000307 void Run(bool RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000308 };
309}
310
Chris Lattner24664722006-03-01 04:53:38 +0000311//===----------------------------------------------------------------------===//
312// TargetLowering::DAGCombinerInfo implementation
313//===----------------------------------------------------------------------===//
314
315void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) {
316 ((DAGCombiner*)DC)->AddToWorkList(N);
317}
318
319SDOperand TargetLowering::DAGCombinerInfo::
320CombineTo(SDNode *N, const std::vector<SDOperand> &To) {
Chris Lattner3577e382006-08-11 17:56:38 +0000321 return ((DAGCombiner*)DC)->CombineTo(N, &To[0], To.size());
Chris Lattner24664722006-03-01 04:53:38 +0000322}
323
324SDOperand TargetLowering::DAGCombinerInfo::
325CombineTo(SDNode *N, SDOperand Res) {
326 return ((DAGCombiner*)DC)->CombineTo(N, Res);
327}
328
329
330SDOperand TargetLowering::DAGCombinerInfo::
331CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1) {
332 return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1);
333}
334
335
336
337
338//===----------------------------------------------------------------------===//
339
340
Nate Begeman4ebd8052005-09-01 23:24:04 +0000341// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
342// that selects between the values 1 and 0, making it equivalent to a setcc.
Nate Begeman646d7e22005-09-02 21:18:40 +0000343// Also, set the incoming LHS, RHS, and CC references to the appropriate
344// nodes based on the type of node we are checking. This simplifies life a
345// bit for the callers.
346static bool isSetCCEquivalent(SDOperand N, SDOperand &LHS, SDOperand &RHS,
347 SDOperand &CC) {
348 if (N.getOpcode() == ISD::SETCC) {
349 LHS = N.getOperand(0);
350 RHS = N.getOperand(1);
351 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000352 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000353 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000354 if (N.getOpcode() == ISD::SELECT_CC &&
355 N.getOperand(2).getOpcode() == ISD::Constant &&
356 N.getOperand(3).getOpcode() == ISD::Constant &&
357 cast<ConstantSDNode>(N.getOperand(2))->getValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000358 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
359 LHS = N.getOperand(0);
360 RHS = N.getOperand(1);
361 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000362 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000363 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000364 return false;
365}
366
Nate Begeman99801192005-09-07 23:25:52 +0000367// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
368// one use. If this is true, it allows the users to invert the operation for
369// free when it is profitable to do so.
370static bool isOneUseSetCC(SDOperand N) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000371 SDOperand N0, N1, N2;
Nate Begeman646d7e22005-09-02 21:18:40 +0000372 if (isSetCCEquivalent(N, N0, N1, N2) && N.Val->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000373 return true;
374 return false;
375}
376
Nate Begemancd4d58c2006-02-03 06:46:56 +0000377SDOperand DAGCombiner::ReassociateOps(unsigned Opc, SDOperand N0, SDOperand N1){
378 MVT::ValueType VT = N0.getValueType();
379 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
380 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
381 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
382 if (isa<ConstantSDNode>(N1)) {
383 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(1), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000384 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000385 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(0));
386 } else if (N0.hasOneUse()) {
387 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(0), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000388 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000389 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(1));
390 }
391 }
392 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
393 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
394 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
395 if (isa<ConstantSDNode>(N0)) {
396 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(1), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000397 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000398 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(0));
399 } else if (N1.hasOneUse()) {
400 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(0), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000401 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000402 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(1));
403 }
404 }
405 return SDOperand();
406}
407
Nate Begeman4ebd8052005-09-01 23:24:04 +0000408void DAGCombiner::Run(bool RunningAfterLegalize) {
409 // set the instance variable, so that the various visit routines may use it.
410 AfterLegalize = RunningAfterLegalize;
411
Nate Begeman646d7e22005-09-02 21:18:40 +0000412 // Add all the dag nodes to the worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000413 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
414 E = DAG.allnodes_end(); I != E; ++I)
415 WorkList.push_back(I);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000416
Chris Lattner95038592005-10-05 06:35:28 +0000417 // Create a dummy node (which is not added to allnodes), that adds a reference
418 // to the root node, preventing it from being deleted, and tracking any
419 // changes of the root.
420 HandleSDNode Dummy(DAG.getRoot());
421
Jim Laskey26f7fa72006-10-17 19:33:52 +0000422 // The root of the dag may dangle to deleted nodes until the dag combiner is
423 // done. Set it to null to avoid confusion.
424 DAG.setRoot(SDOperand());
Chris Lattner24664722006-03-01 04:53:38 +0000425
426 /// DagCombineInfo - Expose the DAG combiner to the target combiner impls.
427 TargetLowering::DAGCombinerInfo
Evan Chengfa1eb272007-02-08 22:13:59 +0000428 DagCombineInfo(DAG, !RunningAfterLegalize, false, this);
Jim Laskey6ff23e52006-10-04 16:53:27 +0000429
Nate Begeman1d4d4142005-09-01 00:19:25 +0000430 // while the worklist isn't empty, inspect the node on the end of it and
431 // try and combine it.
432 while (!WorkList.empty()) {
433 SDNode *N = WorkList.back();
434 WorkList.pop_back();
435
436 // If N has no uses, it is dead. Make sure to revisit all N's operands once
Chris Lattner95038592005-10-05 06:35:28 +0000437 // N is deleted from the DAG, since they too may now be dead or may have a
438 // reduced number of uses, allowing other xforms.
439 if (N->use_empty() && N != &Dummy) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000440 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Jim Laskey6ff23e52006-10-04 16:53:27 +0000441 AddToWorkList(N->getOperand(i).Val);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000442
Chris Lattner95038592005-10-05 06:35:28 +0000443 DAG.DeleteNode(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000444 continue;
445 }
446
Nate Begeman83e75ec2005-09-06 04:43:02 +0000447 SDOperand RV = visit(N);
Chris Lattner24664722006-03-01 04:53:38 +0000448
449 // If nothing happened, try a target-specific DAG combine.
450 if (RV.Val == 0) {
Chris Lattner729c6d12006-05-27 00:43:02 +0000451 assert(N->getOpcode() != ISD::DELETED_NODE &&
452 "Node was deleted but visit returned NULL!");
Chris Lattner24664722006-03-01 04:53:38 +0000453 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
454 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode()))
455 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
456 }
457
Nate Begeman83e75ec2005-09-06 04:43:02 +0000458 if (RV.Val) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000459 ++NodesCombined;
Nate Begeman646d7e22005-09-02 21:18:40 +0000460 // If we get back the same node we passed in, rather than a new node or
461 // zero, we know that the node must have defined multiple values and
462 // CombineTo was used. Since CombineTo takes care of the worklist
463 // mechanics for us, we have no work to do in this case.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000464 if (RV.Val != N) {
Chris Lattner729c6d12006-05-27 00:43:02 +0000465 assert(N->getOpcode() != ISD::DELETED_NODE &&
466 RV.Val->getOpcode() != ISD::DELETED_NODE &&
467 "Node was deleted but visit returned new node!");
468
Bill Wendling832171c2006-12-07 20:04:42 +0000469 DOUT << "\nReplacing.3 "; DEBUG(N->dump());
470 DOUT << "\nWith: "; DEBUG(RV.Val->dump(&DAG));
471 DOUT << '\n';
Chris Lattner01a22022005-10-10 22:04:48 +0000472 std::vector<SDNode*> NowDead;
Evan Cheng2adffa12006-09-21 19:04:05 +0000473 if (N->getNumValues() == RV.Val->getNumValues())
474 DAG.ReplaceAllUsesWith(N, RV.Val, &NowDead);
475 else {
476 assert(N->getValueType(0) == RV.getValueType() && "Type mismatch");
477 SDOperand OpV = RV;
478 DAG.ReplaceAllUsesWith(N, &OpV, &NowDead);
479 }
Nate Begeman646d7e22005-09-02 21:18:40 +0000480
481 // Push the new node and any users onto the worklist
Jim Laskey6ff23e52006-10-04 16:53:27 +0000482 AddToWorkList(RV.Val);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000483 AddUsersToWorkList(RV.Val);
Nate Begeman646d7e22005-09-02 21:18:40 +0000484
Jim Laskey6ff23e52006-10-04 16:53:27 +0000485 // Nodes can be reintroduced into the worklist. Make sure we do not
486 // process a node that has been replaced.
Nate Begeman646d7e22005-09-02 21:18:40 +0000487 removeFromWorkList(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000488 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
489 removeFromWorkList(NowDead[i]);
Chris Lattner5c46f742005-10-05 06:11:08 +0000490
491 // Finally, since the node is now dead, remove it from the graph.
492 DAG.DeleteNode(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000493 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000494 }
495 }
Chris Lattner95038592005-10-05 06:35:28 +0000496
497 // If the root changed (e.g. it was a dead load, update the root).
498 DAG.setRoot(Dummy.getValue());
Nate Begeman1d4d4142005-09-01 00:19:25 +0000499}
500
Nate Begeman83e75ec2005-09-06 04:43:02 +0000501SDOperand DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000502 switch(N->getOpcode()) {
503 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +0000504 case ISD::TokenFactor: return visitTokenFactor(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000505 case ISD::ADD: return visitADD(N);
506 case ISD::SUB: return visitSUB(N);
Chris Lattner91153682007-03-04 20:03:15 +0000507 case ISD::ADDC: return visitADDC(N);
508 case ISD::ADDE: return visitADDE(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000509 case ISD::MUL: return visitMUL(N);
510 case ISD::SDIV: return visitSDIV(N);
511 case ISD::UDIV: return visitUDIV(N);
512 case ISD::SREM: return visitSREM(N);
513 case ISD::UREM: return visitUREM(N);
514 case ISD::MULHU: return visitMULHU(N);
515 case ISD::MULHS: return visitMULHS(N);
516 case ISD::AND: return visitAND(N);
517 case ISD::OR: return visitOR(N);
518 case ISD::XOR: return visitXOR(N);
519 case ISD::SHL: return visitSHL(N);
520 case ISD::SRA: return visitSRA(N);
521 case ISD::SRL: return visitSRL(N);
522 case ISD::CTLZ: return visitCTLZ(N);
523 case ISD::CTTZ: return visitCTTZ(N);
524 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000525 case ISD::SELECT: return visitSELECT(N);
526 case ISD::SELECT_CC: return visitSELECT_CC(N);
527 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000528 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
529 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000530 case ISD::ANY_EXTEND: return visitANY_EXTEND(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000531 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
532 case ISD::TRUNCATE: return visitTRUNCATE(N);
Chris Lattner94683772005-12-23 05:30:37 +0000533 case ISD::BIT_CONVERT: return visitBIT_CONVERT(N);
Chris Lattner6258fb22006-04-02 02:53:43 +0000534 case ISD::VBIT_CONVERT: return visitVBIT_CONVERT(N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000535 case ISD::FADD: return visitFADD(N);
536 case ISD::FSUB: return visitFSUB(N);
537 case ISD::FMUL: return visitFMUL(N);
538 case ISD::FDIV: return visitFDIV(N);
539 case ISD::FREM: return visitFREM(N);
Chris Lattner12d83032006-03-05 05:30:57 +0000540 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000541 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
542 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
543 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
544 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
545 case ISD::FP_ROUND: return visitFP_ROUND(N);
546 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
547 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
548 case ISD::FNEG: return visitFNEG(N);
549 case ISD::FABS: return visitFABS(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000550 case ISD::BRCOND: return visitBRCOND(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000551 case ISD::BR_CC: return visitBR_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000552 case ISD::LOAD: return visitLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000553 case ISD::STORE: return visitSTORE(N);
Chris Lattnerca242442006-03-19 01:27:56 +0000554 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
555 case ISD::VINSERT_VECTOR_ELT: return visitVINSERT_VECTOR_ELT(N);
Chris Lattnerd7648c82006-03-28 20:28:38 +0000556 case ISD::VBUILD_VECTOR: return visitVBUILD_VECTOR(N);
Chris Lattner66445d32006-03-28 22:11:53 +0000557 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
Chris Lattnerf1d0c622006-03-31 22:16:43 +0000558 case ISD::VVECTOR_SHUFFLE: return visitVVECTOR_SHUFFLE(N);
Chris Lattneredab1b92006-04-02 03:25:57 +0000559 case ISD::VADD: return visitVBinOp(N, ISD::ADD , ISD::FADD);
560 case ISD::VSUB: return visitVBinOp(N, ISD::SUB , ISD::FSUB);
561 case ISD::VMUL: return visitVBinOp(N, ISD::MUL , ISD::FMUL);
562 case ISD::VSDIV: return visitVBinOp(N, ISD::SDIV, ISD::FDIV);
563 case ISD::VUDIV: return visitVBinOp(N, ISD::UDIV, ISD::UDIV);
564 case ISD::VAND: return visitVBinOp(N, ISD::AND , ISD::AND);
565 case ISD::VOR: return visitVBinOp(N, ISD::OR , ISD::OR);
566 case ISD::VXOR: return visitVBinOp(N, ISD::XOR , ISD::XOR);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000567 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000568 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000569}
570
Chris Lattner6270f682006-10-08 22:57:01 +0000571/// getInputChainForNode - Given a node, return its input chain if it has one,
572/// otherwise return a null sd operand.
573static SDOperand getInputChainForNode(SDNode *N) {
574 if (unsigned NumOps = N->getNumOperands()) {
575 if (N->getOperand(0).getValueType() == MVT::Other)
576 return N->getOperand(0);
577 else if (N->getOperand(NumOps-1).getValueType() == MVT::Other)
578 return N->getOperand(NumOps-1);
579 for (unsigned i = 1; i < NumOps-1; ++i)
580 if (N->getOperand(i).getValueType() == MVT::Other)
581 return N->getOperand(i);
582 }
583 return SDOperand(0, 0);
584}
585
Nate Begeman83e75ec2005-09-06 04:43:02 +0000586SDOperand DAGCombiner::visitTokenFactor(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +0000587 // If N has two operands, where one has an input chain equal to the other,
588 // the 'other' chain is redundant.
589 if (N->getNumOperands() == 2) {
590 if (getInputChainForNode(N->getOperand(0).Val) == N->getOperand(1))
591 return N->getOperand(0);
592 if (getInputChainForNode(N->getOperand(1).Val) == N->getOperand(0))
593 return N->getOperand(1);
594 }
595
596
Jim Laskey6ff23e52006-10-04 16:53:27 +0000597 SmallVector<SDNode *, 8> TFs; // List of token factors to visit.
Jim Laskey279f0532006-09-25 16:29:54 +0000598 SmallVector<SDOperand, 8> Ops; // Ops for replacing token factor.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000599 bool Changed = false; // If we should replace this token factor.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000600
601 // Start out with this token factor.
Jim Laskey279f0532006-09-25 16:29:54 +0000602 TFs.push_back(N);
Jim Laskey279f0532006-09-25 16:29:54 +0000603
Jim Laskey71382342006-10-07 23:37:56 +0000604 // Iterate through token factors. The TFs grows when new token factors are
Jim Laskeybc588b82006-10-05 15:07:25 +0000605 // encountered.
606 for (unsigned i = 0; i < TFs.size(); ++i) {
607 SDNode *TF = TFs[i];
608
Jim Laskey6ff23e52006-10-04 16:53:27 +0000609 // Check each of the operands.
610 for (unsigned i = 0, ie = TF->getNumOperands(); i != ie; ++i) {
611 SDOperand Op = TF->getOperand(i);
Jim Laskey279f0532006-09-25 16:29:54 +0000612
Jim Laskey6ff23e52006-10-04 16:53:27 +0000613 switch (Op.getOpcode()) {
614 case ISD::EntryToken:
Jim Laskeybc588b82006-10-05 15:07:25 +0000615 // Entry tokens don't need to be added to the list. They are
616 // rededundant.
617 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000618 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000619
Jim Laskey6ff23e52006-10-04 16:53:27 +0000620 case ISD::TokenFactor:
Jim Laskeybc588b82006-10-05 15:07:25 +0000621 if ((CombinerAA || Op.hasOneUse()) &&
622 std::find(TFs.begin(), TFs.end(), Op.Val) == TFs.end()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +0000623 // Queue up for processing.
624 TFs.push_back(Op.Val);
625 // Clean up in case the token factor is removed.
626 AddToWorkList(Op.Val);
627 Changed = true;
628 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000629 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000630 // Fall thru
631
632 default:
Jim Laskeybc588b82006-10-05 15:07:25 +0000633 // Only add if not there prior.
634 if (std::find(Ops.begin(), Ops.end(), Op) == Ops.end())
635 Ops.push_back(Op);
Jim Laskey6ff23e52006-10-04 16:53:27 +0000636 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000637 }
638 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000639 }
640
641 SDOperand Result;
642
643 // If we've change things around then replace token factor.
644 if (Changed) {
645 if (Ops.size() == 0) {
646 // The entry token is the only possible outcome.
647 Result = DAG.getEntryNode();
648 } else {
649 // New and improved token factor.
650 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0], Ops.size());
Nate Begemanded49632005-10-13 03:11:28 +0000651 }
Jim Laskey274062c2006-10-13 23:32:28 +0000652
653 // Don't add users to work list.
654 return CombineTo(N, Result, false);
Nate Begemanded49632005-10-13 03:11:28 +0000655 }
Jim Laskey279f0532006-09-25 16:29:54 +0000656
Jim Laskey6ff23e52006-10-04 16:53:27 +0000657 return Result;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000658}
659
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000660static
661SDOperand combineShlAddConstant(SDOperand N0, SDOperand N1, SelectionDAG &DAG) {
662 MVT::ValueType VT = N0.getValueType();
663 SDOperand N00 = N0.getOperand(0);
664 SDOperand N01 = N0.getOperand(1);
665 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N01);
666 if (N01C && N00.getOpcode() == ISD::ADD && N00.Val->hasOneUse() &&
667 isa<ConstantSDNode>(N00.getOperand(1))) {
668 N0 = DAG.getNode(ISD::ADD, VT,
669 DAG.getNode(ISD::SHL, VT, N00.getOperand(0), N01),
670 DAG.getNode(ISD::SHL, VT, N00.getOperand(1), N01));
671 return DAG.getNode(ISD::ADD, VT, N0, N1);
672 }
673 return SDOperand();
674}
675
Nate Begeman83e75ec2005-09-06 04:43:02 +0000676SDOperand DAGCombiner::visitADD(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000677 SDOperand N0 = N->getOperand(0);
678 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000679 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
680 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemanf89d78d2005-09-07 16:09:19 +0000681 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000682
683 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000684 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000685 return DAG.getNode(ISD::ADD, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000686 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000687 if (N0C && !N1C)
688 return DAG.getNode(ISD::ADD, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000689 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000690 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000691 return N0;
Chris Lattner4aafb4f2006-01-12 20:22:43 +0000692 // fold ((c1-A)+c2) -> (c1+c2)-A
693 if (N1C && N0.getOpcode() == ISD::SUB)
694 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
695 return DAG.getNode(ISD::SUB, VT,
696 DAG.getConstant(N1C->getValue()+N0C->getValue(), VT),
697 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000698 // reassociate add
699 SDOperand RADD = ReassociateOps(ISD::ADD, N0, N1);
700 if (RADD.Val != 0)
701 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000702 // fold ((0-A) + B) -> B-A
703 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
704 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000705 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000706 // fold (A + (0-B)) -> A-B
707 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
708 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000709 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +0000710 // fold (A+(B-A)) -> B
711 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000712 return N1.getOperand(0);
Chris Lattner947c2892006-03-13 06:51:27 +0000713
Evan Cheng860771d2006-03-01 01:09:54 +0000714 if (!MVT::isVector(VT) && SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +0000715 return SDOperand(N, 0);
Chris Lattner947c2892006-03-13 06:51:27 +0000716
717 // fold (a+b) -> (a|b) iff a and b share no bits.
718 if (MVT::isInteger(VT) && !MVT::isVector(VT)) {
719 uint64_t LHSZero, LHSOne;
720 uint64_t RHSZero, RHSOne;
721 uint64_t Mask = MVT::getIntVTBitMask(VT);
722 TLI.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
723 if (LHSZero) {
724 TLI.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
725
726 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
727 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
728 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
729 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
730 return DAG.getNode(ISD::OR, VT, N0, N1);
731 }
732 }
Evan Cheng3ef554d2006-11-06 08:14:30 +0000733
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000734 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
735 if (N0.getOpcode() == ISD::SHL && N0.Val->hasOneUse()) {
736 SDOperand Result = combineShlAddConstant(N0, N1, DAG);
737 if (Result.Val) return Result;
738 }
739 if (N1.getOpcode() == ISD::SHL && N1.Val->hasOneUse()) {
740 SDOperand Result = combineShlAddConstant(N1, N0, DAG);
741 if (Result.Val) return Result;
742 }
743
Nate Begeman83e75ec2005-09-06 04:43:02 +0000744 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000745}
746
Chris Lattner91153682007-03-04 20:03:15 +0000747SDOperand DAGCombiner::visitADDC(SDNode *N) {
748 SDOperand N0 = N->getOperand(0);
749 SDOperand N1 = N->getOperand(1);
750 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
751 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
752 MVT::ValueType VT = N0.getValueType();
753
754 // If the flag result is dead, turn this into an ADD.
755 if (N->hasNUsesOfValue(0, 1))
756 return CombineTo(N, DAG.getNode(ISD::ADD, VT, N1, N0),
757 SDOperand(N, 1));
758
759 // canonicalize constant to RHS.
Chris Lattnerbcf24842007-03-04 20:08:45 +0000760 if (N0C && !N1C) {
761 SDOperand Ops[] = { N1, N0 };
762 return DAG.getNode(ISD::ADDC, N->getVTList(), Ops, 2);
763 }
Chris Lattner91153682007-03-04 20:03:15 +0000764
765 // fold (add x, 0) -> x + no carry out
766 //if (N1C && N1C->isNullValue())
767 // return N0;
768
769 return SDOperand();
770}
771
772SDOperand DAGCombiner::visitADDE(SDNode *N) {
773 SDOperand N0 = N->getOperand(0);
774 SDOperand N1 = N->getOperand(1);
775 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
776 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Chris Lattnerbcf24842007-03-04 20:08:45 +0000777 //MVT::ValueType VT = N0.getValueType();
Chris Lattner91153682007-03-04 20:03:15 +0000778
779 // canonicalize constant to RHS
Chris Lattnerbcf24842007-03-04 20:08:45 +0000780 if (N0C && !N1C) {
781 SDOperand Ops[] = { N1, N0, N->getOperand(2) };
782 return DAG.getNode(ISD::ADDE, N->getVTList(), Ops, 3);
783 }
Chris Lattner91153682007-03-04 20:03:15 +0000784
785 // fold (add x, 0) -> x
786 //if (N1C && N1C->isNullValue())
787 // return N0;
788
789 return SDOperand();
790}
791
792
793
Nate Begeman83e75ec2005-09-06 04:43:02 +0000794SDOperand DAGCombiner::visitSUB(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000795 SDOperand N0 = N->getOperand(0);
796 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000797 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
798 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000799 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000800
Chris Lattner854077d2005-10-17 01:07:11 +0000801 // fold (sub x, x) -> 0
802 if (N0 == N1)
803 return DAG.getConstant(0, N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000804 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000805 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000806 return DAG.getNode(ISD::SUB, VT, N0, N1);
Chris Lattner05b57432005-10-11 06:07:15 +0000807 // fold (sub x, c) -> (add x, -c)
808 if (N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000809 return DAG.getNode(ISD::ADD, VT, N0, DAG.getConstant(-N1C->getValue(), VT));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000810 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +0000811 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000812 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000813 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +0000814 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000815 return N0.getOperand(0);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000816 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000817}
818
Nate Begeman83e75ec2005-09-06 04:43:02 +0000819SDOperand DAGCombiner::visitMUL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000820 SDOperand N0 = N->getOperand(0);
821 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000822 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
823 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman223df222005-09-08 20:18:10 +0000824 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000825
826 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000827 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000828 return DAG.getNode(ISD::MUL, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000829 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000830 if (N0C && !N1C)
831 return DAG.getNode(ISD::MUL, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000832 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000833 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000834 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000835 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +0000836 if (N1C && N1C->isAllOnesValue())
Nate Begeman405e3ec2005-10-21 00:02:42 +0000837 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000838 // fold (mul x, (1 << c)) -> x << c
Nate Begeman646d7e22005-09-02 21:18:40 +0000839 if (N1C && isPowerOf2_64(N1C->getValue()))
Chris Lattner3e6099b2005-10-30 06:41:49 +0000840 return DAG.getNode(ISD::SHL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000841 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000842 TLI.getShiftAmountTy()));
Chris Lattner3e6099b2005-10-30 06:41:49 +0000843 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
844 if (N1C && isPowerOf2_64(-N1C->getSignExtended())) {
845 // FIXME: If the input is something that is easily negated (e.g. a
846 // single-use add), we should put the negate there.
847 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT),
848 DAG.getNode(ISD::SHL, VT, N0,
849 DAG.getConstant(Log2_64(-N1C->getSignExtended()),
850 TLI.getShiftAmountTy())));
851 }
Andrew Lenharth50a0d422006-04-02 21:42:45 +0000852
Chris Lattner0b1a85f2006-03-01 03:44:24 +0000853 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
854 if (N1C && N0.getOpcode() == ISD::SHL &&
855 isa<ConstantSDNode>(N0.getOperand(1))) {
856 SDOperand C3 = DAG.getNode(ISD::SHL, VT, N1, N0.getOperand(1));
Chris Lattner5750df92006-03-01 04:03:14 +0000857 AddToWorkList(C3.Val);
Chris Lattner0b1a85f2006-03-01 03:44:24 +0000858 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0), C3);
859 }
860
861 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
862 // use.
863 {
864 SDOperand Sh(0,0), Y(0,0);
865 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
866 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
867 N0.Val->hasOneUse()) {
868 Sh = N0; Y = N1;
869 } else if (N1.getOpcode() == ISD::SHL &&
870 isa<ConstantSDNode>(N1.getOperand(1)) && N1.Val->hasOneUse()) {
871 Sh = N1; Y = N0;
872 }
873 if (Sh.Val) {
874 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Sh.getOperand(0), Y);
875 return DAG.getNode(ISD::SHL, VT, Mul, Sh.getOperand(1));
876 }
877 }
Chris Lattnera1deca32006-03-04 23:33:26 +0000878 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
879 if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() &&
880 isa<ConstantSDNode>(N0.getOperand(1))) {
881 return DAG.getNode(ISD::ADD, VT,
882 DAG.getNode(ISD::MUL, VT, N0.getOperand(0), N1),
883 DAG.getNode(ISD::MUL, VT, N0.getOperand(1), N1));
884 }
Chris Lattner0b1a85f2006-03-01 03:44:24 +0000885
Nate Begemancd4d58c2006-02-03 06:46:56 +0000886 // reassociate mul
887 SDOperand RMUL = ReassociateOps(ISD::MUL, N0, N1);
888 if (RMUL.Val != 0)
889 return RMUL;
Nate Begeman83e75ec2005-09-06 04:43:02 +0000890 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000891}
892
Nate Begeman83e75ec2005-09-06 04:43:02 +0000893SDOperand DAGCombiner::visitSDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000894 SDOperand N0 = N->getOperand(0);
895 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000896 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
897 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000898 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000899
900 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000901 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000902 return DAG.getNode(ISD::SDIV, VT, N0, N1);
Nate Begeman405e3ec2005-10-21 00:02:42 +0000903 // fold (sdiv X, 1) -> X
904 if (N1C && N1C->getSignExtended() == 1LL)
905 return N0;
906 // fold (sdiv X, -1) -> 0-X
907 if (N1C && N1C->isAllOnesValue())
908 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +0000909 // If we know the sign bits of both operands are zero, strength reduce to a
910 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
911 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000912 if (TLI.MaskedValueIsZero(N1, SignBit) &&
913 TLI.MaskedValueIsZero(N0, SignBit))
Chris Lattner094c8fc2005-10-07 06:10:46 +0000914 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
Nate Begemancd6a6ed2006-02-17 07:26:20 +0000915 // fold (sdiv X, pow2) -> simple ops after legalize
Nate Begemanfb7217b2006-02-17 19:54:08 +0000916 if (N1C && N1C->getValue() && !TLI.isIntDivCheap() &&
Nate Begeman405e3ec2005-10-21 00:02:42 +0000917 (isPowerOf2_64(N1C->getSignExtended()) ||
918 isPowerOf2_64(-N1C->getSignExtended()))) {
919 // If dividing by powers of two is cheap, then don't perform the following
920 // fold.
921 if (TLI.isPow2DivCheap())
922 return SDOperand();
923 int64_t pow2 = N1C->getSignExtended();
924 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
Chris Lattner8f4880b2006-02-16 08:02:36 +0000925 unsigned lg2 = Log2_64(abs2);
926 // Splat the sign bit into the register
927 SDOperand SGN = DAG.getNode(ISD::SRA, VT, N0,
Nate Begeman405e3ec2005-10-21 00:02:42 +0000928 DAG.getConstant(MVT::getSizeInBits(VT)-1,
929 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +0000930 AddToWorkList(SGN.Val);
Chris Lattner8f4880b2006-02-16 08:02:36 +0000931 // Add (N0 < 0) ? abs2 - 1 : 0;
932 SDOperand SRL = DAG.getNode(ISD::SRL, VT, SGN,
933 DAG.getConstant(MVT::getSizeInBits(VT)-lg2,
Nate Begeman405e3ec2005-10-21 00:02:42 +0000934 TLI.getShiftAmountTy()));
Chris Lattner8f4880b2006-02-16 08:02:36 +0000935 SDOperand ADD = DAG.getNode(ISD::ADD, VT, N0, SRL);
Chris Lattner5750df92006-03-01 04:03:14 +0000936 AddToWorkList(SRL.Val);
937 AddToWorkList(ADD.Val); // Divide by pow2
Chris Lattner8f4880b2006-02-16 08:02:36 +0000938 SDOperand SRA = DAG.getNode(ISD::SRA, VT, ADD,
939 DAG.getConstant(lg2, TLI.getShiftAmountTy()));
Nate Begeman405e3ec2005-10-21 00:02:42 +0000940 // If we're dividing by a positive value, we're done. Otherwise, we must
941 // negate the result.
942 if (pow2 > 0)
943 return SRA;
Chris Lattner5750df92006-03-01 04:03:14 +0000944 AddToWorkList(SRA.Val);
Nate Begeman405e3ec2005-10-21 00:02:42 +0000945 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), SRA);
946 }
Nate Begeman69575232005-10-20 02:15:44 +0000947 // if integer divide is expensive and we satisfy the requirements, emit an
948 // alternate sequence.
Nate Begeman405e3ec2005-10-21 00:02:42 +0000949 if (N1C && (N1C->getSignExtended() < -1 || N1C->getSignExtended() > 1) &&
Chris Lattnere9936d12005-10-22 18:50:15 +0000950 !TLI.isIntDivCheap()) {
951 SDOperand Op = BuildSDIV(N);
952 if (Op.Val) return Op;
Nate Begeman69575232005-10-20 02:15:44 +0000953 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000954 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000955}
956
Nate Begeman83e75ec2005-09-06 04:43:02 +0000957SDOperand DAGCombiner::visitUDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000958 SDOperand N0 = N->getOperand(0);
959 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000960 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
961 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000962 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000963
964 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000965 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000966 return DAG.getNode(ISD::UDIV, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000967 // fold (udiv x, (1 << c)) -> x >>u c
Nate Begeman646d7e22005-09-02 21:18:40 +0000968 if (N1C && isPowerOf2_64(N1C->getValue()))
Nate Begemanfb5e4bd2006-02-05 07:20:23 +0000969 return DAG.getNode(ISD::SRL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000970 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000971 TLI.getShiftAmountTy()));
Nate Begemanfb5e4bd2006-02-05 07:20:23 +0000972 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
973 if (N1.getOpcode() == ISD::SHL) {
974 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
975 if (isPowerOf2_64(SHC->getValue())) {
976 MVT::ValueType ADDVT = N1.getOperand(1).getValueType();
Nate Begemanc031e332006-02-05 07:36:48 +0000977 SDOperand Add = DAG.getNode(ISD::ADD, ADDVT, N1.getOperand(1),
978 DAG.getConstant(Log2_64(SHC->getValue()),
979 ADDVT));
Chris Lattner5750df92006-03-01 04:03:14 +0000980 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +0000981 return DAG.getNode(ISD::SRL, VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +0000982 }
983 }
984 }
Nate Begeman69575232005-10-20 02:15:44 +0000985 // fold (udiv x, c) -> alternate
Chris Lattnere9936d12005-10-22 18:50:15 +0000986 if (N1C && N1C->getValue() && !TLI.isIntDivCheap()) {
987 SDOperand Op = BuildUDIV(N);
988 if (Op.Val) return Op;
989 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000990 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000991}
992
Nate Begeman83e75ec2005-09-06 04:43:02 +0000993SDOperand DAGCombiner::visitSREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000994 SDOperand N0 = N->getOperand(0);
995 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000996 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
997 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +0000998 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000999
1000 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001001 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001002 return DAG.getNode(ISD::SREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +00001003 // If we know the sign bits of both operands are zero, strength reduce to a
1004 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
1005 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001006 if (TLI.MaskedValueIsZero(N1, SignBit) &&
1007 TLI.MaskedValueIsZero(N0, SignBit))
Nate Begemana148d982006-01-18 22:35:16 +00001008 return DAG.getNode(ISD::UREM, VT, N0, N1);
Chris Lattner26d29902006-10-12 20:58:32 +00001009
1010 // Unconditionally lower X%C -> X-X/C*C. This allows the X/C logic to hack on
1011 // the remainder operation.
1012 if (N1C && !N1C->isNullValue()) {
1013 SDOperand Div = DAG.getNode(ISD::SDIV, VT, N0, N1);
1014 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Div, N1);
1015 SDOperand Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
1016 AddToWorkList(Div.Val);
1017 AddToWorkList(Mul.Val);
1018 return Sub;
1019 }
1020
Nate Begeman83e75ec2005-09-06 04:43:02 +00001021 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001022}
1023
Nate Begeman83e75ec2005-09-06 04:43:02 +00001024SDOperand DAGCombiner::visitUREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001025 SDOperand N0 = N->getOperand(0);
1026 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001027 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1028 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +00001029 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001030
1031 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001032 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001033 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +00001034 // fold (urem x, pow2) -> (and x, pow2-1)
1035 if (N1C && !N1C->isNullValue() && isPowerOf2_64(N1C->getValue()))
Nate Begemana148d982006-01-18 22:35:16 +00001036 return DAG.getNode(ISD::AND, VT, N0, DAG.getConstant(N1C->getValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +00001037 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
1038 if (N1.getOpcode() == ISD::SHL) {
1039 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
1040 if (isPowerOf2_64(SHC->getValue())) {
Nate Begemanbab92392006-02-05 08:07:24 +00001041 SDOperand Add = DAG.getNode(ISD::ADD, VT, N1,DAG.getConstant(~0ULL,VT));
Chris Lattner5750df92006-03-01 04:03:14 +00001042 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +00001043 return DAG.getNode(ISD::AND, VT, N0, Add);
1044 }
1045 }
1046 }
Chris Lattner26d29902006-10-12 20:58:32 +00001047
1048 // Unconditionally lower X%C -> X-X/C*C. This allows the X/C logic to hack on
1049 // the remainder operation.
1050 if (N1C && !N1C->isNullValue()) {
1051 SDOperand Div = DAG.getNode(ISD::UDIV, VT, N0, N1);
1052 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Div, N1);
1053 SDOperand Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
1054 AddToWorkList(Div.Val);
1055 AddToWorkList(Mul.Val);
1056 return Sub;
1057 }
1058
Nate Begeman83e75ec2005-09-06 04:43:02 +00001059 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001060}
1061
Nate Begeman83e75ec2005-09-06 04:43:02 +00001062SDOperand DAGCombiner::visitMULHS(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001063 SDOperand N0 = N->getOperand(0);
1064 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001065 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001066
1067 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001068 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001069 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001070 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001071 if (N1C && N1C->getValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001072 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
1073 DAG.getConstant(MVT::getSizeInBits(N0.getValueType())-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001074 TLI.getShiftAmountTy()));
1075 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001076}
1077
Nate Begeman83e75ec2005-09-06 04:43:02 +00001078SDOperand DAGCombiner::visitMULHU(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001079 SDOperand N0 = N->getOperand(0);
1080 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001081 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001082
1083 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001084 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001085 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001086 // fold (mulhu x, 1) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001087 if (N1C && N1C->getValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001088 return DAG.getConstant(0, N0.getValueType());
1089 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001090}
1091
Chris Lattner35e5c142006-05-05 05:51:50 +00001092/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
1093/// two operands of the same opcode, try to simplify it.
1094SDOperand DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
1095 SDOperand N0 = N->getOperand(0), N1 = N->getOperand(1);
1096 MVT::ValueType VT = N0.getValueType();
1097 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
1098
Chris Lattner540121f2006-05-05 06:31:05 +00001099 // For each of OP in AND/OR/XOR:
1100 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
1101 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
1102 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
Chris Lattner0d8dae72006-05-05 06:32:04 +00001103 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y))
Chris Lattner540121f2006-05-05 06:31:05 +00001104 if ((N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND||
Chris Lattner0d8dae72006-05-05 06:32:04 +00001105 N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::TRUNCATE) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001106 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1107 SDOperand ORNode = DAG.getNode(N->getOpcode(),
1108 N0.getOperand(0).getValueType(),
1109 N0.getOperand(0), N1.getOperand(0));
1110 AddToWorkList(ORNode.Val);
Chris Lattner540121f2006-05-05 06:31:05 +00001111 return DAG.getNode(N0.getOpcode(), VT, ORNode);
Chris Lattner35e5c142006-05-05 05:51:50 +00001112 }
1113
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001114 // For each of OP in SHL/SRL/SRA/AND...
1115 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
1116 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
1117 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
Chris Lattner35e5c142006-05-05 05:51:50 +00001118 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001119 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001120 N0.getOperand(1) == N1.getOperand(1)) {
1121 SDOperand ORNode = DAG.getNode(N->getOpcode(),
1122 N0.getOperand(0).getValueType(),
1123 N0.getOperand(0), N1.getOperand(0));
1124 AddToWorkList(ORNode.Val);
1125 return DAG.getNode(N0.getOpcode(), VT, ORNode, N0.getOperand(1));
1126 }
1127
1128 return SDOperand();
1129}
1130
Nate Begeman83e75ec2005-09-06 04:43:02 +00001131SDOperand DAGCombiner::visitAND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001132 SDOperand N0 = N->getOperand(0);
1133 SDOperand N1 = N->getOperand(1);
Nate Begemanfb7217b2006-02-17 19:54:08 +00001134 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001135 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1136 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001137 MVT::ValueType VT = N1.getValueType();
1138
1139 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001140 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001141 return DAG.getNode(ISD::AND, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001142 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001143 if (N0C && !N1C)
1144 return DAG.getNode(ISD::AND, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001145 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001146 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001147 return N0;
1148 // if (and x, c) is known to be zero, return 0
Nate Begeman368e18d2006-02-16 21:11:51 +00001149 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001150 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +00001151 // reassociate and
1152 SDOperand RAND = ReassociateOps(ISD::AND, N0, N1);
1153 if (RAND.Val != 0)
1154 return RAND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001155 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
Nate Begeman5dc7e862005-11-02 18:42:59 +00001156 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001157 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Nate Begeman646d7e22005-09-02 21:18:40 +00001158 if ((ORI->getValue() & N1C->getValue()) == N1C->getValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001159 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +00001160 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
1161 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Chris Lattner1ec05d12006-03-01 21:47:21 +00001162 unsigned InMask = MVT::getIntVTBitMask(N0.getOperand(0).getValueType());
Chris Lattner3603cd62006-02-02 07:17:31 +00001163 if (TLI.MaskedValueIsZero(N0.getOperand(0),
Chris Lattner1ec05d12006-03-01 21:47:21 +00001164 ~N1C->getValue() & InMask)) {
1165 SDOperand Zext = DAG.getNode(ISD::ZERO_EXTEND, N0.getValueType(),
1166 N0.getOperand(0));
1167
1168 // Replace uses of the AND with uses of the Zero extend node.
1169 CombineTo(N, Zext);
1170
Chris Lattner3603cd62006-02-02 07:17:31 +00001171 // We actually want to replace all uses of the any_extend with the
1172 // zero_extend, to avoid duplicating things. This will later cause this
1173 // AND to be folded.
Chris Lattner1ec05d12006-03-01 21:47:21 +00001174 CombineTo(N0.Val, Zext);
Chris Lattnerfedced72006-04-20 23:55:59 +00001175 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner3603cd62006-02-02 07:17:31 +00001176 }
1177 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001178 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
1179 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1180 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1181 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1182
1183 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1184 MVT::isInteger(LL.getValueType())) {
1185 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
1186 if (cast<ConstantSDNode>(LR)->getValue() == 0 && Op1 == ISD::SETEQ) {
1187 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001188 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001189 return DAG.getSetCC(VT, ORNode, LR, Op1);
1190 }
1191 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
1192 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
1193 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001194 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001195 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1196 }
1197 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
1198 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
1199 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001200 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001201 return DAG.getSetCC(VT, ORNode, LR, Op1);
1202 }
1203 }
1204 // canonicalize equivalent to ll == rl
1205 if (LL == RR && LR == RL) {
1206 Op1 = ISD::getSetCCSwappedOperands(Op1);
1207 std::swap(RL, RR);
1208 }
1209 if (LL == RL && LR == RR) {
1210 bool isInteger = MVT::isInteger(LL.getValueType());
1211 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
1212 if (Result != ISD::SETCC_INVALID)
1213 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1214 }
1215 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001216
1217 // Simplify: and (op x...), (op y...) -> (op (and x, y))
1218 if (N0.getOpcode() == N1.getOpcode()) {
1219 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1220 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001221 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001222
Nate Begemande996292006-02-03 22:24:05 +00001223 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
1224 // fold (and (sra)) -> (and (srl)) when possible.
Chris Lattner6ea2dee2006-03-25 22:19:00 +00001225 if (!MVT::isVector(VT) &&
1226 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001227 return SDOperand(N, 0);
Nate Begemanded49632005-10-13 03:11:28 +00001228 // fold (zext_inreg (extload x)) -> (zextload x)
Evan Chengc5484282006-10-04 00:56:09 +00001229 if (ISD::isEXTLoad(N0.Val)) {
Evan Cheng466685d2006-10-09 20:57:25 +00001230 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00001231 MVT::ValueType EVT = LN0->getLoadedVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001232 // If we zero all the possible extended bits, then we can turn this into
1233 // a zextload if we are running before legalize or the operation is legal.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001234 if (TLI.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Evan Chengc5484282006-10-04 00:56:09 +00001235 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00001236 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
1237 LN0->getBasePtr(), LN0->getSrcValue(),
1238 LN0->getSrcValueOffset(), EVT);
Chris Lattner5750df92006-03-01 04:03:14 +00001239 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001240 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001241 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001242 }
1243 }
1244 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Evan Chengc5484282006-10-04 00:56:09 +00001245 if (ISD::isSEXTLoad(N0.Val) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001246 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00001247 MVT::ValueType EVT = LN0->getLoadedVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001248 // If we zero all the possible extended bits, then we can turn this into
1249 // a zextload if we are running before legalize or the operation is legal.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001250 if (TLI.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Evan Chengc5484282006-10-04 00:56:09 +00001251 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00001252 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
1253 LN0->getBasePtr(), LN0->getSrcValue(),
1254 LN0->getSrcValueOffset(), EVT);
Chris Lattner5750df92006-03-01 04:03:14 +00001255 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001256 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001257 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001258 }
1259 }
Chris Lattner15045b62006-02-28 06:35:35 +00001260
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001261 // fold (and (load x), 255) -> (zextload x, i8)
1262 // fold (and (extload x, i16), 255) -> (zextload x, i8)
Evan Cheng466685d2006-10-09 20:57:25 +00001263 if (N1C && N0.getOpcode() == ISD::LOAD) {
1264 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
1265 if (LN0->getExtensionType() != ISD::SEXTLOAD &&
1266 N0.hasOneUse()) {
1267 MVT::ValueType EVT, LoadedVT;
1268 if (N1C->getValue() == 255)
1269 EVT = MVT::i8;
1270 else if (N1C->getValue() == 65535)
1271 EVT = MVT::i16;
1272 else if (N1C->getValue() == ~0U)
1273 EVT = MVT::i32;
1274 else
1275 EVT = MVT::Other;
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001276
Evan Cheng2e49f092006-10-11 07:10:22 +00001277 LoadedVT = LN0->getLoadedVT();
Evan Cheng466685d2006-10-09 20:57:25 +00001278 if (EVT != MVT::Other && LoadedVT > EVT &&
1279 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
1280 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
1281 // For big endian targets, we need to add an offset to the pointer to
1282 // load the correct bytes. For little endian systems, we merely need to
1283 // read fewer bytes from the same pointer.
1284 unsigned PtrOff =
1285 (MVT::getSizeInBits(LoadedVT) - MVT::getSizeInBits(EVT)) / 8;
1286 SDOperand NewPtr = LN0->getBasePtr();
1287 if (!TLI.isLittleEndian())
1288 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
1289 DAG.getConstant(PtrOff, PtrType));
1290 AddToWorkList(NewPtr.Val);
1291 SDOperand Load =
1292 DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(), NewPtr,
1293 LN0->getSrcValue(), LN0->getSrcValueOffset(), EVT);
1294 AddToWorkList(N);
1295 CombineTo(N0.Val, Load, Load.getValue(1));
1296 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1297 }
Chris Lattner15045b62006-02-28 06:35:35 +00001298 }
1299 }
1300
Nate Begeman83e75ec2005-09-06 04:43:02 +00001301 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001302}
1303
Nate Begeman83e75ec2005-09-06 04:43:02 +00001304SDOperand DAGCombiner::visitOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001305 SDOperand N0 = N->getOperand(0);
1306 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001307 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001308 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1309 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001310 MVT::ValueType VT = N1.getValueType();
1311 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001312
1313 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001314 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001315 return DAG.getNode(ISD::OR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001316 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001317 if (N0C && !N1C)
1318 return DAG.getNode(ISD::OR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001319 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001320 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001321 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001322 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001323 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001324 return N1;
1325 // fold (or x, c) -> c iff (x & ~c) == 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001326 if (N1C &&
1327 TLI.MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits))))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001328 return N1;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001329 // reassociate or
1330 SDOperand ROR = ReassociateOps(ISD::OR, N0, N1);
1331 if (ROR.Val != 0)
1332 return ROR;
1333 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
1334 if (N1C && N0.getOpcode() == ISD::AND && N0.Val->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00001335 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00001336 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
1337 return DAG.getNode(ISD::AND, VT, DAG.getNode(ISD::OR, VT, N0.getOperand(0),
1338 N1),
1339 DAG.getConstant(N1C->getValue() | C1->getValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00001340 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001341 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
1342 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1343 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1344 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1345
1346 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1347 MVT::isInteger(LL.getValueType())) {
1348 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
1349 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
1350 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
1351 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
1352 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001353 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001354 return DAG.getSetCC(VT, ORNode, LR, Op1);
1355 }
1356 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
1357 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
1358 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
1359 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
1360 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001361 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001362 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1363 }
1364 }
1365 // canonicalize equivalent to ll == rl
1366 if (LL == RR && LR == RL) {
1367 Op1 = ISD::getSetCCSwappedOperands(Op1);
1368 std::swap(RL, RR);
1369 }
1370 if (LL == RL && LR == RR) {
1371 bool isInteger = MVT::isInteger(LL.getValueType());
1372 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
1373 if (Result != ISD::SETCC_INVALID)
1374 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1375 }
1376 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001377
1378 // Simplify: or (op x...), (op y...) -> (op (or x, y))
1379 if (N0.getOpcode() == N1.getOpcode()) {
1380 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1381 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001382 }
Chris Lattner516b9622006-09-14 20:50:57 +00001383
Chris Lattner1ec72732006-09-14 21:11:37 +00001384 // (X & C1) | (Y & C2) -> (X|Y) & C3 if possible.
1385 if (N0.getOpcode() == ISD::AND &&
1386 N1.getOpcode() == ISD::AND &&
1387 N0.getOperand(1).getOpcode() == ISD::Constant &&
1388 N1.getOperand(1).getOpcode() == ISD::Constant &&
1389 // Don't increase # computations.
1390 (N0.Val->hasOneUse() || N1.Val->hasOneUse())) {
1391 // We can only do this xform if we know that bits from X that are set in C2
1392 // but not in C1 are already zero. Likewise for Y.
1393 uint64_t LHSMask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1394 uint64_t RHSMask = cast<ConstantSDNode>(N1.getOperand(1))->getValue();
1395
1396 if (TLI.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) &&
1397 TLI.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) {
1398 SDOperand X =DAG.getNode(ISD::OR, VT, N0.getOperand(0), N1.getOperand(0));
1399 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(LHSMask|RHSMask, VT));
1400 }
1401 }
1402
1403
Chris Lattner516b9622006-09-14 20:50:57 +00001404 // See if this is some rotate idiom.
1405 if (SDNode *Rot = MatchRotate(N0, N1))
1406 return SDOperand(Rot, 0);
Chris Lattner35e5c142006-05-05 05:51:50 +00001407
Nate Begeman83e75ec2005-09-06 04:43:02 +00001408 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001409}
1410
Chris Lattner516b9622006-09-14 20:50:57 +00001411
1412/// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present.
1413static bool MatchRotateHalf(SDOperand Op, SDOperand &Shift, SDOperand &Mask) {
1414 if (Op.getOpcode() == ISD::AND) {
Reid Spencer3ed469c2006-11-02 20:25:50 +00001415 if (isa<ConstantSDNode>(Op.getOperand(1))) {
Chris Lattner516b9622006-09-14 20:50:57 +00001416 Mask = Op.getOperand(1);
1417 Op = Op.getOperand(0);
1418 } else {
1419 return false;
1420 }
1421 }
1422
1423 if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) {
1424 Shift = Op;
1425 return true;
1426 }
1427 return false;
1428}
1429
1430
1431// MatchRotate - Handle an 'or' of two operands. If this is one of the many
1432// idioms for rotate, and if the target supports rotation instructions, generate
1433// a rot[lr].
1434SDNode *DAGCombiner::MatchRotate(SDOperand LHS, SDOperand RHS) {
1435 // Must be a legal type. Expanded an promoted things won't work with rotates.
1436 MVT::ValueType VT = LHS.getValueType();
1437 if (!TLI.isTypeLegal(VT)) return 0;
1438
1439 // The target must have at least one rotate flavor.
1440 bool HasROTL = TLI.isOperationLegal(ISD::ROTL, VT);
1441 bool HasROTR = TLI.isOperationLegal(ISD::ROTR, VT);
1442 if (!HasROTL && !HasROTR) return 0;
1443
1444 // Match "(X shl/srl V1) & V2" where V2 may not be present.
1445 SDOperand LHSShift; // The shift.
1446 SDOperand LHSMask; // AND value if any.
1447 if (!MatchRotateHalf(LHS, LHSShift, LHSMask))
1448 return 0; // Not part of a rotate.
1449
1450 SDOperand RHSShift; // The shift.
1451 SDOperand RHSMask; // AND value if any.
1452 if (!MatchRotateHalf(RHS, RHSShift, RHSMask))
1453 return 0; // Not part of a rotate.
1454
1455 if (LHSShift.getOperand(0) != RHSShift.getOperand(0))
1456 return 0; // Not shifting the same value.
1457
1458 if (LHSShift.getOpcode() == RHSShift.getOpcode())
1459 return 0; // Shifts must disagree.
1460
1461 // Canonicalize shl to left side in a shl/srl pair.
1462 if (RHSShift.getOpcode() == ISD::SHL) {
1463 std::swap(LHS, RHS);
1464 std::swap(LHSShift, RHSShift);
1465 std::swap(LHSMask , RHSMask );
1466 }
1467
1468 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1469
1470 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
1471 // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2)
1472 if (LHSShift.getOperand(1).getOpcode() == ISD::Constant &&
1473 RHSShift.getOperand(1).getOpcode() == ISD::Constant) {
1474 uint64_t LShVal = cast<ConstantSDNode>(LHSShift.getOperand(1))->getValue();
1475 uint64_t RShVal = cast<ConstantSDNode>(RHSShift.getOperand(1))->getValue();
1476 if ((LShVal + RShVal) != OpSizeInBits)
1477 return 0;
1478
1479 SDOperand Rot;
1480 if (HasROTL)
1481 Rot = DAG.getNode(ISD::ROTL, VT, LHSShift.getOperand(0),
1482 LHSShift.getOperand(1));
1483 else
1484 Rot = DAG.getNode(ISD::ROTR, VT, LHSShift.getOperand(0),
1485 RHSShift.getOperand(1));
1486
1487 // If there is an AND of either shifted operand, apply it to the result.
1488 if (LHSMask.Val || RHSMask.Val) {
1489 uint64_t Mask = MVT::getIntVTBitMask(VT);
1490
1491 if (LHSMask.Val) {
1492 uint64_t RHSBits = (1ULL << LShVal)-1;
1493 Mask &= cast<ConstantSDNode>(LHSMask)->getValue() | RHSBits;
1494 }
1495 if (RHSMask.Val) {
1496 uint64_t LHSBits = ~((1ULL << (OpSizeInBits-RShVal))-1);
1497 Mask &= cast<ConstantSDNode>(RHSMask)->getValue() | LHSBits;
1498 }
1499
1500 Rot = DAG.getNode(ISD::AND, VT, Rot, DAG.getConstant(Mask, VT));
1501 }
1502
1503 return Rot.Val;
1504 }
1505
1506 // If there is a mask here, and we have a variable shift, we can't be sure
1507 // that we're masking out the right stuff.
1508 if (LHSMask.Val || RHSMask.Val)
1509 return 0;
1510
1511 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
1512 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotr x, (sub 32, y))
1513 if (RHSShift.getOperand(1).getOpcode() == ISD::SUB &&
1514 LHSShift.getOperand(1) == RHSShift.getOperand(1).getOperand(1)) {
1515 if (ConstantSDNode *SUBC =
1516 dyn_cast<ConstantSDNode>(RHSShift.getOperand(1).getOperand(0))) {
1517 if (SUBC->getValue() == OpSizeInBits)
1518 if (HasROTL)
1519 return DAG.getNode(ISD::ROTL, VT, LHSShift.getOperand(0),
1520 LHSShift.getOperand(1)).Val;
1521 else
1522 return DAG.getNode(ISD::ROTR, VT, LHSShift.getOperand(0),
1523 LHSShift.getOperand(1)).Val;
1524 }
1525 }
1526
1527 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
1528 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotl x, (sub 32, y))
1529 if (LHSShift.getOperand(1).getOpcode() == ISD::SUB &&
1530 RHSShift.getOperand(1) == LHSShift.getOperand(1).getOperand(1)) {
1531 if (ConstantSDNode *SUBC =
1532 dyn_cast<ConstantSDNode>(LHSShift.getOperand(1).getOperand(0))) {
1533 if (SUBC->getValue() == OpSizeInBits)
1534 if (HasROTL)
1535 return DAG.getNode(ISD::ROTL, VT, LHSShift.getOperand(0),
1536 LHSShift.getOperand(1)).Val;
1537 else
1538 return DAG.getNode(ISD::ROTR, VT, LHSShift.getOperand(0),
1539 RHSShift.getOperand(1)).Val;
1540 }
1541 }
1542
1543 return 0;
1544}
1545
1546
Nate Begeman83e75ec2005-09-06 04:43:02 +00001547SDOperand DAGCombiner::visitXOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001548 SDOperand N0 = N->getOperand(0);
1549 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001550 SDOperand LHS, RHS, CC;
1551 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1552 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001553 MVT::ValueType VT = N0.getValueType();
1554
1555 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001556 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001557 return DAG.getNode(ISD::XOR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001558 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001559 if (N0C && !N1C)
1560 return DAG.getNode(ISD::XOR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001561 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001562 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001563 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001564 // reassociate xor
1565 SDOperand RXOR = ReassociateOps(ISD::XOR, N0, N1);
1566 if (RXOR.Val != 0)
1567 return RXOR;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001568 // fold !(x cc y) -> (x !cc y)
Nate Begeman646d7e22005-09-02 21:18:40 +00001569 if (N1C && N1C->getValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
1570 bool isInt = MVT::isInteger(LHS.getValueType());
1571 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
1572 isInt);
1573 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001574 return DAG.getSetCC(VT, LHS, RHS, NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00001575 if (N0.getOpcode() == ISD::SELECT_CC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001576 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00001577 assert(0 && "Unhandled SetCC Equivalent!");
1578 abort();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001579 }
Nate Begeman99801192005-09-07 23:25:52 +00001580 // fold !(x or y) -> (!x and !y) iff x or y are setcc
Chris Lattner734c91d2006-11-10 21:37:15 +00001581 if (N1C && N1C->getValue() == 1 && VT == MVT::i1 &&
Nate Begeman99801192005-09-07 23:25:52 +00001582 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001583 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00001584 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
1585 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001586 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1587 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00001588 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00001589 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001590 }
1591 }
Nate Begeman99801192005-09-07 23:25:52 +00001592 // fold !(x or y) -> (!x and !y) iff x or y are constants
1593 if (N1C && N1C->isAllOnesValue() &&
1594 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001595 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00001596 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
1597 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001598 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1599 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00001600 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00001601 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001602 }
1603 }
Nate Begeman223df222005-09-08 20:18:10 +00001604 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
1605 if (N1C && N0.getOpcode() == ISD::XOR) {
1606 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
1607 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
1608 if (N00C)
1609 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
1610 DAG.getConstant(N1C->getValue()^N00C->getValue(), VT));
1611 if (N01C)
1612 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
1613 DAG.getConstant(N1C->getValue()^N01C->getValue(), VT));
1614 }
1615 // fold (xor x, x) -> 0
Chris Lattner4fbdd592006-03-28 19:11:05 +00001616 if (N0 == N1) {
1617 if (!MVT::isVector(VT)) {
1618 return DAG.getConstant(0, VT);
1619 } else if (!AfterLegalize || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) {
1620 // Produce a vector of zeros.
1621 SDOperand El = DAG.getConstant(0, MVT::getVectorBaseType(VT));
1622 std::vector<SDOperand> Ops(MVT::getVectorNumElements(VT), El);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001623 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4fbdd592006-03-28 19:11:05 +00001624 }
1625 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001626
1627 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
1628 if (N0.getOpcode() == N1.getOpcode()) {
1629 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1630 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001631 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001632
Chris Lattner3e104b12006-04-08 04:15:24 +00001633 // Simplify the expression using non-local knowledge.
1634 if (!MVT::isVector(VT) &&
1635 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001636 return SDOperand(N, 0);
Chris Lattner3e104b12006-04-08 04:15:24 +00001637
Nate Begeman83e75ec2005-09-06 04:43:02 +00001638 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001639}
1640
Nate Begeman83e75ec2005-09-06 04:43:02 +00001641SDOperand DAGCombiner::visitSHL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001642 SDOperand N0 = N->getOperand(0);
1643 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001644 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1645 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001646 MVT::ValueType VT = N0.getValueType();
1647 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1648
1649 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001650 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001651 return DAG.getNode(ISD::SHL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001652 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001653 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001654 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001655 // fold (shl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001656 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001657 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001658 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001659 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001660 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001661 // if (shl x, c) is known to be zero, return 0
Nate Begemanfb7217b2006-02-17 19:54:08 +00001662 if (TLI.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001663 return DAG.getConstant(0, VT);
Chris Lattner012f2412006-02-17 21:58:01 +00001664 if (SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001665 return SDOperand(N, 0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001666 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001667 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001668 N0.getOperand(1).getOpcode() == ISD::Constant) {
1669 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001670 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001671 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001672 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001673 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001674 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001675 }
1676 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
1677 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001678 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001679 N0.getOperand(1).getOpcode() == ISD::Constant) {
1680 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001681 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001682 SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
1683 DAG.getConstant(~0ULL << c1, VT));
1684 if (c2 > c1)
1685 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001686 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001687 else
Nate Begeman83e75ec2005-09-06 04:43:02 +00001688 return DAG.getNode(ISD::SRL, VT, Mask,
1689 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001690 }
1691 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001692 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +00001693 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001694 DAG.getConstant(~0ULL << N1C->getValue(), VT));
1695 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001696}
1697
Nate Begeman83e75ec2005-09-06 04:43:02 +00001698SDOperand DAGCombiner::visitSRA(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001699 SDOperand N0 = N->getOperand(0);
1700 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001701 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1702 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001703 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001704
1705 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001706 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001707 return DAG.getNode(ISD::SRA, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001708 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001709 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001710 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001711 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001712 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001713 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001714 // fold (sra x, c >= size(x)) -> undef
Nate Begemanfb7217b2006-02-17 19:54:08 +00001715 if (N1C && N1C->getValue() >= MVT::getSizeInBits(VT))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001716 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001717 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001718 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001719 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00001720 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
1721 // sext_inreg.
1722 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
1723 unsigned LowBits = MVT::getSizeInBits(VT) - (unsigned)N1C->getValue();
1724 MVT::ValueType EVT;
1725 switch (LowBits) {
1726 default: EVT = MVT::Other; break;
1727 case 1: EVT = MVT::i1; break;
1728 case 8: EVT = MVT::i8; break;
1729 case 16: EVT = MVT::i16; break;
1730 case 32: EVT = MVT::i32; break;
1731 }
1732 if (EVT > MVT::Other && TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, EVT))
1733 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
1734 DAG.getValueType(EVT));
1735 }
Chris Lattner71d9ebc2006-02-28 06:23:04 +00001736
1737 // fold (sra (sra x, c1), c2) -> (sra x, c1+c2)
1738 if (N1C && N0.getOpcode() == ISD::SRA) {
1739 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
1740 unsigned Sum = N1C->getValue() + C1->getValue();
1741 if (Sum >= MVT::getSizeInBits(VT)) Sum = MVT::getSizeInBits(VT)-1;
1742 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0),
1743 DAG.getConstant(Sum, N1C->getValueType(0)));
1744 }
1745 }
1746
Chris Lattnera8504462006-05-08 20:51:54 +00001747 // Simplify, based on bits shifted out of the LHS.
1748 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
1749 return SDOperand(N, 0);
1750
1751
Nate Begeman1d4d4142005-09-01 00:19:25 +00001752 // If the sign bit is known to be zero, switch this to a SRL.
Nate Begemanfb7217b2006-02-17 19:54:08 +00001753 if (TLI.MaskedValueIsZero(N0, MVT::getIntVTSignBit(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001754 return DAG.getNode(ISD::SRL, VT, N0, N1);
1755 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001756}
1757
Nate Begeman83e75ec2005-09-06 04:43:02 +00001758SDOperand DAGCombiner::visitSRL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001759 SDOperand N0 = N->getOperand(0);
1760 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001761 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1762 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001763 MVT::ValueType VT = N0.getValueType();
1764 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1765
1766 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001767 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001768 return DAG.getNode(ISD::SRL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001769 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001770 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001771 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001772 // fold (srl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001773 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001774 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001775 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001776 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001777 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001778 // if (srl x, c) is known to be zero, return 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001779 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001780 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001781 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001782 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001783 N0.getOperand(1).getOpcode() == ISD::Constant) {
1784 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001785 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001786 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001787 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001788 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001789 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001790 }
Chris Lattner350bec02006-04-02 06:11:11 +00001791
Chris Lattner06afe072006-05-05 22:53:17 +00001792 // fold (srl (anyextend x), c) -> (anyextend (srl x, c))
1793 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
1794 // Shifting in all undef bits?
1795 MVT::ValueType SmallVT = N0.getOperand(0).getValueType();
1796 if (N1C->getValue() >= MVT::getSizeInBits(SmallVT))
1797 return DAG.getNode(ISD::UNDEF, VT);
1798
1799 SDOperand SmallShift = DAG.getNode(ISD::SRL, SmallVT, N0.getOperand(0), N1);
1800 AddToWorkList(SmallShift.Val);
1801 return DAG.getNode(ISD::ANY_EXTEND, VT, SmallShift);
1802 }
1803
Chris Lattner3657ffe2006-10-12 20:23:19 +00001804 // fold (srl (sra X, Y), 31) -> (srl X, 31). This srl only looks at the sign
1805 // bit, which is unmodified by sra.
1806 if (N1C && N1C->getValue()+1 == MVT::getSizeInBits(VT)) {
1807 if (N0.getOpcode() == ISD::SRA)
1808 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0), N1);
1809 }
1810
Chris Lattner350bec02006-04-02 06:11:11 +00001811 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
1812 if (N1C && N0.getOpcode() == ISD::CTLZ &&
1813 N1C->getValue() == Log2_32(MVT::getSizeInBits(VT))) {
1814 uint64_t KnownZero, KnownOne, Mask = MVT::getIntVTBitMask(VT);
1815 TLI.ComputeMaskedBits(N0.getOperand(0), Mask, KnownZero, KnownOne);
1816
1817 // If any of the input bits are KnownOne, then the input couldn't be all
1818 // zeros, thus the result of the srl will always be zero.
1819 if (KnownOne) return DAG.getConstant(0, VT);
1820
1821 // If all of the bits input the to ctlz node are known to be zero, then
1822 // the result of the ctlz is "32" and the result of the shift is one.
1823 uint64_t UnknownBits = ~KnownZero & Mask;
1824 if (UnknownBits == 0) return DAG.getConstant(1, VT);
1825
1826 // Otherwise, check to see if there is exactly one bit input to the ctlz.
1827 if ((UnknownBits & (UnknownBits-1)) == 0) {
1828 // Okay, we know that only that the single bit specified by UnknownBits
1829 // could be set on input to the CTLZ node. If this bit is set, the SRL
1830 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
1831 // to an SRL,XOR pair, which is likely to simplify more.
1832 unsigned ShAmt = CountTrailingZeros_64(UnknownBits);
1833 SDOperand Op = N0.getOperand(0);
1834 if (ShAmt) {
1835 Op = DAG.getNode(ISD::SRL, VT, Op,
1836 DAG.getConstant(ShAmt, TLI.getShiftAmountTy()));
1837 AddToWorkList(Op.Val);
1838 }
1839 return DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(1, VT));
1840 }
1841 }
1842
Nate Begeman83e75ec2005-09-06 04:43:02 +00001843 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001844}
1845
Nate Begeman83e75ec2005-09-06 04:43:02 +00001846SDOperand DAGCombiner::visitCTLZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001847 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00001848 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001849
1850 // fold (ctlz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00001851 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00001852 return DAG.getNode(ISD::CTLZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001853 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001854}
1855
Nate Begeman83e75ec2005-09-06 04:43:02 +00001856SDOperand DAGCombiner::visitCTTZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001857 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00001858 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001859
1860 // fold (cttz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00001861 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00001862 return DAG.getNode(ISD::CTTZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001863 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001864}
1865
Nate Begeman83e75ec2005-09-06 04:43:02 +00001866SDOperand DAGCombiner::visitCTPOP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001867 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00001868 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001869
1870 // fold (ctpop c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00001871 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00001872 return DAG.getNode(ISD::CTPOP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001873 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001874}
1875
Nate Begeman452d7be2005-09-16 00:54:12 +00001876SDOperand DAGCombiner::visitSELECT(SDNode *N) {
1877 SDOperand N0 = N->getOperand(0);
1878 SDOperand N1 = N->getOperand(1);
1879 SDOperand N2 = N->getOperand(2);
1880 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1881 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1882 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1883 MVT::ValueType VT = N->getValueType(0);
Nate Begeman44728a72005-09-19 22:34:01 +00001884
Nate Begeman452d7be2005-09-16 00:54:12 +00001885 // fold select C, X, X -> X
1886 if (N1 == N2)
1887 return N1;
1888 // fold select true, X, Y -> X
1889 if (N0C && !N0C->isNullValue())
1890 return N1;
1891 // fold select false, X, Y -> Y
1892 if (N0C && N0C->isNullValue())
1893 return N2;
1894 // fold select C, 1, X -> C | X
Nate Begeman44728a72005-09-19 22:34:01 +00001895 if (MVT::i1 == VT && N1C && N1C->getValue() == 1)
Nate Begeman452d7be2005-09-16 00:54:12 +00001896 return DAG.getNode(ISD::OR, VT, N0, N2);
1897 // fold select C, 0, X -> ~C & X
1898 // FIXME: this should check for C type == X type, not i1?
1899 if (MVT::i1 == VT && N1C && N1C->isNullValue()) {
1900 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00001901 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00001902 return DAG.getNode(ISD::AND, VT, XORNode, N2);
1903 }
1904 // fold select C, X, 1 -> ~C | X
Nate Begeman44728a72005-09-19 22:34:01 +00001905 if (MVT::i1 == VT && N2C && N2C->getValue() == 1) {
Nate Begeman452d7be2005-09-16 00:54:12 +00001906 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00001907 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00001908 return DAG.getNode(ISD::OR, VT, XORNode, N1);
1909 }
1910 // fold select C, X, 0 -> C & X
1911 // FIXME: this should check for C type == X type, not i1?
1912 if (MVT::i1 == VT && N2C && N2C->isNullValue())
1913 return DAG.getNode(ISD::AND, VT, N0, N1);
1914 // fold X ? X : Y --> X ? 1 : Y --> X | Y
1915 if (MVT::i1 == VT && N0 == N1)
1916 return DAG.getNode(ISD::OR, VT, N0, N2);
1917 // fold X ? Y : X --> X ? Y : 0 --> X & Y
1918 if (MVT::i1 == VT && N0 == N2)
1919 return DAG.getNode(ISD::AND, VT, N0, N1);
Chris Lattner729c6d12006-05-27 00:43:02 +00001920
Chris Lattner40c62d52005-10-18 06:04:22 +00001921 // If we can fold this based on the true/false value, do so.
1922 if (SimplifySelectOps(N, N1, N2))
Chris Lattner729c6d12006-05-27 00:43:02 +00001923 return SDOperand(N, 0); // Don't revisit N.
1924
Nate Begeman44728a72005-09-19 22:34:01 +00001925 // fold selects based on a setcc into other things, such as min/max/abs
1926 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman750ac1b2006-02-01 07:19:44 +00001927 // FIXME:
1928 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
1929 // having to say they don't support SELECT_CC on every type the DAG knows
1930 // about, since there is no way to mark an opcode illegal at all value types
1931 if (TLI.isOperationLegal(ISD::SELECT_CC, MVT::Other))
1932 return DAG.getNode(ISD::SELECT_CC, VT, N0.getOperand(0), N0.getOperand(1),
1933 N1, N2, N0.getOperand(2));
1934 else
1935 return SimplifySelect(N0, N1, N2);
Nate Begeman452d7be2005-09-16 00:54:12 +00001936 return SDOperand();
1937}
1938
1939SDOperand DAGCombiner::visitSELECT_CC(SDNode *N) {
Nate Begeman44728a72005-09-19 22:34:01 +00001940 SDOperand N0 = N->getOperand(0);
1941 SDOperand N1 = N->getOperand(1);
1942 SDOperand N2 = N->getOperand(2);
1943 SDOperand N3 = N->getOperand(3);
1944 SDOperand N4 = N->getOperand(4);
Nate Begeman44728a72005-09-19 22:34:01 +00001945 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
1946
Nate Begeman44728a72005-09-19 22:34:01 +00001947 // fold select_cc lhs, rhs, x, x, cc -> x
1948 if (N2 == N3)
1949 return N2;
Chris Lattner40c62d52005-10-18 06:04:22 +00001950
Chris Lattner5f42a242006-09-20 06:19:26 +00001951 // Determine if the condition we're dealing with is constant
1952 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
Chris Lattner30f73e72006-10-14 03:52:46 +00001953 if (SCC.Val) AddToWorkList(SCC.Val);
Chris Lattner5f42a242006-09-20 06:19:26 +00001954
1955 if (ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val)) {
1956 if (SCCC->getValue())
1957 return N2; // cond always true -> true val
1958 else
1959 return N3; // cond always false -> false val
1960 }
1961
1962 // Fold to a simpler select_cc
1963 if (SCC.Val && SCC.getOpcode() == ISD::SETCC)
1964 return DAG.getNode(ISD::SELECT_CC, N2.getValueType(),
1965 SCC.getOperand(0), SCC.getOperand(1), N2, N3,
1966 SCC.getOperand(2));
1967
Chris Lattner40c62d52005-10-18 06:04:22 +00001968 // If we can fold this based on the true/false value, do so.
1969 if (SimplifySelectOps(N, N2, N3))
Chris Lattner729c6d12006-05-27 00:43:02 +00001970 return SDOperand(N, 0); // Don't revisit N.
Chris Lattner40c62d52005-10-18 06:04:22 +00001971
Nate Begeman44728a72005-09-19 22:34:01 +00001972 // fold select_cc into other things, such as min/max/abs
1973 return SimplifySelectCC(N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00001974}
1975
1976SDOperand DAGCombiner::visitSETCC(SDNode *N) {
1977 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
1978 cast<CondCodeSDNode>(N->getOperand(2))->get());
1979}
1980
Nate Begeman83e75ec2005-09-06 04:43:02 +00001981SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001982 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001983 MVT::ValueType VT = N->getValueType(0);
1984
Nate Begeman1d4d4142005-09-01 00:19:25 +00001985 // fold (sext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00001986 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00001987 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0);
Chris Lattner310b5782006-05-06 23:06:26 +00001988
Nate Begeman1d4d4142005-09-01 00:19:25 +00001989 // fold (sext (sext x)) -> (sext x)
Chris Lattner310b5782006-05-06 23:06:26 +00001990 // fold (sext (aext x)) -> (sext x)
1991 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001992 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
Chris Lattner310b5782006-05-06 23:06:26 +00001993
Chris Lattner22558872007-02-26 03:13:59 +00001994 if (N0.getOpcode() == ISD::TRUNCATE) {
1995 // See if the value being truncated is already sign extended. If so, just
1996 // eliminate the trunc/sext pair.
Chris Lattner6007b842006-09-21 06:00:20 +00001997 SDOperand Op = N0.getOperand(0);
Chris Lattner22558872007-02-26 03:13:59 +00001998 unsigned OpBits = MVT::getSizeInBits(Op.getValueType());
1999 unsigned MidBits = MVT::getSizeInBits(N0.getValueType());
2000 unsigned DestBits = MVT::getSizeInBits(VT);
2001 unsigned NumSignBits = TLI.ComputeNumSignBits(Op);
2002
2003 if (OpBits == DestBits) {
2004 // Op is i32, Mid is i8, and Dest is i32. If Op has more than 24 sign
2005 // bits, it is already ready.
2006 if (NumSignBits > DestBits-MidBits)
2007 return Op;
2008 } else if (OpBits < DestBits) {
2009 // Op is i32, Mid is i8, and Dest is i64. If Op has more than 24 sign
2010 // bits, just sext from i32.
2011 if (NumSignBits > OpBits-MidBits)
2012 return DAG.getNode(ISD::SIGN_EXTEND, VT, Op);
2013 } else {
2014 // Op is i64, Mid is i8, and Dest is i32. If Op has more than 56 sign
2015 // bits, just truncate to i32.
2016 if (NumSignBits > OpBits-MidBits)
2017 return DAG.getNode(ISD::TRUNCATE, VT, Op);
Chris Lattner6007b842006-09-21 06:00:20 +00002018 }
Chris Lattner22558872007-02-26 03:13:59 +00002019
2020 // fold (sext (truncate x)) -> (sextinreg x).
2021 if (!AfterLegalize || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG,
2022 N0.getValueType())) {
2023 if (Op.getValueType() < VT)
2024 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
2025 else if (Op.getValueType() > VT)
2026 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
2027 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, Op,
2028 DAG.getValueType(N0.getValueType()));
2029 }
Chris Lattner6007b842006-09-21 06:00:20 +00002030 }
Chris Lattner310b5782006-05-06 23:06:26 +00002031
Evan Cheng110dec22005-12-14 02:19:23 +00002032 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00002033 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00002034 (!AfterLegalize||TLI.isLoadXLegal(ISD::SEXTLOAD, N0.getValueType()))){
Evan Cheng466685d2006-10-09 20:57:25 +00002035 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2036 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2037 LN0->getBasePtr(), LN0->getSrcValue(),
2038 LN0->getSrcValueOffset(),
Nate Begeman3df4d522005-10-12 20:40:40 +00002039 N0.getValueType());
Chris Lattnerd4771842005-12-14 19:25:30 +00002040 CombineTo(N, ExtLoad);
Chris Lattnerf9884052005-10-13 21:52:31 +00002041 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2042 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002043 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begeman3df4d522005-10-12 20:40:40 +00002044 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002045
2046 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
2047 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
Evan Chengc5484282006-10-04 00:56:09 +00002048 if ((ISD::isSEXTLoad(N0.Val) || ISD::isEXTLoad(N0.Val)) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002049 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00002050 MVT::ValueType EVT = LN0->getLoadedVT();
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00002051 if (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT)) {
2052 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2053 LN0->getBasePtr(), LN0->getSrcValue(),
2054 LN0->getSrcValueOffset(), EVT);
2055 CombineTo(N, ExtLoad);
2056 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2057 ExtLoad.getValue(1));
2058 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2059 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002060 }
2061
Nate Begeman83e75ec2005-09-06 04:43:02 +00002062 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002063}
2064
Nate Begeman83e75ec2005-09-06 04:43:02 +00002065SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002066 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002067 MVT::ValueType VT = N->getValueType(0);
2068
Nate Begeman1d4d4142005-09-01 00:19:25 +00002069 // fold (zext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00002070 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002071 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002072 // fold (zext (zext x)) -> (zext x)
Chris Lattner310b5782006-05-06 23:06:26 +00002073 // fold (zext (aext x)) -> (zext x)
2074 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002075 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
Chris Lattner6007b842006-09-21 06:00:20 +00002076
2077 // fold (zext (truncate x)) -> (and x, mask)
2078 if (N0.getOpcode() == ISD::TRUNCATE &&
2079 (!AfterLegalize || TLI.isOperationLegal(ISD::AND, VT))) {
2080 SDOperand Op = N0.getOperand(0);
2081 if (Op.getValueType() < VT) {
2082 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
2083 } else if (Op.getValueType() > VT) {
2084 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
2085 }
2086 return DAG.getZeroExtendInReg(Op, N0.getValueType());
2087 }
2088
Chris Lattner111c2282006-09-21 06:14:31 +00002089 // fold (zext (and (trunc x), cst)) -> (and x, cst).
2090 if (N0.getOpcode() == ISD::AND &&
2091 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
2092 N0.getOperand(1).getOpcode() == ISD::Constant) {
2093 SDOperand X = N0.getOperand(0).getOperand(0);
2094 if (X.getValueType() < VT) {
2095 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
2096 } else if (X.getValueType() > VT) {
2097 X = DAG.getNode(ISD::TRUNCATE, VT, X);
2098 }
2099 uint64_t Mask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
2100 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
2101 }
2102
Evan Cheng110dec22005-12-14 02:19:23 +00002103 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00002104 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00002105 (!AfterLegalize||TLI.isLoadXLegal(ISD::ZEXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00002106 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2107 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
2108 LN0->getBasePtr(), LN0->getSrcValue(),
2109 LN0->getSrcValueOffset(),
Evan Cheng110dec22005-12-14 02:19:23 +00002110 N0.getValueType());
Chris Lattnerd4771842005-12-14 19:25:30 +00002111 CombineTo(N, ExtLoad);
Evan Cheng110dec22005-12-14 02:19:23 +00002112 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2113 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002114 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng110dec22005-12-14 02:19:23 +00002115 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002116
2117 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
2118 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
Evan Chengc5484282006-10-04 00:56:09 +00002119 if ((ISD::isZEXTLoad(N0.Val) || ISD::isEXTLoad(N0.Val)) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002120 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00002121 MVT::ValueType EVT = LN0->getLoadedVT();
Evan Cheng466685d2006-10-09 20:57:25 +00002122 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
2123 LN0->getBasePtr(), LN0->getSrcValue(),
2124 LN0->getSrcValueOffset(), EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00002125 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002126 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2127 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002128 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002129 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00002130 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002131}
2132
Chris Lattner5ffc0662006-05-05 05:58:59 +00002133SDOperand DAGCombiner::visitANY_EXTEND(SDNode *N) {
2134 SDOperand N0 = N->getOperand(0);
Chris Lattner5ffc0662006-05-05 05:58:59 +00002135 MVT::ValueType VT = N->getValueType(0);
2136
2137 // fold (aext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00002138 if (isa<ConstantSDNode>(N0))
Chris Lattner5ffc0662006-05-05 05:58:59 +00002139 return DAG.getNode(ISD::ANY_EXTEND, VT, N0);
2140 // fold (aext (aext x)) -> (aext x)
2141 // fold (aext (zext x)) -> (zext x)
2142 // fold (aext (sext x)) -> (sext x)
2143 if (N0.getOpcode() == ISD::ANY_EXTEND ||
2144 N0.getOpcode() == ISD::ZERO_EXTEND ||
2145 N0.getOpcode() == ISD::SIGN_EXTEND)
2146 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
2147
Chris Lattner84750582006-09-20 06:29:17 +00002148 // fold (aext (truncate x))
2149 if (N0.getOpcode() == ISD::TRUNCATE) {
2150 SDOperand TruncOp = N0.getOperand(0);
2151 if (TruncOp.getValueType() == VT)
2152 return TruncOp; // x iff x size == zext size.
2153 if (TruncOp.getValueType() > VT)
2154 return DAG.getNode(ISD::TRUNCATE, VT, TruncOp);
2155 return DAG.getNode(ISD::ANY_EXTEND, VT, TruncOp);
2156 }
Chris Lattner0e4b9222006-09-21 06:40:43 +00002157
2158 // fold (aext (and (trunc x), cst)) -> (and x, cst).
2159 if (N0.getOpcode() == ISD::AND &&
2160 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
2161 N0.getOperand(1).getOpcode() == ISD::Constant) {
2162 SDOperand X = N0.getOperand(0).getOperand(0);
2163 if (X.getValueType() < VT) {
2164 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
2165 } else if (X.getValueType() > VT) {
2166 X = DAG.getNode(ISD::TRUNCATE, VT, X);
2167 }
2168 uint64_t Mask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
2169 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
2170 }
2171
Chris Lattner5ffc0662006-05-05 05:58:59 +00002172 // fold (aext (load x)) -> (aext (truncate (extload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00002173 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00002174 (!AfterLegalize||TLI.isLoadXLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00002175 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2176 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
2177 LN0->getBasePtr(), LN0->getSrcValue(),
2178 LN0->getSrcValueOffset(),
Chris Lattner5ffc0662006-05-05 05:58:59 +00002179 N0.getValueType());
2180 CombineTo(N, ExtLoad);
2181 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2182 ExtLoad.getValue(1));
2183 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2184 }
2185
2186 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
2187 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
2188 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00002189 if (N0.getOpcode() == ISD::LOAD && !ISD::isNON_EXTLoad(N0.Val) &&
2190 N0.hasOneUse()) {
2191 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00002192 MVT::ValueType EVT = LN0->getLoadedVT();
Evan Cheng466685d2006-10-09 20:57:25 +00002193 SDOperand ExtLoad = DAG.getExtLoad(LN0->getExtensionType(), VT,
2194 LN0->getChain(), LN0->getBasePtr(),
2195 LN0->getSrcValue(),
2196 LN0->getSrcValueOffset(), EVT);
Chris Lattner5ffc0662006-05-05 05:58:59 +00002197 CombineTo(N, ExtLoad);
2198 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2199 ExtLoad.getValue(1));
2200 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2201 }
2202 return SDOperand();
2203}
2204
2205
Nate Begeman83e75ec2005-09-06 04:43:02 +00002206SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002207 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002208 SDOperand N1 = N->getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002209 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002210 MVT::ValueType EVT = cast<VTSDNode>(N1)->getVT();
Nate Begeman07ed4172005-10-10 21:26:48 +00002211 unsigned EVTBits = MVT::getSizeInBits(EVT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002212
Nate Begeman1d4d4142005-09-01 00:19:25 +00002213 // fold (sext_in_reg c1) -> c1
Chris Lattnereaeda562006-05-08 20:59:41 +00002214 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
Chris Lattner310b5782006-05-06 23:06:26 +00002215 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0, N1);
Chris Lattneree4ea922006-05-06 09:30:03 +00002216
Chris Lattner541a24f2006-05-06 22:43:44 +00002217 // If the input is already sign extended, just drop the extension.
Chris Lattneree4ea922006-05-06 09:30:03 +00002218 if (TLI.ComputeNumSignBits(N0) >= MVT::getSizeInBits(VT)-EVTBits+1)
2219 return N0;
2220
Nate Begeman646d7e22005-09-02 21:18:40 +00002221 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
2222 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
2223 EVT < cast<VTSDNode>(N0.getOperand(1))->getVT()) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00002224 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002225 }
Chris Lattner4b37e872006-05-08 21:18:59 +00002226
Nate Begeman07ed4172005-10-10 21:26:48 +00002227 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is zero
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00002228 if (TLI.MaskedValueIsZero(N0, 1ULL << (EVTBits-1)))
Nate Begemande996292006-02-03 22:24:05 +00002229 return DAG.getZeroExtendInReg(N0, EVT);
Chris Lattner4b37e872006-05-08 21:18:59 +00002230
2231 // fold (sext_in_reg (srl X, 24), i8) -> sra X, 24
2232 // fold (sext_in_reg (srl X, 23), i8) -> sra X, 23 iff possible.
2233 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
2234 if (N0.getOpcode() == ISD::SRL) {
2235 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
2236 if (ShAmt->getValue()+EVTBits <= MVT::getSizeInBits(VT)) {
2237 // We can turn this into an SRA iff the input to the SRL is already sign
2238 // extended enough.
2239 unsigned InSignBits = TLI.ComputeNumSignBits(N0.getOperand(0));
2240 if (MVT::getSizeInBits(VT)-(ShAmt->getValue()+EVTBits) < InSignBits)
2241 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0), N0.getOperand(1));
2242 }
2243 }
2244
Nate Begemanded49632005-10-13 03:11:28 +00002245 // fold (sext_inreg (extload x)) -> (sextload x)
Evan Chengc5484282006-10-04 00:56:09 +00002246 if (ISD::isEXTLoad(N0.Val) &&
Evan Cheng2e49f092006-10-11 07:10:22 +00002247 EVT == cast<LoadSDNode>(N0)->getLoadedVT() &&
Evan Chengc5484282006-10-04 00:56:09 +00002248 (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00002249 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2250 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2251 LN0->getBasePtr(), LN0->getSrcValue(),
2252 LN0->getSrcValueOffset(), EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00002253 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00002254 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002255 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00002256 }
2257 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Evan Chengc5484282006-10-04 00:56:09 +00002258 if (ISD::isZEXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Cheng2e49f092006-10-11 07:10:22 +00002259 EVT == cast<LoadSDNode>(N0)->getLoadedVT() &&
Evan Chengc5484282006-10-04 00:56:09 +00002260 (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00002261 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2262 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2263 LN0->getBasePtr(), LN0->getSrcValue(),
2264 LN0->getSrcValueOffset(), EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00002265 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00002266 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002267 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00002268 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00002269 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002270}
2271
Nate Begeman83e75ec2005-09-06 04:43:02 +00002272SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002273 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002274 MVT::ValueType VT = N->getValueType(0);
2275
2276 // noop truncate
2277 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002278 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002279 // fold (truncate c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00002280 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002281 return DAG.getNode(ISD::TRUNCATE, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002282 // fold (truncate (truncate x)) -> (truncate x)
2283 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002284 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002285 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
Chris Lattnerb72773b2006-05-05 22:56:26 +00002286 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND||
2287 N0.getOpcode() == ISD::ANY_EXTEND) {
Chris Lattner32ba1aa2006-11-20 18:05:46 +00002288 if (N0.getOperand(0).getValueType() < VT)
Nate Begeman1d4d4142005-09-01 00:19:25 +00002289 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00002290 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Chris Lattner32ba1aa2006-11-20 18:05:46 +00002291 else if (N0.getOperand(0).getValueType() > VT)
Nate Begeman1d4d4142005-09-01 00:19:25 +00002292 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00002293 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002294 else
2295 // if the source and dest are the same type, we can drop both the extend
2296 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00002297 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002298 }
Nate Begeman3df4d522005-10-12 20:40:40 +00002299 // fold (truncate (load x)) -> (smaller load x)
Chris Lattnerbc4cf8d2006-11-27 04:40:53 +00002300 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
2301 // Do not allow folding to i1 here. i1 is implicitly stored in memory in
2302 // zero extended form: by shrinking the load, we lose track of the fact
2303 // that it is already zero extended.
2304 // FIXME: This should be reevaluated.
2305 VT != MVT::i1) {
Nate Begeman3df4d522005-10-12 20:40:40 +00002306 assert(MVT::getSizeInBits(N0.getValueType()) > MVT::getSizeInBits(VT) &&
2307 "Cannot truncate to larger type!");
Evan Cheng466685d2006-10-09 20:57:25 +00002308 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Nate Begeman3df4d522005-10-12 20:40:40 +00002309 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
Nate Begeman765784a2005-10-12 23:18:53 +00002310 // For big endian targets, we need to add an offset to the pointer to load
2311 // the correct bytes. For little endian systems, we merely need to read
2312 // fewer bytes from the same pointer.
Nate Begeman3df4d522005-10-12 20:40:40 +00002313 uint64_t PtrOff =
2314 (MVT::getSizeInBits(N0.getValueType()) - MVT::getSizeInBits(VT)) / 8;
Evan Cheng466685d2006-10-09 20:57:25 +00002315 SDOperand NewPtr = TLI.isLittleEndian() ? LN0->getBasePtr() :
2316 DAG.getNode(ISD::ADD, PtrType, LN0->getBasePtr(),
Nate Begeman765784a2005-10-12 23:18:53 +00002317 DAG.getConstant(PtrOff, PtrType));
Chris Lattner5750df92006-03-01 04:03:14 +00002318 AddToWorkList(NewPtr.Val);
Evan Cheng466685d2006-10-09 20:57:25 +00002319 SDOperand Load = DAG.getLoad(VT, LN0->getChain(), NewPtr,
2320 LN0->getSrcValue(), LN0->getSrcValueOffset());
Chris Lattner5750df92006-03-01 04:03:14 +00002321 AddToWorkList(N);
Chris Lattner24edbb72005-10-13 22:10:05 +00002322 CombineTo(N0.Val, Load, Load.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002323 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begeman3df4d522005-10-12 20:40:40 +00002324 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00002325 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002326}
2327
Chris Lattner94683772005-12-23 05:30:37 +00002328SDOperand DAGCombiner::visitBIT_CONVERT(SDNode *N) {
2329 SDOperand N0 = N->getOperand(0);
2330 MVT::ValueType VT = N->getValueType(0);
2331
2332 // If the input is a constant, let getNode() fold it.
2333 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
2334 SDOperand Res = DAG.getNode(ISD::BIT_CONVERT, VT, N0);
2335 if (Res.Val != N) return Res;
2336 }
2337
Chris Lattnerc8547d82005-12-23 05:37:50 +00002338 if (N0.getOpcode() == ISD::BIT_CONVERT) // conv(conv(x,t1),t2) -> conv(x,t2)
2339 return DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
Chris Lattner6258fb22006-04-02 02:53:43 +00002340
Chris Lattner57104102005-12-23 05:44:41 +00002341 // fold (conv (load x)) -> (load (conv*)x)
Chris Lattnerbf40c4b2006-01-15 18:58:59 +00002342 // FIXME: These xforms need to know that the resultant load doesn't need a
2343 // higher alignment than the original!
Evan Cheng466685d2006-10-09 20:57:25 +00002344 if (0 && ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse()) {
2345 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2346 SDOperand Load = DAG.getLoad(VT, LN0->getChain(), LN0->getBasePtr(),
2347 LN0->getSrcValue(), LN0->getSrcValueOffset());
Chris Lattner5750df92006-03-01 04:03:14 +00002348 AddToWorkList(N);
Chris Lattner57104102005-12-23 05:44:41 +00002349 CombineTo(N0.Val, DAG.getNode(ISD::BIT_CONVERT, N0.getValueType(), Load),
2350 Load.getValue(1));
2351 return Load;
2352 }
2353
Chris Lattner94683772005-12-23 05:30:37 +00002354 return SDOperand();
2355}
2356
Chris Lattner6258fb22006-04-02 02:53:43 +00002357SDOperand DAGCombiner::visitVBIT_CONVERT(SDNode *N) {
2358 SDOperand N0 = N->getOperand(0);
2359 MVT::ValueType VT = N->getValueType(0);
2360
2361 // If the input is a VBUILD_VECTOR with all constant elements, fold this now.
2362 // First check to see if this is all constant.
2363 if (N0.getOpcode() == ISD::VBUILD_VECTOR && N0.Val->hasOneUse() &&
2364 VT == MVT::Vector) {
2365 bool isSimple = true;
2366 for (unsigned i = 0, e = N0.getNumOperands()-2; i != e; ++i)
2367 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
2368 N0.getOperand(i).getOpcode() != ISD::Constant &&
2369 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
2370 isSimple = false;
2371 break;
2372 }
2373
Chris Lattner97c20732006-04-03 17:29:28 +00002374 MVT::ValueType DestEltVT = cast<VTSDNode>(N->getOperand(2))->getVT();
2375 if (isSimple && !MVT::isVector(DestEltVT)) {
Chris Lattner6258fb22006-04-02 02:53:43 +00002376 return ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(N0.Val, DestEltVT);
2377 }
2378 }
2379
2380 return SDOperand();
2381}
2382
2383/// ConstantFoldVBIT_CONVERTofVBUILD_VECTOR - We know that BV is a vbuild_vector
2384/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
2385/// destination element value type.
2386SDOperand DAGCombiner::
2387ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(SDNode *BV, MVT::ValueType DstEltVT) {
2388 MVT::ValueType SrcEltVT = BV->getOperand(0).getValueType();
2389
2390 // If this is already the right type, we're done.
2391 if (SrcEltVT == DstEltVT) return SDOperand(BV, 0);
2392
2393 unsigned SrcBitSize = MVT::getSizeInBits(SrcEltVT);
2394 unsigned DstBitSize = MVT::getSizeInBits(DstEltVT);
2395
2396 // If this is a conversion of N elements of one type to N elements of another
2397 // type, convert each element. This handles FP<->INT cases.
2398 if (SrcBitSize == DstBitSize) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002399 SmallVector<SDOperand, 8> Ops;
Chris Lattner3e104b12006-04-08 04:15:24 +00002400 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00002401 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, DstEltVT, BV->getOperand(i)));
Chris Lattner3e104b12006-04-08 04:15:24 +00002402 AddToWorkList(Ops.back().Val);
2403 }
Chris Lattner6258fb22006-04-02 02:53:43 +00002404 Ops.push_back(*(BV->op_end()-2)); // Add num elements.
2405 Ops.push_back(DAG.getValueType(DstEltVT));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002406 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00002407 }
2408
2409 // Otherwise, we're growing or shrinking the elements. To avoid having to
2410 // handle annoying details of growing/shrinking FP values, we convert them to
2411 // int first.
2412 if (MVT::isFloatingPoint(SrcEltVT)) {
2413 // Convert the input float vector to a int vector where the elements are the
2414 // same sizes.
2415 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
2416 MVT::ValueType IntVT = SrcEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
2417 BV = ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(BV, IntVT).Val;
2418 SrcEltVT = IntVT;
2419 }
2420
2421 // Now we know the input is an integer vector. If the output is a FP type,
2422 // convert to integer first, then to FP of the right size.
2423 if (MVT::isFloatingPoint(DstEltVT)) {
2424 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
2425 MVT::ValueType TmpVT = DstEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
2426 SDNode *Tmp = ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(BV, TmpVT).Val;
2427
2428 // Next, convert to FP elements of the same size.
2429 return ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(Tmp, DstEltVT);
2430 }
2431
2432 // Okay, we know the src/dst types are both integers of differing types.
2433 // Handling growing first.
2434 assert(MVT::isInteger(SrcEltVT) && MVT::isInteger(DstEltVT));
2435 if (SrcBitSize < DstBitSize) {
2436 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
2437
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002438 SmallVector<SDOperand, 8> Ops;
Chris Lattner6258fb22006-04-02 02:53:43 +00002439 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e;
2440 i += NumInputsPerOutput) {
2441 bool isLE = TLI.isLittleEndian();
2442 uint64_t NewBits = 0;
2443 bool EltIsUndef = true;
2444 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
2445 // Shift the previously computed bits over.
2446 NewBits <<= SrcBitSize;
2447 SDOperand Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
2448 if (Op.getOpcode() == ISD::UNDEF) continue;
2449 EltIsUndef = false;
2450
2451 NewBits |= cast<ConstantSDNode>(Op)->getValue();
2452 }
2453
2454 if (EltIsUndef)
2455 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
2456 else
2457 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
2458 }
2459
2460 Ops.push_back(DAG.getConstant(Ops.size(), MVT::i32)); // Add num elements.
2461 Ops.push_back(DAG.getValueType(DstEltVT)); // Add element size.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002462 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00002463 }
2464
2465 // Finally, this must be the case where we are shrinking elements: each input
2466 // turns into multiple outputs.
2467 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002468 SmallVector<SDOperand, 8> Ops;
Chris Lattner6258fb22006-04-02 02:53:43 +00002469 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e; ++i) {
2470 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
2471 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
2472 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
2473 continue;
2474 }
2475 uint64_t OpVal = cast<ConstantSDNode>(BV->getOperand(i))->getValue();
2476
2477 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
2478 unsigned ThisVal = OpVal & ((1ULL << DstBitSize)-1);
2479 OpVal >>= DstBitSize;
2480 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
2481 }
2482
2483 // For big endian targets, swap the order of the pieces of each element.
2484 if (!TLI.isLittleEndian())
2485 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
2486 }
2487 Ops.push_back(DAG.getConstant(Ops.size(), MVT::i32)); // Add num elements.
2488 Ops.push_back(DAG.getValueType(DstEltVT)); // Add element size.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002489 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00002490}
2491
2492
2493
Chris Lattner01b3d732005-09-28 22:28:18 +00002494SDOperand DAGCombiner::visitFADD(SDNode *N) {
2495 SDOperand N0 = N->getOperand(0);
2496 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00002497 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2498 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002499 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00002500
2501 // fold (fadd c1, c2) -> c1+c2
2502 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002503 return DAG.getNode(ISD::FADD, VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00002504 // canonicalize constant to RHS
2505 if (N0CFP && !N1CFP)
2506 return DAG.getNode(ISD::FADD, VT, N1, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00002507 // fold (A + (-B)) -> A-B
2508 if (N1.getOpcode() == ISD::FNEG)
2509 return DAG.getNode(ISD::FSUB, VT, N0, N1.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00002510 // fold ((-A) + B) -> B-A
2511 if (N0.getOpcode() == ISD::FNEG)
2512 return DAG.getNode(ISD::FSUB, VT, N1, N0.getOperand(0));
Chris Lattnerddae4bd2007-01-08 23:04:05 +00002513
2514 // If allowed, fold (fadd (fadd x, c1), c2) -> (fadd x, (fadd c1, c2))
2515 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FADD &&
2516 N0.Val->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
2517 return DAG.getNode(ISD::FADD, VT, N0.getOperand(0),
2518 DAG.getNode(ISD::FADD, VT, N0.getOperand(1), N1));
2519
Chris Lattner01b3d732005-09-28 22:28:18 +00002520 return SDOperand();
2521}
2522
2523SDOperand DAGCombiner::visitFSUB(SDNode *N) {
2524 SDOperand N0 = N->getOperand(0);
2525 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00002526 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2527 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002528 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00002529
2530 // fold (fsub c1, c2) -> c1-c2
2531 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002532 return DAG.getNode(ISD::FSUB, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002533 // fold (A-(-B)) -> A+B
2534 if (N1.getOpcode() == ISD::FNEG)
Nate Begemana148d982006-01-18 22:35:16 +00002535 return DAG.getNode(ISD::FADD, VT, N0, N1.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00002536 return SDOperand();
2537}
2538
2539SDOperand DAGCombiner::visitFMUL(SDNode *N) {
2540 SDOperand N0 = N->getOperand(0);
2541 SDOperand N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00002542 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2543 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002544 MVT::ValueType VT = N->getValueType(0);
2545
Nate Begeman11af4ea2005-10-17 20:40:11 +00002546 // fold (fmul c1, c2) -> c1*c2
2547 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002548 return DAG.getNode(ISD::FMUL, VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00002549 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00002550 if (N0CFP && !N1CFP)
2551 return DAG.getNode(ISD::FMUL, VT, N1, N0);
Nate Begeman11af4ea2005-10-17 20:40:11 +00002552 // fold (fmul X, 2.0) -> (fadd X, X)
2553 if (N1CFP && N1CFP->isExactlyValue(+2.0))
2554 return DAG.getNode(ISD::FADD, VT, N0, N0);
Chris Lattnerddae4bd2007-01-08 23:04:05 +00002555
2556 // If allowed, fold (fmul (fmul x, c1), c2) -> (fmul x, (fmul c1, c2))
2557 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FMUL &&
2558 N0.Val->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
2559 return DAG.getNode(ISD::FMUL, VT, N0.getOperand(0),
2560 DAG.getNode(ISD::FMUL, VT, N0.getOperand(1), N1));
2561
Chris Lattner01b3d732005-09-28 22:28:18 +00002562 return SDOperand();
2563}
2564
2565SDOperand DAGCombiner::visitFDIV(SDNode *N) {
2566 SDOperand N0 = N->getOperand(0);
2567 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00002568 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2569 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002570 MVT::ValueType VT = N->getValueType(0);
2571
Nate Begemana148d982006-01-18 22:35:16 +00002572 // fold (fdiv c1, c2) -> c1/c2
2573 if (N0CFP && N1CFP)
2574 return DAG.getNode(ISD::FDIV, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002575 return SDOperand();
2576}
2577
2578SDOperand DAGCombiner::visitFREM(SDNode *N) {
2579 SDOperand N0 = N->getOperand(0);
2580 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00002581 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2582 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002583 MVT::ValueType VT = N->getValueType(0);
2584
Nate Begemana148d982006-01-18 22:35:16 +00002585 // fold (frem c1, c2) -> fmod(c1,c2)
2586 if (N0CFP && N1CFP)
2587 return DAG.getNode(ISD::FREM, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002588 return SDOperand();
2589}
2590
Chris Lattner12d83032006-03-05 05:30:57 +00002591SDOperand DAGCombiner::visitFCOPYSIGN(SDNode *N) {
2592 SDOperand N0 = N->getOperand(0);
2593 SDOperand N1 = N->getOperand(1);
2594 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2595 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2596 MVT::ValueType VT = N->getValueType(0);
2597
2598 if (N0CFP && N1CFP) // Constant fold
2599 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1);
2600
2601 if (N1CFP) {
2602 // copysign(x, c1) -> fabs(x) iff ispos(c1)
2603 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
2604 union {
2605 double d;
2606 int64_t i;
2607 } u;
2608 u.d = N1CFP->getValue();
2609 if (u.i >= 0)
2610 return DAG.getNode(ISD::FABS, VT, N0);
2611 else
2612 return DAG.getNode(ISD::FNEG, VT, DAG.getNode(ISD::FABS, VT, N0));
2613 }
2614
2615 // copysign(fabs(x), y) -> copysign(x, y)
2616 // copysign(fneg(x), y) -> copysign(x, y)
2617 // copysign(copysign(x,z), y) -> copysign(x, y)
2618 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
2619 N0.getOpcode() == ISD::FCOPYSIGN)
2620 return DAG.getNode(ISD::FCOPYSIGN, VT, N0.getOperand(0), N1);
2621
2622 // copysign(x, abs(y)) -> abs(x)
2623 if (N1.getOpcode() == ISD::FABS)
2624 return DAG.getNode(ISD::FABS, VT, N0);
2625
2626 // copysign(x, copysign(y,z)) -> copysign(x, z)
2627 if (N1.getOpcode() == ISD::FCOPYSIGN)
2628 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(1));
2629
2630 // copysign(x, fp_extend(y)) -> copysign(x, y)
2631 // copysign(x, fp_round(y)) -> copysign(x, y)
2632 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
2633 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(0));
2634
2635 return SDOperand();
2636}
2637
2638
Chris Lattner01b3d732005-09-28 22:28:18 +00002639
Nate Begeman83e75ec2005-09-06 04:43:02 +00002640SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002641 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002642 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00002643 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002644
2645 // fold (sint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002646 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00002647 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002648 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002649}
2650
Nate Begeman83e75ec2005-09-06 04:43:02 +00002651SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002652 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002653 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00002654 MVT::ValueType VT = N->getValueType(0);
2655
Nate Begeman1d4d4142005-09-01 00:19:25 +00002656 // fold (uint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002657 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00002658 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002659 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002660}
2661
Nate Begeman83e75ec2005-09-06 04:43:02 +00002662SDOperand DAGCombiner::visitFP_TO_SINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002663 SDOperand N0 = N->getOperand(0);
2664 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2665 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002666
2667 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00002668 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002669 return DAG.getNode(ISD::FP_TO_SINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002670 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002671}
2672
Nate Begeman83e75ec2005-09-06 04:43:02 +00002673SDOperand DAGCombiner::visitFP_TO_UINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002674 SDOperand N0 = N->getOperand(0);
2675 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2676 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002677
2678 // fold (fp_to_uint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00002679 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002680 return DAG.getNode(ISD::FP_TO_UINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002681 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002682}
2683
Nate Begeman83e75ec2005-09-06 04:43:02 +00002684SDOperand DAGCombiner::visitFP_ROUND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002685 SDOperand N0 = N->getOperand(0);
2686 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2687 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002688
2689 // fold (fp_round c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002690 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002691 return DAG.getNode(ISD::FP_ROUND, VT, N0);
Chris Lattner79dbea52006-03-13 06:26:26 +00002692
2693 // fold (fp_round (fp_extend x)) -> x
2694 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
2695 return N0.getOperand(0);
2696
2697 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
2698 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.Val->hasOneUse()) {
2699 SDOperand Tmp = DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0));
2700 AddToWorkList(Tmp.Val);
2701 return DAG.getNode(ISD::FCOPYSIGN, VT, Tmp, N0.getOperand(1));
2702 }
2703
Nate Begeman83e75ec2005-09-06 04:43:02 +00002704 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002705}
2706
Nate Begeman83e75ec2005-09-06 04:43:02 +00002707SDOperand DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002708 SDOperand N0 = N->getOperand(0);
2709 MVT::ValueType VT = N->getValueType(0);
2710 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00002711 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002712
Nate Begeman1d4d4142005-09-01 00:19:25 +00002713 // fold (fp_round_inreg c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002714 if (N0CFP) {
2715 SDOperand Round = DAG.getConstantFP(N0CFP->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002716 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002717 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00002718 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002719}
2720
Nate Begeman83e75ec2005-09-06 04:43:02 +00002721SDOperand DAGCombiner::visitFP_EXTEND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002722 SDOperand N0 = N->getOperand(0);
2723 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2724 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002725
2726 // fold (fp_extend c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002727 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002728 return DAG.getNode(ISD::FP_EXTEND, VT, N0);
Chris Lattnere564dbb2006-05-05 21:34:35 +00002729
2730 // fold (fpext (load x)) -> (fpext (fpround (extload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00002731 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00002732 (!AfterLegalize||TLI.isLoadXLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00002733 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2734 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
2735 LN0->getBasePtr(), LN0->getSrcValue(),
2736 LN0->getSrcValueOffset(),
Chris Lattnere564dbb2006-05-05 21:34:35 +00002737 N0.getValueType());
2738 CombineTo(N, ExtLoad);
2739 CombineTo(N0.Val, DAG.getNode(ISD::FP_ROUND, N0.getValueType(), ExtLoad),
2740 ExtLoad.getValue(1));
2741 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2742 }
2743
2744
Nate Begeman83e75ec2005-09-06 04:43:02 +00002745 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002746}
2747
Nate Begeman83e75ec2005-09-06 04:43:02 +00002748SDOperand DAGCombiner::visitFNEG(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002749 SDOperand N0 = N->getOperand(0);
2750 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2751 MVT::ValueType VT = N->getValueType(0);
2752
2753 // fold (fneg c1) -> -c1
Nate Begeman646d7e22005-09-02 21:18:40 +00002754 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002755 return DAG.getNode(ISD::FNEG, VT, N0);
2756 // fold (fneg (sub x, y)) -> (sub y, x)
Chris Lattner12d83032006-03-05 05:30:57 +00002757 if (N0.getOpcode() == ISD::SUB)
2758 return DAG.getNode(ISD::SUB, VT, N0.getOperand(1), N0.getOperand(0));
Nate Begemana148d982006-01-18 22:35:16 +00002759 // fold (fneg (fneg x)) -> x
Chris Lattner12d83032006-03-05 05:30:57 +00002760 if (N0.getOpcode() == ISD::FNEG)
2761 return N0.getOperand(0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002762 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002763}
2764
Nate Begeman83e75ec2005-09-06 04:43:02 +00002765SDOperand DAGCombiner::visitFABS(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002766 SDOperand N0 = N->getOperand(0);
2767 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2768 MVT::ValueType VT = N->getValueType(0);
2769
Nate Begeman1d4d4142005-09-01 00:19:25 +00002770 // fold (fabs c1) -> fabs(c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00002771 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002772 return DAG.getNode(ISD::FABS, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002773 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00002774 if (N0.getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002775 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002776 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00002777 // fold (fabs (fcopysign x, y)) -> (fabs x)
2778 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
2779 return DAG.getNode(ISD::FABS, VT, N0.getOperand(0));
2780
Nate Begeman83e75ec2005-09-06 04:43:02 +00002781 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002782}
2783
Nate Begeman44728a72005-09-19 22:34:01 +00002784SDOperand DAGCombiner::visitBRCOND(SDNode *N) {
2785 SDOperand Chain = N->getOperand(0);
2786 SDOperand N1 = N->getOperand(1);
2787 SDOperand N2 = N->getOperand(2);
2788 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2789
2790 // never taken branch, fold to chain
2791 if (N1C && N1C->isNullValue())
2792 return Chain;
2793 // unconditional branch
Nate Begemane17daeb2005-10-05 21:43:42 +00002794 if (N1C && N1C->getValue() == 1)
Nate Begeman44728a72005-09-19 22:34:01 +00002795 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
Nate Begeman750ac1b2006-02-01 07:19:44 +00002796 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
2797 // on the target.
2798 if (N1.getOpcode() == ISD::SETCC &&
2799 TLI.isOperationLegal(ISD::BR_CC, MVT::Other)) {
2800 return DAG.getNode(ISD::BR_CC, MVT::Other, Chain, N1.getOperand(2),
2801 N1.getOperand(0), N1.getOperand(1), N2);
2802 }
Nate Begeman44728a72005-09-19 22:34:01 +00002803 return SDOperand();
2804}
2805
Chris Lattner3ea0b472005-10-05 06:47:48 +00002806// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
2807//
Nate Begeman44728a72005-09-19 22:34:01 +00002808SDOperand DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00002809 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
2810 SDOperand CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
2811
2812 // Use SimplifySetCC to simplify SETCC's.
Nate Begemane17daeb2005-10-05 21:43:42 +00002813 SDOperand Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false);
Chris Lattner30f73e72006-10-14 03:52:46 +00002814 if (Simp.Val) AddToWorkList(Simp.Val);
2815
Nate Begemane17daeb2005-10-05 21:43:42 +00002816 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.Val);
2817
2818 // fold br_cc true, dest -> br dest (unconditional branch)
2819 if (SCCC && SCCC->getValue())
2820 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
2821 N->getOperand(4));
2822 // fold br_cc false, dest -> unconditional fall through
2823 if (SCCC && SCCC->isNullValue())
2824 return N->getOperand(0);
Chris Lattner30f73e72006-10-14 03:52:46 +00002825
Nate Begemane17daeb2005-10-05 21:43:42 +00002826 // fold to a simpler setcc
2827 if (Simp.Val && Simp.getOpcode() == ISD::SETCC)
2828 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
2829 Simp.getOperand(2), Simp.getOperand(0),
2830 Simp.getOperand(1), N->getOperand(4));
Nate Begeman44728a72005-09-19 22:34:01 +00002831 return SDOperand();
2832}
2833
Chris Lattner448f2192006-11-11 00:39:41 +00002834
2835/// CombineToPreIndexedLoadStore - Try turning a load / store and a
2836/// pre-indexed load / store when the base pointer is a add or subtract
2837/// and it has other uses besides the load / store. After the
2838/// transformation, the new indexed load / store has effectively folded
2839/// the add / subtract in and all of its other uses are redirected to the
2840/// new load / store.
2841bool DAGCombiner::CombineToPreIndexedLoadStore(SDNode *N) {
2842 if (!AfterLegalize)
2843 return false;
2844
2845 bool isLoad = true;
2846 SDOperand Ptr;
2847 MVT::ValueType VT;
2848 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Evan Chenge90460e2006-12-16 06:25:23 +00002849 if (LD->getAddressingMode() != ISD::UNINDEXED)
2850 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00002851 VT = LD->getLoadedVT();
Evan Chenge90460e2006-12-16 06:25:23 +00002852 if (LD->getAddressingMode() != ISD::UNINDEXED &&
2853 !TLI.isIndexedLoadLegal(ISD::PRE_INC, VT) &&
Chris Lattner448f2192006-11-11 00:39:41 +00002854 !TLI.isIndexedLoadLegal(ISD::PRE_DEC, VT))
2855 return false;
2856 Ptr = LD->getBasePtr();
2857 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Evan Chenge90460e2006-12-16 06:25:23 +00002858 if (ST->getAddressingMode() != ISD::UNINDEXED)
2859 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00002860 VT = ST->getStoredVT();
2861 if (!TLI.isIndexedStoreLegal(ISD::PRE_INC, VT) &&
2862 !TLI.isIndexedStoreLegal(ISD::PRE_DEC, VT))
2863 return false;
2864 Ptr = ST->getBasePtr();
2865 isLoad = false;
2866 } else
2867 return false;
2868
Chris Lattner9f1794e2006-11-11 00:56:29 +00002869 // If the pointer is not an add/sub, or if it doesn't have multiple uses, bail
2870 // out. There is no reason to make this a preinc/predec.
2871 if ((Ptr.getOpcode() != ISD::ADD && Ptr.getOpcode() != ISD::SUB) ||
2872 Ptr.Val->hasOneUse())
2873 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00002874
Chris Lattner9f1794e2006-11-11 00:56:29 +00002875 // Ask the target to do addressing mode selection.
2876 SDOperand BasePtr;
2877 SDOperand Offset;
2878 ISD::MemIndexedMode AM = ISD::UNINDEXED;
2879 if (!TLI.getPreIndexedAddressParts(N, BasePtr, Offset, AM, DAG))
2880 return false;
2881
Chris Lattner41e53fd2006-11-11 01:00:15 +00002882 // Try turning it into a pre-indexed load / store except when:
2883 // 1) The base is a frame index.
2884 // 2) If N is a store and the ptr is either the same as or is a
Chris Lattner9f1794e2006-11-11 00:56:29 +00002885 // predecessor of the value being stored.
Chris Lattner41e53fd2006-11-11 01:00:15 +00002886 // 3) Another use of base ptr is a predecessor of N. If ptr is folded
Chris Lattner9f1794e2006-11-11 00:56:29 +00002887 // that would create a cycle.
Chris Lattner41e53fd2006-11-11 01:00:15 +00002888 // 4) All uses are load / store ops that use it as base ptr.
Chris Lattner448f2192006-11-11 00:39:41 +00002889
Chris Lattner41e53fd2006-11-11 01:00:15 +00002890 // Check #1. Preinc'ing a frame index would require copying the stack pointer
2891 // (plus the implicit offset) to a register to preinc anyway.
2892 if (isa<FrameIndexSDNode>(BasePtr))
2893 return false;
2894
2895 // Check #2.
Chris Lattner9f1794e2006-11-11 00:56:29 +00002896 if (!isLoad) {
2897 SDOperand Val = cast<StoreSDNode>(N)->getValue();
2898 if (Val == Ptr || Ptr.Val->isPredecessor(Val.Val))
2899 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00002900 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00002901
2902 // Now check for #2 and #3.
2903 bool RealUse = false;
2904 for (SDNode::use_iterator I = Ptr.Val->use_begin(),
2905 E = Ptr.Val->use_end(); I != E; ++I) {
2906 SDNode *Use = *I;
2907 if (Use == N)
2908 continue;
2909 if (Use->isPredecessor(N))
2910 return false;
2911
2912 if (!((Use->getOpcode() == ISD::LOAD &&
2913 cast<LoadSDNode>(Use)->getBasePtr() == Ptr) ||
2914 (Use->getOpcode() == ISD::STORE) &&
2915 cast<StoreSDNode>(Use)->getBasePtr() == Ptr))
2916 RealUse = true;
2917 }
2918 if (!RealUse)
2919 return false;
2920
2921 SDOperand Result;
2922 if (isLoad)
2923 Result = DAG.getIndexedLoad(SDOperand(N,0), BasePtr, Offset, AM);
2924 else
2925 Result = DAG.getIndexedStore(SDOperand(N,0), BasePtr, Offset, AM);
2926 ++PreIndexedNodes;
2927 ++NodesCombined;
Bill Wendling832171c2006-12-07 20:04:42 +00002928 DOUT << "\nReplacing.4 "; DEBUG(N->dump());
2929 DOUT << "\nWith: "; DEBUG(Result.Val->dump(&DAG));
2930 DOUT << '\n';
Chris Lattner9f1794e2006-11-11 00:56:29 +00002931 std::vector<SDNode*> NowDead;
2932 if (isLoad) {
2933 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(0),
2934 NowDead);
2935 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1), Result.getValue(2),
2936 NowDead);
2937 } else {
2938 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(1),
2939 NowDead);
2940 }
2941
2942 // Nodes can end up on the worklist more than once. Make sure we do
2943 // not process a node that has been replaced.
2944 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
2945 removeFromWorkList(NowDead[i]);
2946 // Finally, since the node is now dead, remove it from the graph.
2947 DAG.DeleteNode(N);
2948
2949 // Replace the uses of Ptr with uses of the updated base value.
2950 DAG.ReplaceAllUsesOfValueWith(Ptr, Result.getValue(isLoad ? 1 : 0),
2951 NowDead);
2952 removeFromWorkList(Ptr.Val);
2953 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
2954 removeFromWorkList(NowDead[i]);
2955 DAG.DeleteNode(Ptr.Val);
2956
2957 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00002958}
2959
2960/// CombineToPostIndexedLoadStore - Try combine a load / store with a
2961/// add / sub of the base pointer node into a post-indexed load / store.
2962/// The transformation folded the add / subtract into the new indexed
2963/// load / store effectively and all of its uses are redirected to the
2964/// new load / store.
2965bool DAGCombiner::CombineToPostIndexedLoadStore(SDNode *N) {
2966 if (!AfterLegalize)
2967 return false;
2968
2969 bool isLoad = true;
2970 SDOperand Ptr;
2971 MVT::ValueType VT;
2972 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Evan Chenge90460e2006-12-16 06:25:23 +00002973 if (LD->getAddressingMode() != ISD::UNINDEXED)
2974 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00002975 VT = LD->getLoadedVT();
2976 if (!TLI.isIndexedLoadLegal(ISD::POST_INC, VT) &&
2977 !TLI.isIndexedLoadLegal(ISD::POST_DEC, VT))
2978 return false;
2979 Ptr = LD->getBasePtr();
2980 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Evan Chenge90460e2006-12-16 06:25:23 +00002981 if (ST->getAddressingMode() != ISD::UNINDEXED)
2982 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00002983 VT = ST->getStoredVT();
2984 if (!TLI.isIndexedStoreLegal(ISD::POST_INC, VT) &&
2985 !TLI.isIndexedStoreLegal(ISD::POST_DEC, VT))
2986 return false;
2987 Ptr = ST->getBasePtr();
2988 isLoad = false;
2989 } else
2990 return false;
2991
Evan Chengcc470212006-11-16 00:08:20 +00002992 if (Ptr.Val->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00002993 return false;
2994
2995 for (SDNode::use_iterator I = Ptr.Val->use_begin(),
2996 E = Ptr.Val->use_end(); I != E; ++I) {
2997 SDNode *Op = *I;
2998 if (Op == N ||
2999 (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB))
3000 continue;
3001
3002 SDOperand BasePtr;
3003 SDOperand Offset;
3004 ISD::MemIndexedMode AM = ISD::UNINDEXED;
3005 if (TLI.getPostIndexedAddressParts(N, Op, BasePtr, Offset, AM, DAG)) {
3006 if (Ptr == Offset)
3007 std::swap(BasePtr, Offset);
3008 if (Ptr != BasePtr)
Chris Lattner448f2192006-11-11 00:39:41 +00003009 continue;
3010
Chris Lattner9f1794e2006-11-11 00:56:29 +00003011 // Try turning it into a post-indexed load / store except when
3012 // 1) All uses are load / store ops that use it as base ptr.
3013 // 2) Op must be independent of N, i.e. Op is neither a predecessor
3014 // nor a successor of N. Otherwise, if Op is folded that would
3015 // create a cycle.
3016
3017 // Check for #1.
3018 bool TryNext = false;
3019 for (SDNode::use_iterator II = BasePtr.Val->use_begin(),
3020 EE = BasePtr.Val->use_end(); II != EE; ++II) {
3021 SDNode *Use = *II;
3022 if (Use == Ptr.Val)
Chris Lattner448f2192006-11-11 00:39:41 +00003023 continue;
3024
Chris Lattner9f1794e2006-11-11 00:56:29 +00003025 // If all the uses are load / store addresses, then don't do the
3026 // transformation.
3027 if (Use->getOpcode() == ISD::ADD || Use->getOpcode() == ISD::SUB){
3028 bool RealUse = false;
3029 for (SDNode::use_iterator III = Use->use_begin(),
3030 EEE = Use->use_end(); III != EEE; ++III) {
3031 SDNode *UseUse = *III;
3032 if (!((UseUse->getOpcode() == ISD::LOAD &&
3033 cast<LoadSDNode>(UseUse)->getBasePtr().Val == Use) ||
3034 (UseUse->getOpcode() == ISD::STORE) &&
3035 cast<StoreSDNode>(UseUse)->getBasePtr().Val == Use))
3036 RealUse = true;
3037 }
Chris Lattner448f2192006-11-11 00:39:41 +00003038
Chris Lattner9f1794e2006-11-11 00:56:29 +00003039 if (!RealUse) {
3040 TryNext = true;
3041 break;
Chris Lattner448f2192006-11-11 00:39:41 +00003042 }
3043 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00003044 }
3045 if (TryNext)
3046 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00003047
Chris Lattner9f1794e2006-11-11 00:56:29 +00003048 // Check for #2
3049 if (!Op->isPredecessor(N) && !N->isPredecessor(Op)) {
3050 SDOperand Result = isLoad
3051 ? DAG.getIndexedLoad(SDOperand(N,0), BasePtr, Offset, AM)
3052 : DAG.getIndexedStore(SDOperand(N,0), BasePtr, Offset, AM);
3053 ++PostIndexedNodes;
3054 ++NodesCombined;
Bill Wendling832171c2006-12-07 20:04:42 +00003055 DOUT << "\nReplacing.5 "; DEBUG(N->dump());
3056 DOUT << "\nWith: "; DEBUG(Result.Val->dump(&DAG));
3057 DOUT << '\n';
Chris Lattner9f1794e2006-11-11 00:56:29 +00003058 std::vector<SDNode*> NowDead;
3059 if (isLoad) {
3060 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(0),
Chris Lattner448f2192006-11-11 00:39:41 +00003061 NowDead);
Chris Lattner9f1794e2006-11-11 00:56:29 +00003062 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1), Result.getValue(2),
3063 NowDead);
3064 } else {
3065 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(1),
3066 NowDead);
Chris Lattner448f2192006-11-11 00:39:41 +00003067 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00003068
3069 // Nodes can end up on the worklist more than once. Make sure we do
3070 // not process a node that has been replaced.
3071 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
3072 removeFromWorkList(NowDead[i]);
3073 // Finally, since the node is now dead, remove it from the graph.
3074 DAG.DeleteNode(N);
3075
3076 // Replace the uses of Use with uses of the updated base value.
3077 DAG.ReplaceAllUsesOfValueWith(SDOperand(Op, 0),
3078 Result.getValue(isLoad ? 1 : 0),
3079 NowDead);
3080 removeFromWorkList(Op);
3081 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
3082 removeFromWorkList(NowDead[i]);
3083 DAG.DeleteNode(Op);
3084
3085 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00003086 }
3087 }
3088 }
3089 return false;
3090}
3091
3092
Chris Lattner01a22022005-10-10 22:04:48 +00003093SDOperand DAGCombiner::visitLOAD(SDNode *N) {
Evan Cheng466685d2006-10-09 20:57:25 +00003094 LoadSDNode *LD = cast<LoadSDNode>(N);
3095 SDOperand Chain = LD->getChain();
3096 SDOperand Ptr = LD->getBasePtr();
Jim Laskey6ff23e52006-10-04 16:53:27 +00003097
Chris Lattnere4b95392006-03-31 18:06:18 +00003098 // If there are no uses of the loaded value, change uses of the chain value
3099 // into uses of the chain input (i.e. delete the dead load).
3100 if (N->hasNUsesOfValue(0, 0))
3101 return CombineTo(N, DAG.getNode(ISD::UNDEF, N->getValueType(0)), Chain);
Chris Lattner01a22022005-10-10 22:04:48 +00003102
3103 // If this load is directly stored, replace the load value with the stored
3104 // value.
3105 // TODO: Handle store large -> read small portion.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00003106 // TODO: Handle TRUNCSTORE/LOADEXT
3107 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00003108 if (ISD::isNON_TRUNCStore(Chain.Val)) {
3109 StoreSDNode *PrevST = cast<StoreSDNode>(Chain);
3110 if (PrevST->getBasePtr() == Ptr &&
3111 PrevST->getValue().getValueType() == N->getValueType(0))
Jim Laskeyc2b19f32006-10-11 17:47:52 +00003112 return CombineTo(N, Chain.getOperand(1), Chain);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003113 }
Jim Laskeyc2b19f32006-10-11 17:47:52 +00003114 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00003115
Jim Laskey7ca56af2006-10-11 13:47:09 +00003116 if (CombinerAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00003117 // Walk up chain skipping non-aliasing memory nodes.
3118 SDOperand BetterChain = FindBetterChain(N, Chain);
3119
Jim Laskey6ff23e52006-10-04 16:53:27 +00003120 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00003121 if (Chain != BetterChain) {
Jim Laskeyc2b19f32006-10-11 17:47:52 +00003122 SDOperand ReplLoad;
3123
Jim Laskey279f0532006-09-25 16:29:54 +00003124 // Replace the chain to void dependency.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00003125 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
3126 ReplLoad = DAG.getLoad(N->getValueType(0), BetterChain, Ptr,
3127 LD->getSrcValue(), LD->getSrcValueOffset());
3128 } else {
3129 ReplLoad = DAG.getExtLoad(LD->getExtensionType(),
3130 LD->getValueType(0),
3131 BetterChain, Ptr, LD->getSrcValue(),
3132 LD->getSrcValueOffset(),
3133 LD->getLoadedVT());
3134 }
Jim Laskey279f0532006-09-25 16:29:54 +00003135
Jim Laskey6ff23e52006-10-04 16:53:27 +00003136 // Create token factor to keep old chain connected.
Jim Laskey288af5e2006-09-25 19:32:58 +00003137 SDOperand Token = DAG.getNode(ISD::TokenFactor, MVT::Other,
3138 Chain, ReplLoad.getValue(1));
Jim Laskey6ff23e52006-10-04 16:53:27 +00003139
Jim Laskey274062c2006-10-13 23:32:28 +00003140 // Replace uses with load result and token factor. Don't add users
3141 // to work list.
3142 return CombineTo(N, ReplLoad.getValue(0), Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00003143 }
3144 }
3145
Evan Cheng7fc033a2006-11-03 03:06:21 +00003146 // Try transforming N to an indexed load.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00003147 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Evan Cheng7fc033a2006-11-03 03:06:21 +00003148 return SDOperand(N, 0);
3149
Chris Lattner01a22022005-10-10 22:04:48 +00003150 return SDOperand();
3151}
3152
Chris Lattner87514ca2005-10-10 22:31:19 +00003153SDOperand DAGCombiner::visitSTORE(SDNode *N) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00003154 StoreSDNode *ST = cast<StoreSDNode>(N);
3155 SDOperand Chain = ST->getChain();
3156 SDOperand Value = ST->getValue();
3157 SDOperand Ptr = ST->getBasePtr();
Jim Laskey7aed46c2006-10-11 18:55:16 +00003158
Chris Lattnerc33baaa2005-12-23 05:48:07 +00003159 // If this is a store of a bit convert, store the input value.
Chris Lattnerbf40c4b2006-01-15 18:58:59 +00003160 // FIXME: This needs to know that the resultant store does not need a
3161 // higher alignment than the original.
Jim Laskey14fbcbf2006-09-25 21:11:32 +00003162 if (0 && Value.getOpcode() == ISD::BIT_CONVERT) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00003163 return DAG.getStore(Chain, Value.getOperand(0), Ptr, ST->getSrcValue(),
3164 ST->getSrcValueOffset());
Jim Laskey279f0532006-09-25 16:29:54 +00003165 }
3166
Nate Begeman2cbba892006-12-11 02:23:46 +00003167 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Nate Begeman2cbba892006-12-11 02:23:46 +00003168 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Value)) {
Evan Cheng25ece662006-12-11 17:25:19 +00003169 if (Value.getOpcode() != ISD::TargetConstantFP) {
3170 SDOperand Tmp;
Chris Lattner62be1a72006-12-12 04:16:14 +00003171 switch (CFP->getValueType(0)) {
3172 default: assert(0 && "Unknown FP type");
3173 case MVT::f32:
3174 if (!AfterLegalize || TLI.isTypeLegal(MVT::i32)) {
3175 Tmp = DAG.getConstant(FloatToBits(CFP->getValue()), MVT::i32);
3176 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
3177 ST->getSrcValueOffset());
3178 }
3179 break;
3180 case MVT::f64:
3181 if (!AfterLegalize || TLI.isTypeLegal(MVT::i64)) {
3182 Tmp = DAG.getConstant(DoubleToBits(CFP->getValue()), MVT::i64);
3183 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
3184 ST->getSrcValueOffset());
3185 } else if (TLI.isTypeLegal(MVT::i32)) {
3186 // Many FP stores are not make apparent until after legalize, e.g. for
3187 // argument passing. Since this is so common, custom legalize the
3188 // 64-bit integer store into two 32-bit stores.
3189 uint64_t Val = DoubleToBits(CFP->getValue());
3190 SDOperand Lo = DAG.getConstant(Val & 0xFFFFFFFF, MVT::i32);
3191 SDOperand Hi = DAG.getConstant(Val >> 32, MVT::i32);
3192 if (!TLI.isLittleEndian()) std::swap(Lo, Hi);
3193
3194 SDOperand St0 = DAG.getStore(Chain, Lo, Ptr, ST->getSrcValue(),
3195 ST->getSrcValueOffset());
3196 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3197 DAG.getConstant(4, Ptr.getValueType()));
3198 SDOperand St1 = DAG.getStore(Chain, Hi, Ptr, ST->getSrcValue(),
3199 ST->getSrcValueOffset()+4);
3200 return DAG.getNode(ISD::TokenFactor, MVT::Other, St0, St1);
3201 }
3202 break;
Evan Cheng25ece662006-12-11 17:25:19 +00003203 }
Nate Begeman2cbba892006-12-11 02:23:46 +00003204 }
Nate Begeman2cbba892006-12-11 02:23:46 +00003205 }
3206
Jim Laskey279f0532006-09-25 16:29:54 +00003207 if (CombinerAA) {
3208 // Walk up chain skipping non-aliasing memory nodes.
3209 SDOperand BetterChain = FindBetterChain(N, Chain);
3210
Jim Laskey6ff23e52006-10-04 16:53:27 +00003211 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00003212 if (Chain != BetterChain) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00003213 // Replace the chain to avoid dependency.
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00003214 SDOperand ReplStore;
3215 if (ST->isTruncatingStore()) {
3216 ReplStore = DAG.getTruncStore(BetterChain, Value, Ptr,
3217 ST->getSrcValue(),ST->getSrcValueOffset(), ST->getStoredVT());
3218 } else {
3219 ReplStore = DAG.getStore(BetterChain, Value, Ptr,
3220 ST->getSrcValue(), ST->getSrcValueOffset());
3221 }
3222
Jim Laskey279f0532006-09-25 16:29:54 +00003223 // Create token to keep both nodes around.
Jim Laskey274062c2006-10-13 23:32:28 +00003224 SDOperand Token =
3225 DAG.getNode(ISD::TokenFactor, MVT::Other, Chain, ReplStore);
3226
3227 // Don't add users to work list.
3228 return CombineTo(N, Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00003229 }
Jim Laskeyd1aed7a2006-09-21 16:28:59 +00003230 }
Chris Lattnerc33baaa2005-12-23 05:48:07 +00003231
Evan Cheng33dbedc2006-11-05 09:31:14 +00003232 // Try transforming N to an indexed store.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00003233 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Evan Cheng33dbedc2006-11-05 09:31:14 +00003234 return SDOperand(N, 0);
3235
Chris Lattner87514ca2005-10-10 22:31:19 +00003236 return SDOperand();
3237}
3238
Chris Lattnerca242442006-03-19 01:27:56 +00003239SDOperand DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
3240 SDOperand InVec = N->getOperand(0);
3241 SDOperand InVal = N->getOperand(1);
3242 SDOperand EltNo = N->getOperand(2);
3243
3244 // If the invec is a BUILD_VECTOR and if EltNo is a constant, build a new
3245 // vector with the inserted element.
3246 if (InVec.getOpcode() == ISD::BUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
3247 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003248 SmallVector<SDOperand, 8> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
Chris Lattnerca242442006-03-19 01:27:56 +00003249 if (Elt < Ops.size())
3250 Ops[Elt] = InVal;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003251 return DAG.getNode(ISD::BUILD_VECTOR, InVec.getValueType(),
3252 &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00003253 }
3254
3255 return SDOperand();
3256}
3257
3258SDOperand DAGCombiner::visitVINSERT_VECTOR_ELT(SDNode *N) {
3259 SDOperand InVec = N->getOperand(0);
3260 SDOperand InVal = N->getOperand(1);
3261 SDOperand EltNo = N->getOperand(2);
3262 SDOperand NumElts = N->getOperand(3);
3263 SDOperand EltType = N->getOperand(4);
3264
3265 // If the invec is a VBUILD_VECTOR and if EltNo is a constant, build a new
3266 // vector with the inserted element.
3267 if (InVec.getOpcode() == ISD::VBUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
3268 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003269 SmallVector<SDOperand, 8> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
Chris Lattnerca242442006-03-19 01:27:56 +00003270 if (Elt < Ops.size()-2)
3271 Ops[Elt] = InVal;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003272 return DAG.getNode(ISD::VBUILD_VECTOR, InVec.getValueType(),
3273 &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00003274 }
3275
3276 return SDOperand();
3277}
3278
Chris Lattnerd7648c82006-03-28 20:28:38 +00003279SDOperand DAGCombiner::visitVBUILD_VECTOR(SDNode *N) {
3280 unsigned NumInScalars = N->getNumOperands()-2;
3281 SDOperand NumElts = N->getOperand(NumInScalars);
3282 SDOperand EltType = N->getOperand(NumInScalars+1);
3283
3284 // Check to see if this is a VBUILD_VECTOR of a bunch of VEXTRACT_VECTOR_ELT
3285 // operations. If so, and if the EXTRACT_ELT vector inputs come from at most
3286 // two distinct vectors, turn this into a shuffle node.
3287 SDOperand VecIn1, VecIn2;
3288 for (unsigned i = 0; i != NumInScalars; ++i) {
3289 // Ignore undef inputs.
3290 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
3291
3292 // If this input is something other than a VEXTRACT_VECTOR_ELT with a
3293 // constant index, bail out.
3294 if (N->getOperand(i).getOpcode() != ISD::VEXTRACT_VECTOR_ELT ||
3295 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
3296 VecIn1 = VecIn2 = SDOperand(0, 0);
3297 break;
3298 }
3299
3300 // If the input vector type disagrees with the result of the vbuild_vector,
3301 // we can't make a shuffle.
3302 SDOperand ExtractedFromVec = N->getOperand(i).getOperand(0);
3303 if (*(ExtractedFromVec.Val->op_end()-2) != NumElts ||
3304 *(ExtractedFromVec.Val->op_end()-1) != EltType) {
3305 VecIn1 = VecIn2 = SDOperand(0, 0);
3306 break;
3307 }
3308
3309 // Otherwise, remember this. We allow up to two distinct input vectors.
3310 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
3311 continue;
3312
3313 if (VecIn1.Val == 0) {
3314 VecIn1 = ExtractedFromVec;
3315 } else if (VecIn2.Val == 0) {
3316 VecIn2 = ExtractedFromVec;
3317 } else {
3318 // Too many inputs.
3319 VecIn1 = VecIn2 = SDOperand(0, 0);
3320 break;
3321 }
3322 }
3323
3324 // If everything is good, we can make a shuffle operation.
3325 if (VecIn1.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003326 SmallVector<SDOperand, 8> BuildVecIndices;
Chris Lattnerd7648c82006-03-28 20:28:38 +00003327 for (unsigned i = 0; i != NumInScalars; ++i) {
3328 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
Evan Cheng597a3bd2007-01-20 10:10:26 +00003329 BuildVecIndices.push_back(DAG.getNode(ISD::UNDEF, TLI.getPointerTy()));
Chris Lattnerd7648c82006-03-28 20:28:38 +00003330 continue;
3331 }
3332
3333 SDOperand Extract = N->getOperand(i);
3334
3335 // If extracting from the first vector, just use the index directly.
3336 if (Extract.getOperand(0) == VecIn1) {
3337 BuildVecIndices.push_back(Extract.getOperand(1));
3338 continue;
3339 }
3340
3341 // Otherwise, use InIdx + VecSize
3342 unsigned Idx = cast<ConstantSDNode>(Extract.getOperand(1))->getValue();
Evan Cheng597a3bd2007-01-20 10:10:26 +00003343 BuildVecIndices.push_back(DAG.getConstant(Idx+NumInScalars,
3344 TLI.getPointerTy()));
Chris Lattnerd7648c82006-03-28 20:28:38 +00003345 }
3346
3347 // Add count and size info.
3348 BuildVecIndices.push_back(NumElts);
Evan Cheng597a3bd2007-01-20 10:10:26 +00003349 BuildVecIndices.push_back(DAG.getValueType(TLI.getPointerTy()));
Chris Lattnerd7648c82006-03-28 20:28:38 +00003350
3351 // Return the new VVECTOR_SHUFFLE node.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003352 SDOperand Ops[5];
3353 Ops[0] = VecIn1;
Chris Lattnercef896e2006-03-28 22:19:47 +00003354 if (VecIn2.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003355 Ops[1] = VecIn2;
Chris Lattnercef896e2006-03-28 22:19:47 +00003356 } else {
3357 // Use an undef vbuild_vector as input for the second operand.
3358 std::vector<SDOperand> UnOps(NumInScalars,
3359 DAG.getNode(ISD::UNDEF,
3360 cast<VTSDNode>(EltType)->getVT()));
3361 UnOps.push_back(NumElts);
3362 UnOps.push_back(EltType);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003363 Ops[1] = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
3364 &UnOps[0], UnOps.size());
3365 AddToWorkList(Ops[1].Val);
Chris Lattnercef896e2006-03-28 22:19:47 +00003366 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003367 Ops[2] = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
3368 &BuildVecIndices[0], BuildVecIndices.size());
3369 Ops[3] = NumElts;
3370 Ops[4] = EltType;
3371 return DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector, Ops, 5);
Chris Lattnerd7648c82006-03-28 20:28:38 +00003372 }
3373
3374 return SDOperand();
3375}
3376
Chris Lattner66445d32006-03-28 22:11:53 +00003377SDOperand DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
Chris Lattnerf1d0c622006-03-31 22:16:43 +00003378 SDOperand ShufMask = N->getOperand(2);
3379 unsigned NumElts = ShufMask.getNumOperands();
3380
3381 // If the shuffle mask is an identity operation on the LHS, return the LHS.
3382 bool isIdentity = true;
3383 for (unsigned i = 0; i != NumElts; ++i) {
3384 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
3385 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
3386 isIdentity = false;
3387 break;
3388 }
3389 }
3390 if (isIdentity) return N->getOperand(0);
3391
3392 // If the shuffle mask is an identity operation on the RHS, return the RHS.
3393 isIdentity = true;
3394 for (unsigned i = 0; i != NumElts; ++i) {
3395 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
3396 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
3397 isIdentity = false;
3398 break;
3399 }
3400 }
3401 if (isIdentity) return N->getOperand(1);
Evan Chenge7bec0d2006-07-20 22:44:41 +00003402
3403 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
3404 // needed at all.
3405 bool isUnary = true;
Evan Cheng917ec982006-07-21 08:25:53 +00003406 bool isSplat = true;
Evan Chenge7bec0d2006-07-20 22:44:41 +00003407 int VecNum = -1;
Reid Spencer9160a6a2006-07-25 20:44:41 +00003408 unsigned BaseIdx = 0;
Evan Chenge7bec0d2006-07-20 22:44:41 +00003409 for (unsigned i = 0; i != NumElts; ++i)
3410 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
3411 unsigned Idx = cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue();
3412 int V = (Idx < NumElts) ? 0 : 1;
Evan Cheng917ec982006-07-21 08:25:53 +00003413 if (VecNum == -1) {
Evan Chenge7bec0d2006-07-20 22:44:41 +00003414 VecNum = V;
Evan Cheng917ec982006-07-21 08:25:53 +00003415 BaseIdx = Idx;
3416 } else {
3417 if (BaseIdx != Idx)
3418 isSplat = false;
3419 if (VecNum != V) {
3420 isUnary = false;
3421 break;
3422 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00003423 }
3424 }
3425
3426 SDOperand N0 = N->getOperand(0);
3427 SDOperand N1 = N->getOperand(1);
3428 // Normalize unary shuffle so the RHS is undef.
3429 if (isUnary && VecNum == 1)
3430 std::swap(N0, N1);
3431
Evan Cheng917ec982006-07-21 08:25:53 +00003432 // If it is a splat, check if the argument vector is a build_vector with
3433 // all scalar elements the same.
3434 if (isSplat) {
3435 SDNode *V = N0.Val;
3436 if (V->getOpcode() == ISD::BIT_CONVERT)
3437 V = V->getOperand(0).Val;
3438 if (V->getOpcode() == ISD::BUILD_VECTOR) {
3439 unsigned NumElems = V->getNumOperands()-2;
3440 if (NumElems > BaseIdx) {
3441 SDOperand Base;
3442 bool AllSame = true;
3443 for (unsigned i = 0; i != NumElems; ++i) {
3444 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
3445 Base = V->getOperand(i);
3446 break;
3447 }
3448 }
3449 // Splat of <u, u, u, u>, return <u, u, u, u>
3450 if (!Base.Val)
3451 return N0;
3452 for (unsigned i = 0; i != NumElems; ++i) {
3453 if (V->getOperand(i).getOpcode() != ISD::UNDEF &&
3454 V->getOperand(i) != Base) {
3455 AllSame = false;
3456 break;
3457 }
3458 }
3459 // Splat of <x, x, x, x>, return <x, x, x, x>
3460 if (AllSame)
3461 return N0;
3462 }
3463 }
3464 }
3465
Evan Chenge7bec0d2006-07-20 22:44:41 +00003466 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
3467 // into an undef.
3468 if (isUnary || N0 == N1) {
3469 if (N0.getOpcode() == ISD::UNDEF)
Evan Chengc04766a2006-04-06 23:20:43 +00003470 return DAG.getNode(ISD::UNDEF, N->getValueType(0));
Chris Lattner66445d32006-03-28 22:11:53 +00003471 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
3472 // first operand.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003473 SmallVector<SDOperand, 8> MappedOps;
Chris Lattner66445d32006-03-28 22:11:53 +00003474 for (unsigned i = 0, e = ShufMask.getNumOperands(); i != e; ++i) {
Evan Chengc04766a2006-04-06 23:20:43 +00003475 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
3476 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) {
3477 MappedOps.push_back(ShufMask.getOperand(i));
3478 } else {
Chris Lattner66445d32006-03-28 22:11:53 +00003479 unsigned NewIdx =
3480 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
3481 MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32));
Chris Lattner66445d32006-03-28 22:11:53 +00003482 }
3483 }
3484 ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMask.getValueType(),
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003485 &MappedOps[0], MappedOps.size());
Chris Lattner3e104b12006-04-08 04:15:24 +00003486 AddToWorkList(ShufMask.Val);
Chris Lattner66445d32006-03-28 22:11:53 +00003487 return DAG.getNode(ISD::VECTOR_SHUFFLE, N->getValueType(0),
Evan Chenge7bec0d2006-07-20 22:44:41 +00003488 N0,
Chris Lattner66445d32006-03-28 22:11:53 +00003489 DAG.getNode(ISD::UNDEF, N->getValueType(0)),
3490 ShufMask);
3491 }
3492
3493 return SDOperand();
3494}
3495
Chris Lattnerf1d0c622006-03-31 22:16:43 +00003496SDOperand DAGCombiner::visitVVECTOR_SHUFFLE(SDNode *N) {
3497 SDOperand ShufMask = N->getOperand(2);
3498 unsigned NumElts = ShufMask.getNumOperands()-2;
3499
3500 // If the shuffle mask is an identity operation on the LHS, return the LHS.
3501 bool isIdentity = true;
3502 for (unsigned i = 0; i != NumElts; ++i) {
3503 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
3504 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
3505 isIdentity = false;
3506 break;
3507 }
3508 }
3509 if (isIdentity) return N->getOperand(0);
3510
3511 // If the shuffle mask is an identity operation on the RHS, return the RHS.
3512 isIdentity = true;
3513 for (unsigned i = 0; i != NumElts; ++i) {
3514 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
3515 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
3516 isIdentity = false;
3517 break;
3518 }
3519 }
3520 if (isIdentity) return N->getOperand(1);
3521
Evan Chenge7bec0d2006-07-20 22:44:41 +00003522 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
3523 // needed at all.
3524 bool isUnary = true;
Evan Cheng917ec982006-07-21 08:25:53 +00003525 bool isSplat = true;
Evan Chenge7bec0d2006-07-20 22:44:41 +00003526 int VecNum = -1;
Reid Spencer9160a6a2006-07-25 20:44:41 +00003527 unsigned BaseIdx = 0;
Evan Chenge7bec0d2006-07-20 22:44:41 +00003528 for (unsigned i = 0; i != NumElts; ++i)
3529 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
3530 unsigned Idx = cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue();
3531 int V = (Idx < NumElts) ? 0 : 1;
Evan Cheng917ec982006-07-21 08:25:53 +00003532 if (VecNum == -1) {
Evan Chenge7bec0d2006-07-20 22:44:41 +00003533 VecNum = V;
Evan Cheng917ec982006-07-21 08:25:53 +00003534 BaseIdx = Idx;
3535 } else {
3536 if (BaseIdx != Idx)
3537 isSplat = false;
3538 if (VecNum != V) {
3539 isUnary = false;
3540 break;
3541 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00003542 }
3543 }
3544
3545 SDOperand N0 = N->getOperand(0);
3546 SDOperand N1 = N->getOperand(1);
3547 // Normalize unary shuffle so the RHS is undef.
3548 if (isUnary && VecNum == 1)
3549 std::swap(N0, N1);
3550
Evan Cheng917ec982006-07-21 08:25:53 +00003551 // If it is a splat, check if the argument vector is a build_vector with
3552 // all scalar elements the same.
3553 if (isSplat) {
3554 SDNode *V = N0.Val;
Evan Cheng59569222006-10-16 22:49:37 +00003555
3556 // If this is a vbit convert that changes the element type of the vector but
3557 // not the number of vector elements, look through it. Be careful not to
3558 // look though conversions that change things like v4f32 to v2f64.
3559 if (V->getOpcode() == ISD::VBIT_CONVERT) {
3560 SDOperand ConvInput = V->getOperand(0);
Evan Cheng5d04a1a2006-10-17 17:06:35 +00003561 if (ConvInput.getValueType() == MVT::Vector &&
3562 NumElts ==
Evan Cheng59569222006-10-16 22:49:37 +00003563 ConvInput.getConstantOperandVal(ConvInput.getNumOperands()-2))
3564 V = ConvInput.Val;
3565 }
3566
Evan Cheng917ec982006-07-21 08:25:53 +00003567 if (V->getOpcode() == ISD::VBUILD_VECTOR) {
3568 unsigned NumElems = V->getNumOperands()-2;
3569 if (NumElems > BaseIdx) {
3570 SDOperand Base;
3571 bool AllSame = true;
3572 for (unsigned i = 0; i != NumElems; ++i) {
3573 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
3574 Base = V->getOperand(i);
3575 break;
3576 }
3577 }
3578 // Splat of <u, u, u, u>, return <u, u, u, u>
3579 if (!Base.Val)
3580 return N0;
3581 for (unsigned i = 0; i != NumElems; ++i) {
3582 if (V->getOperand(i).getOpcode() != ISD::UNDEF &&
3583 V->getOperand(i) != Base) {
3584 AllSame = false;
3585 break;
3586 }
3587 }
3588 // Splat of <x, x, x, x>, return <x, x, x, x>
3589 if (AllSame)
3590 return N0;
3591 }
3592 }
3593 }
3594
Evan Chenge7bec0d2006-07-20 22:44:41 +00003595 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
3596 // into an undef.
3597 if (isUnary || N0 == N1) {
Chris Lattner17614ea2006-04-08 05:34:25 +00003598 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
3599 // first operand.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003600 SmallVector<SDOperand, 8> MappedOps;
Chris Lattner17614ea2006-04-08 05:34:25 +00003601 for (unsigned i = 0; i != NumElts; ++i) {
3602 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
3603 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) {
3604 MappedOps.push_back(ShufMask.getOperand(i));
3605 } else {
3606 unsigned NewIdx =
3607 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
3608 MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32));
3609 }
3610 }
3611 // Add the type/#elts values.
3612 MappedOps.push_back(ShufMask.getOperand(NumElts));
3613 MappedOps.push_back(ShufMask.getOperand(NumElts+1));
3614
3615 ShufMask = DAG.getNode(ISD::VBUILD_VECTOR, ShufMask.getValueType(),
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003616 &MappedOps[0], MappedOps.size());
Chris Lattner17614ea2006-04-08 05:34:25 +00003617 AddToWorkList(ShufMask.Val);
3618
3619 // Build the undef vector.
3620 SDOperand UDVal = DAG.getNode(ISD::UNDEF, MappedOps[0].getValueType());
3621 for (unsigned i = 0; i != NumElts; ++i)
3622 MappedOps[i] = UDVal;
Evan Chenge7bec0d2006-07-20 22:44:41 +00003623 MappedOps[NumElts ] = *(N0.Val->op_end()-2);
3624 MappedOps[NumElts+1] = *(N0.Val->op_end()-1);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003625 UDVal = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
3626 &MappedOps[0], MappedOps.size());
Chris Lattner17614ea2006-04-08 05:34:25 +00003627
3628 return DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector,
Evan Chenge7bec0d2006-07-20 22:44:41 +00003629 N0, UDVal, ShufMask,
Chris Lattner17614ea2006-04-08 05:34:25 +00003630 MappedOps[NumElts], MappedOps[NumElts+1]);
3631 }
3632
Chris Lattnerf1d0c622006-03-31 22:16:43 +00003633 return SDOperand();
3634}
3635
Evan Cheng44f1f092006-04-20 08:56:16 +00003636/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
3637/// a VAND to a vector_shuffle with the destination vector and a zero vector.
3638/// e.g. VAND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
3639/// vector_shuffle V, Zero, <0, 4, 2, 4>
3640SDOperand DAGCombiner::XformToShuffleWithZero(SDNode *N) {
3641 SDOperand LHS = N->getOperand(0);
3642 SDOperand RHS = N->getOperand(1);
3643 if (N->getOpcode() == ISD::VAND) {
3644 SDOperand DstVecSize = *(LHS.Val->op_end()-2);
3645 SDOperand DstVecEVT = *(LHS.Val->op_end()-1);
3646 if (RHS.getOpcode() == ISD::VBIT_CONVERT)
3647 RHS = RHS.getOperand(0);
3648 if (RHS.getOpcode() == ISD::VBUILD_VECTOR) {
3649 std::vector<SDOperand> IdxOps;
3650 unsigned NumOps = RHS.getNumOperands();
3651 unsigned NumElts = NumOps-2;
3652 MVT::ValueType EVT = cast<VTSDNode>(RHS.getOperand(NumOps-1))->getVT();
3653 for (unsigned i = 0; i != NumElts; ++i) {
3654 SDOperand Elt = RHS.getOperand(i);
3655 if (!isa<ConstantSDNode>(Elt))
3656 return SDOperand();
3657 else if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
3658 IdxOps.push_back(DAG.getConstant(i, EVT));
3659 else if (cast<ConstantSDNode>(Elt)->isNullValue())
3660 IdxOps.push_back(DAG.getConstant(NumElts, EVT));
3661 else
3662 return SDOperand();
3663 }
3664
3665 // Let's see if the target supports this vector_shuffle.
3666 if (!TLI.isVectorClearMaskLegal(IdxOps, EVT, DAG))
3667 return SDOperand();
3668
3669 // Return the new VVECTOR_SHUFFLE node.
3670 SDOperand NumEltsNode = DAG.getConstant(NumElts, MVT::i32);
3671 SDOperand EVTNode = DAG.getValueType(EVT);
3672 std::vector<SDOperand> Ops;
Chris Lattner516b9622006-09-14 20:50:57 +00003673 LHS = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, LHS, NumEltsNode,
3674 EVTNode);
Evan Cheng44f1f092006-04-20 08:56:16 +00003675 Ops.push_back(LHS);
3676 AddToWorkList(LHS.Val);
3677 std::vector<SDOperand> ZeroOps(NumElts, DAG.getConstant(0, EVT));
3678 ZeroOps.push_back(NumEltsNode);
3679 ZeroOps.push_back(EVTNode);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003680 Ops.push_back(DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
3681 &ZeroOps[0], ZeroOps.size()));
Evan Cheng44f1f092006-04-20 08:56:16 +00003682 IdxOps.push_back(NumEltsNode);
3683 IdxOps.push_back(EVTNode);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003684 Ops.push_back(DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
3685 &IdxOps[0], IdxOps.size()));
Evan Cheng44f1f092006-04-20 08:56:16 +00003686 Ops.push_back(NumEltsNode);
3687 Ops.push_back(EVTNode);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003688 SDOperand Result = DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector,
3689 &Ops[0], Ops.size());
Evan Cheng44f1f092006-04-20 08:56:16 +00003690 if (NumEltsNode != DstVecSize || EVTNode != DstVecEVT) {
3691 Result = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Result,
3692 DstVecSize, DstVecEVT);
3693 }
3694 return Result;
3695 }
3696 }
3697 return SDOperand();
3698}
3699
Chris Lattneredab1b92006-04-02 03:25:57 +00003700/// visitVBinOp - Visit a binary vector operation, like VADD. IntOp indicates
3701/// the scalar operation of the vop if it is operating on an integer vector
3702/// (e.g. ADD) and FPOp indicates the FP version (e.g. FADD).
3703SDOperand DAGCombiner::visitVBinOp(SDNode *N, ISD::NodeType IntOp,
3704 ISD::NodeType FPOp) {
3705 MVT::ValueType EltType = cast<VTSDNode>(*(N->op_end()-1))->getVT();
3706 ISD::NodeType ScalarOp = MVT::isInteger(EltType) ? IntOp : FPOp;
3707 SDOperand LHS = N->getOperand(0);
3708 SDOperand RHS = N->getOperand(1);
Evan Cheng44f1f092006-04-20 08:56:16 +00003709 SDOperand Shuffle = XformToShuffleWithZero(N);
3710 if (Shuffle.Val) return Shuffle;
3711
Chris Lattneredab1b92006-04-02 03:25:57 +00003712 // If the LHS and RHS are VBUILD_VECTOR nodes, see if we can constant fold
3713 // this operation.
3714 if (LHS.getOpcode() == ISD::VBUILD_VECTOR &&
3715 RHS.getOpcode() == ISD::VBUILD_VECTOR) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003716 SmallVector<SDOperand, 8> Ops;
Chris Lattneredab1b92006-04-02 03:25:57 +00003717 for (unsigned i = 0, e = LHS.getNumOperands()-2; i != e; ++i) {
3718 SDOperand LHSOp = LHS.getOperand(i);
3719 SDOperand RHSOp = RHS.getOperand(i);
3720 // If these two elements can't be folded, bail out.
3721 if ((LHSOp.getOpcode() != ISD::UNDEF &&
3722 LHSOp.getOpcode() != ISD::Constant &&
3723 LHSOp.getOpcode() != ISD::ConstantFP) ||
3724 (RHSOp.getOpcode() != ISD::UNDEF &&
3725 RHSOp.getOpcode() != ISD::Constant &&
3726 RHSOp.getOpcode() != ISD::ConstantFP))
3727 break;
Evan Cheng7b336a82006-05-31 06:08:35 +00003728 // Can't fold divide by zero.
3729 if (N->getOpcode() == ISD::VSDIV || N->getOpcode() == ISD::VUDIV) {
3730 if ((RHSOp.getOpcode() == ISD::Constant &&
3731 cast<ConstantSDNode>(RHSOp.Val)->isNullValue()) ||
3732 (RHSOp.getOpcode() == ISD::ConstantFP &&
3733 !cast<ConstantFPSDNode>(RHSOp.Val)->getValue()))
3734 break;
3735 }
Chris Lattneredab1b92006-04-02 03:25:57 +00003736 Ops.push_back(DAG.getNode(ScalarOp, EltType, LHSOp, RHSOp));
Chris Lattner3e104b12006-04-08 04:15:24 +00003737 AddToWorkList(Ops.back().Val);
Chris Lattneredab1b92006-04-02 03:25:57 +00003738 assert((Ops.back().getOpcode() == ISD::UNDEF ||
3739 Ops.back().getOpcode() == ISD::Constant ||
3740 Ops.back().getOpcode() == ISD::ConstantFP) &&
3741 "Scalar binop didn't fold!");
3742 }
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00003743
3744 if (Ops.size() == LHS.getNumOperands()-2) {
3745 Ops.push_back(*(LHS.Val->op_end()-2));
3746 Ops.push_back(*(LHS.Val->op_end()-1));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003747 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00003748 }
Chris Lattneredab1b92006-04-02 03:25:57 +00003749 }
3750
3751 return SDOperand();
3752}
3753
Nate Begeman44728a72005-09-19 22:34:01 +00003754SDOperand DAGCombiner::SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2){
Nate Begemanf845b452005-10-08 00:29:44 +00003755 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
3756
3757 SDOperand SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
3758 cast<CondCodeSDNode>(N0.getOperand(2))->get());
3759 // If we got a simplified select_cc node back from SimplifySelectCC, then
3760 // break it down into a new SETCC node, and a new SELECT node, and then return
3761 // the SELECT node, since we were called with a SELECT node.
3762 if (SCC.Val) {
3763 // Check to see if we got a select_cc back (to turn into setcc/select).
3764 // Otherwise, just return whatever node we got back, like fabs.
3765 if (SCC.getOpcode() == ISD::SELECT_CC) {
3766 SDOperand SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
3767 SCC.getOperand(0), SCC.getOperand(1),
3768 SCC.getOperand(4));
Chris Lattner5750df92006-03-01 04:03:14 +00003769 AddToWorkList(SETCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003770 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
3771 SCC.getOperand(3), SETCC);
3772 }
3773 return SCC;
3774 }
Nate Begeman44728a72005-09-19 22:34:01 +00003775 return SDOperand();
3776}
3777
Chris Lattner40c62d52005-10-18 06:04:22 +00003778/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
3779/// are the two values being selected between, see if we can simplify the
Chris Lattner729c6d12006-05-27 00:43:02 +00003780/// select. Callers of this should assume that TheSelect is deleted if this
3781/// returns true. As such, they should return the appropriate thing (e.g. the
3782/// node) back to the top-level of the DAG combiner loop to avoid it being
3783/// looked at.
Chris Lattner40c62d52005-10-18 06:04:22 +00003784///
3785bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDOperand LHS,
3786 SDOperand RHS) {
3787
3788 // If this is a select from two identical things, try to pull the operation
3789 // through the select.
3790 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
Chris Lattner40c62d52005-10-18 06:04:22 +00003791 // If this is a load and the token chain is identical, replace the select
3792 // of two loads with a load through a select of the address to load from.
3793 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
3794 // constants have been dropped into the constant pool.
Evan Cheng466685d2006-10-09 20:57:25 +00003795 if (LHS.getOpcode() == ISD::LOAD &&
Chris Lattner40c62d52005-10-18 06:04:22 +00003796 // Token chains must be identical.
Evan Cheng466685d2006-10-09 20:57:25 +00003797 LHS.getOperand(0) == RHS.getOperand(0)) {
3798 LoadSDNode *LLD = cast<LoadSDNode>(LHS);
3799 LoadSDNode *RLD = cast<LoadSDNode>(RHS);
3800
3801 // If this is an EXTLOAD, the VT's must match.
Evan Cheng2e49f092006-10-11 07:10:22 +00003802 if (LLD->getLoadedVT() == RLD->getLoadedVT()) {
Evan Cheng466685d2006-10-09 20:57:25 +00003803 // FIXME: this conflates two src values, discarding one. This is not
3804 // the right thing to do, but nothing uses srcvalues now. When they do,
3805 // turn SrcValue into a list of locations.
3806 SDOperand Addr;
Chris Lattnerc4e664b2007-01-16 05:59:59 +00003807 if (TheSelect->getOpcode() == ISD::SELECT) {
3808 // Check that the condition doesn't reach either load. If so, folding
3809 // this will induce a cycle into the DAG.
3810 if (!LLD->isPredecessor(TheSelect->getOperand(0).Val) &&
3811 !RLD->isPredecessor(TheSelect->getOperand(0).Val)) {
3812 Addr = DAG.getNode(ISD::SELECT, LLD->getBasePtr().getValueType(),
3813 TheSelect->getOperand(0), LLD->getBasePtr(),
3814 RLD->getBasePtr());
3815 }
3816 } else {
3817 // Check that the condition doesn't reach either load. If so, folding
3818 // this will induce a cycle into the DAG.
3819 if (!LLD->isPredecessor(TheSelect->getOperand(0).Val) &&
3820 !RLD->isPredecessor(TheSelect->getOperand(0).Val) &&
3821 !LLD->isPredecessor(TheSelect->getOperand(1).Val) &&
3822 !RLD->isPredecessor(TheSelect->getOperand(1).Val)) {
3823 Addr = DAG.getNode(ISD::SELECT_CC, LLD->getBasePtr().getValueType(),
Evan Cheng466685d2006-10-09 20:57:25 +00003824 TheSelect->getOperand(0),
3825 TheSelect->getOperand(1),
3826 LLD->getBasePtr(), RLD->getBasePtr(),
3827 TheSelect->getOperand(4));
Chris Lattnerc4e664b2007-01-16 05:59:59 +00003828 }
Evan Cheng466685d2006-10-09 20:57:25 +00003829 }
Chris Lattnerc4e664b2007-01-16 05:59:59 +00003830
3831 if (Addr.Val) {
3832 SDOperand Load;
3833 if (LLD->getExtensionType() == ISD::NON_EXTLOAD)
3834 Load = DAG.getLoad(TheSelect->getValueType(0), LLD->getChain(),
3835 Addr,LLD->getSrcValue(),
3836 LLD->getSrcValueOffset());
3837 else {
3838 Load = DAG.getExtLoad(LLD->getExtensionType(),
3839 TheSelect->getValueType(0),
3840 LLD->getChain(), Addr, LLD->getSrcValue(),
3841 LLD->getSrcValueOffset(),
3842 LLD->getLoadedVT());
3843 }
3844 // Users of the select now use the result of the load.
3845 CombineTo(TheSelect, Load);
3846
3847 // Users of the old loads now use the new load's chain. We know the
3848 // old-load value is dead now.
3849 CombineTo(LHS.Val, Load.getValue(0), Load.getValue(1));
3850 CombineTo(RHS.Val, Load.getValue(0), Load.getValue(1));
3851 return true;
3852 }
Evan Chengc5484282006-10-04 00:56:09 +00003853 }
Chris Lattner40c62d52005-10-18 06:04:22 +00003854 }
3855 }
3856
3857 return false;
3858}
3859
Nate Begeman44728a72005-09-19 22:34:01 +00003860SDOperand DAGCombiner::SimplifySelectCC(SDOperand N0, SDOperand N1,
3861 SDOperand N2, SDOperand N3,
3862 ISD::CondCode CC) {
Nate Begemanf845b452005-10-08 00:29:44 +00003863
3864 MVT::ValueType VT = N2.getValueType();
Nate Begemanf845b452005-10-08 00:29:44 +00003865 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
3866 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
3867 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
3868
3869 // Determine if the condition we're dealing with is constant
3870 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
Chris Lattner30f73e72006-10-14 03:52:46 +00003871 if (SCC.Val) AddToWorkList(SCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003872 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
3873
3874 // fold select_cc true, x, y -> x
3875 if (SCCC && SCCC->getValue())
3876 return N2;
3877 // fold select_cc false, x, y -> y
3878 if (SCCC && SCCC->getValue() == 0)
3879 return N3;
3880
3881 // Check to see if we can simplify the select into an fabs node
3882 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
3883 // Allow either -0.0 or 0.0
3884 if (CFP->getValue() == 0.0) {
3885 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
3886 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
3887 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
3888 N2 == N3.getOperand(0))
3889 return DAG.getNode(ISD::FABS, VT, N0);
3890
3891 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
3892 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
3893 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
3894 N2.getOperand(0) == N3)
3895 return DAG.getNode(ISD::FABS, VT, N3);
3896 }
3897 }
3898
3899 // Check to see if we can perform the "gzip trick", transforming
3900 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
Chris Lattnere3152e52006-09-20 06:41:35 +00003901 if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT &&
Nate Begemanf845b452005-10-08 00:29:44 +00003902 MVT::isInteger(N0.getValueType()) &&
Chris Lattnere3152e52006-09-20 06:41:35 +00003903 MVT::isInteger(N2.getValueType()) &&
3904 (N1C->isNullValue() || // (a < 0) ? b : 0
3905 (N1C->getValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0
Nate Begemanf845b452005-10-08 00:29:44 +00003906 MVT::ValueType XType = N0.getValueType();
3907 MVT::ValueType AType = N2.getValueType();
3908 if (XType >= AType) {
3909 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00003910 // single-bit constant.
Nate Begemanf845b452005-10-08 00:29:44 +00003911 if (N2C && ((N2C->getValue() & (N2C->getValue()-1)) == 0)) {
3912 unsigned ShCtV = Log2_64(N2C->getValue());
3913 ShCtV = MVT::getSizeInBits(XType)-ShCtV-1;
3914 SDOperand ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
3915 SDOperand Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
Chris Lattner5750df92006-03-01 04:03:14 +00003916 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003917 if (XType > AType) {
3918 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00003919 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003920 }
3921 return DAG.getNode(ISD::AND, AType, Shift, N2);
3922 }
3923 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
3924 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3925 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00003926 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003927 if (XType > AType) {
3928 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00003929 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00003930 }
3931 return DAG.getNode(ISD::AND, AType, Shift, N2);
3932 }
3933 }
Nate Begeman07ed4172005-10-10 21:26:48 +00003934
3935 // fold select C, 16, 0 -> shl C, 4
3936 if (N2C && N3C && N3C->isNullValue() && isPowerOf2_64(N2C->getValue()) &&
3937 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) {
3938 // Get a SetCC of the condition
3939 // FIXME: Should probably make sure that setcc is legal if we ever have a
3940 // target where it isn't.
Nate Begemanb0d04a72006-02-18 02:40:58 +00003941 SDOperand Temp, SCC;
Nate Begeman07ed4172005-10-10 21:26:48 +00003942 // cast from setcc result type to select result type
Nate Begemanb0d04a72006-02-18 02:40:58 +00003943 if (AfterLegalize) {
3944 SCC = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
Chris Lattner555d8d62006-12-07 22:36:47 +00003945 if (N2.getValueType() < SCC.getValueType())
3946 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
3947 else
3948 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00003949 } else {
3950 SCC = DAG.getSetCC(MVT::i1, N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00003951 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00003952 }
Chris Lattner5750df92006-03-01 04:03:14 +00003953 AddToWorkList(SCC.Val);
3954 AddToWorkList(Temp.Val);
Nate Begeman07ed4172005-10-10 21:26:48 +00003955 // shl setcc result by log2 n2c
3956 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
3957 DAG.getConstant(Log2_64(N2C->getValue()),
3958 TLI.getShiftAmountTy()));
3959 }
3960
Nate Begemanf845b452005-10-08 00:29:44 +00003961 // Check to see if this is the equivalent of setcc
3962 // FIXME: Turn all of these into setcc if setcc if setcc is legal
3963 // otherwise, go ahead with the folds.
3964 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getValue() == 1ULL)) {
3965 MVT::ValueType XType = N0.getValueType();
3966 if (TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultTy())) {
3967 SDOperand Res = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
3968 if (Res.getValueType() != VT)
3969 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
3970 return Res;
3971 }
3972
3973 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
3974 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
3975 TLI.isOperationLegal(ISD::CTLZ, XType)) {
3976 SDOperand Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
3977 return DAG.getNode(ISD::SRL, XType, Ctlz,
3978 DAG.getConstant(Log2_32(MVT::getSizeInBits(XType)),
3979 TLI.getShiftAmountTy()));
3980 }
3981 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
3982 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
3983 SDOperand NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
3984 N0);
3985 SDOperand NotN0 = DAG.getNode(ISD::XOR, XType, N0,
3986 DAG.getConstant(~0ULL, XType));
3987 return DAG.getNode(ISD::SRL, XType,
3988 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
3989 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3990 TLI.getShiftAmountTy()));
3991 }
3992 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
3993 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
3994 SDOperand Sign = DAG.getNode(ISD::SRL, XType, N0,
3995 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3996 TLI.getShiftAmountTy()));
3997 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
3998 }
3999 }
4000
4001 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
4002 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
4003 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
4004 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1)) {
4005 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N2.getOperand(0))) {
4006 MVT::ValueType XType = N0.getValueType();
4007 if (SubC->isNullValue() && MVT::isInteger(XType)) {
4008 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
4009 DAG.getConstant(MVT::getSizeInBits(XType)-1,
4010 TLI.getShiftAmountTy()));
4011 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00004012 AddToWorkList(Shift.Val);
4013 AddToWorkList(Add.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004014 return DAG.getNode(ISD::XOR, XType, Add, Shift);
4015 }
4016 }
4017 }
4018
Nate Begeman44728a72005-09-19 22:34:01 +00004019 return SDOperand();
4020}
4021
Evan Chengfa1eb272007-02-08 22:13:59 +00004022/// SimplifySetCC - This is a stub for TargetLowering::SimplifySetCC.
Nate Begeman452d7be2005-09-16 00:54:12 +00004023SDOperand DAGCombiner::SimplifySetCC(MVT::ValueType VT, SDOperand N0,
Nate Begemane17daeb2005-10-05 21:43:42 +00004024 SDOperand N1, ISD::CondCode Cond,
4025 bool foldBooleans) {
Evan Chengfa1eb272007-02-08 22:13:59 +00004026 TargetLowering::DAGCombinerInfo
4027 DagCombineInfo(DAG, !AfterLegalize, false, this);
4028 return TLI.SimplifySetCC(VT, N0, N1, Cond, foldBooleans, DagCombineInfo);
Nate Begeman452d7be2005-09-16 00:54:12 +00004029}
4030
Nate Begeman69575232005-10-20 02:15:44 +00004031/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
4032/// return a DAG expression to select that will generate the same value by
4033/// multiplying by a magic number. See:
4034/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
4035SDOperand DAGCombiner::BuildSDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00004036 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00004037 SDOperand S = TLI.BuildSDIV(N, DAG, &Built);
4038
Andrew Lenharth232c9102006-06-12 16:07:18 +00004039 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00004040 ii != ee; ++ii)
4041 AddToWorkList(*ii);
4042 return S;
Nate Begeman69575232005-10-20 02:15:44 +00004043}
4044
4045/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
4046/// return a DAG expression to select that will generate the same value by
4047/// multiplying by a magic number. See:
4048/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
4049SDOperand DAGCombiner::BuildUDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00004050 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00004051 SDOperand S = TLI.BuildUDIV(N, DAG, &Built);
Nate Begeman69575232005-10-20 02:15:44 +00004052
Andrew Lenharth232c9102006-06-12 16:07:18 +00004053 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00004054 ii != ee; ++ii)
4055 AddToWorkList(*ii);
4056 return S;
Nate Begeman69575232005-10-20 02:15:44 +00004057}
4058
Jim Laskey71382342006-10-07 23:37:56 +00004059/// FindBaseOffset - Return true if base is known not to alias with anything
4060/// but itself. Provides base object and offset as results.
4061static bool FindBaseOffset(SDOperand Ptr, SDOperand &Base, int64_t &Offset) {
4062 // Assume it is a primitive operation.
4063 Base = Ptr; Offset = 0;
4064
4065 // If it's an adding a simple constant then integrate the offset.
4066 if (Base.getOpcode() == ISD::ADD) {
4067 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Base.getOperand(1))) {
4068 Base = Base.getOperand(0);
4069 Offset += C->getValue();
4070 }
4071 }
4072
4073 // If it's any of the following then it can't alias with anything but itself.
4074 return isa<FrameIndexSDNode>(Base) ||
4075 isa<ConstantPoolSDNode>(Base) ||
4076 isa<GlobalAddressSDNode>(Base);
4077}
4078
4079/// isAlias - Return true if there is any possibility that the two addresses
4080/// overlap.
Jim Laskey096c22e2006-10-18 12:29:57 +00004081bool DAGCombiner::isAlias(SDOperand Ptr1, int64_t Size1,
4082 const Value *SrcValue1, int SrcValueOffset1,
4083 SDOperand Ptr2, int64_t Size2,
4084 const Value *SrcValue2, int SrcValueOffset2)
4085{
Jim Laskey71382342006-10-07 23:37:56 +00004086 // If they are the same then they must be aliases.
4087 if (Ptr1 == Ptr2) return true;
4088
4089 // Gather base node and offset information.
4090 SDOperand Base1, Base2;
4091 int64_t Offset1, Offset2;
4092 bool KnownBase1 = FindBaseOffset(Ptr1, Base1, Offset1);
4093 bool KnownBase2 = FindBaseOffset(Ptr2, Base2, Offset2);
4094
4095 // If they have a same base address then...
4096 if (Base1 == Base2) {
4097 // Check to see if the addresses overlap.
4098 return!((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
4099 }
4100
Jim Laskey096c22e2006-10-18 12:29:57 +00004101 // If we know both bases then they can't alias.
4102 if (KnownBase1 && KnownBase2) return false;
4103
Jim Laskey07a27092006-10-18 19:08:31 +00004104 if (CombinerGlobalAA) {
4105 // Use alias analysis information.
4106 int Overlap1 = Size1 + SrcValueOffset1 + Offset1;
4107 int Overlap2 = Size2 + SrcValueOffset2 + Offset2;
4108 AliasAnalysis::AliasResult AAResult =
Jim Laskey096c22e2006-10-18 12:29:57 +00004109 AA.alias(SrcValue1, Overlap1, SrcValue2, Overlap2);
Jim Laskey07a27092006-10-18 19:08:31 +00004110 if (AAResult == AliasAnalysis::NoAlias)
4111 return false;
4112 }
Jim Laskey096c22e2006-10-18 12:29:57 +00004113
4114 // Otherwise we have to assume they alias.
4115 return true;
Jim Laskey71382342006-10-07 23:37:56 +00004116}
4117
4118/// FindAliasInfo - Extracts the relevant alias information from the memory
4119/// node. Returns true if the operand was a load.
Jim Laskey7ca56af2006-10-11 13:47:09 +00004120bool DAGCombiner::FindAliasInfo(SDNode *N,
Jim Laskey096c22e2006-10-18 12:29:57 +00004121 SDOperand &Ptr, int64_t &Size,
4122 const Value *&SrcValue, int &SrcValueOffset) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00004123 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
4124 Ptr = LD->getBasePtr();
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004125 Size = MVT::getSizeInBits(LD->getLoadedVT()) >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00004126 SrcValue = LD->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00004127 SrcValueOffset = LD->getSrcValueOffset();
Jim Laskey71382342006-10-07 23:37:56 +00004128 return true;
Jim Laskey7ca56af2006-10-11 13:47:09 +00004129 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00004130 Ptr = ST->getBasePtr();
Evan Cheng8b2794a2006-10-13 21:14:26 +00004131 Size = MVT::getSizeInBits(ST->getStoredVT()) >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00004132 SrcValue = ST->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00004133 SrcValueOffset = ST->getSrcValueOffset();
Jim Laskey7ca56af2006-10-11 13:47:09 +00004134 } else {
Jim Laskey71382342006-10-07 23:37:56 +00004135 assert(0 && "FindAliasInfo expected a memory operand");
Jim Laskey71382342006-10-07 23:37:56 +00004136 }
4137
4138 return false;
4139}
4140
Jim Laskey6ff23e52006-10-04 16:53:27 +00004141/// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
4142/// looking for aliasing nodes and adding them to the Aliases vector.
Jim Laskeybc588b82006-10-05 15:07:25 +00004143void DAGCombiner::GatherAllAliases(SDNode *N, SDOperand OriginalChain,
Jim Laskey6ff23e52006-10-04 16:53:27 +00004144 SmallVector<SDOperand, 8> &Aliases) {
Jim Laskeybc588b82006-10-05 15:07:25 +00004145 SmallVector<SDOperand, 8> Chains; // List of chains to visit.
Jim Laskey6ff23e52006-10-04 16:53:27 +00004146 std::set<SDNode *> Visited; // Visited node set.
4147
Jim Laskey279f0532006-09-25 16:29:54 +00004148 // Get alias information for node.
4149 SDOperand Ptr;
4150 int64_t Size;
Jim Laskey7ca56af2006-10-11 13:47:09 +00004151 const Value *SrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00004152 int SrcValueOffset;
4153 bool IsLoad = FindAliasInfo(N, Ptr, Size, SrcValue, SrcValueOffset);
Jim Laskey279f0532006-09-25 16:29:54 +00004154
Jim Laskey6ff23e52006-10-04 16:53:27 +00004155 // Starting off.
Jim Laskeybc588b82006-10-05 15:07:25 +00004156 Chains.push_back(OriginalChain);
Jim Laskey6ff23e52006-10-04 16:53:27 +00004157
Jim Laskeybc588b82006-10-05 15:07:25 +00004158 // Look at each chain and determine if it is an alias. If so, add it to the
4159 // aliases list. If not, then continue up the chain looking for the next
4160 // candidate.
4161 while (!Chains.empty()) {
4162 SDOperand Chain = Chains.back();
4163 Chains.pop_back();
Jim Laskey6ff23e52006-10-04 16:53:27 +00004164
Jim Laskeybc588b82006-10-05 15:07:25 +00004165 // Don't bother if we've been before.
4166 if (Visited.find(Chain.Val) != Visited.end()) continue;
4167 Visited.insert(Chain.Val);
4168
4169 switch (Chain.getOpcode()) {
4170 case ISD::EntryToken:
4171 // Entry token is ideal chain operand, but handled in FindBetterChain.
4172 break;
Jim Laskey6ff23e52006-10-04 16:53:27 +00004173
Jim Laskeybc588b82006-10-05 15:07:25 +00004174 case ISD::LOAD:
4175 case ISD::STORE: {
4176 // Get alias information for Chain.
4177 SDOperand OpPtr;
4178 int64_t OpSize;
Jim Laskey7ca56af2006-10-11 13:47:09 +00004179 const Value *OpSrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00004180 int OpSrcValueOffset;
4181 bool IsOpLoad = FindAliasInfo(Chain.Val, OpPtr, OpSize,
4182 OpSrcValue, OpSrcValueOffset);
Jim Laskeybc588b82006-10-05 15:07:25 +00004183
4184 // If chain is alias then stop here.
4185 if (!(IsLoad && IsOpLoad) &&
Jim Laskey096c22e2006-10-18 12:29:57 +00004186 isAlias(Ptr, Size, SrcValue, SrcValueOffset,
4187 OpPtr, OpSize, OpSrcValue, OpSrcValueOffset)) {
Jim Laskeybc588b82006-10-05 15:07:25 +00004188 Aliases.push_back(Chain);
4189 } else {
4190 // Look further up the chain.
4191 Chains.push_back(Chain.getOperand(0));
4192 // Clean up old chain.
4193 AddToWorkList(Chain.Val);
Jim Laskey279f0532006-09-25 16:29:54 +00004194 }
Jim Laskeybc588b82006-10-05 15:07:25 +00004195 break;
4196 }
4197
4198 case ISD::TokenFactor:
4199 // We have to check each of the operands of the token factor, so we queue
4200 // then up. Adding the operands to the queue (stack) in reverse order
4201 // maintains the original order and increases the likelihood that getNode
4202 // will find a matching token factor (CSE.)
4203 for (unsigned n = Chain.getNumOperands(); n;)
4204 Chains.push_back(Chain.getOperand(--n));
4205 // Eliminate the token factor if we can.
4206 AddToWorkList(Chain.Val);
4207 break;
4208
4209 default:
4210 // For all other instructions we will just have to take what we can get.
4211 Aliases.push_back(Chain);
4212 break;
Jim Laskey279f0532006-09-25 16:29:54 +00004213 }
4214 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00004215}
4216
4217/// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, looking
4218/// for a better chain (aliasing node.)
4219SDOperand DAGCombiner::FindBetterChain(SDNode *N, SDOperand OldChain) {
4220 SmallVector<SDOperand, 8> Aliases; // Ops for replacing token factor.
Jim Laskey279f0532006-09-25 16:29:54 +00004221
Jim Laskey6ff23e52006-10-04 16:53:27 +00004222 // Accumulate all the aliases to this node.
4223 GatherAllAliases(N, OldChain, Aliases);
4224
4225 if (Aliases.size() == 0) {
4226 // If no operands then chain to entry token.
4227 return DAG.getEntryNode();
4228 } else if (Aliases.size() == 1) {
4229 // If a single operand then chain to it. We don't need to revisit it.
4230 return Aliases[0];
4231 }
4232
4233 // Construct a custom tailored token factor.
4234 SDOperand NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
4235 &Aliases[0], Aliases.size());
4236
4237 // Make sure the old chain gets cleaned up.
4238 if (NewChain != OldChain) AddToWorkList(OldChain.Val);
4239
4240 return NewChain;
Jim Laskey279f0532006-09-25 16:29:54 +00004241}
4242
Nate Begeman1d4d4142005-09-01 00:19:25 +00004243// SelectionDAG::Combine - This is the entry point for the file.
4244//
Jim Laskeyc7c3f112006-10-16 20:52:31 +00004245void SelectionDAG::Combine(bool RunningAfterLegalize, AliasAnalysis &AA) {
Chris Lattner938ab022007-01-16 04:55:25 +00004246 if (!RunningAfterLegalize && ViewDAGCombine1)
4247 viewGraph();
4248 if (RunningAfterLegalize && ViewDAGCombine2)
4249 viewGraph();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004250 /// run - This is the main entry point to this class.
4251 ///
Jim Laskeyc7c3f112006-10-16 20:52:31 +00004252 DAGCombiner(*this, AA).Run(RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004253}