blob: 3614cb6939c25500c2bc210ad1e56b6a21795878 [file] [log] [blame]
Nate Begeman4ebd8052005-09-01 23:24:04 +00001//===-- DAGCombiner.cpp - Implement a DAG node combiner -------------------===//
Nate Begeman1d4d4142005-09-01 00:19:25 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Nate Begeman and is distributed under the
6// University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This pass combines dag nodes to form fewer, simpler DAG nodes. It can be run
11// both before and after the DAG is legalized.
12//
13// FIXME: Missing folds
14// sdiv, udiv, srem, urem (X, const) where X is an integer can be expanded into
15// a sequence of multiplies, shifts, and adds. This should be controlled by
16// some kind of hint from the target that int div is expensive.
17// various folds of mulh[s,u] by constants such as -1, powers of 2, etc.
18//
Nate Begeman44728a72005-09-19 22:34:01 +000019// FIXME: select C, pow2, pow2 -> something smart
20// FIXME: trunc(select X, Y, Z) -> select X, trunc(Y), trunc(Z)
Nate Begeman44728a72005-09-19 22:34:01 +000021// FIXME: Dead stores -> nuke
Chris Lattner40c62d52005-10-18 06:04:22 +000022// FIXME: shr X, (and Y,31) -> shr X, Y (TRICKY!)
Nate Begeman1d4d4142005-09-01 00:19:25 +000023// FIXME: mul (x, const) -> shifts + adds
Nate Begeman1d4d4142005-09-01 00:19:25 +000024// FIXME: undef values
Nate Begeman646d7e22005-09-02 21:18:40 +000025// FIXME: divide by zero is currently left unfolded. do we want to turn this
26// into an undef?
Nate Begemanf845b452005-10-08 00:29:44 +000027// FIXME: select ne (select cc, 1, 0), 0, true, false -> select cc, true, false
Nate Begeman1d4d4142005-09-01 00:19:25 +000028//
29//===----------------------------------------------------------------------===//
30
31#define DEBUG_TYPE "dagcombine"
Nate Begeman1d4d4142005-09-01 00:19:25 +000032#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattnerc76d4412007-05-16 06:37:59 +000033#include "llvm/Analysis/AliasAnalysis.h"
Evan Cheng59d5b682007-05-07 21:27:48 +000034#include "llvm/Target/TargetData.h"
Nate Begeman1d4d4142005-09-01 00:19:25 +000035#include "llvm/Target/TargetLowering.h"
Evan Cheng59d5b682007-05-07 21:27:48 +000036#include "llvm/Target/TargetMachine.h"
Chris Lattnerddae4bd2007-01-08 23:04:05 +000037#include "llvm/Target/TargetOptions.h"
Chris Lattnerc76d4412007-05-16 06:37:59 +000038#include "llvm/ADT/SmallPtrSet.h"
39#include "llvm/ADT/Statistic.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000040#include "llvm/Support/Compiler.h"
Jim Laskeyd1aed7a2006-09-21 16:28:59 +000041#include "llvm/Support/CommandLine.h"
Chris Lattnerc76d4412007-05-16 06:37:59 +000042#include "llvm/Support/Debug.h"
43#include "llvm/Support/MathExtras.h"
Chris Lattnera500fc62005-09-09 23:53:39 +000044#include <algorithm>
Nate Begeman1d4d4142005-09-01 00:19:25 +000045using namespace llvm;
46
Chris Lattnercd3245a2006-12-19 22:41:21 +000047STATISTIC(NodesCombined , "Number of dag nodes combined");
48STATISTIC(PreIndexedNodes , "Number of pre-indexed nodes created");
49STATISTIC(PostIndexedNodes, "Number of post-indexed nodes created");
50
Nate Begeman1d4d4142005-09-01 00:19:25 +000051namespace {
Chris Lattner938ab022007-01-16 04:55:25 +000052#ifndef NDEBUG
53 static cl::opt<bool>
54 ViewDAGCombine1("view-dag-combine1-dags", cl::Hidden,
55 cl::desc("Pop up a window to show dags before the first "
56 "dag combine pass"));
57 static cl::opt<bool>
58 ViewDAGCombine2("view-dag-combine2-dags", cl::Hidden,
59 cl::desc("Pop up a window to show dags before the second "
60 "dag combine pass"));
61#else
62 static const bool ViewDAGCombine1 = false;
63 static const bool ViewDAGCombine2 = false;
64#endif
65
Jim Laskey71382342006-10-07 23:37:56 +000066 static cl::opt<bool>
67 CombinerAA("combiner-alias-analysis", cl::Hidden,
Jim Laskey26f7fa72006-10-17 19:33:52 +000068 cl::desc("Turn on alias analysis during testing"));
Jim Laskey3ad175b2006-10-12 15:22:24 +000069
Jim Laskey07a27092006-10-18 19:08:31 +000070 static cl::opt<bool>
71 CombinerGlobalAA("combiner-global-alias-analysis", cl::Hidden,
72 cl::desc("Include global information in alias analysis"));
73
Jim Laskeybc588b82006-10-05 15:07:25 +000074//------------------------------ DAGCombiner ---------------------------------//
75
Jim Laskey71382342006-10-07 23:37:56 +000076 class VISIBILITY_HIDDEN DAGCombiner {
Nate Begeman1d4d4142005-09-01 00:19:25 +000077 SelectionDAG &DAG;
78 TargetLowering &TLI;
Nate Begeman4ebd8052005-09-01 23:24:04 +000079 bool AfterLegalize;
Nate Begeman1d4d4142005-09-01 00:19:25 +000080
81 // Worklist of all of the nodes that need to be simplified.
82 std::vector<SDNode*> WorkList;
83
Jim Laskeyc7c3f112006-10-16 20:52:31 +000084 // AA - Used for DAG load/store alias analysis.
85 AliasAnalysis &AA;
86
Nate Begeman1d4d4142005-09-01 00:19:25 +000087 /// AddUsersToWorkList - When an instruction is simplified, add all users of
88 /// the instruction to the work lists because they might get more simplified
89 /// now.
90 ///
91 void AddUsersToWorkList(SDNode *N) {
92 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
Nate Begeman4ebd8052005-09-01 23:24:04 +000093 UI != UE; ++UI)
Jim Laskey6ff23e52006-10-04 16:53:27 +000094 AddToWorkList(*UI);
Nate Begeman1d4d4142005-09-01 00:19:25 +000095 }
96
97 /// removeFromWorkList - remove all instances of N from the worklist.
Chris Lattner5750df92006-03-01 04:03:14 +000098 ///
Nate Begeman1d4d4142005-09-01 00:19:25 +000099 void removeFromWorkList(SDNode *N) {
100 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), N),
101 WorkList.end());
102 }
103
Dan Gohman389079b2007-10-08 17:57:15 +0000104 /// visit - call the node-specific routine that knows how to fold each
105 /// particular type of node.
106 SDOperand visit(SDNode *N);
107
Chris Lattner24664722006-03-01 04:53:38 +0000108 public:
Jim Laskey6ff23e52006-10-04 16:53:27 +0000109 /// AddToWorkList - Add to the work list making sure it's instance is at the
110 /// the back (next to be processed.)
Chris Lattner5750df92006-03-01 04:03:14 +0000111 void AddToWorkList(SDNode *N) {
Jim Laskey6ff23e52006-10-04 16:53:27 +0000112 removeFromWorkList(N);
Chris Lattner5750df92006-03-01 04:03:14 +0000113 WorkList.push_back(N);
114 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000115
Jim Laskey274062c2006-10-13 23:32:28 +0000116 SDOperand CombineTo(SDNode *N, const SDOperand *To, unsigned NumTo,
117 bool AddTo = true) {
Chris Lattner3577e382006-08-11 17:56:38 +0000118 assert(N->getNumValues() == NumTo && "Broken CombineTo call!");
Chris Lattner87514ca2005-10-10 22:31:19 +0000119 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +0000120 DOUT << "\nReplacing.1 "; DEBUG(N->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +0000121 DOUT << "\nWith: "; DEBUG(To[0].Val->dump(&DAG));
122 DOUT << " and " << NumTo-1 << " other values\n";
Chris Lattner01a22022005-10-10 22:04:48 +0000123 std::vector<SDNode*> NowDead;
Chris Lattner3577e382006-08-11 17:56:38 +0000124 DAG.ReplaceAllUsesWith(N, To, &NowDead);
Chris Lattner01a22022005-10-10 22:04:48 +0000125
Jim Laskey274062c2006-10-13 23:32:28 +0000126 if (AddTo) {
127 // Push the new nodes and any users onto the worklist
128 for (unsigned i = 0, e = NumTo; i != e; ++i) {
129 AddToWorkList(To[i].Val);
130 AddUsersToWorkList(To[i].Val);
131 }
Chris Lattner01a22022005-10-10 22:04:48 +0000132 }
133
Jim Laskey6ff23e52006-10-04 16:53:27 +0000134 // Nodes can be reintroduced into the worklist. Make sure we do not
135 // process a node that has been replaced.
Chris Lattner01a22022005-10-10 22:04:48 +0000136 removeFromWorkList(N);
137 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
138 removeFromWorkList(NowDead[i]);
139
140 // Finally, since the node is now dead, remove it from the graph.
141 DAG.DeleteNode(N);
142 return SDOperand(N, 0);
143 }
Nate Begeman368e18d2006-02-16 21:11:51 +0000144
Jim Laskey274062c2006-10-13 23:32:28 +0000145 SDOperand CombineTo(SDNode *N, SDOperand Res, bool AddTo = true) {
146 return CombineTo(N, &Res, 1, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000147 }
148
Jim Laskey274062c2006-10-13 23:32:28 +0000149 SDOperand CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1,
150 bool AddTo = true) {
Chris Lattner3577e382006-08-11 17:56:38 +0000151 SDOperand To[] = { Res0, Res1 };
Jim Laskey274062c2006-10-13 23:32:28 +0000152 return CombineTo(N, To, 2, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000153 }
154 private:
155
Chris Lattner012f2412006-02-17 21:58:01 +0000156 /// SimplifyDemandedBits - Check the specified integer node value to see if
Chris Lattnerb2742f42006-03-01 19:55:35 +0000157 /// it can be simplified or if things it uses can be simplified by bit
Chris Lattner012f2412006-02-17 21:58:01 +0000158 /// propagation. If so, return true.
Chris Lattnere33544c2007-10-13 06:58:48 +0000159 bool SimplifyDemandedBits(SDOperand Op, uint64_t Demanded = ~0ULL) {
Nate Begeman368e18d2006-02-16 21:11:51 +0000160 TargetLowering::TargetLoweringOpt TLO(DAG);
161 uint64_t KnownZero, KnownOne;
Chris Lattnere33544c2007-10-13 06:58:48 +0000162 Demanded &= MVT::getIntVTBitMask(Op.getValueType());
Chris Lattner012f2412006-02-17 21:58:01 +0000163 if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO))
164 return false;
165
166 // Revisit the node.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000167 AddToWorkList(Op.Val);
Chris Lattner012f2412006-02-17 21:58:01 +0000168
169 // Replace the old value with the new one.
170 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +0000171 DOUT << "\nReplacing.2 "; DEBUG(TLO.Old.Val->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +0000172 DOUT << "\nWith: "; DEBUG(TLO.New.Val->dump(&DAG));
173 DOUT << '\n';
Chris Lattner012f2412006-02-17 21:58:01 +0000174
175 std::vector<SDNode*> NowDead;
Chris Lattner01d029b2007-10-15 06:10:22 +0000176 DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New, &NowDead);
Chris Lattner012f2412006-02-17 21:58:01 +0000177
Chris Lattner7d20d392006-02-20 06:51:04 +0000178 // Push the new node and any (possibly new) users onto the worklist.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000179 AddToWorkList(TLO.New.Val);
Chris Lattner012f2412006-02-17 21:58:01 +0000180 AddUsersToWorkList(TLO.New.Val);
181
182 // Nodes can end up on the worklist more than once. Make sure we do
183 // not process a node that has been replaced.
Chris Lattner012f2412006-02-17 21:58:01 +0000184 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
185 removeFromWorkList(NowDead[i]);
186
Chris Lattner7d20d392006-02-20 06:51:04 +0000187 // Finally, if the node is now dead, remove it from the graph. The node
188 // may not be dead if the replacement process recursively simplified to
189 // something else needing this node.
190 if (TLO.Old.Val->use_empty()) {
191 removeFromWorkList(TLO.Old.Val);
Chris Lattnerec06e9a2007-04-18 03:05:22 +0000192
193 // If the operands of this node are only used by the node, they will now
194 // be dead. Make sure to visit them first to delete dead nodes early.
195 for (unsigned i = 0, e = TLO.Old.Val->getNumOperands(); i != e; ++i)
196 if (TLO.Old.Val->getOperand(i).Val->hasOneUse())
197 AddToWorkList(TLO.Old.Val->getOperand(i).Val);
198
Chris Lattner7d20d392006-02-20 06:51:04 +0000199 DAG.DeleteNode(TLO.Old.Val);
200 }
Chris Lattner012f2412006-02-17 21:58:01 +0000201 return true;
Nate Begeman368e18d2006-02-16 21:11:51 +0000202 }
Chris Lattner87514ca2005-10-10 22:31:19 +0000203
Chris Lattner448f2192006-11-11 00:39:41 +0000204 bool CombineToPreIndexedLoadStore(SDNode *N);
205 bool CombineToPostIndexedLoadStore(SDNode *N);
206
207
Dan Gohman389079b2007-10-08 17:57:15 +0000208 /// combine - call the node-specific routine that knows how to fold each
209 /// particular type of node. If that doesn't do anything, try the
210 /// target-specific DAG combines.
211 SDOperand combine(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000212
213 // Visitation implementation - Implement dag node combining for different
214 // node types. The semantics are as follows:
215 // Return Value:
Nate Begeman2300f552005-09-07 00:15:36 +0000216 // SDOperand.Val == 0 - No change was made
Chris Lattner01a22022005-10-10 22:04:48 +0000217 // SDOperand.Val == N - N was replaced, is dead, and is already handled.
Nate Begeman2300f552005-09-07 00:15:36 +0000218 // otherwise - N should be replaced by the returned Operand.
Nate Begeman1d4d4142005-09-01 00:19:25 +0000219 //
Nate Begeman83e75ec2005-09-06 04:43:02 +0000220 SDOperand visitTokenFactor(SDNode *N);
221 SDOperand visitADD(SDNode *N);
222 SDOperand visitSUB(SDNode *N);
Chris Lattner91153682007-03-04 20:03:15 +0000223 SDOperand visitADDC(SDNode *N);
224 SDOperand visitADDE(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000225 SDOperand visitMUL(SDNode *N);
226 SDOperand visitSDIV(SDNode *N);
227 SDOperand visitUDIV(SDNode *N);
228 SDOperand visitSREM(SDNode *N);
229 SDOperand visitUREM(SDNode *N);
230 SDOperand visitMULHU(SDNode *N);
231 SDOperand visitMULHS(SDNode *N);
Dan Gohman389079b2007-10-08 17:57:15 +0000232 SDOperand visitSMUL_LOHI(SDNode *N);
233 SDOperand visitUMUL_LOHI(SDNode *N);
234 SDOperand visitSDIVREM(SDNode *N);
235 SDOperand visitUDIVREM(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000236 SDOperand visitAND(SDNode *N);
237 SDOperand visitOR(SDNode *N);
238 SDOperand visitXOR(SDNode *N);
Dan Gohman7f321562007-06-25 16:23:39 +0000239 SDOperand SimplifyVBinOp(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000240 SDOperand visitSHL(SDNode *N);
241 SDOperand visitSRA(SDNode *N);
242 SDOperand visitSRL(SDNode *N);
243 SDOperand visitCTLZ(SDNode *N);
244 SDOperand visitCTTZ(SDNode *N);
245 SDOperand visitCTPOP(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000246 SDOperand visitSELECT(SDNode *N);
247 SDOperand visitSELECT_CC(SDNode *N);
248 SDOperand visitSETCC(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000249 SDOperand visitSIGN_EXTEND(SDNode *N);
250 SDOperand visitZERO_EXTEND(SDNode *N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000251 SDOperand visitANY_EXTEND(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000252 SDOperand visitSIGN_EXTEND_INREG(SDNode *N);
253 SDOperand visitTRUNCATE(SDNode *N);
Chris Lattner94683772005-12-23 05:30:37 +0000254 SDOperand visitBIT_CONVERT(SDNode *N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000255 SDOperand visitFADD(SDNode *N);
256 SDOperand visitFSUB(SDNode *N);
257 SDOperand visitFMUL(SDNode *N);
258 SDOperand visitFDIV(SDNode *N);
259 SDOperand visitFREM(SDNode *N);
Chris Lattner12d83032006-03-05 05:30:57 +0000260 SDOperand visitFCOPYSIGN(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000261 SDOperand visitSINT_TO_FP(SDNode *N);
262 SDOperand visitUINT_TO_FP(SDNode *N);
263 SDOperand visitFP_TO_SINT(SDNode *N);
264 SDOperand visitFP_TO_UINT(SDNode *N);
265 SDOperand visitFP_ROUND(SDNode *N);
266 SDOperand visitFP_ROUND_INREG(SDNode *N);
267 SDOperand visitFP_EXTEND(SDNode *N);
268 SDOperand visitFNEG(SDNode *N);
269 SDOperand visitFABS(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000270 SDOperand visitBRCOND(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000271 SDOperand visitBR_CC(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000272 SDOperand visitLOAD(SDNode *N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000273 SDOperand visitSTORE(SDNode *N);
Chris Lattnerca242442006-03-19 01:27:56 +0000274 SDOperand visitINSERT_VECTOR_ELT(SDNode *N);
Evan Cheng513da432007-10-06 08:19:55 +0000275 SDOperand visitEXTRACT_VECTOR_ELT(SDNode *N);
Dan Gohman7f321562007-06-25 16:23:39 +0000276 SDOperand visitBUILD_VECTOR(SDNode *N);
277 SDOperand visitCONCAT_VECTORS(SDNode *N);
Chris Lattner66445d32006-03-28 22:11:53 +0000278 SDOperand visitVECTOR_SHUFFLE(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000279
Evan Cheng44f1f092006-04-20 08:56:16 +0000280 SDOperand XformToShuffleWithZero(SDNode *N);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000281 SDOperand ReassociateOps(unsigned Opc, SDOperand LHS, SDOperand RHS);
282
Chris Lattner40c62d52005-10-18 06:04:22 +0000283 bool SimplifySelectOps(SDNode *SELECT, SDOperand LHS, SDOperand RHS);
Chris Lattner35e5c142006-05-05 05:51:50 +0000284 SDOperand SimplifyBinOpWithSameOpcodeHands(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000285 SDOperand SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2);
286 SDOperand SimplifySelectCC(SDOperand N0, SDOperand N1, SDOperand N2,
Chris Lattner1eba01e2007-04-11 06:50:51 +0000287 SDOperand N3, ISD::CondCode CC,
288 bool NotExtCompare = false);
Nate Begeman452d7be2005-09-16 00:54:12 +0000289 SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N0, SDOperand N1,
Nate Begemane17daeb2005-10-05 21:43:42 +0000290 ISD::CondCode Cond, bool foldBooleans = true);
Dan Gohman389079b2007-10-08 17:57:15 +0000291 bool SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp, unsigned HiOp);
Dan Gohman7f321562007-06-25 16:23:39 +0000292 SDOperand ConstantFoldBIT_CONVERTofBUILD_VECTOR(SDNode *, MVT::ValueType);
Nate Begeman69575232005-10-20 02:15:44 +0000293 SDOperand BuildSDIV(SDNode *N);
Chris Lattner516b9622006-09-14 20:50:57 +0000294 SDOperand BuildUDIV(SDNode *N);
295 SDNode *MatchRotate(SDOperand LHS, SDOperand RHS);
Evan Chengc88138f2007-03-22 01:54:19 +0000296 SDOperand ReduceLoadWidth(SDNode *N);
Jim Laskey279f0532006-09-25 16:29:54 +0000297
Chris Lattner2b4c2792007-10-13 06:35:54 +0000298 SDOperand GetDemandedBits(SDOperand V, uint64_t Mask);
299
Jim Laskey6ff23e52006-10-04 16:53:27 +0000300 /// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
301 /// looking for aliasing nodes and adding them to the Aliases vector.
Jim Laskeybc588b82006-10-05 15:07:25 +0000302 void GatherAllAliases(SDNode *N, SDOperand OriginalChain,
Jim Laskey6ff23e52006-10-04 16:53:27 +0000303 SmallVector<SDOperand, 8> &Aliases);
304
Jim Laskey096c22e2006-10-18 12:29:57 +0000305 /// isAlias - Return true if there is any possibility that the two addresses
306 /// overlap.
307 bool isAlias(SDOperand Ptr1, int64_t Size1,
308 const Value *SrcValue1, int SrcValueOffset1,
309 SDOperand Ptr2, int64_t Size2,
Jeff Cohend41b30d2006-11-05 19:31:28 +0000310 const Value *SrcValue2, int SrcValueOffset2);
Jim Laskey096c22e2006-10-18 12:29:57 +0000311
Jim Laskey7ca56af2006-10-11 13:47:09 +0000312 /// FindAliasInfo - Extracts the relevant alias information from the memory
313 /// node. Returns true if the operand was a load.
314 bool FindAliasInfo(SDNode *N,
Jim Laskey096c22e2006-10-18 12:29:57 +0000315 SDOperand &Ptr, int64_t &Size,
316 const Value *&SrcValue, int &SrcValueOffset);
Jim Laskey7ca56af2006-10-11 13:47:09 +0000317
Jim Laskey279f0532006-09-25 16:29:54 +0000318 /// FindBetterChain - Walk up chain skipping non-aliasing memory nodes,
Jim Laskey6ff23e52006-10-04 16:53:27 +0000319 /// looking for a better chain (aliasing node.)
Jim Laskey279f0532006-09-25 16:29:54 +0000320 SDOperand FindBetterChain(SDNode *N, SDOperand Chain);
321
Nate Begeman1d4d4142005-09-01 00:19:25 +0000322public:
Jim Laskeyc7c3f112006-10-16 20:52:31 +0000323 DAGCombiner(SelectionDAG &D, AliasAnalysis &A)
324 : DAG(D),
325 TLI(D.getTargetLoweringInfo()),
326 AfterLegalize(false),
327 AA(A) {}
Nate Begeman1d4d4142005-09-01 00:19:25 +0000328
329 /// Run - runs the dag combiner on all nodes in the work list
Nate Begeman4ebd8052005-09-01 23:24:04 +0000330 void Run(bool RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000331 };
332}
333
Chris Lattner24664722006-03-01 04:53:38 +0000334//===----------------------------------------------------------------------===//
335// TargetLowering::DAGCombinerInfo implementation
336//===----------------------------------------------------------------------===//
337
338void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) {
339 ((DAGCombiner*)DC)->AddToWorkList(N);
340}
341
342SDOperand TargetLowering::DAGCombinerInfo::
343CombineTo(SDNode *N, const std::vector<SDOperand> &To) {
Chris Lattner3577e382006-08-11 17:56:38 +0000344 return ((DAGCombiner*)DC)->CombineTo(N, &To[0], To.size());
Chris Lattner24664722006-03-01 04:53:38 +0000345}
346
347SDOperand TargetLowering::DAGCombinerInfo::
348CombineTo(SDNode *N, SDOperand Res) {
349 return ((DAGCombiner*)DC)->CombineTo(N, Res);
350}
351
352
353SDOperand TargetLowering::DAGCombinerInfo::
354CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1) {
355 return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1);
356}
357
358
Chris Lattner24664722006-03-01 04:53:38 +0000359//===----------------------------------------------------------------------===//
Chris Lattner29446522007-05-14 22:04:50 +0000360// Helper Functions
361//===----------------------------------------------------------------------===//
362
363/// isNegatibleForFree - Return 1 if we can compute the negated form of the
364/// specified expression for the same cost as the expression itself, or 2 if we
365/// can compute the negated form more cheaply than the expression itself.
Chris Lattner501fee72007-05-23 07:35:22 +0000366static char isNegatibleForFree(SDOperand Op, unsigned Depth = 0) {
Chris Lattner29446522007-05-14 22:04:50 +0000367 // fneg is removable even if it has multiple uses.
368 if (Op.getOpcode() == ISD::FNEG) return 2;
369
370 // Don't allow anything with multiple uses.
371 if (!Op.hasOneUse()) return 0;
372
Chris Lattner3adf9512007-05-25 02:19:06 +0000373 // Don't recurse exponentially.
374 if (Depth > 6) return 0;
375
Chris Lattner29446522007-05-14 22:04:50 +0000376 switch (Op.getOpcode()) {
377 default: return false;
378 case ISD::ConstantFP:
379 return 1;
380 case ISD::FADD:
381 // FIXME: determine better conditions for this xform.
382 if (!UnsafeFPMath) return 0;
383
384 // -(A+B) -> -A - B
Chris Lattner501fee72007-05-23 07:35:22 +0000385 if (char V = isNegatibleForFree(Op.getOperand(0), Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000386 return V;
387 // -(A+B) -> -B - A
Chris Lattner501fee72007-05-23 07:35:22 +0000388 return isNegatibleForFree(Op.getOperand(1), Depth+1);
Chris Lattner29446522007-05-14 22:04:50 +0000389 case ISD::FSUB:
390 // We can't turn -(A-B) into B-A when we honor signed zeros.
391 if (!UnsafeFPMath) return 0;
392
393 // -(A-B) -> B-A
394 return 1;
395
396 case ISD::FMUL:
397 case ISD::FDIV:
398 if (HonorSignDependentRoundingFPMath()) return 0;
399
400 // -(X*Y) -> (-X * Y) or (X*-Y)
Chris Lattner501fee72007-05-23 07:35:22 +0000401 if (char V = isNegatibleForFree(Op.getOperand(0), Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000402 return V;
403
Chris Lattner501fee72007-05-23 07:35:22 +0000404 return isNegatibleForFree(Op.getOperand(1), Depth+1);
Chris Lattner29446522007-05-14 22:04:50 +0000405
406 case ISD::FP_EXTEND:
407 case ISD::FP_ROUND:
408 case ISD::FSIN:
Chris Lattner501fee72007-05-23 07:35:22 +0000409 return isNegatibleForFree(Op.getOperand(0), Depth+1);
Chris Lattner29446522007-05-14 22:04:50 +0000410 }
411}
412
413/// GetNegatedExpression - If isNegatibleForFree returns true, this function
414/// returns the newly negated expression.
Chris Lattner3adf9512007-05-25 02:19:06 +0000415static SDOperand GetNegatedExpression(SDOperand Op, SelectionDAG &DAG,
416 unsigned Depth = 0) {
Chris Lattner29446522007-05-14 22:04:50 +0000417 // fneg is removable even if it has multiple uses.
418 if (Op.getOpcode() == ISD::FNEG) return Op.getOperand(0);
419
420 // Don't allow anything with multiple uses.
421 assert(Op.hasOneUse() && "Unknown reuse!");
422
Chris Lattner3adf9512007-05-25 02:19:06 +0000423 assert(Depth <= 6 && "GetNegatedExpression doesn't match isNegatibleForFree");
Chris Lattner29446522007-05-14 22:04:50 +0000424 switch (Op.getOpcode()) {
425 default: assert(0 && "Unknown code");
Dale Johannesenc4dd3c32007-08-31 23:34:27 +0000426 case ISD::ConstantFP: {
427 APFloat V = cast<ConstantFPSDNode>(Op)->getValueAPF();
428 V.changeSign();
429 return DAG.getConstantFP(V, Op.getValueType());
430 }
Chris Lattner29446522007-05-14 22:04:50 +0000431 case ISD::FADD:
432 // FIXME: determine better conditions for this xform.
433 assert(UnsafeFPMath);
434
435 // -(A+B) -> -A - B
Chris Lattner3adf9512007-05-25 02:19:06 +0000436 if (isNegatibleForFree(Op.getOperand(0), Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000437 return DAG.getNode(ISD::FSUB, Op.getValueType(),
Chris Lattner3adf9512007-05-25 02:19:06 +0000438 GetNegatedExpression(Op.getOperand(0), DAG, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000439 Op.getOperand(1));
440 // -(A+B) -> -B - A
441 return DAG.getNode(ISD::FSUB, Op.getValueType(),
Chris Lattner3adf9512007-05-25 02:19:06 +0000442 GetNegatedExpression(Op.getOperand(1), DAG, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000443 Op.getOperand(0));
444 case ISD::FSUB:
445 // We can't turn -(A-B) into B-A when we honor signed zeros.
446 assert(UnsafeFPMath);
Dan Gohman23ff1822007-07-02 15:48:56 +0000447
448 // -(0-B) -> B
449 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(Op.getOperand(0)))
Dale Johannesenc4dd3c32007-08-31 23:34:27 +0000450 if (N0CFP->getValueAPF().isZero())
Dan Gohman23ff1822007-07-02 15:48:56 +0000451 return Op.getOperand(1);
Chris Lattner29446522007-05-14 22:04:50 +0000452
453 // -(A-B) -> B-A
454 return DAG.getNode(ISD::FSUB, Op.getValueType(), Op.getOperand(1),
455 Op.getOperand(0));
456
457 case ISD::FMUL:
458 case ISD::FDIV:
459 assert(!HonorSignDependentRoundingFPMath());
460
461 // -(X*Y) -> -X * Y
Chris Lattner3adf9512007-05-25 02:19:06 +0000462 if (isNegatibleForFree(Op.getOperand(0), Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000463 return DAG.getNode(Op.getOpcode(), Op.getValueType(),
Chris Lattner3adf9512007-05-25 02:19:06 +0000464 GetNegatedExpression(Op.getOperand(0), DAG, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000465 Op.getOperand(1));
466
467 // -(X*Y) -> X * -Y
468 return DAG.getNode(Op.getOpcode(), Op.getValueType(),
469 Op.getOperand(0),
Chris Lattner3adf9512007-05-25 02:19:06 +0000470 GetNegatedExpression(Op.getOperand(1), DAG, Depth+1));
Chris Lattner29446522007-05-14 22:04:50 +0000471
472 case ISD::FP_EXTEND:
473 case ISD::FP_ROUND:
474 case ISD::FSIN:
475 return DAG.getNode(Op.getOpcode(), Op.getValueType(),
Chris Lattner3adf9512007-05-25 02:19:06 +0000476 GetNegatedExpression(Op.getOperand(0), DAG, Depth+1));
Chris Lattner29446522007-05-14 22:04:50 +0000477 }
478}
Chris Lattner24664722006-03-01 04:53:38 +0000479
480
Nate Begeman4ebd8052005-09-01 23:24:04 +0000481// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
482// that selects between the values 1 and 0, making it equivalent to a setcc.
Nate Begeman646d7e22005-09-02 21:18:40 +0000483// Also, set the incoming LHS, RHS, and CC references to the appropriate
484// nodes based on the type of node we are checking. This simplifies life a
485// bit for the callers.
486static bool isSetCCEquivalent(SDOperand N, SDOperand &LHS, SDOperand &RHS,
487 SDOperand &CC) {
488 if (N.getOpcode() == ISD::SETCC) {
489 LHS = N.getOperand(0);
490 RHS = N.getOperand(1);
491 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000492 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000493 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000494 if (N.getOpcode() == ISD::SELECT_CC &&
495 N.getOperand(2).getOpcode() == ISD::Constant &&
496 N.getOperand(3).getOpcode() == ISD::Constant &&
497 cast<ConstantSDNode>(N.getOperand(2))->getValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000498 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
499 LHS = N.getOperand(0);
500 RHS = N.getOperand(1);
501 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000502 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000503 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000504 return false;
505}
506
Nate Begeman99801192005-09-07 23:25:52 +0000507// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
508// one use. If this is true, it allows the users to invert the operation for
509// free when it is profitable to do so.
510static bool isOneUseSetCC(SDOperand N) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000511 SDOperand N0, N1, N2;
Nate Begeman646d7e22005-09-02 21:18:40 +0000512 if (isSetCCEquivalent(N, N0, N1, N2) && N.Val->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000513 return true;
514 return false;
515}
516
Nate Begemancd4d58c2006-02-03 06:46:56 +0000517SDOperand DAGCombiner::ReassociateOps(unsigned Opc, SDOperand N0, SDOperand N1){
518 MVT::ValueType VT = N0.getValueType();
519 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
520 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
521 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
522 if (isa<ConstantSDNode>(N1)) {
523 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(1), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000524 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000525 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(0));
526 } else if (N0.hasOneUse()) {
527 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(0), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000528 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000529 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(1));
530 }
531 }
532 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
533 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
534 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
535 if (isa<ConstantSDNode>(N0)) {
536 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(1), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000537 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000538 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(0));
539 } else if (N1.hasOneUse()) {
540 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(0), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000541 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000542 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(1));
543 }
544 }
545 return SDOperand();
546}
547
Chris Lattner29446522007-05-14 22:04:50 +0000548//===----------------------------------------------------------------------===//
549// Main DAG Combiner implementation
550//===----------------------------------------------------------------------===//
551
Nate Begeman4ebd8052005-09-01 23:24:04 +0000552void DAGCombiner::Run(bool RunningAfterLegalize) {
553 // set the instance variable, so that the various visit routines may use it.
554 AfterLegalize = RunningAfterLegalize;
555
Nate Begeman646d7e22005-09-02 21:18:40 +0000556 // Add all the dag nodes to the worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000557 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
558 E = DAG.allnodes_end(); I != E; ++I)
559 WorkList.push_back(I);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000560
Chris Lattner95038592005-10-05 06:35:28 +0000561 // Create a dummy node (which is not added to allnodes), that adds a reference
562 // to the root node, preventing it from being deleted, and tracking any
563 // changes of the root.
564 HandleSDNode Dummy(DAG.getRoot());
565
Jim Laskey26f7fa72006-10-17 19:33:52 +0000566 // The root of the dag may dangle to deleted nodes until the dag combiner is
567 // done. Set it to null to avoid confusion.
568 DAG.setRoot(SDOperand());
Chris Lattner24664722006-03-01 04:53:38 +0000569
Nate Begeman1d4d4142005-09-01 00:19:25 +0000570 // while the worklist isn't empty, inspect the node on the end of it and
571 // try and combine it.
572 while (!WorkList.empty()) {
573 SDNode *N = WorkList.back();
574 WorkList.pop_back();
575
576 // If N has no uses, it is dead. Make sure to revisit all N's operands once
Chris Lattner95038592005-10-05 06:35:28 +0000577 // N is deleted from the DAG, since they too may now be dead or may have a
578 // reduced number of uses, allowing other xforms.
579 if (N->use_empty() && N != &Dummy) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000580 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Jim Laskey6ff23e52006-10-04 16:53:27 +0000581 AddToWorkList(N->getOperand(i).Val);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000582
Chris Lattner95038592005-10-05 06:35:28 +0000583 DAG.DeleteNode(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000584 continue;
585 }
586
Dan Gohman389079b2007-10-08 17:57:15 +0000587 SDOperand RV = combine(N);
Chris Lattner24664722006-03-01 04:53:38 +0000588
Nate Begeman83e75ec2005-09-06 04:43:02 +0000589 if (RV.Val) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000590 ++NodesCombined;
Nate Begeman646d7e22005-09-02 21:18:40 +0000591 // If we get back the same node we passed in, rather than a new node or
592 // zero, we know that the node must have defined multiple values and
593 // CombineTo was used. Since CombineTo takes care of the worklist
594 // mechanics for us, we have no work to do in this case.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000595 if (RV.Val != N) {
Chris Lattner729c6d12006-05-27 00:43:02 +0000596 assert(N->getOpcode() != ISD::DELETED_NODE &&
597 RV.Val->getOpcode() != ISD::DELETED_NODE &&
598 "Node was deleted but visit returned new node!");
599
Dan Gohmanb5bec2b2007-06-19 14:13:56 +0000600 DOUT << "\nReplacing.3 "; DEBUG(N->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +0000601 DOUT << "\nWith: "; DEBUG(RV.Val->dump(&DAG));
602 DOUT << '\n';
Chris Lattner01a22022005-10-10 22:04:48 +0000603 std::vector<SDNode*> NowDead;
Evan Cheng2adffa12006-09-21 19:04:05 +0000604 if (N->getNumValues() == RV.Val->getNumValues())
605 DAG.ReplaceAllUsesWith(N, RV.Val, &NowDead);
606 else {
607 assert(N->getValueType(0) == RV.getValueType() && "Type mismatch");
608 SDOperand OpV = RV;
609 DAG.ReplaceAllUsesWith(N, &OpV, &NowDead);
610 }
Nate Begeman646d7e22005-09-02 21:18:40 +0000611
612 // Push the new node and any users onto the worklist
Jim Laskey6ff23e52006-10-04 16:53:27 +0000613 AddToWorkList(RV.Val);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000614 AddUsersToWorkList(RV.Val);
Nate Begeman646d7e22005-09-02 21:18:40 +0000615
Jim Laskey6ff23e52006-10-04 16:53:27 +0000616 // Nodes can be reintroduced into the worklist. Make sure we do not
617 // process a node that has been replaced.
Nate Begeman646d7e22005-09-02 21:18:40 +0000618 removeFromWorkList(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000619 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
620 removeFromWorkList(NowDead[i]);
Chris Lattner5c46f742005-10-05 06:11:08 +0000621
622 // Finally, since the node is now dead, remove it from the graph.
623 DAG.DeleteNode(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000624 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000625 }
626 }
Chris Lattner95038592005-10-05 06:35:28 +0000627
628 // If the root changed (e.g. it was a dead load, update the root).
629 DAG.setRoot(Dummy.getValue());
Nate Begeman1d4d4142005-09-01 00:19:25 +0000630}
631
Nate Begeman83e75ec2005-09-06 04:43:02 +0000632SDOperand DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000633 switch(N->getOpcode()) {
634 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +0000635 case ISD::TokenFactor: return visitTokenFactor(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000636 case ISD::ADD: return visitADD(N);
637 case ISD::SUB: return visitSUB(N);
Chris Lattner91153682007-03-04 20:03:15 +0000638 case ISD::ADDC: return visitADDC(N);
639 case ISD::ADDE: return visitADDE(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000640 case ISD::MUL: return visitMUL(N);
641 case ISD::SDIV: return visitSDIV(N);
642 case ISD::UDIV: return visitUDIV(N);
643 case ISD::SREM: return visitSREM(N);
644 case ISD::UREM: return visitUREM(N);
645 case ISD::MULHU: return visitMULHU(N);
646 case ISD::MULHS: return visitMULHS(N);
Dan Gohman389079b2007-10-08 17:57:15 +0000647 case ISD::SMUL_LOHI: return visitSMUL_LOHI(N);
648 case ISD::UMUL_LOHI: return visitUMUL_LOHI(N);
649 case ISD::SDIVREM: return visitSDIVREM(N);
650 case ISD::UDIVREM: return visitUDIVREM(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000651 case ISD::AND: return visitAND(N);
652 case ISD::OR: return visitOR(N);
653 case ISD::XOR: return visitXOR(N);
654 case ISD::SHL: return visitSHL(N);
655 case ISD::SRA: return visitSRA(N);
656 case ISD::SRL: return visitSRL(N);
657 case ISD::CTLZ: return visitCTLZ(N);
658 case ISD::CTTZ: return visitCTTZ(N);
659 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000660 case ISD::SELECT: return visitSELECT(N);
661 case ISD::SELECT_CC: return visitSELECT_CC(N);
662 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000663 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
664 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000665 case ISD::ANY_EXTEND: return visitANY_EXTEND(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000666 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
667 case ISD::TRUNCATE: return visitTRUNCATE(N);
Chris Lattner94683772005-12-23 05:30:37 +0000668 case ISD::BIT_CONVERT: return visitBIT_CONVERT(N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000669 case ISD::FADD: return visitFADD(N);
670 case ISD::FSUB: return visitFSUB(N);
671 case ISD::FMUL: return visitFMUL(N);
672 case ISD::FDIV: return visitFDIV(N);
673 case ISD::FREM: return visitFREM(N);
Chris Lattner12d83032006-03-05 05:30:57 +0000674 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000675 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
676 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
677 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
678 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
679 case ISD::FP_ROUND: return visitFP_ROUND(N);
680 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
681 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
682 case ISD::FNEG: return visitFNEG(N);
683 case ISD::FABS: return visitFABS(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000684 case ISD::BRCOND: return visitBRCOND(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000685 case ISD::BR_CC: return visitBR_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000686 case ISD::LOAD: return visitLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000687 case ISD::STORE: return visitSTORE(N);
Chris Lattnerca242442006-03-19 01:27:56 +0000688 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
Evan Cheng513da432007-10-06 08:19:55 +0000689 case ISD::EXTRACT_VECTOR_ELT: return visitEXTRACT_VECTOR_ELT(N);
Dan Gohman7f321562007-06-25 16:23:39 +0000690 case ISD::BUILD_VECTOR: return visitBUILD_VECTOR(N);
691 case ISD::CONCAT_VECTORS: return visitCONCAT_VECTORS(N);
Chris Lattner66445d32006-03-28 22:11:53 +0000692 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000693 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000694 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000695}
696
Dan Gohman389079b2007-10-08 17:57:15 +0000697SDOperand DAGCombiner::combine(SDNode *N) {
698
699 SDOperand RV = visit(N);
700
701 // If nothing happened, try a target-specific DAG combine.
702 if (RV.Val == 0) {
703 assert(N->getOpcode() != ISD::DELETED_NODE &&
704 "Node was deleted but visit returned NULL!");
705
706 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
707 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode())) {
708
709 // Expose the DAG combiner to the target combiner impls.
710 TargetLowering::DAGCombinerInfo
711 DagCombineInfo(DAG, !AfterLegalize, false, this);
712
713 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
714 }
715 }
716
717 return RV;
718}
719
Chris Lattner6270f682006-10-08 22:57:01 +0000720/// getInputChainForNode - Given a node, return its input chain if it has one,
721/// otherwise return a null sd operand.
722static SDOperand getInputChainForNode(SDNode *N) {
723 if (unsigned NumOps = N->getNumOperands()) {
724 if (N->getOperand(0).getValueType() == MVT::Other)
725 return N->getOperand(0);
726 else if (N->getOperand(NumOps-1).getValueType() == MVT::Other)
727 return N->getOperand(NumOps-1);
728 for (unsigned i = 1; i < NumOps-1; ++i)
729 if (N->getOperand(i).getValueType() == MVT::Other)
730 return N->getOperand(i);
731 }
732 return SDOperand(0, 0);
733}
734
Nate Begeman83e75ec2005-09-06 04:43:02 +0000735SDOperand DAGCombiner::visitTokenFactor(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +0000736 // If N has two operands, where one has an input chain equal to the other,
737 // the 'other' chain is redundant.
738 if (N->getNumOperands() == 2) {
739 if (getInputChainForNode(N->getOperand(0).Val) == N->getOperand(1))
740 return N->getOperand(0);
741 if (getInputChainForNode(N->getOperand(1).Val) == N->getOperand(0))
742 return N->getOperand(1);
743 }
744
Chris Lattnerc76d4412007-05-16 06:37:59 +0000745 SmallVector<SDNode *, 8> TFs; // List of token factors to visit.
746 SmallVector<SDOperand, 8> Ops; // Ops for replacing token factor.
747 SmallPtrSet<SDNode*, 16> SeenOps;
748 bool Changed = false; // If we should replace this token factor.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000749
750 // Start out with this token factor.
Jim Laskey279f0532006-09-25 16:29:54 +0000751 TFs.push_back(N);
Jim Laskey279f0532006-09-25 16:29:54 +0000752
Jim Laskey71382342006-10-07 23:37:56 +0000753 // Iterate through token factors. The TFs grows when new token factors are
Jim Laskeybc588b82006-10-05 15:07:25 +0000754 // encountered.
755 for (unsigned i = 0; i < TFs.size(); ++i) {
756 SDNode *TF = TFs[i];
757
Jim Laskey6ff23e52006-10-04 16:53:27 +0000758 // Check each of the operands.
759 for (unsigned i = 0, ie = TF->getNumOperands(); i != ie; ++i) {
760 SDOperand Op = TF->getOperand(i);
Jim Laskey279f0532006-09-25 16:29:54 +0000761
Jim Laskey6ff23e52006-10-04 16:53:27 +0000762 switch (Op.getOpcode()) {
763 case ISD::EntryToken:
Jim Laskeybc588b82006-10-05 15:07:25 +0000764 // Entry tokens don't need to be added to the list. They are
765 // rededundant.
766 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000767 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000768
Jim Laskey6ff23e52006-10-04 16:53:27 +0000769 case ISD::TokenFactor:
Jim Laskeybc588b82006-10-05 15:07:25 +0000770 if ((CombinerAA || Op.hasOneUse()) &&
771 std::find(TFs.begin(), TFs.end(), Op.Val) == TFs.end()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +0000772 // Queue up for processing.
773 TFs.push_back(Op.Val);
774 // Clean up in case the token factor is removed.
775 AddToWorkList(Op.Val);
776 Changed = true;
777 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000778 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000779 // Fall thru
780
781 default:
Chris Lattnerc76d4412007-05-16 06:37:59 +0000782 // Only add if it isn't already in the list.
783 if (SeenOps.insert(Op.Val))
Jim Laskeybc588b82006-10-05 15:07:25 +0000784 Ops.push_back(Op);
Chris Lattnerc76d4412007-05-16 06:37:59 +0000785 else
786 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000787 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000788 }
789 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000790 }
791
792 SDOperand Result;
793
794 // If we've change things around then replace token factor.
795 if (Changed) {
796 if (Ops.size() == 0) {
797 // The entry token is the only possible outcome.
798 Result = DAG.getEntryNode();
799 } else {
800 // New and improved token factor.
801 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0], Ops.size());
Nate Begemanded49632005-10-13 03:11:28 +0000802 }
Jim Laskey274062c2006-10-13 23:32:28 +0000803
804 // Don't add users to work list.
805 return CombineTo(N, Result, false);
Nate Begemanded49632005-10-13 03:11:28 +0000806 }
Jim Laskey279f0532006-09-25 16:29:54 +0000807
Jim Laskey6ff23e52006-10-04 16:53:27 +0000808 return Result;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000809}
810
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000811static
812SDOperand combineShlAddConstant(SDOperand N0, SDOperand N1, SelectionDAG &DAG) {
813 MVT::ValueType VT = N0.getValueType();
814 SDOperand N00 = N0.getOperand(0);
815 SDOperand N01 = N0.getOperand(1);
816 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N01);
817 if (N01C && N00.getOpcode() == ISD::ADD && N00.Val->hasOneUse() &&
818 isa<ConstantSDNode>(N00.getOperand(1))) {
819 N0 = DAG.getNode(ISD::ADD, VT,
820 DAG.getNode(ISD::SHL, VT, N00.getOperand(0), N01),
821 DAG.getNode(ISD::SHL, VT, N00.getOperand(1), N01));
822 return DAG.getNode(ISD::ADD, VT, N0, N1);
823 }
824 return SDOperand();
825}
826
Evan Chengb13cdbd2007-06-21 07:39:16 +0000827static
828SDOperand combineSelectAndUse(SDNode *N, SDOperand Slct, SDOperand OtherOp,
829 SelectionDAG &DAG) {
830 MVT::ValueType VT = N->getValueType(0);
831 unsigned Opc = N->getOpcode();
832 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
833 SDOperand LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
834 SDOperand RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
835 ISD::CondCode CC = ISD::SETCC_INVALID;
836 if (isSlctCC)
837 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
838 else {
839 SDOperand CCOp = Slct.getOperand(0);
840 if (CCOp.getOpcode() == ISD::SETCC)
841 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
842 }
843
844 bool DoXform = false;
845 bool InvCC = false;
846 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
847 "Bad input!");
848 if (LHS.getOpcode() == ISD::Constant &&
849 cast<ConstantSDNode>(LHS)->isNullValue())
850 DoXform = true;
851 else if (CC != ISD::SETCC_INVALID &&
852 RHS.getOpcode() == ISD::Constant &&
853 cast<ConstantSDNode>(RHS)->isNullValue()) {
854 std::swap(LHS, RHS);
855 bool isInt = MVT::isInteger(isSlctCC ? Slct.getOperand(0).getValueType()
856 : Slct.getOperand(0).getOperand(0).getValueType());
857 CC = ISD::getSetCCInverse(CC, isInt);
858 DoXform = true;
859 InvCC = true;
860 }
861
862 if (DoXform) {
863 SDOperand Result = DAG.getNode(Opc, VT, OtherOp, RHS);
864 if (isSlctCC)
865 return DAG.getSelectCC(OtherOp, Result,
866 Slct.getOperand(0), Slct.getOperand(1), CC);
867 SDOperand CCOp = Slct.getOperand(0);
868 if (InvCC)
869 CCOp = DAG.getSetCC(CCOp.getValueType(), CCOp.getOperand(0),
870 CCOp.getOperand(1), CC);
871 return DAG.getNode(ISD::SELECT, VT, CCOp, OtherOp, Result);
872 }
873 return SDOperand();
874}
875
Nate Begeman83e75ec2005-09-06 04:43:02 +0000876SDOperand DAGCombiner::visitADD(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000877 SDOperand N0 = N->getOperand(0);
878 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000879 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
880 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemanf89d78d2005-09-07 16:09:19 +0000881 MVT::ValueType VT = N0.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +0000882
883 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +0000884 if (MVT::isVector(VT)) {
885 SDOperand FoldedVOp = SimplifyVBinOp(N);
886 if (FoldedVOp.Val) return FoldedVOp;
887 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000888
Dan Gohman613e0d82007-07-03 14:03:57 +0000889 // fold (add x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +0000890 if (N0.getOpcode() == ISD::UNDEF)
891 return N0;
892 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +0000893 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000894 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000895 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000896 return DAG.getNode(ISD::ADD, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000897 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000898 if (N0C && !N1C)
899 return DAG.getNode(ISD::ADD, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000900 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000901 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000902 return N0;
Chris Lattner4aafb4f2006-01-12 20:22:43 +0000903 // fold ((c1-A)+c2) -> (c1+c2)-A
904 if (N1C && N0.getOpcode() == ISD::SUB)
905 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
906 return DAG.getNode(ISD::SUB, VT,
907 DAG.getConstant(N1C->getValue()+N0C->getValue(), VT),
908 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000909 // reassociate add
910 SDOperand RADD = ReassociateOps(ISD::ADD, N0, N1);
911 if (RADD.Val != 0)
912 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000913 // fold ((0-A) + B) -> B-A
914 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
915 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000916 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000917 // fold (A + (0-B)) -> A-B
918 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
919 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000920 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +0000921 // fold (A+(B-A)) -> B
922 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000923 return N1.getOperand(0);
Chris Lattner947c2892006-03-13 06:51:27 +0000924
Evan Cheng860771d2006-03-01 01:09:54 +0000925 if (!MVT::isVector(VT) && SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +0000926 return SDOperand(N, 0);
Chris Lattner947c2892006-03-13 06:51:27 +0000927
928 // fold (a+b) -> (a|b) iff a and b share no bits.
929 if (MVT::isInteger(VT) && !MVT::isVector(VT)) {
930 uint64_t LHSZero, LHSOne;
931 uint64_t RHSZero, RHSOne;
932 uint64_t Mask = MVT::getIntVTBitMask(VT);
Dan Gohmanea859be2007-06-22 14:59:07 +0000933 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Chris Lattner947c2892006-03-13 06:51:27 +0000934 if (LHSZero) {
Dan Gohmanea859be2007-06-22 14:59:07 +0000935 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
Chris Lattner947c2892006-03-13 06:51:27 +0000936
937 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
938 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
939 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
940 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
941 return DAG.getNode(ISD::OR, VT, N0, N1);
942 }
943 }
Evan Cheng3ef554d2006-11-06 08:14:30 +0000944
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000945 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
946 if (N0.getOpcode() == ISD::SHL && N0.Val->hasOneUse()) {
947 SDOperand Result = combineShlAddConstant(N0, N1, DAG);
948 if (Result.Val) return Result;
949 }
950 if (N1.getOpcode() == ISD::SHL && N1.Val->hasOneUse()) {
951 SDOperand Result = combineShlAddConstant(N1, N0, DAG);
952 if (Result.Val) return Result;
953 }
954
Evan Chengb13cdbd2007-06-21 07:39:16 +0000955 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
956 if (N0.getOpcode() == ISD::SELECT && N0.Val->hasOneUse()) {
957 SDOperand Result = combineSelectAndUse(N, N0, N1, DAG);
958 if (Result.Val) return Result;
959 }
960 if (N1.getOpcode() == ISD::SELECT && N1.Val->hasOneUse()) {
961 SDOperand Result = combineSelectAndUse(N, N1, N0, DAG);
962 if (Result.Val) return Result;
963 }
964
Nate Begeman83e75ec2005-09-06 04:43:02 +0000965 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000966}
967
Chris Lattner91153682007-03-04 20:03:15 +0000968SDOperand DAGCombiner::visitADDC(SDNode *N) {
969 SDOperand N0 = N->getOperand(0);
970 SDOperand N1 = N->getOperand(1);
971 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
972 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
973 MVT::ValueType VT = N0.getValueType();
974
975 // If the flag result is dead, turn this into an ADD.
976 if (N->hasNUsesOfValue(0, 1))
977 return CombineTo(N, DAG.getNode(ISD::ADD, VT, N1, N0),
Chris Lattnerb6541762007-03-04 20:40:38 +0000978 DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
Chris Lattner91153682007-03-04 20:03:15 +0000979
980 // canonicalize constant to RHS.
Chris Lattnerbcf24842007-03-04 20:08:45 +0000981 if (N0C && !N1C) {
982 SDOperand Ops[] = { N1, N0 };
983 return DAG.getNode(ISD::ADDC, N->getVTList(), Ops, 2);
984 }
Chris Lattner91153682007-03-04 20:03:15 +0000985
Chris Lattnerb6541762007-03-04 20:40:38 +0000986 // fold (addc x, 0) -> x + no carry out
987 if (N1C && N1C->isNullValue())
988 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
989
990 // fold (addc a, b) -> (or a, b), CARRY_FALSE iff a and b share no bits.
991 uint64_t LHSZero, LHSOne;
992 uint64_t RHSZero, RHSOne;
993 uint64_t Mask = MVT::getIntVTBitMask(VT);
Dan Gohmanea859be2007-06-22 14:59:07 +0000994 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Chris Lattnerb6541762007-03-04 20:40:38 +0000995 if (LHSZero) {
Dan Gohmanea859be2007-06-22 14:59:07 +0000996 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
Chris Lattnerb6541762007-03-04 20:40:38 +0000997
998 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
999 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
1000 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
1001 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
1002 return CombineTo(N, DAG.getNode(ISD::OR, VT, N0, N1),
1003 DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
1004 }
Chris Lattner91153682007-03-04 20:03:15 +00001005
1006 return SDOperand();
1007}
1008
1009SDOperand DAGCombiner::visitADDE(SDNode *N) {
1010 SDOperand N0 = N->getOperand(0);
1011 SDOperand N1 = N->getOperand(1);
Chris Lattnerb6541762007-03-04 20:40:38 +00001012 SDOperand CarryIn = N->getOperand(2);
Chris Lattner91153682007-03-04 20:03:15 +00001013 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1014 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Chris Lattnerbcf24842007-03-04 20:08:45 +00001015 //MVT::ValueType VT = N0.getValueType();
Chris Lattner91153682007-03-04 20:03:15 +00001016
1017 // canonicalize constant to RHS
Chris Lattnerbcf24842007-03-04 20:08:45 +00001018 if (N0C && !N1C) {
Chris Lattnerb6541762007-03-04 20:40:38 +00001019 SDOperand Ops[] = { N1, N0, CarryIn };
Chris Lattnerbcf24842007-03-04 20:08:45 +00001020 return DAG.getNode(ISD::ADDE, N->getVTList(), Ops, 3);
1021 }
Chris Lattner91153682007-03-04 20:03:15 +00001022
Chris Lattnerb6541762007-03-04 20:40:38 +00001023 // fold (adde x, y, false) -> (addc x, y)
1024 if (CarryIn.getOpcode() == ISD::CARRY_FALSE) {
1025 SDOperand Ops[] = { N1, N0 };
1026 return DAG.getNode(ISD::ADDC, N->getVTList(), Ops, 2);
1027 }
Chris Lattner91153682007-03-04 20:03:15 +00001028
1029 return SDOperand();
1030}
1031
1032
1033
Nate Begeman83e75ec2005-09-06 04:43:02 +00001034SDOperand DAGCombiner::visitSUB(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001035 SDOperand N0 = N->getOperand(0);
1036 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001037 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1038 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +00001039 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001040
Dan Gohman7f321562007-06-25 16:23:39 +00001041 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00001042 if (MVT::isVector(VT)) {
1043 SDOperand FoldedVOp = SimplifyVBinOp(N);
1044 if (FoldedVOp.Val) return FoldedVOp;
1045 }
Dan Gohman7f321562007-06-25 16:23:39 +00001046
Chris Lattner854077d2005-10-17 01:07:11 +00001047 // fold (sub x, x) -> 0
1048 if (N0 == N1)
1049 return DAG.getConstant(0, N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001050 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001051 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001052 return DAG.getNode(ISD::SUB, VT, N0, N1);
Chris Lattner05b57432005-10-11 06:07:15 +00001053 // fold (sub x, c) -> (add x, -c)
1054 if (N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001055 return DAG.getNode(ISD::ADD, VT, N0, DAG.getConstant(-N1C->getValue(), VT));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001056 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +00001057 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001058 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001059 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +00001060 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001061 return N0.getOperand(0);
Evan Chengb13cdbd2007-06-21 07:39:16 +00001062 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
1063 if (N1.getOpcode() == ISD::SELECT && N1.Val->hasOneUse()) {
1064 SDOperand Result = combineSelectAndUse(N, N1, N0, DAG);
1065 if (Result.Val) return Result;
1066 }
Dan Gohman613e0d82007-07-03 14:03:57 +00001067 // If either operand of a sub is undef, the result is undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00001068 if (N0.getOpcode() == ISD::UNDEF)
1069 return N0;
1070 if (N1.getOpcode() == ISD::UNDEF)
1071 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001072
Nate Begeman83e75ec2005-09-06 04:43:02 +00001073 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001074}
1075
Nate Begeman83e75ec2005-09-06 04:43:02 +00001076SDOperand DAGCombiner::visitMUL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001077 SDOperand N0 = N->getOperand(0);
1078 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001079 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1080 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman223df222005-09-08 20:18:10 +00001081 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001082
Dan Gohman7f321562007-06-25 16:23:39 +00001083 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00001084 if (MVT::isVector(VT)) {
1085 SDOperand FoldedVOp = SimplifyVBinOp(N);
1086 if (FoldedVOp.Val) return FoldedVOp;
1087 }
Dan Gohman7f321562007-06-25 16:23:39 +00001088
Dan Gohman613e0d82007-07-03 14:03:57 +00001089 // fold (mul x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001090 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001091 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001092 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001093 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001094 return DAG.getNode(ISD::MUL, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001095 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001096 if (N0C && !N1C)
1097 return DAG.getNode(ISD::MUL, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001098 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001099 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001100 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001101 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +00001102 if (N1C && N1C->isAllOnesValue())
Nate Begeman405e3ec2005-10-21 00:02:42 +00001103 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001104 // fold (mul x, (1 << c)) -> x << c
Nate Begeman646d7e22005-09-02 21:18:40 +00001105 if (N1C && isPowerOf2_64(N1C->getValue()))
Chris Lattner3e6099b2005-10-30 06:41:49 +00001106 return DAG.getNode(ISD::SHL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +00001107 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001108 TLI.getShiftAmountTy()));
Chris Lattner3e6099b2005-10-30 06:41:49 +00001109 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
1110 if (N1C && isPowerOf2_64(-N1C->getSignExtended())) {
1111 // FIXME: If the input is something that is easily negated (e.g. a
1112 // single-use add), we should put the negate there.
1113 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT),
1114 DAG.getNode(ISD::SHL, VT, N0,
1115 DAG.getConstant(Log2_64(-N1C->getSignExtended()),
1116 TLI.getShiftAmountTy())));
1117 }
Andrew Lenharth50a0d422006-04-02 21:42:45 +00001118
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001119 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
1120 if (N1C && N0.getOpcode() == ISD::SHL &&
1121 isa<ConstantSDNode>(N0.getOperand(1))) {
1122 SDOperand C3 = DAG.getNode(ISD::SHL, VT, N1, N0.getOperand(1));
Chris Lattner5750df92006-03-01 04:03:14 +00001123 AddToWorkList(C3.Val);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001124 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0), C3);
1125 }
1126
1127 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
1128 // use.
1129 {
1130 SDOperand Sh(0,0), Y(0,0);
1131 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
1132 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
1133 N0.Val->hasOneUse()) {
1134 Sh = N0; Y = N1;
1135 } else if (N1.getOpcode() == ISD::SHL &&
1136 isa<ConstantSDNode>(N1.getOperand(1)) && N1.Val->hasOneUse()) {
1137 Sh = N1; Y = N0;
1138 }
1139 if (Sh.Val) {
1140 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Sh.getOperand(0), Y);
1141 return DAG.getNode(ISD::SHL, VT, Mul, Sh.getOperand(1));
1142 }
1143 }
Chris Lattnera1deca32006-03-04 23:33:26 +00001144 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
1145 if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() &&
1146 isa<ConstantSDNode>(N0.getOperand(1))) {
1147 return DAG.getNode(ISD::ADD, VT,
1148 DAG.getNode(ISD::MUL, VT, N0.getOperand(0), N1),
1149 DAG.getNode(ISD::MUL, VT, N0.getOperand(1), N1));
1150 }
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001151
Nate Begemancd4d58c2006-02-03 06:46:56 +00001152 // reassociate mul
1153 SDOperand RMUL = ReassociateOps(ISD::MUL, N0, N1);
1154 if (RMUL.Val != 0)
1155 return RMUL;
Dan Gohman7f321562007-06-25 16:23:39 +00001156
Nate Begeman83e75ec2005-09-06 04:43:02 +00001157 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001158}
1159
Nate Begeman83e75ec2005-09-06 04:43:02 +00001160SDOperand DAGCombiner::visitSDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001161 SDOperand N0 = N->getOperand(0);
1162 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001163 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1164 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +00001165 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001166
Dan Gohman7f321562007-06-25 16:23:39 +00001167 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00001168 if (MVT::isVector(VT)) {
1169 SDOperand FoldedVOp = SimplifyVBinOp(N);
1170 if (FoldedVOp.Val) return FoldedVOp;
1171 }
Dan Gohman7f321562007-06-25 16:23:39 +00001172
Nate Begeman1d4d4142005-09-01 00:19:25 +00001173 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001174 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001175 return DAG.getNode(ISD::SDIV, VT, N0, N1);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001176 // fold (sdiv X, 1) -> X
1177 if (N1C && N1C->getSignExtended() == 1LL)
1178 return N0;
1179 // fold (sdiv X, -1) -> 0-X
1180 if (N1C && N1C->isAllOnesValue())
1181 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +00001182 // If we know the sign bits of both operands are zero, strength reduce to a
1183 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
1184 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001185 if (DAG.MaskedValueIsZero(N1, SignBit) &&
1186 DAG.MaskedValueIsZero(N0, SignBit))
Chris Lattner094c8fc2005-10-07 06:10:46 +00001187 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
Nate Begemancd6a6ed2006-02-17 07:26:20 +00001188 // fold (sdiv X, pow2) -> simple ops after legalize
Nate Begemanfb7217b2006-02-17 19:54:08 +00001189 if (N1C && N1C->getValue() && !TLI.isIntDivCheap() &&
Nate Begeman405e3ec2005-10-21 00:02:42 +00001190 (isPowerOf2_64(N1C->getSignExtended()) ||
1191 isPowerOf2_64(-N1C->getSignExtended()))) {
1192 // If dividing by powers of two is cheap, then don't perform the following
1193 // fold.
1194 if (TLI.isPow2DivCheap())
1195 return SDOperand();
1196 int64_t pow2 = N1C->getSignExtended();
1197 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
Chris Lattner8f4880b2006-02-16 08:02:36 +00001198 unsigned lg2 = Log2_64(abs2);
1199 // Splat the sign bit into the register
1200 SDOperand SGN = DAG.getNode(ISD::SRA, VT, N0,
Nate Begeman405e3ec2005-10-21 00:02:42 +00001201 DAG.getConstant(MVT::getSizeInBits(VT)-1,
1202 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00001203 AddToWorkList(SGN.Val);
Chris Lattner8f4880b2006-02-16 08:02:36 +00001204 // Add (N0 < 0) ? abs2 - 1 : 0;
1205 SDOperand SRL = DAG.getNode(ISD::SRL, VT, SGN,
1206 DAG.getConstant(MVT::getSizeInBits(VT)-lg2,
Nate Begeman405e3ec2005-10-21 00:02:42 +00001207 TLI.getShiftAmountTy()));
Chris Lattner8f4880b2006-02-16 08:02:36 +00001208 SDOperand ADD = DAG.getNode(ISD::ADD, VT, N0, SRL);
Chris Lattner5750df92006-03-01 04:03:14 +00001209 AddToWorkList(SRL.Val);
1210 AddToWorkList(ADD.Val); // Divide by pow2
Chris Lattner8f4880b2006-02-16 08:02:36 +00001211 SDOperand SRA = DAG.getNode(ISD::SRA, VT, ADD,
1212 DAG.getConstant(lg2, TLI.getShiftAmountTy()));
Nate Begeman405e3ec2005-10-21 00:02:42 +00001213 // If we're dividing by a positive value, we're done. Otherwise, we must
1214 // negate the result.
1215 if (pow2 > 0)
1216 return SRA;
Chris Lattner5750df92006-03-01 04:03:14 +00001217 AddToWorkList(SRA.Val);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001218 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), SRA);
1219 }
Nate Begeman69575232005-10-20 02:15:44 +00001220 // if integer divide is expensive and we satisfy the requirements, emit an
1221 // alternate sequence.
Nate Begeman405e3ec2005-10-21 00:02:42 +00001222 if (N1C && (N1C->getSignExtended() < -1 || N1C->getSignExtended() > 1) &&
Chris Lattnere9936d12005-10-22 18:50:15 +00001223 !TLI.isIntDivCheap()) {
1224 SDOperand Op = BuildSDIV(N);
1225 if (Op.Val) return Op;
Nate Begeman69575232005-10-20 02:15:44 +00001226 }
Dan Gohman7f321562007-06-25 16:23:39 +00001227
Dan Gohman613e0d82007-07-03 14:03:57 +00001228 // undef / X -> 0
1229 if (N0.getOpcode() == ISD::UNDEF)
1230 return DAG.getConstant(0, VT);
1231 // X / undef -> undef
1232 if (N1.getOpcode() == ISD::UNDEF)
1233 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001234
Nate Begeman83e75ec2005-09-06 04:43:02 +00001235 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001236}
1237
Nate Begeman83e75ec2005-09-06 04:43:02 +00001238SDOperand DAGCombiner::visitUDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001239 SDOperand N0 = N->getOperand(0);
1240 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001241 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1242 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +00001243 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001244
Dan Gohman7f321562007-06-25 16:23:39 +00001245 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00001246 if (MVT::isVector(VT)) {
1247 SDOperand FoldedVOp = SimplifyVBinOp(N);
1248 if (FoldedVOp.Val) return FoldedVOp;
1249 }
Dan Gohman7f321562007-06-25 16:23:39 +00001250
Nate Begeman1d4d4142005-09-01 00:19:25 +00001251 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001252 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001253 return DAG.getNode(ISD::UDIV, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001254 // fold (udiv x, (1 << c)) -> x >>u c
Nate Begeman646d7e22005-09-02 21:18:40 +00001255 if (N1C && isPowerOf2_64(N1C->getValue()))
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001256 return DAG.getNode(ISD::SRL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +00001257 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001258 TLI.getShiftAmountTy()));
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001259 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
1260 if (N1.getOpcode() == ISD::SHL) {
1261 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
1262 if (isPowerOf2_64(SHC->getValue())) {
1263 MVT::ValueType ADDVT = N1.getOperand(1).getValueType();
Nate Begemanc031e332006-02-05 07:36:48 +00001264 SDOperand Add = DAG.getNode(ISD::ADD, ADDVT, N1.getOperand(1),
1265 DAG.getConstant(Log2_64(SHC->getValue()),
1266 ADDVT));
Chris Lattner5750df92006-03-01 04:03:14 +00001267 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +00001268 return DAG.getNode(ISD::SRL, VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001269 }
1270 }
1271 }
Nate Begeman69575232005-10-20 02:15:44 +00001272 // fold (udiv x, c) -> alternate
Chris Lattnere9936d12005-10-22 18:50:15 +00001273 if (N1C && N1C->getValue() && !TLI.isIntDivCheap()) {
1274 SDOperand Op = BuildUDIV(N);
1275 if (Op.Val) return Op;
1276 }
Dan Gohman7f321562007-06-25 16:23:39 +00001277
Dan Gohman613e0d82007-07-03 14:03:57 +00001278 // undef / X -> 0
1279 if (N0.getOpcode() == ISD::UNDEF)
1280 return DAG.getConstant(0, VT);
1281 // X / undef -> undef
1282 if (N1.getOpcode() == ISD::UNDEF)
1283 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001284
Nate Begeman83e75ec2005-09-06 04:43:02 +00001285 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001286}
1287
Nate Begeman83e75ec2005-09-06 04:43:02 +00001288SDOperand DAGCombiner::visitSREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001289 SDOperand N0 = N->getOperand(0);
1290 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001291 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1292 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +00001293 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001294
1295 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001296 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001297 return DAG.getNode(ISD::SREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +00001298 // If we know the sign bits of both operands are zero, strength reduce to a
1299 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
1300 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001301 if (DAG.MaskedValueIsZero(N1, SignBit) &&
1302 DAG.MaskedValueIsZero(N0, SignBit))
Nate Begemana148d982006-01-18 22:35:16 +00001303 return DAG.getNode(ISD::UREM, VT, N0, N1);
Chris Lattner26d29902006-10-12 20:58:32 +00001304
1305 // Unconditionally lower X%C -> X-X/C*C. This allows the X/C logic to hack on
1306 // the remainder operation.
1307 if (N1C && !N1C->isNullValue()) {
1308 SDOperand Div = DAG.getNode(ISD::SDIV, VT, N0, N1);
1309 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Div, N1);
1310 SDOperand Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
1311 AddToWorkList(Div.Val);
1312 AddToWorkList(Mul.Val);
1313 return Sub;
1314 }
1315
Dan Gohman613e0d82007-07-03 14:03:57 +00001316 // undef % X -> 0
1317 if (N0.getOpcode() == ISD::UNDEF)
1318 return DAG.getConstant(0, VT);
1319 // X % undef -> undef
1320 if (N1.getOpcode() == ISD::UNDEF)
1321 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001322
Nate Begeman83e75ec2005-09-06 04:43:02 +00001323 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001324}
1325
Nate Begeman83e75ec2005-09-06 04:43:02 +00001326SDOperand DAGCombiner::visitUREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001327 SDOperand N0 = N->getOperand(0);
1328 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001329 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1330 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +00001331 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001332
1333 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001334 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001335 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +00001336 // fold (urem x, pow2) -> (and x, pow2-1)
1337 if (N1C && !N1C->isNullValue() && isPowerOf2_64(N1C->getValue()))
Nate Begemana148d982006-01-18 22:35:16 +00001338 return DAG.getNode(ISD::AND, VT, N0, DAG.getConstant(N1C->getValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +00001339 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
1340 if (N1.getOpcode() == ISD::SHL) {
1341 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
1342 if (isPowerOf2_64(SHC->getValue())) {
Nate Begemanbab92392006-02-05 08:07:24 +00001343 SDOperand Add = DAG.getNode(ISD::ADD, VT, N1,DAG.getConstant(~0ULL,VT));
Chris Lattner5750df92006-03-01 04:03:14 +00001344 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +00001345 return DAG.getNode(ISD::AND, VT, N0, Add);
1346 }
1347 }
1348 }
Chris Lattner26d29902006-10-12 20:58:32 +00001349
1350 // Unconditionally lower X%C -> X-X/C*C. This allows the X/C logic to hack on
1351 // the remainder operation.
1352 if (N1C && !N1C->isNullValue()) {
1353 SDOperand Div = DAG.getNode(ISD::UDIV, VT, N0, N1);
1354 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Div, N1);
1355 SDOperand Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
1356 AddToWorkList(Div.Val);
1357 AddToWorkList(Mul.Val);
1358 return Sub;
1359 }
1360
Dan Gohman613e0d82007-07-03 14:03:57 +00001361 // undef % X -> 0
1362 if (N0.getOpcode() == ISD::UNDEF)
1363 return DAG.getConstant(0, VT);
1364 // X % undef -> undef
1365 if (N1.getOpcode() == ISD::UNDEF)
1366 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001367
Nate Begeman83e75ec2005-09-06 04:43:02 +00001368 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001369}
1370
Nate Begeman83e75ec2005-09-06 04:43:02 +00001371SDOperand DAGCombiner::visitMULHS(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001372 SDOperand N0 = N->getOperand(0);
1373 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001374 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Dan Gohman7f321562007-06-25 16:23:39 +00001375 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001376
1377 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001378 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001379 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001380 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001381 if (N1C && N1C->getValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001382 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
1383 DAG.getConstant(MVT::getSizeInBits(N0.getValueType())-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001384 TLI.getShiftAmountTy()));
Dan Gohman613e0d82007-07-03 14:03:57 +00001385 // fold (mulhs x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001386 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001387 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00001388
Nate Begeman83e75ec2005-09-06 04:43:02 +00001389 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001390}
1391
Nate Begeman83e75ec2005-09-06 04:43:02 +00001392SDOperand DAGCombiner::visitMULHU(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001393 SDOperand N0 = N->getOperand(0);
1394 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001395 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Dan Gohman7f321562007-06-25 16:23:39 +00001396 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001397
1398 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001399 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001400 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001401 // fold (mulhu x, 1) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001402 if (N1C && N1C->getValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001403 return DAG.getConstant(0, N0.getValueType());
Dan Gohman613e0d82007-07-03 14:03:57 +00001404 // fold (mulhu x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001405 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001406 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00001407
Nate Begeman83e75ec2005-09-06 04:43:02 +00001408 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001409}
1410
Dan Gohman389079b2007-10-08 17:57:15 +00001411/// SimplifyNodeWithTwoResults - Perform optimizations common to nodes that
1412/// compute two values. LoOp and HiOp give the opcodes for the two computations
1413/// that are being performed. Return true if a simplification was made.
1414///
1415bool DAGCombiner::SimplifyNodeWithTwoResults(SDNode *N,
1416 unsigned LoOp, unsigned HiOp) {
Dan Gohman389079b2007-10-08 17:57:15 +00001417 // If the high half is not needed, just compute the low half.
1418 if (!N->hasAnyUseOfValue(1) &&
1419 (!AfterLegalize ||
1420 TLI.isOperationLegal(LoOp, N->getValueType(0)))) {
1421 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0),
1422 DAG.getNode(LoOp, N->getValueType(0),
1423 N->op_begin(),
Chris Lattner01d029b2007-10-15 06:10:22 +00001424 N->getNumOperands()));
Dan Gohman389079b2007-10-08 17:57:15 +00001425 return true;
1426 }
1427
1428 // If the low half is not needed, just compute the high half.
1429 if (!N->hasAnyUseOfValue(0) &&
1430 (!AfterLegalize ||
1431 TLI.isOperationLegal(HiOp, N->getValueType(1)))) {
1432 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1),
1433 DAG.getNode(HiOp, N->getValueType(1),
1434 N->op_begin(),
Chris Lattner01d029b2007-10-15 06:10:22 +00001435 N->getNumOperands()));
Dan Gohman389079b2007-10-08 17:57:15 +00001436 return true;
1437 }
1438
1439 // If the two computed results can be siplified separately, separate them.
1440 SDOperand Lo = DAG.getNode(LoOp, N->getValueType(0),
1441 N->op_begin(), N->getNumOperands());
1442 SDOperand Hi = DAG.getNode(HiOp, N->getValueType(1),
1443 N->op_begin(), N->getNumOperands());
1444 unsigned LoExists = !Lo.use_empty();
1445 unsigned HiExists = !Hi.use_empty();
1446 SDOperand LoOpt = Lo;
1447 SDOperand HiOpt = Hi;
1448 if (!LoExists || !HiExists) {
1449 SDOperand Pair = DAG.getNode(ISD::BUILD_PAIR, MVT::Other, Lo, Hi);
1450 assert(Pair.use_empty() && "Pair with type MVT::Other already exists!");
1451 LoOpt = combine(Lo.Val);
1452 HiOpt = combine(Hi.Val);
1453 if (!LoOpt.Val)
1454 LoOpt = Pair.getOperand(0);
1455 if (!HiOpt.Val)
1456 HiOpt = Pair.getOperand(1);
1457 DAG.DeleteNode(Pair.Val);
1458 }
1459 if ((LoExists || LoOpt != Lo) &&
1460 (HiExists || HiOpt != Hi) &&
1461 TLI.isOperationLegal(LoOpt.getOpcode(), LoOpt.getValueType()) &&
1462 TLI.isOperationLegal(HiOpt.getOpcode(), HiOpt.getValueType())) {
Chris Lattner01d029b2007-10-15 06:10:22 +00001463 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), LoOpt);
1464 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1), HiOpt);
Dan Gohman389079b2007-10-08 17:57:15 +00001465 return true;
1466 }
1467
1468 return false;
1469}
1470
1471SDOperand DAGCombiner::visitSMUL_LOHI(SDNode *N) {
1472
1473 if (SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHS))
1474 return SDOperand();
1475
1476 return SDOperand();
1477}
1478
1479SDOperand DAGCombiner::visitUMUL_LOHI(SDNode *N) {
1480
1481 if (SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHU))
1482 return SDOperand();
1483
1484 return SDOperand();
1485}
1486
1487SDOperand DAGCombiner::visitSDIVREM(SDNode *N) {
1488
1489 if (SimplifyNodeWithTwoResults(N, ISD::SDIV, ISD::SREM))
1490 return SDOperand();
1491
1492 return SDOperand();
1493}
1494
1495SDOperand DAGCombiner::visitUDIVREM(SDNode *N) {
1496
1497 if (SimplifyNodeWithTwoResults(N, ISD::UDIV, ISD::UREM))
1498 return SDOperand();
1499
1500 return SDOperand();
1501}
1502
Chris Lattner35e5c142006-05-05 05:51:50 +00001503/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
1504/// two operands of the same opcode, try to simplify it.
1505SDOperand DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
1506 SDOperand N0 = N->getOperand(0), N1 = N->getOperand(1);
1507 MVT::ValueType VT = N0.getValueType();
1508 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
1509
Chris Lattner540121f2006-05-05 06:31:05 +00001510 // For each of OP in AND/OR/XOR:
1511 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
1512 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
1513 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
Chris Lattner0d8dae72006-05-05 06:32:04 +00001514 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y))
Chris Lattner540121f2006-05-05 06:31:05 +00001515 if ((N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND||
Chris Lattner0d8dae72006-05-05 06:32:04 +00001516 N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::TRUNCATE) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001517 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1518 SDOperand ORNode = DAG.getNode(N->getOpcode(),
1519 N0.getOperand(0).getValueType(),
1520 N0.getOperand(0), N1.getOperand(0));
1521 AddToWorkList(ORNode.Val);
Chris Lattner540121f2006-05-05 06:31:05 +00001522 return DAG.getNode(N0.getOpcode(), VT, ORNode);
Chris Lattner35e5c142006-05-05 05:51:50 +00001523 }
1524
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001525 // For each of OP in SHL/SRL/SRA/AND...
1526 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
1527 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
1528 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
Chris Lattner35e5c142006-05-05 05:51:50 +00001529 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001530 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001531 N0.getOperand(1) == N1.getOperand(1)) {
1532 SDOperand ORNode = DAG.getNode(N->getOpcode(),
1533 N0.getOperand(0).getValueType(),
1534 N0.getOperand(0), N1.getOperand(0));
1535 AddToWorkList(ORNode.Val);
1536 return DAG.getNode(N0.getOpcode(), VT, ORNode, N0.getOperand(1));
1537 }
1538
1539 return SDOperand();
1540}
1541
Nate Begeman83e75ec2005-09-06 04:43:02 +00001542SDOperand DAGCombiner::visitAND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001543 SDOperand N0 = N->getOperand(0);
1544 SDOperand N1 = N->getOperand(1);
Nate Begemanfb7217b2006-02-17 19:54:08 +00001545 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001546 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1547 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001548 MVT::ValueType VT = N1.getValueType();
1549
Dan Gohman7f321562007-06-25 16:23:39 +00001550 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00001551 if (MVT::isVector(VT)) {
1552 SDOperand FoldedVOp = SimplifyVBinOp(N);
1553 if (FoldedVOp.Val) return FoldedVOp;
1554 }
Dan Gohman7f321562007-06-25 16:23:39 +00001555
Dan Gohman613e0d82007-07-03 14:03:57 +00001556 // fold (and x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001557 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001558 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001559 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001560 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001561 return DAG.getNode(ISD::AND, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001562 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001563 if (N0C && !N1C)
1564 return DAG.getNode(ISD::AND, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001565 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001566 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001567 return N0;
1568 // if (and x, c) is known to be zero, return 0
Dan Gohmanea859be2007-06-22 14:59:07 +00001569 if (N1C && DAG.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001570 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +00001571 // reassociate and
1572 SDOperand RAND = ReassociateOps(ISD::AND, N0, N1);
1573 if (RAND.Val != 0)
1574 return RAND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001575 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
Nate Begeman5dc7e862005-11-02 18:42:59 +00001576 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001577 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Nate Begeman646d7e22005-09-02 21:18:40 +00001578 if ((ORI->getValue() & N1C->getValue()) == N1C->getValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001579 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +00001580 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
1581 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Chris Lattner1ec05d12006-03-01 21:47:21 +00001582 unsigned InMask = MVT::getIntVTBitMask(N0.getOperand(0).getValueType());
Dan Gohmanea859be2007-06-22 14:59:07 +00001583 if (DAG.MaskedValueIsZero(N0.getOperand(0),
Chris Lattner1ec05d12006-03-01 21:47:21 +00001584 ~N1C->getValue() & InMask)) {
1585 SDOperand Zext = DAG.getNode(ISD::ZERO_EXTEND, N0.getValueType(),
1586 N0.getOperand(0));
1587
1588 // Replace uses of the AND with uses of the Zero extend node.
1589 CombineTo(N, Zext);
1590
Chris Lattner3603cd62006-02-02 07:17:31 +00001591 // We actually want to replace all uses of the any_extend with the
1592 // zero_extend, to avoid duplicating things. This will later cause this
1593 // AND to be folded.
Chris Lattner1ec05d12006-03-01 21:47:21 +00001594 CombineTo(N0.Val, Zext);
Chris Lattnerfedced72006-04-20 23:55:59 +00001595 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner3603cd62006-02-02 07:17:31 +00001596 }
1597 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001598 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
1599 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1600 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1601 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1602
1603 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1604 MVT::isInteger(LL.getValueType())) {
1605 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
1606 if (cast<ConstantSDNode>(LR)->getValue() == 0 && Op1 == ISD::SETEQ) {
1607 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001608 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001609 return DAG.getSetCC(VT, ORNode, LR, Op1);
1610 }
1611 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
1612 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
1613 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001614 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001615 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1616 }
1617 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
1618 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
1619 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001620 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001621 return DAG.getSetCC(VT, ORNode, LR, Op1);
1622 }
1623 }
1624 // canonicalize equivalent to ll == rl
1625 if (LL == RR && LR == RL) {
1626 Op1 = ISD::getSetCCSwappedOperands(Op1);
1627 std::swap(RL, RR);
1628 }
1629 if (LL == RL && LR == RR) {
1630 bool isInteger = MVT::isInteger(LL.getValueType());
1631 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
1632 if (Result != ISD::SETCC_INVALID)
1633 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1634 }
1635 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001636
1637 // Simplify: and (op x...), (op y...) -> (op (and x, y))
1638 if (N0.getOpcode() == N1.getOpcode()) {
1639 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1640 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001641 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001642
Nate Begemande996292006-02-03 22:24:05 +00001643 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
1644 // fold (and (sra)) -> (and (srl)) when possible.
Chris Lattner6ea2dee2006-03-25 22:19:00 +00001645 if (!MVT::isVector(VT) &&
1646 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001647 return SDOperand(N, 0);
Nate Begemanded49632005-10-13 03:11:28 +00001648 // fold (zext_inreg (extload x)) -> (zextload x)
Evan Cheng83060c52007-03-07 08:07:03 +00001649 if (ISD::isEXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val)) {
Evan Cheng466685d2006-10-09 20:57:25 +00001650 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00001651 MVT::ValueType EVT = LN0->getLoadedVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001652 // If we zero all the possible extended bits, then we can turn this into
1653 // a zextload if we are running before legalize or the operation is legal.
Dan Gohmanea859be2007-06-22 14:59:07 +00001654 if (DAG.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Evan Chengc5484282006-10-04 00:56:09 +00001655 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00001656 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
1657 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00001658 LN0->getSrcValueOffset(), EVT,
1659 LN0->isVolatile(),
1660 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00001661 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001662 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001663 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001664 }
1665 }
1666 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Evan Cheng83060c52007-03-07 08:07:03 +00001667 if (ISD::isSEXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val) &&
1668 N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001669 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00001670 MVT::ValueType EVT = LN0->getLoadedVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001671 // If we zero all the possible extended bits, then we can turn this into
1672 // a zextload if we are running before legalize or the operation is legal.
Dan Gohmanea859be2007-06-22 14:59:07 +00001673 if (DAG.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Evan Chengc5484282006-10-04 00:56:09 +00001674 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00001675 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
1676 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00001677 LN0->getSrcValueOffset(), EVT,
1678 LN0->isVolatile(),
1679 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00001680 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001681 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001682 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001683 }
1684 }
Chris Lattner15045b62006-02-28 06:35:35 +00001685
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001686 // fold (and (load x), 255) -> (zextload x, i8)
1687 // fold (and (extload x, i16), 255) -> (zextload x, i8)
Evan Cheng466685d2006-10-09 20:57:25 +00001688 if (N1C && N0.getOpcode() == ISD::LOAD) {
1689 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
1690 if (LN0->getExtensionType() != ISD::SEXTLOAD &&
Evan Cheng83060c52007-03-07 08:07:03 +00001691 LN0->getAddressingMode() == ISD::UNINDEXED &&
Evan Cheng466685d2006-10-09 20:57:25 +00001692 N0.hasOneUse()) {
1693 MVT::ValueType EVT, LoadedVT;
1694 if (N1C->getValue() == 255)
1695 EVT = MVT::i8;
1696 else if (N1C->getValue() == 65535)
1697 EVT = MVT::i16;
1698 else if (N1C->getValue() == ~0U)
1699 EVT = MVT::i32;
1700 else
1701 EVT = MVT::Other;
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001702
Evan Cheng2e49f092006-10-11 07:10:22 +00001703 LoadedVT = LN0->getLoadedVT();
Evan Cheng466685d2006-10-09 20:57:25 +00001704 if (EVT != MVT::Other && LoadedVT > EVT &&
1705 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
1706 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
1707 // For big endian targets, we need to add an offset to the pointer to
1708 // load the correct bytes. For little endian systems, we merely need to
1709 // read fewer bytes from the same pointer.
1710 unsigned PtrOff =
1711 (MVT::getSizeInBits(LoadedVT) - MVT::getSizeInBits(EVT)) / 8;
1712 SDOperand NewPtr = LN0->getBasePtr();
1713 if (!TLI.isLittleEndian())
1714 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
1715 DAG.getConstant(PtrOff, PtrType));
1716 AddToWorkList(NewPtr.Val);
1717 SDOperand Load =
1718 DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00001719 LN0->getSrcValue(), LN0->getSrcValueOffset(), EVT,
1720 LN0->isVolatile(), LN0->getAlignment());
Evan Cheng466685d2006-10-09 20:57:25 +00001721 AddToWorkList(N);
1722 CombineTo(N0.Val, Load, Load.getValue(1));
1723 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1724 }
Chris Lattner15045b62006-02-28 06:35:35 +00001725 }
1726 }
1727
Nate Begeman83e75ec2005-09-06 04:43:02 +00001728 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001729}
1730
Nate Begeman83e75ec2005-09-06 04:43:02 +00001731SDOperand DAGCombiner::visitOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001732 SDOperand N0 = N->getOperand(0);
1733 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001734 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001735 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1736 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001737 MVT::ValueType VT = N1.getValueType();
1738 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001739
Dan Gohman7f321562007-06-25 16:23:39 +00001740 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00001741 if (MVT::isVector(VT)) {
1742 SDOperand FoldedVOp = SimplifyVBinOp(N);
1743 if (FoldedVOp.Val) return FoldedVOp;
1744 }
Dan Gohman7f321562007-06-25 16:23:39 +00001745
Dan Gohman613e0d82007-07-03 14:03:57 +00001746 // fold (or x, undef) -> -1
Dan Gohmand595b5f2007-07-10 14:20:37 +00001747 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Chris Lattnere094f542007-07-09 16:16:34 +00001748 return DAG.getConstant(~0ULL, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001749 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001750 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001751 return DAG.getNode(ISD::OR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001752 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001753 if (N0C && !N1C)
1754 return DAG.getNode(ISD::OR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001755 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001756 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001757 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001758 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001759 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001760 return N1;
1761 // fold (or x, c) -> c iff (x & ~c) == 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001762 if (N1C &&
Dan Gohmanea859be2007-06-22 14:59:07 +00001763 DAG.MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits))))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001764 return N1;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001765 // reassociate or
1766 SDOperand ROR = ReassociateOps(ISD::OR, N0, N1);
1767 if (ROR.Val != 0)
1768 return ROR;
1769 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
1770 if (N1C && N0.getOpcode() == ISD::AND && N0.Val->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00001771 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00001772 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
1773 return DAG.getNode(ISD::AND, VT, DAG.getNode(ISD::OR, VT, N0.getOperand(0),
1774 N1),
1775 DAG.getConstant(N1C->getValue() | C1->getValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00001776 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001777 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
1778 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1779 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1780 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1781
1782 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1783 MVT::isInteger(LL.getValueType())) {
1784 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
1785 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
1786 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
1787 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
1788 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001789 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001790 return DAG.getSetCC(VT, ORNode, LR, Op1);
1791 }
1792 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
1793 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
1794 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
1795 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
1796 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001797 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001798 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1799 }
1800 }
1801 // canonicalize equivalent to ll == rl
1802 if (LL == RR && LR == RL) {
1803 Op1 = ISD::getSetCCSwappedOperands(Op1);
1804 std::swap(RL, RR);
1805 }
1806 if (LL == RL && LR == RR) {
1807 bool isInteger = MVT::isInteger(LL.getValueType());
1808 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
1809 if (Result != ISD::SETCC_INVALID)
1810 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1811 }
1812 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001813
1814 // Simplify: or (op x...), (op y...) -> (op (or x, y))
1815 if (N0.getOpcode() == N1.getOpcode()) {
1816 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1817 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001818 }
Chris Lattner516b9622006-09-14 20:50:57 +00001819
Chris Lattner1ec72732006-09-14 21:11:37 +00001820 // (X & C1) | (Y & C2) -> (X|Y) & C3 if possible.
1821 if (N0.getOpcode() == ISD::AND &&
1822 N1.getOpcode() == ISD::AND &&
1823 N0.getOperand(1).getOpcode() == ISD::Constant &&
1824 N1.getOperand(1).getOpcode() == ISD::Constant &&
1825 // Don't increase # computations.
1826 (N0.Val->hasOneUse() || N1.Val->hasOneUse())) {
1827 // We can only do this xform if we know that bits from X that are set in C2
1828 // but not in C1 are already zero. Likewise for Y.
1829 uint64_t LHSMask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1830 uint64_t RHSMask = cast<ConstantSDNode>(N1.getOperand(1))->getValue();
1831
Dan Gohmanea859be2007-06-22 14:59:07 +00001832 if (DAG.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) &&
1833 DAG.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) {
Chris Lattner1ec72732006-09-14 21:11:37 +00001834 SDOperand X =DAG.getNode(ISD::OR, VT, N0.getOperand(0), N1.getOperand(0));
1835 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(LHSMask|RHSMask, VT));
1836 }
1837 }
1838
1839
Chris Lattner516b9622006-09-14 20:50:57 +00001840 // See if this is some rotate idiom.
1841 if (SDNode *Rot = MatchRotate(N0, N1))
1842 return SDOperand(Rot, 0);
Chris Lattner35e5c142006-05-05 05:51:50 +00001843
Nate Begeman83e75ec2005-09-06 04:43:02 +00001844 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001845}
1846
Chris Lattner516b9622006-09-14 20:50:57 +00001847
1848/// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present.
1849static bool MatchRotateHalf(SDOperand Op, SDOperand &Shift, SDOperand &Mask) {
1850 if (Op.getOpcode() == ISD::AND) {
Reid Spencer3ed469c2006-11-02 20:25:50 +00001851 if (isa<ConstantSDNode>(Op.getOperand(1))) {
Chris Lattner516b9622006-09-14 20:50:57 +00001852 Mask = Op.getOperand(1);
1853 Op = Op.getOperand(0);
1854 } else {
1855 return false;
1856 }
1857 }
1858
1859 if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) {
1860 Shift = Op;
1861 return true;
1862 }
1863 return false;
1864}
1865
1866
1867// MatchRotate - Handle an 'or' of two operands. If this is one of the many
1868// idioms for rotate, and if the target supports rotation instructions, generate
1869// a rot[lr].
1870SDNode *DAGCombiner::MatchRotate(SDOperand LHS, SDOperand RHS) {
1871 // Must be a legal type. Expanded an promoted things won't work with rotates.
1872 MVT::ValueType VT = LHS.getValueType();
1873 if (!TLI.isTypeLegal(VT)) return 0;
1874
1875 // The target must have at least one rotate flavor.
1876 bool HasROTL = TLI.isOperationLegal(ISD::ROTL, VT);
1877 bool HasROTR = TLI.isOperationLegal(ISD::ROTR, VT);
1878 if (!HasROTL && !HasROTR) return 0;
1879
1880 // Match "(X shl/srl V1) & V2" where V2 may not be present.
1881 SDOperand LHSShift; // The shift.
1882 SDOperand LHSMask; // AND value if any.
1883 if (!MatchRotateHalf(LHS, LHSShift, LHSMask))
1884 return 0; // Not part of a rotate.
1885
1886 SDOperand RHSShift; // The shift.
1887 SDOperand RHSMask; // AND value if any.
1888 if (!MatchRotateHalf(RHS, RHSShift, RHSMask))
1889 return 0; // Not part of a rotate.
1890
1891 if (LHSShift.getOperand(0) != RHSShift.getOperand(0))
1892 return 0; // Not shifting the same value.
1893
1894 if (LHSShift.getOpcode() == RHSShift.getOpcode())
1895 return 0; // Shifts must disagree.
1896
1897 // Canonicalize shl to left side in a shl/srl pair.
1898 if (RHSShift.getOpcode() == ISD::SHL) {
1899 std::swap(LHS, RHS);
1900 std::swap(LHSShift, RHSShift);
1901 std::swap(LHSMask , RHSMask );
1902 }
1903
1904 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Scott Michelc9dc1142007-04-02 21:36:32 +00001905 SDOperand LHSShiftArg = LHSShift.getOperand(0);
1906 SDOperand LHSShiftAmt = LHSShift.getOperand(1);
1907 SDOperand RHSShiftAmt = RHSShift.getOperand(1);
Chris Lattner516b9622006-09-14 20:50:57 +00001908
1909 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
1910 // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2)
Scott Michelc9dc1142007-04-02 21:36:32 +00001911 if (LHSShiftAmt.getOpcode() == ISD::Constant &&
1912 RHSShiftAmt.getOpcode() == ISD::Constant) {
1913 uint64_t LShVal = cast<ConstantSDNode>(LHSShiftAmt)->getValue();
1914 uint64_t RShVal = cast<ConstantSDNode>(RHSShiftAmt)->getValue();
Chris Lattner516b9622006-09-14 20:50:57 +00001915 if ((LShVal + RShVal) != OpSizeInBits)
1916 return 0;
1917
1918 SDOperand Rot;
1919 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00001920 Rot = DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00001921 else
Scott Michelc9dc1142007-04-02 21:36:32 +00001922 Rot = DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00001923
1924 // If there is an AND of either shifted operand, apply it to the result.
1925 if (LHSMask.Val || RHSMask.Val) {
1926 uint64_t Mask = MVT::getIntVTBitMask(VT);
1927
1928 if (LHSMask.Val) {
1929 uint64_t RHSBits = (1ULL << LShVal)-1;
1930 Mask &= cast<ConstantSDNode>(LHSMask)->getValue() | RHSBits;
1931 }
1932 if (RHSMask.Val) {
1933 uint64_t LHSBits = ~((1ULL << (OpSizeInBits-RShVal))-1);
1934 Mask &= cast<ConstantSDNode>(RHSMask)->getValue() | LHSBits;
1935 }
1936
1937 Rot = DAG.getNode(ISD::AND, VT, Rot, DAG.getConstant(Mask, VT));
1938 }
1939
1940 return Rot.Val;
1941 }
1942
1943 // If there is a mask here, and we have a variable shift, we can't be sure
1944 // that we're masking out the right stuff.
1945 if (LHSMask.Val || RHSMask.Val)
1946 return 0;
1947
1948 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
1949 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotr x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00001950 if (RHSShiftAmt.getOpcode() == ISD::SUB &&
1951 LHSShiftAmt == RHSShiftAmt.getOperand(1)) {
Chris Lattner516b9622006-09-14 20:50:57 +00001952 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00001953 dyn_cast<ConstantSDNode>(RHSShiftAmt.getOperand(0))) {
Chris Lattner516b9622006-09-14 20:50:57 +00001954 if (SUBC->getValue() == OpSizeInBits)
1955 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00001956 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
Chris Lattner516b9622006-09-14 20:50:57 +00001957 else
Scott Michelc9dc1142007-04-02 21:36:32 +00001958 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).Val;
Chris Lattner516b9622006-09-14 20:50:57 +00001959 }
1960 }
1961
1962 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
1963 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotl x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00001964 if (LHSShiftAmt.getOpcode() == ISD::SUB &&
1965 RHSShiftAmt == LHSShiftAmt.getOperand(1)) {
Chris Lattner516b9622006-09-14 20:50:57 +00001966 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00001967 dyn_cast<ConstantSDNode>(LHSShiftAmt.getOperand(0))) {
Chris Lattner516b9622006-09-14 20:50:57 +00001968 if (SUBC->getValue() == OpSizeInBits)
1969 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00001970 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
Chris Lattner516b9622006-09-14 20:50:57 +00001971 else
Scott Michelc9dc1142007-04-02 21:36:32 +00001972 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).Val;
1973 }
1974 }
1975
1976 // Look for sign/zext/any-extended cases:
1977 if ((LHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
1978 || LHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
1979 || LHSShiftAmt.getOpcode() == ISD::ANY_EXTEND) &&
1980 (RHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
1981 || RHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
1982 || RHSShiftAmt.getOpcode() == ISD::ANY_EXTEND)) {
1983 SDOperand LExtOp0 = LHSShiftAmt.getOperand(0);
1984 SDOperand RExtOp0 = RHSShiftAmt.getOperand(0);
1985 if (RExtOp0.getOpcode() == ISD::SUB &&
1986 RExtOp0.getOperand(1) == LExtOp0) {
1987 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
1988 // (rotr x, y)
1989 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
1990 // (rotl x, (sub 32, y))
1991 if (ConstantSDNode *SUBC = cast<ConstantSDNode>(RExtOp0.getOperand(0))) {
1992 if (SUBC->getValue() == OpSizeInBits) {
1993 if (HasROTL)
1994 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
1995 else
1996 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).Val;
1997 }
1998 }
1999 } else if (LExtOp0.getOpcode() == ISD::SUB &&
2000 RExtOp0 == LExtOp0.getOperand(1)) {
2001 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext r))) ->
2002 // (rotl x, y)
2003 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext r))) ->
2004 // (rotr x, (sub 32, y))
2005 if (ConstantSDNode *SUBC = cast<ConstantSDNode>(LExtOp0.getOperand(0))) {
2006 if (SUBC->getValue() == OpSizeInBits) {
2007 if (HasROTL)
2008 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, RHSShiftAmt).Val;
2009 else
2010 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
2011 }
2012 }
Chris Lattner516b9622006-09-14 20:50:57 +00002013 }
2014 }
2015
2016 return 0;
2017}
2018
2019
Nate Begeman83e75ec2005-09-06 04:43:02 +00002020SDOperand DAGCombiner::visitXOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002021 SDOperand N0 = N->getOperand(0);
2022 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002023 SDOperand LHS, RHS, CC;
2024 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2025 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002026 MVT::ValueType VT = N0.getValueType();
2027
Dan Gohman7f321562007-06-25 16:23:39 +00002028 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00002029 if (MVT::isVector(VT)) {
2030 SDOperand FoldedVOp = SimplifyVBinOp(N);
2031 if (FoldedVOp.Val) return FoldedVOp;
2032 }
Dan Gohman7f321562007-06-25 16:23:39 +00002033
Dan Gohman613e0d82007-07-03 14:03:57 +00002034 // fold (xor x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00002035 if (N0.getOpcode() == ISD::UNDEF)
2036 return N0;
2037 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00002038 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002039 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002040 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002041 return DAG.getNode(ISD::XOR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00002042 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00002043 if (N0C && !N1C)
2044 return DAG.getNode(ISD::XOR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002045 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002046 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002047 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00002048 // reassociate xor
2049 SDOperand RXOR = ReassociateOps(ISD::XOR, N0, N1);
2050 if (RXOR.Val != 0)
2051 return RXOR;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002052 // fold !(x cc y) -> (x !cc y)
Nate Begeman646d7e22005-09-02 21:18:40 +00002053 if (N1C && N1C->getValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
2054 bool isInt = MVT::isInteger(LHS.getValueType());
2055 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
2056 isInt);
2057 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002058 return DAG.getSetCC(VT, LHS, RHS, NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00002059 if (N0.getOpcode() == ISD::SELECT_CC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002060 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00002061 assert(0 && "Unhandled SetCC Equivalent!");
2062 abort();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002063 }
Chris Lattner61c5ff42007-09-10 21:39:07 +00002064 // fold (not (zext (setcc x, y))) -> (zext (not (setcc x, y)))
2065 if (N1C && N1C->getValue() == 1 && N0.getOpcode() == ISD::ZERO_EXTEND &&
2066 N0.Val->hasOneUse() && isSetCCEquivalent(N0.getOperand(0), LHS, RHS, CC)){
2067 SDOperand V = N0.getOperand(0);
2068 V = DAG.getNode(ISD::XOR, V.getValueType(), V,
Duncan Sands272dce02007-10-10 09:54:50 +00002069 DAG.getConstant(1, V.getValueType()));
Chris Lattner61c5ff42007-09-10 21:39:07 +00002070 AddToWorkList(V.Val);
2071 return DAG.getNode(ISD::ZERO_EXTEND, VT, V);
2072 }
2073
Nate Begeman99801192005-09-07 23:25:52 +00002074 // fold !(x or y) -> (!x and !y) iff x or y are setcc
Chris Lattner734c91d2006-11-10 21:37:15 +00002075 if (N1C && N1C->getValue() == 1 && VT == MVT::i1 &&
Nate Begeman99801192005-09-07 23:25:52 +00002076 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002077 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00002078 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
2079 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002080 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
2081 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00002082 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00002083 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002084 }
2085 }
Nate Begeman99801192005-09-07 23:25:52 +00002086 // fold !(x or y) -> (!x and !y) iff x or y are constants
2087 if (N1C && N1C->isAllOnesValue() &&
2088 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002089 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00002090 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
2091 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002092 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
2093 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00002094 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00002095 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002096 }
2097 }
Nate Begeman223df222005-09-08 20:18:10 +00002098 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
2099 if (N1C && N0.getOpcode() == ISD::XOR) {
2100 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
2101 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2102 if (N00C)
2103 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
2104 DAG.getConstant(N1C->getValue()^N00C->getValue(), VT));
2105 if (N01C)
2106 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
2107 DAG.getConstant(N1C->getValue()^N01C->getValue(), VT));
2108 }
2109 // fold (xor x, x) -> 0
Chris Lattner4fbdd592006-03-28 19:11:05 +00002110 if (N0 == N1) {
2111 if (!MVT::isVector(VT)) {
2112 return DAG.getConstant(0, VT);
2113 } else if (!AfterLegalize || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) {
2114 // Produce a vector of zeros.
Dan Gohman51eaa862007-06-14 22:58:02 +00002115 SDOperand El = DAG.getConstant(0, MVT::getVectorElementType(VT));
Chris Lattner4fbdd592006-03-28 19:11:05 +00002116 std::vector<SDOperand> Ops(MVT::getVectorNumElements(VT), El);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002117 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4fbdd592006-03-28 19:11:05 +00002118 }
2119 }
Chris Lattner35e5c142006-05-05 05:51:50 +00002120
2121 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
2122 if (N0.getOpcode() == N1.getOpcode()) {
2123 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
2124 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002125 }
Chris Lattner35e5c142006-05-05 05:51:50 +00002126
Chris Lattner3e104b12006-04-08 04:15:24 +00002127 // Simplify the expression using non-local knowledge.
2128 if (!MVT::isVector(VT) &&
2129 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00002130 return SDOperand(N, 0);
Chris Lattner3e104b12006-04-08 04:15:24 +00002131
Nate Begeman83e75ec2005-09-06 04:43:02 +00002132 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002133}
2134
Nate Begeman83e75ec2005-09-06 04:43:02 +00002135SDOperand DAGCombiner::visitSHL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002136 SDOperand N0 = N->getOperand(0);
2137 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002138 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2139 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002140 MVT::ValueType VT = N0.getValueType();
2141 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
2142
2143 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002144 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002145 return DAG.getNode(ISD::SHL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002146 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002147 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002148 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002149 // fold (shl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00002150 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002151 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002152 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002153 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002154 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002155 // if (shl x, c) is known to be zero, return 0
Dan Gohmanea859be2007-06-22 14:59:07 +00002156 if (DAG.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002157 return DAG.getConstant(0, VT);
Chris Lattner61a4c072007-04-18 03:06:49 +00002158 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00002159 return SDOperand(N, 0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002160 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002161 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002162 N0.getOperand(1).getOpcode() == ISD::Constant) {
2163 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00002164 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002165 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002166 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002167 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002168 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002169 }
2170 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
2171 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002172 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002173 N0.getOperand(1).getOpcode() == ISD::Constant) {
2174 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00002175 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002176 SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
2177 DAG.getConstant(~0ULL << c1, VT));
2178 if (c2 > c1)
2179 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +00002180 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002181 else
Nate Begeman83e75ec2005-09-06 04:43:02 +00002182 return DAG.getNode(ISD::SRL, VT, Mask,
2183 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002184 }
2185 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00002186 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +00002187 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002188 DAG.getConstant(~0ULL << N1C->getValue(), VT));
2189 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002190}
2191
Nate Begeman83e75ec2005-09-06 04:43:02 +00002192SDOperand DAGCombiner::visitSRA(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002193 SDOperand N0 = N->getOperand(0);
2194 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002195 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2196 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002197 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002198
2199 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002200 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002201 return DAG.getNode(ISD::SRA, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002202 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002203 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002204 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002205 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00002206 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002207 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002208 // fold (sra x, c >= size(x)) -> undef
Nate Begemanfb7217b2006-02-17 19:54:08 +00002209 if (N1C && N1C->getValue() >= MVT::getSizeInBits(VT))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002210 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002211 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002212 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002213 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00002214 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
2215 // sext_inreg.
2216 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
2217 unsigned LowBits = MVT::getSizeInBits(VT) - (unsigned)N1C->getValue();
2218 MVT::ValueType EVT;
2219 switch (LowBits) {
2220 default: EVT = MVT::Other; break;
2221 case 1: EVT = MVT::i1; break;
2222 case 8: EVT = MVT::i8; break;
2223 case 16: EVT = MVT::i16; break;
2224 case 32: EVT = MVT::i32; break;
2225 }
2226 if (EVT > MVT::Other && TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, EVT))
2227 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
2228 DAG.getValueType(EVT));
2229 }
Chris Lattner71d9ebc2006-02-28 06:23:04 +00002230
2231 // fold (sra (sra x, c1), c2) -> (sra x, c1+c2)
2232 if (N1C && N0.getOpcode() == ISD::SRA) {
2233 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2234 unsigned Sum = N1C->getValue() + C1->getValue();
2235 if (Sum >= MVT::getSizeInBits(VT)) Sum = MVT::getSizeInBits(VT)-1;
2236 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0),
2237 DAG.getConstant(Sum, N1C->getValueType(0)));
2238 }
2239 }
2240
Chris Lattnera8504462006-05-08 20:51:54 +00002241 // Simplify, based on bits shifted out of the LHS.
2242 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
2243 return SDOperand(N, 0);
2244
2245
Nate Begeman1d4d4142005-09-01 00:19:25 +00002246 // If the sign bit is known to be zero, switch this to a SRL.
Dan Gohmanea859be2007-06-22 14:59:07 +00002247 if (DAG.MaskedValueIsZero(N0, MVT::getIntVTSignBit(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002248 return DAG.getNode(ISD::SRL, VT, N0, N1);
2249 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002250}
2251
Nate Begeman83e75ec2005-09-06 04:43:02 +00002252SDOperand DAGCombiner::visitSRL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002253 SDOperand N0 = N->getOperand(0);
2254 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002255 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2256 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002257 MVT::ValueType VT = N0.getValueType();
2258 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
2259
2260 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002261 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002262 return DAG.getNode(ISD::SRL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002263 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002264 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002265 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002266 // fold (srl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00002267 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002268 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002269 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002270 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002271 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002272 // if (srl x, c) is known to be zero, return 0
Dan Gohmanea859be2007-06-22 14:59:07 +00002273 if (N1C && DAG.MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002274 return DAG.getConstant(0, VT);
Chris Lattnerec06e9a2007-04-18 03:05:22 +00002275
Nate Begeman1d4d4142005-09-01 00:19:25 +00002276 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002277 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002278 N0.getOperand(1).getOpcode() == ISD::Constant) {
2279 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00002280 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002281 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002282 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002283 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002284 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002285 }
Chris Lattner350bec02006-04-02 06:11:11 +00002286
Chris Lattner06afe072006-05-05 22:53:17 +00002287 // fold (srl (anyextend x), c) -> (anyextend (srl x, c))
2288 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
2289 // Shifting in all undef bits?
2290 MVT::ValueType SmallVT = N0.getOperand(0).getValueType();
2291 if (N1C->getValue() >= MVT::getSizeInBits(SmallVT))
2292 return DAG.getNode(ISD::UNDEF, VT);
2293
2294 SDOperand SmallShift = DAG.getNode(ISD::SRL, SmallVT, N0.getOperand(0), N1);
2295 AddToWorkList(SmallShift.Val);
2296 return DAG.getNode(ISD::ANY_EXTEND, VT, SmallShift);
2297 }
2298
Chris Lattner3657ffe2006-10-12 20:23:19 +00002299 // fold (srl (sra X, Y), 31) -> (srl X, 31). This srl only looks at the sign
2300 // bit, which is unmodified by sra.
2301 if (N1C && N1C->getValue()+1 == MVT::getSizeInBits(VT)) {
2302 if (N0.getOpcode() == ISD::SRA)
2303 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0), N1);
2304 }
2305
Chris Lattner350bec02006-04-02 06:11:11 +00002306 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
2307 if (N1C && N0.getOpcode() == ISD::CTLZ &&
2308 N1C->getValue() == Log2_32(MVT::getSizeInBits(VT))) {
2309 uint64_t KnownZero, KnownOne, Mask = MVT::getIntVTBitMask(VT);
Dan Gohmanea859be2007-06-22 14:59:07 +00002310 DAG.ComputeMaskedBits(N0.getOperand(0), Mask, KnownZero, KnownOne);
Chris Lattner350bec02006-04-02 06:11:11 +00002311
2312 // If any of the input bits are KnownOne, then the input couldn't be all
2313 // zeros, thus the result of the srl will always be zero.
2314 if (KnownOne) return DAG.getConstant(0, VT);
2315
2316 // If all of the bits input the to ctlz node are known to be zero, then
2317 // the result of the ctlz is "32" and the result of the shift is one.
2318 uint64_t UnknownBits = ~KnownZero & Mask;
2319 if (UnknownBits == 0) return DAG.getConstant(1, VT);
2320
2321 // Otherwise, check to see if there is exactly one bit input to the ctlz.
2322 if ((UnknownBits & (UnknownBits-1)) == 0) {
2323 // Okay, we know that only that the single bit specified by UnknownBits
2324 // could be set on input to the CTLZ node. If this bit is set, the SRL
2325 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
2326 // to an SRL,XOR pair, which is likely to simplify more.
2327 unsigned ShAmt = CountTrailingZeros_64(UnknownBits);
2328 SDOperand Op = N0.getOperand(0);
2329 if (ShAmt) {
2330 Op = DAG.getNode(ISD::SRL, VT, Op,
2331 DAG.getConstant(ShAmt, TLI.getShiftAmountTy()));
2332 AddToWorkList(Op.Val);
2333 }
2334 return DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(1, VT));
2335 }
2336 }
Chris Lattner61a4c072007-04-18 03:06:49 +00002337
2338 // fold operands of srl based on knowledge that the low bits are not
2339 // demanded.
2340 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
2341 return SDOperand(N, 0);
2342
Nate Begeman83e75ec2005-09-06 04:43:02 +00002343 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002344}
2345
Nate Begeman83e75ec2005-09-06 04:43:02 +00002346SDOperand DAGCombiner::visitCTLZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002347 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00002348 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002349
2350 // fold (ctlz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002351 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002352 return DAG.getNode(ISD::CTLZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002353 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002354}
2355
Nate Begeman83e75ec2005-09-06 04:43:02 +00002356SDOperand DAGCombiner::visitCTTZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002357 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00002358 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002359
2360 // fold (cttz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002361 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002362 return DAG.getNode(ISD::CTTZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002363 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002364}
2365
Nate Begeman83e75ec2005-09-06 04:43:02 +00002366SDOperand DAGCombiner::visitCTPOP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002367 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00002368 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002369
2370 // fold (ctpop c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002371 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002372 return DAG.getNode(ISD::CTPOP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002373 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002374}
2375
Nate Begeman452d7be2005-09-16 00:54:12 +00002376SDOperand DAGCombiner::visitSELECT(SDNode *N) {
2377 SDOperand N0 = N->getOperand(0);
2378 SDOperand N1 = N->getOperand(1);
2379 SDOperand N2 = N->getOperand(2);
2380 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2381 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2382 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
2383 MVT::ValueType VT = N->getValueType(0);
Evan Cheng571c4782007-08-18 05:57:05 +00002384 MVT::ValueType VT0 = N0.getValueType();
Nate Begeman44728a72005-09-19 22:34:01 +00002385
Nate Begeman452d7be2005-09-16 00:54:12 +00002386 // fold select C, X, X -> X
2387 if (N1 == N2)
2388 return N1;
2389 // fold select true, X, Y -> X
2390 if (N0C && !N0C->isNullValue())
2391 return N1;
2392 // fold select false, X, Y -> Y
2393 if (N0C && N0C->isNullValue())
2394 return N2;
2395 // fold select C, 1, X -> C | X
Nate Begeman44728a72005-09-19 22:34:01 +00002396 if (MVT::i1 == VT && N1C && N1C->getValue() == 1)
Nate Begeman452d7be2005-09-16 00:54:12 +00002397 return DAG.getNode(ISD::OR, VT, N0, N2);
Evan Cheng571c4782007-08-18 05:57:05 +00002398 // fold select C, 0, 1 -> ~C
2399 if (MVT::isInteger(VT) && MVT::isInteger(VT0) &&
2400 N1C && N2C && N1C->isNullValue() && N2C->getValue() == 1) {
2401 SDOperand XORNode = DAG.getNode(ISD::XOR, VT0, N0, DAG.getConstant(1, VT0));
2402 if (VT == VT0)
2403 return XORNode;
2404 AddToWorkList(XORNode.Val);
2405 if (MVT::getSizeInBits(VT) > MVT::getSizeInBits(VT0))
2406 return DAG.getNode(ISD::ZERO_EXTEND, VT, XORNode);
2407 return DAG.getNode(ISD::TRUNCATE, VT, XORNode);
2408 }
Nate Begeman452d7be2005-09-16 00:54:12 +00002409 // fold select C, 0, X -> ~C & X
Evan Cheng571c4782007-08-18 05:57:05 +00002410 if (VT == VT0 && N1C && N1C->isNullValue()) {
Nate Begeman452d7be2005-09-16 00:54:12 +00002411 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00002412 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00002413 return DAG.getNode(ISD::AND, VT, XORNode, N2);
2414 }
2415 // fold select C, X, 1 -> ~C | X
Evan Cheng571c4782007-08-18 05:57:05 +00002416 if (VT == VT0 && N2C && N2C->getValue() == 1) {
Nate Begeman452d7be2005-09-16 00:54:12 +00002417 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00002418 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00002419 return DAG.getNode(ISD::OR, VT, XORNode, N1);
2420 }
2421 // fold select C, X, 0 -> C & X
2422 // FIXME: this should check for C type == X type, not i1?
2423 if (MVT::i1 == VT && N2C && N2C->isNullValue())
2424 return DAG.getNode(ISD::AND, VT, N0, N1);
2425 // fold X ? X : Y --> X ? 1 : Y --> X | Y
2426 if (MVT::i1 == VT && N0 == N1)
2427 return DAG.getNode(ISD::OR, VT, N0, N2);
2428 // fold X ? Y : X --> X ? Y : 0 --> X & Y
2429 if (MVT::i1 == VT && N0 == N2)
2430 return DAG.getNode(ISD::AND, VT, N0, N1);
Chris Lattner729c6d12006-05-27 00:43:02 +00002431
Chris Lattner40c62d52005-10-18 06:04:22 +00002432 // If we can fold this based on the true/false value, do so.
2433 if (SimplifySelectOps(N, N1, N2))
Chris Lattner729c6d12006-05-27 00:43:02 +00002434 return SDOperand(N, 0); // Don't revisit N.
2435
Nate Begeman44728a72005-09-19 22:34:01 +00002436 // fold selects based on a setcc into other things, such as min/max/abs
2437 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman750ac1b2006-02-01 07:19:44 +00002438 // FIXME:
2439 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
2440 // having to say they don't support SELECT_CC on every type the DAG knows
2441 // about, since there is no way to mark an opcode illegal at all value types
2442 if (TLI.isOperationLegal(ISD::SELECT_CC, MVT::Other))
2443 return DAG.getNode(ISD::SELECT_CC, VT, N0.getOperand(0), N0.getOperand(1),
2444 N1, N2, N0.getOperand(2));
2445 else
2446 return SimplifySelect(N0, N1, N2);
Nate Begeman452d7be2005-09-16 00:54:12 +00002447 return SDOperand();
2448}
2449
2450SDOperand DAGCombiner::visitSELECT_CC(SDNode *N) {
Nate Begeman44728a72005-09-19 22:34:01 +00002451 SDOperand N0 = N->getOperand(0);
2452 SDOperand N1 = N->getOperand(1);
2453 SDOperand N2 = N->getOperand(2);
2454 SDOperand N3 = N->getOperand(3);
2455 SDOperand N4 = N->getOperand(4);
Nate Begeman44728a72005-09-19 22:34:01 +00002456 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
2457
Nate Begeman44728a72005-09-19 22:34:01 +00002458 // fold select_cc lhs, rhs, x, x, cc -> x
2459 if (N2 == N3)
2460 return N2;
Chris Lattner40c62d52005-10-18 06:04:22 +00002461
Chris Lattner5f42a242006-09-20 06:19:26 +00002462 // Determine if the condition we're dealing with is constant
2463 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
Chris Lattner30f73e72006-10-14 03:52:46 +00002464 if (SCC.Val) AddToWorkList(SCC.Val);
Chris Lattner5f42a242006-09-20 06:19:26 +00002465
2466 if (ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val)) {
2467 if (SCCC->getValue())
2468 return N2; // cond always true -> true val
2469 else
2470 return N3; // cond always false -> false val
2471 }
2472
2473 // Fold to a simpler select_cc
2474 if (SCC.Val && SCC.getOpcode() == ISD::SETCC)
2475 return DAG.getNode(ISD::SELECT_CC, N2.getValueType(),
2476 SCC.getOperand(0), SCC.getOperand(1), N2, N3,
2477 SCC.getOperand(2));
2478
Chris Lattner40c62d52005-10-18 06:04:22 +00002479 // If we can fold this based on the true/false value, do so.
2480 if (SimplifySelectOps(N, N2, N3))
Chris Lattner729c6d12006-05-27 00:43:02 +00002481 return SDOperand(N, 0); // Don't revisit N.
Chris Lattner40c62d52005-10-18 06:04:22 +00002482
Nate Begeman44728a72005-09-19 22:34:01 +00002483 // fold select_cc into other things, such as min/max/abs
2484 return SimplifySelectCC(N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00002485}
2486
2487SDOperand DAGCombiner::visitSETCC(SDNode *N) {
2488 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
2489 cast<CondCodeSDNode>(N->getOperand(2))->get());
2490}
2491
Nate Begeman83e75ec2005-09-06 04:43:02 +00002492SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002493 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002494 MVT::ValueType VT = N->getValueType(0);
2495
Nate Begeman1d4d4142005-09-01 00:19:25 +00002496 // fold (sext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00002497 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002498 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0);
Chris Lattner310b5782006-05-06 23:06:26 +00002499
Nate Begeman1d4d4142005-09-01 00:19:25 +00002500 // fold (sext (sext x)) -> (sext x)
Chris Lattner310b5782006-05-06 23:06:26 +00002501 // fold (sext (aext x)) -> (sext x)
2502 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002503 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
Chris Lattner310b5782006-05-06 23:06:26 +00002504
Evan Chengc88138f2007-03-22 01:54:19 +00002505 // fold (sext (truncate (load x))) -> (sext (smaller load x))
2506 // fold (sext (truncate (srl (load x), c))) -> (sext (smaller load (x+c/n)))
Chris Lattner22558872007-02-26 03:13:59 +00002507 if (N0.getOpcode() == ISD::TRUNCATE) {
Evan Chengc88138f2007-03-22 01:54:19 +00002508 SDOperand NarrowLoad = ReduceLoadWidth(N0.Val);
Evan Cheng0b063de2007-03-23 02:16:52 +00002509 if (NarrowLoad.Val) {
2510 if (NarrowLoad.Val != N0.Val)
2511 CombineTo(N0.Val, NarrowLoad);
2512 return DAG.getNode(ISD::SIGN_EXTEND, VT, NarrowLoad);
2513 }
Evan Chengc88138f2007-03-22 01:54:19 +00002514 }
2515
2516 // See if the value being truncated is already sign extended. If so, just
2517 // eliminate the trunc/sext pair.
2518 if (N0.getOpcode() == ISD::TRUNCATE) {
Chris Lattner6007b842006-09-21 06:00:20 +00002519 SDOperand Op = N0.getOperand(0);
Chris Lattner22558872007-02-26 03:13:59 +00002520 unsigned OpBits = MVT::getSizeInBits(Op.getValueType());
2521 unsigned MidBits = MVT::getSizeInBits(N0.getValueType());
2522 unsigned DestBits = MVT::getSizeInBits(VT);
Dan Gohmanea859be2007-06-22 14:59:07 +00002523 unsigned NumSignBits = DAG.ComputeNumSignBits(Op);
Chris Lattner22558872007-02-26 03:13:59 +00002524
2525 if (OpBits == DestBits) {
2526 // Op is i32, Mid is i8, and Dest is i32. If Op has more than 24 sign
2527 // bits, it is already ready.
2528 if (NumSignBits > DestBits-MidBits)
2529 return Op;
2530 } else if (OpBits < DestBits) {
2531 // Op is i32, Mid is i8, and Dest is i64. If Op has more than 24 sign
2532 // bits, just sext from i32.
2533 if (NumSignBits > OpBits-MidBits)
2534 return DAG.getNode(ISD::SIGN_EXTEND, VT, Op);
2535 } else {
2536 // Op is i64, Mid is i8, and Dest is i32. If Op has more than 56 sign
2537 // bits, just truncate to i32.
2538 if (NumSignBits > OpBits-MidBits)
2539 return DAG.getNode(ISD::TRUNCATE, VT, Op);
Chris Lattner6007b842006-09-21 06:00:20 +00002540 }
Chris Lattner22558872007-02-26 03:13:59 +00002541
2542 // fold (sext (truncate x)) -> (sextinreg x).
2543 if (!AfterLegalize || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG,
2544 N0.getValueType())) {
2545 if (Op.getValueType() < VT)
2546 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
2547 else if (Op.getValueType() > VT)
2548 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
2549 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, Op,
2550 DAG.getValueType(N0.getValueType()));
2551 }
Chris Lattner6007b842006-09-21 06:00:20 +00002552 }
Chris Lattner310b5782006-05-06 23:06:26 +00002553
Evan Cheng110dec22005-12-14 02:19:23 +00002554 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00002555 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00002556 (!AfterLegalize||TLI.isLoadXLegal(ISD::SEXTLOAD, N0.getValueType()))){
Evan Cheng466685d2006-10-09 20:57:25 +00002557 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2558 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2559 LN0->getBasePtr(), LN0->getSrcValue(),
2560 LN0->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002561 N0.getValueType(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002562 LN0->isVolatile(),
2563 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00002564 CombineTo(N, ExtLoad);
Chris Lattnerf9884052005-10-13 21:52:31 +00002565 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2566 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002567 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begeman3df4d522005-10-12 20:40:40 +00002568 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002569
2570 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
2571 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00002572 if ((ISD::isSEXTLoad(N0.Val) || ISD::isEXTLoad(N0.Val)) &&
2573 ISD::isUNINDEXEDLoad(N0.Val) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002574 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00002575 MVT::ValueType EVT = LN0->getLoadedVT();
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00002576 if (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT)) {
2577 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2578 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002579 LN0->getSrcValueOffset(), EVT,
2580 LN0->isVolatile(),
2581 LN0->getAlignment());
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00002582 CombineTo(N, ExtLoad);
2583 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2584 ExtLoad.getValue(1));
2585 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2586 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002587 }
2588
Chris Lattner20a35c32007-04-11 05:32:27 +00002589 // sext(setcc x,y,cc) -> select_cc x, y, -1, 0, cc
2590 if (N0.getOpcode() == ISD::SETCC) {
2591 SDOperand SCC =
Chris Lattner1eba01e2007-04-11 06:50:51 +00002592 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
2593 DAG.getConstant(~0ULL, VT), DAG.getConstant(0, VT),
2594 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
2595 if (SCC.Val) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00002596 }
2597
Nate Begeman83e75ec2005-09-06 04:43:02 +00002598 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002599}
2600
Nate Begeman83e75ec2005-09-06 04:43:02 +00002601SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002602 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002603 MVT::ValueType VT = N->getValueType(0);
2604
Nate Begeman1d4d4142005-09-01 00:19:25 +00002605 // fold (zext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00002606 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002607 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002608 // fold (zext (zext x)) -> (zext x)
Chris Lattner310b5782006-05-06 23:06:26 +00002609 // fold (zext (aext x)) -> (zext x)
2610 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002611 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
Chris Lattner6007b842006-09-21 06:00:20 +00002612
Evan Chengc88138f2007-03-22 01:54:19 +00002613 // fold (zext (truncate (load x))) -> (zext (smaller load x))
2614 // fold (zext (truncate (srl (load x), c))) -> (zext (small load (x+c/n)))
Dale Johannesen2041a0e2007-03-30 21:38:07 +00002615 if (N0.getOpcode() == ISD::TRUNCATE) {
Evan Chengc88138f2007-03-22 01:54:19 +00002616 SDOperand NarrowLoad = ReduceLoadWidth(N0.Val);
Evan Cheng0b063de2007-03-23 02:16:52 +00002617 if (NarrowLoad.Val) {
2618 if (NarrowLoad.Val != N0.Val)
2619 CombineTo(N0.Val, NarrowLoad);
2620 return DAG.getNode(ISD::ZERO_EXTEND, VT, NarrowLoad);
2621 }
Evan Chengc88138f2007-03-22 01:54:19 +00002622 }
2623
Chris Lattner6007b842006-09-21 06:00:20 +00002624 // fold (zext (truncate x)) -> (and x, mask)
2625 if (N0.getOpcode() == ISD::TRUNCATE &&
2626 (!AfterLegalize || TLI.isOperationLegal(ISD::AND, VT))) {
2627 SDOperand Op = N0.getOperand(0);
2628 if (Op.getValueType() < VT) {
2629 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
2630 } else if (Op.getValueType() > VT) {
2631 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
2632 }
2633 return DAG.getZeroExtendInReg(Op, N0.getValueType());
2634 }
2635
Chris Lattner111c2282006-09-21 06:14:31 +00002636 // fold (zext (and (trunc x), cst)) -> (and x, cst).
2637 if (N0.getOpcode() == ISD::AND &&
2638 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
2639 N0.getOperand(1).getOpcode() == ISD::Constant) {
2640 SDOperand X = N0.getOperand(0).getOperand(0);
2641 if (X.getValueType() < VT) {
2642 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
2643 } else if (X.getValueType() > VT) {
2644 X = DAG.getNode(ISD::TRUNCATE, VT, X);
2645 }
2646 uint64_t Mask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
2647 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
2648 }
2649
Evan Cheng110dec22005-12-14 02:19:23 +00002650 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00002651 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00002652 (!AfterLegalize||TLI.isLoadXLegal(ISD::ZEXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00002653 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2654 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
2655 LN0->getBasePtr(), LN0->getSrcValue(),
2656 LN0->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002657 N0.getValueType(),
2658 LN0->isVolatile(),
2659 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00002660 CombineTo(N, ExtLoad);
Evan Cheng110dec22005-12-14 02:19:23 +00002661 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2662 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002663 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng110dec22005-12-14 02:19:23 +00002664 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002665
2666 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
2667 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00002668 if ((ISD::isZEXTLoad(N0.Val) || ISD::isEXTLoad(N0.Val)) &&
2669 ISD::isUNINDEXEDLoad(N0.Val) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002670 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00002671 MVT::ValueType EVT = LN0->getLoadedVT();
Evan Cheng466685d2006-10-09 20:57:25 +00002672 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
2673 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002674 LN0->getSrcValueOffset(), EVT,
2675 LN0->isVolatile(),
2676 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00002677 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002678 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2679 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002680 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002681 }
Chris Lattner20a35c32007-04-11 05:32:27 +00002682
2683 // zext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
2684 if (N0.getOpcode() == ISD::SETCC) {
2685 SDOperand SCC =
2686 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
2687 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattner1eba01e2007-04-11 06:50:51 +00002688 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
2689 if (SCC.Val) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00002690 }
2691
Nate Begeman83e75ec2005-09-06 04:43:02 +00002692 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002693}
2694
Chris Lattner5ffc0662006-05-05 05:58:59 +00002695SDOperand DAGCombiner::visitANY_EXTEND(SDNode *N) {
2696 SDOperand N0 = N->getOperand(0);
Chris Lattner5ffc0662006-05-05 05:58:59 +00002697 MVT::ValueType VT = N->getValueType(0);
2698
2699 // fold (aext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00002700 if (isa<ConstantSDNode>(N0))
Chris Lattner5ffc0662006-05-05 05:58:59 +00002701 return DAG.getNode(ISD::ANY_EXTEND, VT, N0);
2702 // fold (aext (aext x)) -> (aext x)
2703 // fold (aext (zext x)) -> (zext x)
2704 // fold (aext (sext x)) -> (sext x)
2705 if (N0.getOpcode() == ISD::ANY_EXTEND ||
2706 N0.getOpcode() == ISD::ZERO_EXTEND ||
2707 N0.getOpcode() == ISD::SIGN_EXTEND)
2708 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
2709
Evan Chengc88138f2007-03-22 01:54:19 +00002710 // fold (aext (truncate (load x))) -> (aext (smaller load x))
2711 // fold (aext (truncate (srl (load x), c))) -> (aext (small load (x+c/n)))
2712 if (N0.getOpcode() == ISD::TRUNCATE) {
2713 SDOperand NarrowLoad = ReduceLoadWidth(N0.Val);
Evan Cheng0b063de2007-03-23 02:16:52 +00002714 if (NarrowLoad.Val) {
2715 if (NarrowLoad.Val != N0.Val)
2716 CombineTo(N0.Val, NarrowLoad);
2717 return DAG.getNode(ISD::ANY_EXTEND, VT, NarrowLoad);
2718 }
Evan Chengc88138f2007-03-22 01:54:19 +00002719 }
2720
Chris Lattner84750582006-09-20 06:29:17 +00002721 // fold (aext (truncate x))
2722 if (N0.getOpcode() == ISD::TRUNCATE) {
2723 SDOperand TruncOp = N0.getOperand(0);
2724 if (TruncOp.getValueType() == VT)
2725 return TruncOp; // x iff x size == zext size.
2726 if (TruncOp.getValueType() > VT)
2727 return DAG.getNode(ISD::TRUNCATE, VT, TruncOp);
2728 return DAG.getNode(ISD::ANY_EXTEND, VT, TruncOp);
2729 }
Chris Lattner0e4b9222006-09-21 06:40:43 +00002730
2731 // fold (aext (and (trunc x), cst)) -> (and x, cst).
2732 if (N0.getOpcode() == ISD::AND &&
2733 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
2734 N0.getOperand(1).getOpcode() == ISD::Constant) {
2735 SDOperand X = N0.getOperand(0).getOperand(0);
2736 if (X.getValueType() < VT) {
2737 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
2738 } else if (X.getValueType() > VT) {
2739 X = DAG.getNode(ISD::TRUNCATE, VT, X);
2740 }
2741 uint64_t Mask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
2742 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
2743 }
2744
Chris Lattner5ffc0662006-05-05 05:58:59 +00002745 // fold (aext (load x)) -> (aext (truncate (extload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00002746 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00002747 (!AfterLegalize||TLI.isLoadXLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00002748 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2749 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
2750 LN0->getBasePtr(), LN0->getSrcValue(),
2751 LN0->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002752 N0.getValueType(),
2753 LN0->isVolatile(),
2754 LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00002755 CombineTo(N, ExtLoad);
2756 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2757 ExtLoad.getValue(1));
2758 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2759 }
2760
2761 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
2762 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
2763 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00002764 if (N0.getOpcode() == ISD::LOAD &&
2765 !ISD::isNON_EXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val) &&
Evan Cheng466685d2006-10-09 20:57:25 +00002766 N0.hasOneUse()) {
2767 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00002768 MVT::ValueType EVT = LN0->getLoadedVT();
Evan Cheng466685d2006-10-09 20:57:25 +00002769 SDOperand ExtLoad = DAG.getExtLoad(LN0->getExtensionType(), VT,
2770 LN0->getChain(), LN0->getBasePtr(),
2771 LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002772 LN0->getSrcValueOffset(), EVT,
2773 LN0->isVolatile(),
2774 LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00002775 CombineTo(N, ExtLoad);
2776 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2777 ExtLoad.getValue(1));
2778 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2779 }
Chris Lattner20a35c32007-04-11 05:32:27 +00002780
2781 // aext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
2782 if (N0.getOpcode() == ISD::SETCC) {
2783 SDOperand SCC =
Chris Lattner1eba01e2007-04-11 06:50:51 +00002784 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
2785 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattnerc24bbad2007-04-11 16:51:53 +00002786 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Chris Lattner1eba01e2007-04-11 06:50:51 +00002787 if (SCC.Val)
Chris Lattnerc56a81d2007-04-11 06:43:25 +00002788 return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00002789 }
2790
Chris Lattner5ffc0662006-05-05 05:58:59 +00002791 return SDOperand();
2792}
2793
Chris Lattner2b4c2792007-10-13 06:35:54 +00002794/// GetDemandedBits - See if the specified operand can be simplified with the
2795/// knowledge that only the bits specified by Mask are used. If so, return the
2796/// simpler operand, otherwise return a null SDOperand.
2797SDOperand DAGCombiner::GetDemandedBits(SDOperand V, uint64_t Mask) {
2798 switch (V.getOpcode()) {
2799 default: break;
2800 case ISD::OR:
2801 case ISD::XOR:
2802 // If the LHS or RHS don't contribute bits to the or, drop them.
2803 if (DAG.MaskedValueIsZero(V.getOperand(0), Mask))
2804 return V.getOperand(1);
2805 if (DAG.MaskedValueIsZero(V.getOperand(1), Mask))
2806 return V.getOperand(0);
2807 break;
Chris Lattnere33544c2007-10-13 06:58:48 +00002808 case ISD::SRL:
2809 // Only look at single-use SRLs.
2810 if (!V.Val->hasOneUse())
2811 break;
2812 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
2813 // See if we can recursively simplify the LHS.
2814 unsigned Amt = RHSC->getValue();
2815 Mask = (Mask << Amt) & MVT::getIntVTBitMask(V.getValueType());
2816 SDOperand SimplifyLHS = GetDemandedBits(V.getOperand(0), Mask);
2817 if (SimplifyLHS.Val) {
2818 return DAG.getNode(ISD::SRL, V.getValueType(),
2819 SimplifyLHS, V.getOperand(1));
2820 }
2821 }
Chris Lattner2b4c2792007-10-13 06:35:54 +00002822 }
2823 return SDOperand();
2824}
2825
Evan Chengc88138f2007-03-22 01:54:19 +00002826/// ReduceLoadWidth - If the result of a wider load is shifted to right of N
2827/// bits and then truncated to a narrower type and where N is a multiple
2828/// of number of bits of the narrower type, transform it to a narrower load
2829/// from address + N / num of bits of new type. If the result is to be
2830/// extended, also fold the extension to form a extending load.
2831SDOperand DAGCombiner::ReduceLoadWidth(SDNode *N) {
2832 unsigned Opc = N->getOpcode();
2833 ISD::LoadExtType ExtType = ISD::NON_EXTLOAD;
2834 SDOperand N0 = N->getOperand(0);
2835 MVT::ValueType VT = N->getValueType(0);
2836 MVT::ValueType EVT = N->getValueType(0);
2837
Evan Chenge177e302007-03-23 22:13:36 +00002838 // Special case: SIGN_EXTEND_INREG is basically truncating to EVT then
2839 // extended to VT.
Evan Chengc88138f2007-03-22 01:54:19 +00002840 if (Opc == ISD::SIGN_EXTEND_INREG) {
2841 ExtType = ISD::SEXTLOAD;
2842 EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Evan Chenge177e302007-03-23 22:13:36 +00002843 if (AfterLegalize && !TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))
2844 return SDOperand();
Evan Chengc88138f2007-03-22 01:54:19 +00002845 }
2846
2847 unsigned EVTBits = MVT::getSizeInBits(EVT);
2848 unsigned ShAmt = 0;
Evan Chengb37b80c2007-03-23 20:55:21 +00002849 bool CombineSRL = false;
Evan Chengc88138f2007-03-22 01:54:19 +00002850 if (N0.getOpcode() == ISD::SRL && N0.hasOneUse()) {
2851 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2852 ShAmt = N01->getValue();
2853 // Is the shift amount a multiple of size of VT?
2854 if ((ShAmt & (EVTBits-1)) == 0) {
2855 N0 = N0.getOperand(0);
2856 if (MVT::getSizeInBits(N0.getValueType()) <= EVTBits)
2857 return SDOperand();
Evan Chengb37b80c2007-03-23 20:55:21 +00002858 CombineSRL = true;
Evan Chengc88138f2007-03-22 01:54:19 +00002859 }
2860 }
2861 }
2862
2863 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
2864 // Do not allow folding to i1 here. i1 is implicitly stored in memory in
2865 // zero extended form: by shrinking the load, we lose track of the fact
2866 // that it is already zero extended.
2867 // FIXME: This should be reevaluated.
2868 VT != MVT::i1) {
2869 assert(MVT::getSizeInBits(N0.getValueType()) > EVTBits &&
2870 "Cannot truncate to larger type!");
2871 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2872 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
Evan Chengdae54ce2007-03-24 00:02:43 +00002873 // For big endian targets, we need to adjust the offset to the pointer to
2874 // load the correct bytes.
2875 if (!TLI.isLittleEndian())
2876 ShAmt = MVT::getSizeInBits(N0.getValueType()) - ShAmt - EVTBits;
2877 uint64_t PtrOff = ShAmt / 8;
Evan Chengc88138f2007-03-22 01:54:19 +00002878 SDOperand NewPtr = DAG.getNode(ISD::ADD, PtrType, LN0->getBasePtr(),
2879 DAG.getConstant(PtrOff, PtrType));
2880 AddToWorkList(NewPtr.Val);
2881 SDOperand Load = (ExtType == ISD::NON_EXTLOAD)
2882 ? DAG.getLoad(VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002883 LN0->getSrcValue(), LN0->getSrcValueOffset(),
2884 LN0->isVolatile(), LN0->getAlignment())
Evan Chengc88138f2007-03-22 01:54:19 +00002885 : DAG.getExtLoad(ExtType, VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002886 LN0->getSrcValue(), LN0->getSrcValueOffset(), EVT,
2887 LN0->isVolatile(), LN0->getAlignment());
Evan Chengc88138f2007-03-22 01:54:19 +00002888 AddToWorkList(N);
Evan Chengb37b80c2007-03-23 20:55:21 +00002889 if (CombineSRL) {
Chris Lattner01d029b2007-10-15 06:10:22 +00002890 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), Load.getValue(1));
Evan Chengb37b80c2007-03-23 20:55:21 +00002891 CombineTo(N->getOperand(0).Val, Load);
2892 } else
2893 CombineTo(N0.Val, Load, Load.getValue(1));
Evan Cheng15213b72007-03-26 07:12:51 +00002894 if (ShAmt) {
2895 if (Opc == ISD::SIGN_EXTEND_INREG)
2896 return DAG.getNode(Opc, VT, Load, N->getOperand(1));
2897 else
2898 return DAG.getNode(Opc, VT, Load);
2899 }
Evan Chengc88138f2007-03-22 01:54:19 +00002900 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2901 }
2902
2903 return SDOperand();
2904}
2905
Chris Lattner5ffc0662006-05-05 05:58:59 +00002906
Nate Begeman83e75ec2005-09-06 04:43:02 +00002907SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002908 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002909 SDOperand N1 = N->getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002910 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002911 MVT::ValueType EVT = cast<VTSDNode>(N1)->getVT();
Nate Begeman07ed4172005-10-10 21:26:48 +00002912 unsigned EVTBits = MVT::getSizeInBits(EVT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002913
Nate Begeman1d4d4142005-09-01 00:19:25 +00002914 // fold (sext_in_reg c1) -> c1
Chris Lattnereaeda562006-05-08 20:59:41 +00002915 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
Chris Lattner310b5782006-05-06 23:06:26 +00002916 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0, N1);
Chris Lattneree4ea922006-05-06 09:30:03 +00002917
Chris Lattner541a24f2006-05-06 22:43:44 +00002918 // If the input is already sign extended, just drop the extension.
Dan Gohmanea859be2007-06-22 14:59:07 +00002919 if (DAG.ComputeNumSignBits(N0) >= MVT::getSizeInBits(VT)-EVTBits+1)
Chris Lattneree4ea922006-05-06 09:30:03 +00002920 return N0;
2921
Nate Begeman646d7e22005-09-02 21:18:40 +00002922 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
2923 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
2924 EVT < cast<VTSDNode>(N0.getOperand(1))->getVT()) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00002925 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002926 }
Chris Lattner4b37e872006-05-08 21:18:59 +00002927
Chris Lattner95a5e052007-04-17 19:03:21 +00002928 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is known zero.
Dan Gohmanea859be2007-06-22 14:59:07 +00002929 if (DAG.MaskedValueIsZero(N0, 1ULL << (EVTBits-1)))
Nate Begemande996292006-02-03 22:24:05 +00002930 return DAG.getZeroExtendInReg(N0, EVT);
Chris Lattner4b37e872006-05-08 21:18:59 +00002931
Chris Lattner95a5e052007-04-17 19:03:21 +00002932 // fold operands of sext_in_reg based on knowledge that the top bits are not
2933 // demanded.
2934 if (SimplifyDemandedBits(SDOperand(N, 0)))
2935 return SDOperand(N, 0);
2936
Evan Chengc88138f2007-03-22 01:54:19 +00002937 // fold (sext_in_reg (load x)) -> (smaller sextload x)
2938 // fold (sext_in_reg (srl (load x), c)) -> (smaller sextload (x+c/evtbits))
2939 SDOperand NarrowLoad = ReduceLoadWidth(N);
2940 if (NarrowLoad.Val)
2941 return NarrowLoad;
2942
Chris Lattner4b37e872006-05-08 21:18:59 +00002943 // fold (sext_in_reg (srl X, 24), i8) -> sra X, 24
2944 // fold (sext_in_reg (srl X, 23), i8) -> sra X, 23 iff possible.
2945 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
2946 if (N0.getOpcode() == ISD::SRL) {
2947 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
2948 if (ShAmt->getValue()+EVTBits <= MVT::getSizeInBits(VT)) {
2949 // We can turn this into an SRA iff the input to the SRL is already sign
2950 // extended enough.
Dan Gohmanea859be2007-06-22 14:59:07 +00002951 unsigned InSignBits = DAG.ComputeNumSignBits(N0.getOperand(0));
Chris Lattner4b37e872006-05-08 21:18:59 +00002952 if (MVT::getSizeInBits(VT)-(ShAmt->getValue()+EVTBits) < InSignBits)
2953 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0), N0.getOperand(1));
2954 }
2955 }
Evan Chengc88138f2007-03-22 01:54:19 +00002956
Nate Begemanded49632005-10-13 03:11:28 +00002957 // fold (sext_inreg (extload x)) -> (sextload x)
Evan Chengc5484282006-10-04 00:56:09 +00002958 if (ISD::isEXTLoad(N0.Val) &&
Evan Cheng83060c52007-03-07 08:07:03 +00002959 ISD::isUNINDEXEDLoad(N0.Val) &&
Evan Cheng2e49f092006-10-11 07:10:22 +00002960 EVT == cast<LoadSDNode>(N0)->getLoadedVT() &&
Evan Chengc5484282006-10-04 00:56:09 +00002961 (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00002962 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2963 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2964 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002965 LN0->getSrcValueOffset(), EVT,
2966 LN0->isVolatile(),
2967 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00002968 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00002969 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002970 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00002971 }
2972 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Evan Cheng83060c52007-03-07 08:07:03 +00002973 if (ISD::isZEXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val) &&
2974 N0.hasOneUse() &&
Evan Cheng2e49f092006-10-11 07:10:22 +00002975 EVT == cast<LoadSDNode>(N0)->getLoadedVT() &&
Evan Chengc5484282006-10-04 00:56:09 +00002976 (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00002977 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2978 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2979 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002980 LN0->getSrcValueOffset(), EVT,
2981 LN0->isVolatile(),
2982 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00002983 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00002984 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002985 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00002986 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00002987 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002988}
2989
Nate Begeman83e75ec2005-09-06 04:43:02 +00002990SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002991 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002992 MVT::ValueType VT = N->getValueType(0);
2993
2994 // noop truncate
2995 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002996 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002997 // fold (truncate c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00002998 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002999 return DAG.getNode(ISD::TRUNCATE, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003000 // fold (truncate (truncate x)) -> (truncate x)
3001 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00003002 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003003 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
Chris Lattnerb72773b2006-05-05 22:56:26 +00003004 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND||
3005 N0.getOpcode() == ISD::ANY_EXTEND) {
Chris Lattner32ba1aa2006-11-20 18:05:46 +00003006 if (N0.getOperand(0).getValueType() < VT)
Nate Begeman1d4d4142005-09-01 00:19:25 +00003007 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00003008 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Chris Lattner32ba1aa2006-11-20 18:05:46 +00003009 else if (N0.getOperand(0).getValueType() > VT)
Nate Begeman1d4d4142005-09-01 00:19:25 +00003010 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00003011 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003012 else
3013 // if the source and dest are the same type, we can drop both the extend
3014 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00003015 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003016 }
Evan Cheng007b69e2007-03-21 20:14:05 +00003017
Chris Lattner2b4c2792007-10-13 06:35:54 +00003018 // See if we can simplify the input to this truncate through knowledge that
3019 // only the low bits are being used. For example "trunc (or (shl x, 8), y)"
3020 // -> trunc y
3021 SDOperand Shorter = GetDemandedBits(N0, MVT::getIntVTBitMask(VT));
3022 if (Shorter.Val)
3023 return DAG.getNode(ISD::TRUNCATE, VT, Shorter);
3024
Nate Begeman3df4d522005-10-12 20:40:40 +00003025 // fold (truncate (load x)) -> (smaller load x)
Evan Cheng007b69e2007-03-21 20:14:05 +00003026 // fold (truncate (srl (load x), c)) -> (smaller load (x+c/evtbits))
Evan Chengc88138f2007-03-22 01:54:19 +00003027 return ReduceLoadWidth(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003028}
3029
Chris Lattner94683772005-12-23 05:30:37 +00003030SDOperand DAGCombiner::visitBIT_CONVERT(SDNode *N) {
3031 SDOperand N0 = N->getOperand(0);
3032 MVT::ValueType VT = N->getValueType(0);
3033
Dan Gohman7f321562007-06-25 16:23:39 +00003034 // If the input is a BUILD_VECTOR with all constant elements, fold this now.
3035 // Only do this before legalize, since afterward the target may be depending
3036 // on the bitconvert.
3037 // First check to see if this is all constant.
3038 if (!AfterLegalize &&
3039 N0.getOpcode() == ISD::BUILD_VECTOR && N0.Val->hasOneUse() &&
3040 MVT::isVector(VT)) {
3041 bool isSimple = true;
3042 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i)
3043 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
3044 N0.getOperand(i).getOpcode() != ISD::Constant &&
3045 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
3046 isSimple = false;
3047 break;
3048 }
3049
3050 MVT::ValueType DestEltVT = MVT::getVectorElementType(N->getValueType(0));
3051 assert(!MVT::isVector(DestEltVT) &&
3052 "Element type of vector ValueType must not be vector!");
3053 if (isSimple) {
3054 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(N0.Val, DestEltVT);
3055 }
3056 }
3057
Chris Lattner94683772005-12-23 05:30:37 +00003058 // If the input is a constant, let getNode() fold it.
3059 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
3060 SDOperand Res = DAG.getNode(ISD::BIT_CONVERT, VT, N0);
3061 if (Res.Val != N) return Res;
3062 }
3063
Chris Lattnerc8547d82005-12-23 05:37:50 +00003064 if (N0.getOpcode() == ISD::BIT_CONVERT) // conv(conv(x,t1),t2) -> conv(x,t2)
3065 return DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
Chris Lattner6258fb22006-04-02 02:53:43 +00003066
Chris Lattner57104102005-12-23 05:44:41 +00003067 // fold (conv (load x)) -> (load (conv*)x)
Evan Cheng513da432007-10-06 08:19:55 +00003068 // If the resultant load doesn't need a higher alignment than the original!
3069 if (ISD::isNormalLoad(N0.Val) && N0.hasOneUse() &&
Evan Cheng59d5b682007-05-07 21:27:48 +00003070 TLI.isOperationLegal(ISD::LOAD, VT)) {
Evan Cheng466685d2006-10-09 20:57:25 +00003071 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng59d5b682007-05-07 21:27:48 +00003072 unsigned Align = TLI.getTargetMachine().getTargetData()->
Dan Gohmanfcc4dd92007-05-18 18:41:29 +00003073 getABITypeAlignment(MVT::getTypeForValueType(VT));
Evan Cheng59d5b682007-05-07 21:27:48 +00003074 unsigned OrigAlign = LN0->getAlignment();
3075 if (Align <= OrigAlign) {
3076 SDOperand Load = DAG.getLoad(VT, LN0->getChain(), LN0->getBasePtr(),
3077 LN0->getSrcValue(), LN0->getSrcValueOffset(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00003078 LN0->isVolatile(), Align);
Evan Cheng59d5b682007-05-07 21:27:48 +00003079 AddToWorkList(N);
3080 CombineTo(N0.Val, DAG.getNode(ISD::BIT_CONVERT, N0.getValueType(), Load),
3081 Load.getValue(1));
3082 return Load;
3083 }
Chris Lattner57104102005-12-23 05:44:41 +00003084 }
3085
Chris Lattner94683772005-12-23 05:30:37 +00003086 return SDOperand();
3087}
3088
Dan Gohman7f321562007-06-25 16:23:39 +00003089/// ConstantFoldBIT_CONVERTofBUILD_VECTOR - We know that BV is a build_vector
Chris Lattner6258fb22006-04-02 02:53:43 +00003090/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
3091/// destination element value type.
3092SDOperand DAGCombiner::
Dan Gohman7f321562007-06-25 16:23:39 +00003093ConstantFoldBIT_CONVERTofBUILD_VECTOR(SDNode *BV, MVT::ValueType DstEltVT) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003094 MVT::ValueType SrcEltVT = BV->getOperand(0).getValueType();
3095
3096 // If this is already the right type, we're done.
3097 if (SrcEltVT == DstEltVT) return SDOperand(BV, 0);
3098
3099 unsigned SrcBitSize = MVT::getSizeInBits(SrcEltVT);
3100 unsigned DstBitSize = MVT::getSizeInBits(DstEltVT);
3101
3102 // If this is a conversion of N elements of one type to N elements of another
3103 // type, convert each element. This handles FP<->INT cases.
3104 if (SrcBitSize == DstBitSize) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003105 SmallVector<SDOperand, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003106 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003107 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, DstEltVT, BV->getOperand(i)));
Chris Lattner3e104b12006-04-08 04:15:24 +00003108 AddToWorkList(Ops.back().Val);
3109 }
Dan Gohman7f321562007-06-25 16:23:39 +00003110 MVT::ValueType VT =
3111 MVT::getVectorType(DstEltVT,
3112 MVT::getVectorNumElements(BV->getValueType(0)));
3113 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003114 }
3115
3116 // Otherwise, we're growing or shrinking the elements. To avoid having to
3117 // handle annoying details of growing/shrinking FP values, we convert them to
3118 // int first.
3119 if (MVT::isFloatingPoint(SrcEltVT)) {
3120 // Convert the input float vector to a int vector where the elements are the
3121 // same sizes.
3122 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
3123 MVT::ValueType IntVT = SrcEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
Dan Gohman7f321562007-06-25 16:23:39 +00003124 BV = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, IntVT).Val;
Chris Lattner6258fb22006-04-02 02:53:43 +00003125 SrcEltVT = IntVT;
3126 }
3127
3128 // Now we know the input is an integer vector. If the output is a FP type,
3129 // convert to integer first, then to FP of the right size.
3130 if (MVT::isFloatingPoint(DstEltVT)) {
3131 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
3132 MVT::ValueType TmpVT = DstEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
Dan Gohman7f321562007-06-25 16:23:39 +00003133 SDNode *Tmp = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, TmpVT).Val;
Chris Lattner6258fb22006-04-02 02:53:43 +00003134
3135 // Next, convert to FP elements of the same size.
Dan Gohman7f321562007-06-25 16:23:39 +00003136 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(Tmp, DstEltVT);
Chris Lattner6258fb22006-04-02 02:53:43 +00003137 }
3138
3139 // Okay, we know the src/dst types are both integers of differing types.
3140 // Handling growing first.
3141 assert(MVT::isInteger(SrcEltVT) && MVT::isInteger(DstEltVT));
3142 if (SrcBitSize < DstBitSize) {
3143 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
3144
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003145 SmallVector<SDOperand, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003146 for (unsigned i = 0, e = BV->getNumOperands(); i != e;
Chris Lattner6258fb22006-04-02 02:53:43 +00003147 i += NumInputsPerOutput) {
3148 bool isLE = TLI.isLittleEndian();
3149 uint64_t NewBits = 0;
3150 bool EltIsUndef = true;
3151 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
3152 // Shift the previously computed bits over.
3153 NewBits <<= SrcBitSize;
3154 SDOperand Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
3155 if (Op.getOpcode() == ISD::UNDEF) continue;
3156 EltIsUndef = false;
3157
3158 NewBits |= cast<ConstantSDNode>(Op)->getValue();
3159 }
3160
3161 if (EltIsUndef)
3162 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
3163 else
3164 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
3165 }
3166
Dan Gohman7f321562007-06-25 16:23:39 +00003167 MVT::ValueType VT = MVT::getVectorType(DstEltVT,
3168 Ops.size());
3169 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003170 }
3171
3172 // Finally, this must be the case where we are shrinking elements: each input
3173 // turns into multiple outputs.
3174 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003175 SmallVector<SDOperand, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003176 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003177 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
3178 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
3179 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
3180 continue;
3181 }
3182 uint64_t OpVal = cast<ConstantSDNode>(BV->getOperand(i))->getValue();
3183
3184 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
3185 unsigned ThisVal = OpVal & ((1ULL << DstBitSize)-1);
3186 OpVal >>= DstBitSize;
3187 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
3188 }
3189
3190 // For big endian targets, swap the order of the pieces of each element.
3191 if (!TLI.isLittleEndian())
3192 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
3193 }
Dan Gohman7f321562007-06-25 16:23:39 +00003194 MVT::ValueType VT = MVT::getVectorType(DstEltVT, Ops.size());
3195 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003196}
3197
3198
3199
Chris Lattner01b3d732005-09-28 22:28:18 +00003200SDOperand DAGCombiner::visitFADD(SDNode *N) {
3201 SDOperand N0 = N->getOperand(0);
3202 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003203 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3204 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00003205 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00003206
Dan Gohman7f321562007-06-25 16:23:39 +00003207 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00003208 if (MVT::isVector(VT)) {
3209 SDOperand FoldedVOp = SimplifyVBinOp(N);
3210 if (FoldedVOp.Val) return FoldedVOp;
3211 }
Dan Gohman7f321562007-06-25 16:23:39 +00003212
Nate Begemana0e221d2005-10-18 00:28:13 +00003213 // fold (fadd c1, c2) -> c1+c2
3214 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00003215 return DAG.getNode(ISD::FADD, VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003216 // canonicalize constant to RHS
3217 if (N0CFP && !N1CFP)
3218 return DAG.getNode(ISD::FADD, VT, N1, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00003219 // fold (A + (-B)) -> A-B
Chris Lattner29446522007-05-14 22:04:50 +00003220 if (isNegatibleForFree(N1) == 2)
3221 return DAG.getNode(ISD::FSUB, VT, N0, GetNegatedExpression(N1, DAG));
Chris Lattner01b3d732005-09-28 22:28:18 +00003222 // fold ((-A) + B) -> B-A
Chris Lattner29446522007-05-14 22:04:50 +00003223 if (isNegatibleForFree(N0) == 2)
3224 return DAG.getNode(ISD::FSUB, VT, N1, GetNegatedExpression(N0, DAG));
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003225
3226 // If allowed, fold (fadd (fadd x, c1), c2) -> (fadd x, (fadd c1, c2))
3227 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FADD &&
3228 N0.Val->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
3229 return DAG.getNode(ISD::FADD, VT, N0.getOperand(0),
3230 DAG.getNode(ISD::FADD, VT, N0.getOperand(1), N1));
3231
Chris Lattner01b3d732005-09-28 22:28:18 +00003232 return SDOperand();
3233}
3234
3235SDOperand DAGCombiner::visitFSUB(SDNode *N) {
3236 SDOperand N0 = N->getOperand(0);
3237 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003238 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3239 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00003240 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00003241
Dan Gohman7f321562007-06-25 16:23:39 +00003242 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00003243 if (MVT::isVector(VT)) {
3244 SDOperand FoldedVOp = SimplifyVBinOp(N);
3245 if (FoldedVOp.Val) return FoldedVOp;
3246 }
Dan Gohman7f321562007-06-25 16:23:39 +00003247
Nate Begemana0e221d2005-10-18 00:28:13 +00003248 // fold (fsub c1, c2) -> c1-c2
3249 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00003250 return DAG.getNode(ISD::FSUB, VT, N0, N1);
Dan Gohman23ff1822007-07-02 15:48:56 +00003251 // fold (0-B) -> -B
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003252 if (UnsafeFPMath && N0CFP && N0CFP->getValueAPF().isZero()) {
Dan Gohman23ff1822007-07-02 15:48:56 +00003253 if (isNegatibleForFree(N1))
3254 return GetNegatedExpression(N1, DAG);
3255 return DAG.getNode(ISD::FNEG, VT, N1);
3256 }
Chris Lattner01b3d732005-09-28 22:28:18 +00003257 // fold (A-(-B)) -> A+B
Chris Lattner29446522007-05-14 22:04:50 +00003258 if (isNegatibleForFree(N1))
3259 return DAG.getNode(ISD::FADD, VT, N0, GetNegatedExpression(N1, DAG));
3260
Chris Lattner01b3d732005-09-28 22:28:18 +00003261 return SDOperand();
3262}
3263
3264SDOperand DAGCombiner::visitFMUL(SDNode *N) {
3265 SDOperand N0 = N->getOperand(0);
3266 SDOperand N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003267 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3268 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00003269 MVT::ValueType VT = N->getValueType(0);
3270
Dan Gohman7f321562007-06-25 16:23:39 +00003271 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00003272 if (MVT::isVector(VT)) {
3273 SDOperand FoldedVOp = SimplifyVBinOp(N);
3274 if (FoldedVOp.Val) return FoldedVOp;
3275 }
Dan Gohman7f321562007-06-25 16:23:39 +00003276
Nate Begeman11af4ea2005-10-17 20:40:11 +00003277 // fold (fmul c1, c2) -> c1*c2
3278 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00003279 return DAG.getNode(ISD::FMUL, VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003280 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00003281 if (N0CFP && !N1CFP)
3282 return DAG.getNode(ISD::FMUL, VT, N1, N0);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003283 // fold (fmul X, 2.0) -> (fadd X, X)
3284 if (N1CFP && N1CFP->isExactlyValue(+2.0))
3285 return DAG.getNode(ISD::FADD, VT, N0, N0);
Chris Lattner29446522007-05-14 22:04:50 +00003286 // fold (fmul X, -1.0) -> (fneg X)
3287 if (N1CFP && N1CFP->isExactlyValue(-1.0))
3288 return DAG.getNode(ISD::FNEG, VT, N0);
3289
3290 // -X * -Y -> X*Y
3291 if (char LHSNeg = isNegatibleForFree(N0)) {
3292 if (char RHSNeg = isNegatibleForFree(N1)) {
3293 // Both can be negated for free, check to see if at least one is cheaper
3294 // negated.
3295 if (LHSNeg == 2 || RHSNeg == 2)
3296 return DAG.getNode(ISD::FMUL, VT, GetNegatedExpression(N0, DAG),
3297 GetNegatedExpression(N1, DAG));
3298 }
3299 }
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003300
3301 // If allowed, fold (fmul (fmul x, c1), c2) -> (fmul x, (fmul c1, c2))
3302 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FMUL &&
3303 N0.Val->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
3304 return DAG.getNode(ISD::FMUL, VT, N0.getOperand(0),
3305 DAG.getNode(ISD::FMUL, VT, N0.getOperand(1), N1));
3306
Chris Lattner01b3d732005-09-28 22:28:18 +00003307 return SDOperand();
3308}
3309
3310SDOperand DAGCombiner::visitFDIV(SDNode *N) {
3311 SDOperand N0 = N->getOperand(0);
3312 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003313 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3314 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00003315 MVT::ValueType VT = N->getValueType(0);
3316
Dan Gohman7f321562007-06-25 16:23:39 +00003317 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00003318 if (MVT::isVector(VT)) {
3319 SDOperand FoldedVOp = SimplifyVBinOp(N);
3320 if (FoldedVOp.Val) return FoldedVOp;
3321 }
Dan Gohman7f321562007-06-25 16:23:39 +00003322
Nate Begemana148d982006-01-18 22:35:16 +00003323 // fold (fdiv c1, c2) -> c1/c2
3324 if (N0CFP && N1CFP)
3325 return DAG.getNode(ISD::FDIV, VT, N0, N1);
Chris Lattner29446522007-05-14 22:04:50 +00003326
3327
3328 // -X / -Y -> X*Y
3329 if (char LHSNeg = isNegatibleForFree(N0)) {
3330 if (char RHSNeg = isNegatibleForFree(N1)) {
3331 // Both can be negated for free, check to see if at least one is cheaper
3332 // negated.
3333 if (LHSNeg == 2 || RHSNeg == 2)
3334 return DAG.getNode(ISD::FDIV, VT, GetNegatedExpression(N0, DAG),
3335 GetNegatedExpression(N1, DAG));
3336 }
3337 }
3338
Chris Lattner01b3d732005-09-28 22:28:18 +00003339 return SDOperand();
3340}
3341
3342SDOperand DAGCombiner::visitFREM(SDNode *N) {
3343 SDOperand N0 = N->getOperand(0);
3344 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003345 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3346 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00003347 MVT::ValueType VT = N->getValueType(0);
3348
Nate Begemana148d982006-01-18 22:35:16 +00003349 // fold (frem c1, c2) -> fmod(c1,c2)
3350 if (N0CFP && N1CFP)
3351 return DAG.getNode(ISD::FREM, VT, N0, N1);
Dan Gohman7f321562007-06-25 16:23:39 +00003352
Chris Lattner01b3d732005-09-28 22:28:18 +00003353 return SDOperand();
3354}
3355
Chris Lattner12d83032006-03-05 05:30:57 +00003356SDOperand DAGCombiner::visitFCOPYSIGN(SDNode *N) {
3357 SDOperand N0 = N->getOperand(0);
3358 SDOperand N1 = N->getOperand(1);
3359 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3360 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
3361 MVT::ValueType VT = N->getValueType(0);
3362
3363 if (N0CFP && N1CFP) // Constant fold
3364 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1);
3365
3366 if (N1CFP) {
Dale Johannesene6c17422007-08-26 01:18:27 +00003367 const APFloat& V = N1CFP->getValueAPF();
Chris Lattner12d83032006-03-05 05:30:57 +00003368 // copysign(x, c1) -> fabs(x) iff ispos(c1)
3369 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
Dale Johannesen87503a62007-08-25 22:10:57 +00003370 if (!V.isNegative())
Chris Lattner12d83032006-03-05 05:30:57 +00003371 return DAG.getNode(ISD::FABS, VT, N0);
3372 else
3373 return DAG.getNode(ISD::FNEG, VT, DAG.getNode(ISD::FABS, VT, N0));
3374 }
3375
3376 // copysign(fabs(x), y) -> copysign(x, y)
3377 // copysign(fneg(x), y) -> copysign(x, y)
3378 // copysign(copysign(x,z), y) -> copysign(x, y)
3379 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
3380 N0.getOpcode() == ISD::FCOPYSIGN)
3381 return DAG.getNode(ISD::FCOPYSIGN, VT, N0.getOperand(0), N1);
3382
3383 // copysign(x, abs(y)) -> abs(x)
3384 if (N1.getOpcode() == ISD::FABS)
3385 return DAG.getNode(ISD::FABS, VT, N0);
3386
3387 // copysign(x, copysign(y,z)) -> copysign(x, z)
3388 if (N1.getOpcode() == ISD::FCOPYSIGN)
3389 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(1));
3390
3391 // copysign(x, fp_extend(y)) -> copysign(x, y)
3392 // copysign(x, fp_round(y)) -> copysign(x, y)
3393 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
3394 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(0));
3395
3396 return SDOperand();
3397}
3398
3399
Chris Lattner01b3d732005-09-28 22:28:18 +00003400
Nate Begeman83e75ec2005-09-06 04:43:02 +00003401SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003402 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003403 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00003404 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003405
3406 // fold (sint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00003407 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00003408 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003409 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003410}
3411
Nate Begeman83e75ec2005-09-06 04:43:02 +00003412SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003413 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003414 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00003415 MVT::ValueType VT = N->getValueType(0);
3416
Nate Begeman1d4d4142005-09-01 00:19:25 +00003417 // fold (uint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00003418 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00003419 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003420 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003421}
3422
Nate Begeman83e75ec2005-09-06 04:43:02 +00003423SDOperand DAGCombiner::visitFP_TO_SINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003424 SDOperand N0 = N->getOperand(0);
3425 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3426 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003427
3428 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00003429 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00003430 return DAG.getNode(ISD::FP_TO_SINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003431 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003432}
3433
Nate Begeman83e75ec2005-09-06 04:43:02 +00003434SDOperand DAGCombiner::visitFP_TO_UINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003435 SDOperand N0 = N->getOperand(0);
3436 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3437 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003438
3439 // fold (fp_to_uint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00003440 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00003441 return DAG.getNode(ISD::FP_TO_UINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003442 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003443}
3444
Nate Begeman83e75ec2005-09-06 04:43:02 +00003445SDOperand DAGCombiner::visitFP_ROUND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003446 SDOperand N0 = N->getOperand(0);
3447 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3448 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003449
3450 // fold (fp_round c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00003451 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00003452 return DAG.getNode(ISD::FP_ROUND, VT, N0);
Chris Lattner79dbea52006-03-13 06:26:26 +00003453
3454 // fold (fp_round (fp_extend x)) -> x
3455 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
3456 return N0.getOperand(0);
3457
3458 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
3459 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.Val->hasOneUse()) {
3460 SDOperand Tmp = DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0));
3461 AddToWorkList(Tmp.Val);
3462 return DAG.getNode(ISD::FCOPYSIGN, VT, Tmp, N0.getOperand(1));
3463 }
3464
Nate Begeman83e75ec2005-09-06 04:43:02 +00003465 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003466}
3467
Nate Begeman83e75ec2005-09-06 04:43:02 +00003468SDOperand DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003469 SDOperand N0 = N->getOperand(0);
3470 MVT::ValueType VT = N->getValueType(0);
3471 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00003472 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003473
Nate Begeman1d4d4142005-09-01 00:19:25 +00003474 // fold (fp_round_inreg c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00003475 if (N0CFP) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003476 SDOperand Round = DAG.getConstantFP(N0CFP->getValueAPF(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003477 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003478 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00003479 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003480}
3481
Nate Begeman83e75ec2005-09-06 04:43:02 +00003482SDOperand DAGCombiner::visitFP_EXTEND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003483 SDOperand N0 = N->getOperand(0);
3484 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3485 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003486
3487 // fold (fp_extend c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00003488 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00003489 return DAG.getNode(ISD::FP_EXTEND, VT, N0);
Chris Lattnere564dbb2006-05-05 21:34:35 +00003490
3491 // fold (fpext (load x)) -> (fpext (fpround (extload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00003492 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00003493 (!AfterLegalize||TLI.isLoadXLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003494 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3495 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
3496 LN0->getBasePtr(), LN0->getSrcValue(),
3497 LN0->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003498 N0.getValueType(),
3499 LN0->isVolatile(),
3500 LN0->getAlignment());
Chris Lattnere564dbb2006-05-05 21:34:35 +00003501 CombineTo(N, ExtLoad);
3502 CombineTo(N0.Val, DAG.getNode(ISD::FP_ROUND, N0.getValueType(), ExtLoad),
3503 ExtLoad.getValue(1));
3504 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
3505 }
3506
3507
Nate Begeman83e75ec2005-09-06 04:43:02 +00003508 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003509}
3510
Nate Begeman83e75ec2005-09-06 04:43:02 +00003511SDOperand DAGCombiner::visitFNEG(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003512 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00003513
Dan Gohman23ff1822007-07-02 15:48:56 +00003514 if (isNegatibleForFree(N0))
3515 return GetNegatedExpression(N0, DAG);
3516
Nate Begeman83e75ec2005-09-06 04:43:02 +00003517 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003518}
3519
Nate Begeman83e75ec2005-09-06 04:43:02 +00003520SDOperand DAGCombiner::visitFABS(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003521 SDOperand N0 = N->getOperand(0);
3522 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3523 MVT::ValueType VT = N->getValueType(0);
3524
Nate Begeman1d4d4142005-09-01 00:19:25 +00003525 // fold (fabs c1) -> fabs(c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00003526 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00003527 return DAG.getNode(ISD::FABS, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003528 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00003529 if (N0.getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00003530 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003531 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00003532 // fold (fabs (fcopysign x, y)) -> (fabs x)
3533 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
3534 return DAG.getNode(ISD::FABS, VT, N0.getOperand(0));
3535
Nate Begeman83e75ec2005-09-06 04:43:02 +00003536 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003537}
3538
Nate Begeman44728a72005-09-19 22:34:01 +00003539SDOperand DAGCombiner::visitBRCOND(SDNode *N) {
3540 SDOperand Chain = N->getOperand(0);
3541 SDOperand N1 = N->getOperand(1);
3542 SDOperand N2 = N->getOperand(2);
3543 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
3544
3545 // never taken branch, fold to chain
3546 if (N1C && N1C->isNullValue())
3547 return Chain;
3548 // unconditional branch
Nate Begemane17daeb2005-10-05 21:43:42 +00003549 if (N1C && N1C->getValue() == 1)
Nate Begeman44728a72005-09-19 22:34:01 +00003550 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
Nate Begeman750ac1b2006-02-01 07:19:44 +00003551 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
3552 // on the target.
3553 if (N1.getOpcode() == ISD::SETCC &&
3554 TLI.isOperationLegal(ISD::BR_CC, MVT::Other)) {
3555 return DAG.getNode(ISD::BR_CC, MVT::Other, Chain, N1.getOperand(2),
3556 N1.getOperand(0), N1.getOperand(1), N2);
3557 }
Nate Begeman44728a72005-09-19 22:34:01 +00003558 return SDOperand();
3559}
3560
Chris Lattner3ea0b472005-10-05 06:47:48 +00003561// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
3562//
Nate Begeman44728a72005-09-19 22:34:01 +00003563SDOperand DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00003564 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
3565 SDOperand CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
3566
3567 // Use SimplifySetCC to simplify SETCC's.
Nate Begemane17daeb2005-10-05 21:43:42 +00003568 SDOperand Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false);
Chris Lattner30f73e72006-10-14 03:52:46 +00003569 if (Simp.Val) AddToWorkList(Simp.Val);
3570
Nate Begemane17daeb2005-10-05 21:43:42 +00003571 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.Val);
3572
3573 // fold br_cc true, dest -> br dest (unconditional branch)
3574 if (SCCC && SCCC->getValue())
3575 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
3576 N->getOperand(4));
3577 // fold br_cc false, dest -> unconditional fall through
3578 if (SCCC && SCCC->isNullValue())
3579 return N->getOperand(0);
Chris Lattner30f73e72006-10-14 03:52:46 +00003580
Nate Begemane17daeb2005-10-05 21:43:42 +00003581 // fold to a simpler setcc
3582 if (Simp.Val && Simp.getOpcode() == ISD::SETCC)
3583 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
3584 Simp.getOperand(2), Simp.getOperand(0),
3585 Simp.getOperand(1), N->getOperand(4));
Nate Begeman44728a72005-09-19 22:34:01 +00003586 return SDOperand();
3587}
3588
Chris Lattner448f2192006-11-11 00:39:41 +00003589
3590/// CombineToPreIndexedLoadStore - Try turning a load / store and a
3591/// pre-indexed load / store when the base pointer is a add or subtract
3592/// and it has other uses besides the load / store. After the
3593/// transformation, the new indexed load / store has effectively folded
3594/// the add / subtract in and all of its other uses are redirected to the
3595/// new load / store.
3596bool DAGCombiner::CombineToPreIndexedLoadStore(SDNode *N) {
3597 if (!AfterLegalize)
3598 return false;
3599
3600 bool isLoad = true;
3601 SDOperand Ptr;
3602 MVT::ValueType VT;
3603 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Evan Chenge90460e2006-12-16 06:25:23 +00003604 if (LD->getAddressingMode() != ISD::UNINDEXED)
3605 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00003606 VT = LD->getLoadedVT();
Evan Cheng83060c52007-03-07 08:07:03 +00003607 if (!TLI.isIndexedLoadLegal(ISD::PRE_INC, VT) &&
Chris Lattner448f2192006-11-11 00:39:41 +00003608 !TLI.isIndexedLoadLegal(ISD::PRE_DEC, VT))
3609 return false;
3610 Ptr = LD->getBasePtr();
3611 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Evan Chenge90460e2006-12-16 06:25:23 +00003612 if (ST->getAddressingMode() != ISD::UNINDEXED)
3613 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00003614 VT = ST->getStoredVT();
3615 if (!TLI.isIndexedStoreLegal(ISD::PRE_INC, VT) &&
3616 !TLI.isIndexedStoreLegal(ISD::PRE_DEC, VT))
3617 return false;
3618 Ptr = ST->getBasePtr();
3619 isLoad = false;
3620 } else
3621 return false;
3622
Chris Lattner9f1794e2006-11-11 00:56:29 +00003623 // If the pointer is not an add/sub, or if it doesn't have multiple uses, bail
3624 // out. There is no reason to make this a preinc/predec.
3625 if ((Ptr.getOpcode() != ISD::ADD && Ptr.getOpcode() != ISD::SUB) ||
3626 Ptr.Val->hasOneUse())
3627 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00003628
Chris Lattner9f1794e2006-11-11 00:56:29 +00003629 // Ask the target to do addressing mode selection.
3630 SDOperand BasePtr;
3631 SDOperand Offset;
3632 ISD::MemIndexedMode AM = ISD::UNINDEXED;
3633 if (!TLI.getPreIndexedAddressParts(N, BasePtr, Offset, AM, DAG))
3634 return false;
Evan Chenga7d4a042007-05-03 23:52:19 +00003635 // Don't create a indexed load / store with zero offset.
3636 if (isa<ConstantSDNode>(Offset) &&
3637 cast<ConstantSDNode>(Offset)->getValue() == 0)
3638 return false;
Chris Lattner9f1794e2006-11-11 00:56:29 +00003639
Chris Lattner41e53fd2006-11-11 01:00:15 +00003640 // Try turning it into a pre-indexed load / store except when:
Evan Chengc843abe2007-05-24 02:35:39 +00003641 // 1) The new base ptr is a frame index.
3642 // 2) If N is a store and the new base ptr is either the same as or is a
Chris Lattner9f1794e2006-11-11 00:56:29 +00003643 // predecessor of the value being stored.
Evan Chengc843abe2007-05-24 02:35:39 +00003644 // 3) Another use of old base ptr is a predecessor of N. If ptr is folded
Chris Lattner9f1794e2006-11-11 00:56:29 +00003645 // that would create a cycle.
Evan Chengc843abe2007-05-24 02:35:39 +00003646 // 4) All uses are load / store ops that use it as old base ptr.
Chris Lattner448f2192006-11-11 00:39:41 +00003647
Chris Lattner41e53fd2006-11-11 01:00:15 +00003648 // Check #1. Preinc'ing a frame index would require copying the stack pointer
3649 // (plus the implicit offset) to a register to preinc anyway.
3650 if (isa<FrameIndexSDNode>(BasePtr))
3651 return false;
3652
3653 // Check #2.
Chris Lattner9f1794e2006-11-11 00:56:29 +00003654 if (!isLoad) {
3655 SDOperand Val = cast<StoreSDNode>(N)->getValue();
Evan Chengc843abe2007-05-24 02:35:39 +00003656 if (Val == BasePtr || BasePtr.Val->isPredecessor(Val.Val))
Chris Lattner9f1794e2006-11-11 00:56:29 +00003657 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00003658 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00003659
Evan Chengc843abe2007-05-24 02:35:39 +00003660 // Now check for #3 and #4.
Chris Lattner9f1794e2006-11-11 00:56:29 +00003661 bool RealUse = false;
3662 for (SDNode::use_iterator I = Ptr.Val->use_begin(),
3663 E = Ptr.Val->use_end(); I != E; ++I) {
3664 SDNode *Use = *I;
3665 if (Use == N)
3666 continue;
3667 if (Use->isPredecessor(N))
3668 return false;
3669
3670 if (!((Use->getOpcode() == ISD::LOAD &&
3671 cast<LoadSDNode>(Use)->getBasePtr() == Ptr) ||
3672 (Use->getOpcode() == ISD::STORE) &&
3673 cast<StoreSDNode>(Use)->getBasePtr() == Ptr))
3674 RealUse = true;
3675 }
3676 if (!RealUse)
3677 return false;
3678
3679 SDOperand Result;
3680 if (isLoad)
3681 Result = DAG.getIndexedLoad(SDOperand(N,0), BasePtr, Offset, AM);
3682 else
3683 Result = DAG.getIndexedStore(SDOperand(N,0), BasePtr, Offset, AM);
3684 ++PreIndexedNodes;
3685 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +00003686 DOUT << "\nReplacing.4 "; DEBUG(N->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +00003687 DOUT << "\nWith: "; DEBUG(Result.Val->dump(&DAG));
3688 DOUT << '\n';
Chris Lattner9f1794e2006-11-11 00:56:29 +00003689 std::vector<SDNode*> NowDead;
3690 if (isLoad) {
3691 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(0),
Chris Lattner01d029b2007-10-15 06:10:22 +00003692 &NowDead);
Chris Lattner9f1794e2006-11-11 00:56:29 +00003693 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1), Result.getValue(2),
Chris Lattner01d029b2007-10-15 06:10:22 +00003694 &NowDead);
Chris Lattner9f1794e2006-11-11 00:56:29 +00003695 } else {
3696 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(1),
Chris Lattner01d029b2007-10-15 06:10:22 +00003697 &NowDead);
Chris Lattner9f1794e2006-11-11 00:56:29 +00003698 }
3699
3700 // Nodes can end up on the worklist more than once. Make sure we do
3701 // not process a node that has been replaced.
3702 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
3703 removeFromWorkList(NowDead[i]);
3704 // Finally, since the node is now dead, remove it from the graph.
3705 DAG.DeleteNode(N);
3706
3707 // Replace the uses of Ptr with uses of the updated base value.
3708 DAG.ReplaceAllUsesOfValueWith(Ptr, Result.getValue(isLoad ? 1 : 0),
Chris Lattner01d029b2007-10-15 06:10:22 +00003709 &NowDead);
Chris Lattner9f1794e2006-11-11 00:56:29 +00003710 removeFromWorkList(Ptr.Val);
3711 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
3712 removeFromWorkList(NowDead[i]);
3713 DAG.DeleteNode(Ptr.Val);
3714
3715 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00003716}
3717
3718/// CombineToPostIndexedLoadStore - Try combine a load / store with a
3719/// add / sub of the base pointer node into a post-indexed load / store.
3720/// The transformation folded the add / subtract into the new indexed
3721/// load / store effectively and all of its uses are redirected to the
3722/// new load / store.
3723bool DAGCombiner::CombineToPostIndexedLoadStore(SDNode *N) {
3724 if (!AfterLegalize)
3725 return false;
3726
3727 bool isLoad = true;
3728 SDOperand Ptr;
3729 MVT::ValueType VT;
3730 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Evan Chenge90460e2006-12-16 06:25:23 +00003731 if (LD->getAddressingMode() != ISD::UNINDEXED)
3732 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00003733 VT = LD->getLoadedVT();
3734 if (!TLI.isIndexedLoadLegal(ISD::POST_INC, VT) &&
3735 !TLI.isIndexedLoadLegal(ISD::POST_DEC, VT))
3736 return false;
3737 Ptr = LD->getBasePtr();
3738 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Evan Chenge90460e2006-12-16 06:25:23 +00003739 if (ST->getAddressingMode() != ISD::UNINDEXED)
3740 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00003741 VT = ST->getStoredVT();
3742 if (!TLI.isIndexedStoreLegal(ISD::POST_INC, VT) &&
3743 !TLI.isIndexedStoreLegal(ISD::POST_DEC, VT))
3744 return false;
3745 Ptr = ST->getBasePtr();
3746 isLoad = false;
3747 } else
3748 return false;
3749
Evan Chengcc470212006-11-16 00:08:20 +00003750 if (Ptr.Val->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00003751 return false;
3752
3753 for (SDNode::use_iterator I = Ptr.Val->use_begin(),
3754 E = Ptr.Val->use_end(); I != E; ++I) {
3755 SDNode *Op = *I;
3756 if (Op == N ||
3757 (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB))
3758 continue;
3759
3760 SDOperand BasePtr;
3761 SDOperand Offset;
3762 ISD::MemIndexedMode AM = ISD::UNINDEXED;
3763 if (TLI.getPostIndexedAddressParts(N, Op, BasePtr, Offset, AM, DAG)) {
3764 if (Ptr == Offset)
3765 std::swap(BasePtr, Offset);
3766 if (Ptr != BasePtr)
Chris Lattner448f2192006-11-11 00:39:41 +00003767 continue;
Evan Chenga7d4a042007-05-03 23:52:19 +00003768 // Don't create a indexed load / store with zero offset.
3769 if (isa<ConstantSDNode>(Offset) &&
3770 cast<ConstantSDNode>(Offset)->getValue() == 0)
3771 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00003772
Chris Lattner9f1794e2006-11-11 00:56:29 +00003773 // Try turning it into a post-indexed load / store except when
3774 // 1) All uses are load / store ops that use it as base ptr.
3775 // 2) Op must be independent of N, i.e. Op is neither a predecessor
3776 // nor a successor of N. Otherwise, if Op is folded that would
3777 // create a cycle.
3778
3779 // Check for #1.
3780 bool TryNext = false;
3781 for (SDNode::use_iterator II = BasePtr.Val->use_begin(),
3782 EE = BasePtr.Val->use_end(); II != EE; ++II) {
3783 SDNode *Use = *II;
3784 if (Use == Ptr.Val)
Chris Lattner448f2192006-11-11 00:39:41 +00003785 continue;
3786
Chris Lattner9f1794e2006-11-11 00:56:29 +00003787 // If all the uses are load / store addresses, then don't do the
3788 // transformation.
3789 if (Use->getOpcode() == ISD::ADD || Use->getOpcode() == ISD::SUB){
3790 bool RealUse = false;
3791 for (SDNode::use_iterator III = Use->use_begin(),
3792 EEE = Use->use_end(); III != EEE; ++III) {
3793 SDNode *UseUse = *III;
3794 if (!((UseUse->getOpcode() == ISD::LOAD &&
3795 cast<LoadSDNode>(UseUse)->getBasePtr().Val == Use) ||
3796 (UseUse->getOpcode() == ISD::STORE) &&
3797 cast<StoreSDNode>(UseUse)->getBasePtr().Val == Use))
3798 RealUse = true;
3799 }
Chris Lattner448f2192006-11-11 00:39:41 +00003800
Chris Lattner9f1794e2006-11-11 00:56:29 +00003801 if (!RealUse) {
3802 TryNext = true;
3803 break;
Chris Lattner448f2192006-11-11 00:39:41 +00003804 }
3805 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00003806 }
3807 if (TryNext)
3808 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00003809
Chris Lattner9f1794e2006-11-11 00:56:29 +00003810 // Check for #2
3811 if (!Op->isPredecessor(N) && !N->isPredecessor(Op)) {
3812 SDOperand Result = isLoad
3813 ? DAG.getIndexedLoad(SDOperand(N,0), BasePtr, Offset, AM)
3814 : DAG.getIndexedStore(SDOperand(N,0), BasePtr, Offset, AM);
3815 ++PostIndexedNodes;
3816 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +00003817 DOUT << "\nReplacing.5 "; DEBUG(N->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +00003818 DOUT << "\nWith: "; DEBUG(Result.Val->dump(&DAG));
3819 DOUT << '\n';
Chris Lattner9f1794e2006-11-11 00:56:29 +00003820 std::vector<SDNode*> NowDead;
3821 if (isLoad) {
3822 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(0),
Chris Lattner01d029b2007-10-15 06:10:22 +00003823 &NowDead);
Chris Lattner9f1794e2006-11-11 00:56:29 +00003824 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1), Result.getValue(2),
Chris Lattner01d029b2007-10-15 06:10:22 +00003825 &NowDead);
Chris Lattner9f1794e2006-11-11 00:56:29 +00003826 } else {
3827 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(1),
Chris Lattner01d029b2007-10-15 06:10:22 +00003828 &NowDead);
Chris Lattner448f2192006-11-11 00:39:41 +00003829 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00003830
3831 // Nodes can end up on the worklist more than once. Make sure we do
3832 // not process a node that has been replaced.
3833 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
3834 removeFromWorkList(NowDead[i]);
3835 // Finally, since the node is now dead, remove it from the graph.
3836 DAG.DeleteNode(N);
3837
3838 // Replace the uses of Use with uses of the updated base value.
3839 DAG.ReplaceAllUsesOfValueWith(SDOperand(Op, 0),
3840 Result.getValue(isLoad ? 1 : 0),
Chris Lattner01d029b2007-10-15 06:10:22 +00003841 &NowDead);
Chris Lattner9f1794e2006-11-11 00:56:29 +00003842 removeFromWorkList(Op);
3843 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
3844 removeFromWorkList(NowDead[i]);
3845 DAG.DeleteNode(Op);
3846
3847 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00003848 }
3849 }
3850 }
3851 return false;
3852}
3853
3854
Chris Lattner01a22022005-10-10 22:04:48 +00003855SDOperand DAGCombiner::visitLOAD(SDNode *N) {
Evan Cheng466685d2006-10-09 20:57:25 +00003856 LoadSDNode *LD = cast<LoadSDNode>(N);
3857 SDOperand Chain = LD->getChain();
3858 SDOperand Ptr = LD->getBasePtr();
Evan Cheng45a7ca92007-05-01 00:38:21 +00003859
3860 // If load is not volatile and there are no uses of the loaded value (and
3861 // the updated indexed value in case of indexed loads), change uses of the
3862 // chain value into uses of the chain input (i.e. delete the dead load).
3863 if (!LD->isVolatile()) {
Evan Cheng498f5592007-05-01 08:53:39 +00003864 if (N->getValueType(1) == MVT::Other) {
3865 // Unindexed loads.
3866 if (N->hasNUsesOfValue(0, 0))
3867 return CombineTo(N, DAG.getNode(ISD::UNDEF, N->getValueType(0)), Chain);
3868 } else {
3869 // Indexed loads.
3870 assert(N->getValueType(2) == MVT::Other && "Malformed indexed loads?");
3871 if (N->hasNUsesOfValue(0, 0) && N->hasNUsesOfValue(0, 1)) {
3872 SDOperand Undef0 = DAG.getNode(ISD::UNDEF, N->getValueType(0));
3873 SDOperand Undef1 = DAG.getNode(ISD::UNDEF, N->getValueType(1));
3874 SDOperand To[] = { Undef0, Undef1, Chain };
3875 return CombineTo(N, To, 3);
Evan Cheng45a7ca92007-05-01 00:38:21 +00003876 }
Evan Cheng45a7ca92007-05-01 00:38:21 +00003877 }
3878 }
Chris Lattner01a22022005-10-10 22:04:48 +00003879
3880 // If this load is directly stored, replace the load value with the stored
3881 // value.
3882 // TODO: Handle store large -> read small portion.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00003883 // TODO: Handle TRUNCSTORE/LOADEXT
3884 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00003885 if (ISD::isNON_TRUNCStore(Chain.Val)) {
3886 StoreSDNode *PrevST = cast<StoreSDNode>(Chain);
3887 if (PrevST->getBasePtr() == Ptr &&
3888 PrevST->getValue().getValueType() == N->getValueType(0))
Jim Laskeyc2b19f32006-10-11 17:47:52 +00003889 return CombineTo(N, Chain.getOperand(1), Chain);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003890 }
Jim Laskeyc2b19f32006-10-11 17:47:52 +00003891 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00003892
Jim Laskey7ca56af2006-10-11 13:47:09 +00003893 if (CombinerAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00003894 // Walk up chain skipping non-aliasing memory nodes.
3895 SDOperand BetterChain = FindBetterChain(N, Chain);
3896
Jim Laskey6ff23e52006-10-04 16:53:27 +00003897 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00003898 if (Chain != BetterChain) {
Jim Laskeyc2b19f32006-10-11 17:47:52 +00003899 SDOperand ReplLoad;
3900
Jim Laskey279f0532006-09-25 16:29:54 +00003901 // Replace the chain to void dependency.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00003902 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
3903 ReplLoad = DAG.getLoad(N->getValueType(0), BetterChain, Ptr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00003904 LD->getSrcValue(), LD->getSrcValueOffset(),
3905 LD->isVolatile(), LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00003906 } else {
3907 ReplLoad = DAG.getExtLoad(LD->getExtensionType(),
3908 LD->getValueType(0),
3909 BetterChain, Ptr, LD->getSrcValue(),
3910 LD->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003911 LD->getLoadedVT(),
3912 LD->isVolatile(),
3913 LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00003914 }
Jim Laskey279f0532006-09-25 16:29:54 +00003915
Jim Laskey6ff23e52006-10-04 16:53:27 +00003916 // Create token factor to keep old chain connected.
Jim Laskey288af5e2006-09-25 19:32:58 +00003917 SDOperand Token = DAG.getNode(ISD::TokenFactor, MVT::Other,
3918 Chain, ReplLoad.getValue(1));
Jim Laskey6ff23e52006-10-04 16:53:27 +00003919
Jim Laskey274062c2006-10-13 23:32:28 +00003920 // Replace uses with load result and token factor. Don't add users
3921 // to work list.
3922 return CombineTo(N, ReplLoad.getValue(0), Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00003923 }
3924 }
3925
Evan Cheng7fc033a2006-11-03 03:06:21 +00003926 // Try transforming N to an indexed load.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00003927 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Evan Cheng7fc033a2006-11-03 03:06:21 +00003928 return SDOperand(N, 0);
3929
Chris Lattner01a22022005-10-10 22:04:48 +00003930 return SDOperand();
3931}
3932
Chris Lattner87514ca2005-10-10 22:31:19 +00003933SDOperand DAGCombiner::visitSTORE(SDNode *N) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00003934 StoreSDNode *ST = cast<StoreSDNode>(N);
3935 SDOperand Chain = ST->getChain();
3936 SDOperand Value = ST->getValue();
3937 SDOperand Ptr = ST->getBasePtr();
Jim Laskey7aed46c2006-10-11 18:55:16 +00003938
Evan Cheng59d5b682007-05-07 21:27:48 +00003939 // If this is a store of a bit convert, store the input value if the
Evan Cheng2c4f9432007-05-09 21:49:47 +00003940 // resultant store does not need a higher alignment than the original.
Dale Johannesen98a6c622007-05-16 22:45:30 +00003941 if (Value.getOpcode() == ISD::BIT_CONVERT && !ST->isTruncatingStore() &&
3942 ST->getAddressingMode() == ISD::UNINDEXED) {
Evan Cheng59d5b682007-05-07 21:27:48 +00003943 unsigned Align = ST->getAlignment();
3944 MVT::ValueType SVT = Value.getOperand(0).getValueType();
3945 unsigned OrigAlign = TLI.getTargetMachine().getTargetData()->
Dan Gohmanfcc4dd92007-05-18 18:41:29 +00003946 getABITypeAlignment(MVT::getTypeForValueType(SVT));
Evan Chengc2cd2b22007-05-07 21:36:06 +00003947 if (Align <= OrigAlign && TLI.isOperationLegal(ISD::STORE, SVT))
Evan Cheng59d5b682007-05-07 21:27:48 +00003948 return DAG.getStore(Chain, Value.getOperand(0), Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00003949 ST->getSrcValueOffset(), ST->isVolatile(), Align);
Jim Laskey279f0532006-09-25 16:29:54 +00003950 }
3951
Nate Begeman2cbba892006-12-11 02:23:46 +00003952 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Nate Begeman2cbba892006-12-11 02:23:46 +00003953 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Value)) {
Evan Cheng25ece662006-12-11 17:25:19 +00003954 if (Value.getOpcode() != ISD::TargetConstantFP) {
3955 SDOperand Tmp;
Chris Lattner62be1a72006-12-12 04:16:14 +00003956 switch (CFP->getValueType(0)) {
3957 default: assert(0 && "Unknown FP type");
Dale Johannesenc7b21d52007-09-18 18:36:59 +00003958 case MVT::f80: // We don't do this for these yet.
3959 case MVT::f128:
3960 case MVT::ppcf128:
3961 break;
Chris Lattner62be1a72006-12-12 04:16:14 +00003962 case MVT::f32:
3963 if (!AfterLegalize || TLI.isTypeLegal(MVT::i32)) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00003964 Tmp = DAG.getConstant((uint32_t)CFP->getValueAPF().
3965 convertToAPInt().getZExtValue(), MVT::i32);
Chris Lattner62be1a72006-12-12 04:16:14 +00003966 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00003967 ST->getSrcValueOffset(), ST->isVolatile(),
3968 ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00003969 }
3970 break;
3971 case MVT::f64:
3972 if (!AfterLegalize || TLI.isTypeLegal(MVT::i64)) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00003973 Tmp = DAG.getConstant(CFP->getValueAPF().convertToAPInt().
3974 getZExtValue(), MVT::i64);
Chris Lattner62be1a72006-12-12 04:16:14 +00003975 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00003976 ST->getSrcValueOffset(), ST->isVolatile(),
3977 ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00003978 } else if (TLI.isTypeLegal(MVT::i32)) {
3979 // Many FP stores are not make apparent until after legalize, e.g. for
3980 // argument passing. Since this is so common, custom legalize the
3981 // 64-bit integer store into two 32-bit stores.
Dale Johannesen9d5f4562007-09-12 03:30:33 +00003982 uint64_t Val = CFP->getValueAPF().convertToAPInt().getZExtValue();
Chris Lattner62be1a72006-12-12 04:16:14 +00003983 SDOperand Lo = DAG.getConstant(Val & 0xFFFFFFFF, MVT::i32);
3984 SDOperand Hi = DAG.getConstant(Val >> 32, MVT::i32);
3985 if (!TLI.isLittleEndian()) std::swap(Lo, Hi);
3986
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00003987 int SVOffset = ST->getSrcValueOffset();
3988 unsigned Alignment = ST->getAlignment();
3989 bool isVolatile = ST->isVolatile();
3990
Chris Lattner62be1a72006-12-12 04:16:14 +00003991 SDOperand St0 = DAG.getStore(Chain, Lo, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00003992 ST->getSrcValueOffset(),
3993 isVolatile, ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00003994 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3995 DAG.getConstant(4, Ptr.getValueType()));
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00003996 SVOffset += 4;
3997 if (Alignment > 4)
3998 Alignment = 4;
Chris Lattner62be1a72006-12-12 04:16:14 +00003999 SDOperand St1 = DAG.getStore(Chain, Hi, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004000 SVOffset, isVolatile, Alignment);
Chris Lattner62be1a72006-12-12 04:16:14 +00004001 return DAG.getNode(ISD::TokenFactor, MVT::Other, St0, St1);
4002 }
4003 break;
Evan Cheng25ece662006-12-11 17:25:19 +00004004 }
Nate Begeman2cbba892006-12-11 02:23:46 +00004005 }
Nate Begeman2cbba892006-12-11 02:23:46 +00004006 }
4007
Jim Laskey279f0532006-09-25 16:29:54 +00004008 if (CombinerAA) {
4009 // Walk up chain skipping non-aliasing memory nodes.
4010 SDOperand BetterChain = FindBetterChain(N, Chain);
4011
Jim Laskey6ff23e52006-10-04 16:53:27 +00004012 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00004013 if (Chain != BetterChain) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00004014 // Replace the chain to avoid dependency.
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004015 SDOperand ReplStore;
4016 if (ST->isTruncatingStore()) {
4017 ReplStore = DAG.getTruncStore(BetterChain, Value, Ptr,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004018 ST->getSrcValue(), ST->getSrcValueOffset(), ST->getStoredVT(),
4019 ST->isVolatile(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004020 } else {
4021 ReplStore = DAG.getStore(BetterChain, Value, Ptr,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004022 ST->getSrcValue(), ST->getSrcValueOffset(),
4023 ST->isVolatile(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004024 }
4025
Jim Laskey279f0532006-09-25 16:29:54 +00004026 // Create token to keep both nodes around.
Jim Laskey274062c2006-10-13 23:32:28 +00004027 SDOperand Token =
4028 DAG.getNode(ISD::TokenFactor, MVT::Other, Chain, ReplStore);
4029
4030 // Don't add users to work list.
4031 return CombineTo(N, Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00004032 }
Jim Laskeyd1aed7a2006-09-21 16:28:59 +00004033 }
Chris Lattnerc33baaa2005-12-23 05:48:07 +00004034
Evan Cheng33dbedc2006-11-05 09:31:14 +00004035 // Try transforming N to an indexed store.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00004036 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Evan Cheng33dbedc2006-11-05 09:31:14 +00004037 return SDOperand(N, 0);
4038
Chris Lattner2b4c2792007-10-13 06:35:54 +00004039 // FIXME: is there such a think as a truncating indexed store?
4040 if (ST->isTruncatingStore() && ST->getAddressingMode() == ISD::UNINDEXED &&
4041 MVT::isInteger(Value.getValueType())) {
4042 // See if we can simplify the input to this truncstore with knowledge that
4043 // only the low bits are being used. For example:
4044 // "truncstore (or (shl x, 8), y), i8" -> "truncstore y, i8"
4045 SDOperand Shorter =
4046 GetDemandedBits(Value, MVT::getIntVTBitMask(ST->getStoredVT()));
4047 AddToWorkList(Value.Val);
4048 if (Shorter.Val)
4049 return DAG.getTruncStore(Chain, Shorter, Ptr, ST->getSrcValue(),
4050 ST->getSrcValueOffset(), ST->getStoredVT(),
4051 ST->isVolatile(), ST->getAlignment());
Chris Lattnere33544c2007-10-13 06:58:48 +00004052
4053 // Otherwise, see if we can simplify the operation with
4054 // SimplifyDemandedBits, which only works if the value has a single use.
4055 if (SimplifyDemandedBits(Value, MVT::getIntVTBitMask(ST->getStoredVT())))
4056 return SDOperand(N, 0);
Chris Lattner2b4c2792007-10-13 06:35:54 +00004057 }
4058
Chris Lattner87514ca2005-10-10 22:31:19 +00004059 return SDOperand();
4060}
4061
Chris Lattnerca242442006-03-19 01:27:56 +00004062SDOperand DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
4063 SDOperand InVec = N->getOperand(0);
4064 SDOperand InVal = N->getOperand(1);
4065 SDOperand EltNo = N->getOperand(2);
4066
4067 // If the invec is a BUILD_VECTOR and if EltNo is a constant, build a new
4068 // vector with the inserted element.
4069 if (InVec.getOpcode() == ISD::BUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
4070 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004071 SmallVector<SDOperand, 8> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
Chris Lattnerca242442006-03-19 01:27:56 +00004072 if (Elt < Ops.size())
4073 Ops[Elt] = InVal;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004074 return DAG.getNode(ISD::BUILD_VECTOR, InVec.getValueType(),
4075 &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00004076 }
4077
4078 return SDOperand();
4079}
4080
Evan Cheng513da432007-10-06 08:19:55 +00004081SDOperand DAGCombiner::visitEXTRACT_VECTOR_ELT(SDNode *N) {
4082 SDOperand InVec = N->getOperand(0);
4083 SDOperand EltNo = N->getOperand(1);
4084
4085 // (vextract (v4f32 s2v (f32 load $addr)), 0) -> (f32 load $addr)
4086 // (vextract (v4i32 bc (v4f32 s2v (f32 load $addr))), 0) -> (i32 load $addr)
4087 if (isa<ConstantSDNode>(EltNo)) {
4088 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
4089 bool NewLoad = false;
4090 if (Elt == 0) {
4091 MVT::ValueType VT = InVec.getValueType();
4092 MVT::ValueType EVT = MVT::getVectorElementType(VT);
4093 MVT::ValueType LVT = EVT;
4094 unsigned NumElts = MVT::getVectorNumElements(VT);
4095 if (InVec.getOpcode() == ISD::BIT_CONVERT) {
4096 MVT::ValueType BCVT = InVec.getOperand(0).getValueType();
4097 if (NumElts != MVT::getVectorNumElements(BCVT))
4098 return SDOperand();
4099 InVec = InVec.getOperand(0);
4100 EVT = MVT::getVectorElementType(BCVT);
4101 NewLoad = true;
4102 }
4103 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR &&
4104 InVec.getOperand(0).getValueType() == EVT &&
4105 ISD::isNormalLoad(InVec.getOperand(0).Val) &&
4106 InVec.getOperand(0).hasOneUse()) {
4107 LoadSDNode *LN0 = cast<LoadSDNode>(InVec.getOperand(0));
4108 unsigned Align = LN0->getAlignment();
4109 if (NewLoad) {
4110 // Check the resultant load doesn't need a higher alignment than the
4111 // original load.
4112 unsigned NewAlign = TLI.getTargetMachine().getTargetData()->
4113 getABITypeAlignment(MVT::getTypeForValueType(LVT));
4114 if (!TLI.isOperationLegal(ISD::LOAD, LVT) || NewAlign > Align)
4115 return SDOperand();
4116 Align = NewAlign;
4117 }
4118
4119 return DAG.getLoad(LVT, LN0->getChain(), LN0->getBasePtr(),
4120 LN0->getSrcValue(), LN0->getSrcValueOffset(),
4121 LN0->isVolatile(), Align);
4122 }
4123 }
4124 }
4125 return SDOperand();
4126}
4127
4128
Dan Gohman7f321562007-06-25 16:23:39 +00004129SDOperand DAGCombiner::visitBUILD_VECTOR(SDNode *N) {
4130 unsigned NumInScalars = N->getNumOperands();
4131 MVT::ValueType VT = N->getValueType(0);
4132 unsigned NumElts = MVT::getVectorNumElements(VT);
4133 MVT::ValueType EltType = MVT::getVectorElementType(VT);
Chris Lattnerca242442006-03-19 01:27:56 +00004134
Dan Gohman7f321562007-06-25 16:23:39 +00004135 // Check to see if this is a BUILD_VECTOR of a bunch of EXTRACT_VECTOR_ELT
4136 // operations. If so, and if the EXTRACT_VECTOR_ELT vector inputs come from
4137 // at most two distinct vectors, turn this into a shuffle node.
Chris Lattnerd7648c82006-03-28 20:28:38 +00004138 SDOperand VecIn1, VecIn2;
4139 for (unsigned i = 0; i != NumInScalars; ++i) {
4140 // Ignore undef inputs.
4141 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
4142
Dan Gohman7f321562007-06-25 16:23:39 +00004143 // If this input is something other than a EXTRACT_VECTOR_ELT with a
Chris Lattnerd7648c82006-03-28 20:28:38 +00004144 // constant index, bail out.
Dan Gohman7f321562007-06-25 16:23:39 +00004145 if (N->getOperand(i).getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
Chris Lattnerd7648c82006-03-28 20:28:38 +00004146 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
4147 VecIn1 = VecIn2 = SDOperand(0, 0);
4148 break;
4149 }
4150
Dan Gohman7f321562007-06-25 16:23:39 +00004151 // If the input vector type disagrees with the result of the build_vector,
Chris Lattnerd7648c82006-03-28 20:28:38 +00004152 // we can't make a shuffle.
4153 SDOperand ExtractedFromVec = N->getOperand(i).getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00004154 if (ExtractedFromVec.getValueType() != VT) {
Chris Lattnerd7648c82006-03-28 20:28:38 +00004155 VecIn1 = VecIn2 = SDOperand(0, 0);
4156 break;
4157 }
4158
4159 // Otherwise, remember this. We allow up to two distinct input vectors.
4160 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
4161 continue;
4162
4163 if (VecIn1.Val == 0) {
4164 VecIn1 = ExtractedFromVec;
4165 } else if (VecIn2.Val == 0) {
4166 VecIn2 = ExtractedFromVec;
4167 } else {
4168 // Too many inputs.
4169 VecIn1 = VecIn2 = SDOperand(0, 0);
4170 break;
4171 }
4172 }
4173
4174 // If everything is good, we can make a shuffle operation.
4175 if (VecIn1.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004176 SmallVector<SDOperand, 8> BuildVecIndices;
Chris Lattnerd7648c82006-03-28 20:28:38 +00004177 for (unsigned i = 0; i != NumInScalars; ++i) {
4178 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
Evan Cheng597a3bd2007-01-20 10:10:26 +00004179 BuildVecIndices.push_back(DAG.getNode(ISD::UNDEF, TLI.getPointerTy()));
Chris Lattnerd7648c82006-03-28 20:28:38 +00004180 continue;
4181 }
4182
4183 SDOperand Extract = N->getOperand(i);
4184
4185 // If extracting from the first vector, just use the index directly.
4186 if (Extract.getOperand(0) == VecIn1) {
4187 BuildVecIndices.push_back(Extract.getOperand(1));
4188 continue;
4189 }
4190
4191 // Otherwise, use InIdx + VecSize
4192 unsigned Idx = cast<ConstantSDNode>(Extract.getOperand(1))->getValue();
Evan Cheng597a3bd2007-01-20 10:10:26 +00004193 BuildVecIndices.push_back(DAG.getConstant(Idx+NumInScalars,
4194 TLI.getPointerTy()));
Chris Lattnerd7648c82006-03-28 20:28:38 +00004195 }
4196
4197 // Add count and size info.
Dan Gohman7f321562007-06-25 16:23:39 +00004198 MVT::ValueType BuildVecVT =
4199 MVT::getVectorType(TLI.getPointerTy(), NumElts);
Chris Lattnerd7648c82006-03-28 20:28:38 +00004200
Dan Gohman7f321562007-06-25 16:23:39 +00004201 // Return the new VECTOR_SHUFFLE node.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004202 SDOperand Ops[5];
4203 Ops[0] = VecIn1;
Chris Lattnercef896e2006-03-28 22:19:47 +00004204 if (VecIn2.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004205 Ops[1] = VecIn2;
Chris Lattnercef896e2006-03-28 22:19:47 +00004206 } else {
Dan Gohman7f321562007-06-25 16:23:39 +00004207 // Use an undef build_vector as input for the second operand.
Chris Lattnercef896e2006-03-28 22:19:47 +00004208 std::vector<SDOperand> UnOps(NumInScalars,
4209 DAG.getNode(ISD::UNDEF,
Dan Gohman7f321562007-06-25 16:23:39 +00004210 EltType));
4211 Ops[1] = DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004212 &UnOps[0], UnOps.size());
4213 AddToWorkList(Ops[1].Val);
Chris Lattnercef896e2006-03-28 22:19:47 +00004214 }
Dan Gohman7f321562007-06-25 16:23:39 +00004215 Ops[2] = DAG.getNode(ISD::BUILD_VECTOR, BuildVecVT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004216 &BuildVecIndices[0], BuildVecIndices.size());
Dan Gohman7f321562007-06-25 16:23:39 +00004217 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Ops, 3);
Chris Lattnerd7648c82006-03-28 20:28:38 +00004218 }
4219
4220 return SDOperand();
4221}
4222
Dan Gohman7f321562007-06-25 16:23:39 +00004223SDOperand DAGCombiner::visitCONCAT_VECTORS(SDNode *N) {
4224 // TODO: Check to see if this is a CONCAT_VECTORS of a bunch of
4225 // EXTRACT_SUBVECTOR operations. If so, and if the EXTRACT_SUBVECTOR vector
4226 // inputs come from at most two distinct vectors, turn this into a shuffle
4227 // node.
4228
4229 // If we only have one input vector, we don't need to do any concatenation.
4230 if (N->getNumOperands() == 1) {
4231 return N->getOperand(0);
4232 }
4233
4234 return SDOperand();
4235}
4236
Chris Lattner66445d32006-03-28 22:11:53 +00004237SDOperand DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
Chris Lattnerf1d0c622006-03-31 22:16:43 +00004238 SDOperand ShufMask = N->getOperand(2);
4239 unsigned NumElts = ShufMask.getNumOperands();
4240
4241 // If the shuffle mask is an identity operation on the LHS, return the LHS.
4242 bool isIdentity = true;
4243 for (unsigned i = 0; i != NumElts; ++i) {
4244 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
4245 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
4246 isIdentity = false;
4247 break;
4248 }
4249 }
4250 if (isIdentity) return N->getOperand(0);
4251
4252 // If the shuffle mask is an identity operation on the RHS, return the RHS.
4253 isIdentity = true;
4254 for (unsigned i = 0; i != NumElts; ++i) {
4255 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
4256 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
4257 isIdentity = false;
4258 break;
4259 }
4260 }
4261 if (isIdentity) return N->getOperand(1);
Evan Chenge7bec0d2006-07-20 22:44:41 +00004262
4263 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
4264 // needed at all.
4265 bool isUnary = true;
Evan Cheng917ec982006-07-21 08:25:53 +00004266 bool isSplat = true;
Evan Chenge7bec0d2006-07-20 22:44:41 +00004267 int VecNum = -1;
Reid Spencer9160a6a2006-07-25 20:44:41 +00004268 unsigned BaseIdx = 0;
Evan Chenge7bec0d2006-07-20 22:44:41 +00004269 for (unsigned i = 0; i != NumElts; ++i)
4270 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
4271 unsigned Idx = cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue();
4272 int V = (Idx < NumElts) ? 0 : 1;
Evan Cheng917ec982006-07-21 08:25:53 +00004273 if (VecNum == -1) {
Evan Chenge7bec0d2006-07-20 22:44:41 +00004274 VecNum = V;
Evan Cheng917ec982006-07-21 08:25:53 +00004275 BaseIdx = Idx;
4276 } else {
4277 if (BaseIdx != Idx)
4278 isSplat = false;
4279 if (VecNum != V) {
4280 isUnary = false;
4281 break;
4282 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00004283 }
4284 }
4285
4286 SDOperand N0 = N->getOperand(0);
4287 SDOperand N1 = N->getOperand(1);
4288 // Normalize unary shuffle so the RHS is undef.
4289 if (isUnary && VecNum == 1)
4290 std::swap(N0, N1);
4291
Evan Cheng917ec982006-07-21 08:25:53 +00004292 // If it is a splat, check if the argument vector is a build_vector with
4293 // all scalar elements the same.
4294 if (isSplat) {
4295 SDNode *V = N0.Val;
Evan Cheng917ec982006-07-21 08:25:53 +00004296
Dan Gohman7f321562007-06-25 16:23:39 +00004297 // If this is a bit convert that changes the element type of the vector but
Evan Cheng59569222006-10-16 22:49:37 +00004298 // not the number of vector elements, look through it. Be careful not to
4299 // look though conversions that change things like v4f32 to v2f64.
Dan Gohman7f321562007-06-25 16:23:39 +00004300 if (V->getOpcode() == ISD::BIT_CONVERT) {
Evan Cheng59569222006-10-16 22:49:37 +00004301 SDOperand ConvInput = V->getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00004302 if (MVT::getVectorNumElements(ConvInput.getValueType()) == NumElts)
Evan Cheng59569222006-10-16 22:49:37 +00004303 V = ConvInput.Val;
4304 }
4305
Dan Gohman7f321562007-06-25 16:23:39 +00004306 if (V->getOpcode() == ISD::BUILD_VECTOR) {
4307 unsigned NumElems = V->getNumOperands();
Evan Cheng917ec982006-07-21 08:25:53 +00004308 if (NumElems > BaseIdx) {
4309 SDOperand Base;
4310 bool AllSame = true;
4311 for (unsigned i = 0; i != NumElems; ++i) {
4312 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
4313 Base = V->getOperand(i);
4314 break;
4315 }
4316 }
4317 // Splat of <u, u, u, u>, return <u, u, u, u>
4318 if (!Base.Val)
4319 return N0;
4320 for (unsigned i = 0; i != NumElems; ++i) {
Evan Chenge0480d22007-09-18 21:54:37 +00004321 if (V->getOperand(i) != Base) {
Evan Cheng917ec982006-07-21 08:25:53 +00004322 AllSame = false;
4323 break;
4324 }
4325 }
4326 // Splat of <x, x, x, x>, return <x, x, x, x>
4327 if (AllSame)
4328 return N0;
4329 }
4330 }
4331 }
4332
Evan Chenge7bec0d2006-07-20 22:44:41 +00004333 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
4334 // into an undef.
4335 if (isUnary || N0 == N1) {
Chris Lattner17614ea2006-04-08 05:34:25 +00004336 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
4337 // first operand.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004338 SmallVector<SDOperand, 8> MappedOps;
Chris Lattner17614ea2006-04-08 05:34:25 +00004339 for (unsigned i = 0; i != NumElts; ++i) {
4340 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
4341 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) {
4342 MappedOps.push_back(ShufMask.getOperand(i));
4343 } else {
4344 unsigned NewIdx =
4345 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
4346 MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32));
4347 }
4348 }
Dan Gohman7f321562007-06-25 16:23:39 +00004349 ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMask.getValueType(),
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004350 &MappedOps[0], MappedOps.size());
Chris Lattner17614ea2006-04-08 05:34:25 +00004351 AddToWorkList(ShufMask.Val);
Dan Gohman7f321562007-06-25 16:23:39 +00004352 return DAG.getNode(ISD::VECTOR_SHUFFLE, N->getValueType(0),
4353 N0,
4354 DAG.getNode(ISD::UNDEF, N->getValueType(0)),
4355 ShufMask);
Chris Lattner17614ea2006-04-08 05:34:25 +00004356 }
Dan Gohman7f321562007-06-25 16:23:39 +00004357
Chris Lattnerf1d0c622006-03-31 22:16:43 +00004358 return SDOperand();
4359}
4360
Evan Cheng44f1f092006-04-20 08:56:16 +00004361/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
Dan Gohman7f321562007-06-25 16:23:39 +00004362/// an AND to a vector_shuffle with the destination vector and a zero vector.
4363/// e.g. AND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
Evan Cheng44f1f092006-04-20 08:56:16 +00004364/// vector_shuffle V, Zero, <0, 4, 2, 4>
4365SDOperand DAGCombiner::XformToShuffleWithZero(SDNode *N) {
4366 SDOperand LHS = N->getOperand(0);
4367 SDOperand RHS = N->getOperand(1);
Dan Gohman7f321562007-06-25 16:23:39 +00004368 if (N->getOpcode() == ISD::AND) {
4369 if (RHS.getOpcode() == ISD::BIT_CONVERT)
Evan Cheng44f1f092006-04-20 08:56:16 +00004370 RHS = RHS.getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00004371 if (RHS.getOpcode() == ISD::BUILD_VECTOR) {
Evan Cheng44f1f092006-04-20 08:56:16 +00004372 std::vector<SDOperand> IdxOps;
4373 unsigned NumOps = RHS.getNumOperands();
Dan Gohman7f321562007-06-25 16:23:39 +00004374 unsigned NumElts = NumOps;
4375 MVT::ValueType EVT = MVT::getVectorElementType(RHS.getValueType());
Evan Cheng44f1f092006-04-20 08:56:16 +00004376 for (unsigned i = 0; i != NumElts; ++i) {
4377 SDOperand Elt = RHS.getOperand(i);
4378 if (!isa<ConstantSDNode>(Elt))
4379 return SDOperand();
4380 else if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
4381 IdxOps.push_back(DAG.getConstant(i, EVT));
4382 else if (cast<ConstantSDNode>(Elt)->isNullValue())
4383 IdxOps.push_back(DAG.getConstant(NumElts, EVT));
4384 else
4385 return SDOperand();
4386 }
4387
4388 // Let's see if the target supports this vector_shuffle.
4389 if (!TLI.isVectorClearMaskLegal(IdxOps, EVT, DAG))
4390 return SDOperand();
4391
Dan Gohman7f321562007-06-25 16:23:39 +00004392 // Return the new VECTOR_SHUFFLE node.
4393 MVT::ValueType VT = MVT::getVectorType(EVT, NumElts);
Evan Cheng44f1f092006-04-20 08:56:16 +00004394 std::vector<SDOperand> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00004395 LHS = DAG.getNode(ISD::BIT_CONVERT, VT, LHS);
Evan Cheng44f1f092006-04-20 08:56:16 +00004396 Ops.push_back(LHS);
4397 AddToWorkList(LHS.Val);
4398 std::vector<SDOperand> ZeroOps(NumElts, DAG.getConstant(0, EVT));
Dan Gohman7f321562007-06-25 16:23:39 +00004399 Ops.push_back(DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004400 &ZeroOps[0], ZeroOps.size()));
Dan Gohman7f321562007-06-25 16:23:39 +00004401 Ops.push_back(DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004402 &IdxOps[0], IdxOps.size()));
Dan Gohman7f321562007-06-25 16:23:39 +00004403 SDOperand Result = DAG.getNode(ISD::VECTOR_SHUFFLE, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004404 &Ops[0], Ops.size());
Dan Gohman7f321562007-06-25 16:23:39 +00004405 if (VT != LHS.getValueType()) {
4406 Result = DAG.getNode(ISD::BIT_CONVERT, LHS.getValueType(), Result);
Evan Cheng44f1f092006-04-20 08:56:16 +00004407 }
4408 return Result;
4409 }
4410 }
4411 return SDOperand();
4412}
4413
Dan Gohman7f321562007-06-25 16:23:39 +00004414/// SimplifyVBinOp - Visit a binary vector operation, like ADD.
4415SDOperand DAGCombiner::SimplifyVBinOp(SDNode *N) {
4416 // After legalize, the target may be depending on adds and other
4417 // binary ops to provide legal ways to construct constants or other
4418 // things. Simplifying them may result in a loss of legality.
4419 if (AfterLegalize) return SDOperand();
4420
4421 MVT::ValueType VT = N->getValueType(0);
Dan Gohman05d92fe2007-07-13 20:03:40 +00004422 assert(MVT::isVector(VT) && "SimplifyVBinOp only works on vectors!");
Dan Gohman7f321562007-06-25 16:23:39 +00004423
4424 MVT::ValueType EltType = MVT::getVectorElementType(VT);
Chris Lattneredab1b92006-04-02 03:25:57 +00004425 SDOperand LHS = N->getOperand(0);
4426 SDOperand RHS = N->getOperand(1);
Evan Cheng44f1f092006-04-20 08:56:16 +00004427 SDOperand Shuffle = XformToShuffleWithZero(N);
4428 if (Shuffle.Val) return Shuffle;
4429
Dan Gohman7f321562007-06-25 16:23:39 +00004430 // If the LHS and RHS are BUILD_VECTOR nodes, see if we can constant fold
Chris Lattneredab1b92006-04-02 03:25:57 +00004431 // this operation.
Dan Gohman7f321562007-06-25 16:23:39 +00004432 if (LHS.getOpcode() == ISD::BUILD_VECTOR &&
4433 RHS.getOpcode() == ISD::BUILD_VECTOR) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004434 SmallVector<SDOperand, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00004435 for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) {
Chris Lattneredab1b92006-04-02 03:25:57 +00004436 SDOperand LHSOp = LHS.getOperand(i);
4437 SDOperand RHSOp = RHS.getOperand(i);
4438 // If these two elements can't be folded, bail out.
4439 if ((LHSOp.getOpcode() != ISD::UNDEF &&
4440 LHSOp.getOpcode() != ISD::Constant &&
4441 LHSOp.getOpcode() != ISD::ConstantFP) ||
4442 (RHSOp.getOpcode() != ISD::UNDEF &&
4443 RHSOp.getOpcode() != ISD::Constant &&
4444 RHSOp.getOpcode() != ISD::ConstantFP))
4445 break;
Evan Cheng7b336a82006-05-31 06:08:35 +00004446 // Can't fold divide by zero.
Dan Gohman7f321562007-06-25 16:23:39 +00004447 if (N->getOpcode() == ISD::SDIV || N->getOpcode() == ISD::UDIV ||
4448 N->getOpcode() == ISD::FDIV) {
Evan Cheng7b336a82006-05-31 06:08:35 +00004449 if ((RHSOp.getOpcode() == ISD::Constant &&
4450 cast<ConstantSDNode>(RHSOp.Val)->isNullValue()) ||
4451 (RHSOp.getOpcode() == ISD::ConstantFP &&
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00004452 cast<ConstantFPSDNode>(RHSOp.Val)->getValueAPF().isZero()))
Evan Cheng7b336a82006-05-31 06:08:35 +00004453 break;
4454 }
Dan Gohman7f321562007-06-25 16:23:39 +00004455 Ops.push_back(DAG.getNode(N->getOpcode(), EltType, LHSOp, RHSOp));
Chris Lattner3e104b12006-04-08 04:15:24 +00004456 AddToWorkList(Ops.back().Val);
Chris Lattneredab1b92006-04-02 03:25:57 +00004457 assert((Ops.back().getOpcode() == ISD::UNDEF ||
4458 Ops.back().getOpcode() == ISD::Constant ||
4459 Ops.back().getOpcode() == ISD::ConstantFP) &&
4460 "Scalar binop didn't fold!");
4461 }
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00004462
Dan Gohman7f321562007-06-25 16:23:39 +00004463 if (Ops.size() == LHS.getNumOperands()) {
4464 MVT::ValueType VT = LHS.getValueType();
4465 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00004466 }
Chris Lattneredab1b92006-04-02 03:25:57 +00004467 }
4468
4469 return SDOperand();
4470}
4471
Nate Begeman44728a72005-09-19 22:34:01 +00004472SDOperand DAGCombiner::SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2){
Nate Begemanf845b452005-10-08 00:29:44 +00004473 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
4474
4475 SDOperand SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
4476 cast<CondCodeSDNode>(N0.getOperand(2))->get());
4477 // If we got a simplified select_cc node back from SimplifySelectCC, then
4478 // break it down into a new SETCC node, and a new SELECT node, and then return
4479 // the SELECT node, since we were called with a SELECT node.
4480 if (SCC.Val) {
4481 // Check to see if we got a select_cc back (to turn into setcc/select).
4482 // Otherwise, just return whatever node we got back, like fabs.
4483 if (SCC.getOpcode() == ISD::SELECT_CC) {
4484 SDOperand SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
4485 SCC.getOperand(0), SCC.getOperand(1),
4486 SCC.getOperand(4));
Chris Lattner5750df92006-03-01 04:03:14 +00004487 AddToWorkList(SETCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004488 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
4489 SCC.getOperand(3), SETCC);
4490 }
4491 return SCC;
4492 }
Nate Begeman44728a72005-09-19 22:34:01 +00004493 return SDOperand();
4494}
4495
Chris Lattner40c62d52005-10-18 06:04:22 +00004496/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
4497/// are the two values being selected between, see if we can simplify the
Chris Lattner729c6d12006-05-27 00:43:02 +00004498/// select. Callers of this should assume that TheSelect is deleted if this
4499/// returns true. As such, they should return the appropriate thing (e.g. the
4500/// node) back to the top-level of the DAG combiner loop to avoid it being
4501/// looked at.
Chris Lattner40c62d52005-10-18 06:04:22 +00004502///
4503bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDOperand LHS,
4504 SDOperand RHS) {
4505
4506 // If this is a select from two identical things, try to pull the operation
4507 // through the select.
4508 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
Chris Lattner40c62d52005-10-18 06:04:22 +00004509 // If this is a load and the token chain is identical, replace the select
4510 // of two loads with a load through a select of the address to load from.
4511 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
4512 // constants have been dropped into the constant pool.
Evan Cheng466685d2006-10-09 20:57:25 +00004513 if (LHS.getOpcode() == ISD::LOAD &&
Chris Lattner40c62d52005-10-18 06:04:22 +00004514 // Token chains must be identical.
Evan Cheng466685d2006-10-09 20:57:25 +00004515 LHS.getOperand(0) == RHS.getOperand(0)) {
4516 LoadSDNode *LLD = cast<LoadSDNode>(LHS);
4517 LoadSDNode *RLD = cast<LoadSDNode>(RHS);
4518
4519 // If this is an EXTLOAD, the VT's must match.
Evan Cheng2e49f092006-10-11 07:10:22 +00004520 if (LLD->getLoadedVT() == RLD->getLoadedVT()) {
Evan Cheng466685d2006-10-09 20:57:25 +00004521 // FIXME: this conflates two src values, discarding one. This is not
4522 // the right thing to do, but nothing uses srcvalues now. When they do,
4523 // turn SrcValue into a list of locations.
4524 SDOperand Addr;
Chris Lattnerc4e664b2007-01-16 05:59:59 +00004525 if (TheSelect->getOpcode() == ISD::SELECT) {
4526 // Check that the condition doesn't reach either load. If so, folding
4527 // this will induce a cycle into the DAG.
4528 if (!LLD->isPredecessor(TheSelect->getOperand(0).Val) &&
4529 !RLD->isPredecessor(TheSelect->getOperand(0).Val)) {
4530 Addr = DAG.getNode(ISD::SELECT, LLD->getBasePtr().getValueType(),
4531 TheSelect->getOperand(0), LLD->getBasePtr(),
4532 RLD->getBasePtr());
4533 }
4534 } else {
4535 // Check that the condition doesn't reach either load. If so, folding
4536 // this will induce a cycle into the DAG.
4537 if (!LLD->isPredecessor(TheSelect->getOperand(0).Val) &&
4538 !RLD->isPredecessor(TheSelect->getOperand(0).Val) &&
4539 !LLD->isPredecessor(TheSelect->getOperand(1).Val) &&
4540 !RLD->isPredecessor(TheSelect->getOperand(1).Val)) {
4541 Addr = DAG.getNode(ISD::SELECT_CC, LLD->getBasePtr().getValueType(),
Evan Cheng466685d2006-10-09 20:57:25 +00004542 TheSelect->getOperand(0),
4543 TheSelect->getOperand(1),
4544 LLD->getBasePtr(), RLD->getBasePtr(),
4545 TheSelect->getOperand(4));
Chris Lattnerc4e664b2007-01-16 05:59:59 +00004546 }
Evan Cheng466685d2006-10-09 20:57:25 +00004547 }
Chris Lattnerc4e664b2007-01-16 05:59:59 +00004548
4549 if (Addr.Val) {
4550 SDOperand Load;
4551 if (LLD->getExtensionType() == ISD::NON_EXTLOAD)
4552 Load = DAG.getLoad(TheSelect->getValueType(0), LLD->getChain(),
4553 Addr,LLD->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00004554 LLD->getSrcValueOffset(),
4555 LLD->isVolatile(),
4556 LLD->getAlignment());
Chris Lattnerc4e664b2007-01-16 05:59:59 +00004557 else {
4558 Load = DAG.getExtLoad(LLD->getExtensionType(),
4559 TheSelect->getValueType(0),
4560 LLD->getChain(), Addr, LLD->getSrcValue(),
4561 LLD->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00004562 LLD->getLoadedVT(),
4563 LLD->isVolatile(),
4564 LLD->getAlignment());
Chris Lattnerc4e664b2007-01-16 05:59:59 +00004565 }
4566 // Users of the select now use the result of the load.
4567 CombineTo(TheSelect, Load);
4568
4569 // Users of the old loads now use the new load's chain. We know the
4570 // old-load value is dead now.
4571 CombineTo(LHS.Val, Load.getValue(0), Load.getValue(1));
4572 CombineTo(RHS.Val, Load.getValue(0), Load.getValue(1));
4573 return true;
4574 }
Evan Chengc5484282006-10-04 00:56:09 +00004575 }
Chris Lattner40c62d52005-10-18 06:04:22 +00004576 }
4577 }
4578
4579 return false;
4580}
4581
Nate Begeman44728a72005-09-19 22:34:01 +00004582SDOperand DAGCombiner::SimplifySelectCC(SDOperand N0, SDOperand N1,
4583 SDOperand N2, SDOperand N3,
Chris Lattner1eba01e2007-04-11 06:50:51 +00004584 ISD::CondCode CC, bool NotExtCompare) {
Nate Begemanf845b452005-10-08 00:29:44 +00004585
4586 MVT::ValueType VT = N2.getValueType();
Nate Begemanf845b452005-10-08 00:29:44 +00004587 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
4588 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
4589 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
4590
4591 // Determine if the condition we're dealing with is constant
4592 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
Chris Lattner30f73e72006-10-14 03:52:46 +00004593 if (SCC.Val) AddToWorkList(SCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004594 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
4595
4596 // fold select_cc true, x, y -> x
4597 if (SCCC && SCCC->getValue())
4598 return N2;
4599 // fold select_cc false, x, y -> y
4600 if (SCCC && SCCC->getValue() == 0)
4601 return N3;
4602
4603 // Check to see if we can simplify the select into an fabs node
4604 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
4605 // Allow either -0.0 or 0.0
Dale Johannesen87503a62007-08-25 22:10:57 +00004606 if (CFP->getValueAPF().isZero()) {
Nate Begemanf845b452005-10-08 00:29:44 +00004607 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
4608 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
4609 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
4610 N2 == N3.getOperand(0))
4611 return DAG.getNode(ISD::FABS, VT, N0);
4612
4613 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
4614 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
4615 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
4616 N2.getOperand(0) == N3)
4617 return DAG.getNode(ISD::FABS, VT, N3);
4618 }
4619 }
4620
4621 // Check to see if we can perform the "gzip trick", transforming
4622 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
Chris Lattnere3152e52006-09-20 06:41:35 +00004623 if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT &&
Nate Begemanf845b452005-10-08 00:29:44 +00004624 MVT::isInteger(N0.getValueType()) &&
Chris Lattnere3152e52006-09-20 06:41:35 +00004625 MVT::isInteger(N2.getValueType()) &&
4626 (N1C->isNullValue() || // (a < 0) ? b : 0
4627 (N1C->getValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0
Nate Begemanf845b452005-10-08 00:29:44 +00004628 MVT::ValueType XType = N0.getValueType();
4629 MVT::ValueType AType = N2.getValueType();
4630 if (XType >= AType) {
4631 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00004632 // single-bit constant.
Nate Begemanf845b452005-10-08 00:29:44 +00004633 if (N2C && ((N2C->getValue() & (N2C->getValue()-1)) == 0)) {
4634 unsigned ShCtV = Log2_64(N2C->getValue());
4635 ShCtV = MVT::getSizeInBits(XType)-ShCtV-1;
4636 SDOperand ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
4637 SDOperand Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
Chris Lattner5750df92006-03-01 04:03:14 +00004638 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004639 if (XType > AType) {
4640 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00004641 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004642 }
4643 return DAG.getNode(ISD::AND, AType, Shift, N2);
4644 }
4645 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
4646 DAG.getConstant(MVT::getSizeInBits(XType)-1,
4647 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00004648 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004649 if (XType > AType) {
4650 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00004651 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004652 }
4653 return DAG.getNode(ISD::AND, AType, Shift, N2);
4654 }
4655 }
Nate Begeman07ed4172005-10-10 21:26:48 +00004656
4657 // fold select C, 16, 0 -> shl C, 4
4658 if (N2C && N3C && N3C->isNullValue() && isPowerOf2_64(N2C->getValue()) &&
4659 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) {
Chris Lattner1eba01e2007-04-11 06:50:51 +00004660
4661 // If the caller doesn't want us to simplify this into a zext of a compare,
4662 // don't do it.
4663 if (NotExtCompare && N2C->getValue() == 1)
4664 return SDOperand();
4665
Nate Begeman07ed4172005-10-10 21:26:48 +00004666 // Get a SetCC of the condition
4667 // FIXME: Should probably make sure that setcc is legal if we ever have a
4668 // target where it isn't.
Nate Begemanb0d04a72006-02-18 02:40:58 +00004669 SDOperand Temp, SCC;
Nate Begeman07ed4172005-10-10 21:26:48 +00004670 // cast from setcc result type to select result type
Nate Begemanb0d04a72006-02-18 02:40:58 +00004671 if (AfterLegalize) {
4672 SCC = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
Chris Lattner555d8d62006-12-07 22:36:47 +00004673 if (N2.getValueType() < SCC.getValueType())
4674 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
4675 else
4676 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00004677 } else {
4678 SCC = DAG.getSetCC(MVT::i1, N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00004679 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00004680 }
Chris Lattner5750df92006-03-01 04:03:14 +00004681 AddToWorkList(SCC.Val);
4682 AddToWorkList(Temp.Val);
Chris Lattnerc56a81d2007-04-11 06:43:25 +00004683
4684 if (N2C->getValue() == 1)
4685 return Temp;
Nate Begeman07ed4172005-10-10 21:26:48 +00004686 // shl setcc result by log2 n2c
4687 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
4688 DAG.getConstant(Log2_64(N2C->getValue()),
4689 TLI.getShiftAmountTy()));
4690 }
4691
Nate Begemanf845b452005-10-08 00:29:44 +00004692 // Check to see if this is the equivalent of setcc
4693 // FIXME: Turn all of these into setcc if setcc if setcc is legal
4694 // otherwise, go ahead with the folds.
4695 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getValue() == 1ULL)) {
4696 MVT::ValueType XType = N0.getValueType();
4697 if (TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultTy())) {
4698 SDOperand Res = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
4699 if (Res.getValueType() != VT)
4700 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
4701 return Res;
4702 }
4703
4704 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
4705 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
4706 TLI.isOperationLegal(ISD::CTLZ, XType)) {
4707 SDOperand Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
4708 return DAG.getNode(ISD::SRL, XType, Ctlz,
4709 DAG.getConstant(Log2_32(MVT::getSizeInBits(XType)),
4710 TLI.getShiftAmountTy()));
4711 }
4712 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
4713 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
4714 SDOperand NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
4715 N0);
4716 SDOperand NotN0 = DAG.getNode(ISD::XOR, XType, N0,
4717 DAG.getConstant(~0ULL, XType));
4718 return DAG.getNode(ISD::SRL, XType,
4719 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
4720 DAG.getConstant(MVT::getSizeInBits(XType)-1,
4721 TLI.getShiftAmountTy()));
4722 }
4723 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
4724 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
4725 SDOperand Sign = DAG.getNode(ISD::SRL, XType, N0,
4726 DAG.getConstant(MVT::getSizeInBits(XType)-1,
4727 TLI.getShiftAmountTy()));
4728 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
4729 }
4730 }
4731
4732 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
4733 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
4734 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
Chris Lattner1982ef22007-04-11 05:11:38 +00004735 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1) &&
4736 N2.getOperand(0) == N1 && MVT::isInteger(N0.getValueType())) {
4737 MVT::ValueType XType = N0.getValueType();
4738 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
4739 DAG.getConstant(MVT::getSizeInBits(XType)-1,
4740 TLI.getShiftAmountTy()));
4741 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
4742 AddToWorkList(Shift.Val);
4743 AddToWorkList(Add.Val);
4744 return DAG.getNode(ISD::XOR, XType, Add, Shift);
4745 }
4746 // Check to see if this is an integer abs. select_cc setgt X, -1, X, -X ->
4747 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
4748 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT &&
4749 N0 == N2 && N3.getOpcode() == ISD::SUB && N0 == N3.getOperand(1)) {
4750 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N3.getOperand(0))) {
Nate Begemanf845b452005-10-08 00:29:44 +00004751 MVT::ValueType XType = N0.getValueType();
4752 if (SubC->isNullValue() && MVT::isInteger(XType)) {
4753 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
4754 DAG.getConstant(MVT::getSizeInBits(XType)-1,
Chris Lattner1982ef22007-04-11 05:11:38 +00004755 TLI.getShiftAmountTy()));
Nate Begemanf845b452005-10-08 00:29:44 +00004756 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00004757 AddToWorkList(Shift.Val);
4758 AddToWorkList(Add.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004759 return DAG.getNode(ISD::XOR, XType, Add, Shift);
4760 }
4761 }
4762 }
Chris Lattner1982ef22007-04-11 05:11:38 +00004763
Nate Begeman44728a72005-09-19 22:34:01 +00004764 return SDOperand();
4765}
4766
Evan Chengfa1eb272007-02-08 22:13:59 +00004767/// SimplifySetCC - This is a stub for TargetLowering::SimplifySetCC.
Nate Begeman452d7be2005-09-16 00:54:12 +00004768SDOperand DAGCombiner::SimplifySetCC(MVT::ValueType VT, SDOperand N0,
Nate Begemane17daeb2005-10-05 21:43:42 +00004769 SDOperand N1, ISD::CondCode Cond,
4770 bool foldBooleans) {
Evan Chengfa1eb272007-02-08 22:13:59 +00004771 TargetLowering::DAGCombinerInfo
4772 DagCombineInfo(DAG, !AfterLegalize, false, this);
4773 return TLI.SimplifySetCC(VT, N0, N1, Cond, foldBooleans, DagCombineInfo);
Nate Begeman452d7be2005-09-16 00:54:12 +00004774}
4775
Nate Begeman69575232005-10-20 02:15:44 +00004776/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
4777/// return a DAG expression to select that will generate the same value by
4778/// multiplying by a magic number. See:
4779/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
4780SDOperand DAGCombiner::BuildSDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00004781 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00004782 SDOperand S = TLI.BuildSDIV(N, DAG, &Built);
4783
Andrew Lenharth232c9102006-06-12 16:07:18 +00004784 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00004785 ii != ee; ++ii)
4786 AddToWorkList(*ii);
4787 return S;
Nate Begeman69575232005-10-20 02:15:44 +00004788}
4789
4790/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
4791/// return a DAG expression to select that will generate the same value by
4792/// multiplying by a magic number. See:
4793/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
4794SDOperand DAGCombiner::BuildUDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00004795 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00004796 SDOperand S = TLI.BuildUDIV(N, DAG, &Built);
Nate Begeman69575232005-10-20 02:15:44 +00004797
Andrew Lenharth232c9102006-06-12 16:07:18 +00004798 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00004799 ii != ee; ++ii)
4800 AddToWorkList(*ii);
4801 return S;
Nate Begeman69575232005-10-20 02:15:44 +00004802}
4803
Jim Laskey71382342006-10-07 23:37:56 +00004804/// FindBaseOffset - Return true if base is known not to alias with anything
4805/// but itself. Provides base object and offset as results.
4806static bool FindBaseOffset(SDOperand Ptr, SDOperand &Base, int64_t &Offset) {
4807 // Assume it is a primitive operation.
4808 Base = Ptr; Offset = 0;
4809
4810 // If it's an adding a simple constant then integrate the offset.
4811 if (Base.getOpcode() == ISD::ADD) {
4812 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Base.getOperand(1))) {
4813 Base = Base.getOperand(0);
4814 Offset += C->getValue();
4815 }
4816 }
4817
4818 // If it's any of the following then it can't alias with anything but itself.
4819 return isa<FrameIndexSDNode>(Base) ||
4820 isa<ConstantPoolSDNode>(Base) ||
4821 isa<GlobalAddressSDNode>(Base);
4822}
4823
4824/// isAlias - Return true if there is any possibility that the two addresses
4825/// overlap.
Jim Laskey096c22e2006-10-18 12:29:57 +00004826bool DAGCombiner::isAlias(SDOperand Ptr1, int64_t Size1,
4827 const Value *SrcValue1, int SrcValueOffset1,
4828 SDOperand Ptr2, int64_t Size2,
4829 const Value *SrcValue2, int SrcValueOffset2)
4830{
Jim Laskey71382342006-10-07 23:37:56 +00004831 // If they are the same then they must be aliases.
4832 if (Ptr1 == Ptr2) return true;
4833
4834 // Gather base node and offset information.
4835 SDOperand Base1, Base2;
4836 int64_t Offset1, Offset2;
4837 bool KnownBase1 = FindBaseOffset(Ptr1, Base1, Offset1);
4838 bool KnownBase2 = FindBaseOffset(Ptr2, Base2, Offset2);
4839
4840 // If they have a same base address then...
4841 if (Base1 == Base2) {
4842 // Check to see if the addresses overlap.
4843 return!((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
4844 }
4845
Jim Laskey096c22e2006-10-18 12:29:57 +00004846 // If we know both bases then they can't alias.
4847 if (KnownBase1 && KnownBase2) return false;
4848
Jim Laskey07a27092006-10-18 19:08:31 +00004849 if (CombinerGlobalAA) {
4850 // Use alias analysis information.
Dan Gohmane9c8fa02007-08-27 16:32:11 +00004851 int64_t MinOffset = std::min(SrcValueOffset1, SrcValueOffset2);
4852 int64_t Overlap1 = Size1 + SrcValueOffset1 - MinOffset;
4853 int64_t Overlap2 = Size2 + SrcValueOffset2 - MinOffset;
Jim Laskey07a27092006-10-18 19:08:31 +00004854 AliasAnalysis::AliasResult AAResult =
Jim Laskey096c22e2006-10-18 12:29:57 +00004855 AA.alias(SrcValue1, Overlap1, SrcValue2, Overlap2);
Jim Laskey07a27092006-10-18 19:08:31 +00004856 if (AAResult == AliasAnalysis::NoAlias)
4857 return false;
4858 }
Jim Laskey096c22e2006-10-18 12:29:57 +00004859
4860 // Otherwise we have to assume they alias.
4861 return true;
Jim Laskey71382342006-10-07 23:37:56 +00004862}
4863
4864/// FindAliasInfo - Extracts the relevant alias information from the memory
4865/// node. Returns true if the operand was a load.
Jim Laskey7ca56af2006-10-11 13:47:09 +00004866bool DAGCombiner::FindAliasInfo(SDNode *N,
Jim Laskey096c22e2006-10-18 12:29:57 +00004867 SDOperand &Ptr, int64_t &Size,
4868 const Value *&SrcValue, int &SrcValueOffset) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00004869 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
4870 Ptr = LD->getBasePtr();
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004871 Size = MVT::getSizeInBits(LD->getLoadedVT()) >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00004872 SrcValue = LD->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00004873 SrcValueOffset = LD->getSrcValueOffset();
Jim Laskey71382342006-10-07 23:37:56 +00004874 return true;
Jim Laskey7ca56af2006-10-11 13:47:09 +00004875 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00004876 Ptr = ST->getBasePtr();
Evan Cheng8b2794a2006-10-13 21:14:26 +00004877 Size = MVT::getSizeInBits(ST->getStoredVT()) >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00004878 SrcValue = ST->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00004879 SrcValueOffset = ST->getSrcValueOffset();
Jim Laskey7ca56af2006-10-11 13:47:09 +00004880 } else {
Jim Laskey71382342006-10-07 23:37:56 +00004881 assert(0 && "FindAliasInfo expected a memory operand");
Jim Laskey71382342006-10-07 23:37:56 +00004882 }
4883
4884 return false;
4885}
4886
Jim Laskey6ff23e52006-10-04 16:53:27 +00004887/// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
4888/// looking for aliasing nodes and adding them to the Aliases vector.
Jim Laskeybc588b82006-10-05 15:07:25 +00004889void DAGCombiner::GatherAllAliases(SDNode *N, SDOperand OriginalChain,
Jim Laskey6ff23e52006-10-04 16:53:27 +00004890 SmallVector<SDOperand, 8> &Aliases) {
Jim Laskeybc588b82006-10-05 15:07:25 +00004891 SmallVector<SDOperand, 8> Chains; // List of chains to visit.
Jim Laskey6ff23e52006-10-04 16:53:27 +00004892 std::set<SDNode *> Visited; // Visited node set.
4893
Jim Laskey279f0532006-09-25 16:29:54 +00004894 // Get alias information for node.
4895 SDOperand Ptr;
4896 int64_t Size;
Jim Laskey7ca56af2006-10-11 13:47:09 +00004897 const Value *SrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00004898 int SrcValueOffset;
4899 bool IsLoad = FindAliasInfo(N, Ptr, Size, SrcValue, SrcValueOffset);
Jim Laskey279f0532006-09-25 16:29:54 +00004900
Jim Laskey6ff23e52006-10-04 16:53:27 +00004901 // Starting off.
Jim Laskeybc588b82006-10-05 15:07:25 +00004902 Chains.push_back(OriginalChain);
Jim Laskey6ff23e52006-10-04 16:53:27 +00004903
Jim Laskeybc588b82006-10-05 15:07:25 +00004904 // Look at each chain and determine if it is an alias. If so, add it to the
4905 // aliases list. If not, then continue up the chain looking for the next
4906 // candidate.
4907 while (!Chains.empty()) {
4908 SDOperand Chain = Chains.back();
4909 Chains.pop_back();
Jim Laskey6ff23e52006-10-04 16:53:27 +00004910
Jim Laskeybc588b82006-10-05 15:07:25 +00004911 // Don't bother if we've been before.
4912 if (Visited.find(Chain.Val) != Visited.end()) continue;
4913 Visited.insert(Chain.Val);
4914
4915 switch (Chain.getOpcode()) {
4916 case ISD::EntryToken:
4917 // Entry token is ideal chain operand, but handled in FindBetterChain.
4918 break;
Jim Laskey6ff23e52006-10-04 16:53:27 +00004919
Jim Laskeybc588b82006-10-05 15:07:25 +00004920 case ISD::LOAD:
4921 case ISD::STORE: {
4922 // Get alias information for Chain.
4923 SDOperand OpPtr;
4924 int64_t OpSize;
Jim Laskey7ca56af2006-10-11 13:47:09 +00004925 const Value *OpSrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00004926 int OpSrcValueOffset;
4927 bool IsOpLoad = FindAliasInfo(Chain.Val, OpPtr, OpSize,
4928 OpSrcValue, OpSrcValueOffset);
Jim Laskeybc588b82006-10-05 15:07:25 +00004929
4930 // If chain is alias then stop here.
4931 if (!(IsLoad && IsOpLoad) &&
Jim Laskey096c22e2006-10-18 12:29:57 +00004932 isAlias(Ptr, Size, SrcValue, SrcValueOffset,
4933 OpPtr, OpSize, OpSrcValue, OpSrcValueOffset)) {
Jim Laskeybc588b82006-10-05 15:07:25 +00004934 Aliases.push_back(Chain);
4935 } else {
4936 // Look further up the chain.
4937 Chains.push_back(Chain.getOperand(0));
4938 // Clean up old chain.
4939 AddToWorkList(Chain.Val);
Jim Laskey279f0532006-09-25 16:29:54 +00004940 }
Jim Laskeybc588b82006-10-05 15:07:25 +00004941 break;
4942 }
4943
4944 case ISD::TokenFactor:
4945 // We have to check each of the operands of the token factor, so we queue
4946 // then up. Adding the operands to the queue (stack) in reverse order
4947 // maintains the original order and increases the likelihood that getNode
4948 // will find a matching token factor (CSE.)
4949 for (unsigned n = Chain.getNumOperands(); n;)
4950 Chains.push_back(Chain.getOperand(--n));
4951 // Eliminate the token factor if we can.
4952 AddToWorkList(Chain.Val);
4953 break;
4954
4955 default:
4956 // For all other instructions we will just have to take what we can get.
4957 Aliases.push_back(Chain);
4958 break;
Jim Laskey279f0532006-09-25 16:29:54 +00004959 }
4960 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00004961}
4962
4963/// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, looking
4964/// for a better chain (aliasing node.)
4965SDOperand DAGCombiner::FindBetterChain(SDNode *N, SDOperand OldChain) {
4966 SmallVector<SDOperand, 8> Aliases; // Ops for replacing token factor.
Jim Laskey279f0532006-09-25 16:29:54 +00004967
Jim Laskey6ff23e52006-10-04 16:53:27 +00004968 // Accumulate all the aliases to this node.
4969 GatherAllAliases(N, OldChain, Aliases);
4970
4971 if (Aliases.size() == 0) {
4972 // If no operands then chain to entry token.
4973 return DAG.getEntryNode();
4974 } else if (Aliases.size() == 1) {
4975 // If a single operand then chain to it. We don't need to revisit it.
4976 return Aliases[0];
4977 }
4978
4979 // Construct a custom tailored token factor.
4980 SDOperand NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
4981 &Aliases[0], Aliases.size());
4982
4983 // Make sure the old chain gets cleaned up.
4984 if (NewChain != OldChain) AddToWorkList(OldChain.Val);
4985
4986 return NewChain;
Jim Laskey279f0532006-09-25 16:29:54 +00004987}
4988
Nate Begeman1d4d4142005-09-01 00:19:25 +00004989// SelectionDAG::Combine - This is the entry point for the file.
4990//
Jim Laskeyc7c3f112006-10-16 20:52:31 +00004991void SelectionDAG::Combine(bool RunningAfterLegalize, AliasAnalysis &AA) {
Chris Lattner938ab022007-01-16 04:55:25 +00004992 if (!RunningAfterLegalize && ViewDAGCombine1)
4993 viewGraph();
4994 if (RunningAfterLegalize && ViewDAGCombine2)
4995 viewGraph();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004996 /// run - This is the main entry point to this class.
4997 ///
Jim Laskeyc7c3f112006-10-16 20:52:31 +00004998 DAGCombiner(*this, AA).Run(RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004999}