blob: b646f5ecdc548ba14aa3b5b62979dea2175ea7ac [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.
Chris Lattner501fee72007-05-23 07:35:22 +0000355static char isNegatibleForFree(SDOperand Op, unsigned Depth = 0) {
356 // Don't recurse exponentially.
357 if (Depth > 6) return false;
358
Chris Lattner29446522007-05-14 22:04:50 +0000359 // fneg is removable even if it has multiple uses.
360 if (Op.getOpcode() == ISD::FNEG) return 2;
361
362 // Don't allow anything with multiple uses.
363 if (!Op.hasOneUse()) return 0;
364
365 switch (Op.getOpcode()) {
366 default: return false;
367 case ISD::ConstantFP:
368 return 1;
369 case ISD::FADD:
370 // FIXME: determine better conditions for this xform.
371 if (!UnsafeFPMath) return 0;
372
373 // -(A+B) -> -A - B
Chris Lattner501fee72007-05-23 07:35:22 +0000374 if (char V = isNegatibleForFree(Op.getOperand(0), Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000375 return V;
376 // -(A+B) -> -B - A
Chris Lattner501fee72007-05-23 07:35:22 +0000377 return isNegatibleForFree(Op.getOperand(1), Depth+1);
Chris Lattner29446522007-05-14 22:04:50 +0000378 case ISD::FSUB:
379 // We can't turn -(A-B) into B-A when we honor signed zeros.
380 if (!UnsafeFPMath) return 0;
381
382 // -(A-B) -> B-A
383 return 1;
384
385 case ISD::FMUL:
386 case ISD::FDIV:
387 if (HonorSignDependentRoundingFPMath()) return 0;
388
389 // -(X*Y) -> (-X * Y) or (X*-Y)
Chris Lattner501fee72007-05-23 07:35:22 +0000390 if (char V = isNegatibleForFree(Op.getOperand(0), Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000391 return V;
392
Chris Lattner501fee72007-05-23 07:35:22 +0000393 return isNegatibleForFree(Op.getOperand(1), Depth+1);
Chris Lattner29446522007-05-14 22:04:50 +0000394
395 case ISD::FP_EXTEND:
396 case ISD::FP_ROUND:
397 case ISD::FSIN:
Chris Lattner501fee72007-05-23 07:35:22 +0000398 return isNegatibleForFree(Op.getOperand(0), Depth+1);
Chris Lattner29446522007-05-14 22:04:50 +0000399 }
400}
401
402/// GetNegatedExpression - If isNegatibleForFree returns true, this function
403/// returns the newly negated expression.
404static SDOperand GetNegatedExpression(SDOperand Op, SelectionDAG &DAG) {
405 // fneg is removable even if it has multiple uses.
406 if (Op.getOpcode() == ISD::FNEG) return Op.getOperand(0);
407
408 // Don't allow anything with multiple uses.
409 assert(Op.hasOneUse() && "Unknown reuse!");
410
411 switch (Op.getOpcode()) {
412 default: assert(0 && "Unknown code");
413 case ISD::ConstantFP:
414 return DAG.getConstantFP(-cast<ConstantFPSDNode>(Op)->getValue(),
415 Op.getValueType());
416 case ISD::FADD:
417 // FIXME: determine better conditions for this xform.
418 assert(UnsafeFPMath);
419
420 // -(A+B) -> -A - B
421 if (isNegatibleForFree(Op.getOperand(0)))
422 return DAG.getNode(ISD::FSUB, Op.getValueType(),
423 GetNegatedExpression(Op.getOperand(0), DAG),
424 Op.getOperand(1));
425 // -(A+B) -> -B - A
426 return DAG.getNode(ISD::FSUB, Op.getValueType(),
427 GetNegatedExpression(Op.getOperand(1), DAG),
428 Op.getOperand(0));
429 case ISD::FSUB:
430 // We can't turn -(A-B) into B-A when we honor signed zeros.
431 assert(UnsafeFPMath);
432
433 // -(A-B) -> B-A
434 return DAG.getNode(ISD::FSUB, Op.getValueType(), Op.getOperand(1),
435 Op.getOperand(0));
436
437 case ISD::FMUL:
438 case ISD::FDIV:
439 assert(!HonorSignDependentRoundingFPMath());
440
441 // -(X*Y) -> -X * Y
442 if (isNegatibleForFree(Op.getOperand(0)))
443 return DAG.getNode(Op.getOpcode(), Op.getValueType(),
444 GetNegatedExpression(Op.getOperand(0), DAG),
445 Op.getOperand(1));
446
447 // -(X*Y) -> X * -Y
448 return DAG.getNode(Op.getOpcode(), Op.getValueType(),
449 Op.getOperand(0),
450 GetNegatedExpression(Op.getOperand(1), DAG));
451
452 case ISD::FP_EXTEND:
453 case ISD::FP_ROUND:
454 case ISD::FSIN:
455 return DAG.getNode(Op.getOpcode(), Op.getValueType(),
Lauro Ramos Venanciob5bb7ff2007-05-15 17:05:43 +0000456 GetNegatedExpression(Op.getOperand(0), DAG));
Chris Lattner29446522007-05-14 22:04:50 +0000457 }
458}
Chris Lattner24664722006-03-01 04:53:38 +0000459
460
Nate Begeman4ebd8052005-09-01 23:24:04 +0000461// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
462// that selects between the values 1 and 0, making it equivalent to a setcc.
Nate Begeman646d7e22005-09-02 21:18:40 +0000463// Also, set the incoming LHS, RHS, and CC references to the appropriate
464// nodes based on the type of node we are checking. This simplifies life a
465// bit for the callers.
466static bool isSetCCEquivalent(SDOperand N, SDOperand &LHS, SDOperand &RHS,
467 SDOperand &CC) {
468 if (N.getOpcode() == ISD::SETCC) {
469 LHS = N.getOperand(0);
470 RHS = N.getOperand(1);
471 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000472 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000473 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000474 if (N.getOpcode() == ISD::SELECT_CC &&
475 N.getOperand(2).getOpcode() == ISD::Constant &&
476 N.getOperand(3).getOpcode() == ISD::Constant &&
477 cast<ConstantSDNode>(N.getOperand(2))->getValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000478 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
479 LHS = N.getOperand(0);
480 RHS = N.getOperand(1);
481 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000482 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000483 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000484 return false;
485}
486
Nate Begeman99801192005-09-07 23:25:52 +0000487// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
488// one use. If this is true, it allows the users to invert the operation for
489// free when it is profitable to do so.
490static bool isOneUseSetCC(SDOperand N) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000491 SDOperand N0, N1, N2;
Nate Begeman646d7e22005-09-02 21:18:40 +0000492 if (isSetCCEquivalent(N, N0, N1, N2) && N.Val->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000493 return true;
494 return false;
495}
496
Nate Begemancd4d58c2006-02-03 06:46:56 +0000497SDOperand DAGCombiner::ReassociateOps(unsigned Opc, SDOperand N0, SDOperand N1){
498 MVT::ValueType VT = N0.getValueType();
499 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
500 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
501 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
502 if (isa<ConstantSDNode>(N1)) {
503 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(1), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000504 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000505 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(0));
506 } else if (N0.hasOneUse()) {
507 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(0), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000508 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000509 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(1));
510 }
511 }
512 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
513 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
514 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
515 if (isa<ConstantSDNode>(N0)) {
516 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(1), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000517 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000518 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(0));
519 } else if (N1.hasOneUse()) {
520 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(0), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000521 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000522 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(1));
523 }
524 }
525 return SDOperand();
526}
527
Chris Lattner29446522007-05-14 22:04:50 +0000528//===----------------------------------------------------------------------===//
529// Main DAG Combiner implementation
530//===----------------------------------------------------------------------===//
531
Nate Begeman4ebd8052005-09-01 23:24:04 +0000532void DAGCombiner::Run(bool RunningAfterLegalize) {
533 // set the instance variable, so that the various visit routines may use it.
534 AfterLegalize = RunningAfterLegalize;
535
Nate Begeman646d7e22005-09-02 21:18:40 +0000536 // Add all the dag nodes to the worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000537 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
538 E = DAG.allnodes_end(); I != E; ++I)
539 WorkList.push_back(I);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000540
Chris Lattner95038592005-10-05 06:35:28 +0000541 // Create a dummy node (which is not added to allnodes), that adds a reference
542 // to the root node, preventing it from being deleted, and tracking any
543 // changes of the root.
544 HandleSDNode Dummy(DAG.getRoot());
545
Jim Laskey26f7fa72006-10-17 19:33:52 +0000546 // The root of the dag may dangle to deleted nodes until the dag combiner is
547 // done. Set it to null to avoid confusion.
548 DAG.setRoot(SDOperand());
Chris Lattner24664722006-03-01 04:53:38 +0000549
550 /// DagCombineInfo - Expose the DAG combiner to the target combiner impls.
551 TargetLowering::DAGCombinerInfo
Evan Chengfa1eb272007-02-08 22:13:59 +0000552 DagCombineInfo(DAG, !RunningAfterLegalize, false, this);
Jim Laskey6ff23e52006-10-04 16:53:27 +0000553
Nate Begeman1d4d4142005-09-01 00:19:25 +0000554 // while the worklist isn't empty, inspect the node on the end of it and
555 // try and combine it.
556 while (!WorkList.empty()) {
557 SDNode *N = WorkList.back();
558 WorkList.pop_back();
559
560 // If N has no uses, it is dead. Make sure to revisit all N's operands once
Chris Lattner95038592005-10-05 06:35:28 +0000561 // N is deleted from the DAG, since they too may now be dead or may have a
562 // reduced number of uses, allowing other xforms.
563 if (N->use_empty() && N != &Dummy) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000564 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Jim Laskey6ff23e52006-10-04 16:53:27 +0000565 AddToWorkList(N->getOperand(i).Val);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000566
Chris Lattner95038592005-10-05 06:35:28 +0000567 DAG.DeleteNode(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000568 continue;
569 }
570
Nate Begeman83e75ec2005-09-06 04:43:02 +0000571 SDOperand RV = visit(N);
Chris Lattner24664722006-03-01 04:53:38 +0000572
573 // If nothing happened, try a target-specific DAG combine.
574 if (RV.Val == 0) {
Chris Lattner729c6d12006-05-27 00:43:02 +0000575 assert(N->getOpcode() != ISD::DELETED_NODE &&
576 "Node was deleted but visit returned NULL!");
Chris Lattner24664722006-03-01 04:53:38 +0000577 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
578 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode()))
579 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
580 }
581
Nate Begeman83e75ec2005-09-06 04:43:02 +0000582 if (RV.Val) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000583 ++NodesCombined;
Nate Begeman646d7e22005-09-02 21:18:40 +0000584 // If we get back the same node we passed in, rather than a new node or
585 // zero, we know that the node must have defined multiple values and
586 // CombineTo was used. Since CombineTo takes care of the worklist
587 // mechanics for us, we have no work to do in this case.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000588 if (RV.Val != N) {
Chris Lattner729c6d12006-05-27 00:43:02 +0000589 assert(N->getOpcode() != ISD::DELETED_NODE &&
590 RV.Val->getOpcode() != ISD::DELETED_NODE &&
591 "Node was deleted but visit returned new node!");
592
Bill Wendling832171c2006-12-07 20:04:42 +0000593 DOUT << "\nReplacing.3 "; DEBUG(N->dump());
594 DOUT << "\nWith: "; DEBUG(RV.Val->dump(&DAG));
595 DOUT << '\n';
Chris Lattner01a22022005-10-10 22:04:48 +0000596 std::vector<SDNode*> NowDead;
Evan Cheng2adffa12006-09-21 19:04:05 +0000597 if (N->getNumValues() == RV.Val->getNumValues())
598 DAG.ReplaceAllUsesWith(N, RV.Val, &NowDead);
599 else {
600 assert(N->getValueType(0) == RV.getValueType() && "Type mismatch");
601 SDOperand OpV = RV;
602 DAG.ReplaceAllUsesWith(N, &OpV, &NowDead);
603 }
Nate Begeman646d7e22005-09-02 21:18:40 +0000604
605 // Push the new node and any users onto the worklist
Jim Laskey6ff23e52006-10-04 16:53:27 +0000606 AddToWorkList(RV.Val);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000607 AddUsersToWorkList(RV.Val);
Nate Begeman646d7e22005-09-02 21:18:40 +0000608
Jim Laskey6ff23e52006-10-04 16:53:27 +0000609 // Nodes can be reintroduced into the worklist. Make sure we do not
610 // process a node that has been replaced.
Nate Begeman646d7e22005-09-02 21:18:40 +0000611 removeFromWorkList(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000612 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
613 removeFromWorkList(NowDead[i]);
Chris Lattner5c46f742005-10-05 06:11:08 +0000614
615 // Finally, since the node is now dead, remove it from the graph.
616 DAG.DeleteNode(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000617 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000618 }
619 }
Chris Lattner95038592005-10-05 06:35:28 +0000620
621 // If the root changed (e.g. it was a dead load, update the root).
622 DAG.setRoot(Dummy.getValue());
Nate Begeman1d4d4142005-09-01 00:19:25 +0000623}
624
Nate Begeman83e75ec2005-09-06 04:43:02 +0000625SDOperand DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000626 switch(N->getOpcode()) {
627 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +0000628 case ISD::TokenFactor: return visitTokenFactor(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000629 case ISD::ADD: return visitADD(N);
630 case ISD::SUB: return visitSUB(N);
Chris Lattner91153682007-03-04 20:03:15 +0000631 case ISD::ADDC: return visitADDC(N);
632 case ISD::ADDE: return visitADDE(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000633 case ISD::MUL: return visitMUL(N);
634 case ISD::SDIV: return visitSDIV(N);
635 case ISD::UDIV: return visitUDIV(N);
636 case ISD::SREM: return visitSREM(N);
637 case ISD::UREM: return visitUREM(N);
638 case ISD::MULHU: return visitMULHU(N);
639 case ISD::MULHS: return visitMULHS(N);
640 case ISD::AND: return visitAND(N);
641 case ISD::OR: return visitOR(N);
642 case ISD::XOR: return visitXOR(N);
643 case ISD::SHL: return visitSHL(N);
644 case ISD::SRA: return visitSRA(N);
645 case ISD::SRL: return visitSRL(N);
646 case ISD::CTLZ: return visitCTLZ(N);
647 case ISD::CTTZ: return visitCTTZ(N);
648 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000649 case ISD::SELECT: return visitSELECT(N);
650 case ISD::SELECT_CC: return visitSELECT_CC(N);
651 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000652 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
653 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000654 case ISD::ANY_EXTEND: return visitANY_EXTEND(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000655 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
656 case ISD::TRUNCATE: return visitTRUNCATE(N);
Chris Lattner94683772005-12-23 05:30:37 +0000657 case ISD::BIT_CONVERT: return visitBIT_CONVERT(N);
Chris Lattner6258fb22006-04-02 02:53:43 +0000658 case ISD::VBIT_CONVERT: return visitVBIT_CONVERT(N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000659 case ISD::FADD: return visitFADD(N);
660 case ISD::FSUB: return visitFSUB(N);
661 case ISD::FMUL: return visitFMUL(N);
662 case ISD::FDIV: return visitFDIV(N);
663 case ISD::FREM: return visitFREM(N);
Chris Lattner12d83032006-03-05 05:30:57 +0000664 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000665 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
666 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
667 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
668 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
669 case ISD::FP_ROUND: return visitFP_ROUND(N);
670 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
671 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
672 case ISD::FNEG: return visitFNEG(N);
673 case ISD::FABS: return visitFABS(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000674 case ISD::BRCOND: return visitBRCOND(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000675 case ISD::BR_CC: return visitBR_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000676 case ISD::LOAD: return visitLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000677 case ISD::STORE: return visitSTORE(N);
Chris Lattnerca242442006-03-19 01:27:56 +0000678 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
679 case ISD::VINSERT_VECTOR_ELT: return visitVINSERT_VECTOR_ELT(N);
Chris Lattnerd7648c82006-03-28 20:28:38 +0000680 case ISD::VBUILD_VECTOR: return visitVBUILD_VECTOR(N);
Chris Lattner66445d32006-03-28 22:11:53 +0000681 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
Chris Lattnerf1d0c622006-03-31 22:16:43 +0000682 case ISD::VVECTOR_SHUFFLE: return visitVVECTOR_SHUFFLE(N);
Chris Lattneredab1b92006-04-02 03:25:57 +0000683 case ISD::VADD: return visitVBinOp(N, ISD::ADD , ISD::FADD);
684 case ISD::VSUB: return visitVBinOp(N, ISD::SUB , ISD::FSUB);
685 case ISD::VMUL: return visitVBinOp(N, ISD::MUL , ISD::FMUL);
686 case ISD::VSDIV: return visitVBinOp(N, ISD::SDIV, ISD::FDIV);
687 case ISD::VUDIV: return visitVBinOp(N, ISD::UDIV, ISD::UDIV);
688 case ISD::VAND: return visitVBinOp(N, ISD::AND , ISD::AND);
689 case ISD::VOR: return visitVBinOp(N, ISD::OR , ISD::OR);
690 case ISD::VXOR: return visitVBinOp(N, ISD::XOR , ISD::XOR);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000691 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000692 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000693}
694
Chris Lattner6270f682006-10-08 22:57:01 +0000695/// getInputChainForNode - Given a node, return its input chain if it has one,
696/// otherwise return a null sd operand.
697static SDOperand getInputChainForNode(SDNode *N) {
698 if (unsigned NumOps = N->getNumOperands()) {
699 if (N->getOperand(0).getValueType() == MVT::Other)
700 return N->getOperand(0);
701 else if (N->getOperand(NumOps-1).getValueType() == MVT::Other)
702 return N->getOperand(NumOps-1);
703 for (unsigned i = 1; i < NumOps-1; ++i)
704 if (N->getOperand(i).getValueType() == MVT::Other)
705 return N->getOperand(i);
706 }
707 return SDOperand(0, 0);
708}
709
Nate Begeman83e75ec2005-09-06 04:43:02 +0000710SDOperand DAGCombiner::visitTokenFactor(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +0000711 // If N has two operands, where one has an input chain equal to the other,
712 // the 'other' chain is redundant.
713 if (N->getNumOperands() == 2) {
714 if (getInputChainForNode(N->getOperand(0).Val) == N->getOperand(1))
715 return N->getOperand(0);
716 if (getInputChainForNode(N->getOperand(1).Val) == N->getOperand(0))
717 return N->getOperand(1);
718 }
719
Chris Lattnerc76d4412007-05-16 06:37:59 +0000720 SmallVector<SDNode *, 8> TFs; // List of token factors to visit.
721 SmallVector<SDOperand, 8> Ops; // Ops for replacing token factor.
722 SmallPtrSet<SDNode*, 16> SeenOps;
723 bool Changed = false; // If we should replace this token factor.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000724
725 // Start out with this token factor.
Jim Laskey279f0532006-09-25 16:29:54 +0000726 TFs.push_back(N);
Jim Laskey279f0532006-09-25 16:29:54 +0000727
Jim Laskey71382342006-10-07 23:37:56 +0000728 // Iterate through token factors. The TFs grows when new token factors are
Jim Laskeybc588b82006-10-05 15:07:25 +0000729 // encountered.
730 for (unsigned i = 0; i < TFs.size(); ++i) {
731 SDNode *TF = TFs[i];
732
Jim Laskey6ff23e52006-10-04 16:53:27 +0000733 // Check each of the operands.
734 for (unsigned i = 0, ie = TF->getNumOperands(); i != ie; ++i) {
735 SDOperand Op = TF->getOperand(i);
Jim Laskey279f0532006-09-25 16:29:54 +0000736
Jim Laskey6ff23e52006-10-04 16:53:27 +0000737 switch (Op.getOpcode()) {
738 case ISD::EntryToken:
Jim Laskeybc588b82006-10-05 15:07:25 +0000739 // Entry tokens don't need to be added to the list. They are
740 // rededundant.
741 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000742 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000743
Jim Laskey6ff23e52006-10-04 16:53:27 +0000744 case ISD::TokenFactor:
Jim Laskeybc588b82006-10-05 15:07:25 +0000745 if ((CombinerAA || Op.hasOneUse()) &&
746 std::find(TFs.begin(), TFs.end(), Op.Val) == TFs.end()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +0000747 // Queue up for processing.
748 TFs.push_back(Op.Val);
749 // Clean up in case the token factor is removed.
750 AddToWorkList(Op.Val);
751 Changed = true;
752 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000753 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000754 // Fall thru
755
756 default:
Chris Lattnerc76d4412007-05-16 06:37:59 +0000757 // Only add if it isn't already in the list.
758 if (SeenOps.insert(Op.Val))
Jim Laskeybc588b82006-10-05 15:07:25 +0000759 Ops.push_back(Op);
Chris Lattnerc76d4412007-05-16 06:37:59 +0000760 else
761 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000762 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000763 }
764 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000765 }
766
767 SDOperand Result;
768
769 // If we've change things around then replace token factor.
770 if (Changed) {
771 if (Ops.size() == 0) {
772 // The entry token is the only possible outcome.
773 Result = DAG.getEntryNode();
774 } else {
775 // New and improved token factor.
776 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0], Ops.size());
Nate Begemanded49632005-10-13 03:11:28 +0000777 }
Jim Laskey274062c2006-10-13 23:32:28 +0000778
779 // Don't add users to work list.
780 return CombineTo(N, Result, false);
Nate Begemanded49632005-10-13 03:11:28 +0000781 }
Jim Laskey279f0532006-09-25 16:29:54 +0000782
Jim Laskey6ff23e52006-10-04 16:53:27 +0000783 return Result;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000784}
785
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000786static
787SDOperand combineShlAddConstant(SDOperand N0, SDOperand N1, SelectionDAG &DAG) {
788 MVT::ValueType VT = N0.getValueType();
789 SDOperand N00 = N0.getOperand(0);
790 SDOperand N01 = N0.getOperand(1);
791 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N01);
792 if (N01C && N00.getOpcode() == ISD::ADD && N00.Val->hasOneUse() &&
793 isa<ConstantSDNode>(N00.getOperand(1))) {
794 N0 = DAG.getNode(ISD::ADD, VT,
795 DAG.getNode(ISD::SHL, VT, N00.getOperand(0), N01),
796 DAG.getNode(ISD::SHL, VT, N00.getOperand(1), N01));
797 return DAG.getNode(ISD::ADD, VT, N0, N1);
798 }
799 return SDOperand();
800}
801
Nate Begeman83e75ec2005-09-06 04:43:02 +0000802SDOperand DAGCombiner::visitADD(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000803 SDOperand N0 = N->getOperand(0);
804 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000805 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
806 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemanf89d78d2005-09-07 16:09:19 +0000807 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000808
809 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000810 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000811 return DAG.getNode(ISD::ADD, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000812 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000813 if (N0C && !N1C)
814 return DAG.getNode(ISD::ADD, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000815 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000816 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000817 return N0;
Chris Lattner4aafb4f2006-01-12 20:22:43 +0000818 // fold ((c1-A)+c2) -> (c1+c2)-A
819 if (N1C && N0.getOpcode() == ISD::SUB)
820 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
821 return DAG.getNode(ISD::SUB, VT,
822 DAG.getConstant(N1C->getValue()+N0C->getValue(), VT),
823 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000824 // reassociate add
825 SDOperand RADD = ReassociateOps(ISD::ADD, N0, N1);
826 if (RADD.Val != 0)
827 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000828 // fold ((0-A) + B) -> B-A
829 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
830 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000831 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000832 // fold (A + (0-B)) -> A-B
833 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
834 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000835 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +0000836 // fold (A+(B-A)) -> B
837 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000838 return N1.getOperand(0);
Chris Lattner947c2892006-03-13 06:51:27 +0000839
Evan Cheng860771d2006-03-01 01:09:54 +0000840 if (!MVT::isVector(VT) && SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +0000841 return SDOperand(N, 0);
Chris Lattner947c2892006-03-13 06:51:27 +0000842
843 // fold (a+b) -> (a|b) iff a and b share no bits.
844 if (MVT::isInteger(VT) && !MVT::isVector(VT)) {
845 uint64_t LHSZero, LHSOne;
846 uint64_t RHSZero, RHSOne;
847 uint64_t Mask = MVT::getIntVTBitMask(VT);
848 TLI.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
849 if (LHSZero) {
850 TLI.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
851
852 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
853 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
854 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
855 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
856 return DAG.getNode(ISD::OR, VT, N0, N1);
857 }
858 }
Evan Cheng3ef554d2006-11-06 08:14:30 +0000859
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000860 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
861 if (N0.getOpcode() == ISD::SHL && N0.Val->hasOneUse()) {
862 SDOperand Result = combineShlAddConstant(N0, N1, DAG);
863 if (Result.Val) return Result;
864 }
865 if (N1.getOpcode() == ISD::SHL && N1.Val->hasOneUse()) {
866 SDOperand Result = combineShlAddConstant(N1, N0, DAG);
867 if (Result.Val) return Result;
868 }
869
Nate Begeman83e75ec2005-09-06 04:43:02 +0000870 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000871}
872
Chris Lattner91153682007-03-04 20:03:15 +0000873SDOperand DAGCombiner::visitADDC(SDNode *N) {
874 SDOperand N0 = N->getOperand(0);
875 SDOperand N1 = N->getOperand(1);
876 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
877 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
878 MVT::ValueType VT = N0.getValueType();
879
880 // If the flag result is dead, turn this into an ADD.
881 if (N->hasNUsesOfValue(0, 1))
882 return CombineTo(N, DAG.getNode(ISD::ADD, VT, N1, N0),
Chris Lattnerb6541762007-03-04 20:40:38 +0000883 DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
Chris Lattner91153682007-03-04 20:03:15 +0000884
885 // canonicalize constant to RHS.
Chris Lattnerbcf24842007-03-04 20:08:45 +0000886 if (N0C && !N1C) {
887 SDOperand Ops[] = { N1, N0 };
888 return DAG.getNode(ISD::ADDC, N->getVTList(), Ops, 2);
889 }
Chris Lattner91153682007-03-04 20:03:15 +0000890
Chris Lattnerb6541762007-03-04 20:40:38 +0000891 // fold (addc x, 0) -> x + no carry out
892 if (N1C && N1C->isNullValue())
893 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
894
895 // fold (addc a, b) -> (or a, b), CARRY_FALSE iff a and b share no bits.
896 uint64_t LHSZero, LHSOne;
897 uint64_t RHSZero, RHSOne;
898 uint64_t Mask = MVT::getIntVTBitMask(VT);
899 TLI.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
900 if (LHSZero) {
901 TLI.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
902
903 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
904 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
905 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
906 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
907 return CombineTo(N, DAG.getNode(ISD::OR, VT, N0, N1),
908 DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
909 }
Chris Lattner91153682007-03-04 20:03:15 +0000910
911 return SDOperand();
912}
913
914SDOperand DAGCombiner::visitADDE(SDNode *N) {
915 SDOperand N0 = N->getOperand(0);
916 SDOperand N1 = N->getOperand(1);
Chris Lattnerb6541762007-03-04 20:40:38 +0000917 SDOperand CarryIn = N->getOperand(2);
Chris Lattner91153682007-03-04 20:03:15 +0000918 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
919 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Chris Lattnerbcf24842007-03-04 20:08:45 +0000920 //MVT::ValueType VT = N0.getValueType();
Chris Lattner91153682007-03-04 20:03:15 +0000921
922 // canonicalize constant to RHS
Chris Lattnerbcf24842007-03-04 20:08:45 +0000923 if (N0C && !N1C) {
Chris Lattnerb6541762007-03-04 20:40:38 +0000924 SDOperand Ops[] = { N1, N0, CarryIn };
Chris Lattnerbcf24842007-03-04 20:08:45 +0000925 return DAG.getNode(ISD::ADDE, N->getVTList(), Ops, 3);
926 }
Chris Lattner91153682007-03-04 20:03:15 +0000927
Chris Lattnerb6541762007-03-04 20:40:38 +0000928 // fold (adde x, y, false) -> (addc x, y)
929 if (CarryIn.getOpcode() == ISD::CARRY_FALSE) {
930 SDOperand Ops[] = { N1, N0 };
931 return DAG.getNode(ISD::ADDC, N->getVTList(), Ops, 2);
932 }
Chris Lattner91153682007-03-04 20:03:15 +0000933
934 return SDOperand();
935}
936
937
938
Nate Begeman83e75ec2005-09-06 04:43:02 +0000939SDOperand DAGCombiner::visitSUB(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000940 SDOperand N0 = N->getOperand(0);
941 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000942 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
943 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000944 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000945
Chris Lattner854077d2005-10-17 01:07:11 +0000946 // fold (sub x, x) -> 0
947 if (N0 == N1)
948 return DAG.getConstant(0, N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000949 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000950 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000951 return DAG.getNode(ISD::SUB, VT, N0, N1);
Chris Lattner05b57432005-10-11 06:07:15 +0000952 // fold (sub x, c) -> (add x, -c)
953 if (N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000954 return DAG.getNode(ISD::ADD, VT, N0, DAG.getConstant(-N1C->getValue(), VT));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000955 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +0000956 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000957 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000958 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +0000959 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000960 return N0.getOperand(0);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000961 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000962}
963
Nate Begeman83e75ec2005-09-06 04:43:02 +0000964SDOperand DAGCombiner::visitMUL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000965 SDOperand N0 = N->getOperand(0);
966 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000967 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
968 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman223df222005-09-08 20:18:10 +0000969 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000970
971 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000972 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000973 return DAG.getNode(ISD::MUL, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000974 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000975 if (N0C && !N1C)
976 return DAG.getNode(ISD::MUL, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000977 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000978 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000979 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000980 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +0000981 if (N1C && N1C->isAllOnesValue())
Nate Begeman405e3ec2005-10-21 00:02:42 +0000982 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000983 // fold (mul x, (1 << c)) -> x << c
Nate Begeman646d7e22005-09-02 21:18:40 +0000984 if (N1C && isPowerOf2_64(N1C->getValue()))
Chris Lattner3e6099b2005-10-30 06:41:49 +0000985 return DAG.getNode(ISD::SHL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000986 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000987 TLI.getShiftAmountTy()));
Chris Lattner3e6099b2005-10-30 06:41:49 +0000988 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
989 if (N1C && isPowerOf2_64(-N1C->getSignExtended())) {
990 // FIXME: If the input is something that is easily negated (e.g. a
991 // single-use add), we should put the negate there.
992 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT),
993 DAG.getNode(ISD::SHL, VT, N0,
994 DAG.getConstant(Log2_64(-N1C->getSignExtended()),
995 TLI.getShiftAmountTy())));
996 }
Andrew Lenharth50a0d422006-04-02 21:42:45 +0000997
Chris Lattner0b1a85f2006-03-01 03:44:24 +0000998 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
999 if (N1C && N0.getOpcode() == ISD::SHL &&
1000 isa<ConstantSDNode>(N0.getOperand(1))) {
1001 SDOperand C3 = DAG.getNode(ISD::SHL, VT, N1, N0.getOperand(1));
Chris Lattner5750df92006-03-01 04:03:14 +00001002 AddToWorkList(C3.Val);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001003 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0), C3);
1004 }
1005
1006 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
1007 // use.
1008 {
1009 SDOperand Sh(0,0), Y(0,0);
1010 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
1011 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
1012 N0.Val->hasOneUse()) {
1013 Sh = N0; Y = N1;
1014 } else if (N1.getOpcode() == ISD::SHL &&
1015 isa<ConstantSDNode>(N1.getOperand(1)) && N1.Val->hasOneUse()) {
1016 Sh = N1; Y = N0;
1017 }
1018 if (Sh.Val) {
1019 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Sh.getOperand(0), Y);
1020 return DAG.getNode(ISD::SHL, VT, Mul, Sh.getOperand(1));
1021 }
1022 }
Chris Lattnera1deca32006-03-04 23:33:26 +00001023 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
1024 if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() &&
1025 isa<ConstantSDNode>(N0.getOperand(1))) {
1026 return DAG.getNode(ISD::ADD, VT,
1027 DAG.getNode(ISD::MUL, VT, N0.getOperand(0), N1),
1028 DAG.getNode(ISD::MUL, VT, N0.getOperand(1), N1));
1029 }
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001030
Nate Begemancd4d58c2006-02-03 06:46:56 +00001031 // reassociate mul
1032 SDOperand RMUL = ReassociateOps(ISD::MUL, N0, N1);
1033 if (RMUL.Val != 0)
1034 return RMUL;
Nate Begeman83e75ec2005-09-06 04:43:02 +00001035 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001036}
1037
Nate Begeman83e75ec2005-09-06 04:43:02 +00001038SDOperand DAGCombiner::visitSDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001039 SDOperand N0 = N->getOperand(0);
1040 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001041 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1042 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +00001043 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001044
1045 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001046 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001047 return DAG.getNode(ISD::SDIV, VT, N0, N1);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001048 // fold (sdiv X, 1) -> X
1049 if (N1C && N1C->getSignExtended() == 1LL)
1050 return N0;
1051 // fold (sdiv X, -1) -> 0-X
1052 if (N1C && N1C->isAllOnesValue())
1053 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +00001054 // If we know the sign bits of both operands are zero, strength reduce to a
1055 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
1056 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001057 if (TLI.MaskedValueIsZero(N1, SignBit) &&
1058 TLI.MaskedValueIsZero(N0, SignBit))
Chris Lattner094c8fc2005-10-07 06:10:46 +00001059 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
Nate Begemancd6a6ed2006-02-17 07:26:20 +00001060 // fold (sdiv X, pow2) -> simple ops after legalize
Nate Begemanfb7217b2006-02-17 19:54:08 +00001061 if (N1C && N1C->getValue() && !TLI.isIntDivCheap() &&
Nate Begeman405e3ec2005-10-21 00:02:42 +00001062 (isPowerOf2_64(N1C->getSignExtended()) ||
1063 isPowerOf2_64(-N1C->getSignExtended()))) {
1064 // If dividing by powers of two is cheap, then don't perform the following
1065 // fold.
1066 if (TLI.isPow2DivCheap())
1067 return SDOperand();
1068 int64_t pow2 = N1C->getSignExtended();
1069 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
Chris Lattner8f4880b2006-02-16 08:02:36 +00001070 unsigned lg2 = Log2_64(abs2);
1071 // Splat the sign bit into the register
1072 SDOperand SGN = DAG.getNode(ISD::SRA, VT, N0,
Nate Begeman405e3ec2005-10-21 00:02:42 +00001073 DAG.getConstant(MVT::getSizeInBits(VT)-1,
1074 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00001075 AddToWorkList(SGN.Val);
Chris Lattner8f4880b2006-02-16 08:02:36 +00001076 // Add (N0 < 0) ? abs2 - 1 : 0;
1077 SDOperand SRL = DAG.getNode(ISD::SRL, VT, SGN,
1078 DAG.getConstant(MVT::getSizeInBits(VT)-lg2,
Nate Begeman405e3ec2005-10-21 00:02:42 +00001079 TLI.getShiftAmountTy()));
Chris Lattner8f4880b2006-02-16 08:02:36 +00001080 SDOperand ADD = DAG.getNode(ISD::ADD, VT, N0, SRL);
Chris Lattner5750df92006-03-01 04:03:14 +00001081 AddToWorkList(SRL.Val);
1082 AddToWorkList(ADD.Val); // Divide by pow2
Chris Lattner8f4880b2006-02-16 08:02:36 +00001083 SDOperand SRA = DAG.getNode(ISD::SRA, VT, ADD,
1084 DAG.getConstant(lg2, TLI.getShiftAmountTy()));
Nate Begeman405e3ec2005-10-21 00:02:42 +00001085 // If we're dividing by a positive value, we're done. Otherwise, we must
1086 // negate the result.
1087 if (pow2 > 0)
1088 return SRA;
Chris Lattner5750df92006-03-01 04:03:14 +00001089 AddToWorkList(SRA.Val);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001090 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), SRA);
1091 }
Nate Begeman69575232005-10-20 02:15:44 +00001092 // if integer divide is expensive and we satisfy the requirements, emit an
1093 // alternate sequence.
Nate Begeman405e3ec2005-10-21 00:02:42 +00001094 if (N1C && (N1C->getSignExtended() < -1 || N1C->getSignExtended() > 1) &&
Chris Lattnere9936d12005-10-22 18:50:15 +00001095 !TLI.isIntDivCheap()) {
1096 SDOperand Op = BuildSDIV(N);
1097 if (Op.Val) return Op;
Nate Begeman69575232005-10-20 02:15:44 +00001098 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001099 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001100}
1101
Nate Begeman83e75ec2005-09-06 04:43:02 +00001102SDOperand DAGCombiner::visitUDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001103 SDOperand N0 = N->getOperand(0);
1104 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001105 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1106 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +00001107 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001108
1109 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001110 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001111 return DAG.getNode(ISD::UDIV, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001112 // fold (udiv x, (1 << c)) -> x >>u c
Nate Begeman646d7e22005-09-02 21:18:40 +00001113 if (N1C && isPowerOf2_64(N1C->getValue()))
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001114 return DAG.getNode(ISD::SRL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +00001115 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001116 TLI.getShiftAmountTy()));
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001117 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
1118 if (N1.getOpcode() == ISD::SHL) {
1119 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
1120 if (isPowerOf2_64(SHC->getValue())) {
1121 MVT::ValueType ADDVT = N1.getOperand(1).getValueType();
Nate Begemanc031e332006-02-05 07:36:48 +00001122 SDOperand Add = DAG.getNode(ISD::ADD, ADDVT, N1.getOperand(1),
1123 DAG.getConstant(Log2_64(SHC->getValue()),
1124 ADDVT));
Chris Lattner5750df92006-03-01 04:03:14 +00001125 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +00001126 return DAG.getNode(ISD::SRL, VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001127 }
1128 }
1129 }
Nate Begeman69575232005-10-20 02:15:44 +00001130 // fold (udiv x, c) -> alternate
Chris Lattnere9936d12005-10-22 18:50:15 +00001131 if (N1C && N1C->getValue() && !TLI.isIntDivCheap()) {
1132 SDOperand Op = BuildUDIV(N);
1133 if (Op.Val) return Op;
1134 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001135 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001136}
1137
Nate Begeman83e75ec2005-09-06 04:43:02 +00001138SDOperand DAGCombiner::visitSREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001139 SDOperand N0 = N->getOperand(0);
1140 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001141 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1142 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +00001143 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001144
1145 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001146 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001147 return DAG.getNode(ISD::SREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +00001148 // If we know the sign bits of both operands are zero, strength reduce to a
1149 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
1150 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001151 if (TLI.MaskedValueIsZero(N1, SignBit) &&
1152 TLI.MaskedValueIsZero(N0, SignBit))
Nate Begemana148d982006-01-18 22:35:16 +00001153 return DAG.getNode(ISD::UREM, VT, N0, N1);
Chris Lattner26d29902006-10-12 20:58:32 +00001154
1155 // Unconditionally lower X%C -> X-X/C*C. This allows the X/C logic to hack on
1156 // the remainder operation.
1157 if (N1C && !N1C->isNullValue()) {
1158 SDOperand Div = DAG.getNode(ISD::SDIV, VT, N0, N1);
1159 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Div, N1);
1160 SDOperand Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
1161 AddToWorkList(Div.Val);
1162 AddToWorkList(Mul.Val);
1163 return Sub;
1164 }
1165
Nate Begeman83e75ec2005-09-06 04:43:02 +00001166 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001167}
1168
Nate Begeman83e75ec2005-09-06 04:43:02 +00001169SDOperand DAGCombiner::visitUREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001170 SDOperand N0 = N->getOperand(0);
1171 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001172 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1173 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +00001174 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001175
1176 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001177 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001178 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +00001179 // fold (urem x, pow2) -> (and x, pow2-1)
1180 if (N1C && !N1C->isNullValue() && isPowerOf2_64(N1C->getValue()))
Nate Begemana148d982006-01-18 22:35:16 +00001181 return DAG.getNode(ISD::AND, VT, N0, DAG.getConstant(N1C->getValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +00001182 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
1183 if (N1.getOpcode() == ISD::SHL) {
1184 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
1185 if (isPowerOf2_64(SHC->getValue())) {
Nate Begemanbab92392006-02-05 08:07:24 +00001186 SDOperand Add = DAG.getNode(ISD::ADD, VT, N1,DAG.getConstant(~0ULL,VT));
Chris Lattner5750df92006-03-01 04:03:14 +00001187 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +00001188 return DAG.getNode(ISD::AND, VT, N0, Add);
1189 }
1190 }
1191 }
Chris Lattner26d29902006-10-12 20:58:32 +00001192
1193 // Unconditionally lower X%C -> X-X/C*C. This allows the X/C logic to hack on
1194 // the remainder operation.
1195 if (N1C && !N1C->isNullValue()) {
1196 SDOperand Div = DAG.getNode(ISD::UDIV, VT, N0, N1);
1197 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Div, N1);
1198 SDOperand Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
1199 AddToWorkList(Div.Val);
1200 AddToWorkList(Mul.Val);
1201 return Sub;
1202 }
1203
Nate Begeman83e75ec2005-09-06 04:43:02 +00001204 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001205}
1206
Nate Begeman83e75ec2005-09-06 04:43:02 +00001207SDOperand DAGCombiner::visitMULHS(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001208 SDOperand N0 = N->getOperand(0);
1209 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001210 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001211
1212 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001213 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001214 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001215 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001216 if (N1C && N1C->getValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001217 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
1218 DAG.getConstant(MVT::getSizeInBits(N0.getValueType())-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001219 TLI.getShiftAmountTy()));
1220 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001221}
1222
Nate Begeman83e75ec2005-09-06 04:43:02 +00001223SDOperand DAGCombiner::visitMULHU(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001224 SDOperand N0 = N->getOperand(0);
1225 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001226 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001227
1228 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001229 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001230 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001231 // fold (mulhu x, 1) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001232 if (N1C && N1C->getValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001233 return DAG.getConstant(0, N0.getValueType());
1234 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001235}
1236
Chris Lattner35e5c142006-05-05 05:51:50 +00001237/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
1238/// two operands of the same opcode, try to simplify it.
1239SDOperand DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
1240 SDOperand N0 = N->getOperand(0), N1 = N->getOperand(1);
1241 MVT::ValueType VT = N0.getValueType();
1242 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
1243
Chris Lattner540121f2006-05-05 06:31:05 +00001244 // For each of OP in AND/OR/XOR:
1245 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
1246 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
1247 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
Chris Lattner0d8dae72006-05-05 06:32:04 +00001248 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y))
Chris Lattner540121f2006-05-05 06:31:05 +00001249 if ((N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND||
Chris Lattner0d8dae72006-05-05 06:32:04 +00001250 N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::TRUNCATE) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001251 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1252 SDOperand ORNode = DAG.getNode(N->getOpcode(),
1253 N0.getOperand(0).getValueType(),
1254 N0.getOperand(0), N1.getOperand(0));
1255 AddToWorkList(ORNode.Val);
Chris Lattner540121f2006-05-05 06:31:05 +00001256 return DAG.getNode(N0.getOpcode(), VT, ORNode);
Chris Lattner35e5c142006-05-05 05:51:50 +00001257 }
1258
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001259 // For each of OP in SHL/SRL/SRA/AND...
1260 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
1261 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
1262 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
Chris Lattner35e5c142006-05-05 05:51:50 +00001263 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001264 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001265 N0.getOperand(1) == N1.getOperand(1)) {
1266 SDOperand ORNode = DAG.getNode(N->getOpcode(),
1267 N0.getOperand(0).getValueType(),
1268 N0.getOperand(0), N1.getOperand(0));
1269 AddToWorkList(ORNode.Val);
1270 return DAG.getNode(N0.getOpcode(), VT, ORNode, N0.getOperand(1));
1271 }
1272
1273 return SDOperand();
1274}
1275
Nate Begeman83e75ec2005-09-06 04:43:02 +00001276SDOperand DAGCombiner::visitAND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001277 SDOperand N0 = N->getOperand(0);
1278 SDOperand N1 = N->getOperand(1);
Nate Begemanfb7217b2006-02-17 19:54:08 +00001279 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001280 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1281 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001282 MVT::ValueType VT = N1.getValueType();
1283
1284 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001285 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001286 return DAG.getNode(ISD::AND, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001287 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001288 if (N0C && !N1C)
1289 return DAG.getNode(ISD::AND, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001290 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001291 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001292 return N0;
1293 // if (and x, c) is known to be zero, return 0
Nate Begeman368e18d2006-02-16 21:11:51 +00001294 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001295 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +00001296 // reassociate and
1297 SDOperand RAND = ReassociateOps(ISD::AND, N0, N1);
1298 if (RAND.Val != 0)
1299 return RAND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001300 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
Nate Begeman5dc7e862005-11-02 18:42:59 +00001301 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001302 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Nate Begeman646d7e22005-09-02 21:18:40 +00001303 if ((ORI->getValue() & N1C->getValue()) == N1C->getValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001304 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +00001305 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
1306 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Chris Lattner1ec05d12006-03-01 21:47:21 +00001307 unsigned InMask = MVT::getIntVTBitMask(N0.getOperand(0).getValueType());
Chris Lattner3603cd62006-02-02 07:17:31 +00001308 if (TLI.MaskedValueIsZero(N0.getOperand(0),
Chris Lattner1ec05d12006-03-01 21:47:21 +00001309 ~N1C->getValue() & InMask)) {
1310 SDOperand Zext = DAG.getNode(ISD::ZERO_EXTEND, N0.getValueType(),
1311 N0.getOperand(0));
1312
1313 // Replace uses of the AND with uses of the Zero extend node.
1314 CombineTo(N, Zext);
1315
Chris Lattner3603cd62006-02-02 07:17:31 +00001316 // We actually want to replace all uses of the any_extend with the
1317 // zero_extend, to avoid duplicating things. This will later cause this
1318 // AND to be folded.
Chris Lattner1ec05d12006-03-01 21:47:21 +00001319 CombineTo(N0.Val, Zext);
Chris Lattnerfedced72006-04-20 23:55:59 +00001320 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner3603cd62006-02-02 07:17:31 +00001321 }
1322 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001323 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
1324 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1325 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1326 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1327
1328 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1329 MVT::isInteger(LL.getValueType())) {
1330 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
1331 if (cast<ConstantSDNode>(LR)->getValue() == 0 && Op1 == ISD::SETEQ) {
1332 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001333 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001334 return DAG.getSetCC(VT, ORNode, LR, Op1);
1335 }
1336 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
1337 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
1338 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001339 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001340 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1341 }
1342 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
1343 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
1344 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001345 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001346 return DAG.getSetCC(VT, ORNode, LR, Op1);
1347 }
1348 }
1349 // canonicalize equivalent to ll == rl
1350 if (LL == RR && LR == RL) {
1351 Op1 = ISD::getSetCCSwappedOperands(Op1);
1352 std::swap(RL, RR);
1353 }
1354 if (LL == RL && LR == RR) {
1355 bool isInteger = MVT::isInteger(LL.getValueType());
1356 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
1357 if (Result != ISD::SETCC_INVALID)
1358 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1359 }
1360 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001361
1362 // Simplify: and (op x...), (op y...) -> (op (and x, y))
1363 if (N0.getOpcode() == N1.getOpcode()) {
1364 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1365 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001366 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001367
Nate Begemande996292006-02-03 22:24:05 +00001368 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
1369 // fold (and (sra)) -> (and (srl)) when possible.
Chris Lattner6ea2dee2006-03-25 22:19:00 +00001370 if (!MVT::isVector(VT) &&
1371 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001372 return SDOperand(N, 0);
Nate Begemanded49632005-10-13 03:11:28 +00001373 // fold (zext_inreg (extload x)) -> (zextload x)
Evan Cheng83060c52007-03-07 08:07:03 +00001374 if (ISD::isEXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val)) {
Evan Cheng466685d2006-10-09 20:57:25 +00001375 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00001376 MVT::ValueType EVT = LN0->getLoadedVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001377 // If we zero all the possible extended bits, then we can turn this into
1378 // a zextload if we are running before legalize or the operation is legal.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001379 if (TLI.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Evan Chengc5484282006-10-04 00:56:09 +00001380 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00001381 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
1382 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00001383 LN0->getSrcValueOffset(), EVT,
1384 LN0->isVolatile(),
1385 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00001386 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001387 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001388 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001389 }
1390 }
1391 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Evan Cheng83060c52007-03-07 08:07:03 +00001392 if (ISD::isSEXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val) &&
1393 N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001394 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00001395 MVT::ValueType EVT = LN0->getLoadedVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001396 // If we zero all the possible extended bits, then we can turn this into
1397 // a zextload if we are running before legalize or the operation is legal.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001398 if (TLI.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Evan Chengc5484282006-10-04 00:56:09 +00001399 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00001400 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
1401 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00001402 LN0->getSrcValueOffset(), EVT,
1403 LN0->isVolatile(),
1404 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00001405 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001406 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001407 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001408 }
1409 }
Chris Lattner15045b62006-02-28 06:35:35 +00001410
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001411 // fold (and (load x), 255) -> (zextload x, i8)
1412 // fold (and (extload x, i16), 255) -> (zextload x, i8)
Evan Cheng466685d2006-10-09 20:57:25 +00001413 if (N1C && N0.getOpcode() == ISD::LOAD) {
1414 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
1415 if (LN0->getExtensionType() != ISD::SEXTLOAD &&
Evan Cheng83060c52007-03-07 08:07:03 +00001416 LN0->getAddressingMode() == ISD::UNINDEXED &&
Evan Cheng466685d2006-10-09 20:57:25 +00001417 N0.hasOneUse()) {
1418 MVT::ValueType EVT, LoadedVT;
1419 if (N1C->getValue() == 255)
1420 EVT = MVT::i8;
1421 else if (N1C->getValue() == 65535)
1422 EVT = MVT::i16;
1423 else if (N1C->getValue() == ~0U)
1424 EVT = MVT::i32;
1425 else
1426 EVT = MVT::Other;
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001427
Evan Cheng2e49f092006-10-11 07:10:22 +00001428 LoadedVT = LN0->getLoadedVT();
Evan Cheng466685d2006-10-09 20:57:25 +00001429 if (EVT != MVT::Other && LoadedVT > EVT &&
1430 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
1431 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
1432 // For big endian targets, we need to add an offset to the pointer to
1433 // load the correct bytes. For little endian systems, we merely need to
1434 // read fewer bytes from the same pointer.
1435 unsigned PtrOff =
1436 (MVT::getSizeInBits(LoadedVT) - MVT::getSizeInBits(EVT)) / 8;
1437 SDOperand NewPtr = LN0->getBasePtr();
1438 if (!TLI.isLittleEndian())
1439 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
1440 DAG.getConstant(PtrOff, PtrType));
1441 AddToWorkList(NewPtr.Val);
1442 SDOperand Load =
1443 DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00001444 LN0->getSrcValue(), LN0->getSrcValueOffset(), EVT,
1445 LN0->isVolatile(), LN0->getAlignment());
Evan Cheng466685d2006-10-09 20:57:25 +00001446 AddToWorkList(N);
1447 CombineTo(N0.Val, Load, Load.getValue(1));
1448 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1449 }
Chris Lattner15045b62006-02-28 06:35:35 +00001450 }
1451 }
1452
Nate Begeman83e75ec2005-09-06 04:43:02 +00001453 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001454}
1455
Nate Begeman83e75ec2005-09-06 04:43:02 +00001456SDOperand DAGCombiner::visitOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001457 SDOperand N0 = N->getOperand(0);
1458 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001459 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001460 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1461 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001462 MVT::ValueType VT = N1.getValueType();
1463 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001464
1465 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001466 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001467 return DAG.getNode(ISD::OR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001468 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001469 if (N0C && !N1C)
1470 return DAG.getNode(ISD::OR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001471 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001472 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001473 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001474 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001475 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001476 return N1;
1477 // fold (or x, c) -> c iff (x & ~c) == 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001478 if (N1C &&
1479 TLI.MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits))))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001480 return N1;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001481 // reassociate or
1482 SDOperand ROR = ReassociateOps(ISD::OR, N0, N1);
1483 if (ROR.Val != 0)
1484 return ROR;
1485 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
1486 if (N1C && N0.getOpcode() == ISD::AND && N0.Val->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00001487 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00001488 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
1489 return DAG.getNode(ISD::AND, VT, DAG.getNode(ISD::OR, VT, N0.getOperand(0),
1490 N1),
1491 DAG.getConstant(N1C->getValue() | C1->getValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00001492 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001493 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
1494 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1495 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1496 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1497
1498 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1499 MVT::isInteger(LL.getValueType())) {
1500 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
1501 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
1502 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
1503 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
1504 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001505 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001506 return DAG.getSetCC(VT, ORNode, LR, Op1);
1507 }
1508 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
1509 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
1510 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
1511 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
1512 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001513 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001514 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1515 }
1516 }
1517 // canonicalize equivalent to ll == rl
1518 if (LL == RR && LR == RL) {
1519 Op1 = ISD::getSetCCSwappedOperands(Op1);
1520 std::swap(RL, RR);
1521 }
1522 if (LL == RL && LR == RR) {
1523 bool isInteger = MVT::isInteger(LL.getValueType());
1524 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
1525 if (Result != ISD::SETCC_INVALID)
1526 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1527 }
1528 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001529
1530 // Simplify: or (op x...), (op y...) -> (op (or x, y))
1531 if (N0.getOpcode() == N1.getOpcode()) {
1532 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1533 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001534 }
Chris Lattner516b9622006-09-14 20:50:57 +00001535
Chris Lattner1ec72732006-09-14 21:11:37 +00001536 // (X & C1) | (Y & C2) -> (X|Y) & C3 if possible.
1537 if (N0.getOpcode() == ISD::AND &&
1538 N1.getOpcode() == ISD::AND &&
1539 N0.getOperand(1).getOpcode() == ISD::Constant &&
1540 N1.getOperand(1).getOpcode() == ISD::Constant &&
1541 // Don't increase # computations.
1542 (N0.Val->hasOneUse() || N1.Val->hasOneUse())) {
1543 // We can only do this xform if we know that bits from X that are set in C2
1544 // but not in C1 are already zero. Likewise for Y.
1545 uint64_t LHSMask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1546 uint64_t RHSMask = cast<ConstantSDNode>(N1.getOperand(1))->getValue();
1547
1548 if (TLI.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) &&
1549 TLI.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) {
1550 SDOperand X =DAG.getNode(ISD::OR, VT, N0.getOperand(0), N1.getOperand(0));
1551 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(LHSMask|RHSMask, VT));
1552 }
1553 }
1554
1555
Chris Lattner516b9622006-09-14 20:50:57 +00001556 // See if this is some rotate idiom.
1557 if (SDNode *Rot = MatchRotate(N0, N1))
1558 return SDOperand(Rot, 0);
Chris Lattner35e5c142006-05-05 05:51:50 +00001559
Nate Begeman83e75ec2005-09-06 04:43:02 +00001560 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001561}
1562
Chris Lattner516b9622006-09-14 20:50:57 +00001563
1564/// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present.
1565static bool MatchRotateHalf(SDOperand Op, SDOperand &Shift, SDOperand &Mask) {
1566 if (Op.getOpcode() == ISD::AND) {
Reid Spencer3ed469c2006-11-02 20:25:50 +00001567 if (isa<ConstantSDNode>(Op.getOperand(1))) {
Chris Lattner516b9622006-09-14 20:50:57 +00001568 Mask = Op.getOperand(1);
1569 Op = Op.getOperand(0);
1570 } else {
1571 return false;
1572 }
1573 }
1574
1575 if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) {
1576 Shift = Op;
1577 return true;
1578 }
1579 return false;
1580}
1581
1582
1583// MatchRotate - Handle an 'or' of two operands. If this is one of the many
1584// idioms for rotate, and if the target supports rotation instructions, generate
1585// a rot[lr].
1586SDNode *DAGCombiner::MatchRotate(SDOperand LHS, SDOperand RHS) {
1587 // Must be a legal type. Expanded an promoted things won't work with rotates.
1588 MVT::ValueType VT = LHS.getValueType();
1589 if (!TLI.isTypeLegal(VT)) return 0;
1590
1591 // The target must have at least one rotate flavor.
1592 bool HasROTL = TLI.isOperationLegal(ISD::ROTL, VT);
1593 bool HasROTR = TLI.isOperationLegal(ISD::ROTR, VT);
1594 if (!HasROTL && !HasROTR) return 0;
1595
1596 // Match "(X shl/srl V1) & V2" where V2 may not be present.
1597 SDOperand LHSShift; // The shift.
1598 SDOperand LHSMask; // AND value if any.
1599 if (!MatchRotateHalf(LHS, LHSShift, LHSMask))
1600 return 0; // Not part of a rotate.
1601
1602 SDOperand RHSShift; // The shift.
1603 SDOperand RHSMask; // AND value if any.
1604 if (!MatchRotateHalf(RHS, RHSShift, RHSMask))
1605 return 0; // Not part of a rotate.
1606
1607 if (LHSShift.getOperand(0) != RHSShift.getOperand(0))
1608 return 0; // Not shifting the same value.
1609
1610 if (LHSShift.getOpcode() == RHSShift.getOpcode())
1611 return 0; // Shifts must disagree.
1612
1613 // Canonicalize shl to left side in a shl/srl pair.
1614 if (RHSShift.getOpcode() == ISD::SHL) {
1615 std::swap(LHS, RHS);
1616 std::swap(LHSShift, RHSShift);
1617 std::swap(LHSMask , RHSMask );
1618 }
1619
1620 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Scott Michelc9dc1142007-04-02 21:36:32 +00001621 SDOperand LHSShiftArg = LHSShift.getOperand(0);
1622 SDOperand LHSShiftAmt = LHSShift.getOperand(1);
1623 SDOperand RHSShiftAmt = RHSShift.getOperand(1);
Chris Lattner516b9622006-09-14 20:50:57 +00001624
1625 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
1626 // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2)
Scott Michelc9dc1142007-04-02 21:36:32 +00001627 if (LHSShiftAmt.getOpcode() == ISD::Constant &&
1628 RHSShiftAmt.getOpcode() == ISD::Constant) {
1629 uint64_t LShVal = cast<ConstantSDNode>(LHSShiftAmt)->getValue();
1630 uint64_t RShVal = cast<ConstantSDNode>(RHSShiftAmt)->getValue();
Chris Lattner516b9622006-09-14 20:50:57 +00001631 if ((LShVal + RShVal) != OpSizeInBits)
1632 return 0;
1633
1634 SDOperand Rot;
1635 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00001636 Rot = DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00001637 else
Scott Michelc9dc1142007-04-02 21:36:32 +00001638 Rot = DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00001639
1640 // If there is an AND of either shifted operand, apply it to the result.
1641 if (LHSMask.Val || RHSMask.Val) {
1642 uint64_t Mask = MVT::getIntVTBitMask(VT);
1643
1644 if (LHSMask.Val) {
1645 uint64_t RHSBits = (1ULL << LShVal)-1;
1646 Mask &= cast<ConstantSDNode>(LHSMask)->getValue() | RHSBits;
1647 }
1648 if (RHSMask.Val) {
1649 uint64_t LHSBits = ~((1ULL << (OpSizeInBits-RShVal))-1);
1650 Mask &= cast<ConstantSDNode>(RHSMask)->getValue() | LHSBits;
1651 }
1652
1653 Rot = DAG.getNode(ISD::AND, VT, Rot, DAG.getConstant(Mask, VT));
1654 }
1655
1656 return Rot.Val;
1657 }
1658
1659 // If there is a mask here, and we have a variable shift, we can't be sure
1660 // that we're masking out the right stuff.
1661 if (LHSMask.Val || RHSMask.Val)
1662 return 0;
1663
1664 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
1665 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotr x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00001666 if (RHSShiftAmt.getOpcode() == ISD::SUB &&
1667 LHSShiftAmt == RHSShiftAmt.getOperand(1)) {
Chris Lattner516b9622006-09-14 20:50:57 +00001668 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00001669 dyn_cast<ConstantSDNode>(RHSShiftAmt.getOperand(0))) {
Chris Lattner516b9622006-09-14 20:50:57 +00001670 if (SUBC->getValue() == OpSizeInBits)
1671 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00001672 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
Chris Lattner516b9622006-09-14 20:50:57 +00001673 else
Scott Michelc9dc1142007-04-02 21:36:32 +00001674 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).Val;
Chris Lattner516b9622006-09-14 20:50:57 +00001675 }
1676 }
1677
1678 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
1679 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotl x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00001680 if (LHSShiftAmt.getOpcode() == ISD::SUB &&
1681 RHSShiftAmt == LHSShiftAmt.getOperand(1)) {
Chris Lattner516b9622006-09-14 20:50:57 +00001682 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00001683 dyn_cast<ConstantSDNode>(LHSShiftAmt.getOperand(0))) {
Chris Lattner516b9622006-09-14 20:50:57 +00001684 if (SUBC->getValue() == OpSizeInBits)
1685 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00001686 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
Chris Lattner516b9622006-09-14 20:50:57 +00001687 else
Scott Michelc9dc1142007-04-02 21:36:32 +00001688 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).Val;
1689 }
1690 }
1691
1692 // Look for sign/zext/any-extended cases:
1693 if ((LHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
1694 || LHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
1695 || LHSShiftAmt.getOpcode() == ISD::ANY_EXTEND) &&
1696 (RHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
1697 || RHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
1698 || RHSShiftAmt.getOpcode() == ISD::ANY_EXTEND)) {
1699 SDOperand LExtOp0 = LHSShiftAmt.getOperand(0);
1700 SDOperand RExtOp0 = RHSShiftAmt.getOperand(0);
1701 if (RExtOp0.getOpcode() == ISD::SUB &&
1702 RExtOp0.getOperand(1) == LExtOp0) {
1703 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
1704 // (rotr x, y)
1705 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
1706 // (rotl x, (sub 32, y))
1707 if (ConstantSDNode *SUBC = cast<ConstantSDNode>(RExtOp0.getOperand(0))) {
1708 if (SUBC->getValue() == OpSizeInBits) {
1709 if (HasROTL)
1710 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
1711 else
1712 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).Val;
1713 }
1714 }
1715 } else if (LExtOp0.getOpcode() == ISD::SUB &&
1716 RExtOp0 == LExtOp0.getOperand(1)) {
1717 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext r))) ->
1718 // (rotl x, y)
1719 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext r))) ->
1720 // (rotr x, (sub 32, y))
1721 if (ConstantSDNode *SUBC = cast<ConstantSDNode>(LExtOp0.getOperand(0))) {
1722 if (SUBC->getValue() == OpSizeInBits) {
1723 if (HasROTL)
1724 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, RHSShiftAmt).Val;
1725 else
1726 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
1727 }
1728 }
Chris Lattner516b9622006-09-14 20:50:57 +00001729 }
1730 }
1731
1732 return 0;
1733}
1734
1735
Nate Begeman83e75ec2005-09-06 04:43:02 +00001736SDOperand DAGCombiner::visitXOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001737 SDOperand N0 = N->getOperand(0);
1738 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001739 SDOperand LHS, RHS, CC;
1740 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1741 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001742 MVT::ValueType VT = N0.getValueType();
1743
1744 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001745 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001746 return DAG.getNode(ISD::XOR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001747 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001748 if (N0C && !N1C)
1749 return DAG.getNode(ISD::XOR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001750 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001751 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001752 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001753 // reassociate xor
1754 SDOperand RXOR = ReassociateOps(ISD::XOR, N0, N1);
1755 if (RXOR.Val != 0)
1756 return RXOR;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001757 // fold !(x cc y) -> (x !cc y)
Nate Begeman646d7e22005-09-02 21:18:40 +00001758 if (N1C && N1C->getValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
1759 bool isInt = MVT::isInteger(LHS.getValueType());
1760 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
1761 isInt);
1762 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001763 return DAG.getSetCC(VT, LHS, RHS, NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00001764 if (N0.getOpcode() == ISD::SELECT_CC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001765 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00001766 assert(0 && "Unhandled SetCC Equivalent!");
1767 abort();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001768 }
Nate Begeman99801192005-09-07 23:25:52 +00001769 // fold !(x or y) -> (!x and !y) iff x or y are setcc
Chris Lattner734c91d2006-11-10 21:37:15 +00001770 if (N1C && N1C->getValue() == 1 && VT == MVT::i1 &&
Nate Begeman99801192005-09-07 23:25:52 +00001771 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001772 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00001773 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
1774 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001775 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1776 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00001777 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00001778 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001779 }
1780 }
Nate Begeman99801192005-09-07 23:25:52 +00001781 // fold !(x or y) -> (!x and !y) iff x or y are constants
1782 if (N1C && N1C->isAllOnesValue() &&
1783 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001784 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00001785 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
1786 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001787 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1788 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00001789 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00001790 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001791 }
1792 }
Nate Begeman223df222005-09-08 20:18:10 +00001793 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
1794 if (N1C && N0.getOpcode() == ISD::XOR) {
1795 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
1796 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
1797 if (N00C)
1798 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
1799 DAG.getConstant(N1C->getValue()^N00C->getValue(), VT));
1800 if (N01C)
1801 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
1802 DAG.getConstant(N1C->getValue()^N01C->getValue(), VT));
1803 }
1804 // fold (xor x, x) -> 0
Chris Lattner4fbdd592006-03-28 19:11:05 +00001805 if (N0 == N1) {
1806 if (!MVT::isVector(VT)) {
1807 return DAG.getConstant(0, VT);
1808 } else if (!AfterLegalize || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) {
1809 // Produce a vector of zeros.
1810 SDOperand El = DAG.getConstant(0, MVT::getVectorBaseType(VT));
1811 std::vector<SDOperand> Ops(MVT::getVectorNumElements(VT), El);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001812 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4fbdd592006-03-28 19:11:05 +00001813 }
1814 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001815
1816 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
1817 if (N0.getOpcode() == N1.getOpcode()) {
1818 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1819 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001820 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001821
Chris Lattner3e104b12006-04-08 04:15:24 +00001822 // Simplify the expression using non-local knowledge.
1823 if (!MVT::isVector(VT) &&
1824 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001825 return SDOperand(N, 0);
Chris Lattner3e104b12006-04-08 04:15:24 +00001826
Nate Begeman83e75ec2005-09-06 04:43:02 +00001827 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001828}
1829
Nate Begeman83e75ec2005-09-06 04:43:02 +00001830SDOperand DAGCombiner::visitSHL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001831 SDOperand N0 = N->getOperand(0);
1832 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001833 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1834 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001835 MVT::ValueType VT = N0.getValueType();
1836 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1837
1838 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001839 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001840 return DAG.getNode(ISD::SHL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001841 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001842 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001843 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001844 // fold (shl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001845 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001846 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001847 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001848 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001849 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001850 // if (shl x, c) is known to be zero, return 0
Nate Begemanfb7217b2006-02-17 19:54:08 +00001851 if (TLI.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001852 return DAG.getConstant(0, VT);
Chris Lattner61a4c072007-04-18 03:06:49 +00001853 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001854 return SDOperand(N, 0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001855 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001856 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001857 N0.getOperand(1).getOpcode() == ISD::Constant) {
1858 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001859 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001860 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001861 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001862 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001863 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001864 }
1865 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
1866 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001867 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001868 N0.getOperand(1).getOpcode() == ISD::Constant) {
1869 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001870 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001871 SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
1872 DAG.getConstant(~0ULL << c1, VT));
1873 if (c2 > c1)
1874 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001875 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001876 else
Nate Begeman83e75ec2005-09-06 04:43:02 +00001877 return DAG.getNode(ISD::SRL, VT, Mask,
1878 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001879 }
1880 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001881 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +00001882 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001883 DAG.getConstant(~0ULL << N1C->getValue(), VT));
1884 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001885}
1886
Nate Begeman83e75ec2005-09-06 04:43:02 +00001887SDOperand DAGCombiner::visitSRA(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001888 SDOperand N0 = N->getOperand(0);
1889 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001890 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1891 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001892 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001893
1894 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001895 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001896 return DAG.getNode(ISD::SRA, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001897 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001898 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001899 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001900 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001901 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001902 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001903 // fold (sra x, c >= size(x)) -> undef
Nate Begemanfb7217b2006-02-17 19:54:08 +00001904 if (N1C && N1C->getValue() >= MVT::getSizeInBits(VT))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001905 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001906 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001907 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001908 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00001909 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
1910 // sext_inreg.
1911 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
1912 unsigned LowBits = MVT::getSizeInBits(VT) - (unsigned)N1C->getValue();
1913 MVT::ValueType EVT;
1914 switch (LowBits) {
1915 default: EVT = MVT::Other; break;
1916 case 1: EVT = MVT::i1; break;
1917 case 8: EVT = MVT::i8; break;
1918 case 16: EVT = MVT::i16; break;
1919 case 32: EVT = MVT::i32; break;
1920 }
1921 if (EVT > MVT::Other && TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, EVT))
1922 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
1923 DAG.getValueType(EVT));
1924 }
Chris Lattner71d9ebc2006-02-28 06:23:04 +00001925
1926 // fold (sra (sra x, c1), c2) -> (sra x, c1+c2)
1927 if (N1C && N0.getOpcode() == ISD::SRA) {
1928 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
1929 unsigned Sum = N1C->getValue() + C1->getValue();
1930 if (Sum >= MVT::getSizeInBits(VT)) Sum = MVT::getSizeInBits(VT)-1;
1931 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0),
1932 DAG.getConstant(Sum, N1C->getValueType(0)));
1933 }
1934 }
1935
Chris Lattnera8504462006-05-08 20:51:54 +00001936 // Simplify, based on bits shifted out of the LHS.
1937 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
1938 return SDOperand(N, 0);
1939
1940
Nate Begeman1d4d4142005-09-01 00:19:25 +00001941 // If the sign bit is known to be zero, switch this to a SRL.
Nate Begemanfb7217b2006-02-17 19:54:08 +00001942 if (TLI.MaskedValueIsZero(N0, MVT::getIntVTSignBit(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001943 return DAG.getNode(ISD::SRL, VT, N0, N1);
1944 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001945}
1946
Nate Begeman83e75ec2005-09-06 04:43:02 +00001947SDOperand DAGCombiner::visitSRL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001948 SDOperand N0 = N->getOperand(0);
1949 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001950 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1951 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001952 MVT::ValueType VT = N0.getValueType();
1953 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1954
1955 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001956 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001957 return DAG.getNode(ISD::SRL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001958 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001959 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001960 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001961 // fold (srl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001962 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001963 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001964 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001965 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001966 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001967 // if (srl x, c) is known to be zero, return 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001968 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001969 return DAG.getConstant(0, VT);
Chris Lattnerec06e9a2007-04-18 03:05:22 +00001970
Nate Begeman1d4d4142005-09-01 00:19:25 +00001971 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001972 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001973 N0.getOperand(1).getOpcode() == ISD::Constant) {
1974 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001975 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001976 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001977 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001978 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001979 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001980 }
Chris Lattner350bec02006-04-02 06:11:11 +00001981
Chris Lattner06afe072006-05-05 22:53:17 +00001982 // fold (srl (anyextend x), c) -> (anyextend (srl x, c))
1983 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
1984 // Shifting in all undef bits?
1985 MVT::ValueType SmallVT = N0.getOperand(0).getValueType();
1986 if (N1C->getValue() >= MVT::getSizeInBits(SmallVT))
1987 return DAG.getNode(ISD::UNDEF, VT);
1988
1989 SDOperand SmallShift = DAG.getNode(ISD::SRL, SmallVT, N0.getOperand(0), N1);
1990 AddToWorkList(SmallShift.Val);
1991 return DAG.getNode(ISD::ANY_EXTEND, VT, SmallShift);
1992 }
1993
Chris Lattner3657ffe2006-10-12 20:23:19 +00001994 // fold (srl (sra X, Y), 31) -> (srl X, 31). This srl only looks at the sign
1995 // bit, which is unmodified by sra.
1996 if (N1C && N1C->getValue()+1 == MVT::getSizeInBits(VT)) {
1997 if (N0.getOpcode() == ISD::SRA)
1998 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0), N1);
1999 }
2000
Chris Lattner350bec02006-04-02 06:11:11 +00002001 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
2002 if (N1C && N0.getOpcode() == ISD::CTLZ &&
2003 N1C->getValue() == Log2_32(MVT::getSizeInBits(VT))) {
2004 uint64_t KnownZero, KnownOne, Mask = MVT::getIntVTBitMask(VT);
2005 TLI.ComputeMaskedBits(N0.getOperand(0), Mask, KnownZero, KnownOne);
2006
2007 // If any of the input bits are KnownOne, then the input couldn't be all
2008 // zeros, thus the result of the srl will always be zero.
2009 if (KnownOne) return DAG.getConstant(0, VT);
2010
2011 // If all of the bits input the to ctlz node are known to be zero, then
2012 // the result of the ctlz is "32" and the result of the shift is one.
2013 uint64_t UnknownBits = ~KnownZero & Mask;
2014 if (UnknownBits == 0) return DAG.getConstant(1, VT);
2015
2016 // Otherwise, check to see if there is exactly one bit input to the ctlz.
2017 if ((UnknownBits & (UnknownBits-1)) == 0) {
2018 // Okay, we know that only that the single bit specified by UnknownBits
2019 // could be set on input to the CTLZ node. If this bit is set, the SRL
2020 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
2021 // to an SRL,XOR pair, which is likely to simplify more.
2022 unsigned ShAmt = CountTrailingZeros_64(UnknownBits);
2023 SDOperand Op = N0.getOperand(0);
2024 if (ShAmt) {
2025 Op = DAG.getNode(ISD::SRL, VT, Op,
2026 DAG.getConstant(ShAmt, TLI.getShiftAmountTy()));
2027 AddToWorkList(Op.Val);
2028 }
2029 return DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(1, VT));
2030 }
2031 }
Chris Lattner61a4c072007-04-18 03:06:49 +00002032
2033 // fold operands of srl based on knowledge that the low bits are not
2034 // demanded.
2035 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
2036 return SDOperand(N, 0);
2037
Nate Begeman83e75ec2005-09-06 04:43:02 +00002038 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002039}
2040
Nate Begeman83e75ec2005-09-06 04:43:02 +00002041SDOperand DAGCombiner::visitCTLZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002042 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00002043 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002044
2045 // fold (ctlz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002046 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002047 return DAG.getNode(ISD::CTLZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002048 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002049}
2050
Nate Begeman83e75ec2005-09-06 04:43:02 +00002051SDOperand DAGCombiner::visitCTTZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002052 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00002053 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002054
2055 // fold (cttz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002056 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002057 return DAG.getNode(ISD::CTTZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002058 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002059}
2060
Nate Begeman83e75ec2005-09-06 04:43:02 +00002061SDOperand DAGCombiner::visitCTPOP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002062 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00002063 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002064
2065 // fold (ctpop c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002066 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002067 return DAG.getNode(ISD::CTPOP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002068 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002069}
2070
Nate Begeman452d7be2005-09-16 00:54:12 +00002071SDOperand DAGCombiner::visitSELECT(SDNode *N) {
2072 SDOperand N0 = N->getOperand(0);
2073 SDOperand N1 = N->getOperand(1);
2074 SDOperand N2 = N->getOperand(2);
2075 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2076 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2077 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
2078 MVT::ValueType VT = N->getValueType(0);
Nate Begeman44728a72005-09-19 22:34:01 +00002079
Nate Begeman452d7be2005-09-16 00:54:12 +00002080 // fold select C, X, X -> X
2081 if (N1 == N2)
2082 return N1;
2083 // fold select true, X, Y -> X
2084 if (N0C && !N0C->isNullValue())
2085 return N1;
2086 // fold select false, X, Y -> Y
2087 if (N0C && N0C->isNullValue())
2088 return N2;
2089 // fold select C, 1, X -> C | X
Nate Begeman44728a72005-09-19 22:34:01 +00002090 if (MVT::i1 == VT && N1C && N1C->getValue() == 1)
Nate Begeman452d7be2005-09-16 00:54:12 +00002091 return DAG.getNode(ISD::OR, VT, N0, N2);
2092 // fold select C, 0, X -> ~C & X
2093 // FIXME: this should check for C type == X type, not i1?
2094 if (MVT::i1 == VT && N1C && N1C->isNullValue()) {
2095 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00002096 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00002097 return DAG.getNode(ISD::AND, VT, XORNode, N2);
2098 }
2099 // fold select C, X, 1 -> ~C | X
Nate Begeman44728a72005-09-19 22:34:01 +00002100 if (MVT::i1 == VT && N2C && N2C->getValue() == 1) {
Nate Begeman452d7be2005-09-16 00:54:12 +00002101 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00002102 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00002103 return DAG.getNode(ISD::OR, VT, XORNode, N1);
2104 }
2105 // fold select C, X, 0 -> C & X
2106 // FIXME: this should check for C type == X type, not i1?
2107 if (MVT::i1 == VT && N2C && N2C->isNullValue())
2108 return DAG.getNode(ISD::AND, VT, N0, N1);
2109 // fold X ? X : Y --> X ? 1 : Y --> X | Y
2110 if (MVT::i1 == VT && N0 == N1)
2111 return DAG.getNode(ISD::OR, VT, N0, N2);
2112 // fold X ? Y : X --> X ? Y : 0 --> X & Y
2113 if (MVT::i1 == VT && N0 == N2)
2114 return DAG.getNode(ISD::AND, VT, N0, N1);
Chris Lattner729c6d12006-05-27 00:43:02 +00002115
Chris Lattner40c62d52005-10-18 06:04:22 +00002116 // If we can fold this based on the true/false value, do so.
2117 if (SimplifySelectOps(N, N1, N2))
Chris Lattner729c6d12006-05-27 00:43:02 +00002118 return SDOperand(N, 0); // Don't revisit N.
2119
Nate Begeman44728a72005-09-19 22:34:01 +00002120 // fold selects based on a setcc into other things, such as min/max/abs
2121 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman750ac1b2006-02-01 07:19:44 +00002122 // FIXME:
2123 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
2124 // having to say they don't support SELECT_CC on every type the DAG knows
2125 // about, since there is no way to mark an opcode illegal at all value types
2126 if (TLI.isOperationLegal(ISD::SELECT_CC, MVT::Other))
2127 return DAG.getNode(ISD::SELECT_CC, VT, N0.getOperand(0), N0.getOperand(1),
2128 N1, N2, N0.getOperand(2));
2129 else
2130 return SimplifySelect(N0, N1, N2);
Nate Begeman452d7be2005-09-16 00:54:12 +00002131 return SDOperand();
2132}
2133
2134SDOperand DAGCombiner::visitSELECT_CC(SDNode *N) {
Nate Begeman44728a72005-09-19 22:34:01 +00002135 SDOperand N0 = N->getOperand(0);
2136 SDOperand N1 = N->getOperand(1);
2137 SDOperand N2 = N->getOperand(2);
2138 SDOperand N3 = N->getOperand(3);
2139 SDOperand N4 = N->getOperand(4);
Nate Begeman44728a72005-09-19 22:34:01 +00002140 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
2141
Nate Begeman44728a72005-09-19 22:34:01 +00002142 // fold select_cc lhs, rhs, x, x, cc -> x
2143 if (N2 == N3)
2144 return N2;
Chris Lattner40c62d52005-10-18 06:04:22 +00002145
Chris Lattner5f42a242006-09-20 06:19:26 +00002146 // Determine if the condition we're dealing with is constant
2147 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
Chris Lattner30f73e72006-10-14 03:52:46 +00002148 if (SCC.Val) AddToWorkList(SCC.Val);
Chris Lattner5f42a242006-09-20 06:19:26 +00002149
2150 if (ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val)) {
2151 if (SCCC->getValue())
2152 return N2; // cond always true -> true val
2153 else
2154 return N3; // cond always false -> false val
2155 }
2156
2157 // Fold to a simpler select_cc
2158 if (SCC.Val && SCC.getOpcode() == ISD::SETCC)
2159 return DAG.getNode(ISD::SELECT_CC, N2.getValueType(),
2160 SCC.getOperand(0), SCC.getOperand(1), N2, N3,
2161 SCC.getOperand(2));
2162
Chris Lattner40c62d52005-10-18 06:04:22 +00002163 // If we can fold this based on the true/false value, do so.
2164 if (SimplifySelectOps(N, N2, N3))
Chris Lattner729c6d12006-05-27 00:43:02 +00002165 return SDOperand(N, 0); // Don't revisit N.
Chris Lattner40c62d52005-10-18 06:04:22 +00002166
Nate Begeman44728a72005-09-19 22:34:01 +00002167 // fold select_cc into other things, such as min/max/abs
2168 return SimplifySelectCC(N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00002169}
2170
2171SDOperand DAGCombiner::visitSETCC(SDNode *N) {
2172 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
2173 cast<CondCodeSDNode>(N->getOperand(2))->get());
2174}
2175
Nate Begeman83e75ec2005-09-06 04:43:02 +00002176SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002177 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002178 MVT::ValueType VT = N->getValueType(0);
2179
Nate Begeman1d4d4142005-09-01 00:19:25 +00002180 // fold (sext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00002181 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002182 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0);
Chris Lattner310b5782006-05-06 23:06:26 +00002183
Nate Begeman1d4d4142005-09-01 00:19:25 +00002184 // fold (sext (sext x)) -> (sext x)
Chris Lattner310b5782006-05-06 23:06:26 +00002185 // fold (sext (aext x)) -> (sext x)
2186 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002187 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
Chris Lattner310b5782006-05-06 23:06:26 +00002188
Evan Chengc88138f2007-03-22 01:54:19 +00002189 // fold (sext (truncate (load x))) -> (sext (smaller load x))
2190 // fold (sext (truncate (srl (load x), c))) -> (sext (smaller load (x+c/n)))
Chris Lattner22558872007-02-26 03:13:59 +00002191 if (N0.getOpcode() == ISD::TRUNCATE) {
Evan Chengc88138f2007-03-22 01:54:19 +00002192 SDOperand NarrowLoad = ReduceLoadWidth(N0.Val);
Evan Cheng0b063de2007-03-23 02:16:52 +00002193 if (NarrowLoad.Val) {
2194 if (NarrowLoad.Val != N0.Val)
2195 CombineTo(N0.Val, NarrowLoad);
2196 return DAG.getNode(ISD::SIGN_EXTEND, VT, NarrowLoad);
2197 }
Evan Chengc88138f2007-03-22 01:54:19 +00002198 }
2199
2200 // See if the value being truncated is already sign extended. If so, just
2201 // eliminate the trunc/sext pair.
2202 if (N0.getOpcode() == ISD::TRUNCATE) {
Chris Lattner6007b842006-09-21 06:00:20 +00002203 SDOperand Op = N0.getOperand(0);
Chris Lattner22558872007-02-26 03:13:59 +00002204 unsigned OpBits = MVT::getSizeInBits(Op.getValueType());
2205 unsigned MidBits = MVT::getSizeInBits(N0.getValueType());
2206 unsigned DestBits = MVT::getSizeInBits(VT);
2207 unsigned NumSignBits = TLI.ComputeNumSignBits(Op);
2208
2209 if (OpBits == DestBits) {
2210 // Op is i32, Mid is i8, and Dest is i32. If Op has more than 24 sign
2211 // bits, it is already ready.
2212 if (NumSignBits > DestBits-MidBits)
2213 return Op;
2214 } else if (OpBits < DestBits) {
2215 // Op is i32, Mid is i8, and Dest is i64. If Op has more than 24 sign
2216 // bits, just sext from i32.
2217 if (NumSignBits > OpBits-MidBits)
2218 return DAG.getNode(ISD::SIGN_EXTEND, VT, Op);
2219 } else {
2220 // Op is i64, Mid is i8, and Dest is i32. If Op has more than 56 sign
2221 // bits, just truncate to i32.
2222 if (NumSignBits > OpBits-MidBits)
2223 return DAG.getNode(ISD::TRUNCATE, VT, Op);
Chris Lattner6007b842006-09-21 06:00:20 +00002224 }
Chris Lattner22558872007-02-26 03:13:59 +00002225
2226 // fold (sext (truncate x)) -> (sextinreg x).
2227 if (!AfterLegalize || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG,
2228 N0.getValueType())) {
2229 if (Op.getValueType() < VT)
2230 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
2231 else if (Op.getValueType() > VT)
2232 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
2233 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, Op,
2234 DAG.getValueType(N0.getValueType()));
2235 }
Chris Lattner6007b842006-09-21 06:00:20 +00002236 }
Chris Lattner310b5782006-05-06 23:06:26 +00002237
Evan Cheng110dec22005-12-14 02:19:23 +00002238 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00002239 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00002240 (!AfterLegalize||TLI.isLoadXLegal(ISD::SEXTLOAD, N0.getValueType()))){
Evan Cheng466685d2006-10-09 20:57:25 +00002241 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2242 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2243 LN0->getBasePtr(), LN0->getSrcValue(),
2244 LN0->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002245 N0.getValueType(),
2246 LN0->isVolatile());
Chris Lattnerd4771842005-12-14 19:25:30 +00002247 CombineTo(N, ExtLoad);
Chris Lattnerf9884052005-10-13 21:52:31 +00002248 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2249 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002250 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begeman3df4d522005-10-12 20:40:40 +00002251 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002252
2253 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
2254 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00002255 if ((ISD::isSEXTLoad(N0.Val) || ISD::isEXTLoad(N0.Val)) &&
2256 ISD::isUNINDEXEDLoad(N0.Val) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002257 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00002258 MVT::ValueType EVT = LN0->getLoadedVT();
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00002259 if (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT)) {
2260 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2261 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002262 LN0->getSrcValueOffset(), EVT,
2263 LN0->isVolatile(),
2264 LN0->getAlignment());
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00002265 CombineTo(N, ExtLoad);
2266 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2267 ExtLoad.getValue(1));
2268 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2269 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002270 }
2271
Chris Lattner20a35c32007-04-11 05:32:27 +00002272 // sext(setcc x,y,cc) -> select_cc x, y, -1, 0, cc
2273 if (N0.getOpcode() == ISD::SETCC) {
2274 SDOperand SCC =
Chris Lattner1eba01e2007-04-11 06:50:51 +00002275 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
2276 DAG.getConstant(~0ULL, VT), DAG.getConstant(0, VT),
2277 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
2278 if (SCC.Val) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00002279 }
2280
Nate Begeman83e75ec2005-09-06 04:43:02 +00002281 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002282}
2283
Nate Begeman83e75ec2005-09-06 04:43:02 +00002284SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002285 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002286 MVT::ValueType VT = N->getValueType(0);
2287
Nate Begeman1d4d4142005-09-01 00:19:25 +00002288 // fold (zext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00002289 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002290 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002291 // fold (zext (zext x)) -> (zext x)
Chris Lattner310b5782006-05-06 23:06:26 +00002292 // fold (zext (aext x)) -> (zext x)
2293 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002294 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
Chris Lattner6007b842006-09-21 06:00:20 +00002295
Evan Chengc88138f2007-03-22 01:54:19 +00002296 // fold (zext (truncate (load x))) -> (zext (smaller load x))
2297 // fold (zext (truncate (srl (load x), c))) -> (zext (small load (x+c/n)))
Dale Johannesen2041a0e2007-03-30 21:38:07 +00002298 if (N0.getOpcode() == ISD::TRUNCATE) {
Evan Chengc88138f2007-03-22 01:54:19 +00002299 SDOperand NarrowLoad = ReduceLoadWidth(N0.Val);
Evan Cheng0b063de2007-03-23 02:16:52 +00002300 if (NarrowLoad.Val) {
2301 if (NarrowLoad.Val != N0.Val)
2302 CombineTo(N0.Val, NarrowLoad);
2303 return DAG.getNode(ISD::ZERO_EXTEND, VT, NarrowLoad);
2304 }
Evan Chengc88138f2007-03-22 01:54:19 +00002305 }
2306
Chris Lattner6007b842006-09-21 06:00:20 +00002307 // fold (zext (truncate x)) -> (and x, mask)
2308 if (N0.getOpcode() == ISD::TRUNCATE &&
2309 (!AfterLegalize || TLI.isOperationLegal(ISD::AND, VT))) {
2310 SDOperand Op = N0.getOperand(0);
2311 if (Op.getValueType() < VT) {
2312 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
2313 } else if (Op.getValueType() > VT) {
2314 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
2315 }
2316 return DAG.getZeroExtendInReg(Op, N0.getValueType());
2317 }
2318
Chris Lattner111c2282006-09-21 06:14:31 +00002319 // fold (zext (and (trunc x), cst)) -> (and x, cst).
2320 if (N0.getOpcode() == ISD::AND &&
2321 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
2322 N0.getOperand(1).getOpcode() == ISD::Constant) {
2323 SDOperand X = N0.getOperand(0).getOperand(0);
2324 if (X.getValueType() < VT) {
2325 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
2326 } else if (X.getValueType() > VT) {
2327 X = DAG.getNode(ISD::TRUNCATE, VT, X);
2328 }
2329 uint64_t Mask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
2330 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
2331 }
2332
Evan Cheng110dec22005-12-14 02:19:23 +00002333 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00002334 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00002335 (!AfterLegalize||TLI.isLoadXLegal(ISD::ZEXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00002336 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2337 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
2338 LN0->getBasePtr(), LN0->getSrcValue(),
2339 LN0->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002340 N0.getValueType(),
2341 LN0->isVolatile(),
2342 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00002343 CombineTo(N, ExtLoad);
Evan Cheng110dec22005-12-14 02:19:23 +00002344 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2345 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002346 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng110dec22005-12-14 02:19:23 +00002347 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002348
2349 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
2350 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00002351 if ((ISD::isZEXTLoad(N0.Val) || ISD::isEXTLoad(N0.Val)) &&
2352 ISD::isUNINDEXEDLoad(N0.Val) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002353 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00002354 MVT::ValueType EVT = LN0->getLoadedVT();
Evan Cheng466685d2006-10-09 20:57:25 +00002355 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
2356 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002357 LN0->getSrcValueOffset(), EVT,
2358 LN0->isVolatile(),
2359 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00002360 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002361 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2362 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002363 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002364 }
Chris Lattner20a35c32007-04-11 05:32:27 +00002365
2366 // zext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
2367 if (N0.getOpcode() == ISD::SETCC) {
2368 SDOperand SCC =
2369 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
2370 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattner1eba01e2007-04-11 06:50:51 +00002371 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
2372 if (SCC.Val) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00002373 }
2374
Nate Begeman83e75ec2005-09-06 04:43:02 +00002375 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002376}
2377
Chris Lattner5ffc0662006-05-05 05:58:59 +00002378SDOperand DAGCombiner::visitANY_EXTEND(SDNode *N) {
2379 SDOperand N0 = N->getOperand(0);
Chris Lattner5ffc0662006-05-05 05:58:59 +00002380 MVT::ValueType VT = N->getValueType(0);
2381
2382 // fold (aext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00002383 if (isa<ConstantSDNode>(N0))
Chris Lattner5ffc0662006-05-05 05:58:59 +00002384 return DAG.getNode(ISD::ANY_EXTEND, VT, N0);
2385 // fold (aext (aext x)) -> (aext x)
2386 // fold (aext (zext x)) -> (zext x)
2387 // fold (aext (sext x)) -> (sext x)
2388 if (N0.getOpcode() == ISD::ANY_EXTEND ||
2389 N0.getOpcode() == ISD::ZERO_EXTEND ||
2390 N0.getOpcode() == ISD::SIGN_EXTEND)
2391 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
2392
Evan Chengc88138f2007-03-22 01:54:19 +00002393 // fold (aext (truncate (load x))) -> (aext (smaller load x))
2394 // fold (aext (truncate (srl (load x), c))) -> (aext (small load (x+c/n)))
2395 if (N0.getOpcode() == ISD::TRUNCATE) {
2396 SDOperand NarrowLoad = ReduceLoadWidth(N0.Val);
Evan Cheng0b063de2007-03-23 02:16:52 +00002397 if (NarrowLoad.Val) {
2398 if (NarrowLoad.Val != N0.Val)
2399 CombineTo(N0.Val, NarrowLoad);
2400 return DAG.getNode(ISD::ANY_EXTEND, VT, NarrowLoad);
2401 }
Evan Chengc88138f2007-03-22 01:54:19 +00002402 }
2403
Chris Lattner84750582006-09-20 06:29:17 +00002404 // fold (aext (truncate x))
2405 if (N0.getOpcode() == ISD::TRUNCATE) {
2406 SDOperand TruncOp = N0.getOperand(0);
2407 if (TruncOp.getValueType() == VT)
2408 return TruncOp; // x iff x size == zext size.
2409 if (TruncOp.getValueType() > VT)
2410 return DAG.getNode(ISD::TRUNCATE, VT, TruncOp);
2411 return DAG.getNode(ISD::ANY_EXTEND, VT, TruncOp);
2412 }
Chris Lattner0e4b9222006-09-21 06:40:43 +00002413
2414 // fold (aext (and (trunc x), cst)) -> (and x, cst).
2415 if (N0.getOpcode() == ISD::AND &&
2416 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
2417 N0.getOperand(1).getOpcode() == ISD::Constant) {
2418 SDOperand X = N0.getOperand(0).getOperand(0);
2419 if (X.getValueType() < VT) {
2420 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
2421 } else if (X.getValueType() > VT) {
2422 X = DAG.getNode(ISD::TRUNCATE, VT, X);
2423 }
2424 uint64_t Mask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
2425 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
2426 }
2427
Chris Lattner5ffc0662006-05-05 05:58:59 +00002428 // fold (aext (load x)) -> (aext (truncate (extload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00002429 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00002430 (!AfterLegalize||TLI.isLoadXLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00002431 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2432 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
2433 LN0->getBasePtr(), LN0->getSrcValue(),
2434 LN0->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002435 N0.getValueType(),
2436 LN0->isVolatile(),
2437 LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00002438 CombineTo(N, ExtLoad);
2439 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2440 ExtLoad.getValue(1));
2441 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2442 }
2443
2444 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
2445 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
2446 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00002447 if (N0.getOpcode() == ISD::LOAD &&
2448 !ISD::isNON_EXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val) &&
Evan Cheng466685d2006-10-09 20:57:25 +00002449 N0.hasOneUse()) {
2450 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00002451 MVT::ValueType EVT = LN0->getLoadedVT();
Evan Cheng466685d2006-10-09 20:57:25 +00002452 SDOperand ExtLoad = DAG.getExtLoad(LN0->getExtensionType(), VT,
2453 LN0->getChain(), LN0->getBasePtr(),
2454 LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002455 LN0->getSrcValueOffset(), EVT,
2456 LN0->isVolatile(),
2457 LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00002458 CombineTo(N, ExtLoad);
2459 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2460 ExtLoad.getValue(1));
2461 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2462 }
Chris Lattner20a35c32007-04-11 05:32:27 +00002463
2464 // aext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
2465 if (N0.getOpcode() == ISD::SETCC) {
2466 SDOperand SCC =
Chris Lattner1eba01e2007-04-11 06:50:51 +00002467 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
2468 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattnerc24bbad2007-04-11 16:51:53 +00002469 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Chris Lattner1eba01e2007-04-11 06:50:51 +00002470 if (SCC.Val)
Chris Lattnerc56a81d2007-04-11 06:43:25 +00002471 return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00002472 }
2473
Chris Lattner5ffc0662006-05-05 05:58:59 +00002474 return SDOperand();
2475}
2476
Evan Chengc88138f2007-03-22 01:54:19 +00002477/// ReduceLoadWidth - If the result of a wider load is shifted to right of N
2478/// bits and then truncated to a narrower type and where N is a multiple
2479/// of number of bits of the narrower type, transform it to a narrower load
2480/// from address + N / num of bits of new type. If the result is to be
2481/// extended, also fold the extension to form a extending load.
2482SDOperand DAGCombiner::ReduceLoadWidth(SDNode *N) {
2483 unsigned Opc = N->getOpcode();
2484 ISD::LoadExtType ExtType = ISD::NON_EXTLOAD;
2485 SDOperand N0 = N->getOperand(0);
2486 MVT::ValueType VT = N->getValueType(0);
2487 MVT::ValueType EVT = N->getValueType(0);
2488
Evan Chenge177e302007-03-23 22:13:36 +00002489 // Special case: SIGN_EXTEND_INREG is basically truncating to EVT then
2490 // extended to VT.
Evan Chengc88138f2007-03-22 01:54:19 +00002491 if (Opc == ISD::SIGN_EXTEND_INREG) {
2492 ExtType = ISD::SEXTLOAD;
2493 EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Evan Chenge177e302007-03-23 22:13:36 +00002494 if (AfterLegalize && !TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))
2495 return SDOperand();
Evan Chengc88138f2007-03-22 01:54:19 +00002496 }
2497
2498 unsigned EVTBits = MVT::getSizeInBits(EVT);
2499 unsigned ShAmt = 0;
Evan Chengb37b80c2007-03-23 20:55:21 +00002500 bool CombineSRL = false;
Evan Chengc88138f2007-03-22 01:54:19 +00002501 if (N0.getOpcode() == ISD::SRL && N0.hasOneUse()) {
2502 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2503 ShAmt = N01->getValue();
2504 // Is the shift amount a multiple of size of VT?
2505 if ((ShAmt & (EVTBits-1)) == 0) {
2506 N0 = N0.getOperand(0);
2507 if (MVT::getSizeInBits(N0.getValueType()) <= EVTBits)
2508 return SDOperand();
Evan Chengb37b80c2007-03-23 20:55:21 +00002509 CombineSRL = true;
Evan Chengc88138f2007-03-22 01:54:19 +00002510 }
2511 }
2512 }
2513
2514 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
2515 // Do not allow folding to i1 here. i1 is implicitly stored in memory in
2516 // zero extended form: by shrinking the load, we lose track of the fact
2517 // that it is already zero extended.
2518 // FIXME: This should be reevaluated.
2519 VT != MVT::i1) {
2520 assert(MVT::getSizeInBits(N0.getValueType()) > EVTBits &&
2521 "Cannot truncate to larger type!");
2522 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2523 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
Evan Chengdae54ce2007-03-24 00:02:43 +00002524 // For big endian targets, we need to adjust the offset to the pointer to
2525 // load the correct bytes.
2526 if (!TLI.isLittleEndian())
2527 ShAmt = MVT::getSizeInBits(N0.getValueType()) - ShAmt - EVTBits;
2528 uint64_t PtrOff = ShAmt / 8;
Evan Chengc88138f2007-03-22 01:54:19 +00002529 SDOperand NewPtr = DAG.getNode(ISD::ADD, PtrType, LN0->getBasePtr(),
2530 DAG.getConstant(PtrOff, PtrType));
2531 AddToWorkList(NewPtr.Val);
2532 SDOperand Load = (ExtType == ISD::NON_EXTLOAD)
2533 ? DAG.getLoad(VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002534 LN0->getSrcValue(), LN0->getSrcValueOffset(),
2535 LN0->isVolatile(), LN0->getAlignment())
Evan Chengc88138f2007-03-22 01:54:19 +00002536 : DAG.getExtLoad(ExtType, VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002537 LN0->getSrcValue(), LN0->getSrcValueOffset(), EVT,
2538 LN0->isVolatile(), LN0->getAlignment());
Evan Chengc88138f2007-03-22 01:54:19 +00002539 AddToWorkList(N);
Evan Chengb37b80c2007-03-23 20:55:21 +00002540 if (CombineSRL) {
2541 std::vector<SDNode*> NowDead;
2542 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), Load.getValue(1), NowDead);
2543 CombineTo(N->getOperand(0).Val, Load);
2544 } else
2545 CombineTo(N0.Val, Load, Load.getValue(1));
Evan Cheng15213b72007-03-26 07:12:51 +00002546 if (ShAmt) {
2547 if (Opc == ISD::SIGN_EXTEND_INREG)
2548 return DAG.getNode(Opc, VT, Load, N->getOperand(1));
2549 else
2550 return DAG.getNode(Opc, VT, Load);
2551 }
Evan Chengc88138f2007-03-22 01:54:19 +00002552 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2553 }
2554
2555 return SDOperand();
2556}
2557
Chris Lattner5ffc0662006-05-05 05:58:59 +00002558
Nate Begeman83e75ec2005-09-06 04:43:02 +00002559SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002560 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002561 SDOperand N1 = N->getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002562 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002563 MVT::ValueType EVT = cast<VTSDNode>(N1)->getVT();
Nate Begeman07ed4172005-10-10 21:26:48 +00002564 unsigned EVTBits = MVT::getSizeInBits(EVT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002565
Nate Begeman1d4d4142005-09-01 00:19:25 +00002566 // fold (sext_in_reg c1) -> c1
Chris Lattnereaeda562006-05-08 20:59:41 +00002567 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
Chris Lattner310b5782006-05-06 23:06:26 +00002568 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0, N1);
Chris Lattneree4ea922006-05-06 09:30:03 +00002569
Chris Lattner541a24f2006-05-06 22:43:44 +00002570 // If the input is already sign extended, just drop the extension.
Chris Lattneree4ea922006-05-06 09:30:03 +00002571 if (TLI.ComputeNumSignBits(N0) >= MVT::getSizeInBits(VT)-EVTBits+1)
2572 return N0;
2573
Nate Begeman646d7e22005-09-02 21:18:40 +00002574 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
2575 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
2576 EVT < cast<VTSDNode>(N0.getOperand(1))->getVT()) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00002577 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002578 }
Chris Lattner4b37e872006-05-08 21:18:59 +00002579
Chris Lattner95a5e052007-04-17 19:03:21 +00002580 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is known zero.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00002581 if (TLI.MaskedValueIsZero(N0, 1ULL << (EVTBits-1)))
Nate Begemande996292006-02-03 22:24:05 +00002582 return DAG.getZeroExtendInReg(N0, EVT);
Chris Lattner4b37e872006-05-08 21:18:59 +00002583
Chris Lattner95a5e052007-04-17 19:03:21 +00002584 // fold operands of sext_in_reg based on knowledge that the top bits are not
2585 // demanded.
2586 if (SimplifyDemandedBits(SDOperand(N, 0)))
2587 return SDOperand(N, 0);
2588
Evan Chengc88138f2007-03-22 01:54:19 +00002589 // fold (sext_in_reg (load x)) -> (smaller sextload x)
2590 // fold (sext_in_reg (srl (load x), c)) -> (smaller sextload (x+c/evtbits))
2591 SDOperand NarrowLoad = ReduceLoadWidth(N);
2592 if (NarrowLoad.Val)
2593 return NarrowLoad;
2594
Chris Lattner4b37e872006-05-08 21:18:59 +00002595 // fold (sext_in_reg (srl X, 24), i8) -> sra X, 24
2596 // fold (sext_in_reg (srl X, 23), i8) -> sra X, 23 iff possible.
2597 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
2598 if (N0.getOpcode() == ISD::SRL) {
2599 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
2600 if (ShAmt->getValue()+EVTBits <= MVT::getSizeInBits(VT)) {
2601 // We can turn this into an SRA iff the input to the SRL is already sign
2602 // extended enough.
2603 unsigned InSignBits = TLI.ComputeNumSignBits(N0.getOperand(0));
2604 if (MVT::getSizeInBits(VT)-(ShAmt->getValue()+EVTBits) < InSignBits)
2605 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0), N0.getOperand(1));
2606 }
2607 }
Evan Chengc88138f2007-03-22 01:54:19 +00002608
Nate Begemanded49632005-10-13 03:11:28 +00002609 // fold (sext_inreg (extload x)) -> (sextload x)
Evan Chengc5484282006-10-04 00:56:09 +00002610 if (ISD::isEXTLoad(N0.Val) &&
Evan Cheng83060c52007-03-07 08:07:03 +00002611 ISD::isUNINDEXEDLoad(N0.Val) &&
Evan Cheng2e49f092006-10-11 07:10:22 +00002612 EVT == cast<LoadSDNode>(N0)->getLoadedVT() &&
Evan Chengc5484282006-10-04 00:56:09 +00002613 (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00002614 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2615 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2616 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002617 LN0->getSrcValueOffset(), EVT,
2618 LN0->isVolatile(),
2619 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00002620 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00002621 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002622 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00002623 }
2624 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Evan Cheng83060c52007-03-07 08:07:03 +00002625 if (ISD::isZEXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val) &&
2626 N0.hasOneUse() &&
Evan Cheng2e49f092006-10-11 07:10:22 +00002627 EVT == cast<LoadSDNode>(N0)->getLoadedVT() &&
Evan Chengc5484282006-10-04 00:56:09 +00002628 (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00002629 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2630 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2631 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002632 LN0->getSrcValueOffset(), EVT,
2633 LN0->isVolatile(),
2634 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00002635 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00002636 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002637 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00002638 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00002639 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002640}
2641
Nate Begeman83e75ec2005-09-06 04:43:02 +00002642SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002643 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002644 MVT::ValueType VT = N->getValueType(0);
2645
2646 // noop truncate
2647 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002648 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002649 // fold (truncate c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00002650 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002651 return DAG.getNode(ISD::TRUNCATE, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002652 // fold (truncate (truncate x)) -> (truncate x)
2653 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002654 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002655 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
Chris Lattnerb72773b2006-05-05 22:56:26 +00002656 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND||
2657 N0.getOpcode() == ISD::ANY_EXTEND) {
Chris Lattner32ba1aa2006-11-20 18:05:46 +00002658 if (N0.getOperand(0).getValueType() < VT)
Nate Begeman1d4d4142005-09-01 00:19:25 +00002659 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00002660 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Chris Lattner32ba1aa2006-11-20 18:05:46 +00002661 else if (N0.getOperand(0).getValueType() > VT)
Nate Begeman1d4d4142005-09-01 00:19:25 +00002662 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00002663 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002664 else
2665 // if the source and dest are the same type, we can drop both the extend
2666 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00002667 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002668 }
Evan Cheng007b69e2007-03-21 20:14:05 +00002669
Nate Begeman3df4d522005-10-12 20:40:40 +00002670 // fold (truncate (load x)) -> (smaller load x)
Evan Cheng007b69e2007-03-21 20:14:05 +00002671 // fold (truncate (srl (load x), c)) -> (smaller load (x+c/evtbits))
Evan Chengc88138f2007-03-22 01:54:19 +00002672 return ReduceLoadWidth(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002673}
2674
Chris Lattner94683772005-12-23 05:30:37 +00002675SDOperand DAGCombiner::visitBIT_CONVERT(SDNode *N) {
2676 SDOperand N0 = N->getOperand(0);
2677 MVT::ValueType VT = N->getValueType(0);
2678
2679 // If the input is a constant, let getNode() fold it.
2680 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
2681 SDOperand Res = DAG.getNode(ISD::BIT_CONVERT, VT, N0);
2682 if (Res.Val != N) return Res;
2683 }
2684
Chris Lattnerc8547d82005-12-23 05:37:50 +00002685 if (N0.getOpcode() == ISD::BIT_CONVERT) // conv(conv(x,t1),t2) -> conv(x,t2)
2686 return DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
Chris Lattner6258fb22006-04-02 02:53:43 +00002687
Chris Lattner57104102005-12-23 05:44:41 +00002688 // fold (conv (load x)) -> (load (conv*)x)
Evan Cheng59d5b682007-05-07 21:27:48 +00002689 // If the resultant load doesn't need a higher alignment than the original!
2690 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Dale Johannesen98a6c622007-05-16 22:45:30 +00002691 ISD::isUNINDEXEDLoad(N0.Val) &&
Evan Cheng59d5b682007-05-07 21:27:48 +00002692 TLI.isOperationLegal(ISD::LOAD, VT)) {
Evan Cheng466685d2006-10-09 20:57:25 +00002693 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng59d5b682007-05-07 21:27:48 +00002694 unsigned Align = TLI.getTargetMachine().getTargetData()->
Dan Gohmanfcc4dd92007-05-18 18:41:29 +00002695 getABITypeAlignment(MVT::getTypeForValueType(VT));
Evan Cheng59d5b682007-05-07 21:27:48 +00002696 unsigned OrigAlign = LN0->getAlignment();
2697 if (Align <= OrigAlign) {
2698 SDOperand Load = DAG.getLoad(VT, LN0->getChain(), LN0->getBasePtr(),
2699 LN0->getSrcValue(), LN0->getSrcValueOffset(),
2700 LN0->isVolatile(), LN0->getAlignment());
2701 AddToWorkList(N);
2702 CombineTo(N0.Val, DAG.getNode(ISD::BIT_CONVERT, N0.getValueType(), Load),
2703 Load.getValue(1));
2704 return Load;
2705 }
Chris Lattner57104102005-12-23 05:44:41 +00002706 }
2707
Chris Lattner94683772005-12-23 05:30:37 +00002708 return SDOperand();
2709}
2710
Chris Lattner6258fb22006-04-02 02:53:43 +00002711SDOperand DAGCombiner::visitVBIT_CONVERT(SDNode *N) {
2712 SDOperand N0 = N->getOperand(0);
2713 MVT::ValueType VT = N->getValueType(0);
2714
2715 // If the input is a VBUILD_VECTOR with all constant elements, fold this now.
2716 // First check to see if this is all constant.
2717 if (N0.getOpcode() == ISD::VBUILD_VECTOR && N0.Val->hasOneUse() &&
2718 VT == MVT::Vector) {
2719 bool isSimple = true;
2720 for (unsigned i = 0, e = N0.getNumOperands()-2; i != e; ++i)
2721 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
2722 N0.getOperand(i).getOpcode() != ISD::Constant &&
2723 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
2724 isSimple = false;
2725 break;
2726 }
2727
Chris Lattner97c20732006-04-03 17:29:28 +00002728 MVT::ValueType DestEltVT = cast<VTSDNode>(N->getOperand(2))->getVT();
2729 if (isSimple && !MVT::isVector(DestEltVT)) {
Chris Lattner6258fb22006-04-02 02:53:43 +00002730 return ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(N0.Val, DestEltVT);
2731 }
2732 }
2733
2734 return SDOperand();
2735}
2736
2737/// ConstantFoldVBIT_CONVERTofVBUILD_VECTOR - We know that BV is a vbuild_vector
2738/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
2739/// destination element value type.
2740SDOperand DAGCombiner::
2741ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(SDNode *BV, MVT::ValueType DstEltVT) {
2742 MVT::ValueType SrcEltVT = BV->getOperand(0).getValueType();
2743
2744 // If this is already the right type, we're done.
2745 if (SrcEltVT == DstEltVT) return SDOperand(BV, 0);
2746
2747 unsigned SrcBitSize = MVT::getSizeInBits(SrcEltVT);
2748 unsigned DstBitSize = MVT::getSizeInBits(DstEltVT);
2749
2750 // If this is a conversion of N elements of one type to N elements of another
2751 // type, convert each element. This handles FP<->INT cases.
2752 if (SrcBitSize == DstBitSize) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002753 SmallVector<SDOperand, 8> Ops;
Chris Lattner3e104b12006-04-08 04:15:24 +00002754 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00002755 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, DstEltVT, BV->getOperand(i)));
Chris Lattner3e104b12006-04-08 04:15:24 +00002756 AddToWorkList(Ops.back().Val);
2757 }
Chris Lattner6258fb22006-04-02 02:53:43 +00002758 Ops.push_back(*(BV->op_end()-2)); // Add num elements.
2759 Ops.push_back(DAG.getValueType(DstEltVT));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002760 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00002761 }
2762
2763 // Otherwise, we're growing or shrinking the elements. To avoid having to
2764 // handle annoying details of growing/shrinking FP values, we convert them to
2765 // int first.
2766 if (MVT::isFloatingPoint(SrcEltVT)) {
2767 // Convert the input float vector to a int vector where the elements are the
2768 // same sizes.
2769 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
2770 MVT::ValueType IntVT = SrcEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
2771 BV = ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(BV, IntVT).Val;
2772 SrcEltVT = IntVT;
2773 }
2774
2775 // Now we know the input is an integer vector. If the output is a FP type,
2776 // convert to integer first, then to FP of the right size.
2777 if (MVT::isFloatingPoint(DstEltVT)) {
2778 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
2779 MVT::ValueType TmpVT = DstEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
2780 SDNode *Tmp = ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(BV, TmpVT).Val;
2781
2782 // Next, convert to FP elements of the same size.
2783 return ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(Tmp, DstEltVT);
2784 }
2785
2786 // Okay, we know the src/dst types are both integers of differing types.
2787 // Handling growing first.
2788 assert(MVT::isInteger(SrcEltVT) && MVT::isInteger(DstEltVT));
2789 if (SrcBitSize < DstBitSize) {
2790 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
2791
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002792 SmallVector<SDOperand, 8> Ops;
Chris Lattner6258fb22006-04-02 02:53:43 +00002793 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e;
2794 i += NumInputsPerOutput) {
2795 bool isLE = TLI.isLittleEndian();
2796 uint64_t NewBits = 0;
2797 bool EltIsUndef = true;
2798 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
2799 // Shift the previously computed bits over.
2800 NewBits <<= SrcBitSize;
2801 SDOperand Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
2802 if (Op.getOpcode() == ISD::UNDEF) continue;
2803 EltIsUndef = false;
2804
2805 NewBits |= cast<ConstantSDNode>(Op)->getValue();
2806 }
2807
2808 if (EltIsUndef)
2809 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
2810 else
2811 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
2812 }
2813
2814 Ops.push_back(DAG.getConstant(Ops.size(), MVT::i32)); // Add num elements.
2815 Ops.push_back(DAG.getValueType(DstEltVT)); // Add element size.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002816 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00002817 }
2818
2819 // Finally, this must be the case where we are shrinking elements: each input
2820 // turns into multiple outputs.
2821 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002822 SmallVector<SDOperand, 8> Ops;
Chris Lattner6258fb22006-04-02 02:53:43 +00002823 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e; ++i) {
2824 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
2825 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
2826 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
2827 continue;
2828 }
2829 uint64_t OpVal = cast<ConstantSDNode>(BV->getOperand(i))->getValue();
2830
2831 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
2832 unsigned ThisVal = OpVal & ((1ULL << DstBitSize)-1);
2833 OpVal >>= DstBitSize;
2834 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
2835 }
2836
2837 // For big endian targets, swap the order of the pieces of each element.
2838 if (!TLI.isLittleEndian())
2839 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
2840 }
2841 Ops.push_back(DAG.getConstant(Ops.size(), MVT::i32)); // Add num elements.
2842 Ops.push_back(DAG.getValueType(DstEltVT)); // Add element size.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002843 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00002844}
2845
2846
2847
Chris Lattner01b3d732005-09-28 22:28:18 +00002848SDOperand DAGCombiner::visitFADD(SDNode *N) {
2849 SDOperand N0 = N->getOperand(0);
2850 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00002851 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2852 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002853 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00002854
2855 // fold (fadd c1, c2) -> c1+c2
2856 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002857 return DAG.getNode(ISD::FADD, VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00002858 // canonicalize constant to RHS
2859 if (N0CFP && !N1CFP)
2860 return DAG.getNode(ISD::FADD, VT, N1, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00002861 // fold (A + (-B)) -> A-B
Chris Lattner29446522007-05-14 22:04:50 +00002862 if (isNegatibleForFree(N1) == 2)
2863 return DAG.getNode(ISD::FSUB, VT, N0, GetNegatedExpression(N1, DAG));
Chris Lattner01b3d732005-09-28 22:28:18 +00002864 // fold ((-A) + B) -> B-A
Chris Lattner29446522007-05-14 22:04:50 +00002865 if (isNegatibleForFree(N0) == 2)
2866 return DAG.getNode(ISD::FSUB, VT, N1, GetNegatedExpression(N0, DAG));
Chris Lattnerddae4bd2007-01-08 23:04:05 +00002867
2868 // If allowed, fold (fadd (fadd x, c1), c2) -> (fadd x, (fadd c1, c2))
2869 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FADD &&
2870 N0.Val->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
2871 return DAG.getNode(ISD::FADD, VT, N0.getOperand(0),
2872 DAG.getNode(ISD::FADD, VT, N0.getOperand(1), N1));
2873
Chris Lattner01b3d732005-09-28 22:28:18 +00002874 return SDOperand();
2875}
2876
2877SDOperand DAGCombiner::visitFSUB(SDNode *N) {
2878 SDOperand N0 = N->getOperand(0);
2879 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00002880 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2881 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002882 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00002883
2884 // fold (fsub c1, c2) -> c1-c2
2885 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002886 return DAG.getNode(ISD::FSUB, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002887 // fold (A-(-B)) -> A+B
Chris Lattner29446522007-05-14 22:04:50 +00002888 if (isNegatibleForFree(N1))
2889 return DAG.getNode(ISD::FADD, VT, N0, GetNegatedExpression(N1, DAG));
2890
Chris Lattner01b3d732005-09-28 22:28:18 +00002891 return SDOperand();
2892}
2893
2894SDOperand DAGCombiner::visitFMUL(SDNode *N) {
2895 SDOperand N0 = N->getOperand(0);
2896 SDOperand N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00002897 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2898 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002899 MVT::ValueType VT = N->getValueType(0);
2900
Nate Begeman11af4ea2005-10-17 20:40:11 +00002901 // fold (fmul c1, c2) -> c1*c2
2902 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002903 return DAG.getNode(ISD::FMUL, VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00002904 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00002905 if (N0CFP && !N1CFP)
2906 return DAG.getNode(ISD::FMUL, VT, N1, N0);
Nate Begeman11af4ea2005-10-17 20:40:11 +00002907 // fold (fmul X, 2.0) -> (fadd X, X)
2908 if (N1CFP && N1CFP->isExactlyValue(+2.0))
2909 return DAG.getNode(ISD::FADD, VT, N0, N0);
Chris Lattner29446522007-05-14 22:04:50 +00002910 // fold (fmul X, -1.0) -> (fneg X)
2911 if (N1CFP && N1CFP->isExactlyValue(-1.0))
2912 return DAG.getNode(ISD::FNEG, VT, N0);
2913
2914 // -X * -Y -> X*Y
2915 if (char LHSNeg = isNegatibleForFree(N0)) {
2916 if (char RHSNeg = isNegatibleForFree(N1)) {
2917 // Both can be negated for free, check to see if at least one is cheaper
2918 // negated.
2919 if (LHSNeg == 2 || RHSNeg == 2)
2920 return DAG.getNode(ISD::FMUL, VT, GetNegatedExpression(N0, DAG),
2921 GetNegatedExpression(N1, DAG));
2922 }
2923 }
Chris Lattnerddae4bd2007-01-08 23:04:05 +00002924
2925 // If allowed, fold (fmul (fmul x, c1), c2) -> (fmul x, (fmul c1, c2))
2926 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FMUL &&
2927 N0.Val->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
2928 return DAG.getNode(ISD::FMUL, VT, N0.getOperand(0),
2929 DAG.getNode(ISD::FMUL, VT, N0.getOperand(1), N1));
2930
Chris Lattner01b3d732005-09-28 22:28:18 +00002931 return SDOperand();
2932}
2933
2934SDOperand DAGCombiner::visitFDIV(SDNode *N) {
2935 SDOperand N0 = N->getOperand(0);
2936 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00002937 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2938 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002939 MVT::ValueType VT = N->getValueType(0);
2940
Nate Begemana148d982006-01-18 22:35:16 +00002941 // fold (fdiv c1, c2) -> c1/c2
2942 if (N0CFP && N1CFP)
2943 return DAG.getNode(ISD::FDIV, VT, N0, N1);
Chris Lattner29446522007-05-14 22:04:50 +00002944
2945
2946 // -X / -Y -> X*Y
2947 if (char LHSNeg = isNegatibleForFree(N0)) {
2948 if (char RHSNeg = isNegatibleForFree(N1)) {
2949 // Both can be negated for free, check to see if at least one is cheaper
2950 // negated.
2951 if (LHSNeg == 2 || RHSNeg == 2)
2952 return DAG.getNode(ISD::FDIV, VT, GetNegatedExpression(N0, DAG),
2953 GetNegatedExpression(N1, DAG));
2954 }
2955 }
2956
Chris Lattner01b3d732005-09-28 22:28:18 +00002957 return SDOperand();
2958}
2959
2960SDOperand DAGCombiner::visitFREM(SDNode *N) {
2961 SDOperand N0 = N->getOperand(0);
2962 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00002963 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2964 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002965 MVT::ValueType VT = N->getValueType(0);
2966
Nate Begemana148d982006-01-18 22:35:16 +00002967 // fold (frem c1, c2) -> fmod(c1,c2)
2968 if (N0CFP && N1CFP)
2969 return DAG.getNode(ISD::FREM, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002970 return SDOperand();
2971}
2972
Chris Lattner12d83032006-03-05 05:30:57 +00002973SDOperand DAGCombiner::visitFCOPYSIGN(SDNode *N) {
2974 SDOperand N0 = N->getOperand(0);
2975 SDOperand N1 = N->getOperand(1);
2976 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2977 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2978 MVT::ValueType VT = N->getValueType(0);
2979
2980 if (N0CFP && N1CFP) // Constant fold
2981 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1);
2982
2983 if (N1CFP) {
2984 // copysign(x, c1) -> fabs(x) iff ispos(c1)
2985 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
2986 union {
2987 double d;
2988 int64_t i;
2989 } u;
2990 u.d = N1CFP->getValue();
2991 if (u.i >= 0)
2992 return DAG.getNode(ISD::FABS, VT, N0);
2993 else
2994 return DAG.getNode(ISD::FNEG, VT, DAG.getNode(ISD::FABS, VT, N0));
2995 }
2996
2997 // copysign(fabs(x), y) -> copysign(x, y)
2998 // copysign(fneg(x), y) -> copysign(x, y)
2999 // copysign(copysign(x,z), y) -> copysign(x, y)
3000 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
3001 N0.getOpcode() == ISD::FCOPYSIGN)
3002 return DAG.getNode(ISD::FCOPYSIGN, VT, N0.getOperand(0), N1);
3003
3004 // copysign(x, abs(y)) -> abs(x)
3005 if (N1.getOpcode() == ISD::FABS)
3006 return DAG.getNode(ISD::FABS, VT, N0);
3007
3008 // copysign(x, copysign(y,z)) -> copysign(x, z)
3009 if (N1.getOpcode() == ISD::FCOPYSIGN)
3010 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(1));
3011
3012 // copysign(x, fp_extend(y)) -> copysign(x, y)
3013 // copysign(x, fp_round(y)) -> copysign(x, y)
3014 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
3015 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(0));
3016
3017 return SDOperand();
3018}
3019
3020
Chris Lattner01b3d732005-09-28 22:28:18 +00003021
Nate Begeman83e75ec2005-09-06 04:43:02 +00003022SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003023 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003024 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00003025 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003026
3027 // fold (sint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00003028 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00003029 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003030 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003031}
3032
Nate Begeman83e75ec2005-09-06 04:43:02 +00003033SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003034 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003035 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00003036 MVT::ValueType VT = N->getValueType(0);
3037
Nate Begeman1d4d4142005-09-01 00:19:25 +00003038 // fold (uint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00003039 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00003040 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003041 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003042}
3043
Nate Begeman83e75ec2005-09-06 04:43:02 +00003044SDOperand DAGCombiner::visitFP_TO_SINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003045 SDOperand N0 = N->getOperand(0);
3046 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3047 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003048
3049 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00003050 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00003051 return DAG.getNode(ISD::FP_TO_SINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003052 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003053}
3054
Nate Begeman83e75ec2005-09-06 04:43:02 +00003055SDOperand DAGCombiner::visitFP_TO_UINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003056 SDOperand N0 = N->getOperand(0);
3057 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3058 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003059
3060 // fold (fp_to_uint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00003061 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00003062 return DAG.getNode(ISD::FP_TO_UINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003063 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003064}
3065
Nate Begeman83e75ec2005-09-06 04:43:02 +00003066SDOperand DAGCombiner::visitFP_ROUND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003067 SDOperand N0 = N->getOperand(0);
3068 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3069 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003070
3071 // fold (fp_round c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00003072 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00003073 return DAG.getNode(ISD::FP_ROUND, VT, N0);
Chris Lattner79dbea52006-03-13 06:26:26 +00003074
3075 // fold (fp_round (fp_extend x)) -> x
3076 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
3077 return N0.getOperand(0);
3078
3079 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
3080 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.Val->hasOneUse()) {
3081 SDOperand Tmp = DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0));
3082 AddToWorkList(Tmp.Val);
3083 return DAG.getNode(ISD::FCOPYSIGN, VT, Tmp, N0.getOperand(1));
3084 }
3085
Nate Begeman83e75ec2005-09-06 04:43:02 +00003086 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003087}
3088
Nate Begeman83e75ec2005-09-06 04:43:02 +00003089SDOperand DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003090 SDOperand N0 = N->getOperand(0);
3091 MVT::ValueType VT = N->getValueType(0);
3092 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00003093 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003094
Nate Begeman1d4d4142005-09-01 00:19:25 +00003095 // fold (fp_round_inreg c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00003096 if (N0CFP) {
3097 SDOperand Round = DAG.getConstantFP(N0CFP->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003098 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003099 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00003100 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003101}
3102
Nate Begeman83e75ec2005-09-06 04:43:02 +00003103SDOperand DAGCombiner::visitFP_EXTEND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003104 SDOperand N0 = N->getOperand(0);
3105 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3106 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003107
3108 // fold (fp_extend c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00003109 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00003110 return DAG.getNode(ISD::FP_EXTEND, VT, N0);
Chris Lattnere564dbb2006-05-05 21:34:35 +00003111
3112 // fold (fpext (load x)) -> (fpext (fpround (extload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00003113 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00003114 (!AfterLegalize||TLI.isLoadXLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003115 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3116 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
3117 LN0->getBasePtr(), LN0->getSrcValue(),
3118 LN0->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003119 N0.getValueType(),
3120 LN0->isVolatile(),
3121 LN0->getAlignment());
Chris Lattnere564dbb2006-05-05 21:34:35 +00003122 CombineTo(N, ExtLoad);
3123 CombineTo(N0.Val, DAG.getNode(ISD::FP_ROUND, N0.getValueType(), ExtLoad),
3124 ExtLoad.getValue(1));
3125 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
3126 }
3127
3128
Nate Begeman83e75ec2005-09-06 04:43:02 +00003129 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003130}
3131
Nate Begeman83e75ec2005-09-06 04:43:02 +00003132SDOperand DAGCombiner::visitFNEG(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003133 SDOperand N0 = N->getOperand(0);
3134 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3135 MVT::ValueType VT = N->getValueType(0);
3136
3137 // fold (fneg c1) -> -c1
Nate Begeman646d7e22005-09-02 21:18:40 +00003138 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00003139 return DAG.getNode(ISD::FNEG, VT, N0);
3140 // fold (fneg (sub x, y)) -> (sub y, x)
Chris Lattner12d83032006-03-05 05:30:57 +00003141 if (N0.getOpcode() == ISD::SUB)
3142 return DAG.getNode(ISD::SUB, VT, N0.getOperand(1), N0.getOperand(0));
Nate Begemana148d982006-01-18 22:35:16 +00003143 // fold (fneg (fneg x)) -> x
Chris Lattner12d83032006-03-05 05:30:57 +00003144 if (N0.getOpcode() == ISD::FNEG)
3145 return N0.getOperand(0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003146 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003147}
3148
Nate Begeman83e75ec2005-09-06 04:43:02 +00003149SDOperand DAGCombiner::visitFABS(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003150 SDOperand N0 = N->getOperand(0);
3151 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3152 MVT::ValueType VT = N->getValueType(0);
3153
Nate Begeman1d4d4142005-09-01 00:19:25 +00003154 // fold (fabs c1) -> fabs(c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00003155 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00003156 return DAG.getNode(ISD::FABS, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003157 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00003158 if (N0.getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00003159 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003160 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00003161 // fold (fabs (fcopysign x, y)) -> (fabs x)
3162 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
3163 return DAG.getNode(ISD::FABS, VT, N0.getOperand(0));
3164
Nate Begeman83e75ec2005-09-06 04:43:02 +00003165 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003166}
3167
Nate Begeman44728a72005-09-19 22:34:01 +00003168SDOperand DAGCombiner::visitBRCOND(SDNode *N) {
3169 SDOperand Chain = N->getOperand(0);
3170 SDOperand N1 = N->getOperand(1);
3171 SDOperand N2 = N->getOperand(2);
3172 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
3173
3174 // never taken branch, fold to chain
3175 if (N1C && N1C->isNullValue())
3176 return Chain;
3177 // unconditional branch
Nate Begemane17daeb2005-10-05 21:43:42 +00003178 if (N1C && N1C->getValue() == 1)
Nate Begeman44728a72005-09-19 22:34:01 +00003179 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
Nate Begeman750ac1b2006-02-01 07:19:44 +00003180 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
3181 // on the target.
3182 if (N1.getOpcode() == ISD::SETCC &&
3183 TLI.isOperationLegal(ISD::BR_CC, MVT::Other)) {
3184 return DAG.getNode(ISD::BR_CC, MVT::Other, Chain, N1.getOperand(2),
3185 N1.getOperand(0), N1.getOperand(1), N2);
3186 }
Nate Begeman44728a72005-09-19 22:34:01 +00003187 return SDOperand();
3188}
3189
Chris Lattner3ea0b472005-10-05 06:47:48 +00003190// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
3191//
Nate Begeman44728a72005-09-19 22:34:01 +00003192SDOperand DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00003193 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
3194 SDOperand CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
3195
3196 // Use SimplifySetCC to simplify SETCC's.
Nate Begemane17daeb2005-10-05 21:43:42 +00003197 SDOperand Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false);
Chris Lattner30f73e72006-10-14 03:52:46 +00003198 if (Simp.Val) AddToWorkList(Simp.Val);
3199
Nate Begemane17daeb2005-10-05 21:43:42 +00003200 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.Val);
3201
3202 // fold br_cc true, dest -> br dest (unconditional branch)
3203 if (SCCC && SCCC->getValue())
3204 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
3205 N->getOperand(4));
3206 // fold br_cc false, dest -> unconditional fall through
3207 if (SCCC && SCCC->isNullValue())
3208 return N->getOperand(0);
Chris Lattner30f73e72006-10-14 03:52:46 +00003209
Nate Begemane17daeb2005-10-05 21:43:42 +00003210 // fold to a simpler setcc
3211 if (Simp.Val && Simp.getOpcode() == ISD::SETCC)
3212 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
3213 Simp.getOperand(2), Simp.getOperand(0),
3214 Simp.getOperand(1), N->getOperand(4));
Nate Begeman44728a72005-09-19 22:34:01 +00003215 return SDOperand();
3216}
3217
Chris Lattner448f2192006-11-11 00:39:41 +00003218
3219/// CombineToPreIndexedLoadStore - Try turning a load / store and a
3220/// pre-indexed load / store when the base pointer is a add or subtract
3221/// and it has other uses besides the load / store. After the
3222/// transformation, the new indexed load / store has effectively folded
3223/// the add / subtract in and all of its other uses are redirected to the
3224/// new load / store.
3225bool DAGCombiner::CombineToPreIndexedLoadStore(SDNode *N) {
3226 if (!AfterLegalize)
3227 return false;
3228
3229 bool isLoad = true;
3230 SDOperand Ptr;
3231 MVT::ValueType VT;
3232 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Evan Chenge90460e2006-12-16 06:25:23 +00003233 if (LD->getAddressingMode() != ISD::UNINDEXED)
3234 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00003235 VT = LD->getLoadedVT();
Evan Cheng83060c52007-03-07 08:07:03 +00003236 if (!TLI.isIndexedLoadLegal(ISD::PRE_INC, VT) &&
Chris Lattner448f2192006-11-11 00:39:41 +00003237 !TLI.isIndexedLoadLegal(ISD::PRE_DEC, VT))
3238 return false;
3239 Ptr = LD->getBasePtr();
3240 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Evan Chenge90460e2006-12-16 06:25:23 +00003241 if (ST->getAddressingMode() != ISD::UNINDEXED)
3242 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00003243 VT = ST->getStoredVT();
3244 if (!TLI.isIndexedStoreLegal(ISD::PRE_INC, VT) &&
3245 !TLI.isIndexedStoreLegal(ISD::PRE_DEC, VT))
3246 return false;
3247 Ptr = ST->getBasePtr();
3248 isLoad = false;
3249 } else
3250 return false;
3251
Chris Lattner9f1794e2006-11-11 00:56:29 +00003252 // If the pointer is not an add/sub, or if it doesn't have multiple uses, bail
3253 // out. There is no reason to make this a preinc/predec.
3254 if ((Ptr.getOpcode() != ISD::ADD && Ptr.getOpcode() != ISD::SUB) ||
3255 Ptr.Val->hasOneUse())
3256 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00003257
Chris Lattner9f1794e2006-11-11 00:56:29 +00003258 // Ask the target to do addressing mode selection.
3259 SDOperand BasePtr;
3260 SDOperand Offset;
3261 ISD::MemIndexedMode AM = ISD::UNINDEXED;
3262 if (!TLI.getPreIndexedAddressParts(N, BasePtr, Offset, AM, DAG))
3263 return false;
Evan Chenga7d4a042007-05-03 23:52:19 +00003264 // Don't create a indexed load / store with zero offset.
3265 if (isa<ConstantSDNode>(Offset) &&
3266 cast<ConstantSDNode>(Offset)->getValue() == 0)
3267 return false;
Chris Lattner9f1794e2006-11-11 00:56:29 +00003268
Chris Lattner41e53fd2006-11-11 01:00:15 +00003269 // Try turning it into a pre-indexed load / store except when:
Evan Chengc843abe2007-05-24 02:35:39 +00003270 // 1) The new base ptr is a frame index.
3271 // 2) If N is a store and the new base ptr is either the same as or is a
Chris Lattner9f1794e2006-11-11 00:56:29 +00003272 // predecessor of the value being stored.
Evan Chengc843abe2007-05-24 02:35:39 +00003273 // 3) Another use of old base ptr is a predecessor of N. If ptr is folded
Chris Lattner9f1794e2006-11-11 00:56:29 +00003274 // that would create a cycle.
Evan Chengc843abe2007-05-24 02:35:39 +00003275 // 4) All uses are load / store ops that use it as old base ptr.
Chris Lattner448f2192006-11-11 00:39:41 +00003276
Chris Lattner41e53fd2006-11-11 01:00:15 +00003277 // Check #1. Preinc'ing a frame index would require copying the stack pointer
3278 // (plus the implicit offset) to a register to preinc anyway.
3279 if (isa<FrameIndexSDNode>(BasePtr))
3280 return false;
3281
3282 // Check #2.
Chris Lattner9f1794e2006-11-11 00:56:29 +00003283 if (!isLoad) {
3284 SDOperand Val = cast<StoreSDNode>(N)->getValue();
Evan Chengc843abe2007-05-24 02:35:39 +00003285 if (Val == BasePtr || BasePtr.Val->isPredecessor(Val.Val))
Chris Lattner9f1794e2006-11-11 00:56:29 +00003286 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00003287 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00003288
Evan Chengc843abe2007-05-24 02:35:39 +00003289 // Now check for #3 and #4.
Chris Lattner9f1794e2006-11-11 00:56:29 +00003290 bool RealUse = false;
3291 for (SDNode::use_iterator I = Ptr.Val->use_begin(),
3292 E = Ptr.Val->use_end(); I != E; ++I) {
3293 SDNode *Use = *I;
3294 if (Use == N)
3295 continue;
3296 if (Use->isPredecessor(N))
3297 return false;
3298
3299 if (!((Use->getOpcode() == ISD::LOAD &&
3300 cast<LoadSDNode>(Use)->getBasePtr() == Ptr) ||
3301 (Use->getOpcode() == ISD::STORE) &&
3302 cast<StoreSDNode>(Use)->getBasePtr() == Ptr))
3303 RealUse = true;
3304 }
3305 if (!RealUse)
3306 return false;
3307
3308 SDOperand Result;
3309 if (isLoad)
3310 Result = DAG.getIndexedLoad(SDOperand(N,0), BasePtr, Offset, AM);
3311 else
3312 Result = DAG.getIndexedStore(SDOperand(N,0), BasePtr, Offset, AM);
3313 ++PreIndexedNodes;
3314 ++NodesCombined;
Bill Wendling832171c2006-12-07 20:04:42 +00003315 DOUT << "\nReplacing.4 "; DEBUG(N->dump());
3316 DOUT << "\nWith: "; DEBUG(Result.Val->dump(&DAG));
3317 DOUT << '\n';
Chris Lattner9f1794e2006-11-11 00:56:29 +00003318 std::vector<SDNode*> NowDead;
3319 if (isLoad) {
3320 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(0),
3321 NowDead);
3322 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1), Result.getValue(2),
3323 NowDead);
3324 } else {
3325 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(1),
3326 NowDead);
3327 }
3328
3329 // Nodes can end up on the worklist more than once. Make sure we do
3330 // not process a node that has been replaced.
3331 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
3332 removeFromWorkList(NowDead[i]);
3333 // Finally, since the node is now dead, remove it from the graph.
3334 DAG.DeleteNode(N);
3335
3336 // Replace the uses of Ptr with uses of the updated base value.
3337 DAG.ReplaceAllUsesOfValueWith(Ptr, Result.getValue(isLoad ? 1 : 0),
3338 NowDead);
3339 removeFromWorkList(Ptr.Val);
3340 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
3341 removeFromWorkList(NowDead[i]);
3342 DAG.DeleteNode(Ptr.Val);
3343
3344 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00003345}
3346
3347/// CombineToPostIndexedLoadStore - Try combine a load / store with a
3348/// add / sub of the base pointer node into a post-indexed load / store.
3349/// The transformation folded the add / subtract into the new indexed
3350/// load / store effectively and all of its uses are redirected to the
3351/// new load / store.
3352bool DAGCombiner::CombineToPostIndexedLoadStore(SDNode *N) {
3353 if (!AfterLegalize)
3354 return false;
3355
3356 bool isLoad = true;
3357 SDOperand Ptr;
3358 MVT::ValueType VT;
3359 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Evan Chenge90460e2006-12-16 06:25:23 +00003360 if (LD->getAddressingMode() != ISD::UNINDEXED)
3361 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00003362 VT = LD->getLoadedVT();
3363 if (!TLI.isIndexedLoadLegal(ISD::POST_INC, VT) &&
3364 !TLI.isIndexedLoadLegal(ISD::POST_DEC, VT))
3365 return false;
3366 Ptr = LD->getBasePtr();
3367 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Evan Chenge90460e2006-12-16 06:25:23 +00003368 if (ST->getAddressingMode() != ISD::UNINDEXED)
3369 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00003370 VT = ST->getStoredVT();
3371 if (!TLI.isIndexedStoreLegal(ISD::POST_INC, VT) &&
3372 !TLI.isIndexedStoreLegal(ISD::POST_DEC, VT))
3373 return false;
3374 Ptr = ST->getBasePtr();
3375 isLoad = false;
3376 } else
3377 return false;
3378
Evan Chengcc470212006-11-16 00:08:20 +00003379 if (Ptr.Val->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00003380 return false;
3381
3382 for (SDNode::use_iterator I = Ptr.Val->use_begin(),
3383 E = Ptr.Val->use_end(); I != E; ++I) {
3384 SDNode *Op = *I;
3385 if (Op == N ||
3386 (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB))
3387 continue;
3388
3389 SDOperand BasePtr;
3390 SDOperand Offset;
3391 ISD::MemIndexedMode AM = ISD::UNINDEXED;
3392 if (TLI.getPostIndexedAddressParts(N, Op, BasePtr, Offset, AM, DAG)) {
3393 if (Ptr == Offset)
3394 std::swap(BasePtr, Offset);
3395 if (Ptr != BasePtr)
Chris Lattner448f2192006-11-11 00:39:41 +00003396 continue;
Evan Chenga7d4a042007-05-03 23:52:19 +00003397 // Don't create a indexed load / store with zero offset.
3398 if (isa<ConstantSDNode>(Offset) &&
3399 cast<ConstantSDNode>(Offset)->getValue() == 0)
3400 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00003401
Chris Lattner9f1794e2006-11-11 00:56:29 +00003402 // Try turning it into a post-indexed load / store except when
3403 // 1) All uses are load / store ops that use it as base ptr.
3404 // 2) Op must be independent of N, i.e. Op is neither a predecessor
3405 // nor a successor of N. Otherwise, if Op is folded that would
3406 // create a cycle.
3407
3408 // Check for #1.
3409 bool TryNext = false;
3410 for (SDNode::use_iterator II = BasePtr.Val->use_begin(),
3411 EE = BasePtr.Val->use_end(); II != EE; ++II) {
3412 SDNode *Use = *II;
3413 if (Use == Ptr.Val)
Chris Lattner448f2192006-11-11 00:39:41 +00003414 continue;
3415
Chris Lattner9f1794e2006-11-11 00:56:29 +00003416 // If all the uses are load / store addresses, then don't do the
3417 // transformation.
3418 if (Use->getOpcode() == ISD::ADD || Use->getOpcode() == ISD::SUB){
3419 bool RealUse = false;
3420 for (SDNode::use_iterator III = Use->use_begin(),
3421 EEE = Use->use_end(); III != EEE; ++III) {
3422 SDNode *UseUse = *III;
3423 if (!((UseUse->getOpcode() == ISD::LOAD &&
3424 cast<LoadSDNode>(UseUse)->getBasePtr().Val == Use) ||
3425 (UseUse->getOpcode() == ISD::STORE) &&
3426 cast<StoreSDNode>(UseUse)->getBasePtr().Val == Use))
3427 RealUse = true;
3428 }
Chris Lattner448f2192006-11-11 00:39:41 +00003429
Chris Lattner9f1794e2006-11-11 00:56:29 +00003430 if (!RealUse) {
3431 TryNext = true;
3432 break;
Chris Lattner448f2192006-11-11 00:39:41 +00003433 }
3434 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00003435 }
3436 if (TryNext)
3437 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00003438
Chris Lattner9f1794e2006-11-11 00:56:29 +00003439 // Check for #2
3440 if (!Op->isPredecessor(N) && !N->isPredecessor(Op)) {
3441 SDOperand Result = isLoad
3442 ? DAG.getIndexedLoad(SDOperand(N,0), BasePtr, Offset, AM)
3443 : DAG.getIndexedStore(SDOperand(N,0), BasePtr, Offset, AM);
3444 ++PostIndexedNodes;
3445 ++NodesCombined;
Bill Wendling832171c2006-12-07 20:04:42 +00003446 DOUT << "\nReplacing.5 "; DEBUG(N->dump());
3447 DOUT << "\nWith: "; DEBUG(Result.Val->dump(&DAG));
3448 DOUT << '\n';
Chris Lattner9f1794e2006-11-11 00:56:29 +00003449 std::vector<SDNode*> NowDead;
3450 if (isLoad) {
3451 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(0),
Chris Lattner448f2192006-11-11 00:39:41 +00003452 NowDead);
Chris Lattner9f1794e2006-11-11 00:56:29 +00003453 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1), Result.getValue(2),
3454 NowDead);
3455 } else {
3456 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(1),
3457 NowDead);
Chris Lattner448f2192006-11-11 00:39:41 +00003458 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00003459
3460 // Nodes can end up on the worklist more than once. Make sure we do
3461 // not process a node that has been replaced.
3462 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
3463 removeFromWorkList(NowDead[i]);
3464 // Finally, since the node is now dead, remove it from the graph.
3465 DAG.DeleteNode(N);
3466
3467 // Replace the uses of Use with uses of the updated base value.
3468 DAG.ReplaceAllUsesOfValueWith(SDOperand(Op, 0),
3469 Result.getValue(isLoad ? 1 : 0),
3470 NowDead);
3471 removeFromWorkList(Op);
3472 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
3473 removeFromWorkList(NowDead[i]);
3474 DAG.DeleteNode(Op);
3475
3476 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00003477 }
3478 }
3479 }
3480 return false;
3481}
3482
3483
Chris Lattner01a22022005-10-10 22:04:48 +00003484SDOperand DAGCombiner::visitLOAD(SDNode *N) {
Evan Cheng466685d2006-10-09 20:57:25 +00003485 LoadSDNode *LD = cast<LoadSDNode>(N);
3486 SDOperand Chain = LD->getChain();
3487 SDOperand Ptr = LD->getBasePtr();
Evan Cheng45a7ca92007-05-01 00:38:21 +00003488
3489 // If load is not volatile and there are no uses of the loaded value (and
3490 // the updated indexed value in case of indexed loads), change uses of the
3491 // chain value into uses of the chain input (i.e. delete the dead load).
3492 if (!LD->isVolatile()) {
Evan Cheng498f5592007-05-01 08:53:39 +00003493 if (N->getValueType(1) == MVT::Other) {
3494 // Unindexed loads.
3495 if (N->hasNUsesOfValue(0, 0))
3496 return CombineTo(N, DAG.getNode(ISD::UNDEF, N->getValueType(0)), Chain);
3497 } else {
3498 // Indexed loads.
3499 assert(N->getValueType(2) == MVT::Other && "Malformed indexed loads?");
3500 if (N->hasNUsesOfValue(0, 0) && N->hasNUsesOfValue(0, 1)) {
3501 SDOperand Undef0 = DAG.getNode(ISD::UNDEF, N->getValueType(0));
3502 SDOperand Undef1 = DAG.getNode(ISD::UNDEF, N->getValueType(1));
3503 SDOperand To[] = { Undef0, Undef1, Chain };
3504 return CombineTo(N, To, 3);
Evan Cheng45a7ca92007-05-01 00:38:21 +00003505 }
Evan Cheng45a7ca92007-05-01 00:38:21 +00003506 }
3507 }
Chris Lattner01a22022005-10-10 22:04:48 +00003508
3509 // If this load is directly stored, replace the load value with the stored
3510 // value.
3511 // TODO: Handle store large -> read small portion.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00003512 // TODO: Handle TRUNCSTORE/LOADEXT
3513 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00003514 if (ISD::isNON_TRUNCStore(Chain.Val)) {
3515 StoreSDNode *PrevST = cast<StoreSDNode>(Chain);
3516 if (PrevST->getBasePtr() == Ptr &&
3517 PrevST->getValue().getValueType() == N->getValueType(0))
Jim Laskeyc2b19f32006-10-11 17:47:52 +00003518 return CombineTo(N, Chain.getOperand(1), Chain);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003519 }
Jim Laskeyc2b19f32006-10-11 17:47:52 +00003520 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00003521
Jim Laskey7ca56af2006-10-11 13:47:09 +00003522 if (CombinerAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00003523 // Walk up chain skipping non-aliasing memory nodes.
3524 SDOperand BetterChain = FindBetterChain(N, Chain);
3525
Jim Laskey6ff23e52006-10-04 16:53:27 +00003526 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00003527 if (Chain != BetterChain) {
Jim Laskeyc2b19f32006-10-11 17:47:52 +00003528 SDOperand ReplLoad;
3529
Jim Laskey279f0532006-09-25 16:29:54 +00003530 // Replace the chain to void dependency.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00003531 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
3532 ReplLoad = DAG.getLoad(N->getValueType(0), BetterChain, Ptr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00003533 LD->getSrcValue(), LD->getSrcValueOffset(),
3534 LD->isVolatile(), LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00003535 } else {
3536 ReplLoad = DAG.getExtLoad(LD->getExtensionType(),
3537 LD->getValueType(0),
3538 BetterChain, Ptr, LD->getSrcValue(),
3539 LD->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003540 LD->getLoadedVT(),
3541 LD->isVolatile(),
3542 LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00003543 }
Jim Laskey279f0532006-09-25 16:29:54 +00003544
Jim Laskey6ff23e52006-10-04 16:53:27 +00003545 // Create token factor to keep old chain connected.
Jim Laskey288af5e2006-09-25 19:32:58 +00003546 SDOperand Token = DAG.getNode(ISD::TokenFactor, MVT::Other,
3547 Chain, ReplLoad.getValue(1));
Jim Laskey6ff23e52006-10-04 16:53:27 +00003548
Jim Laskey274062c2006-10-13 23:32:28 +00003549 // Replace uses with load result and token factor. Don't add users
3550 // to work list.
3551 return CombineTo(N, ReplLoad.getValue(0), Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00003552 }
3553 }
3554
Evan Cheng7fc033a2006-11-03 03:06:21 +00003555 // Try transforming N to an indexed load.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00003556 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Evan Cheng7fc033a2006-11-03 03:06:21 +00003557 return SDOperand(N, 0);
3558
Chris Lattner01a22022005-10-10 22:04:48 +00003559 return SDOperand();
3560}
3561
Chris Lattner87514ca2005-10-10 22:31:19 +00003562SDOperand DAGCombiner::visitSTORE(SDNode *N) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00003563 StoreSDNode *ST = cast<StoreSDNode>(N);
3564 SDOperand Chain = ST->getChain();
3565 SDOperand Value = ST->getValue();
3566 SDOperand Ptr = ST->getBasePtr();
Jim Laskey7aed46c2006-10-11 18:55:16 +00003567
Evan Cheng59d5b682007-05-07 21:27:48 +00003568 // If this is a store of a bit convert, store the input value if the
Evan Cheng2c4f9432007-05-09 21:49:47 +00003569 // resultant store does not need a higher alignment than the original.
Dale Johannesen98a6c622007-05-16 22:45:30 +00003570 if (Value.getOpcode() == ISD::BIT_CONVERT && !ST->isTruncatingStore() &&
3571 ST->getAddressingMode() == ISD::UNINDEXED) {
Evan Cheng59d5b682007-05-07 21:27:48 +00003572 unsigned Align = ST->getAlignment();
3573 MVT::ValueType SVT = Value.getOperand(0).getValueType();
3574 unsigned OrigAlign = TLI.getTargetMachine().getTargetData()->
Dan Gohmanfcc4dd92007-05-18 18:41:29 +00003575 getABITypeAlignment(MVT::getTypeForValueType(SVT));
Evan Chengc2cd2b22007-05-07 21:36:06 +00003576 if (Align <= OrigAlign && TLI.isOperationLegal(ISD::STORE, SVT))
Evan Cheng59d5b682007-05-07 21:27:48 +00003577 return DAG.getStore(Chain, Value.getOperand(0), Ptr, ST->getSrcValue(),
3578 ST->getSrcValueOffset());
Jim Laskey279f0532006-09-25 16:29:54 +00003579 }
3580
Nate Begeman2cbba892006-12-11 02:23:46 +00003581 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Nate Begeman2cbba892006-12-11 02:23:46 +00003582 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Value)) {
Evan Cheng25ece662006-12-11 17:25:19 +00003583 if (Value.getOpcode() != ISD::TargetConstantFP) {
3584 SDOperand Tmp;
Chris Lattner62be1a72006-12-12 04:16:14 +00003585 switch (CFP->getValueType(0)) {
3586 default: assert(0 && "Unknown FP type");
3587 case MVT::f32:
3588 if (!AfterLegalize || TLI.isTypeLegal(MVT::i32)) {
3589 Tmp = DAG.getConstant(FloatToBits(CFP->getValue()), MVT::i32);
3590 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
3591 ST->getSrcValueOffset());
3592 }
3593 break;
3594 case MVT::f64:
3595 if (!AfterLegalize || TLI.isTypeLegal(MVT::i64)) {
3596 Tmp = DAG.getConstant(DoubleToBits(CFP->getValue()), MVT::i64);
3597 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
3598 ST->getSrcValueOffset());
3599 } else if (TLI.isTypeLegal(MVT::i32)) {
3600 // Many FP stores are not make apparent until after legalize, e.g. for
3601 // argument passing. Since this is so common, custom legalize the
3602 // 64-bit integer store into two 32-bit stores.
3603 uint64_t Val = DoubleToBits(CFP->getValue());
3604 SDOperand Lo = DAG.getConstant(Val & 0xFFFFFFFF, MVT::i32);
3605 SDOperand Hi = DAG.getConstant(Val >> 32, MVT::i32);
3606 if (!TLI.isLittleEndian()) std::swap(Lo, Hi);
3607
3608 SDOperand St0 = DAG.getStore(Chain, Lo, Ptr, ST->getSrcValue(),
3609 ST->getSrcValueOffset());
3610 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3611 DAG.getConstant(4, Ptr.getValueType()));
3612 SDOperand St1 = DAG.getStore(Chain, Hi, Ptr, ST->getSrcValue(),
3613 ST->getSrcValueOffset()+4);
3614 return DAG.getNode(ISD::TokenFactor, MVT::Other, St0, St1);
3615 }
3616 break;
Evan Cheng25ece662006-12-11 17:25:19 +00003617 }
Nate Begeman2cbba892006-12-11 02:23:46 +00003618 }
Nate Begeman2cbba892006-12-11 02:23:46 +00003619 }
3620
Jim Laskey279f0532006-09-25 16:29:54 +00003621 if (CombinerAA) {
3622 // Walk up chain skipping non-aliasing memory nodes.
3623 SDOperand BetterChain = FindBetterChain(N, Chain);
3624
Jim Laskey6ff23e52006-10-04 16:53:27 +00003625 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00003626 if (Chain != BetterChain) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00003627 // Replace the chain to avoid dependency.
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00003628 SDOperand ReplStore;
3629 if (ST->isTruncatingStore()) {
3630 ReplStore = DAG.getTruncStore(BetterChain, Value, Ptr,
3631 ST->getSrcValue(),ST->getSrcValueOffset(), ST->getStoredVT());
3632 } else {
3633 ReplStore = DAG.getStore(BetterChain, Value, Ptr,
3634 ST->getSrcValue(), ST->getSrcValueOffset());
3635 }
3636
Jim Laskey279f0532006-09-25 16:29:54 +00003637 // Create token to keep both nodes around.
Jim Laskey274062c2006-10-13 23:32:28 +00003638 SDOperand Token =
3639 DAG.getNode(ISD::TokenFactor, MVT::Other, Chain, ReplStore);
3640
3641 // Don't add users to work list.
3642 return CombineTo(N, Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00003643 }
Jim Laskeyd1aed7a2006-09-21 16:28:59 +00003644 }
Chris Lattnerc33baaa2005-12-23 05:48:07 +00003645
Evan Cheng33dbedc2006-11-05 09:31:14 +00003646 // Try transforming N to an indexed store.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00003647 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Evan Cheng33dbedc2006-11-05 09:31:14 +00003648 return SDOperand(N, 0);
3649
Chris Lattner87514ca2005-10-10 22:31:19 +00003650 return SDOperand();
3651}
3652
Chris Lattnerca242442006-03-19 01:27:56 +00003653SDOperand DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
3654 SDOperand InVec = N->getOperand(0);
3655 SDOperand InVal = N->getOperand(1);
3656 SDOperand EltNo = N->getOperand(2);
3657
3658 // If the invec is a BUILD_VECTOR and if EltNo is a constant, build a new
3659 // vector with the inserted element.
3660 if (InVec.getOpcode() == ISD::BUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
3661 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003662 SmallVector<SDOperand, 8> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
Chris Lattnerca242442006-03-19 01:27:56 +00003663 if (Elt < Ops.size())
3664 Ops[Elt] = InVal;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003665 return DAG.getNode(ISD::BUILD_VECTOR, InVec.getValueType(),
3666 &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00003667 }
3668
3669 return SDOperand();
3670}
3671
3672SDOperand DAGCombiner::visitVINSERT_VECTOR_ELT(SDNode *N) {
3673 SDOperand InVec = N->getOperand(0);
3674 SDOperand InVal = N->getOperand(1);
3675 SDOperand EltNo = N->getOperand(2);
3676 SDOperand NumElts = N->getOperand(3);
3677 SDOperand EltType = N->getOperand(4);
3678
3679 // If the invec is a VBUILD_VECTOR and if EltNo is a constant, build a new
3680 // vector with the inserted element.
3681 if (InVec.getOpcode() == ISD::VBUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
3682 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003683 SmallVector<SDOperand, 8> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
Chris Lattnerca242442006-03-19 01:27:56 +00003684 if (Elt < Ops.size()-2)
3685 Ops[Elt] = InVal;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003686 return DAG.getNode(ISD::VBUILD_VECTOR, InVec.getValueType(),
3687 &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00003688 }
3689
3690 return SDOperand();
3691}
3692
Chris Lattnerd7648c82006-03-28 20:28:38 +00003693SDOperand DAGCombiner::visitVBUILD_VECTOR(SDNode *N) {
3694 unsigned NumInScalars = N->getNumOperands()-2;
3695 SDOperand NumElts = N->getOperand(NumInScalars);
3696 SDOperand EltType = N->getOperand(NumInScalars+1);
3697
3698 // Check to see if this is a VBUILD_VECTOR of a bunch of VEXTRACT_VECTOR_ELT
3699 // operations. If so, and if the EXTRACT_ELT vector inputs come from at most
3700 // two distinct vectors, turn this into a shuffle node.
3701 SDOperand VecIn1, VecIn2;
3702 for (unsigned i = 0; i != NumInScalars; ++i) {
3703 // Ignore undef inputs.
3704 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
3705
3706 // If this input is something other than a VEXTRACT_VECTOR_ELT with a
3707 // constant index, bail out.
3708 if (N->getOperand(i).getOpcode() != ISD::VEXTRACT_VECTOR_ELT ||
3709 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
3710 VecIn1 = VecIn2 = SDOperand(0, 0);
3711 break;
3712 }
3713
3714 // If the input vector type disagrees with the result of the vbuild_vector,
3715 // we can't make a shuffle.
3716 SDOperand ExtractedFromVec = N->getOperand(i).getOperand(0);
3717 if (*(ExtractedFromVec.Val->op_end()-2) != NumElts ||
3718 *(ExtractedFromVec.Val->op_end()-1) != EltType) {
3719 VecIn1 = VecIn2 = SDOperand(0, 0);
3720 break;
3721 }
3722
3723 // Otherwise, remember this. We allow up to two distinct input vectors.
3724 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
3725 continue;
3726
3727 if (VecIn1.Val == 0) {
3728 VecIn1 = ExtractedFromVec;
3729 } else if (VecIn2.Val == 0) {
3730 VecIn2 = ExtractedFromVec;
3731 } else {
3732 // Too many inputs.
3733 VecIn1 = VecIn2 = SDOperand(0, 0);
3734 break;
3735 }
3736 }
3737
3738 // If everything is good, we can make a shuffle operation.
3739 if (VecIn1.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003740 SmallVector<SDOperand, 8> BuildVecIndices;
Chris Lattnerd7648c82006-03-28 20:28:38 +00003741 for (unsigned i = 0; i != NumInScalars; ++i) {
3742 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
Evan Cheng597a3bd2007-01-20 10:10:26 +00003743 BuildVecIndices.push_back(DAG.getNode(ISD::UNDEF, TLI.getPointerTy()));
Chris Lattnerd7648c82006-03-28 20:28:38 +00003744 continue;
3745 }
3746
3747 SDOperand Extract = N->getOperand(i);
3748
3749 // If extracting from the first vector, just use the index directly.
3750 if (Extract.getOperand(0) == VecIn1) {
3751 BuildVecIndices.push_back(Extract.getOperand(1));
3752 continue;
3753 }
3754
3755 // Otherwise, use InIdx + VecSize
3756 unsigned Idx = cast<ConstantSDNode>(Extract.getOperand(1))->getValue();
Evan Cheng597a3bd2007-01-20 10:10:26 +00003757 BuildVecIndices.push_back(DAG.getConstant(Idx+NumInScalars,
3758 TLI.getPointerTy()));
Chris Lattnerd7648c82006-03-28 20:28:38 +00003759 }
3760
3761 // Add count and size info.
3762 BuildVecIndices.push_back(NumElts);
Evan Cheng597a3bd2007-01-20 10:10:26 +00003763 BuildVecIndices.push_back(DAG.getValueType(TLI.getPointerTy()));
Chris Lattnerd7648c82006-03-28 20:28:38 +00003764
3765 // Return the new VVECTOR_SHUFFLE node.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003766 SDOperand Ops[5];
3767 Ops[0] = VecIn1;
Chris Lattnercef896e2006-03-28 22:19:47 +00003768 if (VecIn2.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003769 Ops[1] = VecIn2;
Chris Lattnercef896e2006-03-28 22:19:47 +00003770 } else {
Dan Gohmanfcc4dd92007-05-18 18:41:29 +00003771 // Use an undef vbuild_vector as input for the second operand.
Chris Lattnercef896e2006-03-28 22:19:47 +00003772 std::vector<SDOperand> UnOps(NumInScalars,
3773 DAG.getNode(ISD::UNDEF,
3774 cast<VTSDNode>(EltType)->getVT()));
3775 UnOps.push_back(NumElts);
3776 UnOps.push_back(EltType);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003777 Ops[1] = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
3778 &UnOps[0], UnOps.size());
3779 AddToWorkList(Ops[1].Val);
Chris Lattnercef896e2006-03-28 22:19:47 +00003780 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003781 Ops[2] = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
3782 &BuildVecIndices[0], BuildVecIndices.size());
3783 Ops[3] = NumElts;
3784 Ops[4] = EltType;
3785 return DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector, Ops, 5);
Chris Lattnerd7648c82006-03-28 20:28:38 +00003786 }
3787
3788 return SDOperand();
3789}
3790
Chris Lattner66445d32006-03-28 22:11:53 +00003791SDOperand DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
Chris Lattnerf1d0c622006-03-31 22:16:43 +00003792 SDOperand ShufMask = N->getOperand(2);
3793 unsigned NumElts = ShufMask.getNumOperands();
3794
3795 // If the shuffle mask is an identity operation on the LHS, return the LHS.
3796 bool isIdentity = true;
3797 for (unsigned i = 0; i != NumElts; ++i) {
3798 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
3799 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
3800 isIdentity = false;
3801 break;
3802 }
3803 }
3804 if (isIdentity) return N->getOperand(0);
3805
3806 // If the shuffle mask is an identity operation on the RHS, return the RHS.
3807 isIdentity = true;
3808 for (unsigned i = 0; i != NumElts; ++i) {
3809 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
3810 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
3811 isIdentity = false;
3812 break;
3813 }
3814 }
3815 if (isIdentity) return N->getOperand(1);
Evan Chenge7bec0d2006-07-20 22:44:41 +00003816
3817 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
3818 // needed at all.
3819 bool isUnary = true;
Evan Cheng917ec982006-07-21 08:25:53 +00003820 bool isSplat = true;
Evan Chenge7bec0d2006-07-20 22:44:41 +00003821 int VecNum = -1;
Reid Spencer9160a6a2006-07-25 20:44:41 +00003822 unsigned BaseIdx = 0;
Evan Chenge7bec0d2006-07-20 22:44:41 +00003823 for (unsigned i = 0; i != NumElts; ++i)
3824 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
3825 unsigned Idx = cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue();
3826 int V = (Idx < NumElts) ? 0 : 1;
Evan Cheng917ec982006-07-21 08:25:53 +00003827 if (VecNum == -1) {
Evan Chenge7bec0d2006-07-20 22:44:41 +00003828 VecNum = V;
Evan Cheng917ec982006-07-21 08:25:53 +00003829 BaseIdx = Idx;
3830 } else {
3831 if (BaseIdx != Idx)
3832 isSplat = false;
3833 if (VecNum != V) {
3834 isUnary = false;
3835 break;
3836 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00003837 }
3838 }
3839
3840 SDOperand N0 = N->getOperand(0);
3841 SDOperand N1 = N->getOperand(1);
3842 // Normalize unary shuffle so the RHS is undef.
3843 if (isUnary && VecNum == 1)
3844 std::swap(N0, N1);
3845
Evan Cheng917ec982006-07-21 08:25:53 +00003846 // If it is a splat, check if the argument vector is a build_vector with
3847 // all scalar elements the same.
3848 if (isSplat) {
3849 SDNode *V = N0.Val;
3850 if (V->getOpcode() == ISD::BIT_CONVERT)
3851 V = V->getOperand(0).Val;
3852 if (V->getOpcode() == ISD::BUILD_VECTOR) {
3853 unsigned NumElems = V->getNumOperands()-2;
3854 if (NumElems > BaseIdx) {
3855 SDOperand Base;
3856 bool AllSame = true;
3857 for (unsigned i = 0; i != NumElems; ++i) {
3858 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
3859 Base = V->getOperand(i);
3860 break;
3861 }
3862 }
3863 // Splat of <u, u, u, u>, return <u, u, u, u>
3864 if (!Base.Val)
3865 return N0;
3866 for (unsigned i = 0; i != NumElems; ++i) {
3867 if (V->getOperand(i).getOpcode() != ISD::UNDEF &&
3868 V->getOperand(i) != Base) {
3869 AllSame = false;
3870 break;
3871 }
3872 }
3873 // Splat of <x, x, x, x>, return <x, x, x, x>
3874 if (AllSame)
3875 return N0;
3876 }
3877 }
3878 }
3879
Evan Chenge7bec0d2006-07-20 22:44:41 +00003880 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
3881 // into an undef.
3882 if (isUnary || N0 == N1) {
3883 if (N0.getOpcode() == ISD::UNDEF)
Evan Chengc04766a2006-04-06 23:20:43 +00003884 return DAG.getNode(ISD::UNDEF, N->getValueType(0));
Chris Lattner66445d32006-03-28 22:11:53 +00003885 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
3886 // first operand.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003887 SmallVector<SDOperand, 8> MappedOps;
Chris Lattner66445d32006-03-28 22:11:53 +00003888 for (unsigned i = 0, e = ShufMask.getNumOperands(); i != e; ++i) {
Evan Chengc04766a2006-04-06 23:20:43 +00003889 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
3890 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) {
3891 MappedOps.push_back(ShufMask.getOperand(i));
3892 } else {
Chris Lattner66445d32006-03-28 22:11:53 +00003893 unsigned NewIdx =
3894 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
3895 MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32));
Chris Lattner66445d32006-03-28 22:11:53 +00003896 }
3897 }
3898 ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMask.getValueType(),
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003899 &MappedOps[0], MappedOps.size());
Chris Lattner3e104b12006-04-08 04:15:24 +00003900 AddToWorkList(ShufMask.Val);
Chris Lattner66445d32006-03-28 22:11:53 +00003901 return DAG.getNode(ISD::VECTOR_SHUFFLE, N->getValueType(0),
Evan Chenge7bec0d2006-07-20 22:44:41 +00003902 N0,
Chris Lattner66445d32006-03-28 22:11:53 +00003903 DAG.getNode(ISD::UNDEF, N->getValueType(0)),
3904 ShufMask);
3905 }
3906
3907 return SDOperand();
3908}
3909
Chris Lattnerf1d0c622006-03-31 22:16:43 +00003910SDOperand DAGCombiner::visitVVECTOR_SHUFFLE(SDNode *N) {
3911 SDOperand ShufMask = N->getOperand(2);
3912 unsigned NumElts = ShufMask.getNumOperands()-2;
3913
3914 // If the shuffle mask is an identity operation on the LHS, return the LHS.
3915 bool isIdentity = true;
3916 for (unsigned i = 0; i != NumElts; ++i) {
3917 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
3918 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
3919 isIdentity = false;
3920 break;
3921 }
3922 }
3923 if (isIdentity) return N->getOperand(0);
3924
3925 // If the shuffle mask is an identity operation on the RHS, return the RHS.
3926 isIdentity = true;
3927 for (unsigned i = 0; i != NumElts; ++i) {
3928 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
3929 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
3930 isIdentity = false;
3931 break;
3932 }
3933 }
3934 if (isIdentity) return N->getOperand(1);
3935
Evan Chenge7bec0d2006-07-20 22:44:41 +00003936 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
3937 // needed at all.
3938 bool isUnary = true;
Evan Cheng917ec982006-07-21 08:25:53 +00003939 bool isSplat = true;
Evan Chenge7bec0d2006-07-20 22:44:41 +00003940 int VecNum = -1;
Reid Spencer9160a6a2006-07-25 20:44:41 +00003941 unsigned BaseIdx = 0;
Evan Chenge7bec0d2006-07-20 22:44:41 +00003942 for (unsigned i = 0; i != NumElts; ++i)
3943 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
3944 unsigned Idx = cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue();
3945 int V = (Idx < NumElts) ? 0 : 1;
Evan Cheng917ec982006-07-21 08:25:53 +00003946 if (VecNum == -1) {
Evan Chenge7bec0d2006-07-20 22:44:41 +00003947 VecNum = V;
Evan Cheng917ec982006-07-21 08:25:53 +00003948 BaseIdx = Idx;
3949 } else {
3950 if (BaseIdx != Idx)
3951 isSplat = false;
3952 if (VecNum != V) {
3953 isUnary = false;
3954 break;
3955 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00003956 }
3957 }
3958
3959 SDOperand N0 = N->getOperand(0);
3960 SDOperand N1 = N->getOperand(1);
3961 // Normalize unary shuffle so the RHS is undef.
3962 if (isUnary && VecNum == 1)
3963 std::swap(N0, N1);
3964
Evan Cheng917ec982006-07-21 08:25:53 +00003965 // If it is a splat, check if the argument vector is a build_vector with
3966 // all scalar elements the same.
3967 if (isSplat) {
3968 SDNode *V = N0.Val;
Evan Cheng59569222006-10-16 22:49:37 +00003969
3970 // If this is a vbit convert that changes the element type of the vector but
3971 // not the number of vector elements, look through it. Be careful not to
3972 // look though conversions that change things like v4f32 to v2f64.
3973 if (V->getOpcode() == ISD::VBIT_CONVERT) {
3974 SDOperand ConvInput = V->getOperand(0);
Evan Cheng5d04a1a2006-10-17 17:06:35 +00003975 if (ConvInput.getValueType() == MVT::Vector &&
3976 NumElts ==
Evan Cheng59569222006-10-16 22:49:37 +00003977 ConvInput.getConstantOperandVal(ConvInput.getNumOperands()-2))
3978 V = ConvInput.Val;
3979 }
3980
Evan Cheng917ec982006-07-21 08:25:53 +00003981 if (V->getOpcode() == ISD::VBUILD_VECTOR) {
3982 unsigned NumElems = V->getNumOperands()-2;
3983 if (NumElems > BaseIdx) {
3984 SDOperand Base;
3985 bool AllSame = true;
3986 for (unsigned i = 0; i != NumElems; ++i) {
3987 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
3988 Base = V->getOperand(i);
3989 break;
3990 }
3991 }
3992 // Splat of <u, u, u, u>, return <u, u, u, u>
3993 if (!Base.Val)
3994 return N0;
3995 for (unsigned i = 0; i != NumElems; ++i) {
3996 if (V->getOperand(i).getOpcode() != ISD::UNDEF &&
3997 V->getOperand(i) != Base) {
3998 AllSame = false;
3999 break;
4000 }
4001 }
4002 // Splat of <x, x, x, x>, return <x, x, x, x>
4003 if (AllSame)
4004 return N0;
4005 }
4006 }
4007 }
4008
Evan Chenge7bec0d2006-07-20 22:44:41 +00004009 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
4010 // into an undef.
4011 if (isUnary || N0 == N1) {
Chris Lattner17614ea2006-04-08 05:34:25 +00004012 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
4013 // first operand.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004014 SmallVector<SDOperand, 8> MappedOps;
Chris Lattner17614ea2006-04-08 05:34:25 +00004015 for (unsigned i = 0; i != NumElts; ++i) {
4016 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
4017 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) {
4018 MappedOps.push_back(ShufMask.getOperand(i));
4019 } else {
4020 unsigned NewIdx =
4021 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
4022 MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32));
4023 }
4024 }
4025 // Add the type/#elts values.
4026 MappedOps.push_back(ShufMask.getOperand(NumElts));
4027 MappedOps.push_back(ShufMask.getOperand(NumElts+1));
4028
4029 ShufMask = DAG.getNode(ISD::VBUILD_VECTOR, ShufMask.getValueType(),
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004030 &MappedOps[0], MappedOps.size());
Chris Lattner17614ea2006-04-08 05:34:25 +00004031 AddToWorkList(ShufMask.Val);
4032
4033 // Build the undef vector.
4034 SDOperand UDVal = DAG.getNode(ISD::UNDEF, MappedOps[0].getValueType());
4035 for (unsigned i = 0; i != NumElts; ++i)
4036 MappedOps[i] = UDVal;
Evan Chenge7bec0d2006-07-20 22:44:41 +00004037 MappedOps[NumElts ] = *(N0.Val->op_end()-2);
4038 MappedOps[NumElts+1] = *(N0.Val->op_end()-1);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004039 UDVal = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
4040 &MappedOps[0], MappedOps.size());
Chris Lattner17614ea2006-04-08 05:34:25 +00004041
4042 return DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector,
Evan Chenge7bec0d2006-07-20 22:44:41 +00004043 N0, UDVal, ShufMask,
Chris Lattner17614ea2006-04-08 05:34:25 +00004044 MappedOps[NumElts], MappedOps[NumElts+1]);
4045 }
4046
Chris Lattnerf1d0c622006-03-31 22:16:43 +00004047 return SDOperand();
4048}
4049
Evan Cheng44f1f092006-04-20 08:56:16 +00004050/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
4051/// a VAND to a vector_shuffle with the destination vector and a zero vector.
4052/// e.g. VAND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
4053/// vector_shuffle V, Zero, <0, 4, 2, 4>
4054SDOperand DAGCombiner::XformToShuffleWithZero(SDNode *N) {
4055 SDOperand LHS = N->getOperand(0);
4056 SDOperand RHS = N->getOperand(1);
4057 if (N->getOpcode() == ISD::VAND) {
4058 SDOperand DstVecSize = *(LHS.Val->op_end()-2);
4059 SDOperand DstVecEVT = *(LHS.Val->op_end()-1);
4060 if (RHS.getOpcode() == ISD::VBIT_CONVERT)
4061 RHS = RHS.getOperand(0);
4062 if (RHS.getOpcode() == ISD::VBUILD_VECTOR) {
4063 std::vector<SDOperand> IdxOps;
4064 unsigned NumOps = RHS.getNumOperands();
4065 unsigned NumElts = NumOps-2;
4066 MVT::ValueType EVT = cast<VTSDNode>(RHS.getOperand(NumOps-1))->getVT();
4067 for (unsigned i = 0; i != NumElts; ++i) {
4068 SDOperand Elt = RHS.getOperand(i);
4069 if (!isa<ConstantSDNode>(Elt))
4070 return SDOperand();
4071 else if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
4072 IdxOps.push_back(DAG.getConstant(i, EVT));
4073 else if (cast<ConstantSDNode>(Elt)->isNullValue())
4074 IdxOps.push_back(DAG.getConstant(NumElts, EVT));
4075 else
4076 return SDOperand();
4077 }
4078
4079 // Let's see if the target supports this vector_shuffle.
4080 if (!TLI.isVectorClearMaskLegal(IdxOps, EVT, DAG))
4081 return SDOperand();
4082
4083 // Return the new VVECTOR_SHUFFLE node.
4084 SDOperand NumEltsNode = DAG.getConstant(NumElts, MVT::i32);
4085 SDOperand EVTNode = DAG.getValueType(EVT);
4086 std::vector<SDOperand> Ops;
Chris Lattner516b9622006-09-14 20:50:57 +00004087 LHS = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, LHS, NumEltsNode,
4088 EVTNode);
Evan Cheng44f1f092006-04-20 08:56:16 +00004089 Ops.push_back(LHS);
4090 AddToWorkList(LHS.Val);
4091 std::vector<SDOperand> ZeroOps(NumElts, DAG.getConstant(0, EVT));
4092 ZeroOps.push_back(NumEltsNode);
4093 ZeroOps.push_back(EVTNode);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004094 Ops.push_back(DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
4095 &ZeroOps[0], ZeroOps.size()));
Evan Cheng44f1f092006-04-20 08:56:16 +00004096 IdxOps.push_back(NumEltsNode);
4097 IdxOps.push_back(EVTNode);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004098 Ops.push_back(DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
4099 &IdxOps[0], IdxOps.size()));
Evan Cheng44f1f092006-04-20 08:56:16 +00004100 Ops.push_back(NumEltsNode);
4101 Ops.push_back(EVTNode);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004102 SDOperand Result = DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector,
4103 &Ops[0], Ops.size());
Evan Cheng44f1f092006-04-20 08:56:16 +00004104 if (NumEltsNode != DstVecSize || EVTNode != DstVecEVT) {
4105 Result = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Result,
4106 DstVecSize, DstVecEVT);
4107 }
4108 return Result;
4109 }
4110 }
4111 return SDOperand();
4112}
4113
Chris Lattneredab1b92006-04-02 03:25:57 +00004114/// visitVBinOp - Visit a binary vector operation, like VADD. IntOp indicates
4115/// the scalar operation of the vop if it is operating on an integer vector
4116/// (e.g. ADD) and FPOp indicates the FP version (e.g. FADD).
4117SDOperand DAGCombiner::visitVBinOp(SDNode *N, ISD::NodeType IntOp,
4118 ISD::NodeType FPOp) {
4119 MVT::ValueType EltType = cast<VTSDNode>(*(N->op_end()-1))->getVT();
4120 ISD::NodeType ScalarOp = MVT::isInteger(EltType) ? IntOp : FPOp;
4121 SDOperand LHS = N->getOperand(0);
4122 SDOperand RHS = N->getOperand(1);
Evan Cheng44f1f092006-04-20 08:56:16 +00004123 SDOperand Shuffle = XformToShuffleWithZero(N);
4124 if (Shuffle.Val) return Shuffle;
4125
Chris Lattneredab1b92006-04-02 03:25:57 +00004126 // If the LHS and RHS are VBUILD_VECTOR nodes, see if we can constant fold
4127 // this operation.
4128 if (LHS.getOpcode() == ISD::VBUILD_VECTOR &&
4129 RHS.getOpcode() == ISD::VBUILD_VECTOR) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004130 SmallVector<SDOperand, 8> Ops;
Chris Lattneredab1b92006-04-02 03:25:57 +00004131 for (unsigned i = 0, e = LHS.getNumOperands()-2; i != e; ++i) {
4132 SDOperand LHSOp = LHS.getOperand(i);
4133 SDOperand RHSOp = RHS.getOperand(i);
4134 // If these two elements can't be folded, bail out.
4135 if ((LHSOp.getOpcode() != ISD::UNDEF &&
4136 LHSOp.getOpcode() != ISD::Constant &&
4137 LHSOp.getOpcode() != ISD::ConstantFP) ||
4138 (RHSOp.getOpcode() != ISD::UNDEF &&
4139 RHSOp.getOpcode() != ISD::Constant &&
4140 RHSOp.getOpcode() != ISD::ConstantFP))
4141 break;
Evan Cheng7b336a82006-05-31 06:08:35 +00004142 // Can't fold divide by zero.
4143 if (N->getOpcode() == ISD::VSDIV || N->getOpcode() == ISD::VUDIV) {
4144 if ((RHSOp.getOpcode() == ISD::Constant &&
4145 cast<ConstantSDNode>(RHSOp.Val)->isNullValue()) ||
4146 (RHSOp.getOpcode() == ISD::ConstantFP &&
4147 !cast<ConstantFPSDNode>(RHSOp.Val)->getValue()))
4148 break;
4149 }
Chris Lattneredab1b92006-04-02 03:25:57 +00004150 Ops.push_back(DAG.getNode(ScalarOp, EltType, LHSOp, RHSOp));
Chris Lattner3e104b12006-04-08 04:15:24 +00004151 AddToWorkList(Ops.back().Val);
Chris Lattneredab1b92006-04-02 03:25:57 +00004152 assert((Ops.back().getOpcode() == ISD::UNDEF ||
4153 Ops.back().getOpcode() == ISD::Constant ||
4154 Ops.back().getOpcode() == ISD::ConstantFP) &&
4155 "Scalar binop didn't fold!");
4156 }
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00004157
4158 if (Ops.size() == LHS.getNumOperands()-2) {
4159 Ops.push_back(*(LHS.Val->op_end()-2));
4160 Ops.push_back(*(LHS.Val->op_end()-1));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004161 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00004162 }
Chris Lattneredab1b92006-04-02 03:25:57 +00004163 }
4164
4165 return SDOperand();
4166}
4167
Nate Begeman44728a72005-09-19 22:34:01 +00004168SDOperand DAGCombiner::SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2){
Nate Begemanf845b452005-10-08 00:29:44 +00004169 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
4170
4171 SDOperand SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
4172 cast<CondCodeSDNode>(N0.getOperand(2))->get());
4173 // If we got a simplified select_cc node back from SimplifySelectCC, then
4174 // break it down into a new SETCC node, and a new SELECT node, and then return
4175 // the SELECT node, since we were called with a SELECT node.
4176 if (SCC.Val) {
4177 // Check to see if we got a select_cc back (to turn into setcc/select).
4178 // Otherwise, just return whatever node we got back, like fabs.
4179 if (SCC.getOpcode() == ISD::SELECT_CC) {
4180 SDOperand SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
4181 SCC.getOperand(0), SCC.getOperand(1),
4182 SCC.getOperand(4));
Chris Lattner5750df92006-03-01 04:03:14 +00004183 AddToWorkList(SETCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004184 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
4185 SCC.getOperand(3), SETCC);
4186 }
4187 return SCC;
4188 }
Nate Begeman44728a72005-09-19 22:34:01 +00004189 return SDOperand();
4190}
4191
Chris Lattner40c62d52005-10-18 06:04:22 +00004192/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
4193/// are the two values being selected between, see if we can simplify the
Chris Lattner729c6d12006-05-27 00:43:02 +00004194/// select. Callers of this should assume that TheSelect is deleted if this
4195/// returns true. As such, they should return the appropriate thing (e.g. the
4196/// node) back to the top-level of the DAG combiner loop to avoid it being
4197/// looked at.
Chris Lattner40c62d52005-10-18 06:04:22 +00004198///
4199bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDOperand LHS,
4200 SDOperand RHS) {
4201
4202 // If this is a select from two identical things, try to pull the operation
4203 // through the select.
4204 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
Chris Lattner40c62d52005-10-18 06:04:22 +00004205 // If this is a load and the token chain is identical, replace the select
4206 // of two loads with a load through a select of the address to load from.
4207 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
4208 // constants have been dropped into the constant pool.
Evan Cheng466685d2006-10-09 20:57:25 +00004209 if (LHS.getOpcode() == ISD::LOAD &&
Chris Lattner40c62d52005-10-18 06:04:22 +00004210 // Token chains must be identical.
Evan Cheng466685d2006-10-09 20:57:25 +00004211 LHS.getOperand(0) == RHS.getOperand(0)) {
4212 LoadSDNode *LLD = cast<LoadSDNode>(LHS);
4213 LoadSDNode *RLD = cast<LoadSDNode>(RHS);
4214
4215 // If this is an EXTLOAD, the VT's must match.
Evan Cheng2e49f092006-10-11 07:10:22 +00004216 if (LLD->getLoadedVT() == RLD->getLoadedVT()) {
Evan Cheng466685d2006-10-09 20:57:25 +00004217 // FIXME: this conflates two src values, discarding one. This is not
4218 // the right thing to do, but nothing uses srcvalues now. When they do,
4219 // turn SrcValue into a list of locations.
4220 SDOperand Addr;
Chris Lattnerc4e664b2007-01-16 05:59:59 +00004221 if (TheSelect->getOpcode() == ISD::SELECT) {
4222 // Check that the condition doesn't reach either load. If so, folding
4223 // this will induce a cycle into the DAG.
4224 if (!LLD->isPredecessor(TheSelect->getOperand(0).Val) &&
4225 !RLD->isPredecessor(TheSelect->getOperand(0).Val)) {
4226 Addr = DAG.getNode(ISD::SELECT, LLD->getBasePtr().getValueType(),
4227 TheSelect->getOperand(0), LLD->getBasePtr(),
4228 RLD->getBasePtr());
4229 }
4230 } else {
4231 // Check that the condition doesn't reach either load. If so, folding
4232 // this will induce a cycle into the DAG.
4233 if (!LLD->isPredecessor(TheSelect->getOperand(0).Val) &&
4234 !RLD->isPredecessor(TheSelect->getOperand(0).Val) &&
4235 !LLD->isPredecessor(TheSelect->getOperand(1).Val) &&
4236 !RLD->isPredecessor(TheSelect->getOperand(1).Val)) {
4237 Addr = DAG.getNode(ISD::SELECT_CC, LLD->getBasePtr().getValueType(),
Evan Cheng466685d2006-10-09 20:57:25 +00004238 TheSelect->getOperand(0),
4239 TheSelect->getOperand(1),
4240 LLD->getBasePtr(), RLD->getBasePtr(),
4241 TheSelect->getOperand(4));
Chris Lattnerc4e664b2007-01-16 05:59:59 +00004242 }
Evan Cheng466685d2006-10-09 20:57:25 +00004243 }
Chris Lattnerc4e664b2007-01-16 05:59:59 +00004244
4245 if (Addr.Val) {
4246 SDOperand Load;
4247 if (LLD->getExtensionType() == ISD::NON_EXTLOAD)
4248 Load = DAG.getLoad(TheSelect->getValueType(0), LLD->getChain(),
4249 Addr,LLD->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00004250 LLD->getSrcValueOffset(),
4251 LLD->isVolatile(),
4252 LLD->getAlignment());
Chris Lattnerc4e664b2007-01-16 05:59:59 +00004253 else {
4254 Load = DAG.getExtLoad(LLD->getExtensionType(),
4255 TheSelect->getValueType(0),
4256 LLD->getChain(), Addr, LLD->getSrcValue(),
4257 LLD->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00004258 LLD->getLoadedVT(),
4259 LLD->isVolatile(),
4260 LLD->getAlignment());
Chris Lattnerc4e664b2007-01-16 05:59:59 +00004261 }
4262 // Users of the select now use the result of the load.
4263 CombineTo(TheSelect, Load);
4264
4265 // Users of the old loads now use the new load's chain. We know the
4266 // old-load value is dead now.
4267 CombineTo(LHS.Val, Load.getValue(0), Load.getValue(1));
4268 CombineTo(RHS.Val, Load.getValue(0), Load.getValue(1));
4269 return true;
4270 }
Evan Chengc5484282006-10-04 00:56:09 +00004271 }
Chris Lattner40c62d52005-10-18 06:04:22 +00004272 }
4273 }
4274
4275 return false;
4276}
4277
Nate Begeman44728a72005-09-19 22:34:01 +00004278SDOperand DAGCombiner::SimplifySelectCC(SDOperand N0, SDOperand N1,
4279 SDOperand N2, SDOperand N3,
Chris Lattner1eba01e2007-04-11 06:50:51 +00004280 ISD::CondCode CC, bool NotExtCompare) {
Nate Begemanf845b452005-10-08 00:29:44 +00004281
4282 MVT::ValueType VT = N2.getValueType();
Nate Begemanf845b452005-10-08 00:29:44 +00004283 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
4284 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
4285 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
4286
4287 // Determine if the condition we're dealing with is constant
4288 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
Chris Lattner30f73e72006-10-14 03:52:46 +00004289 if (SCC.Val) AddToWorkList(SCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004290 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
4291
4292 // fold select_cc true, x, y -> x
4293 if (SCCC && SCCC->getValue())
4294 return N2;
4295 // fold select_cc false, x, y -> y
4296 if (SCCC && SCCC->getValue() == 0)
4297 return N3;
4298
4299 // Check to see if we can simplify the select into an fabs node
4300 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
4301 // Allow either -0.0 or 0.0
4302 if (CFP->getValue() == 0.0) {
4303 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
4304 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
4305 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
4306 N2 == N3.getOperand(0))
4307 return DAG.getNode(ISD::FABS, VT, N0);
4308
4309 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
4310 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
4311 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
4312 N2.getOperand(0) == N3)
4313 return DAG.getNode(ISD::FABS, VT, N3);
4314 }
4315 }
4316
4317 // Check to see if we can perform the "gzip trick", transforming
4318 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
Chris Lattnere3152e52006-09-20 06:41:35 +00004319 if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT &&
Nate Begemanf845b452005-10-08 00:29:44 +00004320 MVT::isInteger(N0.getValueType()) &&
Chris Lattnere3152e52006-09-20 06:41:35 +00004321 MVT::isInteger(N2.getValueType()) &&
4322 (N1C->isNullValue() || // (a < 0) ? b : 0
4323 (N1C->getValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0
Nate Begemanf845b452005-10-08 00:29:44 +00004324 MVT::ValueType XType = N0.getValueType();
4325 MVT::ValueType AType = N2.getValueType();
4326 if (XType >= AType) {
4327 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00004328 // single-bit constant.
Nate Begemanf845b452005-10-08 00:29:44 +00004329 if (N2C && ((N2C->getValue() & (N2C->getValue()-1)) == 0)) {
4330 unsigned ShCtV = Log2_64(N2C->getValue());
4331 ShCtV = MVT::getSizeInBits(XType)-ShCtV-1;
4332 SDOperand ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
4333 SDOperand Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
Chris Lattner5750df92006-03-01 04:03:14 +00004334 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004335 if (XType > AType) {
4336 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00004337 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004338 }
4339 return DAG.getNode(ISD::AND, AType, Shift, N2);
4340 }
4341 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
4342 DAG.getConstant(MVT::getSizeInBits(XType)-1,
4343 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00004344 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004345 if (XType > AType) {
4346 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00004347 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004348 }
4349 return DAG.getNode(ISD::AND, AType, Shift, N2);
4350 }
4351 }
Nate Begeman07ed4172005-10-10 21:26:48 +00004352
4353 // fold select C, 16, 0 -> shl C, 4
4354 if (N2C && N3C && N3C->isNullValue() && isPowerOf2_64(N2C->getValue()) &&
4355 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) {
Chris Lattner1eba01e2007-04-11 06:50:51 +00004356
4357 // If the caller doesn't want us to simplify this into a zext of a compare,
4358 // don't do it.
4359 if (NotExtCompare && N2C->getValue() == 1)
4360 return SDOperand();
4361
Nate Begeman07ed4172005-10-10 21:26:48 +00004362 // Get a SetCC of the condition
4363 // FIXME: Should probably make sure that setcc is legal if we ever have a
4364 // target where it isn't.
Nate Begemanb0d04a72006-02-18 02:40:58 +00004365 SDOperand Temp, SCC;
Nate Begeman07ed4172005-10-10 21:26:48 +00004366 // cast from setcc result type to select result type
Nate Begemanb0d04a72006-02-18 02:40:58 +00004367 if (AfterLegalize) {
4368 SCC = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
Chris Lattner555d8d62006-12-07 22:36:47 +00004369 if (N2.getValueType() < SCC.getValueType())
4370 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
4371 else
4372 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00004373 } else {
4374 SCC = DAG.getSetCC(MVT::i1, N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00004375 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00004376 }
Chris Lattner5750df92006-03-01 04:03:14 +00004377 AddToWorkList(SCC.Val);
4378 AddToWorkList(Temp.Val);
Chris Lattnerc56a81d2007-04-11 06:43:25 +00004379
4380 if (N2C->getValue() == 1)
4381 return Temp;
Nate Begeman07ed4172005-10-10 21:26:48 +00004382 // shl setcc result by log2 n2c
4383 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
4384 DAG.getConstant(Log2_64(N2C->getValue()),
4385 TLI.getShiftAmountTy()));
4386 }
4387
Nate Begemanf845b452005-10-08 00:29:44 +00004388 // Check to see if this is the equivalent of setcc
4389 // FIXME: Turn all of these into setcc if setcc if setcc is legal
4390 // otherwise, go ahead with the folds.
4391 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getValue() == 1ULL)) {
4392 MVT::ValueType XType = N0.getValueType();
4393 if (TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultTy())) {
4394 SDOperand Res = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
4395 if (Res.getValueType() != VT)
4396 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
4397 return Res;
4398 }
4399
4400 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
4401 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
4402 TLI.isOperationLegal(ISD::CTLZ, XType)) {
4403 SDOperand Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
4404 return DAG.getNode(ISD::SRL, XType, Ctlz,
4405 DAG.getConstant(Log2_32(MVT::getSizeInBits(XType)),
4406 TLI.getShiftAmountTy()));
4407 }
4408 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
4409 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
4410 SDOperand NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
4411 N0);
4412 SDOperand NotN0 = DAG.getNode(ISD::XOR, XType, N0,
4413 DAG.getConstant(~0ULL, XType));
4414 return DAG.getNode(ISD::SRL, XType,
4415 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
4416 DAG.getConstant(MVT::getSizeInBits(XType)-1,
4417 TLI.getShiftAmountTy()));
4418 }
4419 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
4420 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
4421 SDOperand Sign = DAG.getNode(ISD::SRL, XType, N0,
4422 DAG.getConstant(MVT::getSizeInBits(XType)-1,
4423 TLI.getShiftAmountTy()));
4424 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
4425 }
4426 }
4427
4428 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
4429 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
4430 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
Chris Lattner1982ef22007-04-11 05:11:38 +00004431 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1) &&
4432 N2.getOperand(0) == N1 && MVT::isInteger(N0.getValueType())) {
4433 MVT::ValueType XType = N0.getValueType();
4434 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
4435 DAG.getConstant(MVT::getSizeInBits(XType)-1,
4436 TLI.getShiftAmountTy()));
4437 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
4438 AddToWorkList(Shift.Val);
4439 AddToWorkList(Add.Val);
4440 return DAG.getNode(ISD::XOR, XType, Add, Shift);
4441 }
4442 // Check to see if this is an integer abs. select_cc setgt X, -1, X, -X ->
4443 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
4444 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT &&
4445 N0 == N2 && N3.getOpcode() == ISD::SUB && N0 == N3.getOperand(1)) {
4446 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N3.getOperand(0))) {
Nate Begemanf845b452005-10-08 00:29:44 +00004447 MVT::ValueType XType = N0.getValueType();
4448 if (SubC->isNullValue() && MVT::isInteger(XType)) {
4449 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
4450 DAG.getConstant(MVT::getSizeInBits(XType)-1,
Chris Lattner1982ef22007-04-11 05:11:38 +00004451 TLI.getShiftAmountTy()));
Nate Begemanf845b452005-10-08 00:29:44 +00004452 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00004453 AddToWorkList(Shift.Val);
4454 AddToWorkList(Add.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004455 return DAG.getNode(ISD::XOR, XType, Add, Shift);
4456 }
4457 }
4458 }
Chris Lattner1982ef22007-04-11 05:11:38 +00004459
Nate Begeman44728a72005-09-19 22:34:01 +00004460 return SDOperand();
4461}
4462
Evan Chengfa1eb272007-02-08 22:13:59 +00004463/// SimplifySetCC - This is a stub for TargetLowering::SimplifySetCC.
Nate Begeman452d7be2005-09-16 00:54:12 +00004464SDOperand DAGCombiner::SimplifySetCC(MVT::ValueType VT, SDOperand N0,
Nate Begemane17daeb2005-10-05 21:43:42 +00004465 SDOperand N1, ISD::CondCode Cond,
4466 bool foldBooleans) {
Evan Chengfa1eb272007-02-08 22:13:59 +00004467 TargetLowering::DAGCombinerInfo
4468 DagCombineInfo(DAG, !AfterLegalize, false, this);
4469 return TLI.SimplifySetCC(VT, N0, N1, Cond, foldBooleans, DagCombineInfo);
Nate Begeman452d7be2005-09-16 00:54:12 +00004470}
4471
Nate Begeman69575232005-10-20 02:15:44 +00004472/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
4473/// return a DAG expression to select that will generate the same value by
4474/// multiplying by a magic number. See:
4475/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
4476SDOperand DAGCombiner::BuildSDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00004477 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00004478 SDOperand S = TLI.BuildSDIV(N, DAG, &Built);
4479
Andrew Lenharth232c9102006-06-12 16:07:18 +00004480 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00004481 ii != ee; ++ii)
4482 AddToWorkList(*ii);
4483 return S;
Nate Begeman69575232005-10-20 02:15:44 +00004484}
4485
4486/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
4487/// return a DAG expression to select that will generate the same value by
4488/// multiplying by a magic number. See:
4489/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
4490SDOperand DAGCombiner::BuildUDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00004491 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00004492 SDOperand S = TLI.BuildUDIV(N, DAG, &Built);
Nate Begeman69575232005-10-20 02:15:44 +00004493
Andrew Lenharth232c9102006-06-12 16:07:18 +00004494 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00004495 ii != ee; ++ii)
4496 AddToWorkList(*ii);
4497 return S;
Nate Begeman69575232005-10-20 02:15:44 +00004498}
4499
Jim Laskey71382342006-10-07 23:37:56 +00004500/// FindBaseOffset - Return true if base is known not to alias with anything
4501/// but itself. Provides base object and offset as results.
4502static bool FindBaseOffset(SDOperand Ptr, SDOperand &Base, int64_t &Offset) {
4503 // Assume it is a primitive operation.
4504 Base = Ptr; Offset = 0;
4505
4506 // If it's an adding a simple constant then integrate the offset.
4507 if (Base.getOpcode() == ISD::ADD) {
4508 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Base.getOperand(1))) {
4509 Base = Base.getOperand(0);
4510 Offset += C->getValue();
4511 }
4512 }
4513
4514 // If it's any of the following then it can't alias with anything but itself.
4515 return isa<FrameIndexSDNode>(Base) ||
4516 isa<ConstantPoolSDNode>(Base) ||
4517 isa<GlobalAddressSDNode>(Base);
4518}
4519
4520/// isAlias - Return true if there is any possibility that the two addresses
4521/// overlap.
Jim Laskey096c22e2006-10-18 12:29:57 +00004522bool DAGCombiner::isAlias(SDOperand Ptr1, int64_t Size1,
4523 const Value *SrcValue1, int SrcValueOffset1,
4524 SDOperand Ptr2, int64_t Size2,
4525 const Value *SrcValue2, int SrcValueOffset2)
4526{
Jim Laskey71382342006-10-07 23:37:56 +00004527 // If they are the same then they must be aliases.
4528 if (Ptr1 == Ptr2) return true;
4529
4530 // Gather base node and offset information.
4531 SDOperand Base1, Base2;
4532 int64_t Offset1, Offset2;
4533 bool KnownBase1 = FindBaseOffset(Ptr1, Base1, Offset1);
4534 bool KnownBase2 = FindBaseOffset(Ptr2, Base2, Offset2);
4535
4536 // If they have a same base address then...
4537 if (Base1 == Base2) {
4538 // Check to see if the addresses overlap.
4539 return!((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
4540 }
4541
Jim Laskey096c22e2006-10-18 12:29:57 +00004542 // If we know both bases then they can't alias.
4543 if (KnownBase1 && KnownBase2) return false;
4544
Jim Laskey07a27092006-10-18 19:08:31 +00004545 if (CombinerGlobalAA) {
4546 // Use alias analysis information.
4547 int Overlap1 = Size1 + SrcValueOffset1 + Offset1;
4548 int Overlap2 = Size2 + SrcValueOffset2 + Offset2;
4549 AliasAnalysis::AliasResult AAResult =
Jim Laskey096c22e2006-10-18 12:29:57 +00004550 AA.alias(SrcValue1, Overlap1, SrcValue2, Overlap2);
Jim Laskey07a27092006-10-18 19:08:31 +00004551 if (AAResult == AliasAnalysis::NoAlias)
4552 return false;
4553 }
Jim Laskey096c22e2006-10-18 12:29:57 +00004554
4555 // Otherwise we have to assume they alias.
4556 return true;
Jim Laskey71382342006-10-07 23:37:56 +00004557}
4558
4559/// FindAliasInfo - Extracts the relevant alias information from the memory
4560/// node. Returns true if the operand was a load.
Jim Laskey7ca56af2006-10-11 13:47:09 +00004561bool DAGCombiner::FindAliasInfo(SDNode *N,
Jim Laskey096c22e2006-10-18 12:29:57 +00004562 SDOperand &Ptr, int64_t &Size,
4563 const Value *&SrcValue, int &SrcValueOffset) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00004564 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
4565 Ptr = LD->getBasePtr();
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004566 Size = MVT::getSizeInBits(LD->getLoadedVT()) >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00004567 SrcValue = LD->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00004568 SrcValueOffset = LD->getSrcValueOffset();
Jim Laskey71382342006-10-07 23:37:56 +00004569 return true;
Jim Laskey7ca56af2006-10-11 13:47:09 +00004570 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00004571 Ptr = ST->getBasePtr();
Evan Cheng8b2794a2006-10-13 21:14:26 +00004572 Size = MVT::getSizeInBits(ST->getStoredVT()) >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00004573 SrcValue = ST->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00004574 SrcValueOffset = ST->getSrcValueOffset();
Jim Laskey7ca56af2006-10-11 13:47:09 +00004575 } else {
Jim Laskey71382342006-10-07 23:37:56 +00004576 assert(0 && "FindAliasInfo expected a memory operand");
Jim Laskey71382342006-10-07 23:37:56 +00004577 }
4578
4579 return false;
4580}
4581
Jim Laskey6ff23e52006-10-04 16:53:27 +00004582/// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
4583/// looking for aliasing nodes and adding them to the Aliases vector.
Jim Laskeybc588b82006-10-05 15:07:25 +00004584void DAGCombiner::GatherAllAliases(SDNode *N, SDOperand OriginalChain,
Jim Laskey6ff23e52006-10-04 16:53:27 +00004585 SmallVector<SDOperand, 8> &Aliases) {
Jim Laskeybc588b82006-10-05 15:07:25 +00004586 SmallVector<SDOperand, 8> Chains; // List of chains to visit.
Jim Laskey6ff23e52006-10-04 16:53:27 +00004587 std::set<SDNode *> Visited; // Visited node set.
4588
Jim Laskey279f0532006-09-25 16:29:54 +00004589 // Get alias information for node.
4590 SDOperand Ptr;
4591 int64_t Size;
Jim Laskey7ca56af2006-10-11 13:47:09 +00004592 const Value *SrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00004593 int SrcValueOffset;
4594 bool IsLoad = FindAliasInfo(N, Ptr, Size, SrcValue, SrcValueOffset);
Jim Laskey279f0532006-09-25 16:29:54 +00004595
Jim Laskey6ff23e52006-10-04 16:53:27 +00004596 // Starting off.
Jim Laskeybc588b82006-10-05 15:07:25 +00004597 Chains.push_back(OriginalChain);
Jim Laskey6ff23e52006-10-04 16:53:27 +00004598
Jim Laskeybc588b82006-10-05 15:07:25 +00004599 // Look at each chain and determine if it is an alias. If so, add it to the
4600 // aliases list. If not, then continue up the chain looking for the next
4601 // candidate.
4602 while (!Chains.empty()) {
4603 SDOperand Chain = Chains.back();
4604 Chains.pop_back();
Jim Laskey6ff23e52006-10-04 16:53:27 +00004605
Jim Laskeybc588b82006-10-05 15:07:25 +00004606 // Don't bother if we've been before.
4607 if (Visited.find(Chain.Val) != Visited.end()) continue;
4608 Visited.insert(Chain.Val);
4609
4610 switch (Chain.getOpcode()) {
4611 case ISD::EntryToken:
4612 // Entry token is ideal chain operand, but handled in FindBetterChain.
4613 break;
Jim Laskey6ff23e52006-10-04 16:53:27 +00004614
Jim Laskeybc588b82006-10-05 15:07:25 +00004615 case ISD::LOAD:
4616 case ISD::STORE: {
4617 // Get alias information for Chain.
4618 SDOperand OpPtr;
4619 int64_t OpSize;
Jim Laskey7ca56af2006-10-11 13:47:09 +00004620 const Value *OpSrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00004621 int OpSrcValueOffset;
4622 bool IsOpLoad = FindAliasInfo(Chain.Val, OpPtr, OpSize,
4623 OpSrcValue, OpSrcValueOffset);
Jim Laskeybc588b82006-10-05 15:07:25 +00004624
4625 // If chain is alias then stop here.
4626 if (!(IsLoad && IsOpLoad) &&
Jim Laskey096c22e2006-10-18 12:29:57 +00004627 isAlias(Ptr, Size, SrcValue, SrcValueOffset,
4628 OpPtr, OpSize, OpSrcValue, OpSrcValueOffset)) {
Jim Laskeybc588b82006-10-05 15:07:25 +00004629 Aliases.push_back(Chain);
4630 } else {
4631 // Look further up the chain.
4632 Chains.push_back(Chain.getOperand(0));
4633 // Clean up old chain.
4634 AddToWorkList(Chain.Val);
Jim Laskey279f0532006-09-25 16:29:54 +00004635 }
Jim Laskeybc588b82006-10-05 15:07:25 +00004636 break;
4637 }
4638
4639 case ISD::TokenFactor:
4640 // We have to check each of the operands of the token factor, so we queue
4641 // then up. Adding the operands to the queue (stack) in reverse order
4642 // maintains the original order and increases the likelihood that getNode
4643 // will find a matching token factor (CSE.)
4644 for (unsigned n = Chain.getNumOperands(); n;)
4645 Chains.push_back(Chain.getOperand(--n));
4646 // Eliminate the token factor if we can.
4647 AddToWorkList(Chain.Val);
4648 break;
4649
4650 default:
4651 // For all other instructions we will just have to take what we can get.
4652 Aliases.push_back(Chain);
4653 break;
Jim Laskey279f0532006-09-25 16:29:54 +00004654 }
4655 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00004656}
4657
4658/// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, looking
4659/// for a better chain (aliasing node.)
4660SDOperand DAGCombiner::FindBetterChain(SDNode *N, SDOperand OldChain) {
4661 SmallVector<SDOperand, 8> Aliases; // Ops for replacing token factor.
Jim Laskey279f0532006-09-25 16:29:54 +00004662
Jim Laskey6ff23e52006-10-04 16:53:27 +00004663 // Accumulate all the aliases to this node.
4664 GatherAllAliases(N, OldChain, Aliases);
4665
4666 if (Aliases.size() == 0) {
4667 // If no operands then chain to entry token.
4668 return DAG.getEntryNode();
4669 } else if (Aliases.size() == 1) {
4670 // If a single operand then chain to it. We don't need to revisit it.
4671 return Aliases[0];
4672 }
4673
4674 // Construct a custom tailored token factor.
4675 SDOperand NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
4676 &Aliases[0], Aliases.size());
4677
4678 // Make sure the old chain gets cleaned up.
4679 if (NewChain != OldChain) AddToWorkList(OldChain.Val);
4680
4681 return NewChain;
Jim Laskey279f0532006-09-25 16:29:54 +00004682}
4683
Nate Begeman1d4d4142005-09-01 00:19:25 +00004684// SelectionDAG::Combine - This is the entry point for the file.
4685//
Jim Laskeyc7c3f112006-10-16 20:52:31 +00004686void SelectionDAG::Combine(bool RunningAfterLegalize, AliasAnalysis &AA) {
Chris Lattner938ab022007-01-16 04:55:25 +00004687 if (!RunningAfterLegalize && ViewDAGCombine1)
4688 viewGraph();
4689 if (RunningAfterLegalize && ViewDAGCombine2)
4690 viewGraph();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004691 /// run - This is the main entry point to this class.
4692 ///
Jim Laskeyc7c3f112006-10-16 20:52:31 +00004693 DAGCombiner(*this, AA).Run(RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004694}