blob: f6e0a2276f129f3d4595c7ef0734edcbdc99bd60 [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;
Bill Wendling832171c2006-12-07 20:04:42 +0000116 DOUT << "\nReplacing.1 "; DEBUG(N->dump());
117 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;
Bill Wendling832171c2006-12-07 20:04:42 +0000167 DOUT << "\nReplacing.2 "; DEBUG(TLO.Old.Val->dump());
168 DOUT << "\nWith: "; DEBUG(TLO.New.Val->dump(&DAG));
169 DOUT << '\n';
Chris Lattner012f2412006-02-17 21:58:01 +0000170
171 std::vector<SDNode*> NowDead;
172 DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New, NowDead);
173
Chris Lattner7d20d392006-02-20 06:51:04 +0000174 // Push the new node and any (possibly new) users onto the worklist.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000175 AddToWorkList(TLO.New.Val);
Chris Lattner012f2412006-02-17 21:58:01 +0000176 AddUsersToWorkList(TLO.New.Val);
177
178 // Nodes can end up on the worklist more than once. Make sure we do
179 // not process a node that has been replaced.
Chris Lattner012f2412006-02-17 21:58:01 +0000180 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
181 removeFromWorkList(NowDead[i]);
182
Chris Lattner7d20d392006-02-20 06:51:04 +0000183 // Finally, if the node is now dead, remove it from the graph. The node
184 // may not be dead if the replacement process recursively simplified to
185 // something else needing this node.
186 if (TLO.Old.Val->use_empty()) {
187 removeFromWorkList(TLO.Old.Val);
Chris Lattnerec06e9a2007-04-18 03:05:22 +0000188
189 // If the operands of this node are only used by the node, they will now
190 // be dead. Make sure to visit them first to delete dead nodes early.
191 for (unsigned i = 0, e = TLO.Old.Val->getNumOperands(); i != e; ++i)
192 if (TLO.Old.Val->getOperand(i).Val->hasOneUse())
193 AddToWorkList(TLO.Old.Val->getOperand(i).Val);
194
Chris Lattner7d20d392006-02-20 06:51:04 +0000195 DAG.DeleteNode(TLO.Old.Val);
196 }
Chris Lattner012f2412006-02-17 21:58:01 +0000197 return true;
Nate Begeman368e18d2006-02-16 21:11:51 +0000198 }
Chris Lattner87514ca2005-10-10 22:31:19 +0000199
Chris Lattner448f2192006-11-11 00:39:41 +0000200 bool CombineToPreIndexedLoadStore(SDNode *N);
201 bool CombineToPostIndexedLoadStore(SDNode *N);
202
203
Nate Begeman1d4d4142005-09-01 00:19:25 +0000204 /// visit - call the node-specific routine that knows how to fold each
205 /// particular type of node.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000206 SDOperand visit(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000207
208 // Visitation implementation - Implement dag node combining for different
209 // node types. The semantics are as follows:
210 // Return Value:
Nate Begeman2300f552005-09-07 00:15:36 +0000211 // SDOperand.Val == 0 - No change was made
Chris Lattner01a22022005-10-10 22:04:48 +0000212 // SDOperand.Val == N - N was replaced, is dead, and is already handled.
Nate Begeman2300f552005-09-07 00:15:36 +0000213 // otherwise - N should be replaced by the returned Operand.
Nate Begeman1d4d4142005-09-01 00:19:25 +0000214 //
Nate Begeman83e75ec2005-09-06 04:43:02 +0000215 SDOperand visitTokenFactor(SDNode *N);
216 SDOperand visitADD(SDNode *N);
217 SDOperand visitSUB(SDNode *N);
Chris Lattner91153682007-03-04 20:03:15 +0000218 SDOperand visitADDC(SDNode *N);
219 SDOperand visitADDE(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000220 SDOperand visitMUL(SDNode *N);
221 SDOperand visitSDIV(SDNode *N);
222 SDOperand visitUDIV(SDNode *N);
223 SDOperand visitSREM(SDNode *N);
224 SDOperand visitUREM(SDNode *N);
225 SDOperand visitMULHU(SDNode *N);
226 SDOperand visitMULHS(SDNode *N);
227 SDOperand visitAND(SDNode *N);
228 SDOperand visitOR(SDNode *N);
229 SDOperand visitXOR(SDNode *N);
Chris Lattneredab1b92006-04-02 03:25:57 +0000230 SDOperand visitVBinOp(SDNode *N, ISD::NodeType IntOp, ISD::NodeType FPOp);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000231 SDOperand visitSHL(SDNode *N);
232 SDOperand visitSRA(SDNode *N);
233 SDOperand visitSRL(SDNode *N);
234 SDOperand visitCTLZ(SDNode *N);
235 SDOperand visitCTTZ(SDNode *N);
236 SDOperand visitCTPOP(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000237 SDOperand visitSELECT(SDNode *N);
238 SDOperand visitSELECT_CC(SDNode *N);
239 SDOperand visitSETCC(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000240 SDOperand visitSIGN_EXTEND(SDNode *N);
241 SDOperand visitZERO_EXTEND(SDNode *N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000242 SDOperand visitANY_EXTEND(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000243 SDOperand visitSIGN_EXTEND_INREG(SDNode *N);
244 SDOperand visitTRUNCATE(SDNode *N);
Chris Lattner94683772005-12-23 05:30:37 +0000245 SDOperand visitBIT_CONVERT(SDNode *N);
Chris Lattner6258fb22006-04-02 02:53:43 +0000246 SDOperand visitVBIT_CONVERT(SDNode *N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000247 SDOperand visitFADD(SDNode *N);
248 SDOperand visitFSUB(SDNode *N);
249 SDOperand visitFMUL(SDNode *N);
250 SDOperand visitFDIV(SDNode *N);
251 SDOperand visitFREM(SDNode *N);
Chris Lattner12d83032006-03-05 05:30:57 +0000252 SDOperand visitFCOPYSIGN(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000253 SDOperand visitSINT_TO_FP(SDNode *N);
254 SDOperand visitUINT_TO_FP(SDNode *N);
255 SDOperand visitFP_TO_SINT(SDNode *N);
256 SDOperand visitFP_TO_UINT(SDNode *N);
257 SDOperand visitFP_ROUND(SDNode *N);
258 SDOperand visitFP_ROUND_INREG(SDNode *N);
259 SDOperand visitFP_EXTEND(SDNode *N);
260 SDOperand visitFNEG(SDNode *N);
261 SDOperand visitFABS(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000262 SDOperand visitBRCOND(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000263 SDOperand visitBR_CC(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000264 SDOperand visitLOAD(SDNode *N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000265 SDOperand visitSTORE(SDNode *N);
Chris Lattnerca242442006-03-19 01:27:56 +0000266 SDOperand visitINSERT_VECTOR_ELT(SDNode *N);
267 SDOperand visitVINSERT_VECTOR_ELT(SDNode *N);
Chris Lattnerd7648c82006-03-28 20:28:38 +0000268 SDOperand visitVBUILD_VECTOR(SDNode *N);
Chris Lattner66445d32006-03-28 22:11:53 +0000269 SDOperand visitVECTOR_SHUFFLE(SDNode *N);
Chris Lattnerf1d0c622006-03-31 22:16:43 +0000270 SDOperand visitVVECTOR_SHUFFLE(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000271
Evan Cheng44f1f092006-04-20 08:56:16 +0000272 SDOperand XformToShuffleWithZero(SDNode *N);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000273 SDOperand ReassociateOps(unsigned Opc, SDOperand LHS, SDOperand RHS);
274
Chris Lattner40c62d52005-10-18 06:04:22 +0000275 bool SimplifySelectOps(SDNode *SELECT, SDOperand LHS, SDOperand RHS);
Chris Lattner35e5c142006-05-05 05:51:50 +0000276 SDOperand SimplifyBinOpWithSameOpcodeHands(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000277 SDOperand SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2);
278 SDOperand SimplifySelectCC(SDOperand N0, SDOperand N1, SDOperand N2,
Chris Lattner1eba01e2007-04-11 06:50:51 +0000279 SDOperand N3, ISD::CondCode CC,
280 bool NotExtCompare = false);
Nate Begeman452d7be2005-09-16 00:54:12 +0000281 SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N0, SDOperand N1,
Nate Begemane17daeb2005-10-05 21:43:42 +0000282 ISD::CondCode Cond, bool foldBooleans = true);
Chris Lattner6258fb22006-04-02 02:53:43 +0000283 SDOperand ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(SDNode *, MVT::ValueType);
Nate Begeman69575232005-10-20 02:15:44 +0000284 SDOperand BuildSDIV(SDNode *N);
Chris Lattner516b9622006-09-14 20:50:57 +0000285 SDOperand BuildUDIV(SDNode *N);
286 SDNode *MatchRotate(SDOperand LHS, SDOperand RHS);
Evan Chengc88138f2007-03-22 01:54:19 +0000287 SDOperand ReduceLoadWidth(SDNode *N);
Jim Laskey279f0532006-09-25 16:29:54 +0000288
Jim Laskey6ff23e52006-10-04 16:53:27 +0000289 /// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
290 /// looking for aliasing nodes and adding them to the Aliases vector.
Jim Laskeybc588b82006-10-05 15:07:25 +0000291 void GatherAllAliases(SDNode *N, SDOperand OriginalChain,
Jim Laskey6ff23e52006-10-04 16:53:27 +0000292 SmallVector<SDOperand, 8> &Aliases);
293
Jim Laskey096c22e2006-10-18 12:29:57 +0000294 /// isAlias - Return true if there is any possibility that the two addresses
295 /// overlap.
296 bool isAlias(SDOperand Ptr1, int64_t Size1,
297 const Value *SrcValue1, int SrcValueOffset1,
298 SDOperand Ptr2, int64_t Size2,
Jeff Cohend41b30d2006-11-05 19:31:28 +0000299 const Value *SrcValue2, int SrcValueOffset2);
Jim Laskey096c22e2006-10-18 12:29:57 +0000300
Jim Laskey7ca56af2006-10-11 13:47:09 +0000301 /// FindAliasInfo - Extracts the relevant alias information from the memory
302 /// node. Returns true if the operand was a load.
303 bool FindAliasInfo(SDNode *N,
Jim Laskey096c22e2006-10-18 12:29:57 +0000304 SDOperand &Ptr, int64_t &Size,
305 const Value *&SrcValue, int &SrcValueOffset);
Jim Laskey7ca56af2006-10-11 13:47:09 +0000306
Jim Laskey279f0532006-09-25 16:29:54 +0000307 /// FindBetterChain - Walk up chain skipping non-aliasing memory nodes,
Jim Laskey6ff23e52006-10-04 16:53:27 +0000308 /// looking for a better chain (aliasing node.)
Jim Laskey279f0532006-09-25 16:29:54 +0000309 SDOperand FindBetterChain(SDNode *N, SDOperand Chain);
310
Nate Begeman1d4d4142005-09-01 00:19:25 +0000311public:
Jim Laskeyc7c3f112006-10-16 20:52:31 +0000312 DAGCombiner(SelectionDAG &D, AliasAnalysis &A)
313 : DAG(D),
314 TLI(D.getTargetLoweringInfo()),
315 AfterLegalize(false),
316 AA(A) {}
Nate Begeman1d4d4142005-09-01 00:19:25 +0000317
318 /// Run - runs the dag combiner on all nodes in the work list
Nate Begeman4ebd8052005-09-01 23:24:04 +0000319 void Run(bool RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000320 };
321}
322
Chris Lattner24664722006-03-01 04:53:38 +0000323//===----------------------------------------------------------------------===//
324// TargetLowering::DAGCombinerInfo implementation
325//===----------------------------------------------------------------------===//
326
327void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) {
328 ((DAGCombiner*)DC)->AddToWorkList(N);
329}
330
331SDOperand TargetLowering::DAGCombinerInfo::
332CombineTo(SDNode *N, const std::vector<SDOperand> &To) {
Chris Lattner3577e382006-08-11 17:56:38 +0000333 return ((DAGCombiner*)DC)->CombineTo(N, &To[0], To.size());
Chris Lattner24664722006-03-01 04:53:38 +0000334}
335
336SDOperand TargetLowering::DAGCombinerInfo::
337CombineTo(SDNode *N, SDOperand Res) {
338 return ((DAGCombiner*)DC)->CombineTo(N, Res);
339}
340
341
342SDOperand TargetLowering::DAGCombinerInfo::
343CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1) {
344 return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1);
345}
346
347
Chris Lattner24664722006-03-01 04:53:38 +0000348//===----------------------------------------------------------------------===//
Chris Lattner29446522007-05-14 22:04:50 +0000349// Helper Functions
350//===----------------------------------------------------------------------===//
351
352/// isNegatibleForFree - Return 1 if we can compute the negated form of the
353/// specified expression for the same cost as the expression itself, or 2 if we
354/// can compute the negated form more cheaply than the expression itself.
355static char isNegatibleForFree(SDOperand Op) {
356 // fneg is removable even if it has multiple uses.
357 if (Op.getOpcode() == ISD::FNEG) return 2;
358
359 // Don't allow anything with multiple uses.
360 if (!Op.hasOneUse()) return 0;
361
362 switch (Op.getOpcode()) {
363 default: return false;
364 case ISD::ConstantFP:
365 return 1;
366 case ISD::FADD:
367 // FIXME: determine better conditions for this xform.
368 if (!UnsafeFPMath) return 0;
369
370 // -(A+B) -> -A - B
371 if (char V = isNegatibleForFree(Op.getOperand(0)))
372 return V;
373 // -(A+B) -> -B - A
374 return isNegatibleForFree(Op.getOperand(1));
375 case ISD::FSUB:
376 // We can't turn -(A-B) into B-A when we honor signed zeros.
377 if (!UnsafeFPMath) return 0;
378
379 // -(A-B) -> B-A
380 return 1;
381
382 case ISD::FMUL:
383 case ISD::FDIV:
384 if (HonorSignDependentRoundingFPMath()) return 0;
385
386 // -(X*Y) -> (-X * Y) or (X*-Y)
387 if (char V = isNegatibleForFree(Op.getOperand(0)))
388 return V;
389
390 return isNegatibleForFree(Op.getOperand(1));
391
392 case ISD::FP_EXTEND:
393 case ISD::FP_ROUND:
394 case ISD::FSIN:
395 return isNegatibleForFree(Op.getOperand(0));
396 }
397}
398
399/// GetNegatedExpression - If isNegatibleForFree returns true, this function
400/// returns the newly negated expression.
401static SDOperand GetNegatedExpression(SDOperand Op, SelectionDAG &DAG) {
402 // fneg is removable even if it has multiple uses.
403 if (Op.getOpcode() == ISD::FNEG) return Op.getOperand(0);
404
405 // Don't allow anything with multiple uses.
406 assert(Op.hasOneUse() && "Unknown reuse!");
407
408 switch (Op.getOpcode()) {
409 default: assert(0 && "Unknown code");
410 case ISD::ConstantFP:
411 return DAG.getConstantFP(-cast<ConstantFPSDNode>(Op)->getValue(),
412 Op.getValueType());
413 case ISD::FADD:
414 // FIXME: determine better conditions for this xform.
415 assert(UnsafeFPMath);
416
417 // -(A+B) -> -A - B
418 if (isNegatibleForFree(Op.getOperand(0)))
419 return DAG.getNode(ISD::FSUB, Op.getValueType(),
420 GetNegatedExpression(Op.getOperand(0), DAG),
421 Op.getOperand(1));
422 // -(A+B) -> -B - A
423 return DAG.getNode(ISD::FSUB, Op.getValueType(),
424 GetNegatedExpression(Op.getOperand(1), DAG),
425 Op.getOperand(0));
426 case ISD::FSUB:
427 // We can't turn -(A-B) into B-A when we honor signed zeros.
428 assert(UnsafeFPMath);
429
430 // -(A-B) -> B-A
431 return DAG.getNode(ISD::FSUB, Op.getValueType(), Op.getOperand(1),
432 Op.getOperand(0));
433
434 case ISD::FMUL:
435 case ISD::FDIV:
436 assert(!HonorSignDependentRoundingFPMath());
437
438 // -(X*Y) -> -X * Y
439 if (isNegatibleForFree(Op.getOperand(0)))
440 return DAG.getNode(Op.getOpcode(), Op.getValueType(),
441 GetNegatedExpression(Op.getOperand(0), DAG),
442 Op.getOperand(1));
443
444 // -(X*Y) -> X * -Y
445 return DAG.getNode(Op.getOpcode(), Op.getValueType(),
446 Op.getOperand(0),
447 GetNegatedExpression(Op.getOperand(1), DAG));
448
449 case ISD::FP_EXTEND:
450 case ISD::FP_ROUND:
451 case ISD::FSIN:
452 return DAG.getNode(Op.getOpcode(), Op.getValueType(),
Lauro Ramos Venanciob5bb7ff2007-05-15 17:05:43 +0000453 GetNegatedExpression(Op.getOperand(0), DAG));
Chris Lattner29446522007-05-14 22:04:50 +0000454 }
455}
Chris Lattner24664722006-03-01 04:53:38 +0000456
457
Nate Begeman4ebd8052005-09-01 23:24:04 +0000458// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
459// that selects between the values 1 and 0, making it equivalent to a setcc.
Nate Begeman646d7e22005-09-02 21:18:40 +0000460// Also, set the incoming LHS, RHS, and CC references to the appropriate
461// nodes based on the type of node we are checking. This simplifies life a
462// bit for the callers.
463static bool isSetCCEquivalent(SDOperand N, SDOperand &LHS, SDOperand &RHS,
464 SDOperand &CC) {
465 if (N.getOpcode() == ISD::SETCC) {
466 LHS = N.getOperand(0);
467 RHS = N.getOperand(1);
468 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000469 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000470 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000471 if (N.getOpcode() == ISD::SELECT_CC &&
472 N.getOperand(2).getOpcode() == ISD::Constant &&
473 N.getOperand(3).getOpcode() == ISD::Constant &&
474 cast<ConstantSDNode>(N.getOperand(2))->getValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000475 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
476 LHS = N.getOperand(0);
477 RHS = N.getOperand(1);
478 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000479 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000480 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000481 return false;
482}
483
Nate Begeman99801192005-09-07 23:25:52 +0000484// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
485// one use. If this is true, it allows the users to invert the operation for
486// free when it is profitable to do so.
487static bool isOneUseSetCC(SDOperand N) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000488 SDOperand N0, N1, N2;
Nate Begeman646d7e22005-09-02 21:18:40 +0000489 if (isSetCCEquivalent(N, N0, N1, N2) && N.Val->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000490 return true;
491 return false;
492}
493
Nate Begemancd4d58c2006-02-03 06:46:56 +0000494SDOperand DAGCombiner::ReassociateOps(unsigned Opc, SDOperand N0, SDOperand N1){
495 MVT::ValueType VT = N0.getValueType();
496 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
497 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
498 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
499 if (isa<ConstantSDNode>(N1)) {
500 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(1), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000501 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000502 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(0));
503 } else if (N0.hasOneUse()) {
504 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(0), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000505 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000506 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(1));
507 }
508 }
509 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
510 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
511 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
512 if (isa<ConstantSDNode>(N0)) {
513 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(1), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000514 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000515 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(0));
516 } else if (N1.hasOneUse()) {
517 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(0), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000518 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000519 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(1));
520 }
521 }
522 return SDOperand();
523}
524
Chris Lattner29446522007-05-14 22:04:50 +0000525//===----------------------------------------------------------------------===//
526// Main DAG Combiner implementation
527//===----------------------------------------------------------------------===//
528
Nate Begeman4ebd8052005-09-01 23:24:04 +0000529void DAGCombiner::Run(bool RunningAfterLegalize) {
530 // set the instance variable, so that the various visit routines may use it.
531 AfterLegalize = RunningAfterLegalize;
532
Nate Begeman646d7e22005-09-02 21:18:40 +0000533 // Add all the dag nodes to the worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000534 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
535 E = DAG.allnodes_end(); I != E; ++I)
536 WorkList.push_back(I);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000537
Chris Lattner95038592005-10-05 06:35:28 +0000538 // Create a dummy node (which is not added to allnodes), that adds a reference
539 // to the root node, preventing it from being deleted, and tracking any
540 // changes of the root.
541 HandleSDNode Dummy(DAG.getRoot());
542
Jim Laskey26f7fa72006-10-17 19:33:52 +0000543 // The root of the dag may dangle to deleted nodes until the dag combiner is
544 // done. Set it to null to avoid confusion.
545 DAG.setRoot(SDOperand());
Chris Lattner24664722006-03-01 04:53:38 +0000546
547 /// DagCombineInfo - Expose the DAG combiner to the target combiner impls.
548 TargetLowering::DAGCombinerInfo
Evan Chengfa1eb272007-02-08 22:13:59 +0000549 DagCombineInfo(DAG, !RunningAfterLegalize, false, this);
Jim Laskey6ff23e52006-10-04 16:53:27 +0000550
Nate Begeman1d4d4142005-09-01 00:19:25 +0000551 // while the worklist isn't empty, inspect the node on the end of it and
552 // try and combine it.
553 while (!WorkList.empty()) {
554 SDNode *N = WorkList.back();
555 WorkList.pop_back();
556
557 // If N has no uses, it is dead. Make sure to revisit all N's operands once
Chris Lattner95038592005-10-05 06:35:28 +0000558 // N is deleted from the DAG, since they too may now be dead or may have a
559 // reduced number of uses, allowing other xforms.
560 if (N->use_empty() && N != &Dummy) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000561 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Jim Laskey6ff23e52006-10-04 16:53:27 +0000562 AddToWorkList(N->getOperand(i).Val);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000563
Chris Lattner95038592005-10-05 06:35:28 +0000564 DAG.DeleteNode(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000565 continue;
566 }
567
Nate Begeman83e75ec2005-09-06 04:43:02 +0000568 SDOperand RV = visit(N);
Chris Lattner24664722006-03-01 04:53:38 +0000569
570 // If nothing happened, try a target-specific DAG combine.
571 if (RV.Val == 0) {
Chris Lattner729c6d12006-05-27 00:43:02 +0000572 assert(N->getOpcode() != ISD::DELETED_NODE &&
573 "Node was deleted but visit returned NULL!");
Chris Lattner24664722006-03-01 04:53:38 +0000574 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
575 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode()))
576 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
577 }
578
Nate Begeman83e75ec2005-09-06 04:43:02 +0000579 if (RV.Val) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000580 ++NodesCombined;
Nate Begeman646d7e22005-09-02 21:18:40 +0000581 // If we get back the same node we passed in, rather than a new node or
582 // zero, we know that the node must have defined multiple values and
583 // CombineTo was used. Since CombineTo takes care of the worklist
584 // mechanics for us, we have no work to do in this case.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000585 if (RV.Val != N) {
Chris Lattner729c6d12006-05-27 00:43:02 +0000586 assert(N->getOpcode() != ISD::DELETED_NODE &&
587 RV.Val->getOpcode() != ISD::DELETED_NODE &&
588 "Node was deleted but visit returned new node!");
589
Bill Wendling832171c2006-12-07 20:04:42 +0000590 DOUT << "\nReplacing.3 "; DEBUG(N->dump());
591 DOUT << "\nWith: "; DEBUG(RV.Val->dump(&DAG));
592 DOUT << '\n';
Chris Lattner01a22022005-10-10 22:04:48 +0000593 std::vector<SDNode*> NowDead;
Evan Cheng2adffa12006-09-21 19:04:05 +0000594 if (N->getNumValues() == RV.Val->getNumValues())
595 DAG.ReplaceAllUsesWith(N, RV.Val, &NowDead);
596 else {
597 assert(N->getValueType(0) == RV.getValueType() && "Type mismatch");
598 SDOperand OpV = RV;
599 DAG.ReplaceAllUsesWith(N, &OpV, &NowDead);
600 }
Nate Begeman646d7e22005-09-02 21:18:40 +0000601
602 // Push the new node and any users onto the worklist
Jim Laskey6ff23e52006-10-04 16:53:27 +0000603 AddToWorkList(RV.Val);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000604 AddUsersToWorkList(RV.Val);
Nate Begeman646d7e22005-09-02 21:18:40 +0000605
Jim Laskey6ff23e52006-10-04 16:53:27 +0000606 // Nodes can be reintroduced into the worklist. Make sure we do not
607 // process a node that has been replaced.
Nate Begeman646d7e22005-09-02 21:18:40 +0000608 removeFromWorkList(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000609 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
610 removeFromWorkList(NowDead[i]);
Chris Lattner5c46f742005-10-05 06:11:08 +0000611
612 // Finally, since the node is now dead, remove it from the graph.
613 DAG.DeleteNode(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000614 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000615 }
616 }
Chris Lattner95038592005-10-05 06:35:28 +0000617
618 // If the root changed (e.g. it was a dead load, update the root).
619 DAG.setRoot(Dummy.getValue());
Nate Begeman1d4d4142005-09-01 00:19:25 +0000620}
621
Nate Begeman83e75ec2005-09-06 04:43:02 +0000622SDOperand DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000623 switch(N->getOpcode()) {
624 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +0000625 case ISD::TokenFactor: return visitTokenFactor(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000626 case ISD::ADD: return visitADD(N);
627 case ISD::SUB: return visitSUB(N);
Chris Lattner91153682007-03-04 20:03:15 +0000628 case ISD::ADDC: return visitADDC(N);
629 case ISD::ADDE: return visitADDE(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000630 case ISD::MUL: return visitMUL(N);
631 case ISD::SDIV: return visitSDIV(N);
632 case ISD::UDIV: return visitUDIV(N);
633 case ISD::SREM: return visitSREM(N);
634 case ISD::UREM: return visitUREM(N);
635 case ISD::MULHU: return visitMULHU(N);
636 case ISD::MULHS: return visitMULHS(N);
637 case ISD::AND: return visitAND(N);
638 case ISD::OR: return visitOR(N);
639 case ISD::XOR: return visitXOR(N);
640 case ISD::SHL: return visitSHL(N);
641 case ISD::SRA: return visitSRA(N);
642 case ISD::SRL: return visitSRL(N);
643 case ISD::CTLZ: return visitCTLZ(N);
644 case ISD::CTTZ: return visitCTTZ(N);
645 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000646 case ISD::SELECT: return visitSELECT(N);
647 case ISD::SELECT_CC: return visitSELECT_CC(N);
648 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000649 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
650 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000651 case ISD::ANY_EXTEND: return visitANY_EXTEND(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000652 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
653 case ISD::TRUNCATE: return visitTRUNCATE(N);
Chris Lattner94683772005-12-23 05:30:37 +0000654 case ISD::BIT_CONVERT: return visitBIT_CONVERT(N);
Chris Lattner6258fb22006-04-02 02:53:43 +0000655 case ISD::VBIT_CONVERT: return visitVBIT_CONVERT(N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000656 case ISD::FADD: return visitFADD(N);
657 case ISD::FSUB: return visitFSUB(N);
658 case ISD::FMUL: return visitFMUL(N);
659 case ISD::FDIV: return visitFDIV(N);
660 case ISD::FREM: return visitFREM(N);
Chris Lattner12d83032006-03-05 05:30:57 +0000661 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000662 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
663 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
664 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
665 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
666 case ISD::FP_ROUND: return visitFP_ROUND(N);
667 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
668 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
669 case ISD::FNEG: return visitFNEG(N);
670 case ISD::FABS: return visitFABS(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000671 case ISD::BRCOND: return visitBRCOND(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000672 case ISD::BR_CC: return visitBR_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000673 case ISD::LOAD: return visitLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000674 case ISD::STORE: return visitSTORE(N);
Chris Lattnerca242442006-03-19 01:27:56 +0000675 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
676 case ISD::VINSERT_VECTOR_ELT: return visitVINSERT_VECTOR_ELT(N);
Chris Lattnerd7648c82006-03-28 20:28:38 +0000677 case ISD::VBUILD_VECTOR: return visitVBUILD_VECTOR(N);
Chris Lattner66445d32006-03-28 22:11:53 +0000678 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
Chris Lattnerf1d0c622006-03-31 22:16:43 +0000679 case ISD::VVECTOR_SHUFFLE: return visitVVECTOR_SHUFFLE(N);
Chris Lattneredab1b92006-04-02 03:25:57 +0000680 case ISD::VADD: return visitVBinOp(N, ISD::ADD , ISD::FADD);
681 case ISD::VSUB: return visitVBinOp(N, ISD::SUB , ISD::FSUB);
682 case ISD::VMUL: return visitVBinOp(N, ISD::MUL , ISD::FMUL);
683 case ISD::VSDIV: return visitVBinOp(N, ISD::SDIV, ISD::FDIV);
684 case ISD::VUDIV: return visitVBinOp(N, ISD::UDIV, ISD::UDIV);
685 case ISD::VAND: return visitVBinOp(N, ISD::AND , ISD::AND);
686 case ISD::VOR: return visitVBinOp(N, ISD::OR , ISD::OR);
687 case ISD::VXOR: return visitVBinOp(N, ISD::XOR , ISD::XOR);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000688 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000689 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000690}
691
Chris Lattner6270f682006-10-08 22:57:01 +0000692/// getInputChainForNode - Given a node, return its input chain if it has one,
693/// otherwise return a null sd operand.
694static SDOperand getInputChainForNode(SDNode *N) {
695 if (unsigned NumOps = N->getNumOperands()) {
696 if (N->getOperand(0).getValueType() == MVT::Other)
697 return N->getOperand(0);
698 else if (N->getOperand(NumOps-1).getValueType() == MVT::Other)
699 return N->getOperand(NumOps-1);
700 for (unsigned i = 1; i < NumOps-1; ++i)
701 if (N->getOperand(i).getValueType() == MVT::Other)
702 return N->getOperand(i);
703 }
704 return SDOperand(0, 0);
705}
706
Nate Begeman83e75ec2005-09-06 04:43:02 +0000707SDOperand DAGCombiner::visitTokenFactor(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +0000708 // If N has two operands, where one has an input chain equal to the other,
709 // the 'other' chain is redundant.
710 if (N->getNumOperands() == 2) {
711 if (getInputChainForNode(N->getOperand(0).Val) == N->getOperand(1))
712 return N->getOperand(0);
713 if (getInputChainForNode(N->getOperand(1).Val) == N->getOperand(0))
714 return N->getOperand(1);
715 }
716
Chris Lattnerc76d4412007-05-16 06:37:59 +0000717 SmallVector<SDNode *, 8> TFs; // List of token factors to visit.
718 SmallVector<SDOperand, 8> Ops; // Ops for replacing token factor.
719 SmallPtrSet<SDNode*, 16> SeenOps;
720 bool Changed = false; // If we should replace this token factor.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000721
722 // Start out with this token factor.
Jim Laskey279f0532006-09-25 16:29:54 +0000723 TFs.push_back(N);
Jim Laskey279f0532006-09-25 16:29:54 +0000724
Jim Laskey71382342006-10-07 23:37:56 +0000725 // Iterate through token factors. The TFs grows when new token factors are
Jim Laskeybc588b82006-10-05 15:07:25 +0000726 // encountered.
727 for (unsigned i = 0; i < TFs.size(); ++i) {
728 SDNode *TF = TFs[i];
729
Jim Laskey6ff23e52006-10-04 16:53:27 +0000730 // Check each of the operands.
731 for (unsigned i = 0, ie = TF->getNumOperands(); i != ie; ++i) {
732 SDOperand Op = TF->getOperand(i);
Jim Laskey279f0532006-09-25 16:29:54 +0000733
Jim Laskey6ff23e52006-10-04 16:53:27 +0000734 switch (Op.getOpcode()) {
735 case ISD::EntryToken:
Jim Laskeybc588b82006-10-05 15:07:25 +0000736 // Entry tokens don't need to be added to the list. They are
737 // rededundant.
738 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000739 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000740
Jim Laskey6ff23e52006-10-04 16:53:27 +0000741 case ISD::TokenFactor:
Jim Laskeybc588b82006-10-05 15:07:25 +0000742 if ((CombinerAA || Op.hasOneUse()) &&
743 std::find(TFs.begin(), TFs.end(), Op.Val) == TFs.end()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +0000744 // Queue up for processing.
745 TFs.push_back(Op.Val);
746 // Clean up in case the token factor is removed.
747 AddToWorkList(Op.Val);
748 Changed = true;
749 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000750 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000751 // Fall thru
752
753 default:
Chris Lattnerc76d4412007-05-16 06:37:59 +0000754 // Only add if it isn't already in the list.
755 if (SeenOps.insert(Op.Val))
Jim Laskeybc588b82006-10-05 15:07:25 +0000756 Ops.push_back(Op);
Chris Lattnerc76d4412007-05-16 06:37:59 +0000757 else
758 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000759 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000760 }
761 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000762 }
763
764 SDOperand Result;
765
766 // If we've change things around then replace token factor.
767 if (Changed) {
768 if (Ops.size() == 0) {
769 // The entry token is the only possible outcome.
770 Result = DAG.getEntryNode();
771 } else {
772 // New and improved token factor.
773 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0], Ops.size());
Nate Begemanded49632005-10-13 03:11:28 +0000774 }
Jim Laskey274062c2006-10-13 23:32:28 +0000775
776 // Don't add users to work list.
777 return CombineTo(N, Result, false);
Nate Begemanded49632005-10-13 03:11:28 +0000778 }
Jim Laskey279f0532006-09-25 16:29:54 +0000779
Jim Laskey6ff23e52006-10-04 16:53:27 +0000780 return Result;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000781}
782
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000783static
784SDOperand combineShlAddConstant(SDOperand N0, SDOperand N1, SelectionDAG &DAG) {
785 MVT::ValueType VT = N0.getValueType();
786 SDOperand N00 = N0.getOperand(0);
787 SDOperand N01 = N0.getOperand(1);
788 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N01);
789 if (N01C && N00.getOpcode() == ISD::ADD && N00.Val->hasOneUse() &&
790 isa<ConstantSDNode>(N00.getOperand(1))) {
791 N0 = DAG.getNode(ISD::ADD, VT,
792 DAG.getNode(ISD::SHL, VT, N00.getOperand(0), N01),
793 DAG.getNode(ISD::SHL, VT, N00.getOperand(1), N01));
794 return DAG.getNode(ISD::ADD, VT, N0, N1);
795 }
796 return SDOperand();
797}
798
Nate Begeman83e75ec2005-09-06 04:43:02 +0000799SDOperand DAGCombiner::visitADD(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000800 SDOperand N0 = N->getOperand(0);
801 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000802 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
803 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemanf89d78d2005-09-07 16:09:19 +0000804 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000805
806 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000807 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000808 return DAG.getNode(ISD::ADD, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000809 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000810 if (N0C && !N1C)
811 return DAG.getNode(ISD::ADD, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000812 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000813 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000814 return N0;
Chris Lattner4aafb4f2006-01-12 20:22:43 +0000815 // fold ((c1-A)+c2) -> (c1+c2)-A
816 if (N1C && N0.getOpcode() == ISD::SUB)
817 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
818 return DAG.getNode(ISD::SUB, VT,
819 DAG.getConstant(N1C->getValue()+N0C->getValue(), VT),
820 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000821 // reassociate add
822 SDOperand RADD = ReassociateOps(ISD::ADD, N0, N1);
823 if (RADD.Val != 0)
824 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000825 // fold ((0-A) + B) -> B-A
826 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
827 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000828 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000829 // fold (A + (0-B)) -> A-B
830 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
831 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000832 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +0000833 // fold (A+(B-A)) -> B
834 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000835 return N1.getOperand(0);
Chris Lattner947c2892006-03-13 06:51:27 +0000836
Evan Cheng860771d2006-03-01 01:09:54 +0000837 if (!MVT::isVector(VT) && SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +0000838 return SDOperand(N, 0);
Chris Lattner947c2892006-03-13 06:51:27 +0000839
840 // fold (a+b) -> (a|b) iff a and b share no bits.
841 if (MVT::isInteger(VT) && !MVT::isVector(VT)) {
842 uint64_t LHSZero, LHSOne;
843 uint64_t RHSZero, RHSOne;
844 uint64_t Mask = MVT::getIntVTBitMask(VT);
845 TLI.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
846 if (LHSZero) {
847 TLI.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
848
849 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
850 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
851 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
852 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
853 return DAG.getNode(ISD::OR, VT, N0, N1);
854 }
855 }
Evan Cheng3ef554d2006-11-06 08:14:30 +0000856
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000857 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
858 if (N0.getOpcode() == ISD::SHL && N0.Val->hasOneUse()) {
859 SDOperand Result = combineShlAddConstant(N0, N1, DAG);
860 if (Result.Val) return Result;
861 }
862 if (N1.getOpcode() == ISD::SHL && N1.Val->hasOneUse()) {
863 SDOperand Result = combineShlAddConstant(N1, N0, DAG);
864 if (Result.Val) return Result;
865 }
866
Nate Begeman83e75ec2005-09-06 04:43:02 +0000867 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000868}
869
Chris Lattner91153682007-03-04 20:03:15 +0000870SDOperand DAGCombiner::visitADDC(SDNode *N) {
871 SDOperand N0 = N->getOperand(0);
872 SDOperand N1 = N->getOperand(1);
873 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
874 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
875 MVT::ValueType VT = N0.getValueType();
876
877 // If the flag result is dead, turn this into an ADD.
878 if (N->hasNUsesOfValue(0, 1))
879 return CombineTo(N, DAG.getNode(ISD::ADD, VT, N1, N0),
Chris Lattnerb6541762007-03-04 20:40:38 +0000880 DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
Chris Lattner91153682007-03-04 20:03:15 +0000881
882 // canonicalize constant to RHS.
Chris Lattnerbcf24842007-03-04 20:08:45 +0000883 if (N0C && !N1C) {
884 SDOperand Ops[] = { N1, N0 };
885 return DAG.getNode(ISD::ADDC, N->getVTList(), Ops, 2);
886 }
Chris Lattner91153682007-03-04 20:03:15 +0000887
Chris Lattnerb6541762007-03-04 20:40:38 +0000888 // fold (addc x, 0) -> x + no carry out
889 if (N1C && N1C->isNullValue())
890 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
891
892 // fold (addc a, b) -> (or a, b), CARRY_FALSE iff a and b share no bits.
893 uint64_t LHSZero, LHSOne;
894 uint64_t RHSZero, RHSOne;
895 uint64_t Mask = MVT::getIntVTBitMask(VT);
896 TLI.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
897 if (LHSZero) {
898 TLI.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
899
900 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
901 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
902 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
903 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
904 return CombineTo(N, DAG.getNode(ISD::OR, VT, N0, N1),
905 DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
906 }
Chris Lattner91153682007-03-04 20:03:15 +0000907
908 return SDOperand();
909}
910
911SDOperand DAGCombiner::visitADDE(SDNode *N) {
912 SDOperand N0 = N->getOperand(0);
913 SDOperand N1 = N->getOperand(1);
Chris Lattnerb6541762007-03-04 20:40:38 +0000914 SDOperand CarryIn = N->getOperand(2);
Chris Lattner91153682007-03-04 20:03:15 +0000915 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
916 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Chris Lattnerbcf24842007-03-04 20:08:45 +0000917 //MVT::ValueType VT = N0.getValueType();
Chris Lattner91153682007-03-04 20:03:15 +0000918
919 // canonicalize constant to RHS
Chris Lattnerbcf24842007-03-04 20:08:45 +0000920 if (N0C && !N1C) {
Chris Lattnerb6541762007-03-04 20:40:38 +0000921 SDOperand Ops[] = { N1, N0, CarryIn };
Chris Lattnerbcf24842007-03-04 20:08:45 +0000922 return DAG.getNode(ISD::ADDE, N->getVTList(), Ops, 3);
923 }
Chris Lattner91153682007-03-04 20:03:15 +0000924
Chris Lattnerb6541762007-03-04 20:40:38 +0000925 // fold (adde x, y, false) -> (addc x, y)
926 if (CarryIn.getOpcode() == ISD::CARRY_FALSE) {
927 SDOperand Ops[] = { N1, N0 };
928 return DAG.getNode(ISD::ADDC, N->getVTList(), Ops, 2);
929 }
Chris Lattner91153682007-03-04 20:03:15 +0000930
931 return SDOperand();
932}
933
934
935
Nate Begeman83e75ec2005-09-06 04:43:02 +0000936SDOperand DAGCombiner::visitSUB(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000937 SDOperand N0 = N->getOperand(0);
938 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000939 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
940 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000941 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000942
Chris Lattner854077d2005-10-17 01:07:11 +0000943 // fold (sub x, x) -> 0
944 if (N0 == N1)
945 return DAG.getConstant(0, N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000946 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000947 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000948 return DAG.getNode(ISD::SUB, VT, N0, N1);
Chris Lattner05b57432005-10-11 06:07:15 +0000949 // fold (sub x, c) -> (add x, -c)
950 if (N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000951 return DAG.getNode(ISD::ADD, VT, N0, DAG.getConstant(-N1C->getValue(), VT));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000952 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +0000953 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000954 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000955 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +0000956 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000957 return N0.getOperand(0);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000958 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000959}
960
Nate Begeman83e75ec2005-09-06 04:43:02 +0000961SDOperand DAGCombiner::visitMUL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000962 SDOperand N0 = N->getOperand(0);
963 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000964 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
965 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman223df222005-09-08 20:18:10 +0000966 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000967
968 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000969 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000970 return DAG.getNode(ISD::MUL, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000971 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000972 if (N0C && !N1C)
973 return DAG.getNode(ISD::MUL, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000974 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000975 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000976 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000977 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +0000978 if (N1C && N1C->isAllOnesValue())
Nate Begeman405e3ec2005-10-21 00:02:42 +0000979 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000980 // fold (mul x, (1 << c)) -> x << c
Nate Begeman646d7e22005-09-02 21:18:40 +0000981 if (N1C && isPowerOf2_64(N1C->getValue()))
Chris Lattner3e6099b2005-10-30 06:41:49 +0000982 return DAG.getNode(ISD::SHL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000983 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000984 TLI.getShiftAmountTy()));
Chris Lattner3e6099b2005-10-30 06:41:49 +0000985 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
986 if (N1C && isPowerOf2_64(-N1C->getSignExtended())) {
987 // FIXME: If the input is something that is easily negated (e.g. a
988 // single-use add), we should put the negate there.
989 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT),
990 DAG.getNode(ISD::SHL, VT, N0,
991 DAG.getConstant(Log2_64(-N1C->getSignExtended()),
992 TLI.getShiftAmountTy())));
993 }
Andrew Lenharth50a0d422006-04-02 21:42:45 +0000994
Chris Lattner0b1a85f2006-03-01 03:44:24 +0000995 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
996 if (N1C && N0.getOpcode() == ISD::SHL &&
997 isa<ConstantSDNode>(N0.getOperand(1))) {
998 SDOperand C3 = DAG.getNode(ISD::SHL, VT, N1, N0.getOperand(1));
Chris Lattner5750df92006-03-01 04:03:14 +0000999 AddToWorkList(C3.Val);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001000 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0), C3);
1001 }
1002
1003 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
1004 // use.
1005 {
1006 SDOperand Sh(0,0), Y(0,0);
1007 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
1008 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
1009 N0.Val->hasOneUse()) {
1010 Sh = N0; Y = N1;
1011 } else if (N1.getOpcode() == ISD::SHL &&
1012 isa<ConstantSDNode>(N1.getOperand(1)) && N1.Val->hasOneUse()) {
1013 Sh = N1; Y = N0;
1014 }
1015 if (Sh.Val) {
1016 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Sh.getOperand(0), Y);
1017 return DAG.getNode(ISD::SHL, VT, Mul, Sh.getOperand(1));
1018 }
1019 }
Chris Lattnera1deca32006-03-04 23:33:26 +00001020 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
1021 if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() &&
1022 isa<ConstantSDNode>(N0.getOperand(1))) {
1023 return DAG.getNode(ISD::ADD, VT,
1024 DAG.getNode(ISD::MUL, VT, N0.getOperand(0), N1),
1025 DAG.getNode(ISD::MUL, VT, N0.getOperand(1), N1));
1026 }
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001027
Nate Begemancd4d58c2006-02-03 06:46:56 +00001028 // reassociate mul
1029 SDOperand RMUL = ReassociateOps(ISD::MUL, N0, N1);
1030 if (RMUL.Val != 0)
1031 return RMUL;
Nate Begeman83e75ec2005-09-06 04:43:02 +00001032 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001033}
1034
Nate Begeman83e75ec2005-09-06 04:43:02 +00001035SDOperand DAGCombiner::visitSDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001036 SDOperand N0 = N->getOperand(0);
1037 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001038 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1039 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +00001040 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001041
1042 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001043 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001044 return DAG.getNode(ISD::SDIV, VT, N0, N1);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001045 // fold (sdiv X, 1) -> X
1046 if (N1C && N1C->getSignExtended() == 1LL)
1047 return N0;
1048 // fold (sdiv X, -1) -> 0-X
1049 if (N1C && N1C->isAllOnesValue())
1050 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +00001051 // If we know the sign bits of both operands are zero, strength reduce to a
1052 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
1053 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001054 if (TLI.MaskedValueIsZero(N1, SignBit) &&
1055 TLI.MaskedValueIsZero(N0, SignBit))
Chris Lattner094c8fc2005-10-07 06:10:46 +00001056 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
Nate Begemancd6a6ed2006-02-17 07:26:20 +00001057 // fold (sdiv X, pow2) -> simple ops after legalize
Nate Begemanfb7217b2006-02-17 19:54:08 +00001058 if (N1C && N1C->getValue() && !TLI.isIntDivCheap() &&
Nate Begeman405e3ec2005-10-21 00:02:42 +00001059 (isPowerOf2_64(N1C->getSignExtended()) ||
1060 isPowerOf2_64(-N1C->getSignExtended()))) {
1061 // If dividing by powers of two is cheap, then don't perform the following
1062 // fold.
1063 if (TLI.isPow2DivCheap())
1064 return SDOperand();
1065 int64_t pow2 = N1C->getSignExtended();
1066 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
Chris Lattner8f4880b2006-02-16 08:02:36 +00001067 unsigned lg2 = Log2_64(abs2);
1068 // Splat the sign bit into the register
1069 SDOperand SGN = DAG.getNode(ISD::SRA, VT, N0,
Nate Begeman405e3ec2005-10-21 00:02:42 +00001070 DAG.getConstant(MVT::getSizeInBits(VT)-1,
1071 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00001072 AddToWorkList(SGN.Val);
Chris Lattner8f4880b2006-02-16 08:02:36 +00001073 // Add (N0 < 0) ? abs2 - 1 : 0;
1074 SDOperand SRL = DAG.getNode(ISD::SRL, VT, SGN,
1075 DAG.getConstant(MVT::getSizeInBits(VT)-lg2,
Nate Begeman405e3ec2005-10-21 00:02:42 +00001076 TLI.getShiftAmountTy()));
Chris Lattner8f4880b2006-02-16 08:02:36 +00001077 SDOperand ADD = DAG.getNode(ISD::ADD, VT, N0, SRL);
Chris Lattner5750df92006-03-01 04:03:14 +00001078 AddToWorkList(SRL.Val);
1079 AddToWorkList(ADD.Val); // Divide by pow2
Chris Lattner8f4880b2006-02-16 08:02:36 +00001080 SDOperand SRA = DAG.getNode(ISD::SRA, VT, ADD,
1081 DAG.getConstant(lg2, TLI.getShiftAmountTy()));
Nate Begeman405e3ec2005-10-21 00:02:42 +00001082 // If we're dividing by a positive value, we're done. Otherwise, we must
1083 // negate the result.
1084 if (pow2 > 0)
1085 return SRA;
Chris Lattner5750df92006-03-01 04:03:14 +00001086 AddToWorkList(SRA.Val);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001087 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), SRA);
1088 }
Nate Begeman69575232005-10-20 02:15:44 +00001089 // if integer divide is expensive and we satisfy the requirements, emit an
1090 // alternate sequence.
Nate Begeman405e3ec2005-10-21 00:02:42 +00001091 if (N1C && (N1C->getSignExtended() < -1 || N1C->getSignExtended() > 1) &&
Chris Lattnere9936d12005-10-22 18:50:15 +00001092 !TLI.isIntDivCheap()) {
1093 SDOperand Op = BuildSDIV(N);
1094 if (Op.Val) return Op;
Nate Begeman69575232005-10-20 02:15:44 +00001095 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001096 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001097}
1098
Nate Begeman83e75ec2005-09-06 04:43:02 +00001099SDOperand DAGCombiner::visitUDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001100 SDOperand N0 = N->getOperand(0);
1101 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001102 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1103 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +00001104 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001105
1106 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001107 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001108 return DAG.getNode(ISD::UDIV, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001109 // fold (udiv x, (1 << c)) -> x >>u c
Nate Begeman646d7e22005-09-02 21:18:40 +00001110 if (N1C && isPowerOf2_64(N1C->getValue()))
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001111 return DAG.getNode(ISD::SRL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +00001112 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001113 TLI.getShiftAmountTy()));
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001114 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
1115 if (N1.getOpcode() == ISD::SHL) {
1116 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
1117 if (isPowerOf2_64(SHC->getValue())) {
1118 MVT::ValueType ADDVT = N1.getOperand(1).getValueType();
Nate Begemanc031e332006-02-05 07:36:48 +00001119 SDOperand Add = DAG.getNode(ISD::ADD, ADDVT, N1.getOperand(1),
1120 DAG.getConstant(Log2_64(SHC->getValue()),
1121 ADDVT));
Chris Lattner5750df92006-03-01 04:03:14 +00001122 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +00001123 return DAG.getNode(ISD::SRL, VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001124 }
1125 }
1126 }
Nate Begeman69575232005-10-20 02:15:44 +00001127 // fold (udiv x, c) -> alternate
Chris Lattnere9936d12005-10-22 18:50:15 +00001128 if (N1C && N1C->getValue() && !TLI.isIntDivCheap()) {
1129 SDOperand Op = BuildUDIV(N);
1130 if (Op.Val) return Op;
1131 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001132 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001133}
1134
Nate Begeman83e75ec2005-09-06 04:43:02 +00001135SDOperand DAGCombiner::visitSREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001136 SDOperand N0 = N->getOperand(0);
1137 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001138 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1139 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +00001140 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001141
1142 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001143 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001144 return DAG.getNode(ISD::SREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +00001145 // If we know the sign bits of both operands are zero, strength reduce to a
1146 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
1147 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001148 if (TLI.MaskedValueIsZero(N1, SignBit) &&
1149 TLI.MaskedValueIsZero(N0, SignBit))
Nate Begemana148d982006-01-18 22:35:16 +00001150 return DAG.getNode(ISD::UREM, VT, N0, N1);
Chris Lattner26d29902006-10-12 20:58:32 +00001151
1152 // Unconditionally lower X%C -> X-X/C*C. This allows the X/C logic to hack on
1153 // the remainder operation.
1154 if (N1C && !N1C->isNullValue()) {
1155 SDOperand Div = DAG.getNode(ISD::SDIV, VT, N0, N1);
1156 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Div, N1);
1157 SDOperand Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
1158 AddToWorkList(Div.Val);
1159 AddToWorkList(Mul.Val);
1160 return Sub;
1161 }
1162
Nate Begeman83e75ec2005-09-06 04:43:02 +00001163 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001164}
1165
Nate Begeman83e75ec2005-09-06 04:43:02 +00001166SDOperand DAGCombiner::visitUREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001167 SDOperand N0 = N->getOperand(0);
1168 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001169 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1170 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +00001171 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001172
1173 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001174 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001175 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +00001176 // fold (urem x, pow2) -> (and x, pow2-1)
1177 if (N1C && !N1C->isNullValue() && isPowerOf2_64(N1C->getValue()))
Nate Begemana148d982006-01-18 22:35:16 +00001178 return DAG.getNode(ISD::AND, VT, N0, DAG.getConstant(N1C->getValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +00001179 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
1180 if (N1.getOpcode() == ISD::SHL) {
1181 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
1182 if (isPowerOf2_64(SHC->getValue())) {
Nate Begemanbab92392006-02-05 08:07:24 +00001183 SDOperand Add = DAG.getNode(ISD::ADD, VT, N1,DAG.getConstant(~0ULL,VT));
Chris Lattner5750df92006-03-01 04:03:14 +00001184 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +00001185 return DAG.getNode(ISD::AND, VT, N0, Add);
1186 }
1187 }
1188 }
Chris Lattner26d29902006-10-12 20:58:32 +00001189
1190 // Unconditionally lower X%C -> X-X/C*C. This allows the X/C logic to hack on
1191 // the remainder operation.
1192 if (N1C && !N1C->isNullValue()) {
1193 SDOperand Div = DAG.getNode(ISD::UDIV, VT, N0, N1);
1194 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Div, N1);
1195 SDOperand Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
1196 AddToWorkList(Div.Val);
1197 AddToWorkList(Mul.Val);
1198 return Sub;
1199 }
1200
Nate Begeman83e75ec2005-09-06 04:43:02 +00001201 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001202}
1203
Nate Begeman83e75ec2005-09-06 04:43:02 +00001204SDOperand DAGCombiner::visitMULHS(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001205 SDOperand N0 = N->getOperand(0);
1206 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001207 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001208
1209 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001210 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001211 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001212 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001213 if (N1C && N1C->getValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001214 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
1215 DAG.getConstant(MVT::getSizeInBits(N0.getValueType())-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001216 TLI.getShiftAmountTy()));
1217 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001218}
1219
Nate Begeman83e75ec2005-09-06 04:43:02 +00001220SDOperand DAGCombiner::visitMULHU(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001221 SDOperand N0 = N->getOperand(0);
1222 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001223 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001224
1225 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001226 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001227 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001228 // fold (mulhu x, 1) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001229 if (N1C && N1C->getValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001230 return DAG.getConstant(0, N0.getValueType());
1231 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001232}
1233
Chris Lattner35e5c142006-05-05 05:51:50 +00001234/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
1235/// two operands of the same opcode, try to simplify it.
1236SDOperand DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
1237 SDOperand N0 = N->getOperand(0), N1 = N->getOperand(1);
1238 MVT::ValueType VT = N0.getValueType();
1239 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
1240
Chris Lattner540121f2006-05-05 06:31:05 +00001241 // For each of OP in AND/OR/XOR:
1242 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
1243 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
1244 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
Chris Lattner0d8dae72006-05-05 06:32:04 +00001245 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y))
Chris Lattner540121f2006-05-05 06:31:05 +00001246 if ((N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND||
Chris Lattner0d8dae72006-05-05 06:32:04 +00001247 N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::TRUNCATE) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001248 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1249 SDOperand ORNode = DAG.getNode(N->getOpcode(),
1250 N0.getOperand(0).getValueType(),
1251 N0.getOperand(0), N1.getOperand(0));
1252 AddToWorkList(ORNode.Val);
Chris Lattner540121f2006-05-05 06:31:05 +00001253 return DAG.getNode(N0.getOpcode(), VT, ORNode);
Chris Lattner35e5c142006-05-05 05:51:50 +00001254 }
1255
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001256 // For each of OP in SHL/SRL/SRA/AND...
1257 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
1258 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
1259 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
Chris Lattner35e5c142006-05-05 05:51:50 +00001260 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001261 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001262 N0.getOperand(1) == N1.getOperand(1)) {
1263 SDOperand ORNode = DAG.getNode(N->getOpcode(),
1264 N0.getOperand(0).getValueType(),
1265 N0.getOperand(0), N1.getOperand(0));
1266 AddToWorkList(ORNode.Val);
1267 return DAG.getNode(N0.getOpcode(), VT, ORNode, N0.getOperand(1));
1268 }
1269
1270 return SDOperand();
1271}
1272
Nate Begeman83e75ec2005-09-06 04:43:02 +00001273SDOperand DAGCombiner::visitAND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001274 SDOperand N0 = N->getOperand(0);
1275 SDOperand N1 = N->getOperand(1);
Nate Begemanfb7217b2006-02-17 19:54:08 +00001276 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001277 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1278 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001279 MVT::ValueType VT = N1.getValueType();
1280
1281 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001282 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001283 return DAG.getNode(ISD::AND, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001284 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001285 if (N0C && !N1C)
1286 return DAG.getNode(ISD::AND, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001287 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001288 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001289 return N0;
1290 // if (and x, c) is known to be zero, return 0
Nate Begeman368e18d2006-02-16 21:11:51 +00001291 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001292 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +00001293 // reassociate and
1294 SDOperand RAND = ReassociateOps(ISD::AND, N0, N1);
1295 if (RAND.Val != 0)
1296 return RAND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001297 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
Nate Begeman5dc7e862005-11-02 18:42:59 +00001298 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001299 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Nate Begeman646d7e22005-09-02 21:18:40 +00001300 if ((ORI->getValue() & N1C->getValue()) == N1C->getValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001301 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +00001302 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
1303 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Chris Lattner1ec05d12006-03-01 21:47:21 +00001304 unsigned InMask = MVT::getIntVTBitMask(N0.getOperand(0).getValueType());
Chris Lattner3603cd62006-02-02 07:17:31 +00001305 if (TLI.MaskedValueIsZero(N0.getOperand(0),
Chris Lattner1ec05d12006-03-01 21:47:21 +00001306 ~N1C->getValue() & InMask)) {
1307 SDOperand Zext = DAG.getNode(ISD::ZERO_EXTEND, N0.getValueType(),
1308 N0.getOperand(0));
1309
1310 // Replace uses of the AND with uses of the Zero extend node.
1311 CombineTo(N, Zext);
1312
Chris Lattner3603cd62006-02-02 07:17:31 +00001313 // We actually want to replace all uses of the any_extend with the
1314 // zero_extend, to avoid duplicating things. This will later cause this
1315 // AND to be folded.
Chris Lattner1ec05d12006-03-01 21:47:21 +00001316 CombineTo(N0.Val, Zext);
Chris Lattnerfedced72006-04-20 23:55:59 +00001317 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner3603cd62006-02-02 07:17:31 +00001318 }
1319 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001320 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
1321 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1322 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1323 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1324
1325 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1326 MVT::isInteger(LL.getValueType())) {
1327 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
1328 if (cast<ConstantSDNode>(LR)->getValue() == 0 && Op1 == ISD::SETEQ) {
1329 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001330 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001331 return DAG.getSetCC(VT, ORNode, LR, Op1);
1332 }
1333 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
1334 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
1335 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001336 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001337 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1338 }
1339 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
1340 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
1341 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001342 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001343 return DAG.getSetCC(VT, ORNode, LR, Op1);
1344 }
1345 }
1346 // canonicalize equivalent to ll == rl
1347 if (LL == RR && LR == RL) {
1348 Op1 = ISD::getSetCCSwappedOperands(Op1);
1349 std::swap(RL, RR);
1350 }
1351 if (LL == RL && LR == RR) {
1352 bool isInteger = MVT::isInteger(LL.getValueType());
1353 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
1354 if (Result != ISD::SETCC_INVALID)
1355 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1356 }
1357 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001358
1359 // Simplify: and (op x...), (op y...) -> (op (and x, y))
1360 if (N0.getOpcode() == N1.getOpcode()) {
1361 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1362 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001363 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001364
Nate Begemande996292006-02-03 22:24:05 +00001365 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
1366 // fold (and (sra)) -> (and (srl)) when possible.
Chris Lattner6ea2dee2006-03-25 22:19:00 +00001367 if (!MVT::isVector(VT) &&
1368 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001369 return SDOperand(N, 0);
Nate Begemanded49632005-10-13 03:11:28 +00001370 // fold (zext_inreg (extload x)) -> (zextload x)
Evan Cheng83060c52007-03-07 08:07:03 +00001371 if (ISD::isEXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val)) {
Evan Cheng466685d2006-10-09 20:57:25 +00001372 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00001373 MVT::ValueType EVT = LN0->getLoadedVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001374 // If we zero all the possible extended bits, then we can turn this into
1375 // a zextload if we are running before legalize or the operation is legal.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001376 if (TLI.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Evan Chengc5484282006-10-04 00:56:09 +00001377 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00001378 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
1379 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00001380 LN0->getSrcValueOffset(), EVT,
1381 LN0->isVolatile(),
1382 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00001383 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001384 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001385 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001386 }
1387 }
1388 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Evan Cheng83060c52007-03-07 08:07:03 +00001389 if (ISD::isSEXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val) &&
1390 N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001391 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00001392 MVT::ValueType EVT = LN0->getLoadedVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001393 // If we zero all the possible extended bits, then we can turn this into
1394 // a zextload if we are running before legalize or the operation is legal.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001395 if (TLI.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Evan Chengc5484282006-10-04 00:56:09 +00001396 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00001397 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
1398 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00001399 LN0->getSrcValueOffset(), EVT,
1400 LN0->isVolatile(),
1401 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00001402 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001403 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001404 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001405 }
1406 }
Chris Lattner15045b62006-02-28 06:35:35 +00001407
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001408 // fold (and (load x), 255) -> (zextload x, i8)
1409 // fold (and (extload x, i16), 255) -> (zextload x, i8)
Evan Cheng466685d2006-10-09 20:57:25 +00001410 if (N1C && N0.getOpcode() == ISD::LOAD) {
1411 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
1412 if (LN0->getExtensionType() != ISD::SEXTLOAD &&
Evan Cheng83060c52007-03-07 08:07:03 +00001413 LN0->getAddressingMode() == ISD::UNINDEXED &&
Evan Cheng466685d2006-10-09 20:57:25 +00001414 N0.hasOneUse()) {
1415 MVT::ValueType EVT, LoadedVT;
1416 if (N1C->getValue() == 255)
1417 EVT = MVT::i8;
1418 else if (N1C->getValue() == 65535)
1419 EVT = MVT::i16;
1420 else if (N1C->getValue() == ~0U)
1421 EVT = MVT::i32;
1422 else
1423 EVT = MVT::Other;
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001424
Evan Cheng2e49f092006-10-11 07:10:22 +00001425 LoadedVT = LN0->getLoadedVT();
Evan Cheng466685d2006-10-09 20:57:25 +00001426 if (EVT != MVT::Other && LoadedVT > EVT &&
1427 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
1428 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
1429 // For big endian targets, we need to add an offset to the pointer to
1430 // load the correct bytes. For little endian systems, we merely need to
1431 // read fewer bytes from the same pointer.
1432 unsigned PtrOff =
1433 (MVT::getSizeInBits(LoadedVT) - MVT::getSizeInBits(EVT)) / 8;
1434 SDOperand NewPtr = LN0->getBasePtr();
1435 if (!TLI.isLittleEndian())
1436 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
1437 DAG.getConstant(PtrOff, PtrType));
1438 AddToWorkList(NewPtr.Val);
1439 SDOperand Load =
1440 DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00001441 LN0->getSrcValue(), LN0->getSrcValueOffset(), EVT,
1442 LN0->isVolatile(), LN0->getAlignment());
Evan Cheng466685d2006-10-09 20:57:25 +00001443 AddToWorkList(N);
1444 CombineTo(N0.Val, Load, Load.getValue(1));
1445 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1446 }
Chris Lattner15045b62006-02-28 06:35:35 +00001447 }
1448 }
1449
Nate Begeman83e75ec2005-09-06 04:43:02 +00001450 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001451}
1452
Nate Begeman83e75ec2005-09-06 04:43:02 +00001453SDOperand DAGCombiner::visitOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001454 SDOperand N0 = N->getOperand(0);
1455 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001456 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001457 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1458 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001459 MVT::ValueType VT = N1.getValueType();
1460 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001461
1462 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001463 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001464 return DAG.getNode(ISD::OR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001465 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001466 if (N0C && !N1C)
1467 return DAG.getNode(ISD::OR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001468 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001469 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001470 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001471 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001472 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001473 return N1;
1474 // fold (or x, c) -> c iff (x & ~c) == 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001475 if (N1C &&
1476 TLI.MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits))))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001477 return N1;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001478 // reassociate or
1479 SDOperand ROR = ReassociateOps(ISD::OR, N0, N1);
1480 if (ROR.Val != 0)
1481 return ROR;
1482 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
1483 if (N1C && N0.getOpcode() == ISD::AND && N0.Val->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00001484 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00001485 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
1486 return DAG.getNode(ISD::AND, VT, DAG.getNode(ISD::OR, VT, N0.getOperand(0),
1487 N1),
1488 DAG.getConstant(N1C->getValue() | C1->getValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00001489 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001490 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
1491 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1492 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1493 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1494
1495 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1496 MVT::isInteger(LL.getValueType())) {
1497 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
1498 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
1499 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
1500 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
1501 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001502 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001503 return DAG.getSetCC(VT, ORNode, LR, Op1);
1504 }
1505 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
1506 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
1507 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
1508 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
1509 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001510 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001511 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1512 }
1513 }
1514 // canonicalize equivalent to ll == rl
1515 if (LL == RR && LR == RL) {
1516 Op1 = ISD::getSetCCSwappedOperands(Op1);
1517 std::swap(RL, RR);
1518 }
1519 if (LL == RL && LR == RR) {
1520 bool isInteger = MVT::isInteger(LL.getValueType());
1521 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
1522 if (Result != ISD::SETCC_INVALID)
1523 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1524 }
1525 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001526
1527 // Simplify: or (op x...), (op y...) -> (op (or x, y))
1528 if (N0.getOpcode() == N1.getOpcode()) {
1529 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1530 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001531 }
Chris Lattner516b9622006-09-14 20:50:57 +00001532
Chris Lattner1ec72732006-09-14 21:11:37 +00001533 // (X & C1) | (Y & C2) -> (X|Y) & C3 if possible.
1534 if (N0.getOpcode() == ISD::AND &&
1535 N1.getOpcode() == ISD::AND &&
1536 N0.getOperand(1).getOpcode() == ISD::Constant &&
1537 N1.getOperand(1).getOpcode() == ISD::Constant &&
1538 // Don't increase # computations.
1539 (N0.Val->hasOneUse() || N1.Val->hasOneUse())) {
1540 // We can only do this xform if we know that bits from X that are set in C2
1541 // but not in C1 are already zero. Likewise for Y.
1542 uint64_t LHSMask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1543 uint64_t RHSMask = cast<ConstantSDNode>(N1.getOperand(1))->getValue();
1544
1545 if (TLI.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) &&
1546 TLI.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) {
1547 SDOperand X =DAG.getNode(ISD::OR, VT, N0.getOperand(0), N1.getOperand(0));
1548 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(LHSMask|RHSMask, VT));
1549 }
1550 }
1551
1552
Chris Lattner516b9622006-09-14 20:50:57 +00001553 // See if this is some rotate idiom.
1554 if (SDNode *Rot = MatchRotate(N0, N1))
1555 return SDOperand(Rot, 0);
Chris Lattner35e5c142006-05-05 05:51:50 +00001556
Nate Begeman83e75ec2005-09-06 04:43:02 +00001557 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001558}
1559
Chris Lattner516b9622006-09-14 20:50:57 +00001560
1561/// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present.
1562static bool MatchRotateHalf(SDOperand Op, SDOperand &Shift, SDOperand &Mask) {
1563 if (Op.getOpcode() == ISD::AND) {
Reid Spencer3ed469c2006-11-02 20:25:50 +00001564 if (isa<ConstantSDNode>(Op.getOperand(1))) {
Chris Lattner516b9622006-09-14 20:50:57 +00001565 Mask = Op.getOperand(1);
1566 Op = Op.getOperand(0);
1567 } else {
1568 return false;
1569 }
1570 }
1571
1572 if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) {
1573 Shift = Op;
1574 return true;
1575 }
1576 return false;
1577}
1578
1579
1580// MatchRotate - Handle an 'or' of two operands. If this is one of the many
1581// idioms for rotate, and if the target supports rotation instructions, generate
1582// a rot[lr].
1583SDNode *DAGCombiner::MatchRotate(SDOperand LHS, SDOperand RHS) {
1584 // Must be a legal type. Expanded an promoted things won't work with rotates.
1585 MVT::ValueType VT = LHS.getValueType();
1586 if (!TLI.isTypeLegal(VT)) return 0;
1587
1588 // The target must have at least one rotate flavor.
1589 bool HasROTL = TLI.isOperationLegal(ISD::ROTL, VT);
1590 bool HasROTR = TLI.isOperationLegal(ISD::ROTR, VT);
1591 if (!HasROTL && !HasROTR) return 0;
1592
1593 // Match "(X shl/srl V1) & V2" where V2 may not be present.
1594 SDOperand LHSShift; // The shift.
1595 SDOperand LHSMask; // AND value if any.
1596 if (!MatchRotateHalf(LHS, LHSShift, LHSMask))
1597 return 0; // Not part of a rotate.
1598
1599 SDOperand RHSShift; // The shift.
1600 SDOperand RHSMask; // AND value if any.
1601 if (!MatchRotateHalf(RHS, RHSShift, RHSMask))
1602 return 0; // Not part of a rotate.
1603
1604 if (LHSShift.getOperand(0) != RHSShift.getOperand(0))
1605 return 0; // Not shifting the same value.
1606
1607 if (LHSShift.getOpcode() == RHSShift.getOpcode())
1608 return 0; // Shifts must disagree.
1609
1610 // Canonicalize shl to left side in a shl/srl pair.
1611 if (RHSShift.getOpcode() == ISD::SHL) {
1612 std::swap(LHS, RHS);
1613 std::swap(LHSShift, RHSShift);
1614 std::swap(LHSMask , RHSMask );
1615 }
1616
1617 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Scott Michelc9dc1142007-04-02 21:36:32 +00001618 SDOperand LHSShiftArg = LHSShift.getOperand(0);
1619 SDOperand LHSShiftAmt = LHSShift.getOperand(1);
1620 SDOperand RHSShiftAmt = RHSShift.getOperand(1);
Chris Lattner516b9622006-09-14 20:50:57 +00001621
1622 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
1623 // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2)
Scott Michelc9dc1142007-04-02 21:36:32 +00001624 if (LHSShiftAmt.getOpcode() == ISD::Constant &&
1625 RHSShiftAmt.getOpcode() == ISD::Constant) {
1626 uint64_t LShVal = cast<ConstantSDNode>(LHSShiftAmt)->getValue();
1627 uint64_t RShVal = cast<ConstantSDNode>(RHSShiftAmt)->getValue();
Chris Lattner516b9622006-09-14 20:50:57 +00001628 if ((LShVal + RShVal) != OpSizeInBits)
1629 return 0;
1630
1631 SDOperand Rot;
1632 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00001633 Rot = DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00001634 else
Scott Michelc9dc1142007-04-02 21:36:32 +00001635 Rot = DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00001636
1637 // If there is an AND of either shifted operand, apply it to the result.
1638 if (LHSMask.Val || RHSMask.Val) {
1639 uint64_t Mask = MVT::getIntVTBitMask(VT);
1640
1641 if (LHSMask.Val) {
1642 uint64_t RHSBits = (1ULL << LShVal)-1;
1643 Mask &= cast<ConstantSDNode>(LHSMask)->getValue() | RHSBits;
1644 }
1645 if (RHSMask.Val) {
1646 uint64_t LHSBits = ~((1ULL << (OpSizeInBits-RShVal))-1);
1647 Mask &= cast<ConstantSDNode>(RHSMask)->getValue() | LHSBits;
1648 }
1649
1650 Rot = DAG.getNode(ISD::AND, VT, Rot, DAG.getConstant(Mask, VT));
1651 }
1652
1653 return Rot.Val;
1654 }
1655
1656 // If there is a mask here, and we have a variable shift, we can't be sure
1657 // that we're masking out the right stuff.
1658 if (LHSMask.Val || RHSMask.Val)
1659 return 0;
1660
1661 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
1662 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotr x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00001663 if (RHSShiftAmt.getOpcode() == ISD::SUB &&
1664 LHSShiftAmt == RHSShiftAmt.getOperand(1)) {
Chris Lattner516b9622006-09-14 20:50:57 +00001665 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00001666 dyn_cast<ConstantSDNode>(RHSShiftAmt.getOperand(0))) {
Chris Lattner516b9622006-09-14 20:50:57 +00001667 if (SUBC->getValue() == OpSizeInBits)
1668 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00001669 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
Chris Lattner516b9622006-09-14 20:50:57 +00001670 else
Scott Michelc9dc1142007-04-02 21:36:32 +00001671 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).Val;
Chris Lattner516b9622006-09-14 20:50:57 +00001672 }
1673 }
1674
1675 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
1676 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotl x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00001677 if (LHSShiftAmt.getOpcode() == ISD::SUB &&
1678 RHSShiftAmt == LHSShiftAmt.getOperand(1)) {
Chris Lattner516b9622006-09-14 20:50:57 +00001679 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00001680 dyn_cast<ConstantSDNode>(LHSShiftAmt.getOperand(0))) {
Chris Lattner516b9622006-09-14 20:50:57 +00001681 if (SUBC->getValue() == OpSizeInBits)
1682 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00001683 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
Chris Lattner516b9622006-09-14 20:50:57 +00001684 else
Scott Michelc9dc1142007-04-02 21:36:32 +00001685 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).Val;
1686 }
1687 }
1688
1689 // Look for sign/zext/any-extended cases:
1690 if ((LHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
1691 || LHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
1692 || LHSShiftAmt.getOpcode() == ISD::ANY_EXTEND) &&
1693 (RHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
1694 || RHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
1695 || RHSShiftAmt.getOpcode() == ISD::ANY_EXTEND)) {
1696 SDOperand LExtOp0 = LHSShiftAmt.getOperand(0);
1697 SDOperand RExtOp0 = RHSShiftAmt.getOperand(0);
1698 if (RExtOp0.getOpcode() == ISD::SUB &&
1699 RExtOp0.getOperand(1) == LExtOp0) {
1700 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
1701 // (rotr x, y)
1702 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
1703 // (rotl x, (sub 32, y))
1704 if (ConstantSDNode *SUBC = cast<ConstantSDNode>(RExtOp0.getOperand(0))) {
1705 if (SUBC->getValue() == OpSizeInBits) {
1706 if (HasROTL)
1707 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
1708 else
1709 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).Val;
1710 }
1711 }
1712 } else if (LExtOp0.getOpcode() == ISD::SUB &&
1713 RExtOp0 == LExtOp0.getOperand(1)) {
1714 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext r))) ->
1715 // (rotl x, y)
1716 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext r))) ->
1717 // (rotr x, (sub 32, y))
1718 if (ConstantSDNode *SUBC = cast<ConstantSDNode>(LExtOp0.getOperand(0))) {
1719 if (SUBC->getValue() == OpSizeInBits) {
1720 if (HasROTL)
1721 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, RHSShiftAmt).Val;
1722 else
1723 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
1724 }
1725 }
Chris Lattner516b9622006-09-14 20:50:57 +00001726 }
1727 }
1728
1729 return 0;
1730}
1731
1732
Nate Begeman83e75ec2005-09-06 04:43:02 +00001733SDOperand DAGCombiner::visitXOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001734 SDOperand N0 = N->getOperand(0);
1735 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001736 SDOperand LHS, RHS, CC;
1737 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1738 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001739 MVT::ValueType VT = N0.getValueType();
1740
1741 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001742 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001743 return DAG.getNode(ISD::XOR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001744 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001745 if (N0C && !N1C)
1746 return DAG.getNode(ISD::XOR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001747 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001748 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001749 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001750 // reassociate xor
1751 SDOperand RXOR = ReassociateOps(ISD::XOR, N0, N1);
1752 if (RXOR.Val != 0)
1753 return RXOR;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001754 // fold !(x cc y) -> (x !cc y)
Nate Begeman646d7e22005-09-02 21:18:40 +00001755 if (N1C && N1C->getValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
1756 bool isInt = MVT::isInteger(LHS.getValueType());
1757 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
1758 isInt);
1759 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001760 return DAG.getSetCC(VT, LHS, RHS, NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00001761 if (N0.getOpcode() == ISD::SELECT_CC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001762 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00001763 assert(0 && "Unhandled SetCC Equivalent!");
1764 abort();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001765 }
Nate Begeman99801192005-09-07 23:25:52 +00001766 // fold !(x or y) -> (!x and !y) iff x or y are setcc
Chris Lattner734c91d2006-11-10 21:37:15 +00001767 if (N1C && N1C->getValue() == 1 && VT == MVT::i1 &&
Nate Begeman99801192005-09-07 23:25:52 +00001768 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001769 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00001770 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
1771 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001772 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1773 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00001774 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00001775 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001776 }
1777 }
Nate Begeman99801192005-09-07 23:25:52 +00001778 // fold !(x or y) -> (!x and !y) iff x or y are constants
1779 if (N1C && N1C->isAllOnesValue() &&
1780 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001781 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00001782 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
1783 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001784 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1785 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00001786 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00001787 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001788 }
1789 }
Nate Begeman223df222005-09-08 20:18:10 +00001790 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
1791 if (N1C && N0.getOpcode() == ISD::XOR) {
1792 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
1793 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
1794 if (N00C)
1795 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
1796 DAG.getConstant(N1C->getValue()^N00C->getValue(), VT));
1797 if (N01C)
1798 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
1799 DAG.getConstant(N1C->getValue()^N01C->getValue(), VT));
1800 }
1801 // fold (xor x, x) -> 0
Chris Lattner4fbdd592006-03-28 19:11:05 +00001802 if (N0 == N1) {
1803 if (!MVT::isVector(VT)) {
1804 return DAG.getConstant(0, VT);
1805 } else if (!AfterLegalize || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) {
1806 // Produce a vector of zeros.
1807 SDOperand El = DAG.getConstant(0, MVT::getVectorBaseType(VT));
1808 std::vector<SDOperand> Ops(MVT::getVectorNumElements(VT), El);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001809 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4fbdd592006-03-28 19:11:05 +00001810 }
1811 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001812
1813 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
1814 if (N0.getOpcode() == N1.getOpcode()) {
1815 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1816 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001817 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001818
Chris Lattner3e104b12006-04-08 04:15:24 +00001819 // Simplify the expression using non-local knowledge.
1820 if (!MVT::isVector(VT) &&
1821 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001822 return SDOperand(N, 0);
Chris Lattner3e104b12006-04-08 04:15:24 +00001823
Nate Begeman83e75ec2005-09-06 04:43:02 +00001824 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001825}
1826
Nate Begeman83e75ec2005-09-06 04:43:02 +00001827SDOperand DAGCombiner::visitSHL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001828 SDOperand N0 = N->getOperand(0);
1829 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001830 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1831 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001832 MVT::ValueType VT = N0.getValueType();
1833 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1834
1835 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001836 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001837 return DAG.getNode(ISD::SHL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001838 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001839 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001840 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001841 // fold (shl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001842 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001843 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001844 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001845 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001846 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001847 // if (shl x, c) is known to be zero, return 0
Nate Begemanfb7217b2006-02-17 19:54:08 +00001848 if (TLI.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001849 return DAG.getConstant(0, VT);
Chris Lattner61a4c072007-04-18 03:06:49 +00001850 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001851 return SDOperand(N, 0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001852 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001853 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001854 N0.getOperand(1).getOpcode() == ISD::Constant) {
1855 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001856 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001857 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001858 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001859 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001860 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001861 }
1862 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
1863 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001864 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001865 N0.getOperand(1).getOpcode() == ISD::Constant) {
1866 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001867 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001868 SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
1869 DAG.getConstant(~0ULL << c1, VT));
1870 if (c2 > c1)
1871 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001872 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001873 else
Nate Begeman83e75ec2005-09-06 04:43:02 +00001874 return DAG.getNode(ISD::SRL, VT, Mask,
1875 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001876 }
1877 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001878 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +00001879 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001880 DAG.getConstant(~0ULL << N1C->getValue(), VT));
1881 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001882}
1883
Nate Begeman83e75ec2005-09-06 04:43:02 +00001884SDOperand DAGCombiner::visitSRA(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 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1888 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001889 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001890
1891 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001892 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001893 return DAG.getNode(ISD::SRA, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001894 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001895 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001896 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001897 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001898 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001899 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001900 // fold (sra x, c >= size(x)) -> undef
Nate Begemanfb7217b2006-02-17 19:54:08 +00001901 if (N1C && N1C->getValue() >= MVT::getSizeInBits(VT))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001902 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001903 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001904 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001905 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00001906 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
1907 // sext_inreg.
1908 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
1909 unsigned LowBits = MVT::getSizeInBits(VT) - (unsigned)N1C->getValue();
1910 MVT::ValueType EVT;
1911 switch (LowBits) {
1912 default: EVT = MVT::Other; break;
1913 case 1: EVT = MVT::i1; break;
1914 case 8: EVT = MVT::i8; break;
1915 case 16: EVT = MVT::i16; break;
1916 case 32: EVT = MVT::i32; break;
1917 }
1918 if (EVT > MVT::Other && TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, EVT))
1919 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
1920 DAG.getValueType(EVT));
1921 }
Chris Lattner71d9ebc2006-02-28 06:23:04 +00001922
1923 // fold (sra (sra x, c1), c2) -> (sra x, c1+c2)
1924 if (N1C && N0.getOpcode() == ISD::SRA) {
1925 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
1926 unsigned Sum = N1C->getValue() + C1->getValue();
1927 if (Sum >= MVT::getSizeInBits(VT)) Sum = MVT::getSizeInBits(VT)-1;
1928 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0),
1929 DAG.getConstant(Sum, N1C->getValueType(0)));
1930 }
1931 }
1932
Chris Lattnera8504462006-05-08 20:51:54 +00001933 // Simplify, based on bits shifted out of the LHS.
1934 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
1935 return SDOperand(N, 0);
1936
1937
Nate Begeman1d4d4142005-09-01 00:19:25 +00001938 // If the sign bit is known to be zero, switch this to a SRL.
Nate Begemanfb7217b2006-02-17 19:54:08 +00001939 if (TLI.MaskedValueIsZero(N0, MVT::getIntVTSignBit(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001940 return DAG.getNode(ISD::SRL, VT, N0, N1);
1941 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001942}
1943
Nate Begeman83e75ec2005-09-06 04:43:02 +00001944SDOperand DAGCombiner::visitSRL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001945 SDOperand N0 = N->getOperand(0);
1946 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001947 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1948 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001949 MVT::ValueType VT = N0.getValueType();
1950 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1951
1952 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001953 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001954 return DAG.getNode(ISD::SRL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001955 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001956 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001957 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001958 // fold (srl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001959 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001960 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001961 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001962 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001963 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001964 // if (srl x, c) is known to be zero, return 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001965 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001966 return DAG.getConstant(0, VT);
Chris Lattnerec06e9a2007-04-18 03:05:22 +00001967
Nate Begeman1d4d4142005-09-01 00:19:25 +00001968 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001969 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001970 N0.getOperand(1).getOpcode() == ISD::Constant) {
1971 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001972 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001973 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001974 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001975 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001976 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001977 }
Chris Lattner350bec02006-04-02 06:11:11 +00001978
Chris Lattner06afe072006-05-05 22:53:17 +00001979 // fold (srl (anyextend x), c) -> (anyextend (srl x, c))
1980 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
1981 // Shifting in all undef bits?
1982 MVT::ValueType SmallVT = N0.getOperand(0).getValueType();
1983 if (N1C->getValue() >= MVT::getSizeInBits(SmallVT))
1984 return DAG.getNode(ISD::UNDEF, VT);
1985
1986 SDOperand SmallShift = DAG.getNode(ISD::SRL, SmallVT, N0.getOperand(0), N1);
1987 AddToWorkList(SmallShift.Val);
1988 return DAG.getNode(ISD::ANY_EXTEND, VT, SmallShift);
1989 }
1990
Chris Lattner3657ffe2006-10-12 20:23:19 +00001991 // fold (srl (sra X, Y), 31) -> (srl X, 31). This srl only looks at the sign
1992 // bit, which is unmodified by sra.
1993 if (N1C && N1C->getValue()+1 == MVT::getSizeInBits(VT)) {
1994 if (N0.getOpcode() == ISD::SRA)
1995 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0), N1);
1996 }
1997
Chris Lattner350bec02006-04-02 06:11:11 +00001998 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
1999 if (N1C && N0.getOpcode() == ISD::CTLZ &&
2000 N1C->getValue() == Log2_32(MVT::getSizeInBits(VT))) {
2001 uint64_t KnownZero, KnownOne, Mask = MVT::getIntVTBitMask(VT);
2002 TLI.ComputeMaskedBits(N0.getOperand(0), Mask, KnownZero, KnownOne);
2003
2004 // If any of the input bits are KnownOne, then the input couldn't be all
2005 // zeros, thus the result of the srl will always be zero.
2006 if (KnownOne) return DAG.getConstant(0, VT);
2007
2008 // If all of the bits input the to ctlz node are known to be zero, then
2009 // the result of the ctlz is "32" and the result of the shift is one.
2010 uint64_t UnknownBits = ~KnownZero & Mask;
2011 if (UnknownBits == 0) return DAG.getConstant(1, VT);
2012
2013 // Otherwise, check to see if there is exactly one bit input to the ctlz.
2014 if ((UnknownBits & (UnknownBits-1)) == 0) {
2015 // Okay, we know that only that the single bit specified by UnknownBits
2016 // could be set on input to the CTLZ node. If this bit is set, the SRL
2017 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
2018 // to an SRL,XOR pair, which is likely to simplify more.
2019 unsigned ShAmt = CountTrailingZeros_64(UnknownBits);
2020 SDOperand Op = N0.getOperand(0);
2021 if (ShAmt) {
2022 Op = DAG.getNode(ISD::SRL, VT, Op,
2023 DAG.getConstant(ShAmt, TLI.getShiftAmountTy()));
2024 AddToWorkList(Op.Val);
2025 }
2026 return DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(1, VT));
2027 }
2028 }
Chris Lattner61a4c072007-04-18 03:06:49 +00002029
2030 // fold operands of srl based on knowledge that the low bits are not
2031 // demanded.
2032 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
2033 return SDOperand(N, 0);
2034
Nate Begeman83e75ec2005-09-06 04:43:02 +00002035 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002036}
2037
Nate Begeman83e75ec2005-09-06 04:43:02 +00002038SDOperand DAGCombiner::visitCTLZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002039 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00002040 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002041
2042 // fold (ctlz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002043 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002044 return DAG.getNode(ISD::CTLZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002045 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002046}
2047
Nate Begeman83e75ec2005-09-06 04:43:02 +00002048SDOperand DAGCombiner::visitCTTZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002049 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00002050 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002051
2052 // fold (cttz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002053 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002054 return DAG.getNode(ISD::CTTZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002055 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002056}
2057
Nate Begeman83e75ec2005-09-06 04:43:02 +00002058SDOperand DAGCombiner::visitCTPOP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002059 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00002060 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002061
2062 // fold (ctpop c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002063 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002064 return DAG.getNode(ISD::CTPOP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002065 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002066}
2067
Nate Begeman452d7be2005-09-16 00:54:12 +00002068SDOperand DAGCombiner::visitSELECT(SDNode *N) {
2069 SDOperand N0 = N->getOperand(0);
2070 SDOperand N1 = N->getOperand(1);
2071 SDOperand N2 = N->getOperand(2);
2072 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2073 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2074 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
2075 MVT::ValueType VT = N->getValueType(0);
Nate Begeman44728a72005-09-19 22:34:01 +00002076
Nate Begeman452d7be2005-09-16 00:54:12 +00002077 // fold select C, X, X -> X
2078 if (N1 == N2)
2079 return N1;
2080 // fold select true, X, Y -> X
2081 if (N0C && !N0C->isNullValue())
2082 return N1;
2083 // fold select false, X, Y -> Y
2084 if (N0C && N0C->isNullValue())
2085 return N2;
2086 // fold select C, 1, X -> C | X
Nate Begeman44728a72005-09-19 22:34:01 +00002087 if (MVT::i1 == VT && N1C && N1C->getValue() == 1)
Nate Begeman452d7be2005-09-16 00:54:12 +00002088 return DAG.getNode(ISD::OR, VT, N0, N2);
2089 // fold select C, 0, X -> ~C & X
2090 // FIXME: this should check for C type == X type, not i1?
2091 if (MVT::i1 == VT && N1C && N1C->isNullValue()) {
2092 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00002093 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00002094 return DAG.getNode(ISD::AND, VT, XORNode, N2);
2095 }
2096 // fold select C, X, 1 -> ~C | X
Nate Begeman44728a72005-09-19 22:34:01 +00002097 if (MVT::i1 == VT && N2C && N2C->getValue() == 1) {
Nate Begeman452d7be2005-09-16 00:54:12 +00002098 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00002099 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00002100 return DAG.getNode(ISD::OR, VT, XORNode, N1);
2101 }
2102 // fold select C, X, 0 -> C & X
2103 // FIXME: this should check for C type == X type, not i1?
2104 if (MVT::i1 == VT && N2C && N2C->isNullValue())
2105 return DAG.getNode(ISD::AND, VT, N0, N1);
2106 // fold X ? X : Y --> X ? 1 : Y --> X | Y
2107 if (MVT::i1 == VT && N0 == N1)
2108 return DAG.getNode(ISD::OR, VT, N0, N2);
2109 // fold X ? Y : X --> X ? Y : 0 --> X & Y
2110 if (MVT::i1 == VT && N0 == N2)
2111 return DAG.getNode(ISD::AND, VT, N0, N1);
Chris Lattner729c6d12006-05-27 00:43:02 +00002112
Chris Lattner40c62d52005-10-18 06:04:22 +00002113 // If we can fold this based on the true/false value, do so.
2114 if (SimplifySelectOps(N, N1, N2))
Chris Lattner729c6d12006-05-27 00:43:02 +00002115 return SDOperand(N, 0); // Don't revisit N.
2116
Nate Begeman44728a72005-09-19 22:34:01 +00002117 // fold selects based on a setcc into other things, such as min/max/abs
2118 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman750ac1b2006-02-01 07:19:44 +00002119 // FIXME:
2120 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
2121 // having to say they don't support SELECT_CC on every type the DAG knows
2122 // about, since there is no way to mark an opcode illegal at all value types
2123 if (TLI.isOperationLegal(ISD::SELECT_CC, MVT::Other))
2124 return DAG.getNode(ISD::SELECT_CC, VT, N0.getOperand(0), N0.getOperand(1),
2125 N1, N2, N0.getOperand(2));
2126 else
2127 return SimplifySelect(N0, N1, N2);
Nate Begeman452d7be2005-09-16 00:54:12 +00002128 return SDOperand();
2129}
2130
2131SDOperand DAGCombiner::visitSELECT_CC(SDNode *N) {
Nate Begeman44728a72005-09-19 22:34:01 +00002132 SDOperand N0 = N->getOperand(0);
2133 SDOperand N1 = N->getOperand(1);
2134 SDOperand N2 = N->getOperand(2);
2135 SDOperand N3 = N->getOperand(3);
2136 SDOperand N4 = N->getOperand(4);
Nate Begeman44728a72005-09-19 22:34:01 +00002137 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
2138
Nate Begeman44728a72005-09-19 22:34:01 +00002139 // fold select_cc lhs, rhs, x, x, cc -> x
2140 if (N2 == N3)
2141 return N2;
Chris Lattner40c62d52005-10-18 06:04:22 +00002142
Chris Lattner5f42a242006-09-20 06:19:26 +00002143 // Determine if the condition we're dealing with is constant
2144 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
Chris Lattner30f73e72006-10-14 03:52:46 +00002145 if (SCC.Val) AddToWorkList(SCC.Val);
Chris Lattner5f42a242006-09-20 06:19:26 +00002146
2147 if (ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val)) {
2148 if (SCCC->getValue())
2149 return N2; // cond always true -> true val
2150 else
2151 return N3; // cond always false -> false val
2152 }
2153
2154 // Fold to a simpler select_cc
2155 if (SCC.Val && SCC.getOpcode() == ISD::SETCC)
2156 return DAG.getNode(ISD::SELECT_CC, N2.getValueType(),
2157 SCC.getOperand(0), SCC.getOperand(1), N2, N3,
2158 SCC.getOperand(2));
2159
Chris Lattner40c62d52005-10-18 06:04:22 +00002160 // If we can fold this based on the true/false value, do so.
2161 if (SimplifySelectOps(N, N2, N3))
Chris Lattner729c6d12006-05-27 00:43:02 +00002162 return SDOperand(N, 0); // Don't revisit N.
Chris Lattner40c62d52005-10-18 06:04:22 +00002163
Nate Begeman44728a72005-09-19 22:34:01 +00002164 // fold select_cc into other things, such as min/max/abs
2165 return SimplifySelectCC(N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00002166}
2167
2168SDOperand DAGCombiner::visitSETCC(SDNode *N) {
2169 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
2170 cast<CondCodeSDNode>(N->getOperand(2))->get());
2171}
2172
Nate Begeman83e75ec2005-09-06 04:43:02 +00002173SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002174 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002175 MVT::ValueType VT = N->getValueType(0);
2176
Nate Begeman1d4d4142005-09-01 00:19:25 +00002177 // fold (sext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00002178 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002179 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0);
Chris Lattner310b5782006-05-06 23:06:26 +00002180
Nate Begeman1d4d4142005-09-01 00:19:25 +00002181 // fold (sext (sext x)) -> (sext x)
Chris Lattner310b5782006-05-06 23:06:26 +00002182 // fold (sext (aext x)) -> (sext x)
2183 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002184 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
Chris Lattner310b5782006-05-06 23:06:26 +00002185
Evan Chengc88138f2007-03-22 01:54:19 +00002186 // fold (sext (truncate (load x))) -> (sext (smaller load x))
2187 // fold (sext (truncate (srl (load x), c))) -> (sext (smaller load (x+c/n)))
Chris Lattner22558872007-02-26 03:13:59 +00002188 if (N0.getOpcode() == ISD::TRUNCATE) {
Evan Chengc88138f2007-03-22 01:54:19 +00002189 SDOperand NarrowLoad = ReduceLoadWidth(N0.Val);
Evan Cheng0b063de2007-03-23 02:16:52 +00002190 if (NarrowLoad.Val) {
2191 if (NarrowLoad.Val != N0.Val)
2192 CombineTo(N0.Val, NarrowLoad);
2193 return DAG.getNode(ISD::SIGN_EXTEND, VT, NarrowLoad);
2194 }
Evan Chengc88138f2007-03-22 01:54:19 +00002195 }
2196
2197 // See if the value being truncated is already sign extended. If so, just
2198 // eliminate the trunc/sext pair.
2199 if (N0.getOpcode() == ISD::TRUNCATE) {
Chris Lattner6007b842006-09-21 06:00:20 +00002200 SDOperand Op = N0.getOperand(0);
Chris Lattner22558872007-02-26 03:13:59 +00002201 unsigned OpBits = MVT::getSizeInBits(Op.getValueType());
2202 unsigned MidBits = MVT::getSizeInBits(N0.getValueType());
2203 unsigned DestBits = MVT::getSizeInBits(VT);
2204 unsigned NumSignBits = TLI.ComputeNumSignBits(Op);
2205
2206 if (OpBits == DestBits) {
2207 // Op is i32, Mid is i8, and Dest is i32. If Op has more than 24 sign
2208 // bits, it is already ready.
2209 if (NumSignBits > DestBits-MidBits)
2210 return Op;
2211 } else if (OpBits < DestBits) {
2212 // Op is i32, Mid is i8, and Dest is i64. If Op has more than 24 sign
2213 // bits, just sext from i32.
2214 if (NumSignBits > OpBits-MidBits)
2215 return DAG.getNode(ISD::SIGN_EXTEND, VT, Op);
2216 } else {
2217 // Op is i64, Mid is i8, and Dest is i32. If Op has more than 56 sign
2218 // bits, just truncate to i32.
2219 if (NumSignBits > OpBits-MidBits)
2220 return DAG.getNode(ISD::TRUNCATE, VT, Op);
Chris Lattner6007b842006-09-21 06:00:20 +00002221 }
Chris Lattner22558872007-02-26 03:13:59 +00002222
2223 // fold (sext (truncate x)) -> (sextinreg x).
2224 if (!AfterLegalize || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG,
2225 N0.getValueType())) {
2226 if (Op.getValueType() < VT)
2227 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
2228 else if (Op.getValueType() > VT)
2229 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
2230 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, Op,
2231 DAG.getValueType(N0.getValueType()));
2232 }
Chris Lattner6007b842006-09-21 06:00:20 +00002233 }
Chris Lattner310b5782006-05-06 23:06:26 +00002234
Evan Cheng110dec22005-12-14 02:19:23 +00002235 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00002236 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00002237 (!AfterLegalize||TLI.isLoadXLegal(ISD::SEXTLOAD, N0.getValueType()))){
Evan Cheng466685d2006-10-09 20:57:25 +00002238 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2239 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2240 LN0->getBasePtr(), LN0->getSrcValue(),
2241 LN0->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002242 N0.getValueType(),
2243 LN0->isVolatile());
Chris Lattnerd4771842005-12-14 19:25:30 +00002244 CombineTo(N, ExtLoad);
Chris Lattnerf9884052005-10-13 21:52:31 +00002245 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2246 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002247 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begeman3df4d522005-10-12 20:40:40 +00002248 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002249
2250 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
2251 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00002252 if ((ISD::isSEXTLoad(N0.Val) || ISD::isEXTLoad(N0.Val)) &&
2253 ISD::isUNINDEXEDLoad(N0.Val) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002254 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00002255 MVT::ValueType EVT = LN0->getLoadedVT();
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00002256 if (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT)) {
2257 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2258 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002259 LN0->getSrcValueOffset(), EVT,
2260 LN0->isVolatile(),
2261 LN0->getAlignment());
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00002262 CombineTo(N, ExtLoad);
2263 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2264 ExtLoad.getValue(1));
2265 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2266 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002267 }
2268
Chris Lattner20a35c32007-04-11 05:32:27 +00002269 // sext(setcc x,y,cc) -> select_cc x, y, -1, 0, cc
2270 if (N0.getOpcode() == ISD::SETCC) {
2271 SDOperand SCC =
Chris Lattner1eba01e2007-04-11 06:50:51 +00002272 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
2273 DAG.getConstant(~0ULL, VT), DAG.getConstant(0, VT),
2274 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
2275 if (SCC.Val) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00002276 }
2277
Nate Begeman83e75ec2005-09-06 04:43:02 +00002278 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002279}
2280
Nate Begeman83e75ec2005-09-06 04:43:02 +00002281SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002282 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002283 MVT::ValueType VT = N->getValueType(0);
2284
Nate Begeman1d4d4142005-09-01 00:19:25 +00002285 // fold (zext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00002286 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002287 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002288 // fold (zext (zext x)) -> (zext x)
Chris Lattner310b5782006-05-06 23:06:26 +00002289 // fold (zext (aext x)) -> (zext x)
2290 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002291 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
Chris Lattner6007b842006-09-21 06:00:20 +00002292
Evan Chengc88138f2007-03-22 01:54:19 +00002293 // fold (zext (truncate (load x))) -> (zext (smaller load x))
2294 // fold (zext (truncate (srl (load x), c))) -> (zext (small load (x+c/n)))
Dale Johannesen2041a0e2007-03-30 21:38:07 +00002295 if (N0.getOpcode() == ISD::TRUNCATE) {
Evan Chengc88138f2007-03-22 01:54:19 +00002296 SDOperand NarrowLoad = ReduceLoadWidth(N0.Val);
Evan Cheng0b063de2007-03-23 02:16:52 +00002297 if (NarrowLoad.Val) {
2298 if (NarrowLoad.Val != N0.Val)
2299 CombineTo(N0.Val, NarrowLoad);
2300 return DAG.getNode(ISD::ZERO_EXTEND, VT, NarrowLoad);
2301 }
Evan Chengc88138f2007-03-22 01:54:19 +00002302 }
2303
Chris Lattner6007b842006-09-21 06:00:20 +00002304 // fold (zext (truncate x)) -> (and x, mask)
2305 if (N0.getOpcode() == ISD::TRUNCATE &&
2306 (!AfterLegalize || TLI.isOperationLegal(ISD::AND, VT))) {
2307 SDOperand Op = N0.getOperand(0);
2308 if (Op.getValueType() < VT) {
2309 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
2310 } else if (Op.getValueType() > VT) {
2311 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
2312 }
2313 return DAG.getZeroExtendInReg(Op, N0.getValueType());
2314 }
2315
Chris Lattner111c2282006-09-21 06:14:31 +00002316 // fold (zext (and (trunc x), cst)) -> (and x, cst).
2317 if (N0.getOpcode() == ISD::AND &&
2318 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
2319 N0.getOperand(1).getOpcode() == ISD::Constant) {
2320 SDOperand X = N0.getOperand(0).getOperand(0);
2321 if (X.getValueType() < VT) {
2322 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
2323 } else if (X.getValueType() > VT) {
2324 X = DAG.getNode(ISD::TRUNCATE, VT, X);
2325 }
2326 uint64_t Mask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
2327 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
2328 }
2329
Evan Cheng110dec22005-12-14 02:19:23 +00002330 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00002331 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00002332 (!AfterLegalize||TLI.isLoadXLegal(ISD::ZEXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00002333 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2334 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
2335 LN0->getBasePtr(), LN0->getSrcValue(),
2336 LN0->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002337 N0.getValueType(),
2338 LN0->isVolatile(),
2339 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00002340 CombineTo(N, ExtLoad);
Evan Cheng110dec22005-12-14 02:19:23 +00002341 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2342 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002343 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng110dec22005-12-14 02:19:23 +00002344 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002345
2346 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
2347 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00002348 if ((ISD::isZEXTLoad(N0.Val) || ISD::isEXTLoad(N0.Val)) &&
2349 ISD::isUNINDEXEDLoad(N0.Val) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002350 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00002351 MVT::ValueType EVT = LN0->getLoadedVT();
Evan Cheng466685d2006-10-09 20:57:25 +00002352 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
2353 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002354 LN0->getSrcValueOffset(), EVT,
2355 LN0->isVolatile(),
2356 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00002357 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002358 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2359 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002360 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002361 }
Chris Lattner20a35c32007-04-11 05:32:27 +00002362
2363 // zext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
2364 if (N0.getOpcode() == ISD::SETCC) {
2365 SDOperand SCC =
2366 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
2367 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattner1eba01e2007-04-11 06:50:51 +00002368 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
2369 if (SCC.Val) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00002370 }
2371
Nate Begeman83e75ec2005-09-06 04:43:02 +00002372 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002373}
2374
Chris Lattner5ffc0662006-05-05 05:58:59 +00002375SDOperand DAGCombiner::visitANY_EXTEND(SDNode *N) {
2376 SDOperand N0 = N->getOperand(0);
Chris Lattner5ffc0662006-05-05 05:58:59 +00002377 MVT::ValueType VT = N->getValueType(0);
2378
2379 // fold (aext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00002380 if (isa<ConstantSDNode>(N0))
Chris Lattner5ffc0662006-05-05 05:58:59 +00002381 return DAG.getNode(ISD::ANY_EXTEND, VT, N0);
2382 // fold (aext (aext x)) -> (aext x)
2383 // fold (aext (zext x)) -> (zext x)
2384 // fold (aext (sext x)) -> (sext x)
2385 if (N0.getOpcode() == ISD::ANY_EXTEND ||
2386 N0.getOpcode() == ISD::ZERO_EXTEND ||
2387 N0.getOpcode() == ISD::SIGN_EXTEND)
2388 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
2389
Evan Chengc88138f2007-03-22 01:54:19 +00002390 // fold (aext (truncate (load x))) -> (aext (smaller load x))
2391 // fold (aext (truncate (srl (load x), c))) -> (aext (small load (x+c/n)))
2392 if (N0.getOpcode() == ISD::TRUNCATE) {
2393 SDOperand NarrowLoad = ReduceLoadWidth(N0.Val);
Evan Cheng0b063de2007-03-23 02:16:52 +00002394 if (NarrowLoad.Val) {
2395 if (NarrowLoad.Val != N0.Val)
2396 CombineTo(N0.Val, NarrowLoad);
2397 return DAG.getNode(ISD::ANY_EXTEND, VT, NarrowLoad);
2398 }
Evan Chengc88138f2007-03-22 01:54:19 +00002399 }
2400
Chris Lattner84750582006-09-20 06:29:17 +00002401 // fold (aext (truncate x))
2402 if (N0.getOpcode() == ISD::TRUNCATE) {
2403 SDOperand TruncOp = N0.getOperand(0);
2404 if (TruncOp.getValueType() == VT)
2405 return TruncOp; // x iff x size == zext size.
2406 if (TruncOp.getValueType() > VT)
2407 return DAG.getNode(ISD::TRUNCATE, VT, TruncOp);
2408 return DAG.getNode(ISD::ANY_EXTEND, VT, TruncOp);
2409 }
Chris Lattner0e4b9222006-09-21 06:40:43 +00002410
2411 // fold (aext (and (trunc x), cst)) -> (and x, cst).
2412 if (N0.getOpcode() == ISD::AND &&
2413 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
2414 N0.getOperand(1).getOpcode() == ISD::Constant) {
2415 SDOperand X = N0.getOperand(0).getOperand(0);
2416 if (X.getValueType() < VT) {
2417 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
2418 } else if (X.getValueType() > VT) {
2419 X = DAG.getNode(ISD::TRUNCATE, VT, X);
2420 }
2421 uint64_t Mask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
2422 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
2423 }
2424
Chris Lattner5ffc0662006-05-05 05:58:59 +00002425 // fold (aext (load x)) -> (aext (truncate (extload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00002426 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00002427 (!AfterLegalize||TLI.isLoadXLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00002428 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2429 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
2430 LN0->getBasePtr(), LN0->getSrcValue(),
2431 LN0->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002432 N0.getValueType(),
2433 LN0->isVolatile(),
2434 LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00002435 CombineTo(N, ExtLoad);
2436 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2437 ExtLoad.getValue(1));
2438 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2439 }
2440
2441 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
2442 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
2443 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00002444 if (N0.getOpcode() == ISD::LOAD &&
2445 !ISD::isNON_EXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val) &&
Evan Cheng466685d2006-10-09 20:57:25 +00002446 N0.hasOneUse()) {
2447 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00002448 MVT::ValueType EVT = LN0->getLoadedVT();
Evan Cheng466685d2006-10-09 20:57:25 +00002449 SDOperand ExtLoad = DAG.getExtLoad(LN0->getExtensionType(), VT,
2450 LN0->getChain(), LN0->getBasePtr(),
2451 LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002452 LN0->getSrcValueOffset(), EVT,
2453 LN0->isVolatile(),
2454 LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00002455 CombineTo(N, ExtLoad);
2456 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2457 ExtLoad.getValue(1));
2458 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2459 }
Chris Lattner20a35c32007-04-11 05:32:27 +00002460
2461 // aext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
2462 if (N0.getOpcode() == ISD::SETCC) {
2463 SDOperand SCC =
Chris Lattner1eba01e2007-04-11 06:50:51 +00002464 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
2465 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattnerc24bbad2007-04-11 16:51:53 +00002466 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Chris Lattner1eba01e2007-04-11 06:50:51 +00002467 if (SCC.Val)
Chris Lattnerc56a81d2007-04-11 06:43:25 +00002468 return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00002469 }
2470
Chris Lattner5ffc0662006-05-05 05:58:59 +00002471 return SDOperand();
2472}
2473
Evan Chengc88138f2007-03-22 01:54:19 +00002474/// ReduceLoadWidth - If the result of a wider load is shifted to right of N
2475/// bits and then truncated to a narrower type and where N is a multiple
2476/// of number of bits of the narrower type, transform it to a narrower load
2477/// from address + N / num of bits of new type. If the result is to be
2478/// extended, also fold the extension to form a extending load.
2479SDOperand DAGCombiner::ReduceLoadWidth(SDNode *N) {
2480 unsigned Opc = N->getOpcode();
2481 ISD::LoadExtType ExtType = ISD::NON_EXTLOAD;
2482 SDOperand N0 = N->getOperand(0);
2483 MVT::ValueType VT = N->getValueType(0);
2484 MVT::ValueType EVT = N->getValueType(0);
2485
Evan Chenge177e302007-03-23 22:13:36 +00002486 // Special case: SIGN_EXTEND_INREG is basically truncating to EVT then
2487 // extended to VT.
Evan Chengc88138f2007-03-22 01:54:19 +00002488 if (Opc == ISD::SIGN_EXTEND_INREG) {
2489 ExtType = ISD::SEXTLOAD;
2490 EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Evan Chenge177e302007-03-23 22:13:36 +00002491 if (AfterLegalize && !TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))
2492 return SDOperand();
Evan Chengc88138f2007-03-22 01:54:19 +00002493 }
2494
2495 unsigned EVTBits = MVT::getSizeInBits(EVT);
2496 unsigned ShAmt = 0;
Evan Chengb37b80c2007-03-23 20:55:21 +00002497 bool CombineSRL = false;
Evan Chengc88138f2007-03-22 01:54:19 +00002498 if (N0.getOpcode() == ISD::SRL && N0.hasOneUse()) {
2499 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2500 ShAmt = N01->getValue();
2501 // Is the shift amount a multiple of size of VT?
2502 if ((ShAmt & (EVTBits-1)) == 0) {
2503 N0 = N0.getOperand(0);
2504 if (MVT::getSizeInBits(N0.getValueType()) <= EVTBits)
2505 return SDOperand();
Evan Chengb37b80c2007-03-23 20:55:21 +00002506 CombineSRL = true;
Evan Chengc88138f2007-03-22 01:54:19 +00002507 }
2508 }
2509 }
2510
2511 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
2512 // Do not allow folding to i1 here. i1 is implicitly stored in memory in
2513 // zero extended form: by shrinking the load, we lose track of the fact
2514 // that it is already zero extended.
2515 // FIXME: This should be reevaluated.
2516 VT != MVT::i1) {
2517 assert(MVT::getSizeInBits(N0.getValueType()) > EVTBits &&
2518 "Cannot truncate to larger type!");
2519 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2520 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
Evan Chengdae54ce2007-03-24 00:02:43 +00002521 // For big endian targets, we need to adjust the offset to the pointer to
2522 // load the correct bytes.
2523 if (!TLI.isLittleEndian())
2524 ShAmt = MVT::getSizeInBits(N0.getValueType()) - ShAmt - EVTBits;
2525 uint64_t PtrOff = ShAmt / 8;
Evan Chengc88138f2007-03-22 01:54:19 +00002526 SDOperand NewPtr = DAG.getNode(ISD::ADD, PtrType, LN0->getBasePtr(),
2527 DAG.getConstant(PtrOff, PtrType));
2528 AddToWorkList(NewPtr.Val);
2529 SDOperand Load = (ExtType == ISD::NON_EXTLOAD)
2530 ? DAG.getLoad(VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002531 LN0->getSrcValue(), LN0->getSrcValueOffset(),
2532 LN0->isVolatile(), LN0->getAlignment())
Evan Chengc88138f2007-03-22 01:54:19 +00002533 : DAG.getExtLoad(ExtType, VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002534 LN0->getSrcValue(), LN0->getSrcValueOffset(), EVT,
2535 LN0->isVolatile(), LN0->getAlignment());
Evan Chengc88138f2007-03-22 01:54:19 +00002536 AddToWorkList(N);
Evan Chengb37b80c2007-03-23 20:55:21 +00002537 if (CombineSRL) {
2538 std::vector<SDNode*> NowDead;
2539 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), Load.getValue(1), NowDead);
2540 CombineTo(N->getOperand(0).Val, Load);
2541 } else
2542 CombineTo(N0.Val, Load, Load.getValue(1));
Evan Cheng15213b72007-03-26 07:12:51 +00002543 if (ShAmt) {
2544 if (Opc == ISD::SIGN_EXTEND_INREG)
2545 return DAG.getNode(Opc, VT, Load, N->getOperand(1));
2546 else
2547 return DAG.getNode(Opc, VT, Load);
2548 }
Evan Chengc88138f2007-03-22 01:54:19 +00002549 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2550 }
2551
2552 return SDOperand();
2553}
2554
Chris Lattner5ffc0662006-05-05 05:58:59 +00002555
Nate Begeman83e75ec2005-09-06 04:43:02 +00002556SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002557 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002558 SDOperand N1 = N->getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002559 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002560 MVT::ValueType EVT = cast<VTSDNode>(N1)->getVT();
Nate Begeman07ed4172005-10-10 21:26:48 +00002561 unsigned EVTBits = MVT::getSizeInBits(EVT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002562
Nate Begeman1d4d4142005-09-01 00:19:25 +00002563 // fold (sext_in_reg c1) -> c1
Chris Lattnereaeda562006-05-08 20:59:41 +00002564 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
Chris Lattner310b5782006-05-06 23:06:26 +00002565 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0, N1);
Chris Lattneree4ea922006-05-06 09:30:03 +00002566
Chris Lattner541a24f2006-05-06 22:43:44 +00002567 // If the input is already sign extended, just drop the extension.
Chris Lattneree4ea922006-05-06 09:30:03 +00002568 if (TLI.ComputeNumSignBits(N0) >= MVT::getSizeInBits(VT)-EVTBits+1)
2569 return N0;
2570
Nate Begeman646d7e22005-09-02 21:18:40 +00002571 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
2572 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
2573 EVT < cast<VTSDNode>(N0.getOperand(1))->getVT()) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00002574 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002575 }
Chris Lattner4b37e872006-05-08 21:18:59 +00002576
Chris Lattner95a5e052007-04-17 19:03:21 +00002577 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is known zero.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00002578 if (TLI.MaskedValueIsZero(N0, 1ULL << (EVTBits-1)))
Nate Begemande996292006-02-03 22:24:05 +00002579 return DAG.getZeroExtendInReg(N0, EVT);
Chris Lattner4b37e872006-05-08 21:18:59 +00002580
Chris Lattner95a5e052007-04-17 19:03:21 +00002581 // fold operands of sext_in_reg based on knowledge that the top bits are not
2582 // demanded.
2583 if (SimplifyDemandedBits(SDOperand(N, 0)))
2584 return SDOperand(N, 0);
2585
Evan Chengc88138f2007-03-22 01:54:19 +00002586 // fold (sext_in_reg (load x)) -> (smaller sextload x)
2587 // fold (sext_in_reg (srl (load x), c)) -> (smaller sextload (x+c/evtbits))
2588 SDOperand NarrowLoad = ReduceLoadWidth(N);
2589 if (NarrowLoad.Val)
2590 return NarrowLoad;
2591
Chris Lattner4b37e872006-05-08 21:18:59 +00002592 // fold (sext_in_reg (srl X, 24), i8) -> sra X, 24
2593 // fold (sext_in_reg (srl X, 23), i8) -> sra X, 23 iff possible.
2594 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
2595 if (N0.getOpcode() == ISD::SRL) {
2596 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
2597 if (ShAmt->getValue()+EVTBits <= MVT::getSizeInBits(VT)) {
2598 // We can turn this into an SRA iff the input to the SRL is already sign
2599 // extended enough.
2600 unsigned InSignBits = TLI.ComputeNumSignBits(N0.getOperand(0));
2601 if (MVT::getSizeInBits(VT)-(ShAmt->getValue()+EVTBits) < InSignBits)
2602 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0), N0.getOperand(1));
2603 }
2604 }
Evan Chengc88138f2007-03-22 01:54:19 +00002605
Nate Begemanded49632005-10-13 03:11:28 +00002606 // fold (sext_inreg (extload x)) -> (sextload x)
Evan Chengc5484282006-10-04 00:56:09 +00002607 if (ISD::isEXTLoad(N0.Val) &&
Evan Cheng83060c52007-03-07 08:07:03 +00002608 ISD::isUNINDEXEDLoad(N0.Val) &&
Evan Cheng2e49f092006-10-11 07:10:22 +00002609 EVT == cast<LoadSDNode>(N0)->getLoadedVT() &&
Evan Chengc5484282006-10-04 00:56:09 +00002610 (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00002611 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2612 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2613 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002614 LN0->getSrcValueOffset(), EVT,
2615 LN0->isVolatile(),
2616 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00002617 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00002618 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002619 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00002620 }
2621 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Evan Cheng83060c52007-03-07 08:07:03 +00002622 if (ISD::isZEXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val) &&
2623 N0.hasOneUse() &&
Evan Cheng2e49f092006-10-11 07:10:22 +00002624 EVT == cast<LoadSDNode>(N0)->getLoadedVT() &&
Evan Chengc5484282006-10-04 00:56:09 +00002625 (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00002626 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2627 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2628 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002629 LN0->getSrcValueOffset(), EVT,
2630 LN0->isVolatile(),
2631 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00002632 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00002633 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002634 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00002635 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00002636 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002637}
2638
Nate Begeman83e75ec2005-09-06 04:43:02 +00002639SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002640 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002641 MVT::ValueType VT = N->getValueType(0);
2642
2643 // noop truncate
2644 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002645 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002646 // fold (truncate c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00002647 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002648 return DAG.getNode(ISD::TRUNCATE, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002649 // fold (truncate (truncate x)) -> (truncate x)
2650 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002651 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002652 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
Chris Lattnerb72773b2006-05-05 22:56:26 +00002653 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND||
2654 N0.getOpcode() == ISD::ANY_EXTEND) {
Chris Lattner32ba1aa2006-11-20 18:05:46 +00002655 if (N0.getOperand(0).getValueType() < VT)
Nate Begeman1d4d4142005-09-01 00:19:25 +00002656 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00002657 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Chris Lattner32ba1aa2006-11-20 18:05:46 +00002658 else if (N0.getOperand(0).getValueType() > VT)
Nate Begeman1d4d4142005-09-01 00:19:25 +00002659 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00002660 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002661 else
2662 // if the source and dest are the same type, we can drop both the extend
2663 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00002664 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002665 }
Evan Cheng007b69e2007-03-21 20:14:05 +00002666
Nate Begeman3df4d522005-10-12 20:40:40 +00002667 // fold (truncate (load x)) -> (smaller load x)
Evan Cheng007b69e2007-03-21 20:14:05 +00002668 // fold (truncate (srl (load x), c)) -> (smaller load (x+c/evtbits))
Evan Chengc88138f2007-03-22 01:54:19 +00002669 return ReduceLoadWidth(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002670}
2671
Chris Lattner94683772005-12-23 05:30:37 +00002672SDOperand DAGCombiner::visitBIT_CONVERT(SDNode *N) {
2673 SDOperand N0 = N->getOperand(0);
2674 MVT::ValueType VT = N->getValueType(0);
2675
2676 // If the input is a constant, let getNode() fold it.
2677 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
2678 SDOperand Res = DAG.getNode(ISD::BIT_CONVERT, VT, N0);
2679 if (Res.Val != N) return Res;
2680 }
2681
Chris Lattnerc8547d82005-12-23 05:37:50 +00002682 if (N0.getOpcode() == ISD::BIT_CONVERT) // conv(conv(x,t1),t2) -> conv(x,t2)
2683 return DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
Chris Lattner6258fb22006-04-02 02:53:43 +00002684
Chris Lattner57104102005-12-23 05:44:41 +00002685 // fold (conv (load x)) -> (load (conv*)x)
Evan Cheng59d5b682007-05-07 21:27:48 +00002686 // If the resultant load doesn't need a higher alignment than the original!
2687 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Dale Johannesen98a6c622007-05-16 22:45:30 +00002688 ISD::isUNINDEXEDLoad(N0.Val) &&
Evan Cheng59d5b682007-05-07 21:27:48 +00002689 TLI.isOperationLegal(ISD::LOAD, VT)) {
Evan Cheng466685d2006-10-09 20:57:25 +00002690 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng59d5b682007-05-07 21:27:48 +00002691 unsigned Align = TLI.getTargetMachine().getTargetData()->
Evan Cheng93003b82007-05-16 02:04:50 +00002692 getABITypeAlignment(getTypeForValueType(VT));
Evan Cheng59d5b682007-05-07 21:27:48 +00002693 unsigned OrigAlign = LN0->getAlignment();
2694 if (Align <= OrigAlign) {
2695 SDOperand Load = DAG.getLoad(VT, LN0->getChain(), LN0->getBasePtr(),
2696 LN0->getSrcValue(), LN0->getSrcValueOffset(),
2697 LN0->isVolatile(), LN0->getAlignment());
2698 AddToWorkList(N);
2699 CombineTo(N0.Val, DAG.getNode(ISD::BIT_CONVERT, N0.getValueType(), Load),
2700 Load.getValue(1));
2701 return Load;
2702 }
Chris Lattner57104102005-12-23 05:44:41 +00002703 }
2704
Chris Lattner94683772005-12-23 05:30:37 +00002705 return SDOperand();
2706}
2707
Chris Lattner6258fb22006-04-02 02:53:43 +00002708SDOperand DAGCombiner::visitVBIT_CONVERT(SDNode *N) {
2709 SDOperand N0 = N->getOperand(0);
2710 MVT::ValueType VT = N->getValueType(0);
2711
2712 // If the input is a VBUILD_VECTOR with all constant elements, fold this now.
2713 // First check to see if this is all constant.
2714 if (N0.getOpcode() == ISD::VBUILD_VECTOR && N0.Val->hasOneUse() &&
2715 VT == MVT::Vector) {
2716 bool isSimple = true;
2717 for (unsigned i = 0, e = N0.getNumOperands()-2; i != e; ++i)
2718 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
2719 N0.getOperand(i).getOpcode() != ISD::Constant &&
2720 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
2721 isSimple = false;
2722 break;
2723 }
2724
Chris Lattner97c20732006-04-03 17:29:28 +00002725 MVT::ValueType DestEltVT = cast<VTSDNode>(N->getOperand(2))->getVT();
2726 if (isSimple && !MVT::isVector(DestEltVT)) {
Chris Lattner6258fb22006-04-02 02:53:43 +00002727 return ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(N0.Val, DestEltVT);
2728 }
2729 }
2730
2731 return SDOperand();
2732}
2733
2734/// ConstantFoldVBIT_CONVERTofVBUILD_VECTOR - We know that BV is a vbuild_vector
2735/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
2736/// destination element value type.
2737SDOperand DAGCombiner::
2738ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(SDNode *BV, MVT::ValueType DstEltVT) {
2739 MVT::ValueType SrcEltVT = BV->getOperand(0).getValueType();
2740
2741 // If this is already the right type, we're done.
2742 if (SrcEltVT == DstEltVT) return SDOperand(BV, 0);
2743
2744 unsigned SrcBitSize = MVT::getSizeInBits(SrcEltVT);
2745 unsigned DstBitSize = MVT::getSizeInBits(DstEltVT);
2746
2747 // If this is a conversion of N elements of one type to N elements of another
2748 // type, convert each element. This handles FP<->INT cases.
2749 if (SrcBitSize == DstBitSize) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002750 SmallVector<SDOperand, 8> Ops;
Chris Lattner3e104b12006-04-08 04:15:24 +00002751 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00002752 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, DstEltVT, BV->getOperand(i)));
Chris Lattner3e104b12006-04-08 04:15:24 +00002753 AddToWorkList(Ops.back().Val);
2754 }
Chris Lattner6258fb22006-04-02 02:53:43 +00002755 Ops.push_back(*(BV->op_end()-2)); // Add num elements.
2756 Ops.push_back(DAG.getValueType(DstEltVT));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002757 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00002758 }
2759
2760 // Otherwise, we're growing or shrinking the elements. To avoid having to
2761 // handle annoying details of growing/shrinking FP values, we convert them to
2762 // int first.
2763 if (MVT::isFloatingPoint(SrcEltVT)) {
2764 // Convert the input float vector to a int vector where the elements are the
2765 // same sizes.
2766 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
2767 MVT::ValueType IntVT = SrcEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
2768 BV = ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(BV, IntVT).Val;
2769 SrcEltVT = IntVT;
2770 }
2771
2772 // Now we know the input is an integer vector. If the output is a FP type,
2773 // convert to integer first, then to FP of the right size.
2774 if (MVT::isFloatingPoint(DstEltVT)) {
2775 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
2776 MVT::ValueType TmpVT = DstEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
2777 SDNode *Tmp = ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(BV, TmpVT).Val;
2778
2779 // Next, convert to FP elements of the same size.
2780 return ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(Tmp, DstEltVT);
2781 }
2782
2783 // Okay, we know the src/dst types are both integers of differing types.
2784 // Handling growing first.
2785 assert(MVT::isInteger(SrcEltVT) && MVT::isInteger(DstEltVT));
2786 if (SrcBitSize < DstBitSize) {
2787 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
2788
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002789 SmallVector<SDOperand, 8> Ops;
Chris Lattner6258fb22006-04-02 02:53:43 +00002790 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e;
2791 i += NumInputsPerOutput) {
2792 bool isLE = TLI.isLittleEndian();
2793 uint64_t NewBits = 0;
2794 bool EltIsUndef = true;
2795 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
2796 // Shift the previously computed bits over.
2797 NewBits <<= SrcBitSize;
2798 SDOperand Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
2799 if (Op.getOpcode() == ISD::UNDEF) continue;
2800 EltIsUndef = false;
2801
2802 NewBits |= cast<ConstantSDNode>(Op)->getValue();
2803 }
2804
2805 if (EltIsUndef)
2806 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
2807 else
2808 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
2809 }
2810
2811 Ops.push_back(DAG.getConstant(Ops.size(), MVT::i32)); // Add num elements.
2812 Ops.push_back(DAG.getValueType(DstEltVT)); // Add element size.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002813 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00002814 }
2815
2816 // Finally, this must be the case where we are shrinking elements: each input
2817 // turns into multiple outputs.
2818 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002819 SmallVector<SDOperand, 8> Ops;
Chris Lattner6258fb22006-04-02 02:53:43 +00002820 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e; ++i) {
2821 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
2822 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
2823 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
2824 continue;
2825 }
2826 uint64_t OpVal = cast<ConstantSDNode>(BV->getOperand(i))->getValue();
2827
2828 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
2829 unsigned ThisVal = OpVal & ((1ULL << DstBitSize)-1);
2830 OpVal >>= DstBitSize;
2831 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
2832 }
2833
2834 // For big endian targets, swap the order of the pieces of each element.
2835 if (!TLI.isLittleEndian())
2836 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
2837 }
2838 Ops.push_back(DAG.getConstant(Ops.size(), MVT::i32)); // Add num elements.
2839 Ops.push_back(DAG.getValueType(DstEltVT)); // Add element size.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002840 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00002841}
2842
2843
2844
Chris Lattner01b3d732005-09-28 22:28:18 +00002845SDOperand DAGCombiner::visitFADD(SDNode *N) {
2846 SDOperand N0 = N->getOperand(0);
2847 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00002848 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2849 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002850 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00002851
2852 // fold (fadd c1, c2) -> c1+c2
2853 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002854 return DAG.getNode(ISD::FADD, VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00002855 // canonicalize constant to RHS
2856 if (N0CFP && !N1CFP)
2857 return DAG.getNode(ISD::FADD, VT, N1, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00002858 // fold (A + (-B)) -> A-B
Chris Lattner29446522007-05-14 22:04:50 +00002859 if (isNegatibleForFree(N1) == 2)
2860 return DAG.getNode(ISD::FSUB, VT, N0, GetNegatedExpression(N1, DAG));
Chris Lattner01b3d732005-09-28 22:28:18 +00002861 // fold ((-A) + B) -> B-A
Chris Lattner29446522007-05-14 22:04:50 +00002862 if (isNegatibleForFree(N0) == 2)
2863 return DAG.getNode(ISD::FSUB, VT, N1, GetNegatedExpression(N0, DAG));
Chris Lattnerddae4bd2007-01-08 23:04:05 +00002864
2865 // If allowed, fold (fadd (fadd x, c1), c2) -> (fadd x, (fadd c1, c2))
2866 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FADD &&
2867 N0.Val->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
2868 return DAG.getNode(ISD::FADD, VT, N0.getOperand(0),
2869 DAG.getNode(ISD::FADD, VT, N0.getOperand(1), N1));
2870
Chris Lattner01b3d732005-09-28 22:28:18 +00002871 return SDOperand();
2872}
2873
2874SDOperand DAGCombiner::visitFSUB(SDNode *N) {
2875 SDOperand N0 = N->getOperand(0);
2876 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00002877 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2878 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002879 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00002880
2881 // fold (fsub c1, c2) -> c1-c2
2882 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002883 return DAG.getNode(ISD::FSUB, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002884 // fold (A-(-B)) -> A+B
Chris Lattner29446522007-05-14 22:04:50 +00002885 if (isNegatibleForFree(N1))
2886 return DAG.getNode(ISD::FADD, VT, N0, GetNegatedExpression(N1, DAG));
2887
Chris Lattner01b3d732005-09-28 22:28:18 +00002888 return SDOperand();
2889}
2890
2891SDOperand DAGCombiner::visitFMUL(SDNode *N) {
2892 SDOperand N0 = N->getOperand(0);
2893 SDOperand N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00002894 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2895 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002896 MVT::ValueType VT = N->getValueType(0);
2897
Nate Begeman11af4ea2005-10-17 20:40:11 +00002898 // fold (fmul c1, c2) -> c1*c2
2899 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002900 return DAG.getNode(ISD::FMUL, VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00002901 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00002902 if (N0CFP && !N1CFP)
2903 return DAG.getNode(ISD::FMUL, VT, N1, N0);
Nate Begeman11af4ea2005-10-17 20:40:11 +00002904 // fold (fmul X, 2.0) -> (fadd X, X)
2905 if (N1CFP && N1CFP->isExactlyValue(+2.0))
2906 return DAG.getNode(ISD::FADD, VT, N0, N0);
Chris Lattner29446522007-05-14 22:04:50 +00002907 // fold (fmul X, -1.0) -> (fneg X)
2908 if (N1CFP && N1CFP->isExactlyValue(-1.0))
2909 return DAG.getNode(ISD::FNEG, VT, N0);
2910
2911 // -X * -Y -> X*Y
2912 if (char LHSNeg = isNegatibleForFree(N0)) {
2913 if (char RHSNeg = isNegatibleForFree(N1)) {
2914 // Both can be negated for free, check to see if at least one is cheaper
2915 // negated.
2916 if (LHSNeg == 2 || RHSNeg == 2)
2917 return DAG.getNode(ISD::FMUL, VT, GetNegatedExpression(N0, DAG),
2918 GetNegatedExpression(N1, DAG));
2919 }
2920 }
Chris Lattnerddae4bd2007-01-08 23:04:05 +00002921
2922 // If allowed, fold (fmul (fmul x, c1), c2) -> (fmul x, (fmul c1, c2))
2923 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FMUL &&
2924 N0.Val->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
2925 return DAG.getNode(ISD::FMUL, VT, N0.getOperand(0),
2926 DAG.getNode(ISD::FMUL, VT, N0.getOperand(1), N1));
2927
Chris Lattner01b3d732005-09-28 22:28:18 +00002928 return SDOperand();
2929}
2930
2931SDOperand DAGCombiner::visitFDIV(SDNode *N) {
2932 SDOperand N0 = N->getOperand(0);
2933 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00002934 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2935 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002936 MVT::ValueType VT = N->getValueType(0);
2937
Nate Begemana148d982006-01-18 22:35:16 +00002938 // fold (fdiv c1, c2) -> c1/c2
2939 if (N0CFP && N1CFP)
2940 return DAG.getNode(ISD::FDIV, VT, N0, N1);
Chris Lattner29446522007-05-14 22:04:50 +00002941
2942
2943 // -X / -Y -> X*Y
2944 if (char LHSNeg = isNegatibleForFree(N0)) {
2945 if (char RHSNeg = isNegatibleForFree(N1)) {
2946 // Both can be negated for free, check to see if at least one is cheaper
2947 // negated.
2948 if (LHSNeg == 2 || RHSNeg == 2)
2949 return DAG.getNode(ISD::FDIV, VT, GetNegatedExpression(N0, DAG),
2950 GetNegatedExpression(N1, DAG));
2951 }
2952 }
2953
Chris Lattner01b3d732005-09-28 22:28:18 +00002954 return SDOperand();
2955}
2956
2957SDOperand DAGCombiner::visitFREM(SDNode *N) {
2958 SDOperand N0 = N->getOperand(0);
2959 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00002960 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2961 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002962 MVT::ValueType VT = N->getValueType(0);
2963
Nate Begemana148d982006-01-18 22:35:16 +00002964 // fold (frem c1, c2) -> fmod(c1,c2)
2965 if (N0CFP && N1CFP)
2966 return DAG.getNode(ISD::FREM, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002967 return SDOperand();
2968}
2969
Chris Lattner12d83032006-03-05 05:30:57 +00002970SDOperand DAGCombiner::visitFCOPYSIGN(SDNode *N) {
2971 SDOperand N0 = N->getOperand(0);
2972 SDOperand N1 = N->getOperand(1);
2973 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2974 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2975 MVT::ValueType VT = N->getValueType(0);
2976
2977 if (N0CFP && N1CFP) // Constant fold
2978 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1);
2979
2980 if (N1CFP) {
2981 // copysign(x, c1) -> fabs(x) iff ispos(c1)
2982 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
2983 union {
2984 double d;
2985 int64_t i;
2986 } u;
2987 u.d = N1CFP->getValue();
2988 if (u.i >= 0)
2989 return DAG.getNode(ISD::FABS, VT, N0);
2990 else
2991 return DAG.getNode(ISD::FNEG, VT, DAG.getNode(ISD::FABS, VT, N0));
2992 }
2993
2994 // copysign(fabs(x), y) -> copysign(x, y)
2995 // copysign(fneg(x), y) -> copysign(x, y)
2996 // copysign(copysign(x,z), y) -> copysign(x, y)
2997 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
2998 N0.getOpcode() == ISD::FCOPYSIGN)
2999 return DAG.getNode(ISD::FCOPYSIGN, VT, N0.getOperand(0), N1);
3000
3001 // copysign(x, abs(y)) -> abs(x)
3002 if (N1.getOpcode() == ISD::FABS)
3003 return DAG.getNode(ISD::FABS, VT, N0);
3004
3005 // copysign(x, copysign(y,z)) -> copysign(x, z)
3006 if (N1.getOpcode() == ISD::FCOPYSIGN)
3007 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(1));
3008
3009 // copysign(x, fp_extend(y)) -> copysign(x, y)
3010 // copysign(x, fp_round(y)) -> copysign(x, y)
3011 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
3012 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(0));
3013
3014 return SDOperand();
3015}
3016
3017
Chris Lattner01b3d732005-09-28 22:28:18 +00003018
Nate Begeman83e75ec2005-09-06 04:43:02 +00003019SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003020 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003021 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00003022 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003023
3024 // fold (sint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00003025 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00003026 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003027 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003028}
3029
Nate Begeman83e75ec2005-09-06 04:43:02 +00003030SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003031 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003032 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00003033 MVT::ValueType VT = N->getValueType(0);
3034
Nate Begeman1d4d4142005-09-01 00:19:25 +00003035 // fold (uint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00003036 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00003037 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003038 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003039}
3040
Nate Begeman83e75ec2005-09-06 04:43:02 +00003041SDOperand DAGCombiner::visitFP_TO_SINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003042 SDOperand N0 = N->getOperand(0);
3043 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3044 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003045
3046 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00003047 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00003048 return DAG.getNode(ISD::FP_TO_SINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003049 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003050}
3051
Nate Begeman83e75ec2005-09-06 04:43:02 +00003052SDOperand DAGCombiner::visitFP_TO_UINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003053 SDOperand N0 = N->getOperand(0);
3054 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3055 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003056
3057 // fold (fp_to_uint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00003058 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00003059 return DAG.getNode(ISD::FP_TO_UINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003060 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003061}
3062
Nate Begeman83e75ec2005-09-06 04:43:02 +00003063SDOperand DAGCombiner::visitFP_ROUND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003064 SDOperand N0 = N->getOperand(0);
3065 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3066 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003067
3068 // fold (fp_round c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00003069 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00003070 return DAG.getNode(ISD::FP_ROUND, VT, N0);
Chris Lattner79dbea52006-03-13 06:26:26 +00003071
3072 // fold (fp_round (fp_extend x)) -> x
3073 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
3074 return N0.getOperand(0);
3075
3076 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
3077 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.Val->hasOneUse()) {
3078 SDOperand Tmp = DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0));
3079 AddToWorkList(Tmp.Val);
3080 return DAG.getNode(ISD::FCOPYSIGN, VT, Tmp, N0.getOperand(1));
3081 }
3082
Nate Begeman83e75ec2005-09-06 04:43:02 +00003083 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003084}
3085
Nate Begeman83e75ec2005-09-06 04:43:02 +00003086SDOperand DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003087 SDOperand N0 = N->getOperand(0);
3088 MVT::ValueType VT = N->getValueType(0);
3089 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00003090 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003091
Nate Begeman1d4d4142005-09-01 00:19:25 +00003092 // fold (fp_round_inreg c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00003093 if (N0CFP) {
3094 SDOperand Round = DAG.getConstantFP(N0CFP->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003095 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003096 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00003097 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003098}
3099
Nate Begeman83e75ec2005-09-06 04:43:02 +00003100SDOperand DAGCombiner::visitFP_EXTEND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003101 SDOperand N0 = N->getOperand(0);
3102 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3103 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003104
3105 // fold (fp_extend c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00003106 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00003107 return DAG.getNode(ISD::FP_EXTEND, VT, N0);
Chris Lattnere564dbb2006-05-05 21:34:35 +00003108
3109 // fold (fpext (load x)) -> (fpext (fpround (extload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00003110 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00003111 (!AfterLegalize||TLI.isLoadXLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003112 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3113 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
3114 LN0->getBasePtr(), LN0->getSrcValue(),
3115 LN0->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003116 N0.getValueType(),
3117 LN0->isVolatile(),
3118 LN0->getAlignment());
Chris Lattnere564dbb2006-05-05 21:34:35 +00003119 CombineTo(N, ExtLoad);
3120 CombineTo(N0.Val, DAG.getNode(ISD::FP_ROUND, N0.getValueType(), ExtLoad),
3121 ExtLoad.getValue(1));
3122 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
3123 }
3124
3125
Nate Begeman83e75ec2005-09-06 04:43:02 +00003126 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003127}
3128
Nate Begeman83e75ec2005-09-06 04:43:02 +00003129SDOperand DAGCombiner::visitFNEG(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003130 SDOperand N0 = N->getOperand(0);
3131 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3132 MVT::ValueType VT = N->getValueType(0);
3133
3134 // fold (fneg c1) -> -c1
Nate Begeman646d7e22005-09-02 21:18:40 +00003135 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00003136 return DAG.getNode(ISD::FNEG, VT, N0);
3137 // fold (fneg (sub x, y)) -> (sub y, x)
Chris Lattner12d83032006-03-05 05:30:57 +00003138 if (N0.getOpcode() == ISD::SUB)
3139 return DAG.getNode(ISD::SUB, VT, N0.getOperand(1), N0.getOperand(0));
Nate Begemana148d982006-01-18 22:35:16 +00003140 // fold (fneg (fneg x)) -> x
Chris Lattner12d83032006-03-05 05:30:57 +00003141 if (N0.getOpcode() == ISD::FNEG)
3142 return N0.getOperand(0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003143 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003144}
3145
Nate Begeman83e75ec2005-09-06 04:43:02 +00003146SDOperand DAGCombiner::visitFABS(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003147 SDOperand N0 = N->getOperand(0);
3148 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3149 MVT::ValueType VT = N->getValueType(0);
3150
Nate Begeman1d4d4142005-09-01 00:19:25 +00003151 // fold (fabs c1) -> fabs(c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00003152 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00003153 return DAG.getNode(ISD::FABS, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003154 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00003155 if (N0.getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00003156 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003157 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00003158 // fold (fabs (fcopysign x, y)) -> (fabs x)
3159 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
3160 return DAG.getNode(ISD::FABS, VT, N0.getOperand(0));
3161
Nate Begeman83e75ec2005-09-06 04:43:02 +00003162 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003163}
3164
Nate Begeman44728a72005-09-19 22:34:01 +00003165SDOperand DAGCombiner::visitBRCOND(SDNode *N) {
3166 SDOperand Chain = N->getOperand(0);
3167 SDOperand N1 = N->getOperand(1);
3168 SDOperand N2 = N->getOperand(2);
3169 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
3170
3171 // never taken branch, fold to chain
3172 if (N1C && N1C->isNullValue())
3173 return Chain;
3174 // unconditional branch
Nate Begemane17daeb2005-10-05 21:43:42 +00003175 if (N1C && N1C->getValue() == 1)
Nate Begeman44728a72005-09-19 22:34:01 +00003176 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
Nate Begeman750ac1b2006-02-01 07:19:44 +00003177 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
3178 // on the target.
3179 if (N1.getOpcode() == ISD::SETCC &&
3180 TLI.isOperationLegal(ISD::BR_CC, MVT::Other)) {
3181 return DAG.getNode(ISD::BR_CC, MVT::Other, Chain, N1.getOperand(2),
3182 N1.getOperand(0), N1.getOperand(1), N2);
3183 }
Nate Begeman44728a72005-09-19 22:34:01 +00003184 return SDOperand();
3185}
3186
Chris Lattner3ea0b472005-10-05 06:47:48 +00003187// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
3188//
Nate Begeman44728a72005-09-19 22:34:01 +00003189SDOperand DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00003190 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
3191 SDOperand CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
3192
3193 // Use SimplifySetCC to simplify SETCC's.
Nate Begemane17daeb2005-10-05 21:43:42 +00003194 SDOperand Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false);
Chris Lattner30f73e72006-10-14 03:52:46 +00003195 if (Simp.Val) AddToWorkList(Simp.Val);
3196
Nate Begemane17daeb2005-10-05 21:43:42 +00003197 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.Val);
3198
3199 // fold br_cc true, dest -> br dest (unconditional branch)
3200 if (SCCC && SCCC->getValue())
3201 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
3202 N->getOperand(4));
3203 // fold br_cc false, dest -> unconditional fall through
3204 if (SCCC && SCCC->isNullValue())
3205 return N->getOperand(0);
Chris Lattner30f73e72006-10-14 03:52:46 +00003206
Nate Begemane17daeb2005-10-05 21:43:42 +00003207 // fold to a simpler setcc
3208 if (Simp.Val && Simp.getOpcode() == ISD::SETCC)
3209 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
3210 Simp.getOperand(2), Simp.getOperand(0),
3211 Simp.getOperand(1), N->getOperand(4));
Nate Begeman44728a72005-09-19 22:34:01 +00003212 return SDOperand();
3213}
3214
Chris Lattner448f2192006-11-11 00:39:41 +00003215
3216/// CombineToPreIndexedLoadStore - Try turning a load / store and a
3217/// pre-indexed load / store when the base pointer is a add or subtract
3218/// and it has other uses besides the load / store. After the
3219/// transformation, the new indexed load / store has effectively folded
3220/// the add / subtract in and all of its other uses are redirected to the
3221/// new load / store.
3222bool DAGCombiner::CombineToPreIndexedLoadStore(SDNode *N) {
3223 if (!AfterLegalize)
3224 return false;
3225
3226 bool isLoad = true;
3227 SDOperand Ptr;
3228 MVT::ValueType VT;
3229 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Evan Chenge90460e2006-12-16 06:25:23 +00003230 if (LD->getAddressingMode() != ISD::UNINDEXED)
3231 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00003232 VT = LD->getLoadedVT();
Evan Cheng83060c52007-03-07 08:07:03 +00003233 if (!TLI.isIndexedLoadLegal(ISD::PRE_INC, VT) &&
Chris Lattner448f2192006-11-11 00:39:41 +00003234 !TLI.isIndexedLoadLegal(ISD::PRE_DEC, VT))
3235 return false;
3236 Ptr = LD->getBasePtr();
3237 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Evan Chenge90460e2006-12-16 06:25:23 +00003238 if (ST->getAddressingMode() != ISD::UNINDEXED)
3239 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00003240 VT = ST->getStoredVT();
3241 if (!TLI.isIndexedStoreLegal(ISD::PRE_INC, VT) &&
3242 !TLI.isIndexedStoreLegal(ISD::PRE_DEC, VT))
3243 return false;
3244 Ptr = ST->getBasePtr();
3245 isLoad = false;
3246 } else
3247 return false;
3248
Chris Lattner9f1794e2006-11-11 00:56:29 +00003249 // If the pointer is not an add/sub, or if it doesn't have multiple uses, bail
3250 // out. There is no reason to make this a preinc/predec.
3251 if ((Ptr.getOpcode() != ISD::ADD && Ptr.getOpcode() != ISD::SUB) ||
3252 Ptr.Val->hasOneUse())
3253 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00003254
Chris Lattner9f1794e2006-11-11 00:56:29 +00003255 // Ask the target to do addressing mode selection.
3256 SDOperand BasePtr;
3257 SDOperand Offset;
3258 ISD::MemIndexedMode AM = ISD::UNINDEXED;
3259 if (!TLI.getPreIndexedAddressParts(N, BasePtr, Offset, AM, DAG))
3260 return false;
Evan Chenga7d4a042007-05-03 23:52:19 +00003261 // Don't create a indexed load / store with zero offset.
3262 if (isa<ConstantSDNode>(Offset) &&
3263 cast<ConstantSDNode>(Offset)->getValue() == 0)
3264 return false;
Chris Lattner9f1794e2006-11-11 00:56:29 +00003265
Chris Lattner41e53fd2006-11-11 01:00:15 +00003266 // Try turning it into a pre-indexed load / store except when:
3267 // 1) The base is a frame index.
3268 // 2) If N is a store and the ptr is either the same as or is a
Chris Lattner9f1794e2006-11-11 00:56:29 +00003269 // predecessor of the value being stored.
Chris Lattner41e53fd2006-11-11 01:00:15 +00003270 // 3) Another use of base ptr is a predecessor of N. If ptr is folded
Chris Lattner9f1794e2006-11-11 00:56:29 +00003271 // that would create a cycle.
Chris Lattner41e53fd2006-11-11 01:00:15 +00003272 // 4) All uses are load / store ops that use it as base ptr.
Chris Lattner448f2192006-11-11 00:39:41 +00003273
Chris Lattner41e53fd2006-11-11 01:00:15 +00003274 // Check #1. Preinc'ing a frame index would require copying the stack pointer
3275 // (plus the implicit offset) to a register to preinc anyway.
3276 if (isa<FrameIndexSDNode>(BasePtr))
3277 return false;
3278
3279 // Check #2.
Chris Lattner9f1794e2006-11-11 00:56:29 +00003280 if (!isLoad) {
3281 SDOperand Val = cast<StoreSDNode>(N)->getValue();
3282 if (Val == Ptr || Ptr.Val->isPredecessor(Val.Val))
3283 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00003284 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00003285
3286 // Now check for #2 and #3.
3287 bool RealUse = false;
3288 for (SDNode::use_iterator I = Ptr.Val->use_begin(),
3289 E = Ptr.Val->use_end(); I != E; ++I) {
3290 SDNode *Use = *I;
3291 if (Use == N)
3292 continue;
3293 if (Use->isPredecessor(N))
3294 return false;
3295
3296 if (!((Use->getOpcode() == ISD::LOAD &&
3297 cast<LoadSDNode>(Use)->getBasePtr() == Ptr) ||
3298 (Use->getOpcode() == ISD::STORE) &&
3299 cast<StoreSDNode>(Use)->getBasePtr() == Ptr))
3300 RealUse = true;
3301 }
3302 if (!RealUse)
3303 return false;
3304
3305 SDOperand Result;
3306 if (isLoad)
3307 Result = DAG.getIndexedLoad(SDOperand(N,0), BasePtr, Offset, AM);
3308 else
3309 Result = DAG.getIndexedStore(SDOperand(N,0), BasePtr, Offset, AM);
3310 ++PreIndexedNodes;
3311 ++NodesCombined;
Bill Wendling832171c2006-12-07 20:04:42 +00003312 DOUT << "\nReplacing.4 "; DEBUG(N->dump());
3313 DOUT << "\nWith: "; DEBUG(Result.Val->dump(&DAG));
3314 DOUT << '\n';
Chris Lattner9f1794e2006-11-11 00:56:29 +00003315 std::vector<SDNode*> NowDead;
3316 if (isLoad) {
3317 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(0),
3318 NowDead);
3319 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1), Result.getValue(2),
3320 NowDead);
3321 } else {
3322 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(1),
3323 NowDead);
3324 }
3325
3326 // Nodes can end up on the worklist more than once. Make sure we do
3327 // not process a node that has been replaced.
3328 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
3329 removeFromWorkList(NowDead[i]);
3330 // Finally, since the node is now dead, remove it from the graph.
3331 DAG.DeleteNode(N);
3332
3333 // Replace the uses of Ptr with uses of the updated base value.
3334 DAG.ReplaceAllUsesOfValueWith(Ptr, Result.getValue(isLoad ? 1 : 0),
3335 NowDead);
3336 removeFromWorkList(Ptr.Val);
3337 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
3338 removeFromWorkList(NowDead[i]);
3339 DAG.DeleteNode(Ptr.Val);
3340
3341 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00003342}
3343
3344/// CombineToPostIndexedLoadStore - Try combine a load / store with a
3345/// add / sub of the base pointer node into a post-indexed load / store.
3346/// The transformation folded the add / subtract into the new indexed
3347/// load / store effectively and all of its uses are redirected to the
3348/// new load / store.
3349bool DAGCombiner::CombineToPostIndexedLoadStore(SDNode *N) {
3350 if (!AfterLegalize)
3351 return false;
3352
3353 bool isLoad = true;
3354 SDOperand Ptr;
3355 MVT::ValueType VT;
3356 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Evan Chenge90460e2006-12-16 06:25:23 +00003357 if (LD->getAddressingMode() != ISD::UNINDEXED)
3358 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00003359 VT = LD->getLoadedVT();
3360 if (!TLI.isIndexedLoadLegal(ISD::POST_INC, VT) &&
3361 !TLI.isIndexedLoadLegal(ISD::POST_DEC, VT))
3362 return false;
3363 Ptr = LD->getBasePtr();
3364 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Evan Chenge90460e2006-12-16 06:25:23 +00003365 if (ST->getAddressingMode() != ISD::UNINDEXED)
3366 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00003367 VT = ST->getStoredVT();
3368 if (!TLI.isIndexedStoreLegal(ISD::POST_INC, VT) &&
3369 !TLI.isIndexedStoreLegal(ISD::POST_DEC, VT))
3370 return false;
3371 Ptr = ST->getBasePtr();
3372 isLoad = false;
3373 } else
3374 return false;
3375
Evan Chengcc470212006-11-16 00:08:20 +00003376 if (Ptr.Val->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00003377 return false;
3378
3379 for (SDNode::use_iterator I = Ptr.Val->use_begin(),
3380 E = Ptr.Val->use_end(); I != E; ++I) {
3381 SDNode *Op = *I;
3382 if (Op == N ||
3383 (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB))
3384 continue;
3385
3386 SDOperand BasePtr;
3387 SDOperand Offset;
3388 ISD::MemIndexedMode AM = ISD::UNINDEXED;
3389 if (TLI.getPostIndexedAddressParts(N, Op, BasePtr, Offset, AM, DAG)) {
3390 if (Ptr == Offset)
3391 std::swap(BasePtr, Offset);
3392 if (Ptr != BasePtr)
Chris Lattner448f2192006-11-11 00:39:41 +00003393 continue;
Evan Chenga7d4a042007-05-03 23:52:19 +00003394 // Don't create a indexed load / store with zero offset.
3395 if (isa<ConstantSDNode>(Offset) &&
3396 cast<ConstantSDNode>(Offset)->getValue() == 0)
3397 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00003398
Chris Lattner9f1794e2006-11-11 00:56:29 +00003399 // Try turning it into a post-indexed load / store except when
3400 // 1) All uses are load / store ops that use it as base ptr.
3401 // 2) Op must be independent of N, i.e. Op is neither a predecessor
3402 // nor a successor of N. Otherwise, if Op is folded that would
3403 // create a cycle.
3404
3405 // Check for #1.
3406 bool TryNext = false;
3407 for (SDNode::use_iterator II = BasePtr.Val->use_begin(),
3408 EE = BasePtr.Val->use_end(); II != EE; ++II) {
3409 SDNode *Use = *II;
3410 if (Use == Ptr.Val)
Chris Lattner448f2192006-11-11 00:39:41 +00003411 continue;
3412
Chris Lattner9f1794e2006-11-11 00:56:29 +00003413 // If all the uses are load / store addresses, then don't do the
3414 // transformation.
3415 if (Use->getOpcode() == ISD::ADD || Use->getOpcode() == ISD::SUB){
3416 bool RealUse = false;
3417 for (SDNode::use_iterator III = Use->use_begin(),
3418 EEE = Use->use_end(); III != EEE; ++III) {
3419 SDNode *UseUse = *III;
3420 if (!((UseUse->getOpcode() == ISD::LOAD &&
3421 cast<LoadSDNode>(UseUse)->getBasePtr().Val == Use) ||
3422 (UseUse->getOpcode() == ISD::STORE) &&
3423 cast<StoreSDNode>(UseUse)->getBasePtr().Val == Use))
3424 RealUse = true;
3425 }
Chris Lattner448f2192006-11-11 00:39:41 +00003426
Chris Lattner9f1794e2006-11-11 00:56:29 +00003427 if (!RealUse) {
3428 TryNext = true;
3429 break;
Chris Lattner448f2192006-11-11 00:39:41 +00003430 }
3431 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00003432 }
3433 if (TryNext)
3434 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00003435
Chris Lattner9f1794e2006-11-11 00:56:29 +00003436 // Check for #2
3437 if (!Op->isPredecessor(N) && !N->isPredecessor(Op)) {
3438 SDOperand Result = isLoad
3439 ? DAG.getIndexedLoad(SDOperand(N,0), BasePtr, Offset, AM)
3440 : DAG.getIndexedStore(SDOperand(N,0), BasePtr, Offset, AM);
3441 ++PostIndexedNodes;
3442 ++NodesCombined;
Bill Wendling832171c2006-12-07 20:04:42 +00003443 DOUT << "\nReplacing.5 "; DEBUG(N->dump());
3444 DOUT << "\nWith: "; DEBUG(Result.Val->dump(&DAG));
3445 DOUT << '\n';
Chris Lattner9f1794e2006-11-11 00:56:29 +00003446 std::vector<SDNode*> NowDead;
3447 if (isLoad) {
3448 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(0),
Chris Lattner448f2192006-11-11 00:39:41 +00003449 NowDead);
Chris Lattner9f1794e2006-11-11 00:56:29 +00003450 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1), Result.getValue(2),
3451 NowDead);
3452 } else {
3453 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(1),
3454 NowDead);
Chris Lattner448f2192006-11-11 00:39:41 +00003455 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00003456
3457 // Nodes can end up on the worklist more than once. Make sure we do
3458 // not process a node that has been replaced.
3459 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
3460 removeFromWorkList(NowDead[i]);
3461 // Finally, since the node is now dead, remove it from the graph.
3462 DAG.DeleteNode(N);
3463
3464 // Replace the uses of Use with uses of the updated base value.
3465 DAG.ReplaceAllUsesOfValueWith(SDOperand(Op, 0),
3466 Result.getValue(isLoad ? 1 : 0),
3467 NowDead);
3468 removeFromWorkList(Op);
3469 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
3470 removeFromWorkList(NowDead[i]);
3471 DAG.DeleteNode(Op);
3472
3473 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00003474 }
3475 }
3476 }
3477 return false;
3478}
3479
3480
Chris Lattner01a22022005-10-10 22:04:48 +00003481SDOperand DAGCombiner::visitLOAD(SDNode *N) {
Evan Cheng466685d2006-10-09 20:57:25 +00003482 LoadSDNode *LD = cast<LoadSDNode>(N);
3483 SDOperand Chain = LD->getChain();
3484 SDOperand Ptr = LD->getBasePtr();
Evan Cheng45a7ca92007-05-01 00:38:21 +00003485
3486 // If load is not volatile and there are no uses of the loaded value (and
3487 // the updated indexed value in case of indexed loads), change uses of the
3488 // chain value into uses of the chain input (i.e. delete the dead load).
3489 if (!LD->isVolatile()) {
Evan Cheng498f5592007-05-01 08:53:39 +00003490 if (N->getValueType(1) == MVT::Other) {
3491 // Unindexed loads.
3492 if (N->hasNUsesOfValue(0, 0))
3493 return CombineTo(N, DAG.getNode(ISD::UNDEF, N->getValueType(0)), Chain);
3494 } else {
3495 // Indexed loads.
3496 assert(N->getValueType(2) == MVT::Other && "Malformed indexed loads?");
3497 if (N->hasNUsesOfValue(0, 0) && N->hasNUsesOfValue(0, 1)) {
3498 SDOperand Undef0 = DAG.getNode(ISD::UNDEF, N->getValueType(0));
3499 SDOperand Undef1 = DAG.getNode(ISD::UNDEF, N->getValueType(1));
3500 SDOperand To[] = { Undef0, Undef1, Chain };
3501 return CombineTo(N, To, 3);
Evan Cheng45a7ca92007-05-01 00:38:21 +00003502 }
Evan Cheng45a7ca92007-05-01 00:38:21 +00003503 }
3504 }
Chris Lattner01a22022005-10-10 22:04:48 +00003505
3506 // If this load is directly stored, replace the load value with the stored
3507 // value.
3508 // TODO: Handle store large -> read small portion.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00003509 // TODO: Handle TRUNCSTORE/LOADEXT
3510 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00003511 if (ISD::isNON_TRUNCStore(Chain.Val)) {
3512 StoreSDNode *PrevST = cast<StoreSDNode>(Chain);
3513 if (PrevST->getBasePtr() == Ptr &&
3514 PrevST->getValue().getValueType() == N->getValueType(0))
Jim Laskeyc2b19f32006-10-11 17:47:52 +00003515 return CombineTo(N, Chain.getOperand(1), Chain);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003516 }
Jim Laskeyc2b19f32006-10-11 17:47:52 +00003517 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00003518
Jim Laskey7ca56af2006-10-11 13:47:09 +00003519 if (CombinerAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00003520 // Walk up chain skipping non-aliasing memory nodes.
3521 SDOperand BetterChain = FindBetterChain(N, Chain);
3522
Jim Laskey6ff23e52006-10-04 16:53:27 +00003523 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00003524 if (Chain != BetterChain) {
Jim Laskeyc2b19f32006-10-11 17:47:52 +00003525 SDOperand ReplLoad;
3526
Jim Laskey279f0532006-09-25 16:29:54 +00003527 // Replace the chain to void dependency.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00003528 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
3529 ReplLoad = DAG.getLoad(N->getValueType(0), BetterChain, Ptr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00003530 LD->getSrcValue(), LD->getSrcValueOffset(),
3531 LD->isVolatile(), LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00003532 } else {
3533 ReplLoad = DAG.getExtLoad(LD->getExtensionType(),
3534 LD->getValueType(0),
3535 BetterChain, Ptr, LD->getSrcValue(),
3536 LD->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003537 LD->getLoadedVT(),
3538 LD->isVolatile(),
3539 LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00003540 }
Jim Laskey279f0532006-09-25 16:29:54 +00003541
Jim Laskey6ff23e52006-10-04 16:53:27 +00003542 // Create token factor to keep old chain connected.
Jim Laskey288af5e2006-09-25 19:32:58 +00003543 SDOperand Token = DAG.getNode(ISD::TokenFactor, MVT::Other,
3544 Chain, ReplLoad.getValue(1));
Jim Laskey6ff23e52006-10-04 16:53:27 +00003545
Jim Laskey274062c2006-10-13 23:32:28 +00003546 // Replace uses with load result and token factor. Don't add users
3547 // to work list.
3548 return CombineTo(N, ReplLoad.getValue(0), Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00003549 }
3550 }
3551
Evan Cheng7fc033a2006-11-03 03:06:21 +00003552 // Try transforming N to an indexed load.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00003553 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Evan Cheng7fc033a2006-11-03 03:06:21 +00003554 return SDOperand(N, 0);
3555
Chris Lattner01a22022005-10-10 22:04:48 +00003556 return SDOperand();
3557}
3558
Chris Lattner87514ca2005-10-10 22:31:19 +00003559SDOperand DAGCombiner::visitSTORE(SDNode *N) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00003560 StoreSDNode *ST = cast<StoreSDNode>(N);
3561 SDOperand Chain = ST->getChain();
3562 SDOperand Value = ST->getValue();
3563 SDOperand Ptr = ST->getBasePtr();
Jim Laskey7aed46c2006-10-11 18:55:16 +00003564
Evan Cheng59d5b682007-05-07 21:27:48 +00003565 // If this is a store of a bit convert, store the input value if the
Evan Cheng2c4f9432007-05-09 21:49:47 +00003566 // resultant store does not need a higher alignment than the original.
Dale Johannesen98a6c622007-05-16 22:45:30 +00003567 if (Value.getOpcode() == ISD::BIT_CONVERT && !ST->isTruncatingStore() &&
3568 ST->getAddressingMode() == ISD::UNINDEXED) {
Evan Cheng59d5b682007-05-07 21:27:48 +00003569 unsigned Align = ST->getAlignment();
3570 MVT::ValueType SVT = Value.getOperand(0).getValueType();
3571 unsigned OrigAlign = TLI.getTargetMachine().getTargetData()->
Evan Cheng93003b82007-05-16 02:04:50 +00003572 getABITypeAlignment(getTypeForValueType(SVT));
Evan Chengc2cd2b22007-05-07 21:36:06 +00003573 if (Align <= OrigAlign && TLI.isOperationLegal(ISD::STORE, SVT))
Evan Cheng59d5b682007-05-07 21:27:48 +00003574 return DAG.getStore(Chain, Value.getOperand(0), Ptr, ST->getSrcValue(),
3575 ST->getSrcValueOffset());
Jim Laskey279f0532006-09-25 16:29:54 +00003576 }
3577
Nate Begeman2cbba892006-12-11 02:23:46 +00003578 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Nate Begeman2cbba892006-12-11 02:23:46 +00003579 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Value)) {
Evan Cheng25ece662006-12-11 17:25:19 +00003580 if (Value.getOpcode() != ISD::TargetConstantFP) {
3581 SDOperand Tmp;
Chris Lattner62be1a72006-12-12 04:16:14 +00003582 switch (CFP->getValueType(0)) {
3583 default: assert(0 && "Unknown FP type");
3584 case MVT::f32:
3585 if (!AfterLegalize || TLI.isTypeLegal(MVT::i32)) {
3586 Tmp = DAG.getConstant(FloatToBits(CFP->getValue()), MVT::i32);
3587 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
3588 ST->getSrcValueOffset());
3589 }
3590 break;
3591 case MVT::f64:
3592 if (!AfterLegalize || TLI.isTypeLegal(MVT::i64)) {
3593 Tmp = DAG.getConstant(DoubleToBits(CFP->getValue()), MVT::i64);
3594 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
3595 ST->getSrcValueOffset());
3596 } else if (TLI.isTypeLegal(MVT::i32)) {
3597 // Many FP stores are not make apparent until after legalize, e.g. for
3598 // argument passing. Since this is so common, custom legalize the
3599 // 64-bit integer store into two 32-bit stores.
3600 uint64_t Val = DoubleToBits(CFP->getValue());
3601 SDOperand Lo = DAG.getConstant(Val & 0xFFFFFFFF, MVT::i32);
3602 SDOperand Hi = DAG.getConstant(Val >> 32, MVT::i32);
3603 if (!TLI.isLittleEndian()) std::swap(Lo, Hi);
3604
3605 SDOperand St0 = DAG.getStore(Chain, Lo, Ptr, ST->getSrcValue(),
3606 ST->getSrcValueOffset());
3607 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3608 DAG.getConstant(4, Ptr.getValueType()));
3609 SDOperand St1 = DAG.getStore(Chain, Hi, Ptr, ST->getSrcValue(),
3610 ST->getSrcValueOffset()+4);
3611 return DAG.getNode(ISD::TokenFactor, MVT::Other, St0, St1);
3612 }
3613 break;
Evan Cheng25ece662006-12-11 17:25:19 +00003614 }
Nate Begeman2cbba892006-12-11 02:23:46 +00003615 }
Nate Begeman2cbba892006-12-11 02:23:46 +00003616 }
3617
Jim Laskey279f0532006-09-25 16:29:54 +00003618 if (CombinerAA) {
3619 // Walk up chain skipping non-aliasing memory nodes.
3620 SDOperand BetterChain = FindBetterChain(N, Chain);
3621
Jim Laskey6ff23e52006-10-04 16:53:27 +00003622 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00003623 if (Chain != BetterChain) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00003624 // Replace the chain to avoid dependency.
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00003625 SDOperand ReplStore;
3626 if (ST->isTruncatingStore()) {
3627 ReplStore = DAG.getTruncStore(BetterChain, Value, Ptr,
3628 ST->getSrcValue(),ST->getSrcValueOffset(), ST->getStoredVT());
3629 } else {
3630 ReplStore = DAG.getStore(BetterChain, Value, Ptr,
3631 ST->getSrcValue(), ST->getSrcValueOffset());
3632 }
3633
Jim Laskey279f0532006-09-25 16:29:54 +00003634 // Create token to keep both nodes around.
Jim Laskey274062c2006-10-13 23:32:28 +00003635 SDOperand Token =
3636 DAG.getNode(ISD::TokenFactor, MVT::Other, Chain, ReplStore);
3637
3638 // Don't add users to work list.
3639 return CombineTo(N, Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00003640 }
Jim Laskeyd1aed7a2006-09-21 16:28:59 +00003641 }
Chris Lattnerc33baaa2005-12-23 05:48:07 +00003642
Evan Cheng33dbedc2006-11-05 09:31:14 +00003643 // Try transforming N to an indexed store.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00003644 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Evan Cheng33dbedc2006-11-05 09:31:14 +00003645 return SDOperand(N, 0);
3646
Chris Lattner87514ca2005-10-10 22:31:19 +00003647 return SDOperand();
3648}
3649
Chris Lattnerca242442006-03-19 01:27:56 +00003650SDOperand DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
3651 SDOperand InVec = N->getOperand(0);
3652 SDOperand InVal = N->getOperand(1);
3653 SDOperand EltNo = N->getOperand(2);
3654
3655 // If the invec is a BUILD_VECTOR and if EltNo is a constant, build a new
3656 // vector with the inserted element.
3657 if (InVec.getOpcode() == ISD::BUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
3658 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003659 SmallVector<SDOperand, 8> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
Chris Lattnerca242442006-03-19 01:27:56 +00003660 if (Elt < Ops.size())
3661 Ops[Elt] = InVal;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003662 return DAG.getNode(ISD::BUILD_VECTOR, InVec.getValueType(),
3663 &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00003664 }
3665
3666 return SDOperand();
3667}
3668
3669SDOperand DAGCombiner::visitVINSERT_VECTOR_ELT(SDNode *N) {
3670 SDOperand InVec = N->getOperand(0);
3671 SDOperand InVal = N->getOperand(1);
3672 SDOperand EltNo = N->getOperand(2);
3673 SDOperand NumElts = N->getOperand(3);
3674 SDOperand EltType = N->getOperand(4);
3675
3676 // If the invec is a VBUILD_VECTOR and if EltNo is a constant, build a new
3677 // vector with the inserted element.
3678 if (InVec.getOpcode() == ISD::VBUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
3679 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003680 SmallVector<SDOperand, 8> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
Chris Lattnerca242442006-03-19 01:27:56 +00003681 if (Elt < Ops.size()-2)
3682 Ops[Elt] = InVal;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003683 return DAG.getNode(ISD::VBUILD_VECTOR, InVec.getValueType(),
3684 &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00003685 }
3686
3687 return SDOperand();
3688}
3689
Chris Lattnerd7648c82006-03-28 20:28:38 +00003690SDOperand DAGCombiner::visitVBUILD_VECTOR(SDNode *N) {
3691 unsigned NumInScalars = N->getNumOperands()-2;
3692 SDOperand NumElts = N->getOperand(NumInScalars);
3693 SDOperand EltType = N->getOperand(NumInScalars+1);
3694
3695 // Check to see if this is a VBUILD_VECTOR of a bunch of VEXTRACT_VECTOR_ELT
3696 // operations. If so, and if the EXTRACT_ELT vector inputs come from at most
3697 // two distinct vectors, turn this into a shuffle node.
3698 SDOperand VecIn1, VecIn2;
3699 for (unsigned i = 0; i != NumInScalars; ++i) {
3700 // Ignore undef inputs.
3701 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
3702
3703 // If this input is something other than a VEXTRACT_VECTOR_ELT with a
3704 // constant index, bail out.
3705 if (N->getOperand(i).getOpcode() != ISD::VEXTRACT_VECTOR_ELT ||
3706 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
3707 VecIn1 = VecIn2 = SDOperand(0, 0);
3708 break;
3709 }
3710
3711 // If the input vector type disagrees with the result of the vbuild_vector,
3712 // we can't make a shuffle.
3713 SDOperand ExtractedFromVec = N->getOperand(i).getOperand(0);
3714 if (*(ExtractedFromVec.Val->op_end()-2) != NumElts ||
3715 *(ExtractedFromVec.Val->op_end()-1) != EltType) {
3716 VecIn1 = VecIn2 = SDOperand(0, 0);
3717 break;
3718 }
3719
3720 // Otherwise, remember this. We allow up to two distinct input vectors.
3721 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
3722 continue;
3723
3724 if (VecIn1.Val == 0) {
3725 VecIn1 = ExtractedFromVec;
3726 } else if (VecIn2.Val == 0) {
3727 VecIn2 = ExtractedFromVec;
3728 } else {
3729 // Too many inputs.
3730 VecIn1 = VecIn2 = SDOperand(0, 0);
3731 break;
3732 }
3733 }
3734
3735 // If everything is good, we can make a shuffle operation.
3736 if (VecIn1.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003737 SmallVector<SDOperand, 8> BuildVecIndices;
Chris Lattnerd7648c82006-03-28 20:28:38 +00003738 for (unsigned i = 0; i != NumInScalars; ++i) {
3739 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
Evan Cheng597a3bd2007-01-20 10:10:26 +00003740 BuildVecIndices.push_back(DAG.getNode(ISD::UNDEF, TLI.getPointerTy()));
Chris Lattnerd7648c82006-03-28 20:28:38 +00003741 continue;
3742 }
3743
3744 SDOperand Extract = N->getOperand(i);
3745
3746 // If extracting from the first vector, just use the index directly.
3747 if (Extract.getOperand(0) == VecIn1) {
3748 BuildVecIndices.push_back(Extract.getOperand(1));
3749 continue;
3750 }
3751
3752 // Otherwise, use InIdx + VecSize
3753 unsigned Idx = cast<ConstantSDNode>(Extract.getOperand(1))->getValue();
Evan Cheng597a3bd2007-01-20 10:10:26 +00003754 BuildVecIndices.push_back(DAG.getConstant(Idx+NumInScalars,
3755 TLI.getPointerTy()));
Chris Lattnerd7648c82006-03-28 20:28:38 +00003756 }
3757
3758 // Add count and size info.
3759 BuildVecIndices.push_back(NumElts);
Evan Cheng597a3bd2007-01-20 10:10:26 +00003760 BuildVecIndices.push_back(DAG.getValueType(TLI.getPointerTy()));
Chris Lattnerd7648c82006-03-28 20:28:38 +00003761
3762 // Return the new VVECTOR_SHUFFLE node.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003763 SDOperand Ops[5];
3764 Ops[0] = VecIn1;
Chris Lattnercef896e2006-03-28 22:19:47 +00003765 if (VecIn2.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003766 Ops[1] = VecIn2;
Chris Lattnercef896e2006-03-28 22:19:47 +00003767 } else {
3768 // Use an undef vbuild_vector as input for the second operand.
3769 std::vector<SDOperand> UnOps(NumInScalars,
3770 DAG.getNode(ISD::UNDEF,
3771 cast<VTSDNode>(EltType)->getVT()));
3772 UnOps.push_back(NumElts);
3773 UnOps.push_back(EltType);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003774 Ops[1] = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
3775 &UnOps[0], UnOps.size());
3776 AddToWorkList(Ops[1].Val);
Chris Lattnercef896e2006-03-28 22:19:47 +00003777 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003778 Ops[2] = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
3779 &BuildVecIndices[0], BuildVecIndices.size());
3780 Ops[3] = NumElts;
3781 Ops[4] = EltType;
3782 return DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector, Ops, 5);
Chris Lattnerd7648c82006-03-28 20:28:38 +00003783 }
3784
3785 return SDOperand();
3786}
3787
Chris Lattner66445d32006-03-28 22:11:53 +00003788SDOperand DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
Chris Lattnerf1d0c622006-03-31 22:16:43 +00003789 SDOperand ShufMask = N->getOperand(2);
3790 unsigned NumElts = ShufMask.getNumOperands();
3791
3792 // If the shuffle mask is an identity operation on the LHS, return the LHS.
3793 bool isIdentity = true;
3794 for (unsigned i = 0; i != NumElts; ++i) {
3795 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
3796 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
3797 isIdentity = false;
3798 break;
3799 }
3800 }
3801 if (isIdentity) return N->getOperand(0);
3802
3803 // If the shuffle mask is an identity operation on the RHS, return the RHS.
3804 isIdentity = true;
3805 for (unsigned i = 0; i != NumElts; ++i) {
3806 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
3807 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
3808 isIdentity = false;
3809 break;
3810 }
3811 }
3812 if (isIdentity) return N->getOperand(1);
Evan Chenge7bec0d2006-07-20 22:44:41 +00003813
3814 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
3815 // needed at all.
3816 bool isUnary = true;
Evan Cheng917ec982006-07-21 08:25:53 +00003817 bool isSplat = true;
Evan Chenge7bec0d2006-07-20 22:44:41 +00003818 int VecNum = -1;
Reid Spencer9160a6a2006-07-25 20:44:41 +00003819 unsigned BaseIdx = 0;
Evan Chenge7bec0d2006-07-20 22:44:41 +00003820 for (unsigned i = 0; i != NumElts; ++i)
3821 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
3822 unsigned Idx = cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue();
3823 int V = (Idx < NumElts) ? 0 : 1;
Evan Cheng917ec982006-07-21 08:25:53 +00003824 if (VecNum == -1) {
Evan Chenge7bec0d2006-07-20 22:44:41 +00003825 VecNum = V;
Evan Cheng917ec982006-07-21 08:25:53 +00003826 BaseIdx = Idx;
3827 } else {
3828 if (BaseIdx != Idx)
3829 isSplat = false;
3830 if (VecNum != V) {
3831 isUnary = false;
3832 break;
3833 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00003834 }
3835 }
3836
3837 SDOperand N0 = N->getOperand(0);
3838 SDOperand N1 = N->getOperand(1);
3839 // Normalize unary shuffle so the RHS is undef.
3840 if (isUnary && VecNum == 1)
3841 std::swap(N0, N1);
3842
Evan Cheng917ec982006-07-21 08:25:53 +00003843 // If it is a splat, check if the argument vector is a build_vector with
3844 // all scalar elements the same.
3845 if (isSplat) {
3846 SDNode *V = N0.Val;
3847 if (V->getOpcode() == ISD::BIT_CONVERT)
3848 V = V->getOperand(0).Val;
3849 if (V->getOpcode() == ISD::BUILD_VECTOR) {
3850 unsigned NumElems = V->getNumOperands()-2;
3851 if (NumElems > BaseIdx) {
3852 SDOperand Base;
3853 bool AllSame = true;
3854 for (unsigned i = 0; i != NumElems; ++i) {
3855 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
3856 Base = V->getOperand(i);
3857 break;
3858 }
3859 }
3860 // Splat of <u, u, u, u>, return <u, u, u, u>
3861 if (!Base.Val)
3862 return N0;
3863 for (unsigned i = 0; i != NumElems; ++i) {
3864 if (V->getOperand(i).getOpcode() != ISD::UNDEF &&
3865 V->getOperand(i) != Base) {
3866 AllSame = false;
3867 break;
3868 }
3869 }
3870 // Splat of <x, x, x, x>, return <x, x, x, x>
3871 if (AllSame)
3872 return N0;
3873 }
3874 }
3875 }
3876
Evan Chenge7bec0d2006-07-20 22:44:41 +00003877 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
3878 // into an undef.
3879 if (isUnary || N0 == N1) {
3880 if (N0.getOpcode() == ISD::UNDEF)
Evan Chengc04766a2006-04-06 23:20:43 +00003881 return DAG.getNode(ISD::UNDEF, N->getValueType(0));
Chris Lattner66445d32006-03-28 22:11:53 +00003882 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
3883 // first operand.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003884 SmallVector<SDOperand, 8> MappedOps;
Chris Lattner66445d32006-03-28 22:11:53 +00003885 for (unsigned i = 0, e = ShufMask.getNumOperands(); i != e; ++i) {
Evan Chengc04766a2006-04-06 23:20:43 +00003886 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
3887 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) {
3888 MappedOps.push_back(ShufMask.getOperand(i));
3889 } else {
Chris Lattner66445d32006-03-28 22:11:53 +00003890 unsigned NewIdx =
3891 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
3892 MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32));
Chris Lattner66445d32006-03-28 22:11:53 +00003893 }
3894 }
3895 ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMask.getValueType(),
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003896 &MappedOps[0], MappedOps.size());
Chris Lattner3e104b12006-04-08 04:15:24 +00003897 AddToWorkList(ShufMask.Val);
Chris Lattner66445d32006-03-28 22:11:53 +00003898 return DAG.getNode(ISD::VECTOR_SHUFFLE, N->getValueType(0),
Evan Chenge7bec0d2006-07-20 22:44:41 +00003899 N0,
Chris Lattner66445d32006-03-28 22:11:53 +00003900 DAG.getNode(ISD::UNDEF, N->getValueType(0)),
3901 ShufMask);
3902 }
3903
3904 return SDOperand();
3905}
3906
Chris Lattnerf1d0c622006-03-31 22:16:43 +00003907SDOperand DAGCombiner::visitVVECTOR_SHUFFLE(SDNode *N) {
3908 SDOperand ShufMask = N->getOperand(2);
3909 unsigned NumElts = ShufMask.getNumOperands()-2;
3910
3911 // If the shuffle mask is an identity operation on the LHS, return the LHS.
3912 bool isIdentity = true;
3913 for (unsigned i = 0; i != NumElts; ++i) {
3914 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
3915 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
3916 isIdentity = false;
3917 break;
3918 }
3919 }
3920 if (isIdentity) return N->getOperand(0);
3921
3922 // If the shuffle mask is an identity operation on the RHS, return the RHS.
3923 isIdentity = true;
3924 for (unsigned i = 0; i != NumElts; ++i) {
3925 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
3926 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
3927 isIdentity = false;
3928 break;
3929 }
3930 }
3931 if (isIdentity) return N->getOperand(1);
3932
Evan Chenge7bec0d2006-07-20 22:44:41 +00003933 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
3934 // needed at all.
3935 bool isUnary = true;
Evan Cheng917ec982006-07-21 08:25:53 +00003936 bool isSplat = true;
Evan Chenge7bec0d2006-07-20 22:44:41 +00003937 int VecNum = -1;
Reid Spencer9160a6a2006-07-25 20:44:41 +00003938 unsigned BaseIdx = 0;
Evan Chenge7bec0d2006-07-20 22:44:41 +00003939 for (unsigned i = 0; i != NumElts; ++i)
3940 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
3941 unsigned Idx = cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue();
3942 int V = (Idx < NumElts) ? 0 : 1;
Evan Cheng917ec982006-07-21 08:25:53 +00003943 if (VecNum == -1) {
Evan Chenge7bec0d2006-07-20 22:44:41 +00003944 VecNum = V;
Evan Cheng917ec982006-07-21 08:25:53 +00003945 BaseIdx = Idx;
3946 } else {
3947 if (BaseIdx != Idx)
3948 isSplat = false;
3949 if (VecNum != V) {
3950 isUnary = false;
3951 break;
3952 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00003953 }
3954 }
3955
3956 SDOperand N0 = N->getOperand(0);
3957 SDOperand N1 = N->getOperand(1);
3958 // Normalize unary shuffle so the RHS is undef.
3959 if (isUnary && VecNum == 1)
3960 std::swap(N0, N1);
3961
Evan Cheng917ec982006-07-21 08:25:53 +00003962 // If it is a splat, check if the argument vector is a build_vector with
3963 // all scalar elements the same.
3964 if (isSplat) {
3965 SDNode *V = N0.Val;
Evan Cheng59569222006-10-16 22:49:37 +00003966
3967 // If this is a vbit convert that changes the element type of the vector but
3968 // not the number of vector elements, look through it. Be careful not to
3969 // look though conversions that change things like v4f32 to v2f64.
3970 if (V->getOpcode() == ISD::VBIT_CONVERT) {
3971 SDOperand ConvInput = V->getOperand(0);
Evan Cheng5d04a1a2006-10-17 17:06:35 +00003972 if (ConvInput.getValueType() == MVT::Vector &&
3973 NumElts ==
Evan Cheng59569222006-10-16 22:49:37 +00003974 ConvInput.getConstantOperandVal(ConvInput.getNumOperands()-2))
3975 V = ConvInput.Val;
3976 }
3977
Evan Cheng917ec982006-07-21 08:25:53 +00003978 if (V->getOpcode() == ISD::VBUILD_VECTOR) {
3979 unsigned NumElems = V->getNumOperands()-2;
3980 if (NumElems > BaseIdx) {
3981 SDOperand Base;
3982 bool AllSame = true;
3983 for (unsigned i = 0; i != NumElems; ++i) {
3984 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
3985 Base = V->getOperand(i);
3986 break;
3987 }
3988 }
3989 // Splat of <u, u, u, u>, return <u, u, u, u>
3990 if (!Base.Val)
3991 return N0;
3992 for (unsigned i = 0; i != NumElems; ++i) {
3993 if (V->getOperand(i).getOpcode() != ISD::UNDEF &&
3994 V->getOperand(i) != Base) {
3995 AllSame = false;
3996 break;
3997 }
3998 }
3999 // Splat of <x, x, x, x>, return <x, x, x, x>
4000 if (AllSame)
4001 return N0;
4002 }
4003 }
4004 }
4005
Evan Chenge7bec0d2006-07-20 22:44:41 +00004006 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
4007 // into an undef.
4008 if (isUnary || N0 == N1) {
Chris Lattner17614ea2006-04-08 05:34:25 +00004009 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
4010 // first operand.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004011 SmallVector<SDOperand, 8> MappedOps;
Chris Lattner17614ea2006-04-08 05:34:25 +00004012 for (unsigned i = 0; i != NumElts; ++i) {
4013 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
4014 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) {
4015 MappedOps.push_back(ShufMask.getOperand(i));
4016 } else {
4017 unsigned NewIdx =
4018 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
4019 MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32));
4020 }
4021 }
4022 // Add the type/#elts values.
4023 MappedOps.push_back(ShufMask.getOperand(NumElts));
4024 MappedOps.push_back(ShufMask.getOperand(NumElts+1));
4025
4026 ShufMask = DAG.getNode(ISD::VBUILD_VECTOR, ShufMask.getValueType(),
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004027 &MappedOps[0], MappedOps.size());
Chris Lattner17614ea2006-04-08 05:34:25 +00004028 AddToWorkList(ShufMask.Val);
4029
4030 // Build the undef vector.
4031 SDOperand UDVal = DAG.getNode(ISD::UNDEF, MappedOps[0].getValueType());
4032 for (unsigned i = 0; i != NumElts; ++i)
4033 MappedOps[i] = UDVal;
Evan Chenge7bec0d2006-07-20 22:44:41 +00004034 MappedOps[NumElts ] = *(N0.Val->op_end()-2);
4035 MappedOps[NumElts+1] = *(N0.Val->op_end()-1);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004036 UDVal = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
4037 &MappedOps[0], MappedOps.size());
Chris Lattner17614ea2006-04-08 05:34:25 +00004038
4039 return DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector,
Evan Chenge7bec0d2006-07-20 22:44:41 +00004040 N0, UDVal, ShufMask,
Chris Lattner17614ea2006-04-08 05:34:25 +00004041 MappedOps[NumElts], MappedOps[NumElts+1]);
4042 }
4043
Chris Lattnerf1d0c622006-03-31 22:16:43 +00004044 return SDOperand();
4045}
4046
Evan Cheng44f1f092006-04-20 08:56:16 +00004047/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
4048/// a VAND to a vector_shuffle with the destination vector and a zero vector.
4049/// e.g. VAND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
4050/// vector_shuffle V, Zero, <0, 4, 2, 4>
4051SDOperand DAGCombiner::XformToShuffleWithZero(SDNode *N) {
4052 SDOperand LHS = N->getOperand(0);
4053 SDOperand RHS = N->getOperand(1);
4054 if (N->getOpcode() == ISD::VAND) {
4055 SDOperand DstVecSize = *(LHS.Val->op_end()-2);
4056 SDOperand DstVecEVT = *(LHS.Val->op_end()-1);
4057 if (RHS.getOpcode() == ISD::VBIT_CONVERT)
4058 RHS = RHS.getOperand(0);
4059 if (RHS.getOpcode() == ISD::VBUILD_VECTOR) {
4060 std::vector<SDOperand> IdxOps;
4061 unsigned NumOps = RHS.getNumOperands();
4062 unsigned NumElts = NumOps-2;
4063 MVT::ValueType EVT = cast<VTSDNode>(RHS.getOperand(NumOps-1))->getVT();
4064 for (unsigned i = 0; i != NumElts; ++i) {
4065 SDOperand Elt = RHS.getOperand(i);
4066 if (!isa<ConstantSDNode>(Elt))
4067 return SDOperand();
4068 else if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
4069 IdxOps.push_back(DAG.getConstant(i, EVT));
4070 else if (cast<ConstantSDNode>(Elt)->isNullValue())
4071 IdxOps.push_back(DAG.getConstant(NumElts, EVT));
4072 else
4073 return SDOperand();
4074 }
4075
4076 // Let's see if the target supports this vector_shuffle.
4077 if (!TLI.isVectorClearMaskLegal(IdxOps, EVT, DAG))
4078 return SDOperand();
4079
4080 // Return the new VVECTOR_SHUFFLE node.
4081 SDOperand NumEltsNode = DAG.getConstant(NumElts, MVT::i32);
4082 SDOperand EVTNode = DAG.getValueType(EVT);
4083 std::vector<SDOperand> Ops;
Chris Lattner516b9622006-09-14 20:50:57 +00004084 LHS = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, LHS, NumEltsNode,
4085 EVTNode);
Evan Cheng44f1f092006-04-20 08:56:16 +00004086 Ops.push_back(LHS);
4087 AddToWorkList(LHS.Val);
4088 std::vector<SDOperand> ZeroOps(NumElts, DAG.getConstant(0, EVT));
4089 ZeroOps.push_back(NumEltsNode);
4090 ZeroOps.push_back(EVTNode);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004091 Ops.push_back(DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
4092 &ZeroOps[0], ZeroOps.size()));
Evan Cheng44f1f092006-04-20 08:56:16 +00004093 IdxOps.push_back(NumEltsNode);
4094 IdxOps.push_back(EVTNode);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004095 Ops.push_back(DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
4096 &IdxOps[0], IdxOps.size()));
Evan Cheng44f1f092006-04-20 08:56:16 +00004097 Ops.push_back(NumEltsNode);
4098 Ops.push_back(EVTNode);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004099 SDOperand Result = DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector,
4100 &Ops[0], Ops.size());
Evan Cheng44f1f092006-04-20 08:56:16 +00004101 if (NumEltsNode != DstVecSize || EVTNode != DstVecEVT) {
4102 Result = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Result,
4103 DstVecSize, DstVecEVT);
4104 }
4105 return Result;
4106 }
4107 }
4108 return SDOperand();
4109}
4110
Chris Lattneredab1b92006-04-02 03:25:57 +00004111/// visitVBinOp - Visit a binary vector operation, like VADD. IntOp indicates
4112/// the scalar operation of the vop if it is operating on an integer vector
4113/// (e.g. ADD) and FPOp indicates the FP version (e.g. FADD).
4114SDOperand DAGCombiner::visitVBinOp(SDNode *N, ISD::NodeType IntOp,
4115 ISD::NodeType FPOp) {
4116 MVT::ValueType EltType = cast<VTSDNode>(*(N->op_end()-1))->getVT();
4117 ISD::NodeType ScalarOp = MVT::isInteger(EltType) ? IntOp : FPOp;
4118 SDOperand LHS = N->getOperand(0);
4119 SDOperand RHS = N->getOperand(1);
Evan Cheng44f1f092006-04-20 08:56:16 +00004120 SDOperand Shuffle = XformToShuffleWithZero(N);
4121 if (Shuffle.Val) return Shuffle;
4122
Chris Lattneredab1b92006-04-02 03:25:57 +00004123 // If the LHS and RHS are VBUILD_VECTOR nodes, see if we can constant fold
4124 // this operation.
4125 if (LHS.getOpcode() == ISD::VBUILD_VECTOR &&
4126 RHS.getOpcode() == ISD::VBUILD_VECTOR) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004127 SmallVector<SDOperand, 8> Ops;
Chris Lattneredab1b92006-04-02 03:25:57 +00004128 for (unsigned i = 0, e = LHS.getNumOperands()-2; i != e; ++i) {
4129 SDOperand LHSOp = LHS.getOperand(i);
4130 SDOperand RHSOp = RHS.getOperand(i);
4131 // If these two elements can't be folded, bail out.
4132 if ((LHSOp.getOpcode() != ISD::UNDEF &&
4133 LHSOp.getOpcode() != ISD::Constant &&
4134 LHSOp.getOpcode() != ISD::ConstantFP) ||
4135 (RHSOp.getOpcode() != ISD::UNDEF &&
4136 RHSOp.getOpcode() != ISD::Constant &&
4137 RHSOp.getOpcode() != ISD::ConstantFP))
4138 break;
Evan Cheng7b336a82006-05-31 06:08:35 +00004139 // Can't fold divide by zero.
4140 if (N->getOpcode() == ISD::VSDIV || N->getOpcode() == ISD::VUDIV) {
4141 if ((RHSOp.getOpcode() == ISD::Constant &&
4142 cast<ConstantSDNode>(RHSOp.Val)->isNullValue()) ||
4143 (RHSOp.getOpcode() == ISD::ConstantFP &&
4144 !cast<ConstantFPSDNode>(RHSOp.Val)->getValue()))
4145 break;
4146 }
Chris Lattneredab1b92006-04-02 03:25:57 +00004147 Ops.push_back(DAG.getNode(ScalarOp, EltType, LHSOp, RHSOp));
Chris Lattner3e104b12006-04-08 04:15:24 +00004148 AddToWorkList(Ops.back().Val);
Chris Lattneredab1b92006-04-02 03:25:57 +00004149 assert((Ops.back().getOpcode() == ISD::UNDEF ||
4150 Ops.back().getOpcode() == ISD::Constant ||
4151 Ops.back().getOpcode() == ISD::ConstantFP) &&
4152 "Scalar binop didn't fold!");
4153 }
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00004154
4155 if (Ops.size() == LHS.getNumOperands()-2) {
4156 Ops.push_back(*(LHS.Val->op_end()-2));
4157 Ops.push_back(*(LHS.Val->op_end()-1));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004158 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00004159 }
Chris Lattneredab1b92006-04-02 03:25:57 +00004160 }
4161
4162 return SDOperand();
4163}
4164
Nate Begeman44728a72005-09-19 22:34:01 +00004165SDOperand DAGCombiner::SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2){
Nate Begemanf845b452005-10-08 00:29:44 +00004166 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
4167
4168 SDOperand SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
4169 cast<CondCodeSDNode>(N0.getOperand(2))->get());
4170 // If we got a simplified select_cc node back from SimplifySelectCC, then
4171 // break it down into a new SETCC node, and a new SELECT node, and then return
4172 // the SELECT node, since we were called with a SELECT node.
4173 if (SCC.Val) {
4174 // Check to see if we got a select_cc back (to turn into setcc/select).
4175 // Otherwise, just return whatever node we got back, like fabs.
4176 if (SCC.getOpcode() == ISD::SELECT_CC) {
4177 SDOperand SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
4178 SCC.getOperand(0), SCC.getOperand(1),
4179 SCC.getOperand(4));
Chris Lattner5750df92006-03-01 04:03:14 +00004180 AddToWorkList(SETCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004181 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
4182 SCC.getOperand(3), SETCC);
4183 }
4184 return SCC;
4185 }
Nate Begeman44728a72005-09-19 22:34:01 +00004186 return SDOperand();
4187}
4188
Chris Lattner40c62d52005-10-18 06:04:22 +00004189/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
4190/// are the two values being selected between, see if we can simplify the
Chris Lattner729c6d12006-05-27 00:43:02 +00004191/// select. Callers of this should assume that TheSelect is deleted if this
4192/// returns true. As such, they should return the appropriate thing (e.g. the
4193/// node) back to the top-level of the DAG combiner loop to avoid it being
4194/// looked at.
Chris Lattner40c62d52005-10-18 06:04:22 +00004195///
4196bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDOperand LHS,
4197 SDOperand RHS) {
4198
4199 // If this is a select from two identical things, try to pull the operation
4200 // through the select.
4201 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
Chris Lattner40c62d52005-10-18 06:04:22 +00004202 // If this is a load and the token chain is identical, replace the select
4203 // of two loads with a load through a select of the address to load from.
4204 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
4205 // constants have been dropped into the constant pool.
Evan Cheng466685d2006-10-09 20:57:25 +00004206 if (LHS.getOpcode() == ISD::LOAD &&
Chris Lattner40c62d52005-10-18 06:04:22 +00004207 // Token chains must be identical.
Evan Cheng466685d2006-10-09 20:57:25 +00004208 LHS.getOperand(0) == RHS.getOperand(0)) {
4209 LoadSDNode *LLD = cast<LoadSDNode>(LHS);
4210 LoadSDNode *RLD = cast<LoadSDNode>(RHS);
4211
4212 // If this is an EXTLOAD, the VT's must match.
Evan Cheng2e49f092006-10-11 07:10:22 +00004213 if (LLD->getLoadedVT() == RLD->getLoadedVT()) {
Evan Cheng466685d2006-10-09 20:57:25 +00004214 // FIXME: this conflates two src values, discarding one. This is not
4215 // the right thing to do, but nothing uses srcvalues now. When they do,
4216 // turn SrcValue into a list of locations.
4217 SDOperand Addr;
Chris Lattnerc4e664b2007-01-16 05:59:59 +00004218 if (TheSelect->getOpcode() == ISD::SELECT) {
4219 // Check that the condition doesn't reach either load. If so, folding
4220 // this will induce a cycle into the DAG.
4221 if (!LLD->isPredecessor(TheSelect->getOperand(0).Val) &&
4222 !RLD->isPredecessor(TheSelect->getOperand(0).Val)) {
4223 Addr = DAG.getNode(ISD::SELECT, LLD->getBasePtr().getValueType(),
4224 TheSelect->getOperand(0), LLD->getBasePtr(),
4225 RLD->getBasePtr());
4226 }
4227 } else {
4228 // Check that the condition doesn't reach either load. If so, folding
4229 // this will induce a cycle into the DAG.
4230 if (!LLD->isPredecessor(TheSelect->getOperand(0).Val) &&
4231 !RLD->isPredecessor(TheSelect->getOperand(0).Val) &&
4232 !LLD->isPredecessor(TheSelect->getOperand(1).Val) &&
4233 !RLD->isPredecessor(TheSelect->getOperand(1).Val)) {
4234 Addr = DAG.getNode(ISD::SELECT_CC, LLD->getBasePtr().getValueType(),
Evan Cheng466685d2006-10-09 20:57:25 +00004235 TheSelect->getOperand(0),
4236 TheSelect->getOperand(1),
4237 LLD->getBasePtr(), RLD->getBasePtr(),
4238 TheSelect->getOperand(4));
Chris Lattnerc4e664b2007-01-16 05:59:59 +00004239 }
Evan Cheng466685d2006-10-09 20:57:25 +00004240 }
Chris Lattnerc4e664b2007-01-16 05:59:59 +00004241
4242 if (Addr.Val) {
4243 SDOperand Load;
4244 if (LLD->getExtensionType() == ISD::NON_EXTLOAD)
4245 Load = DAG.getLoad(TheSelect->getValueType(0), LLD->getChain(),
4246 Addr,LLD->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00004247 LLD->getSrcValueOffset(),
4248 LLD->isVolatile(),
4249 LLD->getAlignment());
Chris Lattnerc4e664b2007-01-16 05:59:59 +00004250 else {
4251 Load = DAG.getExtLoad(LLD->getExtensionType(),
4252 TheSelect->getValueType(0),
4253 LLD->getChain(), Addr, LLD->getSrcValue(),
4254 LLD->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00004255 LLD->getLoadedVT(),
4256 LLD->isVolatile(),
4257 LLD->getAlignment());
Chris Lattnerc4e664b2007-01-16 05:59:59 +00004258 }
4259 // Users of the select now use the result of the load.
4260 CombineTo(TheSelect, Load);
4261
4262 // Users of the old loads now use the new load's chain. We know the
4263 // old-load value is dead now.
4264 CombineTo(LHS.Val, Load.getValue(0), Load.getValue(1));
4265 CombineTo(RHS.Val, Load.getValue(0), Load.getValue(1));
4266 return true;
4267 }
Evan Chengc5484282006-10-04 00:56:09 +00004268 }
Chris Lattner40c62d52005-10-18 06:04:22 +00004269 }
4270 }
4271
4272 return false;
4273}
4274
Nate Begeman44728a72005-09-19 22:34:01 +00004275SDOperand DAGCombiner::SimplifySelectCC(SDOperand N0, SDOperand N1,
4276 SDOperand N2, SDOperand N3,
Chris Lattner1eba01e2007-04-11 06:50:51 +00004277 ISD::CondCode CC, bool NotExtCompare) {
Nate Begemanf845b452005-10-08 00:29:44 +00004278
4279 MVT::ValueType VT = N2.getValueType();
Nate Begemanf845b452005-10-08 00:29:44 +00004280 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
4281 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
4282 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
4283
4284 // Determine if the condition we're dealing with is constant
4285 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
Chris Lattner30f73e72006-10-14 03:52:46 +00004286 if (SCC.Val) AddToWorkList(SCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004287 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
4288
4289 // fold select_cc true, x, y -> x
4290 if (SCCC && SCCC->getValue())
4291 return N2;
4292 // fold select_cc false, x, y -> y
4293 if (SCCC && SCCC->getValue() == 0)
4294 return N3;
4295
4296 // Check to see if we can simplify the select into an fabs node
4297 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
4298 // Allow either -0.0 or 0.0
4299 if (CFP->getValue() == 0.0) {
4300 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
4301 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
4302 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
4303 N2 == N3.getOperand(0))
4304 return DAG.getNode(ISD::FABS, VT, N0);
4305
4306 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
4307 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
4308 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
4309 N2.getOperand(0) == N3)
4310 return DAG.getNode(ISD::FABS, VT, N3);
4311 }
4312 }
4313
4314 // Check to see if we can perform the "gzip trick", transforming
4315 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
Chris Lattnere3152e52006-09-20 06:41:35 +00004316 if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT &&
Nate Begemanf845b452005-10-08 00:29:44 +00004317 MVT::isInteger(N0.getValueType()) &&
Chris Lattnere3152e52006-09-20 06:41:35 +00004318 MVT::isInteger(N2.getValueType()) &&
4319 (N1C->isNullValue() || // (a < 0) ? b : 0
4320 (N1C->getValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0
Nate Begemanf845b452005-10-08 00:29:44 +00004321 MVT::ValueType XType = N0.getValueType();
4322 MVT::ValueType AType = N2.getValueType();
4323 if (XType >= AType) {
4324 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00004325 // single-bit constant.
Nate Begemanf845b452005-10-08 00:29:44 +00004326 if (N2C && ((N2C->getValue() & (N2C->getValue()-1)) == 0)) {
4327 unsigned ShCtV = Log2_64(N2C->getValue());
4328 ShCtV = MVT::getSizeInBits(XType)-ShCtV-1;
4329 SDOperand ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
4330 SDOperand Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
Chris Lattner5750df92006-03-01 04:03:14 +00004331 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004332 if (XType > AType) {
4333 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00004334 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004335 }
4336 return DAG.getNode(ISD::AND, AType, Shift, N2);
4337 }
4338 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
4339 DAG.getConstant(MVT::getSizeInBits(XType)-1,
4340 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00004341 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004342 if (XType > AType) {
4343 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00004344 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004345 }
4346 return DAG.getNode(ISD::AND, AType, Shift, N2);
4347 }
4348 }
Nate Begeman07ed4172005-10-10 21:26:48 +00004349
4350 // fold select C, 16, 0 -> shl C, 4
4351 if (N2C && N3C && N3C->isNullValue() && isPowerOf2_64(N2C->getValue()) &&
4352 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) {
Chris Lattner1eba01e2007-04-11 06:50:51 +00004353
4354 // If the caller doesn't want us to simplify this into a zext of a compare,
4355 // don't do it.
4356 if (NotExtCompare && N2C->getValue() == 1)
4357 return SDOperand();
4358
Nate Begeman07ed4172005-10-10 21:26:48 +00004359 // Get a SetCC of the condition
4360 // FIXME: Should probably make sure that setcc is legal if we ever have a
4361 // target where it isn't.
Nate Begemanb0d04a72006-02-18 02:40:58 +00004362 SDOperand Temp, SCC;
Nate Begeman07ed4172005-10-10 21:26:48 +00004363 // cast from setcc result type to select result type
Nate Begemanb0d04a72006-02-18 02:40:58 +00004364 if (AfterLegalize) {
4365 SCC = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
Chris Lattner555d8d62006-12-07 22:36:47 +00004366 if (N2.getValueType() < SCC.getValueType())
4367 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
4368 else
4369 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00004370 } else {
4371 SCC = DAG.getSetCC(MVT::i1, N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00004372 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00004373 }
Chris Lattner5750df92006-03-01 04:03:14 +00004374 AddToWorkList(SCC.Val);
4375 AddToWorkList(Temp.Val);
Chris Lattnerc56a81d2007-04-11 06:43:25 +00004376
4377 if (N2C->getValue() == 1)
4378 return Temp;
Nate Begeman07ed4172005-10-10 21:26:48 +00004379 // shl setcc result by log2 n2c
4380 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
4381 DAG.getConstant(Log2_64(N2C->getValue()),
4382 TLI.getShiftAmountTy()));
4383 }
4384
Nate Begemanf845b452005-10-08 00:29:44 +00004385 // Check to see if this is the equivalent of setcc
4386 // FIXME: Turn all of these into setcc if setcc if setcc is legal
4387 // otherwise, go ahead with the folds.
4388 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getValue() == 1ULL)) {
4389 MVT::ValueType XType = N0.getValueType();
4390 if (TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultTy())) {
4391 SDOperand Res = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
4392 if (Res.getValueType() != VT)
4393 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
4394 return Res;
4395 }
4396
4397 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
4398 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
4399 TLI.isOperationLegal(ISD::CTLZ, XType)) {
4400 SDOperand Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
4401 return DAG.getNode(ISD::SRL, XType, Ctlz,
4402 DAG.getConstant(Log2_32(MVT::getSizeInBits(XType)),
4403 TLI.getShiftAmountTy()));
4404 }
4405 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
4406 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
4407 SDOperand NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
4408 N0);
4409 SDOperand NotN0 = DAG.getNode(ISD::XOR, XType, N0,
4410 DAG.getConstant(~0ULL, XType));
4411 return DAG.getNode(ISD::SRL, XType,
4412 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
4413 DAG.getConstant(MVT::getSizeInBits(XType)-1,
4414 TLI.getShiftAmountTy()));
4415 }
4416 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
4417 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
4418 SDOperand Sign = DAG.getNode(ISD::SRL, XType, N0,
4419 DAG.getConstant(MVT::getSizeInBits(XType)-1,
4420 TLI.getShiftAmountTy()));
4421 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
4422 }
4423 }
4424
4425 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
4426 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
4427 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
Chris Lattner1982ef22007-04-11 05:11:38 +00004428 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1) &&
4429 N2.getOperand(0) == N1 && MVT::isInteger(N0.getValueType())) {
4430 MVT::ValueType XType = N0.getValueType();
4431 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
4432 DAG.getConstant(MVT::getSizeInBits(XType)-1,
4433 TLI.getShiftAmountTy()));
4434 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
4435 AddToWorkList(Shift.Val);
4436 AddToWorkList(Add.Val);
4437 return DAG.getNode(ISD::XOR, XType, Add, Shift);
4438 }
4439 // Check to see if this is an integer abs. select_cc setgt X, -1, X, -X ->
4440 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
4441 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT &&
4442 N0 == N2 && N3.getOpcode() == ISD::SUB && N0 == N3.getOperand(1)) {
4443 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N3.getOperand(0))) {
Nate Begemanf845b452005-10-08 00:29:44 +00004444 MVT::ValueType XType = N0.getValueType();
4445 if (SubC->isNullValue() && MVT::isInteger(XType)) {
4446 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
4447 DAG.getConstant(MVT::getSizeInBits(XType)-1,
Chris Lattner1982ef22007-04-11 05:11:38 +00004448 TLI.getShiftAmountTy()));
Nate Begemanf845b452005-10-08 00:29:44 +00004449 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00004450 AddToWorkList(Shift.Val);
4451 AddToWorkList(Add.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004452 return DAG.getNode(ISD::XOR, XType, Add, Shift);
4453 }
4454 }
4455 }
Chris Lattner1982ef22007-04-11 05:11:38 +00004456
Nate Begeman44728a72005-09-19 22:34:01 +00004457 return SDOperand();
4458}
4459
Evan Chengfa1eb272007-02-08 22:13:59 +00004460/// SimplifySetCC - This is a stub for TargetLowering::SimplifySetCC.
Nate Begeman452d7be2005-09-16 00:54:12 +00004461SDOperand DAGCombiner::SimplifySetCC(MVT::ValueType VT, SDOperand N0,
Nate Begemane17daeb2005-10-05 21:43:42 +00004462 SDOperand N1, ISD::CondCode Cond,
4463 bool foldBooleans) {
Evan Chengfa1eb272007-02-08 22:13:59 +00004464 TargetLowering::DAGCombinerInfo
4465 DagCombineInfo(DAG, !AfterLegalize, false, this);
4466 return TLI.SimplifySetCC(VT, N0, N1, Cond, foldBooleans, DagCombineInfo);
Nate Begeman452d7be2005-09-16 00:54:12 +00004467}
4468
Nate Begeman69575232005-10-20 02:15:44 +00004469/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
4470/// return a DAG expression to select that will generate the same value by
4471/// multiplying by a magic number. See:
4472/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
4473SDOperand DAGCombiner::BuildSDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00004474 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00004475 SDOperand S = TLI.BuildSDIV(N, DAG, &Built);
4476
Andrew Lenharth232c9102006-06-12 16:07:18 +00004477 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00004478 ii != ee; ++ii)
4479 AddToWorkList(*ii);
4480 return S;
Nate Begeman69575232005-10-20 02:15:44 +00004481}
4482
4483/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
4484/// return a DAG expression to select that will generate the same value by
4485/// multiplying by a magic number. See:
4486/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
4487SDOperand DAGCombiner::BuildUDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00004488 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00004489 SDOperand S = TLI.BuildUDIV(N, DAG, &Built);
Nate Begeman69575232005-10-20 02:15:44 +00004490
Andrew Lenharth232c9102006-06-12 16:07:18 +00004491 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00004492 ii != ee; ++ii)
4493 AddToWorkList(*ii);
4494 return S;
Nate Begeman69575232005-10-20 02:15:44 +00004495}
4496
Jim Laskey71382342006-10-07 23:37:56 +00004497/// FindBaseOffset - Return true if base is known not to alias with anything
4498/// but itself. Provides base object and offset as results.
4499static bool FindBaseOffset(SDOperand Ptr, SDOperand &Base, int64_t &Offset) {
4500 // Assume it is a primitive operation.
4501 Base = Ptr; Offset = 0;
4502
4503 // If it's an adding a simple constant then integrate the offset.
4504 if (Base.getOpcode() == ISD::ADD) {
4505 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Base.getOperand(1))) {
4506 Base = Base.getOperand(0);
4507 Offset += C->getValue();
4508 }
4509 }
4510
4511 // If it's any of the following then it can't alias with anything but itself.
4512 return isa<FrameIndexSDNode>(Base) ||
4513 isa<ConstantPoolSDNode>(Base) ||
4514 isa<GlobalAddressSDNode>(Base);
4515}
4516
4517/// isAlias - Return true if there is any possibility that the two addresses
4518/// overlap.
Jim Laskey096c22e2006-10-18 12:29:57 +00004519bool DAGCombiner::isAlias(SDOperand Ptr1, int64_t Size1,
4520 const Value *SrcValue1, int SrcValueOffset1,
4521 SDOperand Ptr2, int64_t Size2,
4522 const Value *SrcValue2, int SrcValueOffset2)
4523{
Jim Laskey71382342006-10-07 23:37:56 +00004524 // If they are the same then they must be aliases.
4525 if (Ptr1 == Ptr2) return true;
4526
4527 // Gather base node and offset information.
4528 SDOperand Base1, Base2;
4529 int64_t Offset1, Offset2;
4530 bool KnownBase1 = FindBaseOffset(Ptr1, Base1, Offset1);
4531 bool KnownBase2 = FindBaseOffset(Ptr2, Base2, Offset2);
4532
4533 // If they have a same base address then...
4534 if (Base1 == Base2) {
4535 // Check to see if the addresses overlap.
4536 return!((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
4537 }
4538
Jim Laskey096c22e2006-10-18 12:29:57 +00004539 // If we know both bases then they can't alias.
4540 if (KnownBase1 && KnownBase2) return false;
4541
Jim Laskey07a27092006-10-18 19:08:31 +00004542 if (CombinerGlobalAA) {
4543 // Use alias analysis information.
4544 int Overlap1 = Size1 + SrcValueOffset1 + Offset1;
4545 int Overlap2 = Size2 + SrcValueOffset2 + Offset2;
4546 AliasAnalysis::AliasResult AAResult =
Jim Laskey096c22e2006-10-18 12:29:57 +00004547 AA.alias(SrcValue1, Overlap1, SrcValue2, Overlap2);
Jim Laskey07a27092006-10-18 19:08:31 +00004548 if (AAResult == AliasAnalysis::NoAlias)
4549 return false;
4550 }
Jim Laskey096c22e2006-10-18 12:29:57 +00004551
4552 // Otherwise we have to assume they alias.
4553 return true;
Jim Laskey71382342006-10-07 23:37:56 +00004554}
4555
4556/// FindAliasInfo - Extracts the relevant alias information from the memory
4557/// node. Returns true if the operand was a load.
Jim Laskey7ca56af2006-10-11 13:47:09 +00004558bool DAGCombiner::FindAliasInfo(SDNode *N,
Jim Laskey096c22e2006-10-18 12:29:57 +00004559 SDOperand &Ptr, int64_t &Size,
4560 const Value *&SrcValue, int &SrcValueOffset) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00004561 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
4562 Ptr = LD->getBasePtr();
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004563 Size = MVT::getSizeInBits(LD->getLoadedVT()) >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00004564 SrcValue = LD->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00004565 SrcValueOffset = LD->getSrcValueOffset();
Jim Laskey71382342006-10-07 23:37:56 +00004566 return true;
Jim Laskey7ca56af2006-10-11 13:47:09 +00004567 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00004568 Ptr = ST->getBasePtr();
Evan Cheng8b2794a2006-10-13 21:14:26 +00004569 Size = MVT::getSizeInBits(ST->getStoredVT()) >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00004570 SrcValue = ST->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00004571 SrcValueOffset = ST->getSrcValueOffset();
Jim Laskey7ca56af2006-10-11 13:47:09 +00004572 } else {
Jim Laskey71382342006-10-07 23:37:56 +00004573 assert(0 && "FindAliasInfo expected a memory operand");
Jim Laskey71382342006-10-07 23:37:56 +00004574 }
4575
4576 return false;
4577}
4578
Jim Laskey6ff23e52006-10-04 16:53:27 +00004579/// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
4580/// looking for aliasing nodes and adding them to the Aliases vector.
Jim Laskeybc588b82006-10-05 15:07:25 +00004581void DAGCombiner::GatherAllAliases(SDNode *N, SDOperand OriginalChain,
Jim Laskey6ff23e52006-10-04 16:53:27 +00004582 SmallVector<SDOperand, 8> &Aliases) {
Jim Laskeybc588b82006-10-05 15:07:25 +00004583 SmallVector<SDOperand, 8> Chains; // List of chains to visit.
Jim Laskey6ff23e52006-10-04 16:53:27 +00004584 std::set<SDNode *> Visited; // Visited node set.
4585
Jim Laskey279f0532006-09-25 16:29:54 +00004586 // Get alias information for node.
4587 SDOperand Ptr;
4588 int64_t Size;
Jim Laskey7ca56af2006-10-11 13:47:09 +00004589 const Value *SrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00004590 int SrcValueOffset;
4591 bool IsLoad = FindAliasInfo(N, Ptr, Size, SrcValue, SrcValueOffset);
Jim Laskey279f0532006-09-25 16:29:54 +00004592
Jim Laskey6ff23e52006-10-04 16:53:27 +00004593 // Starting off.
Jim Laskeybc588b82006-10-05 15:07:25 +00004594 Chains.push_back(OriginalChain);
Jim Laskey6ff23e52006-10-04 16:53:27 +00004595
Jim Laskeybc588b82006-10-05 15:07:25 +00004596 // Look at each chain and determine if it is an alias. If so, add it to the
4597 // aliases list. If not, then continue up the chain looking for the next
4598 // candidate.
4599 while (!Chains.empty()) {
4600 SDOperand Chain = Chains.back();
4601 Chains.pop_back();
Jim Laskey6ff23e52006-10-04 16:53:27 +00004602
Jim Laskeybc588b82006-10-05 15:07:25 +00004603 // Don't bother if we've been before.
4604 if (Visited.find(Chain.Val) != Visited.end()) continue;
4605 Visited.insert(Chain.Val);
4606
4607 switch (Chain.getOpcode()) {
4608 case ISD::EntryToken:
4609 // Entry token is ideal chain operand, but handled in FindBetterChain.
4610 break;
Jim Laskey6ff23e52006-10-04 16:53:27 +00004611
Jim Laskeybc588b82006-10-05 15:07:25 +00004612 case ISD::LOAD:
4613 case ISD::STORE: {
4614 // Get alias information for Chain.
4615 SDOperand OpPtr;
4616 int64_t OpSize;
Jim Laskey7ca56af2006-10-11 13:47:09 +00004617 const Value *OpSrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00004618 int OpSrcValueOffset;
4619 bool IsOpLoad = FindAliasInfo(Chain.Val, OpPtr, OpSize,
4620 OpSrcValue, OpSrcValueOffset);
Jim Laskeybc588b82006-10-05 15:07:25 +00004621
4622 // If chain is alias then stop here.
4623 if (!(IsLoad && IsOpLoad) &&
Jim Laskey096c22e2006-10-18 12:29:57 +00004624 isAlias(Ptr, Size, SrcValue, SrcValueOffset,
4625 OpPtr, OpSize, OpSrcValue, OpSrcValueOffset)) {
Jim Laskeybc588b82006-10-05 15:07:25 +00004626 Aliases.push_back(Chain);
4627 } else {
4628 // Look further up the chain.
4629 Chains.push_back(Chain.getOperand(0));
4630 // Clean up old chain.
4631 AddToWorkList(Chain.Val);
Jim Laskey279f0532006-09-25 16:29:54 +00004632 }
Jim Laskeybc588b82006-10-05 15:07:25 +00004633 break;
4634 }
4635
4636 case ISD::TokenFactor:
4637 // We have to check each of the operands of the token factor, so we queue
4638 // then up. Adding the operands to the queue (stack) in reverse order
4639 // maintains the original order and increases the likelihood that getNode
4640 // will find a matching token factor (CSE.)
4641 for (unsigned n = Chain.getNumOperands(); n;)
4642 Chains.push_back(Chain.getOperand(--n));
4643 // Eliminate the token factor if we can.
4644 AddToWorkList(Chain.Val);
4645 break;
4646
4647 default:
4648 // For all other instructions we will just have to take what we can get.
4649 Aliases.push_back(Chain);
4650 break;
Jim Laskey279f0532006-09-25 16:29:54 +00004651 }
4652 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00004653}
4654
4655/// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, looking
4656/// for a better chain (aliasing node.)
4657SDOperand DAGCombiner::FindBetterChain(SDNode *N, SDOperand OldChain) {
4658 SmallVector<SDOperand, 8> Aliases; // Ops for replacing token factor.
Jim Laskey279f0532006-09-25 16:29:54 +00004659
Jim Laskey6ff23e52006-10-04 16:53:27 +00004660 // Accumulate all the aliases to this node.
4661 GatherAllAliases(N, OldChain, Aliases);
4662
4663 if (Aliases.size() == 0) {
4664 // If no operands then chain to entry token.
4665 return DAG.getEntryNode();
4666 } else if (Aliases.size() == 1) {
4667 // If a single operand then chain to it. We don't need to revisit it.
4668 return Aliases[0];
4669 }
4670
4671 // Construct a custom tailored token factor.
4672 SDOperand NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
4673 &Aliases[0], Aliases.size());
4674
4675 // Make sure the old chain gets cleaned up.
4676 if (NewChain != OldChain) AddToWorkList(OldChain.Val);
4677
4678 return NewChain;
Jim Laskey279f0532006-09-25 16:29:54 +00004679}
4680
Nate Begeman1d4d4142005-09-01 00:19:25 +00004681// SelectionDAG::Combine - This is the entry point for the file.
4682//
Jim Laskeyc7c3f112006-10-16 20:52:31 +00004683void SelectionDAG::Combine(bool RunningAfterLegalize, AliasAnalysis &AA) {
Chris Lattner938ab022007-01-16 04:55:25 +00004684 if (!RunningAfterLegalize && ViewDAGCombine1)
4685 viewGraph();
4686 if (RunningAfterLegalize && ViewDAGCombine2)
4687 viewGraph();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004688 /// run - This is the main entry point to this class.
4689 ///
Jim Laskeyc7c3f112006-10-16 20:52:31 +00004690 DAGCombiner(*this, AA).Run(RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004691}