blob: e0f0ece9cd554508d96e8ff377d61e55d9fdbef4 [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);
Dan Gohman23ff1822007-07-02 15:48:56 +0000432
433 // -(0-B) -> B
434 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(Op.getOperand(0)))
435 if (N0CFP->getValue() == 0.0)
436 return Op.getOperand(1);
Chris Lattner29446522007-05-14 22:04:50 +0000437
438 // -(A-B) -> B-A
439 return DAG.getNode(ISD::FSUB, Op.getValueType(), Op.getOperand(1),
440 Op.getOperand(0));
441
442 case ISD::FMUL:
443 case ISD::FDIV:
444 assert(!HonorSignDependentRoundingFPMath());
445
446 // -(X*Y) -> -X * Y
Chris Lattner3adf9512007-05-25 02:19:06 +0000447 if (isNegatibleForFree(Op.getOperand(0), Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000448 return DAG.getNode(Op.getOpcode(), Op.getValueType(),
Chris Lattner3adf9512007-05-25 02:19:06 +0000449 GetNegatedExpression(Op.getOperand(0), DAG, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000450 Op.getOperand(1));
451
452 // -(X*Y) -> X * -Y
453 return DAG.getNode(Op.getOpcode(), Op.getValueType(),
454 Op.getOperand(0),
Chris Lattner3adf9512007-05-25 02:19:06 +0000455 GetNegatedExpression(Op.getOperand(1), DAG, Depth+1));
Chris Lattner29446522007-05-14 22:04:50 +0000456
457 case ISD::FP_EXTEND:
458 case ISD::FP_ROUND:
459 case ISD::FSIN:
460 return DAG.getNode(Op.getOpcode(), Op.getValueType(),
Chris Lattner3adf9512007-05-25 02:19:06 +0000461 GetNegatedExpression(Op.getOperand(0), DAG, Depth+1));
Chris Lattner29446522007-05-14 22:04:50 +0000462 }
463}
Chris Lattner24664722006-03-01 04:53:38 +0000464
465
Nate Begeman4ebd8052005-09-01 23:24:04 +0000466// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
467// that selects between the values 1 and 0, making it equivalent to a setcc.
Nate Begeman646d7e22005-09-02 21:18:40 +0000468// Also, set the incoming LHS, RHS, and CC references to the appropriate
469// nodes based on the type of node we are checking. This simplifies life a
470// bit for the callers.
471static bool isSetCCEquivalent(SDOperand N, SDOperand &LHS, SDOperand &RHS,
472 SDOperand &CC) {
473 if (N.getOpcode() == ISD::SETCC) {
474 LHS = N.getOperand(0);
475 RHS = N.getOperand(1);
476 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000477 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000478 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000479 if (N.getOpcode() == ISD::SELECT_CC &&
480 N.getOperand(2).getOpcode() == ISD::Constant &&
481 N.getOperand(3).getOpcode() == ISD::Constant &&
482 cast<ConstantSDNode>(N.getOperand(2))->getValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000483 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
484 LHS = N.getOperand(0);
485 RHS = N.getOperand(1);
486 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000487 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000488 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000489 return false;
490}
491
Nate Begeman99801192005-09-07 23:25:52 +0000492// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
493// one use. If this is true, it allows the users to invert the operation for
494// free when it is profitable to do so.
495static bool isOneUseSetCC(SDOperand N) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000496 SDOperand N0, N1, N2;
Nate Begeman646d7e22005-09-02 21:18:40 +0000497 if (isSetCCEquivalent(N, N0, N1, N2) && N.Val->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000498 return true;
499 return false;
500}
501
Nate Begemancd4d58c2006-02-03 06:46:56 +0000502SDOperand DAGCombiner::ReassociateOps(unsigned Opc, SDOperand N0, SDOperand N1){
503 MVT::ValueType VT = N0.getValueType();
504 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
505 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
506 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
507 if (isa<ConstantSDNode>(N1)) {
508 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(1), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000509 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000510 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(0));
511 } else if (N0.hasOneUse()) {
512 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(0), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000513 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000514 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(1));
515 }
516 }
517 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
518 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
519 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
520 if (isa<ConstantSDNode>(N0)) {
521 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(1), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000522 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000523 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(0));
524 } else if (N1.hasOneUse()) {
525 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(0), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000526 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000527 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(1));
528 }
529 }
530 return SDOperand();
531}
532
Chris Lattner29446522007-05-14 22:04:50 +0000533//===----------------------------------------------------------------------===//
534// Main DAG Combiner implementation
535//===----------------------------------------------------------------------===//
536
Nate Begeman4ebd8052005-09-01 23:24:04 +0000537void DAGCombiner::Run(bool RunningAfterLegalize) {
538 // set the instance variable, so that the various visit routines may use it.
539 AfterLegalize = RunningAfterLegalize;
540
Nate Begeman646d7e22005-09-02 21:18:40 +0000541 // Add all the dag nodes to the worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000542 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
543 E = DAG.allnodes_end(); I != E; ++I)
544 WorkList.push_back(I);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000545
Chris Lattner95038592005-10-05 06:35:28 +0000546 // Create a dummy node (which is not added to allnodes), that adds a reference
547 // to the root node, preventing it from being deleted, and tracking any
548 // changes of the root.
549 HandleSDNode Dummy(DAG.getRoot());
550
Jim Laskey26f7fa72006-10-17 19:33:52 +0000551 // The root of the dag may dangle to deleted nodes until the dag combiner is
552 // done. Set it to null to avoid confusion.
553 DAG.setRoot(SDOperand());
Chris Lattner24664722006-03-01 04:53:38 +0000554
555 /// DagCombineInfo - Expose the DAG combiner to the target combiner impls.
556 TargetLowering::DAGCombinerInfo
Evan Chengfa1eb272007-02-08 22:13:59 +0000557 DagCombineInfo(DAG, !RunningAfterLegalize, false, this);
Jim Laskey6ff23e52006-10-04 16:53:27 +0000558
Nate Begeman1d4d4142005-09-01 00:19:25 +0000559 // while the worklist isn't empty, inspect the node on the end of it and
560 // try and combine it.
561 while (!WorkList.empty()) {
562 SDNode *N = WorkList.back();
563 WorkList.pop_back();
564
565 // If N has no uses, it is dead. Make sure to revisit all N's operands once
Chris Lattner95038592005-10-05 06:35:28 +0000566 // N is deleted from the DAG, since they too may now be dead or may have a
567 // reduced number of uses, allowing other xforms.
568 if (N->use_empty() && N != &Dummy) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000569 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Jim Laskey6ff23e52006-10-04 16:53:27 +0000570 AddToWorkList(N->getOperand(i).Val);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000571
Chris Lattner95038592005-10-05 06:35:28 +0000572 DAG.DeleteNode(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000573 continue;
574 }
575
Nate Begeman83e75ec2005-09-06 04:43:02 +0000576 SDOperand RV = visit(N);
Chris Lattner24664722006-03-01 04:53:38 +0000577
578 // If nothing happened, try a target-specific DAG combine.
579 if (RV.Val == 0) {
Chris Lattner729c6d12006-05-27 00:43:02 +0000580 assert(N->getOpcode() != ISD::DELETED_NODE &&
581 "Node was deleted but visit returned NULL!");
Chris Lattner24664722006-03-01 04:53:38 +0000582 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
583 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode()))
584 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
585 }
586
Nate Begeman83e75ec2005-09-06 04:43:02 +0000587 if (RV.Val) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000588 ++NodesCombined;
Nate Begeman646d7e22005-09-02 21:18:40 +0000589 // If we get back the same node we passed in, rather than a new node or
590 // zero, we know that the node must have defined multiple values and
591 // CombineTo was used. Since CombineTo takes care of the worklist
592 // mechanics for us, we have no work to do in this case.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000593 if (RV.Val != N) {
Chris Lattner729c6d12006-05-27 00:43:02 +0000594 assert(N->getOpcode() != ISD::DELETED_NODE &&
595 RV.Val->getOpcode() != ISD::DELETED_NODE &&
596 "Node was deleted but visit returned new node!");
597
Dan Gohmanb5bec2b2007-06-19 14:13:56 +0000598 DOUT << "\nReplacing.3 "; DEBUG(N->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +0000599 DOUT << "\nWith: "; DEBUG(RV.Val->dump(&DAG));
600 DOUT << '\n';
Chris Lattner01a22022005-10-10 22:04:48 +0000601 std::vector<SDNode*> NowDead;
Evan Cheng2adffa12006-09-21 19:04:05 +0000602 if (N->getNumValues() == RV.Val->getNumValues())
603 DAG.ReplaceAllUsesWith(N, RV.Val, &NowDead);
604 else {
605 assert(N->getValueType(0) == RV.getValueType() && "Type mismatch");
606 SDOperand OpV = RV;
607 DAG.ReplaceAllUsesWith(N, &OpV, &NowDead);
608 }
Nate Begeman646d7e22005-09-02 21:18:40 +0000609
610 // Push the new node and any users onto the worklist
Jim Laskey6ff23e52006-10-04 16:53:27 +0000611 AddToWorkList(RV.Val);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000612 AddUsersToWorkList(RV.Val);
Nate Begeman646d7e22005-09-02 21:18:40 +0000613
Jim Laskey6ff23e52006-10-04 16:53:27 +0000614 // Nodes can be reintroduced into the worklist. Make sure we do not
615 // process a node that has been replaced.
Nate Begeman646d7e22005-09-02 21:18:40 +0000616 removeFromWorkList(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000617 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
618 removeFromWorkList(NowDead[i]);
Chris Lattner5c46f742005-10-05 06:11:08 +0000619
620 // Finally, since the node is now dead, remove it from the graph.
621 DAG.DeleteNode(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000622 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000623 }
624 }
Chris Lattner95038592005-10-05 06:35:28 +0000625
626 // If the root changed (e.g. it was a dead load, update the root).
627 DAG.setRoot(Dummy.getValue());
Nate Begeman1d4d4142005-09-01 00:19:25 +0000628}
629
Nate Begeman83e75ec2005-09-06 04:43:02 +0000630SDOperand DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000631 switch(N->getOpcode()) {
632 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +0000633 case ISD::TokenFactor: return visitTokenFactor(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000634 case ISD::ADD: return visitADD(N);
635 case ISD::SUB: return visitSUB(N);
Chris Lattner91153682007-03-04 20:03:15 +0000636 case ISD::ADDC: return visitADDC(N);
637 case ISD::ADDE: return visitADDE(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000638 case ISD::MUL: return visitMUL(N);
639 case ISD::SDIV: return visitSDIV(N);
640 case ISD::UDIV: return visitUDIV(N);
641 case ISD::SREM: return visitSREM(N);
642 case ISD::UREM: return visitUREM(N);
643 case ISD::MULHU: return visitMULHU(N);
644 case ISD::MULHS: return visitMULHS(N);
645 case ISD::AND: return visitAND(N);
646 case ISD::OR: return visitOR(N);
647 case ISD::XOR: return visitXOR(N);
648 case ISD::SHL: return visitSHL(N);
649 case ISD::SRA: return visitSRA(N);
650 case ISD::SRL: return visitSRL(N);
651 case ISD::CTLZ: return visitCTLZ(N);
652 case ISD::CTTZ: return visitCTTZ(N);
653 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000654 case ISD::SELECT: return visitSELECT(N);
655 case ISD::SELECT_CC: return visitSELECT_CC(N);
656 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000657 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
658 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000659 case ISD::ANY_EXTEND: return visitANY_EXTEND(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000660 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
661 case ISD::TRUNCATE: return visitTRUNCATE(N);
Chris Lattner94683772005-12-23 05:30:37 +0000662 case ISD::BIT_CONVERT: return visitBIT_CONVERT(N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000663 case ISD::FADD: return visitFADD(N);
664 case ISD::FSUB: return visitFSUB(N);
665 case ISD::FMUL: return visitFMUL(N);
666 case ISD::FDIV: return visitFDIV(N);
667 case ISD::FREM: return visitFREM(N);
Chris Lattner12d83032006-03-05 05:30:57 +0000668 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000669 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
670 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
671 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
672 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
673 case ISD::FP_ROUND: return visitFP_ROUND(N);
674 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
675 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
676 case ISD::FNEG: return visitFNEG(N);
677 case ISD::FABS: return visitFABS(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000678 case ISD::BRCOND: return visitBRCOND(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000679 case ISD::BR_CC: return visitBR_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000680 case ISD::LOAD: return visitLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000681 case ISD::STORE: return visitSTORE(N);
Chris Lattnerca242442006-03-19 01:27:56 +0000682 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
Dan Gohman7f321562007-06-25 16:23:39 +0000683 case ISD::BUILD_VECTOR: return visitBUILD_VECTOR(N);
684 case ISD::CONCAT_VECTORS: return visitCONCAT_VECTORS(N);
Chris Lattner66445d32006-03-28 22:11:53 +0000685 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000686 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000687 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000688}
689
Chris Lattner6270f682006-10-08 22:57:01 +0000690/// getInputChainForNode - Given a node, return its input chain if it has one,
691/// otherwise return a null sd operand.
692static SDOperand getInputChainForNode(SDNode *N) {
693 if (unsigned NumOps = N->getNumOperands()) {
694 if (N->getOperand(0).getValueType() == MVT::Other)
695 return N->getOperand(0);
696 else if (N->getOperand(NumOps-1).getValueType() == MVT::Other)
697 return N->getOperand(NumOps-1);
698 for (unsigned i = 1; i < NumOps-1; ++i)
699 if (N->getOperand(i).getValueType() == MVT::Other)
700 return N->getOperand(i);
701 }
702 return SDOperand(0, 0);
703}
704
Nate Begeman83e75ec2005-09-06 04:43:02 +0000705SDOperand DAGCombiner::visitTokenFactor(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +0000706 // If N has two operands, where one has an input chain equal to the other,
707 // the 'other' chain is redundant.
708 if (N->getNumOperands() == 2) {
709 if (getInputChainForNode(N->getOperand(0).Val) == N->getOperand(1))
710 return N->getOperand(0);
711 if (getInputChainForNode(N->getOperand(1).Val) == N->getOperand(0))
712 return N->getOperand(1);
713 }
714
Chris Lattnerc76d4412007-05-16 06:37:59 +0000715 SmallVector<SDNode *, 8> TFs; // List of token factors to visit.
716 SmallVector<SDOperand, 8> Ops; // Ops for replacing token factor.
717 SmallPtrSet<SDNode*, 16> SeenOps;
718 bool Changed = false; // If we should replace this token factor.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000719
720 // Start out with this token factor.
Jim Laskey279f0532006-09-25 16:29:54 +0000721 TFs.push_back(N);
Jim Laskey279f0532006-09-25 16:29:54 +0000722
Jim Laskey71382342006-10-07 23:37:56 +0000723 // Iterate through token factors. The TFs grows when new token factors are
Jim Laskeybc588b82006-10-05 15:07:25 +0000724 // encountered.
725 for (unsigned i = 0; i < TFs.size(); ++i) {
726 SDNode *TF = TFs[i];
727
Jim Laskey6ff23e52006-10-04 16:53:27 +0000728 // Check each of the operands.
729 for (unsigned i = 0, ie = TF->getNumOperands(); i != ie; ++i) {
730 SDOperand Op = TF->getOperand(i);
Jim Laskey279f0532006-09-25 16:29:54 +0000731
Jim Laskey6ff23e52006-10-04 16:53:27 +0000732 switch (Op.getOpcode()) {
733 case ISD::EntryToken:
Jim Laskeybc588b82006-10-05 15:07:25 +0000734 // Entry tokens don't need to be added to the list. They are
735 // rededundant.
736 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000737 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000738
Jim Laskey6ff23e52006-10-04 16:53:27 +0000739 case ISD::TokenFactor:
Jim Laskeybc588b82006-10-05 15:07:25 +0000740 if ((CombinerAA || Op.hasOneUse()) &&
741 std::find(TFs.begin(), TFs.end(), Op.Val) == TFs.end()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +0000742 // Queue up for processing.
743 TFs.push_back(Op.Val);
744 // Clean up in case the token factor is removed.
745 AddToWorkList(Op.Val);
746 Changed = true;
747 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000748 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000749 // Fall thru
750
751 default:
Chris Lattnerc76d4412007-05-16 06:37:59 +0000752 // Only add if it isn't already in the list.
753 if (SeenOps.insert(Op.Val))
Jim Laskeybc588b82006-10-05 15:07:25 +0000754 Ops.push_back(Op);
Chris Lattnerc76d4412007-05-16 06:37:59 +0000755 else
756 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000757 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000758 }
759 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000760 }
761
762 SDOperand Result;
763
764 // If we've change things around then replace token factor.
765 if (Changed) {
766 if (Ops.size() == 0) {
767 // The entry token is the only possible outcome.
768 Result = DAG.getEntryNode();
769 } else {
770 // New and improved token factor.
771 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0], Ops.size());
Nate Begemanded49632005-10-13 03:11:28 +0000772 }
Jim Laskey274062c2006-10-13 23:32:28 +0000773
774 // Don't add users to work list.
775 return CombineTo(N, Result, false);
Nate Begemanded49632005-10-13 03:11:28 +0000776 }
Jim Laskey279f0532006-09-25 16:29:54 +0000777
Jim Laskey6ff23e52006-10-04 16:53:27 +0000778 return Result;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000779}
780
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000781static
782SDOperand combineShlAddConstant(SDOperand N0, SDOperand N1, SelectionDAG &DAG) {
783 MVT::ValueType VT = N0.getValueType();
784 SDOperand N00 = N0.getOperand(0);
785 SDOperand N01 = N0.getOperand(1);
786 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N01);
787 if (N01C && N00.getOpcode() == ISD::ADD && N00.Val->hasOneUse() &&
788 isa<ConstantSDNode>(N00.getOperand(1))) {
789 N0 = DAG.getNode(ISD::ADD, VT,
790 DAG.getNode(ISD::SHL, VT, N00.getOperand(0), N01),
791 DAG.getNode(ISD::SHL, VT, N00.getOperand(1), N01));
792 return DAG.getNode(ISD::ADD, VT, N0, N1);
793 }
794 return SDOperand();
795}
796
Evan Chengb13cdbd2007-06-21 07:39:16 +0000797static
798SDOperand combineSelectAndUse(SDNode *N, SDOperand Slct, SDOperand OtherOp,
799 SelectionDAG &DAG) {
800 MVT::ValueType VT = N->getValueType(0);
801 unsigned Opc = N->getOpcode();
802 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
803 SDOperand LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
804 SDOperand RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
805 ISD::CondCode CC = ISD::SETCC_INVALID;
806 if (isSlctCC)
807 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
808 else {
809 SDOperand CCOp = Slct.getOperand(0);
810 if (CCOp.getOpcode() == ISD::SETCC)
811 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
812 }
813
814 bool DoXform = false;
815 bool InvCC = false;
816 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
817 "Bad input!");
818 if (LHS.getOpcode() == ISD::Constant &&
819 cast<ConstantSDNode>(LHS)->isNullValue())
820 DoXform = true;
821 else if (CC != ISD::SETCC_INVALID &&
822 RHS.getOpcode() == ISD::Constant &&
823 cast<ConstantSDNode>(RHS)->isNullValue()) {
824 std::swap(LHS, RHS);
825 bool isInt = MVT::isInteger(isSlctCC ? Slct.getOperand(0).getValueType()
826 : Slct.getOperand(0).getOperand(0).getValueType());
827 CC = ISD::getSetCCInverse(CC, isInt);
828 DoXform = true;
829 InvCC = true;
830 }
831
832 if (DoXform) {
833 SDOperand Result = DAG.getNode(Opc, VT, OtherOp, RHS);
834 if (isSlctCC)
835 return DAG.getSelectCC(OtherOp, Result,
836 Slct.getOperand(0), Slct.getOperand(1), CC);
837 SDOperand CCOp = Slct.getOperand(0);
838 if (InvCC)
839 CCOp = DAG.getSetCC(CCOp.getValueType(), CCOp.getOperand(0),
840 CCOp.getOperand(1), CC);
841 return DAG.getNode(ISD::SELECT, VT, CCOp, OtherOp, Result);
842 }
843 return SDOperand();
844}
845
Nate Begeman83e75ec2005-09-06 04:43:02 +0000846SDOperand DAGCombiner::visitADD(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000847 SDOperand N0 = N->getOperand(0);
848 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000849 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
850 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemanf89d78d2005-09-07 16:09:19 +0000851 MVT::ValueType VT = N0.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +0000852
853 // fold vector ops
854 SDOperand FoldedVOp = SimplifyVBinOp(N);
855 if (FoldedVOp.Val) return FoldedVOp;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000856
Dan Gohman613e0d82007-07-03 14:03:57 +0000857 // fold (add x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +0000858 if (N0.getOpcode() == ISD::UNDEF)
859 return N0;
860 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +0000861 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000862 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000863 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000864 return DAG.getNode(ISD::ADD, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000865 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000866 if (N0C && !N1C)
867 return DAG.getNode(ISD::ADD, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000868 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000869 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000870 return N0;
Chris Lattner4aafb4f2006-01-12 20:22:43 +0000871 // fold ((c1-A)+c2) -> (c1+c2)-A
872 if (N1C && N0.getOpcode() == ISD::SUB)
873 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
874 return DAG.getNode(ISD::SUB, VT,
875 DAG.getConstant(N1C->getValue()+N0C->getValue(), VT),
876 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000877 // reassociate add
878 SDOperand RADD = ReassociateOps(ISD::ADD, N0, N1);
879 if (RADD.Val != 0)
880 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000881 // fold ((0-A) + B) -> B-A
882 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
883 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000884 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000885 // fold (A + (0-B)) -> A-B
886 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
887 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000888 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +0000889 // fold (A+(B-A)) -> B
890 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000891 return N1.getOperand(0);
Chris Lattner947c2892006-03-13 06:51:27 +0000892
Evan Cheng860771d2006-03-01 01:09:54 +0000893 if (!MVT::isVector(VT) && SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +0000894 return SDOperand(N, 0);
Chris Lattner947c2892006-03-13 06:51:27 +0000895
896 // fold (a+b) -> (a|b) iff a and b share no bits.
897 if (MVT::isInteger(VT) && !MVT::isVector(VT)) {
898 uint64_t LHSZero, LHSOne;
899 uint64_t RHSZero, RHSOne;
900 uint64_t Mask = MVT::getIntVTBitMask(VT);
Dan Gohmanea859be2007-06-22 14:59:07 +0000901 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Chris Lattner947c2892006-03-13 06:51:27 +0000902 if (LHSZero) {
Dan Gohmanea859be2007-06-22 14:59:07 +0000903 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
Chris Lattner947c2892006-03-13 06:51:27 +0000904
905 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
906 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
907 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
908 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
909 return DAG.getNode(ISD::OR, VT, N0, N1);
910 }
911 }
Evan Cheng3ef554d2006-11-06 08:14:30 +0000912
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000913 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
914 if (N0.getOpcode() == ISD::SHL && N0.Val->hasOneUse()) {
915 SDOperand Result = combineShlAddConstant(N0, N1, DAG);
916 if (Result.Val) return Result;
917 }
918 if (N1.getOpcode() == ISD::SHL && N1.Val->hasOneUse()) {
919 SDOperand Result = combineShlAddConstant(N1, N0, DAG);
920 if (Result.Val) return Result;
921 }
922
Evan Chengb13cdbd2007-06-21 07:39:16 +0000923 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
924 if (N0.getOpcode() == ISD::SELECT && N0.Val->hasOneUse()) {
925 SDOperand Result = combineSelectAndUse(N, N0, N1, DAG);
926 if (Result.Val) return Result;
927 }
928 if (N1.getOpcode() == ISD::SELECT && N1.Val->hasOneUse()) {
929 SDOperand Result = combineSelectAndUse(N, N1, N0, DAG);
930 if (Result.Val) return Result;
931 }
932
Nate Begeman83e75ec2005-09-06 04:43:02 +0000933 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000934}
935
Chris Lattner91153682007-03-04 20:03:15 +0000936SDOperand DAGCombiner::visitADDC(SDNode *N) {
937 SDOperand N0 = N->getOperand(0);
938 SDOperand N1 = N->getOperand(1);
939 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
940 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
941 MVT::ValueType VT = N0.getValueType();
942
943 // If the flag result is dead, turn this into an ADD.
944 if (N->hasNUsesOfValue(0, 1))
945 return CombineTo(N, DAG.getNode(ISD::ADD, VT, N1, N0),
Chris Lattnerb6541762007-03-04 20:40:38 +0000946 DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
Chris Lattner91153682007-03-04 20:03:15 +0000947
948 // canonicalize constant to RHS.
Chris Lattnerbcf24842007-03-04 20:08:45 +0000949 if (N0C && !N1C) {
950 SDOperand Ops[] = { N1, N0 };
951 return DAG.getNode(ISD::ADDC, N->getVTList(), Ops, 2);
952 }
Chris Lattner91153682007-03-04 20:03:15 +0000953
Chris Lattnerb6541762007-03-04 20:40:38 +0000954 // fold (addc x, 0) -> x + no carry out
955 if (N1C && N1C->isNullValue())
956 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
957
958 // fold (addc a, b) -> (or a, b), CARRY_FALSE iff a and b share no bits.
959 uint64_t LHSZero, LHSOne;
960 uint64_t RHSZero, RHSOne;
961 uint64_t Mask = MVT::getIntVTBitMask(VT);
Dan Gohmanea859be2007-06-22 14:59:07 +0000962 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Chris Lattnerb6541762007-03-04 20:40:38 +0000963 if (LHSZero) {
Dan Gohmanea859be2007-06-22 14:59:07 +0000964 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
Chris Lattnerb6541762007-03-04 20:40:38 +0000965
966 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
967 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
968 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
969 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
970 return CombineTo(N, DAG.getNode(ISD::OR, VT, N0, N1),
971 DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
972 }
Chris Lattner91153682007-03-04 20:03:15 +0000973
974 return SDOperand();
975}
976
977SDOperand DAGCombiner::visitADDE(SDNode *N) {
978 SDOperand N0 = N->getOperand(0);
979 SDOperand N1 = N->getOperand(1);
Chris Lattnerb6541762007-03-04 20:40:38 +0000980 SDOperand CarryIn = N->getOperand(2);
Chris Lattner91153682007-03-04 20:03:15 +0000981 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
982 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Chris Lattnerbcf24842007-03-04 20:08:45 +0000983 //MVT::ValueType VT = N0.getValueType();
Chris Lattner91153682007-03-04 20:03:15 +0000984
985 // canonicalize constant to RHS
Chris Lattnerbcf24842007-03-04 20:08:45 +0000986 if (N0C && !N1C) {
Chris Lattnerb6541762007-03-04 20:40:38 +0000987 SDOperand Ops[] = { N1, N0, CarryIn };
Chris Lattnerbcf24842007-03-04 20:08:45 +0000988 return DAG.getNode(ISD::ADDE, N->getVTList(), Ops, 3);
989 }
Chris Lattner91153682007-03-04 20:03:15 +0000990
Chris Lattnerb6541762007-03-04 20:40:38 +0000991 // fold (adde x, y, false) -> (addc x, y)
992 if (CarryIn.getOpcode() == ISD::CARRY_FALSE) {
993 SDOperand Ops[] = { N1, N0 };
994 return DAG.getNode(ISD::ADDC, N->getVTList(), Ops, 2);
995 }
Chris Lattner91153682007-03-04 20:03:15 +0000996
997 return SDOperand();
998}
999
1000
1001
Nate Begeman83e75ec2005-09-06 04:43:02 +00001002SDOperand DAGCombiner::visitSUB(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001003 SDOperand N0 = N->getOperand(0);
1004 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001005 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1006 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +00001007 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001008
Dan Gohman7f321562007-06-25 16:23:39 +00001009 // fold vector ops
1010 SDOperand FoldedVOp = SimplifyVBinOp(N);
1011 if (FoldedVOp.Val) return FoldedVOp;
1012
Chris Lattner854077d2005-10-17 01:07:11 +00001013 // fold (sub x, x) -> 0
1014 if (N0 == N1)
1015 return DAG.getConstant(0, N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001016 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001017 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001018 return DAG.getNode(ISD::SUB, VT, N0, N1);
Chris Lattner05b57432005-10-11 06:07:15 +00001019 // fold (sub x, c) -> (add x, -c)
1020 if (N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001021 return DAG.getNode(ISD::ADD, VT, N0, DAG.getConstant(-N1C->getValue(), VT));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001022 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +00001023 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001024 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001025 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +00001026 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001027 return N0.getOperand(0);
Evan Chengb13cdbd2007-06-21 07:39:16 +00001028 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
1029 if (N1.getOpcode() == ISD::SELECT && N1.Val->hasOneUse()) {
1030 SDOperand Result = combineSelectAndUse(N, N1, N0, DAG);
1031 if (Result.Val) return Result;
1032 }
Dan Gohman613e0d82007-07-03 14:03:57 +00001033 // If either operand of a sub is undef, the result is undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00001034 if (N0.getOpcode() == ISD::UNDEF)
1035 return N0;
1036 if (N1.getOpcode() == ISD::UNDEF)
1037 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001038
Nate Begeman83e75ec2005-09-06 04:43:02 +00001039 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001040}
1041
Nate Begeman83e75ec2005-09-06 04:43:02 +00001042SDOperand DAGCombiner::visitMUL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001043 SDOperand N0 = N->getOperand(0);
1044 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001045 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1046 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman223df222005-09-08 20:18:10 +00001047 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001048
Dan Gohman7f321562007-06-25 16:23:39 +00001049 // fold vector ops
1050 SDOperand FoldedVOp = SimplifyVBinOp(N);
1051 if (FoldedVOp.Val) return FoldedVOp;
1052
Dan Gohman613e0d82007-07-03 14:03:57 +00001053 // fold (mul x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001054 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001055 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001056 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001057 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001058 return DAG.getNode(ISD::MUL, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001059 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001060 if (N0C && !N1C)
1061 return DAG.getNode(ISD::MUL, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001062 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001063 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001064 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001065 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +00001066 if (N1C && N1C->isAllOnesValue())
Nate Begeman405e3ec2005-10-21 00:02:42 +00001067 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001068 // fold (mul x, (1 << c)) -> x << c
Nate Begeman646d7e22005-09-02 21:18:40 +00001069 if (N1C && isPowerOf2_64(N1C->getValue()))
Chris Lattner3e6099b2005-10-30 06:41:49 +00001070 return DAG.getNode(ISD::SHL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +00001071 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001072 TLI.getShiftAmountTy()));
Chris Lattner3e6099b2005-10-30 06:41:49 +00001073 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
1074 if (N1C && isPowerOf2_64(-N1C->getSignExtended())) {
1075 // FIXME: If the input is something that is easily negated (e.g. a
1076 // single-use add), we should put the negate there.
1077 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT),
1078 DAG.getNode(ISD::SHL, VT, N0,
1079 DAG.getConstant(Log2_64(-N1C->getSignExtended()),
1080 TLI.getShiftAmountTy())));
1081 }
Andrew Lenharth50a0d422006-04-02 21:42:45 +00001082
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001083 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
1084 if (N1C && N0.getOpcode() == ISD::SHL &&
1085 isa<ConstantSDNode>(N0.getOperand(1))) {
1086 SDOperand C3 = DAG.getNode(ISD::SHL, VT, N1, N0.getOperand(1));
Chris Lattner5750df92006-03-01 04:03:14 +00001087 AddToWorkList(C3.Val);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001088 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0), C3);
1089 }
1090
1091 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
1092 // use.
1093 {
1094 SDOperand Sh(0,0), Y(0,0);
1095 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
1096 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
1097 N0.Val->hasOneUse()) {
1098 Sh = N0; Y = N1;
1099 } else if (N1.getOpcode() == ISD::SHL &&
1100 isa<ConstantSDNode>(N1.getOperand(1)) && N1.Val->hasOneUse()) {
1101 Sh = N1; Y = N0;
1102 }
1103 if (Sh.Val) {
1104 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Sh.getOperand(0), Y);
1105 return DAG.getNode(ISD::SHL, VT, Mul, Sh.getOperand(1));
1106 }
1107 }
Chris Lattnera1deca32006-03-04 23:33:26 +00001108 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
1109 if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() &&
1110 isa<ConstantSDNode>(N0.getOperand(1))) {
1111 return DAG.getNode(ISD::ADD, VT,
1112 DAG.getNode(ISD::MUL, VT, N0.getOperand(0), N1),
1113 DAG.getNode(ISD::MUL, VT, N0.getOperand(1), N1));
1114 }
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001115
Nate Begemancd4d58c2006-02-03 06:46:56 +00001116 // reassociate mul
1117 SDOperand RMUL = ReassociateOps(ISD::MUL, N0, N1);
1118 if (RMUL.Val != 0)
1119 return RMUL;
Dan Gohman7f321562007-06-25 16:23:39 +00001120
Nate Begeman83e75ec2005-09-06 04:43:02 +00001121 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001122}
1123
Nate Begeman83e75ec2005-09-06 04:43:02 +00001124SDOperand DAGCombiner::visitSDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001125 SDOperand N0 = N->getOperand(0);
1126 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001127 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1128 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +00001129 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001130
Dan Gohman7f321562007-06-25 16:23:39 +00001131 // fold vector ops
1132 SDOperand FoldedVOp = SimplifyVBinOp(N);
1133 if (FoldedVOp.Val) return FoldedVOp;
1134
Nate Begeman1d4d4142005-09-01 00:19:25 +00001135 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001136 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001137 return DAG.getNode(ISD::SDIV, VT, N0, N1);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001138 // fold (sdiv X, 1) -> X
1139 if (N1C && N1C->getSignExtended() == 1LL)
1140 return N0;
1141 // fold (sdiv X, -1) -> 0-X
1142 if (N1C && N1C->isAllOnesValue())
1143 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +00001144 // If we know the sign bits of both operands are zero, strength reduce to a
1145 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
1146 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001147 if (DAG.MaskedValueIsZero(N1, SignBit) &&
1148 DAG.MaskedValueIsZero(N0, SignBit))
Chris Lattner094c8fc2005-10-07 06:10:46 +00001149 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
Nate Begemancd6a6ed2006-02-17 07:26:20 +00001150 // fold (sdiv X, pow2) -> simple ops after legalize
Nate Begemanfb7217b2006-02-17 19:54:08 +00001151 if (N1C && N1C->getValue() && !TLI.isIntDivCheap() &&
Nate Begeman405e3ec2005-10-21 00:02:42 +00001152 (isPowerOf2_64(N1C->getSignExtended()) ||
1153 isPowerOf2_64(-N1C->getSignExtended()))) {
1154 // If dividing by powers of two is cheap, then don't perform the following
1155 // fold.
1156 if (TLI.isPow2DivCheap())
1157 return SDOperand();
1158 int64_t pow2 = N1C->getSignExtended();
1159 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
Chris Lattner8f4880b2006-02-16 08:02:36 +00001160 unsigned lg2 = Log2_64(abs2);
1161 // Splat the sign bit into the register
1162 SDOperand SGN = DAG.getNode(ISD::SRA, VT, N0,
Nate Begeman405e3ec2005-10-21 00:02:42 +00001163 DAG.getConstant(MVT::getSizeInBits(VT)-1,
1164 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00001165 AddToWorkList(SGN.Val);
Chris Lattner8f4880b2006-02-16 08:02:36 +00001166 // Add (N0 < 0) ? abs2 - 1 : 0;
1167 SDOperand SRL = DAG.getNode(ISD::SRL, VT, SGN,
1168 DAG.getConstant(MVT::getSizeInBits(VT)-lg2,
Nate Begeman405e3ec2005-10-21 00:02:42 +00001169 TLI.getShiftAmountTy()));
Chris Lattner8f4880b2006-02-16 08:02:36 +00001170 SDOperand ADD = DAG.getNode(ISD::ADD, VT, N0, SRL);
Chris Lattner5750df92006-03-01 04:03:14 +00001171 AddToWorkList(SRL.Val);
1172 AddToWorkList(ADD.Val); // Divide by pow2
Chris Lattner8f4880b2006-02-16 08:02:36 +00001173 SDOperand SRA = DAG.getNode(ISD::SRA, VT, ADD,
1174 DAG.getConstant(lg2, TLI.getShiftAmountTy()));
Nate Begeman405e3ec2005-10-21 00:02:42 +00001175 // If we're dividing by a positive value, we're done. Otherwise, we must
1176 // negate the result.
1177 if (pow2 > 0)
1178 return SRA;
Chris Lattner5750df92006-03-01 04:03:14 +00001179 AddToWorkList(SRA.Val);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001180 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), SRA);
1181 }
Nate Begeman69575232005-10-20 02:15:44 +00001182 // if integer divide is expensive and we satisfy the requirements, emit an
1183 // alternate sequence.
Nate Begeman405e3ec2005-10-21 00:02:42 +00001184 if (N1C && (N1C->getSignExtended() < -1 || N1C->getSignExtended() > 1) &&
Chris Lattnere9936d12005-10-22 18:50:15 +00001185 !TLI.isIntDivCheap()) {
1186 SDOperand Op = BuildSDIV(N);
1187 if (Op.Val) return Op;
Nate Begeman69575232005-10-20 02:15:44 +00001188 }
Dan Gohman7f321562007-06-25 16:23:39 +00001189
Dan Gohman613e0d82007-07-03 14:03:57 +00001190 // undef / X -> 0
1191 if (N0.getOpcode() == ISD::UNDEF)
1192 return DAG.getConstant(0, VT);
1193 // X / undef -> undef
1194 if (N1.getOpcode() == ISD::UNDEF)
1195 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001196
Nate Begeman83e75ec2005-09-06 04:43:02 +00001197 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001198}
1199
Nate Begeman83e75ec2005-09-06 04:43:02 +00001200SDOperand DAGCombiner::visitUDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001201 SDOperand N0 = N->getOperand(0);
1202 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001203 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1204 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +00001205 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001206
Dan Gohman7f321562007-06-25 16:23:39 +00001207 // fold vector ops
1208 SDOperand FoldedVOp = SimplifyVBinOp(N);
1209 if (FoldedVOp.Val) return FoldedVOp;
1210
Nate Begeman1d4d4142005-09-01 00:19:25 +00001211 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001212 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001213 return DAG.getNode(ISD::UDIV, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001214 // fold (udiv x, (1 << c)) -> x >>u c
Nate Begeman646d7e22005-09-02 21:18:40 +00001215 if (N1C && isPowerOf2_64(N1C->getValue()))
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001216 return DAG.getNode(ISD::SRL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +00001217 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001218 TLI.getShiftAmountTy()));
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001219 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
1220 if (N1.getOpcode() == ISD::SHL) {
1221 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
1222 if (isPowerOf2_64(SHC->getValue())) {
1223 MVT::ValueType ADDVT = N1.getOperand(1).getValueType();
Nate Begemanc031e332006-02-05 07:36:48 +00001224 SDOperand Add = DAG.getNode(ISD::ADD, ADDVT, N1.getOperand(1),
1225 DAG.getConstant(Log2_64(SHC->getValue()),
1226 ADDVT));
Chris Lattner5750df92006-03-01 04:03:14 +00001227 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +00001228 return DAG.getNode(ISD::SRL, VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001229 }
1230 }
1231 }
Nate Begeman69575232005-10-20 02:15:44 +00001232 // fold (udiv x, c) -> alternate
Chris Lattnere9936d12005-10-22 18:50:15 +00001233 if (N1C && N1C->getValue() && !TLI.isIntDivCheap()) {
1234 SDOperand Op = BuildUDIV(N);
1235 if (Op.Val) return Op;
1236 }
Dan Gohman7f321562007-06-25 16:23:39 +00001237
Dan Gohman613e0d82007-07-03 14:03:57 +00001238 // undef / X -> 0
1239 if (N0.getOpcode() == ISD::UNDEF)
1240 return DAG.getConstant(0, VT);
1241 // X / undef -> undef
1242 if (N1.getOpcode() == ISD::UNDEF)
1243 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001244
Nate Begeman83e75ec2005-09-06 04:43:02 +00001245 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001246}
1247
Nate Begeman83e75ec2005-09-06 04:43:02 +00001248SDOperand DAGCombiner::visitSREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001249 SDOperand N0 = N->getOperand(0);
1250 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001251 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1252 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +00001253 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001254
1255 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001256 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001257 return DAG.getNode(ISD::SREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +00001258 // If we know the sign bits of both operands are zero, strength reduce to a
1259 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
1260 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001261 if (DAG.MaskedValueIsZero(N1, SignBit) &&
1262 DAG.MaskedValueIsZero(N0, SignBit))
Nate Begemana148d982006-01-18 22:35:16 +00001263 return DAG.getNode(ISD::UREM, VT, N0, N1);
Chris Lattner26d29902006-10-12 20:58:32 +00001264
1265 // Unconditionally lower X%C -> X-X/C*C. This allows the X/C logic to hack on
1266 // the remainder operation.
1267 if (N1C && !N1C->isNullValue()) {
1268 SDOperand Div = DAG.getNode(ISD::SDIV, VT, N0, N1);
1269 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Div, N1);
1270 SDOperand Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
1271 AddToWorkList(Div.Val);
1272 AddToWorkList(Mul.Val);
1273 return Sub;
1274 }
1275
Dan Gohman613e0d82007-07-03 14:03:57 +00001276 // undef % X -> 0
1277 if (N0.getOpcode() == ISD::UNDEF)
1278 return DAG.getConstant(0, VT);
1279 // X % undef -> undef
1280 if (N1.getOpcode() == ISD::UNDEF)
1281 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001282
Nate Begeman83e75ec2005-09-06 04:43:02 +00001283 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001284}
1285
Nate Begeman83e75ec2005-09-06 04:43:02 +00001286SDOperand DAGCombiner::visitUREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001287 SDOperand N0 = N->getOperand(0);
1288 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001289 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1290 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +00001291 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001292
1293 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001294 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001295 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +00001296 // fold (urem x, pow2) -> (and x, pow2-1)
1297 if (N1C && !N1C->isNullValue() && isPowerOf2_64(N1C->getValue()))
Nate Begemana148d982006-01-18 22:35:16 +00001298 return DAG.getNode(ISD::AND, VT, N0, DAG.getConstant(N1C->getValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +00001299 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
1300 if (N1.getOpcode() == ISD::SHL) {
1301 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
1302 if (isPowerOf2_64(SHC->getValue())) {
Nate Begemanbab92392006-02-05 08:07:24 +00001303 SDOperand Add = DAG.getNode(ISD::ADD, VT, N1,DAG.getConstant(~0ULL,VT));
Chris Lattner5750df92006-03-01 04:03:14 +00001304 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +00001305 return DAG.getNode(ISD::AND, VT, N0, Add);
1306 }
1307 }
1308 }
Chris Lattner26d29902006-10-12 20:58:32 +00001309
1310 // Unconditionally lower X%C -> X-X/C*C. This allows the X/C logic to hack on
1311 // the remainder operation.
1312 if (N1C && !N1C->isNullValue()) {
1313 SDOperand Div = DAG.getNode(ISD::UDIV, VT, N0, N1);
1314 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Div, N1);
1315 SDOperand Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
1316 AddToWorkList(Div.Val);
1317 AddToWorkList(Mul.Val);
1318 return Sub;
1319 }
1320
Dan Gohman613e0d82007-07-03 14:03:57 +00001321 // undef % X -> 0
1322 if (N0.getOpcode() == ISD::UNDEF)
1323 return DAG.getConstant(0, VT);
1324 // X % undef -> undef
1325 if (N1.getOpcode() == ISD::UNDEF)
1326 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001327
Nate Begeman83e75ec2005-09-06 04:43:02 +00001328 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001329}
1330
Nate Begeman83e75ec2005-09-06 04:43:02 +00001331SDOperand DAGCombiner::visitMULHS(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001332 SDOperand N0 = N->getOperand(0);
1333 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001334 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Dan Gohman7f321562007-06-25 16:23:39 +00001335 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001336
1337 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001338 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001339 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001340 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001341 if (N1C && N1C->getValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001342 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
1343 DAG.getConstant(MVT::getSizeInBits(N0.getValueType())-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001344 TLI.getShiftAmountTy()));
Dan Gohman613e0d82007-07-03 14:03:57 +00001345 // fold (mulhs x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001346 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001347 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00001348
Nate Begeman83e75ec2005-09-06 04:43:02 +00001349 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001350}
1351
Nate Begeman83e75ec2005-09-06 04:43:02 +00001352SDOperand DAGCombiner::visitMULHU(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001353 SDOperand N0 = N->getOperand(0);
1354 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001355 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Dan Gohman7f321562007-06-25 16:23:39 +00001356 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001357
1358 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001359 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001360 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001361 // fold (mulhu x, 1) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001362 if (N1C && N1C->getValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001363 return DAG.getConstant(0, N0.getValueType());
Dan Gohman613e0d82007-07-03 14:03:57 +00001364 // fold (mulhu x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001365 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001366 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00001367
Nate Begeman83e75ec2005-09-06 04:43:02 +00001368 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001369}
1370
Chris Lattner35e5c142006-05-05 05:51:50 +00001371/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
1372/// two operands of the same opcode, try to simplify it.
1373SDOperand DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
1374 SDOperand N0 = N->getOperand(0), N1 = N->getOperand(1);
1375 MVT::ValueType VT = N0.getValueType();
1376 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
1377
Chris Lattner540121f2006-05-05 06:31:05 +00001378 // For each of OP in AND/OR/XOR:
1379 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
1380 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
1381 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
Chris Lattner0d8dae72006-05-05 06:32:04 +00001382 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y))
Chris Lattner540121f2006-05-05 06:31:05 +00001383 if ((N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND||
Chris Lattner0d8dae72006-05-05 06:32:04 +00001384 N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::TRUNCATE) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001385 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1386 SDOperand ORNode = DAG.getNode(N->getOpcode(),
1387 N0.getOperand(0).getValueType(),
1388 N0.getOperand(0), N1.getOperand(0));
1389 AddToWorkList(ORNode.Val);
Chris Lattner540121f2006-05-05 06:31:05 +00001390 return DAG.getNode(N0.getOpcode(), VT, ORNode);
Chris Lattner35e5c142006-05-05 05:51:50 +00001391 }
1392
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001393 // For each of OP in SHL/SRL/SRA/AND...
1394 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
1395 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
1396 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
Chris Lattner35e5c142006-05-05 05:51:50 +00001397 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001398 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001399 N0.getOperand(1) == N1.getOperand(1)) {
1400 SDOperand ORNode = DAG.getNode(N->getOpcode(),
1401 N0.getOperand(0).getValueType(),
1402 N0.getOperand(0), N1.getOperand(0));
1403 AddToWorkList(ORNode.Val);
1404 return DAG.getNode(N0.getOpcode(), VT, ORNode, N0.getOperand(1));
1405 }
1406
1407 return SDOperand();
1408}
1409
Nate Begeman83e75ec2005-09-06 04:43:02 +00001410SDOperand DAGCombiner::visitAND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001411 SDOperand N0 = N->getOperand(0);
1412 SDOperand N1 = N->getOperand(1);
Nate Begemanfb7217b2006-02-17 19:54:08 +00001413 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001414 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1415 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001416 MVT::ValueType VT = N1.getValueType();
1417
Dan Gohman7f321562007-06-25 16:23:39 +00001418 // fold vector ops
1419 SDOperand FoldedVOp = SimplifyVBinOp(N);
1420 if (FoldedVOp.Val) return FoldedVOp;
1421
Dan Gohman613e0d82007-07-03 14:03:57 +00001422 // fold (and x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001423 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001424 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001425 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001426 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001427 return DAG.getNode(ISD::AND, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001428 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001429 if (N0C && !N1C)
1430 return DAG.getNode(ISD::AND, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001431 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001432 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001433 return N0;
1434 // if (and x, c) is known to be zero, return 0
Dan Gohmanea859be2007-06-22 14:59:07 +00001435 if (N1C && DAG.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001436 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +00001437 // reassociate and
1438 SDOperand RAND = ReassociateOps(ISD::AND, N0, N1);
1439 if (RAND.Val != 0)
1440 return RAND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001441 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
Nate Begeman5dc7e862005-11-02 18:42:59 +00001442 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001443 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Nate Begeman646d7e22005-09-02 21:18:40 +00001444 if ((ORI->getValue() & N1C->getValue()) == N1C->getValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001445 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +00001446 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
1447 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Chris Lattner1ec05d12006-03-01 21:47:21 +00001448 unsigned InMask = MVT::getIntVTBitMask(N0.getOperand(0).getValueType());
Dan Gohmanea859be2007-06-22 14:59:07 +00001449 if (DAG.MaskedValueIsZero(N0.getOperand(0),
Chris Lattner1ec05d12006-03-01 21:47:21 +00001450 ~N1C->getValue() & InMask)) {
1451 SDOperand Zext = DAG.getNode(ISD::ZERO_EXTEND, N0.getValueType(),
1452 N0.getOperand(0));
1453
1454 // Replace uses of the AND with uses of the Zero extend node.
1455 CombineTo(N, Zext);
1456
Chris Lattner3603cd62006-02-02 07:17:31 +00001457 // We actually want to replace all uses of the any_extend with the
1458 // zero_extend, to avoid duplicating things. This will later cause this
1459 // AND to be folded.
Chris Lattner1ec05d12006-03-01 21:47:21 +00001460 CombineTo(N0.Val, Zext);
Chris Lattnerfedced72006-04-20 23:55:59 +00001461 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner3603cd62006-02-02 07:17:31 +00001462 }
1463 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001464 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
1465 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1466 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1467 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1468
1469 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1470 MVT::isInteger(LL.getValueType())) {
1471 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
1472 if (cast<ConstantSDNode>(LR)->getValue() == 0 && Op1 == ISD::SETEQ) {
1473 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001474 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001475 return DAG.getSetCC(VT, ORNode, LR, Op1);
1476 }
1477 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
1478 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
1479 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001480 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001481 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1482 }
1483 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
1484 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
1485 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001486 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001487 return DAG.getSetCC(VT, ORNode, LR, Op1);
1488 }
1489 }
1490 // canonicalize equivalent to ll == rl
1491 if (LL == RR && LR == RL) {
1492 Op1 = ISD::getSetCCSwappedOperands(Op1);
1493 std::swap(RL, RR);
1494 }
1495 if (LL == RL && LR == RR) {
1496 bool isInteger = MVT::isInteger(LL.getValueType());
1497 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
1498 if (Result != ISD::SETCC_INVALID)
1499 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1500 }
1501 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001502
1503 // Simplify: and (op x...), (op y...) -> (op (and x, y))
1504 if (N0.getOpcode() == N1.getOpcode()) {
1505 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1506 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001507 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001508
Nate Begemande996292006-02-03 22:24:05 +00001509 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
1510 // fold (and (sra)) -> (and (srl)) when possible.
Chris Lattner6ea2dee2006-03-25 22:19:00 +00001511 if (!MVT::isVector(VT) &&
1512 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001513 return SDOperand(N, 0);
Nate Begemanded49632005-10-13 03:11:28 +00001514 // fold (zext_inreg (extload x)) -> (zextload x)
Evan Cheng83060c52007-03-07 08:07:03 +00001515 if (ISD::isEXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val)) {
Evan Cheng466685d2006-10-09 20:57:25 +00001516 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00001517 MVT::ValueType EVT = LN0->getLoadedVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001518 // If we zero all the possible extended bits, then we can turn this into
1519 // a zextload if we are running before legalize or the operation is legal.
Dan Gohmanea859be2007-06-22 14:59:07 +00001520 if (DAG.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Evan Chengc5484282006-10-04 00:56:09 +00001521 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00001522 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
1523 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00001524 LN0->getSrcValueOffset(), EVT,
1525 LN0->isVolatile(),
1526 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00001527 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001528 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001529 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001530 }
1531 }
1532 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Evan Cheng83060c52007-03-07 08:07:03 +00001533 if (ISD::isSEXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val) &&
1534 N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001535 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00001536 MVT::ValueType EVT = LN0->getLoadedVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001537 // If we zero all the possible extended bits, then we can turn this into
1538 // a zextload if we are running before legalize or the operation is legal.
Dan Gohmanea859be2007-06-22 14:59:07 +00001539 if (DAG.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Evan Chengc5484282006-10-04 00:56:09 +00001540 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00001541 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
1542 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00001543 LN0->getSrcValueOffset(), EVT,
1544 LN0->isVolatile(),
1545 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00001546 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001547 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001548 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001549 }
1550 }
Chris Lattner15045b62006-02-28 06:35:35 +00001551
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001552 // fold (and (load x), 255) -> (zextload x, i8)
1553 // fold (and (extload x, i16), 255) -> (zextload x, i8)
Evan Cheng466685d2006-10-09 20:57:25 +00001554 if (N1C && N0.getOpcode() == ISD::LOAD) {
1555 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
1556 if (LN0->getExtensionType() != ISD::SEXTLOAD &&
Evan Cheng83060c52007-03-07 08:07:03 +00001557 LN0->getAddressingMode() == ISD::UNINDEXED &&
Evan Cheng466685d2006-10-09 20:57:25 +00001558 N0.hasOneUse()) {
1559 MVT::ValueType EVT, LoadedVT;
1560 if (N1C->getValue() == 255)
1561 EVT = MVT::i8;
1562 else if (N1C->getValue() == 65535)
1563 EVT = MVT::i16;
1564 else if (N1C->getValue() == ~0U)
1565 EVT = MVT::i32;
1566 else
1567 EVT = MVT::Other;
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001568
Evan Cheng2e49f092006-10-11 07:10:22 +00001569 LoadedVT = LN0->getLoadedVT();
Evan Cheng466685d2006-10-09 20:57:25 +00001570 if (EVT != MVT::Other && LoadedVT > EVT &&
1571 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
1572 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
1573 // For big endian targets, we need to add an offset to the pointer to
1574 // load the correct bytes. For little endian systems, we merely need to
1575 // read fewer bytes from the same pointer.
1576 unsigned PtrOff =
1577 (MVT::getSizeInBits(LoadedVT) - MVT::getSizeInBits(EVT)) / 8;
1578 SDOperand NewPtr = LN0->getBasePtr();
1579 if (!TLI.isLittleEndian())
1580 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
1581 DAG.getConstant(PtrOff, PtrType));
1582 AddToWorkList(NewPtr.Val);
1583 SDOperand Load =
1584 DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00001585 LN0->getSrcValue(), LN0->getSrcValueOffset(), EVT,
1586 LN0->isVolatile(), LN0->getAlignment());
Evan Cheng466685d2006-10-09 20:57:25 +00001587 AddToWorkList(N);
1588 CombineTo(N0.Val, Load, Load.getValue(1));
1589 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1590 }
Chris Lattner15045b62006-02-28 06:35:35 +00001591 }
1592 }
1593
Nate Begeman83e75ec2005-09-06 04:43:02 +00001594 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001595}
1596
Nate Begeman83e75ec2005-09-06 04:43:02 +00001597SDOperand DAGCombiner::visitOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001598 SDOperand N0 = N->getOperand(0);
1599 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001600 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001601 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1602 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001603 MVT::ValueType VT = N1.getValueType();
1604 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001605
Dan Gohman7f321562007-06-25 16:23:39 +00001606 // fold vector ops
1607 SDOperand FoldedVOp = SimplifyVBinOp(N);
1608 if (FoldedVOp.Val) return FoldedVOp;
1609
Dan Gohman613e0d82007-07-03 14:03:57 +00001610 // fold (or x, undef) -> -1
Dan Gohmand595b5f2007-07-10 14:20:37 +00001611 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Chris Lattnere094f542007-07-09 16:16:34 +00001612 return DAG.getConstant(~0ULL, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001613 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001614 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001615 return DAG.getNode(ISD::OR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001616 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001617 if (N0C && !N1C)
1618 return DAG.getNode(ISD::OR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001619 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001620 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001621 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001622 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001623 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001624 return N1;
1625 // fold (or x, c) -> c iff (x & ~c) == 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001626 if (N1C &&
Dan Gohmanea859be2007-06-22 14:59:07 +00001627 DAG.MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits))))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001628 return N1;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001629 // reassociate or
1630 SDOperand ROR = ReassociateOps(ISD::OR, N0, N1);
1631 if (ROR.Val != 0)
1632 return ROR;
1633 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
1634 if (N1C && N0.getOpcode() == ISD::AND && N0.Val->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00001635 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00001636 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
1637 return DAG.getNode(ISD::AND, VT, DAG.getNode(ISD::OR, VT, N0.getOperand(0),
1638 N1),
1639 DAG.getConstant(N1C->getValue() | C1->getValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00001640 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001641 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
1642 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1643 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1644 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1645
1646 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1647 MVT::isInteger(LL.getValueType())) {
1648 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
1649 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
1650 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
1651 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
1652 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001653 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001654 return DAG.getSetCC(VT, ORNode, LR, Op1);
1655 }
1656 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
1657 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
1658 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
1659 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
1660 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001661 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001662 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1663 }
1664 }
1665 // canonicalize equivalent to ll == rl
1666 if (LL == RR && LR == RL) {
1667 Op1 = ISD::getSetCCSwappedOperands(Op1);
1668 std::swap(RL, RR);
1669 }
1670 if (LL == RL && LR == RR) {
1671 bool isInteger = MVT::isInteger(LL.getValueType());
1672 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
1673 if (Result != ISD::SETCC_INVALID)
1674 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1675 }
1676 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001677
1678 // Simplify: or (op x...), (op y...) -> (op (or x, y))
1679 if (N0.getOpcode() == N1.getOpcode()) {
1680 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1681 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001682 }
Chris Lattner516b9622006-09-14 20:50:57 +00001683
Chris Lattner1ec72732006-09-14 21:11:37 +00001684 // (X & C1) | (Y & C2) -> (X|Y) & C3 if possible.
1685 if (N0.getOpcode() == ISD::AND &&
1686 N1.getOpcode() == ISD::AND &&
1687 N0.getOperand(1).getOpcode() == ISD::Constant &&
1688 N1.getOperand(1).getOpcode() == ISD::Constant &&
1689 // Don't increase # computations.
1690 (N0.Val->hasOneUse() || N1.Val->hasOneUse())) {
1691 // We can only do this xform if we know that bits from X that are set in C2
1692 // but not in C1 are already zero. Likewise for Y.
1693 uint64_t LHSMask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1694 uint64_t RHSMask = cast<ConstantSDNode>(N1.getOperand(1))->getValue();
1695
Dan Gohmanea859be2007-06-22 14:59:07 +00001696 if (DAG.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) &&
1697 DAG.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) {
Chris Lattner1ec72732006-09-14 21:11:37 +00001698 SDOperand X =DAG.getNode(ISD::OR, VT, N0.getOperand(0), N1.getOperand(0));
1699 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(LHSMask|RHSMask, VT));
1700 }
1701 }
1702
1703
Chris Lattner516b9622006-09-14 20:50:57 +00001704 // See if this is some rotate idiom.
1705 if (SDNode *Rot = MatchRotate(N0, N1))
1706 return SDOperand(Rot, 0);
Chris Lattner35e5c142006-05-05 05:51:50 +00001707
Nate Begeman83e75ec2005-09-06 04:43:02 +00001708 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001709}
1710
Chris Lattner516b9622006-09-14 20:50:57 +00001711
1712/// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present.
1713static bool MatchRotateHalf(SDOperand Op, SDOperand &Shift, SDOperand &Mask) {
1714 if (Op.getOpcode() == ISD::AND) {
Reid Spencer3ed469c2006-11-02 20:25:50 +00001715 if (isa<ConstantSDNode>(Op.getOperand(1))) {
Chris Lattner516b9622006-09-14 20:50:57 +00001716 Mask = Op.getOperand(1);
1717 Op = Op.getOperand(0);
1718 } else {
1719 return false;
1720 }
1721 }
1722
1723 if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) {
1724 Shift = Op;
1725 return true;
1726 }
1727 return false;
1728}
1729
1730
1731// MatchRotate - Handle an 'or' of two operands. If this is one of the many
1732// idioms for rotate, and if the target supports rotation instructions, generate
1733// a rot[lr].
1734SDNode *DAGCombiner::MatchRotate(SDOperand LHS, SDOperand RHS) {
1735 // Must be a legal type. Expanded an promoted things won't work with rotates.
1736 MVT::ValueType VT = LHS.getValueType();
1737 if (!TLI.isTypeLegal(VT)) return 0;
1738
1739 // The target must have at least one rotate flavor.
1740 bool HasROTL = TLI.isOperationLegal(ISD::ROTL, VT);
1741 bool HasROTR = TLI.isOperationLegal(ISD::ROTR, VT);
1742 if (!HasROTL && !HasROTR) return 0;
1743
1744 // Match "(X shl/srl V1) & V2" where V2 may not be present.
1745 SDOperand LHSShift; // The shift.
1746 SDOperand LHSMask; // AND value if any.
1747 if (!MatchRotateHalf(LHS, LHSShift, LHSMask))
1748 return 0; // Not part of a rotate.
1749
1750 SDOperand RHSShift; // The shift.
1751 SDOperand RHSMask; // AND value if any.
1752 if (!MatchRotateHalf(RHS, RHSShift, RHSMask))
1753 return 0; // Not part of a rotate.
1754
1755 if (LHSShift.getOperand(0) != RHSShift.getOperand(0))
1756 return 0; // Not shifting the same value.
1757
1758 if (LHSShift.getOpcode() == RHSShift.getOpcode())
1759 return 0; // Shifts must disagree.
1760
1761 // Canonicalize shl to left side in a shl/srl pair.
1762 if (RHSShift.getOpcode() == ISD::SHL) {
1763 std::swap(LHS, RHS);
1764 std::swap(LHSShift, RHSShift);
1765 std::swap(LHSMask , RHSMask );
1766 }
1767
1768 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Scott Michelc9dc1142007-04-02 21:36:32 +00001769 SDOperand LHSShiftArg = LHSShift.getOperand(0);
1770 SDOperand LHSShiftAmt = LHSShift.getOperand(1);
1771 SDOperand RHSShiftAmt = RHSShift.getOperand(1);
Chris Lattner516b9622006-09-14 20:50:57 +00001772
1773 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
1774 // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2)
Scott Michelc9dc1142007-04-02 21:36:32 +00001775 if (LHSShiftAmt.getOpcode() == ISD::Constant &&
1776 RHSShiftAmt.getOpcode() == ISD::Constant) {
1777 uint64_t LShVal = cast<ConstantSDNode>(LHSShiftAmt)->getValue();
1778 uint64_t RShVal = cast<ConstantSDNode>(RHSShiftAmt)->getValue();
Chris Lattner516b9622006-09-14 20:50:57 +00001779 if ((LShVal + RShVal) != OpSizeInBits)
1780 return 0;
1781
1782 SDOperand Rot;
1783 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00001784 Rot = DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00001785 else
Scott Michelc9dc1142007-04-02 21:36:32 +00001786 Rot = DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00001787
1788 // If there is an AND of either shifted operand, apply it to the result.
1789 if (LHSMask.Val || RHSMask.Val) {
1790 uint64_t Mask = MVT::getIntVTBitMask(VT);
1791
1792 if (LHSMask.Val) {
1793 uint64_t RHSBits = (1ULL << LShVal)-1;
1794 Mask &= cast<ConstantSDNode>(LHSMask)->getValue() | RHSBits;
1795 }
1796 if (RHSMask.Val) {
1797 uint64_t LHSBits = ~((1ULL << (OpSizeInBits-RShVal))-1);
1798 Mask &= cast<ConstantSDNode>(RHSMask)->getValue() | LHSBits;
1799 }
1800
1801 Rot = DAG.getNode(ISD::AND, VT, Rot, DAG.getConstant(Mask, VT));
1802 }
1803
1804 return Rot.Val;
1805 }
1806
1807 // If there is a mask here, and we have a variable shift, we can't be sure
1808 // that we're masking out the right stuff.
1809 if (LHSMask.Val || RHSMask.Val)
1810 return 0;
1811
1812 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
1813 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotr x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00001814 if (RHSShiftAmt.getOpcode() == ISD::SUB &&
1815 LHSShiftAmt == RHSShiftAmt.getOperand(1)) {
Chris Lattner516b9622006-09-14 20:50:57 +00001816 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00001817 dyn_cast<ConstantSDNode>(RHSShiftAmt.getOperand(0))) {
Chris Lattner516b9622006-09-14 20:50:57 +00001818 if (SUBC->getValue() == OpSizeInBits)
1819 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00001820 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
Chris Lattner516b9622006-09-14 20:50:57 +00001821 else
Scott Michelc9dc1142007-04-02 21:36:32 +00001822 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).Val;
Chris Lattner516b9622006-09-14 20:50:57 +00001823 }
1824 }
1825
1826 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
1827 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotl x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00001828 if (LHSShiftAmt.getOpcode() == ISD::SUB &&
1829 RHSShiftAmt == LHSShiftAmt.getOperand(1)) {
Chris Lattner516b9622006-09-14 20:50:57 +00001830 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00001831 dyn_cast<ConstantSDNode>(LHSShiftAmt.getOperand(0))) {
Chris Lattner516b9622006-09-14 20:50:57 +00001832 if (SUBC->getValue() == OpSizeInBits)
1833 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00001834 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
Chris Lattner516b9622006-09-14 20:50:57 +00001835 else
Scott Michelc9dc1142007-04-02 21:36:32 +00001836 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).Val;
1837 }
1838 }
1839
1840 // Look for sign/zext/any-extended cases:
1841 if ((LHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
1842 || LHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
1843 || LHSShiftAmt.getOpcode() == ISD::ANY_EXTEND) &&
1844 (RHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
1845 || RHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
1846 || RHSShiftAmt.getOpcode() == ISD::ANY_EXTEND)) {
1847 SDOperand LExtOp0 = LHSShiftAmt.getOperand(0);
1848 SDOperand RExtOp0 = RHSShiftAmt.getOperand(0);
1849 if (RExtOp0.getOpcode() == ISD::SUB &&
1850 RExtOp0.getOperand(1) == LExtOp0) {
1851 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
1852 // (rotr x, y)
1853 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
1854 // (rotl x, (sub 32, y))
1855 if (ConstantSDNode *SUBC = cast<ConstantSDNode>(RExtOp0.getOperand(0))) {
1856 if (SUBC->getValue() == OpSizeInBits) {
1857 if (HasROTL)
1858 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
1859 else
1860 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).Val;
1861 }
1862 }
1863 } else if (LExtOp0.getOpcode() == ISD::SUB &&
1864 RExtOp0 == LExtOp0.getOperand(1)) {
1865 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext r))) ->
1866 // (rotl x, y)
1867 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext r))) ->
1868 // (rotr x, (sub 32, y))
1869 if (ConstantSDNode *SUBC = cast<ConstantSDNode>(LExtOp0.getOperand(0))) {
1870 if (SUBC->getValue() == OpSizeInBits) {
1871 if (HasROTL)
1872 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, RHSShiftAmt).Val;
1873 else
1874 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
1875 }
1876 }
Chris Lattner516b9622006-09-14 20:50:57 +00001877 }
1878 }
1879
1880 return 0;
1881}
1882
1883
Nate Begeman83e75ec2005-09-06 04:43:02 +00001884SDOperand DAGCombiner::visitXOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001885 SDOperand N0 = N->getOperand(0);
1886 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001887 SDOperand LHS, RHS, CC;
1888 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1889 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001890 MVT::ValueType VT = N0.getValueType();
1891
Dan Gohman7f321562007-06-25 16:23:39 +00001892 // fold vector ops
1893 SDOperand FoldedVOp = SimplifyVBinOp(N);
1894 if (FoldedVOp.Val) return FoldedVOp;
1895
Dan Gohman613e0d82007-07-03 14:03:57 +00001896 // fold (xor x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00001897 if (N0.getOpcode() == ISD::UNDEF)
1898 return N0;
1899 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001900 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001901 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001902 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001903 return DAG.getNode(ISD::XOR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001904 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001905 if (N0C && !N1C)
1906 return DAG.getNode(ISD::XOR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001907 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001908 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001909 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001910 // reassociate xor
1911 SDOperand RXOR = ReassociateOps(ISD::XOR, N0, N1);
1912 if (RXOR.Val != 0)
1913 return RXOR;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001914 // fold !(x cc y) -> (x !cc y)
Nate Begeman646d7e22005-09-02 21:18:40 +00001915 if (N1C && N1C->getValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
1916 bool isInt = MVT::isInteger(LHS.getValueType());
1917 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
1918 isInt);
1919 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001920 return DAG.getSetCC(VT, LHS, RHS, NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00001921 if (N0.getOpcode() == ISD::SELECT_CC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001922 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00001923 assert(0 && "Unhandled SetCC Equivalent!");
1924 abort();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001925 }
Nate Begeman99801192005-09-07 23:25:52 +00001926 // fold !(x or y) -> (!x and !y) iff x or y are setcc
Chris Lattner734c91d2006-11-10 21:37:15 +00001927 if (N1C && N1C->getValue() == 1 && VT == MVT::i1 &&
Nate Begeman99801192005-09-07 23:25:52 +00001928 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001929 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00001930 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
1931 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001932 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1933 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00001934 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00001935 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001936 }
1937 }
Nate Begeman99801192005-09-07 23:25:52 +00001938 // fold !(x or y) -> (!x and !y) iff x or y are constants
1939 if (N1C && N1C->isAllOnesValue() &&
1940 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001941 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00001942 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
1943 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001944 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1945 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00001946 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00001947 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001948 }
1949 }
Nate Begeman223df222005-09-08 20:18:10 +00001950 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
1951 if (N1C && N0.getOpcode() == ISD::XOR) {
1952 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
1953 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
1954 if (N00C)
1955 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
1956 DAG.getConstant(N1C->getValue()^N00C->getValue(), VT));
1957 if (N01C)
1958 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
1959 DAG.getConstant(N1C->getValue()^N01C->getValue(), VT));
1960 }
1961 // fold (xor x, x) -> 0
Chris Lattner4fbdd592006-03-28 19:11:05 +00001962 if (N0 == N1) {
1963 if (!MVT::isVector(VT)) {
1964 return DAG.getConstant(0, VT);
1965 } else if (!AfterLegalize || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) {
1966 // Produce a vector of zeros.
Dan Gohman51eaa862007-06-14 22:58:02 +00001967 SDOperand El = DAG.getConstant(0, MVT::getVectorElementType(VT));
Chris Lattner4fbdd592006-03-28 19:11:05 +00001968 std::vector<SDOperand> Ops(MVT::getVectorNumElements(VT), El);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001969 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4fbdd592006-03-28 19:11:05 +00001970 }
1971 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001972
1973 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
1974 if (N0.getOpcode() == N1.getOpcode()) {
1975 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1976 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001977 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001978
Chris Lattner3e104b12006-04-08 04:15:24 +00001979 // Simplify the expression using non-local knowledge.
1980 if (!MVT::isVector(VT) &&
1981 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001982 return SDOperand(N, 0);
Chris Lattner3e104b12006-04-08 04:15:24 +00001983
Nate Begeman83e75ec2005-09-06 04:43:02 +00001984 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001985}
1986
Nate Begeman83e75ec2005-09-06 04:43:02 +00001987SDOperand DAGCombiner::visitSHL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001988 SDOperand N0 = N->getOperand(0);
1989 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001990 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1991 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001992 MVT::ValueType VT = N0.getValueType();
1993 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1994
1995 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001996 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001997 return DAG.getNode(ISD::SHL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001998 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001999 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002000 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002001 // fold (shl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00002002 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002003 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002004 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002005 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002006 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002007 // if (shl x, c) is known to be zero, return 0
Dan Gohmanea859be2007-06-22 14:59:07 +00002008 if (DAG.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002009 return DAG.getConstant(0, VT);
Chris Lattner61a4c072007-04-18 03:06:49 +00002010 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00002011 return SDOperand(N, 0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002012 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002013 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002014 N0.getOperand(1).getOpcode() == ISD::Constant) {
2015 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00002016 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002017 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002018 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002019 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002020 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002021 }
2022 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
2023 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002024 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002025 N0.getOperand(1).getOpcode() == ISD::Constant) {
2026 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00002027 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002028 SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
2029 DAG.getConstant(~0ULL << c1, VT));
2030 if (c2 > c1)
2031 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +00002032 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002033 else
Nate Begeman83e75ec2005-09-06 04:43:02 +00002034 return DAG.getNode(ISD::SRL, VT, Mask,
2035 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002036 }
2037 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00002038 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +00002039 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002040 DAG.getConstant(~0ULL << N1C->getValue(), VT));
2041 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002042}
2043
Nate Begeman83e75ec2005-09-06 04:43:02 +00002044SDOperand DAGCombiner::visitSRA(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002045 SDOperand N0 = N->getOperand(0);
2046 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002047 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2048 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002049 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002050
2051 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002052 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002053 return DAG.getNode(ISD::SRA, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002054 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002055 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002056 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002057 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00002058 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002059 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002060 // fold (sra x, c >= size(x)) -> undef
Nate Begemanfb7217b2006-02-17 19:54:08 +00002061 if (N1C && N1C->getValue() >= MVT::getSizeInBits(VT))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002062 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002063 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002064 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002065 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00002066 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
2067 // sext_inreg.
2068 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
2069 unsigned LowBits = MVT::getSizeInBits(VT) - (unsigned)N1C->getValue();
2070 MVT::ValueType EVT;
2071 switch (LowBits) {
2072 default: EVT = MVT::Other; break;
2073 case 1: EVT = MVT::i1; break;
2074 case 8: EVT = MVT::i8; break;
2075 case 16: EVT = MVT::i16; break;
2076 case 32: EVT = MVT::i32; break;
2077 }
2078 if (EVT > MVT::Other && TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, EVT))
2079 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
2080 DAG.getValueType(EVT));
2081 }
Chris Lattner71d9ebc2006-02-28 06:23:04 +00002082
2083 // fold (sra (sra x, c1), c2) -> (sra x, c1+c2)
2084 if (N1C && N0.getOpcode() == ISD::SRA) {
2085 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2086 unsigned Sum = N1C->getValue() + C1->getValue();
2087 if (Sum >= MVT::getSizeInBits(VT)) Sum = MVT::getSizeInBits(VT)-1;
2088 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0),
2089 DAG.getConstant(Sum, N1C->getValueType(0)));
2090 }
2091 }
2092
Chris Lattnera8504462006-05-08 20:51:54 +00002093 // Simplify, based on bits shifted out of the LHS.
2094 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
2095 return SDOperand(N, 0);
2096
2097
Nate Begeman1d4d4142005-09-01 00:19:25 +00002098 // If the sign bit is known to be zero, switch this to a SRL.
Dan Gohmanea859be2007-06-22 14:59:07 +00002099 if (DAG.MaskedValueIsZero(N0, MVT::getIntVTSignBit(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002100 return DAG.getNode(ISD::SRL, VT, N0, N1);
2101 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002102}
2103
Nate Begeman83e75ec2005-09-06 04:43:02 +00002104SDOperand DAGCombiner::visitSRL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002105 SDOperand N0 = N->getOperand(0);
2106 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002107 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2108 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002109 MVT::ValueType VT = N0.getValueType();
2110 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
2111
2112 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002113 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002114 return DAG.getNode(ISD::SRL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002115 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002116 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002117 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002118 // fold (srl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00002119 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002120 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002121 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002122 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002123 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002124 // if (srl x, c) is known to be zero, return 0
Dan Gohmanea859be2007-06-22 14:59:07 +00002125 if (N1C && DAG.MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002126 return DAG.getConstant(0, VT);
Chris Lattnerec06e9a2007-04-18 03:05:22 +00002127
Nate Begeman1d4d4142005-09-01 00:19:25 +00002128 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002129 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002130 N0.getOperand(1).getOpcode() == ISD::Constant) {
2131 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00002132 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002133 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002134 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002135 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002136 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002137 }
Chris Lattner350bec02006-04-02 06:11:11 +00002138
Chris Lattner06afe072006-05-05 22:53:17 +00002139 // fold (srl (anyextend x), c) -> (anyextend (srl x, c))
2140 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
2141 // Shifting in all undef bits?
2142 MVT::ValueType SmallVT = N0.getOperand(0).getValueType();
2143 if (N1C->getValue() >= MVT::getSizeInBits(SmallVT))
2144 return DAG.getNode(ISD::UNDEF, VT);
2145
2146 SDOperand SmallShift = DAG.getNode(ISD::SRL, SmallVT, N0.getOperand(0), N1);
2147 AddToWorkList(SmallShift.Val);
2148 return DAG.getNode(ISD::ANY_EXTEND, VT, SmallShift);
2149 }
2150
Chris Lattner3657ffe2006-10-12 20:23:19 +00002151 // fold (srl (sra X, Y), 31) -> (srl X, 31). This srl only looks at the sign
2152 // bit, which is unmodified by sra.
2153 if (N1C && N1C->getValue()+1 == MVT::getSizeInBits(VT)) {
2154 if (N0.getOpcode() == ISD::SRA)
2155 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0), N1);
2156 }
2157
Chris Lattner350bec02006-04-02 06:11:11 +00002158 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
2159 if (N1C && N0.getOpcode() == ISD::CTLZ &&
2160 N1C->getValue() == Log2_32(MVT::getSizeInBits(VT))) {
2161 uint64_t KnownZero, KnownOne, Mask = MVT::getIntVTBitMask(VT);
Dan Gohmanea859be2007-06-22 14:59:07 +00002162 DAG.ComputeMaskedBits(N0.getOperand(0), Mask, KnownZero, KnownOne);
Chris Lattner350bec02006-04-02 06:11:11 +00002163
2164 // If any of the input bits are KnownOne, then the input couldn't be all
2165 // zeros, thus the result of the srl will always be zero.
2166 if (KnownOne) return DAG.getConstant(0, VT);
2167
2168 // If all of the bits input the to ctlz node are known to be zero, then
2169 // the result of the ctlz is "32" and the result of the shift is one.
2170 uint64_t UnknownBits = ~KnownZero & Mask;
2171 if (UnknownBits == 0) return DAG.getConstant(1, VT);
2172
2173 // Otherwise, check to see if there is exactly one bit input to the ctlz.
2174 if ((UnknownBits & (UnknownBits-1)) == 0) {
2175 // Okay, we know that only that the single bit specified by UnknownBits
2176 // could be set on input to the CTLZ node. If this bit is set, the SRL
2177 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
2178 // to an SRL,XOR pair, which is likely to simplify more.
2179 unsigned ShAmt = CountTrailingZeros_64(UnknownBits);
2180 SDOperand Op = N0.getOperand(0);
2181 if (ShAmt) {
2182 Op = DAG.getNode(ISD::SRL, VT, Op,
2183 DAG.getConstant(ShAmt, TLI.getShiftAmountTy()));
2184 AddToWorkList(Op.Val);
2185 }
2186 return DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(1, VT));
2187 }
2188 }
Chris Lattner61a4c072007-04-18 03:06:49 +00002189
2190 // fold operands of srl based on knowledge that the low bits are not
2191 // demanded.
2192 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
2193 return SDOperand(N, 0);
2194
Nate Begeman83e75ec2005-09-06 04:43:02 +00002195 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002196}
2197
Nate Begeman83e75ec2005-09-06 04:43:02 +00002198SDOperand DAGCombiner::visitCTLZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002199 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00002200 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002201
2202 // fold (ctlz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002203 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002204 return DAG.getNode(ISD::CTLZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002205 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002206}
2207
Nate Begeman83e75ec2005-09-06 04:43:02 +00002208SDOperand DAGCombiner::visitCTTZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002209 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00002210 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002211
2212 // fold (cttz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002213 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002214 return DAG.getNode(ISD::CTTZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002215 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002216}
2217
Nate Begeman83e75ec2005-09-06 04:43:02 +00002218SDOperand DAGCombiner::visitCTPOP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002219 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00002220 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002221
2222 // fold (ctpop c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002223 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002224 return DAG.getNode(ISD::CTPOP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002225 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002226}
2227
Nate Begeman452d7be2005-09-16 00:54:12 +00002228SDOperand DAGCombiner::visitSELECT(SDNode *N) {
2229 SDOperand N0 = N->getOperand(0);
2230 SDOperand N1 = N->getOperand(1);
2231 SDOperand N2 = N->getOperand(2);
2232 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2233 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2234 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
2235 MVT::ValueType VT = N->getValueType(0);
Nate Begeman44728a72005-09-19 22:34:01 +00002236
Nate Begeman452d7be2005-09-16 00:54:12 +00002237 // fold select C, X, X -> X
2238 if (N1 == N2)
2239 return N1;
2240 // fold select true, X, Y -> X
2241 if (N0C && !N0C->isNullValue())
2242 return N1;
2243 // fold select false, X, Y -> Y
2244 if (N0C && N0C->isNullValue())
2245 return N2;
2246 // fold select C, 1, X -> C | X
Nate Begeman44728a72005-09-19 22:34:01 +00002247 if (MVT::i1 == VT && N1C && N1C->getValue() == 1)
Nate Begeman452d7be2005-09-16 00:54:12 +00002248 return DAG.getNode(ISD::OR, VT, N0, N2);
2249 // fold select C, 0, X -> ~C & X
2250 // FIXME: this should check for C type == X type, not i1?
2251 if (MVT::i1 == VT && N1C && N1C->isNullValue()) {
2252 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00002253 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00002254 return DAG.getNode(ISD::AND, VT, XORNode, N2);
2255 }
2256 // fold select C, X, 1 -> ~C | X
Nate Begeman44728a72005-09-19 22:34:01 +00002257 if (MVT::i1 == VT && N2C && N2C->getValue() == 1) {
Nate Begeman452d7be2005-09-16 00:54:12 +00002258 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00002259 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00002260 return DAG.getNode(ISD::OR, VT, XORNode, N1);
2261 }
2262 // fold select C, X, 0 -> C & X
2263 // FIXME: this should check for C type == X type, not i1?
2264 if (MVT::i1 == VT && N2C && N2C->isNullValue())
2265 return DAG.getNode(ISD::AND, VT, N0, N1);
2266 // fold X ? X : Y --> X ? 1 : Y --> X | Y
2267 if (MVT::i1 == VT && N0 == N1)
2268 return DAG.getNode(ISD::OR, VT, N0, N2);
2269 // fold X ? Y : X --> X ? Y : 0 --> X & Y
2270 if (MVT::i1 == VT && N0 == N2)
2271 return DAG.getNode(ISD::AND, VT, N0, N1);
Chris Lattner729c6d12006-05-27 00:43:02 +00002272
Chris Lattner40c62d52005-10-18 06:04:22 +00002273 // If we can fold this based on the true/false value, do so.
2274 if (SimplifySelectOps(N, N1, N2))
Chris Lattner729c6d12006-05-27 00:43:02 +00002275 return SDOperand(N, 0); // Don't revisit N.
2276
Nate Begeman44728a72005-09-19 22:34:01 +00002277 // fold selects based on a setcc into other things, such as min/max/abs
2278 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman750ac1b2006-02-01 07:19:44 +00002279 // FIXME:
2280 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
2281 // having to say they don't support SELECT_CC on every type the DAG knows
2282 // about, since there is no way to mark an opcode illegal at all value types
2283 if (TLI.isOperationLegal(ISD::SELECT_CC, MVT::Other))
2284 return DAG.getNode(ISD::SELECT_CC, VT, N0.getOperand(0), N0.getOperand(1),
2285 N1, N2, N0.getOperand(2));
2286 else
2287 return SimplifySelect(N0, N1, N2);
Nate Begeman452d7be2005-09-16 00:54:12 +00002288 return SDOperand();
2289}
2290
2291SDOperand DAGCombiner::visitSELECT_CC(SDNode *N) {
Nate Begeman44728a72005-09-19 22:34:01 +00002292 SDOperand N0 = N->getOperand(0);
2293 SDOperand N1 = N->getOperand(1);
2294 SDOperand N2 = N->getOperand(2);
2295 SDOperand N3 = N->getOperand(3);
2296 SDOperand N4 = N->getOperand(4);
Nate Begeman44728a72005-09-19 22:34:01 +00002297 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
2298
Nate Begeman44728a72005-09-19 22:34:01 +00002299 // fold select_cc lhs, rhs, x, x, cc -> x
2300 if (N2 == N3)
2301 return N2;
Chris Lattner40c62d52005-10-18 06:04:22 +00002302
Chris Lattner5f42a242006-09-20 06:19:26 +00002303 // Determine if the condition we're dealing with is constant
2304 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
Chris Lattner30f73e72006-10-14 03:52:46 +00002305 if (SCC.Val) AddToWorkList(SCC.Val);
Chris Lattner5f42a242006-09-20 06:19:26 +00002306
2307 if (ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val)) {
2308 if (SCCC->getValue())
2309 return N2; // cond always true -> true val
2310 else
2311 return N3; // cond always false -> false val
2312 }
2313
2314 // Fold to a simpler select_cc
2315 if (SCC.Val && SCC.getOpcode() == ISD::SETCC)
2316 return DAG.getNode(ISD::SELECT_CC, N2.getValueType(),
2317 SCC.getOperand(0), SCC.getOperand(1), N2, N3,
2318 SCC.getOperand(2));
2319
Chris Lattner40c62d52005-10-18 06:04:22 +00002320 // If we can fold this based on the true/false value, do so.
2321 if (SimplifySelectOps(N, N2, N3))
Chris Lattner729c6d12006-05-27 00:43:02 +00002322 return SDOperand(N, 0); // Don't revisit N.
Chris Lattner40c62d52005-10-18 06:04:22 +00002323
Nate Begeman44728a72005-09-19 22:34:01 +00002324 // fold select_cc into other things, such as min/max/abs
2325 return SimplifySelectCC(N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00002326}
2327
2328SDOperand DAGCombiner::visitSETCC(SDNode *N) {
2329 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
2330 cast<CondCodeSDNode>(N->getOperand(2))->get());
2331}
2332
Nate Begeman83e75ec2005-09-06 04:43:02 +00002333SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002334 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002335 MVT::ValueType VT = N->getValueType(0);
2336
Nate Begeman1d4d4142005-09-01 00:19:25 +00002337 // fold (sext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00002338 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002339 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0);
Chris Lattner310b5782006-05-06 23:06:26 +00002340
Nate Begeman1d4d4142005-09-01 00:19:25 +00002341 // fold (sext (sext x)) -> (sext x)
Chris Lattner310b5782006-05-06 23:06:26 +00002342 // fold (sext (aext x)) -> (sext x)
2343 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002344 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
Chris Lattner310b5782006-05-06 23:06:26 +00002345
Evan Chengc88138f2007-03-22 01:54:19 +00002346 // fold (sext (truncate (load x))) -> (sext (smaller load x))
2347 // fold (sext (truncate (srl (load x), c))) -> (sext (smaller load (x+c/n)))
Chris Lattner22558872007-02-26 03:13:59 +00002348 if (N0.getOpcode() == ISD::TRUNCATE) {
Evan Chengc88138f2007-03-22 01:54:19 +00002349 SDOperand NarrowLoad = ReduceLoadWidth(N0.Val);
Evan Cheng0b063de2007-03-23 02:16:52 +00002350 if (NarrowLoad.Val) {
2351 if (NarrowLoad.Val != N0.Val)
2352 CombineTo(N0.Val, NarrowLoad);
2353 return DAG.getNode(ISD::SIGN_EXTEND, VT, NarrowLoad);
2354 }
Evan Chengc88138f2007-03-22 01:54:19 +00002355 }
2356
2357 // See if the value being truncated is already sign extended. If so, just
2358 // eliminate the trunc/sext pair.
2359 if (N0.getOpcode() == ISD::TRUNCATE) {
Chris Lattner6007b842006-09-21 06:00:20 +00002360 SDOperand Op = N0.getOperand(0);
Chris Lattner22558872007-02-26 03:13:59 +00002361 unsigned OpBits = MVT::getSizeInBits(Op.getValueType());
2362 unsigned MidBits = MVT::getSizeInBits(N0.getValueType());
2363 unsigned DestBits = MVT::getSizeInBits(VT);
Dan Gohmanea859be2007-06-22 14:59:07 +00002364 unsigned NumSignBits = DAG.ComputeNumSignBits(Op);
Chris Lattner22558872007-02-26 03:13:59 +00002365
2366 if (OpBits == DestBits) {
2367 // Op is i32, Mid is i8, and Dest is i32. If Op has more than 24 sign
2368 // bits, it is already ready.
2369 if (NumSignBits > DestBits-MidBits)
2370 return Op;
2371 } else if (OpBits < DestBits) {
2372 // Op is i32, Mid is i8, and Dest is i64. If Op has more than 24 sign
2373 // bits, just sext from i32.
2374 if (NumSignBits > OpBits-MidBits)
2375 return DAG.getNode(ISD::SIGN_EXTEND, VT, Op);
2376 } else {
2377 // Op is i64, Mid is i8, and Dest is i32. If Op has more than 56 sign
2378 // bits, just truncate to i32.
2379 if (NumSignBits > OpBits-MidBits)
2380 return DAG.getNode(ISD::TRUNCATE, VT, Op);
Chris Lattner6007b842006-09-21 06:00:20 +00002381 }
Chris Lattner22558872007-02-26 03:13:59 +00002382
2383 // fold (sext (truncate x)) -> (sextinreg x).
2384 if (!AfterLegalize || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG,
2385 N0.getValueType())) {
2386 if (Op.getValueType() < VT)
2387 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
2388 else if (Op.getValueType() > VT)
2389 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
2390 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, Op,
2391 DAG.getValueType(N0.getValueType()));
2392 }
Chris Lattner6007b842006-09-21 06:00:20 +00002393 }
Chris Lattner310b5782006-05-06 23:06:26 +00002394
Evan Cheng110dec22005-12-14 02:19:23 +00002395 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00002396 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00002397 (!AfterLegalize||TLI.isLoadXLegal(ISD::SEXTLOAD, N0.getValueType()))){
Evan Cheng466685d2006-10-09 20:57:25 +00002398 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2399 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2400 LN0->getBasePtr(), LN0->getSrcValue(),
2401 LN0->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002402 N0.getValueType(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002403 LN0->isVolatile(),
2404 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00002405 CombineTo(N, ExtLoad);
Chris Lattnerf9884052005-10-13 21:52:31 +00002406 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2407 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002408 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begeman3df4d522005-10-12 20:40:40 +00002409 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002410
2411 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
2412 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00002413 if ((ISD::isSEXTLoad(N0.Val) || ISD::isEXTLoad(N0.Val)) &&
2414 ISD::isUNINDEXEDLoad(N0.Val) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002415 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00002416 MVT::ValueType EVT = LN0->getLoadedVT();
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00002417 if (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT)) {
2418 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2419 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002420 LN0->getSrcValueOffset(), EVT,
2421 LN0->isVolatile(),
2422 LN0->getAlignment());
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00002423 CombineTo(N, ExtLoad);
2424 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2425 ExtLoad.getValue(1));
2426 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2427 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002428 }
2429
Chris Lattner20a35c32007-04-11 05:32:27 +00002430 // sext(setcc x,y,cc) -> select_cc x, y, -1, 0, cc
2431 if (N0.getOpcode() == ISD::SETCC) {
2432 SDOperand SCC =
Chris Lattner1eba01e2007-04-11 06:50:51 +00002433 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
2434 DAG.getConstant(~0ULL, VT), DAG.getConstant(0, VT),
2435 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
2436 if (SCC.Val) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00002437 }
2438
Nate Begeman83e75ec2005-09-06 04:43:02 +00002439 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002440}
2441
Nate Begeman83e75ec2005-09-06 04:43:02 +00002442SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002443 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002444 MVT::ValueType VT = N->getValueType(0);
2445
Nate Begeman1d4d4142005-09-01 00:19:25 +00002446 // fold (zext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00002447 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002448 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002449 // fold (zext (zext x)) -> (zext x)
Chris Lattner310b5782006-05-06 23:06:26 +00002450 // fold (zext (aext x)) -> (zext x)
2451 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002452 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
Chris Lattner6007b842006-09-21 06:00:20 +00002453
Evan Chengc88138f2007-03-22 01:54:19 +00002454 // fold (zext (truncate (load x))) -> (zext (smaller load x))
2455 // fold (zext (truncate (srl (load x), c))) -> (zext (small load (x+c/n)))
Dale Johannesen2041a0e2007-03-30 21:38:07 +00002456 if (N0.getOpcode() == ISD::TRUNCATE) {
Evan Chengc88138f2007-03-22 01:54:19 +00002457 SDOperand NarrowLoad = ReduceLoadWidth(N0.Val);
Evan Cheng0b063de2007-03-23 02:16:52 +00002458 if (NarrowLoad.Val) {
2459 if (NarrowLoad.Val != N0.Val)
2460 CombineTo(N0.Val, NarrowLoad);
2461 return DAG.getNode(ISD::ZERO_EXTEND, VT, NarrowLoad);
2462 }
Evan Chengc88138f2007-03-22 01:54:19 +00002463 }
2464
Chris Lattner6007b842006-09-21 06:00:20 +00002465 // fold (zext (truncate x)) -> (and x, mask)
2466 if (N0.getOpcode() == ISD::TRUNCATE &&
2467 (!AfterLegalize || TLI.isOperationLegal(ISD::AND, VT))) {
2468 SDOperand Op = N0.getOperand(0);
2469 if (Op.getValueType() < VT) {
2470 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
2471 } else if (Op.getValueType() > VT) {
2472 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
2473 }
2474 return DAG.getZeroExtendInReg(Op, N0.getValueType());
2475 }
2476
Chris Lattner111c2282006-09-21 06:14:31 +00002477 // fold (zext (and (trunc x), cst)) -> (and x, cst).
2478 if (N0.getOpcode() == ISD::AND &&
2479 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
2480 N0.getOperand(1).getOpcode() == ISD::Constant) {
2481 SDOperand X = N0.getOperand(0).getOperand(0);
2482 if (X.getValueType() < VT) {
2483 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
2484 } else if (X.getValueType() > VT) {
2485 X = DAG.getNode(ISD::TRUNCATE, VT, X);
2486 }
2487 uint64_t Mask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
2488 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
2489 }
2490
Evan Cheng110dec22005-12-14 02:19:23 +00002491 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00002492 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00002493 (!AfterLegalize||TLI.isLoadXLegal(ISD::ZEXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00002494 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2495 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
2496 LN0->getBasePtr(), LN0->getSrcValue(),
2497 LN0->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002498 N0.getValueType(),
2499 LN0->isVolatile(),
2500 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00002501 CombineTo(N, ExtLoad);
Evan Cheng110dec22005-12-14 02:19:23 +00002502 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2503 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002504 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng110dec22005-12-14 02:19:23 +00002505 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002506
2507 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
2508 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00002509 if ((ISD::isZEXTLoad(N0.Val) || ISD::isEXTLoad(N0.Val)) &&
2510 ISD::isUNINDEXEDLoad(N0.Val) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002511 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00002512 MVT::ValueType EVT = LN0->getLoadedVT();
Evan Cheng466685d2006-10-09 20:57:25 +00002513 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
2514 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002515 LN0->getSrcValueOffset(), EVT,
2516 LN0->isVolatile(),
2517 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00002518 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002519 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2520 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002521 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002522 }
Chris Lattner20a35c32007-04-11 05:32:27 +00002523
2524 // zext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
2525 if (N0.getOpcode() == ISD::SETCC) {
2526 SDOperand SCC =
2527 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
2528 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattner1eba01e2007-04-11 06:50:51 +00002529 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
2530 if (SCC.Val) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00002531 }
2532
Nate Begeman83e75ec2005-09-06 04:43:02 +00002533 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002534}
2535
Chris Lattner5ffc0662006-05-05 05:58:59 +00002536SDOperand DAGCombiner::visitANY_EXTEND(SDNode *N) {
2537 SDOperand N0 = N->getOperand(0);
Chris Lattner5ffc0662006-05-05 05:58:59 +00002538 MVT::ValueType VT = N->getValueType(0);
2539
2540 // fold (aext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00002541 if (isa<ConstantSDNode>(N0))
Chris Lattner5ffc0662006-05-05 05:58:59 +00002542 return DAG.getNode(ISD::ANY_EXTEND, VT, N0);
2543 // fold (aext (aext x)) -> (aext x)
2544 // fold (aext (zext x)) -> (zext x)
2545 // fold (aext (sext x)) -> (sext x)
2546 if (N0.getOpcode() == ISD::ANY_EXTEND ||
2547 N0.getOpcode() == ISD::ZERO_EXTEND ||
2548 N0.getOpcode() == ISD::SIGN_EXTEND)
2549 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
2550
Evan Chengc88138f2007-03-22 01:54:19 +00002551 // fold (aext (truncate (load x))) -> (aext (smaller load x))
2552 // fold (aext (truncate (srl (load x), c))) -> (aext (small load (x+c/n)))
2553 if (N0.getOpcode() == ISD::TRUNCATE) {
2554 SDOperand NarrowLoad = ReduceLoadWidth(N0.Val);
Evan Cheng0b063de2007-03-23 02:16:52 +00002555 if (NarrowLoad.Val) {
2556 if (NarrowLoad.Val != N0.Val)
2557 CombineTo(N0.Val, NarrowLoad);
2558 return DAG.getNode(ISD::ANY_EXTEND, VT, NarrowLoad);
2559 }
Evan Chengc88138f2007-03-22 01:54:19 +00002560 }
2561
Chris Lattner84750582006-09-20 06:29:17 +00002562 // fold (aext (truncate x))
2563 if (N0.getOpcode() == ISD::TRUNCATE) {
2564 SDOperand TruncOp = N0.getOperand(0);
2565 if (TruncOp.getValueType() == VT)
2566 return TruncOp; // x iff x size == zext size.
2567 if (TruncOp.getValueType() > VT)
2568 return DAG.getNode(ISD::TRUNCATE, VT, TruncOp);
2569 return DAG.getNode(ISD::ANY_EXTEND, VT, TruncOp);
2570 }
Chris Lattner0e4b9222006-09-21 06:40:43 +00002571
2572 // fold (aext (and (trunc x), cst)) -> (and x, cst).
2573 if (N0.getOpcode() == ISD::AND &&
2574 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
2575 N0.getOperand(1).getOpcode() == ISD::Constant) {
2576 SDOperand X = N0.getOperand(0).getOperand(0);
2577 if (X.getValueType() < VT) {
2578 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
2579 } else if (X.getValueType() > VT) {
2580 X = DAG.getNode(ISD::TRUNCATE, VT, X);
2581 }
2582 uint64_t Mask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
2583 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
2584 }
2585
Chris Lattner5ffc0662006-05-05 05:58:59 +00002586 // fold (aext (load x)) -> (aext (truncate (extload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00002587 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00002588 (!AfterLegalize||TLI.isLoadXLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00002589 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2590 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
2591 LN0->getBasePtr(), LN0->getSrcValue(),
2592 LN0->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002593 N0.getValueType(),
2594 LN0->isVolatile(),
2595 LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00002596 CombineTo(N, ExtLoad);
2597 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2598 ExtLoad.getValue(1));
2599 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2600 }
2601
2602 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
2603 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
2604 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00002605 if (N0.getOpcode() == ISD::LOAD &&
2606 !ISD::isNON_EXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val) &&
Evan Cheng466685d2006-10-09 20:57:25 +00002607 N0.hasOneUse()) {
2608 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00002609 MVT::ValueType EVT = LN0->getLoadedVT();
Evan Cheng466685d2006-10-09 20:57:25 +00002610 SDOperand ExtLoad = DAG.getExtLoad(LN0->getExtensionType(), VT,
2611 LN0->getChain(), LN0->getBasePtr(),
2612 LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002613 LN0->getSrcValueOffset(), EVT,
2614 LN0->isVolatile(),
2615 LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00002616 CombineTo(N, ExtLoad);
2617 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2618 ExtLoad.getValue(1));
2619 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2620 }
Chris Lattner20a35c32007-04-11 05:32:27 +00002621
2622 // aext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
2623 if (N0.getOpcode() == ISD::SETCC) {
2624 SDOperand SCC =
Chris Lattner1eba01e2007-04-11 06:50:51 +00002625 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
2626 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattnerc24bbad2007-04-11 16:51:53 +00002627 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Chris Lattner1eba01e2007-04-11 06:50:51 +00002628 if (SCC.Val)
Chris Lattnerc56a81d2007-04-11 06:43:25 +00002629 return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00002630 }
2631
Chris Lattner5ffc0662006-05-05 05:58:59 +00002632 return SDOperand();
2633}
2634
Evan Chengc88138f2007-03-22 01:54:19 +00002635/// ReduceLoadWidth - If the result of a wider load is shifted to right of N
2636/// bits and then truncated to a narrower type and where N is a multiple
2637/// of number of bits of the narrower type, transform it to a narrower load
2638/// from address + N / num of bits of new type. If the result is to be
2639/// extended, also fold the extension to form a extending load.
2640SDOperand DAGCombiner::ReduceLoadWidth(SDNode *N) {
2641 unsigned Opc = N->getOpcode();
2642 ISD::LoadExtType ExtType = ISD::NON_EXTLOAD;
2643 SDOperand N0 = N->getOperand(0);
2644 MVT::ValueType VT = N->getValueType(0);
2645 MVT::ValueType EVT = N->getValueType(0);
2646
Evan Chenge177e302007-03-23 22:13:36 +00002647 // Special case: SIGN_EXTEND_INREG is basically truncating to EVT then
2648 // extended to VT.
Evan Chengc88138f2007-03-22 01:54:19 +00002649 if (Opc == ISD::SIGN_EXTEND_INREG) {
2650 ExtType = ISD::SEXTLOAD;
2651 EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Evan Chenge177e302007-03-23 22:13:36 +00002652 if (AfterLegalize && !TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))
2653 return SDOperand();
Evan Chengc88138f2007-03-22 01:54:19 +00002654 }
2655
2656 unsigned EVTBits = MVT::getSizeInBits(EVT);
2657 unsigned ShAmt = 0;
Evan Chengb37b80c2007-03-23 20:55:21 +00002658 bool CombineSRL = false;
Evan Chengc88138f2007-03-22 01:54:19 +00002659 if (N0.getOpcode() == ISD::SRL && N0.hasOneUse()) {
2660 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2661 ShAmt = N01->getValue();
2662 // Is the shift amount a multiple of size of VT?
2663 if ((ShAmt & (EVTBits-1)) == 0) {
2664 N0 = N0.getOperand(0);
2665 if (MVT::getSizeInBits(N0.getValueType()) <= EVTBits)
2666 return SDOperand();
Evan Chengb37b80c2007-03-23 20:55:21 +00002667 CombineSRL = true;
Evan Chengc88138f2007-03-22 01:54:19 +00002668 }
2669 }
2670 }
2671
2672 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
2673 // Do not allow folding to i1 here. i1 is implicitly stored in memory in
2674 // zero extended form: by shrinking the load, we lose track of the fact
2675 // that it is already zero extended.
2676 // FIXME: This should be reevaluated.
2677 VT != MVT::i1) {
2678 assert(MVT::getSizeInBits(N0.getValueType()) > EVTBits &&
2679 "Cannot truncate to larger type!");
2680 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2681 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
Evan Chengdae54ce2007-03-24 00:02:43 +00002682 // For big endian targets, we need to adjust the offset to the pointer to
2683 // load the correct bytes.
2684 if (!TLI.isLittleEndian())
2685 ShAmt = MVT::getSizeInBits(N0.getValueType()) - ShAmt - EVTBits;
2686 uint64_t PtrOff = ShAmt / 8;
Evan Chengc88138f2007-03-22 01:54:19 +00002687 SDOperand NewPtr = DAG.getNode(ISD::ADD, PtrType, LN0->getBasePtr(),
2688 DAG.getConstant(PtrOff, PtrType));
2689 AddToWorkList(NewPtr.Val);
2690 SDOperand Load = (ExtType == ISD::NON_EXTLOAD)
2691 ? DAG.getLoad(VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002692 LN0->getSrcValue(), LN0->getSrcValueOffset(),
2693 LN0->isVolatile(), LN0->getAlignment())
Evan Chengc88138f2007-03-22 01:54:19 +00002694 : DAG.getExtLoad(ExtType, VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002695 LN0->getSrcValue(), LN0->getSrcValueOffset(), EVT,
2696 LN0->isVolatile(), LN0->getAlignment());
Evan Chengc88138f2007-03-22 01:54:19 +00002697 AddToWorkList(N);
Evan Chengb37b80c2007-03-23 20:55:21 +00002698 if (CombineSRL) {
2699 std::vector<SDNode*> NowDead;
2700 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), Load.getValue(1), NowDead);
2701 CombineTo(N->getOperand(0).Val, Load);
2702 } else
2703 CombineTo(N0.Val, Load, Load.getValue(1));
Evan Cheng15213b72007-03-26 07:12:51 +00002704 if (ShAmt) {
2705 if (Opc == ISD::SIGN_EXTEND_INREG)
2706 return DAG.getNode(Opc, VT, Load, N->getOperand(1));
2707 else
2708 return DAG.getNode(Opc, VT, Load);
2709 }
Evan Chengc88138f2007-03-22 01:54:19 +00002710 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2711 }
2712
2713 return SDOperand();
2714}
2715
Chris Lattner5ffc0662006-05-05 05:58:59 +00002716
Nate Begeman83e75ec2005-09-06 04:43:02 +00002717SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002718 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002719 SDOperand N1 = N->getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002720 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002721 MVT::ValueType EVT = cast<VTSDNode>(N1)->getVT();
Nate Begeman07ed4172005-10-10 21:26:48 +00002722 unsigned EVTBits = MVT::getSizeInBits(EVT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002723
Nate Begeman1d4d4142005-09-01 00:19:25 +00002724 // fold (sext_in_reg c1) -> c1
Chris Lattnereaeda562006-05-08 20:59:41 +00002725 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
Chris Lattner310b5782006-05-06 23:06:26 +00002726 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0, N1);
Chris Lattneree4ea922006-05-06 09:30:03 +00002727
Chris Lattner541a24f2006-05-06 22:43:44 +00002728 // If the input is already sign extended, just drop the extension.
Dan Gohmanea859be2007-06-22 14:59:07 +00002729 if (DAG.ComputeNumSignBits(N0) >= MVT::getSizeInBits(VT)-EVTBits+1)
Chris Lattneree4ea922006-05-06 09:30:03 +00002730 return N0;
2731
Nate Begeman646d7e22005-09-02 21:18:40 +00002732 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
2733 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
2734 EVT < cast<VTSDNode>(N0.getOperand(1))->getVT()) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00002735 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002736 }
Chris Lattner4b37e872006-05-08 21:18:59 +00002737
Chris Lattner95a5e052007-04-17 19:03:21 +00002738 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is known zero.
Dan Gohmanea859be2007-06-22 14:59:07 +00002739 if (DAG.MaskedValueIsZero(N0, 1ULL << (EVTBits-1)))
Nate Begemande996292006-02-03 22:24:05 +00002740 return DAG.getZeroExtendInReg(N0, EVT);
Chris Lattner4b37e872006-05-08 21:18:59 +00002741
Chris Lattner95a5e052007-04-17 19:03:21 +00002742 // fold operands of sext_in_reg based on knowledge that the top bits are not
2743 // demanded.
2744 if (SimplifyDemandedBits(SDOperand(N, 0)))
2745 return SDOperand(N, 0);
2746
Evan Chengc88138f2007-03-22 01:54:19 +00002747 // fold (sext_in_reg (load x)) -> (smaller sextload x)
2748 // fold (sext_in_reg (srl (load x), c)) -> (smaller sextload (x+c/evtbits))
2749 SDOperand NarrowLoad = ReduceLoadWidth(N);
2750 if (NarrowLoad.Val)
2751 return NarrowLoad;
2752
Chris Lattner4b37e872006-05-08 21:18:59 +00002753 // fold (sext_in_reg (srl X, 24), i8) -> sra X, 24
2754 // fold (sext_in_reg (srl X, 23), i8) -> sra X, 23 iff possible.
2755 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
2756 if (N0.getOpcode() == ISD::SRL) {
2757 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
2758 if (ShAmt->getValue()+EVTBits <= MVT::getSizeInBits(VT)) {
2759 // We can turn this into an SRA iff the input to the SRL is already sign
2760 // extended enough.
Dan Gohmanea859be2007-06-22 14:59:07 +00002761 unsigned InSignBits = DAG.ComputeNumSignBits(N0.getOperand(0));
Chris Lattner4b37e872006-05-08 21:18:59 +00002762 if (MVT::getSizeInBits(VT)-(ShAmt->getValue()+EVTBits) < InSignBits)
2763 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0), N0.getOperand(1));
2764 }
2765 }
Evan Chengc88138f2007-03-22 01:54:19 +00002766
Nate Begemanded49632005-10-13 03:11:28 +00002767 // fold (sext_inreg (extload x)) -> (sextload x)
Evan Chengc5484282006-10-04 00:56:09 +00002768 if (ISD::isEXTLoad(N0.Val) &&
Evan Cheng83060c52007-03-07 08:07:03 +00002769 ISD::isUNINDEXEDLoad(N0.Val) &&
Evan Cheng2e49f092006-10-11 07:10:22 +00002770 EVT == cast<LoadSDNode>(N0)->getLoadedVT() &&
Evan Chengc5484282006-10-04 00:56:09 +00002771 (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00002772 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2773 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2774 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002775 LN0->getSrcValueOffset(), EVT,
2776 LN0->isVolatile(),
2777 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00002778 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00002779 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002780 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00002781 }
2782 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Evan Cheng83060c52007-03-07 08:07:03 +00002783 if (ISD::isZEXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val) &&
2784 N0.hasOneUse() &&
Evan Cheng2e49f092006-10-11 07:10:22 +00002785 EVT == cast<LoadSDNode>(N0)->getLoadedVT() &&
Evan Chengc5484282006-10-04 00:56:09 +00002786 (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00002787 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2788 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2789 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002790 LN0->getSrcValueOffset(), EVT,
2791 LN0->isVolatile(),
2792 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00002793 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00002794 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002795 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00002796 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00002797 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002798}
2799
Nate Begeman83e75ec2005-09-06 04:43:02 +00002800SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002801 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002802 MVT::ValueType VT = N->getValueType(0);
2803
2804 // noop truncate
2805 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002806 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002807 // fold (truncate c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00002808 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002809 return DAG.getNode(ISD::TRUNCATE, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002810 // fold (truncate (truncate x)) -> (truncate x)
2811 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002812 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002813 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
Chris Lattnerb72773b2006-05-05 22:56:26 +00002814 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND||
2815 N0.getOpcode() == ISD::ANY_EXTEND) {
Chris Lattner32ba1aa2006-11-20 18:05:46 +00002816 if (N0.getOperand(0).getValueType() < VT)
Nate Begeman1d4d4142005-09-01 00:19:25 +00002817 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00002818 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Chris Lattner32ba1aa2006-11-20 18:05:46 +00002819 else if (N0.getOperand(0).getValueType() > VT)
Nate Begeman1d4d4142005-09-01 00:19:25 +00002820 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00002821 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002822 else
2823 // if the source and dest are the same type, we can drop both the extend
2824 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00002825 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002826 }
Evan Cheng007b69e2007-03-21 20:14:05 +00002827
Nate Begeman3df4d522005-10-12 20:40:40 +00002828 // fold (truncate (load x)) -> (smaller load x)
Evan Cheng007b69e2007-03-21 20:14:05 +00002829 // fold (truncate (srl (load x), c)) -> (smaller load (x+c/evtbits))
Evan Chengc88138f2007-03-22 01:54:19 +00002830 return ReduceLoadWidth(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002831}
2832
Chris Lattner94683772005-12-23 05:30:37 +00002833SDOperand DAGCombiner::visitBIT_CONVERT(SDNode *N) {
2834 SDOperand N0 = N->getOperand(0);
2835 MVT::ValueType VT = N->getValueType(0);
2836
Dan Gohman7f321562007-06-25 16:23:39 +00002837 // If the input is a BUILD_VECTOR with all constant elements, fold this now.
2838 // Only do this before legalize, since afterward the target may be depending
2839 // on the bitconvert.
2840 // First check to see if this is all constant.
2841 if (!AfterLegalize &&
2842 N0.getOpcode() == ISD::BUILD_VECTOR && N0.Val->hasOneUse() &&
2843 MVT::isVector(VT)) {
2844 bool isSimple = true;
2845 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i)
2846 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
2847 N0.getOperand(i).getOpcode() != ISD::Constant &&
2848 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
2849 isSimple = false;
2850 break;
2851 }
2852
2853 MVT::ValueType DestEltVT = MVT::getVectorElementType(N->getValueType(0));
2854 assert(!MVT::isVector(DestEltVT) &&
2855 "Element type of vector ValueType must not be vector!");
2856 if (isSimple) {
2857 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(N0.Val, DestEltVT);
2858 }
2859 }
2860
Chris Lattner94683772005-12-23 05:30:37 +00002861 // If the input is a constant, let getNode() fold it.
2862 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
2863 SDOperand Res = DAG.getNode(ISD::BIT_CONVERT, VT, N0);
2864 if (Res.Val != N) return Res;
2865 }
2866
Chris Lattnerc8547d82005-12-23 05:37:50 +00002867 if (N0.getOpcode() == ISD::BIT_CONVERT) // conv(conv(x,t1),t2) -> conv(x,t2)
2868 return DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
Chris Lattner6258fb22006-04-02 02:53:43 +00002869
Chris Lattner57104102005-12-23 05:44:41 +00002870 // fold (conv (load x)) -> (load (conv*)x)
Evan Cheng59d5b682007-05-07 21:27:48 +00002871 // If the resultant load doesn't need a higher alignment than the original!
2872 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Dale Johannesen98a6c622007-05-16 22:45:30 +00002873 ISD::isUNINDEXEDLoad(N0.Val) &&
Evan Cheng59d5b682007-05-07 21:27:48 +00002874 TLI.isOperationLegal(ISD::LOAD, VT)) {
Evan Cheng466685d2006-10-09 20:57:25 +00002875 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng59d5b682007-05-07 21:27:48 +00002876 unsigned Align = TLI.getTargetMachine().getTargetData()->
Dan Gohmanfcc4dd92007-05-18 18:41:29 +00002877 getABITypeAlignment(MVT::getTypeForValueType(VT));
Evan Cheng59d5b682007-05-07 21:27:48 +00002878 unsigned OrigAlign = LN0->getAlignment();
2879 if (Align <= OrigAlign) {
2880 SDOperand Load = DAG.getLoad(VT, LN0->getChain(), LN0->getBasePtr(),
2881 LN0->getSrcValue(), LN0->getSrcValueOffset(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002882 LN0->isVolatile(), Align);
Evan Cheng59d5b682007-05-07 21:27:48 +00002883 AddToWorkList(N);
2884 CombineTo(N0.Val, DAG.getNode(ISD::BIT_CONVERT, N0.getValueType(), Load),
2885 Load.getValue(1));
2886 return Load;
2887 }
Chris Lattner57104102005-12-23 05:44:41 +00002888 }
2889
Chris Lattner94683772005-12-23 05:30:37 +00002890 return SDOperand();
2891}
2892
Dan Gohman7f321562007-06-25 16:23:39 +00002893/// ConstantFoldBIT_CONVERTofBUILD_VECTOR - We know that BV is a build_vector
Chris Lattner6258fb22006-04-02 02:53:43 +00002894/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
2895/// destination element value type.
2896SDOperand DAGCombiner::
Dan Gohman7f321562007-06-25 16:23:39 +00002897ConstantFoldBIT_CONVERTofBUILD_VECTOR(SDNode *BV, MVT::ValueType DstEltVT) {
Chris Lattner6258fb22006-04-02 02:53:43 +00002898 MVT::ValueType SrcEltVT = BV->getOperand(0).getValueType();
2899
2900 // If this is already the right type, we're done.
2901 if (SrcEltVT == DstEltVT) return SDOperand(BV, 0);
2902
2903 unsigned SrcBitSize = MVT::getSizeInBits(SrcEltVT);
2904 unsigned DstBitSize = MVT::getSizeInBits(DstEltVT);
2905
2906 // If this is a conversion of N elements of one type to N elements of another
2907 // type, convert each element. This handles FP<->INT cases.
2908 if (SrcBitSize == DstBitSize) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002909 SmallVector<SDOperand, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00002910 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00002911 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, DstEltVT, BV->getOperand(i)));
Chris Lattner3e104b12006-04-08 04:15:24 +00002912 AddToWorkList(Ops.back().Val);
2913 }
Dan Gohman7f321562007-06-25 16:23:39 +00002914 MVT::ValueType VT =
2915 MVT::getVectorType(DstEltVT,
2916 MVT::getVectorNumElements(BV->getValueType(0)));
2917 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00002918 }
2919
2920 // Otherwise, we're growing or shrinking the elements. To avoid having to
2921 // handle annoying details of growing/shrinking FP values, we convert them to
2922 // int first.
2923 if (MVT::isFloatingPoint(SrcEltVT)) {
2924 // Convert the input float vector to a int vector where the elements are the
2925 // same sizes.
2926 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
2927 MVT::ValueType IntVT = SrcEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
Dan Gohman7f321562007-06-25 16:23:39 +00002928 BV = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, IntVT).Val;
Chris Lattner6258fb22006-04-02 02:53:43 +00002929 SrcEltVT = IntVT;
2930 }
2931
2932 // Now we know the input is an integer vector. If the output is a FP type,
2933 // convert to integer first, then to FP of the right size.
2934 if (MVT::isFloatingPoint(DstEltVT)) {
2935 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
2936 MVT::ValueType TmpVT = DstEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
Dan Gohman7f321562007-06-25 16:23:39 +00002937 SDNode *Tmp = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, TmpVT).Val;
Chris Lattner6258fb22006-04-02 02:53:43 +00002938
2939 // Next, convert to FP elements of the same size.
Dan Gohman7f321562007-06-25 16:23:39 +00002940 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(Tmp, DstEltVT);
Chris Lattner6258fb22006-04-02 02:53:43 +00002941 }
2942
2943 // Okay, we know the src/dst types are both integers of differing types.
2944 // Handling growing first.
2945 assert(MVT::isInteger(SrcEltVT) && MVT::isInteger(DstEltVT));
2946 if (SrcBitSize < DstBitSize) {
2947 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
2948
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002949 SmallVector<SDOperand, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00002950 for (unsigned i = 0, e = BV->getNumOperands(); i != e;
Chris Lattner6258fb22006-04-02 02:53:43 +00002951 i += NumInputsPerOutput) {
2952 bool isLE = TLI.isLittleEndian();
2953 uint64_t NewBits = 0;
2954 bool EltIsUndef = true;
2955 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
2956 // Shift the previously computed bits over.
2957 NewBits <<= SrcBitSize;
2958 SDOperand Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
2959 if (Op.getOpcode() == ISD::UNDEF) continue;
2960 EltIsUndef = false;
2961
2962 NewBits |= cast<ConstantSDNode>(Op)->getValue();
2963 }
2964
2965 if (EltIsUndef)
2966 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
2967 else
2968 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
2969 }
2970
Dan Gohman7f321562007-06-25 16:23:39 +00002971 MVT::ValueType VT = MVT::getVectorType(DstEltVT,
2972 Ops.size());
2973 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00002974 }
2975
2976 // Finally, this must be the case where we are shrinking elements: each input
2977 // turns into multiple outputs.
2978 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002979 SmallVector<SDOperand, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00002980 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00002981 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
2982 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
2983 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
2984 continue;
2985 }
2986 uint64_t OpVal = cast<ConstantSDNode>(BV->getOperand(i))->getValue();
2987
2988 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
2989 unsigned ThisVal = OpVal & ((1ULL << DstBitSize)-1);
2990 OpVal >>= DstBitSize;
2991 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
2992 }
2993
2994 // For big endian targets, swap the order of the pieces of each element.
2995 if (!TLI.isLittleEndian())
2996 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
2997 }
Dan Gohman7f321562007-06-25 16:23:39 +00002998 MVT::ValueType VT = MVT::getVectorType(DstEltVT, Ops.size());
2999 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003000}
3001
3002
3003
Chris Lattner01b3d732005-09-28 22:28:18 +00003004SDOperand DAGCombiner::visitFADD(SDNode *N) {
3005 SDOperand N0 = N->getOperand(0);
3006 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003007 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3008 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00003009 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00003010
Dan Gohman7f321562007-06-25 16:23:39 +00003011 // fold vector ops
3012 SDOperand FoldedVOp = SimplifyVBinOp(N);
3013 if (FoldedVOp.Val) return FoldedVOp;
3014
Nate Begemana0e221d2005-10-18 00:28:13 +00003015 // fold (fadd c1, c2) -> c1+c2
3016 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00003017 return DAG.getNode(ISD::FADD, VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003018 // canonicalize constant to RHS
3019 if (N0CFP && !N1CFP)
3020 return DAG.getNode(ISD::FADD, VT, N1, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00003021 // fold (A + (-B)) -> A-B
Chris Lattner29446522007-05-14 22:04:50 +00003022 if (isNegatibleForFree(N1) == 2)
3023 return DAG.getNode(ISD::FSUB, VT, N0, GetNegatedExpression(N1, DAG));
Chris Lattner01b3d732005-09-28 22:28:18 +00003024 // fold ((-A) + B) -> B-A
Chris Lattner29446522007-05-14 22:04:50 +00003025 if (isNegatibleForFree(N0) == 2)
3026 return DAG.getNode(ISD::FSUB, VT, N1, GetNegatedExpression(N0, DAG));
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003027
3028 // If allowed, fold (fadd (fadd x, c1), c2) -> (fadd x, (fadd c1, c2))
3029 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FADD &&
3030 N0.Val->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
3031 return DAG.getNode(ISD::FADD, VT, N0.getOperand(0),
3032 DAG.getNode(ISD::FADD, VT, N0.getOperand(1), N1));
3033
Chris Lattner01b3d732005-09-28 22:28:18 +00003034 return SDOperand();
3035}
3036
3037SDOperand DAGCombiner::visitFSUB(SDNode *N) {
3038 SDOperand N0 = N->getOperand(0);
3039 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003040 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3041 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00003042 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00003043
Dan Gohman7f321562007-06-25 16:23:39 +00003044 // fold vector ops
3045 SDOperand FoldedVOp = SimplifyVBinOp(N);
3046 if (FoldedVOp.Val) return FoldedVOp;
3047
Nate Begemana0e221d2005-10-18 00:28:13 +00003048 // fold (fsub c1, c2) -> c1-c2
3049 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00003050 return DAG.getNode(ISD::FSUB, VT, N0, N1);
Dan Gohman23ff1822007-07-02 15:48:56 +00003051 // fold (0-B) -> -B
3052 if (UnsafeFPMath && N0CFP && N0CFP->getValue() == 0.0) {
3053 if (isNegatibleForFree(N1))
3054 return GetNegatedExpression(N1, DAG);
3055 return DAG.getNode(ISD::FNEG, VT, N1);
3056 }
Chris Lattner01b3d732005-09-28 22:28:18 +00003057 // fold (A-(-B)) -> A+B
Chris Lattner29446522007-05-14 22:04:50 +00003058 if (isNegatibleForFree(N1))
3059 return DAG.getNode(ISD::FADD, VT, N0, GetNegatedExpression(N1, DAG));
3060
Chris Lattner01b3d732005-09-28 22:28:18 +00003061 return SDOperand();
3062}
3063
3064SDOperand DAGCombiner::visitFMUL(SDNode *N) {
3065 SDOperand N0 = N->getOperand(0);
3066 SDOperand N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003067 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3068 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00003069 MVT::ValueType VT = N->getValueType(0);
3070
Dan Gohman7f321562007-06-25 16:23:39 +00003071 // fold vector ops
3072 SDOperand FoldedVOp = SimplifyVBinOp(N);
3073 if (FoldedVOp.Val) return FoldedVOp;
3074
Nate Begeman11af4ea2005-10-17 20:40:11 +00003075 // fold (fmul c1, c2) -> c1*c2
3076 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00003077 return DAG.getNode(ISD::FMUL, VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003078 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00003079 if (N0CFP && !N1CFP)
3080 return DAG.getNode(ISD::FMUL, VT, N1, N0);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003081 // fold (fmul X, 2.0) -> (fadd X, X)
3082 if (N1CFP && N1CFP->isExactlyValue(+2.0))
3083 return DAG.getNode(ISD::FADD, VT, N0, N0);
Chris Lattner29446522007-05-14 22:04:50 +00003084 // fold (fmul X, -1.0) -> (fneg X)
3085 if (N1CFP && N1CFP->isExactlyValue(-1.0))
3086 return DAG.getNode(ISD::FNEG, VT, N0);
3087
3088 // -X * -Y -> X*Y
3089 if (char LHSNeg = isNegatibleForFree(N0)) {
3090 if (char RHSNeg = isNegatibleForFree(N1)) {
3091 // Both can be negated for free, check to see if at least one is cheaper
3092 // negated.
3093 if (LHSNeg == 2 || RHSNeg == 2)
3094 return DAG.getNode(ISD::FMUL, VT, GetNegatedExpression(N0, DAG),
3095 GetNegatedExpression(N1, DAG));
3096 }
3097 }
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003098
3099 // If allowed, fold (fmul (fmul x, c1), c2) -> (fmul x, (fmul c1, c2))
3100 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FMUL &&
3101 N0.Val->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
3102 return DAG.getNode(ISD::FMUL, VT, N0.getOperand(0),
3103 DAG.getNode(ISD::FMUL, VT, N0.getOperand(1), N1));
3104
Chris Lattner01b3d732005-09-28 22:28:18 +00003105 return SDOperand();
3106}
3107
3108SDOperand DAGCombiner::visitFDIV(SDNode *N) {
3109 SDOperand N0 = N->getOperand(0);
3110 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003111 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3112 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00003113 MVT::ValueType VT = N->getValueType(0);
3114
Dan Gohman7f321562007-06-25 16:23:39 +00003115 // fold vector ops
3116 SDOperand FoldedVOp = SimplifyVBinOp(N);
3117 if (FoldedVOp.Val) return FoldedVOp;
3118
Nate Begemana148d982006-01-18 22:35:16 +00003119 // fold (fdiv c1, c2) -> c1/c2
3120 if (N0CFP && N1CFP)
3121 return DAG.getNode(ISD::FDIV, VT, N0, N1);
Chris Lattner29446522007-05-14 22:04:50 +00003122
3123
3124 // -X / -Y -> X*Y
3125 if (char LHSNeg = isNegatibleForFree(N0)) {
3126 if (char RHSNeg = isNegatibleForFree(N1)) {
3127 // Both can be negated for free, check to see if at least one is cheaper
3128 // negated.
3129 if (LHSNeg == 2 || RHSNeg == 2)
3130 return DAG.getNode(ISD::FDIV, VT, GetNegatedExpression(N0, DAG),
3131 GetNegatedExpression(N1, DAG));
3132 }
3133 }
3134
Chris Lattner01b3d732005-09-28 22:28:18 +00003135 return SDOperand();
3136}
3137
3138SDOperand DAGCombiner::visitFREM(SDNode *N) {
3139 SDOperand N0 = N->getOperand(0);
3140 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003141 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3142 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00003143 MVT::ValueType VT = N->getValueType(0);
3144
Nate Begemana148d982006-01-18 22:35:16 +00003145 // fold (frem c1, c2) -> fmod(c1,c2)
3146 if (N0CFP && N1CFP)
3147 return DAG.getNode(ISD::FREM, VT, N0, N1);
Dan Gohman7f321562007-06-25 16:23:39 +00003148
Chris Lattner01b3d732005-09-28 22:28:18 +00003149 return SDOperand();
3150}
3151
Chris Lattner12d83032006-03-05 05:30:57 +00003152SDOperand DAGCombiner::visitFCOPYSIGN(SDNode *N) {
3153 SDOperand N0 = N->getOperand(0);
3154 SDOperand N1 = N->getOperand(1);
3155 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3156 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
3157 MVT::ValueType VT = N->getValueType(0);
3158
3159 if (N0CFP && N1CFP) // Constant fold
3160 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1);
3161
3162 if (N1CFP) {
3163 // copysign(x, c1) -> fabs(x) iff ispos(c1)
3164 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
3165 union {
3166 double d;
3167 int64_t i;
3168 } u;
3169 u.d = N1CFP->getValue();
3170 if (u.i >= 0)
3171 return DAG.getNode(ISD::FABS, VT, N0);
3172 else
3173 return DAG.getNode(ISD::FNEG, VT, DAG.getNode(ISD::FABS, VT, N0));
3174 }
3175
3176 // copysign(fabs(x), y) -> copysign(x, y)
3177 // copysign(fneg(x), y) -> copysign(x, y)
3178 // copysign(copysign(x,z), y) -> copysign(x, y)
3179 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
3180 N0.getOpcode() == ISD::FCOPYSIGN)
3181 return DAG.getNode(ISD::FCOPYSIGN, VT, N0.getOperand(0), N1);
3182
3183 // copysign(x, abs(y)) -> abs(x)
3184 if (N1.getOpcode() == ISD::FABS)
3185 return DAG.getNode(ISD::FABS, VT, N0);
3186
3187 // copysign(x, copysign(y,z)) -> copysign(x, z)
3188 if (N1.getOpcode() == ISD::FCOPYSIGN)
3189 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(1));
3190
3191 // copysign(x, fp_extend(y)) -> copysign(x, y)
3192 // copysign(x, fp_round(y)) -> copysign(x, y)
3193 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
3194 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(0));
3195
3196 return SDOperand();
3197}
3198
3199
Chris Lattner01b3d732005-09-28 22:28:18 +00003200
Nate Begeman83e75ec2005-09-06 04:43:02 +00003201SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003202 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003203 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00003204 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003205
3206 // fold (sint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00003207 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00003208 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003209 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003210}
3211
Nate Begeman83e75ec2005-09-06 04:43:02 +00003212SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003213 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003214 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00003215 MVT::ValueType VT = N->getValueType(0);
3216
Nate Begeman1d4d4142005-09-01 00:19:25 +00003217 // fold (uint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00003218 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00003219 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003220 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003221}
3222
Nate Begeman83e75ec2005-09-06 04:43:02 +00003223SDOperand DAGCombiner::visitFP_TO_SINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003224 SDOperand N0 = N->getOperand(0);
3225 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3226 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003227
3228 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00003229 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00003230 return DAG.getNode(ISD::FP_TO_SINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003231 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003232}
3233
Nate Begeman83e75ec2005-09-06 04:43:02 +00003234SDOperand DAGCombiner::visitFP_TO_UINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003235 SDOperand N0 = N->getOperand(0);
3236 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3237 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003238
3239 // fold (fp_to_uint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00003240 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00003241 return DAG.getNode(ISD::FP_TO_UINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003242 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003243}
3244
Nate Begeman83e75ec2005-09-06 04:43:02 +00003245SDOperand DAGCombiner::visitFP_ROUND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003246 SDOperand N0 = N->getOperand(0);
3247 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3248 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003249
3250 // fold (fp_round c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00003251 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00003252 return DAG.getNode(ISD::FP_ROUND, VT, N0);
Chris Lattner79dbea52006-03-13 06:26:26 +00003253
3254 // fold (fp_round (fp_extend x)) -> x
3255 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
3256 return N0.getOperand(0);
3257
3258 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
3259 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.Val->hasOneUse()) {
3260 SDOperand Tmp = DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0));
3261 AddToWorkList(Tmp.Val);
3262 return DAG.getNode(ISD::FCOPYSIGN, VT, Tmp, N0.getOperand(1));
3263 }
3264
Nate Begeman83e75ec2005-09-06 04:43:02 +00003265 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003266}
3267
Nate Begeman83e75ec2005-09-06 04:43:02 +00003268SDOperand DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003269 SDOperand N0 = N->getOperand(0);
3270 MVT::ValueType VT = N->getValueType(0);
3271 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00003272 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003273
Nate Begeman1d4d4142005-09-01 00:19:25 +00003274 // fold (fp_round_inreg c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00003275 if (N0CFP) {
3276 SDOperand Round = DAG.getConstantFP(N0CFP->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003277 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003278 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00003279 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003280}
3281
Nate Begeman83e75ec2005-09-06 04:43:02 +00003282SDOperand DAGCombiner::visitFP_EXTEND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003283 SDOperand N0 = N->getOperand(0);
3284 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3285 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003286
3287 // fold (fp_extend c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00003288 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00003289 return DAG.getNode(ISD::FP_EXTEND, VT, N0);
Chris Lattnere564dbb2006-05-05 21:34:35 +00003290
3291 // fold (fpext (load x)) -> (fpext (fpround (extload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00003292 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00003293 (!AfterLegalize||TLI.isLoadXLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003294 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3295 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
3296 LN0->getBasePtr(), LN0->getSrcValue(),
3297 LN0->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003298 N0.getValueType(),
3299 LN0->isVolatile(),
3300 LN0->getAlignment());
Chris Lattnere564dbb2006-05-05 21:34:35 +00003301 CombineTo(N, ExtLoad);
3302 CombineTo(N0.Val, DAG.getNode(ISD::FP_ROUND, N0.getValueType(), ExtLoad),
3303 ExtLoad.getValue(1));
3304 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
3305 }
3306
3307
Nate Begeman83e75ec2005-09-06 04:43:02 +00003308 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003309}
3310
Nate Begeman83e75ec2005-09-06 04:43:02 +00003311SDOperand DAGCombiner::visitFNEG(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003312 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00003313
Dan Gohman23ff1822007-07-02 15:48:56 +00003314 if (isNegatibleForFree(N0))
3315 return GetNegatedExpression(N0, DAG);
3316
Nate Begeman83e75ec2005-09-06 04:43:02 +00003317 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003318}
3319
Nate Begeman83e75ec2005-09-06 04:43:02 +00003320SDOperand DAGCombiner::visitFABS(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003321 SDOperand N0 = N->getOperand(0);
3322 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3323 MVT::ValueType VT = N->getValueType(0);
3324
Nate Begeman1d4d4142005-09-01 00:19:25 +00003325 // fold (fabs c1) -> fabs(c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00003326 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00003327 return DAG.getNode(ISD::FABS, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003328 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00003329 if (N0.getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00003330 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003331 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00003332 // fold (fabs (fcopysign x, y)) -> (fabs x)
3333 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
3334 return DAG.getNode(ISD::FABS, VT, N0.getOperand(0));
3335
Nate Begeman83e75ec2005-09-06 04:43:02 +00003336 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003337}
3338
Nate Begeman44728a72005-09-19 22:34:01 +00003339SDOperand DAGCombiner::visitBRCOND(SDNode *N) {
3340 SDOperand Chain = N->getOperand(0);
3341 SDOperand N1 = N->getOperand(1);
3342 SDOperand N2 = N->getOperand(2);
3343 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
3344
3345 // never taken branch, fold to chain
3346 if (N1C && N1C->isNullValue())
3347 return Chain;
3348 // unconditional branch
Nate Begemane17daeb2005-10-05 21:43:42 +00003349 if (N1C && N1C->getValue() == 1)
Nate Begeman44728a72005-09-19 22:34:01 +00003350 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
Nate Begeman750ac1b2006-02-01 07:19:44 +00003351 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
3352 // on the target.
3353 if (N1.getOpcode() == ISD::SETCC &&
3354 TLI.isOperationLegal(ISD::BR_CC, MVT::Other)) {
3355 return DAG.getNode(ISD::BR_CC, MVT::Other, Chain, N1.getOperand(2),
3356 N1.getOperand(0), N1.getOperand(1), N2);
3357 }
Nate Begeman44728a72005-09-19 22:34:01 +00003358 return SDOperand();
3359}
3360
Chris Lattner3ea0b472005-10-05 06:47:48 +00003361// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
3362//
Nate Begeman44728a72005-09-19 22:34:01 +00003363SDOperand DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00003364 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
3365 SDOperand CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
3366
3367 // Use SimplifySetCC to simplify SETCC's.
Nate Begemane17daeb2005-10-05 21:43:42 +00003368 SDOperand Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false);
Chris Lattner30f73e72006-10-14 03:52:46 +00003369 if (Simp.Val) AddToWorkList(Simp.Val);
3370
Nate Begemane17daeb2005-10-05 21:43:42 +00003371 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.Val);
3372
3373 // fold br_cc true, dest -> br dest (unconditional branch)
3374 if (SCCC && SCCC->getValue())
3375 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
3376 N->getOperand(4));
3377 // fold br_cc false, dest -> unconditional fall through
3378 if (SCCC && SCCC->isNullValue())
3379 return N->getOperand(0);
Chris Lattner30f73e72006-10-14 03:52:46 +00003380
Nate Begemane17daeb2005-10-05 21:43:42 +00003381 // fold to a simpler setcc
3382 if (Simp.Val && Simp.getOpcode() == ISD::SETCC)
3383 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
3384 Simp.getOperand(2), Simp.getOperand(0),
3385 Simp.getOperand(1), N->getOperand(4));
Nate Begeman44728a72005-09-19 22:34:01 +00003386 return SDOperand();
3387}
3388
Chris Lattner448f2192006-11-11 00:39:41 +00003389
3390/// CombineToPreIndexedLoadStore - Try turning a load / store and a
3391/// pre-indexed load / store when the base pointer is a add or subtract
3392/// and it has other uses besides the load / store. After the
3393/// transformation, the new indexed load / store has effectively folded
3394/// the add / subtract in and all of its other uses are redirected to the
3395/// new load / store.
3396bool DAGCombiner::CombineToPreIndexedLoadStore(SDNode *N) {
3397 if (!AfterLegalize)
3398 return false;
3399
3400 bool isLoad = true;
3401 SDOperand Ptr;
3402 MVT::ValueType VT;
3403 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Evan Chenge90460e2006-12-16 06:25:23 +00003404 if (LD->getAddressingMode() != ISD::UNINDEXED)
3405 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00003406 VT = LD->getLoadedVT();
Evan Cheng83060c52007-03-07 08:07:03 +00003407 if (!TLI.isIndexedLoadLegal(ISD::PRE_INC, VT) &&
Chris Lattner448f2192006-11-11 00:39:41 +00003408 !TLI.isIndexedLoadLegal(ISD::PRE_DEC, VT))
3409 return false;
3410 Ptr = LD->getBasePtr();
3411 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Evan Chenge90460e2006-12-16 06:25:23 +00003412 if (ST->getAddressingMode() != ISD::UNINDEXED)
3413 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00003414 VT = ST->getStoredVT();
3415 if (!TLI.isIndexedStoreLegal(ISD::PRE_INC, VT) &&
3416 !TLI.isIndexedStoreLegal(ISD::PRE_DEC, VT))
3417 return false;
3418 Ptr = ST->getBasePtr();
3419 isLoad = false;
3420 } else
3421 return false;
3422
Chris Lattner9f1794e2006-11-11 00:56:29 +00003423 // If the pointer is not an add/sub, or if it doesn't have multiple uses, bail
3424 // out. There is no reason to make this a preinc/predec.
3425 if ((Ptr.getOpcode() != ISD::ADD && Ptr.getOpcode() != ISD::SUB) ||
3426 Ptr.Val->hasOneUse())
3427 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00003428
Chris Lattner9f1794e2006-11-11 00:56:29 +00003429 // Ask the target to do addressing mode selection.
3430 SDOperand BasePtr;
3431 SDOperand Offset;
3432 ISD::MemIndexedMode AM = ISD::UNINDEXED;
3433 if (!TLI.getPreIndexedAddressParts(N, BasePtr, Offset, AM, DAG))
3434 return false;
Evan Chenga7d4a042007-05-03 23:52:19 +00003435 // Don't create a indexed load / store with zero offset.
3436 if (isa<ConstantSDNode>(Offset) &&
3437 cast<ConstantSDNode>(Offset)->getValue() == 0)
3438 return false;
Chris Lattner9f1794e2006-11-11 00:56:29 +00003439
Chris Lattner41e53fd2006-11-11 01:00:15 +00003440 // Try turning it into a pre-indexed load / store except when:
Evan Chengc843abe2007-05-24 02:35:39 +00003441 // 1) The new base ptr is a frame index.
3442 // 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 +00003443 // predecessor of the value being stored.
Evan Chengc843abe2007-05-24 02:35:39 +00003444 // 3) Another use of old base ptr is a predecessor of N. If ptr is folded
Chris Lattner9f1794e2006-11-11 00:56:29 +00003445 // that would create a cycle.
Evan Chengc843abe2007-05-24 02:35:39 +00003446 // 4) All uses are load / store ops that use it as old base ptr.
Chris Lattner448f2192006-11-11 00:39:41 +00003447
Chris Lattner41e53fd2006-11-11 01:00:15 +00003448 // Check #1. Preinc'ing a frame index would require copying the stack pointer
3449 // (plus the implicit offset) to a register to preinc anyway.
3450 if (isa<FrameIndexSDNode>(BasePtr))
3451 return false;
3452
3453 // Check #2.
Chris Lattner9f1794e2006-11-11 00:56:29 +00003454 if (!isLoad) {
3455 SDOperand Val = cast<StoreSDNode>(N)->getValue();
Evan Chengc843abe2007-05-24 02:35:39 +00003456 if (Val == BasePtr || BasePtr.Val->isPredecessor(Val.Val))
Chris Lattner9f1794e2006-11-11 00:56:29 +00003457 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00003458 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00003459
Evan Chengc843abe2007-05-24 02:35:39 +00003460 // Now check for #3 and #4.
Chris Lattner9f1794e2006-11-11 00:56:29 +00003461 bool RealUse = false;
3462 for (SDNode::use_iterator I = Ptr.Val->use_begin(),
3463 E = Ptr.Val->use_end(); I != E; ++I) {
3464 SDNode *Use = *I;
3465 if (Use == N)
3466 continue;
3467 if (Use->isPredecessor(N))
3468 return false;
3469
3470 if (!((Use->getOpcode() == ISD::LOAD &&
3471 cast<LoadSDNode>(Use)->getBasePtr() == Ptr) ||
3472 (Use->getOpcode() == ISD::STORE) &&
3473 cast<StoreSDNode>(Use)->getBasePtr() == Ptr))
3474 RealUse = true;
3475 }
3476 if (!RealUse)
3477 return false;
3478
3479 SDOperand Result;
3480 if (isLoad)
3481 Result = DAG.getIndexedLoad(SDOperand(N,0), BasePtr, Offset, AM);
3482 else
3483 Result = DAG.getIndexedStore(SDOperand(N,0), BasePtr, Offset, AM);
3484 ++PreIndexedNodes;
3485 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +00003486 DOUT << "\nReplacing.4 "; DEBUG(N->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +00003487 DOUT << "\nWith: "; DEBUG(Result.Val->dump(&DAG));
3488 DOUT << '\n';
Chris Lattner9f1794e2006-11-11 00:56:29 +00003489 std::vector<SDNode*> NowDead;
3490 if (isLoad) {
3491 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(0),
3492 NowDead);
3493 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1), Result.getValue(2),
3494 NowDead);
3495 } else {
3496 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(1),
3497 NowDead);
3498 }
3499
3500 // Nodes can end up on the worklist more than once. Make sure we do
3501 // not process a node that has been replaced.
3502 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
3503 removeFromWorkList(NowDead[i]);
3504 // Finally, since the node is now dead, remove it from the graph.
3505 DAG.DeleteNode(N);
3506
3507 // Replace the uses of Ptr with uses of the updated base value.
3508 DAG.ReplaceAllUsesOfValueWith(Ptr, Result.getValue(isLoad ? 1 : 0),
3509 NowDead);
3510 removeFromWorkList(Ptr.Val);
3511 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
3512 removeFromWorkList(NowDead[i]);
3513 DAG.DeleteNode(Ptr.Val);
3514
3515 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00003516}
3517
3518/// CombineToPostIndexedLoadStore - Try combine a load / store with a
3519/// add / sub of the base pointer node into a post-indexed load / store.
3520/// The transformation folded the add / subtract into the new indexed
3521/// load / store effectively and all of its uses are redirected to the
3522/// new load / store.
3523bool DAGCombiner::CombineToPostIndexedLoadStore(SDNode *N) {
3524 if (!AfterLegalize)
3525 return false;
3526
3527 bool isLoad = true;
3528 SDOperand Ptr;
3529 MVT::ValueType VT;
3530 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Evan Chenge90460e2006-12-16 06:25:23 +00003531 if (LD->getAddressingMode() != ISD::UNINDEXED)
3532 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00003533 VT = LD->getLoadedVT();
3534 if (!TLI.isIndexedLoadLegal(ISD::POST_INC, VT) &&
3535 !TLI.isIndexedLoadLegal(ISD::POST_DEC, VT))
3536 return false;
3537 Ptr = LD->getBasePtr();
3538 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Evan Chenge90460e2006-12-16 06:25:23 +00003539 if (ST->getAddressingMode() != ISD::UNINDEXED)
3540 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00003541 VT = ST->getStoredVT();
3542 if (!TLI.isIndexedStoreLegal(ISD::POST_INC, VT) &&
3543 !TLI.isIndexedStoreLegal(ISD::POST_DEC, VT))
3544 return false;
3545 Ptr = ST->getBasePtr();
3546 isLoad = false;
3547 } else
3548 return false;
3549
Evan Chengcc470212006-11-16 00:08:20 +00003550 if (Ptr.Val->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00003551 return false;
3552
3553 for (SDNode::use_iterator I = Ptr.Val->use_begin(),
3554 E = Ptr.Val->use_end(); I != E; ++I) {
3555 SDNode *Op = *I;
3556 if (Op == N ||
3557 (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB))
3558 continue;
3559
3560 SDOperand BasePtr;
3561 SDOperand Offset;
3562 ISD::MemIndexedMode AM = ISD::UNINDEXED;
3563 if (TLI.getPostIndexedAddressParts(N, Op, BasePtr, Offset, AM, DAG)) {
3564 if (Ptr == Offset)
3565 std::swap(BasePtr, Offset);
3566 if (Ptr != BasePtr)
Chris Lattner448f2192006-11-11 00:39:41 +00003567 continue;
Evan Chenga7d4a042007-05-03 23:52:19 +00003568 // Don't create a indexed load / store with zero offset.
3569 if (isa<ConstantSDNode>(Offset) &&
3570 cast<ConstantSDNode>(Offset)->getValue() == 0)
3571 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00003572
Chris Lattner9f1794e2006-11-11 00:56:29 +00003573 // Try turning it into a post-indexed load / store except when
3574 // 1) All uses are load / store ops that use it as base ptr.
3575 // 2) Op must be independent of N, i.e. Op is neither a predecessor
3576 // nor a successor of N. Otherwise, if Op is folded that would
3577 // create a cycle.
3578
3579 // Check for #1.
3580 bool TryNext = false;
3581 for (SDNode::use_iterator II = BasePtr.Val->use_begin(),
3582 EE = BasePtr.Val->use_end(); II != EE; ++II) {
3583 SDNode *Use = *II;
3584 if (Use == Ptr.Val)
Chris Lattner448f2192006-11-11 00:39:41 +00003585 continue;
3586
Chris Lattner9f1794e2006-11-11 00:56:29 +00003587 // If all the uses are load / store addresses, then don't do the
3588 // transformation.
3589 if (Use->getOpcode() == ISD::ADD || Use->getOpcode() == ISD::SUB){
3590 bool RealUse = false;
3591 for (SDNode::use_iterator III = Use->use_begin(),
3592 EEE = Use->use_end(); III != EEE; ++III) {
3593 SDNode *UseUse = *III;
3594 if (!((UseUse->getOpcode() == ISD::LOAD &&
3595 cast<LoadSDNode>(UseUse)->getBasePtr().Val == Use) ||
3596 (UseUse->getOpcode() == ISD::STORE) &&
3597 cast<StoreSDNode>(UseUse)->getBasePtr().Val == Use))
3598 RealUse = true;
3599 }
Chris Lattner448f2192006-11-11 00:39:41 +00003600
Chris Lattner9f1794e2006-11-11 00:56:29 +00003601 if (!RealUse) {
3602 TryNext = true;
3603 break;
Chris Lattner448f2192006-11-11 00:39:41 +00003604 }
3605 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00003606 }
3607 if (TryNext)
3608 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00003609
Chris Lattner9f1794e2006-11-11 00:56:29 +00003610 // Check for #2
3611 if (!Op->isPredecessor(N) && !N->isPredecessor(Op)) {
3612 SDOperand Result = isLoad
3613 ? DAG.getIndexedLoad(SDOperand(N,0), BasePtr, Offset, AM)
3614 : DAG.getIndexedStore(SDOperand(N,0), BasePtr, Offset, AM);
3615 ++PostIndexedNodes;
3616 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +00003617 DOUT << "\nReplacing.5 "; DEBUG(N->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +00003618 DOUT << "\nWith: "; DEBUG(Result.Val->dump(&DAG));
3619 DOUT << '\n';
Chris Lattner9f1794e2006-11-11 00:56:29 +00003620 std::vector<SDNode*> NowDead;
3621 if (isLoad) {
3622 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(0),
Chris Lattner448f2192006-11-11 00:39:41 +00003623 NowDead);
Chris Lattner9f1794e2006-11-11 00:56:29 +00003624 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1), Result.getValue(2),
3625 NowDead);
3626 } else {
3627 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(1),
3628 NowDead);
Chris Lattner448f2192006-11-11 00:39:41 +00003629 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00003630
3631 // Nodes can end up on the worklist more than once. Make sure we do
3632 // not process a node that has been replaced.
3633 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
3634 removeFromWorkList(NowDead[i]);
3635 // Finally, since the node is now dead, remove it from the graph.
3636 DAG.DeleteNode(N);
3637
3638 // Replace the uses of Use with uses of the updated base value.
3639 DAG.ReplaceAllUsesOfValueWith(SDOperand(Op, 0),
3640 Result.getValue(isLoad ? 1 : 0),
3641 NowDead);
3642 removeFromWorkList(Op);
3643 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
3644 removeFromWorkList(NowDead[i]);
3645 DAG.DeleteNode(Op);
3646
3647 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00003648 }
3649 }
3650 }
3651 return false;
3652}
3653
3654
Chris Lattner01a22022005-10-10 22:04:48 +00003655SDOperand DAGCombiner::visitLOAD(SDNode *N) {
Evan Cheng466685d2006-10-09 20:57:25 +00003656 LoadSDNode *LD = cast<LoadSDNode>(N);
3657 SDOperand Chain = LD->getChain();
3658 SDOperand Ptr = LD->getBasePtr();
Evan Cheng45a7ca92007-05-01 00:38:21 +00003659
3660 // If load is not volatile and there are no uses of the loaded value (and
3661 // the updated indexed value in case of indexed loads), change uses of the
3662 // chain value into uses of the chain input (i.e. delete the dead load).
3663 if (!LD->isVolatile()) {
Evan Cheng498f5592007-05-01 08:53:39 +00003664 if (N->getValueType(1) == MVT::Other) {
3665 // Unindexed loads.
3666 if (N->hasNUsesOfValue(0, 0))
3667 return CombineTo(N, DAG.getNode(ISD::UNDEF, N->getValueType(0)), Chain);
3668 } else {
3669 // Indexed loads.
3670 assert(N->getValueType(2) == MVT::Other && "Malformed indexed loads?");
3671 if (N->hasNUsesOfValue(0, 0) && N->hasNUsesOfValue(0, 1)) {
3672 SDOperand Undef0 = DAG.getNode(ISD::UNDEF, N->getValueType(0));
3673 SDOperand Undef1 = DAG.getNode(ISD::UNDEF, N->getValueType(1));
3674 SDOperand To[] = { Undef0, Undef1, Chain };
3675 return CombineTo(N, To, 3);
Evan Cheng45a7ca92007-05-01 00:38:21 +00003676 }
Evan Cheng45a7ca92007-05-01 00:38:21 +00003677 }
3678 }
Chris Lattner01a22022005-10-10 22:04:48 +00003679
3680 // If this load is directly stored, replace the load value with the stored
3681 // value.
3682 // TODO: Handle store large -> read small portion.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00003683 // TODO: Handle TRUNCSTORE/LOADEXT
3684 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00003685 if (ISD::isNON_TRUNCStore(Chain.Val)) {
3686 StoreSDNode *PrevST = cast<StoreSDNode>(Chain);
3687 if (PrevST->getBasePtr() == Ptr &&
3688 PrevST->getValue().getValueType() == N->getValueType(0))
Jim Laskeyc2b19f32006-10-11 17:47:52 +00003689 return CombineTo(N, Chain.getOperand(1), Chain);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003690 }
Jim Laskeyc2b19f32006-10-11 17:47:52 +00003691 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00003692
Jim Laskey7ca56af2006-10-11 13:47:09 +00003693 if (CombinerAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00003694 // Walk up chain skipping non-aliasing memory nodes.
3695 SDOperand BetterChain = FindBetterChain(N, Chain);
3696
Jim Laskey6ff23e52006-10-04 16:53:27 +00003697 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00003698 if (Chain != BetterChain) {
Jim Laskeyc2b19f32006-10-11 17:47:52 +00003699 SDOperand ReplLoad;
3700
Jim Laskey279f0532006-09-25 16:29:54 +00003701 // Replace the chain to void dependency.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00003702 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
3703 ReplLoad = DAG.getLoad(N->getValueType(0), BetterChain, Ptr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00003704 LD->getSrcValue(), LD->getSrcValueOffset(),
3705 LD->isVolatile(), LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00003706 } else {
3707 ReplLoad = DAG.getExtLoad(LD->getExtensionType(),
3708 LD->getValueType(0),
3709 BetterChain, Ptr, LD->getSrcValue(),
3710 LD->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003711 LD->getLoadedVT(),
3712 LD->isVolatile(),
3713 LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00003714 }
Jim Laskey279f0532006-09-25 16:29:54 +00003715
Jim Laskey6ff23e52006-10-04 16:53:27 +00003716 // Create token factor to keep old chain connected.
Jim Laskey288af5e2006-09-25 19:32:58 +00003717 SDOperand Token = DAG.getNode(ISD::TokenFactor, MVT::Other,
3718 Chain, ReplLoad.getValue(1));
Jim Laskey6ff23e52006-10-04 16:53:27 +00003719
Jim Laskey274062c2006-10-13 23:32:28 +00003720 // Replace uses with load result and token factor. Don't add users
3721 // to work list.
3722 return CombineTo(N, ReplLoad.getValue(0), Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00003723 }
3724 }
3725
Evan Cheng7fc033a2006-11-03 03:06:21 +00003726 // Try transforming N to an indexed load.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00003727 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Evan Cheng7fc033a2006-11-03 03:06:21 +00003728 return SDOperand(N, 0);
3729
Chris Lattner01a22022005-10-10 22:04:48 +00003730 return SDOperand();
3731}
3732
Chris Lattner87514ca2005-10-10 22:31:19 +00003733SDOperand DAGCombiner::visitSTORE(SDNode *N) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00003734 StoreSDNode *ST = cast<StoreSDNode>(N);
3735 SDOperand Chain = ST->getChain();
3736 SDOperand Value = ST->getValue();
3737 SDOperand Ptr = ST->getBasePtr();
Jim Laskey7aed46c2006-10-11 18:55:16 +00003738
Evan Cheng59d5b682007-05-07 21:27:48 +00003739 // If this is a store of a bit convert, store the input value if the
Evan Cheng2c4f9432007-05-09 21:49:47 +00003740 // resultant store does not need a higher alignment than the original.
Dale Johannesen98a6c622007-05-16 22:45:30 +00003741 if (Value.getOpcode() == ISD::BIT_CONVERT && !ST->isTruncatingStore() &&
3742 ST->getAddressingMode() == ISD::UNINDEXED) {
Evan Cheng59d5b682007-05-07 21:27:48 +00003743 unsigned Align = ST->getAlignment();
3744 MVT::ValueType SVT = Value.getOperand(0).getValueType();
3745 unsigned OrigAlign = TLI.getTargetMachine().getTargetData()->
Dan Gohmanfcc4dd92007-05-18 18:41:29 +00003746 getABITypeAlignment(MVT::getTypeForValueType(SVT));
Evan Chengc2cd2b22007-05-07 21:36:06 +00003747 if (Align <= OrigAlign && TLI.isOperationLegal(ISD::STORE, SVT))
Evan Cheng59d5b682007-05-07 21:27:48 +00003748 return DAG.getStore(Chain, Value.getOperand(0), Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00003749 ST->getSrcValueOffset(), ST->isVolatile(), Align);
Jim Laskey279f0532006-09-25 16:29:54 +00003750 }
3751
Nate Begeman2cbba892006-12-11 02:23:46 +00003752 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Nate Begeman2cbba892006-12-11 02:23:46 +00003753 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Value)) {
Evan Cheng25ece662006-12-11 17:25:19 +00003754 if (Value.getOpcode() != ISD::TargetConstantFP) {
3755 SDOperand Tmp;
Chris Lattner62be1a72006-12-12 04:16:14 +00003756 switch (CFP->getValueType(0)) {
3757 default: assert(0 && "Unknown FP type");
3758 case MVT::f32:
3759 if (!AfterLegalize || TLI.isTypeLegal(MVT::i32)) {
3760 Tmp = DAG.getConstant(FloatToBits(CFP->getValue()), MVT::i32);
3761 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00003762 ST->getSrcValueOffset(), ST->isVolatile(),
3763 ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00003764 }
3765 break;
3766 case MVT::f64:
3767 if (!AfterLegalize || TLI.isTypeLegal(MVT::i64)) {
3768 Tmp = DAG.getConstant(DoubleToBits(CFP->getValue()), MVT::i64);
3769 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00003770 ST->getSrcValueOffset(), ST->isVolatile(),
3771 ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00003772 } else if (TLI.isTypeLegal(MVT::i32)) {
3773 // Many FP stores are not make apparent until after legalize, e.g. for
3774 // argument passing. Since this is so common, custom legalize the
3775 // 64-bit integer store into two 32-bit stores.
3776 uint64_t Val = DoubleToBits(CFP->getValue());
3777 SDOperand Lo = DAG.getConstant(Val & 0xFFFFFFFF, MVT::i32);
3778 SDOperand Hi = DAG.getConstant(Val >> 32, MVT::i32);
3779 if (!TLI.isLittleEndian()) std::swap(Lo, Hi);
3780
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00003781 int SVOffset = ST->getSrcValueOffset();
3782 unsigned Alignment = ST->getAlignment();
3783 bool isVolatile = ST->isVolatile();
3784
Chris Lattner62be1a72006-12-12 04:16:14 +00003785 SDOperand St0 = DAG.getStore(Chain, Lo, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00003786 ST->getSrcValueOffset(),
3787 isVolatile, ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00003788 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3789 DAG.getConstant(4, Ptr.getValueType()));
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00003790 SVOffset += 4;
3791 if (Alignment > 4)
3792 Alignment = 4;
Chris Lattner62be1a72006-12-12 04:16:14 +00003793 SDOperand St1 = DAG.getStore(Chain, Hi, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00003794 SVOffset, isVolatile, Alignment);
Chris Lattner62be1a72006-12-12 04:16:14 +00003795 return DAG.getNode(ISD::TokenFactor, MVT::Other, St0, St1);
3796 }
3797 break;
Evan Cheng25ece662006-12-11 17:25:19 +00003798 }
Nate Begeman2cbba892006-12-11 02:23:46 +00003799 }
Nate Begeman2cbba892006-12-11 02:23:46 +00003800 }
3801
Jim Laskey279f0532006-09-25 16:29:54 +00003802 if (CombinerAA) {
3803 // Walk up chain skipping non-aliasing memory nodes.
3804 SDOperand BetterChain = FindBetterChain(N, Chain);
3805
Jim Laskey6ff23e52006-10-04 16:53:27 +00003806 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00003807 if (Chain != BetterChain) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00003808 // Replace the chain to avoid dependency.
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00003809 SDOperand ReplStore;
3810 if (ST->isTruncatingStore()) {
3811 ReplStore = DAG.getTruncStore(BetterChain, Value, Ptr,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00003812 ST->getSrcValue(), ST->getSrcValueOffset(), ST->getStoredVT(),
3813 ST->isVolatile(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00003814 } else {
3815 ReplStore = DAG.getStore(BetterChain, Value, Ptr,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00003816 ST->getSrcValue(), ST->getSrcValueOffset(),
3817 ST->isVolatile(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00003818 }
3819
Jim Laskey279f0532006-09-25 16:29:54 +00003820 // Create token to keep both nodes around.
Jim Laskey274062c2006-10-13 23:32:28 +00003821 SDOperand Token =
3822 DAG.getNode(ISD::TokenFactor, MVT::Other, Chain, ReplStore);
3823
3824 // Don't add users to work list.
3825 return CombineTo(N, Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00003826 }
Jim Laskeyd1aed7a2006-09-21 16:28:59 +00003827 }
Chris Lattnerc33baaa2005-12-23 05:48:07 +00003828
Evan Cheng33dbedc2006-11-05 09:31:14 +00003829 // Try transforming N to an indexed store.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00003830 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Evan Cheng33dbedc2006-11-05 09:31:14 +00003831 return SDOperand(N, 0);
3832
Chris Lattner87514ca2005-10-10 22:31:19 +00003833 return SDOperand();
3834}
3835
Chris Lattnerca242442006-03-19 01:27:56 +00003836SDOperand DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
3837 SDOperand InVec = N->getOperand(0);
3838 SDOperand InVal = N->getOperand(1);
3839 SDOperand EltNo = N->getOperand(2);
3840
3841 // If the invec is a BUILD_VECTOR and if EltNo is a constant, build a new
3842 // vector with the inserted element.
3843 if (InVec.getOpcode() == ISD::BUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
3844 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003845 SmallVector<SDOperand, 8> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
Chris Lattnerca242442006-03-19 01:27:56 +00003846 if (Elt < Ops.size())
3847 Ops[Elt] = InVal;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003848 return DAG.getNode(ISD::BUILD_VECTOR, InVec.getValueType(),
3849 &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00003850 }
3851
3852 return SDOperand();
3853}
3854
Dan Gohman7f321562007-06-25 16:23:39 +00003855SDOperand DAGCombiner::visitBUILD_VECTOR(SDNode *N) {
3856 unsigned NumInScalars = N->getNumOperands();
3857 MVT::ValueType VT = N->getValueType(0);
3858 unsigned NumElts = MVT::getVectorNumElements(VT);
3859 MVT::ValueType EltType = MVT::getVectorElementType(VT);
Chris Lattnerca242442006-03-19 01:27:56 +00003860
Dan Gohman7f321562007-06-25 16:23:39 +00003861 // Check to see if this is a BUILD_VECTOR of a bunch of EXTRACT_VECTOR_ELT
3862 // operations. If so, and if the EXTRACT_VECTOR_ELT vector inputs come from
3863 // at most two distinct vectors, turn this into a shuffle node.
Chris Lattnerd7648c82006-03-28 20:28:38 +00003864 SDOperand VecIn1, VecIn2;
3865 for (unsigned i = 0; i != NumInScalars; ++i) {
3866 // Ignore undef inputs.
3867 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
3868
Dan Gohman7f321562007-06-25 16:23:39 +00003869 // If this input is something other than a EXTRACT_VECTOR_ELT with a
Chris Lattnerd7648c82006-03-28 20:28:38 +00003870 // constant index, bail out.
Dan Gohman7f321562007-06-25 16:23:39 +00003871 if (N->getOperand(i).getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
Chris Lattnerd7648c82006-03-28 20:28:38 +00003872 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
3873 VecIn1 = VecIn2 = SDOperand(0, 0);
3874 break;
3875 }
3876
Dan Gohman7f321562007-06-25 16:23:39 +00003877 // If the input vector type disagrees with the result of the build_vector,
Chris Lattnerd7648c82006-03-28 20:28:38 +00003878 // we can't make a shuffle.
3879 SDOperand ExtractedFromVec = N->getOperand(i).getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00003880 if (ExtractedFromVec.getValueType() != VT) {
Chris Lattnerd7648c82006-03-28 20:28:38 +00003881 VecIn1 = VecIn2 = SDOperand(0, 0);
3882 break;
3883 }
3884
3885 // Otherwise, remember this. We allow up to two distinct input vectors.
3886 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
3887 continue;
3888
3889 if (VecIn1.Val == 0) {
3890 VecIn1 = ExtractedFromVec;
3891 } else if (VecIn2.Val == 0) {
3892 VecIn2 = ExtractedFromVec;
3893 } else {
3894 // Too many inputs.
3895 VecIn1 = VecIn2 = SDOperand(0, 0);
3896 break;
3897 }
3898 }
3899
3900 // If everything is good, we can make a shuffle operation.
3901 if (VecIn1.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003902 SmallVector<SDOperand, 8> BuildVecIndices;
Chris Lattnerd7648c82006-03-28 20:28:38 +00003903 for (unsigned i = 0; i != NumInScalars; ++i) {
3904 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
Evan Cheng597a3bd2007-01-20 10:10:26 +00003905 BuildVecIndices.push_back(DAG.getNode(ISD::UNDEF, TLI.getPointerTy()));
Chris Lattnerd7648c82006-03-28 20:28:38 +00003906 continue;
3907 }
3908
3909 SDOperand Extract = N->getOperand(i);
3910
3911 // If extracting from the first vector, just use the index directly.
3912 if (Extract.getOperand(0) == VecIn1) {
3913 BuildVecIndices.push_back(Extract.getOperand(1));
3914 continue;
3915 }
3916
3917 // Otherwise, use InIdx + VecSize
3918 unsigned Idx = cast<ConstantSDNode>(Extract.getOperand(1))->getValue();
Evan Cheng597a3bd2007-01-20 10:10:26 +00003919 BuildVecIndices.push_back(DAG.getConstant(Idx+NumInScalars,
3920 TLI.getPointerTy()));
Chris Lattnerd7648c82006-03-28 20:28:38 +00003921 }
3922
3923 // Add count and size info.
Dan Gohman7f321562007-06-25 16:23:39 +00003924 MVT::ValueType BuildVecVT =
3925 MVT::getVectorType(TLI.getPointerTy(), NumElts);
Chris Lattnerd7648c82006-03-28 20:28:38 +00003926
Dan Gohman7f321562007-06-25 16:23:39 +00003927 // Return the new VECTOR_SHUFFLE node.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003928 SDOperand Ops[5];
3929 Ops[0] = VecIn1;
Chris Lattnercef896e2006-03-28 22:19:47 +00003930 if (VecIn2.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003931 Ops[1] = VecIn2;
Chris Lattnercef896e2006-03-28 22:19:47 +00003932 } else {
Dan Gohman7f321562007-06-25 16:23:39 +00003933 // Use an undef build_vector as input for the second operand.
Chris Lattnercef896e2006-03-28 22:19:47 +00003934 std::vector<SDOperand> UnOps(NumInScalars,
3935 DAG.getNode(ISD::UNDEF,
Dan Gohman7f321562007-06-25 16:23:39 +00003936 EltType));
3937 Ops[1] = DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003938 &UnOps[0], UnOps.size());
3939 AddToWorkList(Ops[1].Val);
Chris Lattnercef896e2006-03-28 22:19:47 +00003940 }
Dan Gohman7f321562007-06-25 16:23:39 +00003941 Ops[2] = DAG.getNode(ISD::BUILD_VECTOR, BuildVecVT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003942 &BuildVecIndices[0], BuildVecIndices.size());
Dan Gohman7f321562007-06-25 16:23:39 +00003943 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Ops, 3);
Chris Lattnerd7648c82006-03-28 20:28:38 +00003944 }
3945
3946 return SDOperand();
3947}
3948
Dan Gohman7f321562007-06-25 16:23:39 +00003949SDOperand DAGCombiner::visitCONCAT_VECTORS(SDNode *N) {
3950 // TODO: Check to see if this is a CONCAT_VECTORS of a bunch of
3951 // EXTRACT_SUBVECTOR operations. If so, and if the EXTRACT_SUBVECTOR vector
3952 // inputs come from at most two distinct vectors, turn this into a shuffle
3953 // node.
3954
3955 // If we only have one input vector, we don't need to do any concatenation.
3956 if (N->getNumOperands() == 1) {
3957 return N->getOperand(0);
3958 }
3959
3960 return SDOperand();
3961}
3962
Chris Lattner66445d32006-03-28 22:11:53 +00003963SDOperand DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
Chris Lattnerf1d0c622006-03-31 22:16:43 +00003964 SDOperand ShufMask = N->getOperand(2);
3965 unsigned NumElts = ShufMask.getNumOperands();
3966
3967 // If the shuffle mask is an identity operation on the LHS, return the LHS.
3968 bool isIdentity = true;
3969 for (unsigned i = 0; i != NumElts; ++i) {
3970 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
3971 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
3972 isIdentity = false;
3973 break;
3974 }
3975 }
3976 if (isIdentity) return N->getOperand(0);
3977
3978 // If the shuffle mask is an identity operation on the RHS, return the RHS.
3979 isIdentity = true;
3980 for (unsigned i = 0; i != NumElts; ++i) {
3981 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
3982 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
3983 isIdentity = false;
3984 break;
3985 }
3986 }
3987 if (isIdentity) return N->getOperand(1);
Evan Chenge7bec0d2006-07-20 22:44:41 +00003988
3989 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
3990 // needed at all.
3991 bool isUnary = true;
Evan Cheng917ec982006-07-21 08:25:53 +00003992 bool isSplat = true;
Evan Chenge7bec0d2006-07-20 22:44:41 +00003993 int VecNum = -1;
Reid Spencer9160a6a2006-07-25 20:44:41 +00003994 unsigned BaseIdx = 0;
Evan Chenge7bec0d2006-07-20 22:44:41 +00003995 for (unsigned i = 0; i != NumElts; ++i)
3996 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
3997 unsigned Idx = cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue();
3998 int V = (Idx < NumElts) ? 0 : 1;
Evan Cheng917ec982006-07-21 08:25:53 +00003999 if (VecNum == -1) {
Evan Chenge7bec0d2006-07-20 22:44:41 +00004000 VecNum = V;
Evan Cheng917ec982006-07-21 08:25:53 +00004001 BaseIdx = Idx;
4002 } else {
4003 if (BaseIdx != Idx)
4004 isSplat = false;
4005 if (VecNum != V) {
4006 isUnary = false;
4007 break;
4008 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00004009 }
4010 }
4011
4012 SDOperand N0 = N->getOperand(0);
4013 SDOperand N1 = N->getOperand(1);
4014 // Normalize unary shuffle so the RHS is undef.
4015 if (isUnary && VecNum == 1)
4016 std::swap(N0, N1);
4017
Evan Cheng917ec982006-07-21 08:25:53 +00004018 // If it is a splat, check if the argument vector is a build_vector with
4019 // all scalar elements the same.
4020 if (isSplat) {
4021 SDNode *V = N0.Val;
Evan Cheng917ec982006-07-21 08:25:53 +00004022
Dan Gohman7f321562007-06-25 16:23:39 +00004023 // If this is a bit convert that changes the element type of the vector but
Evan Cheng59569222006-10-16 22:49:37 +00004024 // not the number of vector elements, look through it. Be careful not to
4025 // look though conversions that change things like v4f32 to v2f64.
Dan Gohman7f321562007-06-25 16:23:39 +00004026 if (V->getOpcode() == ISD::BIT_CONVERT) {
Evan Cheng59569222006-10-16 22:49:37 +00004027 SDOperand ConvInput = V->getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00004028 if (MVT::getVectorNumElements(ConvInput.getValueType()) == NumElts)
Evan Cheng59569222006-10-16 22:49:37 +00004029 V = ConvInput.Val;
4030 }
4031
Dan Gohman7f321562007-06-25 16:23:39 +00004032 if (V->getOpcode() == ISD::BUILD_VECTOR) {
4033 unsigned NumElems = V->getNumOperands();
Evan Cheng917ec982006-07-21 08:25:53 +00004034 if (NumElems > BaseIdx) {
4035 SDOperand Base;
4036 bool AllSame = true;
4037 for (unsigned i = 0; i != NumElems; ++i) {
4038 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
4039 Base = V->getOperand(i);
4040 break;
4041 }
4042 }
4043 // Splat of <u, u, u, u>, return <u, u, u, u>
4044 if (!Base.Val)
4045 return N0;
4046 for (unsigned i = 0; i != NumElems; ++i) {
4047 if (V->getOperand(i).getOpcode() != ISD::UNDEF &&
4048 V->getOperand(i) != Base) {
4049 AllSame = false;
4050 break;
4051 }
4052 }
4053 // Splat of <x, x, x, x>, return <x, x, x, x>
4054 if (AllSame)
4055 return N0;
4056 }
4057 }
4058 }
4059
Evan Chenge7bec0d2006-07-20 22:44:41 +00004060 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
4061 // into an undef.
4062 if (isUnary || N0 == N1) {
Chris Lattner17614ea2006-04-08 05:34:25 +00004063 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
4064 // first operand.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004065 SmallVector<SDOperand, 8> MappedOps;
Chris Lattner17614ea2006-04-08 05:34:25 +00004066 for (unsigned i = 0; i != NumElts; ++i) {
4067 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
4068 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) {
4069 MappedOps.push_back(ShufMask.getOperand(i));
4070 } else {
4071 unsigned NewIdx =
4072 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
4073 MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32));
4074 }
4075 }
Dan Gohman7f321562007-06-25 16:23:39 +00004076 ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMask.getValueType(),
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004077 &MappedOps[0], MappedOps.size());
Chris Lattner17614ea2006-04-08 05:34:25 +00004078 AddToWorkList(ShufMask.Val);
Dan Gohman7f321562007-06-25 16:23:39 +00004079 return DAG.getNode(ISD::VECTOR_SHUFFLE, N->getValueType(0),
4080 N0,
4081 DAG.getNode(ISD::UNDEF, N->getValueType(0)),
4082 ShufMask);
Chris Lattner17614ea2006-04-08 05:34:25 +00004083 }
Dan Gohman7f321562007-06-25 16:23:39 +00004084
Chris Lattnerf1d0c622006-03-31 22:16:43 +00004085 return SDOperand();
4086}
4087
Evan Cheng44f1f092006-04-20 08:56:16 +00004088/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
Dan Gohman7f321562007-06-25 16:23:39 +00004089/// an AND to a vector_shuffle with the destination vector and a zero vector.
4090/// e.g. AND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
Evan Cheng44f1f092006-04-20 08:56:16 +00004091/// vector_shuffle V, Zero, <0, 4, 2, 4>
4092SDOperand DAGCombiner::XformToShuffleWithZero(SDNode *N) {
4093 SDOperand LHS = N->getOperand(0);
4094 SDOperand RHS = N->getOperand(1);
Dan Gohman7f321562007-06-25 16:23:39 +00004095 if (N->getOpcode() == ISD::AND) {
4096 if (RHS.getOpcode() == ISD::BIT_CONVERT)
Evan Cheng44f1f092006-04-20 08:56:16 +00004097 RHS = RHS.getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00004098 if (RHS.getOpcode() == ISD::BUILD_VECTOR) {
Evan Cheng44f1f092006-04-20 08:56:16 +00004099 std::vector<SDOperand> IdxOps;
4100 unsigned NumOps = RHS.getNumOperands();
Dan Gohman7f321562007-06-25 16:23:39 +00004101 unsigned NumElts = NumOps;
4102 MVT::ValueType EVT = MVT::getVectorElementType(RHS.getValueType());
Evan Cheng44f1f092006-04-20 08:56:16 +00004103 for (unsigned i = 0; i != NumElts; ++i) {
4104 SDOperand Elt = RHS.getOperand(i);
4105 if (!isa<ConstantSDNode>(Elt))
4106 return SDOperand();
4107 else if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
4108 IdxOps.push_back(DAG.getConstant(i, EVT));
4109 else if (cast<ConstantSDNode>(Elt)->isNullValue())
4110 IdxOps.push_back(DAG.getConstant(NumElts, EVT));
4111 else
4112 return SDOperand();
4113 }
4114
4115 // Let's see if the target supports this vector_shuffle.
4116 if (!TLI.isVectorClearMaskLegal(IdxOps, EVT, DAG))
4117 return SDOperand();
4118
Dan Gohman7f321562007-06-25 16:23:39 +00004119 // Return the new VECTOR_SHUFFLE node.
4120 MVT::ValueType VT = MVT::getVectorType(EVT, NumElts);
Evan Cheng44f1f092006-04-20 08:56:16 +00004121 std::vector<SDOperand> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00004122 LHS = DAG.getNode(ISD::BIT_CONVERT, VT, LHS);
Evan Cheng44f1f092006-04-20 08:56:16 +00004123 Ops.push_back(LHS);
4124 AddToWorkList(LHS.Val);
4125 std::vector<SDOperand> ZeroOps(NumElts, DAG.getConstant(0, EVT));
Dan Gohman7f321562007-06-25 16:23:39 +00004126 Ops.push_back(DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004127 &ZeroOps[0], ZeroOps.size()));
Dan Gohman7f321562007-06-25 16:23:39 +00004128 Ops.push_back(DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004129 &IdxOps[0], IdxOps.size()));
Dan Gohman7f321562007-06-25 16:23:39 +00004130 SDOperand Result = DAG.getNode(ISD::VECTOR_SHUFFLE, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004131 &Ops[0], Ops.size());
Dan Gohman7f321562007-06-25 16:23:39 +00004132 if (VT != LHS.getValueType()) {
4133 Result = DAG.getNode(ISD::BIT_CONVERT, LHS.getValueType(), Result);
Evan Cheng44f1f092006-04-20 08:56:16 +00004134 }
4135 return Result;
4136 }
4137 }
4138 return SDOperand();
4139}
4140
Dan Gohman7f321562007-06-25 16:23:39 +00004141/// SimplifyVBinOp - Visit a binary vector operation, like ADD.
4142SDOperand DAGCombiner::SimplifyVBinOp(SDNode *N) {
4143 // After legalize, the target may be depending on adds and other
4144 // binary ops to provide legal ways to construct constants or other
4145 // things. Simplifying them may result in a loss of legality.
4146 if (AfterLegalize) return SDOperand();
4147
4148 MVT::ValueType VT = N->getValueType(0);
4149 if (!MVT::isVector(VT)) return SDOperand();
4150
4151 MVT::ValueType EltType = MVT::getVectorElementType(VT);
Chris Lattneredab1b92006-04-02 03:25:57 +00004152 SDOperand LHS = N->getOperand(0);
4153 SDOperand RHS = N->getOperand(1);
Evan Cheng44f1f092006-04-20 08:56:16 +00004154 SDOperand Shuffle = XformToShuffleWithZero(N);
4155 if (Shuffle.Val) return Shuffle;
4156
Dan Gohman7f321562007-06-25 16:23:39 +00004157 // If the LHS and RHS are BUILD_VECTOR nodes, see if we can constant fold
Chris Lattneredab1b92006-04-02 03:25:57 +00004158 // this operation.
Dan Gohman7f321562007-06-25 16:23:39 +00004159 if (LHS.getOpcode() == ISD::BUILD_VECTOR &&
4160 RHS.getOpcode() == ISD::BUILD_VECTOR) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004161 SmallVector<SDOperand, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00004162 for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) {
Chris Lattneredab1b92006-04-02 03:25:57 +00004163 SDOperand LHSOp = LHS.getOperand(i);
4164 SDOperand RHSOp = RHS.getOperand(i);
4165 // If these two elements can't be folded, bail out.
4166 if ((LHSOp.getOpcode() != ISD::UNDEF &&
4167 LHSOp.getOpcode() != ISD::Constant &&
4168 LHSOp.getOpcode() != ISD::ConstantFP) ||
4169 (RHSOp.getOpcode() != ISD::UNDEF &&
4170 RHSOp.getOpcode() != ISD::Constant &&
4171 RHSOp.getOpcode() != ISD::ConstantFP))
4172 break;
Evan Cheng7b336a82006-05-31 06:08:35 +00004173 // Can't fold divide by zero.
Dan Gohman7f321562007-06-25 16:23:39 +00004174 if (N->getOpcode() == ISD::SDIV || N->getOpcode() == ISD::UDIV ||
4175 N->getOpcode() == ISD::FDIV) {
Evan Cheng7b336a82006-05-31 06:08:35 +00004176 if ((RHSOp.getOpcode() == ISD::Constant &&
4177 cast<ConstantSDNode>(RHSOp.Val)->isNullValue()) ||
4178 (RHSOp.getOpcode() == ISD::ConstantFP &&
4179 !cast<ConstantFPSDNode>(RHSOp.Val)->getValue()))
4180 break;
4181 }
Dan Gohman7f321562007-06-25 16:23:39 +00004182 Ops.push_back(DAG.getNode(N->getOpcode(), EltType, LHSOp, RHSOp));
Chris Lattner3e104b12006-04-08 04:15:24 +00004183 AddToWorkList(Ops.back().Val);
Chris Lattneredab1b92006-04-02 03:25:57 +00004184 assert((Ops.back().getOpcode() == ISD::UNDEF ||
4185 Ops.back().getOpcode() == ISD::Constant ||
4186 Ops.back().getOpcode() == ISD::ConstantFP) &&
4187 "Scalar binop didn't fold!");
4188 }
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00004189
Dan Gohman7f321562007-06-25 16:23:39 +00004190 if (Ops.size() == LHS.getNumOperands()) {
4191 MVT::ValueType VT = LHS.getValueType();
4192 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00004193 }
Chris Lattneredab1b92006-04-02 03:25:57 +00004194 }
4195
4196 return SDOperand();
4197}
4198
Nate Begeman44728a72005-09-19 22:34:01 +00004199SDOperand DAGCombiner::SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2){
Nate Begemanf845b452005-10-08 00:29:44 +00004200 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
4201
4202 SDOperand SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
4203 cast<CondCodeSDNode>(N0.getOperand(2))->get());
4204 // If we got a simplified select_cc node back from SimplifySelectCC, then
4205 // break it down into a new SETCC node, and a new SELECT node, and then return
4206 // the SELECT node, since we were called with a SELECT node.
4207 if (SCC.Val) {
4208 // Check to see if we got a select_cc back (to turn into setcc/select).
4209 // Otherwise, just return whatever node we got back, like fabs.
4210 if (SCC.getOpcode() == ISD::SELECT_CC) {
4211 SDOperand SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
4212 SCC.getOperand(0), SCC.getOperand(1),
4213 SCC.getOperand(4));
Chris Lattner5750df92006-03-01 04:03:14 +00004214 AddToWorkList(SETCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004215 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
4216 SCC.getOperand(3), SETCC);
4217 }
4218 return SCC;
4219 }
Nate Begeman44728a72005-09-19 22:34:01 +00004220 return SDOperand();
4221}
4222
Chris Lattner40c62d52005-10-18 06:04:22 +00004223/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
4224/// are the two values being selected between, see if we can simplify the
Chris Lattner729c6d12006-05-27 00:43:02 +00004225/// select. Callers of this should assume that TheSelect is deleted if this
4226/// returns true. As such, they should return the appropriate thing (e.g. the
4227/// node) back to the top-level of the DAG combiner loop to avoid it being
4228/// looked at.
Chris Lattner40c62d52005-10-18 06:04:22 +00004229///
4230bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDOperand LHS,
4231 SDOperand RHS) {
4232
4233 // If this is a select from two identical things, try to pull the operation
4234 // through the select.
4235 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
Chris Lattner40c62d52005-10-18 06:04:22 +00004236 // If this is a load and the token chain is identical, replace the select
4237 // of two loads with a load through a select of the address to load from.
4238 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
4239 // constants have been dropped into the constant pool.
Evan Cheng466685d2006-10-09 20:57:25 +00004240 if (LHS.getOpcode() == ISD::LOAD &&
Chris Lattner40c62d52005-10-18 06:04:22 +00004241 // Token chains must be identical.
Evan Cheng466685d2006-10-09 20:57:25 +00004242 LHS.getOperand(0) == RHS.getOperand(0)) {
4243 LoadSDNode *LLD = cast<LoadSDNode>(LHS);
4244 LoadSDNode *RLD = cast<LoadSDNode>(RHS);
4245
4246 // If this is an EXTLOAD, the VT's must match.
Evan Cheng2e49f092006-10-11 07:10:22 +00004247 if (LLD->getLoadedVT() == RLD->getLoadedVT()) {
Evan Cheng466685d2006-10-09 20:57:25 +00004248 // FIXME: this conflates two src values, discarding one. This is not
4249 // the right thing to do, but nothing uses srcvalues now. When they do,
4250 // turn SrcValue into a list of locations.
4251 SDOperand Addr;
Chris Lattnerc4e664b2007-01-16 05:59:59 +00004252 if (TheSelect->getOpcode() == ISD::SELECT) {
4253 // Check that the condition doesn't reach either load. If so, folding
4254 // this will induce a cycle into the DAG.
4255 if (!LLD->isPredecessor(TheSelect->getOperand(0).Val) &&
4256 !RLD->isPredecessor(TheSelect->getOperand(0).Val)) {
4257 Addr = DAG.getNode(ISD::SELECT, LLD->getBasePtr().getValueType(),
4258 TheSelect->getOperand(0), LLD->getBasePtr(),
4259 RLD->getBasePtr());
4260 }
4261 } else {
4262 // Check that the condition doesn't reach either load. If so, folding
4263 // this will induce a cycle into the DAG.
4264 if (!LLD->isPredecessor(TheSelect->getOperand(0).Val) &&
4265 !RLD->isPredecessor(TheSelect->getOperand(0).Val) &&
4266 !LLD->isPredecessor(TheSelect->getOperand(1).Val) &&
4267 !RLD->isPredecessor(TheSelect->getOperand(1).Val)) {
4268 Addr = DAG.getNode(ISD::SELECT_CC, LLD->getBasePtr().getValueType(),
Evan Cheng466685d2006-10-09 20:57:25 +00004269 TheSelect->getOperand(0),
4270 TheSelect->getOperand(1),
4271 LLD->getBasePtr(), RLD->getBasePtr(),
4272 TheSelect->getOperand(4));
Chris Lattnerc4e664b2007-01-16 05:59:59 +00004273 }
Evan Cheng466685d2006-10-09 20:57:25 +00004274 }
Chris Lattnerc4e664b2007-01-16 05:59:59 +00004275
4276 if (Addr.Val) {
4277 SDOperand Load;
4278 if (LLD->getExtensionType() == ISD::NON_EXTLOAD)
4279 Load = DAG.getLoad(TheSelect->getValueType(0), LLD->getChain(),
4280 Addr,LLD->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00004281 LLD->getSrcValueOffset(),
4282 LLD->isVolatile(),
4283 LLD->getAlignment());
Chris Lattnerc4e664b2007-01-16 05:59:59 +00004284 else {
4285 Load = DAG.getExtLoad(LLD->getExtensionType(),
4286 TheSelect->getValueType(0),
4287 LLD->getChain(), Addr, LLD->getSrcValue(),
4288 LLD->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00004289 LLD->getLoadedVT(),
4290 LLD->isVolatile(),
4291 LLD->getAlignment());
Chris Lattnerc4e664b2007-01-16 05:59:59 +00004292 }
4293 // Users of the select now use the result of the load.
4294 CombineTo(TheSelect, Load);
4295
4296 // Users of the old loads now use the new load's chain. We know the
4297 // old-load value is dead now.
4298 CombineTo(LHS.Val, Load.getValue(0), Load.getValue(1));
4299 CombineTo(RHS.Val, Load.getValue(0), Load.getValue(1));
4300 return true;
4301 }
Evan Chengc5484282006-10-04 00:56:09 +00004302 }
Chris Lattner40c62d52005-10-18 06:04:22 +00004303 }
4304 }
4305
4306 return false;
4307}
4308
Nate Begeman44728a72005-09-19 22:34:01 +00004309SDOperand DAGCombiner::SimplifySelectCC(SDOperand N0, SDOperand N1,
4310 SDOperand N2, SDOperand N3,
Chris Lattner1eba01e2007-04-11 06:50:51 +00004311 ISD::CondCode CC, bool NotExtCompare) {
Nate Begemanf845b452005-10-08 00:29:44 +00004312
4313 MVT::ValueType VT = N2.getValueType();
Nate Begemanf845b452005-10-08 00:29:44 +00004314 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
4315 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
4316 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
4317
4318 // Determine if the condition we're dealing with is constant
4319 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
Chris Lattner30f73e72006-10-14 03:52:46 +00004320 if (SCC.Val) AddToWorkList(SCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004321 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
4322
4323 // fold select_cc true, x, y -> x
4324 if (SCCC && SCCC->getValue())
4325 return N2;
4326 // fold select_cc false, x, y -> y
4327 if (SCCC && SCCC->getValue() == 0)
4328 return N3;
4329
4330 // Check to see if we can simplify the select into an fabs node
4331 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
4332 // Allow either -0.0 or 0.0
4333 if (CFP->getValue() == 0.0) {
4334 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
4335 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
4336 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
4337 N2 == N3.getOperand(0))
4338 return DAG.getNode(ISD::FABS, VT, N0);
4339
4340 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
4341 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
4342 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
4343 N2.getOperand(0) == N3)
4344 return DAG.getNode(ISD::FABS, VT, N3);
4345 }
4346 }
4347
4348 // Check to see if we can perform the "gzip trick", transforming
4349 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
Chris Lattnere3152e52006-09-20 06:41:35 +00004350 if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT &&
Nate Begemanf845b452005-10-08 00:29:44 +00004351 MVT::isInteger(N0.getValueType()) &&
Chris Lattnere3152e52006-09-20 06:41:35 +00004352 MVT::isInteger(N2.getValueType()) &&
4353 (N1C->isNullValue() || // (a < 0) ? b : 0
4354 (N1C->getValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0
Nate Begemanf845b452005-10-08 00:29:44 +00004355 MVT::ValueType XType = N0.getValueType();
4356 MVT::ValueType AType = N2.getValueType();
4357 if (XType >= AType) {
4358 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00004359 // single-bit constant.
Nate Begemanf845b452005-10-08 00:29:44 +00004360 if (N2C && ((N2C->getValue() & (N2C->getValue()-1)) == 0)) {
4361 unsigned ShCtV = Log2_64(N2C->getValue());
4362 ShCtV = MVT::getSizeInBits(XType)-ShCtV-1;
4363 SDOperand ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
4364 SDOperand Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
Chris Lattner5750df92006-03-01 04:03:14 +00004365 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004366 if (XType > AType) {
4367 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00004368 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004369 }
4370 return DAG.getNode(ISD::AND, AType, Shift, N2);
4371 }
4372 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
4373 DAG.getConstant(MVT::getSizeInBits(XType)-1,
4374 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00004375 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004376 if (XType > AType) {
4377 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00004378 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004379 }
4380 return DAG.getNode(ISD::AND, AType, Shift, N2);
4381 }
4382 }
Nate Begeman07ed4172005-10-10 21:26:48 +00004383
4384 // fold select C, 16, 0 -> shl C, 4
4385 if (N2C && N3C && N3C->isNullValue() && isPowerOf2_64(N2C->getValue()) &&
4386 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) {
Chris Lattner1eba01e2007-04-11 06:50:51 +00004387
4388 // If the caller doesn't want us to simplify this into a zext of a compare,
4389 // don't do it.
4390 if (NotExtCompare && N2C->getValue() == 1)
4391 return SDOperand();
4392
Nate Begeman07ed4172005-10-10 21:26:48 +00004393 // Get a SetCC of the condition
4394 // FIXME: Should probably make sure that setcc is legal if we ever have a
4395 // target where it isn't.
Nate Begemanb0d04a72006-02-18 02:40:58 +00004396 SDOperand Temp, SCC;
Nate Begeman07ed4172005-10-10 21:26:48 +00004397 // cast from setcc result type to select result type
Nate Begemanb0d04a72006-02-18 02:40:58 +00004398 if (AfterLegalize) {
4399 SCC = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
Chris Lattner555d8d62006-12-07 22:36:47 +00004400 if (N2.getValueType() < SCC.getValueType())
4401 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
4402 else
4403 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00004404 } else {
4405 SCC = DAG.getSetCC(MVT::i1, N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00004406 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00004407 }
Chris Lattner5750df92006-03-01 04:03:14 +00004408 AddToWorkList(SCC.Val);
4409 AddToWorkList(Temp.Val);
Chris Lattnerc56a81d2007-04-11 06:43:25 +00004410
4411 if (N2C->getValue() == 1)
4412 return Temp;
Nate Begeman07ed4172005-10-10 21:26:48 +00004413 // shl setcc result by log2 n2c
4414 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
4415 DAG.getConstant(Log2_64(N2C->getValue()),
4416 TLI.getShiftAmountTy()));
4417 }
4418
Nate Begemanf845b452005-10-08 00:29:44 +00004419 // Check to see if this is the equivalent of setcc
4420 // FIXME: Turn all of these into setcc if setcc if setcc is legal
4421 // otherwise, go ahead with the folds.
4422 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getValue() == 1ULL)) {
4423 MVT::ValueType XType = N0.getValueType();
4424 if (TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultTy())) {
4425 SDOperand Res = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
4426 if (Res.getValueType() != VT)
4427 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
4428 return Res;
4429 }
4430
4431 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
4432 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
4433 TLI.isOperationLegal(ISD::CTLZ, XType)) {
4434 SDOperand Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
4435 return DAG.getNode(ISD::SRL, XType, Ctlz,
4436 DAG.getConstant(Log2_32(MVT::getSizeInBits(XType)),
4437 TLI.getShiftAmountTy()));
4438 }
4439 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
4440 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
4441 SDOperand NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
4442 N0);
4443 SDOperand NotN0 = DAG.getNode(ISD::XOR, XType, N0,
4444 DAG.getConstant(~0ULL, XType));
4445 return DAG.getNode(ISD::SRL, XType,
4446 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
4447 DAG.getConstant(MVT::getSizeInBits(XType)-1,
4448 TLI.getShiftAmountTy()));
4449 }
4450 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
4451 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
4452 SDOperand Sign = DAG.getNode(ISD::SRL, XType, N0,
4453 DAG.getConstant(MVT::getSizeInBits(XType)-1,
4454 TLI.getShiftAmountTy()));
4455 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
4456 }
4457 }
4458
4459 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
4460 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
4461 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
Chris Lattner1982ef22007-04-11 05:11:38 +00004462 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1) &&
4463 N2.getOperand(0) == N1 && MVT::isInteger(N0.getValueType())) {
4464 MVT::ValueType XType = N0.getValueType();
4465 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
4466 DAG.getConstant(MVT::getSizeInBits(XType)-1,
4467 TLI.getShiftAmountTy()));
4468 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
4469 AddToWorkList(Shift.Val);
4470 AddToWorkList(Add.Val);
4471 return DAG.getNode(ISD::XOR, XType, Add, Shift);
4472 }
4473 // Check to see if this is an integer abs. select_cc setgt X, -1, X, -X ->
4474 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
4475 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT &&
4476 N0 == N2 && N3.getOpcode() == ISD::SUB && N0 == N3.getOperand(1)) {
4477 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N3.getOperand(0))) {
Nate Begemanf845b452005-10-08 00:29:44 +00004478 MVT::ValueType XType = N0.getValueType();
4479 if (SubC->isNullValue() && MVT::isInteger(XType)) {
4480 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
4481 DAG.getConstant(MVT::getSizeInBits(XType)-1,
Chris Lattner1982ef22007-04-11 05:11:38 +00004482 TLI.getShiftAmountTy()));
Nate Begemanf845b452005-10-08 00:29:44 +00004483 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00004484 AddToWorkList(Shift.Val);
4485 AddToWorkList(Add.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004486 return DAG.getNode(ISD::XOR, XType, Add, Shift);
4487 }
4488 }
4489 }
Chris Lattner1982ef22007-04-11 05:11:38 +00004490
Nate Begeman44728a72005-09-19 22:34:01 +00004491 return SDOperand();
4492}
4493
Evan Chengfa1eb272007-02-08 22:13:59 +00004494/// SimplifySetCC - This is a stub for TargetLowering::SimplifySetCC.
Nate Begeman452d7be2005-09-16 00:54:12 +00004495SDOperand DAGCombiner::SimplifySetCC(MVT::ValueType VT, SDOperand N0,
Nate Begemane17daeb2005-10-05 21:43:42 +00004496 SDOperand N1, ISD::CondCode Cond,
4497 bool foldBooleans) {
Evan Chengfa1eb272007-02-08 22:13:59 +00004498 TargetLowering::DAGCombinerInfo
4499 DagCombineInfo(DAG, !AfterLegalize, false, this);
4500 return TLI.SimplifySetCC(VT, N0, N1, Cond, foldBooleans, DagCombineInfo);
Nate Begeman452d7be2005-09-16 00:54:12 +00004501}
4502
Nate Begeman69575232005-10-20 02:15:44 +00004503/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
4504/// return a DAG expression to select that will generate the same value by
4505/// multiplying by a magic number. See:
4506/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
4507SDOperand DAGCombiner::BuildSDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00004508 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00004509 SDOperand S = TLI.BuildSDIV(N, DAG, &Built);
4510
Andrew Lenharth232c9102006-06-12 16:07:18 +00004511 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00004512 ii != ee; ++ii)
4513 AddToWorkList(*ii);
4514 return S;
Nate Begeman69575232005-10-20 02:15:44 +00004515}
4516
4517/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
4518/// return a DAG expression to select that will generate the same value by
4519/// multiplying by a magic number. See:
4520/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
4521SDOperand DAGCombiner::BuildUDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00004522 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00004523 SDOperand S = TLI.BuildUDIV(N, DAG, &Built);
Nate Begeman69575232005-10-20 02:15:44 +00004524
Andrew Lenharth232c9102006-06-12 16:07:18 +00004525 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00004526 ii != ee; ++ii)
4527 AddToWorkList(*ii);
4528 return S;
Nate Begeman69575232005-10-20 02:15:44 +00004529}
4530
Jim Laskey71382342006-10-07 23:37:56 +00004531/// FindBaseOffset - Return true if base is known not to alias with anything
4532/// but itself. Provides base object and offset as results.
4533static bool FindBaseOffset(SDOperand Ptr, SDOperand &Base, int64_t &Offset) {
4534 // Assume it is a primitive operation.
4535 Base = Ptr; Offset = 0;
4536
4537 // If it's an adding a simple constant then integrate the offset.
4538 if (Base.getOpcode() == ISD::ADD) {
4539 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Base.getOperand(1))) {
4540 Base = Base.getOperand(0);
4541 Offset += C->getValue();
4542 }
4543 }
4544
4545 // If it's any of the following then it can't alias with anything but itself.
4546 return isa<FrameIndexSDNode>(Base) ||
4547 isa<ConstantPoolSDNode>(Base) ||
4548 isa<GlobalAddressSDNode>(Base);
4549}
4550
4551/// isAlias - Return true if there is any possibility that the two addresses
4552/// overlap.
Jim Laskey096c22e2006-10-18 12:29:57 +00004553bool DAGCombiner::isAlias(SDOperand Ptr1, int64_t Size1,
4554 const Value *SrcValue1, int SrcValueOffset1,
4555 SDOperand Ptr2, int64_t Size2,
4556 const Value *SrcValue2, int SrcValueOffset2)
4557{
Jim Laskey71382342006-10-07 23:37:56 +00004558 // If they are the same then they must be aliases.
4559 if (Ptr1 == Ptr2) return true;
4560
4561 // Gather base node and offset information.
4562 SDOperand Base1, Base2;
4563 int64_t Offset1, Offset2;
4564 bool KnownBase1 = FindBaseOffset(Ptr1, Base1, Offset1);
4565 bool KnownBase2 = FindBaseOffset(Ptr2, Base2, Offset2);
4566
4567 // If they have a same base address then...
4568 if (Base1 == Base2) {
4569 // Check to see if the addresses overlap.
4570 return!((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
4571 }
4572
Jim Laskey096c22e2006-10-18 12:29:57 +00004573 // If we know both bases then they can't alias.
4574 if (KnownBase1 && KnownBase2) return false;
4575
Jim Laskey07a27092006-10-18 19:08:31 +00004576 if (CombinerGlobalAA) {
4577 // Use alias analysis information.
4578 int Overlap1 = Size1 + SrcValueOffset1 + Offset1;
4579 int Overlap2 = Size2 + SrcValueOffset2 + Offset2;
4580 AliasAnalysis::AliasResult AAResult =
Jim Laskey096c22e2006-10-18 12:29:57 +00004581 AA.alias(SrcValue1, Overlap1, SrcValue2, Overlap2);
Jim Laskey07a27092006-10-18 19:08:31 +00004582 if (AAResult == AliasAnalysis::NoAlias)
4583 return false;
4584 }
Jim Laskey096c22e2006-10-18 12:29:57 +00004585
4586 // Otherwise we have to assume they alias.
4587 return true;
Jim Laskey71382342006-10-07 23:37:56 +00004588}
4589
4590/// FindAliasInfo - Extracts the relevant alias information from the memory
4591/// node. Returns true if the operand was a load.
Jim Laskey7ca56af2006-10-11 13:47:09 +00004592bool DAGCombiner::FindAliasInfo(SDNode *N,
Jim Laskey096c22e2006-10-18 12:29:57 +00004593 SDOperand &Ptr, int64_t &Size,
4594 const Value *&SrcValue, int &SrcValueOffset) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00004595 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
4596 Ptr = LD->getBasePtr();
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004597 Size = MVT::getSizeInBits(LD->getLoadedVT()) >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00004598 SrcValue = LD->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00004599 SrcValueOffset = LD->getSrcValueOffset();
Jim Laskey71382342006-10-07 23:37:56 +00004600 return true;
Jim Laskey7ca56af2006-10-11 13:47:09 +00004601 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00004602 Ptr = ST->getBasePtr();
Evan Cheng8b2794a2006-10-13 21:14:26 +00004603 Size = MVT::getSizeInBits(ST->getStoredVT()) >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00004604 SrcValue = ST->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00004605 SrcValueOffset = ST->getSrcValueOffset();
Jim Laskey7ca56af2006-10-11 13:47:09 +00004606 } else {
Jim Laskey71382342006-10-07 23:37:56 +00004607 assert(0 && "FindAliasInfo expected a memory operand");
Jim Laskey71382342006-10-07 23:37:56 +00004608 }
4609
4610 return false;
4611}
4612
Jim Laskey6ff23e52006-10-04 16:53:27 +00004613/// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
4614/// looking for aliasing nodes and adding them to the Aliases vector.
Jim Laskeybc588b82006-10-05 15:07:25 +00004615void DAGCombiner::GatherAllAliases(SDNode *N, SDOperand OriginalChain,
Jim Laskey6ff23e52006-10-04 16:53:27 +00004616 SmallVector<SDOperand, 8> &Aliases) {
Jim Laskeybc588b82006-10-05 15:07:25 +00004617 SmallVector<SDOperand, 8> Chains; // List of chains to visit.
Jim Laskey6ff23e52006-10-04 16:53:27 +00004618 std::set<SDNode *> Visited; // Visited node set.
4619
Jim Laskey279f0532006-09-25 16:29:54 +00004620 // Get alias information for node.
4621 SDOperand Ptr;
4622 int64_t Size;
Jim Laskey7ca56af2006-10-11 13:47:09 +00004623 const Value *SrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00004624 int SrcValueOffset;
4625 bool IsLoad = FindAliasInfo(N, Ptr, Size, SrcValue, SrcValueOffset);
Jim Laskey279f0532006-09-25 16:29:54 +00004626
Jim Laskey6ff23e52006-10-04 16:53:27 +00004627 // Starting off.
Jim Laskeybc588b82006-10-05 15:07:25 +00004628 Chains.push_back(OriginalChain);
Jim Laskey6ff23e52006-10-04 16:53:27 +00004629
Jim Laskeybc588b82006-10-05 15:07:25 +00004630 // Look at each chain and determine if it is an alias. If so, add it to the
4631 // aliases list. If not, then continue up the chain looking for the next
4632 // candidate.
4633 while (!Chains.empty()) {
4634 SDOperand Chain = Chains.back();
4635 Chains.pop_back();
Jim Laskey6ff23e52006-10-04 16:53:27 +00004636
Jim Laskeybc588b82006-10-05 15:07:25 +00004637 // Don't bother if we've been before.
4638 if (Visited.find(Chain.Val) != Visited.end()) continue;
4639 Visited.insert(Chain.Val);
4640
4641 switch (Chain.getOpcode()) {
4642 case ISD::EntryToken:
4643 // Entry token is ideal chain operand, but handled in FindBetterChain.
4644 break;
Jim Laskey6ff23e52006-10-04 16:53:27 +00004645
Jim Laskeybc588b82006-10-05 15:07:25 +00004646 case ISD::LOAD:
4647 case ISD::STORE: {
4648 // Get alias information for Chain.
4649 SDOperand OpPtr;
4650 int64_t OpSize;
Jim Laskey7ca56af2006-10-11 13:47:09 +00004651 const Value *OpSrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00004652 int OpSrcValueOffset;
4653 bool IsOpLoad = FindAliasInfo(Chain.Val, OpPtr, OpSize,
4654 OpSrcValue, OpSrcValueOffset);
Jim Laskeybc588b82006-10-05 15:07:25 +00004655
4656 // If chain is alias then stop here.
4657 if (!(IsLoad && IsOpLoad) &&
Jim Laskey096c22e2006-10-18 12:29:57 +00004658 isAlias(Ptr, Size, SrcValue, SrcValueOffset,
4659 OpPtr, OpSize, OpSrcValue, OpSrcValueOffset)) {
Jim Laskeybc588b82006-10-05 15:07:25 +00004660 Aliases.push_back(Chain);
4661 } else {
4662 // Look further up the chain.
4663 Chains.push_back(Chain.getOperand(0));
4664 // Clean up old chain.
4665 AddToWorkList(Chain.Val);
Jim Laskey279f0532006-09-25 16:29:54 +00004666 }
Jim Laskeybc588b82006-10-05 15:07:25 +00004667 break;
4668 }
4669
4670 case ISD::TokenFactor:
4671 // We have to check each of the operands of the token factor, so we queue
4672 // then up. Adding the operands to the queue (stack) in reverse order
4673 // maintains the original order and increases the likelihood that getNode
4674 // will find a matching token factor (CSE.)
4675 for (unsigned n = Chain.getNumOperands(); n;)
4676 Chains.push_back(Chain.getOperand(--n));
4677 // Eliminate the token factor if we can.
4678 AddToWorkList(Chain.Val);
4679 break;
4680
4681 default:
4682 // For all other instructions we will just have to take what we can get.
4683 Aliases.push_back(Chain);
4684 break;
Jim Laskey279f0532006-09-25 16:29:54 +00004685 }
4686 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00004687}
4688
4689/// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, looking
4690/// for a better chain (aliasing node.)
4691SDOperand DAGCombiner::FindBetterChain(SDNode *N, SDOperand OldChain) {
4692 SmallVector<SDOperand, 8> Aliases; // Ops for replacing token factor.
Jim Laskey279f0532006-09-25 16:29:54 +00004693
Jim Laskey6ff23e52006-10-04 16:53:27 +00004694 // Accumulate all the aliases to this node.
4695 GatherAllAliases(N, OldChain, Aliases);
4696
4697 if (Aliases.size() == 0) {
4698 // If no operands then chain to entry token.
4699 return DAG.getEntryNode();
4700 } else if (Aliases.size() == 1) {
4701 // If a single operand then chain to it. We don't need to revisit it.
4702 return Aliases[0];
4703 }
4704
4705 // Construct a custom tailored token factor.
4706 SDOperand NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
4707 &Aliases[0], Aliases.size());
4708
4709 // Make sure the old chain gets cleaned up.
4710 if (NewChain != OldChain) AddToWorkList(OldChain.Val);
4711
4712 return NewChain;
Jim Laskey279f0532006-09-25 16:29:54 +00004713}
4714
Nate Begeman1d4d4142005-09-01 00:19:25 +00004715// SelectionDAG::Combine - This is the entry point for the file.
4716//
Jim Laskeyc7c3f112006-10-16 20:52:31 +00004717void SelectionDAG::Combine(bool RunningAfterLegalize, AliasAnalysis &AA) {
Chris Lattner938ab022007-01-16 04:55:25 +00004718 if (!RunningAfterLegalize && ViewDAGCombine1)
4719 viewGraph();
4720 if (RunningAfterLegalize && ViewDAGCombine2)
4721 viewGraph();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004722 /// run - This is the main entry point to this class.
4723 ///
Jim Laskeyc7c3f112006-10-16 20:52:31 +00004724 DAGCombiner(*this, AA).Run(RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004725}