blob: 57e7a04d2ff1146e661dcb9ad732e327c8e2fda1 [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
Nate Begeman83e75ec2005-09-06 04:43:02 +0000585 if (RV.Val) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000586 ++NodesCombined;
Nate Begeman646d7e22005-09-02 21:18:40 +0000587 // If we get back the same node we passed in, rather than a new node or
588 // zero, we know that the node must have defined multiple values and
589 // CombineTo was used. Since CombineTo takes care of the worklist
590 // mechanics for us, we have no work to do in this case.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000591 if (RV.Val != N) {
Chris Lattner729c6d12006-05-27 00:43:02 +0000592 assert(N->getOpcode() != ISD::DELETED_NODE &&
593 RV.Val->getOpcode() != ISD::DELETED_NODE &&
594 "Node was deleted but visit returned new node!");
595
Dan Gohmanb5bec2b2007-06-19 14:13:56 +0000596 DOUT << "\nReplacing.3 "; DEBUG(N->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +0000597 DOUT << "\nWith: "; DEBUG(RV.Val->dump(&DAG));
598 DOUT << '\n';
Chris Lattner01a22022005-10-10 22:04:48 +0000599 std::vector<SDNode*> NowDead;
Evan Cheng2adffa12006-09-21 19:04:05 +0000600 if (N->getNumValues() == RV.Val->getNumValues())
601 DAG.ReplaceAllUsesWith(N, RV.Val, &NowDead);
602 else {
603 assert(N->getValueType(0) == RV.getValueType() && "Type mismatch");
604 SDOperand OpV = RV;
605 DAG.ReplaceAllUsesWith(N, &OpV, &NowDead);
606 }
Nate Begeman646d7e22005-09-02 21:18:40 +0000607
608 // Push the new node and any users onto the worklist
Jim Laskey6ff23e52006-10-04 16:53:27 +0000609 AddToWorkList(RV.Val);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000610 AddUsersToWorkList(RV.Val);
Chris Lattner20e3d862008-01-24 07:18:21 +0000611
Chris Lattner23e202d2008-01-24 17:10:01 +0000612 // Add any uses of the old node to the worklist in case this node is the
613 // last one that uses them. They may become dead after this node is
614 // deleted.
Chris Lattner20e3d862008-01-24 07:18:21 +0000615 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
616 AddToWorkList(N->getOperand(i).Val);
Nate Begeman646d7e22005-09-02 21:18:40 +0000617
Jim Laskey6ff23e52006-10-04 16:53:27 +0000618 // Nodes can be reintroduced into the worklist. Make sure we do not
619 // process a node that has been replaced.
Nate Begeman646d7e22005-09-02 21:18:40 +0000620 removeFromWorkList(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000621 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
622 removeFromWorkList(NowDead[i]);
Chris Lattner5c46f742005-10-05 06:11:08 +0000623
624 // Finally, since the node is now dead, remove it from the graph.
625 DAG.DeleteNode(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000626 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000627 }
628 }
Chris Lattner95038592005-10-05 06:35:28 +0000629
630 // If the root changed (e.g. it was a dead load, update the root).
631 DAG.setRoot(Dummy.getValue());
Nate Begeman1d4d4142005-09-01 00:19:25 +0000632}
633
Nate Begeman83e75ec2005-09-06 04:43:02 +0000634SDOperand DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000635 switch(N->getOpcode()) {
636 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +0000637 case ISD::TokenFactor: return visitTokenFactor(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000638 case ISD::ADD: return visitADD(N);
639 case ISD::SUB: return visitSUB(N);
Chris Lattner91153682007-03-04 20:03:15 +0000640 case ISD::ADDC: return visitADDC(N);
641 case ISD::ADDE: return visitADDE(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000642 case ISD::MUL: return visitMUL(N);
643 case ISD::SDIV: return visitSDIV(N);
644 case ISD::UDIV: return visitUDIV(N);
645 case ISD::SREM: return visitSREM(N);
646 case ISD::UREM: return visitUREM(N);
647 case ISD::MULHU: return visitMULHU(N);
648 case ISD::MULHS: return visitMULHS(N);
Dan Gohman389079b2007-10-08 17:57:15 +0000649 case ISD::SMUL_LOHI: return visitSMUL_LOHI(N);
650 case ISD::UMUL_LOHI: return visitUMUL_LOHI(N);
651 case ISD::SDIVREM: return visitSDIVREM(N);
652 case ISD::UDIVREM: return visitUDIVREM(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000653 case ISD::AND: return visitAND(N);
654 case ISD::OR: return visitOR(N);
655 case ISD::XOR: return visitXOR(N);
656 case ISD::SHL: return visitSHL(N);
657 case ISD::SRA: return visitSRA(N);
658 case ISD::SRL: return visitSRL(N);
659 case ISD::CTLZ: return visitCTLZ(N);
660 case ISD::CTTZ: return visitCTTZ(N);
661 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000662 case ISD::SELECT: return visitSELECT(N);
663 case ISD::SELECT_CC: return visitSELECT_CC(N);
664 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000665 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
666 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000667 case ISD::ANY_EXTEND: return visitANY_EXTEND(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000668 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
669 case ISD::TRUNCATE: return visitTRUNCATE(N);
Chris Lattner94683772005-12-23 05:30:37 +0000670 case ISD::BIT_CONVERT: return visitBIT_CONVERT(N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000671 case ISD::FADD: return visitFADD(N);
672 case ISD::FSUB: return visitFSUB(N);
673 case ISD::FMUL: return visitFMUL(N);
674 case ISD::FDIV: return visitFDIV(N);
675 case ISD::FREM: return visitFREM(N);
Chris Lattner12d83032006-03-05 05:30:57 +0000676 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000677 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
678 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
679 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
680 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
681 case ISD::FP_ROUND: return visitFP_ROUND(N);
682 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
683 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
684 case ISD::FNEG: return visitFNEG(N);
685 case ISD::FABS: return visitFABS(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000686 case ISD::BRCOND: return visitBRCOND(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000687 case ISD::BR_CC: return visitBR_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000688 case ISD::LOAD: return visitLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000689 case ISD::STORE: return visitSTORE(N);
Chris Lattnerca242442006-03-19 01:27:56 +0000690 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
Evan Cheng513da432007-10-06 08:19:55 +0000691 case ISD::EXTRACT_VECTOR_ELT: return visitEXTRACT_VECTOR_ELT(N);
Dan Gohman7f321562007-06-25 16:23:39 +0000692 case ISD::BUILD_VECTOR: return visitBUILD_VECTOR(N);
693 case ISD::CONCAT_VECTORS: return visitCONCAT_VECTORS(N);
Chris Lattner66445d32006-03-28 22:11:53 +0000694 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000695 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000696 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000697}
698
Dan Gohman389079b2007-10-08 17:57:15 +0000699SDOperand DAGCombiner::combine(SDNode *N) {
700
701 SDOperand RV = visit(N);
702
703 // If nothing happened, try a target-specific DAG combine.
704 if (RV.Val == 0) {
705 assert(N->getOpcode() != ISD::DELETED_NODE &&
706 "Node was deleted but visit returned NULL!");
707
708 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
709 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode())) {
710
711 // Expose the DAG combiner to the target combiner impls.
712 TargetLowering::DAGCombinerInfo
713 DagCombineInfo(DAG, !AfterLegalize, false, this);
714
715 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
716 }
717 }
718
719 return RV;
720}
721
Chris Lattner6270f682006-10-08 22:57:01 +0000722/// getInputChainForNode - Given a node, return its input chain if it has one,
723/// otherwise return a null sd operand.
724static SDOperand getInputChainForNode(SDNode *N) {
725 if (unsigned NumOps = N->getNumOperands()) {
726 if (N->getOperand(0).getValueType() == MVT::Other)
727 return N->getOperand(0);
728 else if (N->getOperand(NumOps-1).getValueType() == MVT::Other)
729 return N->getOperand(NumOps-1);
730 for (unsigned i = 1; i < NumOps-1; ++i)
731 if (N->getOperand(i).getValueType() == MVT::Other)
732 return N->getOperand(i);
733 }
734 return SDOperand(0, 0);
735}
736
Nate Begeman83e75ec2005-09-06 04:43:02 +0000737SDOperand DAGCombiner::visitTokenFactor(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +0000738 // If N has two operands, where one has an input chain equal to the other,
739 // the 'other' chain is redundant.
740 if (N->getNumOperands() == 2) {
741 if (getInputChainForNode(N->getOperand(0).Val) == N->getOperand(1))
742 return N->getOperand(0);
743 if (getInputChainForNode(N->getOperand(1).Val) == N->getOperand(0))
744 return N->getOperand(1);
745 }
746
Chris Lattnerc76d4412007-05-16 06:37:59 +0000747 SmallVector<SDNode *, 8> TFs; // List of token factors to visit.
748 SmallVector<SDOperand, 8> Ops; // Ops for replacing token factor.
749 SmallPtrSet<SDNode*, 16> SeenOps;
750 bool Changed = false; // If we should replace this token factor.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000751
752 // Start out with this token factor.
Jim Laskey279f0532006-09-25 16:29:54 +0000753 TFs.push_back(N);
Jim Laskey279f0532006-09-25 16:29:54 +0000754
Jim Laskey71382342006-10-07 23:37:56 +0000755 // Iterate through token factors. The TFs grows when new token factors are
Jim Laskeybc588b82006-10-05 15:07:25 +0000756 // encountered.
757 for (unsigned i = 0; i < TFs.size(); ++i) {
758 SDNode *TF = TFs[i];
759
Jim Laskey6ff23e52006-10-04 16:53:27 +0000760 // Check each of the operands.
761 for (unsigned i = 0, ie = TF->getNumOperands(); i != ie; ++i) {
762 SDOperand Op = TF->getOperand(i);
Jim Laskey279f0532006-09-25 16:29:54 +0000763
Jim Laskey6ff23e52006-10-04 16:53:27 +0000764 switch (Op.getOpcode()) {
765 case ISD::EntryToken:
Jim Laskeybc588b82006-10-05 15:07:25 +0000766 // Entry tokens don't need to be added to the list. They are
767 // rededundant.
768 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000769 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000770
Jim Laskey6ff23e52006-10-04 16:53:27 +0000771 case ISD::TokenFactor:
Jim Laskeybc588b82006-10-05 15:07:25 +0000772 if ((CombinerAA || Op.hasOneUse()) &&
773 std::find(TFs.begin(), TFs.end(), Op.Val) == TFs.end()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +0000774 // Queue up for processing.
775 TFs.push_back(Op.Val);
776 // Clean up in case the token factor is removed.
777 AddToWorkList(Op.Val);
778 Changed = true;
779 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000780 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000781 // Fall thru
782
783 default:
Chris Lattnerc76d4412007-05-16 06:37:59 +0000784 // Only add if it isn't already in the list.
785 if (SeenOps.insert(Op.Val))
Jim Laskeybc588b82006-10-05 15:07:25 +0000786 Ops.push_back(Op);
Chris Lattnerc76d4412007-05-16 06:37:59 +0000787 else
788 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000789 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000790 }
791 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000792 }
793
794 SDOperand Result;
795
796 // If we've change things around then replace token factor.
797 if (Changed) {
798 if (Ops.size() == 0) {
799 // The entry token is the only possible outcome.
800 Result = DAG.getEntryNode();
801 } else {
802 // New and improved token factor.
803 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0], Ops.size());
Nate Begemanded49632005-10-13 03:11:28 +0000804 }
Jim Laskey274062c2006-10-13 23:32:28 +0000805
806 // Don't add users to work list.
807 return CombineTo(N, Result, false);
Nate Begemanded49632005-10-13 03:11:28 +0000808 }
Jim Laskey279f0532006-09-25 16:29:54 +0000809
Jim Laskey6ff23e52006-10-04 16:53:27 +0000810 return Result;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000811}
812
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000813static
814SDOperand combineShlAddConstant(SDOperand N0, SDOperand N1, SelectionDAG &DAG) {
815 MVT::ValueType VT = N0.getValueType();
816 SDOperand N00 = N0.getOperand(0);
817 SDOperand N01 = N0.getOperand(1);
818 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N01);
819 if (N01C && N00.getOpcode() == ISD::ADD && N00.Val->hasOneUse() &&
820 isa<ConstantSDNode>(N00.getOperand(1))) {
821 N0 = DAG.getNode(ISD::ADD, VT,
822 DAG.getNode(ISD::SHL, VT, N00.getOperand(0), N01),
823 DAG.getNode(ISD::SHL, VT, N00.getOperand(1), N01));
824 return DAG.getNode(ISD::ADD, VT, N0, N1);
825 }
826 return SDOperand();
827}
828
Evan Chengb13cdbd2007-06-21 07:39:16 +0000829static
830SDOperand combineSelectAndUse(SDNode *N, SDOperand Slct, SDOperand OtherOp,
831 SelectionDAG &DAG) {
832 MVT::ValueType VT = N->getValueType(0);
833 unsigned Opc = N->getOpcode();
834 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
835 SDOperand LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
836 SDOperand RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
837 ISD::CondCode CC = ISD::SETCC_INVALID;
838 if (isSlctCC)
839 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
840 else {
841 SDOperand CCOp = Slct.getOperand(0);
842 if (CCOp.getOpcode() == ISD::SETCC)
843 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
844 }
845
846 bool DoXform = false;
847 bool InvCC = false;
848 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
849 "Bad input!");
850 if (LHS.getOpcode() == ISD::Constant &&
851 cast<ConstantSDNode>(LHS)->isNullValue())
852 DoXform = true;
853 else if (CC != ISD::SETCC_INVALID &&
854 RHS.getOpcode() == ISD::Constant &&
855 cast<ConstantSDNode>(RHS)->isNullValue()) {
856 std::swap(LHS, RHS);
Chris Lattner4626b252008-01-17 07:20:38 +0000857 SDOperand Op0 = Slct.getOperand(0);
858 bool isInt = MVT::isInteger(isSlctCC ? Op0.getValueType()
859 : Op0.getOperand(0).getValueType());
Evan Chengb13cdbd2007-06-21 07:39:16 +0000860 CC = ISD::getSetCCInverse(CC, isInt);
861 DoXform = true;
862 InvCC = true;
863 }
864
865 if (DoXform) {
866 SDOperand Result = DAG.getNode(Opc, VT, OtherOp, RHS);
867 if (isSlctCC)
868 return DAG.getSelectCC(OtherOp, Result,
869 Slct.getOperand(0), Slct.getOperand(1), CC);
870 SDOperand CCOp = Slct.getOperand(0);
871 if (InvCC)
872 CCOp = DAG.getSetCC(CCOp.getValueType(), CCOp.getOperand(0),
873 CCOp.getOperand(1), CC);
874 return DAG.getNode(ISD::SELECT, VT, CCOp, OtherOp, Result);
875 }
876 return SDOperand();
877}
878
Nate Begeman83e75ec2005-09-06 04:43:02 +0000879SDOperand DAGCombiner::visitADD(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000880 SDOperand N0 = N->getOperand(0);
881 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000882 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
883 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemanf89d78d2005-09-07 16:09:19 +0000884 MVT::ValueType VT = N0.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +0000885
886 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +0000887 if (MVT::isVector(VT)) {
888 SDOperand FoldedVOp = SimplifyVBinOp(N);
889 if (FoldedVOp.Val) return FoldedVOp;
890 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000891
Dan Gohman613e0d82007-07-03 14:03:57 +0000892 // fold (add x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +0000893 if (N0.getOpcode() == ISD::UNDEF)
894 return N0;
895 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +0000896 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000897 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000898 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000899 return DAG.getNode(ISD::ADD, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000900 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000901 if (N0C && !N1C)
902 return DAG.getNode(ISD::ADD, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000903 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000904 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000905 return N0;
Chris Lattner4aafb4f2006-01-12 20:22:43 +0000906 // fold ((c1-A)+c2) -> (c1+c2)-A
907 if (N1C && N0.getOpcode() == ISD::SUB)
908 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
909 return DAG.getNode(ISD::SUB, VT,
910 DAG.getConstant(N1C->getValue()+N0C->getValue(), VT),
911 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000912 // reassociate add
913 SDOperand RADD = ReassociateOps(ISD::ADD, N0, N1);
914 if (RADD.Val != 0)
915 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000916 // fold ((0-A) + B) -> B-A
917 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
918 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000919 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000920 // fold (A + (0-B)) -> A-B
921 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
922 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000923 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +0000924 // fold (A+(B-A)) -> B
925 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000926 return N1.getOperand(0);
Chris Lattner947c2892006-03-13 06:51:27 +0000927
Evan Cheng860771d2006-03-01 01:09:54 +0000928 if (!MVT::isVector(VT) && SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +0000929 return SDOperand(N, 0);
Chris Lattner947c2892006-03-13 06:51:27 +0000930
931 // fold (a+b) -> (a|b) iff a and b share no bits.
932 if (MVT::isInteger(VT) && !MVT::isVector(VT)) {
933 uint64_t LHSZero, LHSOne;
934 uint64_t RHSZero, RHSOne;
935 uint64_t Mask = MVT::getIntVTBitMask(VT);
Dan Gohmanea859be2007-06-22 14:59:07 +0000936 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Chris Lattner947c2892006-03-13 06:51:27 +0000937 if (LHSZero) {
Dan Gohmanea859be2007-06-22 14:59:07 +0000938 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
Chris Lattner947c2892006-03-13 06:51:27 +0000939
940 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
941 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
942 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
943 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
944 return DAG.getNode(ISD::OR, VT, N0, N1);
945 }
946 }
Evan Cheng3ef554d2006-11-06 08:14:30 +0000947
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000948 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
949 if (N0.getOpcode() == ISD::SHL && N0.Val->hasOneUse()) {
950 SDOperand Result = combineShlAddConstant(N0, N1, DAG);
951 if (Result.Val) return Result;
952 }
953 if (N1.getOpcode() == ISD::SHL && N1.Val->hasOneUse()) {
954 SDOperand Result = combineShlAddConstant(N1, N0, DAG);
955 if (Result.Val) return Result;
956 }
957
Evan Chengb13cdbd2007-06-21 07:39:16 +0000958 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
959 if (N0.getOpcode() == ISD::SELECT && N0.Val->hasOneUse()) {
960 SDOperand Result = combineSelectAndUse(N, N0, N1, DAG);
961 if (Result.Val) return Result;
962 }
963 if (N1.getOpcode() == ISD::SELECT && N1.Val->hasOneUse()) {
964 SDOperand Result = combineSelectAndUse(N, N1, N0, DAG);
965 if (Result.Val) return Result;
966 }
967
Nate Begeman83e75ec2005-09-06 04:43:02 +0000968 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000969}
970
Chris Lattner91153682007-03-04 20:03:15 +0000971SDOperand DAGCombiner::visitADDC(SDNode *N) {
972 SDOperand N0 = N->getOperand(0);
973 SDOperand N1 = N->getOperand(1);
974 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
975 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
976 MVT::ValueType VT = N0.getValueType();
977
978 // If the flag result is dead, turn this into an ADD.
979 if (N->hasNUsesOfValue(0, 1))
980 return CombineTo(N, DAG.getNode(ISD::ADD, VT, N1, N0),
Chris Lattnerb6541762007-03-04 20:40:38 +0000981 DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
Chris Lattner91153682007-03-04 20:03:15 +0000982
983 // canonicalize constant to RHS.
Chris Lattnerbcf24842007-03-04 20:08:45 +0000984 if (N0C && !N1C) {
985 SDOperand Ops[] = { N1, N0 };
986 return DAG.getNode(ISD::ADDC, N->getVTList(), Ops, 2);
987 }
Chris Lattner91153682007-03-04 20:03:15 +0000988
Chris Lattnerb6541762007-03-04 20:40:38 +0000989 // fold (addc x, 0) -> x + no carry out
990 if (N1C && N1C->isNullValue())
991 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
992
993 // fold (addc a, b) -> (or a, b), CARRY_FALSE iff a and b share no bits.
994 uint64_t LHSZero, LHSOne;
995 uint64_t RHSZero, RHSOne;
996 uint64_t Mask = MVT::getIntVTBitMask(VT);
Dan Gohmanea859be2007-06-22 14:59:07 +0000997 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Chris Lattnerb6541762007-03-04 20:40:38 +0000998 if (LHSZero) {
Dan Gohmanea859be2007-06-22 14:59:07 +0000999 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
Chris Lattnerb6541762007-03-04 20:40:38 +00001000
1001 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1002 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
1003 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
1004 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
1005 return CombineTo(N, DAG.getNode(ISD::OR, VT, N0, N1),
1006 DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
1007 }
Chris Lattner91153682007-03-04 20:03:15 +00001008
1009 return SDOperand();
1010}
1011
1012SDOperand DAGCombiner::visitADDE(SDNode *N) {
1013 SDOperand N0 = N->getOperand(0);
1014 SDOperand N1 = N->getOperand(1);
Chris Lattnerb6541762007-03-04 20:40:38 +00001015 SDOperand CarryIn = N->getOperand(2);
Chris Lattner91153682007-03-04 20:03:15 +00001016 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1017 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Chris Lattnerbcf24842007-03-04 20:08:45 +00001018 //MVT::ValueType VT = N0.getValueType();
Chris Lattner91153682007-03-04 20:03:15 +00001019
1020 // canonicalize constant to RHS
Chris Lattnerbcf24842007-03-04 20:08:45 +00001021 if (N0C && !N1C) {
Chris Lattnerb6541762007-03-04 20:40:38 +00001022 SDOperand Ops[] = { N1, N0, CarryIn };
Chris Lattnerbcf24842007-03-04 20:08:45 +00001023 return DAG.getNode(ISD::ADDE, N->getVTList(), Ops, 3);
1024 }
Chris Lattner91153682007-03-04 20:03:15 +00001025
Chris Lattnerb6541762007-03-04 20:40:38 +00001026 // fold (adde x, y, false) -> (addc x, y)
1027 if (CarryIn.getOpcode() == ISD::CARRY_FALSE) {
1028 SDOperand Ops[] = { N1, N0 };
1029 return DAG.getNode(ISD::ADDC, N->getVTList(), Ops, 2);
1030 }
Chris Lattner91153682007-03-04 20:03:15 +00001031
1032 return SDOperand();
1033}
1034
1035
1036
Nate Begeman83e75ec2005-09-06 04:43:02 +00001037SDOperand DAGCombiner::visitSUB(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001038 SDOperand N0 = N->getOperand(0);
1039 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001040 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1041 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +00001042 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001043
Dan Gohman7f321562007-06-25 16:23:39 +00001044 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00001045 if (MVT::isVector(VT)) {
1046 SDOperand FoldedVOp = SimplifyVBinOp(N);
1047 if (FoldedVOp.Val) return FoldedVOp;
1048 }
Dan Gohman7f321562007-06-25 16:23:39 +00001049
Chris Lattner854077d2005-10-17 01:07:11 +00001050 // fold (sub x, x) -> 0
1051 if (N0 == N1)
1052 return DAG.getConstant(0, N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001053 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001054 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001055 return DAG.getNode(ISD::SUB, VT, N0, N1);
Chris Lattner05b57432005-10-11 06:07:15 +00001056 // fold (sub x, c) -> (add x, -c)
1057 if (N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001058 return DAG.getNode(ISD::ADD, VT, N0, DAG.getConstant(-N1C->getValue(), VT));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001059 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +00001060 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001061 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001062 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +00001063 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001064 return N0.getOperand(0);
Evan Chengb13cdbd2007-06-21 07:39:16 +00001065 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
1066 if (N1.getOpcode() == ISD::SELECT && N1.Val->hasOneUse()) {
1067 SDOperand Result = combineSelectAndUse(N, N1, N0, DAG);
1068 if (Result.Val) return Result;
1069 }
Dan Gohman613e0d82007-07-03 14:03:57 +00001070 // If either operand of a sub is undef, the result is undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00001071 if (N0.getOpcode() == ISD::UNDEF)
1072 return N0;
1073 if (N1.getOpcode() == ISD::UNDEF)
1074 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001075
Nate Begeman83e75ec2005-09-06 04:43:02 +00001076 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001077}
1078
Nate Begeman83e75ec2005-09-06 04:43:02 +00001079SDOperand DAGCombiner::visitMUL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001080 SDOperand N0 = N->getOperand(0);
1081 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001082 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1083 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman223df222005-09-08 20:18:10 +00001084 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001085
Dan Gohman7f321562007-06-25 16:23:39 +00001086 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00001087 if (MVT::isVector(VT)) {
1088 SDOperand FoldedVOp = SimplifyVBinOp(N);
1089 if (FoldedVOp.Val) return FoldedVOp;
1090 }
Dan Gohman7f321562007-06-25 16:23:39 +00001091
Dan Gohman613e0d82007-07-03 14:03:57 +00001092 // fold (mul x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001093 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001094 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001095 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001096 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001097 return DAG.getNode(ISD::MUL, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001098 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001099 if (N0C && !N1C)
1100 return DAG.getNode(ISD::MUL, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001101 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001102 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001103 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001104 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +00001105 if (N1C && N1C->isAllOnesValue())
Nate Begeman405e3ec2005-10-21 00:02:42 +00001106 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001107 // fold (mul x, (1 << c)) -> x << c
Nate Begeman646d7e22005-09-02 21:18:40 +00001108 if (N1C && isPowerOf2_64(N1C->getValue()))
Chris Lattner3e6099b2005-10-30 06:41:49 +00001109 return DAG.getNode(ISD::SHL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +00001110 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001111 TLI.getShiftAmountTy()));
Chris Lattner3e6099b2005-10-30 06:41:49 +00001112 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
1113 if (N1C && isPowerOf2_64(-N1C->getSignExtended())) {
1114 // FIXME: If the input is something that is easily negated (e.g. a
1115 // single-use add), we should put the negate there.
1116 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT),
1117 DAG.getNode(ISD::SHL, VT, N0,
1118 DAG.getConstant(Log2_64(-N1C->getSignExtended()),
1119 TLI.getShiftAmountTy())));
1120 }
Andrew Lenharth50a0d422006-04-02 21:42:45 +00001121
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001122 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
1123 if (N1C && N0.getOpcode() == ISD::SHL &&
1124 isa<ConstantSDNode>(N0.getOperand(1))) {
1125 SDOperand C3 = DAG.getNode(ISD::SHL, VT, N1, N0.getOperand(1));
Chris Lattner5750df92006-03-01 04:03:14 +00001126 AddToWorkList(C3.Val);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001127 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0), C3);
1128 }
1129
1130 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
1131 // use.
1132 {
1133 SDOperand Sh(0,0), Y(0,0);
1134 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
1135 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
1136 N0.Val->hasOneUse()) {
1137 Sh = N0; Y = N1;
1138 } else if (N1.getOpcode() == ISD::SHL &&
1139 isa<ConstantSDNode>(N1.getOperand(1)) && N1.Val->hasOneUse()) {
1140 Sh = N1; Y = N0;
1141 }
1142 if (Sh.Val) {
1143 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Sh.getOperand(0), Y);
1144 return DAG.getNode(ISD::SHL, VT, Mul, Sh.getOperand(1));
1145 }
1146 }
Chris Lattnera1deca32006-03-04 23:33:26 +00001147 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
1148 if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() &&
1149 isa<ConstantSDNode>(N0.getOperand(1))) {
1150 return DAG.getNode(ISD::ADD, VT,
1151 DAG.getNode(ISD::MUL, VT, N0.getOperand(0), N1),
1152 DAG.getNode(ISD::MUL, VT, N0.getOperand(1), N1));
1153 }
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001154
Nate Begemancd4d58c2006-02-03 06:46:56 +00001155 // reassociate mul
1156 SDOperand RMUL = ReassociateOps(ISD::MUL, N0, N1);
1157 if (RMUL.Val != 0)
1158 return RMUL;
Dan Gohman7f321562007-06-25 16:23:39 +00001159
Nate Begeman83e75ec2005-09-06 04:43:02 +00001160 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001161}
1162
Nate Begeman83e75ec2005-09-06 04:43:02 +00001163SDOperand DAGCombiner::visitSDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001164 SDOperand N0 = N->getOperand(0);
1165 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001166 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1167 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +00001168 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001169
Dan Gohman7f321562007-06-25 16:23:39 +00001170 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00001171 if (MVT::isVector(VT)) {
1172 SDOperand FoldedVOp = SimplifyVBinOp(N);
1173 if (FoldedVOp.Val) return FoldedVOp;
1174 }
Dan Gohman7f321562007-06-25 16:23:39 +00001175
Nate Begeman1d4d4142005-09-01 00:19:25 +00001176 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001177 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001178 return DAG.getNode(ISD::SDIV, VT, N0, N1);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001179 // fold (sdiv X, 1) -> X
1180 if (N1C && N1C->getSignExtended() == 1LL)
1181 return N0;
1182 // fold (sdiv X, -1) -> 0-X
1183 if (N1C && N1C->isAllOnesValue())
1184 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +00001185 // If we know the sign bits of both operands are zero, strength reduce to a
1186 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
1187 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001188 if (DAG.MaskedValueIsZero(N1, SignBit) &&
1189 DAG.MaskedValueIsZero(N0, SignBit))
Chris Lattner094c8fc2005-10-07 06:10:46 +00001190 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
Nate Begemancd6a6ed2006-02-17 07:26:20 +00001191 // fold (sdiv X, pow2) -> simple ops after legalize
Nate Begemanfb7217b2006-02-17 19:54:08 +00001192 if (N1C && N1C->getValue() && !TLI.isIntDivCheap() &&
Nate Begeman405e3ec2005-10-21 00:02:42 +00001193 (isPowerOf2_64(N1C->getSignExtended()) ||
1194 isPowerOf2_64(-N1C->getSignExtended()))) {
1195 // If dividing by powers of two is cheap, then don't perform the following
1196 // fold.
1197 if (TLI.isPow2DivCheap())
1198 return SDOperand();
1199 int64_t pow2 = N1C->getSignExtended();
1200 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
Chris Lattner8f4880b2006-02-16 08:02:36 +00001201 unsigned lg2 = Log2_64(abs2);
1202 // Splat the sign bit into the register
1203 SDOperand SGN = DAG.getNode(ISD::SRA, VT, N0,
Nate Begeman405e3ec2005-10-21 00:02:42 +00001204 DAG.getConstant(MVT::getSizeInBits(VT)-1,
1205 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00001206 AddToWorkList(SGN.Val);
Chris Lattner8f4880b2006-02-16 08:02:36 +00001207 // Add (N0 < 0) ? abs2 - 1 : 0;
1208 SDOperand SRL = DAG.getNode(ISD::SRL, VT, SGN,
1209 DAG.getConstant(MVT::getSizeInBits(VT)-lg2,
Nate Begeman405e3ec2005-10-21 00:02:42 +00001210 TLI.getShiftAmountTy()));
Chris Lattner8f4880b2006-02-16 08:02:36 +00001211 SDOperand ADD = DAG.getNode(ISD::ADD, VT, N0, SRL);
Chris Lattner5750df92006-03-01 04:03:14 +00001212 AddToWorkList(SRL.Val);
1213 AddToWorkList(ADD.Val); // Divide by pow2
Chris Lattner8f4880b2006-02-16 08:02:36 +00001214 SDOperand SRA = DAG.getNode(ISD::SRA, VT, ADD,
1215 DAG.getConstant(lg2, TLI.getShiftAmountTy()));
Nate Begeman405e3ec2005-10-21 00:02:42 +00001216 // If we're dividing by a positive value, we're done. Otherwise, we must
1217 // negate the result.
1218 if (pow2 > 0)
1219 return SRA;
Chris Lattner5750df92006-03-01 04:03:14 +00001220 AddToWorkList(SRA.Val);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001221 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), SRA);
1222 }
Nate Begeman69575232005-10-20 02:15:44 +00001223 // if integer divide is expensive and we satisfy the requirements, emit an
1224 // alternate sequence.
Nate Begeman405e3ec2005-10-21 00:02:42 +00001225 if (N1C && (N1C->getSignExtended() < -1 || N1C->getSignExtended() > 1) &&
Chris Lattnere9936d12005-10-22 18:50:15 +00001226 !TLI.isIntDivCheap()) {
1227 SDOperand Op = BuildSDIV(N);
1228 if (Op.Val) return Op;
Nate Begeman69575232005-10-20 02:15:44 +00001229 }
Dan Gohman7f321562007-06-25 16:23:39 +00001230
Dan Gohman613e0d82007-07-03 14:03:57 +00001231 // undef / X -> 0
1232 if (N0.getOpcode() == ISD::UNDEF)
1233 return DAG.getConstant(0, VT);
1234 // X / undef -> undef
1235 if (N1.getOpcode() == ISD::UNDEF)
1236 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001237
Nate Begeman83e75ec2005-09-06 04:43:02 +00001238 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001239}
1240
Nate Begeman83e75ec2005-09-06 04:43:02 +00001241SDOperand DAGCombiner::visitUDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001242 SDOperand N0 = N->getOperand(0);
1243 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001244 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1245 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +00001246 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001247
Dan Gohman7f321562007-06-25 16:23:39 +00001248 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00001249 if (MVT::isVector(VT)) {
1250 SDOperand FoldedVOp = SimplifyVBinOp(N);
1251 if (FoldedVOp.Val) return FoldedVOp;
1252 }
Dan Gohman7f321562007-06-25 16:23:39 +00001253
Nate Begeman1d4d4142005-09-01 00:19:25 +00001254 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001255 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001256 return DAG.getNode(ISD::UDIV, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001257 // fold (udiv x, (1 << c)) -> x >>u c
Nate Begeman646d7e22005-09-02 21:18:40 +00001258 if (N1C && isPowerOf2_64(N1C->getValue()))
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001259 return DAG.getNode(ISD::SRL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +00001260 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001261 TLI.getShiftAmountTy()));
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001262 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
1263 if (N1.getOpcode() == ISD::SHL) {
1264 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
1265 if (isPowerOf2_64(SHC->getValue())) {
1266 MVT::ValueType ADDVT = N1.getOperand(1).getValueType();
Nate Begemanc031e332006-02-05 07:36:48 +00001267 SDOperand Add = DAG.getNode(ISD::ADD, ADDVT, N1.getOperand(1),
1268 DAG.getConstant(Log2_64(SHC->getValue()),
1269 ADDVT));
Chris Lattner5750df92006-03-01 04:03:14 +00001270 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +00001271 return DAG.getNode(ISD::SRL, VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001272 }
1273 }
1274 }
Nate Begeman69575232005-10-20 02:15:44 +00001275 // fold (udiv x, c) -> alternate
Chris Lattnere9936d12005-10-22 18:50:15 +00001276 if (N1C && N1C->getValue() && !TLI.isIntDivCheap()) {
1277 SDOperand Op = BuildUDIV(N);
1278 if (Op.Val) return Op;
1279 }
Dan Gohman7f321562007-06-25 16:23:39 +00001280
Dan Gohman613e0d82007-07-03 14:03:57 +00001281 // undef / X -> 0
1282 if (N0.getOpcode() == ISD::UNDEF)
1283 return DAG.getConstant(0, VT);
1284 // X / undef -> undef
1285 if (N1.getOpcode() == ISD::UNDEF)
1286 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001287
Nate Begeman83e75ec2005-09-06 04:43:02 +00001288 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001289}
1290
Nate Begeman83e75ec2005-09-06 04:43:02 +00001291SDOperand DAGCombiner::visitSREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001292 SDOperand N0 = N->getOperand(0);
1293 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001294 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1295 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +00001296 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001297
1298 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001299 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001300 return DAG.getNode(ISD::SREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +00001301 // If we know the sign bits of both operands are zero, strength reduce to a
1302 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
1303 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001304 if (DAG.MaskedValueIsZero(N1, SignBit) &&
1305 DAG.MaskedValueIsZero(N0, SignBit))
Nate Begemana148d982006-01-18 22:35:16 +00001306 return DAG.getNode(ISD::UREM, VT, N0, N1);
Chris Lattner26d29902006-10-12 20:58:32 +00001307
Dan Gohman77003042007-11-26 23:46:11 +00001308 // If X/C can be simplified by the division-by-constant logic, lower
1309 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00001310 if (N1C && !N1C->isNullValue()) {
1311 SDOperand Div = DAG.getNode(ISD::SDIV, VT, N0, N1);
Dan Gohman77003042007-11-26 23:46:11 +00001312 SDOperand OptimizedDiv = combine(Div.Val);
1313 if (OptimizedDiv.Val && OptimizedDiv.Val != Div.Val) {
1314 SDOperand Mul = DAG.getNode(ISD::MUL, VT, OptimizedDiv, N1);
1315 SDOperand Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
1316 AddToWorkList(Mul.Val);
1317 return Sub;
1318 }
Chris Lattner26d29902006-10-12 20:58:32 +00001319 }
1320
Dan Gohman613e0d82007-07-03 14:03:57 +00001321 // undef % X -> 0
1322 if (N0.getOpcode() == ISD::UNDEF)
1323 return DAG.getConstant(0, VT);
1324 // X % undef -> undef
1325 if (N1.getOpcode() == ISD::UNDEF)
1326 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001327
Nate Begeman83e75ec2005-09-06 04:43:02 +00001328 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001329}
1330
Nate Begeman83e75ec2005-09-06 04:43:02 +00001331SDOperand DAGCombiner::visitUREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001332 SDOperand N0 = N->getOperand(0);
1333 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001334 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1335 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +00001336 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001337
1338 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001339 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001340 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +00001341 // fold (urem x, pow2) -> (and x, pow2-1)
1342 if (N1C && !N1C->isNullValue() && isPowerOf2_64(N1C->getValue()))
Nate Begemana148d982006-01-18 22:35:16 +00001343 return DAG.getNode(ISD::AND, VT, N0, DAG.getConstant(N1C->getValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +00001344 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
1345 if (N1.getOpcode() == ISD::SHL) {
1346 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
1347 if (isPowerOf2_64(SHC->getValue())) {
Nate Begemanbab92392006-02-05 08:07:24 +00001348 SDOperand Add = DAG.getNode(ISD::ADD, VT, N1,DAG.getConstant(~0ULL,VT));
Chris Lattner5750df92006-03-01 04:03:14 +00001349 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +00001350 return DAG.getNode(ISD::AND, VT, N0, Add);
1351 }
1352 }
1353 }
Chris Lattner26d29902006-10-12 20:58:32 +00001354
Dan Gohman77003042007-11-26 23:46:11 +00001355 // If X/C can be simplified by the division-by-constant logic, lower
1356 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00001357 if (N1C && !N1C->isNullValue()) {
1358 SDOperand Div = DAG.getNode(ISD::UDIV, VT, N0, N1);
Dan Gohman77003042007-11-26 23:46:11 +00001359 SDOperand OptimizedDiv = combine(Div.Val);
1360 if (OptimizedDiv.Val && OptimizedDiv.Val != Div.Val) {
1361 SDOperand Mul = DAG.getNode(ISD::MUL, VT, OptimizedDiv, N1);
1362 SDOperand Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
1363 AddToWorkList(Mul.Val);
1364 return Sub;
1365 }
Chris Lattner26d29902006-10-12 20:58:32 +00001366 }
1367
Dan Gohman613e0d82007-07-03 14:03:57 +00001368 // undef % X -> 0
1369 if (N0.getOpcode() == ISD::UNDEF)
1370 return DAG.getConstant(0, VT);
1371 // X % undef -> undef
1372 if (N1.getOpcode() == ISD::UNDEF)
1373 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001374
Nate Begeman83e75ec2005-09-06 04:43:02 +00001375 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001376}
1377
Nate Begeman83e75ec2005-09-06 04:43:02 +00001378SDOperand DAGCombiner::visitMULHS(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001379 SDOperand N0 = N->getOperand(0);
1380 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001381 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Dan Gohman7f321562007-06-25 16:23:39 +00001382 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001383
1384 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001385 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001386 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001387 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001388 if (N1C && N1C->getValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001389 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
1390 DAG.getConstant(MVT::getSizeInBits(N0.getValueType())-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001391 TLI.getShiftAmountTy()));
Dan Gohman613e0d82007-07-03 14:03:57 +00001392 // fold (mulhs x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001393 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001394 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00001395
Nate Begeman83e75ec2005-09-06 04:43:02 +00001396 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001397}
1398
Nate Begeman83e75ec2005-09-06 04:43:02 +00001399SDOperand DAGCombiner::visitMULHU(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001400 SDOperand N0 = N->getOperand(0);
1401 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001402 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Dan Gohman7f321562007-06-25 16:23:39 +00001403 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001404
1405 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001406 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001407 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001408 // fold (mulhu x, 1) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001409 if (N1C && N1C->getValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001410 return DAG.getConstant(0, N0.getValueType());
Dan Gohman613e0d82007-07-03 14:03:57 +00001411 // fold (mulhu x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001412 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001413 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00001414
Nate Begeman83e75ec2005-09-06 04:43:02 +00001415 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001416}
1417
Dan Gohman389079b2007-10-08 17:57:15 +00001418/// SimplifyNodeWithTwoResults - Perform optimizations common to nodes that
1419/// compute two values. LoOp and HiOp give the opcodes for the two computations
1420/// that are being performed. Return true if a simplification was made.
1421///
1422bool DAGCombiner::SimplifyNodeWithTwoResults(SDNode *N,
1423 unsigned LoOp, unsigned HiOp) {
Dan Gohman389079b2007-10-08 17:57:15 +00001424 // If the high half is not needed, just compute the low half.
Evan Cheng44711942007-11-08 09:25:29 +00001425 bool HiExists = N->hasAnyUseOfValue(1);
1426 if (!HiExists &&
Dan Gohman389079b2007-10-08 17:57:15 +00001427 (!AfterLegalize ||
1428 TLI.isOperationLegal(LoOp, N->getValueType(0)))) {
1429 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0),
1430 DAG.getNode(LoOp, N->getValueType(0),
1431 N->op_begin(),
Chris Lattner01d029b2007-10-15 06:10:22 +00001432 N->getNumOperands()));
Dan Gohman389079b2007-10-08 17:57:15 +00001433 return true;
1434 }
1435
1436 // If the low half is not needed, just compute the high half.
Evan Cheng44711942007-11-08 09:25:29 +00001437 bool LoExists = N->hasAnyUseOfValue(0);
1438 if (!LoExists &&
Dan Gohman389079b2007-10-08 17:57:15 +00001439 (!AfterLegalize ||
1440 TLI.isOperationLegal(HiOp, N->getValueType(1)))) {
1441 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1),
1442 DAG.getNode(HiOp, N->getValueType(1),
1443 N->op_begin(),
Chris Lattner01d029b2007-10-15 06:10:22 +00001444 N->getNumOperands()));
Dan Gohman389079b2007-10-08 17:57:15 +00001445 return true;
1446 }
1447
Evan Cheng44711942007-11-08 09:25:29 +00001448 // If both halves are used, return as it is.
1449 if (LoExists && HiExists)
1450 return false;
1451
1452 // If the two computed results can be simplified separately, separate them.
1453 bool RetVal = false;
1454 if (LoExists) {
1455 SDOperand Lo = DAG.getNode(LoOp, N->getValueType(0),
1456 N->op_begin(), N->getNumOperands());
1457 SDOperand LoOpt = combine(Lo.Val);
1458 if (LoOpt.Val && LoOpt != Lo &&
1459 TLI.isOperationLegal(LoOpt.getOpcode(), LoOpt.getValueType())) {
1460 RetVal = true;
1461 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), LoOpt);
Evan Cheng02132d62007-12-19 01:34:38 +00001462 } else
1463 DAG.DeleteNode(Lo.Val);
Dan Gohman389079b2007-10-08 17:57:15 +00001464 }
1465
Evan Cheng44711942007-11-08 09:25:29 +00001466 if (HiExists) {
1467 SDOperand Hi = DAG.getNode(HiOp, N->getValueType(1),
1468 N->op_begin(), N->getNumOperands());
1469 SDOperand HiOpt = combine(Hi.Val);
1470 if (HiOpt.Val && HiOpt != Hi &&
1471 TLI.isOperationLegal(HiOpt.getOpcode(), HiOpt.getValueType())) {
1472 RetVal = true;
1473 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1), HiOpt);
Evan Cheng02132d62007-12-19 01:34:38 +00001474 } else
1475 DAG.DeleteNode(Hi.Val);
Evan Cheng44711942007-11-08 09:25:29 +00001476 }
1477
1478 return RetVal;
Dan Gohman389079b2007-10-08 17:57:15 +00001479}
1480
1481SDOperand DAGCombiner::visitSMUL_LOHI(SDNode *N) {
1482
1483 if (SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHS))
1484 return SDOperand();
1485
1486 return SDOperand();
1487}
1488
1489SDOperand DAGCombiner::visitUMUL_LOHI(SDNode *N) {
1490
1491 if (SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHU))
1492 return SDOperand();
1493
1494 return SDOperand();
1495}
1496
1497SDOperand DAGCombiner::visitSDIVREM(SDNode *N) {
1498
1499 if (SimplifyNodeWithTwoResults(N, ISD::SDIV, ISD::SREM))
1500 return SDOperand();
1501
1502 return SDOperand();
1503}
1504
1505SDOperand DAGCombiner::visitUDIVREM(SDNode *N) {
1506
1507 if (SimplifyNodeWithTwoResults(N, ISD::UDIV, ISD::UREM))
1508 return SDOperand();
1509
1510 return SDOperand();
1511}
1512
Chris Lattner35e5c142006-05-05 05:51:50 +00001513/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
1514/// two operands of the same opcode, try to simplify it.
1515SDOperand DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
1516 SDOperand N0 = N->getOperand(0), N1 = N->getOperand(1);
1517 MVT::ValueType VT = N0.getValueType();
1518 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
1519
Chris Lattner540121f2006-05-05 06:31:05 +00001520 // For each of OP in AND/OR/XOR:
1521 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
1522 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
1523 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
Chris Lattner0d8dae72006-05-05 06:32:04 +00001524 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y))
Chris Lattner540121f2006-05-05 06:31:05 +00001525 if ((N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND||
Chris Lattner0d8dae72006-05-05 06:32:04 +00001526 N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::TRUNCATE) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001527 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1528 SDOperand ORNode = DAG.getNode(N->getOpcode(),
1529 N0.getOperand(0).getValueType(),
1530 N0.getOperand(0), N1.getOperand(0));
1531 AddToWorkList(ORNode.Val);
Chris Lattner540121f2006-05-05 06:31:05 +00001532 return DAG.getNode(N0.getOpcode(), VT, ORNode);
Chris Lattner35e5c142006-05-05 05:51:50 +00001533 }
1534
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001535 // For each of OP in SHL/SRL/SRA/AND...
1536 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
1537 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
1538 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
Chris Lattner35e5c142006-05-05 05:51:50 +00001539 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001540 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001541 N0.getOperand(1) == N1.getOperand(1)) {
1542 SDOperand ORNode = DAG.getNode(N->getOpcode(),
1543 N0.getOperand(0).getValueType(),
1544 N0.getOperand(0), N1.getOperand(0));
1545 AddToWorkList(ORNode.Val);
1546 return DAG.getNode(N0.getOpcode(), VT, ORNode, N0.getOperand(1));
1547 }
1548
1549 return SDOperand();
1550}
1551
Nate Begeman83e75ec2005-09-06 04:43:02 +00001552SDOperand DAGCombiner::visitAND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001553 SDOperand N0 = N->getOperand(0);
1554 SDOperand N1 = N->getOperand(1);
Nate Begemanfb7217b2006-02-17 19:54:08 +00001555 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001556 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1557 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001558 MVT::ValueType VT = N1.getValueType();
1559
Dan Gohman7f321562007-06-25 16:23:39 +00001560 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00001561 if (MVT::isVector(VT)) {
1562 SDOperand FoldedVOp = SimplifyVBinOp(N);
1563 if (FoldedVOp.Val) return FoldedVOp;
1564 }
Dan Gohman7f321562007-06-25 16:23:39 +00001565
Dan Gohman613e0d82007-07-03 14:03:57 +00001566 // fold (and x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001567 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001568 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001569 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001570 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001571 return DAG.getNode(ISD::AND, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001572 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001573 if (N0C && !N1C)
1574 return DAG.getNode(ISD::AND, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001575 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001576 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001577 return N0;
1578 // if (and x, c) is known to be zero, return 0
Dan Gohmanea859be2007-06-22 14:59:07 +00001579 if (N1C && DAG.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001580 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +00001581 // reassociate and
1582 SDOperand RAND = ReassociateOps(ISD::AND, N0, N1);
1583 if (RAND.Val != 0)
1584 return RAND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001585 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
Nate Begeman5dc7e862005-11-02 18:42:59 +00001586 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001587 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Nate Begeman646d7e22005-09-02 21:18:40 +00001588 if ((ORI->getValue() & N1C->getValue()) == N1C->getValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001589 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +00001590 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
1591 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Chris Lattner1ec05d12006-03-01 21:47:21 +00001592 unsigned InMask = MVT::getIntVTBitMask(N0.getOperand(0).getValueType());
Dan Gohmanea859be2007-06-22 14:59:07 +00001593 if (DAG.MaskedValueIsZero(N0.getOperand(0),
Chris Lattner1ec05d12006-03-01 21:47:21 +00001594 ~N1C->getValue() & InMask)) {
1595 SDOperand Zext = DAG.getNode(ISD::ZERO_EXTEND, N0.getValueType(),
1596 N0.getOperand(0));
1597
1598 // Replace uses of the AND with uses of the Zero extend node.
1599 CombineTo(N, Zext);
1600
Chris Lattner3603cd62006-02-02 07:17:31 +00001601 // We actually want to replace all uses of the any_extend with the
1602 // zero_extend, to avoid duplicating things. This will later cause this
1603 // AND to be folded.
Chris Lattner1ec05d12006-03-01 21:47:21 +00001604 CombineTo(N0.Val, Zext);
Chris Lattnerfedced72006-04-20 23:55:59 +00001605 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner3603cd62006-02-02 07:17:31 +00001606 }
1607 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001608 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
1609 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1610 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1611 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1612
1613 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1614 MVT::isInteger(LL.getValueType())) {
1615 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
1616 if (cast<ConstantSDNode>(LR)->getValue() == 0 && Op1 == ISD::SETEQ) {
1617 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001618 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001619 return DAG.getSetCC(VT, ORNode, LR, Op1);
1620 }
1621 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
1622 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
1623 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001624 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001625 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1626 }
1627 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
1628 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
1629 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001630 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001631 return DAG.getSetCC(VT, ORNode, LR, Op1);
1632 }
1633 }
1634 // canonicalize equivalent to ll == rl
1635 if (LL == RR && LR == RL) {
1636 Op1 = ISD::getSetCCSwappedOperands(Op1);
1637 std::swap(RL, RR);
1638 }
1639 if (LL == RL && LR == RR) {
1640 bool isInteger = MVT::isInteger(LL.getValueType());
1641 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
1642 if (Result != ISD::SETCC_INVALID)
1643 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1644 }
1645 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001646
1647 // Simplify: and (op x...), (op y...) -> (op (and x, y))
1648 if (N0.getOpcode() == N1.getOpcode()) {
1649 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1650 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001651 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001652
Nate Begemande996292006-02-03 22:24:05 +00001653 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
1654 // fold (and (sra)) -> (and (srl)) when possible.
Chris Lattner6ea2dee2006-03-25 22:19:00 +00001655 if (!MVT::isVector(VT) &&
1656 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001657 return SDOperand(N, 0);
Nate Begemanded49632005-10-13 03:11:28 +00001658 // fold (zext_inreg (extload x)) -> (zextload x)
Evan Cheng83060c52007-03-07 08:07:03 +00001659 if (ISD::isEXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val)) {
Evan Cheng466685d2006-10-09 20:57:25 +00001660 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00001661 MVT::ValueType EVT = LN0->getLoadedVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001662 // If we zero all the possible extended bits, then we can turn this into
1663 // a zextload if we are running before legalize or the operation is legal.
Dan Gohmanea859be2007-06-22 14:59:07 +00001664 if (DAG.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Evan Chengc5484282006-10-04 00:56:09 +00001665 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00001666 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
1667 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00001668 LN0->getSrcValueOffset(), EVT,
1669 LN0->isVolatile(),
1670 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00001671 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001672 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001673 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001674 }
1675 }
1676 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Evan Cheng83060c52007-03-07 08:07:03 +00001677 if (ISD::isSEXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val) &&
1678 N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001679 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00001680 MVT::ValueType EVT = LN0->getLoadedVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001681 // If we zero all the possible extended bits, then we can turn this into
1682 // a zextload if we are running before legalize or the operation is legal.
Dan Gohmanea859be2007-06-22 14:59:07 +00001683 if (DAG.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Evan Chengc5484282006-10-04 00:56:09 +00001684 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00001685 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
1686 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00001687 LN0->getSrcValueOffset(), EVT,
1688 LN0->isVolatile(),
1689 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00001690 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001691 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001692 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001693 }
1694 }
Chris Lattner15045b62006-02-28 06:35:35 +00001695
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001696 // fold (and (load x), 255) -> (zextload x, i8)
1697 // fold (and (extload x, i16), 255) -> (zextload x, i8)
Evan Cheng466685d2006-10-09 20:57:25 +00001698 if (N1C && N0.getOpcode() == ISD::LOAD) {
1699 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
1700 if (LN0->getExtensionType() != ISD::SEXTLOAD &&
Chris Lattnerddf89562008-01-17 19:59:44 +00001701 LN0->isUnindexed() && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001702 MVT::ValueType EVT, LoadedVT;
1703 if (N1C->getValue() == 255)
1704 EVT = MVT::i8;
1705 else if (N1C->getValue() == 65535)
1706 EVT = MVT::i16;
1707 else if (N1C->getValue() == ~0U)
1708 EVT = MVT::i32;
1709 else
1710 EVT = MVT::Other;
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001711
Evan Cheng2e49f092006-10-11 07:10:22 +00001712 LoadedVT = LN0->getLoadedVT();
Evan Cheng466685d2006-10-09 20:57:25 +00001713 if (EVT != MVT::Other && LoadedVT > EVT &&
1714 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
1715 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
1716 // For big endian targets, we need to add an offset to the pointer to
1717 // load the correct bytes. For little endian systems, we merely need to
1718 // read fewer bytes from the same pointer.
Duncan Sandsc6fa1702007-11-09 08:57:19 +00001719 unsigned LVTStoreBytes = MVT::getStoreSizeInBits(LoadedVT)/8;
1720 unsigned EVTStoreBytes = MVT::getStoreSizeInBits(EVT)/8;
1721 unsigned PtrOff = LVTStoreBytes - EVTStoreBytes;
Duncan Sandsdc846502007-10-28 12:59:45 +00001722 unsigned Alignment = LN0->getAlignment();
Evan Cheng466685d2006-10-09 20:57:25 +00001723 SDOperand NewPtr = LN0->getBasePtr();
Duncan Sandsdc846502007-10-28 12:59:45 +00001724 if (!TLI.isLittleEndian()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001725 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
1726 DAG.getConstant(PtrOff, PtrType));
Duncan Sandsdc846502007-10-28 12:59:45 +00001727 Alignment = MinAlign(Alignment, PtrOff);
1728 }
Evan Cheng466685d2006-10-09 20:57:25 +00001729 AddToWorkList(NewPtr.Val);
1730 SDOperand Load =
1731 DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00001732 LN0->getSrcValue(), LN0->getSrcValueOffset(), EVT,
Duncan Sandsdc846502007-10-28 12:59:45 +00001733 LN0->isVolatile(), Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00001734 AddToWorkList(N);
1735 CombineTo(N0.Val, Load, Load.getValue(1));
1736 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1737 }
Chris Lattner15045b62006-02-28 06:35:35 +00001738 }
1739 }
1740
Nate Begeman83e75ec2005-09-06 04:43:02 +00001741 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001742}
1743
Nate Begeman83e75ec2005-09-06 04:43:02 +00001744SDOperand DAGCombiner::visitOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001745 SDOperand N0 = N->getOperand(0);
1746 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001747 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001748 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1749 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001750 MVT::ValueType VT = N1.getValueType();
1751 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001752
Dan Gohman7f321562007-06-25 16:23:39 +00001753 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00001754 if (MVT::isVector(VT)) {
1755 SDOperand FoldedVOp = SimplifyVBinOp(N);
1756 if (FoldedVOp.Val) return FoldedVOp;
1757 }
Dan Gohman7f321562007-06-25 16:23:39 +00001758
Dan Gohman613e0d82007-07-03 14:03:57 +00001759 // fold (or x, undef) -> -1
Dan Gohmand595b5f2007-07-10 14:20:37 +00001760 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Chris Lattnere094f542007-07-09 16:16:34 +00001761 return DAG.getConstant(~0ULL, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001762 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001763 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001764 return DAG.getNode(ISD::OR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001765 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001766 if (N0C && !N1C)
1767 return DAG.getNode(ISD::OR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001768 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001769 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001770 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001771 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001772 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001773 return N1;
1774 // fold (or x, c) -> c iff (x & ~c) == 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001775 if (N1C &&
Dan Gohmanea859be2007-06-22 14:59:07 +00001776 DAG.MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits))))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001777 return N1;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001778 // reassociate or
1779 SDOperand ROR = ReassociateOps(ISD::OR, N0, N1);
1780 if (ROR.Val != 0)
1781 return ROR;
1782 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
1783 if (N1C && N0.getOpcode() == ISD::AND && N0.Val->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00001784 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00001785 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
1786 return DAG.getNode(ISD::AND, VT, DAG.getNode(ISD::OR, VT, N0.getOperand(0),
1787 N1),
1788 DAG.getConstant(N1C->getValue() | C1->getValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00001789 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001790 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
1791 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1792 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1793 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1794
1795 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1796 MVT::isInteger(LL.getValueType())) {
1797 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
1798 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
1799 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
1800 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
1801 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001802 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001803 return DAG.getSetCC(VT, ORNode, LR, Op1);
1804 }
1805 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
1806 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
1807 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
1808 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
1809 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001810 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001811 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1812 }
1813 }
1814 // canonicalize equivalent to ll == rl
1815 if (LL == RR && LR == RL) {
1816 Op1 = ISD::getSetCCSwappedOperands(Op1);
1817 std::swap(RL, RR);
1818 }
1819 if (LL == RL && LR == RR) {
1820 bool isInteger = MVT::isInteger(LL.getValueType());
1821 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
1822 if (Result != ISD::SETCC_INVALID)
1823 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1824 }
1825 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001826
1827 // Simplify: or (op x...), (op y...) -> (op (or x, y))
1828 if (N0.getOpcode() == N1.getOpcode()) {
1829 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1830 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001831 }
Chris Lattner516b9622006-09-14 20:50:57 +00001832
Chris Lattner1ec72732006-09-14 21:11:37 +00001833 // (X & C1) | (Y & C2) -> (X|Y) & C3 if possible.
1834 if (N0.getOpcode() == ISD::AND &&
1835 N1.getOpcode() == ISD::AND &&
1836 N0.getOperand(1).getOpcode() == ISD::Constant &&
1837 N1.getOperand(1).getOpcode() == ISD::Constant &&
1838 // Don't increase # computations.
1839 (N0.Val->hasOneUse() || N1.Val->hasOneUse())) {
1840 // We can only do this xform if we know that bits from X that are set in C2
1841 // but not in C1 are already zero. Likewise for Y.
1842 uint64_t LHSMask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1843 uint64_t RHSMask = cast<ConstantSDNode>(N1.getOperand(1))->getValue();
1844
Dan Gohmanea859be2007-06-22 14:59:07 +00001845 if (DAG.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) &&
1846 DAG.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) {
Chris Lattner1ec72732006-09-14 21:11:37 +00001847 SDOperand X =DAG.getNode(ISD::OR, VT, N0.getOperand(0), N1.getOperand(0));
1848 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(LHSMask|RHSMask, VT));
1849 }
1850 }
1851
1852
Chris Lattner516b9622006-09-14 20:50:57 +00001853 // See if this is some rotate idiom.
1854 if (SDNode *Rot = MatchRotate(N0, N1))
1855 return SDOperand(Rot, 0);
Chris Lattner35e5c142006-05-05 05:51:50 +00001856
Nate Begeman83e75ec2005-09-06 04:43:02 +00001857 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001858}
1859
Chris Lattner516b9622006-09-14 20:50:57 +00001860
1861/// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present.
1862static bool MatchRotateHalf(SDOperand Op, SDOperand &Shift, SDOperand &Mask) {
1863 if (Op.getOpcode() == ISD::AND) {
Reid Spencer3ed469c2006-11-02 20:25:50 +00001864 if (isa<ConstantSDNode>(Op.getOperand(1))) {
Chris Lattner516b9622006-09-14 20:50:57 +00001865 Mask = Op.getOperand(1);
1866 Op = Op.getOperand(0);
1867 } else {
1868 return false;
1869 }
1870 }
1871
1872 if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) {
1873 Shift = Op;
1874 return true;
1875 }
1876 return false;
1877}
1878
1879
1880// MatchRotate - Handle an 'or' of two operands. If this is one of the many
1881// idioms for rotate, and if the target supports rotation instructions, generate
1882// a rot[lr].
1883SDNode *DAGCombiner::MatchRotate(SDOperand LHS, SDOperand RHS) {
1884 // Must be a legal type. Expanded an promoted things won't work with rotates.
1885 MVT::ValueType VT = LHS.getValueType();
1886 if (!TLI.isTypeLegal(VT)) return 0;
1887
1888 // The target must have at least one rotate flavor.
1889 bool HasROTL = TLI.isOperationLegal(ISD::ROTL, VT);
1890 bool HasROTR = TLI.isOperationLegal(ISD::ROTR, VT);
1891 if (!HasROTL && !HasROTR) return 0;
1892
1893 // Match "(X shl/srl V1) & V2" where V2 may not be present.
1894 SDOperand LHSShift; // The shift.
1895 SDOperand LHSMask; // AND value if any.
1896 if (!MatchRotateHalf(LHS, LHSShift, LHSMask))
1897 return 0; // Not part of a rotate.
1898
1899 SDOperand RHSShift; // The shift.
1900 SDOperand RHSMask; // AND value if any.
1901 if (!MatchRotateHalf(RHS, RHSShift, RHSMask))
1902 return 0; // Not part of a rotate.
1903
1904 if (LHSShift.getOperand(0) != RHSShift.getOperand(0))
1905 return 0; // Not shifting the same value.
1906
1907 if (LHSShift.getOpcode() == RHSShift.getOpcode())
1908 return 0; // Shifts must disagree.
1909
1910 // Canonicalize shl to left side in a shl/srl pair.
1911 if (RHSShift.getOpcode() == ISD::SHL) {
1912 std::swap(LHS, RHS);
1913 std::swap(LHSShift, RHSShift);
1914 std::swap(LHSMask , RHSMask );
1915 }
1916
1917 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Scott Michelc9dc1142007-04-02 21:36:32 +00001918 SDOperand LHSShiftArg = LHSShift.getOperand(0);
1919 SDOperand LHSShiftAmt = LHSShift.getOperand(1);
1920 SDOperand RHSShiftAmt = RHSShift.getOperand(1);
Chris Lattner516b9622006-09-14 20:50:57 +00001921
1922 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
1923 // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2)
Scott Michelc9dc1142007-04-02 21:36:32 +00001924 if (LHSShiftAmt.getOpcode() == ISD::Constant &&
1925 RHSShiftAmt.getOpcode() == ISD::Constant) {
1926 uint64_t LShVal = cast<ConstantSDNode>(LHSShiftAmt)->getValue();
1927 uint64_t RShVal = cast<ConstantSDNode>(RHSShiftAmt)->getValue();
Chris Lattner516b9622006-09-14 20:50:57 +00001928 if ((LShVal + RShVal) != OpSizeInBits)
1929 return 0;
1930
1931 SDOperand Rot;
1932 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00001933 Rot = DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00001934 else
Scott Michelc9dc1142007-04-02 21:36:32 +00001935 Rot = DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00001936
1937 // If there is an AND of either shifted operand, apply it to the result.
1938 if (LHSMask.Val || RHSMask.Val) {
1939 uint64_t Mask = MVT::getIntVTBitMask(VT);
1940
1941 if (LHSMask.Val) {
1942 uint64_t RHSBits = (1ULL << LShVal)-1;
1943 Mask &= cast<ConstantSDNode>(LHSMask)->getValue() | RHSBits;
1944 }
1945 if (RHSMask.Val) {
1946 uint64_t LHSBits = ~((1ULL << (OpSizeInBits-RShVal))-1);
1947 Mask &= cast<ConstantSDNode>(RHSMask)->getValue() | LHSBits;
1948 }
1949
1950 Rot = DAG.getNode(ISD::AND, VT, Rot, DAG.getConstant(Mask, VT));
1951 }
1952
1953 return Rot.Val;
1954 }
1955
1956 // If there is a mask here, and we have a variable shift, we can't be sure
1957 // that we're masking out the right stuff.
1958 if (LHSMask.Val || RHSMask.Val)
1959 return 0;
1960
1961 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
1962 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotr x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00001963 if (RHSShiftAmt.getOpcode() == ISD::SUB &&
1964 LHSShiftAmt == RHSShiftAmt.getOperand(1)) {
Chris Lattner516b9622006-09-14 20:50:57 +00001965 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00001966 dyn_cast<ConstantSDNode>(RHSShiftAmt.getOperand(0))) {
Chris Lattner516b9622006-09-14 20:50:57 +00001967 if (SUBC->getValue() == OpSizeInBits)
1968 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00001969 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
Chris Lattner516b9622006-09-14 20:50:57 +00001970 else
Scott Michelc9dc1142007-04-02 21:36:32 +00001971 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).Val;
Chris Lattner516b9622006-09-14 20:50:57 +00001972 }
1973 }
1974
1975 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
1976 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotl x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00001977 if (LHSShiftAmt.getOpcode() == ISD::SUB &&
1978 RHSShiftAmt == LHSShiftAmt.getOperand(1)) {
Chris Lattner516b9622006-09-14 20:50:57 +00001979 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00001980 dyn_cast<ConstantSDNode>(LHSShiftAmt.getOperand(0))) {
Chris Lattner516b9622006-09-14 20:50:57 +00001981 if (SUBC->getValue() == OpSizeInBits)
1982 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00001983 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
Chris Lattner516b9622006-09-14 20:50:57 +00001984 else
Scott Michelc9dc1142007-04-02 21:36:32 +00001985 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).Val;
1986 }
1987 }
1988
1989 // Look for sign/zext/any-extended cases:
1990 if ((LHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
1991 || LHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
1992 || LHSShiftAmt.getOpcode() == ISD::ANY_EXTEND) &&
1993 (RHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
1994 || RHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
1995 || RHSShiftAmt.getOpcode() == ISD::ANY_EXTEND)) {
1996 SDOperand LExtOp0 = LHSShiftAmt.getOperand(0);
1997 SDOperand RExtOp0 = RHSShiftAmt.getOperand(0);
1998 if (RExtOp0.getOpcode() == ISD::SUB &&
1999 RExtOp0.getOperand(1) == LExtOp0) {
2000 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
2001 // (rotr x, y)
2002 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
2003 // (rotl x, (sub 32, y))
2004 if (ConstantSDNode *SUBC = cast<ConstantSDNode>(RExtOp0.getOperand(0))) {
2005 if (SUBC->getValue() == OpSizeInBits) {
2006 if (HasROTL)
2007 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
2008 else
2009 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).Val;
2010 }
2011 }
2012 } else if (LExtOp0.getOpcode() == ISD::SUB &&
2013 RExtOp0 == LExtOp0.getOperand(1)) {
2014 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext r))) ->
2015 // (rotl x, y)
2016 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext r))) ->
2017 // (rotr x, (sub 32, y))
2018 if (ConstantSDNode *SUBC = cast<ConstantSDNode>(LExtOp0.getOperand(0))) {
2019 if (SUBC->getValue() == OpSizeInBits) {
2020 if (HasROTL)
2021 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, RHSShiftAmt).Val;
2022 else
2023 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
2024 }
2025 }
Chris Lattner516b9622006-09-14 20:50:57 +00002026 }
2027 }
2028
2029 return 0;
2030}
2031
2032
Nate Begeman83e75ec2005-09-06 04:43:02 +00002033SDOperand DAGCombiner::visitXOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002034 SDOperand N0 = N->getOperand(0);
2035 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002036 SDOperand LHS, RHS, CC;
2037 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2038 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002039 MVT::ValueType VT = N0.getValueType();
2040
Dan Gohman7f321562007-06-25 16:23:39 +00002041 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00002042 if (MVT::isVector(VT)) {
2043 SDOperand FoldedVOp = SimplifyVBinOp(N);
2044 if (FoldedVOp.Val) return FoldedVOp;
2045 }
Dan Gohman7f321562007-06-25 16:23:39 +00002046
Dan Gohman613e0d82007-07-03 14:03:57 +00002047 // fold (xor x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00002048 if (N0.getOpcode() == ISD::UNDEF)
2049 return N0;
2050 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00002051 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002052 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002053 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002054 return DAG.getNode(ISD::XOR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00002055 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00002056 if (N0C && !N1C)
2057 return DAG.getNode(ISD::XOR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002058 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002059 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002060 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00002061 // reassociate xor
2062 SDOperand RXOR = ReassociateOps(ISD::XOR, N0, N1);
2063 if (RXOR.Val != 0)
2064 return RXOR;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002065 // fold !(x cc y) -> (x !cc y)
Nate Begeman646d7e22005-09-02 21:18:40 +00002066 if (N1C && N1C->getValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
2067 bool isInt = MVT::isInteger(LHS.getValueType());
2068 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
2069 isInt);
2070 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002071 return DAG.getSetCC(VT, LHS, RHS, NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00002072 if (N0.getOpcode() == ISD::SELECT_CC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002073 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00002074 assert(0 && "Unhandled SetCC Equivalent!");
2075 abort();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002076 }
Chris Lattner61c5ff42007-09-10 21:39:07 +00002077 // fold (not (zext (setcc x, y))) -> (zext (not (setcc x, y)))
2078 if (N1C && N1C->getValue() == 1 && N0.getOpcode() == ISD::ZERO_EXTEND &&
2079 N0.Val->hasOneUse() && isSetCCEquivalent(N0.getOperand(0), LHS, RHS, CC)){
2080 SDOperand V = N0.getOperand(0);
2081 V = DAG.getNode(ISD::XOR, V.getValueType(), V,
Duncan Sands272dce02007-10-10 09:54:50 +00002082 DAG.getConstant(1, V.getValueType()));
Chris Lattner61c5ff42007-09-10 21:39:07 +00002083 AddToWorkList(V.Val);
2084 return DAG.getNode(ISD::ZERO_EXTEND, VT, V);
2085 }
2086
Nate Begeman99801192005-09-07 23:25:52 +00002087 // fold !(x or y) -> (!x and !y) iff x or y are setcc
Chris Lattner734c91d2006-11-10 21:37:15 +00002088 if (N1C && N1C->getValue() == 1 && VT == MVT::i1 &&
Nate Begeman99801192005-09-07 23:25:52 +00002089 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002090 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00002091 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
2092 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002093 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
2094 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00002095 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00002096 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002097 }
2098 }
Nate Begeman99801192005-09-07 23:25:52 +00002099 // fold !(x or y) -> (!x and !y) iff x or y are constants
2100 if (N1C && N1C->isAllOnesValue() &&
2101 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002102 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00002103 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
2104 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002105 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
2106 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00002107 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00002108 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002109 }
2110 }
Nate Begeman223df222005-09-08 20:18:10 +00002111 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
2112 if (N1C && N0.getOpcode() == ISD::XOR) {
2113 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
2114 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2115 if (N00C)
2116 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
2117 DAG.getConstant(N1C->getValue()^N00C->getValue(), VT));
2118 if (N01C)
2119 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
2120 DAG.getConstant(N1C->getValue()^N01C->getValue(), VT));
2121 }
2122 // fold (xor x, x) -> 0
Chris Lattner4fbdd592006-03-28 19:11:05 +00002123 if (N0 == N1) {
2124 if (!MVT::isVector(VT)) {
2125 return DAG.getConstant(0, VT);
2126 } else if (!AfterLegalize || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) {
2127 // Produce a vector of zeros.
Dan Gohman51eaa862007-06-14 22:58:02 +00002128 SDOperand El = DAG.getConstant(0, MVT::getVectorElementType(VT));
Chris Lattner4fbdd592006-03-28 19:11:05 +00002129 std::vector<SDOperand> Ops(MVT::getVectorNumElements(VT), El);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002130 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4fbdd592006-03-28 19:11:05 +00002131 }
2132 }
Chris Lattner35e5c142006-05-05 05:51:50 +00002133
2134 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
2135 if (N0.getOpcode() == N1.getOpcode()) {
2136 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
2137 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002138 }
Chris Lattner35e5c142006-05-05 05:51:50 +00002139
Chris Lattner3e104b12006-04-08 04:15:24 +00002140 // Simplify the expression using non-local knowledge.
2141 if (!MVT::isVector(VT) &&
2142 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00002143 return SDOperand(N, 0);
Chris Lattner3e104b12006-04-08 04:15:24 +00002144
Nate Begeman83e75ec2005-09-06 04:43:02 +00002145 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002146}
2147
Chris Lattnere70da202007-12-06 07:33:36 +00002148/// visitShiftByConstant - Handle transforms common to the three shifts, when
2149/// the shift amount is a constant.
2150SDOperand DAGCombiner::visitShiftByConstant(SDNode *N, unsigned Amt) {
2151 SDNode *LHS = N->getOperand(0).Val;
2152 if (!LHS->hasOneUse()) return SDOperand();
2153
2154 // We want to pull some binops through shifts, so that we have (and (shift))
2155 // instead of (shift (and)), likewise for add, or, xor, etc. This sort of
2156 // thing happens with address calculations, so it's important to canonicalize
2157 // it.
2158 bool HighBitSet = false; // Can we transform this if the high bit is set?
2159
2160 switch (LHS->getOpcode()) {
2161 default: return SDOperand();
2162 case ISD::OR:
2163 case ISD::XOR:
2164 HighBitSet = false; // We can only transform sra if the high bit is clear.
2165 break;
2166 case ISD::AND:
2167 HighBitSet = true; // We can only transform sra if the high bit is set.
2168 break;
2169 case ISD::ADD:
2170 if (N->getOpcode() != ISD::SHL)
2171 return SDOperand(); // only shl(add) not sr[al](add).
2172 HighBitSet = false; // We can only transform sra if the high bit is clear.
2173 break;
2174 }
2175
2176 // We require the RHS of the binop to be a constant as well.
2177 ConstantSDNode *BinOpCst = dyn_cast<ConstantSDNode>(LHS->getOperand(1));
2178 if (!BinOpCst) return SDOperand();
2179
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00002180
2181 // FIXME: disable this for unless the input to the binop is a shift by a
2182 // constant. If it is not a shift, it pessimizes some common cases like:
2183 //
2184 //void foo(int *X, int i) { X[i & 1235] = 1; }
2185 //int bar(int *X, int i) { return X[i & 255]; }
2186 SDNode *BinOpLHSVal = LHS->getOperand(0).Val;
2187 if ((BinOpLHSVal->getOpcode() != ISD::SHL &&
2188 BinOpLHSVal->getOpcode() != ISD::SRA &&
2189 BinOpLHSVal->getOpcode() != ISD::SRL) ||
2190 !isa<ConstantSDNode>(BinOpLHSVal->getOperand(1)))
2191 return SDOperand();
2192
Chris Lattnere70da202007-12-06 07:33:36 +00002193 MVT::ValueType VT = N->getValueType(0);
2194
2195 // If this is a signed shift right, and the high bit is modified
2196 // by the logical operation, do not perform the transformation.
2197 // The highBitSet boolean indicates the value of the high bit of
2198 // the constant which would cause it to be modified for this
2199 // operation.
2200 if (N->getOpcode() == ISD::SRA) {
2201 uint64_t BinOpRHSSign = BinOpCst->getValue() >> MVT::getSizeInBits(VT)-1;
2202 if ((bool)BinOpRHSSign != HighBitSet)
2203 return SDOperand();
2204 }
2205
2206 // Fold the constants, shifting the binop RHS by the shift amount.
2207 SDOperand NewRHS = DAG.getNode(N->getOpcode(), N->getValueType(0),
2208 LHS->getOperand(1), N->getOperand(1));
2209
2210 // Create the new shift.
2211 SDOperand NewShift = DAG.getNode(N->getOpcode(), VT, LHS->getOperand(0),
2212 N->getOperand(1));
2213
2214 // Create the new binop.
2215 return DAG.getNode(LHS->getOpcode(), VT, NewShift, NewRHS);
2216}
2217
2218
Nate Begeman83e75ec2005-09-06 04:43:02 +00002219SDOperand DAGCombiner::visitSHL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002220 SDOperand N0 = N->getOperand(0);
2221 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002222 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2223 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002224 MVT::ValueType VT = N0.getValueType();
2225 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
2226
2227 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002228 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002229 return DAG.getNode(ISD::SHL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002230 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002231 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002232 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002233 // fold (shl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00002234 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002235 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002236 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002237 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002238 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002239 // if (shl x, c) is known to be zero, return 0
Dan Gohmanea859be2007-06-22 14:59:07 +00002240 if (DAG.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002241 return DAG.getConstant(0, VT);
Chris Lattner61a4c072007-04-18 03:06:49 +00002242 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00002243 return SDOperand(N, 0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002244 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002245 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002246 N0.getOperand(1).getOpcode() == ISD::Constant) {
2247 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00002248 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002249 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002250 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002251 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002252 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002253 }
2254 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
2255 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002256 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002257 N0.getOperand(1).getOpcode() == ISD::Constant) {
2258 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00002259 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002260 SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
2261 DAG.getConstant(~0ULL << c1, VT));
2262 if (c2 > c1)
2263 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +00002264 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002265 else
Nate Begeman83e75ec2005-09-06 04:43:02 +00002266 return DAG.getNode(ISD::SRL, VT, Mask,
2267 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002268 }
2269 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00002270 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +00002271 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002272 DAG.getConstant(~0ULL << N1C->getValue(), VT));
Chris Lattnere70da202007-12-06 07:33:36 +00002273
2274 return N1C ? visitShiftByConstant(N, N1C->getValue()) : SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002275}
2276
Nate Begeman83e75ec2005-09-06 04:43:02 +00002277SDOperand DAGCombiner::visitSRA(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002278 SDOperand N0 = N->getOperand(0);
2279 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002280 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2281 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002282 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002283
2284 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002285 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002286 return DAG.getNode(ISD::SRA, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002287 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002288 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002289 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002290 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00002291 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002292 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002293 // fold (sra x, c >= size(x)) -> undef
Nate Begemanfb7217b2006-02-17 19:54:08 +00002294 if (N1C && N1C->getValue() >= MVT::getSizeInBits(VT))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002295 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002296 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002297 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002298 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00002299 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
2300 // sext_inreg.
2301 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
2302 unsigned LowBits = MVT::getSizeInBits(VT) - (unsigned)N1C->getValue();
2303 MVT::ValueType EVT;
2304 switch (LowBits) {
2305 default: EVT = MVT::Other; break;
2306 case 1: EVT = MVT::i1; break;
2307 case 8: EVT = MVT::i8; break;
2308 case 16: EVT = MVT::i16; break;
2309 case 32: EVT = MVT::i32; break;
2310 }
2311 if (EVT > MVT::Other && TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, EVT))
2312 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
2313 DAG.getValueType(EVT));
2314 }
Chris Lattner71d9ebc2006-02-28 06:23:04 +00002315
2316 // fold (sra (sra x, c1), c2) -> (sra x, c1+c2)
2317 if (N1C && N0.getOpcode() == ISD::SRA) {
2318 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2319 unsigned Sum = N1C->getValue() + C1->getValue();
2320 if (Sum >= MVT::getSizeInBits(VT)) Sum = MVT::getSizeInBits(VT)-1;
2321 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0),
2322 DAG.getConstant(Sum, N1C->getValueType(0)));
2323 }
2324 }
2325
Chris Lattnera8504462006-05-08 20:51:54 +00002326 // Simplify, based on bits shifted out of the LHS.
2327 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
2328 return SDOperand(N, 0);
2329
2330
Nate Begeman1d4d4142005-09-01 00:19:25 +00002331 // If the sign bit is known to be zero, switch this to a SRL.
Dan Gohmanea859be2007-06-22 14:59:07 +00002332 if (DAG.MaskedValueIsZero(N0, MVT::getIntVTSignBit(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002333 return DAG.getNode(ISD::SRL, VT, N0, N1);
Chris Lattnere70da202007-12-06 07:33:36 +00002334
2335 return N1C ? visitShiftByConstant(N, N1C->getValue()) : SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002336}
2337
Nate Begeman83e75ec2005-09-06 04:43:02 +00002338SDOperand DAGCombiner::visitSRL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002339 SDOperand N0 = N->getOperand(0);
2340 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002341 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2342 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002343 MVT::ValueType VT = N0.getValueType();
2344 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
2345
2346 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002347 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002348 return DAG.getNode(ISD::SRL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002349 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002350 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002351 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002352 // fold (srl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00002353 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002354 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002355 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002356 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002357 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002358 // if (srl x, c) is known to be zero, return 0
Dan Gohmanea859be2007-06-22 14:59:07 +00002359 if (N1C && DAG.MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002360 return DAG.getConstant(0, VT);
Chris Lattnerec06e9a2007-04-18 03:05:22 +00002361
Nate Begeman1d4d4142005-09-01 00:19:25 +00002362 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002363 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002364 N0.getOperand(1).getOpcode() == ISD::Constant) {
2365 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00002366 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002367 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002368 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002369 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002370 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002371 }
Chris Lattner350bec02006-04-02 06:11:11 +00002372
Chris Lattner06afe072006-05-05 22:53:17 +00002373 // fold (srl (anyextend x), c) -> (anyextend (srl x, c))
2374 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
2375 // Shifting in all undef bits?
2376 MVT::ValueType SmallVT = N0.getOperand(0).getValueType();
2377 if (N1C->getValue() >= MVT::getSizeInBits(SmallVT))
2378 return DAG.getNode(ISD::UNDEF, VT);
2379
2380 SDOperand SmallShift = DAG.getNode(ISD::SRL, SmallVT, N0.getOperand(0), N1);
2381 AddToWorkList(SmallShift.Val);
2382 return DAG.getNode(ISD::ANY_EXTEND, VT, SmallShift);
2383 }
2384
Chris Lattner3657ffe2006-10-12 20:23:19 +00002385 // fold (srl (sra X, Y), 31) -> (srl X, 31). This srl only looks at the sign
2386 // bit, which is unmodified by sra.
2387 if (N1C && N1C->getValue()+1 == MVT::getSizeInBits(VT)) {
2388 if (N0.getOpcode() == ISD::SRA)
2389 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0), N1);
2390 }
2391
Chris Lattner350bec02006-04-02 06:11:11 +00002392 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
2393 if (N1C && N0.getOpcode() == ISD::CTLZ &&
2394 N1C->getValue() == Log2_32(MVT::getSizeInBits(VT))) {
2395 uint64_t KnownZero, KnownOne, Mask = MVT::getIntVTBitMask(VT);
Dan Gohmanea859be2007-06-22 14:59:07 +00002396 DAG.ComputeMaskedBits(N0.getOperand(0), Mask, KnownZero, KnownOne);
Chris Lattner350bec02006-04-02 06:11:11 +00002397
2398 // If any of the input bits are KnownOne, then the input couldn't be all
2399 // zeros, thus the result of the srl will always be zero.
2400 if (KnownOne) return DAG.getConstant(0, VT);
2401
2402 // If all of the bits input the to ctlz node are known to be zero, then
2403 // the result of the ctlz is "32" and the result of the shift is one.
2404 uint64_t UnknownBits = ~KnownZero & Mask;
2405 if (UnknownBits == 0) return DAG.getConstant(1, VT);
2406
2407 // Otherwise, check to see if there is exactly one bit input to the ctlz.
2408 if ((UnknownBits & (UnknownBits-1)) == 0) {
2409 // Okay, we know that only that the single bit specified by UnknownBits
2410 // could be set on input to the CTLZ node. If this bit is set, the SRL
2411 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
2412 // to an SRL,XOR pair, which is likely to simplify more.
2413 unsigned ShAmt = CountTrailingZeros_64(UnknownBits);
2414 SDOperand Op = N0.getOperand(0);
2415 if (ShAmt) {
2416 Op = DAG.getNode(ISD::SRL, VT, Op,
2417 DAG.getConstant(ShAmt, TLI.getShiftAmountTy()));
2418 AddToWorkList(Op.Val);
2419 }
2420 return DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(1, VT));
2421 }
2422 }
Chris Lattner61a4c072007-04-18 03:06:49 +00002423
2424 // fold operands of srl based on knowledge that the low bits are not
2425 // demanded.
2426 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
2427 return SDOperand(N, 0);
2428
Chris Lattnere70da202007-12-06 07:33:36 +00002429 return N1C ? visitShiftByConstant(N, N1C->getValue()) : SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002430}
2431
Nate Begeman83e75ec2005-09-06 04:43:02 +00002432SDOperand DAGCombiner::visitCTLZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002433 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00002434 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002435
2436 // fold (ctlz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002437 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002438 return DAG.getNode(ISD::CTLZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002439 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002440}
2441
Nate Begeman83e75ec2005-09-06 04:43:02 +00002442SDOperand DAGCombiner::visitCTTZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002443 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00002444 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002445
2446 // fold (cttz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002447 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002448 return DAG.getNode(ISD::CTTZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002449 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002450}
2451
Nate Begeman83e75ec2005-09-06 04:43:02 +00002452SDOperand DAGCombiner::visitCTPOP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002453 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00002454 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002455
2456 // fold (ctpop c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002457 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002458 return DAG.getNode(ISD::CTPOP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002459 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002460}
2461
Nate Begeman452d7be2005-09-16 00:54:12 +00002462SDOperand DAGCombiner::visitSELECT(SDNode *N) {
2463 SDOperand N0 = N->getOperand(0);
2464 SDOperand N1 = N->getOperand(1);
2465 SDOperand N2 = N->getOperand(2);
2466 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2467 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2468 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
2469 MVT::ValueType VT = N->getValueType(0);
Evan Cheng571c4782007-08-18 05:57:05 +00002470 MVT::ValueType VT0 = N0.getValueType();
Nate Begeman44728a72005-09-19 22:34:01 +00002471
Nate Begeman452d7be2005-09-16 00:54:12 +00002472 // fold select C, X, X -> X
2473 if (N1 == N2)
2474 return N1;
2475 // fold select true, X, Y -> X
2476 if (N0C && !N0C->isNullValue())
2477 return N1;
2478 // fold select false, X, Y -> Y
2479 if (N0C && N0C->isNullValue())
2480 return N2;
2481 // fold select C, 1, X -> C | X
Nate Begeman44728a72005-09-19 22:34:01 +00002482 if (MVT::i1 == VT && N1C && N1C->getValue() == 1)
Nate Begeman452d7be2005-09-16 00:54:12 +00002483 return DAG.getNode(ISD::OR, VT, N0, N2);
Evan Cheng571c4782007-08-18 05:57:05 +00002484 // fold select C, 0, 1 -> ~C
2485 if (MVT::isInteger(VT) && MVT::isInteger(VT0) &&
2486 N1C && N2C && N1C->isNullValue() && N2C->getValue() == 1) {
2487 SDOperand XORNode = DAG.getNode(ISD::XOR, VT0, N0, DAG.getConstant(1, VT0));
2488 if (VT == VT0)
2489 return XORNode;
2490 AddToWorkList(XORNode.Val);
2491 if (MVT::getSizeInBits(VT) > MVT::getSizeInBits(VT0))
2492 return DAG.getNode(ISD::ZERO_EXTEND, VT, XORNode);
2493 return DAG.getNode(ISD::TRUNCATE, VT, XORNode);
2494 }
Nate Begeman452d7be2005-09-16 00:54:12 +00002495 // fold select C, 0, X -> ~C & X
Dale Johannesene0e6fac2007-12-06 17:53:31 +00002496 if (VT == VT0 && VT == MVT::i1 && N1C && N1C->isNullValue()) {
2497 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00002498 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00002499 return DAG.getNode(ISD::AND, VT, XORNode, N2);
2500 }
2501 // fold select C, X, 1 -> ~C | X
Dale Johannesene0e6fac2007-12-06 17:53:31 +00002502 if (VT == VT0 && VT == MVT::i1 && N2C && N2C->getValue() == 1) {
2503 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00002504 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00002505 return DAG.getNode(ISD::OR, VT, XORNode, N1);
2506 }
2507 // fold select C, X, 0 -> C & X
2508 // FIXME: this should check for C type == X type, not i1?
2509 if (MVT::i1 == VT && N2C && N2C->isNullValue())
2510 return DAG.getNode(ISD::AND, VT, N0, N1);
2511 // fold X ? X : Y --> X ? 1 : Y --> X | Y
2512 if (MVT::i1 == VT && N0 == N1)
2513 return DAG.getNode(ISD::OR, VT, N0, N2);
2514 // fold X ? Y : X --> X ? Y : 0 --> X & Y
2515 if (MVT::i1 == VT && N0 == N2)
2516 return DAG.getNode(ISD::AND, VT, N0, N1);
Chris Lattner729c6d12006-05-27 00:43:02 +00002517
Chris Lattner40c62d52005-10-18 06:04:22 +00002518 // If we can fold this based on the true/false value, do so.
2519 if (SimplifySelectOps(N, N1, N2))
Chris Lattner729c6d12006-05-27 00:43:02 +00002520 return SDOperand(N, 0); // Don't revisit N.
2521
Nate Begeman44728a72005-09-19 22:34:01 +00002522 // fold selects based on a setcc into other things, such as min/max/abs
2523 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman750ac1b2006-02-01 07:19:44 +00002524 // FIXME:
2525 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
2526 // having to say they don't support SELECT_CC on every type the DAG knows
2527 // about, since there is no way to mark an opcode illegal at all value types
2528 if (TLI.isOperationLegal(ISD::SELECT_CC, MVT::Other))
2529 return DAG.getNode(ISD::SELECT_CC, VT, N0.getOperand(0), N0.getOperand(1),
2530 N1, N2, N0.getOperand(2));
2531 else
2532 return SimplifySelect(N0, N1, N2);
Nate Begeman452d7be2005-09-16 00:54:12 +00002533 return SDOperand();
2534}
2535
2536SDOperand DAGCombiner::visitSELECT_CC(SDNode *N) {
Nate Begeman44728a72005-09-19 22:34:01 +00002537 SDOperand N0 = N->getOperand(0);
2538 SDOperand N1 = N->getOperand(1);
2539 SDOperand N2 = N->getOperand(2);
2540 SDOperand N3 = N->getOperand(3);
2541 SDOperand N4 = N->getOperand(4);
Nate Begeman44728a72005-09-19 22:34:01 +00002542 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
2543
Nate Begeman44728a72005-09-19 22:34:01 +00002544 // fold select_cc lhs, rhs, x, x, cc -> x
2545 if (N2 == N3)
2546 return N2;
Chris Lattner40c62d52005-10-18 06:04:22 +00002547
Chris Lattner5f42a242006-09-20 06:19:26 +00002548 // Determine if the condition we're dealing with is constant
2549 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
Chris Lattner30f73e72006-10-14 03:52:46 +00002550 if (SCC.Val) AddToWorkList(SCC.Val);
Chris Lattner5f42a242006-09-20 06:19:26 +00002551
2552 if (ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val)) {
2553 if (SCCC->getValue())
2554 return N2; // cond always true -> true val
2555 else
2556 return N3; // cond always false -> false val
2557 }
2558
2559 // Fold to a simpler select_cc
2560 if (SCC.Val && SCC.getOpcode() == ISD::SETCC)
2561 return DAG.getNode(ISD::SELECT_CC, N2.getValueType(),
2562 SCC.getOperand(0), SCC.getOperand(1), N2, N3,
2563 SCC.getOperand(2));
2564
Chris Lattner40c62d52005-10-18 06:04:22 +00002565 // If we can fold this based on the true/false value, do so.
2566 if (SimplifySelectOps(N, N2, N3))
Chris Lattner729c6d12006-05-27 00:43:02 +00002567 return SDOperand(N, 0); // Don't revisit N.
Chris Lattner40c62d52005-10-18 06:04:22 +00002568
Nate Begeman44728a72005-09-19 22:34:01 +00002569 // fold select_cc into other things, such as min/max/abs
2570 return SimplifySelectCC(N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00002571}
2572
2573SDOperand DAGCombiner::visitSETCC(SDNode *N) {
2574 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
2575 cast<CondCodeSDNode>(N->getOperand(2))->get());
2576}
2577
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002578// ExtendUsesToFormExtLoad - Trying to extend uses of a load to enable this:
2579// "fold ({s|z}ext (load x)) -> ({s|z}ext (truncate ({s|z}extload x)))"
2580// transformation. Returns true if extension are possible and the above
2581// mentioned transformation is profitable.
2582static bool ExtendUsesToFormExtLoad(SDNode *N, SDOperand N0,
2583 unsigned ExtOpc,
2584 SmallVector<SDNode*, 4> &ExtendNodes,
2585 TargetLowering &TLI) {
2586 bool HasCopyToRegUses = false;
2587 bool isTruncFree = TLI.isTruncateFree(N->getValueType(0), N0.getValueType());
2588 for (SDNode::use_iterator UI = N0.Val->use_begin(), UE = N0.Val->use_end();
2589 UI != UE; ++UI) {
2590 SDNode *User = *UI;
2591 if (User == N)
2592 continue;
2593 // FIXME: Only extend SETCC N, N and SETCC N, c for now.
2594 if (User->getOpcode() == ISD::SETCC) {
2595 ISD::CondCode CC = cast<CondCodeSDNode>(User->getOperand(2))->get();
2596 if (ExtOpc == ISD::ZERO_EXTEND && ISD::isSignedIntSetCC(CC))
2597 // Sign bits will be lost after a zext.
2598 return false;
2599 bool Add = false;
2600 for (unsigned i = 0; i != 2; ++i) {
2601 SDOperand UseOp = User->getOperand(i);
2602 if (UseOp == N0)
2603 continue;
2604 if (!isa<ConstantSDNode>(UseOp))
2605 return false;
2606 Add = true;
2607 }
2608 if (Add)
2609 ExtendNodes.push_back(User);
2610 } else {
2611 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
2612 SDOperand UseOp = User->getOperand(i);
2613 if (UseOp == N0) {
2614 // If truncate from extended type to original load type is free
2615 // on this target, then it's ok to extend a CopyToReg.
2616 if (isTruncFree && User->getOpcode() == ISD::CopyToReg)
2617 HasCopyToRegUses = true;
2618 else
2619 return false;
2620 }
2621 }
2622 }
2623 }
2624
2625 if (HasCopyToRegUses) {
2626 bool BothLiveOut = false;
2627 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
2628 UI != UE; ++UI) {
2629 SDNode *User = *UI;
2630 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
2631 SDOperand UseOp = User->getOperand(i);
2632 if (UseOp.Val == N && UseOp.ResNo == 0) {
2633 BothLiveOut = true;
2634 break;
2635 }
2636 }
2637 }
2638 if (BothLiveOut)
2639 // Both unextended and extended values are live out. There had better be
2640 // good a reason for the transformation.
2641 return ExtendNodes.size();
2642 }
2643 return true;
2644}
2645
Nate Begeman83e75ec2005-09-06 04:43:02 +00002646SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002647 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002648 MVT::ValueType VT = N->getValueType(0);
2649
Nate Begeman1d4d4142005-09-01 00:19:25 +00002650 // fold (sext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00002651 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002652 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0);
Chris Lattner310b5782006-05-06 23:06:26 +00002653
Nate Begeman1d4d4142005-09-01 00:19:25 +00002654 // fold (sext (sext x)) -> (sext x)
Chris Lattner310b5782006-05-06 23:06:26 +00002655 // fold (sext (aext x)) -> (sext x)
2656 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002657 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
Chris Lattner310b5782006-05-06 23:06:26 +00002658
Evan Chengc88138f2007-03-22 01:54:19 +00002659 // fold (sext (truncate (load x))) -> (sext (smaller load x))
2660 // fold (sext (truncate (srl (load x), c))) -> (sext (smaller load (x+c/n)))
Chris Lattner22558872007-02-26 03:13:59 +00002661 if (N0.getOpcode() == ISD::TRUNCATE) {
Evan Chengc88138f2007-03-22 01:54:19 +00002662 SDOperand NarrowLoad = ReduceLoadWidth(N0.Val);
Evan Cheng0b063de2007-03-23 02:16:52 +00002663 if (NarrowLoad.Val) {
2664 if (NarrowLoad.Val != N0.Val)
2665 CombineTo(N0.Val, NarrowLoad);
2666 return DAG.getNode(ISD::SIGN_EXTEND, VT, NarrowLoad);
2667 }
Evan Chengc88138f2007-03-22 01:54:19 +00002668 }
2669
2670 // See if the value being truncated is already sign extended. If so, just
2671 // eliminate the trunc/sext pair.
2672 if (N0.getOpcode() == ISD::TRUNCATE) {
Chris Lattner6007b842006-09-21 06:00:20 +00002673 SDOperand Op = N0.getOperand(0);
Chris Lattner22558872007-02-26 03:13:59 +00002674 unsigned OpBits = MVT::getSizeInBits(Op.getValueType());
2675 unsigned MidBits = MVT::getSizeInBits(N0.getValueType());
2676 unsigned DestBits = MVT::getSizeInBits(VT);
Dan Gohmanea859be2007-06-22 14:59:07 +00002677 unsigned NumSignBits = DAG.ComputeNumSignBits(Op);
Chris Lattner22558872007-02-26 03:13:59 +00002678
2679 if (OpBits == DestBits) {
2680 // Op is i32, Mid is i8, and Dest is i32. If Op has more than 24 sign
2681 // bits, it is already ready.
2682 if (NumSignBits > DestBits-MidBits)
2683 return Op;
2684 } else if (OpBits < DestBits) {
2685 // Op is i32, Mid is i8, and Dest is i64. If Op has more than 24 sign
2686 // bits, just sext from i32.
2687 if (NumSignBits > OpBits-MidBits)
2688 return DAG.getNode(ISD::SIGN_EXTEND, VT, Op);
2689 } else {
2690 // Op is i64, Mid is i8, and Dest is i32. If Op has more than 56 sign
2691 // bits, just truncate to i32.
2692 if (NumSignBits > OpBits-MidBits)
2693 return DAG.getNode(ISD::TRUNCATE, VT, Op);
Chris Lattner6007b842006-09-21 06:00:20 +00002694 }
Chris Lattner22558872007-02-26 03:13:59 +00002695
2696 // fold (sext (truncate x)) -> (sextinreg x).
2697 if (!AfterLegalize || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG,
2698 N0.getValueType())) {
2699 if (Op.getValueType() < VT)
2700 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
2701 else if (Op.getValueType() > VT)
2702 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
2703 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, Op,
2704 DAG.getValueType(N0.getValueType()));
2705 }
Chris Lattner6007b842006-09-21 06:00:20 +00002706 }
Chris Lattner310b5782006-05-06 23:06:26 +00002707
Evan Cheng110dec22005-12-14 02:19:23 +00002708 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002709 if (ISD::isNON_EXTLoad(N0.Val) &&
Evan Chengc5484282006-10-04 00:56:09 +00002710 (!AfterLegalize||TLI.isLoadXLegal(ISD::SEXTLOAD, N0.getValueType()))){
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002711 bool DoXform = true;
2712 SmallVector<SDNode*, 4> SetCCs;
2713 if (!N0.hasOneUse())
2714 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::SIGN_EXTEND, SetCCs, TLI);
2715 if (DoXform) {
2716 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2717 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2718 LN0->getBasePtr(), LN0->getSrcValue(),
2719 LN0->getSrcValueOffset(),
2720 N0.getValueType(),
2721 LN0->isVolatile(),
2722 LN0->getAlignment());
2723 CombineTo(N, ExtLoad);
2724 SDOperand Trunc = DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad);
2725 CombineTo(N0.Val, Trunc, ExtLoad.getValue(1));
2726 // Extend SetCC uses if necessary.
2727 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
2728 SDNode *SetCC = SetCCs[i];
2729 SmallVector<SDOperand, 4> Ops;
2730 for (unsigned j = 0; j != 2; ++j) {
2731 SDOperand SOp = SetCC->getOperand(j);
2732 if (SOp == Trunc)
2733 Ops.push_back(ExtLoad);
2734 else
2735 Ops.push_back(DAG.getNode(ISD::SIGN_EXTEND, VT, SOp));
2736 }
2737 Ops.push_back(SetCC->getOperand(2));
2738 CombineTo(SetCC, DAG.getNode(ISD::SETCC, SetCC->getValueType(0),
2739 &Ops[0], Ops.size()));
2740 }
2741 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2742 }
Nate Begeman3df4d522005-10-12 20:40:40 +00002743 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002744
2745 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
2746 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00002747 if ((ISD::isSEXTLoad(N0.Val) || ISD::isEXTLoad(N0.Val)) &&
2748 ISD::isUNINDEXEDLoad(N0.Val) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002749 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00002750 MVT::ValueType EVT = LN0->getLoadedVT();
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00002751 if (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT)) {
2752 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2753 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002754 LN0->getSrcValueOffset(), EVT,
2755 LN0->isVolatile(),
2756 LN0->getAlignment());
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00002757 CombineTo(N, ExtLoad);
2758 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2759 ExtLoad.getValue(1));
2760 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2761 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002762 }
2763
Chris Lattner20a35c32007-04-11 05:32:27 +00002764 // sext(setcc x,y,cc) -> select_cc x, y, -1, 0, cc
2765 if (N0.getOpcode() == ISD::SETCC) {
2766 SDOperand SCC =
Chris Lattner1eba01e2007-04-11 06:50:51 +00002767 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
2768 DAG.getConstant(~0ULL, VT), DAG.getConstant(0, VT),
2769 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
2770 if (SCC.Val) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00002771 }
2772
Nate Begeman83e75ec2005-09-06 04:43:02 +00002773 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002774}
2775
Nate Begeman83e75ec2005-09-06 04:43:02 +00002776SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002777 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002778 MVT::ValueType VT = N->getValueType(0);
2779
Nate Begeman1d4d4142005-09-01 00:19:25 +00002780 // fold (zext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00002781 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002782 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002783 // fold (zext (zext x)) -> (zext x)
Chris Lattner310b5782006-05-06 23:06:26 +00002784 // fold (zext (aext x)) -> (zext x)
2785 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002786 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
Chris Lattner6007b842006-09-21 06:00:20 +00002787
Evan Chengc88138f2007-03-22 01:54:19 +00002788 // fold (zext (truncate (load x))) -> (zext (smaller load x))
2789 // fold (zext (truncate (srl (load x), c))) -> (zext (small load (x+c/n)))
Dale Johannesen2041a0e2007-03-30 21:38:07 +00002790 if (N0.getOpcode() == ISD::TRUNCATE) {
Evan Chengc88138f2007-03-22 01:54:19 +00002791 SDOperand NarrowLoad = ReduceLoadWidth(N0.Val);
Evan Cheng0b063de2007-03-23 02:16:52 +00002792 if (NarrowLoad.Val) {
2793 if (NarrowLoad.Val != N0.Val)
2794 CombineTo(N0.Val, NarrowLoad);
2795 return DAG.getNode(ISD::ZERO_EXTEND, VT, NarrowLoad);
2796 }
Evan Chengc88138f2007-03-22 01:54:19 +00002797 }
2798
Chris Lattner6007b842006-09-21 06:00:20 +00002799 // fold (zext (truncate x)) -> (and x, mask)
2800 if (N0.getOpcode() == ISD::TRUNCATE &&
2801 (!AfterLegalize || TLI.isOperationLegal(ISD::AND, VT))) {
2802 SDOperand Op = N0.getOperand(0);
2803 if (Op.getValueType() < VT) {
2804 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
2805 } else if (Op.getValueType() > VT) {
2806 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
2807 }
2808 return DAG.getZeroExtendInReg(Op, N0.getValueType());
2809 }
2810
Chris Lattner111c2282006-09-21 06:14:31 +00002811 // fold (zext (and (trunc x), cst)) -> (and x, cst).
2812 if (N0.getOpcode() == ISD::AND &&
2813 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
2814 N0.getOperand(1).getOpcode() == ISD::Constant) {
2815 SDOperand X = N0.getOperand(0).getOperand(0);
2816 if (X.getValueType() < VT) {
2817 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
2818 } else if (X.getValueType() > VT) {
2819 X = DAG.getNode(ISD::TRUNCATE, VT, X);
2820 }
2821 uint64_t Mask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
2822 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
2823 }
2824
Evan Cheng110dec22005-12-14 02:19:23 +00002825 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002826 if (ISD::isNON_EXTLoad(N0.Val) &&
Evan Chengc5484282006-10-04 00:56:09 +00002827 (!AfterLegalize||TLI.isLoadXLegal(ISD::ZEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002828 bool DoXform = true;
2829 SmallVector<SDNode*, 4> SetCCs;
2830 if (!N0.hasOneUse())
2831 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ZERO_EXTEND, SetCCs, TLI);
2832 if (DoXform) {
2833 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2834 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
2835 LN0->getBasePtr(), LN0->getSrcValue(),
2836 LN0->getSrcValueOffset(),
2837 N0.getValueType(),
2838 LN0->isVolatile(),
2839 LN0->getAlignment());
2840 CombineTo(N, ExtLoad);
2841 SDOperand Trunc = DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad);
2842 CombineTo(N0.Val, Trunc, ExtLoad.getValue(1));
2843 // Extend SetCC uses if necessary.
2844 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
2845 SDNode *SetCC = SetCCs[i];
2846 SmallVector<SDOperand, 4> Ops;
2847 for (unsigned j = 0; j != 2; ++j) {
2848 SDOperand SOp = SetCC->getOperand(j);
2849 if (SOp == Trunc)
2850 Ops.push_back(ExtLoad);
2851 else
Evan Chengde1631b2007-10-30 20:11:21 +00002852 Ops.push_back(DAG.getNode(ISD::ZERO_EXTEND, VT, SOp));
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002853 }
2854 Ops.push_back(SetCC->getOperand(2));
2855 CombineTo(SetCC, DAG.getNode(ISD::SETCC, SetCC->getValueType(0),
2856 &Ops[0], Ops.size()));
2857 }
2858 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2859 }
Evan Cheng110dec22005-12-14 02:19:23 +00002860 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002861
2862 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
2863 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00002864 if ((ISD::isZEXTLoad(N0.Val) || ISD::isEXTLoad(N0.Val)) &&
2865 ISD::isUNINDEXEDLoad(N0.Val) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002866 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00002867 MVT::ValueType EVT = LN0->getLoadedVT();
Evan Cheng466685d2006-10-09 20:57:25 +00002868 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
2869 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002870 LN0->getSrcValueOffset(), EVT,
2871 LN0->isVolatile(),
2872 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00002873 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002874 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2875 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002876 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002877 }
Chris Lattner20a35c32007-04-11 05:32:27 +00002878
2879 // zext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
2880 if (N0.getOpcode() == ISD::SETCC) {
2881 SDOperand SCC =
2882 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
2883 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattner1eba01e2007-04-11 06:50:51 +00002884 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
2885 if (SCC.Val) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00002886 }
2887
Nate Begeman83e75ec2005-09-06 04:43:02 +00002888 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002889}
2890
Chris Lattner5ffc0662006-05-05 05:58:59 +00002891SDOperand DAGCombiner::visitANY_EXTEND(SDNode *N) {
2892 SDOperand N0 = N->getOperand(0);
Chris Lattner5ffc0662006-05-05 05:58:59 +00002893 MVT::ValueType VT = N->getValueType(0);
2894
2895 // fold (aext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00002896 if (isa<ConstantSDNode>(N0))
Chris Lattner5ffc0662006-05-05 05:58:59 +00002897 return DAG.getNode(ISD::ANY_EXTEND, VT, N0);
2898 // fold (aext (aext x)) -> (aext x)
2899 // fold (aext (zext x)) -> (zext x)
2900 // fold (aext (sext x)) -> (sext x)
2901 if (N0.getOpcode() == ISD::ANY_EXTEND ||
2902 N0.getOpcode() == ISD::ZERO_EXTEND ||
2903 N0.getOpcode() == ISD::SIGN_EXTEND)
2904 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
2905
Evan Chengc88138f2007-03-22 01:54:19 +00002906 // fold (aext (truncate (load x))) -> (aext (smaller load x))
2907 // fold (aext (truncate (srl (load x), c))) -> (aext (small load (x+c/n)))
2908 if (N0.getOpcode() == ISD::TRUNCATE) {
2909 SDOperand NarrowLoad = ReduceLoadWidth(N0.Val);
Evan Cheng0b063de2007-03-23 02:16:52 +00002910 if (NarrowLoad.Val) {
2911 if (NarrowLoad.Val != N0.Val)
2912 CombineTo(N0.Val, NarrowLoad);
2913 return DAG.getNode(ISD::ANY_EXTEND, VT, NarrowLoad);
2914 }
Evan Chengc88138f2007-03-22 01:54:19 +00002915 }
2916
Chris Lattner84750582006-09-20 06:29:17 +00002917 // fold (aext (truncate x))
2918 if (N0.getOpcode() == ISD::TRUNCATE) {
2919 SDOperand TruncOp = N0.getOperand(0);
2920 if (TruncOp.getValueType() == VT)
2921 return TruncOp; // x iff x size == zext size.
2922 if (TruncOp.getValueType() > VT)
2923 return DAG.getNode(ISD::TRUNCATE, VT, TruncOp);
2924 return DAG.getNode(ISD::ANY_EXTEND, VT, TruncOp);
2925 }
Chris Lattner0e4b9222006-09-21 06:40:43 +00002926
2927 // fold (aext (and (trunc x), cst)) -> (and x, cst).
2928 if (N0.getOpcode() == ISD::AND &&
2929 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
2930 N0.getOperand(1).getOpcode() == ISD::Constant) {
2931 SDOperand X = N0.getOperand(0).getOperand(0);
2932 if (X.getValueType() < VT) {
2933 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
2934 } else if (X.getValueType() > VT) {
2935 X = DAG.getNode(ISD::TRUNCATE, VT, X);
2936 }
2937 uint64_t Mask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
2938 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
2939 }
2940
Chris Lattner5ffc0662006-05-05 05:58:59 +00002941 // fold (aext (load x)) -> (aext (truncate (extload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00002942 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00002943 (!AfterLegalize||TLI.isLoadXLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00002944 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2945 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
2946 LN0->getBasePtr(), LN0->getSrcValue(),
2947 LN0->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002948 N0.getValueType(),
2949 LN0->isVolatile(),
2950 LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00002951 CombineTo(N, ExtLoad);
2952 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2953 ExtLoad.getValue(1));
2954 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2955 }
2956
2957 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
2958 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
2959 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00002960 if (N0.getOpcode() == ISD::LOAD &&
2961 !ISD::isNON_EXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val) &&
Evan Cheng466685d2006-10-09 20:57:25 +00002962 N0.hasOneUse()) {
2963 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng2e49f092006-10-11 07:10:22 +00002964 MVT::ValueType EVT = LN0->getLoadedVT();
Evan Cheng466685d2006-10-09 20:57:25 +00002965 SDOperand ExtLoad = DAG.getExtLoad(LN0->getExtensionType(), VT,
2966 LN0->getChain(), LN0->getBasePtr(),
2967 LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002968 LN0->getSrcValueOffset(), EVT,
2969 LN0->isVolatile(),
2970 LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00002971 CombineTo(N, ExtLoad);
2972 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2973 ExtLoad.getValue(1));
2974 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2975 }
Chris Lattner20a35c32007-04-11 05:32:27 +00002976
2977 // aext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
2978 if (N0.getOpcode() == ISD::SETCC) {
2979 SDOperand SCC =
Chris Lattner1eba01e2007-04-11 06:50:51 +00002980 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
2981 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattnerc24bbad2007-04-11 16:51:53 +00002982 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Chris Lattner1eba01e2007-04-11 06:50:51 +00002983 if (SCC.Val)
Chris Lattnerc56a81d2007-04-11 06:43:25 +00002984 return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00002985 }
2986
Chris Lattner5ffc0662006-05-05 05:58:59 +00002987 return SDOperand();
2988}
2989
Chris Lattner2b4c2792007-10-13 06:35:54 +00002990/// GetDemandedBits - See if the specified operand can be simplified with the
2991/// knowledge that only the bits specified by Mask are used. If so, return the
2992/// simpler operand, otherwise return a null SDOperand.
2993SDOperand DAGCombiner::GetDemandedBits(SDOperand V, uint64_t Mask) {
2994 switch (V.getOpcode()) {
2995 default: break;
2996 case ISD::OR:
2997 case ISD::XOR:
2998 // If the LHS or RHS don't contribute bits to the or, drop them.
2999 if (DAG.MaskedValueIsZero(V.getOperand(0), Mask))
3000 return V.getOperand(1);
3001 if (DAG.MaskedValueIsZero(V.getOperand(1), Mask))
3002 return V.getOperand(0);
3003 break;
Chris Lattnere33544c2007-10-13 06:58:48 +00003004 case ISD::SRL:
3005 // Only look at single-use SRLs.
3006 if (!V.Val->hasOneUse())
3007 break;
3008 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
3009 // See if we can recursively simplify the LHS.
3010 unsigned Amt = RHSC->getValue();
3011 Mask = (Mask << Amt) & MVT::getIntVTBitMask(V.getValueType());
3012 SDOperand SimplifyLHS = GetDemandedBits(V.getOperand(0), Mask);
3013 if (SimplifyLHS.Val) {
3014 return DAG.getNode(ISD::SRL, V.getValueType(),
3015 SimplifyLHS, V.getOperand(1));
3016 }
3017 }
Chris Lattner2b4c2792007-10-13 06:35:54 +00003018 }
3019 return SDOperand();
3020}
3021
Evan Chengc88138f2007-03-22 01:54:19 +00003022/// ReduceLoadWidth - If the result of a wider load is shifted to right of N
3023/// bits and then truncated to a narrower type and where N is a multiple
3024/// of number of bits of the narrower type, transform it to a narrower load
3025/// from address + N / num of bits of new type. If the result is to be
3026/// extended, also fold the extension to form a extending load.
3027SDOperand DAGCombiner::ReduceLoadWidth(SDNode *N) {
3028 unsigned Opc = N->getOpcode();
3029 ISD::LoadExtType ExtType = ISD::NON_EXTLOAD;
3030 SDOperand N0 = N->getOperand(0);
3031 MVT::ValueType VT = N->getValueType(0);
3032 MVT::ValueType EVT = N->getValueType(0);
3033
Evan Chenge177e302007-03-23 22:13:36 +00003034 // Special case: SIGN_EXTEND_INREG is basically truncating to EVT then
3035 // extended to VT.
Evan Chengc88138f2007-03-22 01:54:19 +00003036 if (Opc == ISD::SIGN_EXTEND_INREG) {
3037 ExtType = ISD::SEXTLOAD;
3038 EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Evan Chenge177e302007-03-23 22:13:36 +00003039 if (AfterLegalize && !TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))
3040 return SDOperand();
Evan Chengc88138f2007-03-22 01:54:19 +00003041 }
3042
3043 unsigned EVTBits = MVT::getSizeInBits(EVT);
3044 unsigned ShAmt = 0;
Evan Chengb37b80c2007-03-23 20:55:21 +00003045 bool CombineSRL = false;
Evan Chengc88138f2007-03-22 01:54:19 +00003046 if (N0.getOpcode() == ISD::SRL && N0.hasOneUse()) {
3047 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
3048 ShAmt = N01->getValue();
3049 // Is the shift amount a multiple of size of VT?
3050 if ((ShAmt & (EVTBits-1)) == 0) {
3051 N0 = N0.getOperand(0);
3052 if (MVT::getSizeInBits(N0.getValueType()) <= EVTBits)
3053 return SDOperand();
Evan Chengb37b80c2007-03-23 20:55:21 +00003054 CombineSRL = true;
Evan Chengc88138f2007-03-22 01:54:19 +00003055 }
3056 }
3057 }
3058
3059 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
3060 // Do not allow folding to i1 here. i1 is implicitly stored in memory in
3061 // zero extended form: by shrinking the load, we lose track of the fact
3062 // that it is already zero extended.
3063 // FIXME: This should be reevaluated.
3064 VT != MVT::i1) {
3065 assert(MVT::getSizeInBits(N0.getValueType()) > EVTBits &&
3066 "Cannot truncate to larger type!");
3067 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3068 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
Evan Chengdae54ce2007-03-24 00:02:43 +00003069 // For big endian targets, we need to adjust the offset to the pointer to
3070 // load the correct bytes.
Duncan Sandsc6fa1702007-11-09 08:57:19 +00003071 if (!TLI.isLittleEndian()) {
3072 unsigned LVTStoreBits = MVT::getStoreSizeInBits(N0.getValueType());
3073 unsigned EVTStoreBits = MVT::getStoreSizeInBits(EVT);
3074 ShAmt = LVTStoreBits - EVTStoreBits - ShAmt;
3075 }
Evan Chengdae54ce2007-03-24 00:02:43 +00003076 uint64_t PtrOff = ShAmt / 8;
Duncan Sandsdc846502007-10-28 12:59:45 +00003077 unsigned NewAlign = MinAlign(LN0->getAlignment(), PtrOff);
Evan Chengc88138f2007-03-22 01:54:19 +00003078 SDOperand NewPtr = DAG.getNode(ISD::ADD, PtrType, LN0->getBasePtr(),
3079 DAG.getConstant(PtrOff, PtrType));
3080 AddToWorkList(NewPtr.Val);
3081 SDOperand Load = (ExtType == ISD::NON_EXTLOAD)
3082 ? DAG.getLoad(VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00003083 LN0->getSrcValue(), LN0->getSrcValueOffset(),
Duncan Sandsdc846502007-10-28 12:59:45 +00003084 LN0->isVolatile(), NewAlign)
Evan Chengc88138f2007-03-22 01:54:19 +00003085 : DAG.getExtLoad(ExtType, VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00003086 LN0->getSrcValue(), LN0->getSrcValueOffset(), EVT,
Duncan Sandsdc846502007-10-28 12:59:45 +00003087 LN0->isVolatile(), NewAlign);
Evan Chengc88138f2007-03-22 01:54:19 +00003088 AddToWorkList(N);
Evan Chengb37b80c2007-03-23 20:55:21 +00003089 if (CombineSRL) {
Chris Lattner01d029b2007-10-15 06:10:22 +00003090 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), Load.getValue(1));
Evan Chengb37b80c2007-03-23 20:55:21 +00003091 CombineTo(N->getOperand(0).Val, Load);
3092 } else
3093 CombineTo(N0.Val, Load, Load.getValue(1));
Evan Cheng15213b72007-03-26 07:12:51 +00003094 if (ShAmt) {
3095 if (Opc == ISD::SIGN_EXTEND_INREG)
3096 return DAG.getNode(Opc, VT, Load, N->getOperand(1));
3097 else
3098 return DAG.getNode(Opc, VT, Load);
3099 }
Evan Chengc88138f2007-03-22 01:54:19 +00003100 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
3101 }
3102
3103 return SDOperand();
3104}
3105
Chris Lattner5ffc0662006-05-05 05:58:59 +00003106
Nate Begeman83e75ec2005-09-06 04:43:02 +00003107SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003108 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003109 SDOperand N1 = N->getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003110 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003111 MVT::ValueType EVT = cast<VTSDNode>(N1)->getVT();
Nate Begeman07ed4172005-10-10 21:26:48 +00003112 unsigned EVTBits = MVT::getSizeInBits(EVT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003113
Nate Begeman1d4d4142005-09-01 00:19:25 +00003114 // fold (sext_in_reg c1) -> c1
Chris Lattnereaeda562006-05-08 20:59:41 +00003115 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
Chris Lattner310b5782006-05-06 23:06:26 +00003116 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0, N1);
Chris Lattneree4ea922006-05-06 09:30:03 +00003117
Chris Lattner541a24f2006-05-06 22:43:44 +00003118 // If the input is already sign extended, just drop the extension.
Dan Gohmanea859be2007-06-22 14:59:07 +00003119 if (DAG.ComputeNumSignBits(N0) >= MVT::getSizeInBits(VT)-EVTBits+1)
Chris Lattneree4ea922006-05-06 09:30:03 +00003120 return N0;
3121
Nate Begeman646d7e22005-09-02 21:18:40 +00003122 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
3123 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
3124 EVT < cast<VTSDNode>(N0.getOperand(1))->getVT()) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00003125 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00003126 }
Chris Lattner4b37e872006-05-08 21:18:59 +00003127
Chris Lattner95a5e052007-04-17 19:03:21 +00003128 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is known zero.
Dan Gohmanea859be2007-06-22 14:59:07 +00003129 if (DAG.MaskedValueIsZero(N0, 1ULL << (EVTBits-1)))
Nate Begemande996292006-02-03 22:24:05 +00003130 return DAG.getZeroExtendInReg(N0, EVT);
Chris Lattner4b37e872006-05-08 21:18:59 +00003131
Chris Lattner95a5e052007-04-17 19:03:21 +00003132 // fold operands of sext_in_reg based on knowledge that the top bits are not
3133 // demanded.
3134 if (SimplifyDemandedBits(SDOperand(N, 0)))
3135 return SDOperand(N, 0);
3136
Evan Chengc88138f2007-03-22 01:54:19 +00003137 // fold (sext_in_reg (load x)) -> (smaller sextload x)
3138 // fold (sext_in_reg (srl (load x), c)) -> (smaller sextload (x+c/evtbits))
3139 SDOperand NarrowLoad = ReduceLoadWidth(N);
3140 if (NarrowLoad.Val)
3141 return NarrowLoad;
3142
Chris Lattner4b37e872006-05-08 21:18:59 +00003143 // fold (sext_in_reg (srl X, 24), i8) -> sra X, 24
3144 // fold (sext_in_reg (srl X, 23), i8) -> sra X, 23 iff possible.
3145 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
3146 if (N0.getOpcode() == ISD::SRL) {
3147 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
3148 if (ShAmt->getValue()+EVTBits <= MVT::getSizeInBits(VT)) {
3149 // We can turn this into an SRA iff the input to the SRL is already sign
3150 // extended enough.
Dan Gohmanea859be2007-06-22 14:59:07 +00003151 unsigned InSignBits = DAG.ComputeNumSignBits(N0.getOperand(0));
Chris Lattner4b37e872006-05-08 21:18:59 +00003152 if (MVT::getSizeInBits(VT)-(ShAmt->getValue()+EVTBits) < InSignBits)
3153 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0), N0.getOperand(1));
3154 }
3155 }
Evan Chengc88138f2007-03-22 01:54:19 +00003156
Nate Begemanded49632005-10-13 03:11:28 +00003157 // fold (sext_inreg (extload x)) -> (sextload x)
Evan Chengc5484282006-10-04 00:56:09 +00003158 if (ISD::isEXTLoad(N0.Val) &&
Evan Cheng83060c52007-03-07 08:07:03 +00003159 ISD::isUNINDEXEDLoad(N0.Val) &&
Evan Cheng2e49f092006-10-11 07:10:22 +00003160 EVT == cast<LoadSDNode>(N0)->getLoadedVT() &&
Evan Chengc5484282006-10-04 00:56:09 +00003161 (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003162 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3163 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
3164 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003165 LN0->getSrcValueOffset(), EVT,
3166 LN0->isVolatile(),
3167 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00003168 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00003169 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00003170 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00003171 }
3172 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Evan Cheng83060c52007-03-07 08:07:03 +00003173 if (ISD::isZEXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val) &&
3174 N0.hasOneUse() &&
Evan Cheng2e49f092006-10-11 07:10:22 +00003175 EVT == cast<LoadSDNode>(N0)->getLoadedVT() &&
Evan Chengc5484282006-10-04 00:56:09 +00003176 (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003177 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3178 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
3179 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003180 LN0->getSrcValueOffset(), EVT,
3181 LN0->isVolatile(),
3182 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00003183 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00003184 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00003185 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00003186 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00003187 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003188}
3189
Nate Begeman83e75ec2005-09-06 04:43:02 +00003190SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003191 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003192 MVT::ValueType VT = N->getValueType(0);
3193
3194 // noop truncate
3195 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00003196 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003197 // fold (truncate c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00003198 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00003199 return DAG.getNode(ISD::TRUNCATE, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003200 // fold (truncate (truncate x)) -> (truncate x)
3201 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00003202 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003203 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
Chris Lattnerb72773b2006-05-05 22:56:26 +00003204 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND||
3205 N0.getOpcode() == ISD::ANY_EXTEND) {
Chris Lattner32ba1aa2006-11-20 18:05:46 +00003206 if (N0.getOperand(0).getValueType() < VT)
Nate Begeman1d4d4142005-09-01 00:19:25 +00003207 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00003208 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Chris Lattner32ba1aa2006-11-20 18:05:46 +00003209 else if (N0.getOperand(0).getValueType() > VT)
Nate Begeman1d4d4142005-09-01 00:19:25 +00003210 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00003211 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003212 else
3213 // if the source and dest are the same type, we can drop both the extend
3214 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00003215 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003216 }
Evan Cheng007b69e2007-03-21 20:14:05 +00003217
Chris Lattner2b4c2792007-10-13 06:35:54 +00003218 // See if we can simplify the input to this truncate through knowledge that
3219 // only the low bits are being used. For example "trunc (or (shl x, 8), y)"
3220 // -> trunc y
3221 SDOperand Shorter = GetDemandedBits(N0, MVT::getIntVTBitMask(VT));
3222 if (Shorter.Val)
3223 return DAG.getNode(ISD::TRUNCATE, VT, Shorter);
3224
Nate Begeman3df4d522005-10-12 20:40:40 +00003225 // fold (truncate (load x)) -> (smaller load x)
Evan Cheng007b69e2007-03-21 20:14:05 +00003226 // fold (truncate (srl (load x), c)) -> (smaller load (x+c/evtbits))
Evan Chengc88138f2007-03-22 01:54:19 +00003227 return ReduceLoadWidth(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003228}
3229
Chris Lattner94683772005-12-23 05:30:37 +00003230SDOperand DAGCombiner::visitBIT_CONVERT(SDNode *N) {
3231 SDOperand N0 = N->getOperand(0);
3232 MVT::ValueType VT = N->getValueType(0);
3233
Dan Gohman7f321562007-06-25 16:23:39 +00003234 // If the input is a BUILD_VECTOR with all constant elements, fold this now.
3235 // Only do this before legalize, since afterward the target may be depending
3236 // on the bitconvert.
3237 // First check to see if this is all constant.
3238 if (!AfterLegalize &&
3239 N0.getOpcode() == ISD::BUILD_VECTOR && N0.Val->hasOneUse() &&
3240 MVT::isVector(VT)) {
3241 bool isSimple = true;
3242 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i)
3243 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
3244 N0.getOperand(i).getOpcode() != ISD::Constant &&
3245 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
3246 isSimple = false;
3247 break;
3248 }
3249
3250 MVT::ValueType DestEltVT = MVT::getVectorElementType(N->getValueType(0));
3251 assert(!MVT::isVector(DestEltVT) &&
3252 "Element type of vector ValueType must not be vector!");
3253 if (isSimple) {
3254 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(N0.Val, DestEltVT);
3255 }
3256 }
3257
Chris Lattner94683772005-12-23 05:30:37 +00003258 // If the input is a constant, let getNode() fold it.
3259 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
3260 SDOperand Res = DAG.getNode(ISD::BIT_CONVERT, VT, N0);
3261 if (Res.Val != N) return Res;
3262 }
3263
Chris Lattnerc8547d82005-12-23 05:37:50 +00003264 if (N0.getOpcode() == ISD::BIT_CONVERT) // conv(conv(x,t1),t2) -> conv(x,t2)
3265 return DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
Chris Lattner6258fb22006-04-02 02:53:43 +00003266
Chris Lattner57104102005-12-23 05:44:41 +00003267 // fold (conv (load x)) -> (load (conv*)x)
Evan Cheng513da432007-10-06 08:19:55 +00003268 // If the resultant load doesn't need a higher alignment than the original!
3269 if (ISD::isNormalLoad(N0.Val) && N0.hasOneUse() &&
Evan Cheng59d5b682007-05-07 21:27:48 +00003270 TLI.isOperationLegal(ISD::LOAD, VT)) {
Evan Cheng466685d2006-10-09 20:57:25 +00003271 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng59d5b682007-05-07 21:27:48 +00003272 unsigned Align = TLI.getTargetMachine().getTargetData()->
Dan Gohmanfcc4dd92007-05-18 18:41:29 +00003273 getABITypeAlignment(MVT::getTypeForValueType(VT));
Evan Cheng59d5b682007-05-07 21:27:48 +00003274 unsigned OrigAlign = LN0->getAlignment();
3275 if (Align <= OrigAlign) {
3276 SDOperand Load = DAG.getLoad(VT, LN0->getChain(), LN0->getBasePtr(),
3277 LN0->getSrcValue(), LN0->getSrcValueOffset(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00003278 LN0->isVolatile(), Align);
Evan Cheng59d5b682007-05-07 21:27:48 +00003279 AddToWorkList(N);
3280 CombineTo(N0.Val, DAG.getNode(ISD::BIT_CONVERT, N0.getValueType(), Load),
3281 Load.getValue(1));
3282 return Load;
3283 }
Chris Lattner57104102005-12-23 05:44:41 +00003284 }
3285
Chris Lattner94683772005-12-23 05:30:37 +00003286 return SDOperand();
3287}
3288
Dan Gohman7f321562007-06-25 16:23:39 +00003289/// ConstantFoldBIT_CONVERTofBUILD_VECTOR - We know that BV is a build_vector
Chris Lattner6258fb22006-04-02 02:53:43 +00003290/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
3291/// destination element value type.
3292SDOperand DAGCombiner::
Dan Gohman7f321562007-06-25 16:23:39 +00003293ConstantFoldBIT_CONVERTofBUILD_VECTOR(SDNode *BV, MVT::ValueType DstEltVT) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003294 MVT::ValueType SrcEltVT = BV->getOperand(0).getValueType();
3295
3296 // If this is already the right type, we're done.
3297 if (SrcEltVT == DstEltVT) return SDOperand(BV, 0);
3298
3299 unsigned SrcBitSize = MVT::getSizeInBits(SrcEltVT);
3300 unsigned DstBitSize = MVT::getSizeInBits(DstEltVT);
3301
3302 // If this is a conversion of N elements of one type to N elements of another
3303 // type, convert each element. This handles FP<->INT cases.
3304 if (SrcBitSize == DstBitSize) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003305 SmallVector<SDOperand, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003306 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003307 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, DstEltVT, BV->getOperand(i)));
Chris Lattner3e104b12006-04-08 04:15:24 +00003308 AddToWorkList(Ops.back().Val);
3309 }
Dan Gohman7f321562007-06-25 16:23:39 +00003310 MVT::ValueType VT =
3311 MVT::getVectorType(DstEltVT,
3312 MVT::getVectorNumElements(BV->getValueType(0)));
3313 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003314 }
3315
3316 // Otherwise, we're growing or shrinking the elements. To avoid having to
3317 // handle annoying details of growing/shrinking FP values, we convert them to
3318 // int first.
3319 if (MVT::isFloatingPoint(SrcEltVT)) {
3320 // Convert the input float vector to a int vector where the elements are the
3321 // same sizes.
3322 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
3323 MVT::ValueType IntVT = SrcEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
Dan Gohman7f321562007-06-25 16:23:39 +00003324 BV = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, IntVT).Val;
Chris Lattner6258fb22006-04-02 02:53:43 +00003325 SrcEltVT = IntVT;
3326 }
3327
3328 // Now we know the input is an integer vector. If the output is a FP type,
3329 // convert to integer first, then to FP of the right size.
3330 if (MVT::isFloatingPoint(DstEltVT)) {
3331 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
3332 MVT::ValueType TmpVT = DstEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
Dan Gohman7f321562007-06-25 16:23:39 +00003333 SDNode *Tmp = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, TmpVT).Val;
Chris Lattner6258fb22006-04-02 02:53:43 +00003334
3335 // Next, convert to FP elements of the same size.
Dan Gohman7f321562007-06-25 16:23:39 +00003336 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(Tmp, DstEltVT);
Chris Lattner6258fb22006-04-02 02:53:43 +00003337 }
3338
3339 // Okay, we know the src/dst types are both integers of differing types.
3340 // Handling growing first.
3341 assert(MVT::isInteger(SrcEltVT) && MVT::isInteger(DstEltVT));
3342 if (SrcBitSize < DstBitSize) {
3343 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
3344
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003345 SmallVector<SDOperand, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003346 for (unsigned i = 0, e = BV->getNumOperands(); i != e;
Chris Lattner6258fb22006-04-02 02:53:43 +00003347 i += NumInputsPerOutput) {
3348 bool isLE = TLI.isLittleEndian();
3349 uint64_t NewBits = 0;
3350 bool EltIsUndef = true;
3351 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
3352 // Shift the previously computed bits over.
3353 NewBits <<= SrcBitSize;
3354 SDOperand Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
3355 if (Op.getOpcode() == ISD::UNDEF) continue;
3356 EltIsUndef = false;
3357
3358 NewBits |= cast<ConstantSDNode>(Op)->getValue();
3359 }
3360
3361 if (EltIsUndef)
3362 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
3363 else
3364 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
3365 }
3366
Dan Gohman7f321562007-06-25 16:23:39 +00003367 MVT::ValueType VT = MVT::getVectorType(DstEltVT,
3368 Ops.size());
3369 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003370 }
3371
3372 // Finally, this must be the case where we are shrinking elements: each input
3373 // turns into multiple outputs.
3374 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003375 SmallVector<SDOperand, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003376 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003377 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
3378 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
3379 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
3380 continue;
3381 }
3382 uint64_t OpVal = cast<ConstantSDNode>(BV->getOperand(i))->getValue();
3383
3384 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
3385 unsigned ThisVal = OpVal & ((1ULL << DstBitSize)-1);
3386 OpVal >>= DstBitSize;
3387 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
3388 }
3389
3390 // For big endian targets, swap the order of the pieces of each element.
3391 if (!TLI.isLittleEndian())
3392 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
3393 }
Dan Gohman7f321562007-06-25 16:23:39 +00003394 MVT::ValueType VT = MVT::getVectorType(DstEltVT, Ops.size());
3395 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003396}
3397
3398
3399
Chris Lattner01b3d732005-09-28 22:28:18 +00003400SDOperand DAGCombiner::visitFADD(SDNode *N) {
3401 SDOperand N0 = N->getOperand(0);
3402 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003403 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3404 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00003405 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00003406
Dan Gohman7f321562007-06-25 16:23:39 +00003407 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00003408 if (MVT::isVector(VT)) {
3409 SDOperand FoldedVOp = SimplifyVBinOp(N);
3410 if (FoldedVOp.Val) return FoldedVOp;
3411 }
Dan Gohman7f321562007-06-25 16:23:39 +00003412
Nate Begemana0e221d2005-10-18 00:28:13 +00003413 // fold (fadd c1, c2) -> c1+c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003414 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003415 return DAG.getNode(ISD::FADD, VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003416 // canonicalize constant to RHS
3417 if (N0CFP && !N1CFP)
3418 return DAG.getNode(ISD::FADD, VT, N1, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00003419 // fold (A + (-B)) -> A-B
Chris Lattner29446522007-05-14 22:04:50 +00003420 if (isNegatibleForFree(N1) == 2)
3421 return DAG.getNode(ISD::FSUB, VT, N0, GetNegatedExpression(N1, DAG));
Chris Lattner01b3d732005-09-28 22:28:18 +00003422 // fold ((-A) + B) -> B-A
Chris Lattner29446522007-05-14 22:04:50 +00003423 if (isNegatibleForFree(N0) == 2)
3424 return DAG.getNode(ISD::FSUB, VT, N1, GetNegatedExpression(N0, DAG));
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003425
3426 // If allowed, fold (fadd (fadd x, c1), c2) -> (fadd x, (fadd c1, c2))
3427 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FADD &&
3428 N0.Val->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
3429 return DAG.getNode(ISD::FADD, VT, N0.getOperand(0),
3430 DAG.getNode(ISD::FADD, VT, N0.getOperand(1), N1));
3431
Chris Lattner01b3d732005-09-28 22:28:18 +00003432 return SDOperand();
3433}
3434
3435SDOperand DAGCombiner::visitFSUB(SDNode *N) {
3436 SDOperand N0 = N->getOperand(0);
3437 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003438 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3439 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00003440 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00003441
Dan Gohman7f321562007-06-25 16:23:39 +00003442 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00003443 if (MVT::isVector(VT)) {
3444 SDOperand FoldedVOp = SimplifyVBinOp(N);
3445 if (FoldedVOp.Val) return FoldedVOp;
3446 }
Dan Gohman7f321562007-06-25 16:23:39 +00003447
Nate Begemana0e221d2005-10-18 00:28:13 +00003448 // fold (fsub c1, c2) -> c1-c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003449 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003450 return DAG.getNode(ISD::FSUB, VT, N0, N1);
Dan Gohman23ff1822007-07-02 15:48:56 +00003451 // fold (0-B) -> -B
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003452 if (UnsafeFPMath && N0CFP && N0CFP->getValueAPF().isZero()) {
Dan Gohman23ff1822007-07-02 15:48:56 +00003453 if (isNegatibleForFree(N1))
3454 return GetNegatedExpression(N1, DAG);
3455 return DAG.getNode(ISD::FNEG, VT, N1);
3456 }
Chris Lattner01b3d732005-09-28 22:28:18 +00003457 // fold (A-(-B)) -> A+B
Chris Lattner29446522007-05-14 22:04:50 +00003458 if (isNegatibleForFree(N1))
3459 return DAG.getNode(ISD::FADD, VT, N0, GetNegatedExpression(N1, DAG));
3460
Chris Lattner01b3d732005-09-28 22:28:18 +00003461 return SDOperand();
3462}
3463
3464SDOperand DAGCombiner::visitFMUL(SDNode *N) {
3465 SDOperand N0 = N->getOperand(0);
3466 SDOperand N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003467 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3468 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00003469 MVT::ValueType VT = N->getValueType(0);
3470
Dan Gohman7f321562007-06-25 16:23:39 +00003471 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00003472 if (MVT::isVector(VT)) {
3473 SDOperand FoldedVOp = SimplifyVBinOp(N);
3474 if (FoldedVOp.Val) return FoldedVOp;
3475 }
Dan Gohman7f321562007-06-25 16:23:39 +00003476
Nate Begeman11af4ea2005-10-17 20:40:11 +00003477 // fold (fmul c1, c2) -> c1*c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003478 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003479 return DAG.getNode(ISD::FMUL, VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003480 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00003481 if (N0CFP && !N1CFP)
3482 return DAG.getNode(ISD::FMUL, VT, N1, N0);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003483 // fold (fmul X, 2.0) -> (fadd X, X)
3484 if (N1CFP && N1CFP->isExactlyValue(+2.0))
3485 return DAG.getNode(ISD::FADD, VT, N0, N0);
Chris Lattner29446522007-05-14 22:04:50 +00003486 // fold (fmul X, -1.0) -> (fneg X)
3487 if (N1CFP && N1CFP->isExactlyValue(-1.0))
3488 return DAG.getNode(ISD::FNEG, VT, N0);
3489
3490 // -X * -Y -> X*Y
3491 if (char LHSNeg = isNegatibleForFree(N0)) {
3492 if (char RHSNeg = isNegatibleForFree(N1)) {
3493 // Both can be negated for free, check to see if at least one is cheaper
3494 // negated.
3495 if (LHSNeg == 2 || RHSNeg == 2)
3496 return DAG.getNode(ISD::FMUL, VT, GetNegatedExpression(N0, DAG),
3497 GetNegatedExpression(N1, DAG));
3498 }
3499 }
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003500
3501 // If allowed, fold (fmul (fmul x, c1), c2) -> (fmul x, (fmul c1, c2))
3502 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FMUL &&
3503 N0.Val->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
3504 return DAG.getNode(ISD::FMUL, VT, N0.getOperand(0),
3505 DAG.getNode(ISD::FMUL, VT, N0.getOperand(1), N1));
3506
Chris Lattner01b3d732005-09-28 22:28:18 +00003507 return SDOperand();
3508}
3509
3510SDOperand DAGCombiner::visitFDIV(SDNode *N) {
3511 SDOperand N0 = N->getOperand(0);
3512 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003513 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3514 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00003515 MVT::ValueType VT = N->getValueType(0);
3516
Dan Gohman7f321562007-06-25 16:23:39 +00003517 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00003518 if (MVT::isVector(VT)) {
3519 SDOperand FoldedVOp = SimplifyVBinOp(N);
3520 if (FoldedVOp.Val) return FoldedVOp;
3521 }
Dan Gohman7f321562007-06-25 16:23:39 +00003522
Nate Begemana148d982006-01-18 22:35:16 +00003523 // fold (fdiv c1, c2) -> c1/c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003524 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003525 return DAG.getNode(ISD::FDIV, VT, N0, N1);
Chris Lattner29446522007-05-14 22:04:50 +00003526
3527
3528 // -X / -Y -> X*Y
3529 if (char LHSNeg = isNegatibleForFree(N0)) {
3530 if (char RHSNeg = isNegatibleForFree(N1)) {
3531 // Both can be negated for free, check to see if at least one is cheaper
3532 // negated.
3533 if (LHSNeg == 2 || RHSNeg == 2)
3534 return DAG.getNode(ISD::FDIV, VT, GetNegatedExpression(N0, DAG),
3535 GetNegatedExpression(N1, DAG));
3536 }
3537 }
3538
Chris Lattner01b3d732005-09-28 22:28:18 +00003539 return SDOperand();
3540}
3541
3542SDOperand DAGCombiner::visitFREM(SDNode *N) {
3543 SDOperand N0 = N->getOperand(0);
3544 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003545 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3546 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00003547 MVT::ValueType VT = N->getValueType(0);
3548
Nate Begemana148d982006-01-18 22:35:16 +00003549 // fold (frem c1, c2) -> fmod(c1,c2)
Dale Johannesendb44bf82007-10-16 23:38:29 +00003550 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003551 return DAG.getNode(ISD::FREM, VT, N0, N1);
Dan Gohman7f321562007-06-25 16:23:39 +00003552
Chris Lattner01b3d732005-09-28 22:28:18 +00003553 return SDOperand();
3554}
3555
Chris Lattner12d83032006-03-05 05:30:57 +00003556SDOperand DAGCombiner::visitFCOPYSIGN(SDNode *N) {
3557 SDOperand N0 = N->getOperand(0);
3558 SDOperand N1 = N->getOperand(1);
3559 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3560 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
3561 MVT::ValueType VT = N->getValueType(0);
3562
Dale Johannesendb44bf82007-10-16 23:38:29 +00003563 if (N0CFP && N1CFP && VT != MVT::ppcf128) // Constant fold
Chris Lattner12d83032006-03-05 05:30:57 +00003564 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1);
3565
3566 if (N1CFP) {
Dale Johannesene6c17422007-08-26 01:18:27 +00003567 const APFloat& V = N1CFP->getValueAPF();
Chris Lattner12d83032006-03-05 05:30:57 +00003568 // copysign(x, c1) -> fabs(x) iff ispos(c1)
3569 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
Dale Johannesen87503a62007-08-25 22:10:57 +00003570 if (!V.isNegative())
Chris Lattner12d83032006-03-05 05:30:57 +00003571 return DAG.getNode(ISD::FABS, VT, N0);
3572 else
3573 return DAG.getNode(ISD::FNEG, VT, DAG.getNode(ISD::FABS, VT, N0));
3574 }
3575
3576 // copysign(fabs(x), y) -> copysign(x, y)
3577 // copysign(fneg(x), y) -> copysign(x, y)
3578 // copysign(copysign(x,z), y) -> copysign(x, y)
3579 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
3580 N0.getOpcode() == ISD::FCOPYSIGN)
3581 return DAG.getNode(ISD::FCOPYSIGN, VT, N0.getOperand(0), N1);
3582
3583 // copysign(x, abs(y)) -> abs(x)
3584 if (N1.getOpcode() == ISD::FABS)
3585 return DAG.getNode(ISD::FABS, VT, N0);
3586
3587 // copysign(x, copysign(y,z)) -> copysign(x, z)
3588 if (N1.getOpcode() == ISD::FCOPYSIGN)
3589 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(1));
3590
3591 // copysign(x, fp_extend(y)) -> copysign(x, y)
3592 // copysign(x, fp_round(y)) -> copysign(x, y)
3593 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
3594 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(0));
3595
3596 return SDOperand();
3597}
3598
3599
Chris Lattner01b3d732005-09-28 22:28:18 +00003600
Nate Begeman83e75ec2005-09-06 04:43:02 +00003601SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003602 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003603 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00003604 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003605
3606 // fold (sint_to_fp c1) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00003607 if (N0C && N0.getValueType() != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003608 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003609 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003610}
3611
Nate Begeman83e75ec2005-09-06 04:43:02 +00003612SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003613 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003614 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00003615 MVT::ValueType VT = N->getValueType(0);
3616
Nate Begeman1d4d4142005-09-01 00:19:25 +00003617 // fold (uint_to_fp c1) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00003618 if (N0C && N0.getValueType() != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003619 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003620 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003621}
3622
Nate Begeman83e75ec2005-09-06 04:43:02 +00003623SDOperand DAGCombiner::visitFP_TO_SINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003624 SDOperand N0 = N->getOperand(0);
3625 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3626 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003627
3628 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00003629 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00003630 return DAG.getNode(ISD::FP_TO_SINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003631 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003632}
3633
Nate Begeman83e75ec2005-09-06 04:43:02 +00003634SDOperand DAGCombiner::visitFP_TO_UINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003635 SDOperand N0 = N->getOperand(0);
3636 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3637 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003638
3639 // fold (fp_to_uint c1fp) -> c1
Dale Johannesendb44bf82007-10-16 23:38:29 +00003640 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003641 return DAG.getNode(ISD::FP_TO_UINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003642 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003643}
3644
Nate Begeman83e75ec2005-09-06 04:43:02 +00003645SDOperand DAGCombiner::visitFP_ROUND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003646 SDOperand N0 = N->getOperand(0);
Chris Lattner0bd48932008-01-17 07:00:52 +00003647 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003648 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3649 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003650
3651 // fold (fp_round c1fp) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00003652 if (N0CFP && N0.getValueType() != MVT::ppcf128)
Chris Lattner0bd48932008-01-17 07:00:52 +00003653 return DAG.getNode(ISD::FP_ROUND, VT, N0, N1);
Chris Lattner79dbea52006-03-13 06:26:26 +00003654
3655 // fold (fp_round (fp_extend x)) -> x
3656 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
3657 return N0.getOperand(0);
3658
Chris Lattner0aa5e6f2008-01-24 06:45:35 +00003659 // fold (fp_round (fp_round x)) -> (fp_round x)
3660 if (N0.getOpcode() == ISD::FP_ROUND) {
3661 // This is a value preserving truncation if both round's are.
3662 bool IsTrunc = N->getConstantOperandVal(1) == 1 &&
3663 N0.Val->getConstantOperandVal(1) == 1;
3664 return DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0),
3665 DAG.getIntPtrConstant(IsTrunc));
3666 }
3667
Chris Lattner79dbea52006-03-13 06:26:26 +00003668 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
3669 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.Val->hasOneUse()) {
Chris Lattner0bd48932008-01-17 07:00:52 +00003670 SDOperand Tmp = DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0), N1);
Chris Lattner79dbea52006-03-13 06:26:26 +00003671 AddToWorkList(Tmp.Val);
3672 return DAG.getNode(ISD::FCOPYSIGN, VT, Tmp, N0.getOperand(1));
3673 }
3674
Nate Begeman83e75ec2005-09-06 04:43:02 +00003675 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003676}
3677
Nate Begeman83e75ec2005-09-06 04:43:02 +00003678SDOperand DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003679 SDOperand N0 = N->getOperand(0);
3680 MVT::ValueType VT = N->getValueType(0);
3681 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00003682 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003683
Nate Begeman1d4d4142005-09-01 00:19:25 +00003684 // fold (fp_round_inreg c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00003685 if (N0CFP) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003686 SDOperand Round = DAG.getConstantFP(N0CFP->getValueAPF(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003687 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003688 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00003689 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003690}
3691
Nate Begeman83e75ec2005-09-06 04:43:02 +00003692SDOperand DAGCombiner::visitFP_EXTEND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003693 SDOperand N0 = N->getOperand(0);
3694 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3695 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003696
Chris Lattner5938bef2007-12-29 06:55:23 +00003697 // If this is fp_round(fpextend), don't fold it, allow ourselves to be folded.
3698 if (N->hasOneUse() && (*N->use_begin())->getOpcode() == ISD::FP_ROUND)
3699 return SDOperand();
Chris Lattner0bd48932008-01-17 07:00:52 +00003700
Nate Begeman1d4d4142005-09-01 00:19:25 +00003701 // fold (fp_extend c1fp) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00003702 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003703 return DAG.getNode(ISD::FP_EXTEND, VT, N0);
Chris Lattner0bd48932008-01-17 07:00:52 +00003704
3705 // Turn fp_extend(fp_round(X, 1)) -> x since the fp_round doesn't affect the
3706 // value of X.
3707 if (N0.getOpcode() == ISD::FP_ROUND && N0.Val->getConstantOperandVal(1) == 1){
3708 SDOperand In = N0.getOperand(0);
3709 if (In.getValueType() == VT) return In;
3710 if (VT < In.getValueType())
3711 return DAG.getNode(ISD::FP_ROUND, VT, In, N0.getOperand(1));
3712 return DAG.getNode(ISD::FP_EXTEND, VT, In);
3713 }
3714
3715 // fold (fpext (load x)) -> (fpext (fptrunc (extload x)))
Dale Johannesenb6210fc2007-10-19 20:29:00 +00003716 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00003717 (!AfterLegalize||TLI.isLoadXLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003718 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3719 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
3720 LN0->getBasePtr(), LN0->getSrcValue(),
3721 LN0->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003722 N0.getValueType(),
3723 LN0->isVolatile(),
3724 LN0->getAlignment());
Chris Lattnere564dbb2006-05-05 21:34:35 +00003725 CombineTo(N, ExtLoad);
Chris Lattner0bd48932008-01-17 07:00:52 +00003726 CombineTo(N0.Val, DAG.getNode(ISD::FP_ROUND, N0.getValueType(), ExtLoad,
3727 DAG.getIntPtrConstant(1)),
Chris Lattnere564dbb2006-05-05 21:34:35 +00003728 ExtLoad.getValue(1));
3729 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
3730 }
3731
3732
Nate Begeman83e75ec2005-09-06 04:43:02 +00003733 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003734}
3735
Nate Begeman83e75ec2005-09-06 04:43:02 +00003736SDOperand DAGCombiner::visitFNEG(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003737 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00003738
Dan Gohman23ff1822007-07-02 15:48:56 +00003739 if (isNegatibleForFree(N0))
3740 return GetNegatedExpression(N0, DAG);
3741
Nate Begeman83e75ec2005-09-06 04:43:02 +00003742 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003743}
3744
Nate Begeman83e75ec2005-09-06 04:43:02 +00003745SDOperand DAGCombiner::visitFABS(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003746 SDOperand N0 = N->getOperand(0);
3747 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3748 MVT::ValueType VT = N->getValueType(0);
3749
Nate Begeman1d4d4142005-09-01 00:19:25 +00003750 // fold (fabs c1) -> fabs(c1)
Dale Johannesendb44bf82007-10-16 23:38:29 +00003751 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003752 return DAG.getNode(ISD::FABS, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003753 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00003754 if (N0.getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00003755 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003756 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00003757 // fold (fabs (fcopysign x, y)) -> (fabs x)
3758 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
3759 return DAG.getNode(ISD::FABS, VT, N0.getOperand(0));
3760
Nate Begeman83e75ec2005-09-06 04:43:02 +00003761 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003762}
3763
Nate Begeman44728a72005-09-19 22:34:01 +00003764SDOperand DAGCombiner::visitBRCOND(SDNode *N) {
3765 SDOperand Chain = N->getOperand(0);
3766 SDOperand N1 = N->getOperand(1);
3767 SDOperand N2 = N->getOperand(2);
3768 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
3769
3770 // never taken branch, fold to chain
3771 if (N1C && N1C->isNullValue())
3772 return Chain;
3773 // unconditional branch
Nate Begemane17daeb2005-10-05 21:43:42 +00003774 if (N1C && N1C->getValue() == 1)
Nate Begeman44728a72005-09-19 22:34:01 +00003775 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
Nate Begeman750ac1b2006-02-01 07:19:44 +00003776 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
3777 // on the target.
3778 if (N1.getOpcode() == ISD::SETCC &&
3779 TLI.isOperationLegal(ISD::BR_CC, MVT::Other)) {
3780 return DAG.getNode(ISD::BR_CC, MVT::Other, Chain, N1.getOperand(2),
3781 N1.getOperand(0), N1.getOperand(1), N2);
3782 }
Nate Begeman44728a72005-09-19 22:34:01 +00003783 return SDOperand();
3784}
3785
Chris Lattner3ea0b472005-10-05 06:47:48 +00003786// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
3787//
Nate Begeman44728a72005-09-19 22:34:01 +00003788SDOperand DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00003789 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
3790 SDOperand CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
3791
3792 // Use SimplifySetCC to simplify SETCC's.
Nate Begemane17daeb2005-10-05 21:43:42 +00003793 SDOperand Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false);
Chris Lattner30f73e72006-10-14 03:52:46 +00003794 if (Simp.Val) AddToWorkList(Simp.Val);
3795
Nate Begemane17daeb2005-10-05 21:43:42 +00003796 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.Val);
3797
3798 // fold br_cc true, dest -> br dest (unconditional branch)
3799 if (SCCC && SCCC->getValue())
3800 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
3801 N->getOperand(4));
3802 // fold br_cc false, dest -> unconditional fall through
3803 if (SCCC && SCCC->isNullValue())
3804 return N->getOperand(0);
Chris Lattner30f73e72006-10-14 03:52:46 +00003805
Nate Begemane17daeb2005-10-05 21:43:42 +00003806 // fold to a simpler setcc
3807 if (Simp.Val && Simp.getOpcode() == ISD::SETCC)
3808 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
3809 Simp.getOperand(2), Simp.getOperand(0),
3810 Simp.getOperand(1), N->getOperand(4));
Nate Begeman44728a72005-09-19 22:34:01 +00003811 return SDOperand();
3812}
3813
Chris Lattner448f2192006-11-11 00:39:41 +00003814
3815/// CombineToPreIndexedLoadStore - Try turning a load / store and a
3816/// pre-indexed load / store when the base pointer is a add or subtract
3817/// and it has other uses besides the load / store. After the
3818/// transformation, the new indexed load / store has effectively folded
3819/// the add / subtract in and all of its other uses are redirected to the
3820/// new load / store.
3821bool DAGCombiner::CombineToPreIndexedLoadStore(SDNode *N) {
3822 if (!AfterLegalize)
3823 return false;
3824
3825 bool isLoad = true;
3826 SDOperand Ptr;
3827 MVT::ValueType VT;
3828 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00003829 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00003830 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00003831 VT = LD->getLoadedVT();
Evan Cheng83060c52007-03-07 08:07:03 +00003832 if (!TLI.isIndexedLoadLegal(ISD::PRE_INC, VT) &&
Chris Lattner448f2192006-11-11 00:39:41 +00003833 !TLI.isIndexedLoadLegal(ISD::PRE_DEC, VT))
3834 return false;
3835 Ptr = LD->getBasePtr();
3836 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00003837 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00003838 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00003839 VT = ST->getStoredVT();
3840 if (!TLI.isIndexedStoreLegal(ISD::PRE_INC, VT) &&
3841 !TLI.isIndexedStoreLegal(ISD::PRE_DEC, VT))
3842 return false;
3843 Ptr = ST->getBasePtr();
3844 isLoad = false;
3845 } else
3846 return false;
3847
Chris Lattner9f1794e2006-11-11 00:56:29 +00003848 // If the pointer is not an add/sub, or if it doesn't have multiple uses, bail
3849 // out. There is no reason to make this a preinc/predec.
3850 if ((Ptr.getOpcode() != ISD::ADD && Ptr.getOpcode() != ISD::SUB) ||
3851 Ptr.Val->hasOneUse())
3852 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00003853
Chris Lattner9f1794e2006-11-11 00:56:29 +00003854 // Ask the target to do addressing mode selection.
3855 SDOperand BasePtr;
3856 SDOperand Offset;
3857 ISD::MemIndexedMode AM = ISD::UNINDEXED;
3858 if (!TLI.getPreIndexedAddressParts(N, BasePtr, Offset, AM, DAG))
3859 return false;
Evan Chenga7d4a042007-05-03 23:52:19 +00003860 // Don't create a indexed load / store with zero offset.
3861 if (isa<ConstantSDNode>(Offset) &&
3862 cast<ConstantSDNode>(Offset)->getValue() == 0)
3863 return false;
Chris Lattner9f1794e2006-11-11 00:56:29 +00003864
Chris Lattner41e53fd2006-11-11 01:00:15 +00003865 // Try turning it into a pre-indexed load / store except when:
Evan Chengc843abe2007-05-24 02:35:39 +00003866 // 1) The new base ptr is a frame index.
3867 // 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 +00003868 // predecessor of the value being stored.
Evan Chengc843abe2007-05-24 02:35:39 +00003869 // 3) Another use of old base ptr is a predecessor of N. If ptr is folded
Chris Lattner9f1794e2006-11-11 00:56:29 +00003870 // that would create a cycle.
Evan Chengc843abe2007-05-24 02:35:39 +00003871 // 4) All uses are load / store ops that use it as old base ptr.
Chris Lattner448f2192006-11-11 00:39:41 +00003872
Chris Lattner41e53fd2006-11-11 01:00:15 +00003873 // Check #1. Preinc'ing a frame index would require copying the stack pointer
3874 // (plus the implicit offset) to a register to preinc anyway.
3875 if (isa<FrameIndexSDNode>(BasePtr))
3876 return false;
3877
3878 // Check #2.
Chris Lattner9f1794e2006-11-11 00:56:29 +00003879 if (!isLoad) {
3880 SDOperand Val = cast<StoreSDNode>(N)->getValue();
Evan Chengc843abe2007-05-24 02:35:39 +00003881 if (Val == BasePtr || BasePtr.Val->isPredecessor(Val.Val))
Chris Lattner9f1794e2006-11-11 00:56:29 +00003882 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00003883 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00003884
Evan Chengc843abe2007-05-24 02:35:39 +00003885 // Now check for #3 and #4.
Chris Lattner9f1794e2006-11-11 00:56:29 +00003886 bool RealUse = false;
3887 for (SDNode::use_iterator I = Ptr.Val->use_begin(),
3888 E = Ptr.Val->use_end(); I != E; ++I) {
3889 SDNode *Use = *I;
3890 if (Use == N)
3891 continue;
3892 if (Use->isPredecessor(N))
3893 return false;
3894
3895 if (!((Use->getOpcode() == ISD::LOAD &&
3896 cast<LoadSDNode>(Use)->getBasePtr() == Ptr) ||
3897 (Use->getOpcode() == ISD::STORE) &&
3898 cast<StoreSDNode>(Use)->getBasePtr() == Ptr))
3899 RealUse = true;
3900 }
3901 if (!RealUse)
3902 return false;
3903
3904 SDOperand Result;
3905 if (isLoad)
3906 Result = DAG.getIndexedLoad(SDOperand(N,0), BasePtr, Offset, AM);
3907 else
3908 Result = DAG.getIndexedStore(SDOperand(N,0), BasePtr, Offset, AM);
3909 ++PreIndexedNodes;
3910 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +00003911 DOUT << "\nReplacing.4 "; DEBUG(N->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +00003912 DOUT << "\nWith: "; DEBUG(Result.Val->dump(&DAG));
3913 DOUT << '\n';
Chris Lattner9f1794e2006-11-11 00:56:29 +00003914 std::vector<SDNode*> NowDead;
3915 if (isLoad) {
3916 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(0),
Chris Lattner01d029b2007-10-15 06:10:22 +00003917 &NowDead);
Chris Lattner9f1794e2006-11-11 00:56:29 +00003918 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1), Result.getValue(2),
Chris Lattner01d029b2007-10-15 06:10:22 +00003919 &NowDead);
Chris Lattner9f1794e2006-11-11 00:56:29 +00003920 } else {
3921 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(1),
Chris Lattner01d029b2007-10-15 06:10:22 +00003922 &NowDead);
Chris Lattner9f1794e2006-11-11 00:56:29 +00003923 }
3924
3925 // Nodes can end up on the worklist more than once. Make sure we do
3926 // not process a node that has been replaced.
3927 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
3928 removeFromWorkList(NowDead[i]);
3929 // Finally, since the node is now dead, remove it from the graph.
3930 DAG.DeleteNode(N);
3931
3932 // Replace the uses of Ptr with uses of the updated base value.
3933 DAG.ReplaceAllUsesOfValueWith(Ptr, Result.getValue(isLoad ? 1 : 0),
Chris Lattner01d029b2007-10-15 06:10:22 +00003934 &NowDead);
Chris Lattner9f1794e2006-11-11 00:56:29 +00003935 removeFromWorkList(Ptr.Val);
3936 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
3937 removeFromWorkList(NowDead[i]);
3938 DAG.DeleteNode(Ptr.Val);
3939
3940 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00003941}
3942
3943/// CombineToPostIndexedLoadStore - Try combine a load / store with a
3944/// add / sub of the base pointer node into a post-indexed load / store.
3945/// The transformation folded the add / subtract into the new indexed
3946/// load / store effectively and all of its uses are redirected to the
3947/// new load / store.
3948bool DAGCombiner::CombineToPostIndexedLoadStore(SDNode *N) {
3949 if (!AfterLegalize)
3950 return false;
3951
3952 bool isLoad = true;
3953 SDOperand Ptr;
3954 MVT::ValueType VT;
3955 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00003956 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00003957 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00003958 VT = LD->getLoadedVT();
3959 if (!TLI.isIndexedLoadLegal(ISD::POST_INC, VT) &&
3960 !TLI.isIndexedLoadLegal(ISD::POST_DEC, VT))
3961 return false;
3962 Ptr = LD->getBasePtr();
3963 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00003964 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00003965 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00003966 VT = ST->getStoredVT();
3967 if (!TLI.isIndexedStoreLegal(ISD::POST_INC, VT) &&
3968 !TLI.isIndexedStoreLegal(ISD::POST_DEC, VT))
3969 return false;
3970 Ptr = ST->getBasePtr();
3971 isLoad = false;
3972 } else
3973 return false;
3974
Evan Chengcc470212006-11-16 00:08:20 +00003975 if (Ptr.Val->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00003976 return false;
3977
3978 for (SDNode::use_iterator I = Ptr.Val->use_begin(),
3979 E = Ptr.Val->use_end(); I != E; ++I) {
3980 SDNode *Op = *I;
3981 if (Op == N ||
3982 (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB))
3983 continue;
3984
3985 SDOperand BasePtr;
3986 SDOperand Offset;
3987 ISD::MemIndexedMode AM = ISD::UNINDEXED;
3988 if (TLI.getPostIndexedAddressParts(N, Op, BasePtr, Offset, AM, DAG)) {
3989 if (Ptr == Offset)
3990 std::swap(BasePtr, Offset);
3991 if (Ptr != BasePtr)
Chris Lattner448f2192006-11-11 00:39:41 +00003992 continue;
Evan Chenga7d4a042007-05-03 23:52:19 +00003993 // Don't create a indexed load / store with zero offset.
3994 if (isa<ConstantSDNode>(Offset) &&
3995 cast<ConstantSDNode>(Offset)->getValue() == 0)
3996 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00003997
Chris Lattner9f1794e2006-11-11 00:56:29 +00003998 // Try turning it into a post-indexed load / store except when
3999 // 1) All uses are load / store ops that use it as base ptr.
4000 // 2) Op must be independent of N, i.e. Op is neither a predecessor
4001 // nor a successor of N. Otherwise, if Op is folded that would
4002 // create a cycle.
4003
4004 // Check for #1.
4005 bool TryNext = false;
4006 for (SDNode::use_iterator II = BasePtr.Val->use_begin(),
4007 EE = BasePtr.Val->use_end(); II != EE; ++II) {
4008 SDNode *Use = *II;
4009 if (Use == Ptr.Val)
Chris Lattner448f2192006-11-11 00:39:41 +00004010 continue;
4011
Chris Lattner9f1794e2006-11-11 00:56:29 +00004012 // If all the uses are load / store addresses, then don't do the
4013 // transformation.
4014 if (Use->getOpcode() == ISD::ADD || Use->getOpcode() == ISD::SUB){
4015 bool RealUse = false;
4016 for (SDNode::use_iterator III = Use->use_begin(),
4017 EEE = Use->use_end(); III != EEE; ++III) {
4018 SDNode *UseUse = *III;
4019 if (!((UseUse->getOpcode() == ISD::LOAD &&
4020 cast<LoadSDNode>(UseUse)->getBasePtr().Val == Use) ||
4021 (UseUse->getOpcode() == ISD::STORE) &&
4022 cast<StoreSDNode>(UseUse)->getBasePtr().Val == Use))
4023 RealUse = true;
4024 }
Chris Lattner448f2192006-11-11 00:39:41 +00004025
Chris Lattner9f1794e2006-11-11 00:56:29 +00004026 if (!RealUse) {
4027 TryNext = true;
4028 break;
Chris Lattner448f2192006-11-11 00:39:41 +00004029 }
4030 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004031 }
4032 if (TryNext)
4033 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00004034
Chris Lattner9f1794e2006-11-11 00:56:29 +00004035 // Check for #2
4036 if (!Op->isPredecessor(N) && !N->isPredecessor(Op)) {
4037 SDOperand Result = isLoad
4038 ? DAG.getIndexedLoad(SDOperand(N,0), BasePtr, Offset, AM)
4039 : DAG.getIndexedStore(SDOperand(N,0), BasePtr, Offset, AM);
4040 ++PostIndexedNodes;
4041 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +00004042 DOUT << "\nReplacing.5 "; DEBUG(N->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +00004043 DOUT << "\nWith: "; DEBUG(Result.Val->dump(&DAG));
4044 DOUT << '\n';
Chris Lattner9f1794e2006-11-11 00:56:29 +00004045 std::vector<SDNode*> NowDead;
4046 if (isLoad) {
4047 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(0),
Chris Lattner01d029b2007-10-15 06:10:22 +00004048 &NowDead);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004049 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1), Result.getValue(2),
Chris Lattner01d029b2007-10-15 06:10:22 +00004050 &NowDead);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004051 } else {
4052 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(1),
Chris Lattner01d029b2007-10-15 06:10:22 +00004053 &NowDead);
Chris Lattner448f2192006-11-11 00:39:41 +00004054 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004055
4056 // Nodes can end up on the worklist more than once. Make sure we do
4057 // not process a node that has been replaced.
4058 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
4059 removeFromWorkList(NowDead[i]);
4060 // Finally, since the node is now dead, remove it from the graph.
4061 DAG.DeleteNode(N);
4062
4063 // Replace the uses of Use with uses of the updated base value.
4064 DAG.ReplaceAllUsesOfValueWith(SDOperand(Op, 0),
4065 Result.getValue(isLoad ? 1 : 0),
Chris Lattner01d029b2007-10-15 06:10:22 +00004066 &NowDead);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004067 removeFromWorkList(Op);
4068 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
4069 removeFromWorkList(NowDead[i]);
4070 DAG.DeleteNode(Op);
4071
4072 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00004073 }
4074 }
4075 }
4076 return false;
4077}
4078
Chris Lattner00161a62008-01-25 07:20:16 +00004079/// InferAlignment - If we can infer some alignment information from this
4080/// pointer, return it.
4081static unsigned InferAlignment(SDOperand Ptr, SelectionDAG &DAG) {
4082 // If this is a direct reference to a stack slot, use information about the
4083 // stack slot's alignment.
4084 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
4085 return DAG.getMachineFunction().getFrameInfo()->
4086 getObjectAlignment(FI->getIndex());
4087 }
4088
4089 // FIXME: Handle FI+CST.
4090
4091 return 0;
4092}
Chris Lattner448f2192006-11-11 00:39:41 +00004093
Chris Lattner01a22022005-10-10 22:04:48 +00004094SDOperand DAGCombiner::visitLOAD(SDNode *N) {
Evan Cheng466685d2006-10-09 20:57:25 +00004095 LoadSDNode *LD = cast<LoadSDNode>(N);
4096 SDOperand Chain = LD->getChain();
4097 SDOperand Ptr = LD->getBasePtr();
Chris Lattner00161a62008-01-25 07:20:16 +00004098
4099 // Try to infer better alignment information than the load already has.
4100 if (LD->isUnindexed()) {
4101 if (unsigned Align = InferAlignment(Ptr, DAG)) {
4102 if (Align > LD->getAlignment())
4103 return DAG.getExtLoad(LD->getExtensionType(), LD->getValueType(0),
4104 Chain, Ptr, LD->getSrcValue(),
4105 LD->getSrcValueOffset(), LD->getLoadedVT(),
4106 LD->isVolatile(), Align);
4107 }
4108 }
4109
Evan Cheng45a7ca92007-05-01 00:38:21 +00004110
4111 // If load is not volatile and there are no uses of the loaded value (and
4112 // the updated indexed value in case of indexed loads), change uses of the
4113 // chain value into uses of the chain input (i.e. delete the dead load).
4114 if (!LD->isVolatile()) {
Evan Cheng498f5592007-05-01 08:53:39 +00004115 if (N->getValueType(1) == MVT::Other) {
4116 // Unindexed loads.
Evan Cheng02c42852008-01-16 23:11:54 +00004117 if (N->hasNUsesOfValue(0, 0)) {
4118 // It's not safe to use the two value CombineTo variant here. e.g.
4119 // v1, chain2 = load chain1, loc
4120 // v2, chain3 = load chain2, loc
4121 // v3 = add v2, c
Chris Lattner125991a2008-01-24 07:57:06 +00004122 // Now we replace use of chain2 with chain1. This makes the second load
4123 // isomorphic to the one we are deleting, and thus makes this load live.
Evan Cheng02c42852008-01-16 23:11:54 +00004124 std::vector<SDNode*> NowDead;
Evan Cheng02c42852008-01-16 23:11:54 +00004125 DOUT << "\nReplacing.6 "; DEBUG(N->dump(&DAG));
Chris Lattner125991a2008-01-24 07:57:06 +00004126 DOUT << "\nWith chain: "; DEBUG(Chain.Val->dump(&DAG));
4127 DOUT << "\n";
Evan Cheng02c42852008-01-16 23:11:54 +00004128 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1), Chain, &NowDead);
Evan Cheng02c42852008-01-16 23:11:54 +00004129 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
4130 removeFromWorkList(NowDead[i]);
Chris Lattner125991a2008-01-24 07:57:06 +00004131 if (N->use_empty()) {
4132 removeFromWorkList(N);
4133 DAG.DeleteNode(N);
4134 }
Evan Cheng02c42852008-01-16 23:11:54 +00004135 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
4136 }
Evan Cheng498f5592007-05-01 08:53:39 +00004137 } else {
4138 // Indexed loads.
4139 assert(N->getValueType(2) == MVT::Other && "Malformed indexed loads?");
4140 if (N->hasNUsesOfValue(0, 0) && N->hasNUsesOfValue(0, 1)) {
Evan Cheng02c42852008-01-16 23:11:54 +00004141 std::vector<SDNode*> NowDead;
4142 SDOperand Undef = DAG.getNode(ISD::UNDEF, N->getValueType(0));
4143 DOUT << "\nReplacing.6 "; DEBUG(N->dump(&DAG));
4144 DOUT << "\nWith: "; DEBUG(Undef.Val->dump(&DAG));
4145 DOUT << " and 2 other values\n";
4146 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Undef, &NowDead);
4147 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1),
Chris Lattner4626b252008-01-17 07:20:38 +00004148 DAG.getNode(ISD::UNDEF, N->getValueType(1)),
Evan Cheng02c42852008-01-16 23:11:54 +00004149 &NowDead);
4150 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 2), Chain, &NowDead);
4151 removeFromWorkList(N);
4152 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
4153 removeFromWorkList(NowDead[i]);
4154 DAG.DeleteNode(N);
4155 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng45a7ca92007-05-01 00:38:21 +00004156 }
Evan Cheng45a7ca92007-05-01 00:38:21 +00004157 }
4158 }
Chris Lattner01a22022005-10-10 22:04:48 +00004159
4160 // If this load is directly stored, replace the load value with the stored
4161 // value.
4162 // TODO: Handle store large -> read small portion.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004163 // TODO: Handle TRUNCSTORE/LOADEXT
4164 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00004165 if (ISD::isNON_TRUNCStore(Chain.Val)) {
4166 StoreSDNode *PrevST = cast<StoreSDNode>(Chain);
4167 if (PrevST->getBasePtr() == Ptr &&
4168 PrevST->getValue().getValueType() == N->getValueType(0))
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004169 return CombineTo(N, Chain.getOperand(1), Chain);
Evan Cheng8b2794a2006-10-13 21:14:26 +00004170 }
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004171 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00004172
Jim Laskey7ca56af2006-10-11 13:47:09 +00004173 if (CombinerAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00004174 // Walk up chain skipping non-aliasing memory nodes.
4175 SDOperand BetterChain = FindBetterChain(N, Chain);
4176
Jim Laskey6ff23e52006-10-04 16:53:27 +00004177 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00004178 if (Chain != BetterChain) {
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004179 SDOperand ReplLoad;
4180
Jim Laskey279f0532006-09-25 16:29:54 +00004181 // Replace the chain to void dependency.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004182 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
4183 ReplLoad = DAG.getLoad(N->getValueType(0), BetterChain, Ptr,
Duncan Sandsdc846502007-10-28 12:59:45 +00004184 LD->getSrcValue(), LD->getSrcValueOffset(),
4185 LD->isVolatile(), LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004186 } else {
4187 ReplLoad = DAG.getExtLoad(LD->getExtensionType(),
4188 LD->getValueType(0),
4189 BetterChain, Ptr, LD->getSrcValue(),
4190 LD->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00004191 LD->getLoadedVT(),
4192 LD->isVolatile(),
4193 LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004194 }
Jim Laskey279f0532006-09-25 16:29:54 +00004195
Jim Laskey6ff23e52006-10-04 16:53:27 +00004196 // Create token factor to keep old chain connected.
Jim Laskey288af5e2006-09-25 19:32:58 +00004197 SDOperand Token = DAG.getNode(ISD::TokenFactor, MVT::Other,
4198 Chain, ReplLoad.getValue(1));
Jim Laskey6ff23e52006-10-04 16:53:27 +00004199
Jim Laskey274062c2006-10-13 23:32:28 +00004200 // Replace uses with load result and token factor. Don't add users
4201 // to work list.
4202 return CombineTo(N, ReplLoad.getValue(0), Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00004203 }
4204 }
4205
Evan Cheng7fc033a2006-11-03 03:06:21 +00004206 // Try transforming N to an indexed load.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00004207 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Evan Cheng7fc033a2006-11-03 03:06:21 +00004208 return SDOperand(N, 0);
4209
Chris Lattner01a22022005-10-10 22:04:48 +00004210 return SDOperand();
4211}
4212
Chris Lattner07649d92008-01-08 23:08:06 +00004213
Chris Lattner87514ca2005-10-10 22:31:19 +00004214SDOperand DAGCombiner::visitSTORE(SDNode *N) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00004215 StoreSDNode *ST = cast<StoreSDNode>(N);
4216 SDOperand Chain = ST->getChain();
4217 SDOperand Value = ST->getValue();
4218 SDOperand Ptr = ST->getBasePtr();
Jim Laskey7aed46c2006-10-11 18:55:16 +00004219
Chris Lattner00161a62008-01-25 07:20:16 +00004220 // Try to infer better alignment information than the store already has.
4221 if (ST->isUnindexed()) {
4222 if (unsigned Align = InferAlignment(Ptr, DAG)) {
4223 if (Align > ST->getAlignment())
4224 return DAG.getTruncStore(Chain, Value, Ptr, ST->getSrcValue(),
4225 ST->getSrcValueOffset(), ST->getStoredVT(),
4226 ST->isVolatile(), Align);
4227 }
4228 }
4229
Evan Cheng59d5b682007-05-07 21:27:48 +00004230 // If this is a store of a bit convert, store the input value if the
Evan Cheng2c4f9432007-05-09 21:49:47 +00004231 // resultant store does not need a higher alignment than the original.
Dale Johannesen98a6c622007-05-16 22:45:30 +00004232 if (Value.getOpcode() == ISD::BIT_CONVERT && !ST->isTruncatingStore() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00004233 ST->isUnindexed()) {
Evan Cheng59d5b682007-05-07 21:27:48 +00004234 unsigned Align = ST->getAlignment();
4235 MVT::ValueType SVT = Value.getOperand(0).getValueType();
4236 unsigned OrigAlign = TLI.getTargetMachine().getTargetData()->
Dan Gohmanfcc4dd92007-05-18 18:41:29 +00004237 getABITypeAlignment(MVT::getTypeForValueType(SVT));
Evan Chengc2cd2b22007-05-07 21:36:06 +00004238 if (Align <= OrigAlign && TLI.isOperationLegal(ISD::STORE, SVT))
Evan Cheng59d5b682007-05-07 21:27:48 +00004239 return DAG.getStore(Chain, Value.getOperand(0), Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004240 ST->getSrcValueOffset(), ST->isVolatile(), Align);
Jim Laskey279f0532006-09-25 16:29:54 +00004241 }
4242
Nate Begeman2cbba892006-12-11 02:23:46 +00004243 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Nate Begeman2cbba892006-12-11 02:23:46 +00004244 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Value)) {
Evan Cheng25ece662006-12-11 17:25:19 +00004245 if (Value.getOpcode() != ISD::TargetConstantFP) {
4246 SDOperand Tmp;
Chris Lattner62be1a72006-12-12 04:16:14 +00004247 switch (CFP->getValueType(0)) {
4248 default: assert(0 && "Unknown FP type");
Dale Johannesenc7b21d52007-09-18 18:36:59 +00004249 case MVT::f80: // We don't do this for these yet.
4250 case MVT::f128:
4251 case MVT::ppcf128:
4252 break;
Chris Lattner62be1a72006-12-12 04:16:14 +00004253 case MVT::f32:
4254 if (!AfterLegalize || TLI.isTypeLegal(MVT::i32)) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00004255 Tmp = DAG.getConstant((uint32_t)CFP->getValueAPF().
4256 convertToAPInt().getZExtValue(), MVT::i32);
Chris Lattner62be1a72006-12-12 04:16:14 +00004257 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004258 ST->getSrcValueOffset(), ST->isVolatile(),
4259 ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00004260 }
4261 break;
4262 case MVT::f64:
4263 if (!AfterLegalize || TLI.isTypeLegal(MVT::i64)) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00004264 Tmp = DAG.getConstant(CFP->getValueAPF().convertToAPInt().
4265 getZExtValue(), MVT::i64);
Chris Lattner62be1a72006-12-12 04:16:14 +00004266 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004267 ST->getSrcValueOffset(), ST->isVolatile(),
4268 ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00004269 } else if (TLI.isTypeLegal(MVT::i32)) {
Duncan Sandsdc846502007-10-28 12:59:45 +00004270 // Many FP stores are not made apparent until after legalize, e.g. for
Chris Lattner62be1a72006-12-12 04:16:14 +00004271 // argument passing. Since this is so common, custom legalize the
4272 // 64-bit integer store into two 32-bit stores.
Dale Johannesen9d5f4562007-09-12 03:30:33 +00004273 uint64_t Val = CFP->getValueAPF().convertToAPInt().getZExtValue();
Chris Lattner62be1a72006-12-12 04:16:14 +00004274 SDOperand Lo = DAG.getConstant(Val & 0xFFFFFFFF, MVT::i32);
4275 SDOperand Hi = DAG.getConstant(Val >> 32, MVT::i32);
4276 if (!TLI.isLittleEndian()) std::swap(Lo, Hi);
4277
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004278 int SVOffset = ST->getSrcValueOffset();
4279 unsigned Alignment = ST->getAlignment();
4280 bool isVolatile = ST->isVolatile();
4281
Chris Lattner62be1a72006-12-12 04:16:14 +00004282 SDOperand St0 = DAG.getStore(Chain, Lo, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004283 ST->getSrcValueOffset(),
4284 isVolatile, ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00004285 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
4286 DAG.getConstant(4, Ptr.getValueType()));
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004287 SVOffset += 4;
Duncan Sandsdc846502007-10-28 12:59:45 +00004288 Alignment = MinAlign(Alignment, 4U);
Chris Lattner62be1a72006-12-12 04:16:14 +00004289 SDOperand St1 = DAG.getStore(Chain, Hi, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004290 SVOffset, isVolatile, Alignment);
Chris Lattner62be1a72006-12-12 04:16:14 +00004291 return DAG.getNode(ISD::TokenFactor, MVT::Other, St0, St1);
4292 }
4293 break;
Evan Cheng25ece662006-12-11 17:25:19 +00004294 }
Nate Begeman2cbba892006-12-11 02:23:46 +00004295 }
Nate Begeman2cbba892006-12-11 02:23:46 +00004296 }
4297
Jim Laskey279f0532006-09-25 16:29:54 +00004298 if (CombinerAA) {
4299 // Walk up chain skipping non-aliasing memory nodes.
4300 SDOperand BetterChain = FindBetterChain(N, Chain);
4301
Jim Laskey6ff23e52006-10-04 16:53:27 +00004302 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00004303 if (Chain != BetterChain) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00004304 // Replace the chain to avoid dependency.
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004305 SDOperand ReplStore;
4306 if (ST->isTruncatingStore()) {
4307 ReplStore = DAG.getTruncStore(BetterChain, Value, Ptr,
Chris Lattner4626b252008-01-17 07:20:38 +00004308 ST->getSrcValue(),ST->getSrcValueOffset(),
4309 ST->getStoredVT(),
4310 ST->isVolatile(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004311 } else {
4312 ReplStore = DAG.getStore(BetterChain, Value, Ptr,
Chris Lattner4626b252008-01-17 07:20:38 +00004313 ST->getSrcValue(), ST->getSrcValueOffset(),
4314 ST->isVolatile(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004315 }
4316
Jim Laskey279f0532006-09-25 16:29:54 +00004317 // Create token to keep both nodes around.
Jim Laskey274062c2006-10-13 23:32:28 +00004318 SDOperand Token =
4319 DAG.getNode(ISD::TokenFactor, MVT::Other, Chain, ReplStore);
4320
4321 // Don't add users to work list.
4322 return CombineTo(N, Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00004323 }
Jim Laskeyd1aed7a2006-09-21 16:28:59 +00004324 }
Chris Lattnerc33baaa2005-12-23 05:48:07 +00004325
Evan Cheng33dbedc2006-11-05 09:31:14 +00004326 // Try transforming N to an indexed store.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00004327 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Evan Cheng33dbedc2006-11-05 09:31:14 +00004328 return SDOperand(N, 0);
4329
Chris Lattner3c872852007-12-29 06:26:16 +00004330 // FIXME: is there such a thing as a truncating indexed store?
Chris Lattnerddf89562008-01-17 19:59:44 +00004331 if (ST->isTruncatingStore() && ST->isUnindexed() &&
Chris Lattner2b4c2792007-10-13 06:35:54 +00004332 MVT::isInteger(Value.getValueType())) {
4333 // See if we can simplify the input to this truncstore with knowledge that
4334 // only the low bits are being used. For example:
4335 // "truncstore (or (shl x, 8), y), i8" -> "truncstore y, i8"
4336 SDOperand Shorter =
4337 GetDemandedBits(Value, MVT::getIntVTBitMask(ST->getStoredVT()));
4338 AddToWorkList(Value.Val);
4339 if (Shorter.Val)
4340 return DAG.getTruncStore(Chain, Shorter, Ptr, ST->getSrcValue(),
4341 ST->getSrcValueOffset(), ST->getStoredVT(),
4342 ST->isVolatile(), ST->getAlignment());
Chris Lattnere33544c2007-10-13 06:58:48 +00004343
4344 // Otherwise, see if we can simplify the operation with
4345 // SimplifyDemandedBits, which only works if the value has a single use.
4346 if (SimplifyDemandedBits(Value, MVT::getIntVTBitMask(ST->getStoredVT())))
4347 return SDOperand(N, 0);
Chris Lattner2b4c2792007-10-13 06:35:54 +00004348 }
4349
Chris Lattner3c872852007-12-29 06:26:16 +00004350 // If this is a load followed by a store to the same location, then the store
4351 // is dead/noop.
4352 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Value)) {
Chris Lattner07649d92008-01-08 23:08:06 +00004353 if (Ld->getBasePtr() == Ptr && ST->getStoredVT() == Ld->getLoadedVT() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00004354 ST->isUnindexed() && !ST->isVolatile() &&
Chris Lattner07649d92008-01-08 23:08:06 +00004355 // There can't be any side effects between the load and store, such as
4356 // a call or store.
Chris Lattner572dee72008-01-16 05:49:24 +00004357 Chain.reachesChainWithoutSideEffects(SDOperand(Ld, 1))) {
Chris Lattner3c872852007-12-29 06:26:16 +00004358 // The store is dead, remove it.
4359 return Chain;
4360 }
4361 }
4362
Chris Lattnerddf89562008-01-17 19:59:44 +00004363 // If this is an FP_ROUND or TRUNC followed by a store, fold this into a
4364 // truncating store. We can do this even if this is already a truncstore.
4365 if ((Value.getOpcode() == ISD::FP_ROUND || Value.getOpcode() == ISD::TRUNCATE)
4366 && TLI.isTypeLegal(Value.getOperand(0).getValueType()) &&
4367 Value.Val->hasOneUse() && ST->isUnindexed() &&
4368 TLI.isTruncStoreLegal(Value.getOperand(0).getValueType(),
4369 ST->getStoredVT())) {
4370 return DAG.getTruncStore(Chain, Value.getOperand(0), Ptr, ST->getSrcValue(),
4371 ST->getSrcValueOffset(), ST->getStoredVT(),
4372 ST->isVolatile(), ST->getAlignment());
4373 }
4374
Chris Lattner87514ca2005-10-10 22:31:19 +00004375 return SDOperand();
4376}
4377
Chris Lattnerca242442006-03-19 01:27:56 +00004378SDOperand DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
4379 SDOperand InVec = N->getOperand(0);
4380 SDOperand InVal = N->getOperand(1);
4381 SDOperand EltNo = N->getOperand(2);
4382
4383 // If the invec is a BUILD_VECTOR and if EltNo is a constant, build a new
4384 // vector with the inserted element.
4385 if (InVec.getOpcode() == ISD::BUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
4386 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004387 SmallVector<SDOperand, 8> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
Chris Lattnerca242442006-03-19 01:27:56 +00004388 if (Elt < Ops.size())
4389 Ops[Elt] = InVal;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004390 return DAG.getNode(ISD::BUILD_VECTOR, InVec.getValueType(),
4391 &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00004392 }
4393
4394 return SDOperand();
4395}
4396
Evan Cheng513da432007-10-06 08:19:55 +00004397SDOperand DAGCombiner::visitEXTRACT_VECTOR_ELT(SDNode *N) {
4398 SDOperand InVec = N->getOperand(0);
4399 SDOperand EltNo = N->getOperand(1);
4400
4401 // (vextract (v4f32 s2v (f32 load $addr)), 0) -> (f32 load $addr)
4402 // (vextract (v4i32 bc (v4f32 s2v (f32 load $addr))), 0) -> (i32 load $addr)
4403 if (isa<ConstantSDNode>(EltNo)) {
4404 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
4405 bool NewLoad = false;
4406 if (Elt == 0) {
4407 MVT::ValueType VT = InVec.getValueType();
4408 MVT::ValueType EVT = MVT::getVectorElementType(VT);
4409 MVT::ValueType LVT = EVT;
4410 unsigned NumElts = MVT::getVectorNumElements(VT);
4411 if (InVec.getOpcode() == ISD::BIT_CONVERT) {
4412 MVT::ValueType BCVT = InVec.getOperand(0).getValueType();
Dan Gohman090b38a2007-10-29 20:44:42 +00004413 if (!MVT::isVector(BCVT) ||
4414 NumElts != MVT::getVectorNumElements(BCVT))
Evan Cheng513da432007-10-06 08:19:55 +00004415 return SDOperand();
4416 InVec = InVec.getOperand(0);
4417 EVT = MVT::getVectorElementType(BCVT);
4418 NewLoad = true;
4419 }
4420 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR &&
4421 InVec.getOperand(0).getValueType() == EVT &&
4422 ISD::isNormalLoad(InVec.getOperand(0).Val) &&
4423 InVec.getOperand(0).hasOneUse()) {
4424 LoadSDNode *LN0 = cast<LoadSDNode>(InVec.getOperand(0));
4425 unsigned Align = LN0->getAlignment();
4426 if (NewLoad) {
4427 // Check the resultant load doesn't need a higher alignment than the
4428 // original load.
4429 unsigned NewAlign = TLI.getTargetMachine().getTargetData()->
4430 getABITypeAlignment(MVT::getTypeForValueType(LVT));
4431 if (!TLI.isOperationLegal(ISD::LOAD, LVT) || NewAlign > Align)
4432 return SDOperand();
4433 Align = NewAlign;
4434 }
4435
4436 return DAG.getLoad(LVT, LN0->getChain(), LN0->getBasePtr(),
4437 LN0->getSrcValue(), LN0->getSrcValueOffset(),
4438 LN0->isVolatile(), Align);
4439 }
4440 }
4441 }
4442 return SDOperand();
4443}
4444
4445
Dan Gohman7f321562007-06-25 16:23:39 +00004446SDOperand DAGCombiner::visitBUILD_VECTOR(SDNode *N) {
4447 unsigned NumInScalars = N->getNumOperands();
4448 MVT::ValueType VT = N->getValueType(0);
4449 unsigned NumElts = MVT::getVectorNumElements(VT);
4450 MVT::ValueType EltType = MVT::getVectorElementType(VT);
Chris Lattnerca242442006-03-19 01:27:56 +00004451
Dan Gohman7f321562007-06-25 16:23:39 +00004452 // Check to see if this is a BUILD_VECTOR of a bunch of EXTRACT_VECTOR_ELT
4453 // operations. If so, and if the EXTRACT_VECTOR_ELT vector inputs come from
4454 // at most two distinct vectors, turn this into a shuffle node.
Chris Lattnerd7648c82006-03-28 20:28:38 +00004455 SDOperand VecIn1, VecIn2;
4456 for (unsigned i = 0; i != NumInScalars; ++i) {
4457 // Ignore undef inputs.
4458 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
4459
Dan Gohman7f321562007-06-25 16:23:39 +00004460 // If this input is something other than a EXTRACT_VECTOR_ELT with a
Chris Lattnerd7648c82006-03-28 20:28:38 +00004461 // constant index, bail out.
Dan Gohman7f321562007-06-25 16:23:39 +00004462 if (N->getOperand(i).getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
Chris Lattnerd7648c82006-03-28 20:28:38 +00004463 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
4464 VecIn1 = VecIn2 = SDOperand(0, 0);
4465 break;
4466 }
4467
Dan Gohman7f321562007-06-25 16:23:39 +00004468 // If the input vector type disagrees with the result of the build_vector,
Chris Lattnerd7648c82006-03-28 20:28:38 +00004469 // we can't make a shuffle.
4470 SDOperand ExtractedFromVec = N->getOperand(i).getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00004471 if (ExtractedFromVec.getValueType() != VT) {
Chris Lattnerd7648c82006-03-28 20:28:38 +00004472 VecIn1 = VecIn2 = SDOperand(0, 0);
4473 break;
4474 }
4475
4476 // Otherwise, remember this. We allow up to two distinct input vectors.
4477 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
4478 continue;
4479
4480 if (VecIn1.Val == 0) {
4481 VecIn1 = ExtractedFromVec;
4482 } else if (VecIn2.Val == 0) {
4483 VecIn2 = ExtractedFromVec;
4484 } else {
4485 // Too many inputs.
4486 VecIn1 = VecIn2 = SDOperand(0, 0);
4487 break;
4488 }
4489 }
4490
4491 // If everything is good, we can make a shuffle operation.
4492 if (VecIn1.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004493 SmallVector<SDOperand, 8> BuildVecIndices;
Chris Lattnerd7648c82006-03-28 20:28:38 +00004494 for (unsigned i = 0; i != NumInScalars; ++i) {
4495 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
Evan Cheng597a3bd2007-01-20 10:10:26 +00004496 BuildVecIndices.push_back(DAG.getNode(ISD::UNDEF, TLI.getPointerTy()));
Chris Lattnerd7648c82006-03-28 20:28:38 +00004497 continue;
4498 }
4499
4500 SDOperand Extract = N->getOperand(i);
4501
4502 // If extracting from the first vector, just use the index directly.
4503 if (Extract.getOperand(0) == VecIn1) {
4504 BuildVecIndices.push_back(Extract.getOperand(1));
4505 continue;
4506 }
4507
4508 // Otherwise, use InIdx + VecSize
4509 unsigned Idx = cast<ConstantSDNode>(Extract.getOperand(1))->getValue();
Chris Lattner0bd48932008-01-17 07:00:52 +00004510 BuildVecIndices.push_back(DAG.getIntPtrConstant(Idx+NumInScalars));
Chris Lattnerd7648c82006-03-28 20:28:38 +00004511 }
4512
4513 // Add count and size info.
Chris Lattner0bd48932008-01-17 07:00:52 +00004514 MVT::ValueType BuildVecVT = MVT::getVectorType(TLI.getPointerTy(), NumElts);
Chris Lattnerd7648c82006-03-28 20:28:38 +00004515
Dan Gohman7f321562007-06-25 16:23:39 +00004516 // Return the new VECTOR_SHUFFLE node.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004517 SDOperand Ops[5];
4518 Ops[0] = VecIn1;
Chris Lattnercef896e2006-03-28 22:19:47 +00004519 if (VecIn2.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004520 Ops[1] = VecIn2;
Chris Lattnercef896e2006-03-28 22:19:47 +00004521 } else {
Dan Gohman7f321562007-06-25 16:23:39 +00004522 // Use an undef build_vector as input for the second operand.
Chris Lattnercef896e2006-03-28 22:19:47 +00004523 std::vector<SDOperand> UnOps(NumInScalars,
4524 DAG.getNode(ISD::UNDEF,
Dan Gohman7f321562007-06-25 16:23:39 +00004525 EltType));
4526 Ops[1] = DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004527 &UnOps[0], UnOps.size());
4528 AddToWorkList(Ops[1].Val);
Chris Lattnercef896e2006-03-28 22:19:47 +00004529 }
Dan Gohman7f321562007-06-25 16:23:39 +00004530 Ops[2] = DAG.getNode(ISD::BUILD_VECTOR, BuildVecVT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004531 &BuildVecIndices[0], BuildVecIndices.size());
Dan Gohman7f321562007-06-25 16:23:39 +00004532 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Ops, 3);
Chris Lattnerd7648c82006-03-28 20:28:38 +00004533 }
4534
4535 return SDOperand();
4536}
4537
Dan Gohman7f321562007-06-25 16:23:39 +00004538SDOperand DAGCombiner::visitCONCAT_VECTORS(SDNode *N) {
4539 // TODO: Check to see if this is a CONCAT_VECTORS of a bunch of
4540 // EXTRACT_SUBVECTOR operations. If so, and if the EXTRACT_SUBVECTOR vector
4541 // inputs come from at most two distinct vectors, turn this into a shuffle
4542 // node.
4543
4544 // If we only have one input vector, we don't need to do any concatenation.
4545 if (N->getNumOperands() == 1) {
4546 return N->getOperand(0);
4547 }
4548
4549 return SDOperand();
4550}
4551
Chris Lattner66445d32006-03-28 22:11:53 +00004552SDOperand DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
Chris Lattnerf1d0c622006-03-31 22:16:43 +00004553 SDOperand ShufMask = N->getOperand(2);
4554 unsigned NumElts = ShufMask.getNumOperands();
4555
4556 // If the shuffle mask is an identity operation on the LHS, return the LHS.
4557 bool isIdentity = true;
4558 for (unsigned i = 0; i != NumElts; ++i) {
4559 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
4560 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
4561 isIdentity = false;
4562 break;
4563 }
4564 }
4565 if (isIdentity) return N->getOperand(0);
4566
4567 // If the shuffle mask is an identity operation on the RHS, return the RHS.
4568 isIdentity = true;
4569 for (unsigned i = 0; i != NumElts; ++i) {
4570 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
4571 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
4572 isIdentity = false;
4573 break;
4574 }
4575 }
4576 if (isIdentity) return N->getOperand(1);
Evan Chenge7bec0d2006-07-20 22:44:41 +00004577
4578 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
4579 // needed at all.
4580 bool isUnary = true;
Evan Cheng917ec982006-07-21 08:25:53 +00004581 bool isSplat = true;
Evan Chenge7bec0d2006-07-20 22:44:41 +00004582 int VecNum = -1;
Reid Spencer9160a6a2006-07-25 20:44:41 +00004583 unsigned BaseIdx = 0;
Evan Chenge7bec0d2006-07-20 22:44:41 +00004584 for (unsigned i = 0; i != NumElts; ++i)
4585 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
4586 unsigned Idx = cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue();
4587 int V = (Idx < NumElts) ? 0 : 1;
Evan Cheng917ec982006-07-21 08:25:53 +00004588 if (VecNum == -1) {
Evan Chenge7bec0d2006-07-20 22:44:41 +00004589 VecNum = V;
Evan Cheng917ec982006-07-21 08:25:53 +00004590 BaseIdx = Idx;
4591 } else {
4592 if (BaseIdx != Idx)
4593 isSplat = false;
4594 if (VecNum != V) {
4595 isUnary = false;
4596 break;
4597 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00004598 }
4599 }
4600
4601 SDOperand N0 = N->getOperand(0);
4602 SDOperand N1 = N->getOperand(1);
4603 // Normalize unary shuffle so the RHS is undef.
4604 if (isUnary && VecNum == 1)
4605 std::swap(N0, N1);
4606
Evan Cheng917ec982006-07-21 08:25:53 +00004607 // If it is a splat, check if the argument vector is a build_vector with
4608 // all scalar elements the same.
4609 if (isSplat) {
4610 SDNode *V = N0.Val;
Evan Cheng917ec982006-07-21 08:25:53 +00004611
Dan Gohman7f321562007-06-25 16:23:39 +00004612 // If this is a bit convert that changes the element type of the vector but
Evan Cheng59569222006-10-16 22:49:37 +00004613 // not the number of vector elements, look through it. Be careful not to
4614 // look though conversions that change things like v4f32 to v2f64.
Dan Gohman7f321562007-06-25 16:23:39 +00004615 if (V->getOpcode() == ISD::BIT_CONVERT) {
Evan Cheng59569222006-10-16 22:49:37 +00004616 SDOperand ConvInput = V->getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00004617 if (MVT::getVectorNumElements(ConvInput.getValueType()) == NumElts)
Evan Cheng59569222006-10-16 22:49:37 +00004618 V = ConvInput.Val;
4619 }
4620
Dan Gohman7f321562007-06-25 16:23:39 +00004621 if (V->getOpcode() == ISD::BUILD_VECTOR) {
4622 unsigned NumElems = V->getNumOperands();
Evan Cheng917ec982006-07-21 08:25:53 +00004623 if (NumElems > BaseIdx) {
4624 SDOperand Base;
4625 bool AllSame = true;
4626 for (unsigned i = 0; i != NumElems; ++i) {
4627 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
4628 Base = V->getOperand(i);
4629 break;
4630 }
4631 }
4632 // Splat of <u, u, u, u>, return <u, u, u, u>
4633 if (!Base.Val)
4634 return N0;
4635 for (unsigned i = 0; i != NumElems; ++i) {
Evan Chenge0480d22007-09-18 21:54:37 +00004636 if (V->getOperand(i) != Base) {
Evan Cheng917ec982006-07-21 08:25:53 +00004637 AllSame = false;
4638 break;
4639 }
4640 }
4641 // Splat of <x, x, x, x>, return <x, x, x, x>
4642 if (AllSame)
4643 return N0;
4644 }
4645 }
4646 }
4647
Evan Chenge7bec0d2006-07-20 22:44:41 +00004648 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
4649 // into an undef.
4650 if (isUnary || N0 == N1) {
Chris Lattner17614ea2006-04-08 05:34:25 +00004651 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
4652 // first operand.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004653 SmallVector<SDOperand, 8> MappedOps;
Chris Lattner17614ea2006-04-08 05:34:25 +00004654 for (unsigned i = 0; i != NumElts; ++i) {
4655 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
4656 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) {
4657 MappedOps.push_back(ShufMask.getOperand(i));
4658 } else {
4659 unsigned NewIdx =
4660 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
4661 MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32));
4662 }
4663 }
Dan Gohman7f321562007-06-25 16:23:39 +00004664 ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMask.getValueType(),
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004665 &MappedOps[0], MappedOps.size());
Chris Lattner17614ea2006-04-08 05:34:25 +00004666 AddToWorkList(ShufMask.Val);
Dan Gohman7f321562007-06-25 16:23:39 +00004667 return DAG.getNode(ISD::VECTOR_SHUFFLE, N->getValueType(0),
4668 N0,
4669 DAG.getNode(ISD::UNDEF, N->getValueType(0)),
4670 ShufMask);
Chris Lattner17614ea2006-04-08 05:34:25 +00004671 }
Dan Gohman7f321562007-06-25 16:23:39 +00004672
Chris Lattnerf1d0c622006-03-31 22:16:43 +00004673 return SDOperand();
4674}
4675
Evan Cheng44f1f092006-04-20 08:56:16 +00004676/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
Dan Gohman7f321562007-06-25 16:23:39 +00004677/// an AND to a vector_shuffle with the destination vector and a zero vector.
4678/// e.g. AND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
Evan Cheng44f1f092006-04-20 08:56:16 +00004679/// vector_shuffle V, Zero, <0, 4, 2, 4>
4680SDOperand DAGCombiner::XformToShuffleWithZero(SDNode *N) {
4681 SDOperand LHS = N->getOperand(0);
4682 SDOperand RHS = N->getOperand(1);
Dan Gohman7f321562007-06-25 16:23:39 +00004683 if (N->getOpcode() == ISD::AND) {
4684 if (RHS.getOpcode() == ISD::BIT_CONVERT)
Evan Cheng44f1f092006-04-20 08:56:16 +00004685 RHS = RHS.getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00004686 if (RHS.getOpcode() == ISD::BUILD_VECTOR) {
Evan Cheng44f1f092006-04-20 08:56:16 +00004687 std::vector<SDOperand> IdxOps;
4688 unsigned NumOps = RHS.getNumOperands();
Dan Gohman7f321562007-06-25 16:23:39 +00004689 unsigned NumElts = NumOps;
4690 MVT::ValueType EVT = MVT::getVectorElementType(RHS.getValueType());
Evan Cheng44f1f092006-04-20 08:56:16 +00004691 for (unsigned i = 0; i != NumElts; ++i) {
4692 SDOperand Elt = RHS.getOperand(i);
4693 if (!isa<ConstantSDNode>(Elt))
4694 return SDOperand();
4695 else if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
4696 IdxOps.push_back(DAG.getConstant(i, EVT));
4697 else if (cast<ConstantSDNode>(Elt)->isNullValue())
4698 IdxOps.push_back(DAG.getConstant(NumElts, EVT));
4699 else
4700 return SDOperand();
4701 }
4702
4703 // Let's see if the target supports this vector_shuffle.
4704 if (!TLI.isVectorClearMaskLegal(IdxOps, EVT, DAG))
4705 return SDOperand();
4706
Dan Gohman7f321562007-06-25 16:23:39 +00004707 // Return the new VECTOR_SHUFFLE node.
4708 MVT::ValueType VT = MVT::getVectorType(EVT, NumElts);
Evan Cheng44f1f092006-04-20 08:56:16 +00004709 std::vector<SDOperand> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00004710 LHS = DAG.getNode(ISD::BIT_CONVERT, VT, LHS);
Evan Cheng44f1f092006-04-20 08:56:16 +00004711 Ops.push_back(LHS);
4712 AddToWorkList(LHS.Val);
4713 std::vector<SDOperand> ZeroOps(NumElts, DAG.getConstant(0, EVT));
Dan Gohman7f321562007-06-25 16:23:39 +00004714 Ops.push_back(DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004715 &ZeroOps[0], ZeroOps.size()));
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 &IdxOps[0], IdxOps.size()));
Dan Gohman7f321562007-06-25 16:23:39 +00004718 SDOperand Result = DAG.getNode(ISD::VECTOR_SHUFFLE, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004719 &Ops[0], Ops.size());
Dan Gohman7f321562007-06-25 16:23:39 +00004720 if (VT != LHS.getValueType()) {
4721 Result = DAG.getNode(ISD::BIT_CONVERT, LHS.getValueType(), Result);
Evan Cheng44f1f092006-04-20 08:56:16 +00004722 }
4723 return Result;
4724 }
4725 }
4726 return SDOperand();
4727}
4728
Dan Gohman7f321562007-06-25 16:23:39 +00004729/// SimplifyVBinOp - Visit a binary vector operation, like ADD.
4730SDOperand DAGCombiner::SimplifyVBinOp(SDNode *N) {
4731 // After legalize, the target may be depending on adds and other
4732 // binary ops to provide legal ways to construct constants or other
4733 // things. Simplifying them may result in a loss of legality.
4734 if (AfterLegalize) return SDOperand();
4735
4736 MVT::ValueType VT = N->getValueType(0);
Dan Gohman05d92fe2007-07-13 20:03:40 +00004737 assert(MVT::isVector(VT) && "SimplifyVBinOp only works on vectors!");
Dan Gohman7f321562007-06-25 16:23:39 +00004738
4739 MVT::ValueType EltType = MVT::getVectorElementType(VT);
Chris Lattneredab1b92006-04-02 03:25:57 +00004740 SDOperand LHS = N->getOperand(0);
4741 SDOperand RHS = N->getOperand(1);
Evan Cheng44f1f092006-04-20 08:56:16 +00004742 SDOperand Shuffle = XformToShuffleWithZero(N);
4743 if (Shuffle.Val) return Shuffle;
4744
Dan Gohman7f321562007-06-25 16:23:39 +00004745 // If the LHS and RHS are BUILD_VECTOR nodes, see if we can constant fold
Chris Lattneredab1b92006-04-02 03:25:57 +00004746 // this operation.
Dan Gohman7f321562007-06-25 16:23:39 +00004747 if (LHS.getOpcode() == ISD::BUILD_VECTOR &&
4748 RHS.getOpcode() == ISD::BUILD_VECTOR) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004749 SmallVector<SDOperand, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00004750 for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) {
Chris Lattneredab1b92006-04-02 03:25:57 +00004751 SDOperand LHSOp = LHS.getOperand(i);
4752 SDOperand RHSOp = RHS.getOperand(i);
4753 // If these two elements can't be folded, bail out.
4754 if ((LHSOp.getOpcode() != ISD::UNDEF &&
4755 LHSOp.getOpcode() != ISD::Constant &&
4756 LHSOp.getOpcode() != ISD::ConstantFP) ||
4757 (RHSOp.getOpcode() != ISD::UNDEF &&
4758 RHSOp.getOpcode() != ISD::Constant &&
4759 RHSOp.getOpcode() != ISD::ConstantFP))
4760 break;
Evan Cheng7b336a82006-05-31 06:08:35 +00004761 // Can't fold divide by zero.
Dan Gohman7f321562007-06-25 16:23:39 +00004762 if (N->getOpcode() == ISD::SDIV || N->getOpcode() == ISD::UDIV ||
4763 N->getOpcode() == ISD::FDIV) {
Evan Cheng7b336a82006-05-31 06:08:35 +00004764 if ((RHSOp.getOpcode() == ISD::Constant &&
4765 cast<ConstantSDNode>(RHSOp.Val)->isNullValue()) ||
4766 (RHSOp.getOpcode() == ISD::ConstantFP &&
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00004767 cast<ConstantFPSDNode>(RHSOp.Val)->getValueAPF().isZero()))
Evan Cheng7b336a82006-05-31 06:08:35 +00004768 break;
4769 }
Dan Gohman7f321562007-06-25 16:23:39 +00004770 Ops.push_back(DAG.getNode(N->getOpcode(), EltType, LHSOp, RHSOp));
Chris Lattner3e104b12006-04-08 04:15:24 +00004771 AddToWorkList(Ops.back().Val);
Chris Lattneredab1b92006-04-02 03:25:57 +00004772 assert((Ops.back().getOpcode() == ISD::UNDEF ||
4773 Ops.back().getOpcode() == ISD::Constant ||
4774 Ops.back().getOpcode() == ISD::ConstantFP) &&
4775 "Scalar binop didn't fold!");
4776 }
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00004777
Dan Gohman7f321562007-06-25 16:23:39 +00004778 if (Ops.size() == LHS.getNumOperands()) {
4779 MVT::ValueType VT = LHS.getValueType();
4780 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00004781 }
Chris Lattneredab1b92006-04-02 03:25:57 +00004782 }
4783
4784 return SDOperand();
4785}
4786
Nate Begeman44728a72005-09-19 22:34:01 +00004787SDOperand DAGCombiner::SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2){
Nate Begemanf845b452005-10-08 00:29:44 +00004788 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
4789
4790 SDOperand SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
4791 cast<CondCodeSDNode>(N0.getOperand(2))->get());
4792 // If we got a simplified select_cc node back from SimplifySelectCC, then
4793 // break it down into a new SETCC node, and a new SELECT node, and then return
4794 // the SELECT node, since we were called with a SELECT node.
4795 if (SCC.Val) {
4796 // Check to see if we got a select_cc back (to turn into setcc/select).
4797 // Otherwise, just return whatever node we got back, like fabs.
4798 if (SCC.getOpcode() == ISD::SELECT_CC) {
4799 SDOperand SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
4800 SCC.getOperand(0), SCC.getOperand(1),
4801 SCC.getOperand(4));
Chris Lattner5750df92006-03-01 04:03:14 +00004802 AddToWorkList(SETCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004803 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
4804 SCC.getOperand(3), SETCC);
4805 }
4806 return SCC;
4807 }
Nate Begeman44728a72005-09-19 22:34:01 +00004808 return SDOperand();
4809}
4810
Chris Lattner40c62d52005-10-18 06:04:22 +00004811/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
4812/// are the two values being selected between, see if we can simplify the
Chris Lattner729c6d12006-05-27 00:43:02 +00004813/// select. Callers of this should assume that TheSelect is deleted if this
4814/// returns true. As such, they should return the appropriate thing (e.g. the
4815/// node) back to the top-level of the DAG combiner loop to avoid it being
4816/// looked at.
Chris Lattner40c62d52005-10-18 06:04:22 +00004817///
4818bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDOperand LHS,
4819 SDOperand RHS) {
4820
4821 // If this is a select from two identical things, try to pull the operation
4822 // through the select.
4823 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
Chris Lattner40c62d52005-10-18 06:04:22 +00004824 // If this is a load and the token chain is identical, replace the select
4825 // of two loads with a load through a select of the address to load from.
4826 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
4827 // constants have been dropped into the constant pool.
Evan Cheng466685d2006-10-09 20:57:25 +00004828 if (LHS.getOpcode() == ISD::LOAD &&
Chris Lattner40c62d52005-10-18 06:04:22 +00004829 // Token chains must be identical.
Evan Cheng466685d2006-10-09 20:57:25 +00004830 LHS.getOperand(0) == RHS.getOperand(0)) {
4831 LoadSDNode *LLD = cast<LoadSDNode>(LHS);
4832 LoadSDNode *RLD = cast<LoadSDNode>(RHS);
4833
4834 // If this is an EXTLOAD, the VT's must match.
Evan Cheng2e49f092006-10-11 07:10:22 +00004835 if (LLD->getLoadedVT() == RLD->getLoadedVT()) {
Evan Cheng466685d2006-10-09 20:57:25 +00004836 // FIXME: this conflates two src values, discarding one. This is not
4837 // the right thing to do, but nothing uses srcvalues now. When they do,
4838 // turn SrcValue into a list of locations.
4839 SDOperand Addr;
Chris Lattnerc4e664b2007-01-16 05:59:59 +00004840 if (TheSelect->getOpcode() == ISD::SELECT) {
4841 // Check that the condition doesn't reach either load. If so, folding
4842 // this will induce a cycle into the DAG.
4843 if (!LLD->isPredecessor(TheSelect->getOperand(0).Val) &&
4844 !RLD->isPredecessor(TheSelect->getOperand(0).Val)) {
4845 Addr = DAG.getNode(ISD::SELECT, LLD->getBasePtr().getValueType(),
4846 TheSelect->getOperand(0), LLD->getBasePtr(),
4847 RLD->getBasePtr());
4848 }
4849 } else {
4850 // Check that the condition doesn't reach either load. If so, folding
4851 // this will induce a cycle into the DAG.
4852 if (!LLD->isPredecessor(TheSelect->getOperand(0).Val) &&
4853 !RLD->isPredecessor(TheSelect->getOperand(0).Val) &&
4854 !LLD->isPredecessor(TheSelect->getOperand(1).Val) &&
4855 !RLD->isPredecessor(TheSelect->getOperand(1).Val)) {
4856 Addr = DAG.getNode(ISD::SELECT_CC, LLD->getBasePtr().getValueType(),
Evan Cheng466685d2006-10-09 20:57:25 +00004857 TheSelect->getOperand(0),
4858 TheSelect->getOperand(1),
4859 LLD->getBasePtr(), RLD->getBasePtr(),
4860 TheSelect->getOperand(4));
Chris Lattnerc4e664b2007-01-16 05:59:59 +00004861 }
Evan Cheng466685d2006-10-09 20:57:25 +00004862 }
Chris Lattnerc4e664b2007-01-16 05:59:59 +00004863
4864 if (Addr.Val) {
4865 SDOperand Load;
4866 if (LLD->getExtensionType() == ISD::NON_EXTLOAD)
4867 Load = DAG.getLoad(TheSelect->getValueType(0), LLD->getChain(),
4868 Addr,LLD->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00004869 LLD->getSrcValueOffset(),
4870 LLD->isVolatile(),
4871 LLD->getAlignment());
Chris Lattnerc4e664b2007-01-16 05:59:59 +00004872 else {
4873 Load = DAG.getExtLoad(LLD->getExtensionType(),
4874 TheSelect->getValueType(0),
4875 LLD->getChain(), Addr, LLD->getSrcValue(),
4876 LLD->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00004877 LLD->getLoadedVT(),
4878 LLD->isVolatile(),
4879 LLD->getAlignment());
Chris Lattnerc4e664b2007-01-16 05:59:59 +00004880 }
4881 // Users of the select now use the result of the load.
4882 CombineTo(TheSelect, Load);
4883
4884 // Users of the old loads now use the new load's chain. We know the
4885 // old-load value is dead now.
4886 CombineTo(LHS.Val, Load.getValue(0), Load.getValue(1));
4887 CombineTo(RHS.Val, Load.getValue(0), Load.getValue(1));
4888 return true;
4889 }
Evan Chengc5484282006-10-04 00:56:09 +00004890 }
Chris Lattner40c62d52005-10-18 06:04:22 +00004891 }
4892 }
4893
4894 return false;
4895}
4896
Nate Begeman44728a72005-09-19 22:34:01 +00004897SDOperand DAGCombiner::SimplifySelectCC(SDOperand N0, SDOperand N1,
4898 SDOperand N2, SDOperand N3,
Chris Lattner1eba01e2007-04-11 06:50:51 +00004899 ISD::CondCode CC, bool NotExtCompare) {
Nate Begemanf845b452005-10-08 00:29:44 +00004900
4901 MVT::ValueType VT = N2.getValueType();
Nate Begemanf845b452005-10-08 00:29:44 +00004902 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
4903 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
4904 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
4905
4906 // Determine if the condition we're dealing with is constant
4907 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
Chris Lattner30f73e72006-10-14 03:52:46 +00004908 if (SCC.Val) AddToWorkList(SCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004909 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
4910
4911 // fold select_cc true, x, y -> x
4912 if (SCCC && SCCC->getValue())
4913 return N2;
4914 // fold select_cc false, x, y -> y
4915 if (SCCC && SCCC->getValue() == 0)
4916 return N3;
4917
4918 // Check to see if we can simplify the select into an fabs node
4919 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
4920 // Allow either -0.0 or 0.0
Dale Johannesen87503a62007-08-25 22:10:57 +00004921 if (CFP->getValueAPF().isZero()) {
Nate Begemanf845b452005-10-08 00:29:44 +00004922 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
4923 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
4924 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
4925 N2 == N3.getOperand(0))
4926 return DAG.getNode(ISD::FABS, VT, N0);
4927
4928 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
4929 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
4930 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
4931 N2.getOperand(0) == N3)
4932 return DAG.getNode(ISD::FABS, VT, N3);
4933 }
4934 }
4935
4936 // Check to see if we can perform the "gzip trick", transforming
4937 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
Chris Lattnere3152e52006-09-20 06:41:35 +00004938 if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT &&
Nate Begemanf845b452005-10-08 00:29:44 +00004939 MVT::isInteger(N0.getValueType()) &&
Chris Lattnere3152e52006-09-20 06:41:35 +00004940 MVT::isInteger(N2.getValueType()) &&
4941 (N1C->isNullValue() || // (a < 0) ? b : 0
4942 (N1C->getValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0
Nate Begemanf845b452005-10-08 00:29:44 +00004943 MVT::ValueType XType = N0.getValueType();
4944 MVT::ValueType AType = N2.getValueType();
4945 if (XType >= AType) {
4946 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00004947 // single-bit constant.
Nate Begemanf845b452005-10-08 00:29:44 +00004948 if (N2C && ((N2C->getValue() & (N2C->getValue()-1)) == 0)) {
4949 unsigned ShCtV = Log2_64(N2C->getValue());
4950 ShCtV = MVT::getSizeInBits(XType)-ShCtV-1;
4951 SDOperand ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
4952 SDOperand Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
Chris Lattner5750df92006-03-01 04:03:14 +00004953 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004954 if (XType > AType) {
4955 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00004956 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004957 }
4958 return DAG.getNode(ISD::AND, AType, Shift, N2);
4959 }
4960 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
4961 DAG.getConstant(MVT::getSizeInBits(XType)-1,
4962 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00004963 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004964 if (XType > AType) {
4965 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00004966 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004967 }
4968 return DAG.getNode(ISD::AND, AType, Shift, N2);
4969 }
4970 }
Nate Begeman07ed4172005-10-10 21:26:48 +00004971
4972 // fold select C, 16, 0 -> shl C, 4
4973 if (N2C && N3C && N3C->isNullValue() && isPowerOf2_64(N2C->getValue()) &&
4974 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) {
Chris Lattner1eba01e2007-04-11 06:50:51 +00004975
4976 // If the caller doesn't want us to simplify this into a zext of a compare,
4977 // don't do it.
4978 if (NotExtCompare && N2C->getValue() == 1)
4979 return SDOperand();
4980
Nate Begeman07ed4172005-10-10 21:26:48 +00004981 // Get a SetCC of the condition
4982 // FIXME: Should probably make sure that setcc is legal if we ever have a
4983 // target where it isn't.
Nate Begemanb0d04a72006-02-18 02:40:58 +00004984 SDOperand Temp, SCC;
Nate Begeman07ed4172005-10-10 21:26:48 +00004985 // cast from setcc result type to select result type
Nate Begemanb0d04a72006-02-18 02:40:58 +00004986 if (AfterLegalize) {
4987 SCC = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
Chris Lattner555d8d62006-12-07 22:36:47 +00004988 if (N2.getValueType() < SCC.getValueType())
4989 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
4990 else
4991 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00004992 } else {
4993 SCC = DAG.getSetCC(MVT::i1, N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00004994 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00004995 }
Chris Lattner5750df92006-03-01 04:03:14 +00004996 AddToWorkList(SCC.Val);
4997 AddToWorkList(Temp.Val);
Chris Lattnerc56a81d2007-04-11 06:43:25 +00004998
4999 if (N2C->getValue() == 1)
5000 return Temp;
Nate Begeman07ed4172005-10-10 21:26:48 +00005001 // shl setcc result by log2 n2c
5002 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
5003 DAG.getConstant(Log2_64(N2C->getValue()),
5004 TLI.getShiftAmountTy()));
5005 }
5006
Nate Begemanf845b452005-10-08 00:29:44 +00005007 // Check to see if this is the equivalent of setcc
5008 // FIXME: Turn all of these into setcc if setcc if setcc is legal
5009 // otherwise, go ahead with the folds.
5010 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getValue() == 1ULL)) {
5011 MVT::ValueType XType = N0.getValueType();
5012 if (TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultTy())) {
5013 SDOperand Res = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
5014 if (Res.getValueType() != VT)
5015 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
5016 return Res;
5017 }
5018
5019 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
5020 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
5021 TLI.isOperationLegal(ISD::CTLZ, XType)) {
5022 SDOperand Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
5023 return DAG.getNode(ISD::SRL, XType, Ctlz,
5024 DAG.getConstant(Log2_32(MVT::getSizeInBits(XType)),
5025 TLI.getShiftAmountTy()));
5026 }
5027 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
5028 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
5029 SDOperand NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
5030 N0);
5031 SDOperand NotN0 = DAG.getNode(ISD::XOR, XType, N0,
5032 DAG.getConstant(~0ULL, XType));
5033 return DAG.getNode(ISD::SRL, XType,
5034 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
5035 DAG.getConstant(MVT::getSizeInBits(XType)-1,
5036 TLI.getShiftAmountTy()));
5037 }
5038 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
5039 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
5040 SDOperand Sign = DAG.getNode(ISD::SRL, XType, N0,
5041 DAG.getConstant(MVT::getSizeInBits(XType)-1,
5042 TLI.getShiftAmountTy()));
5043 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
5044 }
5045 }
5046
5047 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
5048 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
5049 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
Chris Lattner1982ef22007-04-11 05:11:38 +00005050 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1) &&
5051 N2.getOperand(0) == N1 && MVT::isInteger(N0.getValueType())) {
5052 MVT::ValueType XType = N0.getValueType();
5053 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
5054 DAG.getConstant(MVT::getSizeInBits(XType)-1,
5055 TLI.getShiftAmountTy()));
5056 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
5057 AddToWorkList(Shift.Val);
5058 AddToWorkList(Add.Val);
5059 return DAG.getNode(ISD::XOR, XType, Add, Shift);
5060 }
5061 // Check to see if this is an integer abs. select_cc setgt X, -1, X, -X ->
5062 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
5063 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT &&
5064 N0 == N2 && N3.getOpcode() == ISD::SUB && N0 == N3.getOperand(1)) {
5065 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N3.getOperand(0))) {
Nate Begemanf845b452005-10-08 00:29:44 +00005066 MVT::ValueType XType = N0.getValueType();
5067 if (SubC->isNullValue() && MVT::isInteger(XType)) {
5068 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
5069 DAG.getConstant(MVT::getSizeInBits(XType)-1,
Chris Lattner1982ef22007-04-11 05:11:38 +00005070 TLI.getShiftAmountTy()));
Nate Begemanf845b452005-10-08 00:29:44 +00005071 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00005072 AddToWorkList(Shift.Val);
5073 AddToWorkList(Add.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005074 return DAG.getNode(ISD::XOR, XType, Add, Shift);
5075 }
5076 }
5077 }
Chris Lattner1982ef22007-04-11 05:11:38 +00005078
Nate Begeman44728a72005-09-19 22:34:01 +00005079 return SDOperand();
5080}
5081
Evan Chengfa1eb272007-02-08 22:13:59 +00005082/// SimplifySetCC - This is a stub for TargetLowering::SimplifySetCC.
Nate Begeman452d7be2005-09-16 00:54:12 +00005083SDOperand DAGCombiner::SimplifySetCC(MVT::ValueType VT, SDOperand N0,
Nate Begemane17daeb2005-10-05 21:43:42 +00005084 SDOperand N1, ISD::CondCode Cond,
5085 bool foldBooleans) {
Evan Chengfa1eb272007-02-08 22:13:59 +00005086 TargetLowering::DAGCombinerInfo
5087 DagCombineInfo(DAG, !AfterLegalize, false, this);
5088 return TLI.SimplifySetCC(VT, N0, N1, Cond, foldBooleans, DagCombineInfo);
Nate Begeman452d7be2005-09-16 00:54:12 +00005089}
5090
Nate Begeman69575232005-10-20 02:15:44 +00005091/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
5092/// return a DAG expression to select that will generate the same value by
5093/// multiplying by a magic number. See:
5094/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
5095SDOperand DAGCombiner::BuildSDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00005096 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005097 SDOperand S = TLI.BuildSDIV(N, DAG, &Built);
5098
Andrew Lenharth232c9102006-06-12 16:07:18 +00005099 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005100 ii != ee; ++ii)
5101 AddToWorkList(*ii);
5102 return S;
Nate Begeman69575232005-10-20 02:15:44 +00005103}
5104
5105/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
5106/// return a DAG expression to select that will generate the same value by
5107/// multiplying by a magic number. See:
5108/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
5109SDOperand DAGCombiner::BuildUDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00005110 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005111 SDOperand S = TLI.BuildUDIV(N, DAG, &Built);
Nate Begeman69575232005-10-20 02:15:44 +00005112
Andrew Lenharth232c9102006-06-12 16:07:18 +00005113 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005114 ii != ee; ++ii)
5115 AddToWorkList(*ii);
5116 return S;
Nate Begeman69575232005-10-20 02:15:44 +00005117}
5118
Jim Laskey71382342006-10-07 23:37:56 +00005119/// FindBaseOffset - Return true if base is known not to alias with anything
5120/// but itself. Provides base object and offset as results.
5121static bool FindBaseOffset(SDOperand Ptr, SDOperand &Base, int64_t &Offset) {
5122 // Assume it is a primitive operation.
5123 Base = Ptr; Offset = 0;
5124
5125 // If it's an adding a simple constant then integrate the offset.
5126 if (Base.getOpcode() == ISD::ADD) {
5127 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Base.getOperand(1))) {
5128 Base = Base.getOperand(0);
5129 Offset += C->getValue();
5130 }
5131 }
5132
5133 // If it's any of the following then it can't alias with anything but itself.
5134 return isa<FrameIndexSDNode>(Base) ||
5135 isa<ConstantPoolSDNode>(Base) ||
5136 isa<GlobalAddressSDNode>(Base);
5137}
5138
5139/// isAlias - Return true if there is any possibility that the two addresses
5140/// overlap.
Jim Laskey096c22e2006-10-18 12:29:57 +00005141bool DAGCombiner::isAlias(SDOperand Ptr1, int64_t Size1,
5142 const Value *SrcValue1, int SrcValueOffset1,
5143 SDOperand Ptr2, int64_t Size2,
5144 const Value *SrcValue2, int SrcValueOffset2)
5145{
Jim Laskey71382342006-10-07 23:37:56 +00005146 // If they are the same then they must be aliases.
5147 if (Ptr1 == Ptr2) return true;
5148
5149 // Gather base node and offset information.
5150 SDOperand Base1, Base2;
5151 int64_t Offset1, Offset2;
5152 bool KnownBase1 = FindBaseOffset(Ptr1, Base1, Offset1);
5153 bool KnownBase2 = FindBaseOffset(Ptr2, Base2, Offset2);
5154
5155 // If they have a same base address then...
5156 if (Base1 == Base2) {
5157 // Check to see if the addresses overlap.
5158 return!((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
5159 }
5160
Jim Laskey096c22e2006-10-18 12:29:57 +00005161 // If we know both bases then they can't alias.
5162 if (KnownBase1 && KnownBase2) return false;
5163
Jim Laskey07a27092006-10-18 19:08:31 +00005164 if (CombinerGlobalAA) {
5165 // Use alias analysis information.
Dan Gohmane9c8fa02007-08-27 16:32:11 +00005166 int64_t MinOffset = std::min(SrcValueOffset1, SrcValueOffset2);
5167 int64_t Overlap1 = Size1 + SrcValueOffset1 - MinOffset;
5168 int64_t Overlap2 = Size2 + SrcValueOffset2 - MinOffset;
Jim Laskey07a27092006-10-18 19:08:31 +00005169 AliasAnalysis::AliasResult AAResult =
Jim Laskey096c22e2006-10-18 12:29:57 +00005170 AA.alias(SrcValue1, Overlap1, SrcValue2, Overlap2);
Jim Laskey07a27092006-10-18 19:08:31 +00005171 if (AAResult == AliasAnalysis::NoAlias)
5172 return false;
5173 }
Jim Laskey096c22e2006-10-18 12:29:57 +00005174
5175 // Otherwise we have to assume they alias.
5176 return true;
Jim Laskey71382342006-10-07 23:37:56 +00005177}
5178
5179/// FindAliasInfo - Extracts the relevant alias information from the memory
5180/// node. Returns true if the operand was a load.
Jim Laskey7ca56af2006-10-11 13:47:09 +00005181bool DAGCombiner::FindAliasInfo(SDNode *N,
Jim Laskey096c22e2006-10-18 12:29:57 +00005182 SDOperand &Ptr, int64_t &Size,
5183 const Value *&SrcValue, int &SrcValueOffset) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00005184 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
5185 Ptr = LD->getBasePtr();
Jim Laskeyc2b19f32006-10-11 17:47:52 +00005186 Size = MVT::getSizeInBits(LD->getLoadedVT()) >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005187 SrcValue = LD->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00005188 SrcValueOffset = LD->getSrcValueOffset();
Jim Laskey71382342006-10-07 23:37:56 +00005189 return true;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005190 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00005191 Ptr = ST->getBasePtr();
Evan Cheng8b2794a2006-10-13 21:14:26 +00005192 Size = MVT::getSizeInBits(ST->getStoredVT()) >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005193 SrcValue = ST->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00005194 SrcValueOffset = ST->getSrcValueOffset();
Jim Laskey7ca56af2006-10-11 13:47:09 +00005195 } else {
Jim Laskey71382342006-10-07 23:37:56 +00005196 assert(0 && "FindAliasInfo expected a memory operand");
Jim Laskey71382342006-10-07 23:37:56 +00005197 }
5198
5199 return false;
5200}
5201
Jim Laskey6ff23e52006-10-04 16:53:27 +00005202/// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
5203/// looking for aliasing nodes and adding them to the Aliases vector.
Jim Laskeybc588b82006-10-05 15:07:25 +00005204void DAGCombiner::GatherAllAliases(SDNode *N, SDOperand OriginalChain,
Jim Laskey6ff23e52006-10-04 16:53:27 +00005205 SmallVector<SDOperand, 8> &Aliases) {
Jim Laskeybc588b82006-10-05 15:07:25 +00005206 SmallVector<SDOperand, 8> Chains; // List of chains to visit.
Jim Laskey6ff23e52006-10-04 16:53:27 +00005207 std::set<SDNode *> Visited; // Visited node set.
5208
Jim Laskey279f0532006-09-25 16:29:54 +00005209 // Get alias information for node.
5210 SDOperand Ptr;
5211 int64_t Size;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005212 const Value *SrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00005213 int SrcValueOffset;
5214 bool IsLoad = FindAliasInfo(N, Ptr, Size, SrcValue, SrcValueOffset);
Jim Laskey279f0532006-09-25 16:29:54 +00005215
Jim Laskey6ff23e52006-10-04 16:53:27 +00005216 // Starting off.
Jim Laskeybc588b82006-10-05 15:07:25 +00005217 Chains.push_back(OriginalChain);
Jim Laskey6ff23e52006-10-04 16:53:27 +00005218
Jim Laskeybc588b82006-10-05 15:07:25 +00005219 // Look at each chain and determine if it is an alias. If so, add it to the
5220 // aliases list. If not, then continue up the chain looking for the next
5221 // candidate.
5222 while (!Chains.empty()) {
5223 SDOperand Chain = Chains.back();
5224 Chains.pop_back();
Jim Laskey6ff23e52006-10-04 16:53:27 +00005225
Jim Laskeybc588b82006-10-05 15:07:25 +00005226 // Don't bother if we've been before.
5227 if (Visited.find(Chain.Val) != Visited.end()) continue;
5228 Visited.insert(Chain.Val);
5229
5230 switch (Chain.getOpcode()) {
5231 case ISD::EntryToken:
5232 // Entry token is ideal chain operand, but handled in FindBetterChain.
5233 break;
Jim Laskey6ff23e52006-10-04 16:53:27 +00005234
Jim Laskeybc588b82006-10-05 15:07:25 +00005235 case ISD::LOAD:
5236 case ISD::STORE: {
5237 // Get alias information for Chain.
5238 SDOperand OpPtr;
5239 int64_t OpSize;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005240 const Value *OpSrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00005241 int OpSrcValueOffset;
5242 bool IsOpLoad = FindAliasInfo(Chain.Val, OpPtr, OpSize,
5243 OpSrcValue, OpSrcValueOffset);
Jim Laskeybc588b82006-10-05 15:07:25 +00005244
5245 // If chain is alias then stop here.
5246 if (!(IsLoad && IsOpLoad) &&
Jim Laskey096c22e2006-10-18 12:29:57 +00005247 isAlias(Ptr, Size, SrcValue, SrcValueOffset,
5248 OpPtr, OpSize, OpSrcValue, OpSrcValueOffset)) {
Jim Laskeybc588b82006-10-05 15:07:25 +00005249 Aliases.push_back(Chain);
5250 } else {
5251 // Look further up the chain.
5252 Chains.push_back(Chain.getOperand(0));
5253 // Clean up old chain.
5254 AddToWorkList(Chain.Val);
Jim Laskey279f0532006-09-25 16:29:54 +00005255 }
Jim Laskeybc588b82006-10-05 15:07:25 +00005256 break;
5257 }
5258
5259 case ISD::TokenFactor:
5260 // We have to check each of the operands of the token factor, so we queue
5261 // then up. Adding the operands to the queue (stack) in reverse order
5262 // maintains the original order and increases the likelihood that getNode
5263 // will find a matching token factor (CSE.)
5264 for (unsigned n = Chain.getNumOperands(); n;)
5265 Chains.push_back(Chain.getOperand(--n));
5266 // Eliminate the token factor if we can.
5267 AddToWorkList(Chain.Val);
5268 break;
5269
5270 default:
5271 // For all other instructions we will just have to take what we can get.
5272 Aliases.push_back(Chain);
5273 break;
Jim Laskey279f0532006-09-25 16:29:54 +00005274 }
5275 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00005276}
5277
5278/// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, looking
5279/// for a better chain (aliasing node.)
5280SDOperand DAGCombiner::FindBetterChain(SDNode *N, SDOperand OldChain) {
5281 SmallVector<SDOperand, 8> Aliases; // Ops for replacing token factor.
Jim Laskey279f0532006-09-25 16:29:54 +00005282
Jim Laskey6ff23e52006-10-04 16:53:27 +00005283 // Accumulate all the aliases to this node.
5284 GatherAllAliases(N, OldChain, Aliases);
5285
5286 if (Aliases.size() == 0) {
5287 // If no operands then chain to entry token.
5288 return DAG.getEntryNode();
5289 } else if (Aliases.size() == 1) {
5290 // If a single operand then chain to it. We don't need to revisit it.
5291 return Aliases[0];
5292 }
5293
5294 // Construct a custom tailored token factor.
5295 SDOperand NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
5296 &Aliases[0], Aliases.size());
5297
5298 // Make sure the old chain gets cleaned up.
5299 if (NewChain != OldChain) AddToWorkList(OldChain.Val);
5300
5301 return NewChain;
Jim Laskey279f0532006-09-25 16:29:54 +00005302}
5303
Nate Begeman1d4d4142005-09-01 00:19:25 +00005304// SelectionDAG::Combine - This is the entry point for the file.
5305//
Jim Laskeyc7c3f112006-10-16 20:52:31 +00005306void SelectionDAG::Combine(bool RunningAfterLegalize, AliasAnalysis &AA) {
Chris Lattner938ab022007-01-16 04:55:25 +00005307 if (!RunningAfterLegalize && ViewDAGCombine1)
5308 viewGraph();
5309 if (RunningAfterLegalize && ViewDAGCombine2)
5310 viewGraph();
Nate Begeman1d4d4142005-09-01 00:19:25 +00005311 /// run - This is the main entry point to this class.
5312 ///
Jim Laskeyc7c3f112006-10-16 20:52:31 +00005313 DAGCombiner(*this, AA).Run(RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +00005314}