blob: 6510a0f70d7232783afcbebaee7a5406802d0636 [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//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Nate Begeman1d4d4142005-09-01 00:19:25 +00007//
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.
Nate Begeman1d4d4142005-09-01 00:19:25 +000012//
13//===----------------------------------------------------------------------===//
14
15#define DEBUG_TYPE "dagcombine"
Nate Begeman1d4d4142005-09-01 00:19:25 +000016#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattner00161a62008-01-25 07:20:16 +000017#include "llvm/CodeGen/MachineFunction.h"
18#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattnerc76d4412007-05-16 06:37:59 +000019#include "llvm/Analysis/AliasAnalysis.h"
Evan Cheng59d5b682007-05-07 21:27:48 +000020#include "llvm/Target/TargetData.h"
Nate Begeman1d4d4142005-09-01 00:19:25 +000021#include "llvm/Target/TargetLowering.h"
Evan Cheng59d5b682007-05-07 21:27:48 +000022#include "llvm/Target/TargetMachine.h"
Chris Lattnerddae4bd2007-01-08 23:04:05 +000023#include "llvm/Target/TargetOptions.h"
Chris Lattnerc76d4412007-05-16 06:37:59 +000024#include "llvm/ADT/SmallPtrSet.h"
25#include "llvm/ADT/Statistic.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000026#include "llvm/Support/Compiler.h"
Jim Laskeyd1aed7a2006-09-21 16:28:59 +000027#include "llvm/Support/CommandLine.h"
Chris Lattnerc76d4412007-05-16 06:37:59 +000028#include "llvm/Support/Debug.h"
29#include "llvm/Support/MathExtras.h"
Chris Lattnera500fc62005-09-09 23:53:39 +000030#include <algorithm>
Nate Begeman1d4d4142005-09-01 00:19:25 +000031using namespace llvm;
32
Chris Lattnercd3245a2006-12-19 22:41:21 +000033STATISTIC(NodesCombined , "Number of dag nodes combined");
34STATISTIC(PreIndexedNodes , "Number of pre-indexed nodes created");
35STATISTIC(PostIndexedNodes, "Number of post-indexed nodes created");
36
Nate Begeman1d4d4142005-09-01 00:19:25 +000037namespace {
Chris Lattner938ab022007-01-16 04:55:25 +000038#ifndef NDEBUG
39 static cl::opt<bool>
40 ViewDAGCombine1("view-dag-combine1-dags", cl::Hidden,
41 cl::desc("Pop up a window to show dags before the first "
42 "dag combine pass"));
43 static cl::opt<bool>
44 ViewDAGCombine2("view-dag-combine2-dags", cl::Hidden,
45 cl::desc("Pop up a window to show dags before the second "
46 "dag combine pass"));
47#else
48 static const bool ViewDAGCombine1 = false;
49 static const bool ViewDAGCombine2 = false;
50#endif
51
Jim Laskey71382342006-10-07 23:37:56 +000052 static cl::opt<bool>
53 CombinerAA("combiner-alias-analysis", cl::Hidden,
Jim Laskey26f7fa72006-10-17 19:33:52 +000054 cl::desc("Turn on alias analysis during testing"));
Jim Laskey3ad175b2006-10-12 15:22:24 +000055
Jim Laskey07a27092006-10-18 19:08:31 +000056 static cl::opt<bool>
57 CombinerGlobalAA("combiner-global-alias-analysis", cl::Hidden,
58 cl::desc("Include global information in alias analysis"));
59
Jim Laskeybc588b82006-10-05 15:07:25 +000060//------------------------------ DAGCombiner ---------------------------------//
61
Jim Laskey71382342006-10-07 23:37:56 +000062 class VISIBILITY_HIDDEN DAGCombiner {
Nate Begeman1d4d4142005-09-01 00:19:25 +000063 SelectionDAG &DAG;
64 TargetLowering &TLI;
Nate Begeman4ebd8052005-09-01 23:24:04 +000065 bool AfterLegalize;
Nate Begeman1d4d4142005-09-01 00:19:25 +000066
67 // Worklist of all of the nodes that need to be simplified.
68 std::vector<SDNode*> WorkList;
69
Jim Laskeyc7c3f112006-10-16 20:52:31 +000070 // AA - Used for DAG load/store alias analysis.
71 AliasAnalysis &AA;
72
Nate Begeman1d4d4142005-09-01 00:19:25 +000073 /// AddUsersToWorkList - When an instruction is simplified, add all users of
74 /// the instruction to the work lists because they might get more simplified
75 /// now.
76 ///
77 void AddUsersToWorkList(SDNode *N) {
78 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
Nate Begeman4ebd8052005-09-01 23:24:04 +000079 UI != UE; ++UI)
Jim Laskey6ff23e52006-10-04 16:53:27 +000080 AddToWorkList(*UI);
Nate Begeman1d4d4142005-09-01 00:19:25 +000081 }
82
83 /// removeFromWorkList - remove all instances of N from the worklist.
Chris Lattner5750df92006-03-01 04:03:14 +000084 ///
Nate Begeman1d4d4142005-09-01 00:19:25 +000085 void removeFromWorkList(SDNode *N) {
86 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), N),
87 WorkList.end());
88 }
89
Dan Gohman389079b2007-10-08 17:57:15 +000090 /// visit - call the node-specific routine that knows how to fold each
91 /// particular type of node.
92 SDOperand visit(SDNode *N);
93
Chris Lattner24664722006-03-01 04:53:38 +000094 public:
Jim Laskey6ff23e52006-10-04 16:53:27 +000095 /// AddToWorkList - Add to the work list making sure it's instance is at the
96 /// the back (next to be processed.)
Chris Lattner5750df92006-03-01 04:03:14 +000097 void AddToWorkList(SDNode *N) {
Jim Laskey6ff23e52006-10-04 16:53:27 +000098 removeFromWorkList(N);
Chris Lattner5750df92006-03-01 04:03:14 +000099 WorkList.push_back(N);
100 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000101
Jim Laskey274062c2006-10-13 23:32:28 +0000102 SDOperand CombineTo(SDNode *N, const SDOperand *To, unsigned NumTo,
103 bool AddTo = true) {
Chris Lattner3577e382006-08-11 17:56:38 +0000104 assert(N->getNumValues() == NumTo && "Broken CombineTo call!");
Chris Lattner87514ca2005-10-10 22:31:19 +0000105 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +0000106 DOUT << "\nReplacing.1 "; DEBUG(N->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +0000107 DOUT << "\nWith: "; DEBUG(To[0].Val->dump(&DAG));
108 DOUT << " and " << NumTo-1 << " other values\n";
Chris Lattner01a22022005-10-10 22:04:48 +0000109 std::vector<SDNode*> NowDead;
Chris Lattner3577e382006-08-11 17:56:38 +0000110 DAG.ReplaceAllUsesWith(N, To, &NowDead);
Chris Lattner01a22022005-10-10 22:04:48 +0000111
Jim Laskey274062c2006-10-13 23:32:28 +0000112 if (AddTo) {
113 // Push the new nodes and any users onto the worklist
114 for (unsigned i = 0, e = NumTo; i != e; ++i) {
115 AddToWorkList(To[i].Val);
116 AddUsersToWorkList(To[i].Val);
117 }
Chris Lattner01a22022005-10-10 22:04:48 +0000118 }
119
Jim Laskey6ff23e52006-10-04 16:53:27 +0000120 // Nodes can be reintroduced into the worklist. Make sure we do not
121 // process a node that has been replaced.
Chris Lattner01a22022005-10-10 22:04:48 +0000122 removeFromWorkList(N);
123 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
124 removeFromWorkList(NowDead[i]);
125
126 // Finally, since the node is now dead, remove it from the graph.
127 DAG.DeleteNode(N);
128 return SDOperand(N, 0);
129 }
Nate Begeman368e18d2006-02-16 21:11:51 +0000130
Jim Laskey274062c2006-10-13 23:32:28 +0000131 SDOperand CombineTo(SDNode *N, SDOperand Res, bool AddTo = true) {
132 return CombineTo(N, &Res, 1, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000133 }
134
Jim Laskey274062c2006-10-13 23:32:28 +0000135 SDOperand CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1,
136 bool AddTo = true) {
Chris Lattner3577e382006-08-11 17:56:38 +0000137 SDOperand To[] = { Res0, Res1 };
Jim Laskey274062c2006-10-13 23:32:28 +0000138 return CombineTo(N, To, 2, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000139 }
Chris Lattner0bd48932008-01-17 07:00:52 +0000140
Chris Lattner24664722006-03-01 04:53:38 +0000141 private:
142
Chris Lattner012f2412006-02-17 21:58:01 +0000143 /// SimplifyDemandedBits - Check the specified integer node value to see if
Chris Lattnerb2742f42006-03-01 19:55:35 +0000144 /// it can be simplified or if things it uses can be simplified by bit
Chris Lattner012f2412006-02-17 21:58:01 +0000145 /// propagation. If so, return true.
Chris Lattnere33544c2007-10-13 06:58:48 +0000146 bool SimplifyDemandedBits(SDOperand Op, uint64_t Demanded = ~0ULL) {
Chris Lattnerb16f55f2007-12-22 20:56:36 +0000147 TargetLowering::TargetLoweringOpt TLO(DAG, AfterLegalize);
Nate Begeman368e18d2006-02-16 21:11:51 +0000148 uint64_t KnownZero, KnownOne;
Chris Lattnere33544c2007-10-13 06:58:48 +0000149 Demanded &= MVT::getIntVTBitMask(Op.getValueType());
Chris Lattner012f2412006-02-17 21:58:01 +0000150 if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO))
151 return false;
152
153 // Revisit the node.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000154 AddToWorkList(Op.Val);
Chris Lattner012f2412006-02-17 21:58:01 +0000155
156 // Replace the old value with the new one.
157 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +0000158 DOUT << "\nReplacing.2 "; DEBUG(TLO.Old.Val->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +0000159 DOUT << "\nWith: "; DEBUG(TLO.New.Val->dump(&DAG));
160 DOUT << '\n';
Chris Lattner012f2412006-02-17 21:58:01 +0000161
162 std::vector<SDNode*> NowDead;
Chris Lattner01d029b2007-10-15 06:10:22 +0000163 DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New, &NowDead);
Chris Lattner012f2412006-02-17 21:58:01 +0000164
Chris Lattner7d20d392006-02-20 06:51:04 +0000165 // Push the new node and any (possibly new) users onto the worklist.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000166 AddToWorkList(TLO.New.Val);
Chris Lattner012f2412006-02-17 21:58:01 +0000167 AddUsersToWorkList(TLO.New.Val);
168
169 // Nodes can end up on the worklist more than once. Make sure we do
170 // not process a node that has been replaced.
Chris Lattner012f2412006-02-17 21:58:01 +0000171 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
172 removeFromWorkList(NowDead[i]);
173
Chris Lattner7d20d392006-02-20 06:51:04 +0000174 // Finally, if the node is now dead, remove it from the graph. The node
175 // may not be dead if the replacement process recursively simplified to
176 // something else needing this node.
177 if (TLO.Old.Val->use_empty()) {
178 removeFromWorkList(TLO.Old.Val);
Chris Lattnerec06e9a2007-04-18 03:05:22 +0000179
180 // If the operands of this node are only used by the node, they will now
181 // be dead. Make sure to visit them first to delete dead nodes early.
182 for (unsigned i = 0, e = TLO.Old.Val->getNumOperands(); i != e; ++i)
183 if (TLO.Old.Val->getOperand(i).Val->hasOneUse())
184 AddToWorkList(TLO.Old.Val->getOperand(i).Val);
185
Chris Lattner7d20d392006-02-20 06:51:04 +0000186 DAG.DeleteNode(TLO.Old.Val);
187 }
Chris Lattner012f2412006-02-17 21:58:01 +0000188 return true;
Nate Begeman368e18d2006-02-16 21:11:51 +0000189 }
Chris Lattner87514ca2005-10-10 22:31:19 +0000190
Chris Lattner448f2192006-11-11 00:39:41 +0000191 bool CombineToPreIndexedLoadStore(SDNode *N);
192 bool CombineToPostIndexedLoadStore(SDNode *N);
193
194
Dan Gohman389079b2007-10-08 17:57:15 +0000195 /// combine - call the node-specific routine that knows how to fold each
196 /// particular type of node. If that doesn't do anything, try the
197 /// target-specific DAG combines.
198 SDOperand combine(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000199
200 // Visitation implementation - Implement dag node combining for different
201 // node types. The semantics are as follows:
202 // Return Value:
Nate Begeman2300f552005-09-07 00:15:36 +0000203 // SDOperand.Val == 0 - No change was made
Chris Lattner01a22022005-10-10 22:04:48 +0000204 // SDOperand.Val == N - N was replaced, is dead, and is already handled.
Nate Begeman2300f552005-09-07 00:15:36 +0000205 // otherwise - N should be replaced by the returned Operand.
Nate Begeman1d4d4142005-09-01 00:19:25 +0000206 //
Nate Begeman83e75ec2005-09-06 04:43:02 +0000207 SDOperand visitTokenFactor(SDNode *N);
208 SDOperand visitADD(SDNode *N);
209 SDOperand visitSUB(SDNode *N);
Chris Lattner91153682007-03-04 20:03:15 +0000210 SDOperand visitADDC(SDNode *N);
211 SDOperand visitADDE(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000212 SDOperand visitMUL(SDNode *N);
213 SDOperand visitSDIV(SDNode *N);
214 SDOperand visitUDIV(SDNode *N);
215 SDOperand visitSREM(SDNode *N);
216 SDOperand visitUREM(SDNode *N);
217 SDOperand visitMULHU(SDNode *N);
218 SDOperand visitMULHS(SDNode *N);
Dan Gohman389079b2007-10-08 17:57:15 +0000219 SDOperand visitSMUL_LOHI(SDNode *N);
220 SDOperand visitUMUL_LOHI(SDNode *N);
221 SDOperand visitSDIVREM(SDNode *N);
222 SDOperand visitUDIVREM(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000223 SDOperand visitAND(SDNode *N);
224 SDOperand visitOR(SDNode *N);
225 SDOperand visitXOR(SDNode *N);
Dan Gohman7f321562007-06-25 16:23:39 +0000226 SDOperand SimplifyVBinOp(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000227 SDOperand visitSHL(SDNode *N);
228 SDOperand visitSRA(SDNode *N);
229 SDOperand visitSRL(SDNode *N);
230 SDOperand visitCTLZ(SDNode *N);
231 SDOperand visitCTTZ(SDNode *N);
232 SDOperand visitCTPOP(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000233 SDOperand visitSELECT(SDNode *N);
234 SDOperand visitSELECT_CC(SDNode *N);
235 SDOperand visitSETCC(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000236 SDOperand visitSIGN_EXTEND(SDNode *N);
237 SDOperand visitZERO_EXTEND(SDNode *N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000238 SDOperand visitANY_EXTEND(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000239 SDOperand visitSIGN_EXTEND_INREG(SDNode *N);
240 SDOperand visitTRUNCATE(SDNode *N);
Chris Lattner94683772005-12-23 05:30:37 +0000241 SDOperand visitBIT_CONVERT(SDNode *N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000242 SDOperand visitFADD(SDNode *N);
243 SDOperand visitFSUB(SDNode *N);
244 SDOperand visitFMUL(SDNode *N);
245 SDOperand visitFDIV(SDNode *N);
246 SDOperand visitFREM(SDNode *N);
Chris Lattner12d83032006-03-05 05:30:57 +0000247 SDOperand visitFCOPYSIGN(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000248 SDOperand visitSINT_TO_FP(SDNode *N);
249 SDOperand visitUINT_TO_FP(SDNode *N);
250 SDOperand visitFP_TO_SINT(SDNode *N);
251 SDOperand visitFP_TO_UINT(SDNode *N);
252 SDOperand visitFP_ROUND(SDNode *N);
253 SDOperand visitFP_ROUND_INREG(SDNode *N);
254 SDOperand visitFP_EXTEND(SDNode *N);
255 SDOperand visitFNEG(SDNode *N);
256 SDOperand visitFABS(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000257 SDOperand visitBRCOND(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000258 SDOperand visitBR_CC(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000259 SDOperand visitLOAD(SDNode *N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000260 SDOperand visitSTORE(SDNode *N);
Chris Lattnerca242442006-03-19 01:27:56 +0000261 SDOperand visitINSERT_VECTOR_ELT(SDNode *N);
Evan Cheng513da432007-10-06 08:19:55 +0000262 SDOperand visitEXTRACT_VECTOR_ELT(SDNode *N);
Dan Gohman7f321562007-06-25 16:23:39 +0000263 SDOperand visitBUILD_VECTOR(SDNode *N);
264 SDOperand visitCONCAT_VECTORS(SDNode *N);
Chris Lattner66445d32006-03-28 22:11:53 +0000265 SDOperand visitVECTOR_SHUFFLE(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000266
Evan Cheng44f1f092006-04-20 08:56:16 +0000267 SDOperand XformToShuffleWithZero(SDNode *N);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000268 SDOperand ReassociateOps(unsigned Opc, SDOperand LHS, SDOperand RHS);
269
Chris Lattnere70da202007-12-06 07:33:36 +0000270 SDOperand visitShiftByConstant(SDNode *N, unsigned Amt);
271
Chris Lattner40c62d52005-10-18 06:04:22 +0000272 bool SimplifySelectOps(SDNode *SELECT, SDOperand LHS, SDOperand RHS);
Chris Lattner35e5c142006-05-05 05:51:50 +0000273 SDOperand SimplifyBinOpWithSameOpcodeHands(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000274 SDOperand SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2);
275 SDOperand SimplifySelectCC(SDOperand N0, SDOperand N1, SDOperand N2,
Chris Lattner1eba01e2007-04-11 06:50:51 +0000276 SDOperand N3, ISD::CondCode CC,
277 bool NotExtCompare = false);
Nate Begeman452d7be2005-09-16 00:54:12 +0000278 SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N0, SDOperand N1,
Nate Begemane17daeb2005-10-05 21:43:42 +0000279 ISD::CondCode Cond, bool foldBooleans = true);
Dan Gohman389079b2007-10-08 17:57:15 +0000280 bool SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp, unsigned HiOp);
Dan Gohman7f321562007-06-25 16:23:39 +0000281 SDOperand ConstantFoldBIT_CONVERTofBUILD_VECTOR(SDNode *, MVT::ValueType);
Nate Begeman69575232005-10-20 02:15:44 +0000282 SDOperand BuildSDIV(SDNode *N);
Chris Lattner516b9622006-09-14 20:50:57 +0000283 SDOperand BuildUDIV(SDNode *N);
284 SDNode *MatchRotate(SDOperand LHS, SDOperand RHS);
Evan Chengc88138f2007-03-22 01:54:19 +0000285 SDOperand ReduceLoadWidth(SDNode *N);
Jim Laskey279f0532006-09-25 16:29:54 +0000286
Chris Lattner2b4c2792007-10-13 06:35:54 +0000287 SDOperand GetDemandedBits(SDOperand V, uint64_t Mask);
288
Jim Laskey6ff23e52006-10-04 16:53:27 +0000289 /// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
290 /// looking for aliasing nodes and adding them to the Aliases vector.
Jim Laskeybc588b82006-10-05 15:07:25 +0000291 void GatherAllAliases(SDNode *N, SDOperand OriginalChain,
Jim Laskey6ff23e52006-10-04 16:53:27 +0000292 SmallVector<SDOperand, 8> &Aliases);
293
Jim Laskey096c22e2006-10-18 12:29:57 +0000294 /// isAlias - Return true if there is any possibility that the two addresses
295 /// overlap.
296 bool isAlias(SDOperand Ptr1, int64_t Size1,
297 const Value *SrcValue1, int SrcValueOffset1,
298 SDOperand Ptr2, int64_t Size2,
Jeff Cohend41b30d2006-11-05 19:31:28 +0000299 const Value *SrcValue2, int SrcValueOffset2);
Jim Laskey096c22e2006-10-18 12:29:57 +0000300
Jim Laskey7ca56af2006-10-11 13:47:09 +0000301 /// FindAliasInfo - Extracts the relevant alias information from the memory
302 /// node. Returns true if the operand was a load.
303 bool FindAliasInfo(SDNode *N,
Jim Laskey096c22e2006-10-18 12:29:57 +0000304 SDOperand &Ptr, int64_t &Size,
305 const Value *&SrcValue, int &SrcValueOffset);
Jim Laskey7ca56af2006-10-11 13:47:09 +0000306
Jim Laskey279f0532006-09-25 16:29:54 +0000307 /// FindBetterChain - Walk up chain skipping non-aliasing memory nodes,
Jim Laskey6ff23e52006-10-04 16:53:27 +0000308 /// looking for a better chain (aliasing node.)
Jim Laskey279f0532006-09-25 16:29:54 +0000309 SDOperand FindBetterChain(SDNode *N, SDOperand Chain);
310
Nate Begeman1d4d4142005-09-01 00:19:25 +0000311public:
Jim Laskeyc7c3f112006-10-16 20:52:31 +0000312 DAGCombiner(SelectionDAG &D, AliasAnalysis &A)
313 : DAG(D),
314 TLI(D.getTargetLoweringInfo()),
315 AfterLegalize(false),
316 AA(A) {}
Nate Begeman1d4d4142005-09-01 00:19:25 +0000317
318 /// Run - runs the dag combiner on all nodes in the work list
Nate Begeman4ebd8052005-09-01 23:24:04 +0000319 void Run(bool RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000320 };
321}
322
Chris Lattner24664722006-03-01 04:53:38 +0000323//===----------------------------------------------------------------------===//
324// TargetLowering::DAGCombinerInfo implementation
325//===----------------------------------------------------------------------===//
326
327void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) {
328 ((DAGCombiner*)DC)->AddToWorkList(N);
329}
330
331SDOperand TargetLowering::DAGCombinerInfo::
332CombineTo(SDNode *N, const std::vector<SDOperand> &To) {
Chris Lattner3577e382006-08-11 17:56:38 +0000333 return ((DAGCombiner*)DC)->CombineTo(N, &To[0], To.size());
Chris Lattner24664722006-03-01 04:53:38 +0000334}
335
336SDOperand TargetLowering::DAGCombinerInfo::
337CombineTo(SDNode *N, SDOperand Res) {
338 return ((DAGCombiner*)DC)->CombineTo(N, Res);
339}
340
341
342SDOperand TargetLowering::DAGCombinerInfo::
343CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1) {
344 return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1);
345}
346
347
Chris Lattner24664722006-03-01 04:53:38 +0000348//===----------------------------------------------------------------------===//
Chris Lattner29446522007-05-14 22:04:50 +0000349// Helper Functions
350//===----------------------------------------------------------------------===//
351
352/// isNegatibleForFree - Return 1 if we can compute the negated form of the
353/// specified expression for the same cost as the expression itself, or 2 if we
354/// can compute the negated form more cheaply than the expression itself.
Chris Lattner501fee72007-05-23 07:35:22 +0000355static char isNegatibleForFree(SDOperand Op, unsigned Depth = 0) {
Dale Johannesendb44bf82007-10-16 23:38:29 +0000356 // No compile time optimizations on this type.
357 if (Op.getValueType() == MVT::ppcf128)
358 return 0;
359
Chris Lattner29446522007-05-14 22:04:50 +0000360 // fneg is removable even if it has multiple uses.
361 if (Op.getOpcode() == ISD::FNEG) return 2;
362
363 // Don't allow anything with multiple uses.
364 if (!Op.hasOneUse()) return 0;
365
Chris Lattner3adf9512007-05-25 02:19:06 +0000366 // Don't recurse exponentially.
367 if (Depth > 6) return 0;
368
Chris Lattner29446522007-05-14 22:04:50 +0000369 switch (Op.getOpcode()) {
370 default: return false;
371 case ISD::ConstantFP:
372 return 1;
373 case ISD::FADD:
374 // FIXME: determine better conditions for this xform.
375 if (!UnsafeFPMath) return 0;
376
377 // -(A+B) -> -A - B
Chris Lattner501fee72007-05-23 07:35:22 +0000378 if (char V = isNegatibleForFree(Op.getOperand(0), Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000379 return V;
380 // -(A+B) -> -B - A
Chris Lattner501fee72007-05-23 07:35:22 +0000381 return isNegatibleForFree(Op.getOperand(1), Depth+1);
Chris Lattner29446522007-05-14 22:04:50 +0000382 case ISD::FSUB:
383 // We can't turn -(A-B) into B-A when we honor signed zeros.
384 if (!UnsafeFPMath) return 0;
385
386 // -(A-B) -> B-A
387 return 1;
388
389 case ISD::FMUL:
390 case ISD::FDIV:
391 if (HonorSignDependentRoundingFPMath()) return 0;
392
393 // -(X*Y) -> (-X * Y) or (X*-Y)
Chris Lattner501fee72007-05-23 07:35:22 +0000394 if (char V = isNegatibleForFree(Op.getOperand(0), Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000395 return V;
396
Chris Lattner501fee72007-05-23 07:35:22 +0000397 return isNegatibleForFree(Op.getOperand(1), Depth+1);
Chris Lattner29446522007-05-14 22:04:50 +0000398
399 case ISD::FP_EXTEND:
400 case ISD::FP_ROUND:
401 case ISD::FSIN:
Chris Lattner501fee72007-05-23 07:35:22 +0000402 return isNegatibleForFree(Op.getOperand(0), Depth+1);
Chris Lattner29446522007-05-14 22:04:50 +0000403 }
404}
405
406/// GetNegatedExpression - If isNegatibleForFree returns true, this function
407/// returns the newly negated expression.
Chris Lattner3adf9512007-05-25 02:19:06 +0000408static SDOperand GetNegatedExpression(SDOperand Op, SelectionDAG &DAG,
409 unsigned Depth = 0) {
Chris Lattner29446522007-05-14 22:04:50 +0000410 // fneg is removable even if it has multiple uses.
411 if (Op.getOpcode() == ISD::FNEG) return Op.getOperand(0);
412
413 // Don't allow anything with multiple uses.
414 assert(Op.hasOneUse() && "Unknown reuse!");
415
Chris Lattner3adf9512007-05-25 02:19:06 +0000416 assert(Depth <= 6 && "GetNegatedExpression doesn't match isNegatibleForFree");
Chris Lattner29446522007-05-14 22:04:50 +0000417 switch (Op.getOpcode()) {
418 default: assert(0 && "Unknown code");
Dale Johannesenc4dd3c32007-08-31 23:34:27 +0000419 case ISD::ConstantFP: {
420 APFloat V = cast<ConstantFPSDNode>(Op)->getValueAPF();
421 V.changeSign();
422 return DAG.getConstantFP(V, Op.getValueType());
423 }
Chris Lattner29446522007-05-14 22:04:50 +0000424 case ISD::FADD:
425 // FIXME: determine better conditions for this xform.
426 assert(UnsafeFPMath);
427
428 // -(A+B) -> -A - B
Chris Lattner3adf9512007-05-25 02:19:06 +0000429 if (isNegatibleForFree(Op.getOperand(0), Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000430 return DAG.getNode(ISD::FSUB, Op.getValueType(),
Chris Lattner3adf9512007-05-25 02:19:06 +0000431 GetNegatedExpression(Op.getOperand(0), DAG, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000432 Op.getOperand(1));
433 // -(A+B) -> -B - A
434 return DAG.getNode(ISD::FSUB, Op.getValueType(),
Chris Lattner3adf9512007-05-25 02:19:06 +0000435 GetNegatedExpression(Op.getOperand(1), DAG, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000436 Op.getOperand(0));
437 case ISD::FSUB:
438 // We can't turn -(A-B) into B-A when we honor signed zeros.
439 assert(UnsafeFPMath);
Dan Gohman23ff1822007-07-02 15:48:56 +0000440
441 // -(0-B) -> B
442 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(Op.getOperand(0)))
Dale Johannesenc4dd3c32007-08-31 23:34:27 +0000443 if (N0CFP->getValueAPF().isZero())
Dan Gohman23ff1822007-07-02 15:48:56 +0000444 return Op.getOperand(1);
Chris Lattner29446522007-05-14 22:04:50 +0000445
446 // -(A-B) -> B-A
447 return DAG.getNode(ISD::FSUB, Op.getValueType(), Op.getOperand(1),
448 Op.getOperand(0));
449
450 case ISD::FMUL:
451 case ISD::FDIV:
452 assert(!HonorSignDependentRoundingFPMath());
453
454 // -(X*Y) -> -X * Y
Chris Lattner3adf9512007-05-25 02:19:06 +0000455 if (isNegatibleForFree(Op.getOperand(0), Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000456 return DAG.getNode(Op.getOpcode(), Op.getValueType(),
Chris Lattner3adf9512007-05-25 02:19:06 +0000457 GetNegatedExpression(Op.getOperand(0), DAG, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000458 Op.getOperand(1));
459
460 // -(X*Y) -> X * -Y
461 return DAG.getNode(Op.getOpcode(), Op.getValueType(),
462 Op.getOperand(0),
Chris Lattner3adf9512007-05-25 02:19:06 +0000463 GetNegatedExpression(Op.getOperand(1), DAG, Depth+1));
Chris Lattner29446522007-05-14 22:04:50 +0000464
465 case ISD::FP_EXTEND:
Chris Lattner29446522007-05-14 22:04:50 +0000466 case ISD::FSIN:
467 return DAG.getNode(Op.getOpcode(), Op.getValueType(),
Chris Lattner3adf9512007-05-25 02:19:06 +0000468 GetNegatedExpression(Op.getOperand(0), DAG, Depth+1));
Chris Lattner0bd48932008-01-17 07:00:52 +0000469 case ISD::FP_ROUND:
470 return DAG.getNode(ISD::FP_ROUND, Op.getValueType(),
471 GetNegatedExpression(Op.getOperand(0), DAG, Depth+1),
472 Op.getOperand(1));
Chris Lattner29446522007-05-14 22:04:50 +0000473 }
474}
Chris Lattner24664722006-03-01 04:53:38 +0000475
476
Nate Begeman4ebd8052005-09-01 23:24:04 +0000477// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
478// that selects between the values 1 and 0, making it equivalent to a setcc.
Nate Begeman646d7e22005-09-02 21:18:40 +0000479// Also, set the incoming LHS, RHS, and CC references to the appropriate
480// nodes based on the type of node we are checking. This simplifies life a
481// bit for the callers.
482static bool isSetCCEquivalent(SDOperand N, SDOperand &LHS, SDOperand &RHS,
483 SDOperand &CC) {
484 if (N.getOpcode() == ISD::SETCC) {
485 LHS = N.getOperand(0);
486 RHS = N.getOperand(1);
487 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000488 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000489 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000490 if (N.getOpcode() == ISD::SELECT_CC &&
491 N.getOperand(2).getOpcode() == ISD::Constant &&
492 N.getOperand(3).getOpcode() == ISD::Constant &&
493 cast<ConstantSDNode>(N.getOperand(2))->getValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000494 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
495 LHS = N.getOperand(0);
496 RHS = N.getOperand(1);
497 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000498 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000499 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000500 return false;
501}
502
Nate Begeman99801192005-09-07 23:25:52 +0000503// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
504// one use. If this is true, it allows the users to invert the operation for
505// free when it is profitable to do so.
506static bool isOneUseSetCC(SDOperand N) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000507 SDOperand N0, N1, N2;
Nate Begeman646d7e22005-09-02 21:18:40 +0000508 if (isSetCCEquivalent(N, N0, N1, N2) && N.Val->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000509 return true;
510 return false;
511}
512
Nate Begemancd4d58c2006-02-03 06:46:56 +0000513SDOperand DAGCombiner::ReassociateOps(unsigned Opc, SDOperand N0, SDOperand N1){
514 MVT::ValueType VT = N0.getValueType();
515 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
516 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
517 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
518 if (isa<ConstantSDNode>(N1)) {
519 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(1), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000520 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000521 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(0));
522 } else if (N0.hasOneUse()) {
523 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(0), 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(1));
526 }
527 }
528 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
529 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
530 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
531 if (isa<ConstantSDNode>(N0)) {
532 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(1), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000533 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000534 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(0));
535 } else if (N1.hasOneUse()) {
536 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(0), 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(1));
539 }
540 }
541 return SDOperand();
542}
543
Chris Lattner29446522007-05-14 22:04:50 +0000544//===----------------------------------------------------------------------===//
545// Main DAG Combiner implementation
546//===----------------------------------------------------------------------===//
547
Nate Begeman4ebd8052005-09-01 23:24:04 +0000548void DAGCombiner::Run(bool RunningAfterLegalize) {
549 // set the instance variable, so that the various visit routines may use it.
550 AfterLegalize = RunningAfterLegalize;
551
Nate Begeman646d7e22005-09-02 21:18:40 +0000552 // Add all the dag nodes to the worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000553 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
554 E = DAG.allnodes_end(); I != E; ++I)
555 WorkList.push_back(I);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000556
Chris Lattner95038592005-10-05 06:35:28 +0000557 // Create a dummy node (which is not added to allnodes), that adds a reference
558 // to the root node, preventing it from being deleted, and tracking any
559 // changes of the root.
560 HandleSDNode Dummy(DAG.getRoot());
561
Jim Laskey26f7fa72006-10-17 19:33:52 +0000562 // The root of the dag may dangle to deleted nodes until the dag combiner is
563 // done. Set it to null to avoid confusion.
564 DAG.setRoot(SDOperand());
Chris Lattner24664722006-03-01 04:53:38 +0000565
Nate Begeman1d4d4142005-09-01 00:19:25 +0000566 // while the worklist isn't empty, inspect the node on the end of it and
567 // try and combine it.
568 while (!WorkList.empty()) {
569 SDNode *N = WorkList.back();
570 WorkList.pop_back();
571
572 // If N has no uses, it is dead. Make sure to revisit all N's operands once
Chris Lattner95038592005-10-05 06:35:28 +0000573 // N is deleted from the DAG, since they too may now be dead or may have a
574 // reduced number of uses, allowing other xforms.
575 if (N->use_empty() && N != &Dummy) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000576 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Jim Laskey6ff23e52006-10-04 16:53:27 +0000577 AddToWorkList(N->getOperand(i).Val);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000578
Chris Lattner95038592005-10-05 06:35:28 +0000579 DAG.DeleteNode(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000580 continue;
581 }
582
Dan Gohman389079b2007-10-08 17:57:15 +0000583 SDOperand RV = combine(N);
Chris Lattner24664722006-03-01 04:53:38 +0000584
Chris Lattner50d8e492008-01-25 23:34:24 +0000585 if (RV.Val == 0)
586 continue;
587
588 ++NodesCombined;
589 // If we get back the same node we passed in, rather than a new node or
590 // zero, we know that the node must have defined multiple values and
591 // CombineTo was used. Since CombineTo takes care of the worklist
592 // mechanics for us, we have no work to do in this case.
593 if (RV.Val == N)
594 continue;
595
596 assert(N->getOpcode() != ISD::DELETED_NODE &&
597 RV.Val->getOpcode() != ISD::DELETED_NODE &&
598 "Node was deleted but visit returned new node!");
Chris Lattner729c6d12006-05-27 00:43:02 +0000599
Chris Lattner50d8e492008-01-25 23:34:24 +0000600 DOUT << "\nReplacing.3 "; DEBUG(N->dump(&DAG));
601 DOUT << "\nWith: "; DEBUG(RV.Val->dump(&DAG));
602 DOUT << '\n';
603 std::vector<SDNode*> NowDead;
604 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);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000610 }
Chris Lattner50d8e492008-01-25 23:34:24 +0000611
612 // Push the new node and any users onto the worklist
613 AddToWorkList(RV.Val);
614 AddUsersToWorkList(RV.Val);
615
616 // Add any uses of the old node to the worklist in case this node is the
617 // last one that uses them. They may become dead after this node is
618 // deleted.
619 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
620 AddToWorkList(N->getOperand(i).Val);
621
622 // Nodes can be reintroduced into the worklist. Make sure we do not
623 // process a node that has been replaced.
624 removeFromWorkList(N);
625 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
626 removeFromWorkList(NowDead[i]);
627
628 // Finally, since the node is now dead, remove it from the graph.
629 DAG.DeleteNode(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000630 }
Chris Lattner95038592005-10-05 06:35:28 +0000631
632 // If the root changed (e.g. it was a dead load, update the root).
633 DAG.setRoot(Dummy.getValue());
Nate Begeman1d4d4142005-09-01 00:19:25 +0000634}
635
Nate Begeman83e75ec2005-09-06 04:43:02 +0000636SDOperand DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000637 switch(N->getOpcode()) {
638 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +0000639 case ISD::TokenFactor: return visitTokenFactor(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000640 case ISD::ADD: return visitADD(N);
641 case ISD::SUB: return visitSUB(N);
Chris Lattner91153682007-03-04 20:03:15 +0000642 case ISD::ADDC: return visitADDC(N);
643 case ISD::ADDE: return visitADDE(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000644 case ISD::MUL: return visitMUL(N);
645 case ISD::SDIV: return visitSDIV(N);
646 case ISD::UDIV: return visitUDIV(N);
647 case ISD::SREM: return visitSREM(N);
648 case ISD::UREM: return visitUREM(N);
649 case ISD::MULHU: return visitMULHU(N);
650 case ISD::MULHS: return visitMULHS(N);
Dan Gohman389079b2007-10-08 17:57:15 +0000651 case ISD::SMUL_LOHI: return visitSMUL_LOHI(N);
652 case ISD::UMUL_LOHI: return visitUMUL_LOHI(N);
653 case ISD::SDIVREM: return visitSDIVREM(N);
654 case ISD::UDIVREM: return visitUDIVREM(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000655 case ISD::AND: return visitAND(N);
656 case ISD::OR: return visitOR(N);
657 case ISD::XOR: return visitXOR(N);
658 case ISD::SHL: return visitSHL(N);
659 case ISD::SRA: return visitSRA(N);
660 case ISD::SRL: return visitSRL(N);
661 case ISD::CTLZ: return visitCTLZ(N);
662 case ISD::CTTZ: return visitCTTZ(N);
663 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000664 case ISD::SELECT: return visitSELECT(N);
665 case ISD::SELECT_CC: return visitSELECT_CC(N);
666 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000667 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
668 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000669 case ISD::ANY_EXTEND: return visitANY_EXTEND(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000670 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
671 case ISD::TRUNCATE: return visitTRUNCATE(N);
Chris Lattner94683772005-12-23 05:30:37 +0000672 case ISD::BIT_CONVERT: return visitBIT_CONVERT(N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000673 case ISD::FADD: return visitFADD(N);
674 case ISD::FSUB: return visitFSUB(N);
675 case ISD::FMUL: return visitFMUL(N);
676 case ISD::FDIV: return visitFDIV(N);
677 case ISD::FREM: return visitFREM(N);
Chris Lattner12d83032006-03-05 05:30:57 +0000678 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000679 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
680 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
681 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
682 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
683 case ISD::FP_ROUND: return visitFP_ROUND(N);
684 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
685 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
686 case ISD::FNEG: return visitFNEG(N);
687 case ISD::FABS: return visitFABS(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000688 case ISD::BRCOND: return visitBRCOND(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000689 case ISD::BR_CC: return visitBR_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000690 case ISD::LOAD: return visitLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000691 case ISD::STORE: return visitSTORE(N);
Chris Lattnerca242442006-03-19 01:27:56 +0000692 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
Evan Cheng513da432007-10-06 08:19:55 +0000693 case ISD::EXTRACT_VECTOR_ELT: return visitEXTRACT_VECTOR_ELT(N);
Dan Gohman7f321562007-06-25 16:23:39 +0000694 case ISD::BUILD_VECTOR: return visitBUILD_VECTOR(N);
695 case ISD::CONCAT_VECTORS: return visitCONCAT_VECTORS(N);
Chris Lattner66445d32006-03-28 22:11:53 +0000696 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000697 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000698 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000699}
700
Dan Gohman389079b2007-10-08 17:57:15 +0000701SDOperand DAGCombiner::combine(SDNode *N) {
702
703 SDOperand RV = visit(N);
704
705 // If nothing happened, try a target-specific DAG combine.
706 if (RV.Val == 0) {
707 assert(N->getOpcode() != ISD::DELETED_NODE &&
708 "Node was deleted but visit returned NULL!");
709
710 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
711 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode())) {
712
713 // Expose the DAG combiner to the target combiner impls.
714 TargetLowering::DAGCombinerInfo
715 DagCombineInfo(DAG, !AfterLegalize, false, this);
716
717 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
718 }
719 }
720
721 return RV;
722}
723
Chris Lattner6270f682006-10-08 22:57:01 +0000724/// getInputChainForNode - Given a node, return its input chain if it has one,
725/// otherwise return a null sd operand.
726static SDOperand getInputChainForNode(SDNode *N) {
727 if (unsigned NumOps = N->getNumOperands()) {
728 if (N->getOperand(0).getValueType() == MVT::Other)
729 return N->getOperand(0);
730 else if (N->getOperand(NumOps-1).getValueType() == MVT::Other)
731 return N->getOperand(NumOps-1);
732 for (unsigned i = 1; i < NumOps-1; ++i)
733 if (N->getOperand(i).getValueType() == MVT::Other)
734 return N->getOperand(i);
735 }
736 return SDOperand(0, 0);
737}
738
Nate Begeman83e75ec2005-09-06 04:43:02 +0000739SDOperand DAGCombiner::visitTokenFactor(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +0000740 // If N has two operands, where one has an input chain equal to the other,
741 // the 'other' chain is redundant.
742 if (N->getNumOperands() == 2) {
743 if (getInputChainForNode(N->getOperand(0).Val) == N->getOperand(1))
744 return N->getOperand(0);
745 if (getInputChainForNode(N->getOperand(1).Val) == N->getOperand(0))
746 return N->getOperand(1);
747 }
748
Chris Lattnerc76d4412007-05-16 06:37:59 +0000749 SmallVector<SDNode *, 8> TFs; // List of token factors to visit.
750 SmallVector<SDOperand, 8> Ops; // Ops for replacing token factor.
751 SmallPtrSet<SDNode*, 16> SeenOps;
752 bool Changed = false; // If we should replace this token factor.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000753
754 // Start out with this token factor.
Jim Laskey279f0532006-09-25 16:29:54 +0000755 TFs.push_back(N);
Jim Laskey279f0532006-09-25 16:29:54 +0000756
Jim Laskey71382342006-10-07 23:37:56 +0000757 // Iterate through token factors. The TFs grows when new token factors are
Jim Laskeybc588b82006-10-05 15:07:25 +0000758 // encountered.
759 for (unsigned i = 0; i < TFs.size(); ++i) {
760 SDNode *TF = TFs[i];
761
Jim Laskey6ff23e52006-10-04 16:53:27 +0000762 // Check each of the operands.
763 for (unsigned i = 0, ie = TF->getNumOperands(); i != ie; ++i) {
764 SDOperand Op = TF->getOperand(i);
Jim Laskey279f0532006-09-25 16:29:54 +0000765
Jim Laskey6ff23e52006-10-04 16:53:27 +0000766 switch (Op.getOpcode()) {
767 case ISD::EntryToken:
Jim Laskeybc588b82006-10-05 15:07:25 +0000768 // Entry tokens don't need to be added to the list. They are
769 // rededundant.
770 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000771 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000772
Jim Laskey6ff23e52006-10-04 16:53:27 +0000773 case ISD::TokenFactor:
Jim Laskeybc588b82006-10-05 15:07:25 +0000774 if ((CombinerAA || Op.hasOneUse()) &&
775 std::find(TFs.begin(), TFs.end(), Op.Val) == TFs.end()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +0000776 // Queue up for processing.
777 TFs.push_back(Op.Val);
778 // Clean up in case the token factor is removed.
779 AddToWorkList(Op.Val);
780 Changed = true;
781 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000782 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000783 // Fall thru
784
785 default:
Chris Lattnerc76d4412007-05-16 06:37:59 +0000786 // Only add if it isn't already in the list.
787 if (SeenOps.insert(Op.Val))
Jim Laskeybc588b82006-10-05 15:07:25 +0000788 Ops.push_back(Op);
Chris Lattnerc76d4412007-05-16 06:37:59 +0000789 else
790 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000791 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000792 }
793 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000794 }
795
796 SDOperand Result;
797
798 // If we've change things around then replace token factor.
799 if (Changed) {
800 if (Ops.size() == 0) {
801 // The entry token is the only possible outcome.
802 Result = DAG.getEntryNode();
803 } else {
804 // New and improved token factor.
805 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0], Ops.size());
Nate Begemanded49632005-10-13 03:11:28 +0000806 }
Jim Laskey274062c2006-10-13 23:32:28 +0000807
808 // Don't add users to work list.
809 return CombineTo(N, Result, false);
Nate Begemanded49632005-10-13 03:11:28 +0000810 }
Jim Laskey279f0532006-09-25 16:29:54 +0000811
Jim Laskey6ff23e52006-10-04 16:53:27 +0000812 return Result;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000813}
814
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000815static
816SDOperand combineShlAddConstant(SDOperand N0, SDOperand N1, SelectionDAG &DAG) {
817 MVT::ValueType VT = N0.getValueType();
818 SDOperand N00 = N0.getOperand(0);
819 SDOperand N01 = N0.getOperand(1);
820 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N01);
821 if (N01C && N00.getOpcode() == ISD::ADD && N00.Val->hasOneUse() &&
822 isa<ConstantSDNode>(N00.getOperand(1))) {
823 N0 = DAG.getNode(ISD::ADD, VT,
824 DAG.getNode(ISD::SHL, VT, N00.getOperand(0), N01),
825 DAG.getNode(ISD::SHL, VT, N00.getOperand(1), N01));
826 return DAG.getNode(ISD::ADD, VT, N0, N1);
827 }
828 return SDOperand();
829}
830
Evan Chengb13cdbd2007-06-21 07:39:16 +0000831static
832SDOperand combineSelectAndUse(SDNode *N, SDOperand Slct, SDOperand OtherOp,
833 SelectionDAG &DAG) {
834 MVT::ValueType VT = N->getValueType(0);
835 unsigned Opc = N->getOpcode();
836 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
837 SDOperand LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
838 SDOperand RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
839 ISD::CondCode CC = ISD::SETCC_INVALID;
840 if (isSlctCC)
841 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
842 else {
843 SDOperand CCOp = Slct.getOperand(0);
844 if (CCOp.getOpcode() == ISD::SETCC)
845 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
846 }
847
848 bool DoXform = false;
849 bool InvCC = false;
850 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
851 "Bad input!");
852 if (LHS.getOpcode() == ISD::Constant &&
853 cast<ConstantSDNode>(LHS)->isNullValue())
854 DoXform = true;
855 else if (CC != ISD::SETCC_INVALID &&
856 RHS.getOpcode() == ISD::Constant &&
857 cast<ConstantSDNode>(RHS)->isNullValue()) {
858 std::swap(LHS, RHS);
Chris Lattner4626b252008-01-17 07:20:38 +0000859 SDOperand Op0 = Slct.getOperand(0);
860 bool isInt = MVT::isInteger(isSlctCC ? Op0.getValueType()
861 : Op0.getOperand(0).getValueType());
Evan Chengb13cdbd2007-06-21 07:39:16 +0000862 CC = ISD::getSetCCInverse(CC, isInt);
863 DoXform = true;
864 InvCC = true;
865 }
866
867 if (DoXform) {
868 SDOperand Result = DAG.getNode(Opc, VT, OtherOp, RHS);
869 if (isSlctCC)
870 return DAG.getSelectCC(OtherOp, Result,
871 Slct.getOperand(0), Slct.getOperand(1), CC);
872 SDOperand CCOp = Slct.getOperand(0);
873 if (InvCC)
874 CCOp = DAG.getSetCC(CCOp.getValueType(), CCOp.getOperand(0),
875 CCOp.getOperand(1), CC);
876 return DAG.getNode(ISD::SELECT, VT, CCOp, OtherOp, Result);
877 }
878 return SDOperand();
879}
880
Nate Begeman83e75ec2005-09-06 04:43:02 +0000881SDOperand DAGCombiner::visitADD(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000882 SDOperand N0 = N->getOperand(0);
883 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000884 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
885 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemanf89d78d2005-09-07 16:09:19 +0000886 MVT::ValueType VT = N0.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +0000887
888 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +0000889 if (MVT::isVector(VT)) {
890 SDOperand FoldedVOp = SimplifyVBinOp(N);
891 if (FoldedVOp.Val) return FoldedVOp;
892 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000893
Dan Gohman613e0d82007-07-03 14:03:57 +0000894 // fold (add x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +0000895 if (N0.getOpcode() == ISD::UNDEF)
896 return N0;
897 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +0000898 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000899 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000900 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000901 return DAG.getNode(ISD::ADD, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000902 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000903 if (N0C && !N1C)
904 return DAG.getNode(ISD::ADD, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000905 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000906 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000907 return N0;
Chris Lattner4aafb4f2006-01-12 20:22:43 +0000908 // fold ((c1-A)+c2) -> (c1+c2)-A
909 if (N1C && N0.getOpcode() == ISD::SUB)
910 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
911 return DAG.getNode(ISD::SUB, VT,
912 DAG.getConstant(N1C->getValue()+N0C->getValue(), VT),
913 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000914 // reassociate add
915 SDOperand RADD = ReassociateOps(ISD::ADD, N0, N1);
916 if (RADD.Val != 0)
917 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000918 // fold ((0-A) + B) -> B-A
919 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
920 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000921 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000922 // fold (A + (0-B)) -> A-B
923 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
924 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000925 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +0000926 // fold (A+(B-A)) -> B
927 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000928 return N1.getOperand(0);
Chris Lattner947c2892006-03-13 06:51:27 +0000929
Evan Cheng860771d2006-03-01 01:09:54 +0000930 if (!MVT::isVector(VT) && SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +0000931 return SDOperand(N, 0);
Chris Lattner947c2892006-03-13 06:51:27 +0000932
933 // fold (a+b) -> (a|b) iff a and b share no bits.
934 if (MVT::isInteger(VT) && !MVT::isVector(VT)) {
935 uint64_t LHSZero, LHSOne;
936 uint64_t RHSZero, RHSOne;
937 uint64_t Mask = MVT::getIntVTBitMask(VT);
Dan Gohmanea859be2007-06-22 14:59:07 +0000938 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Chris Lattner947c2892006-03-13 06:51:27 +0000939 if (LHSZero) {
Dan Gohmanea859be2007-06-22 14:59:07 +0000940 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
Chris Lattner947c2892006-03-13 06:51:27 +0000941
942 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
943 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
944 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
945 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
946 return DAG.getNode(ISD::OR, VT, N0, N1);
947 }
948 }
Evan Cheng3ef554d2006-11-06 08:14:30 +0000949
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000950 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
951 if (N0.getOpcode() == ISD::SHL && N0.Val->hasOneUse()) {
952 SDOperand Result = combineShlAddConstant(N0, N1, DAG);
953 if (Result.Val) return Result;
954 }
955 if (N1.getOpcode() == ISD::SHL && N1.Val->hasOneUse()) {
956 SDOperand Result = combineShlAddConstant(N1, N0, DAG);
957 if (Result.Val) return Result;
958 }
959
Evan Chengb13cdbd2007-06-21 07:39:16 +0000960 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
961 if (N0.getOpcode() == ISD::SELECT && N0.Val->hasOneUse()) {
962 SDOperand Result = combineSelectAndUse(N, N0, N1, DAG);
963 if (Result.Val) return Result;
964 }
965 if (N1.getOpcode() == ISD::SELECT && N1.Val->hasOneUse()) {
966 SDOperand Result = combineSelectAndUse(N, N1, N0, DAG);
967 if (Result.Val) return Result;
968 }
969
Nate Begeman83e75ec2005-09-06 04:43:02 +0000970 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000971}
972
Chris Lattner91153682007-03-04 20:03:15 +0000973SDOperand DAGCombiner::visitADDC(SDNode *N) {
974 SDOperand N0 = N->getOperand(0);
975 SDOperand N1 = N->getOperand(1);
976 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
977 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
978 MVT::ValueType VT = N0.getValueType();
979
980 // If the flag result is dead, turn this into an ADD.
981 if (N->hasNUsesOfValue(0, 1))
982 return CombineTo(N, DAG.getNode(ISD::ADD, VT, N1, N0),
Chris Lattnerb6541762007-03-04 20:40:38 +0000983 DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
Chris Lattner91153682007-03-04 20:03:15 +0000984
985 // canonicalize constant to RHS.
Chris Lattnerbcf24842007-03-04 20:08:45 +0000986 if (N0C && !N1C) {
987 SDOperand Ops[] = { N1, N0 };
988 return DAG.getNode(ISD::ADDC, N->getVTList(), Ops, 2);
989 }
Chris Lattner91153682007-03-04 20:03:15 +0000990
Chris Lattnerb6541762007-03-04 20:40:38 +0000991 // fold (addc x, 0) -> x + no carry out
992 if (N1C && N1C->isNullValue())
993 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
994
995 // fold (addc a, b) -> (or a, b), CARRY_FALSE iff a and b share no bits.
996 uint64_t LHSZero, LHSOne;
997 uint64_t RHSZero, RHSOne;
998 uint64_t Mask = MVT::getIntVTBitMask(VT);
Dan Gohmanea859be2007-06-22 14:59:07 +0000999 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Chris Lattnerb6541762007-03-04 20:40:38 +00001000 if (LHSZero) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001001 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
Chris Lattnerb6541762007-03-04 20:40:38 +00001002
1003 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1004 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
1005 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
1006 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
1007 return CombineTo(N, DAG.getNode(ISD::OR, VT, N0, N1),
1008 DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
1009 }
Chris Lattner91153682007-03-04 20:03:15 +00001010
1011 return SDOperand();
1012}
1013
1014SDOperand DAGCombiner::visitADDE(SDNode *N) {
1015 SDOperand N0 = N->getOperand(0);
1016 SDOperand N1 = N->getOperand(1);
Chris Lattnerb6541762007-03-04 20:40:38 +00001017 SDOperand CarryIn = N->getOperand(2);
Chris Lattner91153682007-03-04 20:03:15 +00001018 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1019 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Chris Lattnerbcf24842007-03-04 20:08:45 +00001020 //MVT::ValueType VT = N0.getValueType();
Chris Lattner91153682007-03-04 20:03:15 +00001021
1022 // canonicalize constant to RHS
Chris Lattnerbcf24842007-03-04 20:08:45 +00001023 if (N0C && !N1C) {
Chris Lattnerb6541762007-03-04 20:40:38 +00001024 SDOperand Ops[] = { N1, N0, CarryIn };
Chris Lattnerbcf24842007-03-04 20:08:45 +00001025 return DAG.getNode(ISD::ADDE, N->getVTList(), Ops, 3);
1026 }
Chris Lattner91153682007-03-04 20:03:15 +00001027
Chris Lattnerb6541762007-03-04 20:40:38 +00001028 // fold (adde x, y, false) -> (addc x, y)
1029 if (CarryIn.getOpcode() == ISD::CARRY_FALSE) {
1030 SDOperand Ops[] = { N1, N0 };
1031 return DAG.getNode(ISD::ADDC, N->getVTList(), Ops, 2);
1032 }
Chris Lattner91153682007-03-04 20:03:15 +00001033
1034 return SDOperand();
1035}
1036
1037
1038
Nate Begeman83e75ec2005-09-06 04:43:02 +00001039SDOperand DAGCombiner::visitSUB(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001040 SDOperand N0 = N->getOperand(0);
1041 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001042 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1043 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +00001044 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001045
Dan Gohman7f321562007-06-25 16:23:39 +00001046 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00001047 if (MVT::isVector(VT)) {
1048 SDOperand FoldedVOp = SimplifyVBinOp(N);
1049 if (FoldedVOp.Val) return FoldedVOp;
1050 }
Dan Gohman7f321562007-06-25 16:23:39 +00001051
Chris Lattner854077d2005-10-17 01:07:11 +00001052 // fold (sub x, x) -> 0
1053 if (N0 == N1)
1054 return DAG.getConstant(0, N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001055 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001056 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001057 return DAG.getNode(ISD::SUB, VT, N0, N1);
Chris Lattner05b57432005-10-11 06:07:15 +00001058 // fold (sub x, c) -> (add x, -c)
1059 if (N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001060 return DAG.getNode(ISD::ADD, VT, N0, DAG.getConstant(-N1C->getValue(), VT));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001061 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +00001062 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001063 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001064 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +00001065 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001066 return N0.getOperand(0);
Evan Chengb13cdbd2007-06-21 07:39:16 +00001067 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
1068 if (N1.getOpcode() == ISD::SELECT && N1.Val->hasOneUse()) {
1069 SDOperand Result = combineSelectAndUse(N, N1, N0, DAG);
1070 if (Result.Val) return Result;
1071 }
Dan Gohman613e0d82007-07-03 14:03:57 +00001072 // If either operand of a sub is undef, the result is undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00001073 if (N0.getOpcode() == ISD::UNDEF)
1074 return N0;
1075 if (N1.getOpcode() == ISD::UNDEF)
1076 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001077
Nate Begeman83e75ec2005-09-06 04:43:02 +00001078 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001079}
1080
Nate Begeman83e75ec2005-09-06 04:43:02 +00001081SDOperand DAGCombiner::visitMUL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001082 SDOperand N0 = N->getOperand(0);
1083 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001084 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1085 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman223df222005-09-08 20:18:10 +00001086 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001087
Dan Gohman7f321562007-06-25 16:23:39 +00001088 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00001089 if (MVT::isVector(VT)) {
1090 SDOperand FoldedVOp = SimplifyVBinOp(N);
1091 if (FoldedVOp.Val) return FoldedVOp;
1092 }
Dan Gohman7f321562007-06-25 16:23:39 +00001093
Dan Gohman613e0d82007-07-03 14:03:57 +00001094 // fold (mul x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001095 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001096 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001097 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001098 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001099 return DAG.getNode(ISD::MUL, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001100 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001101 if (N0C && !N1C)
1102 return DAG.getNode(ISD::MUL, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001103 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001104 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001105 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001106 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +00001107 if (N1C && N1C->isAllOnesValue())
Nate Begeman405e3ec2005-10-21 00:02:42 +00001108 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001109 // fold (mul x, (1 << c)) -> x << c
Nate Begeman646d7e22005-09-02 21:18:40 +00001110 if (N1C && isPowerOf2_64(N1C->getValue()))
Chris Lattner3e6099b2005-10-30 06:41:49 +00001111 return DAG.getNode(ISD::SHL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +00001112 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001113 TLI.getShiftAmountTy()));
Chris Lattner3e6099b2005-10-30 06:41:49 +00001114 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
1115 if (N1C && isPowerOf2_64(-N1C->getSignExtended())) {
1116 // FIXME: If the input is something that is easily negated (e.g. a
1117 // single-use add), we should put the negate there.
1118 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT),
1119 DAG.getNode(ISD::SHL, VT, N0,
1120 DAG.getConstant(Log2_64(-N1C->getSignExtended()),
1121 TLI.getShiftAmountTy())));
1122 }
Andrew Lenharth50a0d422006-04-02 21:42:45 +00001123
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001124 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
1125 if (N1C && N0.getOpcode() == ISD::SHL &&
1126 isa<ConstantSDNode>(N0.getOperand(1))) {
1127 SDOperand C3 = DAG.getNode(ISD::SHL, VT, N1, N0.getOperand(1));
Chris Lattner5750df92006-03-01 04:03:14 +00001128 AddToWorkList(C3.Val);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001129 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0), C3);
1130 }
1131
1132 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
1133 // use.
1134 {
1135 SDOperand Sh(0,0), Y(0,0);
1136 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
1137 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
1138 N0.Val->hasOneUse()) {
1139 Sh = N0; Y = N1;
1140 } else if (N1.getOpcode() == ISD::SHL &&
1141 isa<ConstantSDNode>(N1.getOperand(1)) && N1.Val->hasOneUse()) {
1142 Sh = N1; Y = N0;
1143 }
1144 if (Sh.Val) {
1145 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Sh.getOperand(0), Y);
1146 return DAG.getNode(ISD::SHL, VT, Mul, Sh.getOperand(1));
1147 }
1148 }
Chris Lattnera1deca32006-03-04 23:33:26 +00001149 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
1150 if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() &&
1151 isa<ConstantSDNode>(N0.getOperand(1))) {
1152 return DAG.getNode(ISD::ADD, VT,
1153 DAG.getNode(ISD::MUL, VT, N0.getOperand(0), N1),
1154 DAG.getNode(ISD::MUL, VT, N0.getOperand(1), N1));
1155 }
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001156
Nate Begemancd4d58c2006-02-03 06:46:56 +00001157 // reassociate mul
1158 SDOperand RMUL = ReassociateOps(ISD::MUL, N0, N1);
1159 if (RMUL.Val != 0)
1160 return RMUL;
Dan Gohman7f321562007-06-25 16:23:39 +00001161
Nate Begeman83e75ec2005-09-06 04:43:02 +00001162 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001163}
1164
Nate Begeman83e75ec2005-09-06 04:43:02 +00001165SDOperand DAGCombiner::visitSDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001166 SDOperand N0 = N->getOperand(0);
1167 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001168 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1169 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +00001170 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001171
Dan Gohman7f321562007-06-25 16:23:39 +00001172 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00001173 if (MVT::isVector(VT)) {
1174 SDOperand FoldedVOp = SimplifyVBinOp(N);
1175 if (FoldedVOp.Val) return FoldedVOp;
1176 }
Dan Gohman7f321562007-06-25 16:23:39 +00001177
Nate Begeman1d4d4142005-09-01 00:19:25 +00001178 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001179 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001180 return DAG.getNode(ISD::SDIV, VT, N0, N1);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001181 // fold (sdiv X, 1) -> X
1182 if (N1C && N1C->getSignExtended() == 1LL)
1183 return N0;
1184 // fold (sdiv X, -1) -> 0-X
1185 if (N1C && N1C->isAllOnesValue())
1186 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +00001187 // If we know the sign bits of both operands are zero, strength reduce to a
1188 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
1189 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001190 if (DAG.MaskedValueIsZero(N1, SignBit) &&
1191 DAG.MaskedValueIsZero(N0, SignBit))
Chris Lattner094c8fc2005-10-07 06:10:46 +00001192 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
Nate Begemancd6a6ed2006-02-17 07:26:20 +00001193 // fold (sdiv X, pow2) -> simple ops after legalize
Nate Begemanfb7217b2006-02-17 19:54:08 +00001194 if (N1C && N1C->getValue() && !TLI.isIntDivCheap() &&
Nate Begeman405e3ec2005-10-21 00:02:42 +00001195 (isPowerOf2_64(N1C->getSignExtended()) ||
1196 isPowerOf2_64(-N1C->getSignExtended()))) {
1197 // If dividing by powers of two is cheap, then don't perform the following
1198 // fold.
1199 if (TLI.isPow2DivCheap())
1200 return SDOperand();
1201 int64_t pow2 = N1C->getSignExtended();
1202 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
Chris Lattner8f4880b2006-02-16 08:02:36 +00001203 unsigned lg2 = Log2_64(abs2);
1204 // Splat the sign bit into the register
1205 SDOperand SGN = DAG.getNode(ISD::SRA, VT, N0,
Nate Begeman405e3ec2005-10-21 00:02:42 +00001206 DAG.getConstant(MVT::getSizeInBits(VT)-1,
1207 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00001208 AddToWorkList(SGN.Val);
Chris Lattner8f4880b2006-02-16 08:02:36 +00001209 // Add (N0 < 0) ? abs2 - 1 : 0;
1210 SDOperand SRL = DAG.getNode(ISD::SRL, VT, SGN,
1211 DAG.getConstant(MVT::getSizeInBits(VT)-lg2,
Nate Begeman405e3ec2005-10-21 00:02:42 +00001212 TLI.getShiftAmountTy()));
Chris Lattner8f4880b2006-02-16 08:02:36 +00001213 SDOperand ADD = DAG.getNode(ISD::ADD, VT, N0, SRL);
Chris Lattner5750df92006-03-01 04:03:14 +00001214 AddToWorkList(SRL.Val);
1215 AddToWorkList(ADD.Val); // Divide by pow2
Chris Lattner8f4880b2006-02-16 08:02:36 +00001216 SDOperand SRA = DAG.getNode(ISD::SRA, VT, ADD,
1217 DAG.getConstant(lg2, TLI.getShiftAmountTy()));
Nate Begeman405e3ec2005-10-21 00:02:42 +00001218 // If we're dividing by a positive value, we're done. Otherwise, we must
1219 // negate the result.
1220 if (pow2 > 0)
1221 return SRA;
Chris Lattner5750df92006-03-01 04:03:14 +00001222 AddToWorkList(SRA.Val);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001223 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), SRA);
1224 }
Nate Begeman69575232005-10-20 02:15:44 +00001225 // if integer divide is expensive and we satisfy the requirements, emit an
1226 // alternate sequence.
Nate Begeman405e3ec2005-10-21 00:02:42 +00001227 if (N1C && (N1C->getSignExtended() < -1 || N1C->getSignExtended() > 1) &&
Chris Lattnere9936d12005-10-22 18:50:15 +00001228 !TLI.isIntDivCheap()) {
1229 SDOperand Op = BuildSDIV(N);
1230 if (Op.Val) return Op;
Nate Begeman69575232005-10-20 02:15:44 +00001231 }
Dan Gohman7f321562007-06-25 16:23:39 +00001232
Dan Gohman613e0d82007-07-03 14:03:57 +00001233 // undef / X -> 0
1234 if (N0.getOpcode() == ISD::UNDEF)
1235 return DAG.getConstant(0, VT);
1236 // X / undef -> undef
1237 if (N1.getOpcode() == ISD::UNDEF)
1238 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001239
Nate Begeman83e75ec2005-09-06 04:43:02 +00001240 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001241}
1242
Nate Begeman83e75ec2005-09-06 04:43:02 +00001243SDOperand DAGCombiner::visitUDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001244 SDOperand N0 = N->getOperand(0);
1245 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001246 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1247 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +00001248 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001249
Dan Gohman7f321562007-06-25 16:23:39 +00001250 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00001251 if (MVT::isVector(VT)) {
1252 SDOperand FoldedVOp = SimplifyVBinOp(N);
1253 if (FoldedVOp.Val) return FoldedVOp;
1254 }
Dan Gohman7f321562007-06-25 16:23:39 +00001255
Nate Begeman1d4d4142005-09-01 00:19:25 +00001256 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001257 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001258 return DAG.getNode(ISD::UDIV, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001259 // fold (udiv x, (1 << c)) -> x >>u c
Nate Begeman646d7e22005-09-02 21:18:40 +00001260 if (N1C && isPowerOf2_64(N1C->getValue()))
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001261 return DAG.getNode(ISD::SRL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +00001262 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001263 TLI.getShiftAmountTy()));
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001264 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
1265 if (N1.getOpcode() == ISD::SHL) {
1266 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
1267 if (isPowerOf2_64(SHC->getValue())) {
1268 MVT::ValueType ADDVT = N1.getOperand(1).getValueType();
Nate Begemanc031e332006-02-05 07:36:48 +00001269 SDOperand Add = DAG.getNode(ISD::ADD, ADDVT, N1.getOperand(1),
1270 DAG.getConstant(Log2_64(SHC->getValue()),
1271 ADDVT));
Chris Lattner5750df92006-03-01 04:03:14 +00001272 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +00001273 return DAG.getNode(ISD::SRL, VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001274 }
1275 }
1276 }
Nate Begeman69575232005-10-20 02:15:44 +00001277 // fold (udiv x, c) -> alternate
Chris Lattnere9936d12005-10-22 18:50:15 +00001278 if (N1C && N1C->getValue() && !TLI.isIntDivCheap()) {
1279 SDOperand Op = BuildUDIV(N);
1280 if (Op.Val) return Op;
1281 }
Dan Gohman7f321562007-06-25 16:23:39 +00001282
Dan Gohman613e0d82007-07-03 14:03:57 +00001283 // undef / X -> 0
1284 if (N0.getOpcode() == ISD::UNDEF)
1285 return DAG.getConstant(0, VT);
1286 // X / undef -> undef
1287 if (N1.getOpcode() == ISD::UNDEF)
1288 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001289
Nate Begeman83e75ec2005-09-06 04:43:02 +00001290 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001291}
1292
Nate Begeman83e75ec2005-09-06 04:43:02 +00001293SDOperand DAGCombiner::visitSREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001294 SDOperand N0 = N->getOperand(0);
1295 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001296 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1297 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +00001298 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001299
1300 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001301 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001302 return DAG.getNode(ISD::SREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +00001303 // If we know the sign bits of both operands are zero, strength reduce to a
1304 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
1305 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001306 if (DAG.MaskedValueIsZero(N1, SignBit) &&
1307 DAG.MaskedValueIsZero(N0, SignBit))
Nate Begemana148d982006-01-18 22:35:16 +00001308 return DAG.getNode(ISD::UREM, VT, N0, N1);
Chris Lattner26d29902006-10-12 20:58:32 +00001309
Dan Gohman77003042007-11-26 23:46:11 +00001310 // If X/C can be simplified by the division-by-constant logic, lower
1311 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00001312 if (N1C && !N1C->isNullValue()) {
1313 SDOperand Div = DAG.getNode(ISD::SDIV, VT, N0, N1);
Dan Gohman77003042007-11-26 23:46:11 +00001314 SDOperand OptimizedDiv = combine(Div.Val);
1315 if (OptimizedDiv.Val && OptimizedDiv.Val != Div.Val) {
1316 SDOperand Mul = DAG.getNode(ISD::MUL, VT, OptimizedDiv, N1);
1317 SDOperand Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
1318 AddToWorkList(Mul.Val);
1319 return Sub;
1320 }
Chris Lattner26d29902006-10-12 20:58:32 +00001321 }
1322
Dan Gohman613e0d82007-07-03 14:03:57 +00001323 // undef % X -> 0
1324 if (N0.getOpcode() == ISD::UNDEF)
1325 return DAG.getConstant(0, VT);
1326 // X % undef -> undef
1327 if (N1.getOpcode() == ISD::UNDEF)
1328 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001329
Nate Begeman83e75ec2005-09-06 04:43:02 +00001330 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001331}
1332
Nate Begeman83e75ec2005-09-06 04:43:02 +00001333SDOperand DAGCombiner::visitUREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001334 SDOperand N0 = N->getOperand(0);
1335 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001336 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1337 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +00001338 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001339
1340 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001341 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001342 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +00001343 // fold (urem x, pow2) -> (and x, pow2-1)
1344 if (N1C && !N1C->isNullValue() && isPowerOf2_64(N1C->getValue()))
Nate Begemana148d982006-01-18 22:35:16 +00001345 return DAG.getNode(ISD::AND, VT, N0, DAG.getConstant(N1C->getValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +00001346 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
1347 if (N1.getOpcode() == ISD::SHL) {
1348 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
1349 if (isPowerOf2_64(SHC->getValue())) {
Nate Begemanbab92392006-02-05 08:07:24 +00001350 SDOperand Add = DAG.getNode(ISD::ADD, VT, N1,DAG.getConstant(~0ULL,VT));
Chris Lattner5750df92006-03-01 04:03:14 +00001351 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +00001352 return DAG.getNode(ISD::AND, VT, N0, Add);
1353 }
1354 }
1355 }
Chris Lattner26d29902006-10-12 20:58:32 +00001356
Dan Gohman77003042007-11-26 23:46:11 +00001357 // If X/C can be simplified by the division-by-constant logic, lower
1358 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00001359 if (N1C && !N1C->isNullValue()) {
1360 SDOperand Div = DAG.getNode(ISD::UDIV, VT, N0, N1);
Dan Gohman77003042007-11-26 23:46:11 +00001361 SDOperand OptimizedDiv = combine(Div.Val);
1362 if (OptimizedDiv.Val && OptimizedDiv.Val != Div.Val) {
1363 SDOperand Mul = DAG.getNode(ISD::MUL, VT, OptimizedDiv, N1);
1364 SDOperand Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
1365 AddToWorkList(Mul.Val);
1366 return Sub;
1367 }
Chris Lattner26d29902006-10-12 20:58:32 +00001368 }
1369
Dan Gohman613e0d82007-07-03 14:03:57 +00001370 // undef % X -> 0
1371 if (N0.getOpcode() == ISD::UNDEF)
1372 return DAG.getConstant(0, VT);
1373 // X % undef -> undef
1374 if (N1.getOpcode() == ISD::UNDEF)
1375 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001376
Nate Begeman83e75ec2005-09-06 04:43:02 +00001377 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001378}
1379
Nate Begeman83e75ec2005-09-06 04:43:02 +00001380SDOperand DAGCombiner::visitMULHS(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001381 SDOperand N0 = N->getOperand(0);
1382 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001383 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Dan Gohman7f321562007-06-25 16:23:39 +00001384 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001385
1386 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001387 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001388 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001389 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001390 if (N1C && N1C->getValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001391 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
1392 DAG.getConstant(MVT::getSizeInBits(N0.getValueType())-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001393 TLI.getShiftAmountTy()));
Dan Gohman613e0d82007-07-03 14:03:57 +00001394 // fold (mulhs x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001395 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001396 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00001397
Nate Begeman83e75ec2005-09-06 04:43:02 +00001398 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001399}
1400
Nate Begeman83e75ec2005-09-06 04:43:02 +00001401SDOperand DAGCombiner::visitMULHU(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001402 SDOperand N0 = N->getOperand(0);
1403 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001404 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Dan Gohman7f321562007-06-25 16:23:39 +00001405 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001406
1407 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001408 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001409 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001410 // fold (mulhu x, 1) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001411 if (N1C && N1C->getValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001412 return DAG.getConstant(0, N0.getValueType());
Dan Gohman613e0d82007-07-03 14:03:57 +00001413 // fold (mulhu x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001414 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001415 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00001416
Nate Begeman83e75ec2005-09-06 04:43:02 +00001417 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001418}
1419
Dan Gohman389079b2007-10-08 17:57:15 +00001420/// SimplifyNodeWithTwoResults - Perform optimizations common to nodes that
1421/// compute two values. LoOp and HiOp give the opcodes for the two computations
1422/// that are being performed. Return true if a simplification was made.
1423///
1424bool DAGCombiner::SimplifyNodeWithTwoResults(SDNode *N,
1425 unsigned LoOp, unsigned HiOp) {
Dan Gohman389079b2007-10-08 17:57:15 +00001426 // If the high half is not needed, just compute the low half.
Evan Cheng44711942007-11-08 09:25:29 +00001427 bool HiExists = N->hasAnyUseOfValue(1);
1428 if (!HiExists &&
Dan Gohman389079b2007-10-08 17:57:15 +00001429 (!AfterLegalize ||
1430 TLI.isOperationLegal(LoOp, N->getValueType(0)))) {
1431 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0),
1432 DAG.getNode(LoOp, N->getValueType(0),
1433 N->op_begin(),
Chris Lattner01d029b2007-10-15 06:10:22 +00001434 N->getNumOperands()));
Dan Gohman389079b2007-10-08 17:57:15 +00001435 return true;
1436 }
1437
1438 // If the low half is not needed, just compute the high half.
Evan Cheng44711942007-11-08 09:25:29 +00001439 bool LoExists = N->hasAnyUseOfValue(0);
1440 if (!LoExists &&
Dan Gohman389079b2007-10-08 17:57:15 +00001441 (!AfterLegalize ||
1442 TLI.isOperationLegal(HiOp, N->getValueType(1)))) {
1443 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1),
1444 DAG.getNode(HiOp, N->getValueType(1),
1445 N->op_begin(),
Chris Lattner01d029b2007-10-15 06:10:22 +00001446 N->getNumOperands()));
Dan Gohman389079b2007-10-08 17:57:15 +00001447 return true;
1448 }
1449
Evan Cheng44711942007-11-08 09:25:29 +00001450 // If both halves are used, return as it is.
1451 if (LoExists && HiExists)
1452 return false;
1453
1454 // If the two computed results can be simplified separately, separate them.
1455 bool RetVal = false;
1456 if (LoExists) {
1457 SDOperand Lo = DAG.getNode(LoOp, N->getValueType(0),
1458 N->op_begin(), N->getNumOperands());
1459 SDOperand LoOpt = combine(Lo.Val);
1460 if (LoOpt.Val && LoOpt != Lo &&
1461 TLI.isOperationLegal(LoOpt.getOpcode(), LoOpt.getValueType())) {
1462 RetVal = true;
1463 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), LoOpt);
Evan Cheng02132d62007-12-19 01:34:38 +00001464 } else
1465 DAG.DeleteNode(Lo.Val);
Dan Gohman389079b2007-10-08 17:57:15 +00001466 }
1467
Evan Cheng44711942007-11-08 09:25:29 +00001468 if (HiExists) {
1469 SDOperand Hi = DAG.getNode(HiOp, N->getValueType(1),
1470 N->op_begin(), N->getNumOperands());
1471 SDOperand HiOpt = combine(Hi.Val);
1472 if (HiOpt.Val && HiOpt != Hi &&
1473 TLI.isOperationLegal(HiOpt.getOpcode(), HiOpt.getValueType())) {
1474 RetVal = true;
1475 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1), HiOpt);
Evan Cheng02132d62007-12-19 01:34:38 +00001476 } else
1477 DAG.DeleteNode(Hi.Val);
Evan Cheng44711942007-11-08 09:25:29 +00001478 }
1479
1480 return RetVal;
Dan Gohman389079b2007-10-08 17:57:15 +00001481}
1482
1483SDOperand DAGCombiner::visitSMUL_LOHI(SDNode *N) {
1484
1485 if (SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHS))
1486 return SDOperand();
1487
1488 return SDOperand();
1489}
1490
1491SDOperand DAGCombiner::visitUMUL_LOHI(SDNode *N) {
1492
1493 if (SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHU))
1494 return SDOperand();
1495
1496 return SDOperand();
1497}
1498
1499SDOperand DAGCombiner::visitSDIVREM(SDNode *N) {
1500
1501 if (SimplifyNodeWithTwoResults(N, ISD::SDIV, ISD::SREM))
1502 return SDOperand();
1503
1504 return SDOperand();
1505}
1506
1507SDOperand DAGCombiner::visitUDIVREM(SDNode *N) {
1508
1509 if (SimplifyNodeWithTwoResults(N, ISD::UDIV, ISD::UREM))
1510 return SDOperand();
1511
1512 return SDOperand();
1513}
1514
Chris Lattner35e5c142006-05-05 05:51:50 +00001515/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
1516/// two operands of the same opcode, try to simplify it.
1517SDOperand DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
1518 SDOperand N0 = N->getOperand(0), N1 = N->getOperand(1);
1519 MVT::ValueType VT = N0.getValueType();
1520 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
1521
Chris Lattner540121f2006-05-05 06:31:05 +00001522 // For each of OP in AND/OR/XOR:
1523 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
1524 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
1525 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
Chris Lattner0d8dae72006-05-05 06:32:04 +00001526 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y))
Chris Lattner540121f2006-05-05 06:31:05 +00001527 if ((N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND||
Chris Lattner0d8dae72006-05-05 06:32:04 +00001528 N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::TRUNCATE) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001529 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1530 SDOperand ORNode = DAG.getNode(N->getOpcode(),
1531 N0.getOperand(0).getValueType(),
1532 N0.getOperand(0), N1.getOperand(0));
1533 AddToWorkList(ORNode.Val);
Chris Lattner540121f2006-05-05 06:31:05 +00001534 return DAG.getNode(N0.getOpcode(), VT, ORNode);
Chris Lattner35e5c142006-05-05 05:51:50 +00001535 }
1536
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001537 // For each of OP in SHL/SRL/SRA/AND...
1538 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
1539 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
1540 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
Chris Lattner35e5c142006-05-05 05:51:50 +00001541 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001542 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001543 N0.getOperand(1) == N1.getOperand(1)) {
1544 SDOperand ORNode = DAG.getNode(N->getOpcode(),
1545 N0.getOperand(0).getValueType(),
1546 N0.getOperand(0), N1.getOperand(0));
1547 AddToWorkList(ORNode.Val);
1548 return DAG.getNode(N0.getOpcode(), VT, ORNode, N0.getOperand(1));
1549 }
1550
1551 return SDOperand();
1552}
1553
Nate Begeman83e75ec2005-09-06 04:43:02 +00001554SDOperand DAGCombiner::visitAND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001555 SDOperand N0 = N->getOperand(0);
1556 SDOperand N1 = N->getOperand(1);
Nate Begemanfb7217b2006-02-17 19:54:08 +00001557 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001558 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1559 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001560 MVT::ValueType VT = N1.getValueType();
1561
Dan Gohman7f321562007-06-25 16:23:39 +00001562 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00001563 if (MVT::isVector(VT)) {
1564 SDOperand FoldedVOp = SimplifyVBinOp(N);
1565 if (FoldedVOp.Val) return FoldedVOp;
1566 }
Dan Gohman7f321562007-06-25 16:23:39 +00001567
Dan Gohman613e0d82007-07-03 14:03:57 +00001568 // fold (and x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001569 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001570 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001571 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001572 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001573 return DAG.getNode(ISD::AND, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001574 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001575 if (N0C && !N1C)
1576 return DAG.getNode(ISD::AND, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001577 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001578 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001579 return N0;
1580 // if (and x, c) is known to be zero, return 0
Dan Gohmanea859be2007-06-22 14:59:07 +00001581 if (N1C && DAG.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001582 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +00001583 // reassociate and
1584 SDOperand RAND = ReassociateOps(ISD::AND, N0, N1);
1585 if (RAND.Val != 0)
1586 return RAND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001587 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
Nate Begeman5dc7e862005-11-02 18:42:59 +00001588 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001589 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Nate Begeman646d7e22005-09-02 21:18:40 +00001590 if ((ORI->getValue() & N1C->getValue()) == N1C->getValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001591 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +00001592 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
1593 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Chris Lattner1ec05d12006-03-01 21:47:21 +00001594 unsigned InMask = MVT::getIntVTBitMask(N0.getOperand(0).getValueType());
Dan Gohmanea859be2007-06-22 14:59:07 +00001595 if (DAG.MaskedValueIsZero(N0.getOperand(0),
Chris Lattner1ec05d12006-03-01 21:47:21 +00001596 ~N1C->getValue() & InMask)) {
1597 SDOperand Zext = DAG.getNode(ISD::ZERO_EXTEND, N0.getValueType(),
1598 N0.getOperand(0));
1599
1600 // Replace uses of the AND with uses of the Zero extend node.
1601 CombineTo(N, Zext);
1602
Chris Lattner3603cd62006-02-02 07:17:31 +00001603 // We actually want to replace all uses of the any_extend with the
1604 // zero_extend, to avoid duplicating things. This will later cause this
1605 // AND to be folded.
Chris Lattner1ec05d12006-03-01 21:47:21 +00001606 CombineTo(N0.Val, Zext);
Chris Lattnerfedced72006-04-20 23:55:59 +00001607 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner3603cd62006-02-02 07:17:31 +00001608 }
1609 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001610 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
1611 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1612 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1613 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1614
1615 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1616 MVT::isInteger(LL.getValueType())) {
1617 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
1618 if (cast<ConstantSDNode>(LR)->getValue() == 0 && Op1 == ISD::SETEQ) {
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 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
1624 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
1625 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001626 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001627 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1628 }
1629 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
1630 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
1631 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001632 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001633 return DAG.getSetCC(VT, ORNode, LR, Op1);
1634 }
1635 }
1636 // canonicalize equivalent to ll == rl
1637 if (LL == RR && LR == RL) {
1638 Op1 = ISD::getSetCCSwappedOperands(Op1);
1639 std::swap(RL, RR);
1640 }
1641 if (LL == RL && LR == RR) {
1642 bool isInteger = MVT::isInteger(LL.getValueType());
1643 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
1644 if (Result != ISD::SETCC_INVALID)
1645 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1646 }
1647 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001648
1649 // Simplify: and (op x...), (op y...) -> (op (and x, y))
1650 if (N0.getOpcode() == N1.getOpcode()) {
1651 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1652 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001653 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001654
Nate Begemande996292006-02-03 22:24:05 +00001655 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
1656 // fold (and (sra)) -> (and (srl)) when possible.
Chris Lattner6ea2dee2006-03-25 22:19:00 +00001657 if (!MVT::isVector(VT) &&
1658 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001659 return SDOperand(N, 0);
Nate Begemanded49632005-10-13 03:11:28 +00001660 // fold (zext_inreg (extload x)) -> (zextload x)
Evan Cheng83060c52007-03-07 08:07:03 +00001661 if (ISD::isEXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val)) {
Evan Cheng466685d2006-10-09 20:57:25 +00001662 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00001663 MVT::ValueType EVT = LN0->getLoadedVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001664 // If we zero all the possible extended bits, then we can turn this into
1665 // a zextload if we are running before legalize or the operation is legal.
Dan Gohmanea859be2007-06-22 14:59:07 +00001666 if (DAG.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Evan Chengc5484282006-10-04 00:56:09 +00001667 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00001668 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
1669 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00001670 LN0->getSrcValueOffset(), EVT,
1671 LN0->isVolatile(),
1672 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00001673 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001674 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001675 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001676 }
1677 }
1678 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Evan Cheng83060c52007-03-07 08:07:03 +00001679 if (ISD::isSEXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val) &&
1680 N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001681 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00001682 MVT::ValueType EVT = LN0->getLoadedVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001683 // If we zero all the possible extended bits, then we can turn this into
1684 // a zextload if we are running before legalize or the operation is legal.
Dan Gohmanea859be2007-06-22 14:59:07 +00001685 if (DAG.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Evan Chengc5484282006-10-04 00:56:09 +00001686 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00001687 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
1688 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00001689 LN0->getSrcValueOffset(), EVT,
1690 LN0->isVolatile(),
1691 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00001692 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001693 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001694 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001695 }
1696 }
Chris Lattner15045b62006-02-28 06:35:35 +00001697
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001698 // fold (and (load x), 255) -> (zextload x, i8)
1699 // fold (and (extload x, i16), 255) -> (zextload x, i8)
Evan Cheng466685d2006-10-09 20:57:25 +00001700 if (N1C && N0.getOpcode() == ISD::LOAD) {
1701 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
1702 if (LN0->getExtensionType() != ISD::SEXTLOAD &&
Chris Lattnerddf89562008-01-17 19:59:44 +00001703 LN0->isUnindexed() && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001704 MVT::ValueType EVT, LoadedVT;
1705 if (N1C->getValue() == 255)
1706 EVT = MVT::i8;
1707 else if (N1C->getValue() == 65535)
1708 EVT = MVT::i16;
1709 else if (N1C->getValue() == ~0U)
1710 EVT = MVT::i32;
1711 else
1712 EVT = MVT::Other;
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001713
Evan Cheng2e49f092006-10-11 07:10:22 +00001714 LoadedVT = LN0->getLoadedVT();
Evan Cheng466685d2006-10-09 20:57:25 +00001715 if (EVT != MVT::Other && LoadedVT > EVT &&
1716 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
1717 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
1718 // For big endian targets, we need to add an offset to the pointer to
1719 // load the correct bytes. For little endian systems, we merely need to
1720 // read fewer bytes from the same pointer.
Duncan Sandsc6fa1702007-11-09 08:57:19 +00001721 unsigned LVTStoreBytes = MVT::getStoreSizeInBits(LoadedVT)/8;
1722 unsigned EVTStoreBytes = MVT::getStoreSizeInBits(EVT)/8;
1723 unsigned PtrOff = LVTStoreBytes - EVTStoreBytes;
Duncan Sandsdc846502007-10-28 12:59:45 +00001724 unsigned Alignment = LN0->getAlignment();
Evan Cheng466685d2006-10-09 20:57:25 +00001725 SDOperand NewPtr = LN0->getBasePtr();
Duncan Sandsdc846502007-10-28 12:59:45 +00001726 if (!TLI.isLittleEndian()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001727 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
1728 DAG.getConstant(PtrOff, PtrType));
Duncan Sandsdc846502007-10-28 12:59:45 +00001729 Alignment = MinAlign(Alignment, PtrOff);
1730 }
Evan Cheng466685d2006-10-09 20:57:25 +00001731 AddToWorkList(NewPtr.Val);
1732 SDOperand Load =
1733 DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00001734 LN0->getSrcValue(), LN0->getSrcValueOffset(), EVT,
Duncan Sandsdc846502007-10-28 12:59:45 +00001735 LN0->isVolatile(), Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00001736 AddToWorkList(N);
1737 CombineTo(N0.Val, Load, Load.getValue(1));
1738 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1739 }
Chris Lattner15045b62006-02-28 06:35:35 +00001740 }
1741 }
1742
Nate Begeman83e75ec2005-09-06 04:43:02 +00001743 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001744}
1745
Nate Begeman83e75ec2005-09-06 04:43:02 +00001746SDOperand DAGCombiner::visitOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001747 SDOperand N0 = N->getOperand(0);
1748 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001749 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001750 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1751 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001752 MVT::ValueType VT = N1.getValueType();
1753 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001754
Dan Gohman7f321562007-06-25 16:23:39 +00001755 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00001756 if (MVT::isVector(VT)) {
1757 SDOperand FoldedVOp = SimplifyVBinOp(N);
1758 if (FoldedVOp.Val) return FoldedVOp;
1759 }
Dan Gohman7f321562007-06-25 16:23:39 +00001760
Dan Gohman613e0d82007-07-03 14:03:57 +00001761 // fold (or x, undef) -> -1
Dan Gohmand595b5f2007-07-10 14:20:37 +00001762 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Chris Lattnere094f542007-07-09 16:16:34 +00001763 return DAG.getConstant(~0ULL, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001764 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001765 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001766 return DAG.getNode(ISD::OR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001767 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001768 if (N0C && !N1C)
1769 return DAG.getNode(ISD::OR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001770 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001771 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001772 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001773 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001774 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001775 return N1;
1776 // fold (or x, c) -> c iff (x & ~c) == 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001777 if (N1C &&
Dan Gohmanea859be2007-06-22 14:59:07 +00001778 DAG.MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits))))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001779 return N1;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001780 // reassociate or
1781 SDOperand ROR = ReassociateOps(ISD::OR, N0, N1);
1782 if (ROR.Val != 0)
1783 return ROR;
1784 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
1785 if (N1C && N0.getOpcode() == ISD::AND && N0.Val->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00001786 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00001787 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
1788 return DAG.getNode(ISD::AND, VT, DAG.getNode(ISD::OR, VT, N0.getOperand(0),
1789 N1),
1790 DAG.getConstant(N1C->getValue() | C1->getValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00001791 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001792 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
1793 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1794 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1795 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1796
1797 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1798 MVT::isInteger(LL.getValueType())) {
1799 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
1800 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
1801 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
1802 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
1803 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001804 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001805 return DAG.getSetCC(VT, ORNode, LR, Op1);
1806 }
1807 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
1808 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
1809 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
1810 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
1811 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001812 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001813 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1814 }
1815 }
1816 // canonicalize equivalent to ll == rl
1817 if (LL == RR && LR == RL) {
1818 Op1 = ISD::getSetCCSwappedOperands(Op1);
1819 std::swap(RL, RR);
1820 }
1821 if (LL == RL && LR == RR) {
1822 bool isInteger = MVT::isInteger(LL.getValueType());
1823 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
1824 if (Result != ISD::SETCC_INVALID)
1825 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1826 }
1827 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001828
1829 // Simplify: or (op x...), (op y...) -> (op (or x, y))
1830 if (N0.getOpcode() == N1.getOpcode()) {
1831 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1832 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001833 }
Chris Lattner516b9622006-09-14 20:50:57 +00001834
Chris Lattner1ec72732006-09-14 21:11:37 +00001835 // (X & C1) | (Y & C2) -> (X|Y) & C3 if possible.
1836 if (N0.getOpcode() == ISD::AND &&
1837 N1.getOpcode() == ISD::AND &&
1838 N0.getOperand(1).getOpcode() == ISD::Constant &&
1839 N1.getOperand(1).getOpcode() == ISD::Constant &&
1840 // Don't increase # computations.
1841 (N0.Val->hasOneUse() || N1.Val->hasOneUse())) {
1842 // We can only do this xform if we know that bits from X that are set in C2
1843 // but not in C1 are already zero. Likewise for Y.
1844 uint64_t LHSMask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1845 uint64_t RHSMask = cast<ConstantSDNode>(N1.getOperand(1))->getValue();
1846
Dan Gohmanea859be2007-06-22 14:59:07 +00001847 if (DAG.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) &&
1848 DAG.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) {
Chris Lattner1ec72732006-09-14 21:11:37 +00001849 SDOperand X =DAG.getNode(ISD::OR, VT, N0.getOperand(0), N1.getOperand(0));
1850 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(LHSMask|RHSMask, VT));
1851 }
1852 }
1853
1854
Chris Lattner516b9622006-09-14 20:50:57 +00001855 // See if this is some rotate idiom.
1856 if (SDNode *Rot = MatchRotate(N0, N1))
1857 return SDOperand(Rot, 0);
Chris Lattner35e5c142006-05-05 05:51:50 +00001858
Nate Begeman83e75ec2005-09-06 04:43:02 +00001859 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001860}
1861
Chris Lattner516b9622006-09-14 20:50:57 +00001862
1863/// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present.
1864static bool MatchRotateHalf(SDOperand Op, SDOperand &Shift, SDOperand &Mask) {
1865 if (Op.getOpcode() == ISD::AND) {
Reid Spencer3ed469c2006-11-02 20:25:50 +00001866 if (isa<ConstantSDNode>(Op.getOperand(1))) {
Chris Lattner516b9622006-09-14 20:50:57 +00001867 Mask = Op.getOperand(1);
1868 Op = Op.getOperand(0);
1869 } else {
1870 return false;
1871 }
1872 }
1873
1874 if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) {
1875 Shift = Op;
1876 return true;
1877 }
1878 return false;
1879}
1880
1881
1882// MatchRotate - Handle an 'or' of two operands. If this is one of the many
1883// idioms for rotate, and if the target supports rotation instructions, generate
1884// a rot[lr].
1885SDNode *DAGCombiner::MatchRotate(SDOperand LHS, SDOperand RHS) {
1886 // Must be a legal type. Expanded an promoted things won't work with rotates.
1887 MVT::ValueType VT = LHS.getValueType();
1888 if (!TLI.isTypeLegal(VT)) return 0;
1889
1890 // The target must have at least one rotate flavor.
1891 bool HasROTL = TLI.isOperationLegal(ISD::ROTL, VT);
1892 bool HasROTR = TLI.isOperationLegal(ISD::ROTR, VT);
1893 if (!HasROTL && !HasROTR) return 0;
1894
1895 // Match "(X shl/srl V1) & V2" where V2 may not be present.
1896 SDOperand LHSShift; // The shift.
1897 SDOperand LHSMask; // AND value if any.
1898 if (!MatchRotateHalf(LHS, LHSShift, LHSMask))
1899 return 0; // Not part of a rotate.
1900
1901 SDOperand RHSShift; // The shift.
1902 SDOperand RHSMask; // AND value if any.
1903 if (!MatchRotateHalf(RHS, RHSShift, RHSMask))
1904 return 0; // Not part of a rotate.
1905
1906 if (LHSShift.getOperand(0) != RHSShift.getOperand(0))
1907 return 0; // Not shifting the same value.
1908
1909 if (LHSShift.getOpcode() == RHSShift.getOpcode())
1910 return 0; // Shifts must disagree.
1911
1912 // Canonicalize shl to left side in a shl/srl pair.
1913 if (RHSShift.getOpcode() == ISD::SHL) {
1914 std::swap(LHS, RHS);
1915 std::swap(LHSShift, RHSShift);
1916 std::swap(LHSMask , RHSMask );
1917 }
1918
1919 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Scott Michelc9dc1142007-04-02 21:36:32 +00001920 SDOperand LHSShiftArg = LHSShift.getOperand(0);
1921 SDOperand LHSShiftAmt = LHSShift.getOperand(1);
1922 SDOperand RHSShiftAmt = RHSShift.getOperand(1);
Chris Lattner516b9622006-09-14 20:50:57 +00001923
1924 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
1925 // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2)
Scott Michelc9dc1142007-04-02 21:36:32 +00001926 if (LHSShiftAmt.getOpcode() == ISD::Constant &&
1927 RHSShiftAmt.getOpcode() == ISD::Constant) {
1928 uint64_t LShVal = cast<ConstantSDNode>(LHSShiftAmt)->getValue();
1929 uint64_t RShVal = cast<ConstantSDNode>(RHSShiftAmt)->getValue();
Chris Lattner516b9622006-09-14 20:50:57 +00001930 if ((LShVal + RShVal) != OpSizeInBits)
1931 return 0;
1932
1933 SDOperand Rot;
1934 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00001935 Rot = DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00001936 else
Scott Michelc9dc1142007-04-02 21:36:32 +00001937 Rot = DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00001938
1939 // If there is an AND of either shifted operand, apply it to the result.
1940 if (LHSMask.Val || RHSMask.Val) {
1941 uint64_t Mask = MVT::getIntVTBitMask(VT);
1942
1943 if (LHSMask.Val) {
1944 uint64_t RHSBits = (1ULL << LShVal)-1;
1945 Mask &= cast<ConstantSDNode>(LHSMask)->getValue() | RHSBits;
1946 }
1947 if (RHSMask.Val) {
1948 uint64_t LHSBits = ~((1ULL << (OpSizeInBits-RShVal))-1);
1949 Mask &= cast<ConstantSDNode>(RHSMask)->getValue() | LHSBits;
1950 }
1951
1952 Rot = DAG.getNode(ISD::AND, VT, Rot, DAG.getConstant(Mask, VT));
1953 }
1954
1955 return Rot.Val;
1956 }
1957
1958 // If there is a mask here, and we have a variable shift, we can't be sure
1959 // that we're masking out the right stuff.
1960 if (LHSMask.Val || RHSMask.Val)
1961 return 0;
1962
1963 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
1964 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotr x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00001965 if (RHSShiftAmt.getOpcode() == ISD::SUB &&
1966 LHSShiftAmt == RHSShiftAmt.getOperand(1)) {
Chris Lattner516b9622006-09-14 20:50:57 +00001967 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00001968 dyn_cast<ConstantSDNode>(RHSShiftAmt.getOperand(0))) {
Chris Lattner516b9622006-09-14 20:50:57 +00001969 if (SUBC->getValue() == OpSizeInBits)
1970 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00001971 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
Chris Lattner516b9622006-09-14 20:50:57 +00001972 else
Scott Michelc9dc1142007-04-02 21:36:32 +00001973 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).Val;
Chris Lattner516b9622006-09-14 20:50:57 +00001974 }
1975 }
1976
1977 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
1978 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotl x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00001979 if (LHSShiftAmt.getOpcode() == ISD::SUB &&
1980 RHSShiftAmt == LHSShiftAmt.getOperand(1)) {
Chris Lattner516b9622006-09-14 20:50:57 +00001981 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00001982 dyn_cast<ConstantSDNode>(LHSShiftAmt.getOperand(0))) {
Chris Lattner516b9622006-09-14 20:50:57 +00001983 if (SUBC->getValue() == OpSizeInBits)
1984 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00001985 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
Chris Lattner516b9622006-09-14 20:50:57 +00001986 else
Scott Michelc9dc1142007-04-02 21:36:32 +00001987 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).Val;
1988 }
1989 }
1990
1991 // Look for sign/zext/any-extended cases:
1992 if ((LHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
1993 || LHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
1994 || LHSShiftAmt.getOpcode() == ISD::ANY_EXTEND) &&
1995 (RHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
1996 || RHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
1997 || RHSShiftAmt.getOpcode() == ISD::ANY_EXTEND)) {
1998 SDOperand LExtOp0 = LHSShiftAmt.getOperand(0);
1999 SDOperand RExtOp0 = RHSShiftAmt.getOperand(0);
2000 if (RExtOp0.getOpcode() == ISD::SUB &&
2001 RExtOp0.getOperand(1) == LExtOp0) {
2002 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
2003 // (rotr x, y)
2004 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
2005 // (rotl x, (sub 32, y))
2006 if (ConstantSDNode *SUBC = cast<ConstantSDNode>(RExtOp0.getOperand(0))) {
2007 if (SUBC->getValue() == OpSizeInBits) {
2008 if (HasROTL)
2009 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
2010 else
2011 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).Val;
2012 }
2013 }
2014 } else if (LExtOp0.getOpcode() == ISD::SUB &&
2015 RExtOp0 == LExtOp0.getOperand(1)) {
2016 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext r))) ->
2017 // (rotl x, y)
2018 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext r))) ->
2019 // (rotr x, (sub 32, y))
2020 if (ConstantSDNode *SUBC = cast<ConstantSDNode>(LExtOp0.getOperand(0))) {
2021 if (SUBC->getValue() == OpSizeInBits) {
2022 if (HasROTL)
2023 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, RHSShiftAmt).Val;
2024 else
2025 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
2026 }
2027 }
Chris Lattner516b9622006-09-14 20:50:57 +00002028 }
2029 }
2030
2031 return 0;
2032}
2033
2034
Nate Begeman83e75ec2005-09-06 04:43:02 +00002035SDOperand DAGCombiner::visitXOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002036 SDOperand N0 = N->getOperand(0);
2037 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002038 SDOperand LHS, RHS, CC;
2039 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2040 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002041 MVT::ValueType VT = N0.getValueType();
2042
Dan Gohman7f321562007-06-25 16:23:39 +00002043 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00002044 if (MVT::isVector(VT)) {
2045 SDOperand FoldedVOp = SimplifyVBinOp(N);
2046 if (FoldedVOp.Val) return FoldedVOp;
2047 }
Dan Gohman7f321562007-06-25 16:23:39 +00002048
Dan Gohman613e0d82007-07-03 14:03:57 +00002049 // fold (xor x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00002050 if (N0.getOpcode() == ISD::UNDEF)
2051 return N0;
2052 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00002053 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002054 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002055 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002056 return DAG.getNode(ISD::XOR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00002057 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00002058 if (N0C && !N1C)
2059 return DAG.getNode(ISD::XOR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002060 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002061 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002062 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00002063 // reassociate xor
2064 SDOperand RXOR = ReassociateOps(ISD::XOR, N0, N1);
2065 if (RXOR.Val != 0)
2066 return RXOR;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002067 // fold !(x cc y) -> (x !cc y)
Nate Begeman646d7e22005-09-02 21:18:40 +00002068 if (N1C && N1C->getValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
2069 bool isInt = MVT::isInteger(LHS.getValueType());
2070 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
2071 isInt);
2072 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002073 return DAG.getSetCC(VT, LHS, RHS, NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00002074 if (N0.getOpcode() == ISD::SELECT_CC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002075 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00002076 assert(0 && "Unhandled SetCC Equivalent!");
2077 abort();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002078 }
Chris Lattner61c5ff42007-09-10 21:39:07 +00002079 // fold (not (zext (setcc x, y))) -> (zext (not (setcc x, y)))
2080 if (N1C && N1C->getValue() == 1 && N0.getOpcode() == ISD::ZERO_EXTEND &&
2081 N0.Val->hasOneUse() && isSetCCEquivalent(N0.getOperand(0), LHS, RHS, CC)){
2082 SDOperand V = N0.getOperand(0);
2083 V = DAG.getNode(ISD::XOR, V.getValueType(), V,
Duncan Sands272dce02007-10-10 09:54:50 +00002084 DAG.getConstant(1, V.getValueType()));
Chris Lattner61c5ff42007-09-10 21:39:07 +00002085 AddToWorkList(V.Val);
2086 return DAG.getNode(ISD::ZERO_EXTEND, VT, V);
2087 }
2088
Nate Begeman99801192005-09-07 23:25:52 +00002089 // fold !(x or y) -> (!x and !y) iff x or y are setcc
Chris Lattner734c91d2006-11-10 21:37:15 +00002090 if (N1C && N1C->getValue() == 1 && VT == MVT::i1 &&
Nate Begeman99801192005-09-07 23:25:52 +00002091 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002092 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00002093 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
2094 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002095 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
2096 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00002097 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00002098 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002099 }
2100 }
Nate Begeman99801192005-09-07 23:25:52 +00002101 // fold !(x or y) -> (!x and !y) iff x or y are constants
2102 if (N1C && N1C->isAllOnesValue() &&
2103 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002104 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00002105 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
2106 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002107 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
2108 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00002109 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00002110 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002111 }
2112 }
Nate Begeman223df222005-09-08 20:18:10 +00002113 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
2114 if (N1C && N0.getOpcode() == ISD::XOR) {
2115 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
2116 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2117 if (N00C)
2118 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
2119 DAG.getConstant(N1C->getValue()^N00C->getValue(), VT));
2120 if (N01C)
2121 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
2122 DAG.getConstant(N1C->getValue()^N01C->getValue(), VT));
2123 }
2124 // fold (xor x, x) -> 0
Chris Lattner4fbdd592006-03-28 19:11:05 +00002125 if (N0 == N1) {
2126 if (!MVT::isVector(VT)) {
2127 return DAG.getConstant(0, VT);
2128 } else if (!AfterLegalize || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) {
2129 // Produce a vector of zeros.
Dan Gohman51eaa862007-06-14 22:58:02 +00002130 SDOperand El = DAG.getConstant(0, MVT::getVectorElementType(VT));
Chris Lattner4fbdd592006-03-28 19:11:05 +00002131 std::vector<SDOperand> Ops(MVT::getVectorNumElements(VT), El);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002132 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4fbdd592006-03-28 19:11:05 +00002133 }
2134 }
Chris Lattner35e5c142006-05-05 05:51:50 +00002135
2136 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
2137 if (N0.getOpcode() == N1.getOpcode()) {
2138 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
2139 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002140 }
Chris Lattner35e5c142006-05-05 05:51:50 +00002141
Chris Lattner3e104b12006-04-08 04:15:24 +00002142 // Simplify the expression using non-local knowledge.
2143 if (!MVT::isVector(VT) &&
2144 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00002145 return SDOperand(N, 0);
Chris Lattner3e104b12006-04-08 04:15:24 +00002146
Nate Begeman83e75ec2005-09-06 04:43:02 +00002147 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002148}
2149
Chris Lattnere70da202007-12-06 07:33:36 +00002150/// visitShiftByConstant - Handle transforms common to the three shifts, when
2151/// the shift amount is a constant.
2152SDOperand DAGCombiner::visitShiftByConstant(SDNode *N, unsigned Amt) {
2153 SDNode *LHS = N->getOperand(0).Val;
2154 if (!LHS->hasOneUse()) return SDOperand();
2155
2156 // We want to pull some binops through shifts, so that we have (and (shift))
2157 // instead of (shift (and)), likewise for add, or, xor, etc. This sort of
2158 // thing happens with address calculations, so it's important to canonicalize
2159 // it.
2160 bool HighBitSet = false; // Can we transform this if the high bit is set?
2161
2162 switch (LHS->getOpcode()) {
2163 default: return SDOperand();
2164 case ISD::OR:
2165 case ISD::XOR:
2166 HighBitSet = false; // We can only transform sra if the high bit is clear.
2167 break;
2168 case ISD::AND:
2169 HighBitSet = true; // We can only transform sra if the high bit is set.
2170 break;
2171 case ISD::ADD:
2172 if (N->getOpcode() != ISD::SHL)
2173 return SDOperand(); // only shl(add) not sr[al](add).
2174 HighBitSet = false; // We can only transform sra if the high bit is clear.
2175 break;
2176 }
2177
2178 // We require the RHS of the binop to be a constant as well.
2179 ConstantSDNode *BinOpCst = dyn_cast<ConstantSDNode>(LHS->getOperand(1));
2180 if (!BinOpCst) return SDOperand();
2181
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00002182
2183 // FIXME: disable this for unless the input to the binop is a shift by a
2184 // constant. If it is not a shift, it pessimizes some common cases like:
2185 //
2186 //void foo(int *X, int i) { X[i & 1235] = 1; }
2187 //int bar(int *X, int i) { return X[i & 255]; }
2188 SDNode *BinOpLHSVal = LHS->getOperand(0).Val;
2189 if ((BinOpLHSVal->getOpcode() != ISD::SHL &&
2190 BinOpLHSVal->getOpcode() != ISD::SRA &&
2191 BinOpLHSVal->getOpcode() != ISD::SRL) ||
2192 !isa<ConstantSDNode>(BinOpLHSVal->getOperand(1)))
2193 return SDOperand();
2194
Chris Lattnere70da202007-12-06 07:33:36 +00002195 MVT::ValueType VT = N->getValueType(0);
2196
2197 // If this is a signed shift right, and the high bit is modified
2198 // by the logical operation, do not perform the transformation.
2199 // The highBitSet boolean indicates the value of the high bit of
2200 // the constant which would cause it to be modified for this
2201 // operation.
2202 if (N->getOpcode() == ISD::SRA) {
2203 uint64_t BinOpRHSSign = BinOpCst->getValue() >> MVT::getSizeInBits(VT)-1;
2204 if ((bool)BinOpRHSSign != HighBitSet)
2205 return SDOperand();
2206 }
2207
2208 // Fold the constants, shifting the binop RHS by the shift amount.
2209 SDOperand NewRHS = DAG.getNode(N->getOpcode(), N->getValueType(0),
2210 LHS->getOperand(1), N->getOperand(1));
2211
2212 // Create the new shift.
2213 SDOperand NewShift = DAG.getNode(N->getOpcode(), VT, LHS->getOperand(0),
2214 N->getOperand(1));
2215
2216 // Create the new binop.
2217 return DAG.getNode(LHS->getOpcode(), VT, NewShift, NewRHS);
2218}
2219
2220
Nate Begeman83e75ec2005-09-06 04:43:02 +00002221SDOperand DAGCombiner::visitSHL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002222 SDOperand N0 = N->getOperand(0);
2223 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002224 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2225 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002226 MVT::ValueType VT = N0.getValueType();
2227 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
2228
2229 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002230 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002231 return DAG.getNode(ISD::SHL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002232 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002233 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002234 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002235 // fold (shl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00002236 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002237 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002238 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002239 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002240 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002241 // if (shl x, c) is known to be zero, return 0
Dan Gohmanea859be2007-06-22 14:59:07 +00002242 if (DAG.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002243 return DAG.getConstant(0, VT);
Chris Lattner61a4c072007-04-18 03:06:49 +00002244 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00002245 return SDOperand(N, 0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002246 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002247 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002248 N0.getOperand(1).getOpcode() == ISD::Constant) {
2249 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00002250 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002251 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002252 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002253 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002254 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002255 }
2256 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
2257 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002258 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002259 N0.getOperand(1).getOpcode() == ISD::Constant) {
2260 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00002261 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002262 SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
2263 DAG.getConstant(~0ULL << c1, VT));
2264 if (c2 > c1)
2265 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +00002266 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002267 else
Nate Begeman83e75ec2005-09-06 04:43:02 +00002268 return DAG.getNode(ISD::SRL, VT, Mask,
2269 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002270 }
2271 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00002272 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +00002273 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002274 DAG.getConstant(~0ULL << N1C->getValue(), VT));
Chris Lattnere70da202007-12-06 07:33:36 +00002275
2276 return N1C ? visitShiftByConstant(N, N1C->getValue()) : SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002277}
2278
Nate Begeman83e75ec2005-09-06 04:43:02 +00002279SDOperand DAGCombiner::visitSRA(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002280 SDOperand N0 = N->getOperand(0);
2281 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002282 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2283 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002284 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002285
2286 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002287 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002288 return DAG.getNode(ISD::SRA, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002289 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002290 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002291 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002292 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00002293 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002294 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002295 // fold (sra x, c >= size(x)) -> undef
Nate Begemanfb7217b2006-02-17 19:54:08 +00002296 if (N1C && N1C->getValue() >= MVT::getSizeInBits(VT))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002297 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002298 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002299 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002300 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00002301 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
2302 // sext_inreg.
2303 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
2304 unsigned LowBits = MVT::getSizeInBits(VT) - (unsigned)N1C->getValue();
2305 MVT::ValueType EVT;
2306 switch (LowBits) {
2307 default: EVT = MVT::Other; break;
2308 case 1: EVT = MVT::i1; break;
2309 case 8: EVT = MVT::i8; break;
2310 case 16: EVT = MVT::i16; break;
2311 case 32: EVT = MVT::i32; break;
2312 }
2313 if (EVT > MVT::Other && TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, EVT))
2314 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
2315 DAG.getValueType(EVT));
2316 }
Chris Lattner71d9ebc2006-02-28 06:23:04 +00002317
2318 // fold (sra (sra x, c1), c2) -> (sra x, c1+c2)
2319 if (N1C && N0.getOpcode() == ISD::SRA) {
2320 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2321 unsigned Sum = N1C->getValue() + C1->getValue();
2322 if (Sum >= MVT::getSizeInBits(VT)) Sum = MVT::getSizeInBits(VT)-1;
2323 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0),
2324 DAG.getConstant(Sum, N1C->getValueType(0)));
2325 }
2326 }
2327
Chris Lattnera8504462006-05-08 20:51:54 +00002328 // Simplify, based on bits shifted out of the LHS.
2329 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
2330 return SDOperand(N, 0);
2331
2332
Nate Begeman1d4d4142005-09-01 00:19:25 +00002333 // If the sign bit is known to be zero, switch this to a SRL.
Dan Gohmanea859be2007-06-22 14:59:07 +00002334 if (DAG.MaskedValueIsZero(N0, MVT::getIntVTSignBit(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002335 return DAG.getNode(ISD::SRL, VT, N0, N1);
Chris Lattnere70da202007-12-06 07:33:36 +00002336
2337 return N1C ? visitShiftByConstant(N, N1C->getValue()) : SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002338}
2339
Nate Begeman83e75ec2005-09-06 04:43:02 +00002340SDOperand DAGCombiner::visitSRL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002341 SDOperand N0 = N->getOperand(0);
2342 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002343 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2344 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002345 MVT::ValueType VT = N0.getValueType();
2346 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
2347
2348 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002349 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002350 return DAG.getNode(ISD::SRL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002351 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002352 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002353 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002354 // fold (srl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00002355 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002356 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002357 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002358 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002359 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002360 // if (srl x, c) is known to be zero, return 0
Dan Gohmanea859be2007-06-22 14:59:07 +00002361 if (N1C && DAG.MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002362 return DAG.getConstant(0, VT);
Chris Lattnerec06e9a2007-04-18 03:05:22 +00002363
Nate Begeman1d4d4142005-09-01 00:19:25 +00002364 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002365 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002366 N0.getOperand(1).getOpcode() == ISD::Constant) {
2367 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00002368 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002369 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002370 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002371 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002372 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002373 }
Chris Lattner350bec02006-04-02 06:11:11 +00002374
Chris Lattner06afe072006-05-05 22:53:17 +00002375 // fold (srl (anyextend x), c) -> (anyextend (srl x, c))
2376 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
2377 // Shifting in all undef bits?
2378 MVT::ValueType SmallVT = N0.getOperand(0).getValueType();
2379 if (N1C->getValue() >= MVT::getSizeInBits(SmallVT))
2380 return DAG.getNode(ISD::UNDEF, VT);
2381
2382 SDOperand SmallShift = DAG.getNode(ISD::SRL, SmallVT, N0.getOperand(0), N1);
2383 AddToWorkList(SmallShift.Val);
2384 return DAG.getNode(ISD::ANY_EXTEND, VT, SmallShift);
2385 }
2386
Chris Lattner3657ffe2006-10-12 20:23:19 +00002387 // fold (srl (sra X, Y), 31) -> (srl X, 31). This srl only looks at the sign
2388 // bit, which is unmodified by sra.
2389 if (N1C && N1C->getValue()+1 == MVT::getSizeInBits(VT)) {
2390 if (N0.getOpcode() == ISD::SRA)
2391 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0), N1);
2392 }
2393
Chris Lattner350bec02006-04-02 06:11:11 +00002394 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
2395 if (N1C && N0.getOpcode() == ISD::CTLZ &&
2396 N1C->getValue() == Log2_32(MVT::getSizeInBits(VT))) {
2397 uint64_t KnownZero, KnownOne, Mask = MVT::getIntVTBitMask(VT);
Dan Gohmanea859be2007-06-22 14:59:07 +00002398 DAG.ComputeMaskedBits(N0.getOperand(0), Mask, KnownZero, KnownOne);
Chris Lattner350bec02006-04-02 06:11:11 +00002399
2400 // If any of the input bits are KnownOne, then the input couldn't be all
2401 // zeros, thus the result of the srl will always be zero.
2402 if (KnownOne) return DAG.getConstant(0, VT);
2403
2404 // If all of the bits input the to ctlz node are known to be zero, then
2405 // the result of the ctlz is "32" and the result of the shift is one.
2406 uint64_t UnknownBits = ~KnownZero & Mask;
2407 if (UnknownBits == 0) return DAG.getConstant(1, VT);
2408
2409 // Otherwise, check to see if there is exactly one bit input to the ctlz.
2410 if ((UnknownBits & (UnknownBits-1)) == 0) {
2411 // Okay, we know that only that the single bit specified by UnknownBits
2412 // could be set on input to the CTLZ node. If this bit is set, the SRL
2413 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
2414 // to an SRL,XOR pair, which is likely to simplify more.
2415 unsigned ShAmt = CountTrailingZeros_64(UnknownBits);
2416 SDOperand Op = N0.getOperand(0);
2417 if (ShAmt) {
2418 Op = DAG.getNode(ISD::SRL, VT, Op,
2419 DAG.getConstant(ShAmt, TLI.getShiftAmountTy()));
2420 AddToWorkList(Op.Val);
2421 }
2422 return DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(1, VT));
2423 }
2424 }
Chris Lattner61a4c072007-04-18 03:06:49 +00002425
2426 // fold operands of srl based on knowledge that the low bits are not
2427 // demanded.
2428 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
2429 return SDOperand(N, 0);
2430
Chris Lattnere70da202007-12-06 07:33:36 +00002431 return N1C ? visitShiftByConstant(N, N1C->getValue()) : SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002432}
2433
Nate Begeman83e75ec2005-09-06 04:43:02 +00002434SDOperand DAGCombiner::visitCTLZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002435 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00002436 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002437
2438 // fold (ctlz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002439 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002440 return DAG.getNode(ISD::CTLZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002441 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002442}
2443
Nate Begeman83e75ec2005-09-06 04:43:02 +00002444SDOperand DAGCombiner::visitCTTZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002445 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00002446 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002447
2448 // fold (cttz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002449 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002450 return DAG.getNode(ISD::CTTZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002451 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002452}
2453
Nate Begeman83e75ec2005-09-06 04:43:02 +00002454SDOperand DAGCombiner::visitCTPOP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002455 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00002456 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002457
2458 // fold (ctpop c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002459 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002460 return DAG.getNode(ISD::CTPOP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002461 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002462}
2463
Nate Begeman452d7be2005-09-16 00:54:12 +00002464SDOperand DAGCombiner::visitSELECT(SDNode *N) {
2465 SDOperand N0 = N->getOperand(0);
2466 SDOperand N1 = N->getOperand(1);
2467 SDOperand N2 = N->getOperand(2);
2468 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2469 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2470 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
2471 MVT::ValueType VT = N->getValueType(0);
Evan Cheng571c4782007-08-18 05:57:05 +00002472 MVT::ValueType VT0 = N0.getValueType();
Nate Begeman44728a72005-09-19 22:34:01 +00002473
Nate Begeman452d7be2005-09-16 00:54:12 +00002474 // fold select C, X, X -> X
2475 if (N1 == N2)
2476 return N1;
2477 // fold select true, X, Y -> X
2478 if (N0C && !N0C->isNullValue())
2479 return N1;
2480 // fold select false, X, Y -> Y
2481 if (N0C && N0C->isNullValue())
2482 return N2;
2483 // fold select C, 1, X -> C | X
Nate Begeman44728a72005-09-19 22:34:01 +00002484 if (MVT::i1 == VT && N1C && N1C->getValue() == 1)
Nate Begeman452d7be2005-09-16 00:54:12 +00002485 return DAG.getNode(ISD::OR, VT, N0, N2);
Evan Cheng571c4782007-08-18 05:57:05 +00002486 // fold select C, 0, 1 -> ~C
2487 if (MVT::isInteger(VT) && MVT::isInteger(VT0) &&
2488 N1C && N2C && N1C->isNullValue() && N2C->getValue() == 1) {
2489 SDOperand XORNode = DAG.getNode(ISD::XOR, VT0, N0, DAG.getConstant(1, VT0));
2490 if (VT == VT0)
2491 return XORNode;
2492 AddToWorkList(XORNode.Val);
2493 if (MVT::getSizeInBits(VT) > MVT::getSizeInBits(VT0))
2494 return DAG.getNode(ISD::ZERO_EXTEND, VT, XORNode);
2495 return DAG.getNode(ISD::TRUNCATE, VT, XORNode);
2496 }
Nate Begeman452d7be2005-09-16 00:54:12 +00002497 // fold select C, 0, X -> ~C & X
Dale Johannesene0e6fac2007-12-06 17:53:31 +00002498 if (VT == VT0 && VT == MVT::i1 && N1C && N1C->isNullValue()) {
2499 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00002500 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00002501 return DAG.getNode(ISD::AND, VT, XORNode, N2);
2502 }
2503 // fold select C, X, 1 -> ~C | X
Dale Johannesene0e6fac2007-12-06 17:53:31 +00002504 if (VT == VT0 && VT == MVT::i1 && N2C && N2C->getValue() == 1) {
2505 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00002506 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00002507 return DAG.getNode(ISD::OR, VT, XORNode, N1);
2508 }
2509 // fold select C, X, 0 -> C & X
2510 // FIXME: this should check for C type == X type, not i1?
2511 if (MVT::i1 == VT && N2C && N2C->isNullValue())
2512 return DAG.getNode(ISD::AND, VT, N0, N1);
2513 // fold X ? X : Y --> X ? 1 : Y --> X | Y
2514 if (MVT::i1 == VT && N0 == N1)
2515 return DAG.getNode(ISD::OR, VT, N0, N2);
2516 // fold X ? Y : X --> X ? Y : 0 --> X & Y
2517 if (MVT::i1 == VT && N0 == N2)
2518 return DAG.getNode(ISD::AND, VT, N0, N1);
Chris Lattner729c6d12006-05-27 00:43:02 +00002519
Chris Lattner40c62d52005-10-18 06:04:22 +00002520 // If we can fold this based on the true/false value, do so.
2521 if (SimplifySelectOps(N, N1, N2))
Chris Lattner729c6d12006-05-27 00:43:02 +00002522 return SDOperand(N, 0); // Don't revisit N.
2523
Nate Begeman44728a72005-09-19 22:34:01 +00002524 // fold selects based on a setcc into other things, such as min/max/abs
2525 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman750ac1b2006-02-01 07:19:44 +00002526 // FIXME:
2527 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
2528 // having to say they don't support SELECT_CC on every type the DAG knows
2529 // about, since there is no way to mark an opcode illegal at all value types
2530 if (TLI.isOperationLegal(ISD::SELECT_CC, MVT::Other))
2531 return DAG.getNode(ISD::SELECT_CC, VT, N0.getOperand(0), N0.getOperand(1),
2532 N1, N2, N0.getOperand(2));
2533 else
2534 return SimplifySelect(N0, N1, N2);
Nate Begeman452d7be2005-09-16 00:54:12 +00002535 return SDOperand();
2536}
2537
2538SDOperand DAGCombiner::visitSELECT_CC(SDNode *N) {
Nate Begeman44728a72005-09-19 22:34:01 +00002539 SDOperand N0 = N->getOperand(0);
2540 SDOperand N1 = N->getOperand(1);
2541 SDOperand N2 = N->getOperand(2);
2542 SDOperand N3 = N->getOperand(3);
2543 SDOperand N4 = N->getOperand(4);
Nate Begeman44728a72005-09-19 22:34:01 +00002544 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
2545
Nate Begeman44728a72005-09-19 22:34:01 +00002546 // fold select_cc lhs, rhs, x, x, cc -> x
2547 if (N2 == N3)
2548 return N2;
Chris Lattner40c62d52005-10-18 06:04:22 +00002549
Chris Lattner5f42a242006-09-20 06:19:26 +00002550 // Determine if the condition we're dealing with is constant
2551 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
Chris Lattner30f73e72006-10-14 03:52:46 +00002552 if (SCC.Val) AddToWorkList(SCC.Val);
Chris Lattner5f42a242006-09-20 06:19:26 +00002553
2554 if (ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val)) {
2555 if (SCCC->getValue())
2556 return N2; // cond always true -> true val
2557 else
2558 return N3; // cond always false -> false val
2559 }
2560
2561 // Fold to a simpler select_cc
2562 if (SCC.Val && SCC.getOpcode() == ISD::SETCC)
2563 return DAG.getNode(ISD::SELECT_CC, N2.getValueType(),
2564 SCC.getOperand(0), SCC.getOperand(1), N2, N3,
2565 SCC.getOperand(2));
2566
Chris Lattner40c62d52005-10-18 06:04:22 +00002567 // If we can fold this based on the true/false value, do so.
2568 if (SimplifySelectOps(N, N2, N3))
Chris Lattner729c6d12006-05-27 00:43:02 +00002569 return SDOperand(N, 0); // Don't revisit N.
Chris Lattner40c62d52005-10-18 06:04:22 +00002570
Nate Begeman44728a72005-09-19 22:34:01 +00002571 // fold select_cc into other things, such as min/max/abs
2572 return SimplifySelectCC(N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00002573}
2574
2575SDOperand DAGCombiner::visitSETCC(SDNode *N) {
2576 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
2577 cast<CondCodeSDNode>(N->getOperand(2))->get());
2578}
2579
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002580// ExtendUsesToFormExtLoad - Trying to extend uses of a load to enable this:
2581// "fold ({s|z}ext (load x)) -> ({s|z}ext (truncate ({s|z}extload x)))"
2582// transformation. Returns true if extension are possible and the above
2583// mentioned transformation is profitable.
2584static bool ExtendUsesToFormExtLoad(SDNode *N, SDOperand N0,
2585 unsigned ExtOpc,
2586 SmallVector<SDNode*, 4> &ExtendNodes,
2587 TargetLowering &TLI) {
2588 bool HasCopyToRegUses = false;
2589 bool isTruncFree = TLI.isTruncateFree(N->getValueType(0), N0.getValueType());
2590 for (SDNode::use_iterator UI = N0.Val->use_begin(), UE = N0.Val->use_end();
2591 UI != UE; ++UI) {
2592 SDNode *User = *UI;
2593 if (User == N)
2594 continue;
2595 // FIXME: Only extend SETCC N, N and SETCC N, c for now.
2596 if (User->getOpcode() == ISD::SETCC) {
2597 ISD::CondCode CC = cast<CondCodeSDNode>(User->getOperand(2))->get();
2598 if (ExtOpc == ISD::ZERO_EXTEND && ISD::isSignedIntSetCC(CC))
2599 // Sign bits will be lost after a zext.
2600 return false;
2601 bool Add = false;
2602 for (unsigned i = 0; i != 2; ++i) {
2603 SDOperand UseOp = User->getOperand(i);
2604 if (UseOp == N0)
2605 continue;
2606 if (!isa<ConstantSDNode>(UseOp))
2607 return false;
2608 Add = true;
2609 }
2610 if (Add)
2611 ExtendNodes.push_back(User);
2612 } else {
2613 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
2614 SDOperand UseOp = User->getOperand(i);
2615 if (UseOp == N0) {
2616 // If truncate from extended type to original load type is free
2617 // on this target, then it's ok to extend a CopyToReg.
2618 if (isTruncFree && User->getOpcode() == ISD::CopyToReg)
2619 HasCopyToRegUses = true;
2620 else
2621 return false;
2622 }
2623 }
2624 }
2625 }
2626
2627 if (HasCopyToRegUses) {
2628 bool BothLiveOut = false;
2629 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
2630 UI != UE; ++UI) {
2631 SDNode *User = *UI;
2632 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
2633 SDOperand UseOp = User->getOperand(i);
2634 if (UseOp.Val == N && UseOp.ResNo == 0) {
2635 BothLiveOut = true;
2636 break;
2637 }
2638 }
2639 }
2640 if (BothLiveOut)
2641 // Both unextended and extended values are live out. There had better be
2642 // good a reason for the transformation.
2643 return ExtendNodes.size();
2644 }
2645 return true;
2646}
2647
Nate Begeman83e75ec2005-09-06 04:43:02 +00002648SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002649 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002650 MVT::ValueType VT = N->getValueType(0);
2651
Nate Begeman1d4d4142005-09-01 00:19:25 +00002652 // fold (sext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00002653 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002654 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0);
Chris Lattner310b5782006-05-06 23:06:26 +00002655
Nate Begeman1d4d4142005-09-01 00:19:25 +00002656 // fold (sext (sext x)) -> (sext x)
Chris Lattner310b5782006-05-06 23:06:26 +00002657 // fold (sext (aext x)) -> (sext x)
2658 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002659 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
Chris Lattner310b5782006-05-06 23:06:26 +00002660
Evan Chengc88138f2007-03-22 01:54:19 +00002661 // fold (sext (truncate (load x))) -> (sext (smaller load x))
2662 // fold (sext (truncate (srl (load x), c))) -> (sext (smaller load (x+c/n)))
Chris Lattner22558872007-02-26 03:13:59 +00002663 if (N0.getOpcode() == ISD::TRUNCATE) {
Evan Chengc88138f2007-03-22 01:54:19 +00002664 SDOperand NarrowLoad = ReduceLoadWidth(N0.Val);
Evan Cheng0b063de2007-03-23 02:16:52 +00002665 if (NarrowLoad.Val) {
2666 if (NarrowLoad.Val != N0.Val)
2667 CombineTo(N0.Val, NarrowLoad);
2668 return DAG.getNode(ISD::SIGN_EXTEND, VT, NarrowLoad);
2669 }
Evan Chengc88138f2007-03-22 01:54:19 +00002670 }
2671
2672 // See if the value being truncated is already sign extended. If so, just
2673 // eliminate the trunc/sext pair.
2674 if (N0.getOpcode() == ISD::TRUNCATE) {
Chris Lattner6007b842006-09-21 06:00:20 +00002675 SDOperand Op = N0.getOperand(0);
Chris Lattner22558872007-02-26 03:13:59 +00002676 unsigned OpBits = MVT::getSizeInBits(Op.getValueType());
2677 unsigned MidBits = MVT::getSizeInBits(N0.getValueType());
2678 unsigned DestBits = MVT::getSizeInBits(VT);
Dan Gohmanea859be2007-06-22 14:59:07 +00002679 unsigned NumSignBits = DAG.ComputeNumSignBits(Op);
Chris Lattner22558872007-02-26 03:13:59 +00002680
2681 if (OpBits == DestBits) {
2682 // Op is i32, Mid is i8, and Dest is i32. If Op has more than 24 sign
2683 // bits, it is already ready.
2684 if (NumSignBits > DestBits-MidBits)
2685 return Op;
2686 } else if (OpBits < DestBits) {
2687 // Op is i32, Mid is i8, and Dest is i64. If Op has more than 24 sign
2688 // bits, just sext from i32.
2689 if (NumSignBits > OpBits-MidBits)
2690 return DAG.getNode(ISD::SIGN_EXTEND, VT, Op);
2691 } else {
2692 // Op is i64, Mid is i8, and Dest is i32. If Op has more than 56 sign
2693 // bits, just truncate to i32.
2694 if (NumSignBits > OpBits-MidBits)
2695 return DAG.getNode(ISD::TRUNCATE, VT, Op);
Chris Lattner6007b842006-09-21 06:00:20 +00002696 }
Chris Lattner22558872007-02-26 03:13:59 +00002697
2698 // fold (sext (truncate x)) -> (sextinreg x).
2699 if (!AfterLegalize || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG,
2700 N0.getValueType())) {
2701 if (Op.getValueType() < VT)
2702 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
2703 else if (Op.getValueType() > VT)
2704 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
2705 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, Op,
2706 DAG.getValueType(N0.getValueType()));
2707 }
Chris Lattner6007b842006-09-21 06:00:20 +00002708 }
Chris Lattner310b5782006-05-06 23:06:26 +00002709
Evan Cheng110dec22005-12-14 02:19:23 +00002710 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002711 if (ISD::isNON_EXTLoad(N0.Val) &&
Evan Chengc5484282006-10-04 00:56:09 +00002712 (!AfterLegalize||TLI.isLoadXLegal(ISD::SEXTLOAD, N0.getValueType()))){
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002713 bool DoXform = true;
2714 SmallVector<SDNode*, 4> SetCCs;
2715 if (!N0.hasOneUse())
2716 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::SIGN_EXTEND, SetCCs, TLI);
2717 if (DoXform) {
2718 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2719 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2720 LN0->getBasePtr(), LN0->getSrcValue(),
2721 LN0->getSrcValueOffset(),
2722 N0.getValueType(),
2723 LN0->isVolatile(),
2724 LN0->getAlignment());
2725 CombineTo(N, ExtLoad);
2726 SDOperand Trunc = DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad);
2727 CombineTo(N0.Val, Trunc, ExtLoad.getValue(1));
2728 // Extend SetCC uses if necessary.
2729 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
2730 SDNode *SetCC = SetCCs[i];
2731 SmallVector<SDOperand, 4> Ops;
2732 for (unsigned j = 0; j != 2; ++j) {
2733 SDOperand SOp = SetCC->getOperand(j);
2734 if (SOp == Trunc)
2735 Ops.push_back(ExtLoad);
2736 else
2737 Ops.push_back(DAG.getNode(ISD::SIGN_EXTEND, VT, SOp));
2738 }
2739 Ops.push_back(SetCC->getOperand(2));
2740 CombineTo(SetCC, DAG.getNode(ISD::SETCC, SetCC->getValueType(0),
2741 &Ops[0], Ops.size()));
2742 }
2743 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2744 }
Nate Begeman3df4d522005-10-12 20:40:40 +00002745 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002746
2747 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
2748 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00002749 if ((ISD::isSEXTLoad(N0.Val) || ISD::isEXTLoad(N0.Val)) &&
2750 ISD::isUNINDEXEDLoad(N0.Val) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002751 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00002752 MVT::ValueType EVT = LN0->getLoadedVT();
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00002753 if (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT)) {
2754 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2755 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002756 LN0->getSrcValueOffset(), EVT,
2757 LN0->isVolatile(),
2758 LN0->getAlignment());
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00002759 CombineTo(N, ExtLoad);
2760 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2761 ExtLoad.getValue(1));
2762 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2763 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002764 }
2765
Chris Lattner20a35c32007-04-11 05:32:27 +00002766 // sext(setcc x,y,cc) -> select_cc x, y, -1, 0, cc
2767 if (N0.getOpcode() == ISD::SETCC) {
2768 SDOperand SCC =
Chris Lattner1eba01e2007-04-11 06:50:51 +00002769 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
2770 DAG.getConstant(~0ULL, VT), DAG.getConstant(0, VT),
2771 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
2772 if (SCC.Val) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00002773 }
2774
Nate Begeman83e75ec2005-09-06 04:43:02 +00002775 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002776}
2777
Nate Begeman83e75ec2005-09-06 04:43:02 +00002778SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002779 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002780 MVT::ValueType VT = N->getValueType(0);
2781
Nate Begeman1d4d4142005-09-01 00:19:25 +00002782 // fold (zext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00002783 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002784 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002785 // fold (zext (zext x)) -> (zext x)
Chris Lattner310b5782006-05-06 23:06:26 +00002786 // fold (zext (aext x)) -> (zext x)
2787 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002788 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
Chris Lattner6007b842006-09-21 06:00:20 +00002789
Evan Chengc88138f2007-03-22 01:54:19 +00002790 // fold (zext (truncate (load x))) -> (zext (smaller load x))
2791 // fold (zext (truncate (srl (load x), c))) -> (zext (small load (x+c/n)))
Dale Johannesen2041a0e2007-03-30 21:38:07 +00002792 if (N0.getOpcode() == ISD::TRUNCATE) {
Evan Chengc88138f2007-03-22 01:54:19 +00002793 SDOperand NarrowLoad = ReduceLoadWidth(N0.Val);
Evan Cheng0b063de2007-03-23 02:16:52 +00002794 if (NarrowLoad.Val) {
2795 if (NarrowLoad.Val != N0.Val)
2796 CombineTo(N0.Val, NarrowLoad);
2797 return DAG.getNode(ISD::ZERO_EXTEND, VT, NarrowLoad);
2798 }
Evan Chengc88138f2007-03-22 01:54:19 +00002799 }
2800
Chris Lattner6007b842006-09-21 06:00:20 +00002801 // fold (zext (truncate x)) -> (and x, mask)
2802 if (N0.getOpcode() == ISD::TRUNCATE &&
2803 (!AfterLegalize || TLI.isOperationLegal(ISD::AND, VT))) {
2804 SDOperand Op = N0.getOperand(0);
2805 if (Op.getValueType() < VT) {
2806 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
2807 } else if (Op.getValueType() > VT) {
2808 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
2809 }
2810 return DAG.getZeroExtendInReg(Op, N0.getValueType());
2811 }
2812
Chris Lattner111c2282006-09-21 06:14:31 +00002813 // fold (zext (and (trunc x), cst)) -> (and x, cst).
2814 if (N0.getOpcode() == ISD::AND &&
2815 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
2816 N0.getOperand(1).getOpcode() == ISD::Constant) {
2817 SDOperand X = N0.getOperand(0).getOperand(0);
2818 if (X.getValueType() < VT) {
2819 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
2820 } else if (X.getValueType() > VT) {
2821 X = DAG.getNode(ISD::TRUNCATE, VT, X);
2822 }
2823 uint64_t Mask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
2824 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
2825 }
2826
Evan Cheng110dec22005-12-14 02:19:23 +00002827 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002828 if (ISD::isNON_EXTLoad(N0.Val) &&
Evan Chengc5484282006-10-04 00:56:09 +00002829 (!AfterLegalize||TLI.isLoadXLegal(ISD::ZEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002830 bool DoXform = true;
2831 SmallVector<SDNode*, 4> SetCCs;
2832 if (!N0.hasOneUse())
2833 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ZERO_EXTEND, SetCCs, TLI);
2834 if (DoXform) {
2835 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2836 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
2837 LN0->getBasePtr(), LN0->getSrcValue(),
2838 LN0->getSrcValueOffset(),
2839 N0.getValueType(),
2840 LN0->isVolatile(),
2841 LN0->getAlignment());
2842 CombineTo(N, ExtLoad);
2843 SDOperand Trunc = DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad);
2844 CombineTo(N0.Val, Trunc, ExtLoad.getValue(1));
2845 // Extend SetCC uses if necessary.
2846 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
2847 SDNode *SetCC = SetCCs[i];
2848 SmallVector<SDOperand, 4> Ops;
2849 for (unsigned j = 0; j != 2; ++j) {
2850 SDOperand SOp = SetCC->getOperand(j);
2851 if (SOp == Trunc)
2852 Ops.push_back(ExtLoad);
2853 else
Evan Chengde1631b2007-10-30 20:11:21 +00002854 Ops.push_back(DAG.getNode(ISD::ZERO_EXTEND, VT, SOp));
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002855 }
2856 Ops.push_back(SetCC->getOperand(2));
2857 CombineTo(SetCC, DAG.getNode(ISD::SETCC, SetCC->getValueType(0),
2858 &Ops[0], Ops.size()));
2859 }
2860 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2861 }
Evan Cheng110dec22005-12-14 02:19:23 +00002862 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002863
2864 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
2865 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00002866 if ((ISD::isZEXTLoad(N0.Val) || ISD::isEXTLoad(N0.Val)) &&
2867 ISD::isUNINDEXEDLoad(N0.Val) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002868 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00002869 MVT::ValueType EVT = LN0->getLoadedVT();
Evan Cheng466685d2006-10-09 20:57:25 +00002870 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
2871 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002872 LN0->getSrcValueOffset(), EVT,
2873 LN0->isVolatile(),
2874 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00002875 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002876 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2877 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002878 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002879 }
Chris Lattner20a35c32007-04-11 05:32:27 +00002880
2881 // zext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
2882 if (N0.getOpcode() == ISD::SETCC) {
2883 SDOperand SCC =
2884 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
2885 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattner1eba01e2007-04-11 06:50:51 +00002886 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
2887 if (SCC.Val) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00002888 }
2889
Nate Begeman83e75ec2005-09-06 04:43:02 +00002890 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002891}
2892
Chris Lattner5ffc0662006-05-05 05:58:59 +00002893SDOperand DAGCombiner::visitANY_EXTEND(SDNode *N) {
2894 SDOperand N0 = N->getOperand(0);
Chris Lattner5ffc0662006-05-05 05:58:59 +00002895 MVT::ValueType VT = N->getValueType(0);
2896
2897 // fold (aext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00002898 if (isa<ConstantSDNode>(N0))
Chris Lattner5ffc0662006-05-05 05:58:59 +00002899 return DAG.getNode(ISD::ANY_EXTEND, VT, N0);
2900 // fold (aext (aext x)) -> (aext x)
2901 // fold (aext (zext x)) -> (zext x)
2902 // fold (aext (sext x)) -> (sext x)
2903 if (N0.getOpcode() == ISD::ANY_EXTEND ||
2904 N0.getOpcode() == ISD::ZERO_EXTEND ||
2905 N0.getOpcode() == ISD::SIGN_EXTEND)
2906 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
2907
Evan Chengc88138f2007-03-22 01:54:19 +00002908 // fold (aext (truncate (load x))) -> (aext (smaller load x))
2909 // fold (aext (truncate (srl (load x), c))) -> (aext (small load (x+c/n)))
2910 if (N0.getOpcode() == ISD::TRUNCATE) {
2911 SDOperand NarrowLoad = ReduceLoadWidth(N0.Val);
Evan Cheng0b063de2007-03-23 02:16:52 +00002912 if (NarrowLoad.Val) {
2913 if (NarrowLoad.Val != N0.Val)
2914 CombineTo(N0.Val, NarrowLoad);
2915 return DAG.getNode(ISD::ANY_EXTEND, VT, NarrowLoad);
2916 }
Evan Chengc88138f2007-03-22 01:54:19 +00002917 }
2918
Chris Lattner84750582006-09-20 06:29:17 +00002919 // fold (aext (truncate x))
2920 if (N0.getOpcode() == ISD::TRUNCATE) {
2921 SDOperand TruncOp = N0.getOperand(0);
2922 if (TruncOp.getValueType() == VT)
2923 return TruncOp; // x iff x size == zext size.
2924 if (TruncOp.getValueType() > VT)
2925 return DAG.getNode(ISD::TRUNCATE, VT, TruncOp);
2926 return DAG.getNode(ISD::ANY_EXTEND, VT, TruncOp);
2927 }
Chris Lattner0e4b9222006-09-21 06:40:43 +00002928
2929 // fold (aext (and (trunc x), cst)) -> (and x, cst).
2930 if (N0.getOpcode() == ISD::AND &&
2931 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
2932 N0.getOperand(1).getOpcode() == ISD::Constant) {
2933 SDOperand X = N0.getOperand(0).getOperand(0);
2934 if (X.getValueType() < VT) {
2935 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
2936 } else if (X.getValueType() > VT) {
2937 X = DAG.getNode(ISD::TRUNCATE, VT, X);
2938 }
2939 uint64_t Mask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
2940 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
2941 }
2942
Chris Lattner5ffc0662006-05-05 05:58:59 +00002943 // fold (aext (load x)) -> (aext (truncate (extload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00002944 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00002945 (!AfterLegalize||TLI.isLoadXLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00002946 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2947 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
2948 LN0->getBasePtr(), LN0->getSrcValue(),
2949 LN0->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002950 N0.getValueType(),
2951 LN0->isVolatile(),
2952 LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00002953 CombineTo(N, ExtLoad);
2954 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2955 ExtLoad.getValue(1));
2956 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2957 }
2958
2959 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
2960 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
2961 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00002962 if (N0.getOpcode() == ISD::LOAD &&
2963 !ISD::isNON_EXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val) &&
Evan Cheng466685d2006-10-09 20:57:25 +00002964 N0.hasOneUse()) {
2965 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00002966 MVT::ValueType EVT = LN0->getLoadedVT();
Evan Cheng466685d2006-10-09 20:57:25 +00002967 SDOperand ExtLoad = DAG.getExtLoad(LN0->getExtensionType(), VT,
2968 LN0->getChain(), LN0->getBasePtr(),
2969 LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002970 LN0->getSrcValueOffset(), EVT,
2971 LN0->isVolatile(),
2972 LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00002973 CombineTo(N, ExtLoad);
2974 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2975 ExtLoad.getValue(1));
2976 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2977 }
Chris Lattner20a35c32007-04-11 05:32:27 +00002978
2979 // aext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
2980 if (N0.getOpcode() == ISD::SETCC) {
2981 SDOperand SCC =
Chris Lattner1eba01e2007-04-11 06:50:51 +00002982 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
2983 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattnerc24bbad2007-04-11 16:51:53 +00002984 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Chris Lattner1eba01e2007-04-11 06:50:51 +00002985 if (SCC.Val)
Chris Lattnerc56a81d2007-04-11 06:43:25 +00002986 return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00002987 }
2988
Chris Lattner5ffc0662006-05-05 05:58:59 +00002989 return SDOperand();
2990}
2991
Chris Lattner2b4c2792007-10-13 06:35:54 +00002992/// GetDemandedBits - See if the specified operand can be simplified with the
2993/// knowledge that only the bits specified by Mask are used. If so, return the
2994/// simpler operand, otherwise return a null SDOperand.
2995SDOperand DAGCombiner::GetDemandedBits(SDOperand V, uint64_t Mask) {
2996 switch (V.getOpcode()) {
2997 default: break;
2998 case ISD::OR:
2999 case ISD::XOR:
3000 // If the LHS or RHS don't contribute bits to the or, drop them.
3001 if (DAG.MaskedValueIsZero(V.getOperand(0), Mask))
3002 return V.getOperand(1);
3003 if (DAG.MaskedValueIsZero(V.getOperand(1), Mask))
3004 return V.getOperand(0);
3005 break;
Chris Lattnere33544c2007-10-13 06:58:48 +00003006 case ISD::SRL:
3007 // Only look at single-use SRLs.
3008 if (!V.Val->hasOneUse())
3009 break;
3010 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
3011 // See if we can recursively simplify the LHS.
3012 unsigned Amt = RHSC->getValue();
3013 Mask = (Mask << Amt) & MVT::getIntVTBitMask(V.getValueType());
3014 SDOperand SimplifyLHS = GetDemandedBits(V.getOperand(0), Mask);
3015 if (SimplifyLHS.Val) {
3016 return DAG.getNode(ISD::SRL, V.getValueType(),
3017 SimplifyLHS, V.getOperand(1));
3018 }
3019 }
Chris Lattner2b4c2792007-10-13 06:35:54 +00003020 }
3021 return SDOperand();
3022}
3023
Evan Chengc88138f2007-03-22 01:54:19 +00003024/// ReduceLoadWidth - If the result of a wider load is shifted to right of N
3025/// bits and then truncated to a narrower type and where N is a multiple
3026/// of number of bits of the narrower type, transform it to a narrower load
3027/// from address + N / num of bits of new type. If the result is to be
3028/// extended, also fold the extension to form a extending load.
3029SDOperand DAGCombiner::ReduceLoadWidth(SDNode *N) {
3030 unsigned Opc = N->getOpcode();
3031 ISD::LoadExtType ExtType = ISD::NON_EXTLOAD;
3032 SDOperand N0 = N->getOperand(0);
3033 MVT::ValueType VT = N->getValueType(0);
3034 MVT::ValueType EVT = N->getValueType(0);
3035
Evan Chenge177e302007-03-23 22:13:36 +00003036 // Special case: SIGN_EXTEND_INREG is basically truncating to EVT then
3037 // extended to VT.
Evan Chengc88138f2007-03-22 01:54:19 +00003038 if (Opc == ISD::SIGN_EXTEND_INREG) {
3039 ExtType = ISD::SEXTLOAD;
3040 EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Evan Chenge177e302007-03-23 22:13:36 +00003041 if (AfterLegalize && !TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))
3042 return SDOperand();
Evan Chengc88138f2007-03-22 01:54:19 +00003043 }
3044
3045 unsigned EVTBits = MVT::getSizeInBits(EVT);
3046 unsigned ShAmt = 0;
Evan Chengb37b80c2007-03-23 20:55:21 +00003047 bool CombineSRL = false;
Evan Chengc88138f2007-03-22 01:54:19 +00003048 if (N0.getOpcode() == ISD::SRL && N0.hasOneUse()) {
3049 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
3050 ShAmt = N01->getValue();
3051 // Is the shift amount a multiple of size of VT?
3052 if ((ShAmt & (EVTBits-1)) == 0) {
3053 N0 = N0.getOperand(0);
3054 if (MVT::getSizeInBits(N0.getValueType()) <= EVTBits)
3055 return SDOperand();
Evan Chengb37b80c2007-03-23 20:55:21 +00003056 CombineSRL = true;
Evan Chengc88138f2007-03-22 01:54:19 +00003057 }
3058 }
3059 }
3060
3061 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
3062 // Do not allow folding to i1 here. i1 is implicitly stored in memory in
3063 // zero extended form: by shrinking the load, we lose track of the fact
3064 // that it is already zero extended.
3065 // FIXME: This should be reevaluated.
3066 VT != MVT::i1) {
3067 assert(MVT::getSizeInBits(N0.getValueType()) > EVTBits &&
3068 "Cannot truncate to larger type!");
3069 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3070 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
Evan Chengdae54ce2007-03-24 00:02:43 +00003071 // For big endian targets, we need to adjust the offset to the pointer to
3072 // load the correct bytes.
Duncan Sandsc6fa1702007-11-09 08:57:19 +00003073 if (!TLI.isLittleEndian()) {
3074 unsigned LVTStoreBits = MVT::getStoreSizeInBits(N0.getValueType());
3075 unsigned EVTStoreBits = MVT::getStoreSizeInBits(EVT);
3076 ShAmt = LVTStoreBits - EVTStoreBits - ShAmt;
3077 }
Evan Chengdae54ce2007-03-24 00:02:43 +00003078 uint64_t PtrOff = ShAmt / 8;
Duncan Sandsdc846502007-10-28 12:59:45 +00003079 unsigned NewAlign = MinAlign(LN0->getAlignment(), PtrOff);
Evan Chengc88138f2007-03-22 01:54:19 +00003080 SDOperand NewPtr = DAG.getNode(ISD::ADD, PtrType, LN0->getBasePtr(),
3081 DAG.getConstant(PtrOff, PtrType));
3082 AddToWorkList(NewPtr.Val);
3083 SDOperand Load = (ExtType == ISD::NON_EXTLOAD)
3084 ? DAG.getLoad(VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00003085 LN0->getSrcValue(), LN0->getSrcValueOffset(),
Duncan Sandsdc846502007-10-28 12:59:45 +00003086 LN0->isVolatile(), NewAlign)
Evan Chengc88138f2007-03-22 01:54:19 +00003087 : DAG.getExtLoad(ExtType, VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00003088 LN0->getSrcValue(), LN0->getSrcValueOffset(), EVT,
Duncan Sandsdc846502007-10-28 12:59:45 +00003089 LN0->isVolatile(), NewAlign);
Evan Chengc88138f2007-03-22 01:54:19 +00003090 AddToWorkList(N);
Evan Chengb37b80c2007-03-23 20:55:21 +00003091 if (CombineSRL) {
Chris Lattner01d029b2007-10-15 06:10:22 +00003092 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), Load.getValue(1));
Evan Chengb37b80c2007-03-23 20:55:21 +00003093 CombineTo(N->getOperand(0).Val, Load);
3094 } else
3095 CombineTo(N0.Val, Load, Load.getValue(1));
Evan Cheng15213b72007-03-26 07:12:51 +00003096 if (ShAmt) {
3097 if (Opc == ISD::SIGN_EXTEND_INREG)
3098 return DAG.getNode(Opc, VT, Load, N->getOperand(1));
3099 else
3100 return DAG.getNode(Opc, VT, Load);
3101 }
Evan Chengc88138f2007-03-22 01:54:19 +00003102 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
3103 }
3104
3105 return SDOperand();
3106}
3107
Chris Lattner5ffc0662006-05-05 05:58:59 +00003108
Nate Begeman83e75ec2005-09-06 04:43:02 +00003109SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003110 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003111 SDOperand N1 = N->getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003112 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003113 MVT::ValueType EVT = cast<VTSDNode>(N1)->getVT();
Nate Begeman07ed4172005-10-10 21:26:48 +00003114 unsigned EVTBits = MVT::getSizeInBits(EVT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003115
Nate Begeman1d4d4142005-09-01 00:19:25 +00003116 // fold (sext_in_reg c1) -> c1
Chris Lattnereaeda562006-05-08 20:59:41 +00003117 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
Chris Lattner310b5782006-05-06 23:06:26 +00003118 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0, N1);
Chris Lattneree4ea922006-05-06 09:30:03 +00003119
Chris Lattner541a24f2006-05-06 22:43:44 +00003120 // If the input is already sign extended, just drop the extension.
Dan Gohmanea859be2007-06-22 14:59:07 +00003121 if (DAG.ComputeNumSignBits(N0) >= MVT::getSizeInBits(VT)-EVTBits+1)
Chris Lattneree4ea922006-05-06 09:30:03 +00003122 return N0;
3123
Nate Begeman646d7e22005-09-02 21:18:40 +00003124 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
3125 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
3126 EVT < cast<VTSDNode>(N0.getOperand(1))->getVT()) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00003127 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00003128 }
Chris Lattner4b37e872006-05-08 21:18:59 +00003129
Chris Lattner95a5e052007-04-17 19:03:21 +00003130 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is known zero.
Dan Gohmanea859be2007-06-22 14:59:07 +00003131 if (DAG.MaskedValueIsZero(N0, 1ULL << (EVTBits-1)))
Nate Begemande996292006-02-03 22:24:05 +00003132 return DAG.getZeroExtendInReg(N0, EVT);
Chris Lattner4b37e872006-05-08 21:18:59 +00003133
Chris Lattner95a5e052007-04-17 19:03:21 +00003134 // fold operands of sext_in_reg based on knowledge that the top bits are not
3135 // demanded.
3136 if (SimplifyDemandedBits(SDOperand(N, 0)))
3137 return SDOperand(N, 0);
3138
Evan Chengc88138f2007-03-22 01:54:19 +00003139 // fold (sext_in_reg (load x)) -> (smaller sextload x)
3140 // fold (sext_in_reg (srl (load x), c)) -> (smaller sextload (x+c/evtbits))
3141 SDOperand NarrowLoad = ReduceLoadWidth(N);
3142 if (NarrowLoad.Val)
3143 return NarrowLoad;
3144
Chris Lattner4b37e872006-05-08 21:18:59 +00003145 // fold (sext_in_reg (srl X, 24), i8) -> sra X, 24
3146 // fold (sext_in_reg (srl X, 23), i8) -> sra X, 23 iff possible.
3147 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
3148 if (N0.getOpcode() == ISD::SRL) {
3149 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
3150 if (ShAmt->getValue()+EVTBits <= MVT::getSizeInBits(VT)) {
3151 // We can turn this into an SRA iff the input to the SRL is already sign
3152 // extended enough.
Dan Gohmanea859be2007-06-22 14:59:07 +00003153 unsigned InSignBits = DAG.ComputeNumSignBits(N0.getOperand(0));
Chris Lattner4b37e872006-05-08 21:18:59 +00003154 if (MVT::getSizeInBits(VT)-(ShAmt->getValue()+EVTBits) < InSignBits)
3155 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0), N0.getOperand(1));
3156 }
3157 }
Evan Chengc88138f2007-03-22 01:54:19 +00003158
Nate Begemanded49632005-10-13 03:11:28 +00003159 // fold (sext_inreg (extload x)) -> (sextload x)
Evan Chengc5484282006-10-04 00:56:09 +00003160 if (ISD::isEXTLoad(N0.Val) &&
Evan Cheng83060c52007-03-07 08:07:03 +00003161 ISD::isUNINDEXEDLoad(N0.Val) &&
Evan Cheng2e49f092006-10-11 07:10:22 +00003162 EVT == cast<LoadSDNode>(N0)->getLoadedVT() &&
Evan Chengc5484282006-10-04 00:56:09 +00003163 (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003164 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3165 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
3166 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003167 LN0->getSrcValueOffset(), EVT,
3168 LN0->isVolatile(),
3169 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00003170 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00003171 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00003172 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00003173 }
3174 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Evan Cheng83060c52007-03-07 08:07:03 +00003175 if (ISD::isZEXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val) &&
3176 N0.hasOneUse() &&
Evan Cheng2e49f092006-10-11 07:10:22 +00003177 EVT == cast<LoadSDNode>(N0)->getLoadedVT() &&
Evan Chengc5484282006-10-04 00:56:09 +00003178 (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003179 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3180 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
3181 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003182 LN0->getSrcValueOffset(), EVT,
3183 LN0->isVolatile(),
3184 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00003185 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00003186 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00003187 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00003188 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00003189 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003190}
3191
Nate Begeman83e75ec2005-09-06 04:43:02 +00003192SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003193 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003194 MVT::ValueType VT = N->getValueType(0);
3195
3196 // noop truncate
3197 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00003198 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003199 // fold (truncate c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00003200 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00003201 return DAG.getNode(ISD::TRUNCATE, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003202 // fold (truncate (truncate x)) -> (truncate x)
3203 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00003204 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003205 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
Chris Lattnerb72773b2006-05-05 22:56:26 +00003206 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND||
3207 N0.getOpcode() == ISD::ANY_EXTEND) {
Chris Lattner32ba1aa2006-11-20 18:05:46 +00003208 if (N0.getOperand(0).getValueType() < VT)
Nate Begeman1d4d4142005-09-01 00:19:25 +00003209 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00003210 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Chris Lattner32ba1aa2006-11-20 18:05:46 +00003211 else if (N0.getOperand(0).getValueType() > VT)
Nate Begeman1d4d4142005-09-01 00:19:25 +00003212 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00003213 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003214 else
3215 // if the source and dest are the same type, we can drop both the extend
3216 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00003217 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003218 }
Evan Cheng007b69e2007-03-21 20:14:05 +00003219
Chris Lattner2b4c2792007-10-13 06:35:54 +00003220 // See if we can simplify the input to this truncate through knowledge that
3221 // only the low bits are being used. For example "trunc (or (shl x, 8), y)"
3222 // -> trunc y
3223 SDOperand Shorter = GetDemandedBits(N0, MVT::getIntVTBitMask(VT));
3224 if (Shorter.Val)
3225 return DAG.getNode(ISD::TRUNCATE, VT, Shorter);
3226
Nate Begeman3df4d522005-10-12 20:40:40 +00003227 // fold (truncate (load x)) -> (smaller load x)
Evan Cheng007b69e2007-03-21 20:14:05 +00003228 // fold (truncate (srl (load x), c)) -> (smaller load (x+c/evtbits))
Evan Chengc88138f2007-03-22 01:54:19 +00003229 return ReduceLoadWidth(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003230}
3231
Chris Lattner94683772005-12-23 05:30:37 +00003232SDOperand DAGCombiner::visitBIT_CONVERT(SDNode *N) {
3233 SDOperand N0 = N->getOperand(0);
3234 MVT::ValueType VT = N->getValueType(0);
3235
Dan Gohman7f321562007-06-25 16:23:39 +00003236 // If the input is a BUILD_VECTOR with all constant elements, fold this now.
3237 // Only do this before legalize, since afterward the target may be depending
3238 // on the bitconvert.
3239 // First check to see if this is all constant.
3240 if (!AfterLegalize &&
3241 N0.getOpcode() == ISD::BUILD_VECTOR && N0.Val->hasOneUse() &&
3242 MVT::isVector(VT)) {
3243 bool isSimple = true;
3244 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i)
3245 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
3246 N0.getOperand(i).getOpcode() != ISD::Constant &&
3247 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
3248 isSimple = false;
3249 break;
3250 }
3251
3252 MVT::ValueType DestEltVT = MVT::getVectorElementType(N->getValueType(0));
3253 assert(!MVT::isVector(DestEltVT) &&
3254 "Element type of vector ValueType must not be vector!");
3255 if (isSimple) {
3256 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(N0.Val, DestEltVT);
3257 }
3258 }
3259
Chris Lattner94683772005-12-23 05:30:37 +00003260 // If the input is a constant, let getNode() fold it.
3261 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
3262 SDOperand Res = DAG.getNode(ISD::BIT_CONVERT, VT, N0);
3263 if (Res.Val != N) return Res;
3264 }
3265
Chris Lattnerc8547d82005-12-23 05:37:50 +00003266 if (N0.getOpcode() == ISD::BIT_CONVERT) // conv(conv(x,t1),t2) -> conv(x,t2)
3267 return DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
Chris Lattner6258fb22006-04-02 02:53:43 +00003268
Chris Lattner57104102005-12-23 05:44:41 +00003269 // fold (conv (load x)) -> (load (conv*)x)
Evan Cheng513da432007-10-06 08:19:55 +00003270 // If the resultant load doesn't need a higher alignment than the original!
3271 if (ISD::isNormalLoad(N0.Val) && N0.hasOneUse() &&
Evan Cheng59d5b682007-05-07 21:27:48 +00003272 TLI.isOperationLegal(ISD::LOAD, VT)) {
Evan Cheng466685d2006-10-09 20:57:25 +00003273 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng59d5b682007-05-07 21:27:48 +00003274 unsigned Align = TLI.getTargetMachine().getTargetData()->
Dan Gohmanfcc4dd92007-05-18 18:41:29 +00003275 getABITypeAlignment(MVT::getTypeForValueType(VT));
Evan Cheng59d5b682007-05-07 21:27:48 +00003276 unsigned OrigAlign = LN0->getAlignment();
3277 if (Align <= OrigAlign) {
3278 SDOperand Load = DAG.getLoad(VT, LN0->getChain(), LN0->getBasePtr(),
3279 LN0->getSrcValue(), LN0->getSrcValueOffset(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00003280 LN0->isVolatile(), Align);
Evan Cheng59d5b682007-05-07 21:27:48 +00003281 AddToWorkList(N);
3282 CombineTo(N0.Val, DAG.getNode(ISD::BIT_CONVERT, N0.getValueType(), Load),
3283 Load.getValue(1));
3284 return Load;
3285 }
Chris Lattner57104102005-12-23 05:44:41 +00003286 }
3287
Chris Lattner94683772005-12-23 05:30:37 +00003288 return SDOperand();
3289}
3290
Dan Gohman7f321562007-06-25 16:23:39 +00003291/// ConstantFoldBIT_CONVERTofBUILD_VECTOR - We know that BV is a build_vector
Chris Lattner6258fb22006-04-02 02:53:43 +00003292/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
3293/// destination element value type.
3294SDOperand DAGCombiner::
Dan Gohman7f321562007-06-25 16:23:39 +00003295ConstantFoldBIT_CONVERTofBUILD_VECTOR(SDNode *BV, MVT::ValueType DstEltVT) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003296 MVT::ValueType SrcEltVT = BV->getOperand(0).getValueType();
3297
3298 // If this is already the right type, we're done.
3299 if (SrcEltVT == DstEltVT) return SDOperand(BV, 0);
3300
3301 unsigned SrcBitSize = MVT::getSizeInBits(SrcEltVT);
3302 unsigned DstBitSize = MVT::getSizeInBits(DstEltVT);
3303
3304 // If this is a conversion of N elements of one type to N elements of another
3305 // type, convert each element. This handles FP<->INT cases.
3306 if (SrcBitSize == DstBitSize) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003307 SmallVector<SDOperand, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003308 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003309 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, DstEltVT, BV->getOperand(i)));
Chris Lattner3e104b12006-04-08 04:15:24 +00003310 AddToWorkList(Ops.back().Val);
3311 }
Dan Gohman7f321562007-06-25 16:23:39 +00003312 MVT::ValueType VT =
3313 MVT::getVectorType(DstEltVT,
3314 MVT::getVectorNumElements(BV->getValueType(0)));
3315 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003316 }
3317
3318 // Otherwise, we're growing or shrinking the elements. To avoid having to
3319 // handle annoying details of growing/shrinking FP values, we convert them to
3320 // int first.
3321 if (MVT::isFloatingPoint(SrcEltVT)) {
3322 // Convert the input float vector to a int vector where the elements are the
3323 // same sizes.
3324 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
3325 MVT::ValueType IntVT = SrcEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
Dan Gohman7f321562007-06-25 16:23:39 +00003326 BV = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, IntVT).Val;
Chris Lattner6258fb22006-04-02 02:53:43 +00003327 SrcEltVT = IntVT;
3328 }
3329
3330 // Now we know the input is an integer vector. If the output is a FP type,
3331 // convert to integer first, then to FP of the right size.
3332 if (MVT::isFloatingPoint(DstEltVT)) {
3333 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
3334 MVT::ValueType TmpVT = DstEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
Dan Gohman7f321562007-06-25 16:23:39 +00003335 SDNode *Tmp = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, TmpVT).Val;
Chris Lattner6258fb22006-04-02 02:53:43 +00003336
3337 // Next, convert to FP elements of the same size.
Dan Gohman7f321562007-06-25 16:23:39 +00003338 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(Tmp, DstEltVT);
Chris Lattner6258fb22006-04-02 02:53:43 +00003339 }
3340
3341 // Okay, we know the src/dst types are both integers of differing types.
3342 // Handling growing first.
3343 assert(MVT::isInteger(SrcEltVT) && MVT::isInteger(DstEltVT));
3344 if (SrcBitSize < DstBitSize) {
3345 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
3346
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003347 SmallVector<SDOperand, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003348 for (unsigned i = 0, e = BV->getNumOperands(); i != e;
Chris Lattner6258fb22006-04-02 02:53:43 +00003349 i += NumInputsPerOutput) {
3350 bool isLE = TLI.isLittleEndian();
3351 uint64_t NewBits = 0;
3352 bool EltIsUndef = true;
3353 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
3354 // Shift the previously computed bits over.
3355 NewBits <<= SrcBitSize;
3356 SDOperand Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
3357 if (Op.getOpcode() == ISD::UNDEF) continue;
3358 EltIsUndef = false;
3359
3360 NewBits |= cast<ConstantSDNode>(Op)->getValue();
3361 }
3362
3363 if (EltIsUndef)
3364 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
3365 else
3366 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
3367 }
3368
Dan Gohman7f321562007-06-25 16:23:39 +00003369 MVT::ValueType VT = MVT::getVectorType(DstEltVT,
3370 Ops.size());
3371 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003372 }
3373
3374 // Finally, this must be the case where we are shrinking elements: each input
3375 // turns into multiple outputs.
3376 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003377 SmallVector<SDOperand, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003378 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003379 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
3380 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
3381 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
3382 continue;
3383 }
3384 uint64_t OpVal = cast<ConstantSDNode>(BV->getOperand(i))->getValue();
3385
3386 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
3387 unsigned ThisVal = OpVal & ((1ULL << DstBitSize)-1);
3388 OpVal >>= DstBitSize;
3389 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
3390 }
3391
3392 // For big endian targets, swap the order of the pieces of each element.
3393 if (!TLI.isLittleEndian())
3394 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
3395 }
Dan Gohman7f321562007-06-25 16:23:39 +00003396 MVT::ValueType VT = MVT::getVectorType(DstEltVT, Ops.size());
3397 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003398}
3399
3400
3401
Chris Lattner01b3d732005-09-28 22:28:18 +00003402SDOperand DAGCombiner::visitFADD(SDNode *N) {
3403 SDOperand N0 = N->getOperand(0);
3404 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003405 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3406 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00003407 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00003408
Dan Gohman7f321562007-06-25 16:23:39 +00003409 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00003410 if (MVT::isVector(VT)) {
3411 SDOperand FoldedVOp = SimplifyVBinOp(N);
3412 if (FoldedVOp.Val) return FoldedVOp;
3413 }
Dan Gohman7f321562007-06-25 16:23:39 +00003414
Nate Begemana0e221d2005-10-18 00:28:13 +00003415 // fold (fadd c1, c2) -> c1+c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003416 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003417 return DAG.getNode(ISD::FADD, VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003418 // canonicalize constant to RHS
3419 if (N0CFP && !N1CFP)
3420 return DAG.getNode(ISD::FADD, VT, N1, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00003421 // fold (A + (-B)) -> A-B
Chris Lattner29446522007-05-14 22:04:50 +00003422 if (isNegatibleForFree(N1) == 2)
3423 return DAG.getNode(ISD::FSUB, VT, N0, GetNegatedExpression(N1, DAG));
Chris Lattner01b3d732005-09-28 22:28:18 +00003424 // fold ((-A) + B) -> B-A
Chris Lattner29446522007-05-14 22:04:50 +00003425 if (isNegatibleForFree(N0) == 2)
3426 return DAG.getNode(ISD::FSUB, VT, N1, GetNegatedExpression(N0, DAG));
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003427
3428 // If allowed, fold (fadd (fadd x, c1), c2) -> (fadd x, (fadd c1, c2))
3429 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FADD &&
3430 N0.Val->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
3431 return DAG.getNode(ISD::FADD, VT, N0.getOperand(0),
3432 DAG.getNode(ISD::FADD, VT, N0.getOperand(1), N1));
3433
Chris Lattner01b3d732005-09-28 22:28:18 +00003434 return SDOperand();
3435}
3436
3437SDOperand DAGCombiner::visitFSUB(SDNode *N) {
3438 SDOperand N0 = N->getOperand(0);
3439 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003440 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3441 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00003442 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00003443
Dan Gohman7f321562007-06-25 16:23:39 +00003444 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00003445 if (MVT::isVector(VT)) {
3446 SDOperand FoldedVOp = SimplifyVBinOp(N);
3447 if (FoldedVOp.Val) return FoldedVOp;
3448 }
Dan Gohman7f321562007-06-25 16:23:39 +00003449
Nate Begemana0e221d2005-10-18 00:28:13 +00003450 // fold (fsub c1, c2) -> c1-c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003451 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003452 return DAG.getNode(ISD::FSUB, VT, N0, N1);
Dan Gohman23ff1822007-07-02 15:48:56 +00003453 // fold (0-B) -> -B
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003454 if (UnsafeFPMath && N0CFP && N0CFP->getValueAPF().isZero()) {
Dan Gohman23ff1822007-07-02 15:48:56 +00003455 if (isNegatibleForFree(N1))
3456 return GetNegatedExpression(N1, DAG);
3457 return DAG.getNode(ISD::FNEG, VT, N1);
3458 }
Chris Lattner01b3d732005-09-28 22:28:18 +00003459 // fold (A-(-B)) -> A+B
Chris Lattner29446522007-05-14 22:04:50 +00003460 if (isNegatibleForFree(N1))
3461 return DAG.getNode(ISD::FADD, VT, N0, GetNegatedExpression(N1, DAG));
3462
Chris Lattner01b3d732005-09-28 22:28:18 +00003463 return SDOperand();
3464}
3465
3466SDOperand DAGCombiner::visitFMUL(SDNode *N) {
3467 SDOperand N0 = N->getOperand(0);
3468 SDOperand N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003469 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3470 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00003471 MVT::ValueType VT = N->getValueType(0);
3472
Dan Gohman7f321562007-06-25 16:23:39 +00003473 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00003474 if (MVT::isVector(VT)) {
3475 SDOperand FoldedVOp = SimplifyVBinOp(N);
3476 if (FoldedVOp.Val) return FoldedVOp;
3477 }
Dan Gohman7f321562007-06-25 16:23:39 +00003478
Nate Begeman11af4ea2005-10-17 20:40:11 +00003479 // fold (fmul c1, c2) -> c1*c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003480 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003481 return DAG.getNode(ISD::FMUL, VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003482 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00003483 if (N0CFP && !N1CFP)
3484 return DAG.getNode(ISD::FMUL, VT, N1, N0);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003485 // fold (fmul X, 2.0) -> (fadd X, X)
3486 if (N1CFP && N1CFP->isExactlyValue(+2.0))
3487 return DAG.getNode(ISD::FADD, VT, N0, N0);
Chris Lattner29446522007-05-14 22:04:50 +00003488 // fold (fmul X, -1.0) -> (fneg X)
3489 if (N1CFP && N1CFP->isExactlyValue(-1.0))
3490 return DAG.getNode(ISD::FNEG, VT, N0);
3491
3492 // -X * -Y -> X*Y
3493 if (char LHSNeg = isNegatibleForFree(N0)) {
3494 if (char RHSNeg = isNegatibleForFree(N1)) {
3495 // Both can be negated for free, check to see if at least one is cheaper
3496 // negated.
3497 if (LHSNeg == 2 || RHSNeg == 2)
3498 return DAG.getNode(ISD::FMUL, VT, GetNegatedExpression(N0, DAG),
3499 GetNegatedExpression(N1, DAG));
3500 }
3501 }
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003502
3503 // If allowed, fold (fmul (fmul x, c1), c2) -> (fmul x, (fmul c1, c2))
3504 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FMUL &&
3505 N0.Val->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
3506 return DAG.getNode(ISD::FMUL, VT, N0.getOperand(0),
3507 DAG.getNode(ISD::FMUL, VT, N0.getOperand(1), N1));
3508
Chris Lattner01b3d732005-09-28 22:28:18 +00003509 return SDOperand();
3510}
3511
3512SDOperand DAGCombiner::visitFDIV(SDNode *N) {
3513 SDOperand N0 = N->getOperand(0);
3514 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003515 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3516 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00003517 MVT::ValueType VT = N->getValueType(0);
3518
Dan Gohman7f321562007-06-25 16:23:39 +00003519 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00003520 if (MVT::isVector(VT)) {
3521 SDOperand FoldedVOp = SimplifyVBinOp(N);
3522 if (FoldedVOp.Val) return FoldedVOp;
3523 }
Dan Gohman7f321562007-06-25 16:23:39 +00003524
Nate Begemana148d982006-01-18 22:35:16 +00003525 // fold (fdiv c1, c2) -> c1/c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003526 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003527 return DAG.getNode(ISD::FDIV, VT, N0, N1);
Chris Lattner29446522007-05-14 22:04:50 +00003528
3529
3530 // -X / -Y -> X*Y
3531 if (char LHSNeg = isNegatibleForFree(N0)) {
3532 if (char RHSNeg = isNegatibleForFree(N1)) {
3533 // Both can be negated for free, check to see if at least one is cheaper
3534 // negated.
3535 if (LHSNeg == 2 || RHSNeg == 2)
3536 return DAG.getNode(ISD::FDIV, VT, GetNegatedExpression(N0, DAG),
3537 GetNegatedExpression(N1, DAG));
3538 }
3539 }
3540
Chris Lattner01b3d732005-09-28 22:28:18 +00003541 return SDOperand();
3542}
3543
3544SDOperand DAGCombiner::visitFREM(SDNode *N) {
3545 SDOperand N0 = N->getOperand(0);
3546 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003547 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3548 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00003549 MVT::ValueType VT = N->getValueType(0);
3550
Nate Begemana148d982006-01-18 22:35:16 +00003551 // fold (frem c1, c2) -> fmod(c1,c2)
Dale Johannesendb44bf82007-10-16 23:38:29 +00003552 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003553 return DAG.getNode(ISD::FREM, VT, N0, N1);
Dan Gohman7f321562007-06-25 16:23:39 +00003554
Chris Lattner01b3d732005-09-28 22:28:18 +00003555 return SDOperand();
3556}
3557
Chris Lattner12d83032006-03-05 05:30:57 +00003558SDOperand DAGCombiner::visitFCOPYSIGN(SDNode *N) {
3559 SDOperand N0 = N->getOperand(0);
3560 SDOperand N1 = N->getOperand(1);
3561 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3562 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
3563 MVT::ValueType VT = N->getValueType(0);
3564
Dale Johannesendb44bf82007-10-16 23:38:29 +00003565 if (N0CFP && N1CFP && VT != MVT::ppcf128) // Constant fold
Chris Lattner12d83032006-03-05 05:30:57 +00003566 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1);
3567
3568 if (N1CFP) {
Dale Johannesene6c17422007-08-26 01:18:27 +00003569 const APFloat& V = N1CFP->getValueAPF();
Chris Lattner12d83032006-03-05 05:30:57 +00003570 // copysign(x, c1) -> fabs(x) iff ispos(c1)
3571 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
Dale Johannesen87503a62007-08-25 22:10:57 +00003572 if (!V.isNegative())
Chris Lattner12d83032006-03-05 05:30:57 +00003573 return DAG.getNode(ISD::FABS, VT, N0);
3574 else
3575 return DAG.getNode(ISD::FNEG, VT, DAG.getNode(ISD::FABS, VT, N0));
3576 }
3577
3578 // copysign(fabs(x), y) -> copysign(x, y)
3579 // copysign(fneg(x), y) -> copysign(x, y)
3580 // copysign(copysign(x,z), y) -> copysign(x, y)
3581 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
3582 N0.getOpcode() == ISD::FCOPYSIGN)
3583 return DAG.getNode(ISD::FCOPYSIGN, VT, N0.getOperand(0), N1);
3584
3585 // copysign(x, abs(y)) -> abs(x)
3586 if (N1.getOpcode() == ISD::FABS)
3587 return DAG.getNode(ISD::FABS, VT, N0);
3588
3589 // copysign(x, copysign(y,z)) -> copysign(x, z)
3590 if (N1.getOpcode() == ISD::FCOPYSIGN)
3591 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(1));
3592
3593 // copysign(x, fp_extend(y)) -> copysign(x, y)
3594 // copysign(x, fp_round(y)) -> copysign(x, y)
3595 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
3596 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(0));
3597
3598 return SDOperand();
3599}
3600
3601
Chris Lattner01b3d732005-09-28 22:28:18 +00003602
Nate Begeman83e75ec2005-09-06 04:43:02 +00003603SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003604 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003605 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00003606 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003607
3608 // fold (sint_to_fp c1) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00003609 if (N0C && N0.getValueType() != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003610 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003611 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003612}
3613
Nate Begeman83e75ec2005-09-06 04:43:02 +00003614SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003615 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003616 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00003617 MVT::ValueType VT = N->getValueType(0);
3618
Nate Begeman1d4d4142005-09-01 00:19:25 +00003619 // fold (uint_to_fp c1) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00003620 if (N0C && N0.getValueType() != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003621 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003622 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003623}
3624
Nate Begeman83e75ec2005-09-06 04:43:02 +00003625SDOperand DAGCombiner::visitFP_TO_SINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003626 SDOperand N0 = N->getOperand(0);
3627 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3628 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003629
3630 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00003631 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00003632 return DAG.getNode(ISD::FP_TO_SINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003633 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003634}
3635
Nate Begeman83e75ec2005-09-06 04:43:02 +00003636SDOperand DAGCombiner::visitFP_TO_UINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003637 SDOperand N0 = N->getOperand(0);
3638 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3639 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003640
3641 // fold (fp_to_uint c1fp) -> c1
Dale Johannesendb44bf82007-10-16 23:38:29 +00003642 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003643 return DAG.getNode(ISD::FP_TO_UINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003644 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003645}
3646
Nate Begeman83e75ec2005-09-06 04:43:02 +00003647SDOperand DAGCombiner::visitFP_ROUND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003648 SDOperand N0 = N->getOperand(0);
Chris Lattner0bd48932008-01-17 07:00:52 +00003649 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003650 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3651 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003652
3653 // fold (fp_round c1fp) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00003654 if (N0CFP && N0.getValueType() != MVT::ppcf128)
Chris Lattner0bd48932008-01-17 07:00:52 +00003655 return DAG.getNode(ISD::FP_ROUND, VT, N0, N1);
Chris Lattner79dbea52006-03-13 06:26:26 +00003656
3657 // fold (fp_round (fp_extend x)) -> x
3658 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
3659 return N0.getOperand(0);
3660
Chris Lattner0aa5e6f2008-01-24 06:45:35 +00003661 // fold (fp_round (fp_round x)) -> (fp_round x)
3662 if (N0.getOpcode() == ISD::FP_ROUND) {
3663 // This is a value preserving truncation if both round's are.
3664 bool IsTrunc = N->getConstantOperandVal(1) == 1 &&
3665 N0.Val->getConstantOperandVal(1) == 1;
3666 return DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0),
3667 DAG.getIntPtrConstant(IsTrunc));
3668 }
3669
Chris Lattner79dbea52006-03-13 06:26:26 +00003670 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
3671 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.Val->hasOneUse()) {
Chris Lattner0bd48932008-01-17 07:00:52 +00003672 SDOperand Tmp = DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0), N1);
Chris Lattner79dbea52006-03-13 06:26:26 +00003673 AddToWorkList(Tmp.Val);
3674 return DAG.getNode(ISD::FCOPYSIGN, VT, Tmp, N0.getOperand(1));
3675 }
3676
Nate Begeman83e75ec2005-09-06 04:43:02 +00003677 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003678}
3679
Nate Begeman83e75ec2005-09-06 04:43:02 +00003680SDOperand DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003681 SDOperand N0 = N->getOperand(0);
3682 MVT::ValueType VT = N->getValueType(0);
3683 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00003684 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003685
Nate Begeman1d4d4142005-09-01 00:19:25 +00003686 // fold (fp_round_inreg c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00003687 if (N0CFP) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003688 SDOperand Round = DAG.getConstantFP(N0CFP->getValueAPF(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003689 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003690 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00003691 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003692}
3693
Nate Begeman83e75ec2005-09-06 04:43:02 +00003694SDOperand DAGCombiner::visitFP_EXTEND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003695 SDOperand N0 = N->getOperand(0);
3696 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3697 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003698
Chris Lattner5938bef2007-12-29 06:55:23 +00003699 // If this is fp_round(fpextend), don't fold it, allow ourselves to be folded.
3700 if (N->hasOneUse() && (*N->use_begin())->getOpcode() == ISD::FP_ROUND)
3701 return SDOperand();
Chris Lattner0bd48932008-01-17 07:00:52 +00003702
Nate Begeman1d4d4142005-09-01 00:19:25 +00003703 // fold (fp_extend c1fp) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00003704 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003705 return DAG.getNode(ISD::FP_EXTEND, VT, N0);
Chris Lattner0bd48932008-01-17 07:00:52 +00003706
3707 // Turn fp_extend(fp_round(X, 1)) -> x since the fp_round doesn't affect the
3708 // value of X.
3709 if (N0.getOpcode() == ISD::FP_ROUND && N0.Val->getConstantOperandVal(1) == 1){
3710 SDOperand In = N0.getOperand(0);
3711 if (In.getValueType() == VT) return In;
3712 if (VT < In.getValueType())
3713 return DAG.getNode(ISD::FP_ROUND, VT, In, N0.getOperand(1));
3714 return DAG.getNode(ISD::FP_EXTEND, VT, In);
3715 }
3716
3717 // fold (fpext (load x)) -> (fpext (fptrunc (extload x)))
Dale Johannesenb6210fc2007-10-19 20:29:00 +00003718 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00003719 (!AfterLegalize||TLI.isLoadXLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003720 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3721 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
3722 LN0->getBasePtr(), LN0->getSrcValue(),
3723 LN0->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003724 N0.getValueType(),
3725 LN0->isVolatile(),
3726 LN0->getAlignment());
Chris Lattnere564dbb2006-05-05 21:34:35 +00003727 CombineTo(N, ExtLoad);
Chris Lattner0bd48932008-01-17 07:00:52 +00003728 CombineTo(N0.Val, DAG.getNode(ISD::FP_ROUND, N0.getValueType(), ExtLoad,
3729 DAG.getIntPtrConstant(1)),
Chris Lattnere564dbb2006-05-05 21:34:35 +00003730 ExtLoad.getValue(1));
3731 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
3732 }
3733
3734
Nate Begeman83e75ec2005-09-06 04:43:02 +00003735 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003736}
3737
Nate Begeman83e75ec2005-09-06 04:43:02 +00003738SDOperand DAGCombiner::visitFNEG(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003739 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00003740
Dan Gohman23ff1822007-07-02 15:48:56 +00003741 if (isNegatibleForFree(N0))
3742 return GetNegatedExpression(N0, DAG);
3743
Nate Begeman83e75ec2005-09-06 04:43:02 +00003744 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003745}
3746
Nate Begeman83e75ec2005-09-06 04:43:02 +00003747SDOperand DAGCombiner::visitFABS(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003748 SDOperand N0 = N->getOperand(0);
3749 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3750 MVT::ValueType VT = N->getValueType(0);
3751
Nate Begeman1d4d4142005-09-01 00:19:25 +00003752 // fold (fabs c1) -> fabs(c1)
Dale Johannesendb44bf82007-10-16 23:38:29 +00003753 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003754 return DAG.getNode(ISD::FABS, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003755 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00003756 if (N0.getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00003757 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003758 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00003759 // fold (fabs (fcopysign x, y)) -> (fabs x)
3760 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
3761 return DAG.getNode(ISD::FABS, VT, N0.getOperand(0));
3762
Nate Begeman83e75ec2005-09-06 04:43:02 +00003763 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003764}
3765
Nate Begeman44728a72005-09-19 22:34:01 +00003766SDOperand DAGCombiner::visitBRCOND(SDNode *N) {
3767 SDOperand Chain = N->getOperand(0);
3768 SDOperand N1 = N->getOperand(1);
3769 SDOperand N2 = N->getOperand(2);
3770 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
3771
3772 // never taken branch, fold to chain
3773 if (N1C && N1C->isNullValue())
3774 return Chain;
3775 // unconditional branch
Nate Begemane17daeb2005-10-05 21:43:42 +00003776 if (N1C && N1C->getValue() == 1)
Nate Begeman44728a72005-09-19 22:34:01 +00003777 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
Nate Begeman750ac1b2006-02-01 07:19:44 +00003778 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
3779 // on the target.
3780 if (N1.getOpcode() == ISD::SETCC &&
3781 TLI.isOperationLegal(ISD::BR_CC, MVT::Other)) {
3782 return DAG.getNode(ISD::BR_CC, MVT::Other, Chain, N1.getOperand(2),
3783 N1.getOperand(0), N1.getOperand(1), N2);
3784 }
Nate Begeman44728a72005-09-19 22:34:01 +00003785 return SDOperand();
3786}
3787
Chris Lattner3ea0b472005-10-05 06:47:48 +00003788// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
3789//
Nate Begeman44728a72005-09-19 22:34:01 +00003790SDOperand DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00003791 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
3792 SDOperand CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
3793
3794 // Use SimplifySetCC to simplify SETCC's.
Nate Begemane17daeb2005-10-05 21:43:42 +00003795 SDOperand Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false);
Chris Lattner30f73e72006-10-14 03:52:46 +00003796 if (Simp.Val) AddToWorkList(Simp.Val);
3797
Nate Begemane17daeb2005-10-05 21:43:42 +00003798 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.Val);
3799
3800 // fold br_cc true, dest -> br dest (unconditional branch)
3801 if (SCCC && SCCC->getValue())
3802 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
3803 N->getOperand(4));
3804 // fold br_cc false, dest -> unconditional fall through
3805 if (SCCC && SCCC->isNullValue())
3806 return N->getOperand(0);
Chris Lattner30f73e72006-10-14 03:52:46 +00003807
Nate Begemane17daeb2005-10-05 21:43:42 +00003808 // fold to a simpler setcc
3809 if (Simp.Val && Simp.getOpcode() == ISD::SETCC)
3810 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
3811 Simp.getOperand(2), Simp.getOperand(0),
3812 Simp.getOperand(1), N->getOperand(4));
Nate Begeman44728a72005-09-19 22:34:01 +00003813 return SDOperand();
3814}
3815
Chris Lattner448f2192006-11-11 00:39:41 +00003816
3817/// CombineToPreIndexedLoadStore - Try turning a load / store and a
3818/// pre-indexed load / store when the base pointer is a add or subtract
3819/// and it has other uses besides the load / store. After the
3820/// transformation, the new indexed load / store has effectively folded
3821/// the add / subtract in and all of its other uses are redirected to the
3822/// new load / store.
3823bool DAGCombiner::CombineToPreIndexedLoadStore(SDNode *N) {
3824 if (!AfterLegalize)
3825 return false;
3826
3827 bool isLoad = true;
3828 SDOperand Ptr;
3829 MVT::ValueType VT;
3830 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00003831 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00003832 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00003833 VT = LD->getLoadedVT();
Evan Cheng83060c52007-03-07 08:07:03 +00003834 if (!TLI.isIndexedLoadLegal(ISD::PRE_INC, VT) &&
Chris Lattner448f2192006-11-11 00:39:41 +00003835 !TLI.isIndexedLoadLegal(ISD::PRE_DEC, VT))
3836 return false;
3837 Ptr = LD->getBasePtr();
3838 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00003839 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00003840 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00003841 VT = ST->getStoredVT();
3842 if (!TLI.isIndexedStoreLegal(ISD::PRE_INC, VT) &&
3843 !TLI.isIndexedStoreLegal(ISD::PRE_DEC, VT))
3844 return false;
3845 Ptr = ST->getBasePtr();
3846 isLoad = false;
3847 } else
3848 return false;
3849
Chris Lattner9f1794e2006-11-11 00:56:29 +00003850 // If the pointer is not an add/sub, or if it doesn't have multiple uses, bail
3851 // out. There is no reason to make this a preinc/predec.
3852 if ((Ptr.getOpcode() != ISD::ADD && Ptr.getOpcode() != ISD::SUB) ||
3853 Ptr.Val->hasOneUse())
3854 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00003855
Chris Lattner9f1794e2006-11-11 00:56:29 +00003856 // Ask the target to do addressing mode selection.
3857 SDOperand BasePtr;
3858 SDOperand Offset;
3859 ISD::MemIndexedMode AM = ISD::UNINDEXED;
3860 if (!TLI.getPreIndexedAddressParts(N, BasePtr, Offset, AM, DAG))
3861 return false;
Evan Chenga7d4a042007-05-03 23:52:19 +00003862 // Don't create a indexed load / store with zero offset.
3863 if (isa<ConstantSDNode>(Offset) &&
3864 cast<ConstantSDNode>(Offset)->getValue() == 0)
3865 return false;
Chris Lattner9f1794e2006-11-11 00:56:29 +00003866
Chris Lattner41e53fd2006-11-11 01:00:15 +00003867 // Try turning it into a pre-indexed load / store except when:
Evan Chengc843abe2007-05-24 02:35:39 +00003868 // 1) The new base ptr is a frame index.
3869 // 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 +00003870 // predecessor of the value being stored.
Evan Chengc843abe2007-05-24 02:35:39 +00003871 // 3) Another use of old base ptr is a predecessor of N. If ptr is folded
Chris Lattner9f1794e2006-11-11 00:56:29 +00003872 // that would create a cycle.
Evan Chengc843abe2007-05-24 02:35:39 +00003873 // 4) All uses are load / store ops that use it as old base ptr.
Chris Lattner448f2192006-11-11 00:39:41 +00003874
Chris Lattner41e53fd2006-11-11 01:00:15 +00003875 // Check #1. Preinc'ing a frame index would require copying the stack pointer
3876 // (plus the implicit offset) to a register to preinc anyway.
3877 if (isa<FrameIndexSDNode>(BasePtr))
3878 return false;
3879
3880 // Check #2.
Chris Lattner9f1794e2006-11-11 00:56:29 +00003881 if (!isLoad) {
3882 SDOperand Val = cast<StoreSDNode>(N)->getValue();
Evan Chengc843abe2007-05-24 02:35:39 +00003883 if (Val == BasePtr || BasePtr.Val->isPredecessor(Val.Val))
Chris Lattner9f1794e2006-11-11 00:56:29 +00003884 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00003885 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00003886
Evan Chengc843abe2007-05-24 02:35:39 +00003887 // Now check for #3 and #4.
Chris Lattner9f1794e2006-11-11 00:56:29 +00003888 bool RealUse = false;
3889 for (SDNode::use_iterator I = Ptr.Val->use_begin(),
3890 E = Ptr.Val->use_end(); I != E; ++I) {
3891 SDNode *Use = *I;
3892 if (Use == N)
3893 continue;
3894 if (Use->isPredecessor(N))
3895 return false;
3896
3897 if (!((Use->getOpcode() == ISD::LOAD &&
3898 cast<LoadSDNode>(Use)->getBasePtr() == Ptr) ||
3899 (Use->getOpcode() == ISD::STORE) &&
3900 cast<StoreSDNode>(Use)->getBasePtr() == Ptr))
3901 RealUse = true;
3902 }
3903 if (!RealUse)
3904 return false;
3905
3906 SDOperand Result;
3907 if (isLoad)
3908 Result = DAG.getIndexedLoad(SDOperand(N,0), BasePtr, Offset, AM);
3909 else
3910 Result = DAG.getIndexedStore(SDOperand(N,0), BasePtr, Offset, AM);
3911 ++PreIndexedNodes;
3912 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +00003913 DOUT << "\nReplacing.4 "; DEBUG(N->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +00003914 DOUT << "\nWith: "; DEBUG(Result.Val->dump(&DAG));
3915 DOUT << '\n';
Chris Lattner9f1794e2006-11-11 00:56:29 +00003916 std::vector<SDNode*> NowDead;
3917 if (isLoad) {
3918 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(0),
Chris Lattner01d029b2007-10-15 06:10:22 +00003919 &NowDead);
Chris Lattner9f1794e2006-11-11 00:56:29 +00003920 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1), Result.getValue(2),
Chris Lattner01d029b2007-10-15 06:10:22 +00003921 &NowDead);
Chris Lattner9f1794e2006-11-11 00:56:29 +00003922 } else {
3923 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(1),
Chris Lattner01d029b2007-10-15 06:10:22 +00003924 &NowDead);
Chris Lattner9f1794e2006-11-11 00:56:29 +00003925 }
3926
3927 // Nodes can end up on the worklist more than once. Make sure we do
3928 // not process a node that has been replaced.
3929 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
3930 removeFromWorkList(NowDead[i]);
3931 // Finally, since the node is now dead, remove it from the graph.
3932 DAG.DeleteNode(N);
3933
3934 // Replace the uses of Ptr with uses of the updated base value.
3935 DAG.ReplaceAllUsesOfValueWith(Ptr, Result.getValue(isLoad ? 1 : 0),
Chris Lattner01d029b2007-10-15 06:10:22 +00003936 &NowDead);
Chris Lattner9f1794e2006-11-11 00:56:29 +00003937 removeFromWorkList(Ptr.Val);
3938 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
3939 removeFromWorkList(NowDead[i]);
3940 DAG.DeleteNode(Ptr.Val);
3941
3942 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00003943}
3944
3945/// CombineToPostIndexedLoadStore - Try combine a load / store with a
3946/// add / sub of the base pointer node into a post-indexed load / store.
3947/// The transformation folded the add / subtract into the new indexed
3948/// load / store effectively and all of its uses are redirected to the
3949/// new load / store.
3950bool DAGCombiner::CombineToPostIndexedLoadStore(SDNode *N) {
3951 if (!AfterLegalize)
3952 return false;
3953
3954 bool isLoad = true;
3955 SDOperand Ptr;
3956 MVT::ValueType VT;
3957 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00003958 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00003959 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00003960 VT = LD->getLoadedVT();
3961 if (!TLI.isIndexedLoadLegal(ISD::POST_INC, VT) &&
3962 !TLI.isIndexedLoadLegal(ISD::POST_DEC, VT))
3963 return false;
3964 Ptr = LD->getBasePtr();
3965 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00003966 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00003967 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00003968 VT = ST->getStoredVT();
3969 if (!TLI.isIndexedStoreLegal(ISD::POST_INC, VT) &&
3970 !TLI.isIndexedStoreLegal(ISD::POST_DEC, VT))
3971 return false;
3972 Ptr = ST->getBasePtr();
3973 isLoad = false;
3974 } else
3975 return false;
3976
Evan Chengcc470212006-11-16 00:08:20 +00003977 if (Ptr.Val->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00003978 return false;
3979
3980 for (SDNode::use_iterator I = Ptr.Val->use_begin(),
3981 E = Ptr.Val->use_end(); I != E; ++I) {
3982 SDNode *Op = *I;
3983 if (Op == N ||
3984 (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB))
3985 continue;
3986
3987 SDOperand BasePtr;
3988 SDOperand Offset;
3989 ISD::MemIndexedMode AM = ISD::UNINDEXED;
3990 if (TLI.getPostIndexedAddressParts(N, Op, BasePtr, Offset, AM, DAG)) {
3991 if (Ptr == Offset)
3992 std::swap(BasePtr, Offset);
3993 if (Ptr != BasePtr)
Chris Lattner448f2192006-11-11 00:39:41 +00003994 continue;
Evan Chenga7d4a042007-05-03 23:52:19 +00003995 // Don't create a indexed load / store with zero offset.
3996 if (isa<ConstantSDNode>(Offset) &&
3997 cast<ConstantSDNode>(Offset)->getValue() == 0)
3998 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00003999
Chris Lattner9f1794e2006-11-11 00:56:29 +00004000 // Try turning it into a post-indexed load / store except when
4001 // 1) All uses are load / store ops that use it as base ptr.
4002 // 2) Op must be independent of N, i.e. Op is neither a predecessor
4003 // nor a successor of N. Otherwise, if Op is folded that would
4004 // create a cycle.
4005
4006 // Check for #1.
4007 bool TryNext = false;
4008 for (SDNode::use_iterator II = BasePtr.Val->use_begin(),
4009 EE = BasePtr.Val->use_end(); II != EE; ++II) {
4010 SDNode *Use = *II;
4011 if (Use == Ptr.Val)
Chris Lattner448f2192006-11-11 00:39:41 +00004012 continue;
4013
Chris Lattner9f1794e2006-11-11 00:56:29 +00004014 // If all the uses are load / store addresses, then don't do the
4015 // transformation.
4016 if (Use->getOpcode() == ISD::ADD || Use->getOpcode() == ISD::SUB){
4017 bool RealUse = false;
4018 for (SDNode::use_iterator III = Use->use_begin(),
4019 EEE = Use->use_end(); III != EEE; ++III) {
4020 SDNode *UseUse = *III;
4021 if (!((UseUse->getOpcode() == ISD::LOAD &&
4022 cast<LoadSDNode>(UseUse)->getBasePtr().Val == Use) ||
4023 (UseUse->getOpcode() == ISD::STORE) &&
4024 cast<StoreSDNode>(UseUse)->getBasePtr().Val == Use))
4025 RealUse = true;
4026 }
Chris Lattner448f2192006-11-11 00:39:41 +00004027
Chris Lattner9f1794e2006-11-11 00:56:29 +00004028 if (!RealUse) {
4029 TryNext = true;
4030 break;
Chris Lattner448f2192006-11-11 00:39:41 +00004031 }
4032 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004033 }
4034 if (TryNext)
4035 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00004036
Chris Lattner9f1794e2006-11-11 00:56:29 +00004037 // Check for #2
4038 if (!Op->isPredecessor(N) && !N->isPredecessor(Op)) {
4039 SDOperand Result = isLoad
4040 ? DAG.getIndexedLoad(SDOperand(N,0), BasePtr, Offset, AM)
4041 : DAG.getIndexedStore(SDOperand(N,0), BasePtr, Offset, AM);
4042 ++PostIndexedNodes;
4043 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +00004044 DOUT << "\nReplacing.5 "; DEBUG(N->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +00004045 DOUT << "\nWith: "; DEBUG(Result.Val->dump(&DAG));
4046 DOUT << '\n';
Chris Lattner9f1794e2006-11-11 00:56:29 +00004047 std::vector<SDNode*> NowDead;
4048 if (isLoad) {
4049 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(0),
Chris Lattner01d029b2007-10-15 06:10:22 +00004050 &NowDead);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004051 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1), Result.getValue(2),
Chris Lattner01d029b2007-10-15 06:10:22 +00004052 &NowDead);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004053 } else {
4054 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(1),
Chris Lattner01d029b2007-10-15 06:10:22 +00004055 &NowDead);
Chris Lattner448f2192006-11-11 00:39:41 +00004056 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004057
4058 // Nodes can end up on the worklist more than once. Make sure we do
4059 // not process a node that has been replaced.
4060 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
4061 removeFromWorkList(NowDead[i]);
4062 // Finally, since the node is now dead, remove it from the graph.
4063 DAG.DeleteNode(N);
4064
4065 // Replace the uses of Use with uses of the updated base value.
4066 DAG.ReplaceAllUsesOfValueWith(SDOperand(Op, 0),
4067 Result.getValue(isLoad ? 1 : 0),
Chris Lattner01d029b2007-10-15 06:10:22 +00004068 &NowDead);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004069 removeFromWorkList(Op);
4070 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
4071 removeFromWorkList(NowDead[i]);
4072 DAG.DeleteNode(Op);
4073
4074 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00004075 }
4076 }
4077 }
4078 return false;
4079}
4080
Chris Lattner00161a62008-01-25 07:20:16 +00004081/// InferAlignment - If we can infer some alignment information from this
4082/// pointer, return it.
4083static unsigned InferAlignment(SDOperand Ptr, SelectionDAG &DAG) {
4084 // If this is a direct reference to a stack slot, use information about the
4085 // stack slot's alignment.
4086 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
4087 return DAG.getMachineFunction().getFrameInfo()->
4088 getObjectAlignment(FI->getIndex());
4089 }
4090
4091 // FIXME: Handle FI+CST.
4092
4093 return 0;
4094}
Chris Lattner448f2192006-11-11 00:39:41 +00004095
Chris Lattner01a22022005-10-10 22:04:48 +00004096SDOperand DAGCombiner::visitLOAD(SDNode *N) {
Evan Cheng466685d2006-10-09 20:57:25 +00004097 LoadSDNode *LD = cast<LoadSDNode>(N);
4098 SDOperand Chain = LD->getChain();
4099 SDOperand Ptr = LD->getBasePtr();
Chris Lattner00161a62008-01-25 07:20:16 +00004100
4101 // Try to infer better alignment information than the load already has.
4102 if (LD->isUnindexed()) {
4103 if (unsigned Align = InferAlignment(Ptr, DAG)) {
4104 if (Align > LD->getAlignment())
4105 return DAG.getExtLoad(LD->getExtensionType(), LD->getValueType(0),
4106 Chain, Ptr, LD->getSrcValue(),
4107 LD->getSrcValueOffset(), LD->getLoadedVT(),
4108 LD->isVolatile(), Align);
4109 }
4110 }
4111
Evan Cheng45a7ca92007-05-01 00:38:21 +00004112
4113 // If load is not volatile and there are no uses of the loaded value (and
4114 // the updated indexed value in case of indexed loads), change uses of the
4115 // chain value into uses of the chain input (i.e. delete the dead load).
4116 if (!LD->isVolatile()) {
Evan Cheng498f5592007-05-01 08:53:39 +00004117 if (N->getValueType(1) == MVT::Other) {
4118 // Unindexed loads.
Evan Cheng02c42852008-01-16 23:11:54 +00004119 if (N->hasNUsesOfValue(0, 0)) {
4120 // It's not safe to use the two value CombineTo variant here. e.g.
4121 // v1, chain2 = load chain1, loc
4122 // v2, chain3 = load chain2, loc
4123 // v3 = add v2, c
Chris Lattner125991a2008-01-24 07:57:06 +00004124 // Now we replace use of chain2 with chain1. This makes the second load
4125 // isomorphic to the one we are deleting, and thus makes this load live.
Evan Cheng02c42852008-01-16 23:11:54 +00004126 std::vector<SDNode*> NowDead;
Evan Cheng02c42852008-01-16 23:11:54 +00004127 DOUT << "\nReplacing.6 "; DEBUG(N->dump(&DAG));
Chris Lattner125991a2008-01-24 07:57:06 +00004128 DOUT << "\nWith chain: "; DEBUG(Chain.Val->dump(&DAG));
4129 DOUT << "\n";
Evan Cheng02c42852008-01-16 23:11:54 +00004130 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1), Chain, &NowDead);
Evan Cheng02c42852008-01-16 23:11:54 +00004131 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
4132 removeFromWorkList(NowDead[i]);
Chris Lattner125991a2008-01-24 07:57:06 +00004133 if (N->use_empty()) {
4134 removeFromWorkList(N);
4135 DAG.DeleteNode(N);
4136 }
Evan Cheng02c42852008-01-16 23:11:54 +00004137 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
4138 }
Evan Cheng498f5592007-05-01 08:53:39 +00004139 } else {
4140 // Indexed loads.
4141 assert(N->getValueType(2) == MVT::Other && "Malformed indexed loads?");
4142 if (N->hasNUsesOfValue(0, 0) && N->hasNUsesOfValue(0, 1)) {
Evan Cheng02c42852008-01-16 23:11:54 +00004143 std::vector<SDNode*> NowDead;
4144 SDOperand Undef = DAG.getNode(ISD::UNDEF, N->getValueType(0));
4145 DOUT << "\nReplacing.6 "; DEBUG(N->dump(&DAG));
4146 DOUT << "\nWith: "; DEBUG(Undef.Val->dump(&DAG));
4147 DOUT << " and 2 other values\n";
4148 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Undef, &NowDead);
4149 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1),
Chris Lattner4626b252008-01-17 07:20:38 +00004150 DAG.getNode(ISD::UNDEF, N->getValueType(1)),
Evan Cheng02c42852008-01-16 23:11:54 +00004151 &NowDead);
4152 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 2), Chain, &NowDead);
4153 removeFromWorkList(N);
4154 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
4155 removeFromWorkList(NowDead[i]);
4156 DAG.DeleteNode(N);
4157 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng45a7ca92007-05-01 00:38:21 +00004158 }
Evan Cheng45a7ca92007-05-01 00:38:21 +00004159 }
4160 }
Chris Lattner01a22022005-10-10 22:04:48 +00004161
4162 // If this load is directly stored, replace the load value with the stored
4163 // value.
4164 // TODO: Handle store large -> read small portion.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004165 // TODO: Handle TRUNCSTORE/LOADEXT
4166 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00004167 if (ISD::isNON_TRUNCStore(Chain.Val)) {
4168 StoreSDNode *PrevST = cast<StoreSDNode>(Chain);
4169 if (PrevST->getBasePtr() == Ptr &&
4170 PrevST->getValue().getValueType() == N->getValueType(0))
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004171 return CombineTo(N, Chain.getOperand(1), Chain);
Evan Cheng8b2794a2006-10-13 21:14:26 +00004172 }
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004173 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00004174
Jim Laskey7ca56af2006-10-11 13:47:09 +00004175 if (CombinerAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00004176 // Walk up chain skipping non-aliasing memory nodes.
4177 SDOperand BetterChain = FindBetterChain(N, Chain);
4178
Jim Laskey6ff23e52006-10-04 16:53:27 +00004179 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00004180 if (Chain != BetterChain) {
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004181 SDOperand ReplLoad;
4182
Jim Laskey279f0532006-09-25 16:29:54 +00004183 // Replace the chain to void dependency.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004184 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
4185 ReplLoad = DAG.getLoad(N->getValueType(0), BetterChain, Ptr,
Duncan Sandsdc846502007-10-28 12:59:45 +00004186 LD->getSrcValue(), LD->getSrcValueOffset(),
4187 LD->isVolatile(), LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004188 } else {
4189 ReplLoad = DAG.getExtLoad(LD->getExtensionType(),
4190 LD->getValueType(0),
4191 BetterChain, Ptr, LD->getSrcValue(),
4192 LD->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00004193 LD->getLoadedVT(),
4194 LD->isVolatile(),
4195 LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004196 }
Jim Laskey279f0532006-09-25 16:29:54 +00004197
Jim Laskey6ff23e52006-10-04 16:53:27 +00004198 // Create token factor to keep old chain connected.
Jim Laskey288af5e2006-09-25 19:32:58 +00004199 SDOperand Token = DAG.getNode(ISD::TokenFactor, MVT::Other,
4200 Chain, ReplLoad.getValue(1));
Jim Laskey6ff23e52006-10-04 16:53:27 +00004201
Jim Laskey274062c2006-10-13 23:32:28 +00004202 // Replace uses with load result and token factor. Don't add users
4203 // to work list.
4204 return CombineTo(N, ReplLoad.getValue(0), Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00004205 }
4206 }
4207
Evan Cheng7fc033a2006-11-03 03:06:21 +00004208 // Try transforming N to an indexed load.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00004209 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Evan Cheng7fc033a2006-11-03 03:06:21 +00004210 return SDOperand(N, 0);
4211
Chris Lattner01a22022005-10-10 22:04:48 +00004212 return SDOperand();
4213}
4214
Chris Lattner07649d92008-01-08 23:08:06 +00004215
Chris Lattner87514ca2005-10-10 22:31:19 +00004216SDOperand DAGCombiner::visitSTORE(SDNode *N) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00004217 StoreSDNode *ST = cast<StoreSDNode>(N);
4218 SDOperand Chain = ST->getChain();
4219 SDOperand Value = ST->getValue();
4220 SDOperand Ptr = ST->getBasePtr();
Jim Laskey7aed46c2006-10-11 18:55:16 +00004221
Chris Lattner00161a62008-01-25 07:20:16 +00004222 // Try to infer better alignment information than the store already has.
4223 if (ST->isUnindexed()) {
4224 if (unsigned Align = InferAlignment(Ptr, DAG)) {
4225 if (Align > ST->getAlignment())
4226 return DAG.getTruncStore(Chain, Value, Ptr, ST->getSrcValue(),
4227 ST->getSrcValueOffset(), ST->getStoredVT(),
4228 ST->isVolatile(), Align);
4229 }
4230 }
4231
Evan Cheng59d5b682007-05-07 21:27:48 +00004232 // If this is a store of a bit convert, store the input value if the
Evan Cheng2c4f9432007-05-09 21:49:47 +00004233 // resultant store does not need a higher alignment than the original.
Dale Johannesen98a6c622007-05-16 22:45:30 +00004234 if (Value.getOpcode() == ISD::BIT_CONVERT && !ST->isTruncatingStore() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00004235 ST->isUnindexed()) {
Evan Cheng59d5b682007-05-07 21:27:48 +00004236 unsigned Align = ST->getAlignment();
4237 MVT::ValueType SVT = Value.getOperand(0).getValueType();
4238 unsigned OrigAlign = TLI.getTargetMachine().getTargetData()->
Dan Gohmanfcc4dd92007-05-18 18:41:29 +00004239 getABITypeAlignment(MVT::getTypeForValueType(SVT));
Evan Chengc2cd2b22007-05-07 21:36:06 +00004240 if (Align <= OrigAlign && TLI.isOperationLegal(ISD::STORE, SVT))
Evan Cheng59d5b682007-05-07 21:27:48 +00004241 return DAG.getStore(Chain, Value.getOperand(0), Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004242 ST->getSrcValueOffset(), ST->isVolatile(), Align);
Jim Laskey279f0532006-09-25 16:29:54 +00004243 }
4244
Nate Begeman2cbba892006-12-11 02:23:46 +00004245 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Nate Begeman2cbba892006-12-11 02:23:46 +00004246 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Value)) {
Evan Cheng25ece662006-12-11 17:25:19 +00004247 if (Value.getOpcode() != ISD::TargetConstantFP) {
4248 SDOperand Tmp;
Chris Lattner62be1a72006-12-12 04:16:14 +00004249 switch (CFP->getValueType(0)) {
4250 default: assert(0 && "Unknown FP type");
Dale Johannesenc7b21d52007-09-18 18:36:59 +00004251 case MVT::f80: // We don't do this for these yet.
4252 case MVT::f128:
4253 case MVT::ppcf128:
4254 break;
Chris Lattner62be1a72006-12-12 04:16:14 +00004255 case MVT::f32:
4256 if (!AfterLegalize || TLI.isTypeLegal(MVT::i32)) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00004257 Tmp = DAG.getConstant((uint32_t)CFP->getValueAPF().
4258 convertToAPInt().getZExtValue(), MVT::i32);
Chris Lattner62be1a72006-12-12 04:16:14 +00004259 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004260 ST->getSrcValueOffset(), ST->isVolatile(),
4261 ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00004262 }
4263 break;
4264 case MVT::f64:
4265 if (!AfterLegalize || TLI.isTypeLegal(MVT::i64)) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00004266 Tmp = DAG.getConstant(CFP->getValueAPF().convertToAPInt().
4267 getZExtValue(), MVT::i64);
Chris Lattner62be1a72006-12-12 04:16:14 +00004268 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004269 ST->getSrcValueOffset(), ST->isVolatile(),
4270 ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00004271 } else if (TLI.isTypeLegal(MVT::i32)) {
Duncan Sandsdc846502007-10-28 12:59:45 +00004272 // Many FP stores are not made apparent until after legalize, e.g. for
Chris Lattner62be1a72006-12-12 04:16:14 +00004273 // argument passing. Since this is so common, custom legalize the
4274 // 64-bit integer store into two 32-bit stores.
Dale Johannesen9d5f4562007-09-12 03:30:33 +00004275 uint64_t Val = CFP->getValueAPF().convertToAPInt().getZExtValue();
Chris Lattner62be1a72006-12-12 04:16:14 +00004276 SDOperand Lo = DAG.getConstant(Val & 0xFFFFFFFF, MVT::i32);
4277 SDOperand Hi = DAG.getConstant(Val >> 32, MVT::i32);
4278 if (!TLI.isLittleEndian()) std::swap(Lo, Hi);
4279
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004280 int SVOffset = ST->getSrcValueOffset();
4281 unsigned Alignment = ST->getAlignment();
4282 bool isVolatile = ST->isVolatile();
4283
Chris Lattner62be1a72006-12-12 04:16:14 +00004284 SDOperand St0 = DAG.getStore(Chain, Lo, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004285 ST->getSrcValueOffset(),
4286 isVolatile, ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00004287 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
4288 DAG.getConstant(4, Ptr.getValueType()));
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004289 SVOffset += 4;
Duncan Sandsdc846502007-10-28 12:59:45 +00004290 Alignment = MinAlign(Alignment, 4U);
Chris Lattner62be1a72006-12-12 04:16:14 +00004291 SDOperand St1 = DAG.getStore(Chain, Hi, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004292 SVOffset, isVolatile, Alignment);
Chris Lattner62be1a72006-12-12 04:16:14 +00004293 return DAG.getNode(ISD::TokenFactor, MVT::Other, St0, St1);
4294 }
4295 break;
Evan Cheng25ece662006-12-11 17:25:19 +00004296 }
Nate Begeman2cbba892006-12-11 02:23:46 +00004297 }
Nate Begeman2cbba892006-12-11 02:23:46 +00004298 }
4299
Jim Laskey279f0532006-09-25 16:29:54 +00004300 if (CombinerAA) {
4301 // Walk up chain skipping non-aliasing memory nodes.
4302 SDOperand BetterChain = FindBetterChain(N, Chain);
4303
Jim Laskey6ff23e52006-10-04 16:53:27 +00004304 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00004305 if (Chain != BetterChain) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00004306 // Replace the chain to avoid dependency.
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004307 SDOperand ReplStore;
4308 if (ST->isTruncatingStore()) {
4309 ReplStore = DAG.getTruncStore(BetterChain, Value, Ptr,
Chris Lattner4626b252008-01-17 07:20:38 +00004310 ST->getSrcValue(),ST->getSrcValueOffset(),
4311 ST->getStoredVT(),
4312 ST->isVolatile(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004313 } else {
4314 ReplStore = DAG.getStore(BetterChain, Value, Ptr,
Chris Lattner4626b252008-01-17 07:20:38 +00004315 ST->getSrcValue(), ST->getSrcValueOffset(),
4316 ST->isVolatile(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004317 }
4318
Jim Laskey279f0532006-09-25 16:29:54 +00004319 // Create token to keep both nodes around.
Jim Laskey274062c2006-10-13 23:32:28 +00004320 SDOperand Token =
4321 DAG.getNode(ISD::TokenFactor, MVT::Other, Chain, ReplStore);
4322
4323 // Don't add users to work list.
4324 return CombineTo(N, Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00004325 }
Jim Laskeyd1aed7a2006-09-21 16:28:59 +00004326 }
Chris Lattnerc33baaa2005-12-23 05:48:07 +00004327
Evan Cheng33dbedc2006-11-05 09:31:14 +00004328 // Try transforming N to an indexed store.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00004329 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Evan Cheng33dbedc2006-11-05 09:31:14 +00004330 return SDOperand(N, 0);
4331
Chris Lattner3c872852007-12-29 06:26:16 +00004332 // FIXME: is there such a thing as a truncating indexed store?
Chris Lattnerddf89562008-01-17 19:59:44 +00004333 if (ST->isTruncatingStore() && ST->isUnindexed() &&
Chris Lattner2b4c2792007-10-13 06:35:54 +00004334 MVT::isInteger(Value.getValueType())) {
4335 // See if we can simplify the input to this truncstore with knowledge that
4336 // only the low bits are being used. For example:
4337 // "truncstore (or (shl x, 8), y), i8" -> "truncstore y, i8"
4338 SDOperand Shorter =
4339 GetDemandedBits(Value, MVT::getIntVTBitMask(ST->getStoredVT()));
4340 AddToWorkList(Value.Val);
4341 if (Shorter.Val)
4342 return DAG.getTruncStore(Chain, Shorter, Ptr, ST->getSrcValue(),
4343 ST->getSrcValueOffset(), ST->getStoredVT(),
4344 ST->isVolatile(), ST->getAlignment());
Chris Lattnere33544c2007-10-13 06:58:48 +00004345
4346 // Otherwise, see if we can simplify the operation with
4347 // SimplifyDemandedBits, which only works if the value has a single use.
4348 if (SimplifyDemandedBits(Value, MVT::getIntVTBitMask(ST->getStoredVT())))
4349 return SDOperand(N, 0);
Chris Lattner2b4c2792007-10-13 06:35:54 +00004350 }
4351
Chris Lattner3c872852007-12-29 06:26:16 +00004352 // If this is a load followed by a store to the same location, then the store
4353 // is dead/noop.
4354 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Value)) {
Chris Lattner07649d92008-01-08 23:08:06 +00004355 if (Ld->getBasePtr() == Ptr && ST->getStoredVT() == Ld->getLoadedVT() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00004356 ST->isUnindexed() && !ST->isVolatile() &&
Chris Lattner07649d92008-01-08 23:08:06 +00004357 // There can't be any side effects between the load and store, such as
4358 // a call or store.
Chris Lattner572dee72008-01-16 05:49:24 +00004359 Chain.reachesChainWithoutSideEffects(SDOperand(Ld, 1))) {
Chris Lattner3c872852007-12-29 06:26:16 +00004360 // The store is dead, remove it.
4361 return Chain;
4362 }
4363 }
4364
Chris Lattnerddf89562008-01-17 19:59:44 +00004365 // If this is an FP_ROUND or TRUNC followed by a store, fold this into a
4366 // truncating store. We can do this even if this is already a truncstore.
4367 if ((Value.getOpcode() == ISD::FP_ROUND || Value.getOpcode() == ISD::TRUNCATE)
4368 && TLI.isTypeLegal(Value.getOperand(0).getValueType()) &&
4369 Value.Val->hasOneUse() && ST->isUnindexed() &&
4370 TLI.isTruncStoreLegal(Value.getOperand(0).getValueType(),
4371 ST->getStoredVT())) {
4372 return DAG.getTruncStore(Chain, Value.getOperand(0), Ptr, ST->getSrcValue(),
4373 ST->getSrcValueOffset(), ST->getStoredVT(),
4374 ST->isVolatile(), ST->getAlignment());
4375 }
4376
Chris Lattner87514ca2005-10-10 22:31:19 +00004377 return SDOperand();
4378}
4379
Chris Lattnerca242442006-03-19 01:27:56 +00004380SDOperand DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
4381 SDOperand InVec = N->getOperand(0);
4382 SDOperand InVal = N->getOperand(1);
4383 SDOperand EltNo = N->getOperand(2);
4384
4385 // If the invec is a BUILD_VECTOR and if EltNo is a constant, build a new
4386 // vector with the inserted element.
4387 if (InVec.getOpcode() == ISD::BUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
4388 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004389 SmallVector<SDOperand, 8> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
Chris Lattnerca242442006-03-19 01:27:56 +00004390 if (Elt < Ops.size())
4391 Ops[Elt] = InVal;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004392 return DAG.getNode(ISD::BUILD_VECTOR, InVec.getValueType(),
4393 &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00004394 }
4395
4396 return SDOperand();
4397}
4398
Evan Cheng513da432007-10-06 08:19:55 +00004399SDOperand DAGCombiner::visitEXTRACT_VECTOR_ELT(SDNode *N) {
4400 SDOperand InVec = N->getOperand(0);
4401 SDOperand EltNo = N->getOperand(1);
4402
4403 // (vextract (v4f32 s2v (f32 load $addr)), 0) -> (f32 load $addr)
4404 // (vextract (v4i32 bc (v4f32 s2v (f32 load $addr))), 0) -> (i32 load $addr)
4405 if (isa<ConstantSDNode>(EltNo)) {
4406 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
4407 bool NewLoad = false;
4408 if (Elt == 0) {
4409 MVT::ValueType VT = InVec.getValueType();
4410 MVT::ValueType EVT = MVT::getVectorElementType(VT);
4411 MVT::ValueType LVT = EVT;
4412 unsigned NumElts = MVT::getVectorNumElements(VT);
4413 if (InVec.getOpcode() == ISD::BIT_CONVERT) {
4414 MVT::ValueType BCVT = InVec.getOperand(0).getValueType();
Dan Gohman090b38a2007-10-29 20:44:42 +00004415 if (!MVT::isVector(BCVT) ||
4416 NumElts != MVT::getVectorNumElements(BCVT))
Evan Cheng513da432007-10-06 08:19:55 +00004417 return SDOperand();
4418 InVec = InVec.getOperand(0);
4419 EVT = MVT::getVectorElementType(BCVT);
4420 NewLoad = true;
4421 }
4422 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR &&
4423 InVec.getOperand(0).getValueType() == EVT &&
4424 ISD::isNormalLoad(InVec.getOperand(0).Val) &&
4425 InVec.getOperand(0).hasOneUse()) {
4426 LoadSDNode *LN0 = cast<LoadSDNode>(InVec.getOperand(0));
4427 unsigned Align = LN0->getAlignment();
4428 if (NewLoad) {
4429 // Check the resultant load doesn't need a higher alignment than the
4430 // original load.
4431 unsigned NewAlign = TLI.getTargetMachine().getTargetData()->
4432 getABITypeAlignment(MVT::getTypeForValueType(LVT));
4433 if (!TLI.isOperationLegal(ISD::LOAD, LVT) || NewAlign > Align)
4434 return SDOperand();
4435 Align = NewAlign;
4436 }
4437
4438 return DAG.getLoad(LVT, LN0->getChain(), LN0->getBasePtr(),
4439 LN0->getSrcValue(), LN0->getSrcValueOffset(),
4440 LN0->isVolatile(), Align);
4441 }
4442 }
4443 }
4444 return SDOperand();
4445}
4446
4447
Dan Gohman7f321562007-06-25 16:23:39 +00004448SDOperand DAGCombiner::visitBUILD_VECTOR(SDNode *N) {
4449 unsigned NumInScalars = N->getNumOperands();
4450 MVT::ValueType VT = N->getValueType(0);
4451 unsigned NumElts = MVT::getVectorNumElements(VT);
4452 MVT::ValueType EltType = MVT::getVectorElementType(VT);
Chris Lattnerca242442006-03-19 01:27:56 +00004453
Dan Gohman7f321562007-06-25 16:23:39 +00004454 // Check to see if this is a BUILD_VECTOR of a bunch of EXTRACT_VECTOR_ELT
4455 // operations. If so, and if the EXTRACT_VECTOR_ELT vector inputs come from
4456 // at most two distinct vectors, turn this into a shuffle node.
Chris Lattnerd7648c82006-03-28 20:28:38 +00004457 SDOperand VecIn1, VecIn2;
4458 for (unsigned i = 0; i != NumInScalars; ++i) {
4459 // Ignore undef inputs.
4460 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
4461
Dan Gohman7f321562007-06-25 16:23:39 +00004462 // If this input is something other than a EXTRACT_VECTOR_ELT with a
Chris Lattnerd7648c82006-03-28 20:28:38 +00004463 // constant index, bail out.
Dan Gohman7f321562007-06-25 16:23:39 +00004464 if (N->getOperand(i).getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
Chris Lattnerd7648c82006-03-28 20:28:38 +00004465 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
4466 VecIn1 = VecIn2 = SDOperand(0, 0);
4467 break;
4468 }
4469
Dan Gohman7f321562007-06-25 16:23:39 +00004470 // If the input vector type disagrees with the result of the build_vector,
Chris Lattnerd7648c82006-03-28 20:28:38 +00004471 // we can't make a shuffle.
4472 SDOperand ExtractedFromVec = N->getOperand(i).getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00004473 if (ExtractedFromVec.getValueType() != VT) {
Chris Lattnerd7648c82006-03-28 20:28:38 +00004474 VecIn1 = VecIn2 = SDOperand(0, 0);
4475 break;
4476 }
4477
4478 // Otherwise, remember this. We allow up to two distinct input vectors.
4479 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
4480 continue;
4481
4482 if (VecIn1.Val == 0) {
4483 VecIn1 = ExtractedFromVec;
4484 } else if (VecIn2.Val == 0) {
4485 VecIn2 = ExtractedFromVec;
4486 } else {
4487 // Too many inputs.
4488 VecIn1 = VecIn2 = SDOperand(0, 0);
4489 break;
4490 }
4491 }
4492
4493 // If everything is good, we can make a shuffle operation.
4494 if (VecIn1.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004495 SmallVector<SDOperand, 8> BuildVecIndices;
Chris Lattnerd7648c82006-03-28 20:28:38 +00004496 for (unsigned i = 0; i != NumInScalars; ++i) {
4497 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
Evan Cheng597a3bd2007-01-20 10:10:26 +00004498 BuildVecIndices.push_back(DAG.getNode(ISD::UNDEF, TLI.getPointerTy()));
Chris Lattnerd7648c82006-03-28 20:28:38 +00004499 continue;
4500 }
4501
4502 SDOperand Extract = N->getOperand(i);
4503
4504 // If extracting from the first vector, just use the index directly.
4505 if (Extract.getOperand(0) == VecIn1) {
4506 BuildVecIndices.push_back(Extract.getOperand(1));
4507 continue;
4508 }
4509
4510 // Otherwise, use InIdx + VecSize
4511 unsigned Idx = cast<ConstantSDNode>(Extract.getOperand(1))->getValue();
Chris Lattner0bd48932008-01-17 07:00:52 +00004512 BuildVecIndices.push_back(DAG.getIntPtrConstant(Idx+NumInScalars));
Chris Lattnerd7648c82006-03-28 20:28:38 +00004513 }
4514
4515 // Add count and size info.
Chris Lattner0bd48932008-01-17 07:00:52 +00004516 MVT::ValueType BuildVecVT = MVT::getVectorType(TLI.getPointerTy(), NumElts);
Chris Lattnerd7648c82006-03-28 20:28:38 +00004517
Dan Gohman7f321562007-06-25 16:23:39 +00004518 // Return the new VECTOR_SHUFFLE node.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004519 SDOperand Ops[5];
4520 Ops[0] = VecIn1;
Chris Lattnercef896e2006-03-28 22:19:47 +00004521 if (VecIn2.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004522 Ops[1] = VecIn2;
Chris Lattnercef896e2006-03-28 22:19:47 +00004523 } else {
Dan Gohman7f321562007-06-25 16:23:39 +00004524 // Use an undef build_vector as input for the second operand.
Chris Lattnercef896e2006-03-28 22:19:47 +00004525 std::vector<SDOperand> UnOps(NumInScalars,
4526 DAG.getNode(ISD::UNDEF,
Dan Gohman7f321562007-06-25 16:23:39 +00004527 EltType));
4528 Ops[1] = DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004529 &UnOps[0], UnOps.size());
4530 AddToWorkList(Ops[1].Val);
Chris Lattnercef896e2006-03-28 22:19:47 +00004531 }
Dan Gohman7f321562007-06-25 16:23:39 +00004532 Ops[2] = DAG.getNode(ISD::BUILD_VECTOR, BuildVecVT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004533 &BuildVecIndices[0], BuildVecIndices.size());
Dan Gohman7f321562007-06-25 16:23:39 +00004534 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Ops, 3);
Chris Lattnerd7648c82006-03-28 20:28:38 +00004535 }
4536
4537 return SDOperand();
4538}
4539
Dan Gohman7f321562007-06-25 16:23:39 +00004540SDOperand DAGCombiner::visitCONCAT_VECTORS(SDNode *N) {
4541 // TODO: Check to see if this is a CONCAT_VECTORS of a bunch of
4542 // EXTRACT_SUBVECTOR operations. If so, and if the EXTRACT_SUBVECTOR vector
4543 // inputs come from at most two distinct vectors, turn this into a shuffle
4544 // node.
4545
4546 // If we only have one input vector, we don't need to do any concatenation.
4547 if (N->getNumOperands() == 1) {
4548 return N->getOperand(0);
4549 }
4550
4551 return SDOperand();
4552}
4553
Chris Lattner66445d32006-03-28 22:11:53 +00004554SDOperand DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
Chris Lattnerf1d0c622006-03-31 22:16:43 +00004555 SDOperand ShufMask = N->getOperand(2);
4556 unsigned NumElts = ShufMask.getNumOperands();
4557
4558 // If the shuffle mask is an identity operation on the LHS, return the LHS.
4559 bool isIdentity = true;
4560 for (unsigned i = 0; i != NumElts; ++i) {
4561 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
4562 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
4563 isIdentity = false;
4564 break;
4565 }
4566 }
4567 if (isIdentity) return N->getOperand(0);
4568
4569 // If the shuffle mask is an identity operation on the RHS, return the RHS.
4570 isIdentity = true;
4571 for (unsigned i = 0; i != NumElts; ++i) {
4572 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
4573 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
4574 isIdentity = false;
4575 break;
4576 }
4577 }
4578 if (isIdentity) return N->getOperand(1);
Evan Chenge7bec0d2006-07-20 22:44:41 +00004579
4580 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
4581 // needed at all.
4582 bool isUnary = true;
Evan Cheng917ec982006-07-21 08:25:53 +00004583 bool isSplat = true;
Evan Chenge7bec0d2006-07-20 22:44:41 +00004584 int VecNum = -1;
Reid Spencer9160a6a2006-07-25 20:44:41 +00004585 unsigned BaseIdx = 0;
Evan Chenge7bec0d2006-07-20 22:44:41 +00004586 for (unsigned i = 0; i != NumElts; ++i)
4587 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
4588 unsigned Idx = cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue();
4589 int V = (Idx < NumElts) ? 0 : 1;
Evan Cheng917ec982006-07-21 08:25:53 +00004590 if (VecNum == -1) {
Evan Chenge7bec0d2006-07-20 22:44:41 +00004591 VecNum = V;
Evan Cheng917ec982006-07-21 08:25:53 +00004592 BaseIdx = Idx;
4593 } else {
4594 if (BaseIdx != Idx)
4595 isSplat = false;
4596 if (VecNum != V) {
4597 isUnary = false;
4598 break;
4599 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00004600 }
4601 }
4602
4603 SDOperand N0 = N->getOperand(0);
4604 SDOperand N1 = N->getOperand(1);
4605 // Normalize unary shuffle so the RHS is undef.
4606 if (isUnary && VecNum == 1)
4607 std::swap(N0, N1);
4608
Evan Cheng917ec982006-07-21 08:25:53 +00004609 // If it is a splat, check if the argument vector is a build_vector with
4610 // all scalar elements the same.
4611 if (isSplat) {
4612 SDNode *V = N0.Val;
Evan Cheng917ec982006-07-21 08:25:53 +00004613
Dan Gohman7f321562007-06-25 16:23:39 +00004614 // If this is a bit convert that changes the element type of the vector but
Evan Cheng59569222006-10-16 22:49:37 +00004615 // not the number of vector elements, look through it. Be careful not to
4616 // look though conversions that change things like v4f32 to v2f64.
Dan Gohman7f321562007-06-25 16:23:39 +00004617 if (V->getOpcode() == ISD::BIT_CONVERT) {
Evan Cheng59569222006-10-16 22:49:37 +00004618 SDOperand ConvInput = V->getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00004619 if (MVT::getVectorNumElements(ConvInput.getValueType()) == NumElts)
Evan Cheng59569222006-10-16 22:49:37 +00004620 V = ConvInput.Val;
4621 }
4622
Dan Gohman7f321562007-06-25 16:23:39 +00004623 if (V->getOpcode() == ISD::BUILD_VECTOR) {
4624 unsigned NumElems = V->getNumOperands();
Evan Cheng917ec982006-07-21 08:25:53 +00004625 if (NumElems > BaseIdx) {
4626 SDOperand Base;
4627 bool AllSame = true;
4628 for (unsigned i = 0; i != NumElems; ++i) {
4629 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
4630 Base = V->getOperand(i);
4631 break;
4632 }
4633 }
4634 // Splat of <u, u, u, u>, return <u, u, u, u>
4635 if (!Base.Val)
4636 return N0;
4637 for (unsigned i = 0; i != NumElems; ++i) {
Evan Chenge0480d22007-09-18 21:54:37 +00004638 if (V->getOperand(i) != Base) {
Evan Cheng917ec982006-07-21 08:25:53 +00004639 AllSame = false;
4640 break;
4641 }
4642 }
4643 // Splat of <x, x, x, x>, return <x, x, x, x>
4644 if (AllSame)
4645 return N0;
4646 }
4647 }
4648 }
4649
Evan Chenge7bec0d2006-07-20 22:44:41 +00004650 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
4651 // into an undef.
4652 if (isUnary || N0 == N1) {
Chris Lattner17614ea2006-04-08 05:34:25 +00004653 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
4654 // first operand.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004655 SmallVector<SDOperand, 8> MappedOps;
Chris Lattner17614ea2006-04-08 05:34:25 +00004656 for (unsigned i = 0; i != NumElts; ++i) {
4657 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
4658 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) {
4659 MappedOps.push_back(ShufMask.getOperand(i));
4660 } else {
4661 unsigned NewIdx =
4662 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
4663 MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32));
4664 }
4665 }
Dan Gohman7f321562007-06-25 16:23:39 +00004666 ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMask.getValueType(),
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004667 &MappedOps[0], MappedOps.size());
Chris Lattner17614ea2006-04-08 05:34:25 +00004668 AddToWorkList(ShufMask.Val);
Dan Gohman7f321562007-06-25 16:23:39 +00004669 return DAG.getNode(ISD::VECTOR_SHUFFLE, N->getValueType(0),
4670 N0,
4671 DAG.getNode(ISD::UNDEF, N->getValueType(0)),
4672 ShufMask);
Chris Lattner17614ea2006-04-08 05:34:25 +00004673 }
Dan Gohman7f321562007-06-25 16:23:39 +00004674
Chris Lattnerf1d0c622006-03-31 22:16:43 +00004675 return SDOperand();
4676}
4677
Evan Cheng44f1f092006-04-20 08:56:16 +00004678/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
Dan Gohman7f321562007-06-25 16:23:39 +00004679/// an AND to a vector_shuffle with the destination vector and a zero vector.
4680/// e.g. AND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
Evan Cheng44f1f092006-04-20 08:56:16 +00004681/// vector_shuffle V, Zero, <0, 4, 2, 4>
4682SDOperand DAGCombiner::XformToShuffleWithZero(SDNode *N) {
4683 SDOperand LHS = N->getOperand(0);
4684 SDOperand RHS = N->getOperand(1);
Dan Gohman7f321562007-06-25 16:23:39 +00004685 if (N->getOpcode() == ISD::AND) {
4686 if (RHS.getOpcode() == ISD::BIT_CONVERT)
Evan Cheng44f1f092006-04-20 08:56:16 +00004687 RHS = RHS.getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00004688 if (RHS.getOpcode() == ISD::BUILD_VECTOR) {
Evan Cheng44f1f092006-04-20 08:56:16 +00004689 std::vector<SDOperand> IdxOps;
4690 unsigned NumOps = RHS.getNumOperands();
Dan Gohman7f321562007-06-25 16:23:39 +00004691 unsigned NumElts = NumOps;
4692 MVT::ValueType EVT = MVT::getVectorElementType(RHS.getValueType());
Evan Cheng44f1f092006-04-20 08:56:16 +00004693 for (unsigned i = 0; i != NumElts; ++i) {
4694 SDOperand Elt = RHS.getOperand(i);
4695 if (!isa<ConstantSDNode>(Elt))
4696 return SDOperand();
4697 else if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
4698 IdxOps.push_back(DAG.getConstant(i, EVT));
4699 else if (cast<ConstantSDNode>(Elt)->isNullValue())
4700 IdxOps.push_back(DAG.getConstant(NumElts, EVT));
4701 else
4702 return SDOperand();
4703 }
4704
4705 // Let's see if the target supports this vector_shuffle.
4706 if (!TLI.isVectorClearMaskLegal(IdxOps, EVT, DAG))
4707 return SDOperand();
4708
Dan Gohman7f321562007-06-25 16:23:39 +00004709 // Return the new VECTOR_SHUFFLE node.
4710 MVT::ValueType VT = MVT::getVectorType(EVT, NumElts);
Evan Cheng44f1f092006-04-20 08:56:16 +00004711 std::vector<SDOperand> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00004712 LHS = DAG.getNode(ISD::BIT_CONVERT, VT, LHS);
Evan Cheng44f1f092006-04-20 08:56:16 +00004713 Ops.push_back(LHS);
4714 AddToWorkList(LHS.Val);
4715 std::vector<SDOperand> ZeroOps(NumElts, DAG.getConstant(0, EVT));
Dan Gohman7f321562007-06-25 16:23:39 +00004716 Ops.push_back(DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004717 &ZeroOps[0], ZeroOps.size()));
Dan Gohman7f321562007-06-25 16:23:39 +00004718 Ops.push_back(DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004719 &IdxOps[0], IdxOps.size()));
Dan Gohman7f321562007-06-25 16:23:39 +00004720 SDOperand Result = DAG.getNode(ISD::VECTOR_SHUFFLE, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004721 &Ops[0], Ops.size());
Dan Gohman7f321562007-06-25 16:23:39 +00004722 if (VT != LHS.getValueType()) {
4723 Result = DAG.getNode(ISD::BIT_CONVERT, LHS.getValueType(), Result);
Evan Cheng44f1f092006-04-20 08:56:16 +00004724 }
4725 return Result;
4726 }
4727 }
4728 return SDOperand();
4729}
4730
Dan Gohman7f321562007-06-25 16:23:39 +00004731/// SimplifyVBinOp - Visit a binary vector operation, like ADD.
4732SDOperand DAGCombiner::SimplifyVBinOp(SDNode *N) {
4733 // After legalize, the target may be depending on adds and other
4734 // binary ops to provide legal ways to construct constants or other
4735 // things. Simplifying them may result in a loss of legality.
4736 if (AfterLegalize) return SDOperand();
4737
4738 MVT::ValueType VT = N->getValueType(0);
Dan Gohman05d92fe2007-07-13 20:03:40 +00004739 assert(MVT::isVector(VT) && "SimplifyVBinOp only works on vectors!");
Dan Gohman7f321562007-06-25 16:23:39 +00004740
4741 MVT::ValueType EltType = MVT::getVectorElementType(VT);
Chris Lattneredab1b92006-04-02 03:25:57 +00004742 SDOperand LHS = N->getOperand(0);
4743 SDOperand RHS = N->getOperand(1);
Evan Cheng44f1f092006-04-20 08:56:16 +00004744 SDOperand Shuffle = XformToShuffleWithZero(N);
4745 if (Shuffle.Val) return Shuffle;
4746
Dan Gohman7f321562007-06-25 16:23:39 +00004747 // If the LHS and RHS are BUILD_VECTOR nodes, see if we can constant fold
Chris Lattneredab1b92006-04-02 03:25:57 +00004748 // this operation.
Dan Gohman7f321562007-06-25 16:23:39 +00004749 if (LHS.getOpcode() == ISD::BUILD_VECTOR &&
4750 RHS.getOpcode() == ISD::BUILD_VECTOR) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004751 SmallVector<SDOperand, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00004752 for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) {
Chris Lattneredab1b92006-04-02 03:25:57 +00004753 SDOperand LHSOp = LHS.getOperand(i);
4754 SDOperand RHSOp = RHS.getOperand(i);
4755 // If these two elements can't be folded, bail out.
4756 if ((LHSOp.getOpcode() != ISD::UNDEF &&
4757 LHSOp.getOpcode() != ISD::Constant &&
4758 LHSOp.getOpcode() != ISD::ConstantFP) ||
4759 (RHSOp.getOpcode() != ISD::UNDEF &&
4760 RHSOp.getOpcode() != ISD::Constant &&
4761 RHSOp.getOpcode() != ISD::ConstantFP))
4762 break;
Evan Cheng7b336a82006-05-31 06:08:35 +00004763 // Can't fold divide by zero.
Dan Gohman7f321562007-06-25 16:23:39 +00004764 if (N->getOpcode() == ISD::SDIV || N->getOpcode() == ISD::UDIV ||
4765 N->getOpcode() == ISD::FDIV) {
Evan Cheng7b336a82006-05-31 06:08:35 +00004766 if ((RHSOp.getOpcode() == ISD::Constant &&
4767 cast<ConstantSDNode>(RHSOp.Val)->isNullValue()) ||
4768 (RHSOp.getOpcode() == ISD::ConstantFP &&
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00004769 cast<ConstantFPSDNode>(RHSOp.Val)->getValueAPF().isZero()))
Evan Cheng7b336a82006-05-31 06:08:35 +00004770 break;
4771 }
Dan Gohman7f321562007-06-25 16:23:39 +00004772 Ops.push_back(DAG.getNode(N->getOpcode(), EltType, LHSOp, RHSOp));
Chris Lattner3e104b12006-04-08 04:15:24 +00004773 AddToWorkList(Ops.back().Val);
Chris Lattneredab1b92006-04-02 03:25:57 +00004774 assert((Ops.back().getOpcode() == ISD::UNDEF ||
4775 Ops.back().getOpcode() == ISD::Constant ||
4776 Ops.back().getOpcode() == ISD::ConstantFP) &&
4777 "Scalar binop didn't fold!");
4778 }
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00004779
Dan Gohman7f321562007-06-25 16:23:39 +00004780 if (Ops.size() == LHS.getNumOperands()) {
4781 MVT::ValueType VT = LHS.getValueType();
4782 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00004783 }
Chris Lattneredab1b92006-04-02 03:25:57 +00004784 }
4785
4786 return SDOperand();
4787}
4788
Nate Begeman44728a72005-09-19 22:34:01 +00004789SDOperand DAGCombiner::SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2){
Nate Begemanf845b452005-10-08 00:29:44 +00004790 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
4791
4792 SDOperand SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
4793 cast<CondCodeSDNode>(N0.getOperand(2))->get());
4794 // If we got a simplified select_cc node back from SimplifySelectCC, then
4795 // break it down into a new SETCC node, and a new SELECT node, and then return
4796 // the SELECT node, since we were called with a SELECT node.
4797 if (SCC.Val) {
4798 // Check to see if we got a select_cc back (to turn into setcc/select).
4799 // Otherwise, just return whatever node we got back, like fabs.
4800 if (SCC.getOpcode() == ISD::SELECT_CC) {
4801 SDOperand SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
4802 SCC.getOperand(0), SCC.getOperand(1),
4803 SCC.getOperand(4));
Chris Lattner5750df92006-03-01 04:03:14 +00004804 AddToWorkList(SETCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004805 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
4806 SCC.getOperand(3), SETCC);
4807 }
4808 return SCC;
4809 }
Nate Begeman44728a72005-09-19 22:34:01 +00004810 return SDOperand();
4811}
4812
Chris Lattner40c62d52005-10-18 06:04:22 +00004813/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
4814/// are the two values being selected between, see if we can simplify the
Chris Lattner729c6d12006-05-27 00:43:02 +00004815/// select. Callers of this should assume that TheSelect is deleted if this
4816/// returns true. As such, they should return the appropriate thing (e.g. the
4817/// node) back to the top-level of the DAG combiner loop to avoid it being
4818/// looked at.
Chris Lattner40c62d52005-10-18 06:04:22 +00004819///
4820bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDOperand LHS,
4821 SDOperand RHS) {
4822
4823 // If this is a select from two identical things, try to pull the operation
4824 // through the select.
4825 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
Chris Lattner40c62d52005-10-18 06:04:22 +00004826 // If this is a load and the token chain is identical, replace the select
4827 // of two loads with a load through a select of the address to load from.
4828 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
4829 // constants have been dropped into the constant pool.
Evan Cheng466685d2006-10-09 20:57:25 +00004830 if (LHS.getOpcode() == ISD::LOAD &&
Chris Lattner40c62d52005-10-18 06:04:22 +00004831 // Token chains must be identical.
Evan Cheng466685d2006-10-09 20:57:25 +00004832 LHS.getOperand(0) == RHS.getOperand(0)) {
4833 LoadSDNode *LLD = cast<LoadSDNode>(LHS);
4834 LoadSDNode *RLD = cast<LoadSDNode>(RHS);
4835
4836 // If this is an EXTLOAD, the VT's must match.
Evan Cheng2e49f092006-10-11 07:10:22 +00004837 if (LLD->getLoadedVT() == RLD->getLoadedVT()) {
Evan Cheng466685d2006-10-09 20:57:25 +00004838 // FIXME: this conflates two src values, discarding one. This is not
4839 // the right thing to do, but nothing uses srcvalues now. When they do,
4840 // turn SrcValue into a list of locations.
4841 SDOperand Addr;
Chris Lattnerc4e664b2007-01-16 05:59:59 +00004842 if (TheSelect->getOpcode() == ISD::SELECT) {
4843 // Check that the condition doesn't reach either load. If so, folding
4844 // this will induce a cycle into the DAG.
4845 if (!LLD->isPredecessor(TheSelect->getOperand(0).Val) &&
4846 !RLD->isPredecessor(TheSelect->getOperand(0).Val)) {
4847 Addr = DAG.getNode(ISD::SELECT, LLD->getBasePtr().getValueType(),
4848 TheSelect->getOperand(0), LLD->getBasePtr(),
4849 RLD->getBasePtr());
4850 }
4851 } else {
4852 // Check that the condition doesn't reach either load. If so, folding
4853 // this will induce a cycle into the DAG.
4854 if (!LLD->isPredecessor(TheSelect->getOperand(0).Val) &&
4855 !RLD->isPredecessor(TheSelect->getOperand(0).Val) &&
4856 !LLD->isPredecessor(TheSelect->getOperand(1).Val) &&
4857 !RLD->isPredecessor(TheSelect->getOperand(1).Val)) {
4858 Addr = DAG.getNode(ISD::SELECT_CC, LLD->getBasePtr().getValueType(),
Evan Cheng466685d2006-10-09 20:57:25 +00004859 TheSelect->getOperand(0),
4860 TheSelect->getOperand(1),
4861 LLD->getBasePtr(), RLD->getBasePtr(),
4862 TheSelect->getOperand(4));
Chris Lattnerc4e664b2007-01-16 05:59:59 +00004863 }
Evan Cheng466685d2006-10-09 20:57:25 +00004864 }
Chris Lattnerc4e664b2007-01-16 05:59:59 +00004865
4866 if (Addr.Val) {
4867 SDOperand Load;
4868 if (LLD->getExtensionType() == ISD::NON_EXTLOAD)
4869 Load = DAG.getLoad(TheSelect->getValueType(0), LLD->getChain(),
4870 Addr,LLD->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00004871 LLD->getSrcValueOffset(),
4872 LLD->isVolatile(),
4873 LLD->getAlignment());
Chris Lattnerc4e664b2007-01-16 05:59:59 +00004874 else {
4875 Load = DAG.getExtLoad(LLD->getExtensionType(),
4876 TheSelect->getValueType(0),
4877 LLD->getChain(), Addr, LLD->getSrcValue(),
4878 LLD->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00004879 LLD->getLoadedVT(),
4880 LLD->isVolatile(),
4881 LLD->getAlignment());
Chris Lattnerc4e664b2007-01-16 05:59:59 +00004882 }
4883 // Users of the select now use the result of the load.
4884 CombineTo(TheSelect, Load);
4885
4886 // Users of the old loads now use the new load's chain. We know the
4887 // old-load value is dead now.
4888 CombineTo(LHS.Val, Load.getValue(0), Load.getValue(1));
4889 CombineTo(RHS.Val, Load.getValue(0), Load.getValue(1));
4890 return true;
4891 }
Evan Chengc5484282006-10-04 00:56:09 +00004892 }
Chris Lattner40c62d52005-10-18 06:04:22 +00004893 }
4894 }
4895
4896 return false;
4897}
4898
Nate Begeman44728a72005-09-19 22:34:01 +00004899SDOperand DAGCombiner::SimplifySelectCC(SDOperand N0, SDOperand N1,
4900 SDOperand N2, SDOperand N3,
Chris Lattner1eba01e2007-04-11 06:50:51 +00004901 ISD::CondCode CC, bool NotExtCompare) {
Nate Begemanf845b452005-10-08 00:29:44 +00004902
4903 MVT::ValueType VT = N2.getValueType();
Nate Begemanf845b452005-10-08 00:29:44 +00004904 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
4905 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
4906 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
4907
4908 // Determine if the condition we're dealing with is constant
4909 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
Chris Lattner30f73e72006-10-14 03:52:46 +00004910 if (SCC.Val) AddToWorkList(SCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004911 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
4912
4913 // fold select_cc true, x, y -> x
4914 if (SCCC && SCCC->getValue())
4915 return N2;
4916 // fold select_cc false, x, y -> y
4917 if (SCCC && SCCC->getValue() == 0)
4918 return N3;
4919
4920 // Check to see if we can simplify the select into an fabs node
4921 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
4922 // Allow either -0.0 or 0.0
Dale Johannesen87503a62007-08-25 22:10:57 +00004923 if (CFP->getValueAPF().isZero()) {
Nate Begemanf845b452005-10-08 00:29:44 +00004924 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
4925 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
4926 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
4927 N2 == N3.getOperand(0))
4928 return DAG.getNode(ISD::FABS, VT, N0);
4929
4930 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
4931 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
4932 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
4933 N2.getOperand(0) == N3)
4934 return DAG.getNode(ISD::FABS, VT, N3);
4935 }
4936 }
4937
4938 // Check to see if we can perform the "gzip trick", transforming
4939 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
Chris Lattnere3152e52006-09-20 06:41:35 +00004940 if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT &&
Nate Begemanf845b452005-10-08 00:29:44 +00004941 MVT::isInteger(N0.getValueType()) &&
Chris Lattnere3152e52006-09-20 06:41:35 +00004942 MVT::isInteger(N2.getValueType()) &&
4943 (N1C->isNullValue() || // (a < 0) ? b : 0
4944 (N1C->getValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0
Nate Begemanf845b452005-10-08 00:29:44 +00004945 MVT::ValueType XType = N0.getValueType();
4946 MVT::ValueType AType = N2.getValueType();
4947 if (XType >= AType) {
4948 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00004949 // single-bit constant.
Nate Begemanf845b452005-10-08 00:29:44 +00004950 if (N2C && ((N2C->getValue() & (N2C->getValue()-1)) == 0)) {
4951 unsigned ShCtV = Log2_64(N2C->getValue());
4952 ShCtV = MVT::getSizeInBits(XType)-ShCtV-1;
4953 SDOperand ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
4954 SDOperand Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
Chris Lattner5750df92006-03-01 04:03:14 +00004955 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004956 if (XType > AType) {
4957 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00004958 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004959 }
4960 return DAG.getNode(ISD::AND, AType, Shift, N2);
4961 }
4962 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
4963 DAG.getConstant(MVT::getSizeInBits(XType)-1,
4964 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00004965 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004966 if (XType > AType) {
4967 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00004968 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004969 }
4970 return DAG.getNode(ISD::AND, AType, Shift, N2);
4971 }
4972 }
Nate Begeman07ed4172005-10-10 21:26:48 +00004973
4974 // fold select C, 16, 0 -> shl C, 4
4975 if (N2C && N3C && N3C->isNullValue() && isPowerOf2_64(N2C->getValue()) &&
4976 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) {
Chris Lattner1eba01e2007-04-11 06:50:51 +00004977
4978 // If the caller doesn't want us to simplify this into a zext of a compare,
4979 // don't do it.
4980 if (NotExtCompare && N2C->getValue() == 1)
4981 return SDOperand();
4982
Nate Begeman07ed4172005-10-10 21:26:48 +00004983 // Get a SetCC of the condition
4984 // FIXME: Should probably make sure that setcc is legal if we ever have a
4985 // target where it isn't.
Nate Begemanb0d04a72006-02-18 02:40:58 +00004986 SDOperand Temp, SCC;
Nate Begeman07ed4172005-10-10 21:26:48 +00004987 // cast from setcc result type to select result type
Nate Begemanb0d04a72006-02-18 02:40:58 +00004988 if (AfterLegalize) {
4989 SCC = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
Chris Lattner555d8d62006-12-07 22:36:47 +00004990 if (N2.getValueType() < SCC.getValueType())
4991 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
4992 else
4993 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00004994 } else {
4995 SCC = DAG.getSetCC(MVT::i1, N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00004996 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00004997 }
Chris Lattner5750df92006-03-01 04:03:14 +00004998 AddToWorkList(SCC.Val);
4999 AddToWorkList(Temp.Val);
Chris Lattnerc56a81d2007-04-11 06:43:25 +00005000
5001 if (N2C->getValue() == 1)
5002 return Temp;
Nate Begeman07ed4172005-10-10 21:26:48 +00005003 // shl setcc result by log2 n2c
5004 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
5005 DAG.getConstant(Log2_64(N2C->getValue()),
5006 TLI.getShiftAmountTy()));
5007 }
5008
Nate Begemanf845b452005-10-08 00:29:44 +00005009 // Check to see if this is the equivalent of setcc
5010 // FIXME: Turn all of these into setcc if setcc if setcc is legal
5011 // otherwise, go ahead with the folds.
5012 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getValue() == 1ULL)) {
5013 MVT::ValueType XType = N0.getValueType();
5014 if (TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultTy())) {
5015 SDOperand Res = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
5016 if (Res.getValueType() != VT)
5017 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
5018 return Res;
5019 }
5020
5021 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
5022 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
5023 TLI.isOperationLegal(ISD::CTLZ, XType)) {
5024 SDOperand Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
5025 return DAG.getNode(ISD::SRL, XType, Ctlz,
5026 DAG.getConstant(Log2_32(MVT::getSizeInBits(XType)),
5027 TLI.getShiftAmountTy()));
5028 }
5029 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
5030 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
5031 SDOperand NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
5032 N0);
5033 SDOperand NotN0 = DAG.getNode(ISD::XOR, XType, N0,
5034 DAG.getConstant(~0ULL, XType));
5035 return DAG.getNode(ISD::SRL, XType,
5036 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
5037 DAG.getConstant(MVT::getSizeInBits(XType)-1,
5038 TLI.getShiftAmountTy()));
5039 }
5040 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
5041 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
5042 SDOperand Sign = DAG.getNode(ISD::SRL, XType, N0,
5043 DAG.getConstant(MVT::getSizeInBits(XType)-1,
5044 TLI.getShiftAmountTy()));
5045 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
5046 }
5047 }
5048
5049 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
5050 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
5051 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
Chris Lattner1982ef22007-04-11 05:11:38 +00005052 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1) &&
5053 N2.getOperand(0) == N1 && MVT::isInteger(N0.getValueType())) {
5054 MVT::ValueType XType = N0.getValueType();
5055 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
5056 DAG.getConstant(MVT::getSizeInBits(XType)-1,
5057 TLI.getShiftAmountTy()));
5058 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
5059 AddToWorkList(Shift.Val);
5060 AddToWorkList(Add.Val);
5061 return DAG.getNode(ISD::XOR, XType, Add, Shift);
5062 }
5063 // Check to see if this is an integer abs. select_cc setgt X, -1, X, -X ->
5064 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
5065 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT &&
5066 N0 == N2 && N3.getOpcode() == ISD::SUB && N0 == N3.getOperand(1)) {
5067 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N3.getOperand(0))) {
Nate Begemanf845b452005-10-08 00:29:44 +00005068 MVT::ValueType XType = N0.getValueType();
5069 if (SubC->isNullValue() && MVT::isInteger(XType)) {
5070 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
5071 DAG.getConstant(MVT::getSizeInBits(XType)-1,
Chris Lattner1982ef22007-04-11 05:11:38 +00005072 TLI.getShiftAmountTy()));
Nate Begemanf845b452005-10-08 00:29:44 +00005073 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00005074 AddToWorkList(Shift.Val);
5075 AddToWorkList(Add.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005076 return DAG.getNode(ISD::XOR, XType, Add, Shift);
5077 }
5078 }
5079 }
Chris Lattner1982ef22007-04-11 05:11:38 +00005080
Nate Begeman44728a72005-09-19 22:34:01 +00005081 return SDOperand();
5082}
5083
Evan Chengfa1eb272007-02-08 22:13:59 +00005084/// SimplifySetCC - This is a stub for TargetLowering::SimplifySetCC.
Nate Begeman452d7be2005-09-16 00:54:12 +00005085SDOperand DAGCombiner::SimplifySetCC(MVT::ValueType VT, SDOperand N0,
Nate Begemane17daeb2005-10-05 21:43:42 +00005086 SDOperand N1, ISD::CondCode Cond,
5087 bool foldBooleans) {
Evan Chengfa1eb272007-02-08 22:13:59 +00005088 TargetLowering::DAGCombinerInfo
5089 DagCombineInfo(DAG, !AfterLegalize, false, this);
5090 return TLI.SimplifySetCC(VT, N0, N1, Cond, foldBooleans, DagCombineInfo);
Nate Begeman452d7be2005-09-16 00:54:12 +00005091}
5092
Nate Begeman69575232005-10-20 02:15:44 +00005093/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
5094/// return a DAG expression to select that will generate the same value by
5095/// multiplying by a magic number. See:
5096/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
5097SDOperand DAGCombiner::BuildSDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00005098 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005099 SDOperand S = TLI.BuildSDIV(N, DAG, &Built);
5100
Andrew Lenharth232c9102006-06-12 16:07:18 +00005101 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005102 ii != ee; ++ii)
5103 AddToWorkList(*ii);
5104 return S;
Nate Begeman69575232005-10-20 02:15:44 +00005105}
5106
5107/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
5108/// return a DAG expression to select that will generate the same value by
5109/// multiplying by a magic number. See:
5110/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
5111SDOperand DAGCombiner::BuildUDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00005112 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005113 SDOperand S = TLI.BuildUDIV(N, DAG, &Built);
Nate Begeman69575232005-10-20 02:15:44 +00005114
Andrew Lenharth232c9102006-06-12 16:07:18 +00005115 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005116 ii != ee; ++ii)
5117 AddToWorkList(*ii);
5118 return S;
Nate Begeman69575232005-10-20 02:15:44 +00005119}
5120
Jim Laskey71382342006-10-07 23:37:56 +00005121/// FindBaseOffset - Return true if base is known not to alias with anything
5122/// but itself. Provides base object and offset as results.
5123static bool FindBaseOffset(SDOperand Ptr, SDOperand &Base, int64_t &Offset) {
5124 // Assume it is a primitive operation.
5125 Base = Ptr; Offset = 0;
5126
5127 // If it's an adding a simple constant then integrate the offset.
5128 if (Base.getOpcode() == ISD::ADD) {
5129 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Base.getOperand(1))) {
5130 Base = Base.getOperand(0);
5131 Offset += C->getValue();
5132 }
5133 }
5134
5135 // If it's any of the following then it can't alias with anything but itself.
5136 return isa<FrameIndexSDNode>(Base) ||
5137 isa<ConstantPoolSDNode>(Base) ||
5138 isa<GlobalAddressSDNode>(Base);
5139}
5140
5141/// isAlias - Return true if there is any possibility that the two addresses
5142/// overlap.
Jim Laskey096c22e2006-10-18 12:29:57 +00005143bool DAGCombiner::isAlias(SDOperand Ptr1, int64_t Size1,
5144 const Value *SrcValue1, int SrcValueOffset1,
5145 SDOperand Ptr2, int64_t Size2,
5146 const Value *SrcValue2, int SrcValueOffset2)
5147{
Jim Laskey71382342006-10-07 23:37:56 +00005148 // If they are the same then they must be aliases.
5149 if (Ptr1 == Ptr2) return true;
5150
5151 // Gather base node and offset information.
5152 SDOperand Base1, Base2;
5153 int64_t Offset1, Offset2;
5154 bool KnownBase1 = FindBaseOffset(Ptr1, Base1, Offset1);
5155 bool KnownBase2 = FindBaseOffset(Ptr2, Base2, Offset2);
5156
5157 // If they have a same base address then...
5158 if (Base1 == Base2) {
5159 // Check to see if the addresses overlap.
5160 return!((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
5161 }
5162
Jim Laskey096c22e2006-10-18 12:29:57 +00005163 // If we know both bases then they can't alias.
5164 if (KnownBase1 && KnownBase2) return false;
5165
Jim Laskey07a27092006-10-18 19:08:31 +00005166 if (CombinerGlobalAA) {
5167 // Use alias analysis information.
Dan Gohmane9c8fa02007-08-27 16:32:11 +00005168 int64_t MinOffset = std::min(SrcValueOffset1, SrcValueOffset2);
5169 int64_t Overlap1 = Size1 + SrcValueOffset1 - MinOffset;
5170 int64_t Overlap2 = Size2 + SrcValueOffset2 - MinOffset;
Jim Laskey07a27092006-10-18 19:08:31 +00005171 AliasAnalysis::AliasResult AAResult =
Jim Laskey096c22e2006-10-18 12:29:57 +00005172 AA.alias(SrcValue1, Overlap1, SrcValue2, Overlap2);
Jim Laskey07a27092006-10-18 19:08:31 +00005173 if (AAResult == AliasAnalysis::NoAlias)
5174 return false;
5175 }
Jim Laskey096c22e2006-10-18 12:29:57 +00005176
5177 // Otherwise we have to assume they alias.
5178 return true;
Jim Laskey71382342006-10-07 23:37:56 +00005179}
5180
5181/// FindAliasInfo - Extracts the relevant alias information from the memory
5182/// node. Returns true if the operand was a load.
Jim Laskey7ca56af2006-10-11 13:47:09 +00005183bool DAGCombiner::FindAliasInfo(SDNode *N,
Jim Laskey096c22e2006-10-18 12:29:57 +00005184 SDOperand &Ptr, int64_t &Size,
5185 const Value *&SrcValue, int &SrcValueOffset) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00005186 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
5187 Ptr = LD->getBasePtr();
Jim Laskeyc2b19f32006-10-11 17:47:52 +00005188 Size = MVT::getSizeInBits(LD->getLoadedVT()) >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005189 SrcValue = LD->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00005190 SrcValueOffset = LD->getSrcValueOffset();
Jim Laskey71382342006-10-07 23:37:56 +00005191 return true;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005192 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00005193 Ptr = ST->getBasePtr();
Evan Cheng8b2794a2006-10-13 21:14:26 +00005194 Size = MVT::getSizeInBits(ST->getStoredVT()) >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005195 SrcValue = ST->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00005196 SrcValueOffset = ST->getSrcValueOffset();
Jim Laskey7ca56af2006-10-11 13:47:09 +00005197 } else {
Jim Laskey71382342006-10-07 23:37:56 +00005198 assert(0 && "FindAliasInfo expected a memory operand");
Jim Laskey71382342006-10-07 23:37:56 +00005199 }
5200
5201 return false;
5202}
5203
Jim Laskey6ff23e52006-10-04 16:53:27 +00005204/// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
5205/// looking for aliasing nodes and adding them to the Aliases vector.
Jim Laskeybc588b82006-10-05 15:07:25 +00005206void DAGCombiner::GatherAllAliases(SDNode *N, SDOperand OriginalChain,
Jim Laskey6ff23e52006-10-04 16:53:27 +00005207 SmallVector<SDOperand, 8> &Aliases) {
Jim Laskeybc588b82006-10-05 15:07:25 +00005208 SmallVector<SDOperand, 8> Chains; // List of chains to visit.
Jim Laskey6ff23e52006-10-04 16:53:27 +00005209 std::set<SDNode *> Visited; // Visited node set.
5210
Jim Laskey279f0532006-09-25 16:29:54 +00005211 // Get alias information for node.
5212 SDOperand Ptr;
5213 int64_t Size;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005214 const Value *SrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00005215 int SrcValueOffset;
5216 bool IsLoad = FindAliasInfo(N, Ptr, Size, SrcValue, SrcValueOffset);
Jim Laskey279f0532006-09-25 16:29:54 +00005217
Jim Laskey6ff23e52006-10-04 16:53:27 +00005218 // Starting off.
Jim Laskeybc588b82006-10-05 15:07:25 +00005219 Chains.push_back(OriginalChain);
Jim Laskey6ff23e52006-10-04 16:53:27 +00005220
Jim Laskeybc588b82006-10-05 15:07:25 +00005221 // Look at each chain and determine if it is an alias. If so, add it to the
5222 // aliases list. If not, then continue up the chain looking for the next
5223 // candidate.
5224 while (!Chains.empty()) {
5225 SDOperand Chain = Chains.back();
5226 Chains.pop_back();
Jim Laskey6ff23e52006-10-04 16:53:27 +00005227
Jim Laskeybc588b82006-10-05 15:07:25 +00005228 // Don't bother if we've been before.
5229 if (Visited.find(Chain.Val) != Visited.end()) continue;
5230 Visited.insert(Chain.Val);
5231
5232 switch (Chain.getOpcode()) {
5233 case ISD::EntryToken:
5234 // Entry token is ideal chain operand, but handled in FindBetterChain.
5235 break;
Jim Laskey6ff23e52006-10-04 16:53:27 +00005236
Jim Laskeybc588b82006-10-05 15:07:25 +00005237 case ISD::LOAD:
5238 case ISD::STORE: {
5239 // Get alias information for Chain.
5240 SDOperand OpPtr;
5241 int64_t OpSize;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005242 const Value *OpSrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00005243 int OpSrcValueOffset;
5244 bool IsOpLoad = FindAliasInfo(Chain.Val, OpPtr, OpSize,
5245 OpSrcValue, OpSrcValueOffset);
Jim Laskeybc588b82006-10-05 15:07:25 +00005246
5247 // If chain is alias then stop here.
5248 if (!(IsLoad && IsOpLoad) &&
Jim Laskey096c22e2006-10-18 12:29:57 +00005249 isAlias(Ptr, Size, SrcValue, SrcValueOffset,
5250 OpPtr, OpSize, OpSrcValue, OpSrcValueOffset)) {
Jim Laskeybc588b82006-10-05 15:07:25 +00005251 Aliases.push_back(Chain);
5252 } else {
5253 // Look further up the chain.
5254 Chains.push_back(Chain.getOperand(0));
5255 // Clean up old chain.
5256 AddToWorkList(Chain.Val);
Jim Laskey279f0532006-09-25 16:29:54 +00005257 }
Jim Laskeybc588b82006-10-05 15:07:25 +00005258 break;
5259 }
5260
5261 case ISD::TokenFactor:
5262 // We have to check each of the operands of the token factor, so we queue
5263 // then up. Adding the operands to the queue (stack) in reverse order
5264 // maintains the original order and increases the likelihood that getNode
5265 // will find a matching token factor (CSE.)
5266 for (unsigned n = Chain.getNumOperands(); n;)
5267 Chains.push_back(Chain.getOperand(--n));
5268 // Eliminate the token factor if we can.
5269 AddToWorkList(Chain.Val);
5270 break;
5271
5272 default:
5273 // For all other instructions we will just have to take what we can get.
5274 Aliases.push_back(Chain);
5275 break;
Jim Laskey279f0532006-09-25 16:29:54 +00005276 }
5277 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00005278}
5279
5280/// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, looking
5281/// for a better chain (aliasing node.)
5282SDOperand DAGCombiner::FindBetterChain(SDNode *N, SDOperand OldChain) {
5283 SmallVector<SDOperand, 8> Aliases; // Ops for replacing token factor.
Jim Laskey279f0532006-09-25 16:29:54 +00005284
Jim Laskey6ff23e52006-10-04 16:53:27 +00005285 // Accumulate all the aliases to this node.
5286 GatherAllAliases(N, OldChain, Aliases);
5287
5288 if (Aliases.size() == 0) {
5289 // If no operands then chain to entry token.
5290 return DAG.getEntryNode();
5291 } else if (Aliases.size() == 1) {
5292 // If a single operand then chain to it. We don't need to revisit it.
5293 return Aliases[0];
5294 }
5295
5296 // Construct a custom tailored token factor.
5297 SDOperand NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
5298 &Aliases[0], Aliases.size());
5299
5300 // Make sure the old chain gets cleaned up.
5301 if (NewChain != OldChain) AddToWorkList(OldChain.Val);
5302
5303 return NewChain;
Jim Laskey279f0532006-09-25 16:29:54 +00005304}
5305
Nate Begeman1d4d4142005-09-01 00:19:25 +00005306// SelectionDAG::Combine - This is the entry point for the file.
5307//
Jim Laskeyc7c3f112006-10-16 20:52:31 +00005308void SelectionDAG::Combine(bool RunningAfterLegalize, AliasAnalysis &AA) {
Chris Lattner938ab022007-01-16 04:55:25 +00005309 if (!RunningAfterLegalize && ViewDAGCombine1)
5310 viewGraph();
5311 if (RunningAfterLegalize && ViewDAGCombine2)
5312 viewGraph();
Nate Begeman1d4d4142005-09-01 00:19:25 +00005313 /// run - This is the main entry point to this class.
5314 ///
Jim Laskeyc7c3f112006-10-16 20:52:31 +00005315 DAGCombiner(*this, AA).Run(RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +00005316}