blob: fd2ad5cd4c9a18e086f62b6799c8868ad2088518 [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"
32#include "llvm/ADT/Statistic.h"
Jim Laskeyc7c3f112006-10-16 20:52:31 +000033#include "llvm/Analysis/AliasAnalysis.h"
Nate Begeman1d4d4142005-09-01 00:19:25 +000034#include "llvm/CodeGen/SelectionDAG.h"
Nate Begeman2300f552005-09-07 00:15:36 +000035#include "llvm/Support/Debug.h"
Nate Begeman1d4d4142005-09-01 00:19:25 +000036#include "llvm/Support/MathExtras.h"
Evan Cheng59d5b682007-05-07 21:27:48 +000037#include "llvm/Target/TargetData.h"
Nate Begeman1d4d4142005-09-01 00:19:25 +000038#include "llvm/Target/TargetLowering.h"
Evan Cheng59d5b682007-05-07 21:27:48 +000039#include "llvm/Target/TargetMachine.h"
Chris Lattnerddae4bd2007-01-08 23:04:05 +000040#include "llvm/Target/TargetOptions.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000041#include "llvm/Support/Compiler.h"
Jim Laskeyd1aed7a2006-09-21 16:28:59 +000042#include "llvm/Support/CommandLine.h"
Chris Lattnera500fc62005-09-09 23:53:39 +000043#include <algorithm>
Nate Begeman1d4d4142005-09-01 00:19:25 +000044using namespace llvm;
45
Chris Lattnercd3245a2006-12-19 22:41:21 +000046STATISTIC(NodesCombined , "Number of dag nodes combined");
47STATISTIC(PreIndexedNodes , "Number of pre-indexed nodes created");
48STATISTIC(PostIndexedNodes, "Number of post-indexed nodes created");
49
Nate Begeman1d4d4142005-09-01 00:19:25 +000050namespace {
Chris Lattner938ab022007-01-16 04:55:25 +000051#ifndef NDEBUG
52 static cl::opt<bool>
53 ViewDAGCombine1("view-dag-combine1-dags", cl::Hidden,
54 cl::desc("Pop up a window to show dags before the first "
55 "dag combine pass"));
56 static cl::opt<bool>
57 ViewDAGCombine2("view-dag-combine2-dags", cl::Hidden,
58 cl::desc("Pop up a window to show dags before the second "
59 "dag combine pass"));
60#else
61 static const bool ViewDAGCombine1 = false;
62 static const bool ViewDAGCombine2 = false;
63#endif
64
Jim Laskey71382342006-10-07 23:37:56 +000065 static cl::opt<bool>
66 CombinerAA("combiner-alias-analysis", cl::Hidden,
Jim Laskey26f7fa72006-10-17 19:33:52 +000067 cl::desc("Turn on alias analysis during testing"));
Jim Laskey3ad175b2006-10-12 15:22:24 +000068
Jim Laskey07a27092006-10-18 19:08:31 +000069 static cl::opt<bool>
70 CombinerGlobalAA("combiner-global-alias-analysis", cl::Hidden,
71 cl::desc("Include global information in alias analysis"));
72
Jim Laskeybc588b82006-10-05 15:07:25 +000073//------------------------------ DAGCombiner ---------------------------------//
74
Jim Laskey71382342006-10-07 23:37:56 +000075 class VISIBILITY_HIDDEN DAGCombiner {
Nate Begeman1d4d4142005-09-01 00:19:25 +000076 SelectionDAG &DAG;
77 TargetLowering &TLI;
Nate Begeman4ebd8052005-09-01 23:24:04 +000078 bool AfterLegalize;
Nate Begeman1d4d4142005-09-01 00:19:25 +000079
80 // Worklist of all of the nodes that need to be simplified.
81 std::vector<SDNode*> WorkList;
82
Jim Laskeyc7c3f112006-10-16 20:52:31 +000083 // AA - Used for DAG load/store alias analysis.
84 AliasAnalysis &AA;
85
Nate Begeman1d4d4142005-09-01 00:19:25 +000086 /// AddUsersToWorkList - When an instruction is simplified, add all users of
87 /// the instruction to the work lists because they might get more simplified
88 /// now.
89 ///
90 void AddUsersToWorkList(SDNode *N) {
91 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
Nate Begeman4ebd8052005-09-01 23:24:04 +000092 UI != UE; ++UI)
Jim Laskey6ff23e52006-10-04 16:53:27 +000093 AddToWorkList(*UI);
Nate Begeman1d4d4142005-09-01 00:19:25 +000094 }
95
96 /// removeFromWorkList - remove all instances of N from the worklist.
Chris Lattner5750df92006-03-01 04:03:14 +000097 ///
Nate Begeman1d4d4142005-09-01 00:19:25 +000098 void removeFromWorkList(SDNode *N) {
99 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), N),
100 WorkList.end());
101 }
102
Chris Lattner24664722006-03-01 04:53:38 +0000103 public:
Jim Laskey6ff23e52006-10-04 16:53:27 +0000104 /// AddToWorkList - Add to the work list making sure it's instance is at the
105 /// the back (next to be processed.)
Chris Lattner5750df92006-03-01 04:03:14 +0000106 void AddToWorkList(SDNode *N) {
Jim Laskey6ff23e52006-10-04 16:53:27 +0000107 removeFromWorkList(N);
Chris Lattner5750df92006-03-01 04:03:14 +0000108 WorkList.push_back(N);
109 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000110
Jim Laskey274062c2006-10-13 23:32:28 +0000111 SDOperand CombineTo(SDNode *N, const SDOperand *To, unsigned NumTo,
112 bool AddTo = true) {
Chris Lattner3577e382006-08-11 17:56:38 +0000113 assert(N->getNumValues() == NumTo && "Broken CombineTo call!");
Chris Lattner87514ca2005-10-10 22:31:19 +0000114 ++NodesCombined;
Bill Wendling832171c2006-12-07 20:04:42 +0000115 DOUT << "\nReplacing.1 "; DEBUG(N->dump());
116 DOUT << "\nWith: "; DEBUG(To[0].Val->dump(&DAG));
117 DOUT << " and " << NumTo-1 << " other values\n";
Chris Lattner01a22022005-10-10 22:04:48 +0000118 std::vector<SDNode*> NowDead;
Chris Lattner3577e382006-08-11 17:56:38 +0000119 DAG.ReplaceAllUsesWith(N, To, &NowDead);
Chris Lattner01a22022005-10-10 22:04:48 +0000120
Jim Laskey274062c2006-10-13 23:32:28 +0000121 if (AddTo) {
122 // Push the new nodes and any users onto the worklist
123 for (unsigned i = 0, e = NumTo; i != e; ++i) {
124 AddToWorkList(To[i].Val);
125 AddUsersToWorkList(To[i].Val);
126 }
Chris Lattner01a22022005-10-10 22:04:48 +0000127 }
128
Jim Laskey6ff23e52006-10-04 16:53:27 +0000129 // Nodes can be reintroduced into the worklist. Make sure we do not
130 // process a node that has been replaced.
Chris Lattner01a22022005-10-10 22:04:48 +0000131 removeFromWorkList(N);
132 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
133 removeFromWorkList(NowDead[i]);
134
135 // Finally, since the node is now dead, remove it from the graph.
136 DAG.DeleteNode(N);
137 return SDOperand(N, 0);
138 }
Nate Begeman368e18d2006-02-16 21:11:51 +0000139
Jim Laskey274062c2006-10-13 23:32:28 +0000140 SDOperand CombineTo(SDNode *N, SDOperand Res, bool AddTo = true) {
141 return CombineTo(N, &Res, 1, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000142 }
143
Jim Laskey274062c2006-10-13 23:32:28 +0000144 SDOperand CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1,
145 bool AddTo = true) {
Chris Lattner3577e382006-08-11 17:56:38 +0000146 SDOperand To[] = { Res0, Res1 };
Jim Laskey274062c2006-10-13 23:32:28 +0000147 return CombineTo(N, To, 2, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000148 }
149 private:
150
Chris Lattner012f2412006-02-17 21:58:01 +0000151 /// SimplifyDemandedBits - Check the specified integer node value to see if
Chris Lattnerb2742f42006-03-01 19:55:35 +0000152 /// it can be simplified or if things it uses can be simplified by bit
Chris Lattner012f2412006-02-17 21:58:01 +0000153 /// propagation. If so, return true.
154 bool SimplifyDemandedBits(SDOperand Op) {
Nate Begeman368e18d2006-02-16 21:11:51 +0000155 TargetLowering::TargetLoweringOpt TLO(DAG);
156 uint64_t KnownZero, KnownOne;
Chris Lattner012f2412006-02-17 21:58:01 +0000157 uint64_t Demanded = MVT::getIntVTBitMask(Op.getValueType());
158 if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO))
159 return false;
160
161 // Revisit the node.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000162 AddToWorkList(Op.Val);
Chris Lattner012f2412006-02-17 21:58:01 +0000163
164 // Replace the old value with the new one.
165 ++NodesCombined;
Bill Wendling832171c2006-12-07 20:04:42 +0000166 DOUT << "\nReplacing.2 "; DEBUG(TLO.Old.Val->dump());
167 DOUT << "\nWith: "; DEBUG(TLO.New.Val->dump(&DAG));
168 DOUT << '\n';
Chris Lattner012f2412006-02-17 21:58:01 +0000169
170 std::vector<SDNode*> NowDead;
171 DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New, NowDead);
172
Chris Lattner7d20d392006-02-20 06:51:04 +0000173 // Push the new node and any (possibly new) users onto the worklist.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000174 AddToWorkList(TLO.New.Val);
Chris Lattner012f2412006-02-17 21:58:01 +0000175 AddUsersToWorkList(TLO.New.Val);
176
177 // Nodes can end up on the worklist more than once. Make sure we do
178 // not process a node that has been replaced.
Chris Lattner012f2412006-02-17 21:58:01 +0000179 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
180 removeFromWorkList(NowDead[i]);
181
Chris Lattner7d20d392006-02-20 06:51:04 +0000182 // Finally, if the node is now dead, remove it from the graph. The node
183 // may not be dead if the replacement process recursively simplified to
184 // something else needing this node.
185 if (TLO.Old.Val->use_empty()) {
186 removeFromWorkList(TLO.Old.Val);
Chris Lattnerec06e9a2007-04-18 03:05:22 +0000187
188 // If the operands of this node are only used by the node, they will now
189 // be dead. Make sure to visit them first to delete dead nodes early.
190 for (unsigned i = 0, e = TLO.Old.Val->getNumOperands(); i != e; ++i)
191 if (TLO.Old.Val->getOperand(i).Val->hasOneUse())
192 AddToWorkList(TLO.Old.Val->getOperand(i).Val);
193
Chris Lattner7d20d392006-02-20 06:51:04 +0000194 DAG.DeleteNode(TLO.Old.Val);
195 }
Chris Lattner012f2412006-02-17 21:58:01 +0000196 return true;
Nate Begeman368e18d2006-02-16 21:11:51 +0000197 }
Chris Lattner87514ca2005-10-10 22:31:19 +0000198
Chris Lattner448f2192006-11-11 00:39:41 +0000199 bool CombineToPreIndexedLoadStore(SDNode *N);
200 bool CombineToPostIndexedLoadStore(SDNode *N);
201
202
Nate Begeman1d4d4142005-09-01 00:19:25 +0000203 /// visit - call the node-specific routine that knows how to fold each
204 /// particular type of node.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000205 SDOperand visit(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000206
207 // Visitation implementation - Implement dag node combining for different
208 // node types. The semantics are as follows:
209 // Return Value:
Nate Begeman2300f552005-09-07 00:15:36 +0000210 // SDOperand.Val == 0 - No change was made
Chris Lattner01a22022005-10-10 22:04:48 +0000211 // SDOperand.Val == N - N was replaced, is dead, and is already handled.
Nate Begeman2300f552005-09-07 00:15:36 +0000212 // otherwise - N should be replaced by the returned Operand.
Nate Begeman1d4d4142005-09-01 00:19:25 +0000213 //
Nate Begeman83e75ec2005-09-06 04:43:02 +0000214 SDOperand visitTokenFactor(SDNode *N);
215 SDOperand visitADD(SDNode *N);
216 SDOperand visitSUB(SDNode *N);
Chris Lattner91153682007-03-04 20:03:15 +0000217 SDOperand visitADDC(SDNode *N);
218 SDOperand visitADDE(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000219 SDOperand visitMUL(SDNode *N);
220 SDOperand visitSDIV(SDNode *N);
221 SDOperand visitUDIV(SDNode *N);
222 SDOperand visitSREM(SDNode *N);
223 SDOperand visitUREM(SDNode *N);
224 SDOperand visitMULHU(SDNode *N);
225 SDOperand visitMULHS(SDNode *N);
226 SDOperand visitAND(SDNode *N);
227 SDOperand visitOR(SDNode *N);
228 SDOperand visitXOR(SDNode *N);
Chris Lattneredab1b92006-04-02 03:25:57 +0000229 SDOperand visitVBinOp(SDNode *N, ISD::NodeType IntOp, ISD::NodeType FPOp);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000230 SDOperand visitSHL(SDNode *N);
231 SDOperand visitSRA(SDNode *N);
232 SDOperand visitSRL(SDNode *N);
233 SDOperand visitCTLZ(SDNode *N);
234 SDOperand visitCTTZ(SDNode *N);
235 SDOperand visitCTPOP(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000236 SDOperand visitSELECT(SDNode *N);
237 SDOperand visitSELECT_CC(SDNode *N);
238 SDOperand visitSETCC(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000239 SDOperand visitSIGN_EXTEND(SDNode *N);
240 SDOperand visitZERO_EXTEND(SDNode *N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000241 SDOperand visitANY_EXTEND(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000242 SDOperand visitSIGN_EXTEND_INREG(SDNode *N);
243 SDOperand visitTRUNCATE(SDNode *N);
Chris Lattner94683772005-12-23 05:30:37 +0000244 SDOperand visitBIT_CONVERT(SDNode *N);
Chris Lattner6258fb22006-04-02 02:53:43 +0000245 SDOperand visitVBIT_CONVERT(SDNode *N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000246 SDOperand visitFADD(SDNode *N);
247 SDOperand visitFSUB(SDNode *N);
248 SDOperand visitFMUL(SDNode *N);
249 SDOperand visitFDIV(SDNode *N);
250 SDOperand visitFREM(SDNode *N);
Chris Lattner12d83032006-03-05 05:30:57 +0000251 SDOperand visitFCOPYSIGN(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000252 SDOperand visitSINT_TO_FP(SDNode *N);
253 SDOperand visitUINT_TO_FP(SDNode *N);
254 SDOperand visitFP_TO_SINT(SDNode *N);
255 SDOperand visitFP_TO_UINT(SDNode *N);
256 SDOperand visitFP_ROUND(SDNode *N);
257 SDOperand visitFP_ROUND_INREG(SDNode *N);
258 SDOperand visitFP_EXTEND(SDNode *N);
259 SDOperand visitFNEG(SDNode *N);
260 SDOperand visitFABS(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000261 SDOperand visitBRCOND(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000262 SDOperand visitBR_CC(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000263 SDOperand visitLOAD(SDNode *N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000264 SDOperand visitSTORE(SDNode *N);
Chris Lattnerca242442006-03-19 01:27:56 +0000265 SDOperand visitINSERT_VECTOR_ELT(SDNode *N);
266 SDOperand visitVINSERT_VECTOR_ELT(SDNode *N);
Chris Lattnerd7648c82006-03-28 20:28:38 +0000267 SDOperand visitVBUILD_VECTOR(SDNode *N);
Chris Lattner66445d32006-03-28 22:11:53 +0000268 SDOperand visitVECTOR_SHUFFLE(SDNode *N);
Chris Lattnerf1d0c622006-03-31 22:16:43 +0000269 SDOperand visitVVECTOR_SHUFFLE(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000270
Evan Cheng44f1f092006-04-20 08:56:16 +0000271 SDOperand XformToShuffleWithZero(SDNode *N);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000272 SDOperand ReassociateOps(unsigned Opc, SDOperand LHS, SDOperand RHS);
273
Chris Lattner40c62d52005-10-18 06:04:22 +0000274 bool SimplifySelectOps(SDNode *SELECT, SDOperand LHS, SDOperand RHS);
Chris Lattner35e5c142006-05-05 05:51:50 +0000275 SDOperand SimplifyBinOpWithSameOpcodeHands(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000276 SDOperand SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2);
277 SDOperand SimplifySelectCC(SDOperand N0, SDOperand N1, SDOperand N2,
Chris Lattner1eba01e2007-04-11 06:50:51 +0000278 SDOperand N3, ISD::CondCode CC,
279 bool NotExtCompare = false);
Nate Begeman452d7be2005-09-16 00:54:12 +0000280 SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N0, SDOperand N1,
Nate Begemane17daeb2005-10-05 21:43:42 +0000281 ISD::CondCode Cond, bool foldBooleans = true);
Chris Lattner6258fb22006-04-02 02:53:43 +0000282 SDOperand ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(SDNode *, MVT::ValueType);
Nate Begeman69575232005-10-20 02:15:44 +0000283 SDOperand BuildSDIV(SDNode *N);
Chris Lattner516b9622006-09-14 20:50:57 +0000284 SDOperand BuildUDIV(SDNode *N);
285 SDNode *MatchRotate(SDOperand LHS, SDOperand RHS);
Evan Chengc88138f2007-03-22 01:54:19 +0000286 SDOperand ReduceLoadWidth(SDNode *N);
Jim Laskey279f0532006-09-25 16:29:54 +0000287
Jim Laskey6ff23e52006-10-04 16:53:27 +0000288 /// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
289 /// looking for aliasing nodes and adding them to the Aliases vector.
Jim Laskeybc588b82006-10-05 15:07:25 +0000290 void GatherAllAliases(SDNode *N, SDOperand OriginalChain,
Jim Laskey6ff23e52006-10-04 16:53:27 +0000291 SmallVector<SDOperand, 8> &Aliases);
292
Jim Laskey096c22e2006-10-18 12:29:57 +0000293 /// isAlias - Return true if there is any possibility that the two addresses
294 /// overlap.
295 bool isAlias(SDOperand Ptr1, int64_t Size1,
296 const Value *SrcValue1, int SrcValueOffset1,
297 SDOperand Ptr2, int64_t Size2,
Jeff Cohend41b30d2006-11-05 19:31:28 +0000298 const Value *SrcValue2, int SrcValueOffset2);
Jim Laskey096c22e2006-10-18 12:29:57 +0000299
Jim Laskey7ca56af2006-10-11 13:47:09 +0000300 /// FindAliasInfo - Extracts the relevant alias information from the memory
301 /// node. Returns true if the operand was a load.
302 bool FindAliasInfo(SDNode *N,
Jim Laskey096c22e2006-10-18 12:29:57 +0000303 SDOperand &Ptr, int64_t &Size,
304 const Value *&SrcValue, int &SrcValueOffset);
Jim Laskey7ca56af2006-10-11 13:47:09 +0000305
Jim Laskey279f0532006-09-25 16:29:54 +0000306 /// FindBetterChain - Walk up chain skipping non-aliasing memory nodes,
Jim Laskey6ff23e52006-10-04 16:53:27 +0000307 /// looking for a better chain (aliasing node.)
Jim Laskey279f0532006-09-25 16:29:54 +0000308 SDOperand FindBetterChain(SDNode *N, SDOperand Chain);
309
Nate Begeman1d4d4142005-09-01 00:19:25 +0000310public:
Jim Laskeyc7c3f112006-10-16 20:52:31 +0000311 DAGCombiner(SelectionDAG &D, AliasAnalysis &A)
312 : DAG(D),
313 TLI(D.getTargetLoweringInfo()),
314 AfterLegalize(false),
315 AA(A) {}
Nate Begeman1d4d4142005-09-01 00:19:25 +0000316
317 /// Run - runs the dag combiner on all nodes in the work list
Nate Begeman4ebd8052005-09-01 23:24:04 +0000318 void Run(bool RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000319 };
320}
321
Chris Lattner24664722006-03-01 04:53:38 +0000322//===----------------------------------------------------------------------===//
323// TargetLowering::DAGCombinerInfo implementation
324//===----------------------------------------------------------------------===//
325
326void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) {
327 ((DAGCombiner*)DC)->AddToWorkList(N);
328}
329
330SDOperand TargetLowering::DAGCombinerInfo::
331CombineTo(SDNode *N, const std::vector<SDOperand> &To) {
Chris Lattner3577e382006-08-11 17:56:38 +0000332 return ((DAGCombiner*)DC)->CombineTo(N, &To[0], To.size());
Chris Lattner24664722006-03-01 04:53:38 +0000333}
334
335SDOperand TargetLowering::DAGCombinerInfo::
336CombineTo(SDNode *N, SDOperand Res) {
337 return ((DAGCombiner*)DC)->CombineTo(N, Res);
338}
339
340
341SDOperand TargetLowering::DAGCombinerInfo::
342CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1) {
343 return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1);
344}
345
346
Chris Lattner24664722006-03-01 04:53:38 +0000347//===----------------------------------------------------------------------===//
Chris Lattner29446522007-05-14 22:04:50 +0000348// Helper Functions
349//===----------------------------------------------------------------------===//
350
351/// isNegatibleForFree - Return 1 if we can compute the negated form of the
352/// specified expression for the same cost as the expression itself, or 2 if we
353/// can compute the negated form more cheaply than the expression itself.
354static char isNegatibleForFree(SDOperand Op) {
355 // fneg is removable even if it has multiple uses.
356 if (Op.getOpcode() == ISD::FNEG) return 2;
357
358 // Don't allow anything with multiple uses.
359 if (!Op.hasOneUse()) return 0;
360
361 switch (Op.getOpcode()) {
362 default: return false;
363 case ISD::ConstantFP:
364 return 1;
365 case ISD::FADD:
366 // FIXME: determine better conditions for this xform.
367 if (!UnsafeFPMath) return 0;
368
369 // -(A+B) -> -A - B
370 if (char V = isNegatibleForFree(Op.getOperand(0)))
371 return V;
372 // -(A+B) -> -B - A
373 return isNegatibleForFree(Op.getOperand(1));
374 case ISD::FSUB:
375 // We can't turn -(A-B) into B-A when we honor signed zeros.
376 if (!UnsafeFPMath) return 0;
377
378 // -(A-B) -> B-A
379 return 1;
380
381 case ISD::FMUL:
382 case ISD::FDIV:
383 if (HonorSignDependentRoundingFPMath()) return 0;
384
385 // -(X*Y) -> (-X * Y) or (X*-Y)
386 if (char V = isNegatibleForFree(Op.getOperand(0)))
387 return V;
388
389 return isNegatibleForFree(Op.getOperand(1));
390
391 case ISD::FP_EXTEND:
392 case ISD::FP_ROUND:
393 case ISD::FSIN:
394 return isNegatibleForFree(Op.getOperand(0));
395 }
396}
397
398/// GetNegatedExpression - If isNegatibleForFree returns true, this function
399/// returns the newly negated expression.
400static SDOperand GetNegatedExpression(SDOperand Op, SelectionDAG &DAG) {
401 // fneg is removable even if it has multiple uses.
402 if (Op.getOpcode() == ISD::FNEG) return Op.getOperand(0);
403
404 // Don't allow anything with multiple uses.
405 assert(Op.hasOneUse() && "Unknown reuse!");
406
407 switch (Op.getOpcode()) {
408 default: assert(0 && "Unknown code");
409 case ISD::ConstantFP:
410 return DAG.getConstantFP(-cast<ConstantFPSDNode>(Op)->getValue(),
411 Op.getValueType());
412 case ISD::FADD:
413 // FIXME: determine better conditions for this xform.
414 assert(UnsafeFPMath);
415
416 // -(A+B) -> -A - B
417 if (isNegatibleForFree(Op.getOperand(0)))
418 return DAG.getNode(ISD::FSUB, Op.getValueType(),
419 GetNegatedExpression(Op.getOperand(0), DAG),
420 Op.getOperand(1));
421 // -(A+B) -> -B - A
422 return DAG.getNode(ISD::FSUB, Op.getValueType(),
423 GetNegatedExpression(Op.getOperand(1), DAG),
424 Op.getOperand(0));
425 case ISD::FSUB:
426 // We can't turn -(A-B) into B-A when we honor signed zeros.
427 assert(UnsafeFPMath);
428
429 // -(A-B) -> B-A
430 return DAG.getNode(ISD::FSUB, Op.getValueType(), Op.getOperand(1),
431 Op.getOperand(0));
432
433 case ISD::FMUL:
434 case ISD::FDIV:
435 assert(!HonorSignDependentRoundingFPMath());
436
437 // -(X*Y) -> -X * Y
438 if (isNegatibleForFree(Op.getOperand(0)))
439 return DAG.getNode(Op.getOpcode(), Op.getValueType(),
440 GetNegatedExpression(Op.getOperand(0), DAG),
441 Op.getOperand(1));
442
443 // -(X*Y) -> X * -Y
444 return DAG.getNode(Op.getOpcode(), Op.getValueType(),
445 Op.getOperand(0),
446 GetNegatedExpression(Op.getOperand(1), DAG));
447
448 case ISD::FP_EXTEND:
449 case ISD::FP_ROUND:
450 case ISD::FSIN:
451 return DAG.getNode(Op.getOpcode(), Op.getValueType(),
452 GetNegatedExpression(Op, DAG));
453 }
454}
Chris Lattner24664722006-03-01 04:53:38 +0000455
456
Nate Begeman4ebd8052005-09-01 23:24:04 +0000457// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
458// that selects between the values 1 and 0, making it equivalent to a setcc.
Nate Begeman646d7e22005-09-02 21:18:40 +0000459// Also, set the incoming LHS, RHS, and CC references to the appropriate
460// nodes based on the type of node we are checking. This simplifies life a
461// bit for the callers.
462static bool isSetCCEquivalent(SDOperand N, SDOperand &LHS, SDOperand &RHS,
463 SDOperand &CC) {
464 if (N.getOpcode() == ISD::SETCC) {
465 LHS = N.getOperand(0);
466 RHS = N.getOperand(1);
467 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000468 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000469 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000470 if (N.getOpcode() == ISD::SELECT_CC &&
471 N.getOperand(2).getOpcode() == ISD::Constant &&
472 N.getOperand(3).getOpcode() == ISD::Constant &&
473 cast<ConstantSDNode>(N.getOperand(2))->getValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000474 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
475 LHS = N.getOperand(0);
476 RHS = N.getOperand(1);
477 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000478 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000479 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000480 return false;
481}
482
Nate Begeman99801192005-09-07 23:25:52 +0000483// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
484// one use. If this is true, it allows the users to invert the operation for
485// free when it is profitable to do so.
486static bool isOneUseSetCC(SDOperand N) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000487 SDOperand N0, N1, N2;
Nate Begeman646d7e22005-09-02 21:18:40 +0000488 if (isSetCCEquivalent(N, N0, N1, N2) && N.Val->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000489 return true;
490 return false;
491}
492
Nate Begemancd4d58c2006-02-03 06:46:56 +0000493SDOperand DAGCombiner::ReassociateOps(unsigned Opc, SDOperand N0, SDOperand N1){
494 MVT::ValueType VT = N0.getValueType();
495 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
496 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
497 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
498 if (isa<ConstantSDNode>(N1)) {
499 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(1), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000500 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000501 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(0));
502 } else if (N0.hasOneUse()) {
503 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(0), 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(1));
506 }
507 }
508 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
509 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
510 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
511 if (isa<ConstantSDNode>(N0)) {
512 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(1), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000513 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000514 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(0));
515 } else if (N1.hasOneUse()) {
516 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(0), 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(1));
519 }
520 }
521 return SDOperand();
522}
523
Chris Lattner29446522007-05-14 22:04:50 +0000524//===----------------------------------------------------------------------===//
525// Main DAG Combiner implementation
526//===----------------------------------------------------------------------===//
527
Nate Begeman4ebd8052005-09-01 23:24:04 +0000528void DAGCombiner::Run(bool RunningAfterLegalize) {
529 // set the instance variable, so that the various visit routines may use it.
530 AfterLegalize = RunningAfterLegalize;
531
Nate Begeman646d7e22005-09-02 21:18:40 +0000532 // Add all the dag nodes to the worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000533 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
534 E = DAG.allnodes_end(); I != E; ++I)
535 WorkList.push_back(I);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000536
Chris Lattner95038592005-10-05 06:35:28 +0000537 // Create a dummy node (which is not added to allnodes), that adds a reference
538 // to the root node, preventing it from being deleted, and tracking any
539 // changes of the root.
540 HandleSDNode Dummy(DAG.getRoot());
541
Jim Laskey26f7fa72006-10-17 19:33:52 +0000542 // The root of the dag may dangle to deleted nodes until the dag combiner is
543 // done. Set it to null to avoid confusion.
544 DAG.setRoot(SDOperand());
Chris Lattner24664722006-03-01 04:53:38 +0000545
546 /// DagCombineInfo - Expose the DAG combiner to the target combiner impls.
547 TargetLowering::DAGCombinerInfo
Evan Chengfa1eb272007-02-08 22:13:59 +0000548 DagCombineInfo(DAG, !RunningAfterLegalize, false, this);
Jim Laskey6ff23e52006-10-04 16:53:27 +0000549
Nate Begeman1d4d4142005-09-01 00:19:25 +0000550 // while the worklist isn't empty, inspect the node on the end of it and
551 // try and combine it.
552 while (!WorkList.empty()) {
553 SDNode *N = WorkList.back();
554 WorkList.pop_back();
555
556 // If N has no uses, it is dead. Make sure to revisit all N's operands once
Chris Lattner95038592005-10-05 06:35:28 +0000557 // N is deleted from the DAG, since they too may now be dead or may have a
558 // reduced number of uses, allowing other xforms.
559 if (N->use_empty() && N != &Dummy) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000560 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Jim Laskey6ff23e52006-10-04 16:53:27 +0000561 AddToWorkList(N->getOperand(i).Val);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000562
Chris Lattner95038592005-10-05 06:35:28 +0000563 DAG.DeleteNode(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000564 continue;
565 }
566
Nate Begeman83e75ec2005-09-06 04:43:02 +0000567 SDOperand RV = visit(N);
Chris Lattner24664722006-03-01 04:53:38 +0000568
569 // If nothing happened, try a target-specific DAG combine.
570 if (RV.Val == 0) {
Chris Lattner729c6d12006-05-27 00:43:02 +0000571 assert(N->getOpcode() != ISD::DELETED_NODE &&
572 "Node was deleted but visit returned NULL!");
Chris Lattner24664722006-03-01 04:53:38 +0000573 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
574 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode()))
575 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
576 }
577
Nate Begeman83e75ec2005-09-06 04:43:02 +0000578 if (RV.Val) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000579 ++NodesCombined;
Nate Begeman646d7e22005-09-02 21:18:40 +0000580 // If we get back the same node we passed in, rather than a new node or
581 // zero, we know that the node must have defined multiple values and
582 // CombineTo was used. Since CombineTo takes care of the worklist
583 // mechanics for us, we have no work to do in this case.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000584 if (RV.Val != N) {
Chris Lattner729c6d12006-05-27 00:43:02 +0000585 assert(N->getOpcode() != ISD::DELETED_NODE &&
586 RV.Val->getOpcode() != ISD::DELETED_NODE &&
587 "Node was deleted but visit returned new node!");
588
Bill Wendling832171c2006-12-07 20:04:42 +0000589 DOUT << "\nReplacing.3 "; DEBUG(N->dump());
590 DOUT << "\nWith: "; DEBUG(RV.Val->dump(&DAG));
591 DOUT << '\n';
Chris Lattner01a22022005-10-10 22:04:48 +0000592 std::vector<SDNode*> NowDead;
Evan Cheng2adffa12006-09-21 19:04:05 +0000593 if (N->getNumValues() == RV.Val->getNumValues())
594 DAG.ReplaceAllUsesWith(N, RV.Val, &NowDead);
595 else {
596 assert(N->getValueType(0) == RV.getValueType() && "Type mismatch");
597 SDOperand OpV = RV;
598 DAG.ReplaceAllUsesWith(N, &OpV, &NowDead);
599 }
Nate Begeman646d7e22005-09-02 21:18:40 +0000600
601 // Push the new node and any users onto the worklist
Jim Laskey6ff23e52006-10-04 16:53:27 +0000602 AddToWorkList(RV.Val);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000603 AddUsersToWorkList(RV.Val);
Nate Begeman646d7e22005-09-02 21:18:40 +0000604
Jim Laskey6ff23e52006-10-04 16:53:27 +0000605 // Nodes can be reintroduced into the worklist. Make sure we do not
606 // process a node that has been replaced.
Nate Begeman646d7e22005-09-02 21:18:40 +0000607 removeFromWorkList(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000608 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
609 removeFromWorkList(NowDead[i]);
Chris Lattner5c46f742005-10-05 06:11:08 +0000610
611 // Finally, since the node is now dead, remove it from the graph.
612 DAG.DeleteNode(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000613 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000614 }
615 }
Chris Lattner95038592005-10-05 06:35:28 +0000616
617 // If the root changed (e.g. it was a dead load, update the root).
618 DAG.setRoot(Dummy.getValue());
Nate Begeman1d4d4142005-09-01 00:19:25 +0000619}
620
Nate Begeman83e75ec2005-09-06 04:43:02 +0000621SDOperand DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000622 switch(N->getOpcode()) {
623 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +0000624 case ISD::TokenFactor: return visitTokenFactor(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000625 case ISD::ADD: return visitADD(N);
626 case ISD::SUB: return visitSUB(N);
Chris Lattner91153682007-03-04 20:03:15 +0000627 case ISD::ADDC: return visitADDC(N);
628 case ISD::ADDE: return visitADDE(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000629 case ISD::MUL: return visitMUL(N);
630 case ISD::SDIV: return visitSDIV(N);
631 case ISD::UDIV: return visitUDIV(N);
632 case ISD::SREM: return visitSREM(N);
633 case ISD::UREM: return visitUREM(N);
634 case ISD::MULHU: return visitMULHU(N);
635 case ISD::MULHS: return visitMULHS(N);
636 case ISD::AND: return visitAND(N);
637 case ISD::OR: return visitOR(N);
638 case ISD::XOR: return visitXOR(N);
639 case ISD::SHL: return visitSHL(N);
640 case ISD::SRA: return visitSRA(N);
641 case ISD::SRL: return visitSRL(N);
642 case ISD::CTLZ: return visitCTLZ(N);
643 case ISD::CTTZ: return visitCTTZ(N);
644 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000645 case ISD::SELECT: return visitSELECT(N);
646 case ISD::SELECT_CC: return visitSELECT_CC(N);
647 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000648 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
649 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000650 case ISD::ANY_EXTEND: return visitANY_EXTEND(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000651 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
652 case ISD::TRUNCATE: return visitTRUNCATE(N);
Chris Lattner94683772005-12-23 05:30:37 +0000653 case ISD::BIT_CONVERT: return visitBIT_CONVERT(N);
Chris Lattner6258fb22006-04-02 02:53:43 +0000654 case ISD::VBIT_CONVERT: return visitVBIT_CONVERT(N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000655 case ISD::FADD: return visitFADD(N);
656 case ISD::FSUB: return visitFSUB(N);
657 case ISD::FMUL: return visitFMUL(N);
658 case ISD::FDIV: return visitFDIV(N);
659 case ISD::FREM: return visitFREM(N);
Chris Lattner12d83032006-03-05 05:30:57 +0000660 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000661 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
662 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
663 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
664 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
665 case ISD::FP_ROUND: return visitFP_ROUND(N);
666 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
667 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
668 case ISD::FNEG: return visitFNEG(N);
669 case ISD::FABS: return visitFABS(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000670 case ISD::BRCOND: return visitBRCOND(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000671 case ISD::BR_CC: return visitBR_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000672 case ISD::LOAD: return visitLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000673 case ISD::STORE: return visitSTORE(N);
Chris Lattnerca242442006-03-19 01:27:56 +0000674 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
675 case ISD::VINSERT_VECTOR_ELT: return visitVINSERT_VECTOR_ELT(N);
Chris Lattnerd7648c82006-03-28 20:28:38 +0000676 case ISD::VBUILD_VECTOR: return visitVBUILD_VECTOR(N);
Chris Lattner66445d32006-03-28 22:11:53 +0000677 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
Chris Lattnerf1d0c622006-03-31 22:16:43 +0000678 case ISD::VVECTOR_SHUFFLE: return visitVVECTOR_SHUFFLE(N);
Chris Lattneredab1b92006-04-02 03:25:57 +0000679 case ISD::VADD: return visitVBinOp(N, ISD::ADD , ISD::FADD);
680 case ISD::VSUB: return visitVBinOp(N, ISD::SUB , ISD::FSUB);
681 case ISD::VMUL: return visitVBinOp(N, ISD::MUL , ISD::FMUL);
682 case ISD::VSDIV: return visitVBinOp(N, ISD::SDIV, ISD::FDIV);
683 case ISD::VUDIV: return visitVBinOp(N, ISD::UDIV, ISD::UDIV);
684 case ISD::VAND: return visitVBinOp(N, ISD::AND , ISD::AND);
685 case ISD::VOR: return visitVBinOp(N, ISD::OR , ISD::OR);
686 case ISD::VXOR: return visitVBinOp(N, ISD::XOR , ISD::XOR);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000687 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000688 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000689}
690
Chris Lattner6270f682006-10-08 22:57:01 +0000691/// getInputChainForNode - Given a node, return its input chain if it has one,
692/// otherwise return a null sd operand.
693static SDOperand getInputChainForNode(SDNode *N) {
694 if (unsigned NumOps = N->getNumOperands()) {
695 if (N->getOperand(0).getValueType() == MVT::Other)
696 return N->getOperand(0);
697 else if (N->getOperand(NumOps-1).getValueType() == MVT::Other)
698 return N->getOperand(NumOps-1);
699 for (unsigned i = 1; i < NumOps-1; ++i)
700 if (N->getOperand(i).getValueType() == MVT::Other)
701 return N->getOperand(i);
702 }
703 return SDOperand(0, 0);
704}
705
Nate Begeman83e75ec2005-09-06 04:43:02 +0000706SDOperand DAGCombiner::visitTokenFactor(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +0000707 // If N has two operands, where one has an input chain equal to the other,
708 // the 'other' chain is redundant.
709 if (N->getNumOperands() == 2) {
710 if (getInputChainForNode(N->getOperand(0).Val) == N->getOperand(1))
711 return N->getOperand(0);
712 if (getInputChainForNode(N->getOperand(1).Val) == N->getOperand(0))
713 return N->getOperand(1);
714 }
715
716
Jim Laskey6ff23e52006-10-04 16:53:27 +0000717 SmallVector<SDNode *, 8> TFs; // List of token factors to visit.
Jim Laskey279f0532006-09-25 16:29:54 +0000718 SmallVector<SDOperand, 8> Ops; // Ops for replacing token factor.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000719 bool Changed = false; // If we should replace this token factor.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000720
721 // Start out with this token factor.
Jim Laskey279f0532006-09-25 16:29:54 +0000722 TFs.push_back(N);
Jim Laskey279f0532006-09-25 16:29:54 +0000723
Jim Laskey71382342006-10-07 23:37:56 +0000724 // Iterate through token factors. The TFs grows when new token factors are
Jim Laskeybc588b82006-10-05 15:07:25 +0000725 // encountered.
726 for (unsigned i = 0; i < TFs.size(); ++i) {
727 SDNode *TF = TFs[i];
728
Jim Laskey6ff23e52006-10-04 16:53:27 +0000729 // Check each of the operands.
730 for (unsigned i = 0, ie = TF->getNumOperands(); i != ie; ++i) {
731 SDOperand Op = TF->getOperand(i);
Jim Laskey279f0532006-09-25 16:29:54 +0000732
Jim Laskey6ff23e52006-10-04 16:53:27 +0000733 switch (Op.getOpcode()) {
734 case ISD::EntryToken:
Jim Laskeybc588b82006-10-05 15:07:25 +0000735 // Entry tokens don't need to be added to the list. They are
736 // rededundant.
737 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000738 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000739
Jim Laskey6ff23e52006-10-04 16:53:27 +0000740 case ISD::TokenFactor:
Jim Laskeybc588b82006-10-05 15:07:25 +0000741 if ((CombinerAA || Op.hasOneUse()) &&
742 std::find(TFs.begin(), TFs.end(), Op.Val) == TFs.end()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +0000743 // Queue up for processing.
744 TFs.push_back(Op.Val);
745 // Clean up in case the token factor is removed.
746 AddToWorkList(Op.Val);
747 Changed = true;
748 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000749 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000750 // Fall thru
751
752 default:
Jim Laskeybc588b82006-10-05 15:07:25 +0000753 // Only add if not there prior.
754 if (std::find(Ops.begin(), Ops.end(), Op) == Ops.end())
755 Ops.push_back(Op);
Jim Laskey6ff23e52006-10-04 16:53:27 +0000756 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000757 }
758 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000759 }
760
761 SDOperand Result;
762
763 // If we've change things around then replace token factor.
764 if (Changed) {
765 if (Ops.size() == 0) {
766 // The entry token is the only possible outcome.
767 Result = DAG.getEntryNode();
768 } else {
769 // New and improved token factor.
770 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0], Ops.size());
Nate Begemanded49632005-10-13 03:11:28 +0000771 }
Jim Laskey274062c2006-10-13 23:32:28 +0000772
773 // Don't add users to work list.
774 return CombineTo(N, Result, false);
Nate Begemanded49632005-10-13 03:11:28 +0000775 }
Jim Laskey279f0532006-09-25 16:29:54 +0000776
Jim Laskey6ff23e52006-10-04 16:53:27 +0000777 return Result;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000778}
779
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000780static
781SDOperand combineShlAddConstant(SDOperand N0, SDOperand N1, SelectionDAG &DAG) {
782 MVT::ValueType VT = N0.getValueType();
783 SDOperand N00 = N0.getOperand(0);
784 SDOperand N01 = N0.getOperand(1);
785 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N01);
786 if (N01C && N00.getOpcode() == ISD::ADD && N00.Val->hasOneUse() &&
787 isa<ConstantSDNode>(N00.getOperand(1))) {
788 N0 = DAG.getNode(ISD::ADD, VT,
789 DAG.getNode(ISD::SHL, VT, N00.getOperand(0), N01),
790 DAG.getNode(ISD::SHL, VT, N00.getOperand(1), N01));
791 return DAG.getNode(ISD::ADD, VT, N0, N1);
792 }
793 return SDOperand();
794}
795
Nate Begeman83e75ec2005-09-06 04:43:02 +0000796SDOperand DAGCombiner::visitADD(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000797 SDOperand N0 = N->getOperand(0);
798 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000799 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
800 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemanf89d78d2005-09-07 16:09:19 +0000801 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000802
803 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000804 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000805 return DAG.getNode(ISD::ADD, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000806 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000807 if (N0C && !N1C)
808 return DAG.getNode(ISD::ADD, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000809 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000810 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000811 return N0;
Chris Lattner4aafb4f2006-01-12 20:22:43 +0000812 // fold ((c1-A)+c2) -> (c1+c2)-A
813 if (N1C && N0.getOpcode() == ISD::SUB)
814 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
815 return DAG.getNode(ISD::SUB, VT,
816 DAG.getConstant(N1C->getValue()+N0C->getValue(), VT),
817 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000818 // reassociate add
819 SDOperand RADD = ReassociateOps(ISD::ADD, N0, N1);
820 if (RADD.Val != 0)
821 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000822 // fold ((0-A) + B) -> B-A
823 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
824 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000825 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000826 // fold (A + (0-B)) -> A-B
827 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
828 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000829 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +0000830 // fold (A+(B-A)) -> B
831 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000832 return N1.getOperand(0);
Chris Lattner947c2892006-03-13 06:51:27 +0000833
Evan Cheng860771d2006-03-01 01:09:54 +0000834 if (!MVT::isVector(VT) && SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +0000835 return SDOperand(N, 0);
Chris Lattner947c2892006-03-13 06:51:27 +0000836
837 // fold (a+b) -> (a|b) iff a and b share no bits.
838 if (MVT::isInteger(VT) && !MVT::isVector(VT)) {
839 uint64_t LHSZero, LHSOne;
840 uint64_t RHSZero, RHSOne;
841 uint64_t Mask = MVT::getIntVTBitMask(VT);
842 TLI.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
843 if (LHSZero) {
844 TLI.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
845
846 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
847 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
848 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
849 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
850 return DAG.getNode(ISD::OR, VT, N0, N1);
851 }
852 }
Evan Cheng3ef554d2006-11-06 08:14:30 +0000853
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000854 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
855 if (N0.getOpcode() == ISD::SHL && N0.Val->hasOneUse()) {
856 SDOperand Result = combineShlAddConstant(N0, N1, DAG);
857 if (Result.Val) return Result;
858 }
859 if (N1.getOpcode() == ISD::SHL && N1.Val->hasOneUse()) {
860 SDOperand Result = combineShlAddConstant(N1, N0, DAG);
861 if (Result.Val) return Result;
862 }
863
Nate Begeman83e75ec2005-09-06 04:43:02 +0000864 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000865}
866
Chris Lattner91153682007-03-04 20:03:15 +0000867SDOperand DAGCombiner::visitADDC(SDNode *N) {
868 SDOperand N0 = N->getOperand(0);
869 SDOperand N1 = N->getOperand(1);
870 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
871 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
872 MVT::ValueType VT = N0.getValueType();
873
874 // If the flag result is dead, turn this into an ADD.
875 if (N->hasNUsesOfValue(0, 1))
876 return CombineTo(N, DAG.getNode(ISD::ADD, VT, N1, N0),
Chris Lattnerb6541762007-03-04 20:40:38 +0000877 DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
Chris Lattner91153682007-03-04 20:03:15 +0000878
879 // canonicalize constant to RHS.
Chris Lattnerbcf24842007-03-04 20:08:45 +0000880 if (N0C && !N1C) {
881 SDOperand Ops[] = { N1, N0 };
882 return DAG.getNode(ISD::ADDC, N->getVTList(), Ops, 2);
883 }
Chris Lattner91153682007-03-04 20:03:15 +0000884
Chris Lattnerb6541762007-03-04 20:40:38 +0000885 // fold (addc x, 0) -> x + no carry out
886 if (N1C && N1C->isNullValue())
887 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
888
889 // fold (addc a, b) -> (or a, b), CARRY_FALSE iff a and b share no bits.
890 uint64_t LHSZero, LHSOne;
891 uint64_t RHSZero, RHSOne;
892 uint64_t Mask = MVT::getIntVTBitMask(VT);
893 TLI.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
894 if (LHSZero) {
895 TLI.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
896
897 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
898 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
899 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
900 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
901 return CombineTo(N, DAG.getNode(ISD::OR, VT, N0, N1),
902 DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
903 }
Chris Lattner91153682007-03-04 20:03:15 +0000904
905 return SDOperand();
906}
907
908SDOperand DAGCombiner::visitADDE(SDNode *N) {
909 SDOperand N0 = N->getOperand(0);
910 SDOperand N1 = N->getOperand(1);
Chris Lattnerb6541762007-03-04 20:40:38 +0000911 SDOperand CarryIn = N->getOperand(2);
Chris Lattner91153682007-03-04 20:03:15 +0000912 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
913 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Chris Lattnerbcf24842007-03-04 20:08:45 +0000914 //MVT::ValueType VT = N0.getValueType();
Chris Lattner91153682007-03-04 20:03:15 +0000915
916 // canonicalize constant to RHS
Chris Lattnerbcf24842007-03-04 20:08:45 +0000917 if (N0C && !N1C) {
Chris Lattnerb6541762007-03-04 20:40:38 +0000918 SDOperand Ops[] = { N1, N0, CarryIn };
Chris Lattnerbcf24842007-03-04 20:08:45 +0000919 return DAG.getNode(ISD::ADDE, N->getVTList(), Ops, 3);
920 }
Chris Lattner91153682007-03-04 20:03:15 +0000921
Chris Lattnerb6541762007-03-04 20:40:38 +0000922 // fold (adde x, y, false) -> (addc x, y)
923 if (CarryIn.getOpcode() == ISD::CARRY_FALSE) {
924 SDOperand Ops[] = { N1, N0 };
925 return DAG.getNode(ISD::ADDC, N->getVTList(), Ops, 2);
926 }
Chris Lattner91153682007-03-04 20:03:15 +0000927
928 return SDOperand();
929}
930
931
932
Nate Begeman83e75ec2005-09-06 04:43:02 +0000933SDOperand DAGCombiner::visitSUB(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000934 SDOperand N0 = N->getOperand(0);
935 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000936 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
937 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000938 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000939
Chris Lattner854077d2005-10-17 01:07:11 +0000940 // fold (sub x, x) -> 0
941 if (N0 == N1)
942 return DAG.getConstant(0, N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000943 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000944 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000945 return DAG.getNode(ISD::SUB, VT, N0, N1);
Chris Lattner05b57432005-10-11 06:07:15 +0000946 // fold (sub x, c) -> (add x, -c)
947 if (N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000948 return DAG.getNode(ISD::ADD, VT, N0, DAG.getConstant(-N1C->getValue(), VT));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000949 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +0000950 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000951 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000952 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +0000953 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000954 return N0.getOperand(0);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000955 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000956}
957
Nate Begeman83e75ec2005-09-06 04:43:02 +0000958SDOperand DAGCombiner::visitMUL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000959 SDOperand N0 = N->getOperand(0);
960 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000961 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
962 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman223df222005-09-08 20:18:10 +0000963 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000964
965 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000966 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000967 return DAG.getNode(ISD::MUL, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000968 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000969 if (N0C && !N1C)
970 return DAG.getNode(ISD::MUL, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000971 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000972 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000973 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000974 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +0000975 if (N1C && N1C->isAllOnesValue())
Nate Begeman405e3ec2005-10-21 00:02:42 +0000976 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000977 // fold (mul x, (1 << c)) -> x << c
Nate Begeman646d7e22005-09-02 21:18:40 +0000978 if (N1C && isPowerOf2_64(N1C->getValue()))
Chris Lattner3e6099b2005-10-30 06:41:49 +0000979 return DAG.getNode(ISD::SHL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000980 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000981 TLI.getShiftAmountTy()));
Chris Lattner3e6099b2005-10-30 06:41:49 +0000982 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
983 if (N1C && isPowerOf2_64(-N1C->getSignExtended())) {
984 // FIXME: If the input is something that is easily negated (e.g. a
985 // single-use add), we should put the negate there.
986 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT),
987 DAG.getNode(ISD::SHL, VT, N0,
988 DAG.getConstant(Log2_64(-N1C->getSignExtended()),
989 TLI.getShiftAmountTy())));
990 }
Andrew Lenharth50a0d422006-04-02 21:42:45 +0000991
Chris Lattner0b1a85f2006-03-01 03:44:24 +0000992 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
993 if (N1C && N0.getOpcode() == ISD::SHL &&
994 isa<ConstantSDNode>(N0.getOperand(1))) {
995 SDOperand C3 = DAG.getNode(ISD::SHL, VT, N1, N0.getOperand(1));
Chris Lattner5750df92006-03-01 04:03:14 +0000996 AddToWorkList(C3.Val);
Chris Lattner0b1a85f2006-03-01 03:44:24 +0000997 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0), C3);
998 }
999
1000 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
1001 // use.
1002 {
1003 SDOperand Sh(0,0), Y(0,0);
1004 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
1005 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
1006 N0.Val->hasOneUse()) {
1007 Sh = N0; Y = N1;
1008 } else if (N1.getOpcode() == ISD::SHL &&
1009 isa<ConstantSDNode>(N1.getOperand(1)) && N1.Val->hasOneUse()) {
1010 Sh = N1; Y = N0;
1011 }
1012 if (Sh.Val) {
1013 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Sh.getOperand(0), Y);
1014 return DAG.getNode(ISD::SHL, VT, Mul, Sh.getOperand(1));
1015 }
1016 }
Chris Lattnera1deca32006-03-04 23:33:26 +00001017 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
1018 if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() &&
1019 isa<ConstantSDNode>(N0.getOperand(1))) {
1020 return DAG.getNode(ISD::ADD, VT,
1021 DAG.getNode(ISD::MUL, VT, N0.getOperand(0), N1),
1022 DAG.getNode(ISD::MUL, VT, N0.getOperand(1), N1));
1023 }
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001024
Nate Begemancd4d58c2006-02-03 06:46:56 +00001025 // reassociate mul
1026 SDOperand RMUL = ReassociateOps(ISD::MUL, N0, N1);
1027 if (RMUL.Val != 0)
1028 return RMUL;
Nate Begeman83e75ec2005-09-06 04:43:02 +00001029 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001030}
1031
Nate Begeman83e75ec2005-09-06 04:43:02 +00001032SDOperand DAGCombiner::visitSDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001033 SDOperand N0 = N->getOperand(0);
1034 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001035 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1036 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +00001037 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001038
1039 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001040 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001041 return DAG.getNode(ISD::SDIV, VT, N0, N1);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001042 // fold (sdiv X, 1) -> X
1043 if (N1C && N1C->getSignExtended() == 1LL)
1044 return N0;
1045 // fold (sdiv X, -1) -> 0-X
1046 if (N1C && N1C->isAllOnesValue())
1047 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +00001048 // If we know the sign bits of both operands are zero, strength reduce to a
1049 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
1050 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001051 if (TLI.MaskedValueIsZero(N1, SignBit) &&
1052 TLI.MaskedValueIsZero(N0, SignBit))
Chris Lattner094c8fc2005-10-07 06:10:46 +00001053 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
Nate Begemancd6a6ed2006-02-17 07:26:20 +00001054 // fold (sdiv X, pow2) -> simple ops after legalize
Nate Begemanfb7217b2006-02-17 19:54:08 +00001055 if (N1C && N1C->getValue() && !TLI.isIntDivCheap() &&
Nate Begeman405e3ec2005-10-21 00:02:42 +00001056 (isPowerOf2_64(N1C->getSignExtended()) ||
1057 isPowerOf2_64(-N1C->getSignExtended()))) {
1058 // If dividing by powers of two is cheap, then don't perform the following
1059 // fold.
1060 if (TLI.isPow2DivCheap())
1061 return SDOperand();
1062 int64_t pow2 = N1C->getSignExtended();
1063 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
Chris Lattner8f4880b2006-02-16 08:02:36 +00001064 unsigned lg2 = Log2_64(abs2);
1065 // Splat the sign bit into the register
1066 SDOperand SGN = DAG.getNode(ISD::SRA, VT, N0,
Nate Begeman405e3ec2005-10-21 00:02:42 +00001067 DAG.getConstant(MVT::getSizeInBits(VT)-1,
1068 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00001069 AddToWorkList(SGN.Val);
Chris Lattner8f4880b2006-02-16 08:02:36 +00001070 // Add (N0 < 0) ? abs2 - 1 : 0;
1071 SDOperand SRL = DAG.getNode(ISD::SRL, VT, SGN,
1072 DAG.getConstant(MVT::getSizeInBits(VT)-lg2,
Nate Begeman405e3ec2005-10-21 00:02:42 +00001073 TLI.getShiftAmountTy()));
Chris Lattner8f4880b2006-02-16 08:02:36 +00001074 SDOperand ADD = DAG.getNode(ISD::ADD, VT, N0, SRL);
Chris Lattner5750df92006-03-01 04:03:14 +00001075 AddToWorkList(SRL.Val);
1076 AddToWorkList(ADD.Val); // Divide by pow2
Chris Lattner8f4880b2006-02-16 08:02:36 +00001077 SDOperand SRA = DAG.getNode(ISD::SRA, VT, ADD,
1078 DAG.getConstant(lg2, TLI.getShiftAmountTy()));
Nate Begeman405e3ec2005-10-21 00:02:42 +00001079 // If we're dividing by a positive value, we're done. Otherwise, we must
1080 // negate the result.
1081 if (pow2 > 0)
1082 return SRA;
Chris Lattner5750df92006-03-01 04:03:14 +00001083 AddToWorkList(SRA.Val);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001084 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), SRA);
1085 }
Nate Begeman69575232005-10-20 02:15:44 +00001086 // if integer divide is expensive and we satisfy the requirements, emit an
1087 // alternate sequence.
Nate Begeman405e3ec2005-10-21 00:02:42 +00001088 if (N1C && (N1C->getSignExtended() < -1 || N1C->getSignExtended() > 1) &&
Chris Lattnere9936d12005-10-22 18:50:15 +00001089 !TLI.isIntDivCheap()) {
1090 SDOperand Op = BuildSDIV(N);
1091 if (Op.Val) return Op;
Nate Begeman69575232005-10-20 02:15:44 +00001092 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001093 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001094}
1095
Nate Begeman83e75ec2005-09-06 04:43:02 +00001096SDOperand DAGCombiner::visitUDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001097 SDOperand N0 = N->getOperand(0);
1098 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001099 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1100 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +00001101 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001102
1103 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001104 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001105 return DAG.getNode(ISD::UDIV, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001106 // fold (udiv x, (1 << c)) -> x >>u c
Nate Begeman646d7e22005-09-02 21:18:40 +00001107 if (N1C && isPowerOf2_64(N1C->getValue()))
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001108 return DAG.getNode(ISD::SRL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +00001109 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001110 TLI.getShiftAmountTy()));
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001111 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
1112 if (N1.getOpcode() == ISD::SHL) {
1113 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
1114 if (isPowerOf2_64(SHC->getValue())) {
1115 MVT::ValueType ADDVT = N1.getOperand(1).getValueType();
Nate Begemanc031e332006-02-05 07:36:48 +00001116 SDOperand Add = DAG.getNode(ISD::ADD, ADDVT, N1.getOperand(1),
1117 DAG.getConstant(Log2_64(SHC->getValue()),
1118 ADDVT));
Chris Lattner5750df92006-03-01 04:03:14 +00001119 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +00001120 return DAG.getNode(ISD::SRL, VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001121 }
1122 }
1123 }
Nate Begeman69575232005-10-20 02:15:44 +00001124 // fold (udiv x, c) -> alternate
Chris Lattnere9936d12005-10-22 18:50:15 +00001125 if (N1C && N1C->getValue() && !TLI.isIntDivCheap()) {
1126 SDOperand Op = BuildUDIV(N);
1127 if (Op.Val) return Op;
1128 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001129 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001130}
1131
Nate Begeman83e75ec2005-09-06 04:43:02 +00001132SDOperand DAGCombiner::visitSREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001133 SDOperand N0 = N->getOperand(0);
1134 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001135 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1136 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +00001137 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001138
1139 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001140 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001141 return DAG.getNode(ISD::SREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +00001142 // If we know the sign bits of both operands are zero, strength reduce to a
1143 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
1144 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001145 if (TLI.MaskedValueIsZero(N1, SignBit) &&
1146 TLI.MaskedValueIsZero(N0, SignBit))
Nate Begemana148d982006-01-18 22:35:16 +00001147 return DAG.getNode(ISD::UREM, VT, N0, N1);
Chris Lattner26d29902006-10-12 20:58:32 +00001148
1149 // Unconditionally lower X%C -> X-X/C*C. This allows the X/C logic to hack on
1150 // the remainder operation.
1151 if (N1C && !N1C->isNullValue()) {
1152 SDOperand Div = DAG.getNode(ISD::SDIV, VT, N0, N1);
1153 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Div, N1);
1154 SDOperand Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
1155 AddToWorkList(Div.Val);
1156 AddToWorkList(Mul.Val);
1157 return Sub;
1158 }
1159
Nate Begeman83e75ec2005-09-06 04:43:02 +00001160 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001161}
1162
Nate Begeman83e75ec2005-09-06 04:43:02 +00001163SDOperand DAGCombiner::visitUREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001164 SDOperand N0 = N->getOperand(0);
1165 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001166 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1167 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +00001168 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001169
1170 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001171 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001172 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +00001173 // fold (urem x, pow2) -> (and x, pow2-1)
1174 if (N1C && !N1C->isNullValue() && isPowerOf2_64(N1C->getValue()))
Nate Begemana148d982006-01-18 22:35:16 +00001175 return DAG.getNode(ISD::AND, VT, N0, DAG.getConstant(N1C->getValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +00001176 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
1177 if (N1.getOpcode() == ISD::SHL) {
1178 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
1179 if (isPowerOf2_64(SHC->getValue())) {
Nate Begemanbab92392006-02-05 08:07:24 +00001180 SDOperand Add = DAG.getNode(ISD::ADD, VT, N1,DAG.getConstant(~0ULL,VT));
Chris Lattner5750df92006-03-01 04:03:14 +00001181 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +00001182 return DAG.getNode(ISD::AND, VT, N0, Add);
1183 }
1184 }
1185 }
Chris Lattner26d29902006-10-12 20:58:32 +00001186
1187 // Unconditionally lower X%C -> X-X/C*C. This allows the X/C logic to hack on
1188 // the remainder operation.
1189 if (N1C && !N1C->isNullValue()) {
1190 SDOperand Div = DAG.getNode(ISD::UDIV, VT, N0, N1);
1191 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Div, N1);
1192 SDOperand Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
1193 AddToWorkList(Div.Val);
1194 AddToWorkList(Mul.Val);
1195 return Sub;
1196 }
1197
Nate Begeman83e75ec2005-09-06 04:43:02 +00001198 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001199}
1200
Nate Begeman83e75ec2005-09-06 04:43:02 +00001201SDOperand DAGCombiner::visitMULHS(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001202 SDOperand N0 = N->getOperand(0);
1203 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001204 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001205
1206 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001207 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001208 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001209 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001210 if (N1C && N1C->getValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001211 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
1212 DAG.getConstant(MVT::getSizeInBits(N0.getValueType())-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001213 TLI.getShiftAmountTy()));
1214 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001215}
1216
Nate Begeman83e75ec2005-09-06 04:43:02 +00001217SDOperand DAGCombiner::visitMULHU(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001218 SDOperand N0 = N->getOperand(0);
1219 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001220 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001221
1222 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001223 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001224 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001225 // fold (mulhu x, 1) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001226 if (N1C && N1C->getValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001227 return DAG.getConstant(0, N0.getValueType());
1228 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001229}
1230
Chris Lattner35e5c142006-05-05 05:51:50 +00001231/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
1232/// two operands of the same opcode, try to simplify it.
1233SDOperand DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
1234 SDOperand N0 = N->getOperand(0), N1 = N->getOperand(1);
1235 MVT::ValueType VT = N0.getValueType();
1236 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
1237
Chris Lattner540121f2006-05-05 06:31:05 +00001238 // For each of OP in AND/OR/XOR:
1239 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
1240 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
1241 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
Chris Lattner0d8dae72006-05-05 06:32:04 +00001242 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y))
Chris Lattner540121f2006-05-05 06:31:05 +00001243 if ((N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND||
Chris Lattner0d8dae72006-05-05 06:32:04 +00001244 N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::TRUNCATE) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001245 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1246 SDOperand ORNode = DAG.getNode(N->getOpcode(),
1247 N0.getOperand(0).getValueType(),
1248 N0.getOperand(0), N1.getOperand(0));
1249 AddToWorkList(ORNode.Val);
Chris Lattner540121f2006-05-05 06:31:05 +00001250 return DAG.getNode(N0.getOpcode(), VT, ORNode);
Chris Lattner35e5c142006-05-05 05:51:50 +00001251 }
1252
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001253 // For each of OP in SHL/SRL/SRA/AND...
1254 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
1255 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
1256 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
Chris Lattner35e5c142006-05-05 05:51:50 +00001257 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001258 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001259 N0.getOperand(1) == N1.getOperand(1)) {
1260 SDOperand ORNode = DAG.getNode(N->getOpcode(),
1261 N0.getOperand(0).getValueType(),
1262 N0.getOperand(0), N1.getOperand(0));
1263 AddToWorkList(ORNode.Val);
1264 return DAG.getNode(N0.getOpcode(), VT, ORNode, N0.getOperand(1));
1265 }
1266
1267 return SDOperand();
1268}
1269
Nate Begeman83e75ec2005-09-06 04:43:02 +00001270SDOperand DAGCombiner::visitAND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001271 SDOperand N0 = N->getOperand(0);
1272 SDOperand N1 = N->getOperand(1);
Nate Begemanfb7217b2006-02-17 19:54:08 +00001273 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001274 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1275 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001276 MVT::ValueType VT = N1.getValueType();
1277
1278 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001279 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001280 return DAG.getNode(ISD::AND, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001281 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001282 if (N0C && !N1C)
1283 return DAG.getNode(ISD::AND, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001284 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001285 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001286 return N0;
1287 // if (and x, c) is known to be zero, return 0
Nate Begeman368e18d2006-02-16 21:11:51 +00001288 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001289 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +00001290 // reassociate and
1291 SDOperand RAND = ReassociateOps(ISD::AND, N0, N1);
1292 if (RAND.Val != 0)
1293 return RAND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001294 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
Nate Begeman5dc7e862005-11-02 18:42:59 +00001295 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001296 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Nate Begeman646d7e22005-09-02 21:18:40 +00001297 if ((ORI->getValue() & N1C->getValue()) == N1C->getValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001298 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +00001299 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
1300 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Chris Lattner1ec05d12006-03-01 21:47:21 +00001301 unsigned InMask = MVT::getIntVTBitMask(N0.getOperand(0).getValueType());
Chris Lattner3603cd62006-02-02 07:17:31 +00001302 if (TLI.MaskedValueIsZero(N0.getOperand(0),
Chris Lattner1ec05d12006-03-01 21:47:21 +00001303 ~N1C->getValue() & InMask)) {
1304 SDOperand Zext = DAG.getNode(ISD::ZERO_EXTEND, N0.getValueType(),
1305 N0.getOperand(0));
1306
1307 // Replace uses of the AND with uses of the Zero extend node.
1308 CombineTo(N, Zext);
1309
Chris Lattner3603cd62006-02-02 07:17:31 +00001310 // We actually want to replace all uses of the any_extend with the
1311 // zero_extend, to avoid duplicating things. This will later cause this
1312 // AND to be folded.
Chris Lattner1ec05d12006-03-01 21:47:21 +00001313 CombineTo(N0.Val, Zext);
Chris Lattnerfedced72006-04-20 23:55:59 +00001314 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner3603cd62006-02-02 07:17:31 +00001315 }
1316 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001317 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
1318 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1319 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1320 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1321
1322 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1323 MVT::isInteger(LL.getValueType())) {
1324 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
1325 if (cast<ConstantSDNode>(LR)->getValue() == 0 && Op1 == ISD::SETEQ) {
1326 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001327 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001328 return DAG.getSetCC(VT, ORNode, LR, Op1);
1329 }
1330 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
1331 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
1332 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001333 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001334 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1335 }
1336 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
1337 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
1338 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001339 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001340 return DAG.getSetCC(VT, ORNode, LR, Op1);
1341 }
1342 }
1343 // canonicalize equivalent to ll == rl
1344 if (LL == RR && LR == RL) {
1345 Op1 = ISD::getSetCCSwappedOperands(Op1);
1346 std::swap(RL, RR);
1347 }
1348 if (LL == RL && LR == RR) {
1349 bool isInteger = MVT::isInteger(LL.getValueType());
1350 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
1351 if (Result != ISD::SETCC_INVALID)
1352 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1353 }
1354 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001355
1356 // Simplify: and (op x...), (op y...) -> (op (and x, y))
1357 if (N0.getOpcode() == N1.getOpcode()) {
1358 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1359 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001360 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001361
Nate Begemande996292006-02-03 22:24:05 +00001362 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
1363 // fold (and (sra)) -> (and (srl)) when possible.
Chris Lattner6ea2dee2006-03-25 22:19:00 +00001364 if (!MVT::isVector(VT) &&
1365 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001366 return SDOperand(N, 0);
Nate Begemanded49632005-10-13 03:11:28 +00001367 // fold (zext_inreg (extload x)) -> (zextload x)
Evan Cheng83060c52007-03-07 08:07:03 +00001368 if (ISD::isEXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val)) {
Evan Cheng466685d2006-10-09 20:57:25 +00001369 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00001370 MVT::ValueType EVT = LN0->getLoadedVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001371 // If we zero all the possible extended bits, then we can turn this into
1372 // a zextload if we are running before legalize or the operation is legal.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001373 if (TLI.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Evan Chengc5484282006-10-04 00:56:09 +00001374 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00001375 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
1376 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00001377 LN0->getSrcValueOffset(), EVT,
1378 LN0->isVolatile(),
1379 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00001380 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001381 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001382 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001383 }
1384 }
1385 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Evan Cheng83060c52007-03-07 08:07:03 +00001386 if (ISD::isSEXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val) &&
1387 N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001388 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00001389 MVT::ValueType EVT = LN0->getLoadedVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001390 // If we zero all the possible extended bits, then we can turn this into
1391 // a zextload if we are running before legalize or the operation is legal.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001392 if (TLI.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Evan Chengc5484282006-10-04 00:56:09 +00001393 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00001394 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
1395 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00001396 LN0->getSrcValueOffset(), EVT,
1397 LN0->isVolatile(),
1398 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00001399 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001400 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001401 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001402 }
1403 }
Chris Lattner15045b62006-02-28 06:35:35 +00001404
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001405 // fold (and (load x), 255) -> (zextload x, i8)
1406 // fold (and (extload x, i16), 255) -> (zextload x, i8)
Evan Cheng466685d2006-10-09 20:57:25 +00001407 if (N1C && N0.getOpcode() == ISD::LOAD) {
1408 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
1409 if (LN0->getExtensionType() != ISD::SEXTLOAD &&
Evan Cheng83060c52007-03-07 08:07:03 +00001410 LN0->getAddressingMode() == ISD::UNINDEXED &&
Evan Cheng466685d2006-10-09 20:57:25 +00001411 N0.hasOneUse()) {
1412 MVT::ValueType EVT, LoadedVT;
1413 if (N1C->getValue() == 255)
1414 EVT = MVT::i8;
1415 else if (N1C->getValue() == 65535)
1416 EVT = MVT::i16;
1417 else if (N1C->getValue() == ~0U)
1418 EVT = MVT::i32;
1419 else
1420 EVT = MVT::Other;
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001421
Evan Cheng2e49f092006-10-11 07:10:22 +00001422 LoadedVT = LN0->getLoadedVT();
Evan Cheng466685d2006-10-09 20:57:25 +00001423 if (EVT != MVT::Other && LoadedVT > EVT &&
1424 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
1425 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
1426 // For big endian targets, we need to add an offset to the pointer to
1427 // load the correct bytes. For little endian systems, we merely need to
1428 // read fewer bytes from the same pointer.
1429 unsigned PtrOff =
1430 (MVT::getSizeInBits(LoadedVT) - MVT::getSizeInBits(EVT)) / 8;
1431 SDOperand NewPtr = LN0->getBasePtr();
1432 if (!TLI.isLittleEndian())
1433 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
1434 DAG.getConstant(PtrOff, PtrType));
1435 AddToWorkList(NewPtr.Val);
1436 SDOperand Load =
1437 DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00001438 LN0->getSrcValue(), LN0->getSrcValueOffset(), EVT,
1439 LN0->isVolatile(), LN0->getAlignment());
Evan Cheng466685d2006-10-09 20:57:25 +00001440 AddToWorkList(N);
1441 CombineTo(N0.Val, Load, Load.getValue(1));
1442 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1443 }
Chris Lattner15045b62006-02-28 06:35:35 +00001444 }
1445 }
1446
Nate Begeman83e75ec2005-09-06 04:43:02 +00001447 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001448}
1449
Nate Begeman83e75ec2005-09-06 04:43:02 +00001450SDOperand DAGCombiner::visitOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001451 SDOperand N0 = N->getOperand(0);
1452 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001453 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001454 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1455 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001456 MVT::ValueType VT = N1.getValueType();
1457 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001458
1459 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001460 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001461 return DAG.getNode(ISD::OR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001462 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001463 if (N0C && !N1C)
1464 return DAG.getNode(ISD::OR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001465 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001466 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001467 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001468 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001469 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001470 return N1;
1471 // fold (or x, c) -> c iff (x & ~c) == 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001472 if (N1C &&
1473 TLI.MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits))))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001474 return N1;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001475 // reassociate or
1476 SDOperand ROR = ReassociateOps(ISD::OR, N0, N1);
1477 if (ROR.Val != 0)
1478 return ROR;
1479 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
1480 if (N1C && N0.getOpcode() == ISD::AND && N0.Val->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00001481 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00001482 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
1483 return DAG.getNode(ISD::AND, VT, DAG.getNode(ISD::OR, VT, N0.getOperand(0),
1484 N1),
1485 DAG.getConstant(N1C->getValue() | C1->getValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00001486 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001487 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
1488 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1489 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1490 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1491
1492 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1493 MVT::isInteger(LL.getValueType())) {
1494 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
1495 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
1496 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
1497 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
1498 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001499 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001500 return DAG.getSetCC(VT, ORNode, LR, Op1);
1501 }
1502 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
1503 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
1504 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
1505 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
1506 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001507 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001508 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1509 }
1510 }
1511 // canonicalize equivalent to ll == rl
1512 if (LL == RR && LR == RL) {
1513 Op1 = ISD::getSetCCSwappedOperands(Op1);
1514 std::swap(RL, RR);
1515 }
1516 if (LL == RL && LR == RR) {
1517 bool isInteger = MVT::isInteger(LL.getValueType());
1518 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
1519 if (Result != ISD::SETCC_INVALID)
1520 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1521 }
1522 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001523
1524 // Simplify: or (op x...), (op y...) -> (op (or x, y))
1525 if (N0.getOpcode() == N1.getOpcode()) {
1526 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1527 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001528 }
Chris Lattner516b9622006-09-14 20:50:57 +00001529
Chris Lattner1ec72732006-09-14 21:11:37 +00001530 // (X & C1) | (Y & C2) -> (X|Y) & C3 if possible.
1531 if (N0.getOpcode() == ISD::AND &&
1532 N1.getOpcode() == ISD::AND &&
1533 N0.getOperand(1).getOpcode() == ISD::Constant &&
1534 N1.getOperand(1).getOpcode() == ISD::Constant &&
1535 // Don't increase # computations.
1536 (N0.Val->hasOneUse() || N1.Val->hasOneUse())) {
1537 // We can only do this xform if we know that bits from X that are set in C2
1538 // but not in C1 are already zero. Likewise for Y.
1539 uint64_t LHSMask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1540 uint64_t RHSMask = cast<ConstantSDNode>(N1.getOperand(1))->getValue();
1541
1542 if (TLI.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) &&
1543 TLI.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) {
1544 SDOperand X =DAG.getNode(ISD::OR, VT, N0.getOperand(0), N1.getOperand(0));
1545 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(LHSMask|RHSMask, VT));
1546 }
1547 }
1548
1549
Chris Lattner516b9622006-09-14 20:50:57 +00001550 // See if this is some rotate idiom.
1551 if (SDNode *Rot = MatchRotate(N0, N1))
1552 return SDOperand(Rot, 0);
Chris Lattner35e5c142006-05-05 05:51:50 +00001553
Nate Begeman83e75ec2005-09-06 04:43:02 +00001554 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001555}
1556
Chris Lattner516b9622006-09-14 20:50:57 +00001557
1558/// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present.
1559static bool MatchRotateHalf(SDOperand Op, SDOperand &Shift, SDOperand &Mask) {
1560 if (Op.getOpcode() == ISD::AND) {
Reid Spencer3ed469c2006-11-02 20:25:50 +00001561 if (isa<ConstantSDNode>(Op.getOperand(1))) {
Chris Lattner516b9622006-09-14 20:50:57 +00001562 Mask = Op.getOperand(1);
1563 Op = Op.getOperand(0);
1564 } else {
1565 return false;
1566 }
1567 }
1568
1569 if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) {
1570 Shift = Op;
1571 return true;
1572 }
1573 return false;
1574}
1575
1576
1577// MatchRotate - Handle an 'or' of two operands. If this is one of the many
1578// idioms for rotate, and if the target supports rotation instructions, generate
1579// a rot[lr].
1580SDNode *DAGCombiner::MatchRotate(SDOperand LHS, SDOperand RHS) {
1581 // Must be a legal type. Expanded an promoted things won't work with rotates.
1582 MVT::ValueType VT = LHS.getValueType();
1583 if (!TLI.isTypeLegal(VT)) return 0;
1584
1585 // The target must have at least one rotate flavor.
1586 bool HasROTL = TLI.isOperationLegal(ISD::ROTL, VT);
1587 bool HasROTR = TLI.isOperationLegal(ISD::ROTR, VT);
1588 if (!HasROTL && !HasROTR) return 0;
1589
1590 // Match "(X shl/srl V1) & V2" where V2 may not be present.
1591 SDOperand LHSShift; // The shift.
1592 SDOperand LHSMask; // AND value if any.
1593 if (!MatchRotateHalf(LHS, LHSShift, LHSMask))
1594 return 0; // Not part of a rotate.
1595
1596 SDOperand RHSShift; // The shift.
1597 SDOperand RHSMask; // AND value if any.
1598 if (!MatchRotateHalf(RHS, RHSShift, RHSMask))
1599 return 0; // Not part of a rotate.
1600
1601 if (LHSShift.getOperand(0) != RHSShift.getOperand(0))
1602 return 0; // Not shifting the same value.
1603
1604 if (LHSShift.getOpcode() == RHSShift.getOpcode())
1605 return 0; // Shifts must disagree.
1606
1607 // Canonicalize shl to left side in a shl/srl pair.
1608 if (RHSShift.getOpcode() == ISD::SHL) {
1609 std::swap(LHS, RHS);
1610 std::swap(LHSShift, RHSShift);
1611 std::swap(LHSMask , RHSMask );
1612 }
1613
1614 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Scott Michelc9dc1142007-04-02 21:36:32 +00001615 SDOperand LHSShiftArg = LHSShift.getOperand(0);
1616 SDOperand LHSShiftAmt = LHSShift.getOperand(1);
1617 SDOperand RHSShiftAmt = RHSShift.getOperand(1);
Chris Lattner516b9622006-09-14 20:50:57 +00001618
1619 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
1620 // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2)
Scott Michelc9dc1142007-04-02 21:36:32 +00001621 if (LHSShiftAmt.getOpcode() == ISD::Constant &&
1622 RHSShiftAmt.getOpcode() == ISD::Constant) {
1623 uint64_t LShVal = cast<ConstantSDNode>(LHSShiftAmt)->getValue();
1624 uint64_t RShVal = cast<ConstantSDNode>(RHSShiftAmt)->getValue();
Chris Lattner516b9622006-09-14 20:50:57 +00001625 if ((LShVal + RShVal) != OpSizeInBits)
1626 return 0;
1627
1628 SDOperand Rot;
1629 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00001630 Rot = DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00001631 else
Scott Michelc9dc1142007-04-02 21:36:32 +00001632 Rot = DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00001633
1634 // If there is an AND of either shifted operand, apply it to the result.
1635 if (LHSMask.Val || RHSMask.Val) {
1636 uint64_t Mask = MVT::getIntVTBitMask(VT);
1637
1638 if (LHSMask.Val) {
1639 uint64_t RHSBits = (1ULL << LShVal)-1;
1640 Mask &= cast<ConstantSDNode>(LHSMask)->getValue() | RHSBits;
1641 }
1642 if (RHSMask.Val) {
1643 uint64_t LHSBits = ~((1ULL << (OpSizeInBits-RShVal))-1);
1644 Mask &= cast<ConstantSDNode>(RHSMask)->getValue() | LHSBits;
1645 }
1646
1647 Rot = DAG.getNode(ISD::AND, VT, Rot, DAG.getConstant(Mask, VT));
1648 }
1649
1650 return Rot.Val;
1651 }
1652
1653 // If there is a mask here, and we have a variable shift, we can't be sure
1654 // that we're masking out the right stuff.
1655 if (LHSMask.Val || RHSMask.Val)
1656 return 0;
1657
1658 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
1659 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotr x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00001660 if (RHSShiftAmt.getOpcode() == ISD::SUB &&
1661 LHSShiftAmt == RHSShiftAmt.getOperand(1)) {
Chris Lattner516b9622006-09-14 20:50:57 +00001662 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00001663 dyn_cast<ConstantSDNode>(RHSShiftAmt.getOperand(0))) {
Chris Lattner516b9622006-09-14 20:50:57 +00001664 if (SUBC->getValue() == OpSizeInBits)
1665 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00001666 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
Chris Lattner516b9622006-09-14 20:50:57 +00001667 else
Scott Michelc9dc1142007-04-02 21:36:32 +00001668 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).Val;
Chris Lattner516b9622006-09-14 20:50:57 +00001669 }
1670 }
1671
1672 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
1673 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotl x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00001674 if (LHSShiftAmt.getOpcode() == ISD::SUB &&
1675 RHSShiftAmt == LHSShiftAmt.getOperand(1)) {
Chris Lattner516b9622006-09-14 20:50:57 +00001676 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00001677 dyn_cast<ConstantSDNode>(LHSShiftAmt.getOperand(0))) {
Chris Lattner516b9622006-09-14 20:50:57 +00001678 if (SUBC->getValue() == OpSizeInBits)
1679 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00001680 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
Chris Lattner516b9622006-09-14 20:50:57 +00001681 else
Scott Michelc9dc1142007-04-02 21:36:32 +00001682 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).Val;
1683 }
1684 }
1685
1686 // Look for sign/zext/any-extended cases:
1687 if ((LHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
1688 || LHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
1689 || LHSShiftAmt.getOpcode() == ISD::ANY_EXTEND) &&
1690 (RHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
1691 || RHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
1692 || RHSShiftAmt.getOpcode() == ISD::ANY_EXTEND)) {
1693 SDOperand LExtOp0 = LHSShiftAmt.getOperand(0);
1694 SDOperand RExtOp0 = RHSShiftAmt.getOperand(0);
1695 if (RExtOp0.getOpcode() == ISD::SUB &&
1696 RExtOp0.getOperand(1) == LExtOp0) {
1697 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
1698 // (rotr x, y)
1699 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
1700 // (rotl x, (sub 32, y))
1701 if (ConstantSDNode *SUBC = cast<ConstantSDNode>(RExtOp0.getOperand(0))) {
1702 if (SUBC->getValue() == OpSizeInBits) {
1703 if (HasROTL)
1704 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
1705 else
1706 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).Val;
1707 }
1708 }
1709 } else if (LExtOp0.getOpcode() == ISD::SUB &&
1710 RExtOp0 == LExtOp0.getOperand(1)) {
1711 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext r))) ->
1712 // (rotl x, y)
1713 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext r))) ->
1714 // (rotr x, (sub 32, y))
1715 if (ConstantSDNode *SUBC = cast<ConstantSDNode>(LExtOp0.getOperand(0))) {
1716 if (SUBC->getValue() == OpSizeInBits) {
1717 if (HasROTL)
1718 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, RHSShiftAmt).Val;
1719 else
1720 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
1721 }
1722 }
Chris Lattner516b9622006-09-14 20:50:57 +00001723 }
1724 }
1725
1726 return 0;
1727}
1728
1729
Nate Begeman83e75ec2005-09-06 04:43:02 +00001730SDOperand DAGCombiner::visitXOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001731 SDOperand N0 = N->getOperand(0);
1732 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001733 SDOperand LHS, RHS, CC;
1734 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1735 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001736 MVT::ValueType VT = N0.getValueType();
1737
1738 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001739 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001740 return DAG.getNode(ISD::XOR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001741 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001742 if (N0C && !N1C)
1743 return DAG.getNode(ISD::XOR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001744 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001745 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001746 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001747 // reassociate xor
1748 SDOperand RXOR = ReassociateOps(ISD::XOR, N0, N1);
1749 if (RXOR.Val != 0)
1750 return RXOR;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001751 // fold !(x cc y) -> (x !cc y)
Nate Begeman646d7e22005-09-02 21:18:40 +00001752 if (N1C && N1C->getValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
1753 bool isInt = MVT::isInteger(LHS.getValueType());
1754 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
1755 isInt);
1756 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001757 return DAG.getSetCC(VT, LHS, RHS, NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00001758 if (N0.getOpcode() == ISD::SELECT_CC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001759 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00001760 assert(0 && "Unhandled SetCC Equivalent!");
1761 abort();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001762 }
Nate Begeman99801192005-09-07 23:25:52 +00001763 // fold !(x or y) -> (!x and !y) iff x or y are setcc
Chris Lattner734c91d2006-11-10 21:37:15 +00001764 if (N1C && N1C->getValue() == 1 && VT == MVT::i1 &&
Nate Begeman99801192005-09-07 23:25:52 +00001765 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001766 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00001767 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
1768 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001769 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1770 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00001771 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00001772 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001773 }
1774 }
Nate Begeman99801192005-09-07 23:25:52 +00001775 // fold !(x or y) -> (!x and !y) iff x or y are constants
1776 if (N1C && N1C->isAllOnesValue() &&
1777 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001778 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00001779 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
1780 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001781 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1782 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00001783 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00001784 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001785 }
1786 }
Nate Begeman223df222005-09-08 20:18:10 +00001787 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
1788 if (N1C && N0.getOpcode() == ISD::XOR) {
1789 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
1790 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
1791 if (N00C)
1792 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
1793 DAG.getConstant(N1C->getValue()^N00C->getValue(), VT));
1794 if (N01C)
1795 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
1796 DAG.getConstant(N1C->getValue()^N01C->getValue(), VT));
1797 }
1798 // fold (xor x, x) -> 0
Chris Lattner4fbdd592006-03-28 19:11:05 +00001799 if (N0 == N1) {
1800 if (!MVT::isVector(VT)) {
1801 return DAG.getConstant(0, VT);
1802 } else if (!AfterLegalize || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) {
1803 // Produce a vector of zeros.
1804 SDOperand El = DAG.getConstant(0, MVT::getVectorBaseType(VT));
1805 std::vector<SDOperand> Ops(MVT::getVectorNumElements(VT), El);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001806 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4fbdd592006-03-28 19:11:05 +00001807 }
1808 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001809
1810 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
1811 if (N0.getOpcode() == N1.getOpcode()) {
1812 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1813 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001814 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001815
Chris Lattner3e104b12006-04-08 04:15:24 +00001816 // Simplify the expression using non-local knowledge.
1817 if (!MVT::isVector(VT) &&
1818 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001819 return SDOperand(N, 0);
Chris Lattner3e104b12006-04-08 04:15:24 +00001820
Nate Begeman83e75ec2005-09-06 04:43:02 +00001821 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001822}
1823
Nate Begeman83e75ec2005-09-06 04:43:02 +00001824SDOperand DAGCombiner::visitSHL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001825 SDOperand N0 = N->getOperand(0);
1826 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001827 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1828 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001829 MVT::ValueType VT = N0.getValueType();
1830 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1831
1832 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001833 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001834 return DAG.getNode(ISD::SHL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001835 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001836 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001837 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001838 // fold (shl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001839 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001840 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001841 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001842 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001843 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001844 // if (shl x, c) is known to be zero, return 0
Nate Begemanfb7217b2006-02-17 19:54:08 +00001845 if (TLI.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001846 return DAG.getConstant(0, VT);
Chris Lattner61a4c072007-04-18 03:06:49 +00001847 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001848 return SDOperand(N, 0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001849 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001850 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001851 N0.getOperand(1).getOpcode() == ISD::Constant) {
1852 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001853 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001854 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001855 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001856 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001857 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001858 }
1859 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
1860 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001861 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001862 N0.getOperand(1).getOpcode() == ISD::Constant) {
1863 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001864 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001865 SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
1866 DAG.getConstant(~0ULL << c1, VT));
1867 if (c2 > c1)
1868 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001869 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001870 else
Nate Begeman83e75ec2005-09-06 04:43:02 +00001871 return DAG.getNode(ISD::SRL, VT, Mask,
1872 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001873 }
1874 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001875 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +00001876 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001877 DAG.getConstant(~0ULL << N1C->getValue(), VT));
1878 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001879}
1880
Nate Begeman83e75ec2005-09-06 04:43:02 +00001881SDOperand DAGCombiner::visitSRA(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001882 SDOperand N0 = N->getOperand(0);
1883 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001884 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1885 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001886 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001887
1888 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001889 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001890 return DAG.getNode(ISD::SRA, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001891 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001892 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001893 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001894 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001895 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001896 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001897 // fold (sra x, c >= size(x)) -> undef
Nate Begemanfb7217b2006-02-17 19:54:08 +00001898 if (N1C && N1C->getValue() >= MVT::getSizeInBits(VT))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001899 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001900 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001901 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001902 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00001903 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
1904 // sext_inreg.
1905 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
1906 unsigned LowBits = MVT::getSizeInBits(VT) - (unsigned)N1C->getValue();
1907 MVT::ValueType EVT;
1908 switch (LowBits) {
1909 default: EVT = MVT::Other; break;
1910 case 1: EVT = MVT::i1; break;
1911 case 8: EVT = MVT::i8; break;
1912 case 16: EVT = MVT::i16; break;
1913 case 32: EVT = MVT::i32; break;
1914 }
1915 if (EVT > MVT::Other && TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, EVT))
1916 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
1917 DAG.getValueType(EVT));
1918 }
Chris Lattner71d9ebc2006-02-28 06:23:04 +00001919
1920 // fold (sra (sra x, c1), c2) -> (sra x, c1+c2)
1921 if (N1C && N0.getOpcode() == ISD::SRA) {
1922 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
1923 unsigned Sum = N1C->getValue() + C1->getValue();
1924 if (Sum >= MVT::getSizeInBits(VT)) Sum = MVT::getSizeInBits(VT)-1;
1925 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0),
1926 DAG.getConstant(Sum, N1C->getValueType(0)));
1927 }
1928 }
1929
Chris Lattnera8504462006-05-08 20:51:54 +00001930 // Simplify, based on bits shifted out of the LHS.
1931 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
1932 return SDOperand(N, 0);
1933
1934
Nate Begeman1d4d4142005-09-01 00:19:25 +00001935 // If the sign bit is known to be zero, switch this to a SRL.
Nate Begemanfb7217b2006-02-17 19:54:08 +00001936 if (TLI.MaskedValueIsZero(N0, MVT::getIntVTSignBit(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001937 return DAG.getNode(ISD::SRL, VT, N0, N1);
1938 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001939}
1940
Nate Begeman83e75ec2005-09-06 04:43:02 +00001941SDOperand DAGCombiner::visitSRL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001942 SDOperand N0 = N->getOperand(0);
1943 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001944 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1945 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001946 MVT::ValueType VT = N0.getValueType();
1947 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1948
1949 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001950 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001951 return DAG.getNode(ISD::SRL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001952 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001953 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001954 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001955 // fold (srl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001956 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001957 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001958 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001959 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001960 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001961 // if (srl x, c) is known to be zero, return 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001962 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001963 return DAG.getConstant(0, VT);
Chris Lattnerec06e9a2007-04-18 03:05:22 +00001964
Nate Begeman1d4d4142005-09-01 00:19:25 +00001965 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001966 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001967 N0.getOperand(1).getOpcode() == ISD::Constant) {
1968 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001969 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001970 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001971 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001972 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001973 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001974 }
Chris Lattner350bec02006-04-02 06:11:11 +00001975
Chris Lattner06afe072006-05-05 22:53:17 +00001976 // fold (srl (anyextend x), c) -> (anyextend (srl x, c))
1977 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
1978 // Shifting in all undef bits?
1979 MVT::ValueType SmallVT = N0.getOperand(0).getValueType();
1980 if (N1C->getValue() >= MVT::getSizeInBits(SmallVT))
1981 return DAG.getNode(ISD::UNDEF, VT);
1982
1983 SDOperand SmallShift = DAG.getNode(ISD::SRL, SmallVT, N0.getOperand(0), N1);
1984 AddToWorkList(SmallShift.Val);
1985 return DAG.getNode(ISD::ANY_EXTEND, VT, SmallShift);
1986 }
1987
Chris Lattner3657ffe2006-10-12 20:23:19 +00001988 // fold (srl (sra X, Y), 31) -> (srl X, 31). This srl only looks at the sign
1989 // bit, which is unmodified by sra.
1990 if (N1C && N1C->getValue()+1 == MVT::getSizeInBits(VT)) {
1991 if (N0.getOpcode() == ISD::SRA)
1992 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0), N1);
1993 }
1994
Chris Lattner350bec02006-04-02 06:11:11 +00001995 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
1996 if (N1C && N0.getOpcode() == ISD::CTLZ &&
1997 N1C->getValue() == Log2_32(MVT::getSizeInBits(VT))) {
1998 uint64_t KnownZero, KnownOne, Mask = MVT::getIntVTBitMask(VT);
1999 TLI.ComputeMaskedBits(N0.getOperand(0), Mask, KnownZero, KnownOne);
2000
2001 // If any of the input bits are KnownOne, then the input couldn't be all
2002 // zeros, thus the result of the srl will always be zero.
2003 if (KnownOne) return DAG.getConstant(0, VT);
2004
2005 // If all of the bits input the to ctlz node are known to be zero, then
2006 // the result of the ctlz is "32" and the result of the shift is one.
2007 uint64_t UnknownBits = ~KnownZero & Mask;
2008 if (UnknownBits == 0) return DAG.getConstant(1, VT);
2009
2010 // Otherwise, check to see if there is exactly one bit input to the ctlz.
2011 if ((UnknownBits & (UnknownBits-1)) == 0) {
2012 // Okay, we know that only that the single bit specified by UnknownBits
2013 // could be set on input to the CTLZ node. If this bit is set, the SRL
2014 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
2015 // to an SRL,XOR pair, which is likely to simplify more.
2016 unsigned ShAmt = CountTrailingZeros_64(UnknownBits);
2017 SDOperand Op = N0.getOperand(0);
2018 if (ShAmt) {
2019 Op = DAG.getNode(ISD::SRL, VT, Op,
2020 DAG.getConstant(ShAmt, TLI.getShiftAmountTy()));
2021 AddToWorkList(Op.Val);
2022 }
2023 return DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(1, VT));
2024 }
2025 }
Chris Lattner61a4c072007-04-18 03:06:49 +00002026
2027 // fold operands of srl based on knowledge that the low bits are not
2028 // demanded.
2029 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
2030 return SDOperand(N, 0);
2031
Nate Begeman83e75ec2005-09-06 04:43:02 +00002032 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002033}
2034
Nate Begeman83e75ec2005-09-06 04:43:02 +00002035SDOperand DAGCombiner::visitCTLZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002036 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00002037 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002038
2039 // fold (ctlz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002040 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002041 return DAG.getNode(ISD::CTLZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002042 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002043}
2044
Nate Begeman83e75ec2005-09-06 04:43:02 +00002045SDOperand DAGCombiner::visitCTTZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002046 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00002047 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002048
2049 // fold (cttz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002050 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002051 return DAG.getNode(ISD::CTTZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002052 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002053}
2054
Nate Begeman83e75ec2005-09-06 04:43:02 +00002055SDOperand DAGCombiner::visitCTPOP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002056 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00002057 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002058
2059 // fold (ctpop c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002060 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002061 return DAG.getNode(ISD::CTPOP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002062 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002063}
2064
Nate Begeman452d7be2005-09-16 00:54:12 +00002065SDOperand DAGCombiner::visitSELECT(SDNode *N) {
2066 SDOperand N0 = N->getOperand(0);
2067 SDOperand N1 = N->getOperand(1);
2068 SDOperand N2 = N->getOperand(2);
2069 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2070 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2071 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
2072 MVT::ValueType VT = N->getValueType(0);
Nate Begeman44728a72005-09-19 22:34:01 +00002073
Nate Begeman452d7be2005-09-16 00:54:12 +00002074 // fold select C, X, X -> X
2075 if (N1 == N2)
2076 return N1;
2077 // fold select true, X, Y -> X
2078 if (N0C && !N0C->isNullValue())
2079 return N1;
2080 // fold select false, X, Y -> Y
2081 if (N0C && N0C->isNullValue())
2082 return N2;
2083 // fold select C, 1, X -> C | X
Nate Begeman44728a72005-09-19 22:34:01 +00002084 if (MVT::i1 == VT && N1C && N1C->getValue() == 1)
Nate Begeman452d7be2005-09-16 00:54:12 +00002085 return DAG.getNode(ISD::OR, VT, N0, N2);
2086 // fold select C, 0, X -> ~C & X
2087 // FIXME: this should check for C type == X type, not i1?
2088 if (MVT::i1 == VT && N1C && N1C->isNullValue()) {
2089 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00002090 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00002091 return DAG.getNode(ISD::AND, VT, XORNode, N2);
2092 }
2093 // fold select C, X, 1 -> ~C | X
Nate Begeman44728a72005-09-19 22:34:01 +00002094 if (MVT::i1 == VT && N2C && N2C->getValue() == 1) {
Nate Begeman452d7be2005-09-16 00:54:12 +00002095 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::OR, VT, XORNode, N1);
2098 }
2099 // fold select C, X, 0 -> C & X
2100 // FIXME: this should check for C type == X type, not i1?
2101 if (MVT::i1 == VT && N2C && N2C->isNullValue())
2102 return DAG.getNode(ISD::AND, VT, N0, N1);
2103 // fold X ? X : Y --> X ? 1 : Y --> X | Y
2104 if (MVT::i1 == VT && N0 == N1)
2105 return DAG.getNode(ISD::OR, VT, N0, N2);
2106 // fold X ? Y : X --> X ? Y : 0 --> X & Y
2107 if (MVT::i1 == VT && N0 == N2)
2108 return DAG.getNode(ISD::AND, VT, N0, N1);
Chris Lattner729c6d12006-05-27 00:43:02 +00002109
Chris Lattner40c62d52005-10-18 06:04:22 +00002110 // If we can fold this based on the true/false value, do so.
2111 if (SimplifySelectOps(N, N1, N2))
Chris Lattner729c6d12006-05-27 00:43:02 +00002112 return SDOperand(N, 0); // Don't revisit N.
2113
Nate Begeman44728a72005-09-19 22:34:01 +00002114 // fold selects based on a setcc into other things, such as min/max/abs
2115 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman750ac1b2006-02-01 07:19:44 +00002116 // FIXME:
2117 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
2118 // having to say they don't support SELECT_CC on every type the DAG knows
2119 // about, since there is no way to mark an opcode illegal at all value types
2120 if (TLI.isOperationLegal(ISD::SELECT_CC, MVT::Other))
2121 return DAG.getNode(ISD::SELECT_CC, VT, N0.getOperand(0), N0.getOperand(1),
2122 N1, N2, N0.getOperand(2));
2123 else
2124 return SimplifySelect(N0, N1, N2);
Nate Begeman452d7be2005-09-16 00:54:12 +00002125 return SDOperand();
2126}
2127
2128SDOperand DAGCombiner::visitSELECT_CC(SDNode *N) {
Nate Begeman44728a72005-09-19 22:34:01 +00002129 SDOperand N0 = N->getOperand(0);
2130 SDOperand N1 = N->getOperand(1);
2131 SDOperand N2 = N->getOperand(2);
2132 SDOperand N3 = N->getOperand(3);
2133 SDOperand N4 = N->getOperand(4);
Nate Begeman44728a72005-09-19 22:34:01 +00002134 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
2135
Nate Begeman44728a72005-09-19 22:34:01 +00002136 // fold select_cc lhs, rhs, x, x, cc -> x
2137 if (N2 == N3)
2138 return N2;
Chris Lattner40c62d52005-10-18 06:04:22 +00002139
Chris Lattner5f42a242006-09-20 06:19:26 +00002140 // Determine if the condition we're dealing with is constant
2141 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
Chris Lattner30f73e72006-10-14 03:52:46 +00002142 if (SCC.Val) AddToWorkList(SCC.Val);
Chris Lattner5f42a242006-09-20 06:19:26 +00002143
2144 if (ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val)) {
2145 if (SCCC->getValue())
2146 return N2; // cond always true -> true val
2147 else
2148 return N3; // cond always false -> false val
2149 }
2150
2151 // Fold to a simpler select_cc
2152 if (SCC.Val && SCC.getOpcode() == ISD::SETCC)
2153 return DAG.getNode(ISD::SELECT_CC, N2.getValueType(),
2154 SCC.getOperand(0), SCC.getOperand(1), N2, N3,
2155 SCC.getOperand(2));
2156
Chris Lattner40c62d52005-10-18 06:04:22 +00002157 // If we can fold this based on the true/false value, do so.
2158 if (SimplifySelectOps(N, N2, N3))
Chris Lattner729c6d12006-05-27 00:43:02 +00002159 return SDOperand(N, 0); // Don't revisit N.
Chris Lattner40c62d52005-10-18 06:04:22 +00002160
Nate Begeman44728a72005-09-19 22:34:01 +00002161 // fold select_cc into other things, such as min/max/abs
2162 return SimplifySelectCC(N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00002163}
2164
2165SDOperand DAGCombiner::visitSETCC(SDNode *N) {
2166 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
2167 cast<CondCodeSDNode>(N->getOperand(2))->get());
2168}
2169
Nate Begeman83e75ec2005-09-06 04:43:02 +00002170SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002171 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002172 MVT::ValueType VT = N->getValueType(0);
2173
Nate Begeman1d4d4142005-09-01 00:19:25 +00002174 // fold (sext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00002175 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002176 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0);
Chris Lattner310b5782006-05-06 23:06:26 +00002177
Nate Begeman1d4d4142005-09-01 00:19:25 +00002178 // fold (sext (sext x)) -> (sext x)
Chris Lattner310b5782006-05-06 23:06:26 +00002179 // fold (sext (aext x)) -> (sext x)
2180 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002181 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
Chris Lattner310b5782006-05-06 23:06:26 +00002182
Evan Chengc88138f2007-03-22 01:54:19 +00002183 // fold (sext (truncate (load x))) -> (sext (smaller load x))
2184 // fold (sext (truncate (srl (load x), c))) -> (sext (smaller load (x+c/n)))
Chris Lattner22558872007-02-26 03:13:59 +00002185 if (N0.getOpcode() == ISD::TRUNCATE) {
Evan Chengc88138f2007-03-22 01:54:19 +00002186 SDOperand NarrowLoad = ReduceLoadWidth(N0.Val);
Evan Cheng0b063de2007-03-23 02:16:52 +00002187 if (NarrowLoad.Val) {
2188 if (NarrowLoad.Val != N0.Val)
2189 CombineTo(N0.Val, NarrowLoad);
2190 return DAG.getNode(ISD::SIGN_EXTEND, VT, NarrowLoad);
2191 }
Evan Chengc88138f2007-03-22 01:54:19 +00002192 }
2193
2194 // See if the value being truncated is already sign extended. If so, just
2195 // eliminate the trunc/sext pair.
2196 if (N0.getOpcode() == ISD::TRUNCATE) {
Chris Lattner6007b842006-09-21 06:00:20 +00002197 SDOperand Op = N0.getOperand(0);
Chris Lattner22558872007-02-26 03:13:59 +00002198 unsigned OpBits = MVT::getSizeInBits(Op.getValueType());
2199 unsigned MidBits = MVT::getSizeInBits(N0.getValueType());
2200 unsigned DestBits = MVT::getSizeInBits(VT);
2201 unsigned NumSignBits = TLI.ComputeNumSignBits(Op);
2202
2203 if (OpBits == DestBits) {
2204 // Op is i32, Mid is i8, and Dest is i32. If Op has more than 24 sign
2205 // bits, it is already ready.
2206 if (NumSignBits > DestBits-MidBits)
2207 return Op;
2208 } else if (OpBits < DestBits) {
2209 // Op is i32, Mid is i8, and Dest is i64. If Op has more than 24 sign
2210 // bits, just sext from i32.
2211 if (NumSignBits > OpBits-MidBits)
2212 return DAG.getNode(ISD::SIGN_EXTEND, VT, Op);
2213 } else {
2214 // Op is i64, Mid is i8, and Dest is i32. If Op has more than 56 sign
2215 // bits, just truncate to i32.
2216 if (NumSignBits > OpBits-MidBits)
2217 return DAG.getNode(ISD::TRUNCATE, VT, Op);
Chris Lattner6007b842006-09-21 06:00:20 +00002218 }
Chris Lattner22558872007-02-26 03:13:59 +00002219
2220 // fold (sext (truncate x)) -> (sextinreg x).
2221 if (!AfterLegalize || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG,
2222 N0.getValueType())) {
2223 if (Op.getValueType() < VT)
2224 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
2225 else if (Op.getValueType() > VT)
2226 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
2227 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, Op,
2228 DAG.getValueType(N0.getValueType()));
2229 }
Chris Lattner6007b842006-09-21 06:00:20 +00002230 }
Chris Lattner310b5782006-05-06 23:06:26 +00002231
Evan Cheng110dec22005-12-14 02:19:23 +00002232 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00002233 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00002234 (!AfterLegalize||TLI.isLoadXLegal(ISD::SEXTLOAD, N0.getValueType()))){
Evan Cheng466685d2006-10-09 20:57:25 +00002235 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2236 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2237 LN0->getBasePtr(), LN0->getSrcValue(),
2238 LN0->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002239 N0.getValueType(),
2240 LN0->isVolatile());
Chris Lattnerd4771842005-12-14 19:25:30 +00002241 CombineTo(N, ExtLoad);
Chris Lattnerf9884052005-10-13 21:52:31 +00002242 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2243 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002244 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begeman3df4d522005-10-12 20:40:40 +00002245 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002246
2247 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
2248 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00002249 if ((ISD::isSEXTLoad(N0.Val) || ISD::isEXTLoad(N0.Val)) &&
2250 ISD::isUNINDEXEDLoad(N0.Val) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002251 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00002252 MVT::ValueType EVT = LN0->getLoadedVT();
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00002253 if (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT)) {
2254 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2255 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002256 LN0->getSrcValueOffset(), EVT,
2257 LN0->isVolatile(),
2258 LN0->getAlignment());
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00002259 CombineTo(N, ExtLoad);
2260 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2261 ExtLoad.getValue(1));
2262 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2263 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002264 }
2265
Chris Lattner20a35c32007-04-11 05:32:27 +00002266 // sext(setcc x,y,cc) -> select_cc x, y, -1, 0, cc
2267 if (N0.getOpcode() == ISD::SETCC) {
2268 SDOperand SCC =
Chris Lattner1eba01e2007-04-11 06:50:51 +00002269 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
2270 DAG.getConstant(~0ULL, VT), DAG.getConstant(0, VT),
2271 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
2272 if (SCC.Val) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00002273 }
2274
Nate Begeman83e75ec2005-09-06 04:43:02 +00002275 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002276}
2277
Nate Begeman83e75ec2005-09-06 04:43:02 +00002278SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002279 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002280 MVT::ValueType VT = N->getValueType(0);
2281
Nate Begeman1d4d4142005-09-01 00:19:25 +00002282 // fold (zext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00002283 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002284 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002285 // fold (zext (zext x)) -> (zext x)
Chris Lattner310b5782006-05-06 23:06:26 +00002286 // fold (zext (aext x)) -> (zext x)
2287 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002288 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
Chris Lattner6007b842006-09-21 06:00:20 +00002289
Evan Chengc88138f2007-03-22 01:54:19 +00002290 // fold (zext (truncate (load x))) -> (zext (smaller load x))
2291 // fold (zext (truncate (srl (load x), c))) -> (zext (small load (x+c/n)))
Dale Johannesen2041a0e2007-03-30 21:38:07 +00002292 if (N0.getOpcode() == ISD::TRUNCATE) {
Evan Chengc88138f2007-03-22 01:54:19 +00002293 SDOperand NarrowLoad = ReduceLoadWidth(N0.Val);
Evan Cheng0b063de2007-03-23 02:16:52 +00002294 if (NarrowLoad.Val) {
2295 if (NarrowLoad.Val != N0.Val)
2296 CombineTo(N0.Val, NarrowLoad);
2297 return DAG.getNode(ISD::ZERO_EXTEND, VT, NarrowLoad);
2298 }
Evan Chengc88138f2007-03-22 01:54:19 +00002299 }
2300
Chris Lattner6007b842006-09-21 06:00:20 +00002301 // fold (zext (truncate x)) -> (and x, mask)
2302 if (N0.getOpcode() == ISD::TRUNCATE &&
2303 (!AfterLegalize || TLI.isOperationLegal(ISD::AND, VT))) {
2304 SDOperand Op = N0.getOperand(0);
2305 if (Op.getValueType() < VT) {
2306 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
2307 } else if (Op.getValueType() > VT) {
2308 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
2309 }
2310 return DAG.getZeroExtendInReg(Op, N0.getValueType());
2311 }
2312
Chris Lattner111c2282006-09-21 06:14:31 +00002313 // fold (zext (and (trunc x), cst)) -> (and x, cst).
2314 if (N0.getOpcode() == ISD::AND &&
2315 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
2316 N0.getOperand(1).getOpcode() == ISD::Constant) {
2317 SDOperand X = N0.getOperand(0).getOperand(0);
2318 if (X.getValueType() < VT) {
2319 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
2320 } else if (X.getValueType() > VT) {
2321 X = DAG.getNode(ISD::TRUNCATE, VT, X);
2322 }
2323 uint64_t Mask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
2324 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
2325 }
2326
Evan Cheng110dec22005-12-14 02:19:23 +00002327 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00002328 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00002329 (!AfterLegalize||TLI.isLoadXLegal(ISD::ZEXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00002330 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2331 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
2332 LN0->getBasePtr(), LN0->getSrcValue(),
2333 LN0->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002334 N0.getValueType(),
2335 LN0->isVolatile(),
2336 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00002337 CombineTo(N, ExtLoad);
Evan Cheng110dec22005-12-14 02:19:23 +00002338 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2339 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002340 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng110dec22005-12-14 02:19:23 +00002341 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002342
2343 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
2344 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00002345 if ((ISD::isZEXTLoad(N0.Val) || ISD::isEXTLoad(N0.Val)) &&
2346 ISD::isUNINDEXEDLoad(N0.Val) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002347 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00002348 MVT::ValueType EVT = LN0->getLoadedVT();
Evan Cheng466685d2006-10-09 20:57:25 +00002349 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
2350 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002351 LN0->getSrcValueOffset(), EVT,
2352 LN0->isVolatile(),
2353 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00002354 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002355 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2356 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002357 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002358 }
Chris Lattner20a35c32007-04-11 05:32:27 +00002359
2360 // zext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
2361 if (N0.getOpcode() == ISD::SETCC) {
2362 SDOperand SCC =
2363 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
2364 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattner1eba01e2007-04-11 06:50:51 +00002365 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
2366 if (SCC.Val) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00002367 }
2368
Nate Begeman83e75ec2005-09-06 04:43:02 +00002369 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002370}
2371
Chris Lattner5ffc0662006-05-05 05:58:59 +00002372SDOperand DAGCombiner::visitANY_EXTEND(SDNode *N) {
2373 SDOperand N0 = N->getOperand(0);
Chris Lattner5ffc0662006-05-05 05:58:59 +00002374 MVT::ValueType VT = N->getValueType(0);
2375
2376 // fold (aext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00002377 if (isa<ConstantSDNode>(N0))
Chris Lattner5ffc0662006-05-05 05:58:59 +00002378 return DAG.getNode(ISD::ANY_EXTEND, VT, N0);
2379 // fold (aext (aext x)) -> (aext x)
2380 // fold (aext (zext x)) -> (zext x)
2381 // fold (aext (sext x)) -> (sext x)
2382 if (N0.getOpcode() == ISD::ANY_EXTEND ||
2383 N0.getOpcode() == ISD::ZERO_EXTEND ||
2384 N0.getOpcode() == ISD::SIGN_EXTEND)
2385 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
2386
Evan Chengc88138f2007-03-22 01:54:19 +00002387 // fold (aext (truncate (load x))) -> (aext (smaller load x))
2388 // fold (aext (truncate (srl (load x), c))) -> (aext (small load (x+c/n)))
2389 if (N0.getOpcode() == ISD::TRUNCATE) {
2390 SDOperand NarrowLoad = ReduceLoadWidth(N0.Val);
Evan Cheng0b063de2007-03-23 02:16:52 +00002391 if (NarrowLoad.Val) {
2392 if (NarrowLoad.Val != N0.Val)
2393 CombineTo(N0.Val, NarrowLoad);
2394 return DAG.getNode(ISD::ANY_EXTEND, VT, NarrowLoad);
2395 }
Evan Chengc88138f2007-03-22 01:54:19 +00002396 }
2397
Chris Lattner84750582006-09-20 06:29:17 +00002398 // fold (aext (truncate x))
2399 if (N0.getOpcode() == ISD::TRUNCATE) {
2400 SDOperand TruncOp = N0.getOperand(0);
2401 if (TruncOp.getValueType() == VT)
2402 return TruncOp; // x iff x size == zext size.
2403 if (TruncOp.getValueType() > VT)
2404 return DAG.getNode(ISD::TRUNCATE, VT, TruncOp);
2405 return DAG.getNode(ISD::ANY_EXTEND, VT, TruncOp);
2406 }
Chris Lattner0e4b9222006-09-21 06:40:43 +00002407
2408 // fold (aext (and (trunc x), cst)) -> (and x, cst).
2409 if (N0.getOpcode() == ISD::AND &&
2410 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
2411 N0.getOperand(1).getOpcode() == ISD::Constant) {
2412 SDOperand X = N0.getOperand(0).getOperand(0);
2413 if (X.getValueType() < VT) {
2414 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
2415 } else if (X.getValueType() > VT) {
2416 X = DAG.getNode(ISD::TRUNCATE, VT, X);
2417 }
2418 uint64_t Mask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
2419 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
2420 }
2421
Chris Lattner5ffc0662006-05-05 05:58:59 +00002422 // fold (aext (load x)) -> (aext (truncate (extload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00002423 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00002424 (!AfterLegalize||TLI.isLoadXLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00002425 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2426 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
2427 LN0->getBasePtr(), LN0->getSrcValue(),
2428 LN0->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002429 N0.getValueType(),
2430 LN0->isVolatile(),
2431 LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00002432 CombineTo(N, ExtLoad);
2433 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2434 ExtLoad.getValue(1));
2435 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2436 }
2437
2438 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
2439 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
2440 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00002441 if (N0.getOpcode() == ISD::LOAD &&
2442 !ISD::isNON_EXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val) &&
Evan Cheng466685d2006-10-09 20:57:25 +00002443 N0.hasOneUse()) {
2444 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00002445 MVT::ValueType EVT = LN0->getLoadedVT();
Evan Cheng466685d2006-10-09 20:57:25 +00002446 SDOperand ExtLoad = DAG.getExtLoad(LN0->getExtensionType(), VT,
2447 LN0->getChain(), LN0->getBasePtr(),
2448 LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002449 LN0->getSrcValueOffset(), EVT,
2450 LN0->isVolatile(),
2451 LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00002452 CombineTo(N, ExtLoad);
2453 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2454 ExtLoad.getValue(1));
2455 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2456 }
Chris Lattner20a35c32007-04-11 05:32:27 +00002457
2458 // aext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
2459 if (N0.getOpcode() == ISD::SETCC) {
2460 SDOperand SCC =
Chris Lattner1eba01e2007-04-11 06:50:51 +00002461 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
2462 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattnerc24bbad2007-04-11 16:51:53 +00002463 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Chris Lattner1eba01e2007-04-11 06:50:51 +00002464 if (SCC.Val)
Chris Lattnerc56a81d2007-04-11 06:43:25 +00002465 return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00002466 }
2467
Chris Lattner5ffc0662006-05-05 05:58:59 +00002468 return SDOperand();
2469}
2470
Evan Chengc88138f2007-03-22 01:54:19 +00002471/// ReduceLoadWidth - If the result of a wider load is shifted to right of N
2472/// bits and then truncated to a narrower type and where N is a multiple
2473/// of number of bits of the narrower type, transform it to a narrower load
2474/// from address + N / num of bits of new type. If the result is to be
2475/// extended, also fold the extension to form a extending load.
2476SDOperand DAGCombiner::ReduceLoadWidth(SDNode *N) {
2477 unsigned Opc = N->getOpcode();
2478 ISD::LoadExtType ExtType = ISD::NON_EXTLOAD;
2479 SDOperand N0 = N->getOperand(0);
2480 MVT::ValueType VT = N->getValueType(0);
2481 MVT::ValueType EVT = N->getValueType(0);
2482
Evan Chenge177e302007-03-23 22:13:36 +00002483 // Special case: SIGN_EXTEND_INREG is basically truncating to EVT then
2484 // extended to VT.
Evan Chengc88138f2007-03-22 01:54:19 +00002485 if (Opc == ISD::SIGN_EXTEND_INREG) {
2486 ExtType = ISD::SEXTLOAD;
2487 EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Evan Chenge177e302007-03-23 22:13:36 +00002488 if (AfterLegalize && !TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))
2489 return SDOperand();
Evan Chengc88138f2007-03-22 01:54:19 +00002490 }
2491
2492 unsigned EVTBits = MVT::getSizeInBits(EVT);
2493 unsigned ShAmt = 0;
Evan Chengb37b80c2007-03-23 20:55:21 +00002494 bool CombineSRL = false;
Evan Chengc88138f2007-03-22 01:54:19 +00002495 if (N0.getOpcode() == ISD::SRL && N0.hasOneUse()) {
2496 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2497 ShAmt = N01->getValue();
2498 // Is the shift amount a multiple of size of VT?
2499 if ((ShAmt & (EVTBits-1)) == 0) {
2500 N0 = N0.getOperand(0);
2501 if (MVT::getSizeInBits(N0.getValueType()) <= EVTBits)
2502 return SDOperand();
Evan Chengb37b80c2007-03-23 20:55:21 +00002503 CombineSRL = true;
Evan Chengc88138f2007-03-22 01:54:19 +00002504 }
2505 }
2506 }
2507
2508 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
2509 // Do not allow folding to i1 here. i1 is implicitly stored in memory in
2510 // zero extended form: by shrinking the load, we lose track of the fact
2511 // that it is already zero extended.
2512 // FIXME: This should be reevaluated.
2513 VT != MVT::i1) {
2514 assert(MVT::getSizeInBits(N0.getValueType()) > EVTBits &&
2515 "Cannot truncate to larger type!");
2516 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2517 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
Evan Chengdae54ce2007-03-24 00:02:43 +00002518 // For big endian targets, we need to adjust the offset to the pointer to
2519 // load the correct bytes.
2520 if (!TLI.isLittleEndian())
2521 ShAmt = MVT::getSizeInBits(N0.getValueType()) - ShAmt - EVTBits;
2522 uint64_t PtrOff = ShAmt / 8;
Evan Chengc88138f2007-03-22 01:54:19 +00002523 SDOperand NewPtr = DAG.getNode(ISD::ADD, PtrType, LN0->getBasePtr(),
2524 DAG.getConstant(PtrOff, PtrType));
2525 AddToWorkList(NewPtr.Val);
2526 SDOperand Load = (ExtType == ISD::NON_EXTLOAD)
2527 ? DAG.getLoad(VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002528 LN0->getSrcValue(), LN0->getSrcValueOffset(),
2529 LN0->isVolatile(), LN0->getAlignment())
Evan Chengc88138f2007-03-22 01:54:19 +00002530 : DAG.getExtLoad(ExtType, VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002531 LN0->getSrcValue(), LN0->getSrcValueOffset(), EVT,
2532 LN0->isVolatile(), LN0->getAlignment());
Evan Chengc88138f2007-03-22 01:54:19 +00002533 AddToWorkList(N);
Evan Chengb37b80c2007-03-23 20:55:21 +00002534 if (CombineSRL) {
2535 std::vector<SDNode*> NowDead;
2536 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), Load.getValue(1), NowDead);
2537 CombineTo(N->getOperand(0).Val, Load);
2538 } else
2539 CombineTo(N0.Val, Load, Load.getValue(1));
Evan Cheng15213b72007-03-26 07:12:51 +00002540 if (ShAmt) {
2541 if (Opc == ISD::SIGN_EXTEND_INREG)
2542 return DAG.getNode(Opc, VT, Load, N->getOperand(1));
2543 else
2544 return DAG.getNode(Opc, VT, Load);
2545 }
Evan Chengc88138f2007-03-22 01:54:19 +00002546 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2547 }
2548
2549 return SDOperand();
2550}
2551
Chris Lattner5ffc0662006-05-05 05:58:59 +00002552
Nate Begeman83e75ec2005-09-06 04:43:02 +00002553SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002554 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002555 SDOperand N1 = N->getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002556 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002557 MVT::ValueType EVT = cast<VTSDNode>(N1)->getVT();
Nate Begeman07ed4172005-10-10 21:26:48 +00002558 unsigned EVTBits = MVT::getSizeInBits(EVT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002559
Nate Begeman1d4d4142005-09-01 00:19:25 +00002560 // fold (sext_in_reg c1) -> c1
Chris Lattnereaeda562006-05-08 20:59:41 +00002561 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
Chris Lattner310b5782006-05-06 23:06:26 +00002562 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0, N1);
Chris Lattneree4ea922006-05-06 09:30:03 +00002563
Chris Lattner541a24f2006-05-06 22:43:44 +00002564 // If the input is already sign extended, just drop the extension.
Chris Lattneree4ea922006-05-06 09:30:03 +00002565 if (TLI.ComputeNumSignBits(N0) >= MVT::getSizeInBits(VT)-EVTBits+1)
2566 return N0;
2567
Nate Begeman646d7e22005-09-02 21:18:40 +00002568 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
2569 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
2570 EVT < cast<VTSDNode>(N0.getOperand(1))->getVT()) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00002571 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002572 }
Chris Lattner4b37e872006-05-08 21:18:59 +00002573
Chris Lattner95a5e052007-04-17 19:03:21 +00002574 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is known zero.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00002575 if (TLI.MaskedValueIsZero(N0, 1ULL << (EVTBits-1)))
Nate Begemande996292006-02-03 22:24:05 +00002576 return DAG.getZeroExtendInReg(N0, EVT);
Chris Lattner4b37e872006-05-08 21:18:59 +00002577
Chris Lattner95a5e052007-04-17 19:03:21 +00002578 // fold operands of sext_in_reg based on knowledge that the top bits are not
2579 // demanded.
2580 if (SimplifyDemandedBits(SDOperand(N, 0)))
2581 return SDOperand(N, 0);
2582
Evan Chengc88138f2007-03-22 01:54:19 +00002583 // fold (sext_in_reg (load x)) -> (smaller sextload x)
2584 // fold (sext_in_reg (srl (load x), c)) -> (smaller sextload (x+c/evtbits))
2585 SDOperand NarrowLoad = ReduceLoadWidth(N);
2586 if (NarrowLoad.Val)
2587 return NarrowLoad;
2588
Chris Lattner4b37e872006-05-08 21:18:59 +00002589 // fold (sext_in_reg (srl X, 24), i8) -> sra X, 24
2590 // fold (sext_in_reg (srl X, 23), i8) -> sra X, 23 iff possible.
2591 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
2592 if (N0.getOpcode() == ISD::SRL) {
2593 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
2594 if (ShAmt->getValue()+EVTBits <= MVT::getSizeInBits(VT)) {
2595 // We can turn this into an SRA iff the input to the SRL is already sign
2596 // extended enough.
2597 unsigned InSignBits = TLI.ComputeNumSignBits(N0.getOperand(0));
2598 if (MVT::getSizeInBits(VT)-(ShAmt->getValue()+EVTBits) < InSignBits)
2599 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0), N0.getOperand(1));
2600 }
2601 }
Evan Chengc88138f2007-03-22 01:54:19 +00002602
Nate Begemanded49632005-10-13 03:11:28 +00002603 // fold (sext_inreg (extload x)) -> (sextload x)
Evan Chengc5484282006-10-04 00:56:09 +00002604 if (ISD::isEXTLoad(N0.Val) &&
Evan Cheng83060c52007-03-07 08:07:03 +00002605 ISD::isUNINDEXEDLoad(N0.Val) &&
Evan Cheng2e49f092006-10-11 07:10:22 +00002606 EVT == cast<LoadSDNode>(N0)->getLoadedVT() &&
Evan Chengc5484282006-10-04 00:56:09 +00002607 (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00002608 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2609 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2610 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002611 LN0->getSrcValueOffset(), EVT,
2612 LN0->isVolatile(),
2613 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00002614 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00002615 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002616 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00002617 }
2618 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Evan Cheng83060c52007-03-07 08:07:03 +00002619 if (ISD::isZEXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val) &&
2620 N0.hasOneUse() &&
Evan Cheng2e49f092006-10-11 07:10:22 +00002621 EVT == cast<LoadSDNode>(N0)->getLoadedVT() &&
Evan Chengc5484282006-10-04 00:56:09 +00002622 (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00002623 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2624 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2625 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002626 LN0->getSrcValueOffset(), EVT,
2627 LN0->isVolatile(),
2628 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00002629 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00002630 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002631 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00002632 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00002633 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002634}
2635
Nate Begeman83e75ec2005-09-06 04:43:02 +00002636SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002637 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002638 MVT::ValueType VT = N->getValueType(0);
2639
2640 // noop truncate
2641 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002642 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002643 // fold (truncate c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00002644 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002645 return DAG.getNode(ISD::TRUNCATE, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002646 // fold (truncate (truncate x)) -> (truncate x)
2647 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002648 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002649 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
Chris Lattnerb72773b2006-05-05 22:56:26 +00002650 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND||
2651 N0.getOpcode() == ISD::ANY_EXTEND) {
Chris Lattner32ba1aa2006-11-20 18:05:46 +00002652 if (N0.getOperand(0).getValueType() < VT)
Nate Begeman1d4d4142005-09-01 00:19:25 +00002653 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00002654 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Chris Lattner32ba1aa2006-11-20 18:05:46 +00002655 else if (N0.getOperand(0).getValueType() > VT)
Nate Begeman1d4d4142005-09-01 00:19:25 +00002656 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00002657 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002658 else
2659 // if the source and dest are the same type, we can drop both the extend
2660 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00002661 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002662 }
Evan Cheng007b69e2007-03-21 20:14:05 +00002663
Nate Begeman3df4d522005-10-12 20:40:40 +00002664 // fold (truncate (load x)) -> (smaller load x)
Evan Cheng007b69e2007-03-21 20:14:05 +00002665 // fold (truncate (srl (load x), c)) -> (smaller load (x+c/evtbits))
Evan Chengc88138f2007-03-22 01:54:19 +00002666 return ReduceLoadWidth(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002667}
2668
Chris Lattner94683772005-12-23 05:30:37 +00002669SDOperand DAGCombiner::visitBIT_CONVERT(SDNode *N) {
2670 SDOperand N0 = N->getOperand(0);
2671 MVT::ValueType VT = N->getValueType(0);
2672
2673 // If the input is a constant, let getNode() fold it.
2674 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
2675 SDOperand Res = DAG.getNode(ISD::BIT_CONVERT, VT, N0);
2676 if (Res.Val != N) return Res;
2677 }
2678
Chris Lattnerc8547d82005-12-23 05:37:50 +00002679 if (N0.getOpcode() == ISD::BIT_CONVERT) // conv(conv(x,t1),t2) -> conv(x,t2)
2680 return DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
Chris Lattner6258fb22006-04-02 02:53:43 +00002681
Chris Lattner57104102005-12-23 05:44:41 +00002682 // fold (conv (load x)) -> (load (conv*)x)
Evan Cheng59d5b682007-05-07 21:27:48 +00002683 // If the resultant load doesn't need a higher alignment than the original!
2684 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
2685 TLI.isOperationLegal(ISD::LOAD, VT)) {
Evan Cheng466685d2006-10-09 20:57:25 +00002686 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng59d5b682007-05-07 21:27:48 +00002687 unsigned Align = TLI.getTargetMachine().getTargetData()->
2688 getPrefTypeAlignment(getTypeForValueType(VT));
2689 unsigned OrigAlign = LN0->getAlignment();
2690 if (Align <= OrigAlign) {
2691 SDOperand Load = DAG.getLoad(VT, LN0->getChain(), LN0->getBasePtr(),
2692 LN0->getSrcValue(), LN0->getSrcValueOffset(),
2693 LN0->isVolatile(), LN0->getAlignment());
2694 AddToWorkList(N);
2695 CombineTo(N0.Val, DAG.getNode(ISD::BIT_CONVERT, N0.getValueType(), Load),
2696 Load.getValue(1));
2697 return Load;
2698 }
Chris Lattner57104102005-12-23 05:44:41 +00002699 }
2700
Chris Lattner94683772005-12-23 05:30:37 +00002701 return SDOperand();
2702}
2703
Chris Lattner6258fb22006-04-02 02:53:43 +00002704SDOperand DAGCombiner::visitVBIT_CONVERT(SDNode *N) {
2705 SDOperand N0 = N->getOperand(0);
2706 MVT::ValueType VT = N->getValueType(0);
2707
2708 // If the input is a VBUILD_VECTOR with all constant elements, fold this now.
2709 // First check to see if this is all constant.
2710 if (N0.getOpcode() == ISD::VBUILD_VECTOR && N0.Val->hasOneUse() &&
2711 VT == MVT::Vector) {
2712 bool isSimple = true;
2713 for (unsigned i = 0, e = N0.getNumOperands()-2; i != e; ++i)
2714 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
2715 N0.getOperand(i).getOpcode() != ISD::Constant &&
2716 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
2717 isSimple = false;
2718 break;
2719 }
2720
Chris Lattner97c20732006-04-03 17:29:28 +00002721 MVT::ValueType DestEltVT = cast<VTSDNode>(N->getOperand(2))->getVT();
2722 if (isSimple && !MVT::isVector(DestEltVT)) {
Chris Lattner6258fb22006-04-02 02:53:43 +00002723 return ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(N0.Val, DestEltVT);
2724 }
2725 }
2726
2727 return SDOperand();
2728}
2729
2730/// ConstantFoldVBIT_CONVERTofVBUILD_VECTOR - We know that BV is a vbuild_vector
2731/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
2732/// destination element value type.
2733SDOperand DAGCombiner::
2734ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(SDNode *BV, MVT::ValueType DstEltVT) {
2735 MVT::ValueType SrcEltVT = BV->getOperand(0).getValueType();
2736
2737 // If this is already the right type, we're done.
2738 if (SrcEltVT == DstEltVT) return SDOperand(BV, 0);
2739
2740 unsigned SrcBitSize = MVT::getSizeInBits(SrcEltVT);
2741 unsigned DstBitSize = MVT::getSizeInBits(DstEltVT);
2742
2743 // If this is a conversion of N elements of one type to N elements of another
2744 // type, convert each element. This handles FP<->INT cases.
2745 if (SrcBitSize == DstBitSize) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002746 SmallVector<SDOperand, 8> Ops;
Chris Lattner3e104b12006-04-08 04:15:24 +00002747 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00002748 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, DstEltVT, BV->getOperand(i)));
Chris Lattner3e104b12006-04-08 04:15:24 +00002749 AddToWorkList(Ops.back().Val);
2750 }
Chris Lattner6258fb22006-04-02 02:53:43 +00002751 Ops.push_back(*(BV->op_end()-2)); // Add num elements.
2752 Ops.push_back(DAG.getValueType(DstEltVT));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002753 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00002754 }
2755
2756 // Otherwise, we're growing or shrinking the elements. To avoid having to
2757 // handle annoying details of growing/shrinking FP values, we convert them to
2758 // int first.
2759 if (MVT::isFloatingPoint(SrcEltVT)) {
2760 // Convert the input float vector to a int vector where the elements are the
2761 // same sizes.
2762 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
2763 MVT::ValueType IntVT = SrcEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
2764 BV = ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(BV, IntVT).Val;
2765 SrcEltVT = IntVT;
2766 }
2767
2768 // Now we know the input is an integer vector. If the output is a FP type,
2769 // convert to integer first, then to FP of the right size.
2770 if (MVT::isFloatingPoint(DstEltVT)) {
2771 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
2772 MVT::ValueType TmpVT = DstEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
2773 SDNode *Tmp = ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(BV, TmpVT).Val;
2774
2775 // Next, convert to FP elements of the same size.
2776 return ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(Tmp, DstEltVT);
2777 }
2778
2779 // Okay, we know the src/dst types are both integers of differing types.
2780 // Handling growing first.
2781 assert(MVT::isInteger(SrcEltVT) && MVT::isInteger(DstEltVT));
2782 if (SrcBitSize < DstBitSize) {
2783 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
2784
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002785 SmallVector<SDOperand, 8> Ops;
Chris Lattner6258fb22006-04-02 02:53:43 +00002786 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e;
2787 i += NumInputsPerOutput) {
2788 bool isLE = TLI.isLittleEndian();
2789 uint64_t NewBits = 0;
2790 bool EltIsUndef = true;
2791 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
2792 // Shift the previously computed bits over.
2793 NewBits <<= SrcBitSize;
2794 SDOperand Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
2795 if (Op.getOpcode() == ISD::UNDEF) continue;
2796 EltIsUndef = false;
2797
2798 NewBits |= cast<ConstantSDNode>(Op)->getValue();
2799 }
2800
2801 if (EltIsUndef)
2802 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
2803 else
2804 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
2805 }
2806
2807 Ops.push_back(DAG.getConstant(Ops.size(), MVT::i32)); // Add num elements.
2808 Ops.push_back(DAG.getValueType(DstEltVT)); // Add element size.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002809 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00002810 }
2811
2812 // Finally, this must be the case where we are shrinking elements: each input
2813 // turns into multiple outputs.
2814 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002815 SmallVector<SDOperand, 8> Ops;
Chris Lattner6258fb22006-04-02 02:53:43 +00002816 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e; ++i) {
2817 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
2818 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
2819 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
2820 continue;
2821 }
2822 uint64_t OpVal = cast<ConstantSDNode>(BV->getOperand(i))->getValue();
2823
2824 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
2825 unsigned ThisVal = OpVal & ((1ULL << DstBitSize)-1);
2826 OpVal >>= DstBitSize;
2827 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
2828 }
2829
2830 // For big endian targets, swap the order of the pieces of each element.
2831 if (!TLI.isLittleEndian())
2832 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
2833 }
2834 Ops.push_back(DAG.getConstant(Ops.size(), MVT::i32)); // Add num elements.
2835 Ops.push_back(DAG.getValueType(DstEltVT)); // Add element size.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002836 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00002837}
2838
2839
2840
Chris Lattner01b3d732005-09-28 22:28:18 +00002841SDOperand DAGCombiner::visitFADD(SDNode *N) {
2842 SDOperand N0 = N->getOperand(0);
2843 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00002844 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2845 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002846 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00002847
2848 // fold (fadd c1, c2) -> c1+c2
2849 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002850 return DAG.getNode(ISD::FADD, VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00002851 // canonicalize constant to RHS
2852 if (N0CFP && !N1CFP)
2853 return DAG.getNode(ISD::FADD, VT, N1, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00002854 // fold (A + (-B)) -> A-B
Chris Lattner29446522007-05-14 22:04:50 +00002855 if (isNegatibleForFree(N1) == 2)
2856 return DAG.getNode(ISD::FSUB, VT, N0, GetNegatedExpression(N1, DAG));
Chris Lattner01b3d732005-09-28 22:28:18 +00002857 // fold ((-A) + B) -> B-A
Chris Lattner29446522007-05-14 22:04:50 +00002858 if (isNegatibleForFree(N0) == 2)
2859 return DAG.getNode(ISD::FSUB, VT, N1, GetNegatedExpression(N0, DAG));
Chris Lattnerddae4bd2007-01-08 23:04:05 +00002860
2861 // If allowed, fold (fadd (fadd x, c1), c2) -> (fadd x, (fadd c1, c2))
2862 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FADD &&
2863 N0.Val->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
2864 return DAG.getNode(ISD::FADD, VT, N0.getOperand(0),
2865 DAG.getNode(ISD::FADD, VT, N0.getOperand(1), N1));
2866
Chris Lattner01b3d732005-09-28 22:28:18 +00002867 return SDOperand();
2868}
2869
2870SDOperand DAGCombiner::visitFSUB(SDNode *N) {
2871 SDOperand N0 = N->getOperand(0);
2872 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00002873 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2874 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002875 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00002876
2877 // fold (fsub c1, c2) -> c1-c2
2878 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002879 return DAG.getNode(ISD::FSUB, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002880 // fold (A-(-B)) -> A+B
Chris Lattner29446522007-05-14 22:04:50 +00002881 if (isNegatibleForFree(N1))
2882 return DAG.getNode(ISD::FADD, VT, N0, GetNegatedExpression(N1, DAG));
2883
Chris Lattner01b3d732005-09-28 22:28:18 +00002884 return SDOperand();
2885}
2886
2887SDOperand DAGCombiner::visitFMUL(SDNode *N) {
2888 SDOperand N0 = N->getOperand(0);
2889 SDOperand N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00002890 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2891 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002892 MVT::ValueType VT = N->getValueType(0);
2893
Nate Begeman11af4ea2005-10-17 20:40:11 +00002894 // fold (fmul c1, c2) -> c1*c2
2895 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002896 return DAG.getNode(ISD::FMUL, VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00002897 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00002898 if (N0CFP && !N1CFP)
2899 return DAG.getNode(ISD::FMUL, VT, N1, N0);
Nate Begeman11af4ea2005-10-17 20:40:11 +00002900 // fold (fmul X, 2.0) -> (fadd X, X)
2901 if (N1CFP && N1CFP->isExactlyValue(+2.0))
2902 return DAG.getNode(ISD::FADD, VT, N0, N0);
Chris Lattner29446522007-05-14 22:04:50 +00002903 // fold (fmul X, -1.0) -> (fneg X)
2904 if (N1CFP && N1CFP->isExactlyValue(-1.0))
2905 return DAG.getNode(ISD::FNEG, VT, N0);
2906
2907 // -X * -Y -> X*Y
2908 if (char LHSNeg = isNegatibleForFree(N0)) {
2909 if (char RHSNeg = isNegatibleForFree(N1)) {
2910 // Both can be negated for free, check to see if at least one is cheaper
2911 // negated.
2912 if (LHSNeg == 2 || RHSNeg == 2)
2913 return DAG.getNode(ISD::FMUL, VT, GetNegatedExpression(N0, DAG),
2914 GetNegatedExpression(N1, DAG));
2915 }
2916 }
Chris Lattnerddae4bd2007-01-08 23:04:05 +00002917
2918 // If allowed, fold (fmul (fmul x, c1), c2) -> (fmul x, (fmul c1, c2))
2919 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FMUL &&
2920 N0.Val->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
2921 return DAG.getNode(ISD::FMUL, VT, N0.getOperand(0),
2922 DAG.getNode(ISD::FMUL, VT, N0.getOperand(1), N1));
2923
Chris Lattner01b3d732005-09-28 22:28:18 +00002924 return SDOperand();
2925}
2926
2927SDOperand DAGCombiner::visitFDIV(SDNode *N) {
2928 SDOperand N0 = N->getOperand(0);
2929 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00002930 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2931 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002932 MVT::ValueType VT = N->getValueType(0);
2933
Nate Begemana148d982006-01-18 22:35:16 +00002934 // fold (fdiv c1, c2) -> c1/c2
2935 if (N0CFP && N1CFP)
2936 return DAG.getNode(ISD::FDIV, VT, N0, N1);
Chris Lattner29446522007-05-14 22:04:50 +00002937
2938
2939 // -X / -Y -> X*Y
2940 if (char LHSNeg = isNegatibleForFree(N0)) {
2941 if (char RHSNeg = isNegatibleForFree(N1)) {
2942 // Both can be negated for free, check to see if at least one is cheaper
2943 // negated.
2944 if (LHSNeg == 2 || RHSNeg == 2)
2945 return DAG.getNode(ISD::FDIV, VT, GetNegatedExpression(N0, DAG),
2946 GetNegatedExpression(N1, DAG));
2947 }
2948 }
2949
Chris Lattner01b3d732005-09-28 22:28:18 +00002950 return SDOperand();
2951}
2952
2953SDOperand DAGCombiner::visitFREM(SDNode *N) {
2954 SDOperand N0 = N->getOperand(0);
2955 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00002956 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2957 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002958 MVT::ValueType VT = N->getValueType(0);
2959
Nate Begemana148d982006-01-18 22:35:16 +00002960 // fold (frem c1, c2) -> fmod(c1,c2)
2961 if (N0CFP && N1CFP)
2962 return DAG.getNode(ISD::FREM, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002963 return SDOperand();
2964}
2965
Chris Lattner12d83032006-03-05 05:30:57 +00002966SDOperand DAGCombiner::visitFCOPYSIGN(SDNode *N) {
2967 SDOperand N0 = N->getOperand(0);
2968 SDOperand N1 = N->getOperand(1);
2969 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2970 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2971 MVT::ValueType VT = N->getValueType(0);
2972
2973 if (N0CFP && N1CFP) // Constant fold
2974 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1);
2975
2976 if (N1CFP) {
2977 // copysign(x, c1) -> fabs(x) iff ispos(c1)
2978 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
2979 union {
2980 double d;
2981 int64_t i;
2982 } u;
2983 u.d = N1CFP->getValue();
2984 if (u.i >= 0)
2985 return DAG.getNode(ISD::FABS, VT, N0);
2986 else
2987 return DAG.getNode(ISD::FNEG, VT, DAG.getNode(ISD::FABS, VT, N0));
2988 }
2989
2990 // copysign(fabs(x), y) -> copysign(x, y)
2991 // copysign(fneg(x), y) -> copysign(x, y)
2992 // copysign(copysign(x,z), y) -> copysign(x, y)
2993 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
2994 N0.getOpcode() == ISD::FCOPYSIGN)
2995 return DAG.getNode(ISD::FCOPYSIGN, VT, N0.getOperand(0), N1);
2996
2997 // copysign(x, abs(y)) -> abs(x)
2998 if (N1.getOpcode() == ISD::FABS)
2999 return DAG.getNode(ISD::FABS, VT, N0);
3000
3001 // copysign(x, copysign(y,z)) -> copysign(x, z)
3002 if (N1.getOpcode() == ISD::FCOPYSIGN)
3003 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(1));
3004
3005 // copysign(x, fp_extend(y)) -> copysign(x, y)
3006 // copysign(x, fp_round(y)) -> copysign(x, y)
3007 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
3008 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(0));
3009
3010 return SDOperand();
3011}
3012
3013
Chris Lattner01b3d732005-09-28 22:28:18 +00003014
Nate Begeman83e75ec2005-09-06 04:43:02 +00003015SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003016 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003017 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00003018 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003019
3020 // fold (sint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00003021 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00003022 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003023 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003024}
3025
Nate Begeman83e75ec2005-09-06 04:43:02 +00003026SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003027 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003028 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00003029 MVT::ValueType VT = N->getValueType(0);
3030
Nate Begeman1d4d4142005-09-01 00:19:25 +00003031 // fold (uint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00003032 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00003033 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003034 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003035}
3036
Nate Begeman83e75ec2005-09-06 04:43:02 +00003037SDOperand DAGCombiner::visitFP_TO_SINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003038 SDOperand N0 = N->getOperand(0);
3039 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3040 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003041
3042 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00003043 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00003044 return DAG.getNode(ISD::FP_TO_SINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003045 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003046}
3047
Nate Begeman83e75ec2005-09-06 04:43:02 +00003048SDOperand DAGCombiner::visitFP_TO_UINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003049 SDOperand N0 = N->getOperand(0);
3050 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3051 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003052
3053 // fold (fp_to_uint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00003054 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00003055 return DAG.getNode(ISD::FP_TO_UINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003056 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003057}
3058
Nate Begeman83e75ec2005-09-06 04:43:02 +00003059SDOperand DAGCombiner::visitFP_ROUND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003060 SDOperand N0 = N->getOperand(0);
3061 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3062 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003063
3064 // fold (fp_round c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00003065 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00003066 return DAG.getNode(ISD::FP_ROUND, VT, N0);
Chris Lattner79dbea52006-03-13 06:26:26 +00003067
3068 // fold (fp_round (fp_extend x)) -> x
3069 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
3070 return N0.getOperand(0);
3071
3072 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
3073 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.Val->hasOneUse()) {
3074 SDOperand Tmp = DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0));
3075 AddToWorkList(Tmp.Val);
3076 return DAG.getNode(ISD::FCOPYSIGN, VT, Tmp, N0.getOperand(1));
3077 }
3078
Nate Begeman83e75ec2005-09-06 04:43:02 +00003079 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003080}
3081
Nate Begeman83e75ec2005-09-06 04:43:02 +00003082SDOperand DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003083 SDOperand N0 = N->getOperand(0);
3084 MVT::ValueType VT = N->getValueType(0);
3085 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00003086 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003087
Nate Begeman1d4d4142005-09-01 00:19:25 +00003088 // fold (fp_round_inreg c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00003089 if (N0CFP) {
3090 SDOperand Round = DAG.getConstantFP(N0CFP->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003091 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003092 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00003093 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003094}
3095
Nate Begeman83e75ec2005-09-06 04:43:02 +00003096SDOperand DAGCombiner::visitFP_EXTEND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003097 SDOperand N0 = N->getOperand(0);
3098 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3099 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003100
3101 // fold (fp_extend c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00003102 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00003103 return DAG.getNode(ISD::FP_EXTEND, VT, N0);
Chris Lattnere564dbb2006-05-05 21:34:35 +00003104
3105 // fold (fpext (load x)) -> (fpext (fpround (extload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00003106 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00003107 (!AfterLegalize||TLI.isLoadXLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003108 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3109 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
3110 LN0->getBasePtr(), LN0->getSrcValue(),
3111 LN0->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003112 N0.getValueType(),
3113 LN0->isVolatile(),
3114 LN0->getAlignment());
Chris Lattnere564dbb2006-05-05 21:34:35 +00003115 CombineTo(N, ExtLoad);
3116 CombineTo(N0.Val, DAG.getNode(ISD::FP_ROUND, N0.getValueType(), ExtLoad),
3117 ExtLoad.getValue(1));
3118 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
3119 }
3120
3121
Nate Begeman83e75ec2005-09-06 04:43:02 +00003122 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003123}
3124
Nate Begeman83e75ec2005-09-06 04:43:02 +00003125SDOperand DAGCombiner::visitFNEG(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003126 SDOperand N0 = N->getOperand(0);
3127 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3128 MVT::ValueType VT = N->getValueType(0);
3129
3130 // fold (fneg c1) -> -c1
Nate Begeman646d7e22005-09-02 21:18:40 +00003131 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00003132 return DAG.getNode(ISD::FNEG, VT, N0);
3133 // fold (fneg (sub x, y)) -> (sub y, x)
Chris Lattner12d83032006-03-05 05:30:57 +00003134 if (N0.getOpcode() == ISD::SUB)
3135 return DAG.getNode(ISD::SUB, VT, N0.getOperand(1), N0.getOperand(0));
Nate Begemana148d982006-01-18 22:35:16 +00003136 // fold (fneg (fneg x)) -> x
Chris Lattner12d83032006-03-05 05:30:57 +00003137 if (N0.getOpcode() == ISD::FNEG)
3138 return N0.getOperand(0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003139 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003140}
3141
Nate Begeman83e75ec2005-09-06 04:43:02 +00003142SDOperand DAGCombiner::visitFABS(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003143 SDOperand N0 = N->getOperand(0);
3144 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3145 MVT::ValueType VT = N->getValueType(0);
3146
Nate Begeman1d4d4142005-09-01 00:19:25 +00003147 // fold (fabs c1) -> fabs(c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00003148 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00003149 return DAG.getNode(ISD::FABS, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003150 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00003151 if (N0.getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00003152 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003153 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00003154 // fold (fabs (fcopysign x, y)) -> (fabs x)
3155 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
3156 return DAG.getNode(ISD::FABS, VT, N0.getOperand(0));
3157
Nate Begeman83e75ec2005-09-06 04:43:02 +00003158 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003159}
3160
Nate Begeman44728a72005-09-19 22:34:01 +00003161SDOperand DAGCombiner::visitBRCOND(SDNode *N) {
3162 SDOperand Chain = N->getOperand(0);
3163 SDOperand N1 = N->getOperand(1);
3164 SDOperand N2 = N->getOperand(2);
3165 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
3166
3167 // never taken branch, fold to chain
3168 if (N1C && N1C->isNullValue())
3169 return Chain;
3170 // unconditional branch
Nate Begemane17daeb2005-10-05 21:43:42 +00003171 if (N1C && N1C->getValue() == 1)
Nate Begeman44728a72005-09-19 22:34:01 +00003172 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
Nate Begeman750ac1b2006-02-01 07:19:44 +00003173 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
3174 // on the target.
3175 if (N1.getOpcode() == ISD::SETCC &&
3176 TLI.isOperationLegal(ISD::BR_CC, MVT::Other)) {
3177 return DAG.getNode(ISD::BR_CC, MVT::Other, Chain, N1.getOperand(2),
3178 N1.getOperand(0), N1.getOperand(1), N2);
3179 }
Nate Begeman44728a72005-09-19 22:34:01 +00003180 return SDOperand();
3181}
3182
Chris Lattner3ea0b472005-10-05 06:47:48 +00003183// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
3184//
Nate Begeman44728a72005-09-19 22:34:01 +00003185SDOperand DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00003186 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
3187 SDOperand CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
3188
3189 // Use SimplifySetCC to simplify SETCC's.
Nate Begemane17daeb2005-10-05 21:43:42 +00003190 SDOperand Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false);
Chris Lattner30f73e72006-10-14 03:52:46 +00003191 if (Simp.Val) AddToWorkList(Simp.Val);
3192
Nate Begemane17daeb2005-10-05 21:43:42 +00003193 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.Val);
3194
3195 // fold br_cc true, dest -> br dest (unconditional branch)
3196 if (SCCC && SCCC->getValue())
3197 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
3198 N->getOperand(4));
3199 // fold br_cc false, dest -> unconditional fall through
3200 if (SCCC && SCCC->isNullValue())
3201 return N->getOperand(0);
Chris Lattner30f73e72006-10-14 03:52:46 +00003202
Nate Begemane17daeb2005-10-05 21:43:42 +00003203 // fold to a simpler setcc
3204 if (Simp.Val && Simp.getOpcode() == ISD::SETCC)
3205 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
3206 Simp.getOperand(2), Simp.getOperand(0),
3207 Simp.getOperand(1), N->getOperand(4));
Nate Begeman44728a72005-09-19 22:34:01 +00003208 return SDOperand();
3209}
3210
Chris Lattner448f2192006-11-11 00:39:41 +00003211
3212/// CombineToPreIndexedLoadStore - Try turning a load / store and a
3213/// pre-indexed load / store when the base pointer is a add or subtract
3214/// and it has other uses besides the load / store. After the
3215/// transformation, the new indexed load / store has effectively folded
3216/// the add / subtract in and all of its other uses are redirected to the
3217/// new load / store.
3218bool DAGCombiner::CombineToPreIndexedLoadStore(SDNode *N) {
3219 if (!AfterLegalize)
3220 return false;
3221
3222 bool isLoad = true;
3223 SDOperand Ptr;
3224 MVT::ValueType VT;
3225 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Evan Chenge90460e2006-12-16 06:25:23 +00003226 if (LD->getAddressingMode() != ISD::UNINDEXED)
3227 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00003228 VT = LD->getLoadedVT();
Evan Cheng83060c52007-03-07 08:07:03 +00003229 if (!TLI.isIndexedLoadLegal(ISD::PRE_INC, VT) &&
Chris Lattner448f2192006-11-11 00:39:41 +00003230 !TLI.isIndexedLoadLegal(ISD::PRE_DEC, VT))
3231 return false;
3232 Ptr = LD->getBasePtr();
3233 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Evan Chenge90460e2006-12-16 06:25:23 +00003234 if (ST->getAddressingMode() != ISD::UNINDEXED)
3235 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00003236 VT = ST->getStoredVT();
3237 if (!TLI.isIndexedStoreLegal(ISD::PRE_INC, VT) &&
3238 !TLI.isIndexedStoreLegal(ISD::PRE_DEC, VT))
3239 return false;
3240 Ptr = ST->getBasePtr();
3241 isLoad = false;
3242 } else
3243 return false;
3244
Chris Lattner9f1794e2006-11-11 00:56:29 +00003245 // If the pointer is not an add/sub, or if it doesn't have multiple uses, bail
3246 // out. There is no reason to make this a preinc/predec.
3247 if ((Ptr.getOpcode() != ISD::ADD && Ptr.getOpcode() != ISD::SUB) ||
3248 Ptr.Val->hasOneUse())
3249 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00003250
Chris Lattner9f1794e2006-11-11 00:56:29 +00003251 // Ask the target to do addressing mode selection.
3252 SDOperand BasePtr;
3253 SDOperand Offset;
3254 ISD::MemIndexedMode AM = ISD::UNINDEXED;
3255 if (!TLI.getPreIndexedAddressParts(N, BasePtr, Offset, AM, DAG))
3256 return false;
Evan Chenga7d4a042007-05-03 23:52:19 +00003257 // Don't create a indexed load / store with zero offset.
3258 if (isa<ConstantSDNode>(Offset) &&
3259 cast<ConstantSDNode>(Offset)->getValue() == 0)
3260 return false;
Chris Lattner9f1794e2006-11-11 00:56:29 +00003261
Chris Lattner41e53fd2006-11-11 01:00:15 +00003262 // Try turning it into a pre-indexed load / store except when:
3263 // 1) The base is a frame index.
3264 // 2) If N is a store and the ptr is either the same as or is a
Chris Lattner9f1794e2006-11-11 00:56:29 +00003265 // predecessor of the value being stored.
Chris Lattner41e53fd2006-11-11 01:00:15 +00003266 // 3) Another use of base ptr is a predecessor of N. If ptr is folded
Chris Lattner9f1794e2006-11-11 00:56:29 +00003267 // that would create a cycle.
Chris Lattner41e53fd2006-11-11 01:00:15 +00003268 // 4) All uses are load / store ops that use it as base ptr.
Chris Lattner448f2192006-11-11 00:39:41 +00003269
Chris Lattner41e53fd2006-11-11 01:00:15 +00003270 // Check #1. Preinc'ing a frame index would require copying the stack pointer
3271 // (plus the implicit offset) to a register to preinc anyway.
3272 if (isa<FrameIndexSDNode>(BasePtr))
3273 return false;
3274
3275 // Check #2.
Chris Lattner9f1794e2006-11-11 00:56:29 +00003276 if (!isLoad) {
3277 SDOperand Val = cast<StoreSDNode>(N)->getValue();
3278 if (Val == Ptr || Ptr.Val->isPredecessor(Val.Val))
3279 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00003280 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00003281
3282 // Now check for #2 and #3.
3283 bool RealUse = false;
3284 for (SDNode::use_iterator I = Ptr.Val->use_begin(),
3285 E = Ptr.Val->use_end(); I != E; ++I) {
3286 SDNode *Use = *I;
3287 if (Use == N)
3288 continue;
3289 if (Use->isPredecessor(N))
3290 return false;
3291
3292 if (!((Use->getOpcode() == ISD::LOAD &&
3293 cast<LoadSDNode>(Use)->getBasePtr() == Ptr) ||
3294 (Use->getOpcode() == ISD::STORE) &&
3295 cast<StoreSDNode>(Use)->getBasePtr() == Ptr))
3296 RealUse = true;
3297 }
3298 if (!RealUse)
3299 return false;
3300
3301 SDOperand Result;
3302 if (isLoad)
3303 Result = DAG.getIndexedLoad(SDOperand(N,0), BasePtr, Offset, AM);
3304 else
3305 Result = DAG.getIndexedStore(SDOperand(N,0), BasePtr, Offset, AM);
3306 ++PreIndexedNodes;
3307 ++NodesCombined;
Bill Wendling832171c2006-12-07 20:04:42 +00003308 DOUT << "\nReplacing.4 "; DEBUG(N->dump());
3309 DOUT << "\nWith: "; DEBUG(Result.Val->dump(&DAG));
3310 DOUT << '\n';
Chris Lattner9f1794e2006-11-11 00:56:29 +00003311 std::vector<SDNode*> NowDead;
3312 if (isLoad) {
3313 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(0),
3314 NowDead);
3315 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1), Result.getValue(2),
3316 NowDead);
3317 } else {
3318 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(1),
3319 NowDead);
3320 }
3321
3322 // Nodes can end up on the worklist more than once. Make sure we do
3323 // not process a node that has been replaced.
3324 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
3325 removeFromWorkList(NowDead[i]);
3326 // Finally, since the node is now dead, remove it from the graph.
3327 DAG.DeleteNode(N);
3328
3329 // Replace the uses of Ptr with uses of the updated base value.
3330 DAG.ReplaceAllUsesOfValueWith(Ptr, Result.getValue(isLoad ? 1 : 0),
3331 NowDead);
3332 removeFromWorkList(Ptr.Val);
3333 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
3334 removeFromWorkList(NowDead[i]);
3335 DAG.DeleteNode(Ptr.Val);
3336
3337 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00003338}
3339
3340/// CombineToPostIndexedLoadStore - Try combine a load / store with a
3341/// add / sub of the base pointer node into a post-indexed load / store.
3342/// The transformation folded the add / subtract into the new indexed
3343/// load / store effectively and all of its uses are redirected to the
3344/// new load / store.
3345bool DAGCombiner::CombineToPostIndexedLoadStore(SDNode *N) {
3346 if (!AfterLegalize)
3347 return false;
3348
3349 bool isLoad = true;
3350 SDOperand Ptr;
3351 MVT::ValueType VT;
3352 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Evan Chenge90460e2006-12-16 06:25:23 +00003353 if (LD->getAddressingMode() != ISD::UNINDEXED)
3354 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00003355 VT = LD->getLoadedVT();
3356 if (!TLI.isIndexedLoadLegal(ISD::POST_INC, VT) &&
3357 !TLI.isIndexedLoadLegal(ISD::POST_DEC, VT))
3358 return false;
3359 Ptr = LD->getBasePtr();
3360 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Evan Chenge90460e2006-12-16 06:25:23 +00003361 if (ST->getAddressingMode() != ISD::UNINDEXED)
3362 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00003363 VT = ST->getStoredVT();
3364 if (!TLI.isIndexedStoreLegal(ISD::POST_INC, VT) &&
3365 !TLI.isIndexedStoreLegal(ISD::POST_DEC, VT))
3366 return false;
3367 Ptr = ST->getBasePtr();
3368 isLoad = false;
3369 } else
3370 return false;
3371
Evan Chengcc470212006-11-16 00:08:20 +00003372 if (Ptr.Val->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00003373 return false;
3374
3375 for (SDNode::use_iterator I = Ptr.Val->use_begin(),
3376 E = Ptr.Val->use_end(); I != E; ++I) {
3377 SDNode *Op = *I;
3378 if (Op == N ||
3379 (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB))
3380 continue;
3381
3382 SDOperand BasePtr;
3383 SDOperand Offset;
3384 ISD::MemIndexedMode AM = ISD::UNINDEXED;
3385 if (TLI.getPostIndexedAddressParts(N, Op, BasePtr, Offset, AM, DAG)) {
3386 if (Ptr == Offset)
3387 std::swap(BasePtr, Offset);
3388 if (Ptr != BasePtr)
Chris Lattner448f2192006-11-11 00:39:41 +00003389 continue;
Evan Chenga7d4a042007-05-03 23:52:19 +00003390 // Don't create a indexed load / store with zero offset.
3391 if (isa<ConstantSDNode>(Offset) &&
3392 cast<ConstantSDNode>(Offset)->getValue() == 0)
3393 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00003394
Chris Lattner9f1794e2006-11-11 00:56:29 +00003395 // Try turning it into a post-indexed load / store except when
3396 // 1) All uses are load / store ops that use it as base ptr.
3397 // 2) Op must be independent of N, i.e. Op is neither a predecessor
3398 // nor a successor of N. Otherwise, if Op is folded that would
3399 // create a cycle.
3400
3401 // Check for #1.
3402 bool TryNext = false;
3403 for (SDNode::use_iterator II = BasePtr.Val->use_begin(),
3404 EE = BasePtr.Val->use_end(); II != EE; ++II) {
3405 SDNode *Use = *II;
3406 if (Use == Ptr.Val)
Chris Lattner448f2192006-11-11 00:39:41 +00003407 continue;
3408
Chris Lattner9f1794e2006-11-11 00:56:29 +00003409 // If all the uses are load / store addresses, then don't do the
3410 // transformation.
3411 if (Use->getOpcode() == ISD::ADD || Use->getOpcode() == ISD::SUB){
3412 bool RealUse = false;
3413 for (SDNode::use_iterator III = Use->use_begin(),
3414 EEE = Use->use_end(); III != EEE; ++III) {
3415 SDNode *UseUse = *III;
3416 if (!((UseUse->getOpcode() == ISD::LOAD &&
3417 cast<LoadSDNode>(UseUse)->getBasePtr().Val == Use) ||
3418 (UseUse->getOpcode() == ISD::STORE) &&
3419 cast<StoreSDNode>(UseUse)->getBasePtr().Val == Use))
3420 RealUse = true;
3421 }
Chris Lattner448f2192006-11-11 00:39:41 +00003422
Chris Lattner9f1794e2006-11-11 00:56:29 +00003423 if (!RealUse) {
3424 TryNext = true;
3425 break;
Chris Lattner448f2192006-11-11 00:39:41 +00003426 }
3427 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00003428 }
3429 if (TryNext)
3430 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00003431
Chris Lattner9f1794e2006-11-11 00:56:29 +00003432 // Check for #2
3433 if (!Op->isPredecessor(N) && !N->isPredecessor(Op)) {
3434 SDOperand Result = isLoad
3435 ? DAG.getIndexedLoad(SDOperand(N,0), BasePtr, Offset, AM)
3436 : DAG.getIndexedStore(SDOperand(N,0), BasePtr, Offset, AM);
3437 ++PostIndexedNodes;
3438 ++NodesCombined;
Bill Wendling832171c2006-12-07 20:04:42 +00003439 DOUT << "\nReplacing.5 "; DEBUG(N->dump());
3440 DOUT << "\nWith: "; DEBUG(Result.Val->dump(&DAG));
3441 DOUT << '\n';
Chris Lattner9f1794e2006-11-11 00:56:29 +00003442 std::vector<SDNode*> NowDead;
3443 if (isLoad) {
3444 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(0),
Chris Lattner448f2192006-11-11 00:39:41 +00003445 NowDead);
Chris Lattner9f1794e2006-11-11 00:56:29 +00003446 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1), Result.getValue(2),
3447 NowDead);
3448 } else {
3449 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(1),
3450 NowDead);
Chris Lattner448f2192006-11-11 00:39:41 +00003451 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00003452
3453 // Nodes can end up on the worklist more than once. Make sure we do
3454 // not process a node that has been replaced.
3455 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
3456 removeFromWorkList(NowDead[i]);
3457 // Finally, since the node is now dead, remove it from the graph.
3458 DAG.DeleteNode(N);
3459
3460 // Replace the uses of Use with uses of the updated base value.
3461 DAG.ReplaceAllUsesOfValueWith(SDOperand(Op, 0),
3462 Result.getValue(isLoad ? 1 : 0),
3463 NowDead);
3464 removeFromWorkList(Op);
3465 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
3466 removeFromWorkList(NowDead[i]);
3467 DAG.DeleteNode(Op);
3468
3469 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00003470 }
3471 }
3472 }
3473 return false;
3474}
3475
3476
Chris Lattner01a22022005-10-10 22:04:48 +00003477SDOperand DAGCombiner::visitLOAD(SDNode *N) {
Evan Cheng466685d2006-10-09 20:57:25 +00003478 LoadSDNode *LD = cast<LoadSDNode>(N);
3479 SDOperand Chain = LD->getChain();
3480 SDOperand Ptr = LD->getBasePtr();
Evan Cheng45a7ca92007-05-01 00:38:21 +00003481
3482 // If load is not volatile and there are no uses of the loaded value (and
3483 // the updated indexed value in case of indexed loads), change uses of the
3484 // chain value into uses of the chain input (i.e. delete the dead load).
3485 if (!LD->isVolatile()) {
Evan Cheng498f5592007-05-01 08:53:39 +00003486 if (N->getValueType(1) == MVT::Other) {
3487 // Unindexed loads.
3488 if (N->hasNUsesOfValue(0, 0))
3489 return CombineTo(N, DAG.getNode(ISD::UNDEF, N->getValueType(0)), Chain);
3490 } else {
3491 // Indexed loads.
3492 assert(N->getValueType(2) == MVT::Other && "Malformed indexed loads?");
3493 if (N->hasNUsesOfValue(0, 0) && N->hasNUsesOfValue(0, 1)) {
3494 SDOperand Undef0 = DAG.getNode(ISD::UNDEF, N->getValueType(0));
3495 SDOperand Undef1 = DAG.getNode(ISD::UNDEF, N->getValueType(1));
3496 SDOperand To[] = { Undef0, Undef1, Chain };
3497 return CombineTo(N, To, 3);
Evan Cheng45a7ca92007-05-01 00:38:21 +00003498 }
Evan Cheng45a7ca92007-05-01 00:38:21 +00003499 }
3500 }
Chris Lattner01a22022005-10-10 22:04:48 +00003501
3502 // If this load is directly stored, replace the load value with the stored
3503 // value.
3504 // TODO: Handle store large -> read small portion.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00003505 // TODO: Handle TRUNCSTORE/LOADEXT
3506 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00003507 if (ISD::isNON_TRUNCStore(Chain.Val)) {
3508 StoreSDNode *PrevST = cast<StoreSDNode>(Chain);
3509 if (PrevST->getBasePtr() == Ptr &&
3510 PrevST->getValue().getValueType() == N->getValueType(0))
Jim Laskeyc2b19f32006-10-11 17:47:52 +00003511 return CombineTo(N, Chain.getOperand(1), Chain);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003512 }
Jim Laskeyc2b19f32006-10-11 17:47:52 +00003513 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00003514
Jim Laskey7ca56af2006-10-11 13:47:09 +00003515 if (CombinerAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00003516 // Walk up chain skipping non-aliasing memory nodes.
3517 SDOperand BetterChain = FindBetterChain(N, Chain);
3518
Jim Laskey6ff23e52006-10-04 16:53:27 +00003519 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00003520 if (Chain != BetterChain) {
Jim Laskeyc2b19f32006-10-11 17:47:52 +00003521 SDOperand ReplLoad;
3522
Jim Laskey279f0532006-09-25 16:29:54 +00003523 // Replace the chain to void dependency.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00003524 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
3525 ReplLoad = DAG.getLoad(N->getValueType(0), BetterChain, Ptr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00003526 LD->getSrcValue(), LD->getSrcValueOffset(),
3527 LD->isVolatile(), LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00003528 } else {
3529 ReplLoad = DAG.getExtLoad(LD->getExtensionType(),
3530 LD->getValueType(0),
3531 BetterChain, Ptr, LD->getSrcValue(),
3532 LD->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003533 LD->getLoadedVT(),
3534 LD->isVolatile(),
3535 LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00003536 }
Jim Laskey279f0532006-09-25 16:29:54 +00003537
Jim Laskey6ff23e52006-10-04 16:53:27 +00003538 // Create token factor to keep old chain connected.
Jim Laskey288af5e2006-09-25 19:32:58 +00003539 SDOperand Token = DAG.getNode(ISD::TokenFactor, MVT::Other,
3540 Chain, ReplLoad.getValue(1));
Jim Laskey6ff23e52006-10-04 16:53:27 +00003541
Jim Laskey274062c2006-10-13 23:32:28 +00003542 // Replace uses with load result and token factor. Don't add users
3543 // to work list.
3544 return CombineTo(N, ReplLoad.getValue(0), Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00003545 }
3546 }
3547
Evan Cheng7fc033a2006-11-03 03:06:21 +00003548 // Try transforming N to an indexed load.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00003549 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Evan Cheng7fc033a2006-11-03 03:06:21 +00003550 return SDOperand(N, 0);
3551
Chris Lattner01a22022005-10-10 22:04:48 +00003552 return SDOperand();
3553}
3554
Chris Lattner87514ca2005-10-10 22:31:19 +00003555SDOperand DAGCombiner::visitSTORE(SDNode *N) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00003556 StoreSDNode *ST = cast<StoreSDNode>(N);
3557 SDOperand Chain = ST->getChain();
3558 SDOperand Value = ST->getValue();
3559 SDOperand Ptr = ST->getBasePtr();
Jim Laskey7aed46c2006-10-11 18:55:16 +00003560
Evan Cheng59d5b682007-05-07 21:27:48 +00003561 // If this is a store of a bit convert, store the input value if the
Evan Cheng2c4f9432007-05-09 21:49:47 +00003562 // resultant store does not need a higher alignment than the original.
3563 if (Value.getOpcode() == ISD::BIT_CONVERT && !ST->isTruncatingStore()) {
Evan Cheng59d5b682007-05-07 21:27:48 +00003564 unsigned Align = ST->getAlignment();
3565 MVT::ValueType SVT = Value.getOperand(0).getValueType();
3566 unsigned OrigAlign = TLI.getTargetMachine().getTargetData()->
3567 getPrefTypeAlignment(getTypeForValueType(SVT));
Evan Chengc2cd2b22007-05-07 21:36:06 +00003568 if (Align <= OrigAlign && TLI.isOperationLegal(ISD::STORE, SVT))
Evan Cheng59d5b682007-05-07 21:27:48 +00003569 return DAG.getStore(Chain, Value.getOperand(0), Ptr, ST->getSrcValue(),
3570 ST->getSrcValueOffset());
Jim Laskey279f0532006-09-25 16:29:54 +00003571 }
3572
Nate Begeman2cbba892006-12-11 02:23:46 +00003573 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Nate Begeman2cbba892006-12-11 02:23:46 +00003574 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Value)) {
Evan Cheng25ece662006-12-11 17:25:19 +00003575 if (Value.getOpcode() != ISD::TargetConstantFP) {
3576 SDOperand Tmp;
Chris Lattner62be1a72006-12-12 04:16:14 +00003577 switch (CFP->getValueType(0)) {
3578 default: assert(0 && "Unknown FP type");
3579 case MVT::f32:
3580 if (!AfterLegalize || TLI.isTypeLegal(MVT::i32)) {
3581 Tmp = DAG.getConstant(FloatToBits(CFP->getValue()), MVT::i32);
3582 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
3583 ST->getSrcValueOffset());
3584 }
3585 break;
3586 case MVT::f64:
3587 if (!AfterLegalize || TLI.isTypeLegal(MVT::i64)) {
3588 Tmp = DAG.getConstant(DoubleToBits(CFP->getValue()), MVT::i64);
3589 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
3590 ST->getSrcValueOffset());
3591 } else if (TLI.isTypeLegal(MVT::i32)) {
3592 // Many FP stores are not make apparent until after legalize, e.g. for
3593 // argument passing. Since this is so common, custom legalize the
3594 // 64-bit integer store into two 32-bit stores.
3595 uint64_t Val = DoubleToBits(CFP->getValue());
3596 SDOperand Lo = DAG.getConstant(Val & 0xFFFFFFFF, MVT::i32);
3597 SDOperand Hi = DAG.getConstant(Val >> 32, MVT::i32);
3598 if (!TLI.isLittleEndian()) std::swap(Lo, Hi);
3599
3600 SDOperand St0 = DAG.getStore(Chain, Lo, Ptr, ST->getSrcValue(),
3601 ST->getSrcValueOffset());
3602 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3603 DAG.getConstant(4, Ptr.getValueType()));
3604 SDOperand St1 = DAG.getStore(Chain, Hi, Ptr, ST->getSrcValue(),
3605 ST->getSrcValueOffset()+4);
3606 return DAG.getNode(ISD::TokenFactor, MVT::Other, St0, St1);
3607 }
3608 break;
Evan Cheng25ece662006-12-11 17:25:19 +00003609 }
Nate Begeman2cbba892006-12-11 02:23:46 +00003610 }
Nate Begeman2cbba892006-12-11 02:23:46 +00003611 }
3612
Jim Laskey279f0532006-09-25 16:29:54 +00003613 if (CombinerAA) {
3614 // Walk up chain skipping non-aliasing memory nodes.
3615 SDOperand BetterChain = FindBetterChain(N, Chain);
3616
Jim Laskey6ff23e52006-10-04 16:53:27 +00003617 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00003618 if (Chain != BetterChain) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00003619 // Replace the chain to avoid dependency.
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00003620 SDOperand ReplStore;
3621 if (ST->isTruncatingStore()) {
3622 ReplStore = DAG.getTruncStore(BetterChain, Value, Ptr,
3623 ST->getSrcValue(),ST->getSrcValueOffset(), ST->getStoredVT());
3624 } else {
3625 ReplStore = DAG.getStore(BetterChain, Value, Ptr,
3626 ST->getSrcValue(), ST->getSrcValueOffset());
3627 }
3628
Jim Laskey279f0532006-09-25 16:29:54 +00003629 // Create token to keep both nodes around.
Jim Laskey274062c2006-10-13 23:32:28 +00003630 SDOperand Token =
3631 DAG.getNode(ISD::TokenFactor, MVT::Other, Chain, ReplStore);
3632
3633 // Don't add users to work list.
3634 return CombineTo(N, Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00003635 }
Jim Laskeyd1aed7a2006-09-21 16:28:59 +00003636 }
Chris Lattnerc33baaa2005-12-23 05:48:07 +00003637
Evan Cheng33dbedc2006-11-05 09:31:14 +00003638 // Try transforming N to an indexed store.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00003639 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Evan Cheng33dbedc2006-11-05 09:31:14 +00003640 return SDOperand(N, 0);
3641
Chris Lattner87514ca2005-10-10 22:31:19 +00003642 return SDOperand();
3643}
3644
Chris Lattnerca242442006-03-19 01:27:56 +00003645SDOperand DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
3646 SDOperand InVec = N->getOperand(0);
3647 SDOperand InVal = N->getOperand(1);
3648 SDOperand EltNo = N->getOperand(2);
3649
3650 // If the invec is a BUILD_VECTOR and if EltNo is a constant, build a new
3651 // vector with the inserted element.
3652 if (InVec.getOpcode() == ISD::BUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
3653 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003654 SmallVector<SDOperand, 8> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
Chris Lattnerca242442006-03-19 01:27:56 +00003655 if (Elt < Ops.size())
3656 Ops[Elt] = InVal;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003657 return DAG.getNode(ISD::BUILD_VECTOR, InVec.getValueType(),
3658 &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00003659 }
3660
3661 return SDOperand();
3662}
3663
3664SDOperand DAGCombiner::visitVINSERT_VECTOR_ELT(SDNode *N) {
3665 SDOperand InVec = N->getOperand(0);
3666 SDOperand InVal = N->getOperand(1);
3667 SDOperand EltNo = N->getOperand(2);
3668 SDOperand NumElts = N->getOperand(3);
3669 SDOperand EltType = N->getOperand(4);
3670
3671 // If the invec is a VBUILD_VECTOR and if EltNo is a constant, build a new
3672 // vector with the inserted element.
3673 if (InVec.getOpcode() == ISD::VBUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
3674 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003675 SmallVector<SDOperand, 8> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
Chris Lattnerca242442006-03-19 01:27:56 +00003676 if (Elt < Ops.size()-2)
3677 Ops[Elt] = InVal;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003678 return DAG.getNode(ISD::VBUILD_VECTOR, InVec.getValueType(),
3679 &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00003680 }
3681
3682 return SDOperand();
3683}
3684
Chris Lattnerd7648c82006-03-28 20:28:38 +00003685SDOperand DAGCombiner::visitVBUILD_VECTOR(SDNode *N) {
3686 unsigned NumInScalars = N->getNumOperands()-2;
3687 SDOperand NumElts = N->getOperand(NumInScalars);
3688 SDOperand EltType = N->getOperand(NumInScalars+1);
3689
3690 // Check to see if this is a VBUILD_VECTOR of a bunch of VEXTRACT_VECTOR_ELT
3691 // operations. If so, and if the EXTRACT_ELT vector inputs come from at most
3692 // two distinct vectors, turn this into a shuffle node.
3693 SDOperand VecIn1, VecIn2;
3694 for (unsigned i = 0; i != NumInScalars; ++i) {
3695 // Ignore undef inputs.
3696 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
3697
3698 // If this input is something other than a VEXTRACT_VECTOR_ELT with a
3699 // constant index, bail out.
3700 if (N->getOperand(i).getOpcode() != ISD::VEXTRACT_VECTOR_ELT ||
3701 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
3702 VecIn1 = VecIn2 = SDOperand(0, 0);
3703 break;
3704 }
3705
3706 // If the input vector type disagrees with the result of the vbuild_vector,
3707 // we can't make a shuffle.
3708 SDOperand ExtractedFromVec = N->getOperand(i).getOperand(0);
3709 if (*(ExtractedFromVec.Val->op_end()-2) != NumElts ||
3710 *(ExtractedFromVec.Val->op_end()-1) != EltType) {
3711 VecIn1 = VecIn2 = SDOperand(0, 0);
3712 break;
3713 }
3714
3715 // Otherwise, remember this. We allow up to two distinct input vectors.
3716 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
3717 continue;
3718
3719 if (VecIn1.Val == 0) {
3720 VecIn1 = ExtractedFromVec;
3721 } else if (VecIn2.Val == 0) {
3722 VecIn2 = ExtractedFromVec;
3723 } else {
3724 // Too many inputs.
3725 VecIn1 = VecIn2 = SDOperand(0, 0);
3726 break;
3727 }
3728 }
3729
3730 // If everything is good, we can make a shuffle operation.
3731 if (VecIn1.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003732 SmallVector<SDOperand, 8> BuildVecIndices;
Chris Lattnerd7648c82006-03-28 20:28:38 +00003733 for (unsigned i = 0; i != NumInScalars; ++i) {
3734 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
Evan Cheng597a3bd2007-01-20 10:10:26 +00003735 BuildVecIndices.push_back(DAG.getNode(ISD::UNDEF, TLI.getPointerTy()));
Chris Lattnerd7648c82006-03-28 20:28:38 +00003736 continue;
3737 }
3738
3739 SDOperand Extract = N->getOperand(i);
3740
3741 // If extracting from the first vector, just use the index directly.
3742 if (Extract.getOperand(0) == VecIn1) {
3743 BuildVecIndices.push_back(Extract.getOperand(1));
3744 continue;
3745 }
3746
3747 // Otherwise, use InIdx + VecSize
3748 unsigned Idx = cast<ConstantSDNode>(Extract.getOperand(1))->getValue();
Evan Cheng597a3bd2007-01-20 10:10:26 +00003749 BuildVecIndices.push_back(DAG.getConstant(Idx+NumInScalars,
3750 TLI.getPointerTy()));
Chris Lattnerd7648c82006-03-28 20:28:38 +00003751 }
3752
3753 // Add count and size info.
3754 BuildVecIndices.push_back(NumElts);
Evan Cheng597a3bd2007-01-20 10:10:26 +00003755 BuildVecIndices.push_back(DAG.getValueType(TLI.getPointerTy()));
Chris Lattnerd7648c82006-03-28 20:28:38 +00003756
3757 // Return the new VVECTOR_SHUFFLE node.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003758 SDOperand Ops[5];
3759 Ops[0] = VecIn1;
Chris Lattnercef896e2006-03-28 22:19:47 +00003760 if (VecIn2.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003761 Ops[1] = VecIn2;
Chris Lattnercef896e2006-03-28 22:19:47 +00003762 } else {
3763 // Use an undef vbuild_vector as input for the second operand.
3764 std::vector<SDOperand> UnOps(NumInScalars,
3765 DAG.getNode(ISD::UNDEF,
3766 cast<VTSDNode>(EltType)->getVT()));
3767 UnOps.push_back(NumElts);
3768 UnOps.push_back(EltType);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003769 Ops[1] = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
3770 &UnOps[0], UnOps.size());
3771 AddToWorkList(Ops[1].Val);
Chris Lattnercef896e2006-03-28 22:19:47 +00003772 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003773 Ops[2] = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
3774 &BuildVecIndices[0], BuildVecIndices.size());
3775 Ops[3] = NumElts;
3776 Ops[4] = EltType;
3777 return DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector, Ops, 5);
Chris Lattnerd7648c82006-03-28 20:28:38 +00003778 }
3779
3780 return SDOperand();
3781}
3782
Chris Lattner66445d32006-03-28 22:11:53 +00003783SDOperand DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
Chris Lattnerf1d0c622006-03-31 22:16:43 +00003784 SDOperand ShufMask = N->getOperand(2);
3785 unsigned NumElts = ShufMask.getNumOperands();
3786
3787 // If the shuffle mask is an identity operation on the LHS, return the LHS.
3788 bool isIdentity = true;
3789 for (unsigned i = 0; i != NumElts; ++i) {
3790 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
3791 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
3792 isIdentity = false;
3793 break;
3794 }
3795 }
3796 if (isIdentity) return N->getOperand(0);
3797
3798 // If the shuffle mask is an identity operation on the RHS, return the RHS.
3799 isIdentity = true;
3800 for (unsigned i = 0; i != NumElts; ++i) {
3801 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
3802 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
3803 isIdentity = false;
3804 break;
3805 }
3806 }
3807 if (isIdentity) return N->getOperand(1);
Evan Chenge7bec0d2006-07-20 22:44:41 +00003808
3809 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
3810 // needed at all.
3811 bool isUnary = true;
Evan Cheng917ec982006-07-21 08:25:53 +00003812 bool isSplat = true;
Evan Chenge7bec0d2006-07-20 22:44:41 +00003813 int VecNum = -1;
Reid Spencer9160a6a2006-07-25 20:44:41 +00003814 unsigned BaseIdx = 0;
Evan Chenge7bec0d2006-07-20 22:44:41 +00003815 for (unsigned i = 0; i != NumElts; ++i)
3816 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
3817 unsigned Idx = cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue();
3818 int V = (Idx < NumElts) ? 0 : 1;
Evan Cheng917ec982006-07-21 08:25:53 +00003819 if (VecNum == -1) {
Evan Chenge7bec0d2006-07-20 22:44:41 +00003820 VecNum = V;
Evan Cheng917ec982006-07-21 08:25:53 +00003821 BaseIdx = Idx;
3822 } else {
3823 if (BaseIdx != Idx)
3824 isSplat = false;
3825 if (VecNum != V) {
3826 isUnary = false;
3827 break;
3828 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00003829 }
3830 }
3831
3832 SDOperand N0 = N->getOperand(0);
3833 SDOperand N1 = N->getOperand(1);
3834 // Normalize unary shuffle so the RHS is undef.
3835 if (isUnary && VecNum == 1)
3836 std::swap(N0, N1);
3837
Evan Cheng917ec982006-07-21 08:25:53 +00003838 // If it is a splat, check if the argument vector is a build_vector with
3839 // all scalar elements the same.
3840 if (isSplat) {
3841 SDNode *V = N0.Val;
3842 if (V->getOpcode() == ISD::BIT_CONVERT)
3843 V = V->getOperand(0).Val;
3844 if (V->getOpcode() == ISD::BUILD_VECTOR) {
3845 unsigned NumElems = V->getNumOperands()-2;
3846 if (NumElems > BaseIdx) {
3847 SDOperand Base;
3848 bool AllSame = true;
3849 for (unsigned i = 0; i != NumElems; ++i) {
3850 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
3851 Base = V->getOperand(i);
3852 break;
3853 }
3854 }
3855 // Splat of <u, u, u, u>, return <u, u, u, u>
3856 if (!Base.Val)
3857 return N0;
3858 for (unsigned i = 0; i != NumElems; ++i) {
3859 if (V->getOperand(i).getOpcode() != ISD::UNDEF &&
3860 V->getOperand(i) != Base) {
3861 AllSame = false;
3862 break;
3863 }
3864 }
3865 // Splat of <x, x, x, x>, return <x, x, x, x>
3866 if (AllSame)
3867 return N0;
3868 }
3869 }
3870 }
3871
Evan Chenge7bec0d2006-07-20 22:44:41 +00003872 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
3873 // into an undef.
3874 if (isUnary || N0 == N1) {
3875 if (N0.getOpcode() == ISD::UNDEF)
Evan Chengc04766a2006-04-06 23:20:43 +00003876 return DAG.getNode(ISD::UNDEF, N->getValueType(0));
Chris Lattner66445d32006-03-28 22:11:53 +00003877 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
3878 // first operand.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003879 SmallVector<SDOperand, 8> MappedOps;
Chris Lattner66445d32006-03-28 22:11:53 +00003880 for (unsigned i = 0, e = ShufMask.getNumOperands(); i != e; ++i) {
Evan Chengc04766a2006-04-06 23:20:43 +00003881 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
3882 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) {
3883 MappedOps.push_back(ShufMask.getOperand(i));
3884 } else {
Chris Lattner66445d32006-03-28 22:11:53 +00003885 unsigned NewIdx =
3886 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
3887 MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32));
Chris Lattner66445d32006-03-28 22:11:53 +00003888 }
3889 }
3890 ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMask.getValueType(),
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003891 &MappedOps[0], MappedOps.size());
Chris Lattner3e104b12006-04-08 04:15:24 +00003892 AddToWorkList(ShufMask.Val);
Chris Lattner66445d32006-03-28 22:11:53 +00003893 return DAG.getNode(ISD::VECTOR_SHUFFLE, N->getValueType(0),
Evan Chenge7bec0d2006-07-20 22:44:41 +00003894 N0,
Chris Lattner66445d32006-03-28 22:11:53 +00003895 DAG.getNode(ISD::UNDEF, N->getValueType(0)),
3896 ShufMask);
3897 }
3898
3899 return SDOperand();
3900}
3901
Chris Lattnerf1d0c622006-03-31 22:16:43 +00003902SDOperand DAGCombiner::visitVVECTOR_SHUFFLE(SDNode *N) {
3903 SDOperand ShufMask = N->getOperand(2);
3904 unsigned NumElts = ShufMask.getNumOperands()-2;
3905
3906 // If the shuffle mask is an identity operation on the LHS, return the LHS.
3907 bool isIdentity = true;
3908 for (unsigned i = 0; i != NumElts; ++i) {
3909 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
3910 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
3911 isIdentity = false;
3912 break;
3913 }
3914 }
3915 if (isIdentity) return N->getOperand(0);
3916
3917 // If the shuffle mask is an identity operation on the RHS, return the RHS.
3918 isIdentity = true;
3919 for (unsigned i = 0; i != NumElts; ++i) {
3920 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
3921 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
3922 isIdentity = false;
3923 break;
3924 }
3925 }
3926 if (isIdentity) return N->getOperand(1);
3927
Evan Chenge7bec0d2006-07-20 22:44:41 +00003928 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
3929 // needed at all.
3930 bool isUnary = true;
Evan Cheng917ec982006-07-21 08:25:53 +00003931 bool isSplat = true;
Evan Chenge7bec0d2006-07-20 22:44:41 +00003932 int VecNum = -1;
Reid Spencer9160a6a2006-07-25 20:44:41 +00003933 unsigned BaseIdx = 0;
Evan Chenge7bec0d2006-07-20 22:44:41 +00003934 for (unsigned i = 0; i != NumElts; ++i)
3935 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
3936 unsigned Idx = cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue();
3937 int V = (Idx < NumElts) ? 0 : 1;
Evan Cheng917ec982006-07-21 08:25:53 +00003938 if (VecNum == -1) {
Evan Chenge7bec0d2006-07-20 22:44:41 +00003939 VecNum = V;
Evan Cheng917ec982006-07-21 08:25:53 +00003940 BaseIdx = Idx;
3941 } else {
3942 if (BaseIdx != Idx)
3943 isSplat = false;
3944 if (VecNum != V) {
3945 isUnary = false;
3946 break;
3947 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00003948 }
3949 }
3950
3951 SDOperand N0 = N->getOperand(0);
3952 SDOperand N1 = N->getOperand(1);
3953 // Normalize unary shuffle so the RHS is undef.
3954 if (isUnary && VecNum == 1)
3955 std::swap(N0, N1);
3956
Evan Cheng917ec982006-07-21 08:25:53 +00003957 // If it is a splat, check if the argument vector is a build_vector with
3958 // all scalar elements the same.
3959 if (isSplat) {
3960 SDNode *V = N0.Val;
Evan Cheng59569222006-10-16 22:49:37 +00003961
3962 // If this is a vbit convert that changes the element type of the vector but
3963 // not the number of vector elements, look through it. Be careful not to
3964 // look though conversions that change things like v4f32 to v2f64.
3965 if (V->getOpcode() == ISD::VBIT_CONVERT) {
3966 SDOperand ConvInput = V->getOperand(0);
Evan Cheng5d04a1a2006-10-17 17:06:35 +00003967 if (ConvInput.getValueType() == MVT::Vector &&
3968 NumElts ==
Evan Cheng59569222006-10-16 22:49:37 +00003969 ConvInput.getConstantOperandVal(ConvInput.getNumOperands()-2))
3970 V = ConvInput.Val;
3971 }
3972
Evan Cheng917ec982006-07-21 08:25:53 +00003973 if (V->getOpcode() == ISD::VBUILD_VECTOR) {
3974 unsigned NumElems = V->getNumOperands()-2;
3975 if (NumElems > BaseIdx) {
3976 SDOperand Base;
3977 bool AllSame = true;
3978 for (unsigned i = 0; i != NumElems; ++i) {
3979 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
3980 Base = V->getOperand(i);
3981 break;
3982 }
3983 }
3984 // Splat of <u, u, u, u>, return <u, u, u, u>
3985 if (!Base.Val)
3986 return N0;
3987 for (unsigned i = 0; i != NumElems; ++i) {
3988 if (V->getOperand(i).getOpcode() != ISD::UNDEF &&
3989 V->getOperand(i) != Base) {
3990 AllSame = false;
3991 break;
3992 }
3993 }
3994 // Splat of <x, x, x, x>, return <x, x, x, x>
3995 if (AllSame)
3996 return N0;
3997 }
3998 }
3999 }
4000
Evan Chenge7bec0d2006-07-20 22:44:41 +00004001 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
4002 // into an undef.
4003 if (isUnary || N0 == N1) {
Chris Lattner17614ea2006-04-08 05:34:25 +00004004 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
4005 // first operand.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004006 SmallVector<SDOperand, 8> MappedOps;
Chris Lattner17614ea2006-04-08 05:34:25 +00004007 for (unsigned i = 0; i != NumElts; ++i) {
4008 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
4009 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) {
4010 MappedOps.push_back(ShufMask.getOperand(i));
4011 } else {
4012 unsigned NewIdx =
4013 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
4014 MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32));
4015 }
4016 }
4017 // Add the type/#elts values.
4018 MappedOps.push_back(ShufMask.getOperand(NumElts));
4019 MappedOps.push_back(ShufMask.getOperand(NumElts+1));
4020
4021 ShufMask = DAG.getNode(ISD::VBUILD_VECTOR, ShufMask.getValueType(),
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004022 &MappedOps[0], MappedOps.size());
Chris Lattner17614ea2006-04-08 05:34:25 +00004023 AddToWorkList(ShufMask.Val);
4024
4025 // Build the undef vector.
4026 SDOperand UDVal = DAG.getNode(ISD::UNDEF, MappedOps[0].getValueType());
4027 for (unsigned i = 0; i != NumElts; ++i)
4028 MappedOps[i] = UDVal;
Evan Chenge7bec0d2006-07-20 22:44:41 +00004029 MappedOps[NumElts ] = *(N0.Val->op_end()-2);
4030 MappedOps[NumElts+1] = *(N0.Val->op_end()-1);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004031 UDVal = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
4032 &MappedOps[0], MappedOps.size());
Chris Lattner17614ea2006-04-08 05:34:25 +00004033
4034 return DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector,
Evan Chenge7bec0d2006-07-20 22:44:41 +00004035 N0, UDVal, ShufMask,
Chris Lattner17614ea2006-04-08 05:34:25 +00004036 MappedOps[NumElts], MappedOps[NumElts+1]);
4037 }
4038
Chris Lattnerf1d0c622006-03-31 22:16:43 +00004039 return SDOperand();
4040}
4041
Evan Cheng44f1f092006-04-20 08:56:16 +00004042/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
4043/// a VAND to a vector_shuffle with the destination vector and a zero vector.
4044/// e.g. VAND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
4045/// vector_shuffle V, Zero, <0, 4, 2, 4>
4046SDOperand DAGCombiner::XformToShuffleWithZero(SDNode *N) {
4047 SDOperand LHS = N->getOperand(0);
4048 SDOperand RHS = N->getOperand(1);
4049 if (N->getOpcode() == ISD::VAND) {
4050 SDOperand DstVecSize = *(LHS.Val->op_end()-2);
4051 SDOperand DstVecEVT = *(LHS.Val->op_end()-1);
4052 if (RHS.getOpcode() == ISD::VBIT_CONVERT)
4053 RHS = RHS.getOperand(0);
4054 if (RHS.getOpcode() == ISD::VBUILD_VECTOR) {
4055 std::vector<SDOperand> IdxOps;
4056 unsigned NumOps = RHS.getNumOperands();
4057 unsigned NumElts = NumOps-2;
4058 MVT::ValueType EVT = cast<VTSDNode>(RHS.getOperand(NumOps-1))->getVT();
4059 for (unsigned i = 0; i != NumElts; ++i) {
4060 SDOperand Elt = RHS.getOperand(i);
4061 if (!isa<ConstantSDNode>(Elt))
4062 return SDOperand();
4063 else if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
4064 IdxOps.push_back(DAG.getConstant(i, EVT));
4065 else if (cast<ConstantSDNode>(Elt)->isNullValue())
4066 IdxOps.push_back(DAG.getConstant(NumElts, EVT));
4067 else
4068 return SDOperand();
4069 }
4070
4071 // Let's see if the target supports this vector_shuffle.
4072 if (!TLI.isVectorClearMaskLegal(IdxOps, EVT, DAG))
4073 return SDOperand();
4074
4075 // Return the new VVECTOR_SHUFFLE node.
4076 SDOperand NumEltsNode = DAG.getConstant(NumElts, MVT::i32);
4077 SDOperand EVTNode = DAG.getValueType(EVT);
4078 std::vector<SDOperand> Ops;
Chris Lattner516b9622006-09-14 20:50:57 +00004079 LHS = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, LHS, NumEltsNode,
4080 EVTNode);
Evan Cheng44f1f092006-04-20 08:56:16 +00004081 Ops.push_back(LHS);
4082 AddToWorkList(LHS.Val);
4083 std::vector<SDOperand> ZeroOps(NumElts, DAG.getConstant(0, EVT));
4084 ZeroOps.push_back(NumEltsNode);
4085 ZeroOps.push_back(EVTNode);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004086 Ops.push_back(DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
4087 &ZeroOps[0], ZeroOps.size()));
Evan Cheng44f1f092006-04-20 08:56:16 +00004088 IdxOps.push_back(NumEltsNode);
4089 IdxOps.push_back(EVTNode);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004090 Ops.push_back(DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
4091 &IdxOps[0], IdxOps.size()));
Evan Cheng44f1f092006-04-20 08:56:16 +00004092 Ops.push_back(NumEltsNode);
4093 Ops.push_back(EVTNode);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004094 SDOperand Result = DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector,
4095 &Ops[0], Ops.size());
Evan Cheng44f1f092006-04-20 08:56:16 +00004096 if (NumEltsNode != DstVecSize || EVTNode != DstVecEVT) {
4097 Result = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Result,
4098 DstVecSize, DstVecEVT);
4099 }
4100 return Result;
4101 }
4102 }
4103 return SDOperand();
4104}
4105
Chris Lattneredab1b92006-04-02 03:25:57 +00004106/// visitVBinOp - Visit a binary vector operation, like VADD. IntOp indicates
4107/// the scalar operation of the vop if it is operating on an integer vector
4108/// (e.g. ADD) and FPOp indicates the FP version (e.g. FADD).
4109SDOperand DAGCombiner::visitVBinOp(SDNode *N, ISD::NodeType IntOp,
4110 ISD::NodeType FPOp) {
4111 MVT::ValueType EltType = cast<VTSDNode>(*(N->op_end()-1))->getVT();
4112 ISD::NodeType ScalarOp = MVT::isInteger(EltType) ? IntOp : FPOp;
4113 SDOperand LHS = N->getOperand(0);
4114 SDOperand RHS = N->getOperand(1);
Evan Cheng44f1f092006-04-20 08:56:16 +00004115 SDOperand Shuffle = XformToShuffleWithZero(N);
4116 if (Shuffle.Val) return Shuffle;
4117
Chris Lattneredab1b92006-04-02 03:25:57 +00004118 // If the LHS and RHS are VBUILD_VECTOR nodes, see if we can constant fold
4119 // this operation.
4120 if (LHS.getOpcode() == ISD::VBUILD_VECTOR &&
4121 RHS.getOpcode() == ISD::VBUILD_VECTOR) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004122 SmallVector<SDOperand, 8> Ops;
Chris Lattneredab1b92006-04-02 03:25:57 +00004123 for (unsigned i = 0, e = LHS.getNumOperands()-2; i != e; ++i) {
4124 SDOperand LHSOp = LHS.getOperand(i);
4125 SDOperand RHSOp = RHS.getOperand(i);
4126 // If these two elements can't be folded, bail out.
4127 if ((LHSOp.getOpcode() != ISD::UNDEF &&
4128 LHSOp.getOpcode() != ISD::Constant &&
4129 LHSOp.getOpcode() != ISD::ConstantFP) ||
4130 (RHSOp.getOpcode() != ISD::UNDEF &&
4131 RHSOp.getOpcode() != ISD::Constant &&
4132 RHSOp.getOpcode() != ISD::ConstantFP))
4133 break;
Evan Cheng7b336a82006-05-31 06:08:35 +00004134 // Can't fold divide by zero.
4135 if (N->getOpcode() == ISD::VSDIV || N->getOpcode() == ISD::VUDIV) {
4136 if ((RHSOp.getOpcode() == ISD::Constant &&
4137 cast<ConstantSDNode>(RHSOp.Val)->isNullValue()) ||
4138 (RHSOp.getOpcode() == ISD::ConstantFP &&
4139 !cast<ConstantFPSDNode>(RHSOp.Val)->getValue()))
4140 break;
4141 }
Chris Lattneredab1b92006-04-02 03:25:57 +00004142 Ops.push_back(DAG.getNode(ScalarOp, EltType, LHSOp, RHSOp));
Chris Lattner3e104b12006-04-08 04:15:24 +00004143 AddToWorkList(Ops.back().Val);
Chris Lattneredab1b92006-04-02 03:25:57 +00004144 assert((Ops.back().getOpcode() == ISD::UNDEF ||
4145 Ops.back().getOpcode() == ISD::Constant ||
4146 Ops.back().getOpcode() == ISD::ConstantFP) &&
4147 "Scalar binop didn't fold!");
4148 }
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00004149
4150 if (Ops.size() == LHS.getNumOperands()-2) {
4151 Ops.push_back(*(LHS.Val->op_end()-2));
4152 Ops.push_back(*(LHS.Val->op_end()-1));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004153 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00004154 }
Chris Lattneredab1b92006-04-02 03:25:57 +00004155 }
4156
4157 return SDOperand();
4158}
4159
Nate Begeman44728a72005-09-19 22:34:01 +00004160SDOperand DAGCombiner::SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2){
Nate Begemanf845b452005-10-08 00:29:44 +00004161 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
4162
4163 SDOperand SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
4164 cast<CondCodeSDNode>(N0.getOperand(2))->get());
4165 // If we got a simplified select_cc node back from SimplifySelectCC, then
4166 // break it down into a new SETCC node, and a new SELECT node, and then return
4167 // the SELECT node, since we were called with a SELECT node.
4168 if (SCC.Val) {
4169 // Check to see if we got a select_cc back (to turn into setcc/select).
4170 // Otherwise, just return whatever node we got back, like fabs.
4171 if (SCC.getOpcode() == ISD::SELECT_CC) {
4172 SDOperand SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
4173 SCC.getOperand(0), SCC.getOperand(1),
4174 SCC.getOperand(4));
Chris Lattner5750df92006-03-01 04:03:14 +00004175 AddToWorkList(SETCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004176 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
4177 SCC.getOperand(3), SETCC);
4178 }
4179 return SCC;
4180 }
Nate Begeman44728a72005-09-19 22:34:01 +00004181 return SDOperand();
4182}
4183
Chris Lattner40c62d52005-10-18 06:04:22 +00004184/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
4185/// are the two values being selected between, see if we can simplify the
Chris Lattner729c6d12006-05-27 00:43:02 +00004186/// select. Callers of this should assume that TheSelect is deleted if this
4187/// returns true. As such, they should return the appropriate thing (e.g. the
4188/// node) back to the top-level of the DAG combiner loop to avoid it being
4189/// looked at.
Chris Lattner40c62d52005-10-18 06:04:22 +00004190///
4191bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDOperand LHS,
4192 SDOperand RHS) {
4193
4194 // If this is a select from two identical things, try to pull the operation
4195 // through the select.
4196 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
Chris Lattner40c62d52005-10-18 06:04:22 +00004197 // If this is a load and the token chain is identical, replace the select
4198 // of two loads with a load through a select of the address to load from.
4199 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
4200 // constants have been dropped into the constant pool.
Evan Cheng466685d2006-10-09 20:57:25 +00004201 if (LHS.getOpcode() == ISD::LOAD &&
Chris Lattner40c62d52005-10-18 06:04:22 +00004202 // Token chains must be identical.
Evan Cheng466685d2006-10-09 20:57:25 +00004203 LHS.getOperand(0) == RHS.getOperand(0)) {
4204 LoadSDNode *LLD = cast<LoadSDNode>(LHS);
4205 LoadSDNode *RLD = cast<LoadSDNode>(RHS);
4206
4207 // If this is an EXTLOAD, the VT's must match.
Evan Cheng2e49f092006-10-11 07:10:22 +00004208 if (LLD->getLoadedVT() == RLD->getLoadedVT()) {
Evan Cheng466685d2006-10-09 20:57:25 +00004209 // FIXME: this conflates two src values, discarding one. This is not
4210 // the right thing to do, but nothing uses srcvalues now. When they do,
4211 // turn SrcValue into a list of locations.
4212 SDOperand Addr;
Chris Lattnerc4e664b2007-01-16 05:59:59 +00004213 if (TheSelect->getOpcode() == ISD::SELECT) {
4214 // Check that the condition doesn't reach either load. If so, folding
4215 // this will induce a cycle into the DAG.
4216 if (!LLD->isPredecessor(TheSelect->getOperand(0).Val) &&
4217 !RLD->isPredecessor(TheSelect->getOperand(0).Val)) {
4218 Addr = DAG.getNode(ISD::SELECT, LLD->getBasePtr().getValueType(),
4219 TheSelect->getOperand(0), LLD->getBasePtr(),
4220 RLD->getBasePtr());
4221 }
4222 } else {
4223 // Check that the condition doesn't reach either load. If so, folding
4224 // this will induce a cycle into the DAG.
4225 if (!LLD->isPredecessor(TheSelect->getOperand(0).Val) &&
4226 !RLD->isPredecessor(TheSelect->getOperand(0).Val) &&
4227 !LLD->isPredecessor(TheSelect->getOperand(1).Val) &&
4228 !RLD->isPredecessor(TheSelect->getOperand(1).Val)) {
4229 Addr = DAG.getNode(ISD::SELECT_CC, LLD->getBasePtr().getValueType(),
Evan Cheng466685d2006-10-09 20:57:25 +00004230 TheSelect->getOperand(0),
4231 TheSelect->getOperand(1),
4232 LLD->getBasePtr(), RLD->getBasePtr(),
4233 TheSelect->getOperand(4));
Chris Lattnerc4e664b2007-01-16 05:59:59 +00004234 }
Evan Cheng466685d2006-10-09 20:57:25 +00004235 }
Chris Lattnerc4e664b2007-01-16 05:59:59 +00004236
4237 if (Addr.Val) {
4238 SDOperand Load;
4239 if (LLD->getExtensionType() == ISD::NON_EXTLOAD)
4240 Load = DAG.getLoad(TheSelect->getValueType(0), LLD->getChain(),
4241 Addr,LLD->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00004242 LLD->getSrcValueOffset(),
4243 LLD->isVolatile(),
4244 LLD->getAlignment());
Chris Lattnerc4e664b2007-01-16 05:59:59 +00004245 else {
4246 Load = DAG.getExtLoad(LLD->getExtensionType(),
4247 TheSelect->getValueType(0),
4248 LLD->getChain(), Addr, LLD->getSrcValue(),
4249 LLD->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00004250 LLD->getLoadedVT(),
4251 LLD->isVolatile(),
4252 LLD->getAlignment());
Chris Lattnerc4e664b2007-01-16 05:59:59 +00004253 }
4254 // Users of the select now use the result of the load.
4255 CombineTo(TheSelect, Load);
4256
4257 // Users of the old loads now use the new load's chain. We know the
4258 // old-load value is dead now.
4259 CombineTo(LHS.Val, Load.getValue(0), Load.getValue(1));
4260 CombineTo(RHS.Val, Load.getValue(0), Load.getValue(1));
4261 return true;
4262 }
Evan Chengc5484282006-10-04 00:56:09 +00004263 }
Chris Lattner40c62d52005-10-18 06:04:22 +00004264 }
4265 }
4266
4267 return false;
4268}
4269
Nate Begeman44728a72005-09-19 22:34:01 +00004270SDOperand DAGCombiner::SimplifySelectCC(SDOperand N0, SDOperand N1,
4271 SDOperand N2, SDOperand N3,
Chris Lattner1eba01e2007-04-11 06:50:51 +00004272 ISD::CondCode CC, bool NotExtCompare) {
Nate Begemanf845b452005-10-08 00:29:44 +00004273
4274 MVT::ValueType VT = N2.getValueType();
Nate Begemanf845b452005-10-08 00:29:44 +00004275 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
4276 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
4277 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
4278
4279 // Determine if the condition we're dealing with is constant
4280 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
Chris Lattner30f73e72006-10-14 03:52:46 +00004281 if (SCC.Val) AddToWorkList(SCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004282 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
4283
4284 // fold select_cc true, x, y -> x
4285 if (SCCC && SCCC->getValue())
4286 return N2;
4287 // fold select_cc false, x, y -> y
4288 if (SCCC && SCCC->getValue() == 0)
4289 return N3;
4290
4291 // Check to see if we can simplify the select into an fabs node
4292 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
4293 // Allow either -0.0 or 0.0
4294 if (CFP->getValue() == 0.0) {
4295 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
4296 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
4297 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
4298 N2 == N3.getOperand(0))
4299 return DAG.getNode(ISD::FABS, VT, N0);
4300
4301 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
4302 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
4303 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
4304 N2.getOperand(0) == N3)
4305 return DAG.getNode(ISD::FABS, VT, N3);
4306 }
4307 }
4308
4309 // Check to see if we can perform the "gzip trick", transforming
4310 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
Chris Lattnere3152e52006-09-20 06:41:35 +00004311 if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT &&
Nate Begemanf845b452005-10-08 00:29:44 +00004312 MVT::isInteger(N0.getValueType()) &&
Chris Lattnere3152e52006-09-20 06:41:35 +00004313 MVT::isInteger(N2.getValueType()) &&
4314 (N1C->isNullValue() || // (a < 0) ? b : 0
4315 (N1C->getValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0
Nate Begemanf845b452005-10-08 00:29:44 +00004316 MVT::ValueType XType = N0.getValueType();
4317 MVT::ValueType AType = N2.getValueType();
4318 if (XType >= AType) {
4319 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00004320 // single-bit constant.
Nate Begemanf845b452005-10-08 00:29:44 +00004321 if (N2C && ((N2C->getValue() & (N2C->getValue()-1)) == 0)) {
4322 unsigned ShCtV = Log2_64(N2C->getValue());
4323 ShCtV = MVT::getSizeInBits(XType)-ShCtV-1;
4324 SDOperand ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
4325 SDOperand Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
Chris Lattner5750df92006-03-01 04:03:14 +00004326 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004327 if (XType > AType) {
4328 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00004329 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004330 }
4331 return DAG.getNode(ISD::AND, AType, Shift, N2);
4332 }
4333 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
4334 DAG.getConstant(MVT::getSizeInBits(XType)-1,
4335 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00004336 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004337 if (XType > AType) {
4338 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00004339 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004340 }
4341 return DAG.getNode(ISD::AND, AType, Shift, N2);
4342 }
4343 }
Nate Begeman07ed4172005-10-10 21:26:48 +00004344
4345 // fold select C, 16, 0 -> shl C, 4
4346 if (N2C && N3C && N3C->isNullValue() && isPowerOf2_64(N2C->getValue()) &&
4347 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) {
Chris Lattner1eba01e2007-04-11 06:50:51 +00004348
4349 // If the caller doesn't want us to simplify this into a zext of a compare,
4350 // don't do it.
4351 if (NotExtCompare && N2C->getValue() == 1)
4352 return SDOperand();
4353
Nate Begeman07ed4172005-10-10 21:26:48 +00004354 // Get a SetCC of the condition
4355 // FIXME: Should probably make sure that setcc is legal if we ever have a
4356 // target where it isn't.
Nate Begemanb0d04a72006-02-18 02:40:58 +00004357 SDOperand Temp, SCC;
Nate Begeman07ed4172005-10-10 21:26:48 +00004358 // cast from setcc result type to select result type
Nate Begemanb0d04a72006-02-18 02:40:58 +00004359 if (AfterLegalize) {
4360 SCC = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
Chris Lattner555d8d62006-12-07 22:36:47 +00004361 if (N2.getValueType() < SCC.getValueType())
4362 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
4363 else
4364 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00004365 } else {
4366 SCC = DAG.getSetCC(MVT::i1, N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00004367 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00004368 }
Chris Lattner5750df92006-03-01 04:03:14 +00004369 AddToWorkList(SCC.Val);
4370 AddToWorkList(Temp.Val);
Chris Lattnerc56a81d2007-04-11 06:43:25 +00004371
4372 if (N2C->getValue() == 1)
4373 return Temp;
Nate Begeman07ed4172005-10-10 21:26:48 +00004374 // shl setcc result by log2 n2c
4375 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
4376 DAG.getConstant(Log2_64(N2C->getValue()),
4377 TLI.getShiftAmountTy()));
4378 }
4379
Nate Begemanf845b452005-10-08 00:29:44 +00004380 // Check to see if this is the equivalent of setcc
4381 // FIXME: Turn all of these into setcc if setcc if setcc is legal
4382 // otherwise, go ahead with the folds.
4383 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getValue() == 1ULL)) {
4384 MVT::ValueType XType = N0.getValueType();
4385 if (TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultTy())) {
4386 SDOperand Res = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
4387 if (Res.getValueType() != VT)
4388 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
4389 return Res;
4390 }
4391
4392 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
4393 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
4394 TLI.isOperationLegal(ISD::CTLZ, XType)) {
4395 SDOperand Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
4396 return DAG.getNode(ISD::SRL, XType, Ctlz,
4397 DAG.getConstant(Log2_32(MVT::getSizeInBits(XType)),
4398 TLI.getShiftAmountTy()));
4399 }
4400 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
4401 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
4402 SDOperand NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
4403 N0);
4404 SDOperand NotN0 = DAG.getNode(ISD::XOR, XType, N0,
4405 DAG.getConstant(~0ULL, XType));
4406 return DAG.getNode(ISD::SRL, XType,
4407 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
4408 DAG.getConstant(MVT::getSizeInBits(XType)-1,
4409 TLI.getShiftAmountTy()));
4410 }
4411 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
4412 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
4413 SDOperand Sign = DAG.getNode(ISD::SRL, XType, N0,
4414 DAG.getConstant(MVT::getSizeInBits(XType)-1,
4415 TLI.getShiftAmountTy()));
4416 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
4417 }
4418 }
4419
4420 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
4421 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
4422 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
Chris Lattner1982ef22007-04-11 05:11:38 +00004423 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1) &&
4424 N2.getOperand(0) == N1 && MVT::isInteger(N0.getValueType())) {
4425 MVT::ValueType XType = N0.getValueType();
4426 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
4427 DAG.getConstant(MVT::getSizeInBits(XType)-1,
4428 TLI.getShiftAmountTy()));
4429 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
4430 AddToWorkList(Shift.Val);
4431 AddToWorkList(Add.Val);
4432 return DAG.getNode(ISD::XOR, XType, Add, Shift);
4433 }
4434 // Check to see if this is an integer abs. select_cc setgt X, -1, X, -X ->
4435 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
4436 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT &&
4437 N0 == N2 && N3.getOpcode() == ISD::SUB && N0 == N3.getOperand(1)) {
4438 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N3.getOperand(0))) {
Nate Begemanf845b452005-10-08 00:29:44 +00004439 MVT::ValueType XType = N0.getValueType();
4440 if (SubC->isNullValue() && MVT::isInteger(XType)) {
4441 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
4442 DAG.getConstant(MVT::getSizeInBits(XType)-1,
Chris Lattner1982ef22007-04-11 05:11:38 +00004443 TLI.getShiftAmountTy()));
Nate Begemanf845b452005-10-08 00:29:44 +00004444 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00004445 AddToWorkList(Shift.Val);
4446 AddToWorkList(Add.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004447 return DAG.getNode(ISD::XOR, XType, Add, Shift);
4448 }
4449 }
4450 }
Chris Lattner1982ef22007-04-11 05:11:38 +00004451
Nate Begeman44728a72005-09-19 22:34:01 +00004452 return SDOperand();
4453}
4454
Evan Chengfa1eb272007-02-08 22:13:59 +00004455/// SimplifySetCC - This is a stub for TargetLowering::SimplifySetCC.
Nate Begeman452d7be2005-09-16 00:54:12 +00004456SDOperand DAGCombiner::SimplifySetCC(MVT::ValueType VT, SDOperand N0,
Nate Begemane17daeb2005-10-05 21:43:42 +00004457 SDOperand N1, ISD::CondCode Cond,
4458 bool foldBooleans) {
Evan Chengfa1eb272007-02-08 22:13:59 +00004459 TargetLowering::DAGCombinerInfo
4460 DagCombineInfo(DAG, !AfterLegalize, false, this);
4461 return TLI.SimplifySetCC(VT, N0, N1, Cond, foldBooleans, DagCombineInfo);
Nate Begeman452d7be2005-09-16 00:54:12 +00004462}
4463
Nate Begeman69575232005-10-20 02:15:44 +00004464/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
4465/// return a DAG expression to select that will generate the same value by
4466/// multiplying by a magic number. See:
4467/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
4468SDOperand DAGCombiner::BuildSDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00004469 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00004470 SDOperand S = TLI.BuildSDIV(N, DAG, &Built);
4471
Andrew Lenharth232c9102006-06-12 16:07:18 +00004472 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00004473 ii != ee; ++ii)
4474 AddToWorkList(*ii);
4475 return S;
Nate Begeman69575232005-10-20 02:15:44 +00004476}
4477
4478/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
4479/// return a DAG expression to select that will generate the same value by
4480/// multiplying by a magic number. See:
4481/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
4482SDOperand DAGCombiner::BuildUDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00004483 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00004484 SDOperand S = TLI.BuildUDIV(N, DAG, &Built);
Nate Begeman69575232005-10-20 02:15:44 +00004485
Andrew Lenharth232c9102006-06-12 16:07:18 +00004486 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00004487 ii != ee; ++ii)
4488 AddToWorkList(*ii);
4489 return S;
Nate Begeman69575232005-10-20 02:15:44 +00004490}
4491
Jim Laskey71382342006-10-07 23:37:56 +00004492/// FindBaseOffset - Return true if base is known not to alias with anything
4493/// but itself. Provides base object and offset as results.
4494static bool FindBaseOffset(SDOperand Ptr, SDOperand &Base, int64_t &Offset) {
4495 // Assume it is a primitive operation.
4496 Base = Ptr; Offset = 0;
4497
4498 // If it's an adding a simple constant then integrate the offset.
4499 if (Base.getOpcode() == ISD::ADD) {
4500 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Base.getOperand(1))) {
4501 Base = Base.getOperand(0);
4502 Offset += C->getValue();
4503 }
4504 }
4505
4506 // If it's any of the following then it can't alias with anything but itself.
4507 return isa<FrameIndexSDNode>(Base) ||
4508 isa<ConstantPoolSDNode>(Base) ||
4509 isa<GlobalAddressSDNode>(Base);
4510}
4511
4512/// isAlias - Return true if there is any possibility that the two addresses
4513/// overlap.
Jim Laskey096c22e2006-10-18 12:29:57 +00004514bool DAGCombiner::isAlias(SDOperand Ptr1, int64_t Size1,
4515 const Value *SrcValue1, int SrcValueOffset1,
4516 SDOperand Ptr2, int64_t Size2,
4517 const Value *SrcValue2, int SrcValueOffset2)
4518{
Jim Laskey71382342006-10-07 23:37:56 +00004519 // If they are the same then they must be aliases.
4520 if (Ptr1 == Ptr2) return true;
4521
4522 // Gather base node and offset information.
4523 SDOperand Base1, Base2;
4524 int64_t Offset1, Offset2;
4525 bool KnownBase1 = FindBaseOffset(Ptr1, Base1, Offset1);
4526 bool KnownBase2 = FindBaseOffset(Ptr2, Base2, Offset2);
4527
4528 // If they have a same base address then...
4529 if (Base1 == Base2) {
4530 // Check to see if the addresses overlap.
4531 return!((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
4532 }
4533
Jim Laskey096c22e2006-10-18 12:29:57 +00004534 // If we know both bases then they can't alias.
4535 if (KnownBase1 && KnownBase2) return false;
4536
Jim Laskey07a27092006-10-18 19:08:31 +00004537 if (CombinerGlobalAA) {
4538 // Use alias analysis information.
4539 int Overlap1 = Size1 + SrcValueOffset1 + Offset1;
4540 int Overlap2 = Size2 + SrcValueOffset2 + Offset2;
4541 AliasAnalysis::AliasResult AAResult =
Jim Laskey096c22e2006-10-18 12:29:57 +00004542 AA.alias(SrcValue1, Overlap1, SrcValue2, Overlap2);
Jim Laskey07a27092006-10-18 19:08:31 +00004543 if (AAResult == AliasAnalysis::NoAlias)
4544 return false;
4545 }
Jim Laskey096c22e2006-10-18 12:29:57 +00004546
4547 // Otherwise we have to assume they alias.
4548 return true;
Jim Laskey71382342006-10-07 23:37:56 +00004549}
4550
4551/// FindAliasInfo - Extracts the relevant alias information from the memory
4552/// node. Returns true if the operand was a load.
Jim Laskey7ca56af2006-10-11 13:47:09 +00004553bool DAGCombiner::FindAliasInfo(SDNode *N,
Jim Laskey096c22e2006-10-18 12:29:57 +00004554 SDOperand &Ptr, int64_t &Size,
4555 const Value *&SrcValue, int &SrcValueOffset) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00004556 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
4557 Ptr = LD->getBasePtr();
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004558 Size = MVT::getSizeInBits(LD->getLoadedVT()) >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00004559 SrcValue = LD->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00004560 SrcValueOffset = LD->getSrcValueOffset();
Jim Laskey71382342006-10-07 23:37:56 +00004561 return true;
Jim Laskey7ca56af2006-10-11 13:47:09 +00004562 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00004563 Ptr = ST->getBasePtr();
Evan Cheng8b2794a2006-10-13 21:14:26 +00004564 Size = MVT::getSizeInBits(ST->getStoredVT()) >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00004565 SrcValue = ST->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00004566 SrcValueOffset = ST->getSrcValueOffset();
Jim Laskey7ca56af2006-10-11 13:47:09 +00004567 } else {
Jim Laskey71382342006-10-07 23:37:56 +00004568 assert(0 && "FindAliasInfo expected a memory operand");
Jim Laskey71382342006-10-07 23:37:56 +00004569 }
4570
4571 return false;
4572}
4573
Jim Laskey6ff23e52006-10-04 16:53:27 +00004574/// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
4575/// looking for aliasing nodes and adding them to the Aliases vector.
Jim Laskeybc588b82006-10-05 15:07:25 +00004576void DAGCombiner::GatherAllAliases(SDNode *N, SDOperand OriginalChain,
Jim Laskey6ff23e52006-10-04 16:53:27 +00004577 SmallVector<SDOperand, 8> &Aliases) {
Jim Laskeybc588b82006-10-05 15:07:25 +00004578 SmallVector<SDOperand, 8> Chains; // List of chains to visit.
Jim Laskey6ff23e52006-10-04 16:53:27 +00004579 std::set<SDNode *> Visited; // Visited node set.
4580
Jim Laskey279f0532006-09-25 16:29:54 +00004581 // Get alias information for node.
4582 SDOperand Ptr;
4583 int64_t Size;
Jim Laskey7ca56af2006-10-11 13:47:09 +00004584 const Value *SrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00004585 int SrcValueOffset;
4586 bool IsLoad = FindAliasInfo(N, Ptr, Size, SrcValue, SrcValueOffset);
Jim Laskey279f0532006-09-25 16:29:54 +00004587
Jim Laskey6ff23e52006-10-04 16:53:27 +00004588 // Starting off.
Jim Laskeybc588b82006-10-05 15:07:25 +00004589 Chains.push_back(OriginalChain);
Jim Laskey6ff23e52006-10-04 16:53:27 +00004590
Jim Laskeybc588b82006-10-05 15:07:25 +00004591 // Look at each chain and determine if it is an alias. If so, add it to the
4592 // aliases list. If not, then continue up the chain looking for the next
4593 // candidate.
4594 while (!Chains.empty()) {
4595 SDOperand Chain = Chains.back();
4596 Chains.pop_back();
Jim Laskey6ff23e52006-10-04 16:53:27 +00004597
Jim Laskeybc588b82006-10-05 15:07:25 +00004598 // Don't bother if we've been before.
4599 if (Visited.find(Chain.Val) != Visited.end()) continue;
4600 Visited.insert(Chain.Val);
4601
4602 switch (Chain.getOpcode()) {
4603 case ISD::EntryToken:
4604 // Entry token is ideal chain operand, but handled in FindBetterChain.
4605 break;
Jim Laskey6ff23e52006-10-04 16:53:27 +00004606
Jim Laskeybc588b82006-10-05 15:07:25 +00004607 case ISD::LOAD:
4608 case ISD::STORE: {
4609 // Get alias information for Chain.
4610 SDOperand OpPtr;
4611 int64_t OpSize;
Jim Laskey7ca56af2006-10-11 13:47:09 +00004612 const Value *OpSrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00004613 int OpSrcValueOffset;
4614 bool IsOpLoad = FindAliasInfo(Chain.Val, OpPtr, OpSize,
4615 OpSrcValue, OpSrcValueOffset);
Jim Laskeybc588b82006-10-05 15:07:25 +00004616
4617 // If chain is alias then stop here.
4618 if (!(IsLoad && IsOpLoad) &&
Jim Laskey096c22e2006-10-18 12:29:57 +00004619 isAlias(Ptr, Size, SrcValue, SrcValueOffset,
4620 OpPtr, OpSize, OpSrcValue, OpSrcValueOffset)) {
Jim Laskeybc588b82006-10-05 15:07:25 +00004621 Aliases.push_back(Chain);
4622 } else {
4623 // Look further up the chain.
4624 Chains.push_back(Chain.getOperand(0));
4625 // Clean up old chain.
4626 AddToWorkList(Chain.Val);
Jim Laskey279f0532006-09-25 16:29:54 +00004627 }
Jim Laskeybc588b82006-10-05 15:07:25 +00004628 break;
4629 }
4630
4631 case ISD::TokenFactor:
4632 // We have to check each of the operands of the token factor, so we queue
4633 // then up. Adding the operands to the queue (stack) in reverse order
4634 // maintains the original order and increases the likelihood that getNode
4635 // will find a matching token factor (CSE.)
4636 for (unsigned n = Chain.getNumOperands(); n;)
4637 Chains.push_back(Chain.getOperand(--n));
4638 // Eliminate the token factor if we can.
4639 AddToWorkList(Chain.Val);
4640 break;
4641
4642 default:
4643 // For all other instructions we will just have to take what we can get.
4644 Aliases.push_back(Chain);
4645 break;
Jim Laskey279f0532006-09-25 16:29:54 +00004646 }
4647 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00004648}
4649
4650/// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, looking
4651/// for a better chain (aliasing node.)
4652SDOperand DAGCombiner::FindBetterChain(SDNode *N, SDOperand OldChain) {
4653 SmallVector<SDOperand, 8> Aliases; // Ops for replacing token factor.
Jim Laskey279f0532006-09-25 16:29:54 +00004654
Jim Laskey6ff23e52006-10-04 16:53:27 +00004655 // Accumulate all the aliases to this node.
4656 GatherAllAliases(N, OldChain, Aliases);
4657
4658 if (Aliases.size() == 0) {
4659 // If no operands then chain to entry token.
4660 return DAG.getEntryNode();
4661 } else if (Aliases.size() == 1) {
4662 // If a single operand then chain to it. We don't need to revisit it.
4663 return Aliases[0];
4664 }
4665
4666 // Construct a custom tailored token factor.
4667 SDOperand NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
4668 &Aliases[0], Aliases.size());
4669
4670 // Make sure the old chain gets cleaned up.
4671 if (NewChain != OldChain) AddToWorkList(OldChain.Val);
4672
4673 return NewChain;
Jim Laskey279f0532006-09-25 16:29:54 +00004674}
4675
Nate Begeman1d4d4142005-09-01 00:19:25 +00004676// SelectionDAG::Combine - This is the entry point for the file.
4677//
Jim Laskeyc7c3f112006-10-16 20:52:31 +00004678void SelectionDAG::Combine(bool RunningAfterLegalize, AliasAnalysis &AA) {
Chris Lattner938ab022007-01-16 04:55:25 +00004679 if (!RunningAfterLegalize && ViewDAGCombine1)
4680 viewGraph();
4681 if (RunningAfterLegalize && ViewDAGCombine2)
4682 viewGraph();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004683 /// run - This is the main entry point to this class.
4684 ///
Jim Laskeyc7c3f112006-10-16 20:52:31 +00004685 DAGCombiner(*this, AA).Run(RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004686}