blob: cb7276e023cf297896f17b5820c9297e1316826b [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);
Dan Gohman7f321562007-06-25 16:23:39 +0000230 SDOperand SimplifyVBinOp(SDNode *N);
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 Lattner01b3d732005-09-28 22:28:18 +0000246 SDOperand visitFADD(SDNode *N);
247 SDOperand visitFSUB(SDNode *N);
248 SDOperand visitFMUL(SDNode *N);
249 SDOperand visitFDIV(SDNode *N);
250 SDOperand visitFREM(SDNode *N);
Chris Lattner12d83032006-03-05 05:30:57 +0000251 SDOperand visitFCOPYSIGN(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000252 SDOperand visitSINT_TO_FP(SDNode *N);
253 SDOperand visitUINT_TO_FP(SDNode *N);
254 SDOperand visitFP_TO_SINT(SDNode *N);
255 SDOperand visitFP_TO_UINT(SDNode *N);
256 SDOperand visitFP_ROUND(SDNode *N);
257 SDOperand visitFP_ROUND_INREG(SDNode *N);
258 SDOperand visitFP_EXTEND(SDNode *N);
259 SDOperand visitFNEG(SDNode *N);
260 SDOperand visitFABS(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000261 SDOperand visitBRCOND(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000262 SDOperand visitBR_CC(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000263 SDOperand visitLOAD(SDNode *N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000264 SDOperand visitSTORE(SDNode *N);
Chris Lattnerca242442006-03-19 01:27:56 +0000265 SDOperand visitINSERT_VECTOR_ELT(SDNode *N);
Dan Gohman7f321562007-06-25 16:23:39 +0000266 SDOperand visitBUILD_VECTOR(SDNode *N);
267 SDOperand visitCONCAT_VECTORS(SDNode *N);
Chris Lattner66445d32006-03-28 22:11:53 +0000268 SDOperand visitVECTOR_SHUFFLE(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000269
Evan Cheng44f1f092006-04-20 08:56:16 +0000270 SDOperand XformToShuffleWithZero(SDNode *N);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000271 SDOperand ReassociateOps(unsigned Opc, SDOperand LHS, SDOperand RHS);
272
Chris Lattner40c62d52005-10-18 06:04:22 +0000273 bool SimplifySelectOps(SDNode *SELECT, SDOperand LHS, SDOperand RHS);
Chris Lattner35e5c142006-05-05 05:51:50 +0000274 SDOperand SimplifyBinOpWithSameOpcodeHands(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000275 SDOperand SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2);
276 SDOperand SimplifySelectCC(SDOperand N0, SDOperand N1, SDOperand N2,
Chris Lattner1eba01e2007-04-11 06:50:51 +0000277 SDOperand N3, ISD::CondCode CC,
278 bool NotExtCompare = false);
Nate Begeman452d7be2005-09-16 00:54:12 +0000279 SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N0, SDOperand N1,
Nate Begemane17daeb2005-10-05 21:43:42 +0000280 ISD::CondCode Cond, bool foldBooleans = true);
Dan Gohman7f321562007-06-25 16:23:39 +0000281 SDOperand ConstantFoldBIT_CONVERTofBUILD_VECTOR(SDNode *, MVT::ValueType);
Nate Begeman69575232005-10-20 02:15:44 +0000282 SDOperand BuildSDIV(SDNode *N);
Chris Lattner516b9622006-09-14 20:50:57 +0000283 SDOperand BuildUDIV(SDNode *N);
284 SDNode *MatchRotate(SDOperand LHS, SDOperand RHS);
Evan Chengc88138f2007-03-22 01:54:19 +0000285 SDOperand ReduceLoadWidth(SDNode *N);
Jim Laskey279f0532006-09-25 16:29:54 +0000286
Jim Laskey6ff23e52006-10-04 16:53:27 +0000287 /// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
288 /// looking for aliasing nodes and adding them to the Aliases vector.
Jim Laskeybc588b82006-10-05 15:07:25 +0000289 void GatherAllAliases(SDNode *N, SDOperand OriginalChain,
Jim Laskey6ff23e52006-10-04 16:53:27 +0000290 SmallVector<SDOperand, 8> &Aliases);
291
Jim Laskey096c22e2006-10-18 12:29:57 +0000292 /// isAlias - Return true if there is any possibility that the two addresses
293 /// overlap.
294 bool isAlias(SDOperand Ptr1, int64_t Size1,
295 const Value *SrcValue1, int SrcValueOffset1,
296 SDOperand Ptr2, int64_t Size2,
Jeff Cohend41b30d2006-11-05 19:31:28 +0000297 const Value *SrcValue2, int SrcValueOffset2);
Jim Laskey096c22e2006-10-18 12:29:57 +0000298
Jim Laskey7ca56af2006-10-11 13:47:09 +0000299 /// FindAliasInfo - Extracts the relevant alias information from the memory
300 /// node. Returns true if the operand was a load.
301 bool FindAliasInfo(SDNode *N,
Jim Laskey096c22e2006-10-18 12:29:57 +0000302 SDOperand &Ptr, int64_t &Size,
303 const Value *&SrcValue, int &SrcValueOffset);
Jim Laskey7ca56af2006-10-11 13:47:09 +0000304
Jim Laskey279f0532006-09-25 16:29:54 +0000305 /// FindBetterChain - Walk up chain skipping non-aliasing memory nodes,
Jim Laskey6ff23e52006-10-04 16:53:27 +0000306 /// looking for a better chain (aliasing node.)
Jim Laskey279f0532006-09-25 16:29:54 +0000307 SDOperand FindBetterChain(SDNode *N, SDOperand Chain);
308
Nate Begeman1d4d4142005-09-01 00:19:25 +0000309public:
Jim Laskeyc7c3f112006-10-16 20:52:31 +0000310 DAGCombiner(SelectionDAG &D, AliasAnalysis &A)
311 : DAG(D),
312 TLI(D.getTargetLoweringInfo()),
313 AfterLegalize(false),
314 AA(A) {}
Nate Begeman1d4d4142005-09-01 00:19:25 +0000315
316 /// Run - runs the dag combiner on all nodes in the work list
Nate Begeman4ebd8052005-09-01 23:24:04 +0000317 void Run(bool RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000318 };
319}
320
Chris Lattner24664722006-03-01 04:53:38 +0000321//===----------------------------------------------------------------------===//
322// TargetLowering::DAGCombinerInfo implementation
323//===----------------------------------------------------------------------===//
324
325void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) {
326 ((DAGCombiner*)DC)->AddToWorkList(N);
327}
328
329SDOperand TargetLowering::DAGCombinerInfo::
330CombineTo(SDNode *N, const std::vector<SDOperand> &To) {
Chris Lattner3577e382006-08-11 17:56:38 +0000331 return ((DAGCombiner*)DC)->CombineTo(N, &To[0], To.size());
Chris Lattner24664722006-03-01 04:53:38 +0000332}
333
334SDOperand TargetLowering::DAGCombinerInfo::
335CombineTo(SDNode *N, SDOperand Res) {
336 return ((DAGCombiner*)DC)->CombineTo(N, Res);
337}
338
339
340SDOperand TargetLowering::DAGCombinerInfo::
341CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1) {
342 return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1);
343}
344
345
Chris Lattner24664722006-03-01 04:53:38 +0000346//===----------------------------------------------------------------------===//
Chris Lattner29446522007-05-14 22:04:50 +0000347// Helper Functions
348//===----------------------------------------------------------------------===//
349
350/// isNegatibleForFree - Return 1 if we can compute the negated form of the
351/// specified expression for the same cost as the expression itself, or 2 if we
352/// can compute the negated form more cheaply than the expression itself.
Chris Lattner501fee72007-05-23 07:35:22 +0000353static char isNegatibleForFree(SDOperand Op, unsigned Depth = 0) {
Chris Lattner29446522007-05-14 22:04:50 +0000354 // fneg is removable even if it has multiple uses.
355 if (Op.getOpcode() == ISD::FNEG) return 2;
356
357 // Don't allow anything with multiple uses.
358 if (!Op.hasOneUse()) return 0;
359
Chris Lattner3adf9512007-05-25 02:19:06 +0000360 // Don't recurse exponentially.
361 if (Depth > 6) return 0;
362
Chris Lattner29446522007-05-14 22:04:50 +0000363 switch (Op.getOpcode()) {
364 default: return false;
365 case ISD::ConstantFP:
366 return 1;
367 case ISD::FADD:
368 // FIXME: determine better conditions for this xform.
369 if (!UnsafeFPMath) return 0;
370
371 // -(A+B) -> -A - B
Chris Lattner501fee72007-05-23 07:35:22 +0000372 if (char V = isNegatibleForFree(Op.getOperand(0), Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000373 return V;
374 // -(A+B) -> -B - A
Chris Lattner501fee72007-05-23 07:35:22 +0000375 return isNegatibleForFree(Op.getOperand(1), Depth+1);
Chris Lattner29446522007-05-14 22:04:50 +0000376 case ISD::FSUB:
377 // We can't turn -(A-B) into B-A when we honor signed zeros.
378 if (!UnsafeFPMath) return 0;
379
380 // -(A-B) -> B-A
381 return 1;
382
383 case ISD::FMUL:
384 case ISD::FDIV:
385 if (HonorSignDependentRoundingFPMath()) return 0;
386
387 // -(X*Y) -> (-X * Y) or (X*-Y)
Chris Lattner501fee72007-05-23 07:35:22 +0000388 if (char V = isNegatibleForFree(Op.getOperand(0), Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000389 return V;
390
Chris Lattner501fee72007-05-23 07:35:22 +0000391 return isNegatibleForFree(Op.getOperand(1), Depth+1);
Chris Lattner29446522007-05-14 22:04:50 +0000392
393 case ISD::FP_EXTEND:
394 case ISD::FP_ROUND:
395 case ISD::FSIN:
Chris Lattner501fee72007-05-23 07:35:22 +0000396 return isNegatibleForFree(Op.getOperand(0), Depth+1);
Chris Lattner29446522007-05-14 22:04:50 +0000397 }
398}
399
400/// GetNegatedExpression - If isNegatibleForFree returns true, this function
401/// returns the newly negated expression.
Chris Lattner3adf9512007-05-25 02:19:06 +0000402static SDOperand GetNegatedExpression(SDOperand Op, SelectionDAG &DAG,
403 unsigned Depth = 0) {
Chris Lattner29446522007-05-14 22:04:50 +0000404 // fneg is removable even if it has multiple uses.
405 if (Op.getOpcode() == ISD::FNEG) return Op.getOperand(0);
406
407 // Don't allow anything with multiple uses.
408 assert(Op.hasOneUse() && "Unknown reuse!");
409
Chris Lattner3adf9512007-05-25 02:19:06 +0000410 assert(Depth <= 6 && "GetNegatedExpression doesn't match isNegatibleForFree");
Chris Lattner29446522007-05-14 22:04:50 +0000411 switch (Op.getOpcode()) {
412 default: assert(0 && "Unknown code");
413 case ISD::ConstantFP:
414 return DAG.getConstantFP(-cast<ConstantFPSDNode>(Op)->getValue(),
415 Op.getValueType());
416 case ISD::FADD:
417 // FIXME: determine better conditions for this xform.
418 assert(UnsafeFPMath);
419
420 // -(A+B) -> -A - B
Chris Lattner3adf9512007-05-25 02:19:06 +0000421 if (isNegatibleForFree(Op.getOperand(0), Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000422 return DAG.getNode(ISD::FSUB, Op.getValueType(),
Chris Lattner3adf9512007-05-25 02:19:06 +0000423 GetNegatedExpression(Op.getOperand(0), DAG, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000424 Op.getOperand(1));
425 // -(A+B) -> -B - A
426 return DAG.getNode(ISD::FSUB, Op.getValueType(),
Chris Lattner3adf9512007-05-25 02:19:06 +0000427 GetNegatedExpression(Op.getOperand(1), DAG, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000428 Op.getOperand(0));
429 case ISD::FSUB:
430 // We can't turn -(A-B) into B-A when we honor signed zeros.
431 assert(UnsafeFPMath);
432
433 // -(A-B) -> B-A
434 return DAG.getNode(ISD::FSUB, Op.getValueType(), Op.getOperand(1),
435 Op.getOperand(0));
436
437 case ISD::FMUL:
438 case ISD::FDIV:
439 assert(!HonorSignDependentRoundingFPMath());
440
441 // -(X*Y) -> -X * Y
Chris Lattner3adf9512007-05-25 02:19:06 +0000442 if (isNegatibleForFree(Op.getOperand(0), Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000443 return DAG.getNode(Op.getOpcode(), Op.getValueType(),
Chris Lattner3adf9512007-05-25 02:19:06 +0000444 GetNegatedExpression(Op.getOperand(0), DAG, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000445 Op.getOperand(1));
446
447 // -(X*Y) -> X * -Y
448 return DAG.getNode(Op.getOpcode(), Op.getValueType(),
449 Op.getOperand(0),
Chris Lattner3adf9512007-05-25 02:19:06 +0000450 GetNegatedExpression(Op.getOperand(1), DAG, Depth+1));
Chris Lattner29446522007-05-14 22:04:50 +0000451
452 case ISD::FP_EXTEND:
453 case ISD::FP_ROUND:
454 case ISD::FSIN:
455 return DAG.getNode(Op.getOpcode(), Op.getValueType(),
Chris Lattner3adf9512007-05-25 02:19:06 +0000456 GetNegatedExpression(Op.getOperand(0), DAG, Depth+1));
Chris Lattner29446522007-05-14 22:04:50 +0000457 }
458}
Chris Lattner24664722006-03-01 04:53:38 +0000459
460
Nate Begeman4ebd8052005-09-01 23:24:04 +0000461// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
462// that selects between the values 1 and 0, making it equivalent to a setcc.
Nate Begeman646d7e22005-09-02 21:18:40 +0000463// Also, set the incoming LHS, RHS, and CC references to the appropriate
464// nodes based on the type of node we are checking. This simplifies life a
465// bit for the callers.
466static bool isSetCCEquivalent(SDOperand N, SDOperand &LHS, SDOperand &RHS,
467 SDOperand &CC) {
468 if (N.getOpcode() == ISD::SETCC) {
469 LHS = N.getOperand(0);
470 RHS = N.getOperand(1);
471 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000472 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000473 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000474 if (N.getOpcode() == ISD::SELECT_CC &&
475 N.getOperand(2).getOpcode() == ISD::Constant &&
476 N.getOperand(3).getOpcode() == ISD::Constant &&
477 cast<ConstantSDNode>(N.getOperand(2))->getValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000478 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
479 LHS = N.getOperand(0);
480 RHS = N.getOperand(1);
481 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000482 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000483 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000484 return false;
485}
486
Nate Begeman99801192005-09-07 23:25:52 +0000487// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
488// one use. If this is true, it allows the users to invert the operation for
489// free when it is profitable to do so.
490static bool isOneUseSetCC(SDOperand N) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000491 SDOperand N0, N1, N2;
Nate Begeman646d7e22005-09-02 21:18:40 +0000492 if (isSetCCEquivalent(N, N0, N1, N2) && N.Val->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000493 return true;
494 return false;
495}
496
Nate Begemancd4d58c2006-02-03 06:46:56 +0000497SDOperand DAGCombiner::ReassociateOps(unsigned Opc, SDOperand N0, SDOperand N1){
498 MVT::ValueType VT = N0.getValueType();
499 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
500 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
501 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
502 if (isa<ConstantSDNode>(N1)) {
503 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(1), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000504 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000505 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(0));
506 } else if (N0.hasOneUse()) {
507 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(0), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000508 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000509 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(1));
510 }
511 }
512 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
513 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
514 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
515 if (isa<ConstantSDNode>(N0)) {
516 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(1), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000517 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000518 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(0));
519 } else if (N1.hasOneUse()) {
520 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(0), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000521 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000522 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(1));
523 }
524 }
525 return SDOperand();
526}
527
Chris Lattner29446522007-05-14 22:04:50 +0000528//===----------------------------------------------------------------------===//
529// Main DAG Combiner implementation
530//===----------------------------------------------------------------------===//
531
Nate Begeman4ebd8052005-09-01 23:24:04 +0000532void DAGCombiner::Run(bool RunningAfterLegalize) {
533 // set the instance variable, so that the various visit routines may use it.
534 AfterLegalize = RunningAfterLegalize;
535
Nate Begeman646d7e22005-09-02 21:18:40 +0000536 // Add all the dag nodes to the worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000537 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
538 E = DAG.allnodes_end(); I != E; ++I)
539 WorkList.push_back(I);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000540
Chris Lattner95038592005-10-05 06:35:28 +0000541 // Create a dummy node (which is not added to allnodes), that adds a reference
542 // to the root node, preventing it from being deleted, and tracking any
543 // changes of the root.
544 HandleSDNode Dummy(DAG.getRoot());
545
Jim Laskey26f7fa72006-10-17 19:33:52 +0000546 // The root of the dag may dangle to deleted nodes until the dag combiner is
547 // done. Set it to null to avoid confusion.
548 DAG.setRoot(SDOperand());
Chris Lattner24664722006-03-01 04:53:38 +0000549
550 /// DagCombineInfo - Expose the DAG combiner to the target combiner impls.
551 TargetLowering::DAGCombinerInfo
Evan Chengfa1eb272007-02-08 22:13:59 +0000552 DagCombineInfo(DAG, !RunningAfterLegalize, false, this);
Jim Laskey6ff23e52006-10-04 16:53:27 +0000553
Nate Begeman1d4d4142005-09-01 00:19:25 +0000554 // while the worklist isn't empty, inspect the node on the end of it and
555 // try and combine it.
556 while (!WorkList.empty()) {
557 SDNode *N = WorkList.back();
558 WorkList.pop_back();
559
560 // If N has no uses, it is dead. Make sure to revisit all N's operands once
Chris Lattner95038592005-10-05 06:35:28 +0000561 // N is deleted from the DAG, since they too may now be dead or may have a
562 // reduced number of uses, allowing other xforms.
563 if (N->use_empty() && N != &Dummy) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000564 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Jim Laskey6ff23e52006-10-04 16:53:27 +0000565 AddToWorkList(N->getOperand(i).Val);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000566
Chris Lattner95038592005-10-05 06:35:28 +0000567 DAG.DeleteNode(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000568 continue;
569 }
570
Nate Begeman83e75ec2005-09-06 04:43:02 +0000571 SDOperand RV = visit(N);
Chris Lattner24664722006-03-01 04:53:38 +0000572
573 // If nothing happened, try a target-specific DAG combine.
574 if (RV.Val == 0) {
Chris Lattner729c6d12006-05-27 00:43:02 +0000575 assert(N->getOpcode() != ISD::DELETED_NODE &&
576 "Node was deleted but visit returned NULL!");
Chris Lattner24664722006-03-01 04:53:38 +0000577 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
578 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode()))
579 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
580 }
581
Nate Begeman83e75ec2005-09-06 04:43:02 +0000582 if (RV.Val) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000583 ++NodesCombined;
Nate Begeman646d7e22005-09-02 21:18:40 +0000584 // If we get back the same node we passed in, rather than a new node or
585 // zero, we know that the node must have defined multiple values and
586 // CombineTo was used. Since CombineTo takes care of the worklist
587 // mechanics for us, we have no work to do in this case.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000588 if (RV.Val != N) {
Chris Lattner729c6d12006-05-27 00:43:02 +0000589 assert(N->getOpcode() != ISD::DELETED_NODE &&
590 RV.Val->getOpcode() != ISD::DELETED_NODE &&
591 "Node was deleted but visit returned new node!");
592
Dan Gohmanb5bec2b2007-06-19 14:13:56 +0000593 DOUT << "\nReplacing.3 "; DEBUG(N->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +0000594 DOUT << "\nWith: "; DEBUG(RV.Val->dump(&DAG));
595 DOUT << '\n';
Chris Lattner01a22022005-10-10 22:04:48 +0000596 std::vector<SDNode*> NowDead;
Evan Cheng2adffa12006-09-21 19:04:05 +0000597 if (N->getNumValues() == RV.Val->getNumValues())
598 DAG.ReplaceAllUsesWith(N, RV.Val, &NowDead);
599 else {
600 assert(N->getValueType(0) == RV.getValueType() && "Type mismatch");
601 SDOperand OpV = RV;
602 DAG.ReplaceAllUsesWith(N, &OpV, &NowDead);
603 }
Nate Begeman646d7e22005-09-02 21:18:40 +0000604
605 // Push the new node and any users onto the worklist
Jim Laskey6ff23e52006-10-04 16:53:27 +0000606 AddToWorkList(RV.Val);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000607 AddUsersToWorkList(RV.Val);
Nate Begeman646d7e22005-09-02 21:18:40 +0000608
Jim Laskey6ff23e52006-10-04 16:53:27 +0000609 // Nodes can be reintroduced into the worklist. Make sure we do not
610 // process a node that has been replaced.
Nate Begeman646d7e22005-09-02 21:18:40 +0000611 removeFromWorkList(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000612 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
613 removeFromWorkList(NowDead[i]);
Chris Lattner5c46f742005-10-05 06:11:08 +0000614
615 // Finally, since the node is now dead, remove it from the graph.
616 DAG.DeleteNode(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000617 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000618 }
619 }
Chris Lattner95038592005-10-05 06:35:28 +0000620
621 // If the root changed (e.g. it was a dead load, update the root).
622 DAG.setRoot(Dummy.getValue());
Nate Begeman1d4d4142005-09-01 00:19:25 +0000623}
624
Nate Begeman83e75ec2005-09-06 04:43:02 +0000625SDOperand DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000626 switch(N->getOpcode()) {
627 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +0000628 case ISD::TokenFactor: return visitTokenFactor(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000629 case ISD::ADD: return visitADD(N);
630 case ISD::SUB: return visitSUB(N);
Chris Lattner91153682007-03-04 20:03:15 +0000631 case ISD::ADDC: return visitADDC(N);
632 case ISD::ADDE: return visitADDE(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000633 case ISD::MUL: return visitMUL(N);
634 case ISD::SDIV: return visitSDIV(N);
635 case ISD::UDIV: return visitUDIV(N);
636 case ISD::SREM: return visitSREM(N);
637 case ISD::UREM: return visitUREM(N);
638 case ISD::MULHU: return visitMULHU(N);
639 case ISD::MULHS: return visitMULHS(N);
640 case ISD::AND: return visitAND(N);
641 case ISD::OR: return visitOR(N);
642 case ISD::XOR: return visitXOR(N);
643 case ISD::SHL: return visitSHL(N);
644 case ISD::SRA: return visitSRA(N);
645 case ISD::SRL: return visitSRL(N);
646 case ISD::CTLZ: return visitCTLZ(N);
647 case ISD::CTTZ: return visitCTTZ(N);
648 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000649 case ISD::SELECT: return visitSELECT(N);
650 case ISD::SELECT_CC: return visitSELECT_CC(N);
651 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000652 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
653 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000654 case ISD::ANY_EXTEND: return visitANY_EXTEND(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000655 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
656 case ISD::TRUNCATE: return visitTRUNCATE(N);
Chris Lattner94683772005-12-23 05:30:37 +0000657 case ISD::BIT_CONVERT: return visitBIT_CONVERT(N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000658 case ISD::FADD: return visitFADD(N);
659 case ISD::FSUB: return visitFSUB(N);
660 case ISD::FMUL: return visitFMUL(N);
661 case ISD::FDIV: return visitFDIV(N);
662 case ISD::FREM: return visitFREM(N);
Chris Lattner12d83032006-03-05 05:30:57 +0000663 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000664 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
665 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
666 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
667 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
668 case ISD::FP_ROUND: return visitFP_ROUND(N);
669 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
670 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
671 case ISD::FNEG: return visitFNEG(N);
672 case ISD::FABS: return visitFABS(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000673 case ISD::BRCOND: return visitBRCOND(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000674 case ISD::BR_CC: return visitBR_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000675 case ISD::LOAD: return visitLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000676 case ISD::STORE: return visitSTORE(N);
Chris Lattnerca242442006-03-19 01:27:56 +0000677 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
Dan Gohman7f321562007-06-25 16:23:39 +0000678 case ISD::BUILD_VECTOR: return visitBUILD_VECTOR(N);
679 case ISD::CONCAT_VECTORS: return visitCONCAT_VECTORS(N);
Chris Lattner66445d32006-03-28 22:11:53 +0000680 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000681 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000682 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000683}
684
Chris Lattner6270f682006-10-08 22:57:01 +0000685/// getInputChainForNode - Given a node, return its input chain if it has one,
686/// otherwise return a null sd operand.
687static SDOperand getInputChainForNode(SDNode *N) {
688 if (unsigned NumOps = N->getNumOperands()) {
689 if (N->getOperand(0).getValueType() == MVT::Other)
690 return N->getOperand(0);
691 else if (N->getOperand(NumOps-1).getValueType() == MVT::Other)
692 return N->getOperand(NumOps-1);
693 for (unsigned i = 1; i < NumOps-1; ++i)
694 if (N->getOperand(i).getValueType() == MVT::Other)
695 return N->getOperand(i);
696 }
697 return SDOperand(0, 0);
698}
699
Nate Begeman83e75ec2005-09-06 04:43:02 +0000700SDOperand DAGCombiner::visitTokenFactor(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +0000701 // If N has two operands, where one has an input chain equal to the other,
702 // the 'other' chain is redundant.
703 if (N->getNumOperands() == 2) {
704 if (getInputChainForNode(N->getOperand(0).Val) == N->getOperand(1))
705 return N->getOperand(0);
706 if (getInputChainForNode(N->getOperand(1).Val) == N->getOperand(0))
707 return N->getOperand(1);
708 }
709
Chris Lattnerc76d4412007-05-16 06:37:59 +0000710 SmallVector<SDNode *, 8> TFs; // List of token factors to visit.
711 SmallVector<SDOperand, 8> Ops; // Ops for replacing token factor.
712 SmallPtrSet<SDNode*, 16> SeenOps;
713 bool Changed = false; // If we should replace this token factor.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000714
715 // Start out with this token factor.
Jim Laskey279f0532006-09-25 16:29:54 +0000716 TFs.push_back(N);
Jim Laskey279f0532006-09-25 16:29:54 +0000717
Jim Laskey71382342006-10-07 23:37:56 +0000718 // Iterate through token factors. The TFs grows when new token factors are
Jim Laskeybc588b82006-10-05 15:07:25 +0000719 // encountered.
720 for (unsigned i = 0; i < TFs.size(); ++i) {
721 SDNode *TF = TFs[i];
722
Jim Laskey6ff23e52006-10-04 16:53:27 +0000723 // Check each of the operands.
724 for (unsigned i = 0, ie = TF->getNumOperands(); i != ie; ++i) {
725 SDOperand Op = TF->getOperand(i);
Jim Laskey279f0532006-09-25 16:29:54 +0000726
Jim Laskey6ff23e52006-10-04 16:53:27 +0000727 switch (Op.getOpcode()) {
728 case ISD::EntryToken:
Jim Laskeybc588b82006-10-05 15:07:25 +0000729 // Entry tokens don't need to be added to the list. They are
730 // rededundant.
731 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000732 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000733
Jim Laskey6ff23e52006-10-04 16:53:27 +0000734 case ISD::TokenFactor:
Jim Laskeybc588b82006-10-05 15:07:25 +0000735 if ((CombinerAA || Op.hasOneUse()) &&
736 std::find(TFs.begin(), TFs.end(), Op.Val) == TFs.end()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +0000737 // Queue up for processing.
738 TFs.push_back(Op.Val);
739 // Clean up in case the token factor is removed.
740 AddToWorkList(Op.Val);
741 Changed = true;
742 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000743 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000744 // Fall thru
745
746 default:
Chris Lattnerc76d4412007-05-16 06:37:59 +0000747 // Only add if it isn't already in the list.
748 if (SeenOps.insert(Op.Val))
Jim Laskeybc588b82006-10-05 15:07:25 +0000749 Ops.push_back(Op);
Chris Lattnerc76d4412007-05-16 06:37:59 +0000750 else
751 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000752 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000753 }
754 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000755 }
756
757 SDOperand Result;
758
759 // If we've change things around then replace token factor.
760 if (Changed) {
761 if (Ops.size() == 0) {
762 // The entry token is the only possible outcome.
763 Result = DAG.getEntryNode();
764 } else {
765 // New and improved token factor.
766 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0], Ops.size());
Nate Begemanded49632005-10-13 03:11:28 +0000767 }
Jim Laskey274062c2006-10-13 23:32:28 +0000768
769 // Don't add users to work list.
770 return CombineTo(N, Result, false);
Nate Begemanded49632005-10-13 03:11:28 +0000771 }
Jim Laskey279f0532006-09-25 16:29:54 +0000772
Jim Laskey6ff23e52006-10-04 16:53:27 +0000773 return Result;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000774}
775
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000776static
777SDOperand combineShlAddConstant(SDOperand N0, SDOperand N1, SelectionDAG &DAG) {
778 MVT::ValueType VT = N0.getValueType();
779 SDOperand N00 = N0.getOperand(0);
780 SDOperand N01 = N0.getOperand(1);
781 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N01);
782 if (N01C && N00.getOpcode() == ISD::ADD && N00.Val->hasOneUse() &&
783 isa<ConstantSDNode>(N00.getOperand(1))) {
784 N0 = DAG.getNode(ISD::ADD, VT,
785 DAG.getNode(ISD::SHL, VT, N00.getOperand(0), N01),
786 DAG.getNode(ISD::SHL, VT, N00.getOperand(1), N01));
787 return DAG.getNode(ISD::ADD, VT, N0, N1);
788 }
789 return SDOperand();
790}
791
Evan Chengb13cdbd2007-06-21 07:39:16 +0000792static
793SDOperand combineSelectAndUse(SDNode *N, SDOperand Slct, SDOperand OtherOp,
794 SelectionDAG &DAG) {
795 MVT::ValueType VT = N->getValueType(0);
796 unsigned Opc = N->getOpcode();
797 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
798 SDOperand LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
799 SDOperand RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
800 ISD::CondCode CC = ISD::SETCC_INVALID;
801 if (isSlctCC)
802 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
803 else {
804 SDOperand CCOp = Slct.getOperand(0);
805 if (CCOp.getOpcode() == ISD::SETCC)
806 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
807 }
808
809 bool DoXform = false;
810 bool InvCC = false;
811 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
812 "Bad input!");
813 if (LHS.getOpcode() == ISD::Constant &&
814 cast<ConstantSDNode>(LHS)->isNullValue())
815 DoXform = true;
816 else if (CC != ISD::SETCC_INVALID &&
817 RHS.getOpcode() == ISD::Constant &&
818 cast<ConstantSDNode>(RHS)->isNullValue()) {
819 std::swap(LHS, RHS);
820 bool isInt = MVT::isInteger(isSlctCC ? Slct.getOperand(0).getValueType()
821 : Slct.getOperand(0).getOperand(0).getValueType());
822 CC = ISD::getSetCCInverse(CC, isInt);
823 DoXform = true;
824 InvCC = true;
825 }
826
827 if (DoXform) {
828 SDOperand Result = DAG.getNode(Opc, VT, OtherOp, RHS);
829 if (isSlctCC)
830 return DAG.getSelectCC(OtherOp, Result,
831 Slct.getOperand(0), Slct.getOperand(1), CC);
832 SDOperand CCOp = Slct.getOperand(0);
833 if (InvCC)
834 CCOp = DAG.getSetCC(CCOp.getValueType(), CCOp.getOperand(0),
835 CCOp.getOperand(1), CC);
836 return DAG.getNode(ISD::SELECT, VT, CCOp, OtherOp, Result);
837 }
838 return SDOperand();
839}
840
Nate Begeman83e75ec2005-09-06 04:43:02 +0000841SDOperand DAGCombiner::visitADD(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000842 SDOperand N0 = N->getOperand(0);
843 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000844 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
845 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemanf89d78d2005-09-07 16:09:19 +0000846 MVT::ValueType VT = N0.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +0000847
848 // fold vector ops
849 SDOperand FoldedVOp = SimplifyVBinOp(N);
850 if (FoldedVOp.Val) return FoldedVOp;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000851
852 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000853 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000854 return DAG.getNode(ISD::ADD, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000855 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000856 if (N0C && !N1C)
857 return DAG.getNode(ISD::ADD, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000858 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000859 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000860 return N0;
Chris Lattner4aafb4f2006-01-12 20:22:43 +0000861 // fold ((c1-A)+c2) -> (c1+c2)-A
862 if (N1C && N0.getOpcode() == ISD::SUB)
863 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
864 return DAG.getNode(ISD::SUB, VT,
865 DAG.getConstant(N1C->getValue()+N0C->getValue(), VT),
866 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000867 // reassociate add
868 SDOperand RADD = ReassociateOps(ISD::ADD, N0, N1);
869 if (RADD.Val != 0)
870 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000871 // fold ((0-A) + B) -> B-A
872 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
873 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000874 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000875 // fold (A + (0-B)) -> A-B
876 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
877 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000878 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +0000879 // fold (A+(B-A)) -> B
880 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000881 return N1.getOperand(0);
Chris Lattner947c2892006-03-13 06:51:27 +0000882
Evan Cheng860771d2006-03-01 01:09:54 +0000883 if (!MVT::isVector(VT) && SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +0000884 return SDOperand(N, 0);
Chris Lattner947c2892006-03-13 06:51:27 +0000885
886 // fold (a+b) -> (a|b) iff a and b share no bits.
887 if (MVT::isInteger(VT) && !MVT::isVector(VT)) {
888 uint64_t LHSZero, LHSOne;
889 uint64_t RHSZero, RHSOne;
890 uint64_t Mask = MVT::getIntVTBitMask(VT);
Dan Gohmanea859be2007-06-22 14:59:07 +0000891 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Chris Lattner947c2892006-03-13 06:51:27 +0000892 if (LHSZero) {
Dan Gohmanea859be2007-06-22 14:59:07 +0000893 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
Chris Lattner947c2892006-03-13 06:51:27 +0000894
895 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
896 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
897 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
898 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
899 return DAG.getNode(ISD::OR, VT, N0, N1);
900 }
901 }
Evan Cheng3ef554d2006-11-06 08:14:30 +0000902
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000903 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
904 if (N0.getOpcode() == ISD::SHL && N0.Val->hasOneUse()) {
905 SDOperand Result = combineShlAddConstant(N0, N1, DAG);
906 if (Result.Val) return Result;
907 }
908 if (N1.getOpcode() == ISD::SHL && N1.Val->hasOneUse()) {
909 SDOperand Result = combineShlAddConstant(N1, N0, DAG);
910 if (Result.Val) return Result;
911 }
912
Evan Chengb13cdbd2007-06-21 07:39:16 +0000913 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
914 if (N0.getOpcode() == ISD::SELECT && N0.Val->hasOneUse()) {
915 SDOperand Result = combineSelectAndUse(N, N0, N1, DAG);
916 if (Result.Val) return Result;
917 }
918 if (N1.getOpcode() == ISD::SELECT && N1.Val->hasOneUse()) {
919 SDOperand Result = combineSelectAndUse(N, N1, N0, DAG);
920 if (Result.Val) return Result;
921 }
922
Dan Gohman7f321562007-06-25 16:23:39 +0000923 // If either operand is undef, the result is undef
924 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
925 return DAG.getNode(ISD::UNDEF, VT);
926
Nate Begeman83e75ec2005-09-06 04:43:02 +0000927 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000928}
929
Chris Lattner91153682007-03-04 20:03:15 +0000930SDOperand DAGCombiner::visitADDC(SDNode *N) {
931 SDOperand N0 = N->getOperand(0);
932 SDOperand N1 = N->getOperand(1);
933 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
934 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
935 MVT::ValueType VT = N0.getValueType();
936
937 // If the flag result is dead, turn this into an ADD.
938 if (N->hasNUsesOfValue(0, 1))
939 return CombineTo(N, DAG.getNode(ISD::ADD, VT, N1, N0),
Chris Lattnerb6541762007-03-04 20:40:38 +0000940 DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
Chris Lattner91153682007-03-04 20:03:15 +0000941
942 // canonicalize constant to RHS.
Chris Lattnerbcf24842007-03-04 20:08:45 +0000943 if (N0C && !N1C) {
944 SDOperand Ops[] = { N1, N0 };
945 return DAG.getNode(ISD::ADDC, N->getVTList(), Ops, 2);
946 }
Chris Lattner91153682007-03-04 20:03:15 +0000947
Chris Lattnerb6541762007-03-04 20:40:38 +0000948 // fold (addc x, 0) -> x + no carry out
949 if (N1C && N1C->isNullValue())
950 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
951
952 // fold (addc a, b) -> (or a, b), CARRY_FALSE iff a and b share no bits.
953 uint64_t LHSZero, LHSOne;
954 uint64_t RHSZero, RHSOne;
955 uint64_t Mask = MVT::getIntVTBitMask(VT);
Dan Gohmanea859be2007-06-22 14:59:07 +0000956 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Chris Lattnerb6541762007-03-04 20:40:38 +0000957 if (LHSZero) {
Dan Gohmanea859be2007-06-22 14:59:07 +0000958 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
Chris Lattnerb6541762007-03-04 20:40:38 +0000959
960 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
961 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
962 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
963 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
964 return CombineTo(N, DAG.getNode(ISD::OR, VT, N0, N1),
965 DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
966 }
Chris Lattner91153682007-03-04 20:03:15 +0000967
968 return SDOperand();
969}
970
971SDOperand DAGCombiner::visitADDE(SDNode *N) {
972 SDOperand N0 = N->getOperand(0);
973 SDOperand N1 = N->getOperand(1);
Chris Lattnerb6541762007-03-04 20:40:38 +0000974 SDOperand CarryIn = N->getOperand(2);
Chris Lattner91153682007-03-04 20:03:15 +0000975 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
976 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Chris Lattnerbcf24842007-03-04 20:08:45 +0000977 //MVT::ValueType VT = N0.getValueType();
Chris Lattner91153682007-03-04 20:03:15 +0000978
979 // canonicalize constant to RHS
Chris Lattnerbcf24842007-03-04 20:08:45 +0000980 if (N0C && !N1C) {
Chris Lattnerb6541762007-03-04 20:40:38 +0000981 SDOperand Ops[] = { N1, N0, CarryIn };
Chris Lattnerbcf24842007-03-04 20:08:45 +0000982 return DAG.getNode(ISD::ADDE, N->getVTList(), Ops, 3);
983 }
Chris Lattner91153682007-03-04 20:03:15 +0000984
Chris Lattnerb6541762007-03-04 20:40:38 +0000985 // fold (adde x, y, false) -> (addc x, y)
986 if (CarryIn.getOpcode() == ISD::CARRY_FALSE) {
987 SDOperand Ops[] = { N1, N0 };
988 return DAG.getNode(ISD::ADDC, N->getVTList(), Ops, 2);
989 }
Chris Lattner91153682007-03-04 20:03:15 +0000990
991 return SDOperand();
992}
993
994
995
Nate Begeman83e75ec2005-09-06 04:43:02 +0000996SDOperand DAGCombiner::visitSUB(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000997 SDOperand N0 = N->getOperand(0);
998 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000999 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1000 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +00001001 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001002
Dan Gohman7f321562007-06-25 16:23:39 +00001003 // fold vector ops
1004 SDOperand FoldedVOp = SimplifyVBinOp(N);
1005 if (FoldedVOp.Val) return FoldedVOp;
1006
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 }
Dan Gohman7f321562007-06-25 16:23:39 +00001027 // If either operand is undef, the result is undef
1028 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
1029 return DAG.getNode(ISD::UNDEF, VT);
1030
Nate Begeman83e75ec2005-09-06 04:43:02 +00001031 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001032}
1033
Nate Begeman83e75ec2005-09-06 04:43:02 +00001034SDOperand DAGCombiner::visitMUL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001035 SDOperand N0 = N->getOperand(0);
1036 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001037 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1038 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman223df222005-09-08 20:18:10 +00001039 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001040
Dan Gohman7f321562007-06-25 16:23:39 +00001041 // fold vector ops
1042 SDOperand FoldedVOp = SimplifyVBinOp(N);
1043 if (FoldedVOp.Val) return FoldedVOp;
1044
Nate Begeman1d4d4142005-09-01 00:19:25 +00001045 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001046 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001047 return DAG.getNode(ISD::MUL, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001048 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001049 if (N0C && !N1C)
1050 return DAG.getNode(ISD::MUL, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001051 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001052 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001053 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001054 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +00001055 if (N1C && N1C->isAllOnesValue())
Nate Begeman405e3ec2005-10-21 00:02:42 +00001056 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001057 // fold (mul x, (1 << c)) -> x << c
Nate Begeman646d7e22005-09-02 21:18:40 +00001058 if (N1C && isPowerOf2_64(N1C->getValue()))
Chris Lattner3e6099b2005-10-30 06:41:49 +00001059 return DAG.getNode(ISD::SHL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +00001060 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001061 TLI.getShiftAmountTy()));
Chris Lattner3e6099b2005-10-30 06:41:49 +00001062 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
1063 if (N1C && isPowerOf2_64(-N1C->getSignExtended())) {
1064 // FIXME: If the input is something that is easily negated (e.g. a
1065 // single-use add), we should put the negate there.
1066 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT),
1067 DAG.getNode(ISD::SHL, VT, N0,
1068 DAG.getConstant(Log2_64(-N1C->getSignExtended()),
1069 TLI.getShiftAmountTy())));
1070 }
Andrew Lenharth50a0d422006-04-02 21:42:45 +00001071
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001072 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
1073 if (N1C && N0.getOpcode() == ISD::SHL &&
1074 isa<ConstantSDNode>(N0.getOperand(1))) {
1075 SDOperand C3 = DAG.getNode(ISD::SHL, VT, N1, N0.getOperand(1));
Chris Lattner5750df92006-03-01 04:03:14 +00001076 AddToWorkList(C3.Val);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001077 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0), C3);
1078 }
1079
1080 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
1081 // use.
1082 {
1083 SDOperand Sh(0,0), Y(0,0);
1084 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
1085 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
1086 N0.Val->hasOneUse()) {
1087 Sh = N0; Y = N1;
1088 } else if (N1.getOpcode() == ISD::SHL &&
1089 isa<ConstantSDNode>(N1.getOperand(1)) && N1.Val->hasOneUse()) {
1090 Sh = N1; Y = N0;
1091 }
1092 if (Sh.Val) {
1093 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Sh.getOperand(0), Y);
1094 return DAG.getNode(ISD::SHL, VT, Mul, Sh.getOperand(1));
1095 }
1096 }
Chris Lattnera1deca32006-03-04 23:33:26 +00001097 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
1098 if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() &&
1099 isa<ConstantSDNode>(N0.getOperand(1))) {
1100 return DAG.getNode(ISD::ADD, VT,
1101 DAG.getNode(ISD::MUL, VT, N0.getOperand(0), N1),
1102 DAG.getNode(ISD::MUL, VT, N0.getOperand(1), N1));
1103 }
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001104
Nate Begemancd4d58c2006-02-03 06:46:56 +00001105 // reassociate mul
1106 SDOperand RMUL = ReassociateOps(ISD::MUL, N0, N1);
1107 if (RMUL.Val != 0)
1108 return RMUL;
Dan Gohman7f321562007-06-25 16:23:39 +00001109
1110 // If either operand is undef, the result is undef
1111 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
1112 return DAG.getNode(ISD::UNDEF, VT);
1113
Nate Begeman83e75ec2005-09-06 04:43:02 +00001114 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001115}
1116
Nate Begeman83e75ec2005-09-06 04:43:02 +00001117SDOperand DAGCombiner::visitSDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001118 SDOperand N0 = N->getOperand(0);
1119 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001120 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1121 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +00001122 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001123
Dan Gohman7f321562007-06-25 16:23:39 +00001124 // fold vector ops
1125 SDOperand FoldedVOp = SimplifyVBinOp(N);
1126 if (FoldedVOp.Val) return FoldedVOp;
1127
Nate Begeman1d4d4142005-09-01 00:19:25 +00001128 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001129 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001130 return DAG.getNode(ISD::SDIV, VT, N0, N1);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001131 // fold (sdiv X, 1) -> X
1132 if (N1C && N1C->getSignExtended() == 1LL)
1133 return N0;
1134 // fold (sdiv X, -1) -> 0-X
1135 if (N1C && N1C->isAllOnesValue())
1136 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +00001137 // If we know the sign bits of both operands are zero, strength reduce to a
1138 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
1139 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001140 if (DAG.MaskedValueIsZero(N1, SignBit) &&
1141 DAG.MaskedValueIsZero(N0, SignBit))
Chris Lattner094c8fc2005-10-07 06:10:46 +00001142 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
Nate Begemancd6a6ed2006-02-17 07:26:20 +00001143 // fold (sdiv X, pow2) -> simple ops after legalize
Nate Begemanfb7217b2006-02-17 19:54:08 +00001144 if (N1C && N1C->getValue() && !TLI.isIntDivCheap() &&
Nate Begeman405e3ec2005-10-21 00:02:42 +00001145 (isPowerOf2_64(N1C->getSignExtended()) ||
1146 isPowerOf2_64(-N1C->getSignExtended()))) {
1147 // If dividing by powers of two is cheap, then don't perform the following
1148 // fold.
1149 if (TLI.isPow2DivCheap())
1150 return SDOperand();
1151 int64_t pow2 = N1C->getSignExtended();
1152 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
Chris Lattner8f4880b2006-02-16 08:02:36 +00001153 unsigned lg2 = Log2_64(abs2);
1154 // Splat the sign bit into the register
1155 SDOperand SGN = DAG.getNode(ISD::SRA, VT, N0,
Nate Begeman405e3ec2005-10-21 00:02:42 +00001156 DAG.getConstant(MVT::getSizeInBits(VT)-1,
1157 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00001158 AddToWorkList(SGN.Val);
Chris Lattner8f4880b2006-02-16 08:02:36 +00001159 // Add (N0 < 0) ? abs2 - 1 : 0;
1160 SDOperand SRL = DAG.getNode(ISD::SRL, VT, SGN,
1161 DAG.getConstant(MVT::getSizeInBits(VT)-lg2,
Nate Begeman405e3ec2005-10-21 00:02:42 +00001162 TLI.getShiftAmountTy()));
Chris Lattner8f4880b2006-02-16 08:02:36 +00001163 SDOperand ADD = DAG.getNode(ISD::ADD, VT, N0, SRL);
Chris Lattner5750df92006-03-01 04:03:14 +00001164 AddToWorkList(SRL.Val);
1165 AddToWorkList(ADD.Val); // Divide by pow2
Chris Lattner8f4880b2006-02-16 08:02:36 +00001166 SDOperand SRA = DAG.getNode(ISD::SRA, VT, ADD,
1167 DAG.getConstant(lg2, TLI.getShiftAmountTy()));
Nate Begeman405e3ec2005-10-21 00:02:42 +00001168 // If we're dividing by a positive value, we're done. Otherwise, we must
1169 // negate the result.
1170 if (pow2 > 0)
1171 return SRA;
Chris Lattner5750df92006-03-01 04:03:14 +00001172 AddToWorkList(SRA.Val);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001173 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), SRA);
1174 }
Nate Begeman69575232005-10-20 02:15:44 +00001175 // if integer divide is expensive and we satisfy the requirements, emit an
1176 // alternate sequence.
Nate Begeman405e3ec2005-10-21 00:02:42 +00001177 if (N1C && (N1C->getSignExtended() < -1 || N1C->getSignExtended() > 1) &&
Chris Lattnere9936d12005-10-22 18:50:15 +00001178 !TLI.isIntDivCheap()) {
1179 SDOperand Op = BuildSDIV(N);
1180 if (Op.Val) return Op;
Nate Begeman69575232005-10-20 02:15:44 +00001181 }
Dan Gohman7f321562007-06-25 16:23:39 +00001182
1183 // If either operand is undef, the result is undef
1184 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
1185 return DAG.getNode(ISD::UNDEF, VT);
1186
Nate Begeman83e75ec2005-09-06 04:43:02 +00001187 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001188}
1189
Nate Begeman83e75ec2005-09-06 04:43:02 +00001190SDOperand DAGCombiner::visitUDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001191 SDOperand N0 = N->getOperand(0);
1192 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001193 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1194 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +00001195 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001196
Dan Gohman7f321562007-06-25 16:23:39 +00001197 // fold vector ops
1198 SDOperand FoldedVOp = SimplifyVBinOp(N);
1199 if (FoldedVOp.Val) return FoldedVOp;
1200
Nate Begeman1d4d4142005-09-01 00:19:25 +00001201 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001202 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001203 return DAG.getNode(ISD::UDIV, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001204 // fold (udiv x, (1 << c)) -> x >>u c
Nate Begeman646d7e22005-09-02 21:18:40 +00001205 if (N1C && isPowerOf2_64(N1C->getValue()))
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001206 return DAG.getNode(ISD::SRL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +00001207 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001208 TLI.getShiftAmountTy()));
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001209 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
1210 if (N1.getOpcode() == ISD::SHL) {
1211 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
1212 if (isPowerOf2_64(SHC->getValue())) {
1213 MVT::ValueType ADDVT = N1.getOperand(1).getValueType();
Nate Begemanc031e332006-02-05 07:36:48 +00001214 SDOperand Add = DAG.getNode(ISD::ADD, ADDVT, N1.getOperand(1),
1215 DAG.getConstant(Log2_64(SHC->getValue()),
1216 ADDVT));
Chris Lattner5750df92006-03-01 04:03:14 +00001217 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +00001218 return DAG.getNode(ISD::SRL, VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001219 }
1220 }
1221 }
Nate Begeman69575232005-10-20 02:15:44 +00001222 // fold (udiv x, c) -> alternate
Chris Lattnere9936d12005-10-22 18:50:15 +00001223 if (N1C && N1C->getValue() && !TLI.isIntDivCheap()) {
1224 SDOperand Op = BuildUDIV(N);
1225 if (Op.Val) return Op;
1226 }
Dan Gohman7f321562007-06-25 16:23:39 +00001227
1228 // If either operand is undef, the result is undef
1229 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
1230 return DAG.getNode(ISD::UNDEF, VT);
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::visitSREM(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 (srem 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::SREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +00001245 // If we know the sign bits of both operands are zero, strength reduce to a
1246 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
1247 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001248 if (DAG.MaskedValueIsZero(N1, SignBit) &&
1249 DAG.MaskedValueIsZero(N0, SignBit))
Nate Begemana148d982006-01-18 22:35:16 +00001250 return DAG.getNode(ISD::UREM, VT, N0, N1);
Chris Lattner26d29902006-10-12 20:58:32 +00001251
1252 // Unconditionally lower X%C -> X-X/C*C. This allows the X/C logic to hack on
1253 // the remainder operation.
1254 if (N1C && !N1C->isNullValue()) {
1255 SDOperand Div = DAG.getNode(ISD::SDIV, VT, N0, N1);
1256 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Div, N1);
1257 SDOperand Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
1258 AddToWorkList(Div.Val);
1259 AddToWorkList(Mul.Val);
1260 return Sub;
1261 }
1262
Dan Gohman7f321562007-06-25 16:23:39 +00001263 // If either operand is undef, the result is undef
1264 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
1265 return DAG.getNode(ISD::UNDEF, VT);
1266
Nate Begeman83e75ec2005-09-06 04:43:02 +00001267 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001268}
1269
Nate Begeman83e75ec2005-09-06 04:43:02 +00001270SDOperand DAGCombiner::visitUREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001271 SDOperand N0 = N->getOperand(0);
1272 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001273 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1274 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +00001275 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001276
1277 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001278 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001279 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +00001280 // fold (urem x, pow2) -> (and x, pow2-1)
1281 if (N1C && !N1C->isNullValue() && isPowerOf2_64(N1C->getValue()))
Nate Begemana148d982006-01-18 22:35:16 +00001282 return DAG.getNode(ISD::AND, VT, N0, DAG.getConstant(N1C->getValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +00001283 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
1284 if (N1.getOpcode() == ISD::SHL) {
1285 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
1286 if (isPowerOf2_64(SHC->getValue())) {
Nate Begemanbab92392006-02-05 08:07:24 +00001287 SDOperand Add = DAG.getNode(ISD::ADD, VT, N1,DAG.getConstant(~0ULL,VT));
Chris Lattner5750df92006-03-01 04:03:14 +00001288 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +00001289 return DAG.getNode(ISD::AND, VT, N0, Add);
1290 }
1291 }
1292 }
Chris Lattner26d29902006-10-12 20:58:32 +00001293
1294 // Unconditionally lower X%C -> X-X/C*C. This allows the X/C logic to hack on
1295 // the remainder operation.
1296 if (N1C && !N1C->isNullValue()) {
1297 SDOperand Div = DAG.getNode(ISD::UDIV, VT, N0, N1);
1298 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Div, N1);
1299 SDOperand Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
1300 AddToWorkList(Div.Val);
1301 AddToWorkList(Mul.Val);
1302 return Sub;
1303 }
1304
Dan Gohman7f321562007-06-25 16:23:39 +00001305 // If either operand is undef, the result is undef
1306 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
1307 return DAG.getNode(ISD::UNDEF, VT);
1308
Nate Begeman83e75ec2005-09-06 04:43:02 +00001309 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001310}
1311
Nate Begeman83e75ec2005-09-06 04:43:02 +00001312SDOperand DAGCombiner::visitMULHS(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001313 SDOperand N0 = N->getOperand(0);
1314 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001315 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Dan Gohman7f321562007-06-25 16:23:39 +00001316 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001317
1318 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001319 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001320 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001321 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001322 if (N1C && N1C->getValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001323 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
1324 DAG.getConstant(MVT::getSizeInBits(N0.getValueType())-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001325 TLI.getShiftAmountTy()));
Dan Gohman7f321562007-06-25 16:23:39 +00001326 // If either operand is undef, the result is undef
1327 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
1328 return DAG.getNode(ISD::UNDEF, VT);
1329
Nate Begeman83e75ec2005-09-06 04:43:02 +00001330 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001331}
1332
Nate Begeman83e75ec2005-09-06 04:43:02 +00001333SDOperand DAGCombiner::visitMULHU(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001334 SDOperand N0 = N->getOperand(0);
1335 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001336 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Dan Gohman7f321562007-06-25 16:23:39 +00001337 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001338
1339 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001340 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001341 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001342 // fold (mulhu x, 1) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001343 if (N1C && N1C->getValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001344 return DAG.getConstant(0, N0.getValueType());
Dan Gohman7f321562007-06-25 16:23:39 +00001345 // If either operand is undef, the result is undef
1346 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
1347 return DAG.getNode(ISD::UNDEF, VT);
1348
Nate Begeman83e75ec2005-09-06 04:43:02 +00001349 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001350}
1351
Chris Lattner35e5c142006-05-05 05:51:50 +00001352/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
1353/// two operands of the same opcode, try to simplify it.
1354SDOperand DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
1355 SDOperand N0 = N->getOperand(0), N1 = N->getOperand(1);
1356 MVT::ValueType VT = N0.getValueType();
1357 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
1358
Chris Lattner540121f2006-05-05 06:31:05 +00001359 // For each of OP in AND/OR/XOR:
1360 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
1361 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
1362 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
Chris Lattner0d8dae72006-05-05 06:32:04 +00001363 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y))
Chris Lattner540121f2006-05-05 06:31:05 +00001364 if ((N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND||
Chris Lattner0d8dae72006-05-05 06:32:04 +00001365 N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::TRUNCATE) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001366 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1367 SDOperand ORNode = DAG.getNode(N->getOpcode(),
1368 N0.getOperand(0).getValueType(),
1369 N0.getOperand(0), N1.getOperand(0));
1370 AddToWorkList(ORNode.Val);
Chris Lattner540121f2006-05-05 06:31:05 +00001371 return DAG.getNode(N0.getOpcode(), VT, ORNode);
Chris Lattner35e5c142006-05-05 05:51:50 +00001372 }
1373
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001374 // For each of OP in SHL/SRL/SRA/AND...
1375 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
1376 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
1377 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
Chris Lattner35e5c142006-05-05 05:51:50 +00001378 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001379 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001380 N0.getOperand(1) == N1.getOperand(1)) {
1381 SDOperand ORNode = DAG.getNode(N->getOpcode(),
1382 N0.getOperand(0).getValueType(),
1383 N0.getOperand(0), N1.getOperand(0));
1384 AddToWorkList(ORNode.Val);
1385 return DAG.getNode(N0.getOpcode(), VT, ORNode, N0.getOperand(1));
1386 }
1387
Dan Gohman7f321562007-06-25 16:23:39 +00001388 // If either operand is undef, the result is undef
1389 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
1390 return DAG.getNode(ISD::UNDEF, VT);
1391
Chris Lattner35e5c142006-05-05 05:51:50 +00001392 return SDOperand();
1393}
1394
Nate Begeman83e75ec2005-09-06 04:43:02 +00001395SDOperand DAGCombiner::visitAND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001396 SDOperand N0 = N->getOperand(0);
1397 SDOperand N1 = N->getOperand(1);
Nate Begemanfb7217b2006-02-17 19:54:08 +00001398 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001399 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1400 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001401 MVT::ValueType VT = N1.getValueType();
1402
Dan Gohman7f321562007-06-25 16:23:39 +00001403 // fold vector ops
1404 SDOperand FoldedVOp = SimplifyVBinOp(N);
1405 if (FoldedVOp.Val) return FoldedVOp;
1406
Nate Begeman1d4d4142005-09-01 00:19:25 +00001407 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001408 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001409 return DAG.getNode(ISD::AND, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001410 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001411 if (N0C && !N1C)
1412 return DAG.getNode(ISD::AND, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001413 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001414 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001415 return N0;
1416 // if (and x, c) is known to be zero, return 0
Dan Gohmanea859be2007-06-22 14:59:07 +00001417 if (N1C && DAG.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001418 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +00001419 // reassociate and
1420 SDOperand RAND = ReassociateOps(ISD::AND, N0, N1);
1421 if (RAND.Val != 0)
1422 return RAND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001423 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
Nate Begeman5dc7e862005-11-02 18:42:59 +00001424 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001425 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Nate Begeman646d7e22005-09-02 21:18:40 +00001426 if ((ORI->getValue() & N1C->getValue()) == N1C->getValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001427 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +00001428 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
1429 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Chris Lattner1ec05d12006-03-01 21:47:21 +00001430 unsigned InMask = MVT::getIntVTBitMask(N0.getOperand(0).getValueType());
Dan Gohmanea859be2007-06-22 14:59:07 +00001431 if (DAG.MaskedValueIsZero(N0.getOperand(0),
Chris Lattner1ec05d12006-03-01 21:47:21 +00001432 ~N1C->getValue() & InMask)) {
1433 SDOperand Zext = DAG.getNode(ISD::ZERO_EXTEND, N0.getValueType(),
1434 N0.getOperand(0));
1435
1436 // Replace uses of the AND with uses of the Zero extend node.
1437 CombineTo(N, Zext);
1438
Chris Lattner3603cd62006-02-02 07:17:31 +00001439 // We actually want to replace all uses of the any_extend with the
1440 // zero_extend, to avoid duplicating things. This will later cause this
1441 // AND to be folded.
Chris Lattner1ec05d12006-03-01 21:47:21 +00001442 CombineTo(N0.Val, Zext);
Chris Lattnerfedced72006-04-20 23:55:59 +00001443 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner3603cd62006-02-02 07:17:31 +00001444 }
1445 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001446 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
1447 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1448 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1449 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1450
1451 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1452 MVT::isInteger(LL.getValueType())) {
1453 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
1454 if (cast<ConstantSDNode>(LR)->getValue() == 0 && Op1 == ISD::SETEQ) {
1455 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001456 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001457 return DAG.getSetCC(VT, ORNode, LR, Op1);
1458 }
1459 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
1460 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
1461 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001462 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001463 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1464 }
1465 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
1466 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
1467 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001468 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001469 return DAG.getSetCC(VT, ORNode, LR, Op1);
1470 }
1471 }
1472 // canonicalize equivalent to ll == rl
1473 if (LL == RR && LR == RL) {
1474 Op1 = ISD::getSetCCSwappedOperands(Op1);
1475 std::swap(RL, RR);
1476 }
1477 if (LL == RL && LR == RR) {
1478 bool isInteger = MVT::isInteger(LL.getValueType());
1479 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
1480 if (Result != ISD::SETCC_INVALID)
1481 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1482 }
1483 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001484
1485 // Simplify: and (op x...), (op y...) -> (op (and x, y))
1486 if (N0.getOpcode() == N1.getOpcode()) {
1487 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1488 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001489 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001490
Nate Begemande996292006-02-03 22:24:05 +00001491 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
1492 // fold (and (sra)) -> (and (srl)) when possible.
Chris Lattner6ea2dee2006-03-25 22:19:00 +00001493 if (!MVT::isVector(VT) &&
1494 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001495 return SDOperand(N, 0);
Nate Begemanded49632005-10-13 03:11:28 +00001496 // fold (zext_inreg (extload x)) -> (zextload x)
Evan Cheng83060c52007-03-07 08:07:03 +00001497 if (ISD::isEXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val)) {
Evan Cheng466685d2006-10-09 20:57:25 +00001498 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00001499 MVT::ValueType EVT = LN0->getLoadedVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001500 // If we zero all the possible extended bits, then we can turn this into
1501 // a zextload if we are running before legalize or the operation is legal.
Dan Gohmanea859be2007-06-22 14:59:07 +00001502 if (DAG.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Evan Chengc5484282006-10-04 00:56:09 +00001503 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00001504 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
1505 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00001506 LN0->getSrcValueOffset(), EVT,
1507 LN0->isVolatile(),
1508 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00001509 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001510 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001511 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001512 }
1513 }
1514 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Evan Cheng83060c52007-03-07 08:07:03 +00001515 if (ISD::isSEXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val) &&
1516 N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001517 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00001518 MVT::ValueType EVT = LN0->getLoadedVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001519 // If we zero all the possible extended bits, then we can turn this into
1520 // a zextload if we are running before legalize or the operation is legal.
Dan Gohmanea859be2007-06-22 14:59:07 +00001521 if (DAG.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Evan Chengc5484282006-10-04 00:56:09 +00001522 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00001523 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
1524 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00001525 LN0->getSrcValueOffset(), EVT,
1526 LN0->isVolatile(),
1527 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00001528 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001529 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001530 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001531 }
1532 }
Chris Lattner15045b62006-02-28 06:35:35 +00001533
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001534 // fold (and (load x), 255) -> (zextload x, i8)
1535 // fold (and (extload x, i16), 255) -> (zextload x, i8)
Evan Cheng466685d2006-10-09 20:57:25 +00001536 if (N1C && N0.getOpcode() == ISD::LOAD) {
1537 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
1538 if (LN0->getExtensionType() != ISD::SEXTLOAD &&
Evan Cheng83060c52007-03-07 08:07:03 +00001539 LN0->getAddressingMode() == ISD::UNINDEXED &&
Evan Cheng466685d2006-10-09 20:57:25 +00001540 N0.hasOneUse()) {
1541 MVT::ValueType EVT, LoadedVT;
1542 if (N1C->getValue() == 255)
1543 EVT = MVT::i8;
1544 else if (N1C->getValue() == 65535)
1545 EVT = MVT::i16;
1546 else if (N1C->getValue() == ~0U)
1547 EVT = MVT::i32;
1548 else
1549 EVT = MVT::Other;
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001550
Evan Cheng2e49f092006-10-11 07:10:22 +00001551 LoadedVT = LN0->getLoadedVT();
Evan Cheng466685d2006-10-09 20:57:25 +00001552 if (EVT != MVT::Other && LoadedVT > EVT &&
1553 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
1554 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
1555 // For big endian targets, we need to add an offset to the pointer to
1556 // load the correct bytes. For little endian systems, we merely need to
1557 // read fewer bytes from the same pointer.
1558 unsigned PtrOff =
1559 (MVT::getSizeInBits(LoadedVT) - MVT::getSizeInBits(EVT)) / 8;
1560 SDOperand NewPtr = LN0->getBasePtr();
1561 if (!TLI.isLittleEndian())
1562 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
1563 DAG.getConstant(PtrOff, PtrType));
1564 AddToWorkList(NewPtr.Val);
1565 SDOperand Load =
1566 DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00001567 LN0->getSrcValue(), LN0->getSrcValueOffset(), EVT,
1568 LN0->isVolatile(), LN0->getAlignment());
Evan Cheng466685d2006-10-09 20:57:25 +00001569 AddToWorkList(N);
1570 CombineTo(N0.Val, Load, Load.getValue(1));
1571 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1572 }
Chris Lattner15045b62006-02-28 06:35:35 +00001573 }
1574 }
1575
Nate Begeman83e75ec2005-09-06 04:43:02 +00001576 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001577}
1578
Nate Begeman83e75ec2005-09-06 04:43:02 +00001579SDOperand DAGCombiner::visitOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001580 SDOperand N0 = N->getOperand(0);
1581 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001582 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001583 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1584 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001585 MVT::ValueType VT = N1.getValueType();
1586 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001587
Dan Gohman7f321562007-06-25 16:23:39 +00001588 // fold vector ops
1589 SDOperand FoldedVOp = SimplifyVBinOp(N);
1590 if (FoldedVOp.Val) return FoldedVOp;
1591
Nate Begeman1d4d4142005-09-01 00:19:25 +00001592 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001593 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001594 return DAG.getNode(ISD::OR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001595 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001596 if (N0C && !N1C)
1597 return DAG.getNode(ISD::OR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001598 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001599 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001600 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001601 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001602 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001603 return N1;
1604 // fold (or x, c) -> c iff (x & ~c) == 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001605 if (N1C &&
Dan Gohmanea859be2007-06-22 14:59:07 +00001606 DAG.MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits))))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001607 return N1;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001608 // reassociate or
1609 SDOperand ROR = ReassociateOps(ISD::OR, N0, N1);
1610 if (ROR.Val != 0)
1611 return ROR;
1612 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
1613 if (N1C && N0.getOpcode() == ISD::AND && N0.Val->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00001614 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00001615 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
1616 return DAG.getNode(ISD::AND, VT, DAG.getNode(ISD::OR, VT, N0.getOperand(0),
1617 N1),
1618 DAG.getConstant(N1C->getValue() | C1->getValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00001619 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001620 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
1621 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1622 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1623 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1624
1625 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1626 MVT::isInteger(LL.getValueType())) {
1627 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
1628 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
1629 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
1630 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
1631 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001632 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001633 return DAG.getSetCC(VT, ORNode, LR, Op1);
1634 }
1635 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
1636 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
1637 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
1638 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
1639 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001640 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001641 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1642 }
1643 }
1644 // canonicalize equivalent to ll == rl
1645 if (LL == RR && LR == RL) {
1646 Op1 = ISD::getSetCCSwappedOperands(Op1);
1647 std::swap(RL, RR);
1648 }
1649 if (LL == RL && LR == RR) {
1650 bool isInteger = MVT::isInteger(LL.getValueType());
1651 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
1652 if (Result != ISD::SETCC_INVALID)
1653 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1654 }
1655 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001656
1657 // Simplify: or (op x...), (op y...) -> (op (or x, y))
1658 if (N0.getOpcode() == N1.getOpcode()) {
1659 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1660 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001661 }
Chris Lattner516b9622006-09-14 20:50:57 +00001662
Chris Lattner1ec72732006-09-14 21:11:37 +00001663 // (X & C1) | (Y & C2) -> (X|Y) & C3 if possible.
1664 if (N0.getOpcode() == ISD::AND &&
1665 N1.getOpcode() == ISD::AND &&
1666 N0.getOperand(1).getOpcode() == ISD::Constant &&
1667 N1.getOperand(1).getOpcode() == ISD::Constant &&
1668 // Don't increase # computations.
1669 (N0.Val->hasOneUse() || N1.Val->hasOneUse())) {
1670 // We can only do this xform if we know that bits from X that are set in C2
1671 // but not in C1 are already zero. Likewise for Y.
1672 uint64_t LHSMask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1673 uint64_t RHSMask = cast<ConstantSDNode>(N1.getOperand(1))->getValue();
1674
Dan Gohmanea859be2007-06-22 14:59:07 +00001675 if (DAG.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) &&
1676 DAG.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) {
Chris Lattner1ec72732006-09-14 21:11:37 +00001677 SDOperand X =DAG.getNode(ISD::OR, VT, N0.getOperand(0), N1.getOperand(0));
1678 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(LHSMask|RHSMask, VT));
1679 }
1680 }
1681
1682
Chris Lattner516b9622006-09-14 20:50:57 +00001683 // See if this is some rotate idiom.
1684 if (SDNode *Rot = MatchRotate(N0, N1))
1685 return SDOperand(Rot, 0);
Chris Lattner35e5c142006-05-05 05:51:50 +00001686
Nate Begeman83e75ec2005-09-06 04:43:02 +00001687 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001688}
1689
Chris Lattner516b9622006-09-14 20:50:57 +00001690
1691/// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present.
1692static bool MatchRotateHalf(SDOperand Op, SDOperand &Shift, SDOperand &Mask) {
1693 if (Op.getOpcode() == ISD::AND) {
Reid Spencer3ed469c2006-11-02 20:25:50 +00001694 if (isa<ConstantSDNode>(Op.getOperand(1))) {
Chris Lattner516b9622006-09-14 20:50:57 +00001695 Mask = Op.getOperand(1);
1696 Op = Op.getOperand(0);
1697 } else {
1698 return false;
1699 }
1700 }
1701
1702 if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) {
1703 Shift = Op;
1704 return true;
1705 }
1706 return false;
1707}
1708
1709
1710// MatchRotate - Handle an 'or' of two operands. If this is one of the many
1711// idioms for rotate, and if the target supports rotation instructions, generate
1712// a rot[lr].
1713SDNode *DAGCombiner::MatchRotate(SDOperand LHS, SDOperand RHS) {
1714 // Must be a legal type. Expanded an promoted things won't work with rotates.
1715 MVT::ValueType VT = LHS.getValueType();
1716 if (!TLI.isTypeLegal(VT)) return 0;
1717
1718 // The target must have at least one rotate flavor.
1719 bool HasROTL = TLI.isOperationLegal(ISD::ROTL, VT);
1720 bool HasROTR = TLI.isOperationLegal(ISD::ROTR, VT);
1721 if (!HasROTL && !HasROTR) return 0;
1722
1723 // Match "(X shl/srl V1) & V2" where V2 may not be present.
1724 SDOperand LHSShift; // The shift.
1725 SDOperand LHSMask; // AND value if any.
1726 if (!MatchRotateHalf(LHS, LHSShift, LHSMask))
1727 return 0; // Not part of a rotate.
1728
1729 SDOperand RHSShift; // The shift.
1730 SDOperand RHSMask; // AND value if any.
1731 if (!MatchRotateHalf(RHS, RHSShift, RHSMask))
1732 return 0; // Not part of a rotate.
1733
1734 if (LHSShift.getOperand(0) != RHSShift.getOperand(0))
1735 return 0; // Not shifting the same value.
1736
1737 if (LHSShift.getOpcode() == RHSShift.getOpcode())
1738 return 0; // Shifts must disagree.
1739
1740 // Canonicalize shl to left side in a shl/srl pair.
1741 if (RHSShift.getOpcode() == ISD::SHL) {
1742 std::swap(LHS, RHS);
1743 std::swap(LHSShift, RHSShift);
1744 std::swap(LHSMask , RHSMask );
1745 }
1746
1747 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Scott Michelc9dc1142007-04-02 21:36:32 +00001748 SDOperand LHSShiftArg = LHSShift.getOperand(0);
1749 SDOperand LHSShiftAmt = LHSShift.getOperand(1);
1750 SDOperand RHSShiftAmt = RHSShift.getOperand(1);
Chris Lattner516b9622006-09-14 20:50:57 +00001751
1752 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
1753 // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2)
Scott Michelc9dc1142007-04-02 21:36:32 +00001754 if (LHSShiftAmt.getOpcode() == ISD::Constant &&
1755 RHSShiftAmt.getOpcode() == ISD::Constant) {
1756 uint64_t LShVal = cast<ConstantSDNode>(LHSShiftAmt)->getValue();
1757 uint64_t RShVal = cast<ConstantSDNode>(RHSShiftAmt)->getValue();
Chris Lattner516b9622006-09-14 20:50:57 +00001758 if ((LShVal + RShVal) != OpSizeInBits)
1759 return 0;
1760
1761 SDOperand Rot;
1762 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00001763 Rot = DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00001764 else
Scott Michelc9dc1142007-04-02 21:36:32 +00001765 Rot = DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00001766
1767 // If there is an AND of either shifted operand, apply it to the result.
1768 if (LHSMask.Val || RHSMask.Val) {
1769 uint64_t Mask = MVT::getIntVTBitMask(VT);
1770
1771 if (LHSMask.Val) {
1772 uint64_t RHSBits = (1ULL << LShVal)-1;
1773 Mask &= cast<ConstantSDNode>(LHSMask)->getValue() | RHSBits;
1774 }
1775 if (RHSMask.Val) {
1776 uint64_t LHSBits = ~((1ULL << (OpSizeInBits-RShVal))-1);
1777 Mask &= cast<ConstantSDNode>(RHSMask)->getValue() | LHSBits;
1778 }
1779
1780 Rot = DAG.getNode(ISD::AND, VT, Rot, DAG.getConstant(Mask, VT));
1781 }
1782
1783 return Rot.Val;
1784 }
1785
1786 // If there is a mask here, and we have a variable shift, we can't be sure
1787 // that we're masking out the right stuff.
1788 if (LHSMask.Val || RHSMask.Val)
1789 return 0;
1790
1791 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
1792 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotr x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00001793 if (RHSShiftAmt.getOpcode() == ISD::SUB &&
1794 LHSShiftAmt == RHSShiftAmt.getOperand(1)) {
Chris Lattner516b9622006-09-14 20:50:57 +00001795 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00001796 dyn_cast<ConstantSDNode>(RHSShiftAmt.getOperand(0))) {
Chris Lattner516b9622006-09-14 20:50:57 +00001797 if (SUBC->getValue() == OpSizeInBits)
1798 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00001799 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
Chris Lattner516b9622006-09-14 20:50:57 +00001800 else
Scott Michelc9dc1142007-04-02 21:36:32 +00001801 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).Val;
Chris Lattner516b9622006-09-14 20:50:57 +00001802 }
1803 }
1804
1805 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
1806 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotl x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00001807 if (LHSShiftAmt.getOpcode() == ISD::SUB &&
1808 RHSShiftAmt == LHSShiftAmt.getOperand(1)) {
Chris Lattner516b9622006-09-14 20:50:57 +00001809 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00001810 dyn_cast<ConstantSDNode>(LHSShiftAmt.getOperand(0))) {
Chris Lattner516b9622006-09-14 20:50:57 +00001811 if (SUBC->getValue() == OpSizeInBits)
1812 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00001813 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
Chris Lattner516b9622006-09-14 20:50:57 +00001814 else
Scott Michelc9dc1142007-04-02 21:36:32 +00001815 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).Val;
1816 }
1817 }
1818
1819 // Look for sign/zext/any-extended cases:
1820 if ((LHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
1821 || LHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
1822 || LHSShiftAmt.getOpcode() == ISD::ANY_EXTEND) &&
1823 (RHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
1824 || RHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
1825 || RHSShiftAmt.getOpcode() == ISD::ANY_EXTEND)) {
1826 SDOperand LExtOp0 = LHSShiftAmt.getOperand(0);
1827 SDOperand RExtOp0 = RHSShiftAmt.getOperand(0);
1828 if (RExtOp0.getOpcode() == ISD::SUB &&
1829 RExtOp0.getOperand(1) == LExtOp0) {
1830 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
1831 // (rotr x, y)
1832 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
1833 // (rotl x, (sub 32, y))
1834 if (ConstantSDNode *SUBC = cast<ConstantSDNode>(RExtOp0.getOperand(0))) {
1835 if (SUBC->getValue() == OpSizeInBits) {
1836 if (HasROTL)
1837 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
1838 else
1839 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).Val;
1840 }
1841 }
1842 } else if (LExtOp0.getOpcode() == ISD::SUB &&
1843 RExtOp0 == LExtOp0.getOperand(1)) {
1844 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext r))) ->
1845 // (rotl x, y)
1846 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext r))) ->
1847 // (rotr x, (sub 32, y))
1848 if (ConstantSDNode *SUBC = cast<ConstantSDNode>(LExtOp0.getOperand(0))) {
1849 if (SUBC->getValue() == OpSizeInBits) {
1850 if (HasROTL)
1851 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, RHSShiftAmt).Val;
1852 else
1853 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
1854 }
1855 }
Chris Lattner516b9622006-09-14 20:50:57 +00001856 }
1857 }
1858
1859 return 0;
1860}
1861
1862
Nate Begeman83e75ec2005-09-06 04:43:02 +00001863SDOperand DAGCombiner::visitXOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001864 SDOperand N0 = N->getOperand(0);
1865 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001866 SDOperand LHS, RHS, CC;
1867 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1868 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001869 MVT::ValueType VT = N0.getValueType();
1870
Dan Gohman7f321562007-06-25 16:23:39 +00001871 // fold vector ops
1872 SDOperand FoldedVOp = SimplifyVBinOp(N);
1873 if (FoldedVOp.Val) return FoldedVOp;
1874
Nate Begeman1d4d4142005-09-01 00:19:25 +00001875 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001876 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001877 return DAG.getNode(ISD::XOR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001878 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001879 if (N0C && !N1C)
1880 return DAG.getNode(ISD::XOR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001881 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001882 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001883 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001884 // reassociate xor
1885 SDOperand RXOR = ReassociateOps(ISD::XOR, N0, N1);
1886 if (RXOR.Val != 0)
1887 return RXOR;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001888 // fold !(x cc y) -> (x !cc y)
Nate Begeman646d7e22005-09-02 21:18:40 +00001889 if (N1C && N1C->getValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
1890 bool isInt = MVT::isInteger(LHS.getValueType());
1891 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
1892 isInt);
1893 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001894 return DAG.getSetCC(VT, LHS, RHS, NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00001895 if (N0.getOpcode() == ISD::SELECT_CC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001896 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00001897 assert(0 && "Unhandled SetCC Equivalent!");
1898 abort();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001899 }
Nate Begeman99801192005-09-07 23:25:52 +00001900 // fold !(x or y) -> (!x and !y) iff x or y are setcc
Chris Lattner734c91d2006-11-10 21:37:15 +00001901 if (N1C && N1C->getValue() == 1 && VT == MVT::i1 &&
Nate Begeman99801192005-09-07 23:25:52 +00001902 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001903 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00001904 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
1905 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001906 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1907 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00001908 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00001909 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001910 }
1911 }
Nate Begeman99801192005-09-07 23:25:52 +00001912 // fold !(x or y) -> (!x and !y) iff x or y are constants
1913 if (N1C && N1C->isAllOnesValue() &&
1914 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001915 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00001916 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
1917 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001918 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1919 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00001920 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00001921 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001922 }
1923 }
Nate Begeman223df222005-09-08 20:18:10 +00001924 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
1925 if (N1C && N0.getOpcode() == ISD::XOR) {
1926 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
1927 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
1928 if (N00C)
1929 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
1930 DAG.getConstant(N1C->getValue()^N00C->getValue(), VT));
1931 if (N01C)
1932 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
1933 DAG.getConstant(N1C->getValue()^N01C->getValue(), VT));
1934 }
1935 // fold (xor x, x) -> 0
Chris Lattner4fbdd592006-03-28 19:11:05 +00001936 if (N0 == N1) {
1937 if (!MVT::isVector(VT)) {
1938 return DAG.getConstant(0, VT);
1939 } else if (!AfterLegalize || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) {
1940 // Produce a vector of zeros.
Dan Gohman51eaa862007-06-14 22:58:02 +00001941 SDOperand El = DAG.getConstant(0, MVT::getVectorElementType(VT));
Chris Lattner4fbdd592006-03-28 19:11:05 +00001942 std::vector<SDOperand> Ops(MVT::getVectorNumElements(VT), El);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001943 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4fbdd592006-03-28 19:11:05 +00001944 }
1945 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001946
1947 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
1948 if (N0.getOpcode() == N1.getOpcode()) {
1949 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1950 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001951 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001952
Chris Lattner3e104b12006-04-08 04:15:24 +00001953 // Simplify the expression using non-local knowledge.
1954 if (!MVT::isVector(VT) &&
1955 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001956 return SDOperand(N, 0);
Chris Lattner3e104b12006-04-08 04:15:24 +00001957
Nate Begeman83e75ec2005-09-06 04:43:02 +00001958 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001959}
1960
Nate Begeman83e75ec2005-09-06 04:43:02 +00001961SDOperand DAGCombiner::visitSHL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001962 SDOperand N0 = N->getOperand(0);
1963 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001964 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1965 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001966 MVT::ValueType VT = N0.getValueType();
1967 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1968
1969 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001970 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001971 return DAG.getNode(ISD::SHL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001972 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001973 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001974 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001975 // fold (shl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001976 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001977 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001978 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001979 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001980 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001981 // if (shl x, c) is known to be zero, return 0
Dan Gohmanea859be2007-06-22 14:59:07 +00001982 if (DAG.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001983 return DAG.getConstant(0, VT);
Chris Lattner61a4c072007-04-18 03:06:49 +00001984 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001985 return SDOperand(N, 0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001986 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001987 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001988 N0.getOperand(1).getOpcode() == ISD::Constant) {
1989 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001990 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001991 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001992 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001993 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001994 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001995 }
1996 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
1997 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001998 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001999 N0.getOperand(1).getOpcode() == ISD::Constant) {
2000 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00002001 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002002 SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
2003 DAG.getConstant(~0ULL << c1, VT));
2004 if (c2 > c1)
2005 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +00002006 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002007 else
Nate Begeman83e75ec2005-09-06 04:43:02 +00002008 return DAG.getNode(ISD::SRL, VT, Mask,
2009 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002010 }
2011 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00002012 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +00002013 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002014 DAG.getConstant(~0ULL << N1C->getValue(), VT));
2015 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002016}
2017
Nate Begeman83e75ec2005-09-06 04:43:02 +00002018SDOperand DAGCombiner::visitSRA(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002019 SDOperand N0 = N->getOperand(0);
2020 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002021 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2022 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002023 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002024
2025 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002026 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002027 return DAG.getNode(ISD::SRA, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002028 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002029 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002030 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002031 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00002032 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002033 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002034 // fold (sra x, c >= size(x)) -> undef
Nate Begemanfb7217b2006-02-17 19:54:08 +00002035 if (N1C && N1C->getValue() >= MVT::getSizeInBits(VT))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002036 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002037 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002038 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002039 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00002040 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
2041 // sext_inreg.
2042 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
2043 unsigned LowBits = MVT::getSizeInBits(VT) - (unsigned)N1C->getValue();
2044 MVT::ValueType EVT;
2045 switch (LowBits) {
2046 default: EVT = MVT::Other; break;
2047 case 1: EVT = MVT::i1; break;
2048 case 8: EVT = MVT::i8; break;
2049 case 16: EVT = MVT::i16; break;
2050 case 32: EVT = MVT::i32; break;
2051 }
2052 if (EVT > MVT::Other && TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, EVT))
2053 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
2054 DAG.getValueType(EVT));
2055 }
Chris Lattner71d9ebc2006-02-28 06:23:04 +00002056
2057 // fold (sra (sra x, c1), c2) -> (sra x, c1+c2)
2058 if (N1C && N0.getOpcode() == ISD::SRA) {
2059 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2060 unsigned Sum = N1C->getValue() + C1->getValue();
2061 if (Sum >= MVT::getSizeInBits(VT)) Sum = MVT::getSizeInBits(VT)-1;
2062 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0),
2063 DAG.getConstant(Sum, N1C->getValueType(0)));
2064 }
2065 }
2066
Chris Lattnera8504462006-05-08 20:51:54 +00002067 // Simplify, based on bits shifted out of the LHS.
2068 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
2069 return SDOperand(N, 0);
2070
2071
Nate Begeman1d4d4142005-09-01 00:19:25 +00002072 // If the sign bit is known to be zero, switch this to a SRL.
Dan Gohmanea859be2007-06-22 14:59:07 +00002073 if (DAG.MaskedValueIsZero(N0, MVT::getIntVTSignBit(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002074 return DAG.getNode(ISD::SRL, VT, N0, N1);
2075 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002076}
2077
Nate Begeman83e75ec2005-09-06 04:43:02 +00002078SDOperand DAGCombiner::visitSRL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002079 SDOperand N0 = N->getOperand(0);
2080 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002081 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2082 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002083 MVT::ValueType VT = N0.getValueType();
2084 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
2085
2086 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002087 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002088 return DAG.getNode(ISD::SRL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002089 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002090 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002091 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002092 // fold (srl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00002093 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002094 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002095 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002096 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002097 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002098 // if (srl x, c) is known to be zero, return 0
Dan Gohmanea859be2007-06-22 14:59:07 +00002099 if (N1C && DAG.MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002100 return DAG.getConstant(0, VT);
Chris Lattnerec06e9a2007-04-18 03:05:22 +00002101
Nate Begeman1d4d4142005-09-01 00:19:25 +00002102 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002103 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002104 N0.getOperand(1).getOpcode() == ISD::Constant) {
2105 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00002106 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002107 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002108 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002109 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002110 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002111 }
Chris Lattner350bec02006-04-02 06:11:11 +00002112
Chris Lattner06afe072006-05-05 22:53:17 +00002113 // fold (srl (anyextend x), c) -> (anyextend (srl x, c))
2114 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
2115 // Shifting in all undef bits?
2116 MVT::ValueType SmallVT = N0.getOperand(0).getValueType();
2117 if (N1C->getValue() >= MVT::getSizeInBits(SmallVT))
2118 return DAG.getNode(ISD::UNDEF, VT);
2119
2120 SDOperand SmallShift = DAG.getNode(ISD::SRL, SmallVT, N0.getOperand(0), N1);
2121 AddToWorkList(SmallShift.Val);
2122 return DAG.getNode(ISD::ANY_EXTEND, VT, SmallShift);
2123 }
2124
Chris Lattner3657ffe2006-10-12 20:23:19 +00002125 // fold (srl (sra X, Y), 31) -> (srl X, 31). This srl only looks at the sign
2126 // bit, which is unmodified by sra.
2127 if (N1C && N1C->getValue()+1 == MVT::getSizeInBits(VT)) {
2128 if (N0.getOpcode() == ISD::SRA)
2129 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0), N1);
2130 }
2131
Chris Lattner350bec02006-04-02 06:11:11 +00002132 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
2133 if (N1C && N0.getOpcode() == ISD::CTLZ &&
2134 N1C->getValue() == Log2_32(MVT::getSizeInBits(VT))) {
2135 uint64_t KnownZero, KnownOne, Mask = MVT::getIntVTBitMask(VT);
Dan Gohmanea859be2007-06-22 14:59:07 +00002136 DAG.ComputeMaskedBits(N0.getOperand(0), Mask, KnownZero, KnownOne);
Chris Lattner350bec02006-04-02 06:11:11 +00002137
2138 // If any of the input bits are KnownOne, then the input couldn't be all
2139 // zeros, thus the result of the srl will always be zero.
2140 if (KnownOne) return DAG.getConstant(0, VT);
2141
2142 // If all of the bits input the to ctlz node are known to be zero, then
2143 // the result of the ctlz is "32" and the result of the shift is one.
2144 uint64_t UnknownBits = ~KnownZero & Mask;
2145 if (UnknownBits == 0) return DAG.getConstant(1, VT);
2146
2147 // Otherwise, check to see if there is exactly one bit input to the ctlz.
2148 if ((UnknownBits & (UnknownBits-1)) == 0) {
2149 // Okay, we know that only that the single bit specified by UnknownBits
2150 // could be set on input to the CTLZ node. If this bit is set, the SRL
2151 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
2152 // to an SRL,XOR pair, which is likely to simplify more.
2153 unsigned ShAmt = CountTrailingZeros_64(UnknownBits);
2154 SDOperand Op = N0.getOperand(0);
2155 if (ShAmt) {
2156 Op = DAG.getNode(ISD::SRL, VT, Op,
2157 DAG.getConstant(ShAmt, TLI.getShiftAmountTy()));
2158 AddToWorkList(Op.Val);
2159 }
2160 return DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(1, VT));
2161 }
2162 }
Chris Lattner61a4c072007-04-18 03:06:49 +00002163
2164 // fold operands of srl based on knowledge that the low bits are not
2165 // demanded.
2166 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
2167 return SDOperand(N, 0);
2168
Nate Begeman83e75ec2005-09-06 04:43:02 +00002169 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002170}
2171
Nate Begeman83e75ec2005-09-06 04:43:02 +00002172SDOperand DAGCombiner::visitCTLZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002173 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00002174 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002175
2176 // fold (ctlz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002177 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002178 return DAG.getNode(ISD::CTLZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002179 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002180}
2181
Nate Begeman83e75ec2005-09-06 04:43:02 +00002182SDOperand DAGCombiner::visitCTTZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002183 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00002184 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002185
2186 // fold (cttz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002187 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002188 return DAG.getNode(ISD::CTTZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002189 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002190}
2191
Nate Begeman83e75ec2005-09-06 04:43:02 +00002192SDOperand DAGCombiner::visitCTPOP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002193 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00002194 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002195
2196 // fold (ctpop c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002197 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002198 return DAG.getNode(ISD::CTPOP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002199 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002200}
2201
Nate Begeman452d7be2005-09-16 00:54:12 +00002202SDOperand DAGCombiner::visitSELECT(SDNode *N) {
2203 SDOperand N0 = N->getOperand(0);
2204 SDOperand N1 = N->getOperand(1);
2205 SDOperand N2 = N->getOperand(2);
2206 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2207 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2208 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
2209 MVT::ValueType VT = N->getValueType(0);
Nate Begeman44728a72005-09-19 22:34:01 +00002210
Nate Begeman452d7be2005-09-16 00:54:12 +00002211 // fold select C, X, X -> X
2212 if (N1 == N2)
2213 return N1;
2214 // fold select true, X, Y -> X
2215 if (N0C && !N0C->isNullValue())
2216 return N1;
2217 // fold select false, X, Y -> Y
2218 if (N0C && N0C->isNullValue())
2219 return N2;
2220 // fold select C, 1, X -> C | X
Nate Begeman44728a72005-09-19 22:34:01 +00002221 if (MVT::i1 == VT && N1C && N1C->getValue() == 1)
Nate Begeman452d7be2005-09-16 00:54:12 +00002222 return DAG.getNode(ISD::OR, VT, N0, N2);
2223 // fold select C, 0, X -> ~C & X
2224 // FIXME: this should check for C type == X type, not i1?
2225 if (MVT::i1 == VT && N1C && N1C->isNullValue()) {
2226 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00002227 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00002228 return DAG.getNode(ISD::AND, VT, XORNode, N2);
2229 }
2230 // fold select C, X, 1 -> ~C | X
Nate Begeman44728a72005-09-19 22:34:01 +00002231 if (MVT::i1 == VT && N2C && N2C->getValue() == 1) {
Nate Begeman452d7be2005-09-16 00:54:12 +00002232 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00002233 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00002234 return DAG.getNode(ISD::OR, VT, XORNode, N1);
2235 }
2236 // fold select C, X, 0 -> C & X
2237 // FIXME: this should check for C type == X type, not i1?
2238 if (MVT::i1 == VT && N2C && N2C->isNullValue())
2239 return DAG.getNode(ISD::AND, VT, N0, N1);
2240 // fold X ? X : Y --> X ? 1 : Y --> X | Y
2241 if (MVT::i1 == VT && N0 == N1)
2242 return DAG.getNode(ISD::OR, VT, N0, N2);
2243 // fold X ? Y : X --> X ? Y : 0 --> X & Y
2244 if (MVT::i1 == VT && N0 == N2)
2245 return DAG.getNode(ISD::AND, VT, N0, N1);
Chris Lattner729c6d12006-05-27 00:43:02 +00002246
Chris Lattner40c62d52005-10-18 06:04:22 +00002247 // If we can fold this based on the true/false value, do so.
2248 if (SimplifySelectOps(N, N1, N2))
Chris Lattner729c6d12006-05-27 00:43:02 +00002249 return SDOperand(N, 0); // Don't revisit N.
2250
Nate Begeman44728a72005-09-19 22:34:01 +00002251 // fold selects based on a setcc into other things, such as min/max/abs
2252 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman750ac1b2006-02-01 07:19:44 +00002253 // FIXME:
2254 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
2255 // having to say they don't support SELECT_CC on every type the DAG knows
2256 // about, since there is no way to mark an opcode illegal at all value types
2257 if (TLI.isOperationLegal(ISD::SELECT_CC, MVT::Other))
2258 return DAG.getNode(ISD::SELECT_CC, VT, N0.getOperand(0), N0.getOperand(1),
2259 N1, N2, N0.getOperand(2));
2260 else
2261 return SimplifySelect(N0, N1, N2);
Nate Begeman452d7be2005-09-16 00:54:12 +00002262 return SDOperand();
2263}
2264
2265SDOperand DAGCombiner::visitSELECT_CC(SDNode *N) {
Nate Begeman44728a72005-09-19 22:34:01 +00002266 SDOperand N0 = N->getOperand(0);
2267 SDOperand N1 = N->getOperand(1);
2268 SDOperand N2 = N->getOperand(2);
2269 SDOperand N3 = N->getOperand(3);
2270 SDOperand N4 = N->getOperand(4);
Nate Begeman44728a72005-09-19 22:34:01 +00002271 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
2272
Nate Begeman44728a72005-09-19 22:34:01 +00002273 // fold select_cc lhs, rhs, x, x, cc -> x
2274 if (N2 == N3)
2275 return N2;
Chris Lattner40c62d52005-10-18 06:04:22 +00002276
Chris Lattner5f42a242006-09-20 06:19:26 +00002277 // Determine if the condition we're dealing with is constant
2278 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
Chris Lattner30f73e72006-10-14 03:52:46 +00002279 if (SCC.Val) AddToWorkList(SCC.Val);
Chris Lattner5f42a242006-09-20 06:19:26 +00002280
2281 if (ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val)) {
2282 if (SCCC->getValue())
2283 return N2; // cond always true -> true val
2284 else
2285 return N3; // cond always false -> false val
2286 }
2287
2288 // Fold to a simpler select_cc
2289 if (SCC.Val && SCC.getOpcode() == ISD::SETCC)
2290 return DAG.getNode(ISD::SELECT_CC, N2.getValueType(),
2291 SCC.getOperand(0), SCC.getOperand(1), N2, N3,
2292 SCC.getOperand(2));
2293
Chris Lattner40c62d52005-10-18 06:04:22 +00002294 // If we can fold this based on the true/false value, do so.
2295 if (SimplifySelectOps(N, N2, N3))
Chris Lattner729c6d12006-05-27 00:43:02 +00002296 return SDOperand(N, 0); // Don't revisit N.
Chris Lattner40c62d52005-10-18 06:04:22 +00002297
Nate Begeman44728a72005-09-19 22:34:01 +00002298 // fold select_cc into other things, such as min/max/abs
2299 return SimplifySelectCC(N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00002300}
2301
2302SDOperand DAGCombiner::visitSETCC(SDNode *N) {
2303 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
2304 cast<CondCodeSDNode>(N->getOperand(2))->get());
2305}
2306
Nate Begeman83e75ec2005-09-06 04:43:02 +00002307SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002308 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002309 MVT::ValueType VT = N->getValueType(0);
2310
Nate Begeman1d4d4142005-09-01 00:19:25 +00002311 // fold (sext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00002312 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002313 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0);
Chris Lattner310b5782006-05-06 23:06:26 +00002314
Nate Begeman1d4d4142005-09-01 00:19:25 +00002315 // fold (sext (sext x)) -> (sext x)
Chris Lattner310b5782006-05-06 23:06:26 +00002316 // fold (sext (aext x)) -> (sext x)
2317 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002318 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
Chris Lattner310b5782006-05-06 23:06:26 +00002319
Evan Chengc88138f2007-03-22 01:54:19 +00002320 // fold (sext (truncate (load x))) -> (sext (smaller load x))
2321 // fold (sext (truncate (srl (load x), c))) -> (sext (smaller load (x+c/n)))
Chris Lattner22558872007-02-26 03:13:59 +00002322 if (N0.getOpcode() == ISD::TRUNCATE) {
Evan Chengc88138f2007-03-22 01:54:19 +00002323 SDOperand NarrowLoad = ReduceLoadWidth(N0.Val);
Evan Cheng0b063de2007-03-23 02:16:52 +00002324 if (NarrowLoad.Val) {
2325 if (NarrowLoad.Val != N0.Val)
2326 CombineTo(N0.Val, NarrowLoad);
2327 return DAG.getNode(ISD::SIGN_EXTEND, VT, NarrowLoad);
2328 }
Evan Chengc88138f2007-03-22 01:54:19 +00002329 }
2330
2331 // See if the value being truncated is already sign extended. If so, just
2332 // eliminate the trunc/sext pair.
2333 if (N0.getOpcode() == ISD::TRUNCATE) {
Chris Lattner6007b842006-09-21 06:00:20 +00002334 SDOperand Op = N0.getOperand(0);
Chris Lattner22558872007-02-26 03:13:59 +00002335 unsigned OpBits = MVT::getSizeInBits(Op.getValueType());
2336 unsigned MidBits = MVT::getSizeInBits(N0.getValueType());
2337 unsigned DestBits = MVT::getSizeInBits(VT);
Dan Gohmanea859be2007-06-22 14:59:07 +00002338 unsigned NumSignBits = DAG.ComputeNumSignBits(Op);
Chris Lattner22558872007-02-26 03:13:59 +00002339
2340 if (OpBits == DestBits) {
2341 // Op is i32, Mid is i8, and Dest is i32. If Op has more than 24 sign
2342 // bits, it is already ready.
2343 if (NumSignBits > DestBits-MidBits)
2344 return Op;
2345 } else if (OpBits < DestBits) {
2346 // Op is i32, Mid is i8, and Dest is i64. If Op has more than 24 sign
2347 // bits, just sext from i32.
2348 if (NumSignBits > OpBits-MidBits)
2349 return DAG.getNode(ISD::SIGN_EXTEND, VT, Op);
2350 } else {
2351 // Op is i64, Mid is i8, and Dest is i32. If Op has more than 56 sign
2352 // bits, just truncate to i32.
2353 if (NumSignBits > OpBits-MidBits)
2354 return DAG.getNode(ISD::TRUNCATE, VT, Op);
Chris Lattner6007b842006-09-21 06:00:20 +00002355 }
Chris Lattner22558872007-02-26 03:13:59 +00002356
2357 // fold (sext (truncate x)) -> (sextinreg x).
2358 if (!AfterLegalize || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG,
2359 N0.getValueType())) {
2360 if (Op.getValueType() < VT)
2361 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
2362 else if (Op.getValueType() > VT)
2363 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
2364 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, Op,
2365 DAG.getValueType(N0.getValueType()));
2366 }
Chris Lattner6007b842006-09-21 06:00:20 +00002367 }
Chris Lattner310b5782006-05-06 23:06:26 +00002368
Evan Cheng110dec22005-12-14 02:19:23 +00002369 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00002370 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00002371 (!AfterLegalize||TLI.isLoadXLegal(ISD::SEXTLOAD, N0.getValueType()))){
Evan Cheng466685d2006-10-09 20:57:25 +00002372 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2373 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2374 LN0->getBasePtr(), LN0->getSrcValue(),
2375 LN0->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002376 N0.getValueType(),
2377 LN0->isVolatile());
Chris Lattnerd4771842005-12-14 19:25:30 +00002378 CombineTo(N, ExtLoad);
Chris Lattnerf9884052005-10-13 21:52:31 +00002379 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2380 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002381 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begeman3df4d522005-10-12 20:40:40 +00002382 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002383
2384 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
2385 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00002386 if ((ISD::isSEXTLoad(N0.Val) || ISD::isEXTLoad(N0.Val)) &&
2387 ISD::isUNINDEXEDLoad(N0.Val) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002388 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00002389 MVT::ValueType EVT = LN0->getLoadedVT();
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00002390 if (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT)) {
2391 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2392 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002393 LN0->getSrcValueOffset(), EVT,
2394 LN0->isVolatile(),
2395 LN0->getAlignment());
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00002396 CombineTo(N, ExtLoad);
2397 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2398 ExtLoad.getValue(1));
2399 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2400 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002401 }
2402
Chris Lattner20a35c32007-04-11 05:32:27 +00002403 // sext(setcc x,y,cc) -> select_cc x, y, -1, 0, cc
2404 if (N0.getOpcode() == ISD::SETCC) {
2405 SDOperand SCC =
Chris Lattner1eba01e2007-04-11 06:50:51 +00002406 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
2407 DAG.getConstant(~0ULL, VT), DAG.getConstant(0, VT),
2408 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
2409 if (SCC.Val) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00002410 }
2411
Nate Begeman83e75ec2005-09-06 04:43:02 +00002412 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002413}
2414
Nate Begeman83e75ec2005-09-06 04:43:02 +00002415SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002416 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002417 MVT::ValueType VT = N->getValueType(0);
2418
Nate Begeman1d4d4142005-09-01 00:19:25 +00002419 // fold (zext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00002420 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002421 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002422 // fold (zext (zext x)) -> (zext x)
Chris Lattner310b5782006-05-06 23:06:26 +00002423 // fold (zext (aext x)) -> (zext x)
2424 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002425 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
Chris Lattner6007b842006-09-21 06:00:20 +00002426
Evan Chengc88138f2007-03-22 01:54:19 +00002427 // fold (zext (truncate (load x))) -> (zext (smaller load x))
2428 // fold (zext (truncate (srl (load x), c))) -> (zext (small load (x+c/n)))
Dale Johannesen2041a0e2007-03-30 21:38:07 +00002429 if (N0.getOpcode() == ISD::TRUNCATE) {
Evan Chengc88138f2007-03-22 01:54:19 +00002430 SDOperand NarrowLoad = ReduceLoadWidth(N0.Val);
Evan Cheng0b063de2007-03-23 02:16:52 +00002431 if (NarrowLoad.Val) {
2432 if (NarrowLoad.Val != N0.Val)
2433 CombineTo(N0.Val, NarrowLoad);
2434 return DAG.getNode(ISD::ZERO_EXTEND, VT, NarrowLoad);
2435 }
Evan Chengc88138f2007-03-22 01:54:19 +00002436 }
2437
Chris Lattner6007b842006-09-21 06:00:20 +00002438 // fold (zext (truncate x)) -> (and x, mask)
2439 if (N0.getOpcode() == ISD::TRUNCATE &&
2440 (!AfterLegalize || TLI.isOperationLegal(ISD::AND, VT))) {
2441 SDOperand Op = N0.getOperand(0);
2442 if (Op.getValueType() < VT) {
2443 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
2444 } else if (Op.getValueType() > VT) {
2445 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
2446 }
2447 return DAG.getZeroExtendInReg(Op, N0.getValueType());
2448 }
2449
Chris Lattner111c2282006-09-21 06:14:31 +00002450 // fold (zext (and (trunc x), cst)) -> (and x, cst).
2451 if (N0.getOpcode() == ISD::AND &&
2452 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
2453 N0.getOperand(1).getOpcode() == ISD::Constant) {
2454 SDOperand X = N0.getOperand(0).getOperand(0);
2455 if (X.getValueType() < VT) {
2456 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
2457 } else if (X.getValueType() > VT) {
2458 X = DAG.getNode(ISD::TRUNCATE, VT, X);
2459 }
2460 uint64_t Mask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
2461 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
2462 }
2463
Evan Cheng110dec22005-12-14 02:19:23 +00002464 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00002465 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00002466 (!AfterLegalize||TLI.isLoadXLegal(ISD::ZEXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00002467 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2468 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
2469 LN0->getBasePtr(), LN0->getSrcValue(),
2470 LN0->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002471 N0.getValueType(),
2472 LN0->isVolatile(),
2473 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00002474 CombineTo(N, ExtLoad);
Evan Cheng110dec22005-12-14 02:19:23 +00002475 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2476 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002477 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng110dec22005-12-14 02:19:23 +00002478 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002479
2480 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
2481 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00002482 if ((ISD::isZEXTLoad(N0.Val) || ISD::isEXTLoad(N0.Val)) &&
2483 ISD::isUNINDEXEDLoad(N0.Val) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002484 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00002485 MVT::ValueType EVT = LN0->getLoadedVT();
Evan Cheng466685d2006-10-09 20:57:25 +00002486 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
2487 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002488 LN0->getSrcValueOffset(), EVT,
2489 LN0->isVolatile(),
2490 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00002491 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002492 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2493 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002494 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002495 }
Chris Lattner20a35c32007-04-11 05:32:27 +00002496
2497 // zext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
2498 if (N0.getOpcode() == ISD::SETCC) {
2499 SDOperand SCC =
2500 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
2501 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattner1eba01e2007-04-11 06:50:51 +00002502 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
2503 if (SCC.Val) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00002504 }
2505
Nate Begeman83e75ec2005-09-06 04:43:02 +00002506 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002507}
2508
Chris Lattner5ffc0662006-05-05 05:58:59 +00002509SDOperand DAGCombiner::visitANY_EXTEND(SDNode *N) {
2510 SDOperand N0 = N->getOperand(0);
Chris Lattner5ffc0662006-05-05 05:58:59 +00002511 MVT::ValueType VT = N->getValueType(0);
2512
2513 // fold (aext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00002514 if (isa<ConstantSDNode>(N0))
Chris Lattner5ffc0662006-05-05 05:58:59 +00002515 return DAG.getNode(ISD::ANY_EXTEND, VT, N0);
2516 // fold (aext (aext x)) -> (aext x)
2517 // fold (aext (zext x)) -> (zext x)
2518 // fold (aext (sext x)) -> (sext x)
2519 if (N0.getOpcode() == ISD::ANY_EXTEND ||
2520 N0.getOpcode() == ISD::ZERO_EXTEND ||
2521 N0.getOpcode() == ISD::SIGN_EXTEND)
2522 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
2523
Evan Chengc88138f2007-03-22 01:54:19 +00002524 // fold (aext (truncate (load x))) -> (aext (smaller load x))
2525 // fold (aext (truncate (srl (load x), c))) -> (aext (small load (x+c/n)))
2526 if (N0.getOpcode() == ISD::TRUNCATE) {
2527 SDOperand NarrowLoad = ReduceLoadWidth(N0.Val);
Evan Cheng0b063de2007-03-23 02:16:52 +00002528 if (NarrowLoad.Val) {
2529 if (NarrowLoad.Val != N0.Val)
2530 CombineTo(N0.Val, NarrowLoad);
2531 return DAG.getNode(ISD::ANY_EXTEND, VT, NarrowLoad);
2532 }
Evan Chengc88138f2007-03-22 01:54:19 +00002533 }
2534
Chris Lattner84750582006-09-20 06:29:17 +00002535 // fold (aext (truncate x))
2536 if (N0.getOpcode() == ISD::TRUNCATE) {
2537 SDOperand TruncOp = N0.getOperand(0);
2538 if (TruncOp.getValueType() == VT)
2539 return TruncOp; // x iff x size == zext size.
2540 if (TruncOp.getValueType() > VT)
2541 return DAG.getNode(ISD::TRUNCATE, VT, TruncOp);
2542 return DAG.getNode(ISD::ANY_EXTEND, VT, TruncOp);
2543 }
Chris Lattner0e4b9222006-09-21 06:40:43 +00002544
2545 // fold (aext (and (trunc x), cst)) -> (and x, cst).
2546 if (N0.getOpcode() == ISD::AND &&
2547 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
2548 N0.getOperand(1).getOpcode() == ISD::Constant) {
2549 SDOperand X = N0.getOperand(0).getOperand(0);
2550 if (X.getValueType() < VT) {
2551 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
2552 } else if (X.getValueType() > VT) {
2553 X = DAG.getNode(ISD::TRUNCATE, VT, X);
2554 }
2555 uint64_t Mask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
2556 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
2557 }
2558
Chris Lattner5ffc0662006-05-05 05:58:59 +00002559 // fold (aext (load x)) -> (aext (truncate (extload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00002560 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00002561 (!AfterLegalize||TLI.isLoadXLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00002562 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2563 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
2564 LN0->getBasePtr(), LN0->getSrcValue(),
2565 LN0->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002566 N0.getValueType(),
2567 LN0->isVolatile(),
2568 LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00002569 CombineTo(N, ExtLoad);
2570 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2571 ExtLoad.getValue(1));
2572 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2573 }
2574
2575 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
2576 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
2577 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00002578 if (N0.getOpcode() == ISD::LOAD &&
2579 !ISD::isNON_EXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val) &&
Evan Cheng466685d2006-10-09 20:57:25 +00002580 N0.hasOneUse()) {
2581 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00002582 MVT::ValueType EVT = LN0->getLoadedVT();
Evan Cheng466685d2006-10-09 20:57:25 +00002583 SDOperand ExtLoad = DAG.getExtLoad(LN0->getExtensionType(), VT,
2584 LN0->getChain(), LN0->getBasePtr(),
2585 LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002586 LN0->getSrcValueOffset(), EVT,
2587 LN0->isVolatile(),
2588 LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00002589 CombineTo(N, ExtLoad);
2590 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2591 ExtLoad.getValue(1));
2592 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2593 }
Chris Lattner20a35c32007-04-11 05:32:27 +00002594
2595 // aext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
2596 if (N0.getOpcode() == ISD::SETCC) {
2597 SDOperand SCC =
Chris Lattner1eba01e2007-04-11 06:50:51 +00002598 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
2599 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattnerc24bbad2007-04-11 16:51:53 +00002600 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Chris Lattner1eba01e2007-04-11 06:50:51 +00002601 if (SCC.Val)
Chris Lattnerc56a81d2007-04-11 06:43:25 +00002602 return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00002603 }
2604
Chris Lattner5ffc0662006-05-05 05:58:59 +00002605 return SDOperand();
2606}
2607
Evan Chengc88138f2007-03-22 01:54:19 +00002608/// ReduceLoadWidth - If the result of a wider load is shifted to right of N
2609/// bits and then truncated to a narrower type and where N is a multiple
2610/// of number of bits of the narrower type, transform it to a narrower load
2611/// from address + N / num of bits of new type. If the result is to be
2612/// extended, also fold the extension to form a extending load.
2613SDOperand DAGCombiner::ReduceLoadWidth(SDNode *N) {
2614 unsigned Opc = N->getOpcode();
2615 ISD::LoadExtType ExtType = ISD::NON_EXTLOAD;
2616 SDOperand N0 = N->getOperand(0);
2617 MVT::ValueType VT = N->getValueType(0);
2618 MVT::ValueType EVT = N->getValueType(0);
2619
Evan Chenge177e302007-03-23 22:13:36 +00002620 // Special case: SIGN_EXTEND_INREG is basically truncating to EVT then
2621 // extended to VT.
Evan Chengc88138f2007-03-22 01:54:19 +00002622 if (Opc == ISD::SIGN_EXTEND_INREG) {
2623 ExtType = ISD::SEXTLOAD;
2624 EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Evan Chenge177e302007-03-23 22:13:36 +00002625 if (AfterLegalize && !TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))
2626 return SDOperand();
Evan Chengc88138f2007-03-22 01:54:19 +00002627 }
2628
2629 unsigned EVTBits = MVT::getSizeInBits(EVT);
2630 unsigned ShAmt = 0;
Evan Chengb37b80c2007-03-23 20:55:21 +00002631 bool CombineSRL = false;
Evan Chengc88138f2007-03-22 01:54:19 +00002632 if (N0.getOpcode() == ISD::SRL && N0.hasOneUse()) {
2633 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2634 ShAmt = N01->getValue();
2635 // Is the shift amount a multiple of size of VT?
2636 if ((ShAmt & (EVTBits-1)) == 0) {
2637 N0 = N0.getOperand(0);
2638 if (MVT::getSizeInBits(N0.getValueType()) <= EVTBits)
2639 return SDOperand();
Evan Chengb37b80c2007-03-23 20:55:21 +00002640 CombineSRL = true;
Evan Chengc88138f2007-03-22 01:54:19 +00002641 }
2642 }
2643 }
2644
2645 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
2646 // Do not allow folding to i1 here. i1 is implicitly stored in memory in
2647 // zero extended form: by shrinking the load, we lose track of the fact
2648 // that it is already zero extended.
2649 // FIXME: This should be reevaluated.
2650 VT != MVT::i1) {
2651 assert(MVT::getSizeInBits(N0.getValueType()) > EVTBits &&
2652 "Cannot truncate to larger type!");
2653 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2654 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
Evan Chengdae54ce2007-03-24 00:02:43 +00002655 // For big endian targets, we need to adjust the offset to the pointer to
2656 // load the correct bytes.
2657 if (!TLI.isLittleEndian())
2658 ShAmt = MVT::getSizeInBits(N0.getValueType()) - ShAmt - EVTBits;
2659 uint64_t PtrOff = ShAmt / 8;
Evan Chengc88138f2007-03-22 01:54:19 +00002660 SDOperand NewPtr = DAG.getNode(ISD::ADD, PtrType, LN0->getBasePtr(),
2661 DAG.getConstant(PtrOff, PtrType));
2662 AddToWorkList(NewPtr.Val);
2663 SDOperand Load = (ExtType == ISD::NON_EXTLOAD)
2664 ? DAG.getLoad(VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002665 LN0->getSrcValue(), LN0->getSrcValueOffset(),
2666 LN0->isVolatile(), LN0->getAlignment())
Evan Chengc88138f2007-03-22 01:54:19 +00002667 : DAG.getExtLoad(ExtType, VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002668 LN0->getSrcValue(), LN0->getSrcValueOffset(), EVT,
2669 LN0->isVolatile(), LN0->getAlignment());
Evan Chengc88138f2007-03-22 01:54:19 +00002670 AddToWorkList(N);
Evan Chengb37b80c2007-03-23 20:55:21 +00002671 if (CombineSRL) {
2672 std::vector<SDNode*> NowDead;
2673 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), Load.getValue(1), NowDead);
2674 CombineTo(N->getOperand(0).Val, Load);
2675 } else
2676 CombineTo(N0.Val, Load, Load.getValue(1));
Evan Cheng15213b72007-03-26 07:12:51 +00002677 if (ShAmt) {
2678 if (Opc == ISD::SIGN_EXTEND_INREG)
2679 return DAG.getNode(Opc, VT, Load, N->getOperand(1));
2680 else
2681 return DAG.getNode(Opc, VT, Load);
2682 }
Evan Chengc88138f2007-03-22 01:54:19 +00002683 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2684 }
2685
2686 return SDOperand();
2687}
2688
Chris Lattner5ffc0662006-05-05 05:58:59 +00002689
Nate Begeman83e75ec2005-09-06 04:43:02 +00002690SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002691 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002692 SDOperand N1 = N->getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002693 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002694 MVT::ValueType EVT = cast<VTSDNode>(N1)->getVT();
Nate Begeman07ed4172005-10-10 21:26:48 +00002695 unsigned EVTBits = MVT::getSizeInBits(EVT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002696
Nate Begeman1d4d4142005-09-01 00:19:25 +00002697 // fold (sext_in_reg c1) -> c1
Chris Lattnereaeda562006-05-08 20:59:41 +00002698 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
Chris Lattner310b5782006-05-06 23:06:26 +00002699 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0, N1);
Chris Lattneree4ea922006-05-06 09:30:03 +00002700
Chris Lattner541a24f2006-05-06 22:43:44 +00002701 // If the input is already sign extended, just drop the extension.
Dan Gohmanea859be2007-06-22 14:59:07 +00002702 if (DAG.ComputeNumSignBits(N0) >= MVT::getSizeInBits(VT)-EVTBits+1)
Chris Lattneree4ea922006-05-06 09:30:03 +00002703 return N0;
2704
Nate Begeman646d7e22005-09-02 21:18:40 +00002705 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
2706 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
2707 EVT < cast<VTSDNode>(N0.getOperand(1))->getVT()) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00002708 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002709 }
Chris Lattner4b37e872006-05-08 21:18:59 +00002710
Chris Lattner95a5e052007-04-17 19:03:21 +00002711 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is known zero.
Dan Gohmanea859be2007-06-22 14:59:07 +00002712 if (DAG.MaskedValueIsZero(N0, 1ULL << (EVTBits-1)))
Nate Begemande996292006-02-03 22:24:05 +00002713 return DAG.getZeroExtendInReg(N0, EVT);
Chris Lattner4b37e872006-05-08 21:18:59 +00002714
Chris Lattner95a5e052007-04-17 19:03:21 +00002715 // fold operands of sext_in_reg based on knowledge that the top bits are not
2716 // demanded.
2717 if (SimplifyDemandedBits(SDOperand(N, 0)))
2718 return SDOperand(N, 0);
2719
Evan Chengc88138f2007-03-22 01:54:19 +00002720 // fold (sext_in_reg (load x)) -> (smaller sextload x)
2721 // fold (sext_in_reg (srl (load x), c)) -> (smaller sextload (x+c/evtbits))
2722 SDOperand NarrowLoad = ReduceLoadWidth(N);
2723 if (NarrowLoad.Val)
2724 return NarrowLoad;
2725
Chris Lattner4b37e872006-05-08 21:18:59 +00002726 // fold (sext_in_reg (srl X, 24), i8) -> sra X, 24
2727 // fold (sext_in_reg (srl X, 23), i8) -> sra X, 23 iff possible.
2728 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
2729 if (N0.getOpcode() == ISD::SRL) {
2730 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
2731 if (ShAmt->getValue()+EVTBits <= MVT::getSizeInBits(VT)) {
2732 // We can turn this into an SRA iff the input to the SRL is already sign
2733 // extended enough.
Dan Gohmanea859be2007-06-22 14:59:07 +00002734 unsigned InSignBits = DAG.ComputeNumSignBits(N0.getOperand(0));
Chris Lattner4b37e872006-05-08 21:18:59 +00002735 if (MVT::getSizeInBits(VT)-(ShAmt->getValue()+EVTBits) < InSignBits)
2736 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0), N0.getOperand(1));
2737 }
2738 }
Evan Chengc88138f2007-03-22 01:54:19 +00002739
Nate Begemanded49632005-10-13 03:11:28 +00002740 // fold (sext_inreg (extload x)) -> (sextload x)
Evan Chengc5484282006-10-04 00:56:09 +00002741 if (ISD::isEXTLoad(N0.Val) &&
Evan Cheng83060c52007-03-07 08:07:03 +00002742 ISD::isUNINDEXEDLoad(N0.Val) &&
Evan Cheng2e49f092006-10-11 07:10:22 +00002743 EVT == cast<LoadSDNode>(N0)->getLoadedVT() &&
Evan Chengc5484282006-10-04 00:56:09 +00002744 (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00002745 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2746 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2747 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002748 LN0->getSrcValueOffset(), EVT,
2749 LN0->isVolatile(),
2750 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00002751 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00002752 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002753 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00002754 }
2755 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Evan Cheng83060c52007-03-07 08:07:03 +00002756 if (ISD::isZEXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val) &&
2757 N0.hasOneUse() &&
Evan Cheng2e49f092006-10-11 07:10:22 +00002758 EVT == cast<LoadSDNode>(N0)->getLoadedVT() &&
Evan Chengc5484282006-10-04 00:56:09 +00002759 (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00002760 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2761 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2762 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002763 LN0->getSrcValueOffset(), EVT,
2764 LN0->isVolatile(),
2765 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00002766 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00002767 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002768 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00002769 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00002770 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002771}
2772
Nate Begeman83e75ec2005-09-06 04:43:02 +00002773SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002774 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002775 MVT::ValueType VT = N->getValueType(0);
2776
2777 // noop truncate
2778 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002779 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002780 // fold (truncate c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00002781 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002782 return DAG.getNode(ISD::TRUNCATE, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002783 // fold (truncate (truncate x)) -> (truncate x)
2784 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002785 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002786 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
Chris Lattnerb72773b2006-05-05 22:56:26 +00002787 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND||
2788 N0.getOpcode() == ISD::ANY_EXTEND) {
Chris Lattner32ba1aa2006-11-20 18:05:46 +00002789 if (N0.getOperand(0).getValueType() < VT)
Nate Begeman1d4d4142005-09-01 00:19:25 +00002790 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00002791 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Chris Lattner32ba1aa2006-11-20 18:05:46 +00002792 else if (N0.getOperand(0).getValueType() > VT)
Nate Begeman1d4d4142005-09-01 00:19:25 +00002793 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00002794 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002795 else
2796 // if the source and dest are the same type, we can drop both the extend
2797 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00002798 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002799 }
Evan Cheng007b69e2007-03-21 20:14:05 +00002800
Nate Begeman3df4d522005-10-12 20:40:40 +00002801 // fold (truncate (load x)) -> (smaller load x)
Evan Cheng007b69e2007-03-21 20:14:05 +00002802 // fold (truncate (srl (load x), c)) -> (smaller load (x+c/evtbits))
Evan Chengc88138f2007-03-22 01:54:19 +00002803 return ReduceLoadWidth(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002804}
2805
Chris Lattner94683772005-12-23 05:30:37 +00002806SDOperand DAGCombiner::visitBIT_CONVERT(SDNode *N) {
2807 SDOperand N0 = N->getOperand(0);
2808 MVT::ValueType VT = N->getValueType(0);
2809
Dan Gohman7f321562007-06-25 16:23:39 +00002810 // If the input is a BUILD_VECTOR with all constant elements, fold this now.
2811 // Only do this before legalize, since afterward the target may be depending
2812 // on the bitconvert.
2813 // First check to see if this is all constant.
2814 if (!AfterLegalize &&
2815 N0.getOpcode() == ISD::BUILD_VECTOR && N0.Val->hasOneUse() &&
2816 MVT::isVector(VT)) {
2817 bool isSimple = true;
2818 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i)
2819 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
2820 N0.getOperand(i).getOpcode() != ISD::Constant &&
2821 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
2822 isSimple = false;
2823 break;
2824 }
2825
2826 MVT::ValueType DestEltVT = MVT::getVectorElementType(N->getValueType(0));
2827 assert(!MVT::isVector(DestEltVT) &&
2828 "Element type of vector ValueType must not be vector!");
2829 if (isSimple) {
2830 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(N0.Val, DestEltVT);
2831 }
2832 }
2833
Chris Lattner94683772005-12-23 05:30:37 +00002834 // If the input is a constant, let getNode() fold it.
2835 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
2836 SDOperand Res = DAG.getNode(ISD::BIT_CONVERT, VT, N0);
2837 if (Res.Val != N) return Res;
2838 }
2839
Chris Lattnerc8547d82005-12-23 05:37:50 +00002840 if (N0.getOpcode() == ISD::BIT_CONVERT) // conv(conv(x,t1),t2) -> conv(x,t2)
2841 return DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
Chris Lattner6258fb22006-04-02 02:53:43 +00002842
Chris Lattner57104102005-12-23 05:44:41 +00002843 // fold (conv (load x)) -> (load (conv*)x)
Evan Cheng59d5b682007-05-07 21:27:48 +00002844 // If the resultant load doesn't need a higher alignment than the original!
2845 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Dale Johannesen98a6c622007-05-16 22:45:30 +00002846 ISD::isUNINDEXEDLoad(N0.Val) &&
Evan Cheng59d5b682007-05-07 21:27:48 +00002847 TLI.isOperationLegal(ISD::LOAD, VT)) {
Evan Cheng466685d2006-10-09 20:57:25 +00002848 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng59d5b682007-05-07 21:27:48 +00002849 unsigned Align = TLI.getTargetMachine().getTargetData()->
Dan Gohmanfcc4dd92007-05-18 18:41:29 +00002850 getABITypeAlignment(MVT::getTypeForValueType(VT));
Evan Cheng59d5b682007-05-07 21:27:48 +00002851 unsigned OrigAlign = LN0->getAlignment();
2852 if (Align <= OrigAlign) {
2853 SDOperand Load = DAG.getLoad(VT, LN0->getChain(), LN0->getBasePtr(),
2854 LN0->getSrcValue(), LN0->getSrcValueOffset(),
2855 LN0->isVolatile(), LN0->getAlignment());
2856 AddToWorkList(N);
2857 CombineTo(N0.Val, DAG.getNode(ISD::BIT_CONVERT, N0.getValueType(), Load),
2858 Load.getValue(1));
2859 return Load;
2860 }
Chris Lattner57104102005-12-23 05:44:41 +00002861 }
2862
Chris Lattner94683772005-12-23 05:30:37 +00002863 return SDOperand();
2864}
2865
Dan Gohman7f321562007-06-25 16:23:39 +00002866/// ConstantFoldBIT_CONVERTofBUILD_VECTOR - We know that BV is a build_vector
Chris Lattner6258fb22006-04-02 02:53:43 +00002867/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
2868/// destination element value type.
2869SDOperand DAGCombiner::
Dan Gohman7f321562007-06-25 16:23:39 +00002870ConstantFoldBIT_CONVERTofBUILD_VECTOR(SDNode *BV, MVT::ValueType DstEltVT) {
Chris Lattner6258fb22006-04-02 02:53:43 +00002871 MVT::ValueType SrcEltVT = BV->getOperand(0).getValueType();
2872
2873 // If this is already the right type, we're done.
2874 if (SrcEltVT == DstEltVT) return SDOperand(BV, 0);
2875
2876 unsigned SrcBitSize = MVT::getSizeInBits(SrcEltVT);
2877 unsigned DstBitSize = MVT::getSizeInBits(DstEltVT);
2878
2879 // If this is a conversion of N elements of one type to N elements of another
2880 // type, convert each element. This handles FP<->INT cases.
2881 if (SrcBitSize == DstBitSize) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002882 SmallVector<SDOperand, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00002883 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00002884 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, DstEltVT, BV->getOperand(i)));
Chris Lattner3e104b12006-04-08 04:15:24 +00002885 AddToWorkList(Ops.back().Val);
2886 }
Dan Gohman7f321562007-06-25 16:23:39 +00002887 MVT::ValueType VT =
2888 MVT::getVectorType(DstEltVT,
2889 MVT::getVectorNumElements(BV->getValueType(0)));
2890 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00002891 }
2892
2893 // Otherwise, we're growing or shrinking the elements. To avoid having to
2894 // handle annoying details of growing/shrinking FP values, we convert them to
2895 // int first.
2896 if (MVT::isFloatingPoint(SrcEltVT)) {
2897 // Convert the input float vector to a int vector where the elements are the
2898 // same sizes.
2899 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
2900 MVT::ValueType IntVT = SrcEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
Dan Gohman7f321562007-06-25 16:23:39 +00002901 BV = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, IntVT).Val;
Chris Lattner6258fb22006-04-02 02:53:43 +00002902 SrcEltVT = IntVT;
2903 }
2904
2905 // Now we know the input is an integer vector. If the output is a FP type,
2906 // convert to integer first, then to FP of the right size.
2907 if (MVT::isFloatingPoint(DstEltVT)) {
2908 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
2909 MVT::ValueType TmpVT = DstEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
Dan Gohman7f321562007-06-25 16:23:39 +00002910 SDNode *Tmp = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, TmpVT).Val;
Chris Lattner6258fb22006-04-02 02:53:43 +00002911
2912 // Next, convert to FP elements of the same size.
Dan Gohman7f321562007-06-25 16:23:39 +00002913 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(Tmp, DstEltVT);
Chris Lattner6258fb22006-04-02 02:53:43 +00002914 }
2915
2916 // Okay, we know the src/dst types are both integers of differing types.
2917 // Handling growing first.
2918 assert(MVT::isInteger(SrcEltVT) && MVT::isInteger(DstEltVT));
2919 if (SrcBitSize < DstBitSize) {
2920 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
2921
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002922 SmallVector<SDOperand, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00002923 for (unsigned i = 0, e = BV->getNumOperands(); i != e;
Chris Lattner6258fb22006-04-02 02:53:43 +00002924 i += NumInputsPerOutput) {
2925 bool isLE = TLI.isLittleEndian();
2926 uint64_t NewBits = 0;
2927 bool EltIsUndef = true;
2928 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
2929 // Shift the previously computed bits over.
2930 NewBits <<= SrcBitSize;
2931 SDOperand Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
2932 if (Op.getOpcode() == ISD::UNDEF) continue;
2933 EltIsUndef = false;
2934
2935 NewBits |= cast<ConstantSDNode>(Op)->getValue();
2936 }
2937
2938 if (EltIsUndef)
2939 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
2940 else
2941 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
2942 }
2943
Dan Gohman7f321562007-06-25 16:23:39 +00002944 MVT::ValueType VT = MVT::getVectorType(DstEltVT,
2945 Ops.size());
2946 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00002947 }
2948
2949 // Finally, this must be the case where we are shrinking elements: each input
2950 // turns into multiple outputs.
2951 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002952 SmallVector<SDOperand, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00002953 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00002954 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
2955 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
2956 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
2957 continue;
2958 }
2959 uint64_t OpVal = cast<ConstantSDNode>(BV->getOperand(i))->getValue();
2960
2961 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
2962 unsigned ThisVal = OpVal & ((1ULL << DstBitSize)-1);
2963 OpVal >>= DstBitSize;
2964 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
2965 }
2966
2967 // For big endian targets, swap the order of the pieces of each element.
2968 if (!TLI.isLittleEndian())
2969 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
2970 }
Dan Gohman7f321562007-06-25 16:23:39 +00002971 MVT::ValueType VT = MVT::getVectorType(DstEltVT, Ops.size());
2972 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00002973}
2974
2975
2976
Chris Lattner01b3d732005-09-28 22:28:18 +00002977SDOperand DAGCombiner::visitFADD(SDNode *N) {
2978 SDOperand N0 = N->getOperand(0);
2979 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00002980 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2981 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002982 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00002983
Dan Gohman7f321562007-06-25 16:23:39 +00002984 // fold vector ops
2985 SDOperand FoldedVOp = SimplifyVBinOp(N);
2986 if (FoldedVOp.Val) return FoldedVOp;
2987
Nate Begemana0e221d2005-10-18 00:28:13 +00002988 // fold (fadd c1, c2) -> c1+c2
2989 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002990 return DAG.getNode(ISD::FADD, VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00002991 // canonicalize constant to RHS
2992 if (N0CFP && !N1CFP)
2993 return DAG.getNode(ISD::FADD, VT, N1, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00002994 // fold (A + (-B)) -> A-B
Chris Lattner29446522007-05-14 22:04:50 +00002995 if (isNegatibleForFree(N1) == 2)
2996 return DAG.getNode(ISD::FSUB, VT, N0, GetNegatedExpression(N1, DAG));
Chris Lattner01b3d732005-09-28 22:28:18 +00002997 // fold ((-A) + B) -> B-A
Chris Lattner29446522007-05-14 22:04:50 +00002998 if (isNegatibleForFree(N0) == 2)
2999 return DAG.getNode(ISD::FSUB, VT, N1, GetNegatedExpression(N0, DAG));
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003000
3001 // If allowed, fold (fadd (fadd x, c1), c2) -> (fadd x, (fadd c1, c2))
3002 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FADD &&
3003 N0.Val->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
3004 return DAG.getNode(ISD::FADD, VT, N0.getOperand(0),
3005 DAG.getNode(ISD::FADD, VT, N0.getOperand(1), N1));
3006
Dan Gohman7f321562007-06-25 16:23:39 +00003007 // If either operand is undef, the result is undef
3008 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
3009 return DAG.getNode(ISD::UNDEF, VT);
3010
Chris Lattner01b3d732005-09-28 22:28:18 +00003011 return SDOperand();
3012}
3013
3014SDOperand DAGCombiner::visitFSUB(SDNode *N) {
3015 SDOperand N0 = N->getOperand(0);
3016 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003017 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3018 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00003019 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00003020
Dan Gohman7f321562007-06-25 16:23:39 +00003021 // fold vector ops
3022 SDOperand FoldedVOp = SimplifyVBinOp(N);
3023 if (FoldedVOp.Val) return FoldedVOp;
3024
Nate Begemana0e221d2005-10-18 00:28:13 +00003025 // fold (fsub c1, c2) -> c1-c2
3026 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00003027 return DAG.getNode(ISD::FSUB, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00003028 // fold (A-(-B)) -> A+B
Chris Lattner29446522007-05-14 22:04:50 +00003029 if (isNegatibleForFree(N1))
3030 return DAG.getNode(ISD::FADD, VT, N0, GetNegatedExpression(N1, DAG));
3031
Dan Gohman7f321562007-06-25 16:23:39 +00003032 // If either operand is undef, the result is undef
3033 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
3034 return DAG.getNode(ISD::UNDEF, VT);
3035
Chris Lattner01b3d732005-09-28 22:28:18 +00003036 return SDOperand();
3037}
3038
3039SDOperand DAGCombiner::visitFMUL(SDNode *N) {
3040 SDOperand N0 = N->getOperand(0);
3041 SDOperand N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003042 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3043 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00003044 MVT::ValueType VT = N->getValueType(0);
3045
Dan Gohman7f321562007-06-25 16:23:39 +00003046 // fold vector ops
3047 SDOperand FoldedVOp = SimplifyVBinOp(N);
3048 if (FoldedVOp.Val) return FoldedVOp;
3049
Nate Begeman11af4ea2005-10-17 20:40:11 +00003050 // fold (fmul c1, c2) -> c1*c2
3051 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00003052 return DAG.getNode(ISD::FMUL, VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003053 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00003054 if (N0CFP && !N1CFP)
3055 return DAG.getNode(ISD::FMUL, VT, N1, N0);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003056 // fold (fmul X, 2.0) -> (fadd X, X)
3057 if (N1CFP && N1CFP->isExactlyValue(+2.0))
3058 return DAG.getNode(ISD::FADD, VT, N0, N0);
Chris Lattner29446522007-05-14 22:04:50 +00003059 // fold (fmul X, -1.0) -> (fneg X)
3060 if (N1CFP && N1CFP->isExactlyValue(-1.0))
3061 return DAG.getNode(ISD::FNEG, VT, N0);
3062
3063 // -X * -Y -> X*Y
3064 if (char LHSNeg = isNegatibleForFree(N0)) {
3065 if (char RHSNeg = isNegatibleForFree(N1)) {
3066 // Both can be negated for free, check to see if at least one is cheaper
3067 // negated.
3068 if (LHSNeg == 2 || RHSNeg == 2)
3069 return DAG.getNode(ISD::FMUL, VT, GetNegatedExpression(N0, DAG),
3070 GetNegatedExpression(N1, DAG));
3071 }
3072 }
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003073
3074 // If allowed, fold (fmul (fmul x, c1), c2) -> (fmul x, (fmul c1, c2))
3075 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FMUL &&
3076 N0.Val->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
3077 return DAG.getNode(ISD::FMUL, VT, N0.getOperand(0),
3078 DAG.getNode(ISD::FMUL, VT, N0.getOperand(1), N1));
3079
Dan Gohman7f321562007-06-25 16:23:39 +00003080 // If either operand is undef, the result is undef
3081 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
3082 return DAG.getNode(ISD::UNDEF, VT);
3083
Chris Lattner01b3d732005-09-28 22:28:18 +00003084 return SDOperand();
3085}
3086
3087SDOperand DAGCombiner::visitFDIV(SDNode *N) {
3088 SDOperand N0 = N->getOperand(0);
3089 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003090 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3091 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00003092 MVT::ValueType VT = N->getValueType(0);
3093
Dan Gohman7f321562007-06-25 16:23:39 +00003094 // fold vector ops
3095 SDOperand FoldedVOp = SimplifyVBinOp(N);
3096 if (FoldedVOp.Val) return FoldedVOp;
3097
Nate Begemana148d982006-01-18 22:35:16 +00003098 // fold (fdiv c1, c2) -> c1/c2
3099 if (N0CFP && N1CFP)
3100 return DAG.getNode(ISD::FDIV, VT, N0, N1);
Chris Lattner29446522007-05-14 22:04:50 +00003101
3102
3103 // -X / -Y -> X*Y
3104 if (char LHSNeg = isNegatibleForFree(N0)) {
3105 if (char RHSNeg = isNegatibleForFree(N1)) {
3106 // Both can be negated for free, check to see if at least one is cheaper
3107 // negated.
3108 if (LHSNeg == 2 || RHSNeg == 2)
3109 return DAG.getNode(ISD::FDIV, VT, GetNegatedExpression(N0, DAG),
3110 GetNegatedExpression(N1, DAG));
3111 }
3112 }
3113
Dan Gohman7f321562007-06-25 16:23:39 +00003114 // If either operand is undef, the result is undef
3115 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
3116 return DAG.getNode(ISD::UNDEF, VT);
3117
Chris Lattner01b3d732005-09-28 22:28:18 +00003118 return SDOperand();
3119}
3120
3121SDOperand DAGCombiner::visitFREM(SDNode *N) {
3122 SDOperand N0 = N->getOperand(0);
3123 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003124 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3125 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00003126 MVT::ValueType VT = N->getValueType(0);
3127
Nate Begemana148d982006-01-18 22:35:16 +00003128 // fold (frem c1, c2) -> fmod(c1,c2)
3129 if (N0CFP && N1CFP)
3130 return DAG.getNode(ISD::FREM, VT, N0, N1);
Dan Gohman7f321562007-06-25 16:23:39 +00003131
3132 // If either operand is undef, the result is undef
3133 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
3134 return DAG.getNode(ISD::UNDEF, VT);
3135
Chris Lattner01b3d732005-09-28 22:28:18 +00003136 return SDOperand();
3137}
3138
Chris Lattner12d83032006-03-05 05:30:57 +00003139SDOperand DAGCombiner::visitFCOPYSIGN(SDNode *N) {
3140 SDOperand N0 = N->getOperand(0);
3141 SDOperand N1 = N->getOperand(1);
3142 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3143 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
3144 MVT::ValueType VT = N->getValueType(0);
3145
3146 if (N0CFP && N1CFP) // Constant fold
3147 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1);
3148
3149 if (N1CFP) {
3150 // copysign(x, c1) -> fabs(x) iff ispos(c1)
3151 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
3152 union {
3153 double d;
3154 int64_t i;
3155 } u;
3156 u.d = N1CFP->getValue();
3157 if (u.i >= 0)
3158 return DAG.getNode(ISD::FABS, VT, N0);
3159 else
3160 return DAG.getNode(ISD::FNEG, VT, DAG.getNode(ISD::FABS, VT, N0));
3161 }
3162
3163 // copysign(fabs(x), y) -> copysign(x, y)
3164 // copysign(fneg(x), y) -> copysign(x, y)
3165 // copysign(copysign(x,z), y) -> copysign(x, y)
3166 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
3167 N0.getOpcode() == ISD::FCOPYSIGN)
3168 return DAG.getNode(ISD::FCOPYSIGN, VT, N0.getOperand(0), N1);
3169
3170 // copysign(x, abs(y)) -> abs(x)
3171 if (N1.getOpcode() == ISD::FABS)
3172 return DAG.getNode(ISD::FABS, VT, N0);
3173
3174 // copysign(x, copysign(y,z)) -> copysign(x, z)
3175 if (N1.getOpcode() == ISD::FCOPYSIGN)
3176 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(1));
3177
3178 // copysign(x, fp_extend(y)) -> copysign(x, y)
3179 // copysign(x, fp_round(y)) -> copysign(x, y)
3180 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
3181 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(0));
3182
3183 return SDOperand();
3184}
3185
3186
Chris Lattner01b3d732005-09-28 22:28:18 +00003187
Nate Begeman83e75ec2005-09-06 04:43:02 +00003188SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003189 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003190 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00003191 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003192
3193 // fold (sint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00003194 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00003195 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003196 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003197}
3198
Nate Begeman83e75ec2005-09-06 04:43:02 +00003199SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003200 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003201 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00003202 MVT::ValueType VT = N->getValueType(0);
3203
Nate Begeman1d4d4142005-09-01 00:19:25 +00003204 // fold (uint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00003205 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00003206 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003207 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003208}
3209
Nate Begeman83e75ec2005-09-06 04:43:02 +00003210SDOperand DAGCombiner::visitFP_TO_SINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003211 SDOperand N0 = N->getOperand(0);
3212 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3213 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003214
3215 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00003216 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00003217 return DAG.getNode(ISD::FP_TO_SINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003218 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003219}
3220
Nate Begeman83e75ec2005-09-06 04:43:02 +00003221SDOperand DAGCombiner::visitFP_TO_UINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003222 SDOperand N0 = N->getOperand(0);
3223 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3224 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003225
3226 // fold (fp_to_uint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00003227 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00003228 return DAG.getNode(ISD::FP_TO_UINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003229 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003230}
3231
Nate Begeman83e75ec2005-09-06 04:43:02 +00003232SDOperand DAGCombiner::visitFP_ROUND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003233 SDOperand N0 = N->getOperand(0);
3234 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3235 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003236
3237 // fold (fp_round c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00003238 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00003239 return DAG.getNode(ISD::FP_ROUND, VT, N0);
Chris Lattner79dbea52006-03-13 06:26:26 +00003240
3241 // fold (fp_round (fp_extend x)) -> x
3242 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
3243 return N0.getOperand(0);
3244
3245 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
3246 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.Val->hasOneUse()) {
3247 SDOperand Tmp = DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0));
3248 AddToWorkList(Tmp.Val);
3249 return DAG.getNode(ISD::FCOPYSIGN, VT, Tmp, N0.getOperand(1));
3250 }
3251
Nate Begeman83e75ec2005-09-06 04:43:02 +00003252 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003253}
3254
Nate Begeman83e75ec2005-09-06 04:43:02 +00003255SDOperand DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003256 SDOperand N0 = N->getOperand(0);
3257 MVT::ValueType VT = N->getValueType(0);
3258 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00003259 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003260
Nate Begeman1d4d4142005-09-01 00:19:25 +00003261 // fold (fp_round_inreg c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00003262 if (N0CFP) {
3263 SDOperand Round = DAG.getConstantFP(N0CFP->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003264 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003265 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00003266 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003267}
3268
Nate Begeman83e75ec2005-09-06 04:43:02 +00003269SDOperand DAGCombiner::visitFP_EXTEND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003270 SDOperand N0 = N->getOperand(0);
3271 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3272 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003273
3274 // fold (fp_extend c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00003275 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00003276 return DAG.getNode(ISD::FP_EXTEND, VT, N0);
Chris Lattnere564dbb2006-05-05 21:34:35 +00003277
3278 // fold (fpext (load x)) -> (fpext (fpround (extload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00003279 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00003280 (!AfterLegalize||TLI.isLoadXLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003281 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3282 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
3283 LN0->getBasePtr(), LN0->getSrcValue(),
3284 LN0->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003285 N0.getValueType(),
3286 LN0->isVolatile(),
3287 LN0->getAlignment());
Chris Lattnere564dbb2006-05-05 21:34:35 +00003288 CombineTo(N, ExtLoad);
3289 CombineTo(N0.Val, DAG.getNode(ISD::FP_ROUND, N0.getValueType(), ExtLoad),
3290 ExtLoad.getValue(1));
3291 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
3292 }
3293
3294
Nate Begeman83e75ec2005-09-06 04:43:02 +00003295 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003296}
3297
Nate Begeman83e75ec2005-09-06 04:43:02 +00003298SDOperand DAGCombiner::visitFNEG(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003299 SDOperand N0 = N->getOperand(0);
3300 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3301 MVT::ValueType VT = N->getValueType(0);
3302
3303 // fold (fneg c1) -> -c1
Nate Begeman646d7e22005-09-02 21:18:40 +00003304 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00003305 return DAG.getNode(ISD::FNEG, VT, N0);
3306 // fold (fneg (sub x, y)) -> (sub y, x)
Chris Lattner12d83032006-03-05 05:30:57 +00003307 if (N0.getOpcode() == ISD::SUB)
3308 return DAG.getNode(ISD::SUB, VT, N0.getOperand(1), N0.getOperand(0));
Nate Begemana148d982006-01-18 22:35:16 +00003309 // fold (fneg (fneg x)) -> x
Chris Lattner12d83032006-03-05 05:30:57 +00003310 if (N0.getOpcode() == ISD::FNEG)
3311 return N0.getOperand(0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003312 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003313}
3314
Nate Begeman83e75ec2005-09-06 04:43:02 +00003315SDOperand DAGCombiner::visitFABS(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003316 SDOperand N0 = N->getOperand(0);
3317 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3318 MVT::ValueType VT = N->getValueType(0);
3319
Nate Begeman1d4d4142005-09-01 00:19:25 +00003320 // fold (fabs c1) -> fabs(c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00003321 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00003322 return DAG.getNode(ISD::FABS, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003323 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00003324 if (N0.getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00003325 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003326 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00003327 // fold (fabs (fcopysign x, y)) -> (fabs x)
3328 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
3329 return DAG.getNode(ISD::FABS, VT, N0.getOperand(0));
3330
Nate Begeman83e75ec2005-09-06 04:43:02 +00003331 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003332}
3333
Nate Begeman44728a72005-09-19 22:34:01 +00003334SDOperand DAGCombiner::visitBRCOND(SDNode *N) {
3335 SDOperand Chain = N->getOperand(0);
3336 SDOperand N1 = N->getOperand(1);
3337 SDOperand N2 = N->getOperand(2);
3338 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
3339
3340 // never taken branch, fold to chain
3341 if (N1C && N1C->isNullValue())
3342 return Chain;
3343 // unconditional branch
Nate Begemane17daeb2005-10-05 21:43:42 +00003344 if (N1C && N1C->getValue() == 1)
Nate Begeman44728a72005-09-19 22:34:01 +00003345 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
Nate Begeman750ac1b2006-02-01 07:19:44 +00003346 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
3347 // on the target.
3348 if (N1.getOpcode() == ISD::SETCC &&
3349 TLI.isOperationLegal(ISD::BR_CC, MVT::Other)) {
3350 return DAG.getNode(ISD::BR_CC, MVT::Other, Chain, N1.getOperand(2),
3351 N1.getOperand(0), N1.getOperand(1), N2);
3352 }
Nate Begeman44728a72005-09-19 22:34:01 +00003353 return SDOperand();
3354}
3355
Chris Lattner3ea0b472005-10-05 06:47:48 +00003356// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
3357//
Nate Begeman44728a72005-09-19 22:34:01 +00003358SDOperand DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00003359 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
3360 SDOperand CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
3361
3362 // Use SimplifySetCC to simplify SETCC's.
Nate Begemane17daeb2005-10-05 21:43:42 +00003363 SDOperand Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false);
Chris Lattner30f73e72006-10-14 03:52:46 +00003364 if (Simp.Val) AddToWorkList(Simp.Val);
3365
Nate Begemane17daeb2005-10-05 21:43:42 +00003366 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.Val);
3367
3368 // fold br_cc true, dest -> br dest (unconditional branch)
3369 if (SCCC && SCCC->getValue())
3370 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
3371 N->getOperand(4));
3372 // fold br_cc false, dest -> unconditional fall through
3373 if (SCCC && SCCC->isNullValue())
3374 return N->getOperand(0);
Chris Lattner30f73e72006-10-14 03:52:46 +00003375
Nate Begemane17daeb2005-10-05 21:43:42 +00003376 // fold to a simpler setcc
3377 if (Simp.Val && Simp.getOpcode() == ISD::SETCC)
3378 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
3379 Simp.getOperand(2), Simp.getOperand(0),
3380 Simp.getOperand(1), N->getOperand(4));
Nate Begeman44728a72005-09-19 22:34:01 +00003381 return SDOperand();
3382}
3383
Chris Lattner448f2192006-11-11 00:39:41 +00003384
3385/// CombineToPreIndexedLoadStore - Try turning a load / store and a
3386/// pre-indexed load / store when the base pointer is a add or subtract
3387/// and it has other uses besides the load / store. After the
3388/// transformation, the new indexed load / store has effectively folded
3389/// the add / subtract in and all of its other uses are redirected to the
3390/// new load / store.
3391bool DAGCombiner::CombineToPreIndexedLoadStore(SDNode *N) {
3392 if (!AfterLegalize)
3393 return false;
3394
3395 bool isLoad = true;
3396 SDOperand Ptr;
3397 MVT::ValueType VT;
3398 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Evan Chenge90460e2006-12-16 06:25:23 +00003399 if (LD->getAddressingMode() != ISD::UNINDEXED)
3400 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00003401 VT = LD->getLoadedVT();
Evan Cheng83060c52007-03-07 08:07:03 +00003402 if (!TLI.isIndexedLoadLegal(ISD::PRE_INC, VT) &&
Chris Lattner448f2192006-11-11 00:39:41 +00003403 !TLI.isIndexedLoadLegal(ISD::PRE_DEC, VT))
3404 return false;
3405 Ptr = LD->getBasePtr();
3406 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Evan Chenge90460e2006-12-16 06:25:23 +00003407 if (ST->getAddressingMode() != ISD::UNINDEXED)
3408 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00003409 VT = ST->getStoredVT();
3410 if (!TLI.isIndexedStoreLegal(ISD::PRE_INC, VT) &&
3411 !TLI.isIndexedStoreLegal(ISD::PRE_DEC, VT))
3412 return false;
3413 Ptr = ST->getBasePtr();
3414 isLoad = false;
3415 } else
3416 return false;
3417
Chris Lattner9f1794e2006-11-11 00:56:29 +00003418 // If the pointer is not an add/sub, or if it doesn't have multiple uses, bail
3419 // out. There is no reason to make this a preinc/predec.
3420 if ((Ptr.getOpcode() != ISD::ADD && Ptr.getOpcode() != ISD::SUB) ||
3421 Ptr.Val->hasOneUse())
3422 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00003423
Chris Lattner9f1794e2006-11-11 00:56:29 +00003424 // Ask the target to do addressing mode selection.
3425 SDOperand BasePtr;
3426 SDOperand Offset;
3427 ISD::MemIndexedMode AM = ISD::UNINDEXED;
3428 if (!TLI.getPreIndexedAddressParts(N, BasePtr, Offset, AM, DAG))
3429 return false;
Evan Chenga7d4a042007-05-03 23:52:19 +00003430 // Don't create a indexed load / store with zero offset.
3431 if (isa<ConstantSDNode>(Offset) &&
3432 cast<ConstantSDNode>(Offset)->getValue() == 0)
3433 return false;
Chris Lattner9f1794e2006-11-11 00:56:29 +00003434
Chris Lattner41e53fd2006-11-11 01:00:15 +00003435 // Try turning it into a pre-indexed load / store except when:
Evan Chengc843abe2007-05-24 02:35:39 +00003436 // 1) The new base ptr is a frame index.
3437 // 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 +00003438 // predecessor of the value being stored.
Evan Chengc843abe2007-05-24 02:35:39 +00003439 // 3) Another use of old base ptr is a predecessor of N. If ptr is folded
Chris Lattner9f1794e2006-11-11 00:56:29 +00003440 // that would create a cycle.
Evan Chengc843abe2007-05-24 02:35:39 +00003441 // 4) All uses are load / store ops that use it as old base ptr.
Chris Lattner448f2192006-11-11 00:39:41 +00003442
Chris Lattner41e53fd2006-11-11 01:00:15 +00003443 // Check #1. Preinc'ing a frame index would require copying the stack pointer
3444 // (plus the implicit offset) to a register to preinc anyway.
3445 if (isa<FrameIndexSDNode>(BasePtr))
3446 return false;
3447
3448 // Check #2.
Chris Lattner9f1794e2006-11-11 00:56:29 +00003449 if (!isLoad) {
3450 SDOperand Val = cast<StoreSDNode>(N)->getValue();
Evan Chengc843abe2007-05-24 02:35:39 +00003451 if (Val == BasePtr || BasePtr.Val->isPredecessor(Val.Val))
Chris Lattner9f1794e2006-11-11 00:56:29 +00003452 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00003453 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00003454
Evan Chengc843abe2007-05-24 02:35:39 +00003455 // Now check for #3 and #4.
Chris Lattner9f1794e2006-11-11 00:56:29 +00003456 bool RealUse = false;
3457 for (SDNode::use_iterator I = Ptr.Val->use_begin(),
3458 E = Ptr.Val->use_end(); I != E; ++I) {
3459 SDNode *Use = *I;
3460 if (Use == N)
3461 continue;
3462 if (Use->isPredecessor(N))
3463 return false;
3464
3465 if (!((Use->getOpcode() == ISD::LOAD &&
3466 cast<LoadSDNode>(Use)->getBasePtr() == Ptr) ||
3467 (Use->getOpcode() == ISD::STORE) &&
3468 cast<StoreSDNode>(Use)->getBasePtr() == Ptr))
3469 RealUse = true;
3470 }
3471 if (!RealUse)
3472 return false;
3473
3474 SDOperand Result;
3475 if (isLoad)
3476 Result = DAG.getIndexedLoad(SDOperand(N,0), BasePtr, Offset, AM);
3477 else
3478 Result = DAG.getIndexedStore(SDOperand(N,0), BasePtr, Offset, AM);
3479 ++PreIndexedNodes;
3480 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +00003481 DOUT << "\nReplacing.4 "; DEBUG(N->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +00003482 DOUT << "\nWith: "; DEBUG(Result.Val->dump(&DAG));
3483 DOUT << '\n';
Chris Lattner9f1794e2006-11-11 00:56:29 +00003484 std::vector<SDNode*> NowDead;
3485 if (isLoad) {
3486 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(0),
3487 NowDead);
3488 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1), Result.getValue(2),
3489 NowDead);
3490 } else {
3491 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(1),
3492 NowDead);
3493 }
3494
3495 // Nodes can end up on the worklist more than once. Make sure we do
3496 // not process a node that has been replaced.
3497 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
3498 removeFromWorkList(NowDead[i]);
3499 // Finally, since the node is now dead, remove it from the graph.
3500 DAG.DeleteNode(N);
3501
3502 // Replace the uses of Ptr with uses of the updated base value.
3503 DAG.ReplaceAllUsesOfValueWith(Ptr, Result.getValue(isLoad ? 1 : 0),
3504 NowDead);
3505 removeFromWorkList(Ptr.Val);
3506 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
3507 removeFromWorkList(NowDead[i]);
3508 DAG.DeleteNode(Ptr.Val);
3509
3510 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00003511}
3512
3513/// CombineToPostIndexedLoadStore - Try combine a load / store with a
3514/// add / sub of the base pointer node into a post-indexed load / store.
3515/// The transformation folded the add / subtract into the new indexed
3516/// load / store effectively and all of its uses are redirected to the
3517/// new load / store.
3518bool DAGCombiner::CombineToPostIndexedLoadStore(SDNode *N) {
3519 if (!AfterLegalize)
3520 return false;
3521
3522 bool isLoad = true;
3523 SDOperand Ptr;
3524 MVT::ValueType VT;
3525 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Evan Chenge90460e2006-12-16 06:25:23 +00003526 if (LD->getAddressingMode() != ISD::UNINDEXED)
3527 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00003528 VT = LD->getLoadedVT();
3529 if (!TLI.isIndexedLoadLegal(ISD::POST_INC, VT) &&
3530 !TLI.isIndexedLoadLegal(ISD::POST_DEC, VT))
3531 return false;
3532 Ptr = LD->getBasePtr();
3533 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Evan Chenge90460e2006-12-16 06:25:23 +00003534 if (ST->getAddressingMode() != ISD::UNINDEXED)
3535 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00003536 VT = ST->getStoredVT();
3537 if (!TLI.isIndexedStoreLegal(ISD::POST_INC, VT) &&
3538 !TLI.isIndexedStoreLegal(ISD::POST_DEC, VT))
3539 return false;
3540 Ptr = ST->getBasePtr();
3541 isLoad = false;
3542 } else
3543 return false;
3544
Evan Chengcc470212006-11-16 00:08:20 +00003545 if (Ptr.Val->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00003546 return false;
3547
3548 for (SDNode::use_iterator I = Ptr.Val->use_begin(),
3549 E = Ptr.Val->use_end(); I != E; ++I) {
3550 SDNode *Op = *I;
3551 if (Op == N ||
3552 (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB))
3553 continue;
3554
3555 SDOperand BasePtr;
3556 SDOperand Offset;
3557 ISD::MemIndexedMode AM = ISD::UNINDEXED;
3558 if (TLI.getPostIndexedAddressParts(N, Op, BasePtr, Offset, AM, DAG)) {
3559 if (Ptr == Offset)
3560 std::swap(BasePtr, Offset);
3561 if (Ptr != BasePtr)
Chris Lattner448f2192006-11-11 00:39:41 +00003562 continue;
Evan Chenga7d4a042007-05-03 23:52:19 +00003563 // Don't create a indexed load / store with zero offset.
3564 if (isa<ConstantSDNode>(Offset) &&
3565 cast<ConstantSDNode>(Offset)->getValue() == 0)
3566 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00003567
Chris Lattner9f1794e2006-11-11 00:56:29 +00003568 // Try turning it into a post-indexed load / store except when
3569 // 1) All uses are load / store ops that use it as base ptr.
3570 // 2) Op must be independent of N, i.e. Op is neither a predecessor
3571 // nor a successor of N. Otherwise, if Op is folded that would
3572 // create a cycle.
3573
3574 // Check for #1.
3575 bool TryNext = false;
3576 for (SDNode::use_iterator II = BasePtr.Val->use_begin(),
3577 EE = BasePtr.Val->use_end(); II != EE; ++II) {
3578 SDNode *Use = *II;
3579 if (Use == Ptr.Val)
Chris Lattner448f2192006-11-11 00:39:41 +00003580 continue;
3581
Chris Lattner9f1794e2006-11-11 00:56:29 +00003582 // If all the uses are load / store addresses, then don't do the
3583 // transformation.
3584 if (Use->getOpcode() == ISD::ADD || Use->getOpcode() == ISD::SUB){
3585 bool RealUse = false;
3586 for (SDNode::use_iterator III = Use->use_begin(),
3587 EEE = Use->use_end(); III != EEE; ++III) {
3588 SDNode *UseUse = *III;
3589 if (!((UseUse->getOpcode() == ISD::LOAD &&
3590 cast<LoadSDNode>(UseUse)->getBasePtr().Val == Use) ||
3591 (UseUse->getOpcode() == ISD::STORE) &&
3592 cast<StoreSDNode>(UseUse)->getBasePtr().Val == Use))
3593 RealUse = true;
3594 }
Chris Lattner448f2192006-11-11 00:39:41 +00003595
Chris Lattner9f1794e2006-11-11 00:56:29 +00003596 if (!RealUse) {
3597 TryNext = true;
3598 break;
Chris Lattner448f2192006-11-11 00:39:41 +00003599 }
3600 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00003601 }
3602 if (TryNext)
3603 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00003604
Chris Lattner9f1794e2006-11-11 00:56:29 +00003605 // Check for #2
3606 if (!Op->isPredecessor(N) && !N->isPredecessor(Op)) {
3607 SDOperand Result = isLoad
3608 ? DAG.getIndexedLoad(SDOperand(N,0), BasePtr, Offset, AM)
3609 : DAG.getIndexedStore(SDOperand(N,0), BasePtr, Offset, AM);
3610 ++PostIndexedNodes;
3611 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +00003612 DOUT << "\nReplacing.5 "; DEBUG(N->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +00003613 DOUT << "\nWith: "; DEBUG(Result.Val->dump(&DAG));
3614 DOUT << '\n';
Chris Lattner9f1794e2006-11-11 00:56:29 +00003615 std::vector<SDNode*> NowDead;
3616 if (isLoad) {
3617 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(0),
Chris Lattner448f2192006-11-11 00:39:41 +00003618 NowDead);
Chris Lattner9f1794e2006-11-11 00:56:29 +00003619 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1), Result.getValue(2),
3620 NowDead);
3621 } else {
3622 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(1),
3623 NowDead);
Chris Lattner448f2192006-11-11 00:39:41 +00003624 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00003625
3626 // Nodes can end up on the worklist more than once. Make sure we do
3627 // not process a node that has been replaced.
3628 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
3629 removeFromWorkList(NowDead[i]);
3630 // Finally, since the node is now dead, remove it from the graph.
3631 DAG.DeleteNode(N);
3632
3633 // Replace the uses of Use with uses of the updated base value.
3634 DAG.ReplaceAllUsesOfValueWith(SDOperand(Op, 0),
3635 Result.getValue(isLoad ? 1 : 0),
3636 NowDead);
3637 removeFromWorkList(Op);
3638 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
3639 removeFromWorkList(NowDead[i]);
3640 DAG.DeleteNode(Op);
3641
3642 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00003643 }
3644 }
3645 }
3646 return false;
3647}
3648
3649
Chris Lattner01a22022005-10-10 22:04:48 +00003650SDOperand DAGCombiner::visitLOAD(SDNode *N) {
Evan Cheng466685d2006-10-09 20:57:25 +00003651 LoadSDNode *LD = cast<LoadSDNode>(N);
3652 SDOperand Chain = LD->getChain();
3653 SDOperand Ptr = LD->getBasePtr();
Evan Cheng45a7ca92007-05-01 00:38:21 +00003654
3655 // If load is not volatile and there are no uses of the loaded value (and
3656 // the updated indexed value in case of indexed loads), change uses of the
3657 // chain value into uses of the chain input (i.e. delete the dead load).
3658 if (!LD->isVolatile()) {
Evan Cheng498f5592007-05-01 08:53:39 +00003659 if (N->getValueType(1) == MVT::Other) {
3660 // Unindexed loads.
3661 if (N->hasNUsesOfValue(0, 0))
3662 return CombineTo(N, DAG.getNode(ISD::UNDEF, N->getValueType(0)), Chain);
3663 } else {
3664 // Indexed loads.
3665 assert(N->getValueType(2) == MVT::Other && "Malformed indexed loads?");
3666 if (N->hasNUsesOfValue(0, 0) && N->hasNUsesOfValue(0, 1)) {
3667 SDOperand Undef0 = DAG.getNode(ISD::UNDEF, N->getValueType(0));
3668 SDOperand Undef1 = DAG.getNode(ISD::UNDEF, N->getValueType(1));
3669 SDOperand To[] = { Undef0, Undef1, Chain };
3670 return CombineTo(N, To, 3);
Evan Cheng45a7ca92007-05-01 00:38:21 +00003671 }
Evan Cheng45a7ca92007-05-01 00:38:21 +00003672 }
3673 }
Chris Lattner01a22022005-10-10 22:04:48 +00003674
3675 // If this load is directly stored, replace the load value with the stored
3676 // value.
3677 // TODO: Handle store large -> read small portion.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00003678 // TODO: Handle TRUNCSTORE/LOADEXT
3679 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00003680 if (ISD::isNON_TRUNCStore(Chain.Val)) {
3681 StoreSDNode *PrevST = cast<StoreSDNode>(Chain);
3682 if (PrevST->getBasePtr() == Ptr &&
3683 PrevST->getValue().getValueType() == N->getValueType(0))
Jim Laskeyc2b19f32006-10-11 17:47:52 +00003684 return CombineTo(N, Chain.getOperand(1), Chain);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003685 }
Jim Laskeyc2b19f32006-10-11 17:47:52 +00003686 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00003687
Jim Laskey7ca56af2006-10-11 13:47:09 +00003688 if (CombinerAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00003689 // Walk up chain skipping non-aliasing memory nodes.
3690 SDOperand BetterChain = FindBetterChain(N, Chain);
3691
Jim Laskey6ff23e52006-10-04 16:53:27 +00003692 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00003693 if (Chain != BetterChain) {
Jim Laskeyc2b19f32006-10-11 17:47:52 +00003694 SDOperand ReplLoad;
3695
Jim Laskey279f0532006-09-25 16:29:54 +00003696 // Replace the chain to void dependency.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00003697 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
3698 ReplLoad = DAG.getLoad(N->getValueType(0), BetterChain, Ptr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00003699 LD->getSrcValue(), LD->getSrcValueOffset(),
3700 LD->isVolatile(), LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00003701 } else {
3702 ReplLoad = DAG.getExtLoad(LD->getExtensionType(),
3703 LD->getValueType(0),
3704 BetterChain, Ptr, LD->getSrcValue(),
3705 LD->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003706 LD->getLoadedVT(),
3707 LD->isVolatile(),
3708 LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00003709 }
Jim Laskey279f0532006-09-25 16:29:54 +00003710
Jim Laskey6ff23e52006-10-04 16:53:27 +00003711 // Create token factor to keep old chain connected.
Jim Laskey288af5e2006-09-25 19:32:58 +00003712 SDOperand Token = DAG.getNode(ISD::TokenFactor, MVT::Other,
3713 Chain, ReplLoad.getValue(1));
Jim Laskey6ff23e52006-10-04 16:53:27 +00003714
Jim Laskey274062c2006-10-13 23:32:28 +00003715 // Replace uses with load result and token factor. Don't add users
3716 // to work list.
3717 return CombineTo(N, ReplLoad.getValue(0), Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00003718 }
3719 }
3720
Evan Cheng7fc033a2006-11-03 03:06:21 +00003721 // Try transforming N to an indexed load.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00003722 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Evan Cheng7fc033a2006-11-03 03:06:21 +00003723 return SDOperand(N, 0);
3724
Chris Lattner01a22022005-10-10 22:04:48 +00003725 return SDOperand();
3726}
3727
Chris Lattner87514ca2005-10-10 22:31:19 +00003728SDOperand DAGCombiner::visitSTORE(SDNode *N) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00003729 StoreSDNode *ST = cast<StoreSDNode>(N);
3730 SDOperand Chain = ST->getChain();
3731 SDOperand Value = ST->getValue();
3732 SDOperand Ptr = ST->getBasePtr();
Jim Laskey7aed46c2006-10-11 18:55:16 +00003733
Evan Cheng59d5b682007-05-07 21:27:48 +00003734 // If this is a store of a bit convert, store the input value if the
Evan Cheng2c4f9432007-05-09 21:49:47 +00003735 // resultant store does not need a higher alignment than the original.
Dale Johannesen98a6c622007-05-16 22:45:30 +00003736 if (Value.getOpcode() == ISD::BIT_CONVERT && !ST->isTruncatingStore() &&
3737 ST->getAddressingMode() == ISD::UNINDEXED) {
Evan Cheng59d5b682007-05-07 21:27:48 +00003738 unsigned Align = ST->getAlignment();
3739 MVT::ValueType SVT = Value.getOperand(0).getValueType();
3740 unsigned OrigAlign = TLI.getTargetMachine().getTargetData()->
Dan Gohmanfcc4dd92007-05-18 18:41:29 +00003741 getABITypeAlignment(MVT::getTypeForValueType(SVT));
Evan Chengc2cd2b22007-05-07 21:36:06 +00003742 if (Align <= OrigAlign && TLI.isOperationLegal(ISD::STORE, SVT))
Evan Cheng59d5b682007-05-07 21:27:48 +00003743 return DAG.getStore(Chain, Value.getOperand(0), Ptr, ST->getSrcValue(),
3744 ST->getSrcValueOffset());
Jim Laskey279f0532006-09-25 16:29:54 +00003745 }
3746
Nate Begeman2cbba892006-12-11 02:23:46 +00003747 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Nate Begeman2cbba892006-12-11 02:23:46 +00003748 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Value)) {
Evan Cheng25ece662006-12-11 17:25:19 +00003749 if (Value.getOpcode() != ISD::TargetConstantFP) {
3750 SDOperand Tmp;
Chris Lattner62be1a72006-12-12 04:16:14 +00003751 switch (CFP->getValueType(0)) {
3752 default: assert(0 && "Unknown FP type");
3753 case MVT::f32:
3754 if (!AfterLegalize || TLI.isTypeLegal(MVT::i32)) {
3755 Tmp = DAG.getConstant(FloatToBits(CFP->getValue()), MVT::i32);
3756 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
3757 ST->getSrcValueOffset());
3758 }
3759 break;
3760 case MVT::f64:
3761 if (!AfterLegalize || TLI.isTypeLegal(MVT::i64)) {
3762 Tmp = DAG.getConstant(DoubleToBits(CFP->getValue()), MVT::i64);
3763 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
3764 ST->getSrcValueOffset());
3765 } else if (TLI.isTypeLegal(MVT::i32)) {
3766 // Many FP stores are not make apparent until after legalize, e.g. for
3767 // argument passing. Since this is so common, custom legalize the
3768 // 64-bit integer store into two 32-bit stores.
3769 uint64_t Val = DoubleToBits(CFP->getValue());
3770 SDOperand Lo = DAG.getConstant(Val & 0xFFFFFFFF, MVT::i32);
3771 SDOperand Hi = DAG.getConstant(Val >> 32, MVT::i32);
3772 if (!TLI.isLittleEndian()) std::swap(Lo, Hi);
3773
3774 SDOperand St0 = DAG.getStore(Chain, Lo, Ptr, ST->getSrcValue(),
3775 ST->getSrcValueOffset());
3776 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3777 DAG.getConstant(4, Ptr.getValueType()));
3778 SDOperand St1 = DAG.getStore(Chain, Hi, Ptr, ST->getSrcValue(),
3779 ST->getSrcValueOffset()+4);
3780 return DAG.getNode(ISD::TokenFactor, MVT::Other, St0, St1);
3781 }
3782 break;
Evan Cheng25ece662006-12-11 17:25:19 +00003783 }
Nate Begeman2cbba892006-12-11 02:23:46 +00003784 }
Nate Begeman2cbba892006-12-11 02:23:46 +00003785 }
3786
Jim Laskey279f0532006-09-25 16:29:54 +00003787 if (CombinerAA) {
3788 // Walk up chain skipping non-aliasing memory nodes.
3789 SDOperand BetterChain = FindBetterChain(N, Chain);
3790
Jim Laskey6ff23e52006-10-04 16:53:27 +00003791 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00003792 if (Chain != BetterChain) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00003793 // Replace the chain to avoid dependency.
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00003794 SDOperand ReplStore;
3795 if (ST->isTruncatingStore()) {
3796 ReplStore = DAG.getTruncStore(BetterChain, Value, Ptr,
3797 ST->getSrcValue(),ST->getSrcValueOffset(), ST->getStoredVT());
3798 } else {
3799 ReplStore = DAG.getStore(BetterChain, Value, Ptr,
3800 ST->getSrcValue(), ST->getSrcValueOffset());
3801 }
3802
Jim Laskey279f0532006-09-25 16:29:54 +00003803 // Create token to keep both nodes around.
Jim Laskey274062c2006-10-13 23:32:28 +00003804 SDOperand Token =
3805 DAG.getNode(ISD::TokenFactor, MVT::Other, Chain, ReplStore);
3806
3807 // Don't add users to work list.
3808 return CombineTo(N, Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00003809 }
Jim Laskeyd1aed7a2006-09-21 16:28:59 +00003810 }
Chris Lattnerc33baaa2005-12-23 05:48:07 +00003811
Evan Cheng33dbedc2006-11-05 09:31:14 +00003812 // Try transforming N to an indexed store.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00003813 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Evan Cheng33dbedc2006-11-05 09:31:14 +00003814 return SDOperand(N, 0);
3815
Chris Lattner87514ca2005-10-10 22:31:19 +00003816 return SDOperand();
3817}
3818
Chris Lattnerca242442006-03-19 01:27:56 +00003819SDOperand DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
3820 SDOperand InVec = N->getOperand(0);
3821 SDOperand InVal = N->getOperand(1);
3822 SDOperand EltNo = N->getOperand(2);
3823
3824 // If the invec is a BUILD_VECTOR and if EltNo is a constant, build a new
3825 // vector with the inserted element.
3826 if (InVec.getOpcode() == ISD::BUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
3827 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003828 SmallVector<SDOperand, 8> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
Chris Lattnerca242442006-03-19 01:27:56 +00003829 if (Elt < Ops.size())
3830 Ops[Elt] = InVal;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003831 return DAG.getNode(ISD::BUILD_VECTOR, InVec.getValueType(),
3832 &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00003833 }
3834
3835 return SDOperand();
3836}
3837
Dan Gohman7f321562007-06-25 16:23:39 +00003838SDOperand DAGCombiner::visitBUILD_VECTOR(SDNode *N) {
3839 unsigned NumInScalars = N->getNumOperands();
3840 MVT::ValueType VT = N->getValueType(0);
3841 unsigned NumElts = MVT::getVectorNumElements(VT);
3842 MVT::ValueType EltType = MVT::getVectorElementType(VT);
Chris Lattnerca242442006-03-19 01:27:56 +00003843
Dan Gohman7f321562007-06-25 16:23:39 +00003844 // Check to see if this is a BUILD_VECTOR of a bunch of EXTRACT_VECTOR_ELT
3845 // operations. If so, and if the EXTRACT_VECTOR_ELT vector inputs come from
3846 // at most two distinct vectors, turn this into a shuffle node.
Chris Lattnerd7648c82006-03-28 20:28:38 +00003847 SDOperand VecIn1, VecIn2;
3848 for (unsigned i = 0; i != NumInScalars; ++i) {
3849 // Ignore undef inputs.
3850 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
3851
Dan Gohman7f321562007-06-25 16:23:39 +00003852 // If this input is something other than a EXTRACT_VECTOR_ELT with a
Chris Lattnerd7648c82006-03-28 20:28:38 +00003853 // constant index, bail out.
Dan Gohman7f321562007-06-25 16:23:39 +00003854 if (N->getOperand(i).getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
Chris Lattnerd7648c82006-03-28 20:28:38 +00003855 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
3856 VecIn1 = VecIn2 = SDOperand(0, 0);
3857 break;
3858 }
3859
Dan Gohman7f321562007-06-25 16:23:39 +00003860 // If the input vector type disagrees with the result of the build_vector,
Chris Lattnerd7648c82006-03-28 20:28:38 +00003861 // we can't make a shuffle.
3862 SDOperand ExtractedFromVec = N->getOperand(i).getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00003863 if (ExtractedFromVec.getValueType() != VT) {
Chris Lattnerd7648c82006-03-28 20:28:38 +00003864 VecIn1 = VecIn2 = SDOperand(0, 0);
3865 break;
3866 }
3867
3868 // Otherwise, remember this. We allow up to two distinct input vectors.
3869 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
3870 continue;
3871
3872 if (VecIn1.Val == 0) {
3873 VecIn1 = ExtractedFromVec;
3874 } else if (VecIn2.Val == 0) {
3875 VecIn2 = ExtractedFromVec;
3876 } else {
3877 // Too many inputs.
3878 VecIn1 = VecIn2 = SDOperand(0, 0);
3879 break;
3880 }
3881 }
3882
3883 // If everything is good, we can make a shuffle operation.
3884 if (VecIn1.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003885 SmallVector<SDOperand, 8> BuildVecIndices;
Chris Lattnerd7648c82006-03-28 20:28:38 +00003886 for (unsigned i = 0; i != NumInScalars; ++i) {
3887 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
Evan Cheng597a3bd2007-01-20 10:10:26 +00003888 BuildVecIndices.push_back(DAG.getNode(ISD::UNDEF, TLI.getPointerTy()));
Chris Lattnerd7648c82006-03-28 20:28:38 +00003889 continue;
3890 }
3891
3892 SDOperand Extract = N->getOperand(i);
3893
3894 // If extracting from the first vector, just use the index directly.
3895 if (Extract.getOperand(0) == VecIn1) {
3896 BuildVecIndices.push_back(Extract.getOperand(1));
3897 continue;
3898 }
3899
3900 // Otherwise, use InIdx + VecSize
3901 unsigned Idx = cast<ConstantSDNode>(Extract.getOperand(1))->getValue();
Evan Cheng597a3bd2007-01-20 10:10:26 +00003902 BuildVecIndices.push_back(DAG.getConstant(Idx+NumInScalars,
3903 TLI.getPointerTy()));
Chris Lattnerd7648c82006-03-28 20:28:38 +00003904 }
3905
3906 // Add count and size info.
Dan Gohman7f321562007-06-25 16:23:39 +00003907 MVT::ValueType BuildVecVT =
3908 MVT::getVectorType(TLI.getPointerTy(), NumElts);
Chris Lattnerd7648c82006-03-28 20:28:38 +00003909
Dan Gohman7f321562007-06-25 16:23:39 +00003910 // Return the new VECTOR_SHUFFLE node.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003911 SDOperand Ops[5];
3912 Ops[0] = VecIn1;
Chris Lattnercef896e2006-03-28 22:19:47 +00003913 if (VecIn2.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003914 Ops[1] = VecIn2;
Chris Lattnercef896e2006-03-28 22:19:47 +00003915 } else {
Dan Gohman7f321562007-06-25 16:23:39 +00003916 // Use an undef build_vector as input for the second operand.
Chris Lattnercef896e2006-03-28 22:19:47 +00003917 std::vector<SDOperand> UnOps(NumInScalars,
3918 DAG.getNode(ISD::UNDEF,
Dan Gohman7f321562007-06-25 16:23:39 +00003919 EltType));
3920 Ops[1] = DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003921 &UnOps[0], UnOps.size());
3922 AddToWorkList(Ops[1].Val);
Chris Lattnercef896e2006-03-28 22:19:47 +00003923 }
Dan Gohman7f321562007-06-25 16:23:39 +00003924 Ops[2] = DAG.getNode(ISD::BUILD_VECTOR, BuildVecVT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003925 &BuildVecIndices[0], BuildVecIndices.size());
Dan Gohman7f321562007-06-25 16:23:39 +00003926 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Ops, 3);
Chris Lattnerd7648c82006-03-28 20:28:38 +00003927 }
3928
3929 return SDOperand();
3930}
3931
Dan Gohman7f321562007-06-25 16:23:39 +00003932SDOperand DAGCombiner::visitCONCAT_VECTORS(SDNode *N) {
3933 // TODO: Check to see if this is a CONCAT_VECTORS of a bunch of
3934 // EXTRACT_SUBVECTOR operations. If so, and if the EXTRACT_SUBVECTOR vector
3935 // inputs come from at most two distinct vectors, turn this into a shuffle
3936 // node.
3937
3938 // If we only have one input vector, we don't need to do any concatenation.
3939 if (N->getNumOperands() == 1) {
3940 return N->getOperand(0);
3941 }
3942
3943 return SDOperand();
3944}
3945
Chris Lattner66445d32006-03-28 22:11:53 +00003946SDOperand DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
Chris Lattnerf1d0c622006-03-31 22:16:43 +00003947 SDOperand ShufMask = N->getOperand(2);
3948 unsigned NumElts = ShufMask.getNumOperands();
3949
3950 // If the shuffle mask is an identity operation on the LHS, return the LHS.
3951 bool isIdentity = true;
3952 for (unsigned i = 0; i != NumElts; ++i) {
3953 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
3954 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
3955 isIdentity = false;
3956 break;
3957 }
3958 }
3959 if (isIdentity) return N->getOperand(0);
3960
3961 // If the shuffle mask is an identity operation on the RHS, return the RHS.
3962 isIdentity = true;
3963 for (unsigned i = 0; i != NumElts; ++i) {
3964 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
3965 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
3966 isIdentity = false;
3967 break;
3968 }
3969 }
3970 if (isIdentity) return N->getOperand(1);
Evan Chenge7bec0d2006-07-20 22:44:41 +00003971
3972 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
3973 // needed at all.
3974 bool isUnary = true;
Evan Cheng917ec982006-07-21 08:25:53 +00003975 bool isSplat = true;
Evan Chenge7bec0d2006-07-20 22:44:41 +00003976 int VecNum = -1;
Reid Spencer9160a6a2006-07-25 20:44:41 +00003977 unsigned BaseIdx = 0;
Evan Chenge7bec0d2006-07-20 22:44:41 +00003978 for (unsigned i = 0; i != NumElts; ++i)
3979 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
3980 unsigned Idx = cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue();
3981 int V = (Idx < NumElts) ? 0 : 1;
Evan Cheng917ec982006-07-21 08:25:53 +00003982 if (VecNum == -1) {
Evan Chenge7bec0d2006-07-20 22:44:41 +00003983 VecNum = V;
Evan Cheng917ec982006-07-21 08:25:53 +00003984 BaseIdx = Idx;
3985 } else {
3986 if (BaseIdx != Idx)
3987 isSplat = false;
3988 if (VecNum != V) {
3989 isUnary = false;
3990 break;
3991 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00003992 }
3993 }
3994
3995 SDOperand N0 = N->getOperand(0);
3996 SDOperand N1 = N->getOperand(1);
3997 // Normalize unary shuffle so the RHS is undef.
3998 if (isUnary && VecNum == 1)
3999 std::swap(N0, N1);
4000
Evan Cheng917ec982006-07-21 08:25:53 +00004001 // If it is a splat, check if the argument vector is a build_vector with
4002 // all scalar elements the same.
4003 if (isSplat) {
4004 SDNode *V = N0.Val;
Evan Cheng917ec982006-07-21 08:25:53 +00004005
Dan Gohman7f321562007-06-25 16:23:39 +00004006 // If this is a bit convert that changes the element type of the vector but
Evan Cheng59569222006-10-16 22:49:37 +00004007 // not the number of vector elements, look through it. Be careful not to
4008 // look though conversions that change things like v4f32 to v2f64.
Dan Gohman7f321562007-06-25 16:23:39 +00004009 if (V->getOpcode() == ISD::BIT_CONVERT) {
Evan Cheng59569222006-10-16 22:49:37 +00004010 SDOperand ConvInput = V->getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00004011 if (MVT::getVectorNumElements(ConvInput.getValueType()) == NumElts)
Evan Cheng59569222006-10-16 22:49:37 +00004012 V = ConvInput.Val;
4013 }
4014
Dan Gohman7f321562007-06-25 16:23:39 +00004015 if (V->getOpcode() == ISD::BUILD_VECTOR) {
4016 unsigned NumElems = V->getNumOperands();
Evan Cheng917ec982006-07-21 08:25:53 +00004017 if (NumElems > BaseIdx) {
4018 SDOperand Base;
4019 bool AllSame = true;
4020 for (unsigned i = 0; i != NumElems; ++i) {
4021 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
4022 Base = V->getOperand(i);
4023 break;
4024 }
4025 }
4026 // Splat of <u, u, u, u>, return <u, u, u, u>
4027 if (!Base.Val)
4028 return N0;
4029 for (unsigned i = 0; i != NumElems; ++i) {
4030 if (V->getOperand(i).getOpcode() != ISD::UNDEF &&
4031 V->getOperand(i) != Base) {
4032 AllSame = false;
4033 break;
4034 }
4035 }
4036 // Splat of <x, x, x, x>, return <x, x, x, x>
4037 if (AllSame)
4038 return N0;
4039 }
4040 }
4041 }
4042
Evan Chenge7bec0d2006-07-20 22:44:41 +00004043 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
4044 // into an undef.
4045 if (isUnary || N0 == N1) {
Chris Lattner17614ea2006-04-08 05:34:25 +00004046 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
4047 // first operand.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004048 SmallVector<SDOperand, 8> MappedOps;
Chris Lattner17614ea2006-04-08 05:34:25 +00004049 for (unsigned i = 0; i != NumElts; ++i) {
4050 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
4051 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) {
4052 MappedOps.push_back(ShufMask.getOperand(i));
4053 } else {
4054 unsigned NewIdx =
4055 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
4056 MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32));
4057 }
4058 }
Dan Gohman7f321562007-06-25 16:23:39 +00004059 ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMask.getValueType(),
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004060 &MappedOps[0], MappedOps.size());
Chris Lattner17614ea2006-04-08 05:34:25 +00004061 AddToWorkList(ShufMask.Val);
Dan Gohman7f321562007-06-25 16:23:39 +00004062 return DAG.getNode(ISD::VECTOR_SHUFFLE, N->getValueType(0),
4063 N0,
4064 DAG.getNode(ISD::UNDEF, N->getValueType(0)),
4065 ShufMask);
Chris Lattner17614ea2006-04-08 05:34:25 +00004066 }
Dan Gohman7f321562007-06-25 16:23:39 +00004067
Chris Lattnerf1d0c622006-03-31 22:16:43 +00004068 return SDOperand();
4069}
4070
Evan Cheng44f1f092006-04-20 08:56:16 +00004071/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
Dan Gohman7f321562007-06-25 16:23:39 +00004072/// an AND to a vector_shuffle with the destination vector and a zero vector.
4073/// e.g. AND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
Evan Cheng44f1f092006-04-20 08:56:16 +00004074/// vector_shuffle V, Zero, <0, 4, 2, 4>
4075SDOperand DAGCombiner::XformToShuffleWithZero(SDNode *N) {
4076 SDOperand LHS = N->getOperand(0);
4077 SDOperand RHS = N->getOperand(1);
Dan Gohman7f321562007-06-25 16:23:39 +00004078 if (N->getOpcode() == ISD::AND) {
4079 if (RHS.getOpcode() == ISD::BIT_CONVERT)
Evan Cheng44f1f092006-04-20 08:56:16 +00004080 RHS = RHS.getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00004081 if (RHS.getOpcode() == ISD::BUILD_VECTOR) {
Evan Cheng44f1f092006-04-20 08:56:16 +00004082 std::vector<SDOperand> IdxOps;
4083 unsigned NumOps = RHS.getNumOperands();
Dan Gohman7f321562007-06-25 16:23:39 +00004084 unsigned NumElts = NumOps;
4085 MVT::ValueType EVT = MVT::getVectorElementType(RHS.getValueType());
Evan Cheng44f1f092006-04-20 08:56:16 +00004086 for (unsigned i = 0; i != NumElts; ++i) {
4087 SDOperand Elt = RHS.getOperand(i);
4088 if (!isa<ConstantSDNode>(Elt))
4089 return SDOperand();
4090 else if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
4091 IdxOps.push_back(DAG.getConstant(i, EVT));
4092 else if (cast<ConstantSDNode>(Elt)->isNullValue())
4093 IdxOps.push_back(DAG.getConstant(NumElts, EVT));
4094 else
4095 return SDOperand();
4096 }
4097
4098 // Let's see if the target supports this vector_shuffle.
4099 if (!TLI.isVectorClearMaskLegal(IdxOps, EVT, DAG))
4100 return SDOperand();
4101
Dan Gohman7f321562007-06-25 16:23:39 +00004102 // Return the new VECTOR_SHUFFLE node.
4103 MVT::ValueType VT = MVT::getVectorType(EVT, NumElts);
Evan Cheng44f1f092006-04-20 08:56:16 +00004104 std::vector<SDOperand> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00004105 LHS = DAG.getNode(ISD::BIT_CONVERT, VT, LHS);
Evan Cheng44f1f092006-04-20 08:56:16 +00004106 Ops.push_back(LHS);
4107 AddToWorkList(LHS.Val);
4108 std::vector<SDOperand> ZeroOps(NumElts, DAG.getConstant(0, EVT));
Dan Gohman7f321562007-06-25 16:23:39 +00004109 Ops.push_back(DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004110 &ZeroOps[0], ZeroOps.size()));
Dan Gohman7f321562007-06-25 16:23:39 +00004111 Ops.push_back(DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004112 &IdxOps[0], IdxOps.size()));
Dan Gohman7f321562007-06-25 16:23:39 +00004113 SDOperand Result = DAG.getNode(ISD::VECTOR_SHUFFLE, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004114 &Ops[0], Ops.size());
Dan Gohman7f321562007-06-25 16:23:39 +00004115 if (VT != LHS.getValueType()) {
4116 Result = DAG.getNode(ISD::BIT_CONVERT, LHS.getValueType(), Result);
Evan Cheng44f1f092006-04-20 08:56:16 +00004117 }
4118 return Result;
4119 }
4120 }
4121 return SDOperand();
4122}
4123
Dan Gohman7f321562007-06-25 16:23:39 +00004124/// SimplifyVBinOp - Visit a binary vector operation, like ADD.
4125SDOperand DAGCombiner::SimplifyVBinOp(SDNode *N) {
4126 // After legalize, the target may be depending on adds and other
4127 // binary ops to provide legal ways to construct constants or other
4128 // things. Simplifying them may result in a loss of legality.
4129 if (AfterLegalize) return SDOperand();
4130
4131 MVT::ValueType VT = N->getValueType(0);
4132 if (!MVT::isVector(VT)) return SDOperand();
4133
4134 MVT::ValueType EltType = MVT::getVectorElementType(VT);
Chris Lattneredab1b92006-04-02 03:25:57 +00004135 SDOperand LHS = N->getOperand(0);
4136 SDOperand RHS = N->getOperand(1);
Evan Cheng44f1f092006-04-20 08:56:16 +00004137 SDOperand Shuffle = XformToShuffleWithZero(N);
4138 if (Shuffle.Val) return Shuffle;
4139
Dan Gohman7f321562007-06-25 16:23:39 +00004140 // If the LHS and RHS are BUILD_VECTOR nodes, see if we can constant fold
Chris Lattneredab1b92006-04-02 03:25:57 +00004141 // this operation.
Dan Gohman7f321562007-06-25 16:23:39 +00004142 if (LHS.getOpcode() == ISD::BUILD_VECTOR &&
4143 RHS.getOpcode() == ISD::BUILD_VECTOR) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004144 SmallVector<SDOperand, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00004145 for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) {
Chris Lattneredab1b92006-04-02 03:25:57 +00004146 SDOperand LHSOp = LHS.getOperand(i);
4147 SDOperand RHSOp = RHS.getOperand(i);
4148 // If these two elements can't be folded, bail out.
4149 if ((LHSOp.getOpcode() != ISD::UNDEF &&
4150 LHSOp.getOpcode() != ISD::Constant &&
4151 LHSOp.getOpcode() != ISD::ConstantFP) ||
4152 (RHSOp.getOpcode() != ISD::UNDEF &&
4153 RHSOp.getOpcode() != ISD::Constant &&
4154 RHSOp.getOpcode() != ISD::ConstantFP))
4155 break;
Evan Cheng7b336a82006-05-31 06:08:35 +00004156 // Can't fold divide by zero.
Dan Gohman7f321562007-06-25 16:23:39 +00004157 if (N->getOpcode() == ISD::SDIV || N->getOpcode() == ISD::UDIV ||
4158 N->getOpcode() == ISD::FDIV) {
Evan Cheng7b336a82006-05-31 06:08:35 +00004159 if ((RHSOp.getOpcode() == ISD::Constant &&
4160 cast<ConstantSDNode>(RHSOp.Val)->isNullValue()) ||
4161 (RHSOp.getOpcode() == ISD::ConstantFP &&
4162 !cast<ConstantFPSDNode>(RHSOp.Val)->getValue()))
4163 break;
4164 }
Dan Gohman7f321562007-06-25 16:23:39 +00004165 Ops.push_back(DAG.getNode(N->getOpcode(), EltType, LHSOp, RHSOp));
Chris Lattner3e104b12006-04-08 04:15:24 +00004166 AddToWorkList(Ops.back().Val);
Chris Lattneredab1b92006-04-02 03:25:57 +00004167 assert((Ops.back().getOpcode() == ISD::UNDEF ||
4168 Ops.back().getOpcode() == ISD::Constant ||
4169 Ops.back().getOpcode() == ISD::ConstantFP) &&
4170 "Scalar binop didn't fold!");
4171 }
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00004172
Dan Gohman7f321562007-06-25 16:23:39 +00004173 if (Ops.size() == LHS.getNumOperands()) {
4174 MVT::ValueType VT = LHS.getValueType();
4175 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00004176 }
Chris Lattneredab1b92006-04-02 03:25:57 +00004177 }
4178
4179 return SDOperand();
4180}
4181
Nate Begeman44728a72005-09-19 22:34:01 +00004182SDOperand DAGCombiner::SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2){
Nate Begemanf845b452005-10-08 00:29:44 +00004183 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
4184
4185 SDOperand SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
4186 cast<CondCodeSDNode>(N0.getOperand(2))->get());
4187 // If we got a simplified select_cc node back from SimplifySelectCC, then
4188 // break it down into a new SETCC node, and a new SELECT node, and then return
4189 // the SELECT node, since we were called with a SELECT node.
4190 if (SCC.Val) {
4191 // Check to see if we got a select_cc back (to turn into setcc/select).
4192 // Otherwise, just return whatever node we got back, like fabs.
4193 if (SCC.getOpcode() == ISD::SELECT_CC) {
4194 SDOperand SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
4195 SCC.getOperand(0), SCC.getOperand(1),
4196 SCC.getOperand(4));
Chris Lattner5750df92006-03-01 04:03:14 +00004197 AddToWorkList(SETCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004198 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
4199 SCC.getOperand(3), SETCC);
4200 }
4201 return SCC;
4202 }
Nate Begeman44728a72005-09-19 22:34:01 +00004203 return SDOperand();
4204}
4205
Chris Lattner40c62d52005-10-18 06:04:22 +00004206/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
4207/// are the two values being selected between, see if we can simplify the
Chris Lattner729c6d12006-05-27 00:43:02 +00004208/// select. Callers of this should assume that TheSelect is deleted if this
4209/// returns true. As such, they should return the appropriate thing (e.g. the
4210/// node) back to the top-level of the DAG combiner loop to avoid it being
4211/// looked at.
Chris Lattner40c62d52005-10-18 06:04:22 +00004212///
4213bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDOperand LHS,
4214 SDOperand RHS) {
4215
4216 // If this is a select from two identical things, try to pull the operation
4217 // through the select.
4218 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
Chris Lattner40c62d52005-10-18 06:04:22 +00004219 // If this is a load and the token chain is identical, replace the select
4220 // of two loads with a load through a select of the address to load from.
4221 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
4222 // constants have been dropped into the constant pool.
Evan Cheng466685d2006-10-09 20:57:25 +00004223 if (LHS.getOpcode() == ISD::LOAD &&
Chris Lattner40c62d52005-10-18 06:04:22 +00004224 // Token chains must be identical.
Evan Cheng466685d2006-10-09 20:57:25 +00004225 LHS.getOperand(0) == RHS.getOperand(0)) {
4226 LoadSDNode *LLD = cast<LoadSDNode>(LHS);
4227 LoadSDNode *RLD = cast<LoadSDNode>(RHS);
4228
4229 // If this is an EXTLOAD, the VT's must match.
Evan Cheng2e49f092006-10-11 07:10:22 +00004230 if (LLD->getLoadedVT() == RLD->getLoadedVT()) {
Evan Cheng466685d2006-10-09 20:57:25 +00004231 // FIXME: this conflates two src values, discarding one. This is not
4232 // the right thing to do, but nothing uses srcvalues now. When they do,
4233 // turn SrcValue into a list of locations.
4234 SDOperand Addr;
Chris Lattnerc4e664b2007-01-16 05:59:59 +00004235 if (TheSelect->getOpcode() == ISD::SELECT) {
4236 // Check that the condition doesn't reach either load. If so, folding
4237 // this will induce a cycle into the DAG.
4238 if (!LLD->isPredecessor(TheSelect->getOperand(0).Val) &&
4239 !RLD->isPredecessor(TheSelect->getOperand(0).Val)) {
4240 Addr = DAG.getNode(ISD::SELECT, LLD->getBasePtr().getValueType(),
4241 TheSelect->getOperand(0), LLD->getBasePtr(),
4242 RLD->getBasePtr());
4243 }
4244 } else {
4245 // Check that the condition doesn't reach either load. If so, folding
4246 // this will induce a cycle into the DAG.
4247 if (!LLD->isPredecessor(TheSelect->getOperand(0).Val) &&
4248 !RLD->isPredecessor(TheSelect->getOperand(0).Val) &&
4249 !LLD->isPredecessor(TheSelect->getOperand(1).Val) &&
4250 !RLD->isPredecessor(TheSelect->getOperand(1).Val)) {
4251 Addr = DAG.getNode(ISD::SELECT_CC, LLD->getBasePtr().getValueType(),
Evan Cheng466685d2006-10-09 20:57:25 +00004252 TheSelect->getOperand(0),
4253 TheSelect->getOperand(1),
4254 LLD->getBasePtr(), RLD->getBasePtr(),
4255 TheSelect->getOperand(4));
Chris Lattnerc4e664b2007-01-16 05:59:59 +00004256 }
Evan Cheng466685d2006-10-09 20:57:25 +00004257 }
Chris Lattnerc4e664b2007-01-16 05:59:59 +00004258
4259 if (Addr.Val) {
4260 SDOperand Load;
4261 if (LLD->getExtensionType() == ISD::NON_EXTLOAD)
4262 Load = DAG.getLoad(TheSelect->getValueType(0), LLD->getChain(),
4263 Addr,LLD->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00004264 LLD->getSrcValueOffset(),
4265 LLD->isVolatile(),
4266 LLD->getAlignment());
Chris Lattnerc4e664b2007-01-16 05:59:59 +00004267 else {
4268 Load = DAG.getExtLoad(LLD->getExtensionType(),
4269 TheSelect->getValueType(0),
4270 LLD->getChain(), Addr, LLD->getSrcValue(),
4271 LLD->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00004272 LLD->getLoadedVT(),
4273 LLD->isVolatile(),
4274 LLD->getAlignment());
Chris Lattnerc4e664b2007-01-16 05:59:59 +00004275 }
4276 // Users of the select now use the result of the load.
4277 CombineTo(TheSelect, Load);
4278
4279 // Users of the old loads now use the new load's chain. We know the
4280 // old-load value is dead now.
4281 CombineTo(LHS.Val, Load.getValue(0), Load.getValue(1));
4282 CombineTo(RHS.Val, Load.getValue(0), Load.getValue(1));
4283 return true;
4284 }
Evan Chengc5484282006-10-04 00:56:09 +00004285 }
Chris Lattner40c62d52005-10-18 06:04:22 +00004286 }
4287 }
4288
4289 return false;
4290}
4291
Nate Begeman44728a72005-09-19 22:34:01 +00004292SDOperand DAGCombiner::SimplifySelectCC(SDOperand N0, SDOperand N1,
4293 SDOperand N2, SDOperand N3,
Chris Lattner1eba01e2007-04-11 06:50:51 +00004294 ISD::CondCode CC, bool NotExtCompare) {
Nate Begemanf845b452005-10-08 00:29:44 +00004295
4296 MVT::ValueType VT = N2.getValueType();
Nate Begemanf845b452005-10-08 00:29:44 +00004297 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
4298 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
4299 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
4300
4301 // Determine if the condition we're dealing with is constant
4302 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
Chris Lattner30f73e72006-10-14 03:52:46 +00004303 if (SCC.Val) AddToWorkList(SCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004304 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
4305
4306 // fold select_cc true, x, y -> x
4307 if (SCCC && SCCC->getValue())
4308 return N2;
4309 // fold select_cc false, x, y -> y
4310 if (SCCC && SCCC->getValue() == 0)
4311 return N3;
4312
4313 // Check to see if we can simplify the select into an fabs node
4314 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
4315 // Allow either -0.0 or 0.0
4316 if (CFP->getValue() == 0.0) {
4317 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
4318 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
4319 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
4320 N2 == N3.getOperand(0))
4321 return DAG.getNode(ISD::FABS, VT, N0);
4322
4323 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
4324 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
4325 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
4326 N2.getOperand(0) == N3)
4327 return DAG.getNode(ISD::FABS, VT, N3);
4328 }
4329 }
4330
4331 // Check to see if we can perform the "gzip trick", transforming
4332 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
Chris Lattnere3152e52006-09-20 06:41:35 +00004333 if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT &&
Nate Begemanf845b452005-10-08 00:29:44 +00004334 MVT::isInteger(N0.getValueType()) &&
Chris Lattnere3152e52006-09-20 06:41:35 +00004335 MVT::isInteger(N2.getValueType()) &&
4336 (N1C->isNullValue() || // (a < 0) ? b : 0
4337 (N1C->getValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0
Nate Begemanf845b452005-10-08 00:29:44 +00004338 MVT::ValueType XType = N0.getValueType();
4339 MVT::ValueType AType = N2.getValueType();
4340 if (XType >= AType) {
4341 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00004342 // single-bit constant.
Nate Begemanf845b452005-10-08 00:29:44 +00004343 if (N2C && ((N2C->getValue() & (N2C->getValue()-1)) == 0)) {
4344 unsigned ShCtV = Log2_64(N2C->getValue());
4345 ShCtV = MVT::getSizeInBits(XType)-ShCtV-1;
4346 SDOperand ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
4347 SDOperand Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
Chris Lattner5750df92006-03-01 04:03:14 +00004348 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004349 if (XType > AType) {
4350 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00004351 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004352 }
4353 return DAG.getNode(ISD::AND, AType, Shift, N2);
4354 }
4355 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
4356 DAG.getConstant(MVT::getSizeInBits(XType)-1,
4357 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00004358 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004359 if (XType > AType) {
4360 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00004361 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004362 }
4363 return DAG.getNode(ISD::AND, AType, Shift, N2);
4364 }
4365 }
Nate Begeman07ed4172005-10-10 21:26:48 +00004366
4367 // fold select C, 16, 0 -> shl C, 4
4368 if (N2C && N3C && N3C->isNullValue() && isPowerOf2_64(N2C->getValue()) &&
4369 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) {
Chris Lattner1eba01e2007-04-11 06:50:51 +00004370
4371 // If the caller doesn't want us to simplify this into a zext of a compare,
4372 // don't do it.
4373 if (NotExtCompare && N2C->getValue() == 1)
4374 return SDOperand();
4375
Nate Begeman07ed4172005-10-10 21:26:48 +00004376 // Get a SetCC of the condition
4377 // FIXME: Should probably make sure that setcc is legal if we ever have a
4378 // target where it isn't.
Nate Begemanb0d04a72006-02-18 02:40:58 +00004379 SDOperand Temp, SCC;
Nate Begeman07ed4172005-10-10 21:26:48 +00004380 // cast from setcc result type to select result type
Nate Begemanb0d04a72006-02-18 02:40:58 +00004381 if (AfterLegalize) {
4382 SCC = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
Chris Lattner555d8d62006-12-07 22:36:47 +00004383 if (N2.getValueType() < SCC.getValueType())
4384 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
4385 else
4386 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00004387 } else {
4388 SCC = DAG.getSetCC(MVT::i1, N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00004389 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00004390 }
Chris Lattner5750df92006-03-01 04:03:14 +00004391 AddToWorkList(SCC.Val);
4392 AddToWorkList(Temp.Val);
Chris Lattnerc56a81d2007-04-11 06:43:25 +00004393
4394 if (N2C->getValue() == 1)
4395 return Temp;
Nate Begeman07ed4172005-10-10 21:26:48 +00004396 // shl setcc result by log2 n2c
4397 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
4398 DAG.getConstant(Log2_64(N2C->getValue()),
4399 TLI.getShiftAmountTy()));
4400 }
4401
Nate Begemanf845b452005-10-08 00:29:44 +00004402 // Check to see if this is the equivalent of setcc
4403 // FIXME: Turn all of these into setcc if setcc if setcc is legal
4404 // otherwise, go ahead with the folds.
4405 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getValue() == 1ULL)) {
4406 MVT::ValueType XType = N0.getValueType();
4407 if (TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultTy())) {
4408 SDOperand Res = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
4409 if (Res.getValueType() != VT)
4410 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
4411 return Res;
4412 }
4413
4414 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
4415 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
4416 TLI.isOperationLegal(ISD::CTLZ, XType)) {
4417 SDOperand Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
4418 return DAG.getNode(ISD::SRL, XType, Ctlz,
4419 DAG.getConstant(Log2_32(MVT::getSizeInBits(XType)),
4420 TLI.getShiftAmountTy()));
4421 }
4422 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
4423 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
4424 SDOperand NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
4425 N0);
4426 SDOperand NotN0 = DAG.getNode(ISD::XOR, XType, N0,
4427 DAG.getConstant(~0ULL, XType));
4428 return DAG.getNode(ISD::SRL, XType,
4429 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
4430 DAG.getConstant(MVT::getSizeInBits(XType)-1,
4431 TLI.getShiftAmountTy()));
4432 }
4433 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
4434 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
4435 SDOperand Sign = DAG.getNode(ISD::SRL, XType, N0,
4436 DAG.getConstant(MVT::getSizeInBits(XType)-1,
4437 TLI.getShiftAmountTy()));
4438 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
4439 }
4440 }
4441
4442 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
4443 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
4444 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
Chris Lattner1982ef22007-04-11 05:11:38 +00004445 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1) &&
4446 N2.getOperand(0) == N1 && MVT::isInteger(N0.getValueType())) {
4447 MVT::ValueType XType = N0.getValueType();
4448 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
4449 DAG.getConstant(MVT::getSizeInBits(XType)-1,
4450 TLI.getShiftAmountTy()));
4451 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
4452 AddToWorkList(Shift.Val);
4453 AddToWorkList(Add.Val);
4454 return DAG.getNode(ISD::XOR, XType, Add, Shift);
4455 }
4456 // Check to see if this is an integer abs. select_cc setgt X, -1, X, -X ->
4457 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
4458 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT &&
4459 N0 == N2 && N3.getOpcode() == ISD::SUB && N0 == N3.getOperand(1)) {
4460 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N3.getOperand(0))) {
Nate Begemanf845b452005-10-08 00:29:44 +00004461 MVT::ValueType XType = N0.getValueType();
4462 if (SubC->isNullValue() && MVT::isInteger(XType)) {
4463 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
4464 DAG.getConstant(MVT::getSizeInBits(XType)-1,
Chris Lattner1982ef22007-04-11 05:11:38 +00004465 TLI.getShiftAmountTy()));
Nate Begemanf845b452005-10-08 00:29:44 +00004466 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00004467 AddToWorkList(Shift.Val);
4468 AddToWorkList(Add.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004469 return DAG.getNode(ISD::XOR, XType, Add, Shift);
4470 }
4471 }
4472 }
Chris Lattner1982ef22007-04-11 05:11:38 +00004473
Nate Begeman44728a72005-09-19 22:34:01 +00004474 return SDOperand();
4475}
4476
Evan Chengfa1eb272007-02-08 22:13:59 +00004477/// SimplifySetCC - This is a stub for TargetLowering::SimplifySetCC.
Nate Begeman452d7be2005-09-16 00:54:12 +00004478SDOperand DAGCombiner::SimplifySetCC(MVT::ValueType VT, SDOperand N0,
Nate Begemane17daeb2005-10-05 21:43:42 +00004479 SDOperand N1, ISD::CondCode Cond,
4480 bool foldBooleans) {
Evan Chengfa1eb272007-02-08 22:13:59 +00004481 TargetLowering::DAGCombinerInfo
4482 DagCombineInfo(DAG, !AfterLegalize, false, this);
4483 return TLI.SimplifySetCC(VT, N0, N1, Cond, foldBooleans, DagCombineInfo);
Nate Begeman452d7be2005-09-16 00:54:12 +00004484}
4485
Nate Begeman69575232005-10-20 02:15:44 +00004486/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
4487/// return a DAG expression to select that will generate the same value by
4488/// multiplying by a magic number. See:
4489/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
4490SDOperand DAGCombiner::BuildSDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00004491 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00004492 SDOperand S = TLI.BuildSDIV(N, DAG, &Built);
4493
Andrew Lenharth232c9102006-06-12 16:07:18 +00004494 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00004495 ii != ee; ++ii)
4496 AddToWorkList(*ii);
4497 return S;
Nate Begeman69575232005-10-20 02:15:44 +00004498}
4499
4500/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
4501/// return a DAG expression to select that will generate the same value by
4502/// multiplying by a magic number. See:
4503/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
4504SDOperand DAGCombiner::BuildUDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00004505 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00004506 SDOperand S = TLI.BuildUDIV(N, DAG, &Built);
Nate Begeman69575232005-10-20 02:15:44 +00004507
Andrew Lenharth232c9102006-06-12 16:07:18 +00004508 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00004509 ii != ee; ++ii)
4510 AddToWorkList(*ii);
4511 return S;
Nate Begeman69575232005-10-20 02:15:44 +00004512}
4513
Jim Laskey71382342006-10-07 23:37:56 +00004514/// FindBaseOffset - Return true if base is known not to alias with anything
4515/// but itself. Provides base object and offset as results.
4516static bool FindBaseOffset(SDOperand Ptr, SDOperand &Base, int64_t &Offset) {
4517 // Assume it is a primitive operation.
4518 Base = Ptr; Offset = 0;
4519
4520 // If it's an adding a simple constant then integrate the offset.
4521 if (Base.getOpcode() == ISD::ADD) {
4522 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Base.getOperand(1))) {
4523 Base = Base.getOperand(0);
4524 Offset += C->getValue();
4525 }
4526 }
4527
4528 // If it's any of the following then it can't alias with anything but itself.
4529 return isa<FrameIndexSDNode>(Base) ||
4530 isa<ConstantPoolSDNode>(Base) ||
4531 isa<GlobalAddressSDNode>(Base);
4532}
4533
4534/// isAlias - Return true if there is any possibility that the two addresses
4535/// overlap.
Jim Laskey096c22e2006-10-18 12:29:57 +00004536bool DAGCombiner::isAlias(SDOperand Ptr1, int64_t Size1,
4537 const Value *SrcValue1, int SrcValueOffset1,
4538 SDOperand Ptr2, int64_t Size2,
4539 const Value *SrcValue2, int SrcValueOffset2)
4540{
Jim Laskey71382342006-10-07 23:37:56 +00004541 // If they are the same then they must be aliases.
4542 if (Ptr1 == Ptr2) return true;
4543
4544 // Gather base node and offset information.
4545 SDOperand Base1, Base2;
4546 int64_t Offset1, Offset2;
4547 bool KnownBase1 = FindBaseOffset(Ptr1, Base1, Offset1);
4548 bool KnownBase2 = FindBaseOffset(Ptr2, Base2, Offset2);
4549
4550 // If they have a same base address then...
4551 if (Base1 == Base2) {
4552 // Check to see if the addresses overlap.
4553 return!((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
4554 }
4555
Jim Laskey096c22e2006-10-18 12:29:57 +00004556 // If we know both bases then they can't alias.
4557 if (KnownBase1 && KnownBase2) return false;
4558
Jim Laskey07a27092006-10-18 19:08:31 +00004559 if (CombinerGlobalAA) {
4560 // Use alias analysis information.
4561 int Overlap1 = Size1 + SrcValueOffset1 + Offset1;
4562 int Overlap2 = Size2 + SrcValueOffset2 + Offset2;
4563 AliasAnalysis::AliasResult AAResult =
Jim Laskey096c22e2006-10-18 12:29:57 +00004564 AA.alias(SrcValue1, Overlap1, SrcValue2, Overlap2);
Jim Laskey07a27092006-10-18 19:08:31 +00004565 if (AAResult == AliasAnalysis::NoAlias)
4566 return false;
4567 }
Jim Laskey096c22e2006-10-18 12:29:57 +00004568
4569 // Otherwise we have to assume they alias.
4570 return true;
Jim Laskey71382342006-10-07 23:37:56 +00004571}
4572
4573/// FindAliasInfo - Extracts the relevant alias information from the memory
4574/// node. Returns true if the operand was a load.
Jim Laskey7ca56af2006-10-11 13:47:09 +00004575bool DAGCombiner::FindAliasInfo(SDNode *N,
Jim Laskey096c22e2006-10-18 12:29:57 +00004576 SDOperand &Ptr, int64_t &Size,
4577 const Value *&SrcValue, int &SrcValueOffset) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00004578 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
4579 Ptr = LD->getBasePtr();
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004580 Size = MVT::getSizeInBits(LD->getLoadedVT()) >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00004581 SrcValue = LD->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00004582 SrcValueOffset = LD->getSrcValueOffset();
Jim Laskey71382342006-10-07 23:37:56 +00004583 return true;
Jim Laskey7ca56af2006-10-11 13:47:09 +00004584 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00004585 Ptr = ST->getBasePtr();
Evan Cheng8b2794a2006-10-13 21:14:26 +00004586 Size = MVT::getSizeInBits(ST->getStoredVT()) >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00004587 SrcValue = ST->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00004588 SrcValueOffset = ST->getSrcValueOffset();
Jim Laskey7ca56af2006-10-11 13:47:09 +00004589 } else {
Jim Laskey71382342006-10-07 23:37:56 +00004590 assert(0 && "FindAliasInfo expected a memory operand");
Jim Laskey71382342006-10-07 23:37:56 +00004591 }
4592
4593 return false;
4594}
4595
Jim Laskey6ff23e52006-10-04 16:53:27 +00004596/// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
4597/// looking for aliasing nodes and adding them to the Aliases vector.
Jim Laskeybc588b82006-10-05 15:07:25 +00004598void DAGCombiner::GatherAllAliases(SDNode *N, SDOperand OriginalChain,
Jim Laskey6ff23e52006-10-04 16:53:27 +00004599 SmallVector<SDOperand, 8> &Aliases) {
Jim Laskeybc588b82006-10-05 15:07:25 +00004600 SmallVector<SDOperand, 8> Chains; // List of chains to visit.
Jim Laskey6ff23e52006-10-04 16:53:27 +00004601 std::set<SDNode *> Visited; // Visited node set.
4602
Jim Laskey279f0532006-09-25 16:29:54 +00004603 // Get alias information for node.
4604 SDOperand Ptr;
4605 int64_t Size;
Jim Laskey7ca56af2006-10-11 13:47:09 +00004606 const Value *SrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00004607 int SrcValueOffset;
4608 bool IsLoad = FindAliasInfo(N, Ptr, Size, SrcValue, SrcValueOffset);
Jim Laskey279f0532006-09-25 16:29:54 +00004609
Jim Laskey6ff23e52006-10-04 16:53:27 +00004610 // Starting off.
Jim Laskeybc588b82006-10-05 15:07:25 +00004611 Chains.push_back(OriginalChain);
Jim Laskey6ff23e52006-10-04 16:53:27 +00004612
Jim Laskeybc588b82006-10-05 15:07:25 +00004613 // Look at each chain and determine if it is an alias. If so, add it to the
4614 // aliases list. If not, then continue up the chain looking for the next
4615 // candidate.
4616 while (!Chains.empty()) {
4617 SDOperand Chain = Chains.back();
4618 Chains.pop_back();
Jim Laskey6ff23e52006-10-04 16:53:27 +00004619
Jim Laskeybc588b82006-10-05 15:07:25 +00004620 // Don't bother if we've been before.
4621 if (Visited.find(Chain.Val) != Visited.end()) continue;
4622 Visited.insert(Chain.Val);
4623
4624 switch (Chain.getOpcode()) {
4625 case ISD::EntryToken:
4626 // Entry token is ideal chain operand, but handled in FindBetterChain.
4627 break;
Jim Laskey6ff23e52006-10-04 16:53:27 +00004628
Jim Laskeybc588b82006-10-05 15:07:25 +00004629 case ISD::LOAD:
4630 case ISD::STORE: {
4631 // Get alias information for Chain.
4632 SDOperand OpPtr;
4633 int64_t OpSize;
Jim Laskey7ca56af2006-10-11 13:47:09 +00004634 const Value *OpSrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00004635 int OpSrcValueOffset;
4636 bool IsOpLoad = FindAliasInfo(Chain.Val, OpPtr, OpSize,
4637 OpSrcValue, OpSrcValueOffset);
Jim Laskeybc588b82006-10-05 15:07:25 +00004638
4639 // If chain is alias then stop here.
4640 if (!(IsLoad && IsOpLoad) &&
Jim Laskey096c22e2006-10-18 12:29:57 +00004641 isAlias(Ptr, Size, SrcValue, SrcValueOffset,
4642 OpPtr, OpSize, OpSrcValue, OpSrcValueOffset)) {
Jim Laskeybc588b82006-10-05 15:07:25 +00004643 Aliases.push_back(Chain);
4644 } else {
4645 // Look further up the chain.
4646 Chains.push_back(Chain.getOperand(0));
4647 // Clean up old chain.
4648 AddToWorkList(Chain.Val);
Jim Laskey279f0532006-09-25 16:29:54 +00004649 }
Jim Laskeybc588b82006-10-05 15:07:25 +00004650 break;
4651 }
4652
4653 case ISD::TokenFactor:
4654 // We have to check each of the operands of the token factor, so we queue
4655 // then up. Adding the operands to the queue (stack) in reverse order
4656 // maintains the original order and increases the likelihood that getNode
4657 // will find a matching token factor (CSE.)
4658 for (unsigned n = Chain.getNumOperands(); n;)
4659 Chains.push_back(Chain.getOperand(--n));
4660 // Eliminate the token factor if we can.
4661 AddToWorkList(Chain.Val);
4662 break;
4663
4664 default:
4665 // For all other instructions we will just have to take what we can get.
4666 Aliases.push_back(Chain);
4667 break;
Jim Laskey279f0532006-09-25 16:29:54 +00004668 }
4669 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00004670}
4671
4672/// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, looking
4673/// for a better chain (aliasing node.)
4674SDOperand DAGCombiner::FindBetterChain(SDNode *N, SDOperand OldChain) {
4675 SmallVector<SDOperand, 8> Aliases; // Ops for replacing token factor.
Jim Laskey279f0532006-09-25 16:29:54 +00004676
Jim Laskey6ff23e52006-10-04 16:53:27 +00004677 // Accumulate all the aliases to this node.
4678 GatherAllAliases(N, OldChain, Aliases);
4679
4680 if (Aliases.size() == 0) {
4681 // If no operands then chain to entry token.
4682 return DAG.getEntryNode();
4683 } else if (Aliases.size() == 1) {
4684 // If a single operand then chain to it. We don't need to revisit it.
4685 return Aliases[0];
4686 }
4687
4688 // Construct a custom tailored token factor.
4689 SDOperand NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
4690 &Aliases[0], Aliases.size());
4691
4692 // Make sure the old chain gets cleaned up.
4693 if (NewChain != OldChain) AddToWorkList(OldChain.Val);
4694
4695 return NewChain;
Jim Laskey279f0532006-09-25 16:29:54 +00004696}
4697
Nate Begeman1d4d4142005-09-01 00:19:25 +00004698// SelectionDAG::Combine - This is the entry point for the file.
4699//
Jim Laskeyc7c3f112006-10-16 20:52:31 +00004700void SelectionDAG::Combine(bool RunningAfterLegalize, AliasAnalysis &AA) {
Chris Lattner938ab022007-01-16 04:55:25 +00004701 if (!RunningAfterLegalize && ViewDAGCombine1)
4702 viewGraph();
4703 if (RunningAfterLegalize && ViewDAGCombine2)
4704 viewGraph();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004705 /// run - This is the main entry point to this class.
4706 ///
Jim Laskeyc7c3f112006-10-16 20:52:31 +00004707 DAGCombiner(*this, AA).Run(RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004708}