blob: 32ab5d323eba2cbf61ef8ca5ecfa7473fffd0d6c [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"
Nate Begeman1d4d4142005-09-01 00:19:25 +000032#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattnerc76d4412007-05-16 06:37:59 +000033#include "llvm/Analysis/AliasAnalysis.h"
Evan Cheng59d5b682007-05-07 21:27:48 +000034#include "llvm/Target/TargetData.h"
Nate Begeman1d4d4142005-09-01 00:19:25 +000035#include "llvm/Target/TargetLowering.h"
Evan Cheng59d5b682007-05-07 21:27:48 +000036#include "llvm/Target/TargetMachine.h"
Chris Lattnerddae4bd2007-01-08 23:04:05 +000037#include "llvm/Target/TargetOptions.h"
Chris Lattnerc76d4412007-05-16 06:37:59 +000038#include "llvm/ADT/SmallPtrSet.h"
39#include "llvm/ADT/Statistic.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000040#include "llvm/Support/Compiler.h"
Jim Laskeyd1aed7a2006-09-21 16:28:59 +000041#include "llvm/Support/CommandLine.h"
Chris Lattnerc76d4412007-05-16 06:37:59 +000042#include "llvm/Support/Debug.h"
43#include "llvm/Support/MathExtras.h"
Chris Lattnera500fc62005-09-09 23:53:39 +000044#include <algorithm>
Nate Begeman1d4d4142005-09-01 00:19:25 +000045using namespace llvm;
46
Chris Lattnercd3245a2006-12-19 22:41:21 +000047STATISTIC(NodesCombined , "Number of dag nodes combined");
48STATISTIC(PreIndexedNodes , "Number of pre-indexed nodes created");
49STATISTIC(PostIndexedNodes, "Number of post-indexed nodes created");
50
Nate Begeman1d4d4142005-09-01 00:19:25 +000051namespace {
Chris Lattner938ab022007-01-16 04:55:25 +000052#ifndef NDEBUG
53 static cl::opt<bool>
54 ViewDAGCombine1("view-dag-combine1-dags", cl::Hidden,
55 cl::desc("Pop up a window to show dags before the first "
56 "dag combine pass"));
57 static cl::opt<bool>
58 ViewDAGCombine2("view-dag-combine2-dags", cl::Hidden,
59 cl::desc("Pop up a window to show dags before the second "
60 "dag combine pass"));
61#else
62 static const bool ViewDAGCombine1 = false;
63 static const bool ViewDAGCombine2 = false;
64#endif
65
Jim Laskey71382342006-10-07 23:37:56 +000066 static cl::opt<bool>
67 CombinerAA("combiner-alias-analysis", cl::Hidden,
Jim Laskey26f7fa72006-10-17 19:33:52 +000068 cl::desc("Turn on alias analysis during testing"));
Jim Laskey3ad175b2006-10-12 15:22:24 +000069
Jim Laskey07a27092006-10-18 19:08:31 +000070 static cl::opt<bool>
71 CombinerGlobalAA("combiner-global-alias-analysis", cl::Hidden,
72 cl::desc("Include global information in alias analysis"));
73
Jim Laskeybc588b82006-10-05 15:07:25 +000074//------------------------------ DAGCombiner ---------------------------------//
75
Jim Laskey71382342006-10-07 23:37:56 +000076 class VISIBILITY_HIDDEN DAGCombiner {
Nate Begeman1d4d4142005-09-01 00:19:25 +000077 SelectionDAG &DAG;
78 TargetLowering &TLI;
Nate Begeman4ebd8052005-09-01 23:24:04 +000079 bool AfterLegalize;
Nate Begeman1d4d4142005-09-01 00:19:25 +000080
81 // Worklist of all of the nodes that need to be simplified.
82 std::vector<SDNode*> WorkList;
83
Jim Laskeyc7c3f112006-10-16 20:52:31 +000084 // AA - Used for DAG load/store alias analysis.
85 AliasAnalysis &AA;
86
Nate Begeman1d4d4142005-09-01 00:19:25 +000087 /// AddUsersToWorkList - When an instruction is simplified, add all users of
88 /// the instruction to the work lists because they might get more simplified
89 /// now.
90 ///
91 void AddUsersToWorkList(SDNode *N) {
92 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
Nate Begeman4ebd8052005-09-01 23:24:04 +000093 UI != UE; ++UI)
Jim Laskey6ff23e52006-10-04 16:53:27 +000094 AddToWorkList(*UI);
Nate Begeman1d4d4142005-09-01 00:19:25 +000095 }
96
97 /// removeFromWorkList - remove all instances of N from the worklist.
Chris Lattner5750df92006-03-01 04:03:14 +000098 ///
Nate Begeman1d4d4142005-09-01 00:19:25 +000099 void removeFromWorkList(SDNode *N) {
100 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), N),
101 WorkList.end());
102 }
103
Chris Lattner24664722006-03-01 04:53:38 +0000104 public:
Jim Laskey6ff23e52006-10-04 16:53:27 +0000105 /// AddToWorkList - Add to the work list making sure it's instance is at the
106 /// the back (next to be processed.)
Chris Lattner5750df92006-03-01 04:03:14 +0000107 void AddToWorkList(SDNode *N) {
Jim Laskey6ff23e52006-10-04 16:53:27 +0000108 removeFromWorkList(N);
Chris Lattner5750df92006-03-01 04:03:14 +0000109 WorkList.push_back(N);
110 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000111
Jim Laskey274062c2006-10-13 23:32:28 +0000112 SDOperand CombineTo(SDNode *N, const SDOperand *To, unsigned NumTo,
113 bool AddTo = true) {
Chris Lattner3577e382006-08-11 17:56:38 +0000114 assert(N->getNumValues() == NumTo && "Broken CombineTo call!");
Chris Lattner87514ca2005-10-10 22:31:19 +0000115 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +0000116 DOUT << "\nReplacing.1 "; DEBUG(N->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +0000117 DOUT << "\nWith: "; DEBUG(To[0].Val->dump(&DAG));
118 DOUT << " and " << NumTo-1 << " other values\n";
Chris Lattner01a22022005-10-10 22:04:48 +0000119 std::vector<SDNode*> NowDead;
Chris Lattner3577e382006-08-11 17:56:38 +0000120 DAG.ReplaceAllUsesWith(N, To, &NowDead);
Chris Lattner01a22022005-10-10 22:04:48 +0000121
Jim Laskey274062c2006-10-13 23:32:28 +0000122 if (AddTo) {
123 // Push the new nodes and any users onto the worklist
124 for (unsigned i = 0, e = NumTo; i != e; ++i) {
125 AddToWorkList(To[i].Val);
126 AddUsersToWorkList(To[i].Val);
127 }
Chris Lattner01a22022005-10-10 22:04:48 +0000128 }
129
Jim Laskey6ff23e52006-10-04 16:53:27 +0000130 // Nodes can be reintroduced into the worklist. Make sure we do not
131 // process a node that has been replaced.
Chris Lattner01a22022005-10-10 22:04:48 +0000132 removeFromWorkList(N);
133 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
134 removeFromWorkList(NowDead[i]);
135
136 // Finally, since the node is now dead, remove it from the graph.
137 DAG.DeleteNode(N);
138 return SDOperand(N, 0);
139 }
Nate Begeman368e18d2006-02-16 21:11:51 +0000140
Jim Laskey274062c2006-10-13 23:32:28 +0000141 SDOperand CombineTo(SDNode *N, SDOperand Res, bool AddTo = true) {
142 return CombineTo(N, &Res, 1, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000143 }
144
Jim Laskey274062c2006-10-13 23:32:28 +0000145 SDOperand CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1,
146 bool AddTo = true) {
Chris Lattner3577e382006-08-11 17:56:38 +0000147 SDOperand To[] = { Res0, Res1 };
Jim Laskey274062c2006-10-13 23:32:28 +0000148 return CombineTo(N, To, 2, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000149 }
150 private:
151
Chris Lattner012f2412006-02-17 21:58:01 +0000152 /// SimplifyDemandedBits - Check the specified integer node value to see if
Chris Lattnerb2742f42006-03-01 19:55:35 +0000153 /// it can be simplified or if things it uses can be simplified by bit
Chris Lattner012f2412006-02-17 21:58:01 +0000154 /// propagation. If so, return true.
155 bool SimplifyDemandedBits(SDOperand Op) {
Nate Begeman368e18d2006-02-16 21:11:51 +0000156 TargetLowering::TargetLoweringOpt TLO(DAG);
157 uint64_t KnownZero, KnownOne;
Chris Lattner012f2412006-02-17 21:58:01 +0000158 uint64_t Demanded = MVT::getIntVTBitMask(Op.getValueType());
159 if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO))
160 return false;
161
162 // Revisit the node.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000163 AddToWorkList(Op.Val);
Chris Lattner012f2412006-02-17 21:58:01 +0000164
165 // Replace the old value with the new one.
166 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +0000167 DOUT << "\nReplacing.2 "; DEBUG(TLO.Old.Val->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +0000168 DOUT << "\nWith: "; DEBUG(TLO.New.Val->dump(&DAG));
169 DOUT << '\n';
Chris Lattner012f2412006-02-17 21:58:01 +0000170
171 std::vector<SDNode*> NowDead;
172 DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New, NowDead);
173
Chris Lattner7d20d392006-02-20 06:51:04 +0000174 // Push the new node and any (possibly new) users onto the worklist.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000175 AddToWorkList(TLO.New.Val);
Chris Lattner012f2412006-02-17 21:58:01 +0000176 AddUsersToWorkList(TLO.New.Val);
177
178 // Nodes can end up on the worklist more than once. Make sure we do
179 // not process a node that has been replaced.
Chris Lattner012f2412006-02-17 21:58:01 +0000180 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
181 removeFromWorkList(NowDead[i]);
182
Chris Lattner7d20d392006-02-20 06:51:04 +0000183 // Finally, if the node is now dead, remove it from the graph. The node
184 // may not be dead if the replacement process recursively simplified to
185 // something else needing this node.
186 if (TLO.Old.Val->use_empty()) {
187 removeFromWorkList(TLO.Old.Val);
Chris Lattnerec06e9a2007-04-18 03:05:22 +0000188
189 // If the operands of this node are only used by the node, they will now
190 // be dead. Make sure to visit them first to delete dead nodes early.
191 for (unsigned i = 0, e = TLO.Old.Val->getNumOperands(); i != e; ++i)
192 if (TLO.Old.Val->getOperand(i).Val->hasOneUse())
193 AddToWorkList(TLO.Old.Val->getOperand(i).Val);
194
Chris Lattner7d20d392006-02-20 06:51:04 +0000195 DAG.DeleteNode(TLO.Old.Val);
196 }
Chris Lattner012f2412006-02-17 21:58:01 +0000197 return true;
Nate Begeman368e18d2006-02-16 21:11:51 +0000198 }
Chris Lattner87514ca2005-10-10 22:31:19 +0000199
Chris Lattner448f2192006-11-11 00:39:41 +0000200 bool CombineToPreIndexedLoadStore(SDNode *N);
201 bool CombineToPostIndexedLoadStore(SDNode *N);
202
203
Nate Begeman1d4d4142005-09-01 00:19:25 +0000204 /// visit - call the node-specific routine that knows how to fold each
205 /// particular type of node.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000206 SDOperand visit(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000207
208 // Visitation implementation - Implement dag node combining for different
209 // node types. The semantics are as follows:
210 // Return Value:
Nate Begeman2300f552005-09-07 00:15:36 +0000211 // SDOperand.Val == 0 - No change was made
Chris Lattner01a22022005-10-10 22:04:48 +0000212 // SDOperand.Val == N - N was replaced, is dead, and is already handled.
Nate Begeman2300f552005-09-07 00:15:36 +0000213 // otherwise - N should be replaced by the returned Operand.
Nate Begeman1d4d4142005-09-01 00:19:25 +0000214 //
Nate Begeman83e75ec2005-09-06 04:43:02 +0000215 SDOperand visitTokenFactor(SDNode *N);
216 SDOperand visitADD(SDNode *N);
217 SDOperand visitSUB(SDNode *N);
Chris Lattner91153682007-03-04 20:03:15 +0000218 SDOperand visitADDC(SDNode *N);
219 SDOperand visitADDE(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000220 SDOperand visitMUL(SDNode *N);
221 SDOperand visitSDIV(SDNode *N);
222 SDOperand visitUDIV(SDNode *N);
223 SDOperand visitSREM(SDNode *N);
224 SDOperand visitUREM(SDNode *N);
225 SDOperand visitMULHU(SDNode *N);
226 SDOperand visitMULHS(SDNode *N);
227 SDOperand visitAND(SDNode *N);
228 SDOperand visitOR(SDNode *N);
229 SDOperand visitXOR(SDNode *N);
Chris Lattneredab1b92006-04-02 03:25:57 +0000230 SDOperand visitVBinOp(SDNode *N, ISD::NodeType IntOp, ISD::NodeType FPOp);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000231 SDOperand visitSHL(SDNode *N);
232 SDOperand visitSRA(SDNode *N);
233 SDOperand visitSRL(SDNode *N);
234 SDOperand visitCTLZ(SDNode *N);
235 SDOperand visitCTTZ(SDNode *N);
236 SDOperand visitCTPOP(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000237 SDOperand visitSELECT(SDNode *N);
238 SDOperand visitSELECT_CC(SDNode *N);
239 SDOperand visitSETCC(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000240 SDOperand visitSIGN_EXTEND(SDNode *N);
241 SDOperand visitZERO_EXTEND(SDNode *N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000242 SDOperand visitANY_EXTEND(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000243 SDOperand visitSIGN_EXTEND_INREG(SDNode *N);
244 SDOperand visitTRUNCATE(SDNode *N);
Chris Lattner94683772005-12-23 05:30:37 +0000245 SDOperand visitBIT_CONVERT(SDNode *N);
Chris Lattner6258fb22006-04-02 02:53:43 +0000246 SDOperand visitVBIT_CONVERT(SDNode *N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000247 SDOperand visitFADD(SDNode *N);
248 SDOperand visitFSUB(SDNode *N);
249 SDOperand visitFMUL(SDNode *N);
250 SDOperand visitFDIV(SDNode *N);
251 SDOperand visitFREM(SDNode *N);
Chris Lattner12d83032006-03-05 05:30:57 +0000252 SDOperand visitFCOPYSIGN(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000253 SDOperand visitSINT_TO_FP(SDNode *N);
254 SDOperand visitUINT_TO_FP(SDNode *N);
255 SDOperand visitFP_TO_SINT(SDNode *N);
256 SDOperand visitFP_TO_UINT(SDNode *N);
257 SDOperand visitFP_ROUND(SDNode *N);
258 SDOperand visitFP_ROUND_INREG(SDNode *N);
259 SDOperand visitFP_EXTEND(SDNode *N);
260 SDOperand visitFNEG(SDNode *N);
261 SDOperand visitFABS(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000262 SDOperand visitBRCOND(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000263 SDOperand visitBR_CC(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000264 SDOperand visitLOAD(SDNode *N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000265 SDOperand visitSTORE(SDNode *N);
Chris Lattnerca242442006-03-19 01:27:56 +0000266 SDOperand visitINSERT_VECTOR_ELT(SDNode *N);
267 SDOperand visitVINSERT_VECTOR_ELT(SDNode *N);
Chris Lattnerd7648c82006-03-28 20:28:38 +0000268 SDOperand visitVBUILD_VECTOR(SDNode *N);
Chris Lattner66445d32006-03-28 22:11:53 +0000269 SDOperand visitVECTOR_SHUFFLE(SDNode *N);
Chris Lattnerf1d0c622006-03-31 22:16:43 +0000270 SDOperand visitVVECTOR_SHUFFLE(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000271
Evan Cheng44f1f092006-04-20 08:56:16 +0000272 SDOperand XformToShuffleWithZero(SDNode *N);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000273 SDOperand ReassociateOps(unsigned Opc, SDOperand LHS, SDOperand RHS);
274
Chris Lattner40c62d52005-10-18 06:04:22 +0000275 bool SimplifySelectOps(SDNode *SELECT, SDOperand LHS, SDOperand RHS);
Chris Lattner35e5c142006-05-05 05:51:50 +0000276 SDOperand SimplifyBinOpWithSameOpcodeHands(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000277 SDOperand SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2);
278 SDOperand SimplifySelectCC(SDOperand N0, SDOperand N1, SDOperand N2,
Chris Lattner1eba01e2007-04-11 06:50:51 +0000279 SDOperand N3, ISD::CondCode CC,
280 bool NotExtCompare = false);
Nate Begeman452d7be2005-09-16 00:54:12 +0000281 SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N0, SDOperand N1,
Nate Begemane17daeb2005-10-05 21:43:42 +0000282 ISD::CondCode Cond, bool foldBooleans = true);
Chris Lattner6258fb22006-04-02 02:53:43 +0000283 SDOperand ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(SDNode *, MVT::ValueType);
Nate Begeman69575232005-10-20 02:15:44 +0000284 SDOperand BuildSDIV(SDNode *N);
Chris Lattner516b9622006-09-14 20:50:57 +0000285 SDOperand BuildUDIV(SDNode *N);
286 SDNode *MatchRotate(SDOperand LHS, SDOperand RHS);
Evan Chengc88138f2007-03-22 01:54:19 +0000287 SDOperand ReduceLoadWidth(SDNode *N);
Jim Laskey279f0532006-09-25 16:29:54 +0000288
Jim Laskey6ff23e52006-10-04 16:53:27 +0000289 /// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
290 /// looking for aliasing nodes and adding them to the Aliases vector.
Jim Laskeybc588b82006-10-05 15:07:25 +0000291 void GatherAllAliases(SDNode *N, SDOperand OriginalChain,
Jim Laskey6ff23e52006-10-04 16:53:27 +0000292 SmallVector<SDOperand, 8> &Aliases);
293
Jim Laskey096c22e2006-10-18 12:29:57 +0000294 /// isAlias - Return true if there is any possibility that the two addresses
295 /// overlap.
296 bool isAlias(SDOperand Ptr1, int64_t Size1,
297 const Value *SrcValue1, int SrcValueOffset1,
298 SDOperand Ptr2, int64_t Size2,
Jeff Cohend41b30d2006-11-05 19:31:28 +0000299 const Value *SrcValue2, int SrcValueOffset2);
Jim Laskey096c22e2006-10-18 12:29:57 +0000300
Jim Laskey7ca56af2006-10-11 13:47:09 +0000301 /// FindAliasInfo - Extracts the relevant alias information from the memory
302 /// node. Returns true if the operand was a load.
303 bool FindAliasInfo(SDNode *N,
Jim Laskey096c22e2006-10-18 12:29:57 +0000304 SDOperand &Ptr, int64_t &Size,
305 const Value *&SrcValue, int &SrcValueOffset);
Jim Laskey7ca56af2006-10-11 13:47:09 +0000306
Jim Laskey279f0532006-09-25 16:29:54 +0000307 /// FindBetterChain - Walk up chain skipping non-aliasing memory nodes,
Jim Laskey6ff23e52006-10-04 16:53:27 +0000308 /// looking for a better chain (aliasing node.)
Jim Laskey279f0532006-09-25 16:29:54 +0000309 SDOperand FindBetterChain(SDNode *N, SDOperand Chain);
310
Nate Begeman1d4d4142005-09-01 00:19:25 +0000311public:
Jim Laskeyc7c3f112006-10-16 20:52:31 +0000312 DAGCombiner(SelectionDAG &D, AliasAnalysis &A)
313 : DAG(D),
314 TLI(D.getTargetLoweringInfo()),
315 AfterLegalize(false),
316 AA(A) {}
Nate Begeman1d4d4142005-09-01 00:19:25 +0000317
318 /// Run - runs the dag combiner on all nodes in the work list
Nate Begeman4ebd8052005-09-01 23:24:04 +0000319 void Run(bool RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000320 };
321}
322
Chris Lattner24664722006-03-01 04:53:38 +0000323//===----------------------------------------------------------------------===//
324// TargetLowering::DAGCombinerInfo implementation
325//===----------------------------------------------------------------------===//
326
327void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) {
328 ((DAGCombiner*)DC)->AddToWorkList(N);
329}
330
331SDOperand TargetLowering::DAGCombinerInfo::
332CombineTo(SDNode *N, const std::vector<SDOperand> &To) {
Chris Lattner3577e382006-08-11 17:56:38 +0000333 return ((DAGCombiner*)DC)->CombineTo(N, &To[0], To.size());
Chris Lattner24664722006-03-01 04:53:38 +0000334}
335
336SDOperand TargetLowering::DAGCombinerInfo::
337CombineTo(SDNode *N, SDOperand Res) {
338 return ((DAGCombiner*)DC)->CombineTo(N, Res);
339}
340
341
342SDOperand TargetLowering::DAGCombinerInfo::
343CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1) {
344 return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1);
345}
346
347
Chris Lattner24664722006-03-01 04:53:38 +0000348//===----------------------------------------------------------------------===//
Chris Lattner29446522007-05-14 22:04:50 +0000349// Helper Functions
350//===----------------------------------------------------------------------===//
351
352/// isNegatibleForFree - Return 1 if we can compute the negated form of the
353/// specified expression for the same cost as the expression itself, or 2 if we
354/// can compute the negated form more cheaply than the expression itself.
Chris Lattner501fee72007-05-23 07:35:22 +0000355static char isNegatibleForFree(SDOperand Op, unsigned Depth = 0) {
Chris Lattner29446522007-05-14 22:04:50 +0000356 // fneg is removable even if it has multiple uses.
357 if (Op.getOpcode() == ISD::FNEG) return 2;
358
359 // Don't allow anything with multiple uses.
360 if (!Op.hasOneUse()) return 0;
361
Chris Lattner3adf9512007-05-25 02:19:06 +0000362 // Don't recurse exponentially.
363 if (Depth > 6) return 0;
364
Chris Lattner29446522007-05-14 22:04:50 +0000365 switch (Op.getOpcode()) {
366 default: return false;
367 case ISD::ConstantFP:
368 return 1;
369 case ISD::FADD:
370 // FIXME: determine better conditions for this xform.
371 if (!UnsafeFPMath) return 0;
372
373 // -(A+B) -> -A - B
Chris Lattner501fee72007-05-23 07:35:22 +0000374 if (char V = isNegatibleForFree(Op.getOperand(0), Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000375 return V;
376 // -(A+B) -> -B - A
Chris Lattner501fee72007-05-23 07:35:22 +0000377 return isNegatibleForFree(Op.getOperand(1), Depth+1);
Chris Lattner29446522007-05-14 22:04:50 +0000378 case ISD::FSUB:
379 // We can't turn -(A-B) into B-A when we honor signed zeros.
380 if (!UnsafeFPMath) return 0;
381
382 // -(A-B) -> B-A
383 return 1;
384
385 case ISD::FMUL:
386 case ISD::FDIV:
387 if (HonorSignDependentRoundingFPMath()) return 0;
388
389 // -(X*Y) -> (-X * Y) or (X*-Y)
Chris Lattner501fee72007-05-23 07:35:22 +0000390 if (char V = isNegatibleForFree(Op.getOperand(0), Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000391 return V;
392
Chris Lattner501fee72007-05-23 07:35:22 +0000393 return isNegatibleForFree(Op.getOperand(1), Depth+1);
Chris Lattner29446522007-05-14 22:04:50 +0000394
395 case ISD::FP_EXTEND:
396 case ISD::FP_ROUND:
397 case ISD::FSIN:
Chris Lattner501fee72007-05-23 07:35:22 +0000398 return isNegatibleForFree(Op.getOperand(0), Depth+1);
Chris Lattner29446522007-05-14 22:04:50 +0000399 }
400}
401
402/// GetNegatedExpression - If isNegatibleForFree returns true, this function
403/// returns the newly negated expression.
Chris Lattner3adf9512007-05-25 02:19:06 +0000404static SDOperand GetNegatedExpression(SDOperand Op, SelectionDAG &DAG,
405 unsigned Depth = 0) {
Chris Lattner29446522007-05-14 22:04:50 +0000406 // fneg is removable even if it has multiple uses.
407 if (Op.getOpcode() == ISD::FNEG) return Op.getOperand(0);
408
409 // Don't allow anything with multiple uses.
410 assert(Op.hasOneUse() && "Unknown reuse!");
411
Chris Lattner3adf9512007-05-25 02:19:06 +0000412 assert(Depth <= 6 && "GetNegatedExpression doesn't match isNegatibleForFree");
Chris Lattner29446522007-05-14 22:04:50 +0000413 switch (Op.getOpcode()) {
414 default: assert(0 && "Unknown code");
415 case ISD::ConstantFP:
416 return DAG.getConstantFP(-cast<ConstantFPSDNode>(Op)->getValue(),
417 Op.getValueType());
418 case ISD::FADD:
419 // FIXME: determine better conditions for this xform.
420 assert(UnsafeFPMath);
421
422 // -(A+B) -> -A - B
Chris Lattner3adf9512007-05-25 02:19:06 +0000423 if (isNegatibleForFree(Op.getOperand(0), Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000424 return DAG.getNode(ISD::FSUB, Op.getValueType(),
Chris Lattner3adf9512007-05-25 02:19:06 +0000425 GetNegatedExpression(Op.getOperand(0), DAG, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000426 Op.getOperand(1));
427 // -(A+B) -> -B - A
428 return DAG.getNode(ISD::FSUB, Op.getValueType(),
Chris Lattner3adf9512007-05-25 02:19:06 +0000429 GetNegatedExpression(Op.getOperand(1), DAG, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000430 Op.getOperand(0));
431 case ISD::FSUB:
432 // We can't turn -(A-B) into B-A when we honor signed zeros.
433 assert(UnsafeFPMath);
434
435 // -(A-B) -> B-A
436 return DAG.getNode(ISD::FSUB, Op.getValueType(), Op.getOperand(1),
437 Op.getOperand(0));
438
439 case ISD::FMUL:
440 case ISD::FDIV:
441 assert(!HonorSignDependentRoundingFPMath());
442
443 // -(X*Y) -> -X * Y
Chris Lattner3adf9512007-05-25 02:19:06 +0000444 if (isNegatibleForFree(Op.getOperand(0), Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000445 return DAG.getNode(Op.getOpcode(), Op.getValueType(),
Chris Lattner3adf9512007-05-25 02:19:06 +0000446 GetNegatedExpression(Op.getOperand(0), DAG, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000447 Op.getOperand(1));
448
449 // -(X*Y) -> X * -Y
450 return DAG.getNode(Op.getOpcode(), Op.getValueType(),
451 Op.getOperand(0),
Chris Lattner3adf9512007-05-25 02:19:06 +0000452 GetNegatedExpression(Op.getOperand(1), DAG, Depth+1));
Chris Lattner29446522007-05-14 22:04:50 +0000453
454 case ISD::FP_EXTEND:
455 case ISD::FP_ROUND:
456 case ISD::FSIN:
457 return DAG.getNode(Op.getOpcode(), Op.getValueType(),
Chris Lattner3adf9512007-05-25 02:19:06 +0000458 GetNegatedExpression(Op.getOperand(0), DAG, Depth+1));
Chris Lattner29446522007-05-14 22:04:50 +0000459 }
460}
Chris Lattner24664722006-03-01 04:53:38 +0000461
462
Nate Begeman4ebd8052005-09-01 23:24:04 +0000463// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
464// that selects between the values 1 and 0, making it equivalent to a setcc.
Nate Begeman646d7e22005-09-02 21:18:40 +0000465// Also, set the incoming LHS, RHS, and CC references to the appropriate
466// nodes based on the type of node we are checking. This simplifies life a
467// bit for the callers.
468static bool isSetCCEquivalent(SDOperand N, SDOperand &LHS, SDOperand &RHS,
469 SDOperand &CC) {
470 if (N.getOpcode() == ISD::SETCC) {
471 LHS = N.getOperand(0);
472 RHS = N.getOperand(1);
473 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000474 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000475 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000476 if (N.getOpcode() == ISD::SELECT_CC &&
477 N.getOperand(2).getOpcode() == ISD::Constant &&
478 N.getOperand(3).getOpcode() == ISD::Constant &&
479 cast<ConstantSDNode>(N.getOperand(2))->getValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000480 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
481 LHS = N.getOperand(0);
482 RHS = N.getOperand(1);
483 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000484 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000485 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000486 return false;
487}
488
Nate Begeman99801192005-09-07 23:25:52 +0000489// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
490// one use. If this is true, it allows the users to invert the operation for
491// free when it is profitable to do so.
492static bool isOneUseSetCC(SDOperand N) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000493 SDOperand N0, N1, N2;
Nate Begeman646d7e22005-09-02 21:18:40 +0000494 if (isSetCCEquivalent(N, N0, N1, N2) && N.Val->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000495 return true;
496 return false;
497}
498
Nate Begemancd4d58c2006-02-03 06:46:56 +0000499SDOperand DAGCombiner::ReassociateOps(unsigned Opc, SDOperand N0, SDOperand N1){
500 MVT::ValueType VT = N0.getValueType();
501 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
502 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
503 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
504 if (isa<ConstantSDNode>(N1)) {
505 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(1), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000506 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000507 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(0));
508 } else if (N0.hasOneUse()) {
509 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(0), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000510 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000511 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(1));
512 }
513 }
514 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
515 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
516 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
517 if (isa<ConstantSDNode>(N0)) {
518 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(1), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000519 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000520 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(0));
521 } else if (N1.hasOneUse()) {
522 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(0), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000523 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000524 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(1));
525 }
526 }
527 return SDOperand();
528}
529
Chris Lattner29446522007-05-14 22:04:50 +0000530//===----------------------------------------------------------------------===//
531// Main DAG Combiner implementation
532//===----------------------------------------------------------------------===//
533
Nate Begeman4ebd8052005-09-01 23:24:04 +0000534void DAGCombiner::Run(bool RunningAfterLegalize) {
535 // set the instance variable, so that the various visit routines may use it.
536 AfterLegalize = RunningAfterLegalize;
537
Nate Begeman646d7e22005-09-02 21:18:40 +0000538 // Add all the dag nodes to the worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000539 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
540 E = DAG.allnodes_end(); I != E; ++I)
541 WorkList.push_back(I);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000542
Chris Lattner95038592005-10-05 06:35:28 +0000543 // Create a dummy node (which is not added to allnodes), that adds a reference
544 // to the root node, preventing it from being deleted, and tracking any
545 // changes of the root.
546 HandleSDNode Dummy(DAG.getRoot());
547
Jim Laskey26f7fa72006-10-17 19:33:52 +0000548 // The root of the dag may dangle to deleted nodes until the dag combiner is
549 // done. Set it to null to avoid confusion.
550 DAG.setRoot(SDOperand());
Chris Lattner24664722006-03-01 04:53:38 +0000551
552 /// DagCombineInfo - Expose the DAG combiner to the target combiner impls.
553 TargetLowering::DAGCombinerInfo
Evan Chengfa1eb272007-02-08 22:13:59 +0000554 DagCombineInfo(DAG, !RunningAfterLegalize, false, this);
Jim Laskey6ff23e52006-10-04 16:53:27 +0000555
Nate Begeman1d4d4142005-09-01 00:19:25 +0000556 // while the worklist isn't empty, inspect the node on the end of it and
557 // try and combine it.
558 while (!WorkList.empty()) {
559 SDNode *N = WorkList.back();
560 WorkList.pop_back();
561
562 // If N has no uses, it is dead. Make sure to revisit all N's operands once
Chris Lattner95038592005-10-05 06:35:28 +0000563 // N is deleted from the DAG, since they too may now be dead or may have a
564 // reduced number of uses, allowing other xforms.
565 if (N->use_empty() && N != &Dummy) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000566 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Jim Laskey6ff23e52006-10-04 16:53:27 +0000567 AddToWorkList(N->getOperand(i).Val);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000568
Chris Lattner95038592005-10-05 06:35:28 +0000569 DAG.DeleteNode(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000570 continue;
571 }
572
Nate Begeman83e75ec2005-09-06 04:43:02 +0000573 SDOperand RV = visit(N);
Chris Lattner24664722006-03-01 04:53:38 +0000574
575 // If nothing happened, try a target-specific DAG combine.
576 if (RV.Val == 0) {
Chris Lattner729c6d12006-05-27 00:43:02 +0000577 assert(N->getOpcode() != ISD::DELETED_NODE &&
578 "Node was deleted but visit returned NULL!");
Chris Lattner24664722006-03-01 04:53:38 +0000579 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
580 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode()))
581 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
582 }
583
Nate Begeman83e75ec2005-09-06 04:43:02 +0000584 if (RV.Val) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000585 ++NodesCombined;
Nate Begeman646d7e22005-09-02 21:18:40 +0000586 // If we get back the same node we passed in, rather than a new node or
587 // zero, we know that the node must have defined multiple values and
588 // CombineTo was used. Since CombineTo takes care of the worklist
589 // mechanics for us, we have no work to do in this case.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000590 if (RV.Val != N) {
Chris Lattner729c6d12006-05-27 00:43:02 +0000591 assert(N->getOpcode() != ISD::DELETED_NODE &&
592 RV.Val->getOpcode() != ISD::DELETED_NODE &&
593 "Node was deleted but visit returned new node!");
594
Dan Gohmanb5bec2b2007-06-19 14:13:56 +0000595 DOUT << "\nReplacing.3 "; DEBUG(N->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +0000596 DOUT << "\nWith: "; DEBUG(RV.Val->dump(&DAG));
597 DOUT << '\n';
Chris Lattner01a22022005-10-10 22:04:48 +0000598 std::vector<SDNode*> NowDead;
Evan Cheng2adffa12006-09-21 19:04:05 +0000599 if (N->getNumValues() == RV.Val->getNumValues())
600 DAG.ReplaceAllUsesWith(N, RV.Val, &NowDead);
601 else {
602 assert(N->getValueType(0) == RV.getValueType() && "Type mismatch");
603 SDOperand OpV = RV;
604 DAG.ReplaceAllUsesWith(N, &OpV, &NowDead);
605 }
Nate Begeman646d7e22005-09-02 21:18:40 +0000606
607 // Push the new node and any users onto the worklist
Jim Laskey6ff23e52006-10-04 16:53:27 +0000608 AddToWorkList(RV.Val);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000609 AddUsersToWorkList(RV.Val);
Nate Begeman646d7e22005-09-02 21:18:40 +0000610
Jim Laskey6ff23e52006-10-04 16:53:27 +0000611 // Nodes can be reintroduced into the worklist. Make sure we do not
612 // process a node that has been replaced.
Nate Begeman646d7e22005-09-02 21:18:40 +0000613 removeFromWorkList(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000614 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
615 removeFromWorkList(NowDead[i]);
Chris Lattner5c46f742005-10-05 06:11:08 +0000616
617 // Finally, since the node is now dead, remove it from the graph.
618 DAG.DeleteNode(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000619 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000620 }
621 }
Chris Lattner95038592005-10-05 06:35:28 +0000622
623 // If the root changed (e.g. it was a dead load, update the root).
624 DAG.setRoot(Dummy.getValue());
Nate Begeman1d4d4142005-09-01 00:19:25 +0000625}
626
Nate Begeman83e75ec2005-09-06 04:43:02 +0000627SDOperand DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000628 switch(N->getOpcode()) {
629 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +0000630 case ISD::TokenFactor: return visitTokenFactor(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000631 case ISD::ADD: return visitADD(N);
632 case ISD::SUB: return visitSUB(N);
Chris Lattner91153682007-03-04 20:03:15 +0000633 case ISD::ADDC: return visitADDC(N);
634 case ISD::ADDE: return visitADDE(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000635 case ISD::MUL: return visitMUL(N);
636 case ISD::SDIV: return visitSDIV(N);
637 case ISD::UDIV: return visitUDIV(N);
638 case ISD::SREM: return visitSREM(N);
639 case ISD::UREM: return visitUREM(N);
640 case ISD::MULHU: return visitMULHU(N);
641 case ISD::MULHS: return visitMULHS(N);
642 case ISD::AND: return visitAND(N);
643 case ISD::OR: return visitOR(N);
644 case ISD::XOR: return visitXOR(N);
645 case ISD::SHL: return visitSHL(N);
646 case ISD::SRA: return visitSRA(N);
647 case ISD::SRL: return visitSRL(N);
648 case ISD::CTLZ: return visitCTLZ(N);
649 case ISD::CTTZ: return visitCTTZ(N);
650 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000651 case ISD::SELECT: return visitSELECT(N);
652 case ISD::SELECT_CC: return visitSELECT_CC(N);
653 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000654 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
655 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000656 case ISD::ANY_EXTEND: return visitANY_EXTEND(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000657 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
658 case ISD::TRUNCATE: return visitTRUNCATE(N);
Chris Lattner94683772005-12-23 05:30:37 +0000659 case ISD::BIT_CONVERT: return visitBIT_CONVERT(N);
Chris Lattner6258fb22006-04-02 02:53:43 +0000660 case ISD::VBIT_CONVERT: return visitVBIT_CONVERT(N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000661 case ISD::FADD: return visitFADD(N);
662 case ISD::FSUB: return visitFSUB(N);
663 case ISD::FMUL: return visitFMUL(N);
664 case ISD::FDIV: return visitFDIV(N);
665 case ISD::FREM: return visitFREM(N);
Chris Lattner12d83032006-03-05 05:30:57 +0000666 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000667 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
668 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
669 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
670 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
671 case ISD::FP_ROUND: return visitFP_ROUND(N);
672 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
673 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
674 case ISD::FNEG: return visitFNEG(N);
675 case ISD::FABS: return visitFABS(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000676 case ISD::BRCOND: return visitBRCOND(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000677 case ISD::BR_CC: return visitBR_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000678 case ISD::LOAD: return visitLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000679 case ISD::STORE: return visitSTORE(N);
Chris Lattnerca242442006-03-19 01:27:56 +0000680 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
681 case ISD::VINSERT_VECTOR_ELT: return visitVINSERT_VECTOR_ELT(N);
Chris Lattnerd7648c82006-03-28 20:28:38 +0000682 case ISD::VBUILD_VECTOR: return visitVBUILD_VECTOR(N);
Chris Lattner66445d32006-03-28 22:11:53 +0000683 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
Chris Lattnerf1d0c622006-03-31 22:16:43 +0000684 case ISD::VVECTOR_SHUFFLE: return visitVVECTOR_SHUFFLE(N);
Chris Lattneredab1b92006-04-02 03:25:57 +0000685 case ISD::VADD: return visitVBinOp(N, ISD::ADD , ISD::FADD);
686 case ISD::VSUB: return visitVBinOp(N, ISD::SUB , ISD::FSUB);
687 case ISD::VMUL: return visitVBinOp(N, ISD::MUL , ISD::FMUL);
688 case ISD::VSDIV: return visitVBinOp(N, ISD::SDIV, ISD::FDIV);
689 case ISD::VUDIV: return visitVBinOp(N, ISD::UDIV, ISD::UDIV);
690 case ISD::VAND: return visitVBinOp(N, ISD::AND , ISD::AND);
691 case ISD::VOR: return visitVBinOp(N, ISD::OR , ISD::OR);
692 case ISD::VXOR: return visitVBinOp(N, ISD::XOR , ISD::XOR);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000693 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000694 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000695}
696
Chris Lattner6270f682006-10-08 22:57:01 +0000697/// getInputChainForNode - Given a node, return its input chain if it has one,
698/// otherwise return a null sd operand.
699static SDOperand getInputChainForNode(SDNode *N) {
700 if (unsigned NumOps = N->getNumOperands()) {
701 if (N->getOperand(0).getValueType() == MVT::Other)
702 return N->getOperand(0);
703 else if (N->getOperand(NumOps-1).getValueType() == MVT::Other)
704 return N->getOperand(NumOps-1);
705 for (unsigned i = 1; i < NumOps-1; ++i)
706 if (N->getOperand(i).getValueType() == MVT::Other)
707 return N->getOperand(i);
708 }
709 return SDOperand(0, 0);
710}
711
Nate Begeman83e75ec2005-09-06 04:43:02 +0000712SDOperand DAGCombiner::visitTokenFactor(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +0000713 // If N has two operands, where one has an input chain equal to the other,
714 // the 'other' chain is redundant.
715 if (N->getNumOperands() == 2) {
716 if (getInputChainForNode(N->getOperand(0).Val) == N->getOperand(1))
717 return N->getOperand(0);
718 if (getInputChainForNode(N->getOperand(1).Val) == N->getOperand(0))
719 return N->getOperand(1);
720 }
721
Chris Lattnerc76d4412007-05-16 06:37:59 +0000722 SmallVector<SDNode *, 8> TFs; // List of token factors to visit.
723 SmallVector<SDOperand, 8> Ops; // Ops for replacing token factor.
724 SmallPtrSet<SDNode*, 16> SeenOps;
725 bool Changed = false; // If we should replace this token factor.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000726
727 // Start out with this token factor.
Jim Laskey279f0532006-09-25 16:29:54 +0000728 TFs.push_back(N);
Jim Laskey279f0532006-09-25 16:29:54 +0000729
Jim Laskey71382342006-10-07 23:37:56 +0000730 // Iterate through token factors. The TFs grows when new token factors are
Jim Laskeybc588b82006-10-05 15:07:25 +0000731 // encountered.
732 for (unsigned i = 0; i < TFs.size(); ++i) {
733 SDNode *TF = TFs[i];
734
Jim Laskey6ff23e52006-10-04 16:53:27 +0000735 // Check each of the operands.
736 for (unsigned i = 0, ie = TF->getNumOperands(); i != ie; ++i) {
737 SDOperand Op = TF->getOperand(i);
Jim Laskey279f0532006-09-25 16:29:54 +0000738
Jim Laskey6ff23e52006-10-04 16:53:27 +0000739 switch (Op.getOpcode()) {
740 case ISD::EntryToken:
Jim Laskeybc588b82006-10-05 15:07:25 +0000741 // Entry tokens don't need to be added to the list. They are
742 // rededundant.
743 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000744 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000745
Jim Laskey6ff23e52006-10-04 16:53:27 +0000746 case ISD::TokenFactor:
Jim Laskeybc588b82006-10-05 15:07:25 +0000747 if ((CombinerAA || Op.hasOneUse()) &&
748 std::find(TFs.begin(), TFs.end(), Op.Val) == TFs.end()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +0000749 // Queue up for processing.
750 TFs.push_back(Op.Val);
751 // Clean up in case the token factor is removed.
752 AddToWorkList(Op.Val);
753 Changed = true;
754 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000755 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000756 // Fall thru
757
758 default:
Chris Lattnerc76d4412007-05-16 06:37:59 +0000759 // Only add if it isn't already in the list.
760 if (SeenOps.insert(Op.Val))
Jim Laskeybc588b82006-10-05 15:07:25 +0000761 Ops.push_back(Op);
Chris Lattnerc76d4412007-05-16 06:37:59 +0000762 else
763 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000764 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000765 }
766 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000767 }
768
769 SDOperand Result;
770
771 // If we've change things around then replace token factor.
772 if (Changed) {
773 if (Ops.size() == 0) {
774 // The entry token is the only possible outcome.
775 Result = DAG.getEntryNode();
776 } else {
777 // New and improved token factor.
778 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0], Ops.size());
Nate Begemanded49632005-10-13 03:11:28 +0000779 }
Jim Laskey274062c2006-10-13 23:32:28 +0000780
781 // Don't add users to work list.
782 return CombineTo(N, Result, false);
Nate Begemanded49632005-10-13 03:11:28 +0000783 }
Jim Laskey279f0532006-09-25 16:29:54 +0000784
Jim Laskey6ff23e52006-10-04 16:53:27 +0000785 return Result;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000786}
787
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000788static
789SDOperand combineShlAddConstant(SDOperand N0, SDOperand N1, SelectionDAG &DAG) {
790 MVT::ValueType VT = N0.getValueType();
791 SDOperand N00 = N0.getOperand(0);
792 SDOperand N01 = N0.getOperand(1);
793 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N01);
794 if (N01C && N00.getOpcode() == ISD::ADD && N00.Val->hasOneUse() &&
795 isa<ConstantSDNode>(N00.getOperand(1))) {
796 N0 = DAG.getNode(ISD::ADD, VT,
797 DAG.getNode(ISD::SHL, VT, N00.getOperand(0), N01),
798 DAG.getNode(ISD::SHL, VT, N00.getOperand(1), N01));
799 return DAG.getNode(ISD::ADD, VT, N0, N1);
800 }
801 return SDOperand();
802}
803
Evan Chengb13cdbd2007-06-21 07:39:16 +0000804static
805SDOperand combineSelectAndUse(SDNode *N, SDOperand Slct, SDOperand OtherOp,
806 SelectionDAG &DAG) {
807 MVT::ValueType VT = N->getValueType(0);
808 unsigned Opc = N->getOpcode();
809 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
810 SDOperand LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
811 SDOperand RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
812 ISD::CondCode CC = ISD::SETCC_INVALID;
813 if (isSlctCC)
814 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
815 else {
816 SDOperand CCOp = Slct.getOperand(0);
817 if (CCOp.getOpcode() == ISD::SETCC)
818 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
819 }
820
821 bool DoXform = false;
822 bool InvCC = false;
823 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
824 "Bad input!");
825 if (LHS.getOpcode() == ISD::Constant &&
826 cast<ConstantSDNode>(LHS)->isNullValue())
827 DoXform = true;
828 else if (CC != ISD::SETCC_INVALID &&
829 RHS.getOpcode() == ISD::Constant &&
830 cast<ConstantSDNode>(RHS)->isNullValue()) {
831 std::swap(LHS, RHS);
832 bool isInt = MVT::isInteger(isSlctCC ? Slct.getOperand(0).getValueType()
833 : Slct.getOperand(0).getOperand(0).getValueType());
834 CC = ISD::getSetCCInverse(CC, isInt);
835 DoXform = true;
836 InvCC = true;
837 }
838
839 if (DoXform) {
840 SDOperand Result = DAG.getNode(Opc, VT, OtherOp, RHS);
841 if (isSlctCC)
842 return DAG.getSelectCC(OtherOp, Result,
843 Slct.getOperand(0), Slct.getOperand(1), CC);
844 SDOperand CCOp = Slct.getOperand(0);
845 if (InvCC)
846 CCOp = DAG.getSetCC(CCOp.getValueType(), CCOp.getOperand(0),
847 CCOp.getOperand(1), CC);
848 return DAG.getNode(ISD::SELECT, VT, CCOp, OtherOp, Result);
849 }
850 return SDOperand();
851}
852
Nate Begeman83e75ec2005-09-06 04:43:02 +0000853SDOperand DAGCombiner::visitADD(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000854 SDOperand N0 = N->getOperand(0);
855 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000856 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
857 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemanf89d78d2005-09-07 16:09:19 +0000858 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000859
860 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000861 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000862 return DAG.getNode(ISD::ADD, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000863 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000864 if (N0C && !N1C)
865 return DAG.getNode(ISD::ADD, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000866 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000867 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000868 return N0;
Chris Lattner4aafb4f2006-01-12 20:22:43 +0000869 // fold ((c1-A)+c2) -> (c1+c2)-A
870 if (N1C && N0.getOpcode() == ISD::SUB)
871 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
872 return DAG.getNode(ISD::SUB, VT,
873 DAG.getConstant(N1C->getValue()+N0C->getValue(), VT),
874 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000875 // reassociate add
876 SDOperand RADD = ReassociateOps(ISD::ADD, N0, N1);
877 if (RADD.Val != 0)
878 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000879 // fold ((0-A) + B) -> B-A
880 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
881 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000882 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000883 // fold (A + (0-B)) -> A-B
884 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
885 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000886 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +0000887 // fold (A+(B-A)) -> B
888 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000889 return N1.getOperand(0);
Chris Lattner947c2892006-03-13 06:51:27 +0000890
Evan Cheng860771d2006-03-01 01:09:54 +0000891 if (!MVT::isVector(VT) && SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +0000892 return SDOperand(N, 0);
Chris Lattner947c2892006-03-13 06:51:27 +0000893
894 // fold (a+b) -> (a|b) iff a and b share no bits.
895 if (MVT::isInteger(VT) && !MVT::isVector(VT)) {
896 uint64_t LHSZero, LHSOne;
897 uint64_t RHSZero, RHSOne;
898 uint64_t Mask = MVT::getIntVTBitMask(VT);
Dan Gohmanea859be2007-06-22 14:59:07 +0000899 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Chris Lattner947c2892006-03-13 06:51:27 +0000900 if (LHSZero) {
Dan Gohmanea859be2007-06-22 14:59:07 +0000901 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
Chris Lattner947c2892006-03-13 06:51:27 +0000902
903 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
904 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
905 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
906 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
907 return DAG.getNode(ISD::OR, VT, N0, N1);
908 }
909 }
Evan Cheng3ef554d2006-11-06 08:14:30 +0000910
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000911 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
912 if (N0.getOpcode() == ISD::SHL && N0.Val->hasOneUse()) {
913 SDOperand Result = combineShlAddConstant(N0, N1, DAG);
914 if (Result.Val) return Result;
915 }
916 if (N1.getOpcode() == ISD::SHL && N1.Val->hasOneUse()) {
917 SDOperand Result = combineShlAddConstant(N1, N0, DAG);
918 if (Result.Val) return Result;
919 }
920
Evan Chengb13cdbd2007-06-21 07:39:16 +0000921 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
922 if (N0.getOpcode() == ISD::SELECT && N0.Val->hasOneUse()) {
923 SDOperand Result = combineSelectAndUse(N, N0, N1, DAG);
924 if (Result.Val) return Result;
925 }
926 if (N1.getOpcode() == ISD::SELECT && N1.Val->hasOneUse()) {
927 SDOperand Result = combineSelectAndUse(N, N1, N0, DAG);
928 if (Result.Val) return Result;
929 }
930
Nate Begeman83e75ec2005-09-06 04:43:02 +0000931 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000932}
933
Chris Lattner91153682007-03-04 20:03:15 +0000934SDOperand DAGCombiner::visitADDC(SDNode *N) {
935 SDOperand N0 = N->getOperand(0);
936 SDOperand N1 = N->getOperand(1);
937 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
938 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
939 MVT::ValueType VT = N0.getValueType();
940
941 // If the flag result is dead, turn this into an ADD.
942 if (N->hasNUsesOfValue(0, 1))
943 return CombineTo(N, DAG.getNode(ISD::ADD, VT, N1, N0),
Chris Lattnerb6541762007-03-04 20:40:38 +0000944 DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
Chris Lattner91153682007-03-04 20:03:15 +0000945
946 // canonicalize constant to RHS.
Chris Lattnerbcf24842007-03-04 20:08:45 +0000947 if (N0C && !N1C) {
948 SDOperand Ops[] = { N1, N0 };
949 return DAG.getNode(ISD::ADDC, N->getVTList(), Ops, 2);
950 }
Chris Lattner91153682007-03-04 20:03:15 +0000951
Chris Lattnerb6541762007-03-04 20:40:38 +0000952 // fold (addc x, 0) -> x + no carry out
953 if (N1C && N1C->isNullValue())
954 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
955
956 // fold (addc a, b) -> (or a, b), CARRY_FALSE iff a and b share no bits.
957 uint64_t LHSZero, LHSOne;
958 uint64_t RHSZero, RHSOne;
959 uint64_t Mask = MVT::getIntVTBitMask(VT);
Dan Gohmanea859be2007-06-22 14:59:07 +0000960 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Chris Lattnerb6541762007-03-04 20:40:38 +0000961 if (LHSZero) {
Dan Gohmanea859be2007-06-22 14:59:07 +0000962 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
Chris Lattnerb6541762007-03-04 20:40:38 +0000963
964 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
965 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
966 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
967 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
968 return CombineTo(N, DAG.getNode(ISD::OR, VT, N0, N1),
969 DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
970 }
Chris Lattner91153682007-03-04 20:03:15 +0000971
972 return SDOperand();
973}
974
975SDOperand DAGCombiner::visitADDE(SDNode *N) {
976 SDOperand N0 = N->getOperand(0);
977 SDOperand N1 = N->getOperand(1);
Chris Lattnerb6541762007-03-04 20:40:38 +0000978 SDOperand CarryIn = N->getOperand(2);
Chris Lattner91153682007-03-04 20:03:15 +0000979 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
980 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Chris Lattnerbcf24842007-03-04 20:08:45 +0000981 //MVT::ValueType VT = N0.getValueType();
Chris Lattner91153682007-03-04 20:03:15 +0000982
983 // canonicalize constant to RHS
Chris Lattnerbcf24842007-03-04 20:08:45 +0000984 if (N0C && !N1C) {
Chris Lattnerb6541762007-03-04 20:40:38 +0000985 SDOperand Ops[] = { N1, N0, CarryIn };
Chris Lattnerbcf24842007-03-04 20:08:45 +0000986 return DAG.getNode(ISD::ADDE, N->getVTList(), Ops, 3);
987 }
Chris Lattner91153682007-03-04 20:03:15 +0000988
Chris Lattnerb6541762007-03-04 20:40:38 +0000989 // fold (adde x, y, false) -> (addc x, y)
990 if (CarryIn.getOpcode() == ISD::CARRY_FALSE) {
991 SDOperand Ops[] = { N1, N0 };
992 return DAG.getNode(ISD::ADDC, N->getVTList(), Ops, 2);
993 }
Chris Lattner91153682007-03-04 20:03:15 +0000994
995 return SDOperand();
996}
997
998
999
Nate Begeman83e75ec2005-09-06 04:43:02 +00001000SDOperand DAGCombiner::visitSUB(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001001 SDOperand N0 = N->getOperand(0);
1002 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001003 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1004 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +00001005 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001006
Chris Lattner854077d2005-10-17 01:07:11 +00001007 // fold (sub x, x) -> 0
1008 if (N0 == N1)
1009 return DAG.getConstant(0, N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001010 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001011 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001012 return DAG.getNode(ISD::SUB, VT, N0, N1);
Chris Lattner05b57432005-10-11 06:07:15 +00001013 // fold (sub x, c) -> (add x, -c)
1014 if (N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001015 return DAG.getNode(ISD::ADD, VT, N0, DAG.getConstant(-N1C->getValue(), VT));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001016 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +00001017 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001018 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001019 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +00001020 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001021 return N0.getOperand(0);
Evan Chengb13cdbd2007-06-21 07:39:16 +00001022 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
1023 if (N1.getOpcode() == ISD::SELECT && N1.Val->hasOneUse()) {
1024 SDOperand Result = combineSelectAndUse(N, N1, N0, DAG);
1025 if (Result.Val) return Result;
1026 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001027 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001028}
1029
Nate Begeman83e75ec2005-09-06 04:43:02 +00001030SDOperand DAGCombiner::visitMUL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001031 SDOperand N0 = N->getOperand(0);
1032 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001033 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1034 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman223df222005-09-08 20:18:10 +00001035 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001036
1037 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001038 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001039 return DAG.getNode(ISD::MUL, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001040 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001041 if (N0C && !N1C)
1042 return DAG.getNode(ISD::MUL, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001043 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001044 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001045 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001046 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +00001047 if (N1C && N1C->isAllOnesValue())
Nate Begeman405e3ec2005-10-21 00:02:42 +00001048 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001049 // fold (mul x, (1 << c)) -> x << c
Nate Begeman646d7e22005-09-02 21:18:40 +00001050 if (N1C && isPowerOf2_64(N1C->getValue()))
Chris Lattner3e6099b2005-10-30 06:41:49 +00001051 return DAG.getNode(ISD::SHL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +00001052 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001053 TLI.getShiftAmountTy()));
Chris Lattner3e6099b2005-10-30 06:41:49 +00001054 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
1055 if (N1C && isPowerOf2_64(-N1C->getSignExtended())) {
1056 // FIXME: If the input is something that is easily negated (e.g. a
1057 // single-use add), we should put the negate there.
1058 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT),
1059 DAG.getNode(ISD::SHL, VT, N0,
1060 DAG.getConstant(Log2_64(-N1C->getSignExtended()),
1061 TLI.getShiftAmountTy())));
1062 }
Andrew Lenharth50a0d422006-04-02 21:42:45 +00001063
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001064 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
1065 if (N1C && N0.getOpcode() == ISD::SHL &&
1066 isa<ConstantSDNode>(N0.getOperand(1))) {
1067 SDOperand C3 = DAG.getNode(ISD::SHL, VT, N1, N0.getOperand(1));
Chris Lattner5750df92006-03-01 04:03:14 +00001068 AddToWorkList(C3.Val);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001069 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0), C3);
1070 }
1071
1072 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
1073 // use.
1074 {
1075 SDOperand Sh(0,0), Y(0,0);
1076 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
1077 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
1078 N0.Val->hasOneUse()) {
1079 Sh = N0; Y = N1;
1080 } else if (N1.getOpcode() == ISD::SHL &&
1081 isa<ConstantSDNode>(N1.getOperand(1)) && N1.Val->hasOneUse()) {
1082 Sh = N1; Y = N0;
1083 }
1084 if (Sh.Val) {
1085 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Sh.getOperand(0), Y);
1086 return DAG.getNode(ISD::SHL, VT, Mul, Sh.getOperand(1));
1087 }
1088 }
Chris Lattnera1deca32006-03-04 23:33:26 +00001089 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
1090 if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() &&
1091 isa<ConstantSDNode>(N0.getOperand(1))) {
1092 return DAG.getNode(ISD::ADD, VT,
1093 DAG.getNode(ISD::MUL, VT, N0.getOperand(0), N1),
1094 DAG.getNode(ISD::MUL, VT, N0.getOperand(1), N1));
1095 }
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001096
Nate Begemancd4d58c2006-02-03 06:46:56 +00001097 // reassociate mul
1098 SDOperand RMUL = ReassociateOps(ISD::MUL, N0, N1);
1099 if (RMUL.Val != 0)
1100 return RMUL;
Nate Begeman83e75ec2005-09-06 04:43:02 +00001101 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001102}
1103
Nate Begeman83e75ec2005-09-06 04:43:02 +00001104SDOperand DAGCombiner::visitSDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001105 SDOperand N0 = N->getOperand(0);
1106 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001107 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1108 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +00001109 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001110
1111 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001112 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001113 return DAG.getNode(ISD::SDIV, VT, N0, N1);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001114 // fold (sdiv X, 1) -> X
1115 if (N1C && N1C->getSignExtended() == 1LL)
1116 return N0;
1117 // fold (sdiv X, -1) -> 0-X
1118 if (N1C && N1C->isAllOnesValue())
1119 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +00001120 // If we know the sign bits of both operands are zero, strength reduce to a
1121 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
1122 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001123 if (DAG.MaskedValueIsZero(N1, SignBit) &&
1124 DAG.MaskedValueIsZero(N0, SignBit))
Chris Lattner094c8fc2005-10-07 06:10:46 +00001125 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
Nate Begemancd6a6ed2006-02-17 07:26:20 +00001126 // fold (sdiv X, pow2) -> simple ops after legalize
Nate Begemanfb7217b2006-02-17 19:54:08 +00001127 if (N1C && N1C->getValue() && !TLI.isIntDivCheap() &&
Nate Begeman405e3ec2005-10-21 00:02:42 +00001128 (isPowerOf2_64(N1C->getSignExtended()) ||
1129 isPowerOf2_64(-N1C->getSignExtended()))) {
1130 // If dividing by powers of two is cheap, then don't perform the following
1131 // fold.
1132 if (TLI.isPow2DivCheap())
1133 return SDOperand();
1134 int64_t pow2 = N1C->getSignExtended();
1135 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
Chris Lattner8f4880b2006-02-16 08:02:36 +00001136 unsigned lg2 = Log2_64(abs2);
1137 // Splat the sign bit into the register
1138 SDOperand SGN = DAG.getNode(ISD::SRA, VT, N0,
Nate Begeman405e3ec2005-10-21 00:02:42 +00001139 DAG.getConstant(MVT::getSizeInBits(VT)-1,
1140 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00001141 AddToWorkList(SGN.Val);
Chris Lattner8f4880b2006-02-16 08:02:36 +00001142 // Add (N0 < 0) ? abs2 - 1 : 0;
1143 SDOperand SRL = DAG.getNode(ISD::SRL, VT, SGN,
1144 DAG.getConstant(MVT::getSizeInBits(VT)-lg2,
Nate Begeman405e3ec2005-10-21 00:02:42 +00001145 TLI.getShiftAmountTy()));
Chris Lattner8f4880b2006-02-16 08:02:36 +00001146 SDOperand ADD = DAG.getNode(ISD::ADD, VT, N0, SRL);
Chris Lattner5750df92006-03-01 04:03:14 +00001147 AddToWorkList(SRL.Val);
1148 AddToWorkList(ADD.Val); // Divide by pow2
Chris Lattner8f4880b2006-02-16 08:02:36 +00001149 SDOperand SRA = DAG.getNode(ISD::SRA, VT, ADD,
1150 DAG.getConstant(lg2, TLI.getShiftAmountTy()));
Nate Begeman405e3ec2005-10-21 00:02:42 +00001151 // If we're dividing by a positive value, we're done. Otherwise, we must
1152 // negate the result.
1153 if (pow2 > 0)
1154 return SRA;
Chris Lattner5750df92006-03-01 04:03:14 +00001155 AddToWorkList(SRA.Val);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001156 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), SRA);
1157 }
Nate Begeman69575232005-10-20 02:15:44 +00001158 // if integer divide is expensive and we satisfy the requirements, emit an
1159 // alternate sequence.
Nate Begeman405e3ec2005-10-21 00:02:42 +00001160 if (N1C && (N1C->getSignExtended() < -1 || N1C->getSignExtended() > 1) &&
Chris Lattnere9936d12005-10-22 18:50:15 +00001161 !TLI.isIntDivCheap()) {
1162 SDOperand Op = BuildSDIV(N);
1163 if (Op.Val) return Op;
Nate Begeman69575232005-10-20 02:15:44 +00001164 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001165 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001166}
1167
Nate Begeman83e75ec2005-09-06 04:43:02 +00001168SDOperand DAGCombiner::visitUDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001169 SDOperand N0 = N->getOperand(0);
1170 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001171 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1172 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +00001173 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001174
1175 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001176 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001177 return DAG.getNode(ISD::UDIV, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001178 // fold (udiv x, (1 << c)) -> x >>u c
Nate Begeman646d7e22005-09-02 21:18:40 +00001179 if (N1C && isPowerOf2_64(N1C->getValue()))
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001180 return DAG.getNode(ISD::SRL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +00001181 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001182 TLI.getShiftAmountTy()));
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001183 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
1184 if (N1.getOpcode() == ISD::SHL) {
1185 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
1186 if (isPowerOf2_64(SHC->getValue())) {
1187 MVT::ValueType ADDVT = N1.getOperand(1).getValueType();
Nate Begemanc031e332006-02-05 07:36:48 +00001188 SDOperand Add = DAG.getNode(ISD::ADD, ADDVT, N1.getOperand(1),
1189 DAG.getConstant(Log2_64(SHC->getValue()),
1190 ADDVT));
Chris Lattner5750df92006-03-01 04:03:14 +00001191 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +00001192 return DAG.getNode(ISD::SRL, VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001193 }
1194 }
1195 }
Nate Begeman69575232005-10-20 02:15:44 +00001196 // fold (udiv x, c) -> alternate
Chris Lattnere9936d12005-10-22 18:50:15 +00001197 if (N1C && N1C->getValue() && !TLI.isIntDivCheap()) {
1198 SDOperand Op = BuildUDIV(N);
1199 if (Op.Val) return Op;
1200 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001201 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001202}
1203
Nate Begeman83e75ec2005-09-06 04:43:02 +00001204SDOperand DAGCombiner::visitSREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001205 SDOperand N0 = N->getOperand(0);
1206 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001207 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1208 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +00001209 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001210
1211 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001212 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001213 return DAG.getNode(ISD::SREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +00001214 // If we know the sign bits of both operands are zero, strength reduce to a
1215 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
1216 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001217 if (DAG.MaskedValueIsZero(N1, SignBit) &&
1218 DAG.MaskedValueIsZero(N0, SignBit))
Nate Begemana148d982006-01-18 22:35:16 +00001219 return DAG.getNode(ISD::UREM, VT, N0, N1);
Chris Lattner26d29902006-10-12 20:58:32 +00001220
1221 // Unconditionally lower X%C -> X-X/C*C. This allows the X/C logic to hack on
1222 // the remainder operation.
1223 if (N1C && !N1C->isNullValue()) {
1224 SDOperand Div = DAG.getNode(ISD::SDIV, VT, N0, N1);
1225 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Div, N1);
1226 SDOperand Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
1227 AddToWorkList(Div.Val);
1228 AddToWorkList(Mul.Val);
1229 return Sub;
1230 }
1231
Nate Begeman83e75ec2005-09-06 04:43:02 +00001232 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001233}
1234
Nate Begeman83e75ec2005-09-06 04:43:02 +00001235SDOperand DAGCombiner::visitUREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001236 SDOperand N0 = N->getOperand(0);
1237 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001238 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1239 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +00001240 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001241
1242 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001243 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001244 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +00001245 // fold (urem x, pow2) -> (and x, pow2-1)
1246 if (N1C && !N1C->isNullValue() && isPowerOf2_64(N1C->getValue()))
Nate Begemana148d982006-01-18 22:35:16 +00001247 return DAG.getNode(ISD::AND, VT, N0, DAG.getConstant(N1C->getValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +00001248 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
1249 if (N1.getOpcode() == ISD::SHL) {
1250 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
1251 if (isPowerOf2_64(SHC->getValue())) {
Nate Begemanbab92392006-02-05 08:07:24 +00001252 SDOperand Add = DAG.getNode(ISD::ADD, VT, N1,DAG.getConstant(~0ULL,VT));
Chris Lattner5750df92006-03-01 04:03:14 +00001253 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +00001254 return DAG.getNode(ISD::AND, VT, N0, Add);
1255 }
1256 }
1257 }
Chris Lattner26d29902006-10-12 20:58:32 +00001258
1259 // Unconditionally lower X%C -> X-X/C*C. This allows the X/C logic to hack on
1260 // the remainder operation.
1261 if (N1C && !N1C->isNullValue()) {
1262 SDOperand Div = DAG.getNode(ISD::UDIV, VT, N0, N1);
1263 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Div, N1);
1264 SDOperand Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
1265 AddToWorkList(Div.Val);
1266 AddToWorkList(Mul.Val);
1267 return Sub;
1268 }
1269
Nate Begeman83e75ec2005-09-06 04:43:02 +00001270 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001271}
1272
Nate Begeman83e75ec2005-09-06 04:43:02 +00001273SDOperand DAGCombiner::visitMULHS(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001274 SDOperand N0 = N->getOperand(0);
1275 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001276 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001277
1278 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001279 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001280 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001281 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001282 if (N1C && N1C->getValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001283 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
1284 DAG.getConstant(MVT::getSizeInBits(N0.getValueType())-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001285 TLI.getShiftAmountTy()));
1286 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001287}
1288
Nate Begeman83e75ec2005-09-06 04:43:02 +00001289SDOperand DAGCombiner::visitMULHU(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001290 SDOperand N0 = N->getOperand(0);
1291 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001292 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001293
1294 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001295 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001296 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001297 // fold (mulhu x, 1) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001298 if (N1C && N1C->getValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001299 return DAG.getConstant(0, N0.getValueType());
1300 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001301}
1302
Chris Lattner35e5c142006-05-05 05:51:50 +00001303/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
1304/// two operands of the same opcode, try to simplify it.
1305SDOperand DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
1306 SDOperand N0 = N->getOperand(0), N1 = N->getOperand(1);
1307 MVT::ValueType VT = N0.getValueType();
1308 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
1309
Chris Lattner540121f2006-05-05 06:31:05 +00001310 // For each of OP in AND/OR/XOR:
1311 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
1312 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
1313 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
Chris Lattner0d8dae72006-05-05 06:32:04 +00001314 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y))
Chris Lattner540121f2006-05-05 06:31:05 +00001315 if ((N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND||
Chris Lattner0d8dae72006-05-05 06:32:04 +00001316 N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::TRUNCATE) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001317 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1318 SDOperand ORNode = DAG.getNode(N->getOpcode(),
1319 N0.getOperand(0).getValueType(),
1320 N0.getOperand(0), N1.getOperand(0));
1321 AddToWorkList(ORNode.Val);
Chris Lattner540121f2006-05-05 06:31:05 +00001322 return DAG.getNode(N0.getOpcode(), VT, ORNode);
Chris Lattner35e5c142006-05-05 05:51:50 +00001323 }
1324
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001325 // For each of OP in SHL/SRL/SRA/AND...
1326 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
1327 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
1328 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
Chris Lattner35e5c142006-05-05 05:51:50 +00001329 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001330 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001331 N0.getOperand(1) == N1.getOperand(1)) {
1332 SDOperand ORNode = DAG.getNode(N->getOpcode(),
1333 N0.getOperand(0).getValueType(),
1334 N0.getOperand(0), N1.getOperand(0));
1335 AddToWorkList(ORNode.Val);
1336 return DAG.getNode(N0.getOpcode(), VT, ORNode, N0.getOperand(1));
1337 }
1338
1339 return SDOperand();
1340}
1341
Nate Begeman83e75ec2005-09-06 04:43:02 +00001342SDOperand DAGCombiner::visitAND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001343 SDOperand N0 = N->getOperand(0);
1344 SDOperand N1 = N->getOperand(1);
Nate Begemanfb7217b2006-02-17 19:54:08 +00001345 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001346 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1347 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001348 MVT::ValueType VT = N1.getValueType();
1349
1350 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001351 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001352 return DAG.getNode(ISD::AND, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001353 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001354 if (N0C && !N1C)
1355 return DAG.getNode(ISD::AND, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001356 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001357 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001358 return N0;
1359 // if (and x, c) is known to be zero, return 0
Dan Gohmanea859be2007-06-22 14:59:07 +00001360 if (N1C && DAG.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001361 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +00001362 // reassociate and
1363 SDOperand RAND = ReassociateOps(ISD::AND, N0, N1);
1364 if (RAND.Val != 0)
1365 return RAND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001366 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
Nate Begeman5dc7e862005-11-02 18:42:59 +00001367 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001368 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Nate Begeman646d7e22005-09-02 21:18:40 +00001369 if ((ORI->getValue() & N1C->getValue()) == N1C->getValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001370 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +00001371 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
1372 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Chris Lattner1ec05d12006-03-01 21:47:21 +00001373 unsigned InMask = MVT::getIntVTBitMask(N0.getOperand(0).getValueType());
Dan Gohmanea859be2007-06-22 14:59:07 +00001374 if (DAG.MaskedValueIsZero(N0.getOperand(0),
Chris Lattner1ec05d12006-03-01 21:47:21 +00001375 ~N1C->getValue() & InMask)) {
1376 SDOperand Zext = DAG.getNode(ISD::ZERO_EXTEND, N0.getValueType(),
1377 N0.getOperand(0));
1378
1379 // Replace uses of the AND with uses of the Zero extend node.
1380 CombineTo(N, Zext);
1381
Chris Lattner3603cd62006-02-02 07:17:31 +00001382 // We actually want to replace all uses of the any_extend with the
1383 // zero_extend, to avoid duplicating things. This will later cause this
1384 // AND to be folded.
Chris Lattner1ec05d12006-03-01 21:47:21 +00001385 CombineTo(N0.Val, Zext);
Chris Lattnerfedced72006-04-20 23:55:59 +00001386 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner3603cd62006-02-02 07:17:31 +00001387 }
1388 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001389 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
1390 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1391 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1392 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1393
1394 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1395 MVT::isInteger(LL.getValueType())) {
1396 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
1397 if (cast<ConstantSDNode>(LR)->getValue() == 0 && Op1 == ISD::SETEQ) {
1398 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001399 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001400 return DAG.getSetCC(VT, ORNode, LR, Op1);
1401 }
1402 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
1403 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
1404 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001405 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001406 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1407 }
1408 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
1409 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
1410 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001411 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001412 return DAG.getSetCC(VT, ORNode, LR, Op1);
1413 }
1414 }
1415 // canonicalize equivalent to ll == rl
1416 if (LL == RR && LR == RL) {
1417 Op1 = ISD::getSetCCSwappedOperands(Op1);
1418 std::swap(RL, RR);
1419 }
1420 if (LL == RL && LR == RR) {
1421 bool isInteger = MVT::isInteger(LL.getValueType());
1422 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
1423 if (Result != ISD::SETCC_INVALID)
1424 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1425 }
1426 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001427
1428 // Simplify: and (op x...), (op y...) -> (op (and x, y))
1429 if (N0.getOpcode() == N1.getOpcode()) {
1430 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1431 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001432 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001433
Nate Begemande996292006-02-03 22:24:05 +00001434 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
1435 // fold (and (sra)) -> (and (srl)) when possible.
Chris Lattner6ea2dee2006-03-25 22:19:00 +00001436 if (!MVT::isVector(VT) &&
1437 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001438 return SDOperand(N, 0);
Nate Begemanded49632005-10-13 03:11:28 +00001439 // fold (zext_inreg (extload x)) -> (zextload x)
Evan Cheng83060c52007-03-07 08:07:03 +00001440 if (ISD::isEXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val)) {
Evan Cheng466685d2006-10-09 20:57:25 +00001441 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00001442 MVT::ValueType EVT = LN0->getLoadedVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001443 // If we zero all the possible extended bits, then we can turn this into
1444 // a zextload if we are running before legalize or the operation is legal.
Dan Gohmanea859be2007-06-22 14:59:07 +00001445 if (DAG.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Evan Chengc5484282006-10-04 00:56:09 +00001446 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00001447 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
1448 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00001449 LN0->getSrcValueOffset(), EVT,
1450 LN0->isVolatile(),
1451 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00001452 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001453 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001454 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001455 }
1456 }
1457 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Evan Cheng83060c52007-03-07 08:07:03 +00001458 if (ISD::isSEXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val) &&
1459 N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001460 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00001461 MVT::ValueType EVT = LN0->getLoadedVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001462 // If we zero all the possible extended bits, then we can turn this into
1463 // a zextload if we are running before legalize or the operation is legal.
Dan Gohmanea859be2007-06-22 14:59:07 +00001464 if (DAG.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Evan Chengc5484282006-10-04 00:56:09 +00001465 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00001466 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
1467 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00001468 LN0->getSrcValueOffset(), EVT,
1469 LN0->isVolatile(),
1470 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00001471 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001472 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001473 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001474 }
1475 }
Chris Lattner15045b62006-02-28 06:35:35 +00001476
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001477 // fold (and (load x), 255) -> (zextload x, i8)
1478 // fold (and (extload x, i16), 255) -> (zextload x, i8)
Evan Cheng466685d2006-10-09 20:57:25 +00001479 if (N1C && N0.getOpcode() == ISD::LOAD) {
1480 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
1481 if (LN0->getExtensionType() != ISD::SEXTLOAD &&
Evan Cheng83060c52007-03-07 08:07:03 +00001482 LN0->getAddressingMode() == ISD::UNINDEXED &&
Evan Cheng466685d2006-10-09 20:57:25 +00001483 N0.hasOneUse()) {
1484 MVT::ValueType EVT, LoadedVT;
1485 if (N1C->getValue() == 255)
1486 EVT = MVT::i8;
1487 else if (N1C->getValue() == 65535)
1488 EVT = MVT::i16;
1489 else if (N1C->getValue() == ~0U)
1490 EVT = MVT::i32;
1491 else
1492 EVT = MVT::Other;
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001493
Evan Cheng2e49f092006-10-11 07:10:22 +00001494 LoadedVT = LN0->getLoadedVT();
Evan Cheng466685d2006-10-09 20:57:25 +00001495 if (EVT != MVT::Other && LoadedVT > EVT &&
1496 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
1497 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
1498 // For big endian targets, we need to add an offset to the pointer to
1499 // load the correct bytes. For little endian systems, we merely need to
1500 // read fewer bytes from the same pointer.
1501 unsigned PtrOff =
1502 (MVT::getSizeInBits(LoadedVT) - MVT::getSizeInBits(EVT)) / 8;
1503 SDOperand NewPtr = LN0->getBasePtr();
1504 if (!TLI.isLittleEndian())
1505 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
1506 DAG.getConstant(PtrOff, PtrType));
1507 AddToWorkList(NewPtr.Val);
1508 SDOperand Load =
1509 DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00001510 LN0->getSrcValue(), LN0->getSrcValueOffset(), EVT,
1511 LN0->isVolatile(), LN0->getAlignment());
Evan Cheng466685d2006-10-09 20:57:25 +00001512 AddToWorkList(N);
1513 CombineTo(N0.Val, Load, Load.getValue(1));
1514 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1515 }
Chris Lattner15045b62006-02-28 06:35:35 +00001516 }
1517 }
1518
Nate Begeman83e75ec2005-09-06 04:43:02 +00001519 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001520}
1521
Nate Begeman83e75ec2005-09-06 04:43:02 +00001522SDOperand DAGCombiner::visitOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001523 SDOperand N0 = N->getOperand(0);
1524 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001525 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001526 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1527 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001528 MVT::ValueType VT = N1.getValueType();
1529 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001530
1531 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001532 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001533 return DAG.getNode(ISD::OR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001534 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001535 if (N0C && !N1C)
1536 return DAG.getNode(ISD::OR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001537 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001538 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001539 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001540 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001541 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001542 return N1;
1543 // fold (or x, c) -> c iff (x & ~c) == 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001544 if (N1C &&
Dan Gohmanea859be2007-06-22 14:59:07 +00001545 DAG.MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits))))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001546 return N1;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001547 // reassociate or
1548 SDOperand ROR = ReassociateOps(ISD::OR, N0, N1);
1549 if (ROR.Val != 0)
1550 return ROR;
1551 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
1552 if (N1C && N0.getOpcode() == ISD::AND && N0.Val->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00001553 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00001554 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
1555 return DAG.getNode(ISD::AND, VT, DAG.getNode(ISD::OR, VT, N0.getOperand(0),
1556 N1),
1557 DAG.getConstant(N1C->getValue() | C1->getValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00001558 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001559 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
1560 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1561 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1562 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1563
1564 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1565 MVT::isInteger(LL.getValueType())) {
1566 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
1567 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
1568 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
1569 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
1570 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001571 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001572 return DAG.getSetCC(VT, ORNode, LR, Op1);
1573 }
1574 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
1575 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
1576 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
1577 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
1578 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001579 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001580 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1581 }
1582 }
1583 // canonicalize equivalent to ll == rl
1584 if (LL == RR && LR == RL) {
1585 Op1 = ISD::getSetCCSwappedOperands(Op1);
1586 std::swap(RL, RR);
1587 }
1588 if (LL == RL && LR == RR) {
1589 bool isInteger = MVT::isInteger(LL.getValueType());
1590 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
1591 if (Result != ISD::SETCC_INVALID)
1592 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1593 }
1594 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001595
1596 // Simplify: or (op x...), (op y...) -> (op (or x, y))
1597 if (N0.getOpcode() == N1.getOpcode()) {
1598 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1599 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001600 }
Chris Lattner516b9622006-09-14 20:50:57 +00001601
Chris Lattner1ec72732006-09-14 21:11:37 +00001602 // (X & C1) | (Y & C2) -> (X|Y) & C3 if possible.
1603 if (N0.getOpcode() == ISD::AND &&
1604 N1.getOpcode() == ISD::AND &&
1605 N0.getOperand(1).getOpcode() == ISD::Constant &&
1606 N1.getOperand(1).getOpcode() == ISD::Constant &&
1607 // Don't increase # computations.
1608 (N0.Val->hasOneUse() || N1.Val->hasOneUse())) {
1609 // We can only do this xform if we know that bits from X that are set in C2
1610 // but not in C1 are already zero. Likewise for Y.
1611 uint64_t LHSMask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1612 uint64_t RHSMask = cast<ConstantSDNode>(N1.getOperand(1))->getValue();
1613
Dan Gohmanea859be2007-06-22 14:59:07 +00001614 if (DAG.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) &&
1615 DAG.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) {
Chris Lattner1ec72732006-09-14 21:11:37 +00001616 SDOperand X =DAG.getNode(ISD::OR, VT, N0.getOperand(0), N1.getOperand(0));
1617 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(LHSMask|RHSMask, VT));
1618 }
1619 }
1620
1621
Chris Lattner516b9622006-09-14 20:50:57 +00001622 // See if this is some rotate idiom.
1623 if (SDNode *Rot = MatchRotate(N0, N1))
1624 return SDOperand(Rot, 0);
Chris Lattner35e5c142006-05-05 05:51:50 +00001625
Nate Begeman83e75ec2005-09-06 04:43:02 +00001626 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001627}
1628
Chris Lattner516b9622006-09-14 20:50:57 +00001629
1630/// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present.
1631static bool MatchRotateHalf(SDOperand Op, SDOperand &Shift, SDOperand &Mask) {
1632 if (Op.getOpcode() == ISD::AND) {
Reid Spencer3ed469c2006-11-02 20:25:50 +00001633 if (isa<ConstantSDNode>(Op.getOperand(1))) {
Chris Lattner516b9622006-09-14 20:50:57 +00001634 Mask = Op.getOperand(1);
1635 Op = Op.getOperand(0);
1636 } else {
1637 return false;
1638 }
1639 }
1640
1641 if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) {
1642 Shift = Op;
1643 return true;
1644 }
1645 return false;
1646}
1647
1648
1649// MatchRotate - Handle an 'or' of two operands. If this is one of the many
1650// idioms for rotate, and if the target supports rotation instructions, generate
1651// a rot[lr].
1652SDNode *DAGCombiner::MatchRotate(SDOperand LHS, SDOperand RHS) {
1653 // Must be a legal type. Expanded an promoted things won't work with rotates.
1654 MVT::ValueType VT = LHS.getValueType();
1655 if (!TLI.isTypeLegal(VT)) return 0;
1656
1657 // The target must have at least one rotate flavor.
1658 bool HasROTL = TLI.isOperationLegal(ISD::ROTL, VT);
1659 bool HasROTR = TLI.isOperationLegal(ISD::ROTR, VT);
1660 if (!HasROTL && !HasROTR) return 0;
1661
1662 // Match "(X shl/srl V1) & V2" where V2 may not be present.
1663 SDOperand LHSShift; // The shift.
1664 SDOperand LHSMask; // AND value if any.
1665 if (!MatchRotateHalf(LHS, LHSShift, LHSMask))
1666 return 0; // Not part of a rotate.
1667
1668 SDOperand RHSShift; // The shift.
1669 SDOperand RHSMask; // AND value if any.
1670 if (!MatchRotateHalf(RHS, RHSShift, RHSMask))
1671 return 0; // Not part of a rotate.
1672
1673 if (LHSShift.getOperand(0) != RHSShift.getOperand(0))
1674 return 0; // Not shifting the same value.
1675
1676 if (LHSShift.getOpcode() == RHSShift.getOpcode())
1677 return 0; // Shifts must disagree.
1678
1679 // Canonicalize shl to left side in a shl/srl pair.
1680 if (RHSShift.getOpcode() == ISD::SHL) {
1681 std::swap(LHS, RHS);
1682 std::swap(LHSShift, RHSShift);
1683 std::swap(LHSMask , RHSMask );
1684 }
1685
1686 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Scott Michelc9dc1142007-04-02 21:36:32 +00001687 SDOperand LHSShiftArg = LHSShift.getOperand(0);
1688 SDOperand LHSShiftAmt = LHSShift.getOperand(1);
1689 SDOperand RHSShiftAmt = RHSShift.getOperand(1);
Chris Lattner516b9622006-09-14 20:50:57 +00001690
1691 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
1692 // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2)
Scott Michelc9dc1142007-04-02 21:36:32 +00001693 if (LHSShiftAmt.getOpcode() == ISD::Constant &&
1694 RHSShiftAmt.getOpcode() == ISD::Constant) {
1695 uint64_t LShVal = cast<ConstantSDNode>(LHSShiftAmt)->getValue();
1696 uint64_t RShVal = cast<ConstantSDNode>(RHSShiftAmt)->getValue();
Chris Lattner516b9622006-09-14 20:50:57 +00001697 if ((LShVal + RShVal) != OpSizeInBits)
1698 return 0;
1699
1700 SDOperand Rot;
1701 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00001702 Rot = DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00001703 else
Scott Michelc9dc1142007-04-02 21:36:32 +00001704 Rot = DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00001705
1706 // If there is an AND of either shifted operand, apply it to the result.
1707 if (LHSMask.Val || RHSMask.Val) {
1708 uint64_t Mask = MVT::getIntVTBitMask(VT);
1709
1710 if (LHSMask.Val) {
1711 uint64_t RHSBits = (1ULL << LShVal)-1;
1712 Mask &= cast<ConstantSDNode>(LHSMask)->getValue() | RHSBits;
1713 }
1714 if (RHSMask.Val) {
1715 uint64_t LHSBits = ~((1ULL << (OpSizeInBits-RShVal))-1);
1716 Mask &= cast<ConstantSDNode>(RHSMask)->getValue() | LHSBits;
1717 }
1718
1719 Rot = DAG.getNode(ISD::AND, VT, Rot, DAG.getConstant(Mask, VT));
1720 }
1721
1722 return Rot.Val;
1723 }
1724
1725 // If there is a mask here, and we have a variable shift, we can't be sure
1726 // that we're masking out the right stuff.
1727 if (LHSMask.Val || RHSMask.Val)
1728 return 0;
1729
1730 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
1731 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotr x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00001732 if (RHSShiftAmt.getOpcode() == ISD::SUB &&
1733 LHSShiftAmt == RHSShiftAmt.getOperand(1)) {
Chris Lattner516b9622006-09-14 20:50:57 +00001734 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00001735 dyn_cast<ConstantSDNode>(RHSShiftAmt.getOperand(0))) {
Chris Lattner516b9622006-09-14 20:50:57 +00001736 if (SUBC->getValue() == OpSizeInBits)
1737 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00001738 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
Chris Lattner516b9622006-09-14 20:50:57 +00001739 else
Scott Michelc9dc1142007-04-02 21:36:32 +00001740 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).Val;
Chris Lattner516b9622006-09-14 20:50:57 +00001741 }
1742 }
1743
1744 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
1745 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotl x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00001746 if (LHSShiftAmt.getOpcode() == ISD::SUB &&
1747 RHSShiftAmt == LHSShiftAmt.getOperand(1)) {
Chris Lattner516b9622006-09-14 20:50:57 +00001748 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00001749 dyn_cast<ConstantSDNode>(LHSShiftAmt.getOperand(0))) {
Chris Lattner516b9622006-09-14 20:50:57 +00001750 if (SUBC->getValue() == OpSizeInBits)
1751 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00001752 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
Chris Lattner516b9622006-09-14 20:50:57 +00001753 else
Scott Michelc9dc1142007-04-02 21:36:32 +00001754 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).Val;
1755 }
1756 }
1757
1758 // Look for sign/zext/any-extended cases:
1759 if ((LHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
1760 || LHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
1761 || LHSShiftAmt.getOpcode() == ISD::ANY_EXTEND) &&
1762 (RHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
1763 || RHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
1764 || RHSShiftAmt.getOpcode() == ISD::ANY_EXTEND)) {
1765 SDOperand LExtOp0 = LHSShiftAmt.getOperand(0);
1766 SDOperand RExtOp0 = RHSShiftAmt.getOperand(0);
1767 if (RExtOp0.getOpcode() == ISD::SUB &&
1768 RExtOp0.getOperand(1) == LExtOp0) {
1769 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
1770 // (rotr x, y)
1771 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
1772 // (rotl x, (sub 32, y))
1773 if (ConstantSDNode *SUBC = cast<ConstantSDNode>(RExtOp0.getOperand(0))) {
1774 if (SUBC->getValue() == OpSizeInBits) {
1775 if (HasROTL)
1776 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
1777 else
1778 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).Val;
1779 }
1780 }
1781 } else if (LExtOp0.getOpcode() == ISD::SUB &&
1782 RExtOp0 == LExtOp0.getOperand(1)) {
1783 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext r))) ->
1784 // (rotl x, y)
1785 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext r))) ->
1786 // (rotr x, (sub 32, y))
1787 if (ConstantSDNode *SUBC = cast<ConstantSDNode>(LExtOp0.getOperand(0))) {
1788 if (SUBC->getValue() == OpSizeInBits) {
1789 if (HasROTL)
1790 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, RHSShiftAmt).Val;
1791 else
1792 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
1793 }
1794 }
Chris Lattner516b9622006-09-14 20:50:57 +00001795 }
1796 }
1797
1798 return 0;
1799}
1800
1801
Nate Begeman83e75ec2005-09-06 04:43:02 +00001802SDOperand DAGCombiner::visitXOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001803 SDOperand N0 = N->getOperand(0);
1804 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001805 SDOperand LHS, RHS, CC;
1806 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1807 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001808 MVT::ValueType VT = N0.getValueType();
1809
1810 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001811 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001812 return DAG.getNode(ISD::XOR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001813 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001814 if (N0C && !N1C)
1815 return DAG.getNode(ISD::XOR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001816 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001817 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001818 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001819 // reassociate xor
1820 SDOperand RXOR = ReassociateOps(ISD::XOR, N0, N1);
1821 if (RXOR.Val != 0)
1822 return RXOR;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001823 // fold !(x cc y) -> (x !cc y)
Nate Begeman646d7e22005-09-02 21:18:40 +00001824 if (N1C && N1C->getValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
1825 bool isInt = MVT::isInteger(LHS.getValueType());
1826 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
1827 isInt);
1828 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001829 return DAG.getSetCC(VT, LHS, RHS, NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00001830 if (N0.getOpcode() == ISD::SELECT_CC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001831 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00001832 assert(0 && "Unhandled SetCC Equivalent!");
1833 abort();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001834 }
Nate Begeman99801192005-09-07 23:25:52 +00001835 // fold !(x or y) -> (!x and !y) iff x or y are setcc
Chris Lattner734c91d2006-11-10 21:37:15 +00001836 if (N1C && N1C->getValue() == 1 && VT == MVT::i1 &&
Nate Begeman99801192005-09-07 23:25:52 +00001837 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001838 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00001839 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
1840 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001841 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1842 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00001843 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00001844 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001845 }
1846 }
Nate Begeman99801192005-09-07 23:25:52 +00001847 // fold !(x or y) -> (!x and !y) iff x or y are constants
1848 if (N1C && N1C->isAllOnesValue() &&
1849 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001850 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00001851 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
1852 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001853 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1854 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00001855 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00001856 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001857 }
1858 }
Nate Begeman223df222005-09-08 20:18:10 +00001859 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
1860 if (N1C && N0.getOpcode() == ISD::XOR) {
1861 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
1862 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
1863 if (N00C)
1864 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
1865 DAG.getConstant(N1C->getValue()^N00C->getValue(), VT));
1866 if (N01C)
1867 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
1868 DAG.getConstant(N1C->getValue()^N01C->getValue(), VT));
1869 }
1870 // fold (xor x, x) -> 0
Chris Lattner4fbdd592006-03-28 19:11:05 +00001871 if (N0 == N1) {
1872 if (!MVT::isVector(VT)) {
1873 return DAG.getConstant(0, VT);
1874 } else if (!AfterLegalize || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) {
1875 // Produce a vector of zeros.
Dan Gohman51eaa862007-06-14 22:58:02 +00001876 SDOperand El = DAG.getConstant(0, MVT::getVectorElementType(VT));
Chris Lattner4fbdd592006-03-28 19:11:05 +00001877 std::vector<SDOperand> Ops(MVT::getVectorNumElements(VT), El);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001878 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4fbdd592006-03-28 19:11:05 +00001879 }
1880 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001881
1882 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
1883 if (N0.getOpcode() == N1.getOpcode()) {
1884 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1885 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001886 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001887
Chris Lattner3e104b12006-04-08 04:15:24 +00001888 // Simplify the expression using non-local knowledge.
1889 if (!MVT::isVector(VT) &&
1890 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001891 return SDOperand(N, 0);
Chris Lattner3e104b12006-04-08 04:15:24 +00001892
Nate Begeman83e75ec2005-09-06 04:43:02 +00001893 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001894}
1895
Nate Begeman83e75ec2005-09-06 04:43:02 +00001896SDOperand DAGCombiner::visitSHL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001897 SDOperand N0 = N->getOperand(0);
1898 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001899 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1900 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001901 MVT::ValueType VT = N0.getValueType();
1902 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1903
1904 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001905 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001906 return DAG.getNode(ISD::SHL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001907 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001908 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001909 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001910 // fold (shl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001911 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001912 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001913 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001914 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001915 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001916 // if (shl x, c) is known to be zero, return 0
Dan Gohmanea859be2007-06-22 14:59:07 +00001917 if (DAG.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001918 return DAG.getConstant(0, VT);
Chris Lattner61a4c072007-04-18 03:06:49 +00001919 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001920 return SDOperand(N, 0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001921 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001922 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001923 N0.getOperand(1).getOpcode() == ISD::Constant) {
1924 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001925 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001926 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001927 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001928 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001929 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001930 }
1931 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
1932 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001933 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001934 N0.getOperand(1).getOpcode() == ISD::Constant) {
1935 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001936 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001937 SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
1938 DAG.getConstant(~0ULL << c1, VT));
1939 if (c2 > c1)
1940 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001941 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001942 else
Nate Begeman83e75ec2005-09-06 04:43:02 +00001943 return DAG.getNode(ISD::SRL, VT, Mask,
1944 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001945 }
1946 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001947 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +00001948 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001949 DAG.getConstant(~0ULL << N1C->getValue(), VT));
1950 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001951}
1952
Nate Begeman83e75ec2005-09-06 04:43:02 +00001953SDOperand DAGCombiner::visitSRA(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001954 SDOperand N0 = N->getOperand(0);
1955 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001956 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1957 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001958 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001959
1960 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001961 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001962 return DAG.getNode(ISD::SRA, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001963 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001964 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001965 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001966 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001967 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001968 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001969 // fold (sra x, c >= size(x)) -> undef
Nate Begemanfb7217b2006-02-17 19:54:08 +00001970 if (N1C && N1C->getValue() >= MVT::getSizeInBits(VT))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001971 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001972 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001973 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001974 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00001975 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
1976 // sext_inreg.
1977 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
1978 unsigned LowBits = MVT::getSizeInBits(VT) - (unsigned)N1C->getValue();
1979 MVT::ValueType EVT;
1980 switch (LowBits) {
1981 default: EVT = MVT::Other; break;
1982 case 1: EVT = MVT::i1; break;
1983 case 8: EVT = MVT::i8; break;
1984 case 16: EVT = MVT::i16; break;
1985 case 32: EVT = MVT::i32; break;
1986 }
1987 if (EVT > MVT::Other && TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, EVT))
1988 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
1989 DAG.getValueType(EVT));
1990 }
Chris Lattner71d9ebc2006-02-28 06:23:04 +00001991
1992 // fold (sra (sra x, c1), c2) -> (sra x, c1+c2)
1993 if (N1C && N0.getOpcode() == ISD::SRA) {
1994 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
1995 unsigned Sum = N1C->getValue() + C1->getValue();
1996 if (Sum >= MVT::getSizeInBits(VT)) Sum = MVT::getSizeInBits(VT)-1;
1997 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0),
1998 DAG.getConstant(Sum, N1C->getValueType(0)));
1999 }
2000 }
2001
Chris Lattnera8504462006-05-08 20:51:54 +00002002 // Simplify, based on bits shifted out of the LHS.
2003 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
2004 return SDOperand(N, 0);
2005
2006
Nate Begeman1d4d4142005-09-01 00:19:25 +00002007 // If the sign bit is known to be zero, switch this to a SRL.
Dan Gohmanea859be2007-06-22 14:59:07 +00002008 if (DAG.MaskedValueIsZero(N0, MVT::getIntVTSignBit(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002009 return DAG.getNode(ISD::SRL, VT, N0, N1);
2010 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002011}
2012
Nate Begeman83e75ec2005-09-06 04:43:02 +00002013SDOperand DAGCombiner::visitSRL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002014 SDOperand N0 = N->getOperand(0);
2015 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002016 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2017 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002018 MVT::ValueType VT = N0.getValueType();
2019 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
2020
2021 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002022 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002023 return DAG.getNode(ISD::SRL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002024 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002025 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002026 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002027 // fold (srl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00002028 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002029 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002030 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002031 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002032 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002033 // if (srl x, c) is known to be zero, return 0
Dan Gohmanea859be2007-06-22 14:59:07 +00002034 if (N1C && DAG.MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002035 return DAG.getConstant(0, VT);
Chris Lattnerec06e9a2007-04-18 03:05:22 +00002036
Nate Begeman1d4d4142005-09-01 00:19:25 +00002037 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002038 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002039 N0.getOperand(1).getOpcode() == ISD::Constant) {
2040 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00002041 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002042 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002043 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002044 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002045 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002046 }
Chris Lattner350bec02006-04-02 06:11:11 +00002047
Chris Lattner06afe072006-05-05 22:53:17 +00002048 // fold (srl (anyextend x), c) -> (anyextend (srl x, c))
2049 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
2050 // Shifting in all undef bits?
2051 MVT::ValueType SmallVT = N0.getOperand(0).getValueType();
2052 if (N1C->getValue() >= MVT::getSizeInBits(SmallVT))
2053 return DAG.getNode(ISD::UNDEF, VT);
2054
2055 SDOperand SmallShift = DAG.getNode(ISD::SRL, SmallVT, N0.getOperand(0), N1);
2056 AddToWorkList(SmallShift.Val);
2057 return DAG.getNode(ISD::ANY_EXTEND, VT, SmallShift);
2058 }
2059
Chris Lattner3657ffe2006-10-12 20:23:19 +00002060 // fold (srl (sra X, Y), 31) -> (srl X, 31). This srl only looks at the sign
2061 // bit, which is unmodified by sra.
2062 if (N1C && N1C->getValue()+1 == MVT::getSizeInBits(VT)) {
2063 if (N0.getOpcode() == ISD::SRA)
2064 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0), N1);
2065 }
2066
Chris Lattner350bec02006-04-02 06:11:11 +00002067 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
2068 if (N1C && N0.getOpcode() == ISD::CTLZ &&
2069 N1C->getValue() == Log2_32(MVT::getSizeInBits(VT))) {
2070 uint64_t KnownZero, KnownOne, Mask = MVT::getIntVTBitMask(VT);
Dan Gohmanea859be2007-06-22 14:59:07 +00002071 DAG.ComputeMaskedBits(N0.getOperand(0), Mask, KnownZero, KnownOne);
Chris Lattner350bec02006-04-02 06:11:11 +00002072
2073 // If any of the input bits are KnownOne, then the input couldn't be all
2074 // zeros, thus the result of the srl will always be zero.
2075 if (KnownOne) return DAG.getConstant(0, VT);
2076
2077 // If all of the bits input the to ctlz node are known to be zero, then
2078 // the result of the ctlz is "32" and the result of the shift is one.
2079 uint64_t UnknownBits = ~KnownZero & Mask;
2080 if (UnknownBits == 0) return DAG.getConstant(1, VT);
2081
2082 // Otherwise, check to see if there is exactly one bit input to the ctlz.
2083 if ((UnknownBits & (UnknownBits-1)) == 0) {
2084 // Okay, we know that only that the single bit specified by UnknownBits
2085 // could be set on input to the CTLZ node. If this bit is set, the SRL
2086 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
2087 // to an SRL,XOR pair, which is likely to simplify more.
2088 unsigned ShAmt = CountTrailingZeros_64(UnknownBits);
2089 SDOperand Op = N0.getOperand(0);
2090 if (ShAmt) {
2091 Op = DAG.getNode(ISD::SRL, VT, Op,
2092 DAG.getConstant(ShAmt, TLI.getShiftAmountTy()));
2093 AddToWorkList(Op.Val);
2094 }
2095 return DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(1, VT));
2096 }
2097 }
Chris Lattner61a4c072007-04-18 03:06:49 +00002098
2099 // fold operands of srl based on knowledge that the low bits are not
2100 // demanded.
2101 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
2102 return SDOperand(N, 0);
2103
Nate Begeman83e75ec2005-09-06 04:43:02 +00002104 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002105}
2106
Nate Begeman83e75ec2005-09-06 04:43:02 +00002107SDOperand DAGCombiner::visitCTLZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002108 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00002109 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002110
2111 // fold (ctlz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002112 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002113 return DAG.getNode(ISD::CTLZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002114 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002115}
2116
Nate Begeman83e75ec2005-09-06 04:43:02 +00002117SDOperand DAGCombiner::visitCTTZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002118 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00002119 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002120
2121 // fold (cttz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002122 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002123 return DAG.getNode(ISD::CTTZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002124 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002125}
2126
Nate Begeman83e75ec2005-09-06 04:43:02 +00002127SDOperand DAGCombiner::visitCTPOP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002128 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00002129 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002130
2131 // fold (ctpop c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002132 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002133 return DAG.getNode(ISD::CTPOP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002134 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002135}
2136
Nate Begeman452d7be2005-09-16 00:54:12 +00002137SDOperand DAGCombiner::visitSELECT(SDNode *N) {
2138 SDOperand N0 = N->getOperand(0);
2139 SDOperand N1 = N->getOperand(1);
2140 SDOperand N2 = N->getOperand(2);
2141 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2142 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2143 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
2144 MVT::ValueType VT = N->getValueType(0);
Nate Begeman44728a72005-09-19 22:34:01 +00002145
Nate Begeman452d7be2005-09-16 00:54:12 +00002146 // fold select C, X, X -> X
2147 if (N1 == N2)
2148 return N1;
2149 // fold select true, X, Y -> X
2150 if (N0C && !N0C->isNullValue())
2151 return N1;
2152 // fold select false, X, Y -> Y
2153 if (N0C && N0C->isNullValue())
2154 return N2;
2155 // fold select C, 1, X -> C | X
Nate Begeman44728a72005-09-19 22:34:01 +00002156 if (MVT::i1 == VT && N1C && N1C->getValue() == 1)
Nate Begeman452d7be2005-09-16 00:54:12 +00002157 return DAG.getNode(ISD::OR, VT, N0, N2);
2158 // fold select C, 0, X -> ~C & X
2159 // FIXME: this should check for C type == X type, not i1?
2160 if (MVT::i1 == VT && N1C && N1C->isNullValue()) {
2161 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00002162 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00002163 return DAG.getNode(ISD::AND, VT, XORNode, N2);
2164 }
2165 // fold select C, X, 1 -> ~C | X
Nate Begeman44728a72005-09-19 22:34:01 +00002166 if (MVT::i1 == VT && N2C && N2C->getValue() == 1) {
Nate Begeman452d7be2005-09-16 00:54:12 +00002167 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00002168 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00002169 return DAG.getNode(ISD::OR, VT, XORNode, N1);
2170 }
2171 // fold select C, X, 0 -> C & X
2172 // FIXME: this should check for C type == X type, not i1?
2173 if (MVT::i1 == VT && N2C && N2C->isNullValue())
2174 return DAG.getNode(ISD::AND, VT, N0, N1);
2175 // fold X ? X : Y --> X ? 1 : Y --> X | Y
2176 if (MVT::i1 == VT && N0 == N1)
2177 return DAG.getNode(ISD::OR, VT, N0, N2);
2178 // fold X ? Y : X --> X ? Y : 0 --> X & Y
2179 if (MVT::i1 == VT && N0 == N2)
2180 return DAG.getNode(ISD::AND, VT, N0, N1);
Chris Lattner729c6d12006-05-27 00:43:02 +00002181
Chris Lattner40c62d52005-10-18 06:04:22 +00002182 // If we can fold this based on the true/false value, do so.
2183 if (SimplifySelectOps(N, N1, N2))
Chris Lattner729c6d12006-05-27 00:43:02 +00002184 return SDOperand(N, 0); // Don't revisit N.
2185
Nate Begeman44728a72005-09-19 22:34:01 +00002186 // fold selects based on a setcc into other things, such as min/max/abs
2187 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman750ac1b2006-02-01 07:19:44 +00002188 // FIXME:
2189 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
2190 // having to say they don't support SELECT_CC on every type the DAG knows
2191 // about, since there is no way to mark an opcode illegal at all value types
2192 if (TLI.isOperationLegal(ISD::SELECT_CC, MVT::Other))
2193 return DAG.getNode(ISD::SELECT_CC, VT, N0.getOperand(0), N0.getOperand(1),
2194 N1, N2, N0.getOperand(2));
2195 else
2196 return SimplifySelect(N0, N1, N2);
Nate Begeman452d7be2005-09-16 00:54:12 +00002197 return SDOperand();
2198}
2199
2200SDOperand DAGCombiner::visitSELECT_CC(SDNode *N) {
Nate Begeman44728a72005-09-19 22:34:01 +00002201 SDOperand N0 = N->getOperand(0);
2202 SDOperand N1 = N->getOperand(1);
2203 SDOperand N2 = N->getOperand(2);
2204 SDOperand N3 = N->getOperand(3);
2205 SDOperand N4 = N->getOperand(4);
Nate Begeman44728a72005-09-19 22:34:01 +00002206 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
2207
Nate Begeman44728a72005-09-19 22:34:01 +00002208 // fold select_cc lhs, rhs, x, x, cc -> x
2209 if (N2 == N3)
2210 return N2;
Chris Lattner40c62d52005-10-18 06:04:22 +00002211
Chris Lattner5f42a242006-09-20 06:19:26 +00002212 // Determine if the condition we're dealing with is constant
2213 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
Chris Lattner30f73e72006-10-14 03:52:46 +00002214 if (SCC.Val) AddToWorkList(SCC.Val);
Chris Lattner5f42a242006-09-20 06:19:26 +00002215
2216 if (ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val)) {
2217 if (SCCC->getValue())
2218 return N2; // cond always true -> true val
2219 else
2220 return N3; // cond always false -> false val
2221 }
2222
2223 // Fold to a simpler select_cc
2224 if (SCC.Val && SCC.getOpcode() == ISD::SETCC)
2225 return DAG.getNode(ISD::SELECT_CC, N2.getValueType(),
2226 SCC.getOperand(0), SCC.getOperand(1), N2, N3,
2227 SCC.getOperand(2));
2228
Chris Lattner40c62d52005-10-18 06:04:22 +00002229 // If we can fold this based on the true/false value, do so.
2230 if (SimplifySelectOps(N, N2, N3))
Chris Lattner729c6d12006-05-27 00:43:02 +00002231 return SDOperand(N, 0); // Don't revisit N.
Chris Lattner40c62d52005-10-18 06:04:22 +00002232
Nate Begeman44728a72005-09-19 22:34:01 +00002233 // fold select_cc into other things, such as min/max/abs
2234 return SimplifySelectCC(N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00002235}
2236
2237SDOperand DAGCombiner::visitSETCC(SDNode *N) {
2238 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
2239 cast<CondCodeSDNode>(N->getOperand(2))->get());
2240}
2241
Nate Begeman83e75ec2005-09-06 04:43:02 +00002242SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002243 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002244 MVT::ValueType VT = N->getValueType(0);
2245
Nate Begeman1d4d4142005-09-01 00:19:25 +00002246 // fold (sext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00002247 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002248 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0);
Chris Lattner310b5782006-05-06 23:06:26 +00002249
Nate Begeman1d4d4142005-09-01 00:19:25 +00002250 // fold (sext (sext x)) -> (sext x)
Chris Lattner310b5782006-05-06 23:06:26 +00002251 // fold (sext (aext x)) -> (sext x)
2252 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002253 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
Chris Lattner310b5782006-05-06 23:06:26 +00002254
Evan Chengc88138f2007-03-22 01:54:19 +00002255 // fold (sext (truncate (load x))) -> (sext (smaller load x))
2256 // fold (sext (truncate (srl (load x), c))) -> (sext (smaller load (x+c/n)))
Chris Lattner22558872007-02-26 03:13:59 +00002257 if (N0.getOpcode() == ISD::TRUNCATE) {
Evan Chengc88138f2007-03-22 01:54:19 +00002258 SDOperand NarrowLoad = ReduceLoadWidth(N0.Val);
Evan Cheng0b063de2007-03-23 02:16:52 +00002259 if (NarrowLoad.Val) {
2260 if (NarrowLoad.Val != N0.Val)
2261 CombineTo(N0.Val, NarrowLoad);
2262 return DAG.getNode(ISD::SIGN_EXTEND, VT, NarrowLoad);
2263 }
Evan Chengc88138f2007-03-22 01:54:19 +00002264 }
2265
2266 // See if the value being truncated is already sign extended. If so, just
2267 // eliminate the trunc/sext pair.
2268 if (N0.getOpcode() == ISD::TRUNCATE) {
Chris Lattner6007b842006-09-21 06:00:20 +00002269 SDOperand Op = N0.getOperand(0);
Chris Lattner22558872007-02-26 03:13:59 +00002270 unsigned OpBits = MVT::getSizeInBits(Op.getValueType());
2271 unsigned MidBits = MVT::getSizeInBits(N0.getValueType());
2272 unsigned DestBits = MVT::getSizeInBits(VT);
Dan Gohmanea859be2007-06-22 14:59:07 +00002273 unsigned NumSignBits = DAG.ComputeNumSignBits(Op);
Chris Lattner22558872007-02-26 03:13:59 +00002274
2275 if (OpBits == DestBits) {
2276 // Op is i32, Mid is i8, and Dest is i32. If Op has more than 24 sign
2277 // bits, it is already ready.
2278 if (NumSignBits > DestBits-MidBits)
2279 return Op;
2280 } else if (OpBits < DestBits) {
2281 // Op is i32, Mid is i8, and Dest is i64. If Op has more than 24 sign
2282 // bits, just sext from i32.
2283 if (NumSignBits > OpBits-MidBits)
2284 return DAG.getNode(ISD::SIGN_EXTEND, VT, Op);
2285 } else {
2286 // Op is i64, Mid is i8, and Dest is i32. If Op has more than 56 sign
2287 // bits, just truncate to i32.
2288 if (NumSignBits > OpBits-MidBits)
2289 return DAG.getNode(ISD::TRUNCATE, VT, Op);
Chris Lattner6007b842006-09-21 06:00:20 +00002290 }
Chris Lattner22558872007-02-26 03:13:59 +00002291
2292 // fold (sext (truncate x)) -> (sextinreg x).
2293 if (!AfterLegalize || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG,
2294 N0.getValueType())) {
2295 if (Op.getValueType() < VT)
2296 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
2297 else if (Op.getValueType() > VT)
2298 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
2299 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, Op,
2300 DAG.getValueType(N0.getValueType()));
2301 }
Chris Lattner6007b842006-09-21 06:00:20 +00002302 }
Chris Lattner310b5782006-05-06 23:06:26 +00002303
Evan Cheng110dec22005-12-14 02:19:23 +00002304 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00002305 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00002306 (!AfterLegalize||TLI.isLoadXLegal(ISD::SEXTLOAD, N0.getValueType()))){
Evan Cheng466685d2006-10-09 20:57:25 +00002307 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2308 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2309 LN0->getBasePtr(), LN0->getSrcValue(),
2310 LN0->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002311 N0.getValueType(),
2312 LN0->isVolatile());
Chris Lattnerd4771842005-12-14 19:25:30 +00002313 CombineTo(N, ExtLoad);
Chris Lattnerf9884052005-10-13 21:52:31 +00002314 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2315 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002316 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begeman3df4d522005-10-12 20:40:40 +00002317 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002318
2319 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
2320 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00002321 if ((ISD::isSEXTLoad(N0.Val) || ISD::isEXTLoad(N0.Val)) &&
2322 ISD::isUNINDEXEDLoad(N0.Val) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002323 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00002324 MVT::ValueType EVT = LN0->getLoadedVT();
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00002325 if (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT)) {
2326 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2327 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002328 LN0->getSrcValueOffset(), EVT,
2329 LN0->isVolatile(),
2330 LN0->getAlignment());
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00002331 CombineTo(N, ExtLoad);
2332 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2333 ExtLoad.getValue(1));
2334 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2335 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002336 }
2337
Chris Lattner20a35c32007-04-11 05:32:27 +00002338 // sext(setcc x,y,cc) -> select_cc x, y, -1, 0, cc
2339 if (N0.getOpcode() == ISD::SETCC) {
2340 SDOperand SCC =
Chris Lattner1eba01e2007-04-11 06:50:51 +00002341 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
2342 DAG.getConstant(~0ULL, VT), DAG.getConstant(0, VT),
2343 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
2344 if (SCC.Val) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00002345 }
2346
Nate Begeman83e75ec2005-09-06 04:43:02 +00002347 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002348}
2349
Nate Begeman83e75ec2005-09-06 04:43:02 +00002350SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002351 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002352 MVT::ValueType VT = N->getValueType(0);
2353
Nate Begeman1d4d4142005-09-01 00:19:25 +00002354 // fold (zext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00002355 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002356 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002357 // fold (zext (zext x)) -> (zext x)
Chris Lattner310b5782006-05-06 23:06:26 +00002358 // fold (zext (aext x)) -> (zext x)
2359 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002360 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
Chris Lattner6007b842006-09-21 06:00:20 +00002361
Evan Chengc88138f2007-03-22 01:54:19 +00002362 // fold (zext (truncate (load x))) -> (zext (smaller load x))
2363 // fold (zext (truncate (srl (load x), c))) -> (zext (small load (x+c/n)))
Dale Johannesen2041a0e2007-03-30 21:38:07 +00002364 if (N0.getOpcode() == ISD::TRUNCATE) {
Evan Chengc88138f2007-03-22 01:54:19 +00002365 SDOperand NarrowLoad = ReduceLoadWidth(N0.Val);
Evan Cheng0b063de2007-03-23 02:16:52 +00002366 if (NarrowLoad.Val) {
2367 if (NarrowLoad.Val != N0.Val)
2368 CombineTo(N0.Val, NarrowLoad);
2369 return DAG.getNode(ISD::ZERO_EXTEND, VT, NarrowLoad);
2370 }
Evan Chengc88138f2007-03-22 01:54:19 +00002371 }
2372
Chris Lattner6007b842006-09-21 06:00:20 +00002373 // fold (zext (truncate x)) -> (and x, mask)
2374 if (N0.getOpcode() == ISD::TRUNCATE &&
2375 (!AfterLegalize || TLI.isOperationLegal(ISD::AND, VT))) {
2376 SDOperand Op = N0.getOperand(0);
2377 if (Op.getValueType() < VT) {
2378 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
2379 } else if (Op.getValueType() > VT) {
2380 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
2381 }
2382 return DAG.getZeroExtendInReg(Op, N0.getValueType());
2383 }
2384
Chris Lattner111c2282006-09-21 06:14:31 +00002385 // fold (zext (and (trunc x), cst)) -> (and x, cst).
2386 if (N0.getOpcode() == ISD::AND &&
2387 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
2388 N0.getOperand(1).getOpcode() == ISD::Constant) {
2389 SDOperand X = N0.getOperand(0).getOperand(0);
2390 if (X.getValueType() < VT) {
2391 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
2392 } else if (X.getValueType() > VT) {
2393 X = DAG.getNode(ISD::TRUNCATE, VT, X);
2394 }
2395 uint64_t Mask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
2396 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
2397 }
2398
Evan Cheng110dec22005-12-14 02:19:23 +00002399 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00002400 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00002401 (!AfterLegalize||TLI.isLoadXLegal(ISD::ZEXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00002402 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2403 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
2404 LN0->getBasePtr(), LN0->getSrcValue(),
2405 LN0->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002406 N0.getValueType(),
2407 LN0->isVolatile(),
2408 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00002409 CombineTo(N, ExtLoad);
Evan Cheng110dec22005-12-14 02:19:23 +00002410 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2411 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002412 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng110dec22005-12-14 02:19:23 +00002413 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002414
2415 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
2416 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00002417 if ((ISD::isZEXTLoad(N0.Val) || ISD::isEXTLoad(N0.Val)) &&
2418 ISD::isUNINDEXEDLoad(N0.Val) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002419 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00002420 MVT::ValueType EVT = LN0->getLoadedVT();
Evan Cheng466685d2006-10-09 20:57:25 +00002421 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
2422 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002423 LN0->getSrcValueOffset(), EVT,
2424 LN0->isVolatile(),
2425 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00002426 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002427 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2428 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002429 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002430 }
Chris Lattner20a35c32007-04-11 05:32:27 +00002431
2432 // zext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
2433 if (N0.getOpcode() == ISD::SETCC) {
2434 SDOperand SCC =
2435 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
2436 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattner1eba01e2007-04-11 06:50:51 +00002437 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
2438 if (SCC.Val) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00002439 }
2440
Nate Begeman83e75ec2005-09-06 04:43:02 +00002441 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002442}
2443
Chris Lattner5ffc0662006-05-05 05:58:59 +00002444SDOperand DAGCombiner::visitANY_EXTEND(SDNode *N) {
2445 SDOperand N0 = N->getOperand(0);
Chris Lattner5ffc0662006-05-05 05:58:59 +00002446 MVT::ValueType VT = N->getValueType(0);
2447
2448 // fold (aext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00002449 if (isa<ConstantSDNode>(N0))
Chris Lattner5ffc0662006-05-05 05:58:59 +00002450 return DAG.getNode(ISD::ANY_EXTEND, VT, N0);
2451 // fold (aext (aext x)) -> (aext x)
2452 // fold (aext (zext x)) -> (zext x)
2453 // fold (aext (sext x)) -> (sext x)
2454 if (N0.getOpcode() == ISD::ANY_EXTEND ||
2455 N0.getOpcode() == ISD::ZERO_EXTEND ||
2456 N0.getOpcode() == ISD::SIGN_EXTEND)
2457 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
2458
Evan Chengc88138f2007-03-22 01:54:19 +00002459 // fold (aext (truncate (load x))) -> (aext (smaller load x))
2460 // fold (aext (truncate (srl (load x), c))) -> (aext (small load (x+c/n)))
2461 if (N0.getOpcode() == ISD::TRUNCATE) {
2462 SDOperand NarrowLoad = ReduceLoadWidth(N0.Val);
Evan Cheng0b063de2007-03-23 02:16:52 +00002463 if (NarrowLoad.Val) {
2464 if (NarrowLoad.Val != N0.Val)
2465 CombineTo(N0.Val, NarrowLoad);
2466 return DAG.getNode(ISD::ANY_EXTEND, VT, NarrowLoad);
2467 }
Evan Chengc88138f2007-03-22 01:54:19 +00002468 }
2469
Chris Lattner84750582006-09-20 06:29:17 +00002470 // fold (aext (truncate x))
2471 if (N0.getOpcode() == ISD::TRUNCATE) {
2472 SDOperand TruncOp = N0.getOperand(0);
2473 if (TruncOp.getValueType() == VT)
2474 return TruncOp; // x iff x size == zext size.
2475 if (TruncOp.getValueType() > VT)
2476 return DAG.getNode(ISD::TRUNCATE, VT, TruncOp);
2477 return DAG.getNode(ISD::ANY_EXTEND, VT, TruncOp);
2478 }
Chris Lattner0e4b9222006-09-21 06:40:43 +00002479
2480 // fold (aext (and (trunc x), cst)) -> (and x, cst).
2481 if (N0.getOpcode() == ISD::AND &&
2482 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
2483 N0.getOperand(1).getOpcode() == ISD::Constant) {
2484 SDOperand X = N0.getOperand(0).getOperand(0);
2485 if (X.getValueType() < VT) {
2486 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
2487 } else if (X.getValueType() > VT) {
2488 X = DAG.getNode(ISD::TRUNCATE, VT, X);
2489 }
2490 uint64_t Mask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
2491 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
2492 }
2493
Chris Lattner5ffc0662006-05-05 05:58:59 +00002494 // fold (aext (load x)) -> (aext (truncate (extload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00002495 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00002496 (!AfterLegalize||TLI.isLoadXLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00002497 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2498 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
2499 LN0->getBasePtr(), LN0->getSrcValue(),
2500 LN0->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002501 N0.getValueType(),
2502 LN0->isVolatile(),
2503 LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00002504 CombineTo(N, ExtLoad);
2505 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2506 ExtLoad.getValue(1));
2507 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2508 }
2509
2510 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
2511 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
2512 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00002513 if (N0.getOpcode() == ISD::LOAD &&
2514 !ISD::isNON_EXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val) &&
Evan Cheng466685d2006-10-09 20:57:25 +00002515 N0.hasOneUse()) {
2516 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00002517 MVT::ValueType EVT = LN0->getLoadedVT();
Evan Cheng466685d2006-10-09 20:57:25 +00002518 SDOperand ExtLoad = DAG.getExtLoad(LN0->getExtensionType(), VT,
2519 LN0->getChain(), LN0->getBasePtr(),
2520 LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002521 LN0->getSrcValueOffset(), EVT,
2522 LN0->isVolatile(),
2523 LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00002524 CombineTo(N, ExtLoad);
2525 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2526 ExtLoad.getValue(1));
2527 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2528 }
Chris Lattner20a35c32007-04-11 05:32:27 +00002529
2530 // aext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
2531 if (N0.getOpcode() == ISD::SETCC) {
2532 SDOperand SCC =
Chris Lattner1eba01e2007-04-11 06:50:51 +00002533 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
2534 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattnerc24bbad2007-04-11 16:51:53 +00002535 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Chris Lattner1eba01e2007-04-11 06:50:51 +00002536 if (SCC.Val)
Chris Lattnerc56a81d2007-04-11 06:43:25 +00002537 return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00002538 }
2539
Chris Lattner5ffc0662006-05-05 05:58:59 +00002540 return SDOperand();
2541}
2542
Evan Chengc88138f2007-03-22 01:54:19 +00002543/// ReduceLoadWidth - If the result of a wider load is shifted to right of N
2544/// bits and then truncated to a narrower type and where N is a multiple
2545/// of number of bits of the narrower type, transform it to a narrower load
2546/// from address + N / num of bits of new type. If the result is to be
2547/// extended, also fold the extension to form a extending load.
2548SDOperand DAGCombiner::ReduceLoadWidth(SDNode *N) {
2549 unsigned Opc = N->getOpcode();
2550 ISD::LoadExtType ExtType = ISD::NON_EXTLOAD;
2551 SDOperand N0 = N->getOperand(0);
2552 MVT::ValueType VT = N->getValueType(0);
2553 MVT::ValueType EVT = N->getValueType(0);
2554
Evan Chenge177e302007-03-23 22:13:36 +00002555 // Special case: SIGN_EXTEND_INREG is basically truncating to EVT then
2556 // extended to VT.
Evan Chengc88138f2007-03-22 01:54:19 +00002557 if (Opc == ISD::SIGN_EXTEND_INREG) {
2558 ExtType = ISD::SEXTLOAD;
2559 EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Evan Chenge177e302007-03-23 22:13:36 +00002560 if (AfterLegalize && !TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))
2561 return SDOperand();
Evan Chengc88138f2007-03-22 01:54:19 +00002562 }
2563
2564 unsigned EVTBits = MVT::getSizeInBits(EVT);
2565 unsigned ShAmt = 0;
Evan Chengb37b80c2007-03-23 20:55:21 +00002566 bool CombineSRL = false;
Evan Chengc88138f2007-03-22 01:54:19 +00002567 if (N0.getOpcode() == ISD::SRL && N0.hasOneUse()) {
2568 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2569 ShAmt = N01->getValue();
2570 // Is the shift amount a multiple of size of VT?
2571 if ((ShAmt & (EVTBits-1)) == 0) {
2572 N0 = N0.getOperand(0);
2573 if (MVT::getSizeInBits(N0.getValueType()) <= EVTBits)
2574 return SDOperand();
Evan Chengb37b80c2007-03-23 20:55:21 +00002575 CombineSRL = true;
Evan Chengc88138f2007-03-22 01:54:19 +00002576 }
2577 }
2578 }
2579
2580 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
2581 // Do not allow folding to i1 here. i1 is implicitly stored in memory in
2582 // zero extended form: by shrinking the load, we lose track of the fact
2583 // that it is already zero extended.
2584 // FIXME: This should be reevaluated.
2585 VT != MVT::i1) {
2586 assert(MVT::getSizeInBits(N0.getValueType()) > EVTBits &&
2587 "Cannot truncate to larger type!");
2588 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2589 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
Evan Chengdae54ce2007-03-24 00:02:43 +00002590 // For big endian targets, we need to adjust the offset to the pointer to
2591 // load the correct bytes.
2592 if (!TLI.isLittleEndian())
2593 ShAmt = MVT::getSizeInBits(N0.getValueType()) - ShAmt - EVTBits;
2594 uint64_t PtrOff = ShAmt / 8;
Evan Chengc88138f2007-03-22 01:54:19 +00002595 SDOperand NewPtr = DAG.getNode(ISD::ADD, PtrType, LN0->getBasePtr(),
2596 DAG.getConstant(PtrOff, PtrType));
2597 AddToWorkList(NewPtr.Val);
2598 SDOperand Load = (ExtType == ISD::NON_EXTLOAD)
2599 ? DAG.getLoad(VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002600 LN0->getSrcValue(), LN0->getSrcValueOffset(),
2601 LN0->isVolatile(), LN0->getAlignment())
Evan Chengc88138f2007-03-22 01:54:19 +00002602 : DAG.getExtLoad(ExtType, VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002603 LN0->getSrcValue(), LN0->getSrcValueOffset(), EVT,
2604 LN0->isVolatile(), LN0->getAlignment());
Evan Chengc88138f2007-03-22 01:54:19 +00002605 AddToWorkList(N);
Evan Chengb37b80c2007-03-23 20:55:21 +00002606 if (CombineSRL) {
2607 std::vector<SDNode*> NowDead;
2608 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), Load.getValue(1), NowDead);
2609 CombineTo(N->getOperand(0).Val, Load);
2610 } else
2611 CombineTo(N0.Val, Load, Load.getValue(1));
Evan Cheng15213b72007-03-26 07:12:51 +00002612 if (ShAmt) {
2613 if (Opc == ISD::SIGN_EXTEND_INREG)
2614 return DAG.getNode(Opc, VT, Load, N->getOperand(1));
2615 else
2616 return DAG.getNode(Opc, VT, Load);
2617 }
Evan Chengc88138f2007-03-22 01:54:19 +00002618 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2619 }
2620
2621 return SDOperand();
2622}
2623
Chris Lattner5ffc0662006-05-05 05:58:59 +00002624
Nate Begeman83e75ec2005-09-06 04:43:02 +00002625SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002626 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002627 SDOperand N1 = N->getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002628 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002629 MVT::ValueType EVT = cast<VTSDNode>(N1)->getVT();
Nate Begeman07ed4172005-10-10 21:26:48 +00002630 unsigned EVTBits = MVT::getSizeInBits(EVT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002631
Nate Begeman1d4d4142005-09-01 00:19:25 +00002632 // fold (sext_in_reg c1) -> c1
Chris Lattnereaeda562006-05-08 20:59:41 +00002633 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
Chris Lattner310b5782006-05-06 23:06:26 +00002634 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0, N1);
Chris Lattneree4ea922006-05-06 09:30:03 +00002635
Chris Lattner541a24f2006-05-06 22:43:44 +00002636 // If the input is already sign extended, just drop the extension.
Dan Gohmanea859be2007-06-22 14:59:07 +00002637 if (DAG.ComputeNumSignBits(N0) >= MVT::getSizeInBits(VT)-EVTBits+1)
Chris Lattneree4ea922006-05-06 09:30:03 +00002638 return N0;
2639
Nate Begeman646d7e22005-09-02 21:18:40 +00002640 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
2641 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
2642 EVT < cast<VTSDNode>(N0.getOperand(1))->getVT()) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00002643 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002644 }
Chris Lattner4b37e872006-05-08 21:18:59 +00002645
Chris Lattner95a5e052007-04-17 19:03:21 +00002646 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is known zero.
Dan Gohmanea859be2007-06-22 14:59:07 +00002647 if (DAG.MaskedValueIsZero(N0, 1ULL << (EVTBits-1)))
Nate Begemande996292006-02-03 22:24:05 +00002648 return DAG.getZeroExtendInReg(N0, EVT);
Chris Lattner4b37e872006-05-08 21:18:59 +00002649
Chris Lattner95a5e052007-04-17 19:03:21 +00002650 // fold operands of sext_in_reg based on knowledge that the top bits are not
2651 // demanded.
2652 if (SimplifyDemandedBits(SDOperand(N, 0)))
2653 return SDOperand(N, 0);
2654
Evan Chengc88138f2007-03-22 01:54:19 +00002655 // fold (sext_in_reg (load x)) -> (smaller sextload x)
2656 // fold (sext_in_reg (srl (load x), c)) -> (smaller sextload (x+c/evtbits))
2657 SDOperand NarrowLoad = ReduceLoadWidth(N);
2658 if (NarrowLoad.Val)
2659 return NarrowLoad;
2660
Chris Lattner4b37e872006-05-08 21:18:59 +00002661 // fold (sext_in_reg (srl X, 24), i8) -> sra X, 24
2662 // fold (sext_in_reg (srl X, 23), i8) -> sra X, 23 iff possible.
2663 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
2664 if (N0.getOpcode() == ISD::SRL) {
2665 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
2666 if (ShAmt->getValue()+EVTBits <= MVT::getSizeInBits(VT)) {
2667 // We can turn this into an SRA iff the input to the SRL is already sign
2668 // extended enough.
Dan Gohmanea859be2007-06-22 14:59:07 +00002669 unsigned InSignBits = DAG.ComputeNumSignBits(N0.getOperand(0));
Chris Lattner4b37e872006-05-08 21:18:59 +00002670 if (MVT::getSizeInBits(VT)-(ShAmt->getValue()+EVTBits) < InSignBits)
2671 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0), N0.getOperand(1));
2672 }
2673 }
Evan Chengc88138f2007-03-22 01:54:19 +00002674
Nate Begemanded49632005-10-13 03:11:28 +00002675 // fold (sext_inreg (extload x)) -> (sextload x)
Evan Chengc5484282006-10-04 00:56:09 +00002676 if (ISD::isEXTLoad(N0.Val) &&
Evan Cheng83060c52007-03-07 08:07:03 +00002677 ISD::isUNINDEXEDLoad(N0.Val) &&
Evan Cheng2e49f092006-10-11 07:10:22 +00002678 EVT == cast<LoadSDNode>(N0)->getLoadedVT() &&
Evan Chengc5484282006-10-04 00:56:09 +00002679 (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00002680 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2681 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2682 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002683 LN0->getSrcValueOffset(), EVT,
2684 LN0->isVolatile(),
2685 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00002686 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00002687 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002688 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00002689 }
2690 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Evan Cheng83060c52007-03-07 08:07:03 +00002691 if (ISD::isZEXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val) &&
2692 N0.hasOneUse() &&
Evan Cheng2e49f092006-10-11 07:10:22 +00002693 EVT == cast<LoadSDNode>(N0)->getLoadedVT() &&
Evan Chengc5484282006-10-04 00:56:09 +00002694 (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00002695 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2696 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2697 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002698 LN0->getSrcValueOffset(), EVT,
2699 LN0->isVolatile(),
2700 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00002701 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00002702 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002703 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00002704 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00002705 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002706}
2707
Nate Begeman83e75ec2005-09-06 04:43:02 +00002708SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002709 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002710 MVT::ValueType VT = N->getValueType(0);
2711
2712 // noop truncate
2713 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002714 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002715 // fold (truncate c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00002716 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002717 return DAG.getNode(ISD::TRUNCATE, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002718 // fold (truncate (truncate x)) -> (truncate x)
2719 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002720 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002721 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
Chris Lattnerb72773b2006-05-05 22:56:26 +00002722 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND||
2723 N0.getOpcode() == ISD::ANY_EXTEND) {
Chris Lattner32ba1aa2006-11-20 18:05:46 +00002724 if (N0.getOperand(0).getValueType() < VT)
Nate Begeman1d4d4142005-09-01 00:19:25 +00002725 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00002726 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Chris Lattner32ba1aa2006-11-20 18:05:46 +00002727 else if (N0.getOperand(0).getValueType() > VT)
Nate Begeman1d4d4142005-09-01 00:19:25 +00002728 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00002729 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002730 else
2731 // if the source and dest are the same type, we can drop both the extend
2732 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00002733 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002734 }
Evan Cheng007b69e2007-03-21 20:14:05 +00002735
Nate Begeman3df4d522005-10-12 20:40:40 +00002736 // fold (truncate (load x)) -> (smaller load x)
Evan Cheng007b69e2007-03-21 20:14:05 +00002737 // fold (truncate (srl (load x), c)) -> (smaller load (x+c/evtbits))
Evan Chengc88138f2007-03-22 01:54:19 +00002738 return ReduceLoadWidth(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002739}
2740
Chris Lattner94683772005-12-23 05:30:37 +00002741SDOperand DAGCombiner::visitBIT_CONVERT(SDNode *N) {
2742 SDOperand N0 = N->getOperand(0);
2743 MVT::ValueType VT = N->getValueType(0);
2744
2745 // If the input is a constant, let getNode() fold it.
2746 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
2747 SDOperand Res = DAG.getNode(ISD::BIT_CONVERT, VT, N0);
2748 if (Res.Val != N) return Res;
2749 }
2750
Chris Lattnerc8547d82005-12-23 05:37:50 +00002751 if (N0.getOpcode() == ISD::BIT_CONVERT) // conv(conv(x,t1),t2) -> conv(x,t2)
2752 return DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
Chris Lattner6258fb22006-04-02 02:53:43 +00002753
Chris Lattner57104102005-12-23 05:44:41 +00002754 // fold (conv (load x)) -> (load (conv*)x)
Evan Cheng59d5b682007-05-07 21:27:48 +00002755 // If the resultant load doesn't need a higher alignment than the original!
2756 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Dale Johannesen98a6c622007-05-16 22:45:30 +00002757 ISD::isUNINDEXEDLoad(N0.Val) &&
Evan Cheng59d5b682007-05-07 21:27:48 +00002758 TLI.isOperationLegal(ISD::LOAD, VT)) {
Evan Cheng466685d2006-10-09 20:57:25 +00002759 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng59d5b682007-05-07 21:27:48 +00002760 unsigned Align = TLI.getTargetMachine().getTargetData()->
Dan Gohmanfcc4dd92007-05-18 18:41:29 +00002761 getABITypeAlignment(MVT::getTypeForValueType(VT));
Evan Cheng59d5b682007-05-07 21:27:48 +00002762 unsigned OrigAlign = LN0->getAlignment();
2763 if (Align <= OrigAlign) {
2764 SDOperand Load = DAG.getLoad(VT, LN0->getChain(), LN0->getBasePtr(),
2765 LN0->getSrcValue(), LN0->getSrcValueOffset(),
2766 LN0->isVolatile(), LN0->getAlignment());
2767 AddToWorkList(N);
2768 CombineTo(N0.Val, DAG.getNode(ISD::BIT_CONVERT, N0.getValueType(), Load),
2769 Load.getValue(1));
2770 return Load;
2771 }
Chris Lattner57104102005-12-23 05:44:41 +00002772 }
2773
Chris Lattner94683772005-12-23 05:30:37 +00002774 return SDOperand();
2775}
2776
Chris Lattner6258fb22006-04-02 02:53:43 +00002777SDOperand DAGCombiner::visitVBIT_CONVERT(SDNode *N) {
2778 SDOperand N0 = N->getOperand(0);
2779 MVT::ValueType VT = N->getValueType(0);
2780
2781 // If the input is a VBUILD_VECTOR with all constant elements, fold this now.
2782 // First check to see if this is all constant.
2783 if (N0.getOpcode() == ISD::VBUILD_VECTOR && N0.Val->hasOneUse() &&
2784 VT == MVT::Vector) {
2785 bool isSimple = true;
2786 for (unsigned i = 0, e = N0.getNumOperands()-2; i != e; ++i)
2787 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
2788 N0.getOperand(i).getOpcode() != ISD::Constant &&
2789 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
2790 isSimple = false;
2791 break;
2792 }
2793
Chris Lattner97c20732006-04-03 17:29:28 +00002794 MVT::ValueType DestEltVT = cast<VTSDNode>(N->getOperand(2))->getVT();
2795 if (isSimple && !MVT::isVector(DestEltVT)) {
Chris Lattner6258fb22006-04-02 02:53:43 +00002796 return ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(N0.Val, DestEltVT);
2797 }
2798 }
2799
2800 return SDOperand();
2801}
2802
2803/// ConstantFoldVBIT_CONVERTofVBUILD_VECTOR - We know that BV is a vbuild_vector
2804/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
2805/// destination element value type.
2806SDOperand DAGCombiner::
2807ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(SDNode *BV, MVT::ValueType DstEltVT) {
2808 MVT::ValueType SrcEltVT = BV->getOperand(0).getValueType();
2809
2810 // If this is already the right type, we're done.
2811 if (SrcEltVT == DstEltVT) return SDOperand(BV, 0);
2812
2813 unsigned SrcBitSize = MVT::getSizeInBits(SrcEltVT);
2814 unsigned DstBitSize = MVT::getSizeInBits(DstEltVT);
2815
2816 // If this is a conversion of N elements of one type to N elements of another
2817 // type, convert each element. This handles FP<->INT cases.
2818 if (SrcBitSize == DstBitSize) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002819 SmallVector<SDOperand, 8> Ops;
Chris Lattner3e104b12006-04-08 04:15:24 +00002820 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00002821 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, DstEltVT, BV->getOperand(i)));
Chris Lattner3e104b12006-04-08 04:15:24 +00002822 AddToWorkList(Ops.back().Val);
2823 }
Chris Lattner6258fb22006-04-02 02:53:43 +00002824 Ops.push_back(*(BV->op_end()-2)); // Add num elements.
2825 Ops.push_back(DAG.getValueType(DstEltVT));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002826 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00002827 }
2828
2829 // Otherwise, we're growing or shrinking the elements. To avoid having to
2830 // handle annoying details of growing/shrinking FP values, we convert them to
2831 // int first.
2832 if (MVT::isFloatingPoint(SrcEltVT)) {
2833 // Convert the input float vector to a int vector where the elements are the
2834 // same sizes.
2835 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
2836 MVT::ValueType IntVT = SrcEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
2837 BV = ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(BV, IntVT).Val;
2838 SrcEltVT = IntVT;
2839 }
2840
2841 // Now we know the input is an integer vector. If the output is a FP type,
2842 // convert to integer first, then to FP of the right size.
2843 if (MVT::isFloatingPoint(DstEltVT)) {
2844 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
2845 MVT::ValueType TmpVT = DstEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
2846 SDNode *Tmp = ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(BV, TmpVT).Val;
2847
2848 // Next, convert to FP elements of the same size.
2849 return ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(Tmp, DstEltVT);
2850 }
2851
2852 // Okay, we know the src/dst types are both integers of differing types.
2853 // Handling growing first.
2854 assert(MVT::isInteger(SrcEltVT) && MVT::isInteger(DstEltVT));
2855 if (SrcBitSize < DstBitSize) {
2856 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
2857
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002858 SmallVector<SDOperand, 8> Ops;
Chris Lattner6258fb22006-04-02 02:53:43 +00002859 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e;
2860 i += NumInputsPerOutput) {
2861 bool isLE = TLI.isLittleEndian();
2862 uint64_t NewBits = 0;
2863 bool EltIsUndef = true;
2864 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
2865 // Shift the previously computed bits over.
2866 NewBits <<= SrcBitSize;
2867 SDOperand Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
2868 if (Op.getOpcode() == ISD::UNDEF) continue;
2869 EltIsUndef = false;
2870
2871 NewBits |= cast<ConstantSDNode>(Op)->getValue();
2872 }
2873
2874 if (EltIsUndef)
2875 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
2876 else
2877 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
2878 }
2879
2880 Ops.push_back(DAG.getConstant(Ops.size(), MVT::i32)); // Add num elements.
2881 Ops.push_back(DAG.getValueType(DstEltVT)); // Add element size.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002882 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00002883 }
2884
2885 // Finally, this must be the case where we are shrinking elements: each input
2886 // turns into multiple outputs.
2887 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002888 SmallVector<SDOperand, 8> Ops;
Chris Lattner6258fb22006-04-02 02:53:43 +00002889 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e; ++i) {
2890 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
2891 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
2892 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
2893 continue;
2894 }
2895 uint64_t OpVal = cast<ConstantSDNode>(BV->getOperand(i))->getValue();
2896
2897 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
2898 unsigned ThisVal = OpVal & ((1ULL << DstBitSize)-1);
2899 OpVal >>= DstBitSize;
2900 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
2901 }
2902
2903 // For big endian targets, swap the order of the pieces of each element.
2904 if (!TLI.isLittleEndian())
2905 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
2906 }
2907 Ops.push_back(DAG.getConstant(Ops.size(), MVT::i32)); // Add num elements.
2908 Ops.push_back(DAG.getValueType(DstEltVT)); // Add element size.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002909 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00002910}
2911
2912
2913
Chris Lattner01b3d732005-09-28 22:28:18 +00002914SDOperand DAGCombiner::visitFADD(SDNode *N) {
2915 SDOperand N0 = N->getOperand(0);
2916 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00002917 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2918 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002919 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00002920
2921 // fold (fadd c1, c2) -> c1+c2
2922 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002923 return DAG.getNode(ISD::FADD, VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00002924 // canonicalize constant to RHS
2925 if (N0CFP && !N1CFP)
2926 return DAG.getNode(ISD::FADD, VT, N1, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00002927 // fold (A + (-B)) -> A-B
Chris Lattner29446522007-05-14 22:04:50 +00002928 if (isNegatibleForFree(N1) == 2)
2929 return DAG.getNode(ISD::FSUB, VT, N0, GetNegatedExpression(N1, DAG));
Chris Lattner01b3d732005-09-28 22:28:18 +00002930 // fold ((-A) + B) -> B-A
Chris Lattner29446522007-05-14 22:04:50 +00002931 if (isNegatibleForFree(N0) == 2)
2932 return DAG.getNode(ISD::FSUB, VT, N1, GetNegatedExpression(N0, DAG));
Chris Lattnerddae4bd2007-01-08 23:04:05 +00002933
2934 // If allowed, fold (fadd (fadd x, c1), c2) -> (fadd x, (fadd c1, c2))
2935 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FADD &&
2936 N0.Val->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
2937 return DAG.getNode(ISD::FADD, VT, N0.getOperand(0),
2938 DAG.getNode(ISD::FADD, VT, N0.getOperand(1), N1));
2939
Chris Lattner01b3d732005-09-28 22:28:18 +00002940 return SDOperand();
2941}
2942
2943SDOperand DAGCombiner::visitFSUB(SDNode *N) {
2944 SDOperand N0 = N->getOperand(0);
2945 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00002946 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2947 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002948 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00002949
2950 // fold (fsub c1, c2) -> c1-c2
2951 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002952 return DAG.getNode(ISD::FSUB, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002953 // fold (A-(-B)) -> A+B
Chris Lattner29446522007-05-14 22:04:50 +00002954 if (isNegatibleForFree(N1))
2955 return DAG.getNode(ISD::FADD, VT, N0, GetNegatedExpression(N1, DAG));
2956
Chris Lattner01b3d732005-09-28 22:28:18 +00002957 return SDOperand();
2958}
2959
2960SDOperand DAGCombiner::visitFMUL(SDNode *N) {
2961 SDOperand N0 = N->getOperand(0);
2962 SDOperand N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00002963 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2964 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002965 MVT::ValueType VT = N->getValueType(0);
2966
Nate Begeman11af4ea2005-10-17 20:40:11 +00002967 // fold (fmul c1, c2) -> c1*c2
2968 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002969 return DAG.getNode(ISD::FMUL, VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00002970 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00002971 if (N0CFP && !N1CFP)
2972 return DAG.getNode(ISD::FMUL, VT, N1, N0);
Nate Begeman11af4ea2005-10-17 20:40:11 +00002973 // fold (fmul X, 2.0) -> (fadd X, X)
2974 if (N1CFP && N1CFP->isExactlyValue(+2.0))
2975 return DAG.getNode(ISD::FADD, VT, N0, N0);
Chris Lattner29446522007-05-14 22:04:50 +00002976 // fold (fmul X, -1.0) -> (fneg X)
2977 if (N1CFP && N1CFP->isExactlyValue(-1.0))
2978 return DAG.getNode(ISD::FNEG, VT, N0);
2979
2980 // -X * -Y -> X*Y
2981 if (char LHSNeg = isNegatibleForFree(N0)) {
2982 if (char RHSNeg = isNegatibleForFree(N1)) {
2983 // Both can be negated for free, check to see if at least one is cheaper
2984 // negated.
2985 if (LHSNeg == 2 || RHSNeg == 2)
2986 return DAG.getNode(ISD::FMUL, VT, GetNegatedExpression(N0, DAG),
2987 GetNegatedExpression(N1, DAG));
2988 }
2989 }
Chris Lattnerddae4bd2007-01-08 23:04:05 +00002990
2991 // If allowed, fold (fmul (fmul x, c1), c2) -> (fmul x, (fmul c1, c2))
2992 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FMUL &&
2993 N0.Val->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
2994 return DAG.getNode(ISD::FMUL, VT, N0.getOperand(0),
2995 DAG.getNode(ISD::FMUL, VT, N0.getOperand(1), N1));
2996
Chris Lattner01b3d732005-09-28 22:28:18 +00002997 return SDOperand();
2998}
2999
3000SDOperand DAGCombiner::visitFDIV(SDNode *N) {
3001 SDOperand N0 = N->getOperand(0);
3002 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003003 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3004 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00003005 MVT::ValueType VT = N->getValueType(0);
3006
Nate Begemana148d982006-01-18 22:35:16 +00003007 // fold (fdiv c1, c2) -> c1/c2
3008 if (N0CFP && N1CFP)
3009 return DAG.getNode(ISD::FDIV, VT, N0, N1);
Chris Lattner29446522007-05-14 22:04:50 +00003010
3011
3012 // -X / -Y -> X*Y
3013 if (char LHSNeg = isNegatibleForFree(N0)) {
3014 if (char RHSNeg = isNegatibleForFree(N1)) {
3015 // Both can be negated for free, check to see if at least one is cheaper
3016 // negated.
3017 if (LHSNeg == 2 || RHSNeg == 2)
3018 return DAG.getNode(ISD::FDIV, VT, GetNegatedExpression(N0, DAG),
3019 GetNegatedExpression(N1, DAG));
3020 }
3021 }
3022
Chris Lattner01b3d732005-09-28 22:28:18 +00003023 return SDOperand();
3024}
3025
3026SDOperand DAGCombiner::visitFREM(SDNode *N) {
3027 SDOperand N0 = N->getOperand(0);
3028 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003029 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3030 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00003031 MVT::ValueType VT = N->getValueType(0);
3032
Nate Begemana148d982006-01-18 22:35:16 +00003033 // fold (frem c1, c2) -> fmod(c1,c2)
3034 if (N0CFP && N1CFP)
3035 return DAG.getNode(ISD::FREM, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00003036 return SDOperand();
3037}
3038
Chris Lattner12d83032006-03-05 05:30:57 +00003039SDOperand DAGCombiner::visitFCOPYSIGN(SDNode *N) {
3040 SDOperand N0 = N->getOperand(0);
3041 SDOperand N1 = N->getOperand(1);
3042 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3043 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
3044 MVT::ValueType VT = N->getValueType(0);
3045
3046 if (N0CFP && N1CFP) // Constant fold
3047 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1);
3048
3049 if (N1CFP) {
3050 // copysign(x, c1) -> fabs(x) iff ispos(c1)
3051 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
3052 union {
3053 double d;
3054 int64_t i;
3055 } u;
3056 u.d = N1CFP->getValue();
3057 if (u.i >= 0)
3058 return DAG.getNode(ISD::FABS, VT, N0);
3059 else
3060 return DAG.getNode(ISD::FNEG, VT, DAG.getNode(ISD::FABS, VT, N0));
3061 }
3062
3063 // copysign(fabs(x), y) -> copysign(x, y)
3064 // copysign(fneg(x), y) -> copysign(x, y)
3065 // copysign(copysign(x,z), y) -> copysign(x, y)
3066 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
3067 N0.getOpcode() == ISD::FCOPYSIGN)
3068 return DAG.getNode(ISD::FCOPYSIGN, VT, N0.getOperand(0), N1);
3069
3070 // copysign(x, abs(y)) -> abs(x)
3071 if (N1.getOpcode() == ISD::FABS)
3072 return DAG.getNode(ISD::FABS, VT, N0);
3073
3074 // copysign(x, copysign(y,z)) -> copysign(x, z)
3075 if (N1.getOpcode() == ISD::FCOPYSIGN)
3076 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(1));
3077
3078 // copysign(x, fp_extend(y)) -> copysign(x, y)
3079 // copysign(x, fp_round(y)) -> copysign(x, y)
3080 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
3081 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(0));
3082
3083 return SDOperand();
3084}
3085
3086
Chris Lattner01b3d732005-09-28 22:28:18 +00003087
Nate Begeman83e75ec2005-09-06 04:43:02 +00003088SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003089 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003090 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00003091 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003092
3093 // fold (sint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00003094 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00003095 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003096 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003097}
3098
Nate Begeman83e75ec2005-09-06 04:43:02 +00003099SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003100 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003101 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00003102 MVT::ValueType VT = N->getValueType(0);
3103
Nate Begeman1d4d4142005-09-01 00:19:25 +00003104 // fold (uint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00003105 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00003106 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003107 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003108}
3109
Nate Begeman83e75ec2005-09-06 04:43:02 +00003110SDOperand DAGCombiner::visitFP_TO_SINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003111 SDOperand N0 = N->getOperand(0);
3112 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3113 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003114
3115 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00003116 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00003117 return DAG.getNode(ISD::FP_TO_SINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003118 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003119}
3120
Nate Begeman83e75ec2005-09-06 04:43:02 +00003121SDOperand DAGCombiner::visitFP_TO_UINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003122 SDOperand N0 = N->getOperand(0);
3123 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3124 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003125
3126 // fold (fp_to_uint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00003127 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00003128 return DAG.getNode(ISD::FP_TO_UINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003129 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003130}
3131
Nate Begeman83e75ec2005-09-06 04:43:02 +00003132SDOperand DAGCombiner::visitFP_ROUND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003133 SDOperand N0 = N->getOperand(0);
3134 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3135 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003136
3137 // fold (fp_round c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00003138 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00003139 return DAG.getNode(ISD::FP_ROUND, VT, N0);
Chris Lattner79dbea52006-03-13 06:26:26 +00003140
3141 // fold (fp_round (fp_extend x)) -> x
3142 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
3143 return N0.getOperand(0);
3144
3145 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
3146 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.Val->hasOneUse()) {
3147 SDOperand Tmp = DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0));
3148 AddToWorkList(Tmp.Val);
3149 return DAG.getNode(ISD::FCOPYSIGN, VT, Tmp, N0.getOperand(1));
3150 }
3151
Nate Begeman83e75ec2005-09-06 04:43:02 +00003152 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003153}
3154
Nate Begeman83e75ec2005-09-06 04:43:02 +00003155SDOperand DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003156 SDOperand N0 = N->getOperand(0);
3157 MVT::ValueType VT = N->getValueType(0);
3158 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00003159 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003160
Nate Begeman1d4d4142005-09-01 00:19:25 +00003161 // fold (fp_round_inreg c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00003162 if (N0CFP) {
3163 SDOperand Round = DAG.getConstantFP(N0CFP->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003164 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003165 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00003166 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003167}
3168
Nate Begeman83e75ec2005-09-06 04:43:02 +00003169SDOperand DAGCombiner::visitFP_EXTEND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003170 SDOperand N0 = N->getOperand(0);
3171 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3172 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003173
3174 // fold (fp_extend c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00003175 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00003176 return DAG.getNode(ISD::FP_EXTEND, VT, N0);
Chris Lattnere564dbb2006-05-05 21:34:35 +00003177
3178 // fold (fpext (load x)) -> (fpext (fpround (extload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00003179 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00003180 (!AfterLegalize||TLI.isLoadXLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003181 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3182 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
3183 LN0->getBasePtr(), LN0->getSrcValue(),
3184 LN0->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003185 N0.getValueType(),
3186 LN0->isVolatile(),
3187 LN0->getAlignment());
Chris Lattnere564dbb2006-05-05 21:34:35 +00003188 CombineTo(N, ExtLoad);
3189 CombineTo(N0.Val, DAG.getNode(ISD::FP_ROUND, N0.getValueType(), ExtLoad),
3190 ExtLoad.getValue(1));
3191 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
3192 }
3193
3194
Nate Begeman83e75ec2005-09-06 04:43:02 +00003195 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003196}
3197
Nate Begeman83e75ec2005-09-06 04:43:02 +00003198SDOperand DAGCombiner::visitFNEG(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003199 SDOperand N0 = N->getOperand(0);
3200 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3201 MVT::ValueType VT = N->getValueType(0);
3202
3203 // fold (fneg c1) -> -c1
Nate Begeman646d7e22005-09-02 21:18:40 +00003204 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00003205 return DAG.getNode(ISD::FNEG, VT, N0);
3206 // fold (fneg (sub x, y)) -> (sub y, x)
Chris Lattner12d83032006-03-05 05:30:57 +00003207 if (N0.getOpcode() == ISD::SUB)
3208 return DAG.getNode(ISD::SUB, VT, N0.getOperand(1), N0.getOperand(0));
Nate Begemana148d982006-01-18 22:35:16 +00003209 // fold (fneg (fneg x)) -> x
Chris Lattner12d83032006-03-05 05:30:57 +00003210 if (N0.getOpcode() == ISD::FNEG)
3211 return N0.getOperand(0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003212 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003213}
3214
Nate Begeman83e75ec2005-09-06 04:43:02 +00003215SDOperand DAGCombiner::visitFABS(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003216 SDOperand N0 = N->getOperand(0);
3217 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3218 MVT::ValueType VT = N->getValueType(0);
3219
Nate Begeman1d4d4142005-09-01 00:19:25 +00003220 // fold (fabs c1) -> fabs(c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00003221 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00003222 return DAG.getNode(ISD::FABS, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003223 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00003224 if (N0.getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00003225 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003226 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00003227 // fold (fabs (fcopysign x, y)) -> (fabs x)
3228 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
3229 return DAG.getNode(ISD::FABS, VT, N0.getOperand(0));
3230
Nate Begeman83e75ec2005-09-06 04:43:02 +00003231 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003232}
3233
Nate Begeman44728a72005-09-19 22:34:01 +00003234SDOperand DAGCombiner::visitBRCOND(SDNode *N) {
3235 SDOperand Chain = N->getOperand(0);
3236 SDOperand N1 = N->getOperand(1);
3237 SDOperand N2 = N->getOperand(2);
3238 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
3239
3240 // never taken branch, fold to chain
3241 if (N1C && N1C->isNullValue())
3242 return Chain;
3243 // unconditional branch
Nate Begemane17daeb2005-10-05 21:43:42 +00003244 if (N1C && N1C->getValue() == 1)
Nate Begeman44728a72005-09-19 22:34:01 +00003245 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
Nate Begeman750ac1b2006-02-01 07:19:44 +00003246 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
3247 // on the target.
3248 if (N1.getOpcode() == ISD::SETCC &&
3249 TLI.isOperationLegal(ISD::BR_CC, MVT::Other)) {
3250 return DAG.getNode(ISD::BR_CC, MVT::Other, Chain, N1.getOperand(2),
3251 N1.getOperand(0), N1.getOperand(1), N2);
3252 }
Nate Begeman44728a72005-09-19 22:34:01 +00003253 return SDOperand();
3254}
3255
Chris Lattner3ea0b472005-10-05 06:47:48 +00003256// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
3257//
Nate Begeman44728a72005-09-19 22:34:01 +00003258SDOperand DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00003259 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
3260 SDOperand CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
3261
3262 // Use SimplifySetCC to simplify SETCC's.
Nate Begemane17daeb2005-10-05 21:43:42 +00003263 SDOperand Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false);
Chris Lattner30f73e72006-10-14 03:52:46 +00003264 if (Simp.Val) AddToWorkList(Simp.Val);
3265
Nate Begemane17daeb2005-10-05 21:43:42 +00003266 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.Val);
3267
3268 // fold br_cc true, dest -> br dest (unconditional branch)
3269 if (SCCC && SCCC->getValue())
3270 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
3271 N->getOperand(4));
3272 // fold br_cc false, dest -> unconditional fall through
3273 if (SCCC && SCCC->isNullValue())
3274 return N->getOperand(0);
Chris Lattner30f73e72006-10-14 03:52:46 +00003275
Nate Begemane17daeb2005-10-05 21:43:42 +00003276 // fold to a simpler setcc
3277 if (Simp.Val && Simp.getOpcode() == ISD::SETCC)
3278 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
3279 Simp.getOperand(2), Simp.getOperand(0),
3280 Simp.getOperand(1), N->getOperand(4));
Nate Begeman44728a72005-09-19 22:34:01 +00003281 return SDOperand();
3282}
3283
Chris Lattner448f2192006-11-11 00:39:41 +00003284
3285/// CombineToPreIndexedLoadStore - Try turning a load / store and a
3286/// pre-indexed load / store when the base pointer is a add or subtract
3287/// and it has other uses besides the load / store. After the
3288/// transformation, the new indexed load / store has effectively folded
3289/// the add / subtract in and all of its other uses are redirected to the
3290/// new load / store.
3291bool DAGCombiner::CombineToPreIndexedLoadStore(SDNode *N) {
3292 if (!AfterLegalize)
3293 return false;
3294
3295 bool isLoad = true;
3296 SDOperand Ptr;
3297 MVT::ValueType VT;
3298 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Evan Chenge90460e2006-12-16 06:25:23 +00003299 if (LD->getAddressingMode() != ISD::UNINDEXED)
3300 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00003301 VT = LD->getLoadedVT();
Evan Cheng83060c52007-03-07 08:07:03 +00003302 if (!TLI.isIndexedLoadLegal(ISD::PRE_INC, VT) &&
Chris Lattner448f2192006-11-11 00:39:41 +00003303 !TLI.isIndexedLoadLegal(ISD::PRE_DEC, VT))
3304 return false;
3305 Ptr = LD->getBasePtr();
3306 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Evan Chenge90460e2006-12-16 06:25:23 +00003307 if (ST->getAddressingMode() != ISD::UNINDEXED)
3308 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00003309 VT = ST->getStoredVT();
3310 if (!TLI.isIndexedStoreLegal(ISD::PRE_INC, VT) &&
3311 !TLI.isIndexedStoreLegal(ISD::PRE_DEC, VT))
3312 return false;
3313 Ptr = ST->getBasePtr();
3314 isLoad = false;
3315 } else
3316 return false;
3317
Chris Lattner9f1794e2006-11-11 00:56:29 +00003318 // If the pointer is not an add/sub, or if it doesn't have multiple uses, bail
3319 // out. There is no reason to make this a preinc/predec.
3320 if ((Ptr.getOpcode() != ISD::ADD && Ptr.getOpcode() != ISD::SUB) ||
3321 Ptr.Val->hasOneUse())
3322 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00003323
Chris Lattner9f1794e2006-11-11 00:56:29 +00003324 // Ask the target to do addressing mode selection.
3325 SDOperand BasePtr;
3326 SDOperand Offset;
3327 ISD::MemIndexedMode AM = ISD::UNINDEXED;
3328 if (!TLI.getPreIndexedAddressParts(N, BasePtr, Offset, AM, DAG))
3329 return false;
Evan Chenga7d4a042007-05-03 23:52:19 +00003330 // Don't create a indexed load / store with zero offset.
3331 if (isa<ConstantSDNode>(Offset) &&
3332 cast<ConstantSDNode>(Offset)->getValue() == 0)
3333 return false;
Chris Lattner9f1794e2006-11-11 00:56:29 +00003334
Chris Lattner41e53fd2006-11-11 01:00:15 +00003335 // Try turning it into a pre-indexed load / store except when:
Evan Chengc843abe2007-05-24 02:35:39 +00003336 // 1) The new base ptr is a frame index.
3337 // 2) If N is a store and the new base ptr is either the same as or is a
Chris Lattner9f1794e2006-11-11 00:56:29 +00003338 // predecessor of the value being stored.
Evan Chengc843abe2007-05-24 02:35:39 +00003339 // 3) Another use of old base ptr is a predecessor of N. If ptr is folded
Chris Lattner9f1794e2006-11-11 00:56:29 +00003340 // that would create a cycle.
Evan Chengc843abe2007-05-24 02:35:39 +00003341 // 4) All uses are load / store ops that use it as old base ptr.
Chris Lattner448f2192006-11-11 00:39:41 +00003342
Chris Lattner41e53fd2006-11-11 01:00:15 +00003343 // Check #1. Preinc'ing a frame index would require copying the stack pointer
3344 // (plus the implicit offset) to a register to preinc anyway.
3345 if (isa<FrameIndexSDNode>(BasePtr))
3346 return false;
3347
3348 // Check #2.
Chris Lattner9f1794e2006-11-11 00:56:29 +00003349 if (!isLoad) {
3350 SDOperand Val = cast<StoreSDNode>(N)->getValue();
Evan Chengc843abe2007-05-24 02:35:39 +00003351 if (Val == BasePtr || BasePtr.Val->isPredecessor(Val.Val))
Chris Lattner9f1794e2006-11-11 00:56:29 +00003352 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00003353 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00003354
Evan Chengc843abe2007-05-24 02:35:39 +00003355 // Now check for #3 and #4.
Chris Lattner9f1794e2006-11-11 00:56:29 +00003356 bool RealUse = false;
3357 for (SDNode::use_iterator I = Ptr.Val->use_begin(),
3358 E = Ptr.Val->use_end(); I != E; ++I) {
3359 SDNode *Use = *I;
3360 if (Use == N)
3361 continue;
3362 if (Use->isPredecessor(N))
3363 return false;
3364
3365 if (!((Use->getOpcode() == ISD::LOAD &&
3366 cast<LoadSDNode>(Use)->getBasePtr() == Ptr) ||
3367 (Use->getOpcode() == ISD::STORE) &&
3368 cast<StoreSDNode>(Use)->getBasePtr() == Ptr))
3369 RealUse = true;
3370 }
3371 if (!RealUse)
3372 return false;
3373
3374 SDOperand Result;
3375 if (isLoad)
3376 Result = DAG.getIndexedLoad(SDOperand(N,0), BasePtr, Offset, AM);
3377 else
3378 Result = DAG.getIndexedStore(SDOperand(N,0), BasePtr, Offset, AM);
3379 ++PreIndexedNodes;
3380 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +00003381 DOUT << "\nReplacing.4 "; DEBUG(N->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +00003382 DOUT << "\nWith: "; DEBUG(Result.Val->dump(&DAG));
3383 DOUT << '\n';
Chris Lattner9f1794e2006-11-11 00:56:29 +00003384 std::vector<SDNode*> NowDead;
3385 if (isLoad) {
3386 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(0),
3387 NowDead);
3388 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1), Result.getValue(2),
3389 NowDead);
3390 } else {
3391 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(1),
3392 NowDead);
3393 }
3394
3395 // Nodes can end up on the worklist more than once. Make sure we do
3396 // not process a node that has been replaced.
3397 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
3398 removeFromWorkList(NowDead[i]);
3399 // Finally, since the node is now dead, remove it from the graph.
3400 DAG.DeleteNode(N);
3401
3402 // Replace the uses of Ptr with uses of the updated base value.
3403 DAG.ReplaceAllUsesOfValueWith(Ptr, Result.getValue(isLoad ? 1 : 0),
3404 NowDead);
3405 removeFromWorkList(Ptr.Val);
3406 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
3407 removeFromWorkList(NowDead[i]);
3408 DAG.DeleteNode(Ptr.Val);
3409
3410 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00003411}
3412
3413/// CombineToPostIndexedLoadStore - Try combine a load / store with a
3414/// add / sub of the base pointer node into a post-indexed load / store.
3415/// The transformation folded the add / subtract into the new indexed
3416/// load / store effectively and all of its uses are redirected to the
3417/// new load / store.
3418bool DAGCombiner::CombineToPostIndexedLoadStore(SDNode *N) {
3419 if (!AfterLegalize)
3420 return false;
3421
3422 bool isLoad = true;
3423 SDOperand Ptr;
3424 MVT::ValueType VT;
3425 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Evan Chenge90460e2006-12-16 06:25:23 +00003426 if (LD->getAddressingMode() != ISD::UNINDEXED)
3427 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00003428 VT = LD->getLoadedVT();
3429 if (!TLI.isIndexedLoadLegal(ISD::POST_INC, VT) &&
3430 !TLI.isIndexedLoadLegal(ISD::POST_DEC, VT))
3431 return false;
3432 Ptr = LD->getBasePtr();
3433 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Evan Chenge90460e2006-12-16 06:25:23 +00003434 if (ST->getAddressingMode() != ISD::UNINDEXED)
3435 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00003436 VT = ST->getStoredVT();
3437 if (!TLI.isIndexedStoreLegal(ISD::POST_INC, VT) &&
3438 !TLI.isIndexedStoreLegal(ISD::POST_DEC, VT))
3439 return false;
3440 Ptr = ST->getBasePtr();
3441 isLoad = false;
3442 } else
3443 return false;
3444
Evan Chengcc470212006-11-16 00:08:20 +00003445 if (Ptr.Val->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00003446 return false;
3447
3448 for (SDNode::use_iterator I = Ptr.Val->use_begin(),
3449 E = Ptr.Val->use_end(); I != E; ++I) {
3450 SDNode *Op = *I;
3451 if (Op == N ||
3452 (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB))
3453 continue;
3454
3455 SDOperand BasePtr;
3456 SDOperand Offset;
3457 ISD::MemIndexedMode AM = ISD::UNINDEXED;
3458 if (TLI.getPostIndexedAddressParts(N, Op, BasePtr, Offset, AM, DAG)) {
3459 if (Ptr == Offset)
3460 std::swap(BasePtr, Offset);
3461 if (Ptr != BasePtr)
Chris Lattner448f2192006-11-11 00:39:41 +00003462 continue;
Evan Chenga7d4a042007-05-03 23:52:19 +00003463 // Don't create a indexed load / store with zero offset.
3464 if (isa<ConstantSDNode>(Offset) &&
3465 cast<ConstantSDNode>(Offset)->getValue() == 0)
3466 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00003467
Chris Lattner9f1794e2006-11-11 00:56:29 +00003468 // Try turning it into a post-indexed load / store except when
3469 // 1) All uses are load / store ops that use it as base ptr.
3470 // 2) Op must be independent of N, i.e. Op is neither a predecessor
3471 // nor a successor of N. Otherwise, if Op is folded that would
3472 // create a cycle.
3473
3474 // Check for #1.
3475 bool TryNext = false;
3476 for (SDNode::use_iterator II = BasePtr.Val->use_begin(),
3477 EE = BasePtr.Val->use_end(); II != EE; ++II) {
3478 SDNode *Use = *II;
3479 if (Use == Ptr.Val)
Chris Lattner448f2192006-11-11 00:39:41 +00003480 continue;
3481
Chris Lattner9f1794e2006-11-11 00:56:29 +00003482 // If all the uses are load / store addresses, then don't do the
3483 // transformation.
3484 if (Use->getOpcode() == ISD::ADD || Use->getOpcode() == ISD::SUB){
3485 bool RealUse = false;
3486 for (SDNode::use_iterator III = Use->use_begin(),
3487 EEE = Use->use_end(); III != EEE; ++III) {
3488 SDNode *UseUse = *III;
3489 if (!((UseUse->getOpcode() == ISD::LOAD &&
3490 cast<LoadSDNode>(UseUse)->getBasePtr().Val == Use) ||
3491 (UseUse->getOpcode() == ISD::STORE) &&
3492 cast<StoreSDNode>(UseUse)->getBasePtr().Val == Use))
3493 RealUse = true;
3494 }
Chris Lattner448f2192006-11-11 00:39:41 +00003495
Chris Lattner9f1794e2006-11-11 00:56:29 +00003496 if (!RealUse) {
3497 TryNext = true;
3498 break;
Chris Lattner448f2192006-11-11 00:39:41 +00003499 }
3500 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00003501 }
3502 if (TryNext)
3503 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00003504
Chris Lattner9f1794e2006-11-11 00:56:29 +00003505 // Check for #2
3506 if (!Op->isPredecessor(N) && !N->isPredecessor(Op)) {
3507 SDOperand Result = isLoad
3508 ? DAG.getIndexedLoad(SDOperand(N,0), BasePtr, Offset, AM)
3509 : DAG.getIndexedStore(SDOperand(N,0), BasePtr, Offset, AM);
3510 ++PostIndexedNodes;
3511 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +00003512 DOUT << "\nReplacing.5 "; DEBUG(N->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +00003513 DOUT << "\nWith: "; DEBUG(Result.Val->dump(&DAG));
3514 DOUT << '\n';
Chris Lattner9f1794e2006-11-11 00:56:29 +00003515 std::vector<SDNode*> NowDead;
3516 if (isLoad) {
3517 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(0),
Chris Lattner448f2192006-11-11 00:39:41 +00003518 NowDead);
Chris Lattner9f1794e2006-11-11 00:56:29 +00003519 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1), Result.getValue(2),
3520 NowDead);
3521 } else {
3522 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(1),
3523 NowDead);
Chris Lattner448f2192006-11-11 00:39:41 +00003524 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00003525
3526 // Nodes can end up on the worklist more than once. Make sure we do
3527 // not process a node that has been replaced.
3528 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
3529 removeFromWorkList(NowDead[i]);
3530 // Finally, since the node is now dead, remove it from the graph.
3531 DAG.DeleteNode(N);
3532
3533 // Replace the uses of Use with uses of the updated base value.
3534 DAG.ReplaceAllUsesOfValueWith(SDOperand(Op, 0),
3535 Result.getValue(isLoad ? 1 : 0),
3536 NowDead);
3537 removeFromWorkList(Op);
3538 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
3539 removeFromWorkList(NowDead[i]);
3540 DAG.DeleteNode(Op);
3541
3542 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00003543 }
3544 }
3545 }
3546 return false;
3547}
3548
3549
Chris Lattner01a22022005-10-10 22:04:48 +00003550SDOperand DAGCombiner::visitLOAD(SDNode *N) {
Evan Cheng466685d2006-10-09 20:57:25 +00003551 LoadSDNode *LD = cast<LoadSDNode>(N);
3552 SDOperand Chain = LD->getChain();
3553 SDOperand Ptr = LD->getBasePtr();
Evan Cheng45a7ca92007-05-01 00:38:21 +00003554
3555 // If load is not volatile and there are no uses of the loaded value (and
3556 // the updated indexed value in case of indexed loads), change uses of the
3557 // chain value into uses of the chain input (i.e. delete the dead load).
3558 if (!LD->isVolatile()) {
Evan Cheng498f5592007-05-01 08:53:39 +00003559 if (N->getValueType(1) == MVT::Other) {
3560 // Unindexed loads.
3561 if (N->hasNUsesOfValue(0, 0))
3562 return CombineTo(N, DAG.getNode(ISD::UNDEF, N->getValueType(0)), Chain);
3563 } else {
3564 // Indexed loads.
3565 assert(N->getValueType(2) == MVT::Other && "Malformed indexed loads?");
3566 if (N->hasNUsesOfValue(0, 0) && N->hasNUsesOfValue(0, 1)) {
3567 SDOperand Undef0 = DAG.getNode(ISD::UNDEF, N->getValueType(0));
3568 SDOperand Undef1 = DAG.getNode(ISD::UNDEF, N->getValueType(1));
3569 SDOperand To[] = { Undef0, Undef1, Chain };
3570 return CombineTo(N, To, 3);
Evan Cheng45a7ca92007-05-01 00:38:21 +00003571 }
Evan Cheng45a7ca92007-05-01 00:38:21 +00003572 }
3573 }
Chris Lattner01a22022005-10-10 22:04:48 +00003574
3575 // If this load is directly stored, replace the load value with the stored
3576 // value.
3577 // TODO: Handle store large -> read small portion.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00003578 // TODO: Handle TRUNCSTORE/LOADEXT
3579 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00003580 if (ISD::isNON_TRUNCStore(Chain.Val)) {
3581 StoreSDNode *PrevST = cast<StoreSDNode>(Chain);
3582 if (PrevST->getBasePtr() == Ptr &&
3583 PrevST->getValue().getValueType() == N->getValueType(0))
Jim Laskeyc2b19f32006-10-11 17:47:52 +00003584 return CombineTo(N, Chain.getOperand(1), Chain);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003585 }
Jim Laskeyc2b19f32006-10-11 17:47:52 +00003586 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00003587
Jim Laskey7ca56af2006-10-11 13:47:09 +00003588 if (CombinerAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00003589 // Walk up chain skipping non-aliasing memory nodes.
3590 SDOperand BetterChain = FindBetterChain(N, Chain);
3591
Jim Laskey6ff23e52006-10-04 16:53:27 +00003592 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00003593 if (Chain != BetterChain) {
Jim Laskeyc2b19f32006-10-11 17:47:52 +00003594 SDOperand ReplLoad;
3595
Jim Laskey279f0532006-09-25 16:29:54 +00003596 // Replace the chain to void dependency.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00003597 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
3598 ReplLoad = DAG.getLoad(N->getValueType(0), BetterChain, Ptr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00003599 LD->getSrcValue(), LD->getSrcValueOffset(),
3600 LD->isVolatile(), LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00003601 } else {
3602 ReplLoad = DAG.getExtLoad(LD->getExtensionType(),
3603 LD->getValueType(0),
3604 BetterChain, Ptr, LD->getSrcValue(),
3605 LD->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003606 LD->getLoadedVT(),
3607 LD->isVolatile(),
3608 LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00003609 }
Jim Laskey279f0532006-09-25 16:29:54 +00003610
Jim Laskey6ff23e52006-10-04 16:53:27 +00003611 // Create token factor to keep old chain connected.
Jim Laskey288af5e2006-09-25 19:32:58 +00003612 SDOperand Token = DAG.getNode(ISD::TokenFactor, MVT::Other,
3613 Chain, ReplLoad.getValue(1));
Jim Laskey6ff23e52006-10-04 16:53:27 +00003614
Jim Laskey274062c2006-10-13 23:32:28 +00003615 // Replace uses with load result and token factor. Don't add users
3616 // to work list.
3617 return CombineTo(N, ReplLoad.getValue(0), Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00003618 }
3619 }
3620
Evan Cheng7fc033a2006-11-03 03:06:21 +00003621 // Try transforming N to an indexed load.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00003622 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Evan Cheng7fc033a2006-11-03 03:06:21 +00003623 return SDOperand(N, 0);
3624
Chris Lattner01a22022005-10-10 22:04:48 +00003625 return SDOperand();
3626}
3627
Chris Lattner87514ca2005-10-10 22:31:19 +00003628SDOperand DAGCombiner::visitSTORE(SDNode *N) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00003629 StoreSDNode *ST = cast<StoreSDNode>(N);
3630 SDOperand Chain = ST->getChain();
3631 SDOperand Value = ST->getValue();
3632 SDOperand Ptr = ST->getBasePtr();
Jim Laskey7aed46c2006-10-11 18:55:16 +00003633
Evan Cheng59d5b682007-05-07 21:27:48 +00003634 // If this is a store of a bit convert, store the input value if the
Evan Cheng2c4f9432007-05-09 21:49:47 +00003635 // resultant store does not need a higher alignment than the original.
Dale Johannesen98a6c622007-05-16 22:45:30 +00003636 if (Value.getOpcode() == ISD::BIT_CONVERT && !ST->isTruncatingStore() &&
3637 ST->getAddressingMode() == ISD::UNINDEXED) {
Evan Cheng59d5b682007-05-07 21:27:48 +00003638 unsigned Align = ST->getAlignment();
3639 MVT::ValueType SVT = Value.getOperand(0).getValueType();
3640 unsigned OrigAlign = TLI.getTargetMachine().getTargetData()->
Dan Gohmanfcc4dd92007-05-18 18:41:29 +00003641 getABITypeAlignment(MVT::getTypeForValueType(SVT));
Evan Chengc2cd2b22007-05-07 21:36:06 +00003642 if (Align <= OrigAlign && TLI.isOperationLegal(ISD::STORE, SVT))
Evan Cheng59d5b682007-05-07 21:27:48 +00003643 return DAG.getStore(Chain, Value.getOperand(0), Ptr, ST->getSrcValue(),
3644 ST->getSrcValueOffset());
Jim Laskey279f0532006-09-25 16:29:54 +00003645 }
3646
Nate Begeman2cbba892006-12-11 02:23:46 +00003647 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Nate Begeman2cbba892006-12-11 02:23:46 +00003648 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Value)) {
Evan Cheng25ece662006-12-11 17:25:19 +00003649 if (Value.getOpcode() != ISD::TargetConstantFP) {
3650 SDOperand Tmp;
Chris Lattner62be1a72006-12-12 04:16:14 +00003651 switch (CFP->getValueType(0)) {
3652 default: assert(0 && "Unknown FP type");
3653 case MVT::f32:
3654 if (!AfterLegalize || TLI.isTypeLegal(MVT::i32)) {
3655 Tmp = DAG.getConstant(FloatToBits(CFP->getValue()), MVT::i32);
3656 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
3657 ST->getSrcValueOffset());
3658 }
3659 break;
3660 case MVT::f64:
3661 if (!AfterLegalize || TLI.isTypeLegal(MVT::i64)) {
3662 Tmp = DAG.getConstant(DoubleToBits(CFP->getValue()), MVT::i64);
3663 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
3664 ST->getSrcValueOffset());
3665 } else if (TLI.isTypeLegal(MVT::i32)) {
3666 // Many FP stores are not make apparent until after legalize, e.g. for
3667 // argument passing. Since this is so common, custom legalize the
3668 // 64-bit integer store into two 32-bit stores.
3669 uint64_t Val = DoubleToBits(CFP->getValue());
3670 SDOperand Lo = DAG.getConstant(Val & 0xFFFFFFFF, MVT::i32);
3671 SDOperand Hi = DAG.getConstant(Val >> 32, MVT::i32);
3672 if (!TLI.isLittleEndian()) std::swap(Lo, Hi);
3673
3674 SDOperand St0 = DAG.getStore(Chain, Lo, Ptr, ST->getSrcValue(),
3675 ST->getSrcValueOffset());
3676 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3677 DAG.getConstant(4, Ptr.getValueType()));
3678 SDOperand St1 = DAG.getStore(Chain, Hi, Ptr, ST->getSrcValue(),
3679 ST->getSrcValueOffset()+4);
3680 return DAG.getNode(ISD::TokenFactor, MVT::Other, St0, St1);
3681 }
3682 break;
Evan Cheng25ece662006-12-11 17:25:19 +00003683 }
Nate Begeman2cbba892006-12-11 02:23:46 +00003684 }
Nate Begeman2cbba892006-12-11 02:23:46 +00003685 }
3686
Jim Laskey279f0532006-09-25 16:29:54 +00003687 if (CombinerAA) {
3688 // Walk up chain skipping non-aliasing memory nodes.
3689 SDOperand BetterChain = FindBetterChain(N, Chain);
3690
Jim Laskey6ff23e52006-10-04 16:53:27 +00003691 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00003692 if (Chain != BetterChain) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00003693 // Replace the chain to avoid dependency.
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00003694 SDOperand ReplStore;
3695 if (ST->isTruncatingStore()) {
3696 ReplStore = DAG.getTruncStore(BetterChain, Value, Ptr,
3697 ST->getSrcValue(),ST->getSrcValueOffset(), ST->getStoredVT());
3698 } else {
3699 ReplStore = DAG.getStore(BetterChain, Value, Ptr,
3700 ST->getSrcValue(), ST->getSrcValueOffset());
3701 }
3702
Jim Laskey279f0532006-09-25 16:29:54 +00003703 // Create token to keep both nodes around.
Jim Laskey274062c2006-10-13 23:32:28 +00003704 SDOperand Token =
3705 DAG.getNode(ISD::TokenFactor, MVT::Other, Chain, ReplStore);
3706
3707 // Don't add users to work list.
3708 return CombineTo(N, Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00003709 }
Jim Laskeyd1aed7a2006-09-21 16:28:59 +00003710 }
Chris Lattnerc33baaa2005-12-23 05:48:07 +00003711
Evan Cheng33dbedc2006-11-05 09:31:14 +00003712 // Try transforming N to an indexed store.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00003713 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Evan Cheng33dbedc2006-11-05 09:31:14 +00003714 return SDOperand(N, 0);
3715
Chris Lattner87514ca2005-10-10 22:31:19 +00003716 return SDOperand();
3717}
3718
Chris Lattnerca242442006-03-19 01:27:56 +00003719SDOperand DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
3720 SDOperand InVec = N->getOperand(0);
3721 SDOperand InVal = N->getOperand(1);
3722 SDOperand EltNo = N->getOperand(2);
3723
3724 // If the invec is a BUILD_VECTOR and if EltNo is a constant, build a new
3725 // vector with the inserted element.
3726 if (InVec.getOpcode() == ISD::BUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
3727 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003728 SmallVector<SDOperand, 8> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
Chris Lattnerca242442006-03-19 01:27:56 +00003729 if (Elt < Ops.size())
3730 Ops[Elt] = InVal;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003731 return DAG.getNode(ISD::BUILD_VECTOR, InVec.getValueType(),
3732 &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00003733 }
3734
3735 return SDOperand();
3736}
3737
3738SDOperand DAGCombiner::visitVINSERT_VECTOR_ELT(SDNode *N) {
3739 SDOperand InVec = N->getOperand(0);
3740 SDOperand InVal = N->getOperand(1);
3741 SDOperand EltNo = N->getOperand(2);
3742 SDOperand NumElts = N->getOperand(3);
3743 SDOperand EltType = N->getOperand(4);
3744
3745 // If the invec is a VBUILD_VECTOR and if EltNo is a constant, build a new
3746 // vector with the inserted element.
3747 if (InVec.getOpcode() == ISD::VBUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
3748 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003749 SmallVector<SDOperand, 8> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
Chris Lattnerca242442006-03-19 01:27:56 +00003750 if (Elt < Ops.size()-2)
3751 Ops[Elt] = InVal;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003752 return DAG.getNode(ISD::VBUILD_VECTOR, InVec.getValueType(),
3753 &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00003754 }
3755
3756 return SDOperand();
3757}
3758
Chris Lattnerd7648c82006-03-28 20:28:38 +00003759SDOperand DAGCombiner::visitVBUILD_VECTOR(SDNode *N) {
3760 unsigned NumInScalars = N->getNumOperands()-2;
3761 SDOperand NumElts = N->getOperand(NumInScalars);
3762 SDOperand EltType = N->getOperand(NumInScalars+1);
3763
3764 // Check to see if this is a VBUILD_VECTOR of a bunch of VEXTRACT_VECTOR_ELT
3765 // operations. If so, and if the EXTRACT_ELT vector inputs come from at most
3766 // two distinct vectors, turn this into a shuffle node.
3767 SDOperand VecIn1, VecIn2;
3768 for (unsigned i = 0; i != NumInScalars; ++i) {
3769 // Ignore undef inputs.
3770 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
3771
3772 // If this input is something other than a VEXTRACT_VECTOR_ELT with a
3773 // constant index, bail out.
3774 if (N->getOperand(i).getOpcode() != ISD::VEXTRACT_VECTOR_ELT ||
3775 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
3776 VecIn1 = VecIn2 = SDOperand(0, 0);
3777 break;
3778 }
3779
3780 // If the input vector type disagrees with the result of the vbuild_vector,
3781 // we can't make a shuffle.
3782 SDOperand ExtractedFromVec = N->getOperand(i).getOperand(0);
3783 if (*(ExtractedFromVec.Val->op_end()-2) != NumElts ||
3784 *(ExtractedFromVec.Val->op_end()-1) != EltType) {
3785 VecIn1 = VecIn2 = SDOperand(0, 0);
3786 break;
3787 }
3788
3789 // Otherwise, remember this. We allow up to two distinct input vectors.
3790 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
3791 continue;
3792
3793 if (VecIn1.Val == 0) {
3794 VecIn1 = ExtractedFromVec;
3795 } else if (VecIn2.Val == 0) {
3796 VecIn2 = ExtractedFromVec;
3797 } else {
3798 // Too many inputs.
3799 VecIn1 = VecIn2 = SDOperand(0, 0);
3800 break;
3801 }
3802 }
3803
3804 // If everything is good, we can make a shuffle operation.
3805 if (VecIn1.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003806 SmallVector<SDOperand, 8> BuildVecIndices;
Chris Lattnerd7648c82006-03-28 20:28:38 +00003807 for (unsigned i = 0; i != NumInScalars; ++i) {
3808 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
Evan Cheng597a3bd2007-01-20 10:10:26 +00003809 BuildVecIndices.push_back(DAG.getNode(ISD::UNDEF, TLI.getPointerTy()));
Chris Lattnerd7648c82006-03-28 20:28:38 +00003810 continue;
3811 }
3812
3813 SDOperand Extract = N->getOperand(i);
3814
3815 // If extracting from the first vector, just use the index directly.
3816 if (Extract.getOperand(0) == VecIn1) {
3817 BuildVecIndices.push_back(Extract.getOperand(1));
3818 continue;
3819 }
3820
3821 // Otherwise, use InIdx + VecSize
3822 unsigned Idx = cast<ConstantSDNode>(Extract.getOperand(1))->getValue();
Evan Cheng597a3bd2007-01-20 10:10:26 +00003823 BuildVecIndices.push_back(DAG.getConstant(Idx+NumInScalars,
3824 TLI.getPointerTy()));
Chris Lattnerd7648c82006-03-28 20:28:38 +00003825 }
3826
3827 // Add count and size info.
3828 BuildVecIndices.push_back(NumElts);
Evan Cheng597a3bd2007-01-20 10:10:26 +00003829 BuildVecIndices.push_back(DAG.getValueType(TLI.getPointerTy()));
Chris Lattnerd7648c82006-03-28 20:28:38 +00003830
3831 // Return the new VVECTOR_SHUFFLE node.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003832 SDOperand Ops[5];
3833 Ops[0] = VecIn1;
Chris Lattnercef896e2006-03-28 22:19:47 +00003834 if (VecIn2.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003835 Ops[1] = VecIn2;
Chris Lattnercef896e2006-03-28 22:19:47 +00003836 } else {
Dan Gohmanfcc4dd92007-05-18 18:41:29 +00003837 // Use an undef vbuild_vector as input for the second operand.
Chris Lattnercef896e2006-03-28 22:19:47 +00003838 std::vector<SDOperand> UnOps(NumInScalars,
3839 DAG.getNode(ISD::UNDEF,
3840 cast<VTSDNode>(EltType)->getVT()));
3841 UnOps.push_back(NumElts);
3842 UnOps.push_back(EltType);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003843 Ops[1] = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
3844 &UnOps[0], UnOps.size());
3845 AddToWorkList(Ops[1].Val);
Chris Lattnercef896e2006-03-28 22:19:47 +00003846 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003847 Ops[2] = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
3848 &BuildVecIndices[0], BuildVecIndices.size());
3849 Ops[3] = NumElts;
3850 Ops[4] = EltType;
3851 return DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector, Ops, 5);
Chris Lattnerd7648c82006-03-28 20:28:38 +00003852 }
3853
3854 return SDOperand();
3855}
3856
Chris Lattner66445d32006-03-28 22:11:53 +00003857SDOperand DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
Chris Lattnerf1d0c622006-03-31 22:16:43 +00003858 SDOperand ShufMask = N->getOperand(2);
3859 unsigned NumElts = ShufMask.getNumOperands();
3860
3861 // If the shuffle mask is an identity operation on the LHS, return the LHS.
3862 bool isIdentity = true;
3863 for (unsigned i = 0; i != NumElts; ++i) {
3864 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
3865 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
3866 isIdentity = false;
3867 break;
3868 }
3869 }
3870 if (isIdentity) return N->getOperand(0);
3871
3872 // If the shuffle mask is an identity operation on the RHS, return the RHS.
3873 isIdentity = true;
3874 for (unsigned i = 0; i != NumElts; ++i) {
3875 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
3876 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
3877 isIdentity = false;
3878 break;
3879 }
3880 }
3881 if (isIdentity) return N->getOperand(1);
Evan Chenge7bec0d2006-07-20 22:44:41 +00003882
3883 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
3884 // needed at all.
3885 bool isUnary = true;
Evan Cheng917ec982006-07-21 08:25:53 +00003886 bool isSplat = true;
Evan Chenge7bec0d2006-07-20 22:44:41 +00003887 int VecNum = -1;
Reid Spencer9160a6a2006-07-25 20:44:41 +00003888 unsigned BaseIdx = 0;
Evan Chenge7bec0d2006-07-20 22:44:41 +00003889 for (unsigned i = 0; i != NumElts; ++i)
3890 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
3891 unsigned Idx = cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue();
3892 int V = (Idx < NumElts) ? 0 : 1;
Evan Cheng917ec982006-07-21 08:25:53 +00003893 if (VecNum == -1) {
Evan Chenge7bec0d2006-07-20 22:44:41 +00003894 VecNum = V;
Evan Cheng917ec982006-07-21 08:25:53 +00003895 BaseIdx = Idx;
3896 } else {
3897 if (BaseIdx != Idx)
3898 isSplat = false;
3899 if (VecNum != V) {
3900 isUnary = false;
3901 break;
3902 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00003903 }
3904 }
3905
3906 SDOperand N0 = N->getOperand(0);
3907 SDOperand N1 = N->getOperand(1);
3908 // Normalize unary shuffle so the RHS is undef.
3909 if (isUnary && VecNum == 1)
3910 std::swap(N0, N1);
3911
Evan Cheng917ec982006-07-21 08:25:53 +00003912 // If it is a splat, check if the argument vector is a build_vector with
3913 // all scalar elements the same.
3914 if (isSplat) {
3915 SDNode *V = N0.Val;
3916 if (V->getOpcode() == ISD::BIT_CONVERT)
3917 V = V->getOperand(0).Val;
3918 if (V->getOpcode() == ISD::BUILD_VECTOR) {
3919 unsigned NumElems = V->getNumOperands()-2;
3920 if (NumElems > BaseIdx) {
3921 SDOperand Base;
3922 bool AllSame = true;
3923 for (unsigned i = 0; i != NumElems; ++i) {
3924 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
3925 Base = V->getOperand(i);
3926 break;
3927 }
3928 }
3929 // Splat of <u, u, u, u>, return <u, u, u, u>
3930 if (!Base.Val)
3931 return N0;
3932 for (unsigned i = 0; i != NumElems; ++i) {
3933 if (V->getOperand(i).getOpcode() != ISD::UNDEF &&
3934 V->getOperand(i) != Base) {
3935 AllSame = false;
3936 break;
3937 }
3938 }
3939 // Splat of <x, x, x, x>, return <x, x, x, x>
3940 if (AllSame)
3941 return N0;
3942 }
3943 }
3944 }
3945
Evan Chenge7bec0d2006-07-20 22:44:41 +00003946 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
3947 // into an undef.
3948 if (isUnary || N0 == N1) {
3949 if (N0.getOpcode() == ISD::UNDEF)
Evan Chengc04766a2006-04-06 23:20:43 +00003950 return DAG.getNode(ISD::UNDEF, N->getValueType(0));
Chris Lattner66445d32006-03-28 22:11:53 +00003951 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
3952 // first operand.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003953 SmallVector<SDOperand, 8> MappedOps;
Chris Lattner66445d32006-03-28 22:11:53 +00003954 for (unsigned i = 0, e = ShufMask.getNumOperands(); i != e; ++i) {
Evan Chengc04766a2006-04-06 23:20:43 +00003955 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
3956 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) {
3957 MappedOps.push_back(ShufMask.getOperand(i));
3958 } else {
Chris Lattner66445d32006-03-28 22:11:53 +00003959 unsigned NewIdx =
3960 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
3961 MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32));
Chris Lattner66445d32006-03-28 22:11:53 +00003962 }
3963 }
3964 ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMask.getValueType(),
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003965 &MappedOps[0], MappedOps.size());
Chris Lattner3e104b12006-04-08 04:15:24 +00003966 AddToWorkList(ShufMask.Val);
Chris Lattner66445d32006-03-28 22:11:53 +00003967 return DAG.getNode(ISD::VECTOR_SHUFFLE, N->getValueType(0),
Evan Chenge7bec0d2006-07-20 22:44:41 +00003968 N0,
Chris Lattner66445d32006-03-28 22:11:53 +00003969 DAG.getNode(ISD::UNDEF, N->getValueType(0)),
3970 ShufMask);
3971 }
3972
3973 return SDOperand();
3974}
3975
Chris Lattnerf1d0c622006-03-31 22:16:43 +00003976SDOperand DAGCombiner::visitVVECTOR_SHUFFLE(SDNode *N) {
3977 SDOperand ShufMask = N->getOperand(2);
3978 unsigned NumElts = ShufMask.getNumOperands()-2;
3979
3980 // If the shuffle mask is an identity operation on the LHS, return the LHS.
3981 bool isIdentity = true;
3982 for (unsigned i = 0; i != NumElts; ++i) {
3983 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
3984 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
3985 isIdentity = false;
3986 break;
3987 }
3988 }
3989 if (isIdentity) return N->getOperand(0);
3990
3991 // If the shuffle mask is an identity operation on the RHS, return the RHS.
3992 isIdentity = true;
3993 for (unsigned i = 0; i != NumElts; ++i) {
3994 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
3995 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
3996 isIdentity = false;
3997 break;
3998 }
3999 }
4000 if (isIdentity) return N->getOperand(1);
4001
Evan Chenge7bec0d2006-07-20 22:44:41 +00004002 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
4003 // needed at all.
4004 bool isUnary = true;
Evan Cheng917ec982006-07-21 08:25:53 +00004005 bool isSplat = true;
Evan Chenge7bec0d2006-07-20 22:44:41 +00004006 int VecNum = -1;
Reid Spencer9160a6a2006-07-25 20:44:41 +00004007 unsigned BaseIdx = 0;
Evan Chenge7bec0d2006-07-20 22:44:41 +00004008 for (unsigned i = 0; i != NumElts; ++i)
4009 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
4010 unsigned Idx = cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue();
4011 int V = (Idx < NumElts) ? 0 : 1;
Evan Cheng917ec982006-07-21 08:25:53 +00004012 if (VecNum == -1) {
Evan Chenge7bec0d2006-07-20 22:44:41 +00004013 VecNum = V;
Evan Cheng917ec982006-07-21 08:25:53 +00004014 BaseIdx = Idx;
4015 } else {
4016 if (BaseIdx != Idx)
4017 isSplat = false;
4018 if (VecNum != V) {
4019 isUnary = false;
4020 break;
4021 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00004022 }
4023 }
4024
4025 SDOperand N0 = N->getOperand(0);
4026 SDOperand N1 = N->getOperand(1);
4027 // Normalize unary shuffle so the RHS is undef.
4028 if (isUnary && VecNum == 1)
4029 std::swap(N0, N1);
4030
Evan Cheng917ec982006-07-21 08:25:53 +00004031 // If it is a splat, check if the argument vector is a build_vector with
4032 // all scalar elements the same.
4033 if (isSplat) {
4034 SDNode *V = N0.Val;
Evan Cheng59569222006-10-16 22:49:37 +00004035
4036 // If this is a vbit convert that changes the element type of the vector but
4037 // not the number of vector elements, look through it. Be careful not to
4038 // look though conversions that change things like v4f32 to v2f64.
4039 if (V->getOpcode() == ISD::VBIT_CONVERT) {
4040 SDOperand ConvInput = V->getOperand(0);
Evan Cheng5d04a1a2006-10-17 17:06:35 +00004041 if (ConvInput.getValueType() == MVT::Vector &&
4042 NumElts ==
Evan Cheng59569222006-10-16 22:49:37 +00004043 ConvInput.getConstantOperandVal(ConvInput.getNumOperands()-2))
4044 V = ConvInput.Val;
4045 }
4046
Evan Cheng917ec982006-07-21 08:25:53 +00004047 if (V->getOpcode() == ISD::VBUILD_VECTOR) {
4048 unsigned NumElems = V->getNumOperands()-2;
4049 if (NumElems > BaseIdx) {
4050 SDOperand Base;
4051 bool AllSame = true;
4052 for (unsigned i = 0; i != NumElems; ++i) {
4053 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
4054 Base = V->getOperand(i);
4055 break;
4056 }
4057 }
4058 // Splat of <u, u, u, u>, return <u, u, u, u>
4059 if (!Base.Val)
4060 return N0;
4061 for (unsigned i = 0; i != NumElems; ++i) {
4062 if (V->getOperand(i).getOpcode() != ISD::UNDEF &&
4063 V->getOperand(i) != Base) {
4064 AllSame = false;
4065 break;
4066 }
4067 }
4068 // Splat of <x, x, x, x>, return <x, x, x, x>
4069 if (AllSame)
4070 return N0;
4071 }
4072 }
4073 }
4074
Evan Chenge7bec0d2006-07-20 22:44:41 +00004075 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
4076 // into an undef.
4077 if (isUnary || N0 == N1) {
Chris Lattner17614ea2006-04-08 05:34:25 +00004078 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
4079 // first operand.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004080 SmallVector<SDOperand, 8> MappedOps;
Chris Lattner17614ea2006-04-08 05:34:25 +00004081 for (unsigned i = 0; i != NumElts; ++i) {
4082 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
4083 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) {
4084 MappedOps.push_back(ShufMask.getOperand(i));
4085 } else {
4086 unsigned NewIdx =
4087 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
4088 MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32));
4089 }
4090 }
4091 // Add the type/#elts values.
4092 MappedOps.push_back(ShufMask.getOperand(NumElts));
4093 MappedOps.push_back(ShufMask.getOperand(NumElts+1));
4094
4095 ShufMask = DAG.getNode(ISD::VBUILD_VECTOR, ShufMask.getValueType(),
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004096 &MappedOps[0], MappedOps.size());
Chris Lattner17614ea2006-04-08 05:34:25 +00004097 AddToWorkList(ShufMask.Val);
4098
4099 // Build the undef vector.
4100 SDOperand UDVal = DAG.getNode(ISD::UNDEF, MappedOps[0].getValueType());
4101 for (unsigned i = 0; i != NumElts; ++i)
4102 MappedOps[i] = UDVal;
Evan Chenge7bec0d2006-07-20 22:44:41 +00004103 MappedOps[NumElts ] = *(N0.Val->op_end()-2);
4104 MappedOps[NumElts+1] = *(N0.Val->op_end()-1);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004105 UDVal = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
4106 &MappedOps[0], MappedOps.size());
Chris Lattner17614ea2006-04-08 05:34:25 +00004107
4108 return DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector,
Evan Chenge7bec0d2006-07-20 22:44:41 +00004109 N0, UDVal, ShufMask,
Chris Lattner17614ea2006-04-08 05:34:25 +00004110 MappedOps[NumElts], MappedOps[NumElts+1]);
4111 }
4112
Chris Lattnerf1d0c622006-03-31 22:16:43 +00004113 return SDOperand();
4114}
4115
Evan Cheng44f1f092006-04-20 08:56:16 +00004116/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
4117/// a VAND to a vector_shuffle with the destination vector and a zero vector.
4118/// e.g. VAND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
4119/// vector_shuffle V, Zero, <0, 4, 2, 4>
4120SDOperand DAGCombiner::XformToShuffleWithZero(SDNode *N) {
4121 SDOperand LHS = N->getOperand(0);
4122 SDOperand RHS = N->getOperand(1);
4123 if (N->getOpcode() == ISD::VAND) {
4124 SDOperand DstVecSize = *(LHS.Val->op_end()-2);
4125 SDOperand DstVecEVT = *(LHS.Val->op_end()-1);
4126 if (RHS.getOpcode() == ISD::VBIT_CONVERT)
4127 RHS = RHS.getOperand(0);
4128 if (RHS.getOpcode() == ISD::VBUILD_VECTOR) {
4129 std::vector<SDOperand> IdxOps;
4130 unsigned NumOps = RHS.getNumOperands();
4131 unsigned NumElts = NumOps-2;
4132 MVT::ValueType EVT = cast<VTSDNode>(RHS.getOperand(NumOps-1))->getVT();
4133 for (unsigned i = 0; i != NumElts; ++i) {
4134 SDOperand Elt = RHS.getOperand(i);
4135 if (!isa<ConstantSDNode>(Elt))
4136 return SDOperand();
4137 else if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
4138 IdxOps.push_back(DAG.getConstant(i, EVT));
4139 else if (cast<ConstantSDNode>(Elt)->isNullValue())
4140 IdxOps.push_back(DAG.getConstant(NumElts, EVT));
4141 else
4142 return SDOperand();
4143 }
4144
4145 // Let's see if the target supports this vector_shuffle.
4146 if (!TLI.isVectorClearMaskLegal(IdxOps, EVT, DAG))
4147 return SDOperand();
4148
4149 // Return the new VVECTOR_SHUFFLE node.
4150 SDOperand NumEltsNode = DAG.getConstant(NumElts, MVT::i32);
4151 SDOperand EVTNode = DAG.getValueType(EVT);
4152 std::vector<SDOperand> Ops;
Chris Lattner516b9622006-09-14 20:50:57 +00004153 LHS = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, LHS, NumEltsNode,
4154 EVTNode);
Evan Cheng44f1f092006-04-20 08:56:16 +00004155 Ops.push_back(LHS);
4156 AddToWorkList(LHS.Val);
4157 std::vector<SDOperand> ZeroOps(NumElts, DAG.getConstant(0, EVT));
4158 ZeroOps.push_back(NumEltsNode);
4159 ZeroOps.push_back(EVTNode);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004160 Ops.push_back(DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
4161 &ZeroOps[0], ZeroOps.size()));
Evan Cheng44f1f092006-04-20 08:56:16 +00004162 IdxOps.push_back(NumEltsNode);
4163 IdxOps.push_back(EVTNode);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004164 Ops.push_back(DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
4165 &IdxOps[0], IdxOps.size()));
Evan Cheng44f1f092006-04-20 08:56:16 +00004166 Ops.push_back(NumEltsNode);
4167 Ops.push_back(EVTNode);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004168 SDOperand Result = DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector,
4169 &Ops[0], Ops.size());
Evan Cheng44f1f092006-04-20 08:56:16 +00004170 if (NumEltsNode != DstVecSize || EVTNode != DstVecEVT) {
4171 Result = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Result,
4172 DstVecSize, DstVecEVT);
4173 }
4174 return Result;
4175 }
4176 }
4177 return SDOperand();
4178}
4179
Chris Lattneredab1b92006-04-02 03:25:57 +00004180/// visitVBinOp - Visit a binary vector operation, like VADD. IntOp indicates
4181/// the scalar operation of the vop if it is operating on an integer vector
4182/// (e.g. ADD) and FPOp indicates the FP version (e.g. FADD).
4183SDOperand DAGCombiner::visitVBinOp(SDNode *N, ISD::NodeType IntOp,
4184 ISD::NodeType FPOp) {
4185 MVT::ValueType EltType = cast<VTSDNode>(*(N->op_end()-1))->getVT();
4186 ISD::NodeType ScalarOp = MVT::isInteger(EltType) ? IntOp : FPOp;
4187 SDOperand LHS = N->getOperand(0);
4188 SDOperand RHS = N->getOperand(1);
Evan Cheng44f1f092006-04-20 08:56:16 +00004189 SDOperand Shuffle = XformToShuffleWithZero(N);
4190 if (Shuffle.Val) return Shuffle;
4191
Chris Lattneredab1b92006-04-02 03:25:57 +00004192 // If the LHS and RHS are VBUILD_VECTOR nodes, see if we can constant fold
4193 // this operation.
4194 if (LHS.getOpcode() == ISD::VBUILD_VECTOR &&
4195 RHS.getOpcode() == ISD::VBUILD_VECTOR) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004196 SmallVector<SDOperand, 8> Ops;
Chris Lattneredab1b92006-04-02 03:25:57 +00004197 for (unsigned i = 0, e = LHS.getNumOperands()-2; i != e; ++i) {
4198 SDOperand LHSOp = LHS.getOperand(i);
4199 SDOperand RHSOp = RHS.getOperand(i);
4200 // If these two elements can't be folded, bail out.
4201 if ((LHSOp.getOpcode() != ISD::UNDEF &&
4202 LHSOp.getOpcode() != ISD::Constant &&
4203 LHSOp.getOpcode() != ISD::ConstantFP) ||
4204 (RHSOp.getOpcode() != ISD::UNDEF &&
4205 RHSOp.getOpcode() != ISD::Constant &&
4206 RHSOp.getOpcode() != ISD::ConstantFP))
4207 break;
Evan Cheng7b336a82006-05-31 06:08:35 +00004208 // Can't fold divide by zero.
4209 if (N->getOpcode() == ISD::VSDIV || N->getOpcode() == ISD::VUDIV) {
4210 if ((RHSOp.getOpcode() == ISD::Constant &&
4211 cast<ConstantSDNode>(RHSOp.Val)->isNullValue()) ||
4212 (RHSOp.getOpcode() == ISD::ConstantFP &&
4213 !cast<ConstantFPSDNode>(RHSOp.Val)->getValue()))
4214 break;
4215 }
Chris Lattneredab1b92006-04-02 03:25:57 +00004216 Ops.push_back(DAG.getNode(ScalarOp, EltType, LHSOp, RHSOp));
Chris Lattner3e104b12006-04-08 04:15:24 +00004217 AddToWorkList(Ops.back().Val);
Chris Lattneredab1b92006-04-02 03:25:57 +00004218 assert((Ops.back().getOpcode() == ISD::UNDEF ||
4219 Ops.back().getOpcode() == ISD::Constant ||
4220 Ops.back().getOpcode() == ISD::ConstantFP) &&
4221 "Scalar binop didn't fold!");
4222 }
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00004223
4224 if (Ops.size() == LHS.getNumOperands()-2) {
4225 Ops.push_back(*(LHS.Val->op_end()-2));
4226 Ops.push_back(*(LHS.Val->op_end()-1));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004227 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00004228 }
Chris Lattneredab1b92006-04-02 03:25:57 +00004229 }
4230
4231 return SDOperand();
4232}
4233
Nate Begeman44728a72005-09-19 22:34:01 +00004234SDOperand DAGCombiner::SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2){
Nate Begemanf845b452005-10-08 00:29:44 +00004235 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
4236
4237 SDOperand SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
4238 cast<CondCodeSDNode>(N0.getOperand(2))->get());
4239 // If we got a simplified select_cc node back from SimplifySelectCC, then
4240 // break it down into a new SETCC node, and a new SELECT node, and then return
4241 // the SELECT node, since we were called with a SELECT node.
4242 if (SCC.Val) {
4243 // Check to see if we got a select_cc back (to turn into setcc/select).
4244 // Otherwise, just return whatever node we got back, like fabs.
4245 if (SCC.getOpcode() == ISD::SELECT_CC) {
4246 SDOperand SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
4247 SCC.getOperand(0), SCC.getOperand(1),
4248 SCC.getOperand(4));
Chris Lattner5750df92006-03-01 04:03:14 +00004249 AddToWorkList(SETCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004250 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
4251 SCC.getOperand(3), SETCC);
4252 }
4253 return SCC;
4254 }
Nate Begeman44728a72005-09-19 22:34:01 +00004255 return SDOperand();
4256}
4257
Chris Lattner40c62d52005-10-18 06:04:22 +00004258/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
4259/// are the two values being selected between, see if we can simplify the
Chris Lattner729c6d12006-05-27 00:43:02 +00004260/// select. Callers of this should assume that TheSelect is deleted if this
4261/// returns true. As such, they should return the appropriate thing (e.g. the
4262/// node) back to the top-level of the DAG combiner loop to avoid it being
4263/// looked at.
Chris Lattner40c62d52005-10-18 06:04:22 +00004264///
4265bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDOperand LHS,
4266 SDOperand RHS) {
4267
4268 // If this is a select from two identical things, try to pull the operation
4269 // through the select.
4270 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
Chris Lattner40c62d52005-10-18 06:04:22 +00004271 // If this is a load and the token chain is identical, replace the select
4272 // of two loads with a load through a select of the address to load from.
4273 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
4274 // constants have been dropped into the constant pool.
Evan Cheng466685d2006-10-09 20:57:25 +00004275 if (LHS.getOpcode() == ISD::LOAD &&
Chris Lattner40c62d52005-10-18 06:04:22 +00004276 // Token chains must be identical.
Evan Cheng466685d2006-10-09 20:57:25 +00004277 LHS.getOperand(0) == RHS.getOperand(0)) {
4278 LoadSDNode *LLD = cast<LoadSDNode>(LHS);
4279 LoadSDNode *RLD = cast<LoadSDNode>(RHS);
4280
4281 // If this is an EXTLOAD, the VT's must match.
Evan Cheng2e49f092006-10-11 07:10:22 +00004282 if (LLD->getLoadedVT() == RLD->getLoadedVT()) {
Evan Cheng466685d2006-10-09 20:57:25 +00004283 // FIXME: this conflates two src values, discarding one. This is not
4284 // the right thing to do, but nothing uses srcvalues now. When they do,
4285 // turn SrcValue into a list of locations.
4286 SDOperand Addr;
Chris Lattnerc4e664b2007-01-16 05:59:59 +00004287 if (TheSelect->getOpcode() == ISD::SELECT) {
4288 // Check that the condition doesn't reach either load. If so, folding
4289 // this will induce a cycle into the DAG.
4290 if (!LLD->isPredecessor(TheSelect->getOperand(0).Val) &&
4291 !RLD->isPredecessor(TheSelect->getOperand(0).Val)) {
4292 Addr = DAG.getNode(ISD::SELECT, LLD->getBasePtr().getValueType(),
4293 TheSelect->getOperand(0), LLD->getBasePtr(),
4294 RLD->getBasePtr());
4295 }
4296 } else {
4297 // Check that the condition doesn't reach either load. If so, folding
4298 // this will induce a cycle into the DAG.
4299 if (!LLD->isPredecessor(TheSelect->getOperand(0).Val) &&
4300 !RLD->isPredecessor(TheSelect->getOperand(0).Val) &&
4301 !LLD->isPredecessor(TheSelect->getOperand(1).Val) &&
4302 !RLD->isPredecessor(TheSelect->getOperand(1).Val)) {
4303 Addr = DAG.getNode(ISD::SELECT_CC, LLD->getBasePtr().getValueType(),
Evan Cheng466685d2006-10-09 20:57:25 +00004304 TheSelect->getOperand(0),
4305 TheSelect->getOperand(1),
4306 LLD->getBasePtr(), RLD->getBasePtr(),
4307 TheSelect->getOperand(4));
Chris Lattnerc4e664b2007-01-16 05:59:59 +00004308 }
Evan Cheng466685d2006-10-09 20:57:25 +00004309 }
Chris Lattnerc4e664b2007-01-16 05:59:59 +00004310
4311 if (Addr.Val) {
4312 SDOperand Load;
4313 if (LLD->getExtensionType() == ISD::NON_EXTLOAD)
4314 Load = DAG.getLoad(TheSelect->getValueType(0), LLD->getChain(),
4315 Addr,LLD->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00004316 LLD->getSrcValueOffset(),
4317 LLD->isVolatile(),
4318 LLD->getAlignment());
Chris Lattnerc4e664b2007-01-16 05:59:59 +00004319 else {
4320 Load = DAG.getExtLoad(LLD->getExtensionType(),
4321 TheSelect->getValueType(0),
4322 LLD->getChain(), Addr, LLD->getSrcValue(),
4323 LLD->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00004324 LLD->getLoadedVT(),
4325 LLD->isVolatile(),
4326 LLD->getAlignment());
Chris Lattnerc4e664b2007-01-16 05:59:59 +00004327 }
4328 // Users of the select now use the result of the load.
4329 CombineTo(TheSelect, Load);
4330
4331 // Users of the old loads now use the new load's chain. We know the
4332 // old-load value is dead now.
4333 CombineTo(LHS.Val, Load.getValue(0), Load.getValue(1));
4334 CombineTo(RHS.Val, Load.getValue(0), Load.getValue(1));
4335 return true;
4336 }
Evan Chengc5484282006-10-04 00:56:09 +00004337 }
Chris Lattner40c62d52005-10-18 06:04:22 +00004338 }
4339 }
4340
4341 return false;
4342}
4343
Nate Begeman44728a72005-09-19 22:34:01 +00004344SDOperand DAGCombiner::SimplifySelectCC(SDOperand N0, SDOperand N1,
4345 SDOperand N2, SDOperand N3,
Chris Lattner1eba01e2007-04-11 06:50:51 +00004346 ISD::CondCode CC, bool NotExtCompare) {
Nate Begemanf845b452005-10-08 00:29:44 +00004347
4348 MVT::ValueType VT = N2.getValueType();
Nate Begemanf845b452005-10-08 00:29:44 +00004349 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
4350 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
4351 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
4352
4353 // Determine if the condition we're dealing with is constant
4354 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
Chris Lattner30f73e72006-10-14 03:52:46 +00004355 if (SCC.Val) AddToWorkList(SCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004356 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
4357
4358 // fold select_cc true, x, y -> x
4359 if (SCCC && SCCC->getValue())
4360 return N2;
4361 // fold select_cc false, x, y -> y
4362 if (SCCC && SCCC->getValue() == 0)
4363 return N3;
4364
4365 // Check to see if we can simplify the select into an fabs node
4366 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
4367 // Allow either -0.0 or 0.0
4368 if (CFP->getValue() == 0.0) {
4369 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
4370 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
4371 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
4372 N2 == N3.getOperand(0))
4373 return DAG.getNode(ISD::FABS, VT, N0);
4374
4375 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
4376 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
4377 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
4378 N2.getOperand(0) == N3)
4379 return DAG.getNode(ISD::FABS, VT, N3);
4380 }
4381 }
4382
4383 // Check to see if we can perform the "gzip trick", transforming
4384 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
Chris Lattnere3152e52006-09-20 06:41:35 +00004385 if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT &&
Nate Begemanf845b452005-10-08 00:29:44 +00004386 MVT::isInteger(N0.getValueType()) &&
Chris Lattnere3152e52006-09-20 06:41:35 +00004387 MVT::isInteger(N2.getValueType()) &&
4388 (N1C->isNullValue() || // (a < 0) ? b : 0
4389 (N1C->getValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0
Nate Begemanf845b452005-10-08 00:29:44 +00004390 MVT::ValueType XType = N0.getValueType();
4391 MVT::ValueType AType = N2.getValueType();
4392 if (XType >= AType) {
4393 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00004394 // single-bit constant.
Nate Begemanf845b452005-10-08 00:29:44 +00004395 if (N2C && ((N2C->getValue() & (N2C->getValue()-1)) == 0)) {
4396 unsigned ShCtV = Log2_64(N2C->getValue());
4397 ShCtV = MVT::getSizeInBits(XType)-ShCtV-1;
4398 SDOperand ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
4399 SDOperand Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
Chris Lattner5750df92006-03-01 04:03:14 +00004400 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004401 if (XType > AType) {
4402 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00004403 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004404 }
4405 return DAG.getNode(ISD::AND, AType, Shift, N2);
4406 }
4407 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
4408 DAG.getConstant(MVT::getSizeInBits(XType)-1,
4409 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00004410 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004411 if (XType > AType) {
4412 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00004413 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004414 }
4415 return DAG.getNode(ISD::AND, AType, Shift, N2);
4416 }
4417 }
Nate Begeman07ed4172005-10-10 21:26:48 +00004418
4419 // fold select C, 16, 0 -> shl C, 4
4420 if (N2C && N3C && N3C->isNullValue() && isPowerOf2_64(N2C->getValue()) &&
4421 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) {
Chris Lattner1eba01e2007-04-11 06:50:51 +00004422
4423 // If the caller doesn't want us to simplify this into a zext of a compare,
4424 // don't do it.
4425 if (NotExtCompare && N2C->getValue() == 1)
4426 return SDOperand();
4427
Nate Begeman07ed4172005-10-10 21:26:48 +00004428 // Get a SetCC of the condition
4429 // FIXME: Should probably make sure that setcc is legal if we ever have a
4430 // target where it isn't.
Nate Begemanb0d04a72006-02-18 02:40:58 +00004431 SDOperand Temp, SCC;
Nate Begeman07ed4172005-10-10 21:26:48 +00004432 // cast from setcc result type to select result type
Nate Begemanb0d04a72006-02-18 02:40:58 +00004433 if (AfterLegalize) {
4434 SCC = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
Chris Lattner555d8d62006-12-07 22:36:47 +00004435 if (N2.getValueType() < SCC.getValueType())
4436 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
4437 else
4438 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00004439 } else {
4440 SCC = DAG.getSetCC(MVT::i1, N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00004441 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00004442 }
Chris Lattner5750df92006-03-01 04:03:14 +00004443 AddToWorkList(SCC.Val);
4444 AddToWorkList(Temp.Val);
Chris Lattnerc56a81d2007-04-11 06:43:25 +00004445
4446 if (N2C->getValue() == 1)
4447 return Temp;
Nate Begeman07ed4172005-10-10 21:26:48 +00004448 // shl setcc result by log2 n2c
4449 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
4450 DAG.getConstant(Log2_64(N2C->getValue()),
4451 TLI.getShiftAmountTy()));
4452 }
4453
Nate Begemanf845b452005-10-08 00:29:44 +00004454 // Check to see if this is the equivalent of setcc
4455 // FIXME: Turn all of these into setcc if setcc if setcc is legal
4456 // otherwise, go ahead with the folds.
4457 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getValue() == 1ULL)) {
4458 MVT::ValueType XType = N0.getValueType();
4459 if (TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultTy())) {
4460 SDOperand Res = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
4461 if (Res.getValueType() != VT)
4462 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
4463 return Res;
4464 }
4465
4466 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
4467 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
4468 TLI.isOperationLegal(ISD::CTLZ, XType)) {
4469 SDOperand Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
4470 return DAG.getNode(ISD::SRL, XType, Ctlz,
4471 DAG.getConstant(Log2_32(MVT::getSizeInBits(XType)),
4472 TLI.getShiftAmountTy()));
4473 }
4474 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
4475 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
4476 SDOperand NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
4477 N0);
4478 SDOperand NotN0 = DAG.getNode(ISD::XOR, XType, N0,
4479 DAG.getConstant(~0ULL, XType));
4480 return DAG.getNode(ISD::SRL, XType,
4481 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
4482 DAG.getConstant(MVT::getSizeInBits(XType)-1,
4483 TLI.getShiftAmountTy()));
4484 }
4485 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
4486 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
4487 SDOperand Sign = DAG.getNode(ISD::SRL, XType, N0,
4488 DAG.getConstant(MVT::getSizeInBits(XType)-1,
4489 TLI.getShiftAmountTy()));
4490 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
4491 }
4492 }
4493
4494 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
4495 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
4496 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
Chris Lattner1982ef22007-04-11 05:11:38 +00004497 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1) &&
4498 N2.getOperand(0) == N1 && MVT::isInteger(N0.getValueType())) {
4499 MVT::ValueType XType = N0.getValueType();
4500 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
4501 DAG.getConstant(MVT::getSizeInBits(XType)-1,
4502 TLI.getShiftAmountTy()));
4503 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
4504 AddToWorkList(Shift.Val);
4505 AddToWorkList(Add.Val);
4506 return DAG.getNode(ISD::XOR, XType, Add, Shift);
4507 }
4508 // Check to see if this is an integer abs. select_cc setgt X, -1, X, -X ->
4509 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
4510 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT &&
4511 N0 == N2 && N3.getOpcode() == ISD::SUB && N0 == N3.getOperand(1)) {
4512 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N3.getOperand(0))) {
Nate Begemanf845b452005-10-08 00:29:44 +00004513 MVT::ValueType XType = N0.getValueType();
4514 if (SubC->isNullValue() && MVT::isInteger(XType)) {
4515 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
4516 DAG.getConstant(MVT::getSizeInBits(XType)-1,
Chris Lattner1982ef22007-04-11 05:11:38 +00004517 TLI.getShiftAmountTy()));
Nate Begemanf845b452005-10-08 00:29:44 +00004518 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00004519 AddToWorkList(Shift.Val);
4520 AddToWorkList(Add.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004521 return DAG.getNode(ISD::XOR, XType, Add, Shift);
4522 }
4523 }
4524 }
Chris Lattner1982ef22007-04-11 05:11:38 +00004525
Nate Begeman44728a72005-09-19 22:34:01 +00004526 return SDOperand();
4527}
4528
Evan Chengfa1eb272007-02-08 22:13:59 +00004529/// SimplifySetCC - This is a stub for TargetLowering::SimplifySetCC.
Nate Begeman452d7be2005-09-16 00:54:12 +00004530SDOperand DAGCombiner::SimplifySetCC(MVT::ValueType VT, SDOperand N0,
Nate Begemane17daeb2005-10-05 21:43:42 +00004531 SDOperand N1, ISD::CondCode Cond,
4532 bool foldBooleans) {
Evan Chengfa1eb272007-02-08 22:13:59 +00004533 TargetLowering::DAGCombinerInfo
4534 DagCombineInfo(DAG, !AfterLegalize, false, this);
4535 return TLI.SimplifySetCC(VT, N0, N1, Cond, foldBooleans, DagCombineInfo);
Nate Begeman452d7be2005-09-16 00:54:12 +00004536}
4537
Nate Begeman69575232005-10-20 02:15:44 +00004538/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
4539/// return a DAG expression to select that will generate the same value by
4540/// multiplying by a magic number. See:
4541/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
4542SDOperand DAGCombiner::BuildSDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00004543 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00004544 SDOperand S = TLI.BuildSDIV(N, DAG, &Built);
4545
Andrew Lenharth232c9102006-06-12 16:07:18 +00004546 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00004547 ii != ee; ++ii)
4548 AddToWorkList(*ii);
4549 return S;
Nate Begeman69575232005-10-20 02:15:44 +00004550}
4551
4552/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
4553/// return a DAG expression to select that will generate the same value by
4554/// multiplying by a magic number. See:
4555/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
4556SDOperand DAGCombiner::BuildUDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00004557 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00004558 SDOperand S = TLI.BuildUDIV(N, DAG, &Built);
Nate Begeman69575232005-10-20 02:15:44 +00004559
Andrew Lenharth232c9102006-06-12 16:07:18 +00004560 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00004561 ii != ee; ++ii)
4562 AddToWorkList(*ii);
4563 return S;
Nate Begeman69575232005-10-20 02:15:44 +00004564}
4565
Jim Laskey71382342006-10-07 23:37:56 +00004566/// FindBaseOffset - Return true if base is known not to alias with anything
4567/// but itself. Provides base object and offset as results.
4568static bool FindBaseOffset(SDOperand Ptr, SDOperand &Base, int64_t &Offset) {
4569 // Assume it is a primitive operation.
4570 Base = Ptr; Offset = 0;
4571
4572 // If it's an adding a simple constant then integrate the offset.
4573 if (Base.getOpcode() == ISD::ADD) {
4574 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Base.getOperand(1))) {
4575 Base = Base.getOperand(0);
4576 Offset += C->getValue();
4577 }
4578 }
4579
4580 // If it's any of the following then it can't alias with anything but itself.
4581 return isa<FrameIndexSDNode>(Base) ||
4582 isa<ConstantPoolSDNode>(Base) ||
4583 isa<GlobalAddressSDNode>(Base);
4584}
4585
4586/// isAlias - Return true if there is any possibility that the two addresses
4587/// overlap.
Jim Laskey096c22e2006-10-18 12:29:57 +00004588bool DAGCombiner::isAlias(SDOperand Ptr1, int64_t Size1,
4589 const Value *SrcValue1, int SrcValueOffset1,
4590 SDOperand Ptr2, int64_t Size2,
4591 const Value *SrcValue2, int SrcValueOffset2)
4592{
Jim Laskey71382342006-10-07 23:37:56 +00004593 // If they are the same then they must be aliases.
4594 if (Ptr1 == Ptr2) return true;
4595
4596 // Gather base node and offset information.
4597 SDOperand Base1, Base2;
4598 int64_t Offset1, Offset2;
4599 bool KnownBase1 = FindBaseOffset(Ptr1, Base1, Offset1);
4600 bool KnownBase2 = FindBaseOffset(Ptr2, Base2, Offset2);
4601
4602 // If they have a same base address then...
4603 if (Base1 == Base2) {
4604 // Check to see if the addresses overlap.
4605 return!((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
4606 }
4607
Jim Laskey096c22e2006-10-18 12:29:57 +00004608 // If we know both bases then they can't alias.
4609 if (KnownBase1 && KnownBase2) return false;
4610
Jim Laskey07a27092006-10-18 19:08:31 +00004611 if (CombinerGlobalAA) {
4612 // Use alias analysis information.
4613 int Overlap1 = Size1 + SrcValueOffset1 + Offset1;
4614 int Overlap2 = Size2 + SrcValueOffset2 + Offset2;
4615 AliasAnalysis::AliasResult AAResult =
Jim Laskey096c22e2006-10-18 12:29:57 +00004616 AA.alias(SrcValue1, Overlap1, SrcValue2, Overlap2);
Jim Laskey07a27092006-10-18 19:08:31 +00004617 if (AAResult == AliasAnalysis::NoAlias)
4618 return false;
4619 }
Jim Laskey096c22e2006-10-18 12:29:57 +00004620
4621 // Otherwise we have to assume they alias.
4622 return true;
Jim Laskey71382342006-10-07 23:37:56 +00004623}
4624
4625/// FindAliasInfo - Extracts the relevant alias information from the memory
4626/// node. Returns true if the operand was a load.
Jim Laskey7ca56af2006-10-11 13:47:09 +00004627bool DAGCombiner::FindAliasInfo(SDNode *N,
Jim Laskey096c22e2006-10-18 12:29:57 +00004628 SDOperand &Ptr, int64_t &Size,
4629 const Value *&SrcValue, int &SrcValueOffset) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00004630 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
4631 Ptr = LD->getBasePtr();
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004632 Size = MVT::getSizeInBits(LD->getLoadedVT()) >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00004633 SrcValue = LD->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00004634 SrcValueOffset = LD->getSrcValueOffset();
Jim Laskey71382342006-10-07 23:37:56 +00004635 return true;
Jim Laskey7ca56af2006-10-11 13:47:09 +00004636 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00004637 Ptr = ST->getBasePtr();
Evan Cheng8b2794a2006-10-13 21:14:26 +00004638 Size = MVT::getSizeInBits(ST->getStoredVT()) >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00004639 SrcValue = ST->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00004640 SrcValueOffset = ST->getSrcValueOffset();
Jim Laskey7ca56af2006-10-11 13:47:09 +00004641 } else {
Jim Laskey71382342006-10-07 23:37:56 +00004642 assert(0 && "FindAliasInfo expected a memory operand");
Jim Laskey71382342006-10-07 23:37:56 +00004643 }
4644
4645 return false;
4646}
4647
Jim Laskey6ff23e52006-10-04 16:53:27 +00004648/// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
4649/// looking for aliasing nodes and adding them to the Aliases vector.
Jim Laskeybc588b82006-10-05 15:07:25 +00004650void DAGCombiner::GatherAllAliases(SDNode *N, SDOperand OriginalChain,
Jim Laskey6ff23e52006-10-04 16:53:27 +00004651 SmallVector<SDOperand, 8> &Aliases) {
Jim Laskeybc588b82006-10-05 15:07:25 +00004652 SmallVector<SDOperand, 8> Chains; // List of chains to visit.
Jim Laskey6ff23e52006-10-04 16:53:27 +00004653 std::set<SDNode *> Visited; // Visited node set.
4654
Jim Laskey279f0532006-09-25 16:29:54 +00004655 // Get alias information for node.
4656 SDOperand Ptr;
4657 int64_t Size;
Jim Laskey7ca56af2006-10-11 13:47:09 +00004658 const Value *SrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00004659 int SrcValueOffset;
4660 bool IsLoad = FindAliasInfo(N, Ptr, Size, SrcValue, SrcValueOffset);
Jim Laskey279f0532006-09-25 16:29:54 +00004661
Jim Laskey6ff23e52006-10-04 16:53:27 +00004662 // Starting off.
Jim Laskeybc588b82006-10-05 15:07:25 +00004663 Chains.push_back(OriginalChain);
Jim Laskey6ff23e52006-10-04 16:53:27 +00004664
Jim Laskeybc588b82006-10-05 15:07:25 +00004665 // Look at each chain and determine if it is an alias. If so, add it to the
4666 // aliases list. If not, then continue up the chain looking for the next
4667 // candidate.
4668 while (!Chains.empty()) {
4669 SDOperand Chain = Chains.back();
4670 Chains.pop_back();
Jim Laskey6ff23e52006-10-04 16:53:27 +00004671
Jim Laskeybc588b82006-10-05 15:07:25 +00004672 // Don't bother if we've been before.
4673 if (Visited.find(Chain.Val) != Visited.end()) continue;
4674 Visited.insert(Chain.Val);
4675
4676 switch (Chain.getOpcode()) {
4677 case ISD::EntryToken:
4678 // Entry token is ideal chain operand, but handled in FindBetterChain.
4679 break;
Jim Laskey6ff23e52006-10-04 16:53:27 +00004680
Jim Laskeybc588b82006-10-05 15:07:25 +00004681 case ISD::LOAD:
4682 case ISD::STORE: {
4683 // Get alias information for Chain.
4684 SDOperand OpPtr;
4685 int64_t OpSize;
Jim Laskey7ca56af2006-10-11 13:47:09 +00004686 const Value *OpSrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00004687 int OpSrcValueOffset;
4688 bool IsOpLoad = FindAliasInfo(Chain.Val, OpPtr, OpSize,
4689 OpSrcValue, OpSrcValueOffset);
Jim Laskeybc588b82006-10-05 15:07:25 +00004690
4691 // If chain is alias then stop here.
4692 if (!(IsLoad && IsOpLoad) &&
Jim Laskey096c22e2006-10-18 12:29:57 +00004693 isAlias(Ptr, Size, SrcValue, SrcValueOffset,
4694 OpPtr, OpSize, OpSrcValue, OpSrcValueOffset)) {
Jim Laskeybc588b82006-10-05 15:07:25 +00004695 Aliases.push_back(Chain);
4696 } else {
4697 // Look further up the chain.
4698 Chains.push_back(Chain.getOperand(0));
4699 // Clean up old chain.
4700 AddToWorkList(Chain.Val);
Jim Laskey279f0532006-09-25 16:29:54 +00004701 }
Jim Laskeybc588b82006-10-05 15:07:25 +00004702 break;
4703 }
4704
4705 case ISD::TokenFactor:
4706 // We have to check each of the operands of the token factor, so we queue
4707 // then up. Adding the operands to the queue (stack) in reverse order
4708 // maintains the original order and increases the likelihood that getNode
4709 // will find a matching token factor (CSE.)
4710 for (unsigned n = Chain.getNumOperands(); n;)
4711 Chains.push_back(Chain.getOperand(--n));
4712 // Eliminate the token factor if we can.
4713 AddToWorkList(Chain.Val);
4714 break;
4715
4716 default:
4717 // For all other instructions we will just have to take what we can get.
4718 Aliases.push_back(Chain);
4719 break;
Jim Laskey279f0532006-09-25 16:29:54 +00004720 }
4721 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00004722}
4723
4724/// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, looking
4725/// for a better chain (aliasing node.)
4726SDOperand DAGCombiner::FindBetterChain(SDNode *N, SDOperand OldChain) {
4727 SmallVector<SDOperand, 8> Aliases; // Ops for replacing token factor.
Jim Laskey279f0532006-09-25 16:29:54 +00004728
Jim Laskey6ff23e52006-10-04 16:53:27 +00004729 // Accumulate all the aliases to this node.
4730 GatherAllAliases(N, OldChain, Aliases);
4731
4732 if (Aliases.size() == 0) {
4733 // If no operands then chain to entry token.
4734 return DAG.getEntryNode();
4735 } else if (Aliases.size() == 1) {
4736 // If a single operand then chain to it. We don't need to revisit it.
4737 return Aliases[0];
4738 }
4739
4740 // Construct a custom tailored token factor.
4741 SDOperand NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
4742 &Aliases[0], Aliases.size());
4743
4744 // Make sure the old chain gets cleaned up.
4745 if (NewChain != OldChain) AddToWorkList(OldChain.Val);
4746
4747 return NewChain;
Jim Laskey279f0532006-09-25 16:29:54 +00004748}
4749
Nate Begeman1d4d4142005-09-01 00:19:25 +00004750// SelectionDAG::Combine - This is the entry point for the file.
4751//
Jim Laskeyc7c3f112006-10-16 20:52:31 +00004752void SelectionDAG::Combine(bool RunningAfterLegalize, AliasAnalysis &AA) {
Chris Lattner938ab022007-01-16 04:55:25 +00004753 if (!RunningAfterLegalize && ViewDAGCombine1)
4754 viewGraph();
4755 if (RunningAfterLegalize && ViewDAGCombine2)
4756 viewGraph();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004757 /// run - This is the main entry point to this class.
4758 ///
Jim Laskeyc7c3f112006-10-16 20:52:31 +00004759 DAGCombiner(*this, AA).Run(RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004760}