blob: 7074c5980e658839644b80daaf4b3e494a817787 [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"
Chris Lattner1329cb82008-01-26 19:45:50 +000021#include "llvm/Target/TargetFrameInfo.h"
Nate Begeman1d4d4142005-09-01 00:19:25 +000022#include "llvm/Target/TargetLowering.h"
Evan Cheng59d5b682007-05-07 21:27:48 +000023#include "llvm/Target/TargetMachine.h"
Chris Lattnerddae4bd2007-01-08 23:04:05 +000024#include "llvm/Target/TargetOptions.h"
Chris Lattnerc76d4412007-05-16 06:37:59 +000025#include "llvm/ADT/SmallPtrSet.h"
26#include "llvm/ADT/Statistic.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000027#include "llvm/Support/Compiler.h"
Jim Laskeyd1aed7a2006-09-21 16:28:59 +000028#include "llvm/Support/CommandLine.h"
Chris Lattnerc76d4412007-05-16 06:37:59 +000029#include "llvm/Support/Debug.h"
30#include "llvm/Support/MathExtras.h"
Chris Lattnera500fc62005-09-09 23:53:39 +000031#include <algorithm>
Dan Gohmanee335e32008-05-23 20:40:06 +000032#include <set>
Nate Begeman1d4d4142005-09-01 00:19:25 +000033using namespace llvm;
34
Chris Lattnercd3245a2006-12-19 22:41:21 +000035STATISTIC(NodesCombined , "Number of dag nodes combined");
36STATISTIC(PreIndexedNodes , "Number of pre-indexed nodes created");
37STATISTIC(PostIndexedNodes, "Number of post-indexed nodes created");
38
Nate Begeman1d4d4142005-09-01 00:19:25 +000039namespace {
Jim Laskey71382342006-10-07 23:37:56 +000040 static cl::opt<bool>
41 CombinerAA("combiner-alias-analysis", cl::Hidden,
Jim Laskey26f7fa72006-10-17 19:33:52 +000042 cl::desc("Turn on alias analysis during testing"));
Jim Laskey3ad175b2006-10-12 15:22:24 +000043
Jim Laskey07a27092006-10-18 19:08:31 +000044 static cl::opt<bool>
45 CombinerGlobalAA("combiner-global-alias-analysis", cl::Hidden,
46 cl::desc("Include global information in alias analysis"));
47
Jim Laskeybc588b82006-10-05 15:07:25 +000048//------------------------------ DAGCombiner ---------------------------------//
49
Jim Laskey71382342006-10-07 23:37:56 +000050 class VISIBILITY_HIDDEN DAGCombiner {
Nate Begeman1d4d4142005-09-01 00:19:25 +000051 SelectionDAG &DAG;
52 TargetLowering &TLI;
Nate Begeman4ebd8052005-09-01 23:24:04 +000053 bool AfterLegalize;
Dan Gohmana2676512008-08-20 16:30:28 +000054 bool Fast;
Nate Begeman1d4d4142005-09-01 00:19:25 +000055
56 // Worklist of all of the nodes that need to be simplified.
Evan Cheng17a568b2008-08-29 22:21:44 +000057 std::vector<SDNode*> WorkList;
Nate Begeman1d4d4142005-09-01 00:19:25 +000058
Jim Laskeyc7c3f112006-10-16 20:52:31 +000059 // AA - Used for DAG load/store alias analysis.
60 AliasAnalysis &AA;
61
Nate Begeman1d4d4142005-09-01 00:19:25 +000062 /// AddUsersToWorkList - When an instruction is simplified, add all users of
63 /// the instruction to the work lists because they might get more simplified
64 /// now.
65 ///
66 void AddUsersToWorkList(SDNode *N) {
67 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
Nate Begeman4ebd8052005-09-01 23:24:04 +000068 UI != UE; ++UI)
Dan Gohman89684502008-07-27 20:43:25 +000069 AddToWorkList(*UI);
Nate Begeman1d4d4142005-09-01 00:19:25 +000070 }
71
Dan Gohman389079b2007-10-08 17:57:15 +000072 /// visit - call the node-specific routine that knows how to fold each
73 /// particular type of node.
Dan Gohman475871a2008-07-27 21:46:04 +000074 SDValue visit(SDNode *N);
Dan Gohman389079b2007-10-08 17:57:15 +000075
Chris Lattner24664722006-03-01 04:53:38 +000076 public:
Jim Laskey6ff23e52006-10-04 16:53:27 +000077 /// AddToWorkList - Add to the work list making sure it's instance is at the
78 /// the back (next to be processed.)
Chris Lattner5750df92006-03-01 04:03:14 +000079 void AddToWorkList(SDNode *N) {
Jim Laskey6ff23e52006-10-04 16:53:27 +000080 removeFromWorkList(N);
Chris Lattner5750df92006-03-01 04:03:14 +000081 WorkList.push_back(N);
82 }
Jim Laskey6ff23e52006-10-04 16:53:27 +000083
Chris Lattnerf8dc0612008-02-03 06:49:24 +000084 /// removeFromWorkList - remove all instances of N from the worklist.
85 ///
86 void removeFromWorkList(SDNode *N) {
87 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), N),
88 WorkList.end());
Chris Lattner01a22022005-10-10 22:04:48 +000089 }
Nate Begeman368e18d2006-02-16 21:11:51 +000090
Dan Gohman475871a2008-07-27 21:46:04 +000091 SDValue CombineTo(SDNode *N, const SDValue *To, unsigned NumTo,
Chris Lattnerf8dc0612008-02-03 06:49:24 +000092 bool AddTo = true);
93
Dan Gohman475871a2008-07-27 21:46:04 +000094 SDValue CombineTo(SDNode *N, SDValue Res, bool AddTo = true) {
Jim Laskey274062c2006-10-13 23:32:28 +000095 return CombineTo(N, &Res, 1, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +000096 }
97
Dan Gohman475871a2008-07-27 21:46:04 +000098 SDValue CombineTo(SDNode *N, SDValue Res0, SDValue Res1,
Jim Laskey274062c2006-10-13 23:32:28 +000099 bool AddTo = true) {
Dan Gohman475871a2008-07-27 21:46:04 +0000100 SDValue To[] = { Res0, Res1 };
Jim Laskey274062c2006-10-13 23:32:28 +0000101 return CombineTo(N, To, 2, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000102 }
Chris Lattner0bd48932008-01-17 07:00:52 +0000103
Chris Lattner24664722006-03-01 04:53:38 +0000104 private:
105
Chris Lattner012f2412006-02-17 21:58:01 +0000106 /// SimplifyDemandedBits - Check the specified integer node value to see if
Chris Lattnerb2742f42006-03-01 19:55:35 +0000107 /// it can be simplified or if things it uses can be simplified by bit
Chris Lattner012f2412006-02-17 21:58:01 +0000108 /// propagation. If so, return true.
Dan Gohman475871a2008-07-27 21:46:04 +0000109 bool SimplifyDemandedBits(SDValue Op) {
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000110 APInt Demanded = APInt::getAllOnesValue(Op.getValueSizeInBits());
111 return SimplifyDemandedBits(Op, Demanded);
112 }
113
Dan Gohman475871a2008-07-27 21:46:04 +0000114 bool SimplifyDemandedBits(SDValue Op, const APInt &Demanded);
Chris Lattner87514ca2005-10-10 22:31:19 +0000115
Chris Lattner448f2192006-11-11 00:39:41 +0000116 bool CombineToPreIndexedLoadStore(SDNode *N);
117 bool CombineToPostIndexedLoadStore(SDNode *N);
118
119
Dan Gohman389079b2007-10-08 17:57:15 +0000120 /// combine - call the node-specific routine that knows how to fold each
121 /// particular type of node. If that doesn't do anything, try the
122 /// target-specific DAG combines.
Dan Gohman475871a2008-07-27 21:46:04 +0000123 SDValue combine(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000124
125 // Visitation implementation - Implement dag node combining for different
126 // node types. The semantics are as follows:
127 // Return Value:
Evan Cheng17a568b2008-08-29 22:21:44 +0000128 // SDValue.getNode() == 0 - No change was made
129 // SDValue.getNode() == N - N was replaced, is dead and has been handled.
130 // otherwise - N should be replaced by the returned Operand.
Nate Begeman1d4d4142005-09-01 00:19:25 +0000131 //
Dan Gohman475871a2008-07-27 21:46:04 +0000132 SDValue visitTokenFactor(SDNode *N);
133 SDValue visitMERGE_VALUES(SDNode *N);
134 SDValue visitADD(SDNode *N);
135 SDValue visitSUB(SDNode *N);
136 SDValue visitADDC(SDNode *N);
137 SDValue visitADDE(SDNode *N);
138 SDValue visitMUL(SDNode *N);
139 SDValue visitSDIV(SDNode *N);
140 SDValue visitUDIV(SDNode *N);
141 SDValue visitSREM(SDNode *N);
142 SDValue visitUREM(SDNode *N);
143 SDValue visitMULHU(SDNode *N);
144 SDValue visitMULHS(SDNode *N);
145 SDValue visitSMUL_LOHI(SDNode *N);
146 SDValue visitUMUL_LOHI(SDNode *N);
147 SDValue visitSDIVREM(SDNode *N);
148 SDValue visitUDIVREM(SDNode *N);
149 SDValue visitAND(SDNode *N);
150 SDValue visitOR(SDNode *N);
151 SDValue visitXOR(SDNode *N);
152 SDValue SimplifyVBinOp(SDNode *N);
153 SDValue visitSHL(SDNode *N);
154 SDValue visitSRA(SDNode *N);
155 SDValue visitSRL(SDNode *N);
156 SDValue visitCTLZ(SDNode *N);
157 SDValue visitCTTZ(SDNode *N);
158 SDValue visitCTPOP(SDNode *N);
159 SDValue visitSELECT(SDNode *N);
160 SDValue visitSELECT_CC(SDNode *N);
161 SDValue visitSETCC(SDNode *N);
162 SDValue visitSIGN_EXTEND(SDNode *N);
163 SDValue visitZERO_EXTEND(SDNode *N);
164 SDValue visitANY_EXTEND(SDNode *N);
165 SDValue visitSIGN_EXTEND_INREG(SDNode *N);
166 SDValue visitTRUNCATE(SDNode *N);
167 SDValue visitBIT_CONVERT(SDNode *N);
168 SDValue visitBUILD_PAIR(SDNode *N);
169 SDValue visitFADD(SDNode *N);
170 SDValue visitFSUB(SDNode *N);
171 SDValue visitFMUL(SDNode *N);
172 SDValue visitFDIV(SDNode *N);
173 SDValue visitFREM(SDNode *N);
174 SDValue visitFCOPYSIGN(SDNode *N);
175 SDValue visitSINT_TO_FP(SDNode *N);
176 SDValue visitUINT_TO_FP(SDNode *N);
177 SDValue visitFP_TO_SINT(SDNode *N);
178 SDValue visitFP_TO_UINT(SDNode *N);
179 SDValue visitFP_ROUND(SDNode *N);
180 SDValue visitFP_ROUND_INREG(SDNode *N);
181 SDValue visitFP_EXTEND(SDNode *N);
182 SDValue visitFNEG(SDNode *N);
183 SDValue visitFABS(SDNode *N);
184 SDValue visitBRCOND(SDNode *N);
185 SDValue visitBR_CC(SDNode *N);
186 SDValue visitLOAD(SDNode *N);
187 SDValue visitSTORE(SDNode *N);
188 SDValue visitINSERT_VECTOR_ELT(SDNode *N);
189 SDValue visitEXTRACT_VECTOR_ELT(SDNode *N);
190 SDValue visitBUILD_VECTOR(SDNode *N);
191 SDValue visitCONCAT_VECTORS(SDNode *N);
192 SDValue visitVECTOR_SHUFFLE(SDNode *N);
Bill Wendling1c55a9c2008-11-21 02:12:42 +0000193 SDValue visitSADDO(SDNode *N);
194 SDValue visitUADDO(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000195
Dan Gohman475871a2008-07-27 21:46:04 +0000196 SDValue XformToShuffleWithZero(SDNode *N);
197 SDValue ReassociateOps(unsigned Opc, SDValue LHS, SDValue RHS);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000198
Dan Gohman475871a2008-07-27 21:46:04 +0000199 SDValue visitShiftByConstant(SDNode *N, unsigned Amt);
Chris Lattnere70da202007-12-06 07:33:36 +0000200
Dan Gohman475871a2008-07-27 21:46:04 +0000201 bool SimplifySelectOps(SDNode *SELECT, SDValue LHS, SDValue RHS);
202 SDValue SimplifyBinOpWithSameOpcodeHands(SDNode *N);
203 SDValue SimplifySelect(SDValue N0, SDValue N1, SDValue N2);
204 SDValue SimplifySelectCC(SDValue N0, SDValue N1, SDValue N2,
205 SDValue N3, ISD::CondCode CC,
Chris Lattner1eba01e2007-04-11 06:50:51 +0000206 bool NotExtCompare = false);
Dan Gohman475871a2008-07-27 21:46:04 +0000207 SDValue SimplifySetCC(MVT VT, SDValue N0, SDValue N1,
Nate Begemane17daeb2005-10-05 21:43:42 +0000208 ISD::CondCode Cond, bool foldBooleans = true);
Dan Gohman475871a2008-07-27 21:46:04 +0000209 SDValue SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
Chris Lattner5eee4272008-01-26 01:09:19 +0000210 unsigned HiOp);
Dan Gohman475871a2008-07-27 21:46:04 +0000211 SDValue CombineConsecutiveLoads(SDNode *N, MVT VT);
212 SDValue ConstantFoldBIT_CONVERTofBUILD_VECTOR(SDNode *, MVT);
213 SDValue BuildSDIV(SDNode *N);
214 SDValue BuildUDIV(SDNode *N);
215 SDNode *MatchRotate(SDValue LHS, SDValue RHS);
216 SDValue ReduceLoadWidth(SDNode *N);
Jim Laskey279f0532006-09-25 16:29:54 +0000217
Dan Gohman475871a2008-07-27 21:46:04 +0000218 SDValue GetDemandedBits(SDValue V, const APInt &Mask);
Chris Lattner2b4c2792007-10-13 06:35:54 +0000219
Jim Laskey6ff23e52006-10-04 16:53:27 +0000220 /// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
221 /// looking for aliasing nodes and adding them to the Aliases vector.
Dan Gohman475871a2008-07-27 21:46:04 +0000222 void GatherAllAliases(SDNode *N, SDValue OriginalChain,
223 SmallVector<SDValue, 8> &Aliases);
Jim Laskey6ff23e52006-10-04 16:53:27 +0000224
Jim Laskey096c22e2006-10-18 12:29:57 +0000225 /// isAlias - Return true if there is any possibility that the two addresses
226 /// overlap.
Dan Gohman475871a2008-07-27 21:46:04 +0000227 bool isAlias(SDValue Ptr1, int64_t Size1,
Jim Laskey096c22e2006-10-18 12:29:57 +0000228 const Value *SrcValue1, int SrcValueOffset1,
Dan Gohman475871a2008-07-27 21:46:04 +0000229 SDValue Ptr2, int64_t Size2,
Jeff Cohend41b30d2006-11-05 19:31:28 +0000230 const Value *SrcValue2, int SrcValueOffset2);
Jim Laskey096c22e2006-10-18 12:29:57 +0000231
Jim Laskey7ca56af2006-10-11 13:47:09 +0000232 /// FindAliasInfo - Extracts the relevant alias information from the memory
233 /// node. Returns true if the operand was a load.
234 bool FindAliasInfo(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +0000235 SDValue &Ptr, int64_t &Size,
Jim Laskey096c22e2006-10-18 12:29:57 +0000236 const Value *&SrcValue, int &SrcValueOffset);
Jim Laskey7ca56af2006-10-11 13:47:09 +0000237
Jim Laskey279f0532006-09-25 16:29:54 +0000238 /// FindBetterChain - Walk up chain skipping non-aliasing memory nodes,
Jim Laskey6ff23e52006-10-04 16:53:27 +0000239 /// looking for a better chain (aliasing node.)
Dan Gohman475871a2008-07-27 21:46:04 +0000240 SDValue FindBetterChain(SDNode *N, SDValue Chain);
Jim Laskey279f0532006-09-25 16:29:54 +0000241
Nate Begeman1d4d4142005-09-01 00:19:25 +0000242public:
Dan Gohmana2676512008-08-20 16:30:28 +0000243 DAGCombiner(SelectionDAG &D, AliasAnalysis &A, bool fast)
Jim Laskeyc7c3f112006-10-16 20:52:31 +0000244 : DAG(D),
245 TLI(D.getTargetLoweringInfo()),
246 AfterLegalize(false),
Dan Gohmana2676512008-08-20 16:30:28 +0000247 Fast(fast),
Jim Laskeyc7c3f112006-10-16 20:52:31 +0000248 AA(A) {}
Nate Begeman1d4d4142005-09-01 00:19:25 +0000249
250 /// Run - runs the dag combiner on all nodes in the work list
Nate Begeman4ebd8052005-09-01 23:24:04 +0000251 void Run(bool RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000252 };
253}
254
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000255
256namespace {
257/// WorkListRemover - This class is a DAGUpdateListener that removes any deleted
258/// nodes from the worklist.
259class VISIBILITY_HIDDEN WorkListRemover :
260 public SelectionDAG::DAGUpdateListener {
261 DAGCombiner &DC;
262public:
Dan Gohmanb5660dc2008-02-20 16:44:09 +0000263 explicit WorkListRemover(DAGCombiner &dc) : DC(dc) {}
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000264
Duncan Sandsedfcf592008-06-11 11:42:12 +0000265 virtual void NodeDeleted(SDNode *N, SDNode *E) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000266 DC.removeFromWorkList(N);
267 }
268
269 virtual void NodeUpdated(SDNode *N) {
270 // Ignore updates.
271 }
272};
273}
274
Chris Lattner24664722006-03-01 04:53:38 +0000275//===----------------------------------------------------------------------===//
276// TargetLowering::DAGCombinerInfo implementation
277//===----------------------------------------------------------------------===//
278
279void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) {
280 ((DAGCombiner*)DC)->AddToWorkList(N);
281}
282
Dan Gohman475871a2008-07-27 21:46:04 +0000283SDValue TargetLowering::DAGCombinerInfo::
284CombineTo(SDNode *N, const std::vector<SDValue> &To) {
Chris Lattner3577e382006-08-11 17:56:38 +0000285 return ((DAGCombiner*)DC)->CombineTo(N, &To[0], To.size());
Chris Lattner24664722006-03-01 04:53:38 +0000286}
287
Dan Gohman475871a2008-07-27 21:46:04 +0000288SDValue TargetLowering::DAGCombinerInfo::
289CombineTo(SDNode *N, SDValue Res) {
Chris Lattner24664722006-03-01 04:53:38 +0000290 return ((DAGCombiner*)DC)->CombineTo(N, Res);
291}
292
293
Dan Gohman475871a2008-07-27 21:46:04 +0000294SDValue TargetLowering::DAGCombinerInfo::
295CombineTo(SDNode *N, SDValue Res0, SDValue Res1) {
Chris Lattner24664722006-03-01 04:53:38 +0000296 return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1);
297}
298
299
Chris Lattner24664722006-03-01 04:53:38 +0000300//===----------------------------------------------------------------------===//
Chris Lattner29446522007-05-14 22:04:50 +0000301// Helper Functions
302//===----------------------------------------------------------------------===//
303
304/// isNegatibleForFree - Return 1 if we can compute the negated form of the
305/// specified expression for the same cost as the expression itself, or 2 if we
306/// can compute the negated form more cheaply than the expression itself.
Dan Gohman475871a2008-07-27 21:46:04 +0000307static char isNegatibleForFree(SDValue Op, bool AfterLegalize,
Chris Lattner0254e702008-02-26 07:04:54 +0000308 unsigned Depth = 0) {
Dale Johannesendb44bf82007-10-16 23:38:29 +0000309 // No compile time optimizations on this type.
310 if (Op.getValueType() == MVT::ppcf128)
311 return 0;
312
Chris Lattner29446522007-05-14 22:04:50 +0000313 // fneg is removable even if it has multiple uses.
314 if (Op.getOpcode() == ISD::FNEG) return 2;
315
316 // Don't allow anything with multiple uses.
317 if (!Op.hasOneUse()) return 0;
318
Chris Lattner3adf9512007-05-25 02:19:06 +0000319 // Don't recurse exponentially.
320 if (Depth > 6) return 0;
321
Chris Lattner29446522007-05-14 22:04:50 +0000322 switch (Op.getOpcode()) {
323 default: return false;
324 case ISD::ConstantFP:
Chris Lattner0254e702008-02-26 07:04:54 +0000325 // Don't invert constant FP values after legalize. The negated constant
326 // isn't necessarily legal.
327 return AfterLegalize ? 0 : 1;
Chris Lattner29446522007-05-14 22:04:50 +0000328 case ISD::FADD:
329 // FIXME: determine better conditions for this xform.
330 if (!UnsafeFPMath) return 0;
331
332 // -(A+B) -> -A - B
Chris Lattner0254e702008-02-26 07:04:54 +0000333 if (char V = isNegatibleForFree(Op.getOperand(0), AfterLegalize, Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000334 return V;
335 // -(A+B) -> -B - A
Chris Lattner0254e702008-02-26 07:04:54 +0000336 return isNegatibleForFree(Op.getOperand(1), AfterLegalize, Depth+1);
Chris Lattner29446522007-05-14 22:04:50 +0000337 case ISD::FSUB:
338 // We can't turn -(A-B) into B-A when we honor signed zeros.
339 if (!UnsafeFPMath) return 0;
340
341 // -(A-B) -> B-A
342 return 1;
343
344 case ISD::FMUL:
345 case ISD::FDIV:
346 if (HonorSignDependentRoundingFPMath()) return 0;
347
348 // -(X*Y) -> (-X * Y) or (X*-Y)
Chris Lattner0254e702008-02-26 07:04:54 +0000349 if (char V = isNegatibleForFree(Op.getOperand(0), AfterLegalize, Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000350 return V;
351
Chris Lattner0254e702008-02-26 07:04:54 +0000352 return isNegatibleForFree(Op.getOperand(1), AfterLegalize, Depth+1);
Chris Lattner29446522007-05-14 22:04:50 +0000353
354 case ISD::FP_EXTEND:
355 case ISD::FP_ROUND:
356 case ISD::FSIN:
Chris Lattner0254e702008-02-26 07:04:54 +0000357 return isNegatibleForFree(Op.getOperand(0), AfterLegalize, Depth+1);
Chris Lattner29446522007-05-14 22:04:50 +0000358 }
359}
360
361/// GetNegatedExpression - If isNegatibleForFree returns true, this function
362/// returns the newly negated expression.
Dan Gohman475871a2008-07-27 21:46:04 +0000363static SDValue GetNegatedExpression(SDValue Op, SelectionDAG &DAG,
Chris Lattner0254e702008-02-26 07:04:54 +0000364 bool AfterLegalize, unsigned Depth = 0) {
Chris Lattner29446522007-05-14 22:04:50 +0000365 // fneg is removable even if it has multiple uses.
366 if (Op.getOpcode() == ISD::FNEG) return Op.getOperand(0);
367
368 // Don't allow anything with multiple uses.
369 assert(Op.hasOneUse() && "Unknown reuse!");
370
Chris Lattner3adf9512007-05-25 02:19:06 +0000371 assert(Depth <= 6 && "GetNegatedExpression doesn't match isNegatibleForFree");
Chris Lattner29446522007-05-14 22:04:50 +0000372 switch (Op.getOpcode()) {
373 default: assert(0 && "Unknown code");
Dale Johannesenc4dd3c32007-08-31 23:34:27 +0000374 case ISD::ConstantFP: {
375 APFloat V = cast<ConstantFPSDNode>(Op)->getValueAPF();
376 V.changeSign();
377 return DAG.getConstantFP(V, Op.getValueType());
378 }
Chris Lattner29446522007-05-14 22:04:50 +0000379 case ISD::FADD:
380 // FIXME: determine better conditions for this xform.
381 assert(UnsafeFPMath);
382
383 // -(A+B) -> -A - B
Chris Lattner0254e702008-02-26 07:04:54 +0000384 if (isNegatibleForFree(Op.getOperand(0), AfterLegalize, Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000385 return DAG.getNode(ISD::FSUB, Op.getValueType(),
Chris Lattner0254e702008-02-26 07:04:54 +0000386 GetNegatedExpression(Op.getOperand(0), DAG,
387 AfterLegalize, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000388 Op.getOperand(1));
389 // -(A+B) -> -B - A
390 return DAG.getNode(ISD::FSUB, Op.getValueType(),
Chris Lattner0254e702008-02-26 07:04:54 +0000391 GetNegatedExpression(Op.getOperand(1), DAG,
392 AfterLegalize, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000393 Op.getOperand(0));
394 case ISD::FSUB:
395 // We can't turn -(A-B) into B-A when we honor signed zeros.
396 assert(UnsafeFPMath);
Dan Gohman23ff1822007-07-02 15:48:56 +0000397
398 // -(0-B) -> B
399 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(Op.getOperand(0)))
Dale Johannesenc4dd3c32007-08-31 23:34:27 +0000400 if (N0CFP->getValueAPF().isZero())
Dan Gohman23ff1822007-07-02 15:48:56 +0000401 return Op.getOperand(1);
Chris Lattner29446522007-05-14 22:04:50 +0000402
403 // -(A-B) -> B-A
404 return DAG.getNode(ISD::FSUB, Op.getValueType(), Op.getOperand(1),
405 Op.getOperand(0));
406
407 case ISD::FMUL:
408 case ISD::FDIV:
409 assert(!HonorSignDependentRoundingFPMath());
410
411 // -(X*Y) -> -X * Y
Chris Lattneraeecb6c2008-02-26 17:09:59 +0000412 if (isNegatibleForFree(Op.getOperand(0), AfterLegalize, Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000413 return DAG.getNode(Op.getOpcode(), Op.getValueType(),
Chris Lattner0254e702008-02-26 07:04:54 +0000414 GetNegatedExpression(Op.getOperand(0), DAG,
415 AfterLegalize, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000416 Op.getOperand(1));
417
418 // -(X*Y) -> X * -Y
419 return DAG.getNode(Op.getOpcode(), Op.getValueType(),
420 Op.getOperand(0),
Chris Lattner0254e702008-02-26 07:04:54 +0000421 GetNegatedExpression(Op.getOperand(1), DAG,
422 AfterLegalize, Depth+1));
Chris Lattner29446522007-05-14 22:04:50 +0000423
424 case ISD::FP_EXTEND:
Chris Lattner29446522007-05-14 22:04:50 +0000425 case ISD::FSIN:
426 return DAG.getNode(Op.getOpcode(), Op.getValueType(),
Chris Lattner0254e702008-02-26 07:04:54 +0000427 GetNegatedExpression(Op.getOperand(0), DAG,
428 AfterLegalize, Depth+1));
Chris Lattner0bd48932008-01-17 07:00:52 +0000429 case ISD::FP_ROUND:
430 return DAG.getNode(ISD::FP_ROUND, Op.getValueType(),
Chris Lattner0254e702008-02-26 07:04:54 +0000431 GetNegatedExpression(Op.getOperand(0), DAG,
432 AfterLegalize, Depth+1),
Chris Lattner0bd48932008-01-17 07:00:52 +0000433 Op.getOperand(1));
Chris Lattner29446522007-05-14 22:04:50 +0000434 }
435}
Chris Lattner24664722006-03-01 04:53:38 +0000436
437
Nate Begeman4ebd8052005-09-01 23:24:04 +0000438// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
439// that selects between the values 1 and 0, making it equivalent to a setcc.
Nate Begeman646d7e22005-09-02 21:18:40 +0000440// Also, set the incoming LHS, RHS, and CC references to the appropriate
441// nodes based on the type of node we are checking. This simplifies life a
442// bit for the callers.
Dan Gohman475871a2008-07-27 21:46:04 +0000443static bool isSetCCEquivalent(SDValue N, SDValue &LHS, SDValue &RHS,
444 SDValue &CC) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000445 if (N.getOpcode() == ISD::SETCC) {
446 LHS = N.getOperand(0);
447 RHS = N.getOperand(1);
448 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000449 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000450 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000451 if (N.getOpcode() == ISD::SELECT_CC &&
452 N.getOperand(2).getOpcode() == ISD::Constant &&
453 N.getOperand(3).getOpcode() == ISD::Constant &&
Dan Gohman002e5d02008-03-13 22:13:53 +0000454 cast<ConstantSDNode>(N.getOperand(2))->getAPIntValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000455 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
456 LHS = N.getOperand(0);
457 RHS = N.getOperand(1);
458 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000459 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000460 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000461 return false;
462}
463
Nate Begeman99801192005-09-07 23:25:52 +0000464// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
465// one use. If this is true, it allows the users to invert the operation for
466// free when it is profitable to do so.
Dan Gohman475871a2008-07-27 21:46:04 +0000467static bool isOneUseSetCC(SDValue N) {
468 SDValue N0, N1, N2;
Gabor Greifba36cb52008-08-28 21:40:38 +0000469 if (isSetCCEquivalent(N, N0, N1, N2) && N.getNode()->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000470 return true;
471 return false;
472}
473
Dan Gohman475871a2008-07-27 21:46:04 +0000474SDValue DAGCombiner::ReassociateOps(unsigned Opc, SDValue N0, SDValue N1){
Duncan Sands83ec4b62008-06-06 12:08:01 +0000475 MVT VT = N0.getValueType();
Nate Begemancd4d58c2006-02-03 06:46:56 +0000476 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
477 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
478 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
479 if (isa<ConstantSDNode>(N1)) {
Dan Gohman475871a2008-07-27 21:46:04 +0000480 SDValue OpNode = DAG.getNode(Opc, VT, N0.getOperand(1), N1);
Gabor Greifba36cb52008-08-28 21:40:38 +0000481 AddToWorkList(OpNode.getNode());
Nate Begemancd4d58c2006-02-03 06:46:56 +0000482 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(0));
483 } else if (N0.hasOneUse()) {
Dan Gohman475871a2008-07-27 21:46:04 +0000484 SDValue OpNode = DAG.getNode(Opc, VT, N0.getOperand(0), N1);
Gabor Greifba36cb52008-08-28 21:40:38 +0000485 AddToWorkList(OpNode.getNode());
Nate Begemancd4d58c2006-02-03 06:46:56 +0000486 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(1));
487 }
488 }
489 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
490 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
491 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
492 if (isa<ConstantSDNode>(N0)) {
Dan Gohman475871a2008-07-27 21:46:04 +0000493 SDValue OpNode = DAG.getNode(Opc, VT, N1.getOperand(1), N0);
Gabor Greifba36cb52008-08-28 21:40:38 +0000494 AddToWorkList(OpNode.getNode());
Nate Begemancd4d58c2006-02-03 06:46:56 +0000495 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(0));
496 } else if (N1.hasOneUse()) {
Dan Gohman475871a2008-07-27 21:46:04 +0000497 SDValue OpNode = DAG.getNode(Opc, VT, N1.getOperand(0), N0);
Gabor Greifba36cb52008-08-28 21:40:38 +0000498 AddToWorkList(OpNode.getNode());
Nate Begemancd4d58c2006-02-03 06:46:56 +0000499 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(1));
500 }
501 }
Dan Gohman475871a2008-07-27 21:46:04 +0000502 return SDValue();
Nate Begemancd4d58c2006-02-03 06:46:56 +0000503}
504
Dan Gohman475871a2008-07-27 21:46:04 +0000505SDValue DAGCombiner::CombineTo(SDNode *N, const SDValue *To, unsigned NumTo,
506 bool AddTo) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000507 assert(N->getNumValues() == NumTo && "Broken CombineTo call!");
508 ++NodesCombined;
509 DOUT << "\nReplacing.1 "; DEBUG(N->dump(&DAG));
Gabor Greifba36cb52008-08-28 21:40:38 +0000510 DOUT << "\nWith: "; DEBUG(To[0].getNode()->dump(&DAG));
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000511 DOUT << " and " << NumTo-1 << " other values\n";
512 WorkListRemover DeadNodes(*this);
513 DAG.ReplaceAllUsesWith(N, To, &DeadNodes);
514
515 if (AddTo) {
516 // Push the new nodes and any users onto the worklist
517 for (unsigned i = 0, e = NumTo; i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000518 AddToWorkList(To[i].getNode());
519 AddUsersToWorkList(To[i].getNode());
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000520 }
521 }
522
523 // Nodes can be reintroduced into the worklist. Make sure we do not
524 // process a node that has been replaced.
525 removeFromWorkList(N);
526
527 // Finally, since the node is now dead, remove it from the graph.
528 DAG.DeleteNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +0000529 return SDValue(N, 0);
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000530}
531
532/// SimplifyDemandedBits - Check the specified integer node value to see if
533/// it can be simplified or if things it uses can be simplified by bit
534/// propagation. If so, return true.
Dan Gohman475871a2008-07-27 21:46:04 +0000535bool DAGCombiner::SimplifyDemandedBits(SDValue Op, const APInt &Demanded) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000536 TargetLowering::TargetLoweringOpt TLO(DAG, AfterLegalize);
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000537 APInt KnownZero, KnownOne;
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000538 if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO))
539 return false;
540
541 // Revisit the node.
Gabor Greifba36cb52008-08-28 21:40:38 +0000542 AddToWorkList(Op.getNode());
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000543
544 // Replace the old value with the new one.
545 ++NodesCombined;
Gabor Greifba36cb52008-08-28 21:40:38 +0000546 DOUT << "\nReplacing.2 "; DEBUG(TLO.Old.getNode()->dump(&DAG));
547 DOUT << "\nWith: "; DEBUG(TLO.New.getNode()->dump(&DAG));
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000548 DOUT << '\n';
549
550 // Replace all uses. If any nodes become isomorphic to other nodes and
551 // are deleted, make sure to remove them from our worklist.
552 WorkListRemover DeadNodes(*this);
553 DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New, &DeadNodes);
554
555 // Push the new node and any (possibly new) users onto the worklist.
Gabor Greifba36cb52008-08-28 21:40:38 +0000556 AddToWorkList(TLO.New.getNode());
557 AddUsersToWorkList(TLO.New.getNode());
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000558
559 // Finally, if the node is now dead, remove it from the graph. The node
560 // may not be dead if the replacement process recursively simplified to
561 // something else needing this node.
Gabor Greifba36cb52008-08-28 21:40:38 +0000562 if (TLO.Old.getNode()->use_empty()) {
563 removeFromWorkList(TLO.Old.getNode());
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000564
565 // If the operands of this node are only used by the node, they will now
566 // be dead. Make sure to visit them first to delete dead nodes early.
Gabor Greifba36cb52008-08-28 21:40:38 +0000567 for (unsigned i = 0, e = TLO.Old.getNode()->getNumOperands(); i != e; ++i)
568 if (TLO.Old.getNode()->getOperand(i).getNode()->hasOneUse())
569 AddToWorkList(TLO.Old.getNode()->getOperand(i).getNode());
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000570
Gabor Greifba36cb52008-08-28 21:40:38 +0000571 DAG.DeleteNode(TLO.Old.getNode());
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000572 }
573 return true;
574}
575
Chris Lattner29446522007-05-14 22:04:50 +0000576//===----------------------------------------------------------------------===//
577// Main DAG Combiner implementation
578//===----------------------------------------------------------------------===//
579
Nate Begeman4ebd8052005-09-01 23:24:04 +0000580void DAGCombiner::Run(bool RunningAfterLegalize) {
581 // set the instance variable, so that the various visit routines may use it.
582 AfterLegalize = RunningAfterLegalize;
583
Evan Cheng17a568b2008-08-29 22:21:44 +0000584 // Add all the dag nodes to the worklist.
585 WorkList.reserve(DAG.allnodes_size());
586 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
587 E = DAG.allnodes_end(); I != E; ++I)
588 WorkList.push_back(I);
589
590 // Create a dummy node (which is not added to allnodes), that adds a reference
591 // to the root node, preventing it from being deleted, and tracking any
592 // changes of the root.
593 HandleSDNode Dummy(DAG.getRoot());
594
Jim Laskey26f7fa72006-10-17 19:33:52 +0000595 // The root of the dag may dangle to deleted nodes until the dag combiner is
596 // done. Set it to null to avoid confusion.
Dan Gohman475871a2008-07-27 21:46:04 +0000597 DAG.setRoot(SDValue());
Chris Lattner24664722006-03-01 04:53:38 +0000598
Evan Cheng17a568b2008-08-29 22:21:44 +0000599 // while the worklist isn't empty, inspect the node on the end of it and
600 // try and combine it.
601 while (!WorkList.empty()) {
602 SDNode *N = WorkList.back();
603 WorkList.pop_back();
604
605 // If N has no uses, it is dead. Make sure to revisit all N's operands once
606 // N is deleted from the DAG, since they too may now be dead or may have a
607 // reduced number of uses, allowing other xforms.
608 if (N->use_empty() && N != &Dummy) {
609 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
610 AddToWorkList(N->getOperand(i).getNode());
611
612 DAG.DeleteNode(N);
613 continue;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000614 }
Evan Cheng17a568b2008-08-29 22:21:44 +0000615
616 SDValue RV = combine(N);
617
618 if (RV.getNode() == 0)
619 continue;
620
621 ++NodesCombined;
622
623 // If we get back the same node we passed in, rather than a new node or
624 // zero, we know that the node must have defined multiple values and
625 // CombineTo was used. Since CombineTo takes care of the worklist
626 // mechanics for us, we have no work to do in this case.
627 if (RV.getNode() == N)
628 continue;
629
630 assert(N->getOpcode() != ISD::DELETED_NODE &&
631 RV.getNode()->getOpcode() != ISD::DELETED_NODE &&
632 "Node was deleted but visit returned new node!");
Chris Lattner729c6d12006-05-27 00:43:02 +0000633
Evan Cheng17a568b2008-08-29 22:21:44 +0000634 DOUT << "\nReplacing.3 "; DEBUG(N->dump(&DAG));
635 DOUT << "\nWith: "; DEBUG(RV.getNode()->dump(&DAG));
636 DOUT << '\n';
637 WorkListRemover DeadNodes(*this);
638 if (N->getNumValues() == RV.getNode()->getNumValues())
639 DAG.ReplaceAllUsesWith(N, RV.getNode(), &DeadNodes);
640 else {
641 assert(N->getValueType(0) == RV.getValueType() &&
642 N->getNumValues() == 1 && "Type mismatch");
643 SDValue OpV = RV;
644 DAG.ReplaceAllUsesWith(N, &OpV, &DeadNodes);
645 }
646
647 // Push the new node and any users onto the worklist
648 AddToWorkList(RV.getNode());
649 AddUsersToWorkList(RV.getNode());
650
651 // Add any uses of the old node to the worklist in case this node is the
652 // last one that uses them. They may become dead after this node is
653 // deleted.
654 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
655 AddToWorkList(N->getOperand(i).getNode());
656
657 // Nodes can be reintroduced into the worklist. Make sure we do not
658 // process a node that has been replaced.
659 removeFromWorkList(N);
660
661 // Finally, since the node is now dead, remove it from the graph.
662 DAG.DeleteNode(N);
663 }
664
Chris Lattner95038592005-10-05 06:35:28 +0000665 // If the root changed (e.g. it was a dead load, update the root).
666 DAG.setRoot(Dummy.getValue());
Nate Begeman1d4d4142005-09-01 00:19:25 +0000667}
668
Dan Gohman475871a2008-07-27 21:46:04 +0000669SDValue DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000670 switch(N->getOpcode()) {
671 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +0000672 case ISD::TokenFactor: return visitTokenFactor(N);
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000673 case ISD::MERGE_VALUES: return visitMERGE_VALUES(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000674 case ISD::ADD: return visitADD(N);
675 case ISD::SUB: return visitSUB(N);
Chris Lattner91153682007-03-04 20:03:15 +0000676 case ISD::ADDC: return visitADDC(N);
677 case ISD::ADDE: return visitADDE(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000678 case ISD::MUL: return visitMUL(N);
679 case ISD::SDIV: return visitSDIV(N);
680 case ISD::UDIV: return visitUDIV(N);
681 case ISD::SREM: return visitSREM(N);
682 case ISD::UREM: return visitUREM(N);
683 case ISD::MULHU: return visitMULHU(N);
684 case ISD::MULHS: return visitMULHS(N);
Dan Gohman389079b2007-10-08 17:57:15 +0000685 case ISD::SMUL_LOHI: return visitSMUL_LOHI(N);
686 case ISD::UMUL_LOHI: return visitUMUL_LOHI(N);
687 case ISD::SDIVREM: return visitSDIVREM(N);
688 case ISD::UDIVREM: return visitUDIVREM(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000689 case ISD::AND: return visitAND(N);
690 case ISD::OR: return visitOR(N);
691 case ISD::XOR: return visitXOR(N);
692 case ISD::SHL: return visitSHL(N);
693 case ISD::SRA: return visitSRA(N);
694 case ISD::SRL: return visitSRL(N);
695 case ISD::CTLZ: return visitCTLZ(N);
696 case ISD::CTTZ: return visitCTTZ(N);
697 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7beb2005-09-16 00:54:12 +0000698 case ISD::SELECT: return visitSELECT(N);
699 case ISD::SELECT_CC: return visitSELECT_CC(N);
700 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000701 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
702 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000703 case ISD::ANY_EXTEND: return visitANY_EXTEND(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000704 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
705 case ISD::TRUNCATE: return visitTRUNCATE(N);
Chris Lattner94683772005-12-23 05:30:37 +0000706 case ISD::BIT_CONVERT: return visitBIT_CONVERT(N);
Evan Cheng9bfa03c2008-05-12 23:04:07 +0000707 case ISD::BUILD_PAIR: return visitBUILD_PAIR(N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000708 case ISD::FADD: return visitFADD(N);
709 case ISD::FSUB: return visitFSUB(N);
710 case ISD::FMUL: return visitFMUL(N);
711 case ISD::FDIV: return visitFDIV(N);
712 case ISD::FREM: return visitFREM(N);
Chris Lattner12d83032006-03-05 05:30:57 +0000713 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000714 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
715 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
716 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
717 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
718 case ISD::FP_ROUND: return visitFP_ROUND(N);
719 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
720 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
721 case ISD::FNEG: return visitFNEG(N);
722 case ISD::FABS: return visitFABS(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000723 case ISD::BRCOND: return visitBRCOND(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000724 case ISD::BR_CC: return visitBR_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000725 case ISD::LOAD: return visitLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000726 case ISD::STORE: return visitSTORE(N);
Chris Lattnerca242442006-03-19 01:27:56 +0000727 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
Evan Cheng513da432007-10-06 08:19:55 +0000728 case ISD::EXTRACT_VECTOR_ELT: return visitEXTRACT_VECTOR_ELT(N);
Dan Gohman7f321562007-06-25 16:23:39 +0000729 case ISD::BUILD_VECTOR: return visitBUILD_VECTOR(N);
730 case ISD::CONCAT_VECTORS: return visitCONCAT_VECTORS(N);
Chris Lattner66445d32006-03-28 22:11:53 +0000731 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
Bill Wendling1c55a9c2008-11-21 02:12:42 +0000732 case ISD::SADDO: return visitSADDO(N);
733 case ISD::UADDO: return visitUADDO(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000734 }
Dan Gohman475871a2008-07-27 21:46:04 +0000735 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000736}
737
Dan Gohman475871a2008-07-27 21:46:04 +0000738SDValue DAGCombiner::combine(SDNode *N) {
Dan Gohman389079b2007-10-08 17:57:15 +0000739
Dan Gohman475871a2008-07-27 21:46:04 +0000740 SDValue RV = visit(N);
Dan Gohman389079b2007-10-08 17:57:15 +0000741
742 // If nothing happened, try a target-specific DAG combine.
Gabor Greifba36cb52008-08-28 21:40:38 +0000743 if (RV.getNode() == 0) {
Dan Gohman389079b2007-10-08 17:57:15 +0000744 assert(N->getOpcode() != ISD::DELETED_NODE &&
745 "Node was deleted but visit returned NULL!");
746
747 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
748 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode())) {
749
750 // Expose the DAG combiner to the target combiner impls.
751 TargetLowering::DAGCombinerInfo
752 DagCombineInfo(DAG, !AfterLegalize, false, this);
753
754 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
755 }
756 }
757
Evan Cheng08b11732008-03-22 01:55:50 +0000758 // If N is a commutative binary node, try commuting it to enable more
759 // sdisel CSE.
Gabor Greifba36cb52008-08-28 21:40:38 +0000760 if (RV.getNode() == 0 &&
Evan Cheng08b11732008-03-22 01:55:50 +0000761 SelectionDAG::isCommutativeBinOp(N->getOpcode()) &&
762 N->getNumValues() == 1) {
Dan Gohman475871a2008-07-27 21:46:04 +0000763 SDValue N0 = N->getOperand(0);
764 SDValue N1 = N->getOperand(1);
Evan Cheng08b11732008-03-22 01:55:50 +0000765 // Constant operands are canonicalized to RHS.
766 if (isa<ConstantSDNode>(N0) || !isa<ConstantSDNode>(N1)) {
Dan Gohman475871a2008-07-27 21:46:04 +0000767 SDValue Ops[] = { N1, N0 };
Evan Cheng08b11732008-03-22 01:55:50 +0000768 SDNode *CSENode = DAG.getNodeIfExists(N->getOpcode(), N->getVTList(),
769 Ops, 2);
Evan Chengea100462008-03-24 23:55:16 +0000770 if (CSENode)
Dan Gohman475871a2008-07-27 21:46:04 +0000771 return SDValue(CSENode, 0);
Evan Cheng08b11732008-03-22 01:55:50 +0000772 }
773 }
774
Dan Gohman389079b2007-10-08 17:57:15 +0000775 return RV;
776}
777
Chris Lattner6270f682006-10-08 22:57:01 +0000778/// getInputChainForNode - Given a node, return its input chain if it has one,
779/// otherwise return a null sd operand.
Dan Gohman475871a2008-07-27 21:46:04 +0000780static SDValue getInputChainForNode(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +0000781 if (unsigned NumOps = N->getNumOperands()) {
782 if (N->getOperand(0).getValueType() == MVT::Other)
783 return N->getOperand(0);
784 else if (N->getOperand(NumOps-1).getValueType() == MVT::Other)
785 return N->getOperand(NumOps-1);
786 for (unsigned i = 1; i < NumOps-1; ++i)
787 if (N->getOperand(i).getValueType() == MVT::Other)
788 return N->getOperand(i);
789 }
Dan Gohman475871a2008-07-27 21:46:04 +0000790 return SDValue(0, 0);
Chris Lattner6270f682006-10-08 22:57:01 +0000791}
792
Dan Gohman475871a2008-07-27 21:46:04 +0000793SDValue DAGCombiner::visitTokenFactor(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +0000794 // If N has two operands, where one has an input chain equal to the other,
795 // the 'other' chain is redundant.
796 if (N->getNumOperands() == 2) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000797 if (getInputChainForNode(N->getOperand(0).getNode()) == N->getOperand(1))
Chris Lattner6270f682006-10-08 22:57:01 +0000798 return N->getOperand(0);
Gabor Greifba36cb52008-08-28 21:40:38 +0000799 if (getInputChainForNode(N->getOperand(1).getNode()) == N->getOperand(0))
Chris Lattner6270f682006-10-08 22:57:01 +0000800 return N->getOperand(1);
801 }
802
Chris Lattnerc76d4412007-05-16 06:37:59 +0000803 SmallVector<SDNode *, 8> TFs; // List of token factors to visit.
Dan Gohman475871a2008-07-27 21:46:04 +0000804 SmallVector<SDValue, 8> Ops; // Ops for replacing token factor.
Chris Lattnerc76d4412007-05-16 06:37:59 +0000805 SmallPtrSet<SDNode*, 16> SeenOps;
806 bool Changed = false; // If we should replace this token factor.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000807
808 // Start out with this token factor.
Jim Laskey279f0532006-09-25 16:29:54 +0000809 TFs.push_back(N);
Jim Laskey279f0532006-09-25 16:29:54 +0000810
Jim Laskey71382342006-10-07 23:37:56 +0000811 // Iterate through token factors. The TFs grows when new token factors are
Jim Laskeybc588b82006-10-05 15:07:25 +0000812 // encountered.
813 for (unsigned i = 0; i < TFs.size(); ++i) {
814 SDNode *TF = TFs[i];
815
Jim Laskey6ff23e52006-10-04 16:53:27 +0000816 // Check each of the operands.
817 for (unsigned i = 0, ie = TF->getNumOperands(); i != ie; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +0000818 SDValue Op = TF->getOperand(i);
Jim Laskey279f0532006-09-25 16:29:54 +0000819
Jim Laskey6ff23e52006-10-04 16:53:27 +0000820 switch (Op.getOpcode()) {
821 case ISD::EntryToken:
Jim Laskeybc588b82006-10-05 15:07:25 +0000822 // Entry tokens don't need to be added to the list. They are
823 // rededundant.
824 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000825 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000826
Jim Laskey6ff23e52006-10-04 16:53:27 +0000827 case ISD::TokenFactor:
Jim Laskeybc588b82006-10-05 15:07:25 +0000828 if ((CombinerAA || Op.hasOneUse()) &&
Gabor Greifba36cb52008-08-28 21:40:38 +0000829 std::find(TFs.begin(), TFs.end(), Op.getNode()) == TFs.end()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +0000830 // Queue up for processing.
Gabor Greifba36cb52008-08-28 21:40:38 +0000831 TFs.push_back(Op.getNode());
Jim Laskey6ff23e52006-10-04 16:53:27 +0000832 // Clean up in case the token factor is removed.
Gabor Greifba36cb52008-08-28 21:40:38 +0000833 AddToWorkList(Op.getNode());
Jim Laskey6ff23e52006-10-04 16:53:27 +0000834 Changed = true;
835 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000836 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000837 // Fall thru
838
839 default:
Chris Lattnerc76d4412007-05-16 06:37:59 +0000840 // Only add if it isn't already in the list.
Gabor Greifba36cb52008-08-28 21:40:38 +0000841 if (SeenOps.insert(Op.getNode()))
Jim Laskeybc588b82006-10-05 15:07:25 +0000842 Ops.push_back(Op);
Chris Lattnerc76d4412007-05-16 06:37:59 +0000843 else
844 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000845 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000846 }
847 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000848 }
849
Dan Gohman475871a2008-07-27 21:46:04 +0000850 SDValue Result;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000851
852 // If we've change things around then replace token factor.
853 if (Changed) {
Dan Gohman30359592008-01-29 13:02:09 +0000854 if (Ops.empty()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +0000855 // The entry token is the only possible outcome.
856 Result = DAG.getEntryNode();
857 } else {
858 // New and improved token factor.
859 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0], Ops.size());
Nate Begemanded49632005-10-13 03:11:28 +0000860 }
Jim Laskey274062c2006-10-13 23:32:28 +0000861
862 // Don't add users to work list.
863 return CombineTo(N, Result, false);
Nate Begemanded49632005-10-13 03:11:28 +0000864 }
Jim Laskey279f0532006-09-25 16:29:54 +0000865
Jim Laskey6ff23e52006-10-04 16:53:27 +0000866 return Result;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000867}
868
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000869/// MERGE_VALUES can always be eliminated.
Dan Gohman475871a2008-07-27 21:46:04 +0000870SDValue DAGCombiner::visitMERGE_VALUES(SDNode *N) {
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000871 WorkListRemover DeadNodes(*this);
872 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Dan Gohman475871a2008-07-27 21:46:04 +0000873 DAG.ReplaceAllUsesOfValueWith(SDValue(N, i), N->getOperand(i),
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000874 &DeadNodes);
875 removeFromWorkList(N);
876 DAG.DeleteNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +0000877 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000878}
879
880
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000881static
Dan Gohman475871a2008-07-27 21:46:04 +0000882SDValue combineShlAddConstant(SDValue N0, SDValue N1, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000883 MVT VT = N0.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +0000884 SDValue N00 = N0.getOperand(0);
885 SDValue N01 = N0.getOperand(1);
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000886 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N01);
Gabor Greifba36cb52008-08-28 21:40:38 +0000887 if (N01C && N00.getOpcode() == ISD::ADD && N00.getNode()->hasOneUse() &&
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000888 isa<ConstantSDNode>(N00.getOperand(1))) {
889 N0 = DAG.getNode(ISD::ADD, VT,
890 DAG.getNode(ISD::SHL, VT, N00.getOperand(0), N01),
891 DAG.getNode(ISD::SHL, VT, N00.getOperand(1), N01));
892 return DAG.getNode(ISD::ADD, VT, N0, N1);
893 }
Dan Gohman475871a2008-07-27 21:46:04 +0000894 return SDValue();
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000895}
896
Evan Chengb13cdbd2007-06-21 07:39:16 +0000897static
Dan Gohman475871a2008-07-27 21:46:04 +0000898SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
Bill Wendlingae89bb12008-11-11 08:25:46 +0000899 SelectionDAG &DAG, const TargetLowering &TLI,
900 bool AfterLegalize) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000901 MVT VT = N->getValueType(0);
Evan Chengb13cdbd2007-06-21 07:39:16 +0000902 unsigned Opc = N->getOpcode();
903 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
Dan Gohman475871a2008-07-27 21:46:04 +0000904 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
905 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
Evan Chengb13cdbd2007-06-21 07:39:16 +0000906 ISD::CondCode CC = ISD::SETCC_INVALID;
Bill Wendlingae89bb12008-11-11 08:25:46 +0000907
908 if (isSlctCC) {
Evan Chengb13cdbd2007-06-21 07:39:16 +0000909 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
Bill Wendlingae89bb12008-11-11 08:25:46 +0000910 } else {
Dan Gohman475871a2008-07-27 21:46:04 +0000911 SDValue CCOp = Slct.getOperand(0);
Evan Chengb13cdbd2007-06-21 07:39:16 +0000912 if (CCOp.getOpcode() == ISD::SETCC)
913 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
914 }
915
916 bool DoXform = false;
917 bool InvCC = false;
918 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
919 "Bad input!");
Bill Wendlingae89bb12008-11-11 08:25:46 +0000920
Evan Chengb13cdbd2007-06-21 07:39:16 +0000921 if (LHS.getOpcode() == ISD::Constant &&
Bill Wendlingae89bb12008-11-11 08:25:46 +0000922 cast<ConstantSDNode>(LHS)->isNullValue()) {
Evan Chengb13cdbd2007-06-21 07:39:16 +0000923 DoXform = true;
Bill Wendlingae89bb12008-11-11 08:25:46 +0000924 } else if (CC != ISD::SETCC_INVALID &&
925 RHS.getOpcode() == ISD::Constant &&
926 cast<ConstantSDNode>(RHS)->isNullValue()) {
Evan Chengb13cdbd2007-06-21 07:39:16 +0000927 std::swap(LHS, RHS);
Dan Gohman475871a2008-07-27 21:46:04 +0000928 SDValue Op0 = Slct.getOperand(0);
Bill Wendlingae89bb12008-11-11 08:25:46 +0000929 MVT OpVT = isSlctCC ? Op0.getValueType() :
930 Op0.getOperand(0).getValueType();
931 bool isInt = OpVT.isInteger();
Evan Chengb13cdbd2007-06-21 07:39:16 +0000932 CC = ISD::getSetCCInverse(CC, isInt);
Bill Wendlingae89bb12008-11-11 08:25:46 +0000933
934 if (AfterLegalize && !TLI.isCondCodeLegal(CC, OpVT))
935 return SDValue(); // Inverse operator isn't legal.
936
Evan Chengb13cdbd2007-06-21 07:39:16 +0000937 DoXform = true;
938 InvCC = true;
939 }
940
941 if (DoXform) {
Dan Gohman475871a2008-07-27 21:46:04 +0000942 SDValue Result = DAG.getNode(Opc, VT, OtherOp, RHS);
Evan Chengb13cdbd2007-06-21 07:39:16 +0000943 if (isSlctCC)
944 return DAG.getSelectCC(OtherOp, Result,
945 Slct.getOperand(0), Slct.getOperand(1), CC);
Dan Gohman475871a2008-07-27 21:46:04 +0000946 SDValue CCOp = Slct.getOperand(0);
Evan Chengb13cdbd2007-06-21 07:39:16 +0000947 if (InvCC)
948 CCOp = DAG.getSetCC(CCOp.getValueType(), CCOp.getOperand(0),
949 CCOp.getOperand(1), CC);
950 return DAG.getNode(ISD::SELECT, VT, CCOp, OtherOp, Result);
951 }
Dan Gohman475871a2008-07-27 21:46:04 +0000952 return SDValue();
Evan Chengb13cdbd2007-06-21 07:39:16 +0000953}
954
Dan Gohman475871a2008-07-27 21:46:04 +0000955SDValue DAGCombiner::visitADD(SDNode *N) {
956 SDValue N0 = N->getOperand(0);
957 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000958 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
959 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000960 MVT VT = N0.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +0000961
962 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +0000963 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +0000964 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +0000965 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +0000966 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000967
Dan Gohman613e0d82007-07-03 14:03:57 +0000968 // fold (add x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +0000969 if (N0.getOpcode() == ISD::UNDEF)
970 return N0;
971 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +0000972 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000973 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000974 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +0000975 return DAG.FoldConstantArithmetic(ISD::ADD, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +0000976 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000977 if (N0C && !N1C)
978 return DAG.getNode(ISD::ADD, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000979 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000980 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000981 return N0;
Dan Gohman6520e202008-10-18 02:06:02 +0000982 // fold (add Sym, c) -> Sym+c
983 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0))
984 if (!AfterLegalize && TLI.isOffsetFoldingLegal(GA) && N1C &&
985 GA->getOpcode() == ISD::GlobalAddress)
986 return DAG.getGlobalAddress(GA->getGlobal(), VT,
987 GA->getOffset() +
988 (uint64_t)N1C->getSExtValue());
Chris Lattner4aafb4f2006-01-12 20:22:43 +0000989 // fold ((c1-A)+c2) -> (c1+c2)-A
990 if (N1C && N0.getOpcode() == ISD::SUB)
991 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
992 return DAG.getNode(ISD::SUB, VT,
Dan Gohman002e5d02008-03-13 22:13:53 +0000993 DAG.getConstant(N1C->getAPIntValue()+
994 N0C->getAPIntValue(), VT),
Chris Lattner4aafb4f2006-01-12 20:22:43 +0000995 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000996 // reassociate add
Dan Gohman475871a2008-07-27 21:46:04 +0000997 SDValue RADD = ReassociateOps(ISD::ADD, N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +0000998 if (RADD.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +0000999 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001000 // fold ((0-A) + B) -> B-A
1001 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
1002 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +00001003 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001004 // fold (A + (0-B)) -> A-B
1005 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
1006 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +00001007 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +00001008 // fold (A+(B-A)) -> B
1009 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001010 return N1.getOperand(0);
Chris Lattner947c2892006-03-13 06:51:27 +00001011
Dan Gohman475871a2008-07-27 21:46:04 +00001012 if (!VT.isVector() && SimplifyDemandedBits(SDValue(N, 0)))
1013 return SDValue(N, 0);
Chris Lattner947c2892006-03-13 06:51:27 +00001014
1015 // fold (a+b) -> (a|b) iff a and b share no bits.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001016 if (VT.isInteger() && !VT.isVector()) {
Dan Gohman948d8ea2008-02-20 16:33:30 +00001017 APInt LHSZero, LHSOne;
1018 APInt RHSZero, RHSOne;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001019 APInt Mask = APInt::getAllOnesValue(VT.getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00001020 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Dan Gohman948d8ea2008-02-20 16:33:30 +00001021 if (LHSZero.getBoolValue()) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001022 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
Chris Lattner947c2892006-03-13 06:51:27 +00001023
1024 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1025 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
1026 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
1027 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
1028 return DAG.getNode(ISD::OR, VT, N0, N1);
1029 }
1030 }
Evan Cheng3ef554d2006-11-06 08:14:30 +00001031
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001032 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
Gabor Greifba36cb52008-08-28 21:40:38 +00001033 if (N0.getOpcode() == ISD::SHL && N0.getNode()->hasOneUse()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001034 SDValue Result = combineShlAddConstant(N0, N1, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001035 if (Result.getNode()) return Result;
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001036 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001037 if (N1.getOpcode() == ISD::SHL && N1.getNode()->hasOneUse()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001038 SDValue Result = combineShlAddConstant(N1, N0, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001039 if (Result.getNode()) return Result;
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001040 }
1041
Evan Chengb13cdbd2007-06-21 07:39:16 +00001042 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
Gabor Greifba36cb52008-08-28 21:40:38 +00001043 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
Bill Wendlingae89bb12008-11-11 08:25:46 +00001044 SDValue Result = combineSelectAndUse(N, N0, N1, DAG, TLI, AfterLegalize);
Gabor Greifba36cb52008-08-28 21:40:38 +00001045 if (Result.getNode()) return Result;
Evan Chengb13cdbd2007-06-21 07:39:16 +00001046 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001047 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
Bill Wendlingae89bb12008-11-11 08:25:46 +00001048 SDValue Result = combineSelectAndUse(N, N1, N0, DAG, TLI, AfterLegalize);
Gabor Greifba36cb52008-08-28 21:40:38 +00001049 if (Result.getNode()) return Result;
Evan Chengb13cdbd2007-06-21 07:39:16 +00001050 }
1051
Dan Gohman475871a2008-07-27 21:46:04 +00001052 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001053}
1054
Dan Gohman475871a2008-07-27 21:46:04 +00001055SDValue DAGCombiner::visitADDC(SDNode *N) {
1056 SDValue N0 = N->getOperand(0);
1057 SDValue N1 = N->getOperand(1);
Chris Lattner91153682007-03-04 20:03:15 +00001058 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1059 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001060 MVT VT = N0.getValueType();
Chris Lattner91153682007-03-04 20:03:15 +00001061
1062 // If the flag result is dead, turn this into an ADD.
1063 if (N->hasNUsesOfValue(0, 1))
1064 return CombineTo(N, DAG.getNode(ISD::ADD, VT, N1, N0),
Chris Lattnerb6541762007-03-04 20:40:38 +00001065 DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
Chris Lattner91153682007-03-04 20:03:15 +00001066
1067 // canonicalize constant to RHS.
Dan Gohman0a4627d2008-06-23 15:29:14 +00001068 if (N0C && !N1C)
Dan Gohman56867522008-06-21 22:06:07 +00001069 return DAG.getNode(ISD::ADDC, N->getVTList(), N1, N0);
Chris Lattner91153682007-03-04 20:03:15 +00001070
Chris Lattnerb6541762007-03-04 20:40:38 +00001071 // fold (addc x, 0) -> x + no carry out
1072 if (N1C && N1C->isNullValue())
1073 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
1074
1075 // fold (addc a, b) -> (or a, b), CARRY_FALSE iff a and b share no bits.
Dan Gohman948d8ea2008-02-20 16:33:30 +00001076 APInt LHSZero, LHSOne;
1077 APInt RHSZero, RHSOne;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001078 APInt Mask = APInt::getAllOnesValue(VT.getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00001079 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Dan Gohman948d8ea2008-02-20 16:33:30 +00001080 if (LHSZero.getBoolValue()) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001081 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
Chris Lattnerb6541762007-03-04 20:40:38 +00001082
1083 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1084 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
1085 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
1086 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
1087 return CombineTo(N, DAG.getNode(ISD::OR, VT, N0, N1),
1088 DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
1089 }
Chris Lattner91153682007-03-04 20:03:15 +00001090
Dan Gohman475871a2008-07-27 21:46:04 +00001091 return SDValue();
Chris Lattner91153682007-03-04 20:03:15 +00001092}
1093
Dan Gohman475871a2008-07-27 21:46:04 +00001094SDValue DAGCombiner::visitADDE(SDNode *N) {
1095 SDValue N0 = N->getOperand(0);
1096 SDValue N1 = N->getOperand(1);
1097 SDValue CarryIn = N->getOperand(2);
Chris Lattner91153682007-03-04 20:03:15 +00001098 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1099 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001100 //MVT VT = N0.getValueType();
Chris Lattner91153682007-03-04 20:03:15 +00001101
1102 // canonicalize constant to RHS
Dan Gohman0a4627d2008-06-23 15:29:14 +00001103 if (N0C && !N1C)
Dan Gohman56867522008-06-21 22:06:07 +00001104 return DAG.getNode(ISD::ADDE, N->getVTList(), N1, N0, CarryIn);
Chris Lattner91153682007-03-04 20:03:15 +00001105
Chris Lattnerb6541762007-03-04 20:40:38 +00001106 // fold (adde x, y, false) -> (addc x, y)
Dan Gohman0a4627d2008-06-23 15:29:14 +00001107 if (CarryIn.getOpcode() == ISD::CARRY_FALSE)
Dan Gohman56867522008-06-21 22:06:07 +00001108 return DAG.getNode(ISD::ADDC, N->getVTList(), N1, N0);
Chris Lattner91153682007-03-04 20:03:15 +00001109
Dan Gohman475871a2008-07-27 21:46:04 +00001110 return SDValue();
Chris Lattner91153682007-03-04 20:03:15 +00001111}
1112
1113
1114
Dan Gohman475871a2008-07-27 21:46:04 +00001115SDValue DAGCombiner::visitSUB(SDNode *N) {
1116 SDValue N0 = N->getOperand(0);
1117 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001118 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1119 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Duncan Sands83ec4b62008-06-06 12:08:01 +00001120 MVT VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001121
Dan Gohman7f321562007-06-25 16:23:39 +00001122 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001123 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001124 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001125 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001126 }
Dan Gohman7f321562007-06-25 16:23:39 +00001127
Chris Lattner854077d2005-10-17 01:07:11 +00001128 // fold (sub x, x) -> 0
Evan Chengc8e3b142008-03-12 07:02:50 +00001129 if (N0 == N1)
Chris Lattner854077d2005-10-17 01:07:11 +00001130 return DAG.getConstant(0, N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001131 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001132 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001133 return DAG.FoldConstantArithmetic(ISD::SUB, VT, N0C, N1C);
Chris Lattner05b57432005-10-11 06:07:15 +00001134 // fold (sub x, c) -> (add x, -c)
1135 if (N1C)
Dan Gohman002e5d02008-03-13 22:13:53 +00001136 return DAG.getNode(ISD::ADD, VT, N0,
1137 DAG.getConstant(-N1C->getAPIntValue(), VT));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001138 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +00001139 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001140 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001141 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +00001142 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001143 return N0.getOperand(0);
Evan Chengb13cdbd2007-06-21 07:39:16 +00001144 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
Gabor Greifba36cb52008-08-28 21:40:38 +00001145 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
Bill Wendlingae89bb12008-11-11 08:25:46 +00001146 SDValue Result = combineSelectAndUse(N, N1, N0, DAG, TLI, AfterLegalize);
Gabor Greifba36cb52008-08-28 21:40:38 +00001147 if (Result.getNode()) return Result;
Evan Chengb13cdbd2007-06-21 07:39:16 +00001148 }
Dan Gohman613e0d82007-07-03 14:03:57 +00001149 // If either operand of a sub is undef, the result is undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00001150 if (N0.getOpcode() == ISD::UNDEF)
1151 return N0;
1152 if (N1.getOpcode() == ISD::UNDEF)
1153 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001154
Dan Gohman6520e202008-10-18 02:06:02 +00001155 // If the relocation model supports it, consider symbol offsets.
1156 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0))
1157 if (!AfterLegalize && TLI.isOffsetFoldingLegal(GA)) {
1158 // fold (sub Sym, c) -> Sym-c
1159 if (N1C && GA->getOpcode() == ISD::GlobalAddress)
1160 return DAG.getGlobalAddress(GA->getGlobal(), VT,
1161 GA->getOffset() -
1162 (uint64_t)N1C->getSExtValue());
1163 // fold (sub Sym+c1, Sym+c2) -> c1-c2
1164 if (GlobalAddressSDNode *GB = dyn_cast<GlobalAddressSDNode>(N1))
1165 if (GA->getGlobal() == GB->getGlobal())
1166 return DAG.getConstant((uint64_t)GA->getOffset() - GB->getOffset(),
1167 VT);
1168 }
1169
Dan Gohman475871a2008-07-27 21:46:04 +00001170 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001171}
1172
Dan Gohman475871a2008-07-27 21:46:04 +00001173SDValue DAGCombiner::visitMUL(SDNode *N) {
1174 SDValue N0 = N->getOperand(0);
1175 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001176 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1177 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001178 MVT VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001179
Dan Gohman7f321562007-06-25 16:23:39 +00001180 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001181 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001182 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001183 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001184 }
Dan Gohman7f321562007-06-25 16:23:39 +00001185
Dan Gohman613e0d82007-07-03 14:03:57 +00001186 // fold (mul x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001187 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001188 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001189 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001190 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001191 return DAG.FoldConstantArithmetic(ISD::MUL, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00001192 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001193 if (N0C && !N1C)
1194 return DAG.getNode(ISD::MUL, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001195 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001196 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001197 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001198 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +00001199 if (N1C && N1C->isAllOnesValue())
Nate Begeman405e3ec2005-10-21 00:02:42 +00001200 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001201 // fold (mul x, (1 << c)) -> x << c
Dan Gohman002e5d02008-03-13 22:13:53 +00001202 if (N1C && N1C->getAPIntValue().isPowerOf2())
Chris Lattner3e6099b2005-10-30 06:41:49 +00001203 return DAG.getNode(ISD::SHL, VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001204 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001205 TLI.getShiftAmountTy()));
Chris Lattner3e6099b2005-10-30 06:41:49 +00001206 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
Dan Gohman7810bfe2008-09-26 21:54:37 +00001207 if (N1C && isPowerOf2_64(-N1C->getSExtValue())) {
Chris Lattner3e6099b2005-10-30 06:41:49 +00001208 // FIXME: If the input is something that is easily negated (e.g. a
1209 // single-use add), we should put the negate there.
1210 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT),
1211 DAG.getNode(ISD::SHL, VT, N0,
Dan Gohman7810bfe2008-09-26 21:54:37 +00001212 DAG.getConstant(Log2_64(-N1C->getSExtValue()),
Chris Lattner3e6099b2005-10-30 06:41:49 +00001213 TLI.getShiftAmountTy())));
1214 }
Andrew Lenharth50a0d422006-04-02 21:42:45 +00001215
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001216 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
1217 if (N1C && N0.getOpcode() == ISD::SHL &&
1218 isa<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohman475871a2008-07-27 21:46:04 +00001219 SDValue C3 = DAG.getNode(ISD::SHL, VT, N1, N0.getOperand(1));
Gabor Greifba36cb52008-08-28 21:40:38 +00001220 AddToWorkList(C3.getNode());
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001221 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0), C3);
1222 }
1223
1224 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
1225 // use.
1226 {
Dan Gohman475871a2008-07-27 21:46:04 +00001227 SDValue Sh(0,0), Y(0,0);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001228 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
1229 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001230 N0.getNode()->hasOneUse()) {
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001231 Sh = N0; Y = N1;
1232 } else if (N1.getOpcode() == ISD::SHL &&
Gabor Greif12632d22008-08-30 19:29:20 +00001233 isa<ConstantSDNode>(N1.getOperand(1)) &&
1234 N1.getNode()->hasOneUse()) {
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001235 Sh = N1; Y = N0;
1236 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001237 if (Sh.getNode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001238 SDValue Mul = DAG.getNode(ISD::MUL, VT, Sh.getOperand(0), Y);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001239 return DAG.getNode(ISD::SHL, VT, Mul, Sh.getOperand(1));
1240 }
1241 }
Chris Lattnera1deca32006-03-04 23:33:26 +00001242 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
Gabor Greifba36cb52008-08-28 21:40:38 +00001243 if (N1C && N0.getOpcode() == ISD::ADD && N0.getNode()->hasOneUse() &&
Chris Lattnera1deca32006-03-04 23:33:26 +00001244 isa<ConstantSDNode>(N0.getOperand(1))) {
1245 return DAG.getNode(ISD::ADD, VT,
1246 DAG.getNode(ISD::MUL, VT, N0.getOperand(0), N1),
1247 DAG.getNode(ISD::MUL, VT, N0.getOperand(1), N1));
1248 }
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001249
Nate Begemancd4d58c2006-02-03 06:46:56 +00001250 // reassociate mul
Dan Gohman475871a2008-07-27 21:46:04 +00001251 SDValue RMUL = ReassociateOps(ISD::MUL, N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001252 if (RMUL.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001253 return RMUL;
Dan Gohman7f321562007-06-25 16:23:39 +00001254
Dan Gohman475871a2008-07-27 21:46:04 +00001255 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001256}
1257
Dan Gohman475871a2008-07-27 21:46:04 +00001258SDValue DAGCombiner::visitSDIV(SDNode *N) {
1259 SDValue N0 = N->getOperand(0);
1260 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001261 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1262 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Duncan Sands83ec4b62008-06-06 12:08:01 +00001263 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001264
Dan Gohman7f321562007-06-25 16:23:39 +00001265 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001266 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001267 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001268 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001269 }
Dan Gohman7f321562007-06-25 16:23:39 +00001270
Nate Begeman1d4d4142005-09-01 00:19:25 +00001271 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001272 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001273 return DAG.FoldConstantArithmetic(ISD::SDIV, VT, N0C, N1C);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001274 // fold (sdiv X, 1) -> X
Dan Gohman7810bfe2008-09-26 21:54:37 +00001275 if (N1C && N1C->getSExtValue() == 1LL)
Nate Begeman405e3ec2005-10-21 00:02:42 +00001276 return N0;
1277 // fold (sdiv X, -1) -> 0-X
1278 if (N1C && N1C->isAllOnesValue())
1279 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +00001280 // If we know the sign bits of both operands are zero, strength reduce to a
1281 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
Duncan Sands83ec4b62008-06-06 12:08:01 +00001282 if (!VT.isVector()) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001283 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Chris Lattnerf32aac32008-01-27 23:32:17 +00001284 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
1285 }
Nate Begemancd6a6ed2006-02-17 07:26:20 +00001286 // fold (sdiv X, pow2) -> simple ops after legalize
Dan Gohman002e5d02008-03-13 22:13:53 +00001287 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap() &&
Dan Gohman7810bfe2008-09-26 21:54:37 +00001288 (isPowerOf2_64(N1C->getSExtValue()) ||
1289 isPowerOf2_64(-N1C->getSExtValue()))) {
Nate Begeman405e3ec2005-10-21 00:02:42 +00001290 // If dividing by powers of two is cheap, then don't perform the following
1291 // fold.
1292 if (TLI.isPow2DivCheap())
Dan Gohman475871a2008-07-27 21:46:04 +00001293 return SDValue();
Dan Gohman7810bfe2008-09-26 21:54:37 +00001294 int64_t pow2 = N1C->getSExtValue();
Nate Begeman405e3ec2005-10-21 00:02:42 +00001295 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
Chris Lattner8f4880b2006-02-16 08:02:36 +00001296 unsigned lg2 = Log2_64(abs2);
1297 // Splat the sign bit into the register
Dan Gohman475871a2008-07-27 21:46:04 +00001298 SDValue SGN = DAG.getNode(ISD::SRA, VT, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001299 DAG.getConstant(VT.getSizeInBits()-1,
Nate Begeman405e3ec2005-10-21 00:02:42 +00001300 TLI.getShiftAmountTy()));
Gabor Greifba36cb52008-08-28 21:40:38 +00001301 AddToWorkList(SGN.getNode());
Chris Lattner8f4880b2006-02-16 08:02:36 +00001302 // Add (N0 < 0) ? abs2 - 1 : 0;
Dan Gohman475871a2008-07-27 21:46:04 +00001303 SDValue SRL = DAG.getNode(ISD::SRL, VT, SGN,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001304 DAG.getConstant(VT.getSizeInBits()-lg2,
Nate Begeman405e3ec2005-10-21 00:02:42 +00001305 TLI.getShiftAmountTy()));
Dan Gohman475871a2008-07-27 21:46:04 +00001306 SDValue ADD = DAG.getNode(ISD::ADD, VT, N0, SRL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001307 AddToWorkList(SRL.getNode());
1308 AddToWorkList(ADD.getNode()); // Divide by pow2
Dan Gohman475871a2008-07-27 21:46:04 +00001309 SDValue SRA = DAG.getNode(ISD::SRA, VT, ADD,
Chris Lattner8f4880b2006-02-16 08:02:36 +00001310 DAG.getConstant(lg2, TLI.getShiftAmountTy()));
Nate Begeman405e3ec2005-10-21 00:02:42 +00001311 // If we're dividing by a positive value, we're done. Otherwise, we must
1312 // negate the result.
1313 if (pow2 > 0)
1314 return SRA;
Gabor Greifba36cb52008-08-28 21:40:38 +00001315 AddToWorkList(SRA.getNode());
Nate Begeman405e3ec2005-10-21 00:02:42 +00001316 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), SRA);
1317 }
Nate Begeman69575232005-10-20 02:15:44 +00001318 // if integer divide is expensive and we satisfy the requirements, emit an
1319 // alternate sequence.
Dan Gohman7810bfe2008-09-26 21:54:37 +00001320 if (N1C && (N1C->getSExtValue() < -1 || N1C->getSExtValue() > 1) &&
Chris Lattnere9936d12005-10-22 18:50:15 +00001321 !TLI.isIntDivCheap()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001322 SDValue Op = BuildSDIV(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001323 if (Op.getNode()) return Op;
Nate Begeman69575232005-10-20 02:15:44 +00001324 }
Dan Gohman7f321562007-06-25 16:23:39 +00001325
Dan Gohman613e0d82007-07-03 14:03:57 +00001326 // undef / X -> 0
1327 if (N0.getOpcode() == ISD::UNDEF)
1328 return DAG.getConstant(0, VT);
1329 // X / undef -> undef
1330 if (N1.getOpcode() == ISD::UNDEF)
1331 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001332
Dan Gohman475871a2008-07-27 21:46:04 +00001333 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001334}
1335
Dan Gohman475871a2008-07-27 21:46:04 +00001336SDValue DAGCombiner::visitUDIV(SDNode *N) {
1337 SDValue N0 = N->getOperand(0);
1338 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001339 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1340 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Duncan Sands83ec4b62008-06-06 12:08:01 +00001341 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001342
Dan Gohman7f321562007-06-25 16:23:39 +00001343 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001344 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001345 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001346 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001347 }
Dan Gohman7f321562007-06-25 16:23:39 +00001348
Nate Begeman1d4d4142005-09-01 00:19:25 +00001349 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001350 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001351 return DAG.FoldConstantArithmetic(ISD::UDIV, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001352 // fold (udiv x, (1 << c)) -> x >>u c
Dan Gohman002e5d02008-03-13 22:13:53 +00001353 if (N1C && N1C->getAPIntValue().isPowerOf2())
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001354 return DAG.getNode(ISD::SRL, VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001355 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001356 TLI.getShiftAmountTy()));
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001357 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
1358 if (N1.getOpcode() == ISD::SHL) {
1359 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00001360 if (SHC->getAPIntValue().isPowerOf2()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001361 MVT ADDVT = N1.getOperand(1).getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00001362 SDValue Add = DAG.getNode(ISD::ADD, ADDVT, N1.getOperand(1),
Dan Gohman002e5d02008-03-13 22:13:53 +00001363 DAG.getConstant(SHC->getAPIntValue()
1364 .logBase2(),
Nate Begemanc031e332006-02-05 07:36:48 +00001365 ADDVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00001366 AddToWorkList(Add.getNode());
Nate Begemanc031e332006-02-05 07:36:48 +00001367 return DAG.getNode(ISD::SRL, VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001368 }
1369 }
1370 }
Nate Begeman69575232005-10-20 02:15:44 +00001371 // fold (udiv x, c) -> alternate
Dan Gohman002e5d02008-03-13 22:13:53 +00001372 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001373 SDValue Op = BuildUDIV(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001374 if (Op.getNode()) return Op;
Chris Lattnere9936d12005-10-22 18:50:15 +00001375 }
Dan Gohman7f321562007-06-25 16:23:39 +00001376
Dan Gohman613e0d82007-07-03 14:03:57 +00001377 // undef / X -> 0
1378 if (N0.getOpcode() == ISD::UNDEF)
1379 return DAG.getConstant(0, VT);
1380 // X / undef -> undef
1381 if (N1.getOpcode() == ISD::UNDEF)
1382 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001383
Dan Gohman475871a2008-07-27 21:46:04 +00001384 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001385}
1386
Dan Gohman475871a2008-07-27 21:46:04 +00001387SDValue DAGCombiner::visitSREM(SDNode *N) {
1388 SDValue N0 = N->getOperand(0);
1389 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001390 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1391 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001392 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001393
1394 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001395 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001396 return DAG.FoldConstantArithmetic(ISD::SREM, VT, N0C, N1C);
Nate Begeman07ed4172005-10-10 21:26:48 +00001397 // If we know the sign bits of both operands are zero, strength reduce to a
1398 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
Duncan Sands83ec4b62008-06-06 12:08:01 +00001399 if (!VT.isVector()) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001400 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Chris Lattneree339f42008-01-27 23:21:58 +00001401 return DAG.getNode(ISD::UREM, VT, N0, N1);
1402 }
Chris Lattner26d29902006-10-12 20:58:32 +00001403
Dan Gohman77003042007-11-26 23:46:11 +00001404 // If X/C can be simplified by the division-by-constant logic, lower
1405 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00001406 if (N1C && !N1C->isNullValue()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001407 SDValue Div = DAG.getNode(ISD::SDIV, VT, N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001408 AddToWorkList(Div.getNode());
1409 SDValue OptimizedDiv = combine(Div.getNode());
1410 if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001411 SDValue Mul = DAG.getNode(ISD::MUL, VT, OptimizedDiv, N1);
1412 SDValue Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
Gabor Greifba36cb52008-08-28 21:40:38 +00001413 AddToWorkList(Mul.getNode());
Dan Gohman77003042007-11-26 23:46:11 +00001414 return Sub;
1415 }
Chris Lattner26d29902006-10-12 20:58:32 +00001416 }
1417
Dan Gohman613e0d82007-07-03 14:03:57 +00001418 // undef % X -> 0
1419 if (N0.getOpcode() == ISD::UNDEF)
1420 return DAG.getConstant(0, VT);
1421 // X % undef -> undef
1422 if (N1.getOpcode() == ISD::UNDEF)
1423 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001424
Dan Gohman475871a2008-07-27 21:46:04 +00001425 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001426}
1427
Dan Gohman475871a2008-07-27 21:46:04 +00001428SDValue DAGCombiner::visitUREM(SDNode *N) {
1429 SDValue N0 = N->getOperand(0);
1430 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001431 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1432 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001433 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001434
1435 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001436 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001437 return DAG.FoldConstantArithmetic(ISD::UREM, VT, N0C, N1C);
Nate Begeman07ed4172005-10-10 21:26:48 +00001438 // fold (urem x, pow2) -> (and x, pow2-1)
Dan Gohman002e5d02008-03-13 22:13:53 +00001439 if (N1C && !N1C->isNullValue() && N1C->getAPIntValue().isPowerOf2())
1440 return DAG.getNode(ISD::AND, VT, N0,
1441 DAG.getConstant(N1C->getAPIntValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +00001442 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
1443 if (N1.getOpcode() == ISD::SHL) {
1444 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00001445 if (SHC->getAPIntValue().isPowerOf2()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001446 SDValue Add =
Dan Gohman002e5d02008-03-13 22:13:53 +00001447 DAG.getNode(ISD::ADD, VT, N1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001448 DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()),
Dan Gohman002e5d02008-03-13 22:13:53 +00001449 VT));
Gabor Greifba36cb52008-08-28 21:40:38 +00001450 AddToWorkList(Add.getNode());
Nate Begemanc031e332006-02-05 07:36:48 +00001451 return DAG.getNode(ISD::AND, VT, N0, Add);
1452 }
1453 }
1454 }
Chris Lattner26d29902006-10-12 20:58:32 +00001455
Dan Gohman77003042007-11-26 23:46:11 +00001456 // If X/C can be simplified by the division-by-constant logic, lower
1457 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00001458 if (N1C && !N1C->isNullValue()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001459 SDValue Div = DAG.getNode(ISD::UDIV, VT, N0, N1);
Dan Gohman942ca7f2008-09-08 16:59:01 +00001460 AddToWorkList(Div.getNode());
Gabor Greifba36cb52008-08-28 21:40:38 +00001461 SDValue OptimizedDiv = combine(Div.getNode());
1462 if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001463 SDValue Mul = DAG.getNode(ISD::MUL, VT, OptimizedDiv, N1);
1464 SDValue Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
Gabor Greifba36cb52008-08-28 21:40:38 +00001465 AddToWorkList(Mul.getNode());
Dan Gohman77003042007-11-26 23:46:11 +00001466 return Sub;
1467 }
Chris Lattner26d29902006-10-12 20:58:32 +00001468 }
1469
Dan Gohman613e0d82007-07-03 14:03:57 +00001470 // undef % X -> 0
1471 if (N0.getOpcode() == ISD::UNDEF)
1472 return DAG.getConstant(0, VT);
1473 // X % undef -> undef
1474 if (N1.getOpcode() == ISD::UNDEF)
1475 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001476
Dan Gohman475871a2008-07-27 21:46:04 +00001477 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001478}
1479
Dan Gohman475871a2008-07-27 21:46:04 +00001480SDValue DAGCombiner::visitMULHS(SDNode *N) {
1481 SDValue N0 = N->getOperand(0);
1482 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001483 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001484 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001485
1486 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001487 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001488 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001489 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Dan Gohman002e5d02008-03-13 22:13:53 +00001490 if (N1C && N1C->getAPIntValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001491 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001492 DAG.getConstant(N0.getValueType().getSizeInBits()-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001493 TLI.getShiftAmountTy()));
Dan Gohman613e0d82007-07-03 14:03:57 +00001494 // fold (mulhs x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001495 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001496 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00001497
Dan Gohman475871a2008-07-27 21:46:04 +00001498 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001499}
1500
Dan Gohman475871a2008-07-27 21:46:04 +00001501SDValue DAGCombiner::visitMULHU(SDNode *N) {
1502 SDValue N0 = N->getOperand(0);
1503 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001504 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001505 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001506
1507 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001508 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001509 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001510 // fold (mulhu x, 1) -> 0
Dan Gohman002e5d02008-03-13 22:13:53 +00001511 if (N1C && N1C->getAPIntValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001512 return DAG.getConstant(0, N0.getValueType());
Dan Gohman613e0d82007-07-03 14:03:57 +00001513 // fold (mulhu x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001514 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001515 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00001516
Dan Gohman475871a2008-07-27 21:46:04 +00001517 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001518}
1519
Dan Gohman389079b2007-10-08 17:57:15 +00001520/// SimplifyNodeWithTwoResults - Perform optimizations common to nodes that
1521/// compute two values. LoOp and HiOp give the opcodes for the two computations
1522/// that are being performed. Return true if a simplification was made.
1523///
Dan Gohman475871a2008-07-27 21:46:04 +00001524SDValue DAGCombiner::SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
1525 unsigned HiOp) {
Dan Gohman389079b2007-10-08 17:57:15 +00001526 // If the high half is not needed, just compute the low half.
Evan Cheng44711942007-11-08 09:25:29 +00001527 bool HiExists = N->hasAnyUseOfValue(1);
1528 if (!HiExists &&
Dan Gohman389079b2007-10-08 17:57:15 +00001529 (!AfterLegalize ||
1530 TLI.isOperationLegal(LoOp, N->getValueType(0)))) {
Dan Gohman475871a2008-07-27 21:46:04 +00001531 SDValue Res = DAG.getNode(LoOp, N->getValueType(0), N->op_begin(),
Chris Lattner5eee4272008-01-26 01:09:19 +00001532 N->getNumOperands());
1533 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00001534 }
1535
1536 // If the low half is not needed, just compute the high half.
Evan Cheng44711942007-11-08 09:25:29 +00001537 bool LoExists = N->hasAnyUseOfValue(0);
1538 if (!LoExists &&
Dan Gohman389079b2007-10-08 17:57:15 +00001539 (!AfterLegalize ||
1540 TLI.isOperationLegal(HiOp, N->getValueType(1)))) {
Dan Gohman475871a2008-07-27 21:46:04 +00001541 SDValue Res = DAG.getNode(HiOp, N->getValueType(1), N->op_begin(),
Chris Lattner5eee4272008-01-26 01:09:19 +00001542 N->getNumOperands());
1543 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00001544 }
1545
Evan Cheng44711942007-11-08 09:25:29 +00001546 // If both halves are used, return as it is.
1547 if (LoExists && HiExists)
Dan Gohman475871a2008-07-27 21:46:04 +00001548 return SDValue();
Evan Cheng44711942007-11-08 09:25:29 +00001549
1550 // If the two computed results can be simplified separately, separate them.
Evan Cheng44711942007-11-08 09:25:29 +00001551 if (LoExists) {
Dan Gohman475871a2008-07-27 21:46:04 +00001552 SDValue Lo = DAG.getNode(LoOp, N->getValueType(0),
Evan Cheng44711942007-11-08 09:25:29 +00001553 N->op_begin(), N->getNumOperands());
Gabor Greifba36cb52008-08-28 21:40:38 +00001554 AddToWorkList(Lo.getNode());
1555 SDValue LoOpt = combine(Lo.getNode());
1556 if (LoOpt.getNode() && LoOpt.getNode() != Lo.getNode() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001557 (!AfterLegalize ||
1558 TLI.isOperationLegal(LoOpt.getOpcode(), LoOpt.getValueType())))
Chris Lattner5eee4272008-01-26 01:09:19 +00001559 return CombineTo(N, LoOpt, LoOpt);
Dan Gohman389079b2007-10-08 17:57:15 +00001560 }
1561
Evan Cheng44711942007-11-08 09:25:29 +00001562 if (HiExists) {
Dan Gohman475871a2008-07-27 21:46:04 +00001563 SDValue Hi = DAG.getNode(HiOp, N->getValueType(1),
Evan Cheng44711942007-11-08 09:25:29 +00001564 N->op_begin(), N->getNumOperands());
Gabor Greifba36cb52008-08-28 21:40:38 +00001565 AddToWorkList(Hi.getNode());
1566 SDValue HiOpt = combine(Hi.getNode());
1567 if (HiOpt.getNode() && HiOpt != Hi &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001568 (!AfterLegalize ||
1569 TLI.isOperationLegal(HiOpt.getOpcode(), HiOpt.getValueType())))
Chris Lattner5eee4272008-01-26 01:09:19 +00001570 return CombineTo(N, HiOpt, HiOpt);
Evan Cheng44711942007-11-08 09:25:29 +00001571 }
Dan Gohman475871a2008-07-27 21:46:04 +00001572 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001573}
1574
Dan Gohman475871a2008-07-27 21:46:04 +00001575SDValue DAGCombiner::visitSMUL_LOHI(SDNode *N) {
1576 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHS);
Gabor Greifba36cb52008-08-28 21:40:38 +00001577 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001578
Dan Gohman475871a2008-07-27 21:46:04 +00001579 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001580}
1581
Dan Gohman475871a2008-07-27 21:46:04 +00001582SDValue DAGCombiner::visitUMUL_LOHI(SDNode *N) {
1583 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHU);
Gabor Greifba36cb52008-08-28 21:40:38 +00001584 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001585
Dan Gohman475871a2008-07-27 21:46:04 +00001586 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001587}
1588
Dan Gohman475871a2008-07-27 21:46:04 +00001589SDValue DAGCombiner::visitSDIVREM(SDNode *N) {
1590 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::SDIV, ISD::SREM);
Gabor Greifba36cb52008-08-28 21:40:38 +00001591 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001592
Dan Gohman475871a2008-07-27 21:46:04 +00001593 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001594}
1595
Dan Gohman475871a2008-07-27 21:46:04 +00001596SDValue DAGCombiner::visitUDIVREM(SDNode *N) {
1597 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::UDIV, ISD::UREM);
Gabor Greifba36cb52008-08-28 21:40:38 +00001598 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001599
Dan Gohman475871a2008-07-27 21:46:04 +00001600 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001601}
1602
Chris Lattner35e5c142006-05-05 05:51:50 +00001603/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
1604/// two operands of the same opcode, try to simplify it.
Dan Gohman475871a2008-07-27 21:46:04 +00001605SDValue DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
1606 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001607 MVT VT = N0.getValueType();
Chris Lattner35e5c142006-05-05 05:51:50 +00001608 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
1609
Chris Lattner540121f2006-05-05 06:31:05 +00001610 // For each of OP in AND/OR/XOR:
1611 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
1612 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
1613 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
Chris Lattner0d8dae72006-05-05 06:32:04 +00001614 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y))
Chris Lattner540121f2006-05-05 06:31:05 +00001615 if ((N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND||
Chris Lattner0d8dae72006-05-05 06:32:04 +00001616 N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::TRUNCATE) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001617 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001618 SDValue ORNode = DAG.getNode(N->getOpcode(),
Chris Lattner35e5c142006-05-05 05:51:50 +00001619 N0.getOperand(0).getValueType(),
1620 N0.getOperand(0), N1.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00001621 AddToWorkList(ORNode.getNode());
Chris Lattner540121f2006-05-05 06:31:05 +00001622 return DAG.getNode(N0.getOpcode(), VT, ORNode);
Chris Lattner35e5c142006-05-05 05:51:50 +00001623 }
1624
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001625 // For each of OP in SHL/SRL/SRA/AND...
1626 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
1627 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
1628 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
Chris Lattner35e5c142006-05-05 05:51:50 +00001629 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001630 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001631 N0.getOperand(1) == N1.getOperand(1)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001632 SDValue ORNode = DAG.getNode(N->getOpcode(),
Chris Lattner35e5c142006-05-05 05:51:50 +00001633 N0.getOperand(0).getValueType(),
1634 N0.getOperand(0), N1.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00001635 AddToWorkList(ORNode.getNode());
Chris Lattner35e5c142006-05-05 05:51:50 +00001636 return DAG.getNode(N0.getOpcode(), VT, ORNode, N0.getOperand(1));
1637 }
1638
Dan Gohman475871a2008-07-27 21:46:04 +00001639 return SDValue();
Chris Lattner35e5c142006-05-05 05:51:50 +00001640}
1641
Dan Gohman475871a2008-07-27 21:46:04 +00001642SDValue DAGCombiner::visitAND(SDNode *N) {
1643 SDValue N0 = N->getOperand(0);
1644 SDValue N1 = N->getOperand(1);
1645 SDValue LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001646 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1647 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001648 MVT VT = N1.getValueType();
1649 unsigned BitWidth = VT.getSizeInBits();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001650
Dan Gohman7f321562007-06-25 16:23:39 +00001651 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001652 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001653 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001654 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001655 }
Dan Gohman7f321562007-06-25 16:23:39 +00001656
Dan Gohman613e0d82007-07-03 14:03:57 +00001657 // fold (and x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001658 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001659 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001660 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001661 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001662 return DAG.FoldConstantArithmetic(ISD::AND, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00001663 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001664 if (N0C && !N1C)
1665 return DAG.getNode(ISD::AND, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001666 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001667 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001668 return N0;
1669 // if (and x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00001670 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001671 APInt::getAllOnesValue(BitWidth)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001672 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +00001673 // reassociate and
Dan Gohman475871a2008-07-27 21:46:04 +00001674 SDValue RAND = ReassociateOps(ISD::AND, N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001675 if (RAND.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001676 return RAND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001677 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
Nate Begeman5dc7e862005-11-02 18:42:59 +00001678 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001679 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohman002e5d02008-03-13 22:13:53 +00001680 if ((ORI->getAPIntValue() & N1C->getAPIntValue()) == N1C->getAPIntValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001681 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +00001682 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
1683 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Dan Gohman475871a2008-07-27 21:46:04 +00001684 SDValue N0Op0 = N0.getOperand(0);
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001685 APInt Mask = ~N1C->getAPIntValue();
1686 Mask.trunc(N0Op0.getValueSizeInBits());
1687 if (DAG.MaskedValueIsZero(N0Op0, Mask)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001688 SDValue Zext = DAG.getNode(ISD::ZERO_EXTEND, N0.getValueType(),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001689 N0Op0);
Chris Lattner1ec05d12006-03-01 21:47:21 +00001690
1691 // Replace uses of the AND with uses of the Zero extend node.
1692 CombineTo(N, Zext);
1693
Chris Lattner3603cd62006-02-02 07:17:31 +00001694 // We actually want to replace all uses of the any_extend with the
1695 // zero_extend, to avoid duplicating things. This will later cause this
1696 // AND to be folded.
Gabor Greifba36cb52008-08-28 21:40:38 +00001697 CombineTo(N0.getNode(), Zext);
Dan Gohman475871a2008-07-27 21:46:04 +00001698 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner3603cd62006-02-02 07:17:31 +00001699 }
1700 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001701 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
1702 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1703 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1704 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1705
1706 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00001707 LL.getValueType().isInteger()) {
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001708 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
Dan Gohman002e5d02008-03-13 22:13:53 +00001709 if (cast<ConstantSDNode>(LR)->isNullValue() && Op1 == ISD::SETEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00001710 SDValue ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001711 AddToWorkList(ORNode.getNode());
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001712 return DAG.getSetCC(VT, ORNode, LR, Op1);
1713 }
1714 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
1715 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00001716 SDValue ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001717 AddToWorkList(ANDNode.getNode());
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001718 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1719 }
1720 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
1721 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
Dan Gohman475871a2008-07-27 21:46:04 +00001722 SDValue ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001723 AddToWorkList(ORNode.getNode());
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001724 return DAG.getSetCC(VT, ORNode, LR, Op1);
1725 }
1726 }
1727 // canonicalize equivalent to ll == rl
1728 if (LL == RR && LR == RL) {
1729 Op1 = ISD::getSetCCSwappedOperands(Op1);
1730 std::swap(RL, RR);
1731 }
1732 if (LL == RL && LR == RR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001733 bool isInteger = LL.getValueType().isInteger();
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001734 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
Chris Lattner6e1c6232008-10-28 07:11:07 +00001735 if (Result != ISD::SETCC_INVALID &&
1736 (!AfterLegalize || TLI.isCondCodeLegal(Result, LL.getValueType())))
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001737 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1738 }
1739 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001740
1741 // Simplify: and (op x...), (op y...) -> (op (and x, y))
1742 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001743 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001744 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001745 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001746
Nate Begemande996292006-02-03 22:24:05 +00001747 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
1748 // fold (and (sra)) -> (and (srl)) when possible.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001749 if (!VT.isVector() &&
Dan Gohman475871a2008-07-27 21:46:04 +00001750 SimplifyDemandedBits(SDValue(N, 0)))
1751 return SDValue(N, 0);
Nate Begemanded49632005-10-13 03:11:28 +00001752 // fold (zext_inreg (extload x)) -> (zextload x)
Gabor Greifba36cb52008-08-28 21:40:38 +00001753 if (ISD::isEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode())) {
Evan Cheng466685d2006-10-09 20:57:25 +00001754 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001755 MVT EVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001756 // If we zero all the possible extended bits, then we can turn this into
1757 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001758 unsigned BitWidth = N1.getValueSizeInBits();
1759 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001760 BitWidth - EVT.getSizeInBits())) &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001761 ((!AfterLegalize && !LN0->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00001762 TLI.isLoadExtLegal(ISD::ZEXTLOAD, EVT))) {
Dan Gohman475871a2008-07-27 21:46:04 +00001763 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
Evan Cheng466685d2006-10-09 20:57:25 +00001764 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00001765 LN0->getSrcValueOffset(), EVT,
1766 LN0->isVolatile(),
1767 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00001768 AddToWorkList(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001769 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00001770 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001771 }
1772 }
1773 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Gabor Greifba36cb52008-08-28 21:40:38 +00001774 if (ISD::isSEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng83060c52007-03-07 08:07:03 +00001775 N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001776 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001777 MVT EVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001778 // If we zero all the possible extended bits, then we can turn this into
1779 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001780 unsigned BitWidth = N1.getValueSizeInBits();
1781 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001782 BitWidth - EVT.getSizeInBits())) &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001783 ((!AfterLegalize && !LN0->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00001784 TLI.isLoadExtLegal(ISD::ZEXTLOAD, EVT))) {
Dan Gohman475871a2008-07-27 21:46:04 +00001785 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
Evan Cheng466685d2006-10-09 20:57:25 +00001786 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00001787 LN0->getSrcValueOffset(), EVT,
1788 LN0->isVolatile(),
1789 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00001790 AddToWorkList(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001791 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00001792 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001793 }
1794 }
Chris Lattner15045b62006-02-28 06:35:35 +00001795
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001796 // fold (and (load x), 255) -> (zextload x, i8)
1797 // fold (and (extload x, i16), 255) -> (zextload x, i8)
Evan Cheng466685d2006-10-09 20:57:25 +00001798 if (N1C && N0.getOpcode() == ISD::LOAD) {
1799 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
1800 if (LN0->getExtensionType() != ISD::SEXTLOAD &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001801 LN0->isUnindexed() && N0.hasOneUse() &&
1802 // Do not change the width of a volatile load.
1803 !LN0->isVolatile()) {
Duncan Sands8eab8a22008-06-09 11:32:28 +00001804 MVT EVT = MVT::Other;
1805 uint32_t ActiveBits = N1C->getAPIntValue().getActiveBits();
1806 if (ActiveBits > 0 && APIntOps::isMask(ActiveBits, N1C->getAPIntValue()))
1807 EVT = MVT::getIntegerVT(ActiveBits);
1808
1809 MVT LoadedVT = LN0->getMemoryVT();
Duncan Sandsad205a72008-06-16 08:14:38 +00001810 // Do not generate loads of non-round integer types since these can
1811 // be expensive (and would be wrong if the type is not byte sized).
1812 if (EVT != MVT::Other && LoadedVT.bitsGT(EVT) && EVT.isRound() &&
Evan Cheng03294662008-10-14 21:26:46 +00001813 (!AfterLegalize || TLI.isLoadExtLegal(ISD::ZEXTLOAD, EVT))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001814 MVT PtrType = N0.getOperand(1).getValueType();
Evan Cheng466685d2006-10-09 20:57:25 +00001815 // For big endian targets, we need to add an offset to the pointer to
1816 // load the correct bytes. For little endian systems, we merely need to
1817 // read fewer bytes from the same pointer.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001818 unsigned LVTStoreBytes = LoadedVT.getStoreSizeInBits()/8;
1819 unsigned EVTStoreBytes = EVT.getStoreSizeInBits()/8;
Duncan Sandsc6fa1702007-11-09 08:57:19 +00001820 unsigned PtrOff = LVTStoreBytes - EVTStoreBytes;
Duncan Sandsdc846502007-10-28 12:59:45 +00001821 unsigned Alignment = LN0->getAlignment();
Dan Gohman475871a2008-07-27 21:46:04 +00001822 SDValue NewPtr = LN0->getBasePtr();
Duncan Sands0753fc12008-02-11 10:37:04 +00001823 if (TLI.isBigEndian()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001824 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
1825 DAG.getConstant(PtrOff, PtrType));
Duncan Sandsdc846502007-10-28 12:59:45 +00001826 Alignment = MinAlign(Alignment, PtrOff);
1827 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001828 AddToWorkList(NewPtr.getNode());
Dan Gohman475871a2008-07-27 21:46:04 +00001829 SDValue Load =
Evan Cheng466685d2006-10-09 20:57:25 +00001830 DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00001831 LN0->getSrcValue(), LN0->getSrcValueOffset(), EVT,
Duncan Sandsdc846502007-10-28 12:59:45 +00001832 LN0->isVolatile(), Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00001833 AddToWorkList(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001834 CombineTo(N0.getNode(), Load, Load.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00001835 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng466685d2006-10-09 20:57:25 +00001836 }
Chris Lattner15045b62006-02-28 06:35:35 +00001837 }
1838 }
1839
Dan Gohman475871a2008-07-27 21:46:04 +00001840 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001841}
1842
Dan Gohman475871a2008-07-27 21:46:04 +00001843SDValue DAGCombiner::visitOR(SDNode *N) {
1844 SDValue N0 = N->getOperand(0);
1845 SDValue N1 = N->getOperand(1);
1846 SDValue LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001847 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1848 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001849 MVT VT = N1.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001850
Dan Gohman7f321562007-06-25 16:23:39 +00001851 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001852 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001853 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001854 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001855 }
Dan Gohman7f321562007-06-25 16:23:39 +00001856
Dan Gohman613e0d82007-07-03 14:03:57 +00001857 // fold (or x, undef) -> -1
Dan Gohmand595b5f2007-07-10 14:20:37 +00001858 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Chris Lattnere094f542007-07-09 16:16:34 +00001859 return DAG.getConstant(~0ULL, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001860 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001861 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001862 return DAG.FoldConstantArithmetic(ISD::OR, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00001863 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001864 if (N0C && !N1C)
1865 return DAG.getNode(ISD::OR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001866 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001867 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001868 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001869 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001870 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001871 return N1;
1872 // fold (or x, c) -> c iff (x & ~c) == 0
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001873 if (N1C && DAG.MaskedValueIsZero(N0, ~N1C->getAPIntValue()))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001874 return N1;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001875 // reassociate or
Dan Gohman475871a2008-07-27 21:46:04 +00001876 SDValue ROR = ReassociateOps(ISD::OR, N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001877 if (ROR.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001878 return ROR;
1879 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
Gabor Greifba36cb52008-08-28 21:40:38 +00001880 if (N1C && N0.getOpcode() == ISD::AND && N0.getNode()->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00001881 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00001882 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
1883 return DAG.getNode(ISD::AND, VT, DAG.getNode(ISD::OR, VT, N0.getOperand(0),
1884 N1),
Dan Gohman002e5d02008-03-13 22:13:53 +00001885 DAG.getConstant(N1C->getAPIntValue() |
1886 C1->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00001887 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001888 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
1889 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1890 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1891 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1892
1893 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00001894 LL.getValueType().isInteger()) {
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001895 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
1896 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
Dan Gohman002e5d02008-03-13 22:13:53 +00001897 if (cast<ConstantSDNode>(LR)->isNullValue() &&
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001898 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001899 SDValue ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001900 AddToWorkList(ORNode.getNode());
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001901 return DAG.getSetCC(VT, ORNode, LR, Op1);
1902 }
1903 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
1904 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
1905 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
1906 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001907 SDValue ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001908 AddToWorkList(ANDNode.getNode());
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001909 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1910 }
1911 }
1912 // canonicalize equivalent to ll == rl
1913 if (LL == RR && LR == RL) {
1914 Op1 = ISD::getSetCCSwappedOperands(Op1);
1915 std::swap(RL, RR);
1916 }
1917 if (LL == RL && LR == RR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001918 bool isInteger = LL.getValueType().isInteger();
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001919 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
Chris Lattner6e1c6232008-10-28 07:11:07 +00001920 if (Result != ISD::SETCC_INVALID &&
1921 (!AfterLegalize || TLI.isCondCodeLegal(Result, LL.getValueType())))
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001922 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1923 }
1924 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001925
1926 // Simplify: or (op x...), (op y...) -> (op (or x, y))
1927 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001928 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001929 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001930 }
Chris Lattner516b9622006-09-14 20:50:57 +00001931
Chris Lattner1ec72732006-09-14 21:11:37 +00001932 // (X & C1) | (Y & C2) -> (X|Y) & C3 if possible.
1933 if (N0.getOpcode() == ISD::AND &&
1934 N1.getOpcode() == ISD::AND &&
1935 N0.getOperand(1).getOpcode() == ISD::Constant &&
1936 N1.getOperand(1).getOpcode() == ISD::Constant &&
1937 // Don't increase # computations.
Gabor Greifba36cb52008-08-28 21:40:38 +00001938 (N0.getNode()->hasOneUse() || N1.getNode()->hasOneUse())) {
Chris Lattner1ec72732006-09-14 21:11:37 +00001939 // We can only do this xform if we know that bits from X that are set in C2
1940 // but not in C1 are already zero. Likewise for Y.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001941 const APInt &LHSMask =
1942 cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
1943 const APInt &RHSMask =
1944 cast<ConstantSDNode>(N1.getOperand(1))->getAPIntValue();
Chris Lattner1ec72732006-09-14 21:11:37 +00001945
Dan Gohmanea859be2007-06-22 14:59:07 +00001946 if (DAG.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) &&
1947 DAG.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001948 SDValue X =DAG.getNode(ISD::OR, VT, N0.getOperand(0), N1.getOperand(0));
Chris Lattner1ec72732006-09-14 21:11:37 +00001949 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(LHSMask|RHSMask, VT));
1950 }
1951 }
1952
1953
Chris Lattner516b9622006-09-14 20:50:57 +00001954 // See if this is some rotate idiom.
1955 if (SDNode *Rot = MatchRotate(N0, N1))
Dan Gohman475871a2008-07-27 21:46:04 +00001956 return SDValue(Rot, 0);
Chris Lattner35e5c142006-05-05 05:51:50 +00001957
Dan Gohman475871a2008-07-27 21:46:04 +00001958 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001959}
1960
Chris Lattner516b9622006-09-14 20:50:57 +00001961
1962/// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present.
Dan Gohman475871a2008-07-27 21:46:04 +00001963static bool MatchRotateHalf(SDValue Op, SDValue &Shift, SDValue &Mask) {
Chris Lattner516b9622006-09-14 20:50:57 +00001964 if (Op.getOpcode() == ISD::AND) {
Reid Spencer3ed469c2006-11-02 20:25:50 +00001965 if (isa<ConstantSDNode>(Op.getOperand(1))) {
Chris Lattner516b9622006-09-14 20:50:57 +00001966 Mask = Op.getOperand(1);
1967 Op = Op.getOperand(0);
1968 } else {
1969 return false;
1970 }
1971 }
1972
1973 if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) {
1974 Shift = Op;
1975 return true;
1976 }
1977 return false;
1978}
1979
1980
1981// MatchRotate - Handle an 'or' of two operands. If this is one of the many
1982// idioms for rotate, and if the target supports rotation instructions, generate
1983// a rot[lr].
Dan Gohman475871a2008-07-27 21:46:04 +00001984SDNode *DAGCombiner::MatchRotate(SDValue LHS, SDValue RHS) {
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001985 // Must be a legal type. Expanded 'n promoted things won't work with rotates.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001986 MVT VT = LHS.getValueType();
Chris Lattner516b9622006-09-14 20:50:57 +00001987 if (!TLI.isTypeLegal(VT)) return 0;
1988
1989 // The target must have at least one rotate flavor.
1990 bool HasROTL = TLI.isOperationLegal(ISD::ROTL, VT);
1991 bool HasROTR = TLI.isOperationLegal(ISD::ROTR, VT);
1992 if (!HasROTL && !HasROTR) return 0;
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001993
Chris Lattner516b9622006-09-14 20:50:57 +00001994 // Match "(X shl/srl V1) & V2" where V2 may not be present.
Dan Gohman475871a2008-07-27 21:46:04 +00001995 SDValue LHSShift; // The shift.
1996 SDValue LHSMask; // AND value if any.
Chris Lattner516b9622006-09-14 20:50:57 +00001997 if (!MatchRotateHalf(LHS, LHSShift, LHSMask))
1998 return 0; // Not part of a rotate.
1999
Dan Gohman475871a2008-07-27 21:46:04 +00002000 SDValue RHSShift; // The shift.
2001 SDValue RHSMask; // AND value if any.
Chris Lattner516b9622006-09-14 20:50:57 +00002002 if (!MatchRotateHalf(RHS, RHSShift, RHSMask))
2003 return 0; // Not part of a rotate.
2004
2005 if (LHSShift.getOperand(0) != RHSShift.getOperand(0))
2006 return 0; // Not shifting the same value.
2007
2008 if (LHSShift.getOpcode() == RHSShift.getOpcode())
2009 return 0; // Shifts must disagree.
2010
2011 // Canonicalize shl to left side in a shl/srl pair.
2012 if (RHSShift.getOpcode() == ISD::SHL) {
2013 std::swap(LHS, RHS);
2014 std::swap(LHSShift, RHSShift);
2015 std::swap(LHSMask , RHSMask );
2016 }
2017
Duncan Sands83ec4b62008-06-06 12:08:01 +00002018 unsigned OpSizeInBits = VT.getSizeInBits();
Dan Gohman475871a2008-07-27 21:46:04 +00002019 SDValue LHSShiftArg = LHSShift.getOperand(0);
2020 SDValue LHSShiftAmt = LHSShift.getOperand(1);
2021 SDValue RHSShiftAmt = RHSShift.getOperand(1);
Chris Lattner516b9622006-09-14 20:50:57 +00002022
2023 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
2024 // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2)
Scott Michelc9dc1142007-04-02 21:36:32 +00002025 if (LHSShiftAmt.getOpcode() == ISD::Constant &&
2026 RHSShiftAmt.getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002027 uint64_t LShVal = cast<ConstantSDNode>(LHSShiftAmt)->getZExtValue();
2028 uint64_t RShVal = cast<ConstantSDNode>(RHSShiftAmt)->getZExtValue();
Chris Lattner516b9622006-09-14 20:50:57 +00002029 if ((LShVal + RShVal) != OpSizeInBits)
2030 return 0;
2031
Dan Gohman475871a2008-07-27 21:46:04 +00002032 SDValue Rot;
Chris Lattner516b9622006-09-14 20:50:57 +00002033 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00002034 Rot = DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00002035 else
Scott Michelc9dc1142007-04-02 21:36:32 +00002036 Rot = DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00002037
2038 // If there is an AND of either shifted operand, apply it to the result.
Gabor Greifba36cb52008-08-28 21:40:38 +00002039 if (LHSMask.getNode() || RHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00002040 APInt Mask = APInt::getAllOnesValue(OpSizeInBits);
Chris Lattner516b9622006-09-14 20:50:57 +00002041
Gabor Greifba36cb52008-08-28 21:40:38 +00002042 if (LHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00002043 APInt RHSBits = APInt::getLowBitsSet(OpSizeInBits, LShVal);
2044 Mask &= cast<ConstantSDNode>(LHSMask)->getAPIntValue() | RHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00002045 }
Gabor Greifba36cb52008-08-28 21:40:38 +00002046 if (RHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00002047 APInt LHSBits = APInt::getHighBitsSet(OpSizeInBits, RShVal);
2048 Mask &= cast<ConstantSDNode>(RHSMask)->getAPIntValue() | LHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00002049 }
2050
2051 Rot = DAG.getNode(ISD::AND, VT, Rot, DAG.getConstant(Mask, VT));
2052 }
2053
Gabor Greifba36cb52008-08-28 21:40:38 +00002054 return Rot.getNode();
Chris Lattner516b9622006-09-14 20:50:57 +00002055 }
2056
2057 // If there is a mask here, and we have a variable shift, we can't be sure
2058 // that we're masking out the right stuff.
Gabor Greifba36cb52008-08-28 21:40:38 +00002059 if (LHSMask.getNode() || RHSMask.getNode())
Chris Lattner516b9622006-09-14 20:50:57 +00002060 return 0;
2061
2062 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
2063 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotr x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00002064 if (RHSShiftAmt.getOpcode() == ISD::SUB &&
2065 LHSShiftAmt == RHSShiftAmt.getOperand(1)) {
Chris Lattner516b9622006-09-14 20:50:57 +00002066 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00002067 dyn_cast<ConstantSDNode>(RHSShiftAmt.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002068 if (SUBC->getAPIntValue() == OpSizeInBits) {
Chris Lattner516b9622006-09-14 20:50:57 +00002069 if (HasROTL)
Gabor Greifba36cb52008-08-28 21:40:38 +00002070 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).getNode();
Chris Lattner516b9622006-09-14 20:50:57 +00002071 else
Gabor Greifba36cb52008-08-28 21:40:38 +00002072 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).getNode();
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002073 }
Chris Lattner516b9622006-09-14 20:50:57 +00002074 }
2075 }
2076
2077 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
2078 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotl x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00002079 if (LHSShiftAmt.getOpcode() == ISD::SUB &&
2080 RHSShiftAmt == LHSShiftAmt.getOperand(1)) {
Chris Lattner516b9622006-09-14 20:50:57 +00002081 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00002082 dyn_cast<ConstantSDNode>(LHSShiftAmt.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002083 if (SUBC->getAPIntValue() == OpSizeInBits) {
Bill Wendling2692d592008-08-31 01:13:31 +00002084 if (HasROTR)
Gabor Greifba36cb52008-08-28 21:40:38 +00002085 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).getNode();
Bill Wendling2692d592008-08-31 01:13:31 +00002086 else
2087 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).getNode();
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002088 }
Scott Michelc9dc1142007-04-02 21:36:32 +00002089 }
2090 }
2091
Dan Gohman74feef22008-10-17 01:23:35 +00002092 // Look for sign/zext/any-extended or truncate cases:
Scott Michelc9dc1142007-04-02 21:36:32 +00002093 if ((LHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
2094 || LHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
Dan Gohman74feef22008-10-17 01:23:35 +00002095 || LHSShiftAmt.getOpcode() == ISD::ANY_EXTEND
2096 || LHSShiftAmt.getOpcode() == ISD::TRUNCATE) &&
Scott Michelc9dc1142007-04-02 21:36:32 +00002097 (RHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
2098 || RHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
Dan Gohman74feef22008-10-17 01:23:35 +00002099 || RHSShiftAmt.getOpcode() == ISD::ANY_EXTEND
2100 || RHSShiftAmt.getOpcode() == ISD::TRUNCATE)) {
Dan Gohman475871a2008-07-27 21:46:04 +00002101 SDValue LExtOp0 = LHSShiftAmt.getOperand(0);
2102 SDValue RExtOp0 = RHSShiftAmt.getOperand(0);
Scott Michelc9dc1142007-04-02 21:36:32 +00002103 if (RExtOp0.getOpcode() == ISD::SUB &&
2104 RExtOp0.getOperand(1) == LExtOp0) {
2105 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00002106 // (rotl x, y)
Scott Michelc9dc1142007-04-02 21:36:32 +00002107 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00002108 // (rotr x, (sub 32, y))
Dan Gohman74feef22008-10-17 01:23:35 +00002109 if (ConstantSDNode *SUBC =
2110 dyn_cast<ConstantSDNode>(RExtOp0.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002111 if (SUBC->getAPIntValue() == OpSizeInBits) {
Gabor Greif12632d22008-08-30 19:29:20 +00002112 return DAG.getNode(HasROTL ? ISD::ROTL : ISD::ROTR, VT, LHSShiftArg,
2113 HasROTL ? LHSShiftAmt : RHSShiftAmt).getNode();
Scott Michelc9dc1142007-04-02 21:36:32 +00002114 }
2115 }
2116 } else if (LExtOp0.getOpcode() == ISD::SUB &&
2117 RExtOp0 == LExtOp0.getOperand(1)) {
Bill Wendling353dea22008-08-31 01:04:56 +00002118 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext y))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00002119 // (rotr x, y)
Bill Wendling353dea22008-08-31 01:04:56 +00002120 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext y))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00002121 // (rotl x, (sub 32, y))
Dan Gohman74feef22008-10-17 01:23:35 +00002122 if (ConstantSDNode *SUBC =
2123 dyn_cast<ConstantSDNode>(LExtOp0.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002124 if (SUBC->getAPIntValue() == OpSizeInBits) {
Bill Wendling353dea22008-08-31 01:04:56 +00002125 return DAG.getNode(HasROTR ? ISD::ROTR : ISD::ROTL, VT, LHSShiftArg,
2126 HasROTR ? RHSShiftAmt : LHSShiftAmt).getNode();
Scott Michelc9dc1142007-04-02 21:36:32 +00002127 }
2128 }
Chris Lattner516b9622006-09-14 20:50:57 +00002129 }
2130 }
2131
2132 return 0;
2133}
2134
2135
Dan Gohman475871a2008-07-27 21:46:04 +00002136SDValue DAGCombiner::visitXOR(SDNode *N) {
2137 SDValue N0 = N->getOperand(0);
2138 SDValue N1 = N->getOperand(1);
2139 SDValue LHS, RHS, CC;
Nate Begeman646d7e22005-09-02 21:18:40 +00002140 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2141 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002142 MVT VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002143
Dan Gohman7f321562007-06-25 16:23:39 +00002144 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00002145 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002146 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002147 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00002148 }
Dan Gohman7f321562007-06-25 16:23:39 +00002149
Evan Cheng26471c42008-03-25 20:08:07 +00002150 // fold (xor undef, undef) -> 0. This is a common idiom (misuse).
2151 if (N0.getOpcode() == ISD::UNDEF && N1.getOpcode() == ISD::UNDEF)
2152 return DAG.getConstant(0, VT);
Dan Gohman613e0d82007-07-03 14:03:57 +00002153 // fold (xor x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00002154 if (N0.getOpcode() == ISD::UNDEF)
2155 return N0;
2156 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00002157 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002158 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002159 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002160 return DAG.FoldConstantArithmetic(ISD::XOR, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00002161 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00002162 if (N0C && !N1C)
2163 return DAG.getNode(ISD::XOR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002164 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002165 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002166 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00002167 // reassociate xor
Dan Gohman475871a2008-07-27 21:46:04 +00002168 SDValue RXOR = ReassociateOps(ISD::XOR, N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00002169 if (RXOR.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00002170 return RXOR;
Bill Wendlingae89bb12008-11-11 08:25:46 +00002171
Nate Begeman1d4d4142005-09-01 00:19:25 +00002172 // fold !(x cc y) -> (x !cc y)
Dan Gohman002e5d02008-03-13 22:13:53 +00002173 if (N1C && N1C->getAPIntValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002174 bool isInt = LHS.getValueType().isInteger();
Nate Begeman646d7e22005-09-02 21:18:40 +00002175 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
2176 isInt);
Bill Wendlingae89bb12008-11-11 08:25:46 +00002177
2178 if (!AfterLegalize || TLI.isCondCodeLegal(NotCC, LHS.getValueType())) {
2179 switch (N0.getOpcode()) {
2180 default:
2181 assert(0 && "Unhandled SetCC Equivalent!");
2182 abort();
2183 case ISD::SETCC:
2184 return DAG.getSetCC(VT, LHS, RHS, NotCC);
2185 case ISD::SELECT_CC:
2186 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),
2187 N0.getOperand(3), NotCC);
2188 }
2189 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00002190 }
Bill Wendlingae89bb12008-11-11 08:25:46 +00002191
Chris Lattner61c5ff42007-09-10 21:39:07 +00002192 // fold (not (zext (setcc x, y))) -> (zext (not (setcc x, y)))
Dan Gohman002e5d02008-03-13 22:13:53 +00002193 if (N1C && N1C->getAPIntValue() == 1 && N0.getOpcode() == ISD::ZERO_EXTEND &&
Gabor Greif12632d22008-08-30 19:29:20 +00002194 N0.getNode()->hasOneUse() &&
2195 isSetCCEquivalent(N0.getOperand(0), LHS, RHS, CC)){
Dan Gohman475871a2008-07-27 21:46:04 +00002196 SDValue V = N0.getOperand(0);
Chris Lattner61c5ff42007-09-10 21:39:07 +00002197 V = DAG.getNode(ISD::XOR, V.getValueType(), V,
Duncan Sands272dce02007-10-10 09:54:50 +00002198 DAG.getConstant(1, V.getValueType()));
Gabor Greifba36cb52008-08-28 21:40:38 +00002199 AddToWorkList(V.getNode());
Chris Lattner61c5ff42007-09-10 21:39:07 +00002200 return DAG.getNode(ISD::ZERO_EXTEND, VT, V);
2201 }
2202
Nate Begeman99801192005-09-07 23:25:52 +00002203 // fold !(x or y) -> (!x and !y) iff x or y are setcc
Dan Gohman002e5d02008-03-13 22:13:53 +00002204 if (N1C && N1C->getAPIntValue() == 1 && VT == MVT::i1 &&
Nate Begeman99801192005-09-07 23:25:52 +00002205 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Dan Gohman475871a2008-07-27 21:46:04 +00002206 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00002207 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
2208 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002209 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
2210 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Gabor Greifba36cb52008-08-28 21:40:38 +00002211 AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode());
Nate Begeman99801192005-09-07 23:25:52 +00002212 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002213 }
2214 }
Nate Begeman99801192005-09-07 23:25:52 +00002215 // fold !(x or y) -> (!x and !y) iff x or y are constants
2216 if (N1C && N1C->isAllOnesValue() &&
2217 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Dan Gohman475871a2008-07-27 21:46:04 +00002218 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00002219 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
2220 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002221 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
2222 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Gabor Greifba36cb52008-08-28 21:40:38 +00002223 AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode());
Nate Begeman99801192005-09-07 23:25:52 +00002224 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002225 }
2226 }
Nate Begeman223df222005-09-08 20:18:10 +00002227 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
2228 if (N1C && N0.getOpcode() == ISD::XOR) {
2229 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
2230 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2231 if (N00C)
2232 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
Dan Gohman002e5d02008-03-13 22:13:53 +00002233 DAG.getConstant(N1C->getAPIntValue()^
2234 N00C->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00002235 if (N01C)
2236 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
Dan Gohman002e5d02008-03-13 22:13:53 +00002237 DAG.getConstant(N1C->getAPIntValue()^
2238 N01C->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00002239 }
2240 // fold (xor x, x) -> 0
Chris Lattner4fbdd592006-03-28 19:11:05 +00002241 if (N0 == N1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002242 if (!VT.isVector()) {
Chris Lattner4fbdd592006-03-28 19:11:05 +00002243 return DAG.getConstant(0, VT);
2244 } else if (!AfterLegalize || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) {
2245 // Produce a vector of zeros.
Dan Gohman475871a2008-07-27 21:46:04 +00002246 SDValue El = DAG.getConstant(0, VT.getVectorElementType());
2247 std::vector<SDValue> Ops(VT.getVectorNumElements(), El);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002248 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4fbdd592006-03-28 19:11:05 +00002249 }
2250 }
Chris Lattner35e5c142006-05-05 05:51:50 +00002251
2252 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
2253 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002254 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002255 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002256 }
Chris Lattner35e5c142006-05-05 05:51:50 +00002257
Chris Lattner3e104b12006-04-08 04:15:24 +00002258 // Simplify the expression using non-local knowledge.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002259 if (!VT.isVector() &&
Dan Gohman475871a2008-07-27 21:46:04 +00002260 SimplifyDemandedBits(SDValue(N, 0)))
2261 return SDValue(N, 0);
Chris Lattner3e104b12006-04-08 04:15:24 +00002262
Dan Gohman475871a2008-07-27 21:46:04 +00002263 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002264}
2265
Chris Lattnere70da202007-12-06 07:33:36 +00002266/// visitShiftByConstant - Handle transforms common to the three shifts, when
2267/// the shift amount is a constant.
Dan Gohman475871a2008-07-27 21:46:04 +00002268SDValue DAGCombiner::visitShiftByConstant(SDNode *N, unsigned Amt) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002269 SDNode *LHS = N->getOperand(0).getNode();
Dan Gohman475871a2008-07-27 21:46:04 +00002270 if (!LHS->hasOneUse()) return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00002271
2272 // We want to pull some binops through shifts, so that we have (and (shift))
2273 // instead of (shift (and)), likewise for add, or, xor, etc. This sort of
2274 // thing happens with address calculations, so it's important to canonicalize
2275 // it.
2276 bool HighBitSet = false; // Can we transform this if the high bit is set?
2277
2278 switch (LHS->getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002279 default: return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00002280 case ISD::OR:
2281 case ISD::XOR:
2282 HighBitSet = false; // We can only transform sra if the high bit is clear.
2283 break;
2284 case ISD::AND:
2285 HighBitSet = true; // We can only transform sra if the high bit is set.
2286 break;
2287 case ISD::ADD:
2288 if (N->getOpcode() != ISD::SHL)
Dan Gohman475871a2008-07-27 21:46:04 +00002289 return SDValue(); // only shl(add) not sr[al](add).
Chris Lattnere70da202007-12-06 07:33:36 +00002290 HighBitSet = false; // We can only transform sra if the high bit is clear.
2291 break;
2292 }
2293
2294 // We require the RHS of the binop to be a constant as well.
2295 ConstantSDNode *BinOpCst = dyn_cast<ConstantSDNode>(LHS->getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00002296 if (!BinOpCst) return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00002297
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00002298
2299 // FIXME: disable this for unless the input to the binop is a shift by a
2300 // constant. If it is not a shift, it pessimizes some common cases like:
2301 //
2302 //void foo(int *X, int i) { X[i & 1235] = 1; }
2303 //int bar(int *X, int i) { return X[i & 255]; }
Gabor Greifba36cb52008-08-28 21:40:38 +00002304 SDNode *BinOpLHSVal = LHS->getOperand(0).getNode();
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00002305 if ((BinOpLHSVal->getOpcode() != ISD::SHL &&
2306 BinOpLHSVal->getOpcode() != ISD::SRA &&
2307 BinOpLHSVal->getOpcode() != ISD::SRL) ||
2308 !isa<ConstantSDNode>(BinOpLHSVal->getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00002309 return SDValue();
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00002310
Duncan Sands83ec4b62008-06-06 12:08:01 +00002311 MVT VT = N->getValueType(0);
Chris Lattnere70da202007-12-06 07:33:36 +00002312
2313 // If this is a signed shift right, and the high bit is modified
2314 // by the logical operation, do not perform the transformation.
2315 // The highBitSet boolean indicates the value of the high bit of
2316 // the constant which would cause it to be modified for this
2317 // operation.
2318 if (N->getOpcode() == ISD::SRA) {
Dan Gohman220a8232008-03-03 23:51:38 +00002319 bool BinOpRHSSignSet = BinOpCst->getAPIntValue().isNegative();
2320 if (BinOpRHSSignSet != HighBitSet)
Dan Gohman475871a2008-07-27 21:46:04 +00002321 return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00002322 }
2323
2324 // Fold the constants, shifting the binop RHS by the shift amount.
Dan Gohman475871a2008-07-27 21:46:04 +00002325 SDValue NewRHS = DAG.getNode(N->getOpcode(), N->getValueType(0),
Chris Lattnere70da202007-12-06 07:33:36 +00002326 LHS->getOperand(1), N->getOperand(1));
2327
2328 // Create the new shift.
Dan Gohman475871a2008-07-27 21:46:04 +00002329 SDValue NewShift = DAG.getNode(N->getOpcode(), VT, LHS->getOperand(0),
Chris Lattnere70da202007-12-06 07:33:36 +00002330 N->getOperand(1));
2331
2332 // Create the new binop.
2333 return DAG.getNode(LHS->getOpcode(), VT, NewShift, NewRHS);
2334}
2335
2336
Dan Gohman475871a2008-07-27 21:46:04 +00002337SDValue DAGCombiner::visitSHL(SDNode *N) {
2338 SDValue N0 = N->getOperand(0);
2339 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002340 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2341 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002342 MVT VT = N0.getValueType();
2343 unsigned OpSizeInBits = VT.getSizeInBits();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002344
2345 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002346 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002347 return DAG.FoldConstantArithmetic(ISD::SHL, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002348 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002349 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002350 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002351 // fold (shl x, c >= size(x)) -> undef
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002352 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002353 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002354 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002355 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002356 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002357 // if (shl x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00002358 if (DAG.MaskedValueIsZero(SDValue(N, 0),
Duncan Sands83ec4b62008-06-06 12:08:01 +00002359 APInt::getAllOnesValue(VT.getSizeInBits())))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002360 return DAG.getConstant(0, VT);
Evan Chengeb9f8922008-08-30 02:03:58 +00002361 // fold (shl x, (trunc (and y, c))) -> (shl x, (and (trunc y), c))
2362 // iff (trunc c) == c
2363 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00002364 N1.getOperand(0).getOpcode() == ISD::AND &&
2365 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00002366 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00002367 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Evan Chengeb9f8922008-08-30 02:03:58 +00002368 MVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00002369 SDValue N100 = N1.getOperand(0).getOperand(0);
2370 return DAG.getNode(ISD::SHL, VT, N0,
2371 DAG.getNode(ISD::AND, TruncVT,
2372 DAG.getNode(ISD::TRUNCATE, TruncVT, N100),
2373 DAG.getConstant(N101C->getZExtValue(),
2374 TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00002375 }
2376 }
2377
Dan Gohman475871a2008-07-27 21:46:04 +00002378 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
2379 return SDValue(N, 0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002380 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002381 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002382 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002383 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
2384 uint64_t c2 = N1C->getZExtValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002385 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002386 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002387 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002388 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002389 }
2390 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
2391 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002392 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002393 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002394 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
2395 uint64_t c2 = N1C->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00002396 SDValue Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman1d4d4142005-09-01 00:19:25 +00002397 DAG.getConstant(~0ULL << c1, VT));
2398 if (c2 > c1)
2399 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +00002400 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002401 else
Nate Begeman83e75ec2005-09-06 04:43:02 +00002402 return DAG.getNode(ISD::SRL, VT, Mask,
2403 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002404 }
2405 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00002406 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +00002407 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002408 DAG.getConstant(~0ULL << N1C->getZExtValue(), VT));
Chris Lattnere70da202007-12-06 07:33:36 +00002409
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002410 return N1C ? visitShiftByConstant(N, N1C->getZExtValue()) : SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002411}
2412
Dan Gohman475871a2008-07-27 21:46:04 +00002413SDValue DAGCombiner::visitSRA(SDNode *N) {
2414 SDValue N0 = N->getOperand(0);
2415 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002416 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2417 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002418 MVT VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002419
2420 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002421 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002422 return DAG.FoldConstantArithmetic(ISD::SRA, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002423 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002424 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002425 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002426 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00002427 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002428 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002429 // fold (sra x, c >= size(x)) -> undef
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002430 if (N1C && N1C->getZExtValue() >= VT.getSizeInBits())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002431 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002432 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002433 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002434 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00002435 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
2436 // sext_inreg.
2437 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002438 unsigned LowBits = VT.getSizeInBits() - (unsigned)N1C->getZExtValue();
Duncan Sands8eab8a22008-06-09 11:32:28 +00002439 MVT EVT = MVT::getIntegerVT(LowBits);
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002440 if (EVT.isSimple() && // TODO: remove when apint codegen support lands.
2441 (!AfterLegalize || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, EVT)))
Nate Begemanfb7217b2006-02-17 19:54:08 +00002442 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
2443 DAG.getValueType(EVT));
2444 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002445
Chris Lattner71d9ebc2006-02-28 06:23:04 +00002446 // fold (sra (sra x, c1), c2) -> (sra x, c1+c2)
2447 if (N1C && N0.getOpcode() == ISD::SRA) {
2448 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002449 unsigned Sum = N1C->getZExtValue() + C1->getZExtValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002450 if (Sum >= VT.getSizeInBits()) Sum = VT.getSizeInBits()-1;
Chris Lattner71d9ebc2006-02-28 06:23:04 +00002451 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0),
2452 DAG.getConstant(Sum, N1C->getValueType(0)));
2453 }
2454 }
Christopher Lamb15cbde32008-03-19 08:30:06 +00002455
2456 // fold sra (shl X, m), result_size - n
2457 // -> (sign_extend (trunc (shl X, result_size - n - m))) for
Christopher Lambb9b04282008-03-20 04:31:39 +00002458 // result_size - n != m.
2459 // If truncate is free for the target sext(shl) is likely to result in better
2460 // code.
Christopher Lamb15cbde32008-03-19 08:30:06 +00002461 if (N0.getOpcode() == ISD::SHL) {
2462 // Get the two constanst of the shifts, CN0 = m, CN = n.
2463 const ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2464 if (N01C && N1C) {
Christopher Lambb9b04282008-03-20 04:31:39 +00002465 // Determine what the truncate's result bitsize and type would be.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002466 unsigned VTValSize = VT.getSizeInBits();
2467 MVT TruncVT =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002468 MVT::getIntegerVT(VTValSize - N1C->getZExtValue());
Christopher Lambb9b04282008-03-20 04:31:39 +00002469 // Determine the residual right-shift amount.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002470 unsigned ShiftAmt = N1C->getZExtValue() - N01C->getZExtValue();
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002471
Christopher Lambb9b04282008-03-20 04:31:39 +00002472 // If the shift is not a no-op (in which case this should be just a sign
2473 // extend already), the truncated to type is legal, sign_extend is legal
Gabor Greif12632d22008-08-30 19:29:20 +00002474 // on that type, and the the truncate to that type is both legal and free,
Christopher Lambb9b04282008-03-20 04:31:39 +00002475 // perform the transform.
2476 if (ShiftAmt &&
Christopher Lambb9b04282008-03-20 04:31:39 +00002477 TLI.isOperationLegal(ISD::SIGN_EXTEND, TruncVT) &&
2478 TLI.isOperationLegal(ISD::TRUNCATE, VT) &&
Evan Cheng260e07e2008-03-20 02:18:41 +00002479 TLI.isTruncateFree(VT, TruncVT)) {
Christopher Lambb9b04282008-03-20 04:31:39 +00002480
Dan Gohman475871a2008-07-27 21:46:04 +00002481 SDValue Amt = DAG.getConstant(ShiftAmt, TLI.getShiftAmountTy());
2482 SDValue Shift = DAG.getNode(ISD::SRL, VT, N0.getOperand(0), Amt);
2483 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, TruncVT, Shift);
Christopher Lambb9b04282008-03-20 04:31:39 +00002484 return DAG.getNode(ISD::SIGN_EXTEND, N->getValueType(0), Trunc);
Christopher Lamb15cbde32008-03-19 08:30:06 +00002485 }
2486 }
2487 }
Chris Lattner71d9ebc2006-02-28 06:23:04 +00002488
Evan Chengeb9f8922008-08-30 02:03:58 +00002489 // fold (sra x, (trunc (and y, c))) -> (sra x, (and (trunc y), c))
2490 // iff (trunc c) == c
2491 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00002492 N1.getOperand(0).getOpcode() == ISD::AND &&
2493 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00002494 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00002495 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Evan Chengeb9f8922008-08-30 02:03:58 +00002496 MVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00002497 SDValue N100 = N1.getOperand(0).getOperand(0);
2498 return DAG.getNode(ISD::SRA, VT, N0,
2499 DAG.getNode(ISD::AND, TruncVT,
2500 DAG.getNode(ISD::TRUNCATE, TruncVT, N100),
2501 DAG.getConstant(N101C->getZExtValue(),
2502 TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00002503 }
2504 }
2505
Chris Lattnera8504462006-05-08 20:51:54 +00002506 // Simplify, based on bits shifted out of the LHS.
Dan Gohman475871a2008-07-27 21:46:04 +00002507 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
2508 return SDValue(N, 0);
Chris Lattnera8504462006-05-08 20:51:54 +00002509
2510
Nate Begeman1d4d4142005-09-01 00:19:25 +00002511 // If the sign bit is known to be zero, switch this to a SRL.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002512 if (DAG.SignBitIsZero(N0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002513 return DAG.getNode(ISD::SRL, VT, N0, N1);
Chris Lattnere70da202007-12-06 07:33:36 +00002514
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002515 return N1C ? visitShiftByConstant(N, N1C->getZExtValue()) : SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002516}
2517
Dan Gohman475871a2008-07-27 21:46:04 +00002518SDValue DAGCombiner::visitSRL(SDNode *N) {
2519 SDValue N0 = N->getOperand(0);
2520 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002521 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2522 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002523 MVT VT = N0.getValueType();
2524 unsigned OpSizeInBits = VT.getSizeInBits();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002525
2526 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002527 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002528 return DAG.FoldConstantArithmetic(ISD::SRL, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002529 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002530 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002531 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002532 // fold (srl x, c >= size(x)) -> undef
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002533 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002534 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002535 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002536 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002537 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002538 // if (srl x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00002539 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002540 APInt::getAllOnesValue(OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002541 return DAG.getConstant(0, VT);
Chris Lattnerec06e9a2007-04-18 03:05:22 +00002542
Nate Begeman1d4d4142005-09-01 00:19:25 +00002543 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002544 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002545 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002546 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
2547 uint64_t c2 = N1C->getZExtValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002548 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002549 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002550 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002551 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002552 }
Chris Lattner350bec02006-04-02 06:11:11 +00002553
Chris Lattner06afe072006-05-05 22:53:17 +00002554 // fold (srl (anyextend x), c) -> (anyextend (srl x, c))
2555 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
2556 // Shifting in all undef bits?
Duncan Sands83ec4b62008-06-06 12:08:01 +00002557 MVT SmallVT = N0.getOperand(0).getValueType();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002558 if (N1C->getZExtValue() >= SmallVT.getSizeInBits())
Chris Lattner06afe072006-05-05 22:53:17 +00002559 return DAG.getNode(ISD::UNDEF, VT);
2560
Dan Gohman475871a2008-07-27 21:46:04 +00002561 SDValue SmallShift = DAG.getNode(ISD::SRL, SmallVT, N0.getOperand(0), N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00002562 AddToWorkList(SmallShift.getNode());
Chris Lattner06afe072006-05-05 22:53:17 +00002563 return DAG.getNode(ISD::ANY_EXTEND, VT, SmallShift);
2564 }
2565
Chris Lattner3657ffe2006-10-12 20:23:19 +00002566 // fold (srl (sra X, Y), 31) -> (srl X, 31). This srl only looks at the sign
2567 // bit, which is unmodified by sra.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002568 if (N1C && N1C->getZExtValue()+1 == VT.getSizeInBits()) {
Chris Lattner3657ffe2006-10-12 20:23:19 +00002569 if (N0.getOpcode() == ISD::SRA)
2570 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0), N1);
2571 }
2572
Chris Lattner350bec02006-04-02 06:11:11 +00002573 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
2574 if (N1C && N0.getOpcode() == ISD::CTLZ &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002575 N1C->getAPIntValue() == Log2_32(VT.getSizeInBits())) {
Dan Gohman948d8ea2008-02-20 16:33:30 +00002576 APInt KnownZero, KnownOne;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002577 APInt Mask = APInt::getAllOnesValue(VT.getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00002578 DAG.ComputeMaskedBits(N0.getOperand(0), Mask, KnownZero, KnownOne);
Chris Lattner350bec02006-04-02 06:11:11 +00002579
2580 // If any of the input bits are KnownOne, then the input couldn't be all
2581 // zeros, thus the result of the srl will always be zero.
Dan Gohman948d8ea2008-02-20 16:33:30 +00002582 if (KnownOne.getBoolValue()) return DAG.getConstant(0, VT);
Chris Lattner350bec02006-04-02 06:11:11 +00002583
2584 // If all of the bits input the to ctlz node are known to be zero, then
2585 // the result of the ctlz is "32" and the result of the shift is one.
Dan Gohman948d8ea2008-02-20 16:33:30 +00002586 APInt UnknownBits = ~KnownZero & Mask;
Chris Lattner350bec02006-04-02 06:11:11 +00002587 if (UnknownBits == 0) return DAG.getConstant(1, VT);
2588
2589 // Otherwise, check to see if there is exactly one bit input to the ctlz.
2590 if ((UnknownBits & (UnknownBits-1)) == 0) {
2591 // Okay, we know that only that the single bit specified by UnknownBits
2592 // could be set on input to the CTLZ node. If this bit is set, the SRL
2593 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
2594 // to an SRL,XOR pair, which is likely to simplify more.
Dan Gohman948d8ea2008-02-20 16:33:30 +00002595 unsigned ShAmt = UnknownBits.countTrailingZeros();
Dan Gohman475871a2008-07-27 21:46:04 +00002596 SDValue Op = N0.getOperand(0);
Chris Lattner350bec02006-04-02 06:11:11 +00002597 if (ShAmt) {
2598 Op = DAG.getNode(ISD::SRL, VT, Op,
2599 DAG.getConstant(ShAmt, TLI.getShiftAmountTy()));
Gabor Greifba36cb52008-08-28 21:40:38 +00002600 AddToWorkList(Op.getNode());
Chris Lattner350bec02006-04-02 06:11:11 +00002601 }
2602 return DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(1, VT));
2603 }
2604 }
Evan Chengeb9f8922008-08-30 02:03:58 +00002605
2606 // fold (srl x, (trunc (and y, c))) -> (srl x, (and (trunc y), c))
2607 // iff (trunc c) == c
2608 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00002609 N1.getOperand(0).getOpcode() == ISD::AND &&
2610 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00002611 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00002612 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Evan Chengeb9f8922008-08-30 02:03:58 +00002613 MVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00002614 SDValue N100 = N1.getOperand(0).getOperand(0);
2615 return DAG.getNode(ISD::SRL, VT, N0,
2616 DAG.getNode(ISD::AND, TruncVT,
2617 DAG.getNode(ISD::TRUNCATE, TruncVT, N100),
2618 DAG.getConstant(N101C->getZExtValue(),
2619 TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00002620 }
2621 }
Chris Lattner61a4c072007-04-18 03:06:49 +00002622
2623 // fold operands of srl based on knowledge that the low bits are not
2624 // demanded.
Dan Gohman475871a2008-07-27 21:46:04 +00002625 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
2626 return SDValue(N, 0);
Chris Lattner61a4c072007-04-18 03:06:49 +00002627
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002628 return N1C ? visitShiftByConstant(N, N1C->getZExtValue()) : SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002629}
2630
Dan Gohman475871a2008-07-27 21:46:04 +00002631SDValue DAGCombiner::visitCTLZ(SDNode *N) {
2632 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002633 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002634
2635 // fold (ctlz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002636 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002637 return DAG.getNode(ISD::CTLZ, VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00002638 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002639}
2640
Dan Gohman475871a2008-07-27 21:46:04 +00002641SDValue DAGCombiner::visitCTTZ(SDNode *N) {
2642 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002643 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002644
2645 // fold (cttz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002646 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002647 return DAG.getNode(ISD::CTTZ, VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00002648 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002649}
2650
Dan Gohman475871a2008-07-27 21:46:04 +00002651SDValue DAGCombiner::visitCTPOP(SDNode *N) {
2652 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002653 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002654
2655 // fold (ctpop c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002656 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002657 return DAG.getNode(ISD::CTPOP, VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00002658 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002659}
2660
Dan Gohman475871a2008-07-27 21:46:04 +00002661SDValue DAGCombiner::visitSELECT(SDNode *N) {
2662 SDValue N0 = N->getOperand(0);
2663 SDValue N1 = N->getOperand(1);
2664 SDValue N2 = N->getOperand(2);
Nate Begeman452d7beb2005-09-16 00:54:12 +00002665 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2666 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2667 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002668 MVT VT = N->getValueType(0);
2669 MVT VT0 = N0.getValueType();
Nate Begeman44728a72005-09-19 22:34:01 +00002670
Nate Begeman452d7beb2005-09-16 00:54:12 +00002671 // fold select C, X, X -> X
2672 if (N1 == N2)
2673 return N1;
2674 // fold select true, X, Y -> X
2675 if (N0C && !N0C->isNullValue())
2676 return N1;
2677 // fold select false, X, Y -> Y
2678 if (N0C && N0C->isNullValue())
2679 return N2;
2680 // fold select C, 1, X -> C | X
Duncan Sands83ec4b62008-06-06 12:08:01 +00002681 if (VT == MVT::i1 && N1C && N1C->getAPIntValue() == 1)
Nate Begeman452d7beb2005-09-16 00:54:12 +00002682 return DAG.getNode(ISD::OR, VT, N0, N2);
Evan Cheng571c4782007-08-18 05:57:05 +00002683 // fold select C, 0, 1 -> ~C
Duncan Sands83ec4b62008-06-06 12:08:01 +00002684 if (VT.isInteger() && VT0.isInteger() &&
Dan Gohman002e5d02008-03-13 22:13:53 +00002685 N1C && N2C && N1C->isNullValue() && N2C->getAPIntValue() == 1) {
Dan Gohman475871a2008-07-27 21:46:04 +00002686 SDValue XORNode = DAG.getNode(ISD::XOR, VT0, N0, DAG.getConstant(1, VT0));
Evan Cheng571c4782007-08-18 05:57:05 +00002687 if (VT == VT0)
2688 return XORNode;
Gabor Greifba36cb52008-08-28 21:40:38 +00002689 AddToWorkList(XORNode.getNode());
Duncan Sands8e4eb092008-06-08 20:54:56 +00002690 if (VT.bitsGT(VT0))
Evan Cheng571c4782007-08-18 05:57:05 +00002691 return DAG.getNode(ISD::ZERO_EXTEND, VT, XORNode);
2692 return DAG.getNode(ISD::TRUNCATE, VT, XORNode);
2693 }
Nate Begeman452d7beb2005-09-16 00:54:12 +00002694 // fold select C, 0, X -> ~C & X
Dale Johannesene0e6fac2007-12-06 17:53:31 +00002695 if (VT == VT0 && VT == MVT::i1 && N1C && N1C->isNullValue()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002696 SDValue XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Gabor Greifba36cb52008-08-28 21:40:38 +00002697 AddToWorkList(XORNode.getNode());
Nate Begeman452d7beb2005-09-16 00:54:12 +00002698 return DAG.getNode(ISD::AND, VT, XORNode, N2);
2699 }
2700 // fold select C, X, 1 -> ~C | X
Dan Gohman002e5d02008-03-13 22:13:53 +00002701 if (VT == VT0 && VT == MVT::i1 && N2C && N2C->getAPIntValue() == 1) {
Dan Gohman475871a2008-07-27 21:46:04 +00002702 SDValue XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Gabor Greifba36cb52008-08-28 21:40:38 +00002703 AddToWorkList(XORNode.getNode());
Nate Begeman452d7beb2005-09-16 00:54:12 +00002704 return DAG.getNode(ISD::OR, VT, XORNode, N1);
2705 }
2706 // fold select C, X, 0 -> C & X
2707 // FIXME: this should check for C type == X type, not i1?
Duncan Sands83ec4b62008-06-06 12:08:01 +00002708 if (VT == MVT::i1 && N2C && N2C->isNullValue())
Nate Begeman452d7beb2005-09-16 00:54:12 +00002709 return DAG.getNode(ISD::AND, VT, N0, N1);
2710 // fold X ? X : Y --> X ? 1 : Y --> X | Y
Duncan Sands83ec4b62008-06-06 12:08:01 +00002711 if (VT == MVT::i1 && N0 == N1)
Nate Begeman452d7beb2005-09-16 00:54:12 +00002712 return DAG.getNode(ISD::OR, VT, N0, N2);
2713 // fold X ? Y : X --> X ? Y : 0 --> X & Y
Duncan Sands83ec4b62008-06-06 12:08:01 +00002714 if (VT == MVT::i1 && N0 == N2)
Nate Begeman452d7beb2005-09-16 00:54:12 +00002715 return DAG.getNode(ISD::AND, VT, N0, N1);
Chris Lattner729c6d12006-05-27 00:43:02 +00002716
Chris Lattner40c62d52005-10-18 06:04:22 +00002717 // If we can fold this based on the true/false value, do so.
2718 if (SimplifySelectOps(N, N1, N2))
Dan Gohman475871a2008-07-27 21:46:04 +00002719 return SDValue(N, 0); // Don't revisit N.
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002720
Nate Begeman44728a72005-09-19 22:34:01 +00002721 // fold selects based on a setcc into other things, such as min/max/abs
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002722 if (N0.getOpcode() == ISD::SETCC) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00002723 // FIXME:
2724 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
2725 // having to say they don't support SELECT_CC on every type the DAG knows
2726 // about, since there is no way to mark an opcode illegal at all value types
2727 if (TLI.isOperationLegal(ISD::SELECT_CC, MVT::Other))
2728 return DAG.getNode(ISD::SELECT_CC, VT, N0.getOperand(0), N0.getOperand(1),
2729 N1, N2, N0.getOperand(2));
2730 else
2731 return SimplifySelect(N0, N1, N2);
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002732 }
Dan Gohman475871a2008-07-27 21:46:04 +00002733 return SDValue();
Nate Begeman452d7beb2005-09-16 00:54:12 +00002734}
2735
Dan Gohman475871a2008-07-27 21:46:04 +00002736SDValue DAGCombiner::visitSELECT_CC(SDNode *N) {
2737 SDValue N0 = N->getOperand(0);
2738 SDValue N1 = N->getOperand(1);
2739 SDValue N2 = N->getOperand(2);
2740 SDValue N3 = N->getOperand(3);
2741 SDValue N4 = N->getOperand(4);
Nate Begeman44728a72005-09-19 22:34:01 +00002742 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
2743
Nate Begeman44728a72005-09-19 22:34:01 +00002744 // fold select_cc lhs, rhs, x, x, cc -> x
2745 if (N2 == N3)
2746 return N2;
Chris Lattner40c62d52005-10-18 06:04:22 +00002747
Chris Lattner5f42a242006-09-20 06:19:26 +00002748 // Determine if the condition we're dealing with is constant
Dan Gohman475871a2008-07-27 21:46:04 +00002749 SDValue SCC = SimplifySetCC(TLI.getSetCCResultType(N0), N0, N1, CC, false);
Gabor Greifba36cb52008-08-28 21:40:38 +00002750 if (SCC.getNode()) AddToWorkList(SCC.getNode());
Chris Lattner5f42a242006-09-20 06:19:26 +00002751
Gabor Greifba36cb52008-08-28 21:40:38 +00002752 if (ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.getNode())) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002753 if (!SCCC->isNullValue())
Chris Lattner5f42a242006-09-20 06:19:26 +00002754 return N2; // cond always true -> true val
2755 else
2756 return N3; // cond always false -> false val
2757 }
2758
2759 // Fold to a simpler select_cc
Gabor Greifba36cb52008-08-28 21:40:38 +00002760 if (SCC.getNode() && SCC.getOpcode() == ISD::SETCC)
Chris Lattner5f42a242006-09-20 06:19:26 +00002761 return DAG.getNode(ISD::SELECT_CC, N2.getValueType(),
2762 SCC.getOperand(0), SCC.getOperand(1), N2, N3,
2763 SCC.getOperand(2));
2764
Chris Lattner40c62d52005-10-18 06:04:22 +00002765 // If we can fold this based on the true/false value, do so.
2766 if (SimplifySelectOps(N, N2, N3))
Dan Gohman475871a2008-07-27 21:46:04 +00002767 return SDValue(N, 0); // Don't revisit N.
Chris Lattner40c62d52005-10-18 06:04:22 +00002768
Nate Begeman44728a72005-09-19 22:34:01 +00002769 // fold select_cc into other things, such as min/max/abs
2770 return SimplifySelectCC(N0, N1, N2, N3, CC);
Nate Begeman452d7beb2005-09-16 00:54:12 +00002771}
2772
Dan Gohman475871a2008-07-27 21:46:04 +00002773SDValue DAGCombiner::visitSETCC(SDNode *N) {
Nate Begeman452d7beb2005-09-16 00:54:12 +00002774 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
2775 cast<CondCodeSDNode>(N->getOperand(2))->get());
2776}
2777
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002778// ExtendUsesToFormExtLoad - Trying to extend uses of a load to enable this:
2779// "fold ({s|z}ext (load x)) -> ({s|z}ext (truncate ({s|z}extload x)))"
2780// transformation. Returns true if extension are possible and the above
2781// mentioned transformation is profitable.
Dan Gohman475871a2008-07-27 21:46:04 +00002782static bool ExtendUsesToFormExtLoad(SDNode *N, SDValue N0,
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002783 unsigned ExtOpc,
2784 SmallVector<SDNode*, 4> &ExtendNodes,
2785 TargetLowering &TLI) {
2786 bool HasCopyToRegUses = false;
2787 bool isTruncFree = TLI.isTruncateFree(N->getValueType(0), N0.getValueType());
Gabor Greif12632d22008-08-30 19:29:20 +00002788 for (SDNode::use_iterator UI = N0.getNode()->use_begin(),
2789 UE = N0.getNode()->use_end();
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002790 UI != UE; ++UI) {
Dan Gohman89684502008-07-27 20:43:25 +00002791 SDNode *User = *UI;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002792 if (User == N)
2793 continue;
2794 // FIXME: Only extend SETCC N, N and SETCC N, c for now.
2795 if (User->getOpcode() == ISD::SETCC) {
2796 ISD::CondCode CC = cast<CondCodeSDNode>(User->getOperand(2))->get();
2797 if (ExtOpc == ISD::ZERO_EXTEND && ISD::isSignedIntSetCC(CC))
2798 // Sign bits will be lost after a zext.
2799 return false;
2800 bool Add = false;
2801 for (unsigned i = 0; i != 2; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002802 SDValue UseOp = User->getOperand(i);
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002803 if (UseOp == N0)
2804 continue;
2805 if (!isa<ConstantSDNode>(UseOp))
2806 return false;
2807 Add = true;
2808 }
2809 if (Add)
2810 ExtendNodes.push_back(User);
2811 } else {
2812 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002813 SDValue UseOp = User->getOperand(i);
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002814 if (UseOp == N0) {
2815 // If truncate from extended type to original load type is free
2816 // on this target, then it's ok to extend a CopyToReg.
2817 if (isTruncFree && User->getOpcode() == ISD::CopyToReg)
2818 HasCopyToRegUses = true;
2819 else
2820 return false;
2821 }
2822 }
2823 }
2824 }
2825
2826 if (HasCopyToRegUses) {
2827 bool BothLiveOut = false;
2828 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
2829 UI != UE; ++UI) {
Dan Gohman89684502008-07-27 20:43:25 +00002830 SDNode *User = *UI;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002831 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002832 SDValue UseOp = User->getOperand(i);
Gabor Greifba36cb52008-08-28 21:40:38 +00002833 if (UseOp.getNode() == N && UseOp.getResNo() == 0) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002834 BothLiveOut = true;
2835 break;
2836 }
2837 }
2838 }
2839 if (BothLiveOut)
2840 // Both unextended and extended values are live out. There had better be
2841 // good a reason for the transformation.
2842 return ExtendNodes.size();
2843 }
2844 return true;
2845}
2846
Dan Gohman475871a2008-07-27 21:46:04 +00002847SDValue DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
2848 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002849 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002850
Nate Begeman1d4d4142005-09-01 00:19:25 +00002851 // fold (sext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00002852 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002853 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0);
Chris Lattner310b5782006-05-06 23:06:26 +00002854
Nate Begeman1d4d4142005-09-01 00:19:25 +00002855 // fold (sext (sext x)) -> (sext x)
Chris Lattner310b5782006-05-06 23:06:26 +00002856 // fold (sext (aext x)) -> (sext x)
2857 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002858 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
Chris Lattner310b5782006-05-06 23:06:26 +00002859
Chris Lattner22558872007-02-26 03:13:59 +00002860 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman1fdfa6a2008-05-20 20:56:33 +00002861 // fold (sext (truncate (load x))) -> (sext (smaller load x))
2862 // fold (sext (truncate (srl (load x), c))) -> (sext (smaller load (x+c/n)))
Gabor Greifba36cb52008-08-28 21:40:38 +00002863 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
2864 if (NarrowLoad.getNode()) {
2865 if (NarrowLoad.getNode() != N0.getNode())
2866 CombineTo(N0.getNode(), NarrowLoad);
Evan Cheng0b063de2007-03-23 02:16:52 +00002867 return DAG.getNode(ISD::SIGN_EXTEND, VT, NarrowLoad);
2868 }
Evan Chengc88138f2007-03-22 01:54:19 +00002869
Dan Gohman1fdfa6a2008-05-20 20:56:33 +00002870 // See if the value being truncated is already sign extended. If so, just
2871 // eliminate the trunc/sext pair.
Dan Gohman475871a2008-07-27 21:46:04 +00002872 SDValue Op = N0.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002873 unsigned OpBits = Op.getValueType().getSizeInBits();
2874 unsigned MidBits = N0.getValueType().getSizeInBits();
2875 unsigned DestBits = VT.getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002876 unsigned NumSignBits = DAG.ComputeNumSignBits(Op);
Chris Lattner22558872007-02-26 03:13:59 +00002877
2878 if (OpBits == DestBits) {
2879 // Op is i32, Mid is i8, and Dest is i32. If Op has more than 24 sign
2880 // bits, it is already ready.
2881 if (NumSignBits > DestBits-MidBits)
2882 return Op;
2883 } else if (OpBits < DestBits) {
2884 // Op is i32, Mid is i8, and Dest is i64. If Op has more than 24 sign
2885 // bits, just sext from i32.
2886 if (NumSignBits > OpBits-MidBits)
2887 return DAG.getNode(ISD::SIGN_EXTEND, VT, Op);
2888 } else {
2889 // Op is i64, Mid is i8, and Dest is i32. If Op has more than 56 sign
2890 // bits, just truncate to i32.
2891 if (NumSignBits > OpBits-MidBits)
2892 return DAG.getNode(ISD::TRUNCATE, VT, Op);
Chris Lattner6007b842006-09-21 06:00:20 +00002893 }
Chris Lattner22558872007-02-26 03:13:59 +00002894
2895 // fold (sext (truncate x)) -> (sextinreg x).
2896 if (!AfterLegalize || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG,
2897 N0.getValueType())) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00002898 if (Op.getValueType().bitsLT(VT))
Chris Lattner22558872007-02-26 03:13:59 +00002899 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
Duncan Sands8e4eb092008-06-08 20:54:56 +00002900 else if (Op.getValueType().bitsGT(VT))
Chris Lattner22558872007-02-26 03:13:59 +00002901 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
2902 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, Op,
2903 DAG.getValueType(N0.getValueType()));
2904 }
Chris Lattner6007b842006-09-21 06:00:20 +00002905 }
Chris Lattner310b5782006-05-06 23:06:26 +00002906
Evan Cheng110dec22005-12-14 02:19:23 +00002907 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00002908 if (ISD::isNON_EXTLoad(N0.getNode()) &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002909 ((!AfterLegalize && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00002910 TLI.isLoadExtLegal(ISD::SEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002911 bool DoXform = true;
2912 SmallVector<SDNode*, 4> SetCCs;
2913 if (!N0.hasOneUse())
2914 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::SIGN_EXTEND, SetCCs, TLI);
2915 if (DoXform) {
2916 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman475871a2008-07-27 21:46:04 +00002917 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002918 LN0->getBasePtr(), LN0->getSrcValue(),
2919 LN0->getSrcValueOffset(),
2920 N0.getValueType(),
2921 LN0->isVolatile(),
2922 LN0->getAlignment());
2923 CombineTo(N, ExtLoad);
Dan Gohman475871a2008-07-27 21:46:04 +00002924 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00002925 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002926 // Extend SetCC uses if necessary.
2927 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
2928 SDNode *SetCC = SetCCs[i];
Dan Gohman475871a2008-07-27 21:46:04 +00002929 SmallVector<SDValue, 4> Ops;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002930 for (unsigned j = 0; j != 2; ++j) {
Dan Gohman475871a2008-07-27 21:46:04 +00002931 SDValue SOp = SetCC->getOperand(j);
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002932 if (SOp == Trunc)
2933 Ops.push_back(ExtLoad);
2934 else
2935 Ops.push_back(DAG.getNode(ISD::SIGN_EXTEND, VT, SOp));
2936 }
2937 Ops.push_back(SetCC->getOperand(2));
2938 CombineTo(SetCC, DAG.getNode(ISD::SETCC, SetCC->getValueType(0),
2939 &Ops[0], Ops.size()));
2940 }
Dan Gohman475871a2008-07-27 21:46:04 +00002941 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002942 }
Nate Begeman3df4d522005-10-12 20:40:40 +00002943 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002944
2945 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
2946 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00002947 if ((ISD::isSEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) &&
2948 ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002949 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002950 MVT EVT = LN0->getMemoryVT();
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002951 if ((!AfterLegalize && !LN0->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00002952 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT)) {
Dan Gohman475871a2008-07-27 21:46:04 +00002953 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00002954 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002955 LN0->getSrcValueOffset(), EVT,
2956 LN0->isVolatile(),
2957 LN0->getAlignment());
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00002958 CombineTo(N, ExtLoad);
Gabor Greif12632d22008-08-30 19:29:20 +00002959 CombineTo(N0.getNode(),
2960 DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00002961 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00002962 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00002963 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002964 }
2965
Chris Lattner20a35c32007-04-11 05:32:27 +00002966 // sext(setcc x,y,cc) -> select_cc x, y, -1, 0, cc
2967 if (N0.getOpcode() == ISD::SETCC) {
Dan Gohman475871a2008-07-27 21:46:04 +00002968 SDValue SCC =
Chris Lattner1eba01e2007-04-11 06:50:51 +00002969 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
2970 DAG.getConstant(~0ULL, VT), DAG.getConstant(0, VT),
2971 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00002972 if (SCC.getNode()) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00002973 }
2974
Dan Gohman8f0ad582008-04-28 16:58:24 +00002975 // fold (sext x) -> (zext x) if the sign bit is known zero.
Dan Gohman187db7b2008-04-28 18:47:17 +00002976 if ((!AfterLegalize || TLI.isOperationLegal(ISD::ZERO_EXTEND, VT)) &&
2977 DAG.SignBitIsZero(N0))
Dan Gohman8f0ad582008-04-28 16:58:24 +00002978 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
2979
Dan Gohman475871a2008-07-27 21:46:04 +00002980 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002981}
2982
Dan Gohman475871a2008-07-27 21:46:04 +00002983SDValue DAGCombiner::visitZERO_EXTEND(SDNode *N) {
2984 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002985 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002986
Nate Begeman1d4d4142005-09-01 00:19:25 +00002987 // fold (zext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00002988 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002989 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002990 // fold (zext (zext x)) -> (zext x)
Chris Lattner310b5782006-05-06 23:06:26 +00002991 // fold (zext (aext x)) -> (zext x)
2992 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002993 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
Chris Lattner6007b842006-09-21 06:00:20 +00002994
Evan Chengc88138f2007-03-22 01:54:19 +00002995 // fold (zext (truncate (load x))) -> (zext (smaller load x))
2996 // fold (zext (truncate (srl (load x), c))) -> (zext (small load (x+c/n)))
Dale Johannesen2041a0e2007-03-30 21:38:07 +00002997 if (N0.getOpcode() == ISD::TRUNCATE) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002998 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
2999 if (NarrowLoad.getNode()) {
3000 if (NarrowLoad.getNode() != N0.getNode())
3001 CombineTo(N0.getNode(), NarrowLoad);
Evan Cheng0b063de2007-03-23 02:16:52 +00003002 return DAG.getNode(ISD::ZERO_EXTEND, VT, NarrowLoad);
3003 }
Evan Chengc88138f2007-03-22 01:54:19 +00003004 }
3005
Chris Lattner6007b842006-09-21 06:00:20 +00003006 // fold (zext (truncate x)) -> (and x, mask)
3007 if (N0.getOpcode() == ISD::TRUNCATE &&
3008 (!AfterLegalize || TLI.isOperationLegal(ISD::AND, VT))) {
Dan Gohman475871a2008-07-27 21:46:04 +00003009 SDValue Op = N0.getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003010 if (Op.getValueType().bitsLT(VT)) {
Chris Lattner6007b842006-09-21 06:00:20 +00003011 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003012 } else if (Op.getValueType().bitsGT(VT)) {
Chris Lattner6007b842006-09-21 06:00:20 +00003013 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
3014 }
3015 return DAG.getZeroExtendInReg(Op, N0.getValueType());
3016 }
3017
Chris Lattner111c2282006-09-21 06:14:31 +00003018 // fold (zext (and (trunc x), cst)) -> (and x, cst).
3019 if (N0.getOpcode() == ISD::AND &&
3020 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
3021 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohman475871a2008-07-27 21:46:04 +00003022 SDValue X = N0.getOperand(0).getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003023 if (X.getValueType().bitsLT(VT)) {
Chris Lattner111c2282006-09-21 06:14:31 +00003024 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003025 } else if (X.getValueType().bitsGT(VT)) {
Chris Lattner111c2282006-09-21 06:14:31 +00003026 X = DAG.getNode(ISD::TRUNCATE, VT, X);
3027 }
Dan Gohman220a8232008-03-03 23:51:38 +00003028 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003029 Mask.zext(VT.getSizeInBits());
Chris Lattner111c2282006-09-21 06:14:31 +00003030 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
3031 }
3032
Evan Cheng110dec22005-12-14 02:19:23 +00003033 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00003034 if (ISD::isNON_EXTLoad(N0.getNode()) &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003035 ((!AfterLegalize && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00003036 TLI.isLoadExtLegal(ISD::ZEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003037 bool DoXform = true;
3038 SmallVector<SDNode*, 4> SetCCs;
3039 if (!N0.hasOneUse())
3040 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ZERO_EXTEND, SetCCs, TLI);
3041 if (DoXform) {
3042 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman475871a2008-07-27 21:46:04 +00003043 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003044 LN0->getBasePtr(), LN0->getSrcValue(),
3045 LN0->getSrcValueOffset(),
3046 N0.getValueType(),
3047 LN0->isVolatile(),
3048 LN0->getAlignment());
3049 CombineTo(N, ExtLoad);
Dan Gohman475871a2008-07-27 21:46:04 +00003050 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00003051 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003052 // Extend SetCC uses if necessary.
3053 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
3054 SDNode *SetCC = SetCCs[i];
Dan Gohman475871a2008-07-27 21:46:04 +00003055 SmallVector<SDValue, 4> Ops;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003056 for (unsigned j = 0; j != 2; ++j) {
Dan Gohman475871a2008-07-27 21:46:04 +00003057 SDValue SOp = SetCC->getOperand(j);
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003058 if (SOp == Trunc)
3059 Ops.push_back(ExtLoad);
3060 else
Evan Chengde1631b2007-10-30 20:11:21 +00003061 Ops.push_back(DAG.getNode(ISD::ZERO_EXTEND, VT, SOp));
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003062 }
3063 Ops.push_back(SetCC->getOperand(2));
3064 CombineTo(SetCC, DAG.getNode(ISD::SETCC, SetCC->getValueType(0),
3065 &Ops[0], Ops.size()));
3066 }
Dan Gohman475871a2008-07-27 21:46:04 +00003067 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003068 }
Evan Cheng110dec22005-12-14 02:19:23 +00003069 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00003070
3071 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
3072 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00003073 if ((ISD::isZEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) &&
3074 ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00003075 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003076 MVT EVT = LN0->getMemoryVT();
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003077 if ((!AfterLegalize && !LN0->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00003078 TLI.isLoadExtLegal(ISD::ZEXTLOAD, EVT)) {
Dan Gohman475871a2008-07-27 21:46:04 +00003079 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003080 LN0->getBasePtr(), LN0->getSrcValue(),
3081 LN0->getSrcValueOffset(), EVT,
3082 LN0->isVolatile(),
3083 LN0->getAlignment());
3084 CombineTo(N, ExtLoad);
Gabor Greif12632d22008-08-30 19:29:20 +00003085 CombineTo(N0.getNode(),
3086 DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003087 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003088 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003089 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00003090 }
Chris Lattner20a35c32007-04-11 05:32:27 +00003091
3092 // zext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
3093 if (N0.getOpcode() == ISD::SETCC) {
Dan Gohman475871a2008-07-27 21:46:04 +00003094 SDValue SCC =
Chris Lattner20a35c32007-04-11 05:32:27 +00003095 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
3096 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattner1eba01e2007-04-11 06:50:51 +00003097 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00003098 if (SCC.getNode()) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00003099 }
3100
Dan Gohman475871a2008-07-27 21:46:04 +00003101 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003102}
3103
Dan Gohman475871a2008-07-27 21:46:04 +00003104SDValue DAGCombiner::visitANY_EXTEND(SDNode *N) {
3105 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003106 MVT VT = N->getValueType(0);
Chris Lattner5ffc0662006-05-05 05:58:59 +00003107
3108 // fold (aext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00003109 if (isa<ConstantSDNode>(N0))
Chris Lattner5ffc0662006-05-05 05:58:59 +00003110 return DAG.getNode(ISD::ANY_EXTEND, VT, N0);
3111 // fold (aext (aext x)) -> (aext x)
3112 // fold (aext (zext x)) -> (zext x)
3113 // fold (aext (sext x)) -> (sext x)
3114 if (N0.getOpcode() == ISD::ANY_EXTEND ||
3115 N0.getOpcode() == ISD::ZERO_EXTEND ||
3116 N0.getOpcode() == ISD::SIGN_EXTEND)
3117 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
3118
Evan Chengc88138f2007-03-22 01:54:19 +00003119 // fold (aext (truncate (load x))) -> (aext (smaller load x))
3120 // fold (aext (truncate (srl (load x), c))) -> (aext (small load (x+c/n)))
3121 if (N0.getOpcode() == ISD::TRUNCATE) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003122 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
3123 if (NarrowLoad.getNode()) {
3124 if (NarrowLoad.getNode() != N0.getNode())
3125 CombineTo(N0.getNode(), NarrowLoad);
Evan Cheng0b063de2007-03-23 02:16:52 +00003126 return DAG.getNode(ISD::ANY_EXTEND, VT, NarrowLoad);
3127 }
Evan Chengc88138f2007-03-22 01:54:19 +00003128 }
3129
Chris Lattner84750582006-09-20 06:29:17 +00003130 // fold (aext (truncate x))
3131 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman475871a2008-07-27 21:46:04 +00003132 SDValue TruncOp = N0.getOperand(0);
Chris Lattner84750582006-09-20 06:29:17 +00003133 if (TruncOp.getValueType() == VT)
3134 return TruncOp; // x iff x size == zext size.
Duncan Sands8e4eb092008-06-08 20:54:56 +00003135 if (TruncOp.getValueType().bitsGT(VT))
Chris Lattner84750582006-09-20 06:29:17 +00003136 return DAG.getNode(ISD::TRUNCATE, VT, TruncOp);
3137 return DAG.getNode(ISD::ANY_EXTEND, VT, TruncOp);
3138 }
Chris Lattner0e4b9222006-09-21 06:40:43 +00003139
3140 // fold (aext (and (trunc x), cst)) -> (and x, cst).
3141 if (N0.getOpcode() == ISD::AND &&
3142 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
3143 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohman475871a2008-07-27 21:46:04 +00003144 SDValue X = N0.getOperand(0).getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003145 if (X.getValueType().bitsLT(VT)) {
Chris Lattner0e4b9222006-09-21 06:40:43 +00003146 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003147 } else if (X.getValueType().bitsGT(VT)) {
Chris Lattner0e4b9222006-09-21 06:40:43 +00003148 X = DAG.getNode(ISD::TRUNCATE, VT, X);
3149 }
Dan Gohman220a8232008-03-03 23:51:38 +00003150 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003151 Mask.zext(VT.getSizeInBits());
Chris Lattner0e4b9222006-09-21 06:40:43 +00003152 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
3153 }
3154
Chris Lattner5ffc0662006-05-05 05:58:59 +00003155 // fold (aext (load x)) -> (aext (truncate (extload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00003156 if (ISD::isNON_EXTLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003157 ((!AfterLegalize && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00003158 TLI.isLoadExtLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003159 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman475871a2008-07-27 21:46:04 +00003160 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
Evan Cheng466685d2006-10-09 20:57:25 +00003161 LN0->getBasePtr(), LN0->getSrcValue(),
3162 LN0->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003163 N0.getValueType(),
3164 LN0->isVolatile(),
3165 LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00003166 CombineTo(N, ExtLoad);
Dan Gohman75dcf082008-07-31 00:50:31 +00003167 // Redirect any chain users to the new load.
Evan Cheng45299662008-08-29 23:20:46 +00003168 DAG.ReplaceAllUsesOfValueWith(SDValue(LN0, 1),
3169 SDValue(ExtLoad.getNode(), 1));
Dan Gohman75dcf082008-07-31 00:50:31 +00003170 // If any node needs the original loaded value, recompute it.
3171 if (!LN0->use_empty())
3172 CombineTo(LN0, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
3173 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003174 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner5ffc0662006-05-05 05:58:59 +00003175 }
3176
3177 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
3178 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
3179 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00003180 if (N0.getOpcode() == ISD::LOAD &&
Gabor Greifba36cb52008-08-28 21:40:38 +00003181 !ISD::isNON_EXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng466685d2006-10-09 20:57:25 +00003182 N0.hasOneUse()) {
3183 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003184 MVT EVT = LN0->getMemoryVT();
Dan Gohman475871a2008-07-27 21:46:04 +00003185 SDValue ExtLoad = DAG.getExtLoad(LN0->getExtensionType(), VT,
Evan Cheng466685d2006-10-09 20:57:25 +00003186 LN0->getChain(), LN0->getBasePtr(),
3187 LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003188 LN0->getSrcValueOffset(), EVT,
3189 LN0->isVolatile(),
3190 LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00003191 CombineTo(N, ExtLoad);
Evan Cheng45299662008-08-29 23:20:46 +00003192 CombineTo(N0.getNode(),
3193 DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
Chris Lattner5ffc0662006-05-05 05:58:59 +00003194 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003195 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner5ffc0662006-05-05 05:58:59 +00003196 }
Chris Lattner20a35c32007-04-11 05:32:27 +00003197
3198 // aext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
3199 if (N0.getOpcode() == ISD::SETCC) {
Dan Gohman475871a2008-07-27 21:46:04 +00003200 SDValue SCC =
Chris Lattner1eba01e2007-04-11 06:50:51 +00003201 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
3202 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattnerc24bbad2007-04-11 16:51:53 +00003203 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00003204 if (SCC.getNode())
Chris Lattnerc56a81d2007-04-11 06:43:25 +00003205 return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00003206 }
3207
Dan Gohman475871a2008-07-27 21:46:04 +00003208 return SDValue();
Chris Lattner5ffc0662006-05-05 05:58:59 +00003209}
3210
Chris Lattner2b4c2792007-10-13 06:35:54 +00003211/// GetDemandedBits - See if the specified operand can be simplified with the
3212/// knowledge that only the bits specified by Mask are used. If so, return the
Dan Gohman475871a2008-07-27 21:46:04 +00003213/// simpler operand, otherwise return a null SDValue.
3214SDValue DAGCombiner::GetDemandedBits(SDValue V, const APInt &Mask) {
Chris Lattner2b4c2792007-10-13 06:35:54 +00003215 switch (V.getOpcode()) {
3216 default: break;
3217 case ISD::OR:
3218 case ISD::XOR:
3219 // If the LHS or RHS don't contribute bits to the or, drop them.
3220 if (DAG.MaskedValueIsZero(V.getOperand(0), Mask))
3221 return V.getOperand(1);
3222 if (DAG.MaskedValueIsZero(V.getOperand(1), Mask))
3223 return V.getOperand(0);
3224 break;
Chris Lattnere33544c2007-10-13 06:58:48 +00003225 case ISD::SRL:
3226 // Only look at single-use SRLs.
Gabor Greifba36cb52008-08-28 21:40:38 +00003227 if (!V.getNode()->hasOneUse())
Chris Lattnere33544c2007-10-13 06:58:48 +00003228 break;
3229 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
3230 // See if we can recursively simplify the LHS.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003231 unsigned Amt = RHSC->getZExtValue();
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003232 APInt NewMask = Mask << Amt;
Dan Gohman475871a2008-07-27 21:46:04 +00003233 SDValue SimplifyLHS = GetDemandedBits(V.getOperand(0), NewMask);
Gabor Greifba36cb52008-08-28 21:40:38 +00003234 if (SimplifyLHS.getNode()) {
Chris Lattnere33544c2007-10-13 06:58:48 +00003235 return DAG.getNode(ISD::SRL, V.getValueType(),
3236 SimplifyLHS, V.getOperand(1));
3237 }
3238 }
Chris Lattner2b4c2792007-10-13 06:35:54 +00003239 }
Dan Gohman475871a2008-07-27 21:46:04 +00003240 return SDValue();
Chris Lattner2b4c2792007-10-13 06:35:54 +00003241}
3242
Evan Chengc88138f2007-03-22 01:54:19 +00003243/// ReduceLoadWidth - If the result of a wider load is shifted to right of N
3244/// bits and then truncated to a narrower type and where N is a multiple
3245/// of number of bits of the narrower type, transform it to a narrower load
3246/// from address + N / num of bits of new type. If the result is to be
3247/// extended, also fold the extension to form a extending load.
Dan Gohman475871a2008-07-27 21:46:04 +00003248SDValue DAGCombiner::ReduceLoadWidth(SDNode *N) {
Evan Chengc88138f2007-03-22 01:54:19 +00003249 unsigned Opc = N->getOpcode();
3250 ISD::LoadExtType ExtType = ISD::NON_EXTLOAD;
Dan Gohman475871a2008-07-27 21:46:04 +00003251 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003252 MVT VT = N->getValueType(0);
3253 MVT EVT = N->getValueType(0);
Evan Chengc88138f2007-03-22 01:54:19 +00003254
Dan Gohman7f8613e2008-08-14 20:04:46 +00003255 // This transformation isn't valid for vector loads.
3256 if (VT.isVector())
3257 return SDValue();
3258
Evan Chenge177e302007-03-23 22:13:36 +00003259 // Special case: SIGN_EXTEND_INREG is basically truncating to EVT then
3260 // extended to VT.
Evan Chengc88138f2007-03-22 01:54:19 +00003261 if (Opc == ISD::SIGN_EXTEND_INREG) {
3262 ExtType = ISD::SEXTLOAD;
3263 EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Evan Cheng03294662008-10-14 21:26:46 +00003264 if (AfterLegalize && !TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))
Dan Gohman475871a2008-07-27 21:46:04 +00003265 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00003266 }
3267
Duncan Sands83ec4b62008-06-06 12:08:01 +00003268 unsigned EVTBits = EVT.getSizeInBits();
Evan Chengc88138f2007-03-22 01:54:19 +00003269 unsigned ShAmt = 0;
Evan Chengb37b80c2007-03-23 20:55:21 +00003270 bool CombineSRL = false;
Evan Chengc88138f2007-03-22 01:54:19 +00003271 if (N0.getOpcode() == ISD::SRL && N0.hasOneUse()) {
3272 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003273 ShAmt = N01->getZExtValue();
Evan Chengc88138f2007-03-22 01:54:19 +00003274 // Is the shift amount a multiple of size of VT?
3275 if ((ShAmt & (EVTBits-1)) == 0) {
3276 N0 = N0.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003277 if (N0.getValueType().getSizeInBits() <= EVTBits)
Dan Gohman475871a2008-07-27 21:46:04 +00003278 return SDValue();
Evan Chengb37b80c2007-03-23 20:55:21 +00003279 CombineSRL = true;
Evan Chengc88138f2007-03-22 01:54:19 +00003280 }
3281 }
3282 }
3283
Duncan Sandsad205a72008-06-16 08:14:38 +00003284 // Do not generate loads of non-round integer types since these can
3285 // be expensive (and would be wrong if the type is not byte sized).
Dan Gohman75dcf082008-07-31 00:50:31 +00003286 if (isa<LoadSDNode>(N0) && N0.hasOneUse() && VT.isRound() &&
3287 cast<LoadSDNode>(N0)->getMemoryVT().getSizeInBits() > EVTBits &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003288 // Do not change the width of a volatile load.
3289 !cast<LoadSDNode>(N0)->isVolatile()) {
Evan Chengc88138f2007-03-22 01:54:19 +00003290 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003291 MVT PtrType = N0.getOperand(1).getValueType();
Evan Chengdae54ce2007-03-24 00:02:43 +00003292 // For big endian targets, we need to adjust the offset to the pointer to
3293 // load the correct bytes.
Duncan Sands0753fc12008-02-11 10:37:04 +00003294 if (TLI.isBigEndian()) {
Dan Gohman75dcf082008-07-31 00:50:31 +00003295 unsigned LVTStoreBits = LN0->getMemoryVT().getStoreSizeInBits();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003296 unsigned EVTStoreBits = EVT.getStoreSizeInBits();
Duncan Sandsc6fa1702007-11-09 08:57:19 +00003297 ShAmt = LVTStoreBits - EVTStoreBits - ShAmt;
3298 }
Evan Chengdae54ce2007-03-24 00:02:43 +00003299 uint64_t PtrOff = ShAmt / 8;
Duncan Sandsdc846502007-10-28 12:59:45 +00003300 unsigned NewAlign = MinAlign(LN0->getAlignment(), PtrOff);
Dan Gohman475871a2008-07-27 21:46:04 +00003301 SDValue NewPtr = DAG.getNode(ISD::ADD, PtrType, LN0->getBasePtr(),
Evan Chengc88138f2007-03-22 01:54:19 +00003302 DAG.getConstant(PtrOff, PtrType));
Gabor Greifba36cb52008-08-28 21:40:38 +00003303 AddToWorkList(NewPtr.getNode());
Dan Gohman475871a2008-07-27 21:46:04 +00003304 SDValue Load = (ExtType == ISD::NON_EXTLOAD)
Evan Chengc88138f2007-03-22 01:54:19 +00003305 ? DAG.getLoad(VT, LN0->getChain(), NewPtr,
Dan Gohman75dcf082008-07-31 00:50:31 +00003306 LN0->getSrcValue(), LN0->getSrcValueOffset() + PtrOff,
Duncan Sandsdc846502007-10-28 12:59:45 +00003307 LN0->isVolatile(), NewAlign)
Evan Chengc88138f2007-03-22 01:54:19 +00003308 : DAG.getExtLoad(ExtType, VT, LN0->getChain(), NewPtr,
Dan Gohman75dcf082008-07-31 00:50:31 +00003309 LN0->getSrcValue(), LN0->getSrcValueOffset() + PtrOff,
3310 EVT, LN0->isVolatile(), NewAlign);
Evan Chengc88138f2007-03-22 01:54:19 +00003311 AddToWorkList(N);
Evan Chengb37b80c2007-03-23 20:55:21 +00003312 if (CombineSRL) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003313 WorkListRemover DeadNodes(*this);
3314 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), Load.getValue(1),
3315 &DeadNodes);
Gabor Greifba36cb52008-08-28 21:40:38 +00003316 CombineTo(N->getOperand(0).getNode(), Load);
Evan Chengb37b80c2007-03-23 20:55:21 +00003317 } else
Gabor Greifba36cb52008-08-28 21:40:38 +00003318 CombineTo(N0.getNode(), Load, Load.getValue(1));
Evan Cheng15213b72007-03-26 07:12:51 +00003319 if (ShAmt) {
3320 if (Opc == ISD::SIGN_EXTEND_INREG)
3321 return DAG.getNode(Opc, VT, Load, N->getOperand(1));
3322 else
3323 return DAG.getNode(Opc, VT, Load);
3324 }
Dan Gohman475871a2008-07-27 21:46:04 +00003325 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Chengc88138f2007-03-22 01:54:19 +00003326 }
3327
Dan Gohman475871a2008-07-27 21:46:04 +00003328 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00003329}
3330
Chris Lattner5ffc0662006-05-05 05:58:59 +00003331
Dan Gohman475871a2008-07-27 21:46:04 +00003332SDValue DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
3333 SDValue N0 = N->getOperand(0);
3334 SDValue N1 = N->getOperand(1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003335 MVT VT = N->getValueType(0);
3336 MVT EVT = cast<VTSDNode>(N1)->getVT();
3337 unsigned VTBits = VT.getSizeInBits();
3338 unsigned EVTBits = EVT.getSizeInBits();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003339
Nate Begeman1d4d4142005-09-01 00:19:25 +00003340 // fold (sext_in_reg c1) -> c1
Chris Lattnereaeda562006-05-08 20:59:41 +00003341 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
Chris Lattner310b5782006-05-06 23:06:26 +00003342 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0, N1);
Chris Lattneree4ea922006-05-06 09:30:03 +00003343
Chris Lattner541a24f2006-05-06 22:43:44 +00003344 // If the input is already sign extended, just drop the extension.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003345 if (DAG.ComputeNumSignBits(N0) >= VT.getSizeInBits()-EVTBits+1)
Chris Lattneree4ea922006-05-06 09:30:03 +00003346 return N0;
3347
Nate Begeman646d7e22005-09-02 21:18:40 +00003348 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
3349 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Duncan Sands8e4eb092008-06-08 20:54:56 +00003350 EVT.bitsLT(cast<VTSDNode>(N0.getOperand(1))->getVT())) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00003351 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00003352 }
Chris Lattner4b37e872006-05-08 21:18:59 +00003353
Dan Gohman75dcf082008-07-31 00:50:31 +00003354 // fold (sext_in_reg (sext x)) -> (sext x)
3355 // fold (sext_in_reg (aext x)) -> (sext x)
3356 // if x is small enough.
3357 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND) {
3358 SDValue N00 = N0.getOperand(0);
3359 if (N00.getValueType().getSizeInBits() < EVTBits)
3360 return DAG.getNode(ISD::SIGN_EXTEND, VT, N00, N1);
3361 }
3362
Chris Lattner95a5e052007-04-17 19:03:21 +00003363 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is known zero.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003364 if (DAG.MaskedValueIsZero(N0, APInt::getBitsSet(VTBits, EVTBits-1, EVTBits)))
Nate Begemande996292006-02-03 22:24:05 +00003365 return DAG.getZeroExtendInReg(N0, EVT);
Chris Lattner4b37e872006-05-08 21:18:59 +00003366
Chris Lattner95a5e052007-04-17 19:03:21 +00003367 // fold operands of sext_in_reg based on knowledge that the top bits are not
3368 // demanded.
Dan Gohman475871a2008-07-27 21:46:04 +00003369 if (SimplifyDemandedBits(SDValue(N, 0)))
3370 return SDValue(N, 0);
Chris Lattner95a5e052007-04-17 19:03:21 +00003371
Evan Chengc88138f2007-03-22 01:54:19 +00003372 // fold (sext_in_reg (load x)) -> (smaller sextload x)
3373 // fold (sext_in_reg (srl (load x), c)) -> (smaller sextload (x+c/evtbits))
Dan Gohman475871a2008-07-27 21:46:04 +00003374 SDValue NarrowLoad = ReduceLoadWidth(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003375 if (NarrowLoad.getNode())
Evan Chengc88138f2007-03-22 01:54:19 +00003376 return NarrowLoad;
3377
Chris Lattner4b37e872006-05-08 21:18:59 +00003378 // fold (sext_in_reg (srl X, 24), i8) -> sra X, 24
3379 // fold (sext_in_reg (srl X, 23), i8) -> sra X, 23 iff possible.
3380 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
3381 if (N0.getOpcode() == ISD::SRL) {
3382 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003383 if (ShAmt->getZExtValue()+EVTBits <= VT.getSizeInBits()) {
Chris Lattner4b37e872006-05-08 21:18:59 +00003384 // We can turn this into an SRA iff the input to the SRL is already sign
3385 // extended enough.
Dan Gohmanea859be2007-06-22 14:59:07 +00003386 unsigned InSignBits = DAG.ComputeNumSignBits(N0.getOperand(0));
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003387 if (VT.getSizeInBits()-(ShAmt->getZExtValue()+EVTBits) < InSignBits)
Chris Lattner4b37e872006-05-08 21:18:59 +00003388 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0), N0.getOperand(1));
3389 }
3390 }
Evan Chengc88138f2007-03-22 01:54:19 +00003391
Nate Begemanded49632005-10-13 03:11:28 +00003392 // fold (sext_inreg (extload x)) -> (sextload x)
Gabor Greifba36cb52008-08-28 21:40:38 +00003393 if (ISD::isEXTLoad(N0.getNode()) &&
3394 ISD::isUNINDEXEDLoad(N0.getNode()) &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003395 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003396 ((!AfterLegalize && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00003397 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003398 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman475871a2008-07-27 21:46:04 +00003399 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
Evan Cheng466685d2006-10-09 20:57:25 +00003400 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003401 LN0->getSrcValueOffset(), EVT,
3402 LN0->isVolatile(),
3403 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00003404 CombineTo(N, ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00003405 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003406 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00003407 }
3408 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Gabor Greifba36cb52008-08-28 21:40:38 +00003409 if (ISD::isZEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng83060c52007-03-07 08:07:03 +00003410 N0.hasOneUse() &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003411 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003412 ((!AfterLegalize && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00003413 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003414 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman475871a2008-07-27 21:46:04 +00003415 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
Evan Cheng466685d2006-10-09 20:57:25 +00003416 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003417 LN0->getSrcValueOffset(), EVT,
3418 LN0->isVolatile(),
3419 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00003420 CombineTo(N, ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00003421 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003422 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00003423 }
Dan Gohman475871a2008-07-27 21:46:04 +00003424 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003425}
3426
Dan Gohman475871a2008-07-27 21:46:04 +00003427SDValue DAGCombiner::visitTRUNCATE(SDNode *N) {
3428 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003429 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003430
3431 // noop truncate
3432 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00003433 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003434 // fold (truncate c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00003435 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00003436 return DAG.getNode(ISD::TRUNCATE, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003437 // fold (truncate (truncate x)) -> (truncate x)
3438 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00003439 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003440 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
Chris Lattnerb72773b2006-05-05 22:56:26 +00003441 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND||
3442 N0.getOpcode() == ISD::ANY_EXTEND) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00003443 if (N0.getOperand(0).getValueType().bitsLT(VT))
Nate Begeman1d4d4142005-09-01 00:19:25 +00003444 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00003445 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Duncan Sands8e4eb092008-06-08 20:54:56 +00003446 else if (N0.getOperand(0).getValueType().bitsGT(VT))
Nate Begeman1d4d4142005-09-01 00:19:25 +00003447 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00003448 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003449 else
3450 // if the source and dest are the same type, we can drop both the extend
3451 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00003452 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003453 }
Evan Cheng007b69e2007-03-21 20:14:05 +00003454
Chris Lattner2b4c2792007-10-13 06:35:54 +00003455 // See if we can simplify the input to this truncate through knowledge that
3456 // only the low bits are being used. For example "trunc (or (shl x, 8), y)"
3457 // -> trunc y
Dan Gohman475871a2008-07-27 21:46:04 +00003458 SDValue Shorter =
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003459 GetDemandedBits(N0, APInt::getLowBitsSet(N0.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +00003460 VT.getSizeInBits()));
Gabor Greifba36cb52008-08-28 21:40:38 +00003461 if (Shorter.getNode())
Chris Lattner2b4c2792007-10-13 06:35:54 +00003462 return DAG.getNode(ISD::TRUNCATE, VT, Shorter);
3463
Nate Begeman3df4d522005-10-12 20:40:40 +00003464 // fold (truncate (load x)) -> (smaller load x)
Evan Cheng007b69e2007-03-21 20:14:05 +00003465 // fold (truncate (srl (load x), c)) -> (smaller load (x+c/evtbits))
Evan Chengc88138f2007-03-22 01:54:19 +00003466 return ReduceLoadWidth(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003467}
3468
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003469static SDNode *getBuildPairElt(SDNode *N, unsigned i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003470 SDValue Elt = N->getOperand(i);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003471 if (Elt.getOpcode() != ISD::MERGE_VALUES)
Gabor Greifba36cb52008-08-28 21:40:38 +00003472 return Elt.getNode();
3473 return Elt.getOperand(Elt.getResNo()).getNode();
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003474}
3475
3476/// CombineConsecutiveLoads - build_pair (load, load) -> load
3477/// if load locations are consecutive.
Dan Gohman475871a2008-07-27 21:46:04 +00003478SDValue DAGCombiner::CombineConsecutiveLoads(SDNode *N, MVT VT) {
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003479 assert(N->getOpcode() == ISD::BUILD_PAIR);
3480
3481 SDNode *LD1 = getBuildPairElt(N, 0);
3482 if (!ISD::isNON_EXTLoad(LD1) || !LD1->hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00003483 return SDValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003484 MVT LD1VT = LD1->getValueType(0);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003485 SDNode *LD2 = getBuildPairElt(N, 1);
3486 const MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3487 if (ISD::isNON_EXTLoad(LD2) &&
3488 LD2->hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003489 // If both are volatile this would reduce the number of volatile loads.
3490 // If one is volatile it might be ok, but play conservative and bail out.
3491 !cast<LoadSDNode>(LD1)->isVolatile() &&
3492 !cast<LoadSDNode>(LD2)->isVolatile() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003493 TLI.isConsecutiveLoad(LD2, LD1, LD1VT.getSizeInBits()/8, 1, MFI)) {
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003494 LoadSDNode *LD = cast<LoadSDNode>(LD1);
3495 unsigned Align = LD->getAlignment();
Dan Gohman6448d912008-09-04 15:39:15 +00003496 unsigned NewAlign = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00003497 getABITypeAlignment(VT.getTypeForMVT());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003498 if (NewAlign <= Align &&
3499 (!AfterLegalize || TLI.isOperationLegal(ISD::LOAD, VT)))
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003500 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(),
3501 LD->getSrcValue(), LD->getSrcValueOffset(),
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003502 false, Align);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003503 }
Dan Gohman475871a2008-07-27 21:46:04 +00003504 return SDValue();
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003505}
3506
Dan Gohman475871a2008-07-27 21:46:04 +00003507SDValue DAGCombiner::visitBIT_CONVERT(SDNode *N) {
3508 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003509 MVT VT = N->getValueType(0);
Chris Lattner94683772005-12-23 05:30:37 +00003510
Dan Gohman7f321562007-06-25 16:23:39 +00003511 // If the input is a BUILD_VECTOR with all constant elements, fold this now.
3512 // Only do this before legalize, since afterward the target may be depending
3513 // on the bitconvert.
3514 // First check to see if this is all constant.
3515 if (!AfterLegalize &&
Gabor Greifba36cb52008-08-28 21:40:38 +00003516 N0.getOpcode() == ISD::BUILD_VECTOR && N0.getNode()->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003517 VT.isVector()) {
Dan Gohman7f321562007-06-25 16:23:39 +00003518 bool isSimple = true;
3519 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i)
3520 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
3521 N0.getOperand(i).getOpcode() != ISD::Constant &&
3522 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
3523 isSimple = false;
3524 break;
3525 }
3526
Duncan Sands83ec4b62008-06-06 12:08:01 +00003527 MVT DestEltVT = N->getValueType(0).getVectorElementType();
3528 assert(!DestEltVT.isVector() &&
Dan Gohman7f321562007-06-25 16:23:39 +00003529 "Element type of vector ValueType must not be vector!");
3530 if (isSimple) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003531 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(N0.getNode(), DestEltVT);
Dan Gohman7f321562007-06-25 16:23:39 +00003532 }
3533 }
3534
Dan Gohman3dd168d2008-09-05 01:58:21 +00003535 // If the input is a constant, let getNode fold it.
Chris Lattner94683772005-12-23 05:30:37 +00003536 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
Dan Gohman475871a2008-07-27 21:46:04 +00003537 SDValue Res = DAG.getNode(ISD::BIT_CONVERT, VT, N0);
Gabor Greifba36cb52008-08-28 21:40:38 +00003538 if (Res.getNode() != N) return Res;
Chris Lattner94683772005-12-23 05:30:37 +00003539 }
3540
Chris Lattnerc8547d82005-12-23 05:37:50 +00003541 if (N0.getOpcode() == ISD::BIT_CONVERT) // conv(conv(x,t1),t2) -> conv(x,t2)
3542 return DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
Chris Lattner6258fb22006-04-02 02:53:43 +00003543
Chris Lattner57104102005-12-23 05:44:41 +00003544 // fold (conv (load x)) -> (load (conv*)x)
Evan Cheng513da432007-10-06 08:19:55 +00003545 // If the resultant load doesn't need a higher alignment than the original!
Gabor Greifba36cb52008-08-28 21:40:38 +00003546 if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003547 // Do not change the width of a volatile load.
3548 !cast<LoadSDNode>(N0)->isVolatile() &&
3549 (!AfterLegalize || TLI.isOperationLegal(ISD::LOAD, VT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003550 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman6448d912008-09-04 15:39:15 +00003551 unsigned Align = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00003552 getABITypeAlignment(VT.getTypeForMVT());
Evan Cheng59d5b682007-05-07 21:27:48 +00003553 unsigned OrigAlign = LN0->getAlignment();
3554 if (Align <= OrigAlign) {
Dan Gohman475871a2008-07-27 21:46:04 +00003555 SDValue Load = DAG.getLoad(VT, LN0->getChain(), LN0->getBasePtr(),
Evan Cheng59d5b682007-05-07 21:27:48 +00003556 LN0->getSrcValue(), LN0->getSrcValueOffset(),
Dan Gohman77455612008-06-28 00:45:22 +00003557 LN0->isVolatile(), OrigAlign);
Evan Cheng59d5b682007-05-07 21:27:48 +00003558 AddToWorkList(N);
Gabor Greif12632d22008-08-30 19:29:20 +00003559 CombineTo(N0.getNode(),
3560 DAG.getNode(ISD::BIT_CONVERT, N0.getValueType(), Load),
Evan Cheng59d5b682007-05-07 21:27:48 +00003561 Load.getValue(1));
3562 return Load;
3563 }
Chris Lattner57104102005-12-23 05:44:41 +00003564 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003565
Chris Lattner3bd39d42008-01-27 17:42:27 +00003566 // Fold bitconvert(fneg(x)) -> xor(bitconvert(x), signbit)
3567 // Fold bitconvert(fabs(x)) -> and(bitconvert(x), ~signbit)
3568 // This often reduces constant pool loads.
3569 if ((N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FABS) &&
Gabor Greifba36cb52008-08-28 21:40:38 +00003570 N0.getNode()->hasOneUse() && VT.isInteger() && !VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003571 SDValue NewConv = DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00003572 AddToWorkList(NewConv.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003573
Duncan Sands83ec4b62008-06-06 12:08:01 +00003574 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003575 if (N0.getOpcode() == ISD::FNEG)
3576 return DAG.getNode(ISD::XOR, VT, NewConv, DAG.getConstant(SignBit, VT));
3577 assert(N0.getOpcode() == ISD::FABS);
3578 return DAG.getNode(ISD::AND, VT, NewConv, DAG.getConstant(~SignBit, VT));
3579 }
3580
3581 // Fold bitconvert(fcopysign(cst, x)) -> bitconvert(x)&sign | cst&~sign'
3582 // Note that we don't handle copysign(x,cst) because this can always be folded
3583 // to an fneg or fabs.
Gabor Greifba36cb52008-08-28 21:40:38 +00003584 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse() &&
Chris Lattnerf32aac32008-01-27 23:32:17 +00003585 isa<ConstantFPSDNode>(N0.getOperand(0)) &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003586 VT.isInteger() && !VT.isVector()) {
3587 unsigned OrigXWidth = N0.getOperand(1).getValueType().getSizeInBits();
Dan Gohman475871a2008-07-27 21:46:04 +00003588 SDValue X = DAG.getNode(ISD::BIT_CONVERT,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003589 MVT::getIntegerVT(OrigXWidth),
Chris Lattner3bd39d42008-01-27 17:42:27 +00003590 N0.getOperand(1));
Gabor Greifba36cb52008-08-28 21:40:38 +00003591 AddToWorkList(X.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003592
3593 // If X has a different width than the result/lhs, sext it or truncate it.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003594 unsigned VTWidth = VT.getSizeInBits();
Chris Lattner3bd39d42008-01-27 17:42:27 +00003595 if (OrigXWidth < VTWidth) {
3596 X = DAG.getNode(ISD::SIGN_EXTEND, VT, X);
Gabor Greifba36cb52008-08-28 21:40:38 +00003597 AddToWorkList(X.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003598 } else if (OrigXWidth > VTWidth) {
3599 // To get the sign bit in the right place, we have to shift it right
3600 // before truncating.
3601 X = DAG.getNode(ISD::SRL, X.getValueType(), X,
3602 DAG.getConstant(OrigXWidth-VTWidth, X.getValueType()));
Gabor Greifba36cb52008-08-28 21:40:38 +00003603 AddToWorkList(X.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003604 X = DAG.getNode(ISD::TRUNCATE, VT, X);
Gabor Greifba36cb52008-08-28 21:40:38 +00003605 AddToWorkList(X.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003606 }
3607
Duncan Sands83ec4b62008-06-06 12:08:01 +00003608 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003609 X = DAG.getNode(ISD::AND, VT, X, DAG.getConstant(SignBit, VT));
Gabor Greifba36cb52008-08-28 21:40:38 +00003610 AddToWorkList(X.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003611
Dan Gohman475871a2008-07-27 21:46:04 +00003612 SDValue Cst = DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
Chris Lattner3bd39d42008-01-27 17:42:27 +00003613 Cst = DAG.getNode(ISD::AND, VT, Cst, DAG.getConstant(~SignBit, VT));
Gabor Greifba36cb52008-08-28 21:40:38 +00003614 AddToWorkList(Cst.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003615
3616 return DAG.getNode(ISD::OR, VT, X, Cst);
3617 }
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003618
3619 // bitconvert(build_pair(ld, ld)) -> ld iff load locations are consecutive.
3620 if (N0.getOpcode() == ISD::BUILD_PAIR) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003621 SDValue CombineLD = CombineConsecutiveLoads(N0.getNode(), VT);
3622 if (CombineLD.getNode())
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003623 return CombineLD;
3624 }
Chris Lattner3bd39d42008-01-27 17:42:27 +00003625
Dan Gohman475871a2008-07-27 21:46:04 +00003626 return SDValue();
Chris Lattner94683772005-12-23 05:30:37 +00003627}
3628
Dan Gohman475871a2008-07-27 21:46:04 +00003629SDValue DAGCombiner::visitBUILD_PAIR(SDNode *N) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003630 MVT VT = N->getValueType(0);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003631 return CombineConsecutiveLoads(N, VT);
3632}
3633
Dan Gohman7f321562007-06-25 16:23:39 +00003634/// ConstantFoldBIT_CONVERTofBUILD_VECTOR - We know that BV is a build_vector
Chris Lattner6258fb22006-04-02 02:53:43 +00003635/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
3636/// destination element value type.
Dan Gohman475871a2008-07-27 21:46:04 +00003637SDValue DAGCombiner::
Duncan Sands83ec4b62008-06-06 12:08:01 +00003638ConstantFoldBIT_CONVERTofBUILD_VECTOR(SDNode *BV, MVT DstEltVT) {
3639 MVT SrcEltVT = BV->getOperand(0).getValueType();
Chris Lattner6258fb22006-04-02 02:53:43 +00003640
3641 // If this is already the right type, we're done.
Dan Gohman475871a2008-07-27 21:46:04 +00003642 if (SrcEltVT == DstEltVT) return SDValue(BV, 0);
Chris Lattner6258fb22006-04-02 02:53:43 +00003643
Duncan Sands83ec4b62008-06-06 12:08:01 +00003644 unsigned SrcBitSize = SrcEltVT.getSizeInBits();
3645 unsigned DstBitSize = DstEltVT.getSizeInBits();
Chris Lattner6258fb22006-04-02 02:53:43 +00003646
3647 // If this is a conversion of N elements of one type to N elements of another
3648 // type, convert each element. This handles FP<->INT cases.
3649 if (SrcBitSize == DstBitSize) {
Dan Gohman475871a2008-07-27 21:46:04 +00003650 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003651 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003652 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, DstEltVT, BV->getOperand(i)));
Gabor Greifba36cb52008-08-28 21:40:38 +00003653 AddToWorkList(Ops.back().getNode());
Chris Lattner3e104b12006-04-08 04:15:24 +00003654 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00003655 MVT VT = MVT::getVectorVT(DstEltVT,
3656 BV->getValueType(0).getVectorNumElements());
Dan Gohman7f321562007-06-25 16:23:39 +00003657 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003658 }
3659
3660 // Otherwise, we're growing or shrinking the elements. To avoid having to
3661 // handle annoying details of growing/shrinking FP values, we convert them to
3662 // int first.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003663 if (SrcEltVT.isFloatingPoint()) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003664 // Convert the input float vector to a int vector where the elements are the
3665 // same sizes.
3666 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
Duncan Sands8eab8a22008-06-09 11:32:28 +00003667 MVT IntVT = MVT::getIntegerVT(SrcEltVT.getSizeInBits());
Gabor Greifba36cb52008-08-28 21:40:38 +00003668 BV = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, IntVT).getNode();
Chris Lattner6258fb22006-04-02 02:53:43 +00003669 SrcEltVT = IntVT;
3670 }
3671
3672 // Now we know the input is an integer vector. If the output is a FP type,
3673 // convert to integer first, then to FP of the right size.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003674 if (DstEltVT.isFloatingPoint()) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003675 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
Duncan Sands8eab8a22008-06-09 11:32:28 +00003676 MVT TmpVT = MVT::getIntegerVT(DstEltVT.getSizeInBits());
Gabor Greifba36cb52008-08-28 21:40:38 +00003677 SDNode *Tmp = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, TmpVT).getNode();
Chris Lattner6258fb22006-04-02 02:53:43 +00003678
3679 // Next, convert to FP elements of the same size.
Dan Gohman7f321562007-06-25 16:23:39 +00003680 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(Tmp, DstEltVT);
Chris Lattner6258fb22006-04-02 02:53:43 +00003681 }
3682
3683 // Okay, we know the src/dst types are both integers of differing types.
3684 // Handling growing first.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003685 assert(SrcEltVT.isInteger() && DstEltVT.isInteger());
Chris Lattner6258fb22006-04-02 02:53:43 +00003686 if (SrcBitSize < DstBitSize) {
3687 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
3688
Dan Gohman475871a2008-07-27 21:46:04 +00003689 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003690 for (unsigned i = 0, e = BV->getNumOperands(); i != e;
Chris Lattner6258fb22006-04-02 02:53:43 +00003691 i += NumInputsPerOutput) {
3692 bool isLE = TLI.isLittleEndian();
Dan Gohman220a8232008-03-03 23:51:38 +00003693 APInt NewBits = APInt(DstBitSize, 0);
Chris Lattner6258fb22006-04-02 02:53:43 +00003694 bool EltIsUndef = true;
3695 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
3696 // Shift the previously computed bits over.
3697 NewBits <<= SrcBitSize;
Dan Gohman475871a2008-07-27 21:46:04 +00003698 SDValue Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
Chris Lattner6258fb22006-04-02 02:53:43 +00003699 if (Op.getOpcode() == ISD::UNDEF) continue;
3700 EltIsUndef = false;
3701
Dan Gohman220a8232008-03-03 23:51:38 +00003702 NewBits |=
3703 APInt(cast<ConstantSDNode>(Op)->getAPIntValue()).zext(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00003704 }
3705
3706 if (EltIsUndef)
3707 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
3708 else
3709 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
3710 }
3711
Duncan Sands83ec4b62008-06-06 12:08:01 +00003712 MVT VT = MVT::getVectorVT(DstEltVT, Ops.size());
Dan Gohman7f321562007-06-25 16:23:39 +00003713 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003714 }
3715
3716 // Finally, this must be the case where we are shrinking elements: each input
3717 // turns into multiple outputs.
Evan Chengefec7512008-02-18 23:04:32 +00003718 bool isS2V = ISD::isScalarToVector(BV);
Chris Lattner6258fb22006-04-02 02:53:43 +00003719 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003720 MVT VT = MVT::getVectorVT(DstEltVT, NumOutputsPerInput*BV->getNumOperands());
Dan Gohman475871a2008-07-27 21:46:04 +00003721 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003722 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003723 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
3724 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
3725 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
3726 continue;
3727 }
Dan Gohman220a8232008-03-03 23:51:38 +00003728 APInt OpVal = cast<ConstantSDNode>(BV->getOperand(i))->getAPIntValue();
Chris Lattner6258fb22006-04-02 02:53:43 +00003729 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
Dan Gohman220a8232008-03-03 23:51:38 +00003730 APInt ThisVal = APInt(OpVal).trunc(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00003731 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
Dan Gohman220a8232008-03-03 23:51:38 +00003732 if (isS2V && i == 0 && j == 0 && APInt(ThisVal).zext(SrcBitSize) == OpVal)
Evan Chengefec7512008-02-18 23:04:32 +00003733 // Simply turn this into a SCALAR_TO_VECTOR of the new type.
3734 return DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Ops[0]);
Dan Gohman220a8232008-03-03 23:51:38 +00003735 OpVal = OpVal.lshr(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00003736 }
3737
3738 // For big endian targets, swap the order of the pieces of each element.
Duncan Sands0753fc12008-02-11 10:37:04 +00003739 if (TLI.isBigEndian())
Chris Lattner6258fb22006-04-02 02:53:43 +00003740 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
3741 }
Dan Gohman7f321562007-06-25 16:23:39 +00003742 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003743}
3744
3745
3746
Dan Gohman475871a2008-07-27 21:46:04 +00003747SDValue DAGCombiner::visitFADD(SDNode *N) {
3748 SDValue N0 = N->getOperand(0);
3749 SDValue N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003750 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3751 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003752 MVT VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00003753
Dan Gohman7f321562007-06-25 16:23:39 +00003754 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003755 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003756 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003757 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00003758 }
Dan Gohman7f321562007-06-25 16:23:39 +00003759
Nate Begemana0e221d2005-10-18 00:28:13 +00003760 // fold (fadd c1, c2) -> c1+c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003761 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003762 return DAG.getNode(ISD::FADD, VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003763 // canonicalize constant to RHS
3764 if (N0CFP && !N1CFP)
3765 return DAG.getNode(ISD::FADD, VT, N1, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00003766 // fold (A + (-B)) -> A-B
Chris Lattner0254e702008-02-26 07:04:54 +00003767 if (isNegatibleForFree(N1, AfterLegalize) == 2)
3768 return DAG.getNode(ISD::FSUB, VT, N0,
3769 GetNegatedExpression(N1, DAG, AfterLegalize));
Chris Lattner01b3d732005-09-28 22:28:18 +00003770 // fold ((-A) + B) -> B-A
Chris Lattner0254e702008-02-26 07:04:54 +00003771 if (isNegatibleForFree(N0, AfterLegalize) == 2)
3772 return DAG.getNode(ISD::FSUB, VT, N1,
3773 GetNegatedExpression(N0, DAG, AfterLegalize));
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003774
3775 // If allowed, fold (fadd (fadd x, c1), c2) -> (fadd x, (fadd c1, c2))
3776 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FADD &&
Gabor Greifba36cb52008-08-28 21:40:38 +00003777 N0.getNode()->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003778 return DAG.getNode(ISD::FADD, VT, N0.getOperand(0),
3779 DAG.getNode(ISD::FADD, VT, N0.getOperand(1), N1));
3780
Dan Gohman475871a2008-07-27 21:46:04 +00003781 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00003782}
3783
Dan Gohman475871a2008-07-27 21:46:04 +00003784SDValue DAGCombiner::visitFSUB(SDNode *N) {
3785 SDValue N0 = N->getOperand(0);
3786 SDValue N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003787 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3788 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003789 MVT VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00003790
Dan Gohman7f321562007-06-25 16:23:39 +00003791 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003792 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003793 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003794 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00003795 }
Dan Gohman7f321562007-06-25 16:23:39 +00003796
Nate Begemana0e221d2005-10-18 00:28:13 +00003797 // fold (fsub c1, c2) -> c1-c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003798 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003799 return DAG.getNode(ISD::FSUB, VT, N0, N1);
Dan Gohman23ff1822007-07-02 15:48:56 +00003800 // fold (0-B) -> -B
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003801 if (UnsafeFPMath && N0CFP && N0CFP->getValueAPF().isZero()) {
Chris Lattner0254e702008-02-26 07:04:54 +00003802 if (isNegatibleForFree(N1, AfterLegalize))
3803 return GetNegatedExpression(N1, DAG, AfterLegalize);
Dan Gohman23ff1822007-07-02 15:48:56 +00003804 return DAG.getNode(ISD::FNEG, VT, N1);
3805 }
Chris Lattner01b3d732005-09-28 22:28:18 +00003806 // fold (A-(-B)) -> A+B
Chris Lattner0254e702008-02-26 07:04:54 +00003807 if (isNegatibleForFree(N1, AfterLegalize))
3808 return DAG.getNode(ISD::FADD, VT, N0,
3809 GetNegatedExpression(N1, DAG, AfterLegalize));
Chris Lattner29446522007-05-14 22:04:50 +00003810
Dan Gohman475871a2008-07-27 21:46:04 +00003811 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00003812}
3813
Dan Gohman475871a2008-07-27 21:46:04 +00003814SDValue DAGCombiner::visitFMUL(SDNode *N) {
3815 SDValue N0 = N->getOperand(0);
3816 SDValue N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003817 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3818 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003819 MVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00003820
Dan Gohman7f321562007-06-25 16:23:39 +00003821 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003822 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003823 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003824 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00003825 }
Dan Gohman7f321562007-06-25 16:23:39 +00003826
Nate Begeman11af4ea2005-10-17 20:40:11 +00003827 // fold (fmul c1, c2) -> c1*c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003828 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003829 return DAG.getNode(ISD::FMUL, VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003830 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00003831 if (N0CFP && !N1CFP)
3832 return DAG.getNode(ISD::FMUL, VT, N1, N0);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003833 // fold (fmul X, 2.0) -> (fadd X, X)
3834 if (N1CFP && N1CFP->isExactlyValue(+2.0))
3835 return DAG.getNode(ISD::FADD, VT, N0, N0);
Chris Lattner29446522007-05-14 22:04:50 +00003836 // fold (fmul X, -1.0) -> (fneg X)
3837 if (N1CFP && N1CFP->isExactlyValue(-1.0))
3838 return DAG.getNode(ISD::FNEG, VT, N0);
3839
3840 // -X * -Y -> X*Y
Chris Lattner0254e702008-02-26 07:04:54 +00003841 if (char LHSNeg = isNegatibleForFree(N0, AfterLegalize)) {
3842 if (char RHSNeg = isNegatibleForFree(N1, AfterLegalize)) {
Chris Lattner29446522007-05-14 22:04:50 +00003843 // Both can be negated for free, check to see if at least one is cheaper
3844 // negated.
3845 if (LHSNeg == 2 || RHSNeg == 2)
Chris Lattner0254e702008-02-26 07:04:54 +00003846 return DAG.getNode(ISD::FMUL, VT,
3847 GetNegatedExpression(N0, DAG, AfterLegalize),
3848 GetNegatedExpression(N1, DAG, AfterLegalize));
Chris Lattner29446522007-05-14 22:04:50 +00003849 }
3850 }
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003851
3852 // If allowed, fold (fmul (fmul x, c1), c2) -> (fmul x, (fmul c1, c2))
3853 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FMUL &&
Gabor Greifba36cb52008-08-28 21:40:38 +00003854 N0.getNode()->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003855 return DAG.getNode(ISD::FMUL, VT, N0.getOperand(0),
3856 DAG.getNode(ISD::FMUL, VT, N0.getOperand(1), N1));
3857
Dan Gohman475871a2008-07-27 21:46:04 +00003858 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00003859}
3860
Dan Gohman475871a2008-07-27 21:46:04 +00003861SDValue DAGCombiner::visitFDIV(SDNode *N) {
3862 SDValue N0 = N->getOperand(0);
3863 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003864 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3865 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003866 MVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00003867
Dan Gohman7f321562007-06-25 16:23:39 +00003868 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003869 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003870 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003871 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00003872 }
Dan Gohman7f321562007-06-25 16:23:39 +00003873
Nate Begemana148d982006-01-18 22:35:16 +00003874 // fold (fdiv c1, c2) -> c1/c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003875 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003876 return DAG.getNode(ISD::FDIV, VT, N0, N1);
Chris Lattner29446522007-05-14 22:04:50 +00003877
3878
3879 // -X / -Y -> X*Y
Chris Lattner0254e702008-02-26 07:04:54 +00003880 if (char LHSNeg = isNegatibleForFree(N0, AfterLegalize)) {
3881 if (char RHSNeg = isNegatibleForFree(N1, AfterLegalize)) {
Chris Lattner29446522007-05-14 22:04:50 +00003882 // Both can be negated for free, check to see if at least one is cheaper
3883 // negated.
3884 if (LHSNeg == 2 || RHSNeg == 2)
Chris Lattner0254e702008-02-26 07:04:54 +00003885 return DAG.getNode(ISD::FDIV, VT,
3886 GetNegatedExpression(N0, DAG, AfterLegalize),
3887 GetNegatedExpression(N1, DAG, AfterLegalize));
Chris Lattner29446522007-05-14 22:04:50 +00003888 }
3889 }
3890
Dan Gohman475871a2008-07-27 21:46:04 +00003891 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00003892}
3893
Dan Gohman475871a2008-07-27 21:46:04 +00003894SDValue DAGCombiner::visitFREM(SDNode *N) {
3895 SDValue N0 = N->getOperand(0);
3896 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003897 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3898 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003899 MVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00003900
Nate Begemana148d982006-01-18 22:35:16 +00003901 // fold (frem c1, c2) -> fmod(c1,c2)
Dale Johannesendb44bf82007-10-16 23:38:29 +00003902 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003903 return DAG.getNode(ISD::FREM, VT, N0, N1);
Dan Gohman7f321562007-06-25 16:23:39 +00003904
Dan Gohman475871a2008-07-27 21:46:04 +00003905 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00003906}
3907
Dan Gohman475871a2008-07-27 21:46:04 +00003908SDValue DAGCombiner::visitFCOPYSIGN(SDNode *N) {
3909 SDValue N0 = N->getOperand(0);
3910 SDValue N1 = N->getOperand(1);
Chris Lattner12d83032006-03-05 05:30:57 +00003911 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3912 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003913 MVT VT = N->getValueType(0);
Chris Lattner12d83032006-03-05 05:30:57 +00003914
Dale Johannesendb44bf82007-10-16 23:38:29 +00003915 if (N0CFP && N1CFP && VT != MVT::ppcf128) // Constant fold
Chris Lattner12d83032006-03-05 05:30:57 +00003916 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1);
3917
3918 if (N1CFP) {
Dale Johannesene6c17422007-08-26 01:18:27 +00003919 const APFloat& V = N1CFP->getValueAPF();
Chris Lattner12d83032006-03-05 05:30:57 +00003920 // copysign(x, c1) -> fabs(x) iff ispos(c1)
3921 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
Dale Johannesen87503a62007-08-25 22:10:57 +00003922 if (!V.isNegative())
Chris Lattner12d83032006-03-05 05:30:57 +00003923 return DAG.getNode(ISD::FABS, VT, N0);
3924 else
3925 return DAG.getNode(ISD::FNEG, VT, DAG.getNode(ISD::FABS, VT, N0));
3926 }
3927
3928 // copysign(fabs(x), y) -> copysign(x, y)
3929 // copysign(fneg(x), y) -> copysign(x, y)
3930 // copysign(copysign(x,z), y) -> copysign(x, y)
3931 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
3932 N0.getOpcode() == ISD::FCOPYSIGN)
3933 return DAG.getNode(ISD::FCOPYSIGN, VT, N0.getOperand(0), N1);
3934
3935 // copysign(x, abs(y)) -> abs(x)
3936 if (N1.getOpcode() == ISD::FABS)
3937 return DAG.getNode(ISD::FABS, VT, N0);
3938
3939 // copysign(x, copysign(y,z)) -> copysign(x, z)
3940 if (N1.getOpcode() == ISD::FCOPYSIGN)
3941 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(1));
3942
3943 // copysign(x, fp_extend(y)) -> copysign(x, y)
3944 // copysign(x, fp_round(y)) -> copysign(x, y)
3945 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
3946 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(0));
3947
Dan Gohman475871a2008-07-27 21:46:04 +00003948 return SDValue();
Chris Lattner12d83032006-03-05 05:30:57 +00003949}
3950
3951
Chris Lattner01b3d732005-09-28 22:28:18 +00003952
Dan Gohman475871a2008-07-27 21:46:04 +00003953SDValue DAGCombiner::visitSINT_TO_FP(SDNode *N) {
3954 SDValue N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003955 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003956 MVT VT = N->getValueType(0);
Chris Lattnercda88752008-06-26 00:16:49 +00003957 MVT OpVT = N0.getValueType();
3958
Nate Begeman1d4d4142005-09-01 00:19:25 +00003959 // fold (sint_to_fp c1) -> c1fp
Chris Lattnercda88752008-06-26 00:16:49 +00003960 if (N0C && OpVT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003961 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
Chris Lattnercda88752008-06-26 00:16:49 +00003962
3963 // If the input is a legal type, and SINT_TO_FP is not legal on this target,
3964 // but UINT_TO_FP is legal on this target, try to convert.
Chris Lattnerf77e46b2008-06-26 17:16:00 +00003965 if (!TLI.isOperationLegal(ISD::SINT_TO_FP, OpVT) &&
Chris Lattnercda88752008-06-26 00:16:49 +00003966 TLI.isOperationLegal(ISD::UINT_TO_FP, OpVT)) {
3967 // If the sign bit is known to be zero, we can change this to UINT_TO_FP.
3968 if (DAG.SignBitIsZero(N0))
3969 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
3970 }
3971
3972
Dan Gohman475871a2008-07-27 21:46:04 +00003973 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003974}
3975
Dan Gohman475871a2008-07-27 21:46:04 +00003976SDValue DAGCombiner::visitUINT_TO_FP(SDNode *N) {
3977 SDValue N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003978 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003979 MVT VT = N->getValueType(0);
Chris Lattnercda88752008-06-26 00:16:49 +00003980 MVT OpVT = N0.getValueType();
Nate Begemana148d982006-01-18 22:35:16 +00003981
Nate Begeman1d4d4142005-09-01 00:19:25 +00003982 // fold (uint_to_fp c1) -> c1fp
Chris Lattnercda88752008-06-26 00:16:49 +00003983 if (N0C && OpVT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003984 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
Chris Lattnercda88752008-06-26 00:16:49 +00003985
3986 // If the input is a legal type, and UINT_TO_FP is not legal on this target,
3987 // but SINT_TO_FP is legal on this target, try to convert.
Chris Lattnerf77e46b2008-06-26 17:16:00 +00003988 if (!TLI.isOperationLegal(ISD::UINT_TO_FP, OpVT) &&
Chris Lattnercda88752008-06-26 00:16:49 +00003989 TLI.isOperationLegal(ISD::SINT_TO_FP, OpVT)) {
3990 // If the sign bit is known to be zero, we can change this to SINT_TO_FP.
3991 if (DAG.SignBitIsZero(N0))
3992 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
3993 }
3994
Dan Gohman475871a2008-07-27 21:46:04 +00003995 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003996}
3997
Dan Gohman475871a2008-07-27 21:46:04 +00003998SDValue DAGCombiner::visitFP_TO_SINT(SDNode *N) {
3999 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00004000 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004001 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004002
4003 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00004004 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00004005 return DAG.getNode(ISD::FP_TO_SINT, VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00004006 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004007}
4008
Dan Gohman475871a2008-07-27 21:46:04 +00004009SDValue DAGCombiner::visitFP_TO_UINT(SDNode *N) {
4010 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00004011 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004012 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004013
4014 // fold (fp_to_uint c1fp) -> c1
Dale Johannesendb44bf82007-10-16 23:38:29 +00004015 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00004016 return DAG.getNode(ISD::FP_TO_UINT, VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00004017 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004018}
4019
Dan Gohman475871a2008-07-27 21:46:04 +00004020SDValue DAGCombiner::visitFP_ROUND(SDNode *N) {
4021 SDValue N0 = N->getOperand(0);
4022 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00004023 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004024 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004025
4026 // fold (fp_round c1fp) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00004027 if (N0CFP && N0.getValueType() != MVT::ppcf128)
Chris Lattner0bd48932008-01-17 07:00:52 +00004028 return DAG.getNode(ISD::FP_ROUND, VT, N0, N1);
Chris Lattner79dbea52006-03-13 06:26:26 +00004029
4030 // fold (fp_round (fp_extend x)) -> x
4031 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
4032 return N0.getOperand(0);
4033
Chris Lattner0aa5e6f2008-01-24 06:45:35 +00004034 // fold (fp_round (fp_round x)) -> (fp_round x)
4035 if (N0.getOpcode() == ISD::FP_ROUND) {
4036 // This is a value preserving truncation if both round's are.
4037 bool IsTrunc = N->getConstantOperandVal(1) == 1 &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004038 N0.getNode()->getConstantOperandVal(1) == 1;
Chris Lattner0aa5e6f2008-01-24 06:45:35 +00004039 return DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0),
4040 DAG.getIntPtrConstant(IsTrunc));
4041 }
4042
Chris Lattner79dbea52006-03-13 06:26:26 +00004043 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
Gabor Greifba36cb52008-08-28 21:40:38 +00004044 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004045 SDValue Tmp = DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0), N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00004046 AddToWorkList(Tmp.getNode());
Chris Lattner79dbea52006-03-13 06:26:26 +00004047 return DAG.getNode(ISD::FCOPYSIGN, VT, Tmp, N0.getOperand(1));
4048 }
4049
Dan Gohman475871a2008-07-27 21:46:04 +00004050 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004051}
4052
Dan Gohman475871a2008-07-27 21:46:04 +00004053SDValue DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
4054 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004055 MVT VT = N->getValueType(0);
4056 MVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00004057 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004058
Nate Begeman1d4d4142005-09-01 00:19:25 +00004059 // fold (fp_round_inreg c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00004060 if (N0CFP) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00004061 SDValue Round = DAG.getConstantFP(*N0CFP->getConstantFPValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00004062 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004063 }
Dan Gohman475871a2008-07-27 21:46:04 +00004064 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004065}
4066
Dan Gohman475871a2008-07-27 21:46:04 +00004067SDValue DAGCombiner::visitFP_EXTEND(SDNode *N) {
4068 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00004069 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004070 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004071
Chris Lattner5938bef2007-12-29 06:55:23 +00004072 // If this is fp_round(fpextend), don't fold it, allow ourselves to be folded.
Roman Levenstein9cac5252008-04-16 16:15:27 +00004073 if (N->hasOneUse() &&
Dan Gohman475871a2008-07-27 21:46:04 +00004074 N->use_begin().getUse().getSDValue().getOpcode() == ISD::FP_ROUND)
4075 return SDValue();
Chris Lattner0bd48932008-01-17 07:00:52 +00004076
Nate Begeman1d4d4142005-09-01 00:19:25 +00004077 // fold (fp_extend c1fp) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00004078 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00004079 return DAG.getNode(ISD::FP_EXTEND, VT, N0);
Chris Lattner0bd48932008-01-17 07:00:52 +00004080
4081 // Turn fp_extend(fp_round(X, 1)) -> x since the fp_round doesn't affect the
4082 // value of X.
Gabor Greif12632d22008-08-30 19:29:20 +00004083 if (N0.getOpcode() == ISD::FP_ROUND
4084 && N0.getNode()->getConstantOperandVal(1) == 1) {
Dan Gohman475871a2008-07-27 21:46:04 +00004085 SDValue In = N0.getOperand(0);
Chris Lattner0bd48932008-01-17 07:00:52 +00004086 if (In.getValueType() == VT) return In;
Duncan Sands8e4eb092008-06-08 20:54:56 +00004087 if (VT.bitsLT(In.getValueType()))
Chris Lattner0bd48932008-01-17 07:00:52 +00004088 return DAG.getNode(ISD::FP_ROUND, VT, In, N0.getOperand(1));
4089 return DAG.getNode(ISD::FP_EXTEND, VT, In);
4090 }
4091
4092 // fold (fpext (load x)) -> (fpext (fptrunc (extload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00004093 if (ISD::isNON_EXTLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004094 ((!AfterLegalize && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00004095 TLI.isLoadExtLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00004096 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman475871a2008-07-27 21:46:04 +00004097 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
Evan Cheng466685d2006-10-09 20:57:25 +00004098 LN0->getBasePtr(), LN0->getSrcValue(),
4099 LN0->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00004100 N0.getValueType(),
4101 LN0->isVolatile(),
4102 LN0->getAlignment());
Chris Lattnere564dbb2006-05-05 21:34:35 +00004103 CombineTo(N, ExtLoad);
Gabor Greif12632d22008-08-30 19:29:20 +00004104 CombineTo(N0.getNode(), DAG.getNode(ISD::FP_ROUND, N0.getValueType(),
4105 ExtLoad, DAG.getIntPtrConstant(1)),
Chris Lattnere564dbb2006-05-05 21:34:35 +00004106 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004107 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnere564dbb2006-05-05 21:34:35 +00004108 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004109
Dan Gohman475871a2008-07-27 21:46:04 +00004110 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004111}
4112
Dan Gohman475871a2008-07-27 21:46:04 +00004113SDValue DAGCombiner::visitFNEG(SDNode *N) {
4114 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00004115
Chris Lattner0254e702008-02-26 07:04:54 +00004116 if (isNegatibleForFree(N0, AfterLegalize))
4117 return GetNegatedExpression(N0, DAG, AfterLegalize);
Dan Gohman23ff1822007-07-02 15:48:56 +00004118
Chris Lattner3bd39d42008-01-27 17:42:27 +00004119 // Transform fneg(bitconvert(x)) -> bitconvert(x^sign) to avoid loading
4120 // constant pool values.
Gabor Greifba36cb52008-08-28 21:40:38 +00004121 if (N0.getOpcode() == ISD::BIT_CONVERT && N0.getNode()->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00004122 N0.getOperand(0).getValueType().isInteger() &&
4123 !N0.getOperand(0).getValueType().isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004124 SDValue Int = N0.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004125 MVT IntVT = Int.getValueType();
4126 if (IntVT.isInteger() && !IntVT.isVector()) {
Chris Lattner3bd39d42008-01-27 17:42:27 +00004127 Int = DAG.getNode(ISD::XOR, IntVT, Int,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004128 DAG.getConstant(IntVT.getIntegerVTSignBit(), IntVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00004129 AddToWorkList(Int.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00004130 return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Int);
4131 }
4132 }
4133
Dan Gohman475871a2008-07-27 21:46:04 +00004134 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004135}
4136
Dan Gohman475871a2008-07-27 21:46:04 +00004137SDValue DAGCombiner::visitFABS(SDNode *N) {
4138 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00004139 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004140 MVT VT = N->getValueType(0);
Nate Begemana148d982006-01-18 22:35:16 +00004141
Nate Begeman1d4d4142005-09-01 00:19:25 +00004142 // fold (fabs c1) -> fabs(c1)
Dale Johannesendb44bf82007-10-16 23:38:29 +00004143 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00004144 return DAG.getNode(ISD::FABS, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004145 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00004146 if (N0.getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00004147 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004148 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00004149 // fold (fabs (fcopysign x, y)) -> (fabs x)
4150 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
4151 return DAG.getNode(ISD::FABS, VT, N0.getOperand(0));
4152
Chris Lattner3bd39d42008-01-27 17:42:27 +00004153 // Transform fabs(bitconvert(x)) -> bitconvert(x&~sign) to avoid loading
4154 // constant pool values.
Gabor Greifba36cb52008-08-28 21:40:38 +00004155 if (N0.getOpcode() == ISD::BIT_CONVERT && N0.getNode()->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00004156 N0.getOperand(0).getValueType().isInteger() &&
4157 !N0.getOperand(0).getValueType().isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004158 SDValue Int = N0.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004159 MVT IntVT = Int.getValueType();
4160 if (IntVT.isInteger() && !IntVT.isVector()) {
Chris Lattner3bd39d42008-01-27 17:42:27 +00004161 Int = DAG.getNode(ISD::AND, IntVT, Int,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004162 DAG.getConstant(~IntVT.getIntegerVTSignBit(), IntVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00004163 AddToWorkList(Int.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00004164 return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Int);
4165 }
4166 }
4167
Dan Gohman475871a2008-07-27 21:46:04 +00004168 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004169}
4170
Dan Gohman475871a2008-07-27 21:46:04 +00004171SDValue DAGCombiner::visitBRCOND(SDNode *N) {
4172 SDValue Chain = N->getOperand(0);
4173 SDValue N1 = N->getOperand(1);
4174 SDValue N2 = N->getOperand(2);
Nate Begeman44728a72005-09-19 22:34:01 +00004175 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
4176
4177 // never taken branch, fold to chain
4178 if (N1C && N1C->isNullValue())
4179 return Chain;
4180 // unconditional branch
Dan Gohman002e5d02008-03-13 22:13:53 +00004181 if (N1C && N1C->getAPIntValue() == 1)
Nate Begeman44728a72005-09-19 22:34:01 +00004182 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
Nate Begeman750ac1b2006-02-01 07:19:44 +00004183 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
4184 // on the target.
4185 if (N1.getOpcode() == ISD::SETCC &&
4186 TLI.isOperationLegal(ISD::BR_CC, MVT::Other)) {
4187 return DAG.getNode(ISD::BR_CC, MVT::Other, Chain, N1.getOperand(2),
4188 N1.getOperand(0), N1.getOperand(1), N2);
4189 }
Dan Gohman475871a2008-07-27 21:46:04 +00004190 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00004191}
4192
Chris Lattner3ea0b472005-10-05 06:47:48 +00004193// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
4194//
Dan Gohman475871a2008-07-27 21:46:04 +00004195SDValue DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00004196 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004197 SDValue CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
Chris Lattner3ea0b472005-10-05 06:47:48 +00004198
Duncan Sands8eab8a22008-06-09 11:32:28 +00004199 // Use SimplifySetCC to simplify SETCC's.
Dan Gohman475871a2008-07-27 21:46:04 +00004200 SDValue Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false);
Gabor Greifba36cb52008-08-28 21:40:38 +00004201 if (Simp.getNode()) AddToWorkList(Simp.getNode());
Chris Lattner30f73e72006-10-14 03:52:46 +00004202
Gabor Greifba36cb52008-08-28 21:40:38 +00004203 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.getNode());
Nate Begemane17daeb2005-10-05 21:43:42 +00004204
4205 // fold br_cc true, dest -> br dest (unconditional branch)
Dan Gohman002e5d02008-03-13 22:13:53 +00004206 if (SCCC && !SCCC->isNullValue())
Nate Begemane17daeb2005-10-05 21:43:42 +00004207 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
4208 N->getOperand(4));
4209 // fold br_cc false, dest -> unconditional fall through
4210 if (SCCC && SCCC->isNullValue())
4211 return N->getOperand(0);
Chris Lattner30f73e72006-10-14 03:52:46 +00004212
Nate Begemane17daeb2005-10-05 21:43:42 +00004213 // fold to a simpler setcc
Gabor Greifba36cb52008-08-28 21:40:38 +00004214 if (Simp.getNode() && Simp.getOpcode() == ISD::SETCC)
Nate Begemane17daeb2005-10-05 21:43:42 +00004215 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
4216 Simp.getOperand(2), Simp.getOperand(0),
4217 Simp.getOperand(1), N->getOperand(4));
Dan Gohman475871a2008-07-27 21:46:04 +00004218 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00004219}
4220
Chris Lattner448f2192006-11-11 00:39:41 +00004221
Duncan Sandsec87aa82008-06-15 20:12:31 +00004222/// CombineToPreIndexedLoadStore - Try turning a load / store into a
4223/// pre-indexed load / store when the base pointer is an add or subtract
Chris Lattner448f2192006-11-11 00:39:41 +00004224/// and it has other uses besides the load / store. After the
4225/// transformation, the new indexed load / store has effectively folded
4226/// the add / subtract in and all of its other uses are redirected to the
4227/// new load / store.
4228bool DAGCombiner::CombineToPreIndexedLoadStore(SDNode *N) {
4229 if (!AfterLegalize)
4230 return false;
4231
4232 bool isLoad = true;
Dan Gohman475871a2008-07-27 21:46:04 +00004233 SDValue Ptr;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004234 MVT VT;
Chris Lattner448f2192006-11-11 00:39:41 +00004235 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004236 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004237 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004238 VT = LD->getMemoryVT();
Evan Cheng83060c52007-03-07 08:07:03 +00004239 if (!TLI.isIndexedLoadLegal(ISD::PRE_INC, VT) &&
Chris Lattner448f2192006-11-11 00:39:41 +00004240 !TLI.isIndexedLoadLegal(ISD::PRE_DEC, VT))
4241 return false;
4242 Ptr = LD->getBasePtr();
4243 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004244 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004245 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004246 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00004247 if (!TLI.isIndexedStoreLegal(ISD::PRE_INC, VT) &&
4248 !TLI.isIndexedStoreLegal(ISD::PRE_DEC, VT))
4249 return false;
4250 Ptr = ST->getBasePtr();
4251 isLoad = false;
4252 } else
4253 return false;
4254
Chris Lattner9f1794e2006-11-11 00:56:29 +00004255 // If the pointer is not an add/sub, or if it doesn't have multiple uses, bail
4256 // out. There is no reason to make this a preinc/predec.
4257 if ((Ptr.getOpcode() != ISD::ADD && Ptr.getOpcode() != ISD::SUB) ||
Gabor Greifba36cb52008-08-28 21:40:38 +00004258 Ptr.getNode()->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00004259 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00004260
Chris Lattner9f1794e2006-11-11 00:56:29 +00004261 // Ask the target to do addressing mode selection.
Dan Gohman475871a2008-07-27 21:46:04 +00004262 SDValue BasePtr;
4263 SDValue Offset;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004264 ISD::MemIndexedMode AM = ISD::UNINDEXED;
4265 if (!TLI.getPreIndexedAddressParts(N, BasePtr, Offset, AM, DAG))
4266 return false;
Evan Chenga7d4a042007-05-03 23:52:19 +00004267 // Don't create a indexed load / store with zero offset.
4268 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00004269 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Chenga7d4a042007-05-03 23:52:19 +00004270 return false;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004271
Chris Lattner41e53fd2006-11-11 01:00:15 +00004272 // Try turning it into a pre-indexed load / store except when:
Evan Chengc843abe2007-05-24 02:35:39 +00004273 // 1) The new base ptr is a frame index.
4274 // 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 +00004275 // predecessor of the value being stored.
Evan Chengc843abe2007-05-24 02:35:39 +00004276 // 3) Another use of old base ptr is a predecessor of N. If ptr is folded
Chris Lattner9f1794e2006-11-11 00:56:29 +00004277 // that would create a cycle.
Evan Chengc843abe2007-05-24 02:35:39 +00004278 // 4) All uses are load / store ops that use it as old base ptr.
Chris Lattner448f2192006-11-11 00:39:41 +00004279
Chris Lattner41e53fd2006-11-11 01:00:15 +00004280 // Check #1. Preinc'ing a frame index would require copying the stack pointer
4281 // (plus the implicit offset) to a register to preinc anyway.
4282 if (isa<FrameIndexSDNode>(BasePtr))
4283 return false;
4284
4285 // Check #2.
Chris Lattner9f1794e2006-11-11 00:56:29 +00004286 if (!isLoad) {
Dan Gohman475871a2008-07-27 21:46:04 +00004287 SDValue Val = cast<StoreSDNode>(N)->getValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00004288 if (Val == BasePtr || BasePtr.getNode()->isPredecessorOf(Val.getNode()))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004289 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00004290 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004291
Evan Chengc843abe2007-05-24 02:35:39 +00004292 // Now check for #3 and #4.
Chris Lattner9f1794e2006-11-11 00:56:29 +00004293 bool RealUse = false;
Gabor Greifba36cb52008-08-28 21:40:38 +00004294 for (SDNode::use_iterator I = Ptr.getNode()->use_begin(),
4295 E = Ptr.getNode()->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00004296 SDNode *Use = *I;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004297 if (Use == N)
4298 continue;
Evan Cheng917be682008-03-04 00:41:45 +00004299 if (Use->isPredecessorOf(N))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004300 return false;
4301
4302 if (!((Use->getOpcode() == ISD::LOAD &&
4303 cast<LoadSDNode>(Use)->getBasePtr() == Ptr) ||
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00004304 (Use->getOpcode() == ISD::STORE &&
4305 cast<StoreSDNode>(Use)->getBasePtr() == Ptr)))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004306 RealUse = true;
4307 }
4308 if (!RealUse)
4309 return false;
4310
Dan Gohman475871a2008-07-27 21:46:04 +00004311 SDValue Result;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004312 if (isLoad)
Dan Gohman475871a2008-07-27 21:46:04 +00004313 Result = DAG.getIndexedLoad(SDValue(N,0), BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004314 else
Dan Gohman475871a2008-07-27 21:46:04 +00004315 Result = DAG.getIndexedStore(SDValue(N,0), BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004316 ++PreIndexedNodes;
4317 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +00004318 DOUT << "\nReplacing.4 "; DEBUG(N->dump(&DAG));
Gabor Greifba36cb52008-08-28 21:40:38 +00004319 DOUT << "\nWith: "; DEBUG(Result.getNode()->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +00004320 DOUT << '\n';
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004321 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004322 if (isLoad) {
Dan Gohman475871a2008-07-27 21:46:04 +00004323 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004324 &DeadNodes);
Dan Gohman475871a2008-07-27 21:46:04 +00004325 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004326 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004327 } else {
Dan Gohman475871a2008-07-27 21:46:04 +00004328 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004329 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004330 }
4331
Chris Lattner9f1794e2006-11-11 00:56:29 +00004332 // Finally, since the node is now dead, remove it from the graph.
4333 DAG.DeleteNode(N);
4334
4335 // Replace the uses of Ptr with uses of the updated base value.
4336 DAG.ReplaceAllUsesOfValueWith(Ptr, Result.getValue(isLoad ? 1 : 0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004337 &DeadNodes);
Gabor Greifba36cb52008-08-28 21:40:38 +00004338 removeFromWorkList(Ptr.getNode());
4339 DAG.DeleteNode(Ptr.getNode());
Chris Lattner9f1794e2006-11-11 00:56:29 +00004340
4341 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00004342}
4343
Duncan Sandsec87aa82008-06-15 20:12:31 +00004344/// CombineToPostIndexedLoadStore - Try to combine a load / store with a
Chris Lattner448f2192006-11-11 00:39:41 +00004345/// add / sub of the base pointer node into a post-indexed load / store.
4346/// The transformation folded the add / subtract into the new indexed
4347/// load / store effectively and all of its uses are redirected to the
4348/// new load / store.
4349bool DAGCombiner::CombineToPostIndexedLoadStore(SDNode *N) {
4350 if (!AfterLegalize)
4351 return false;
4352
4353 bool isLoad = true;
Dan Gohman475871a2008-07-27 21:46:04 +00004354 SDValue Ptr;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004355 MVT VT;
Chris Lattner448f2192006-11-11 00:39:41 +00004356 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004357 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004358 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004359 VT = LD->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00004360 if (!TLI.isIndexedLoadLegal(ISD::POST_INC, VT) &&
4361 !TLI.isIndexedLoadLegal(ISD::POST_DEC, VT))
4362 return false;
4363 Ptr = LD->getBasePtr();
4364 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004365 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004366 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004367 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00004368 if (!TLI.isIndexedStoreLegal(ISD::POST_INC, VT) &&
4369 !TLI.isIndexedStoreLegal(ISD::POST_DEC, VT))
4370 return false;
4371 Ptr = ST->getBasePtr();
4372 isLoad = false;
4373 } else
4374 return false;
4375
Gabor Greifba36cb52008-08-28 21:40:38 +00004376 if (Ptr.getNode()->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00004377 return false;
4378
Gabor Greifba36cb52008-08-28 21:40:38 +00004379 for (SDNode::use_iterator I = Ptr.getNode()->use_begin(),
4380 E = Ptr.getNode()->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00004381 SDNode *Op = *I;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004382 if (Op == N ||
4383 (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB))
4384 continue;
4385
Dan Gohman475871a2008-07-27 21:46:04 +00004386 SDValue BasePtr;
4387 SDValue Offset;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004388 ISD::MemIndexedMode AM = ISD::UNINDEXED;
4389 if (TLI.getPostIndexedAddressParts(N, Op, BasePtr, Offset, AM, DAG)) {
4390 if (Ptr == Offset)
4391 std::swap(BasePtr, Offset);
4392 if (Ptr != BasePtr)
Chris Lattner448f2192006-11-11 00:39:41 +00004393 continue;
Evan Chenga7d4a042007-05-03 23:52:19 +00004394 // Don't create a indexed load / store with zero offset.
4395 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00004396 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Chenga7d4a042007-05-03 23:52:19 +00004397 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00004398
Chris Lattner9f1794e2006-11-11 00:56:29 +00004399 // Try turning it into a post-indexed load / store except when
4400 // 1) All uses are load / store ops that use it as base ptr.
4401 // 2) Op must be independent of N, i.e. Op is neither a predecessor
4402 // nor a successor of N. Otherwise, if Op is folded that would
4403 // create a cycle.
4404
4405 // Check for #1.
4406 bool TryNext = false;
Gabor Greifba36cb52008-08-28 21:40:38 +00004407 for (SDNode::use_iterator II = BasePtr.getNode()->use_begin(),
4408 EE = BasePtr.getNode()->use_end(); II != EE; ++II) {
Dan Gohman89684502008-07-27 20:43:25 +00004409 SDNode *Use = *II;
Gabor Greifba36cb52008-08-28 21:40:38 +00004410 if (Use == Ptr.getNode())
Chris Lattner448f2192006-11-11 00:39:41 +00004411 continue;
4412
Chris Lattner9f1794e2006-11-11 00:56:29 +00004413 // If all the uses are load / store addresses, then don't do the
4414 // transformation.
4415 if (Use->getOpcode() == ISD::ADD || Use->getOpcode() == ISD::SUB){
4416 bool RealUse = false;
4417 for (SDNode::use_iterator III = Use->use_begin(),
4418 EEE = Use->use_end(); III != EEE; ++III) {
Dan Gohman89684502008-07-27 20:43:25 +00004419 SDNode *UseUse = *III;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004420 if (!((UseUse->getOpcode() == ISD::LOAD &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004421 cast<LoadSDNode>(UseUse)->getBasePtr().getNode() == Use) ||
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00004422 (UseUse->getOpcode() == ISD::STORE &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004423 cast<StoreSDNode>(UseUse)->getBasePtr().getNode() == Use)))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004424 RealUse = true;
4425 }
Chris Lattner448f2192006-11-11 00:39:41 +00004426
Chris Lattner9f1794e2006-11-11 00:56:29 +00004427 if (!RealUse) {
4428 TryNext = true;
4429 break;
Chris Lattner448f2192006-11-11 00:39:41 +00004430 }
4431 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004432 }
4433 if (TryNext)
4434 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00004435
Chris Lattner9f1794e2006-11-11 00:56:29 +00004436 // Check for #2
Evan Cheng917be682008-03-04 00:41:45 +00004437 if (!Op->isPredecessorOf(N) && !N->isPredecessorOf(Op)) {
Dan Gohman475871a2008-07-27 21:46:04 +00004438 SDValue Result = isLoad
4439 ? DAG.getIndexedLoad(SDValue(N,0), BasePtr, Offset, AM)
4440 : DAG.getIndexedStore(SDValue(N,0), BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004441 ++PostIndexedNodes;
4442 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +00004443 DOUT << "\nReplacing.5 "; DEBUG(N->dump(&DAG));
Gabor Greifba36cb52008-08-28 21:40:38 +00004444 DOUT << "\nWith: "; DEBUG(Result.getNode()->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +00004445 DOUT << '\n';
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004446 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004447 if (isLoad) {
Dan Gohman475871a2008-07-27 21:46:04 +00004448 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004449 &DeadNodes);
Dan Gohman475871a2008-07-27 21:46:04 +00004450 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004451 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004452 } else {
Dan Gohman475871a2008-07-27 21:46:04 +00004453 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004454 &DeadNodes);
Chris Lattner448f2192006-11-11 00:39:41 +00004455 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004456
Chris Lattner9f1794e2006-11-11 00:56:29 +00004457 // Finally, since the node is now dead, remove it from the graph.
4458 DAG.DeleteNode(N);
4459
4460 // Replace the uses of Use with uses of the updated base value.
Dan Gohman475871a2008-07-27 21:46:04 +00004461 DAG.ReplaceAllUsesOfValueWith(SDValue(Op, 0),
Chris Lattner9f1794e2006-11-11 00:56:29 +00004462 Result.getValue(isLoad ? 1 : 0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004463 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004464 removeFromWorkList(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004465 DAG.DeleteNode(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004466 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00004467 }
4468 }
4469 }
4470 return false;
4471}
4472
Chris Lattner00161a62008-01-25 07:20:16 +00004473/// InferAlignment - If we can infer some alignment information from this
4474/// pointer, return it.
Dan Gohman475871a2008-07-27 21:46:04 +00004475static unsigned InferAlignment(SDValue Ptr, SelectionDAG &DAG) {
Chris Lattner00161a62008-01-25 07:20:16 +00004476 // If this is a direct reference to a stack slot, use information about the
4477 // stack slot's alignment.
Chris Lattner1329cb82008-01-26 19:45:50 +00004478 int FrameIdx = 1 << 31;
4479 int64_t FrameOffset = 0;
Chris Lattner00161a62008-01-25 07:20:16 +00004480 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
Chris Lattner1329cb82008-01-26 19:45:50 +00004481 FrameIdx = FI->getIndex();
4482 } else if (Ptr.getOpcode() == ISD::ADD &&
4483 isa<ConstantSDNode>(Ptr.getOperand(1)) &&
4484 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4485 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4486 FrameOffset = Ptr.getConstantOperandVal(1);
Chris Lattner00161a62008-01-25 07:20:16 +00004487 }
Chris Lattner1329cb82008-01-26 19:45:50 +00004488
4489 if (FrameIdx != (1 << 31)) {
4490 // FIXME: Handle FI+CST.
4491 const MachineFrameInfo &MFI = *DAG.getMachineFunction().getFrameInfo();
4492 if (MFI.isFixedObjectIndex(FrameIdx)) {
Dan Gohman8cea8ff2008-08-11 18:27:03 +00004493 int64_t ObjectOffset = MFI.getObjectOffset(FrameIdx) + FrameOffset;
Chris Lattner1329cb82008-01-26 19:45:50 +00004494
4495 // The alignment of the frame index can be determined from its offset from
4496 // the incoming frame position. If the frame object is at offset 32 and
4497 // the stack is guaranteed to be 16-byte aligned, then we know that the
4498 // object is 16-byte aligned.
4499 unsigned StackAlign = DAG.getTarget().getFrameInfo()->getStackAlignment();
4500 unsigned Align = MinAlign(ObjectOffset, StackAlign);
4501
4502 // Finally, the frame object itself may have a known alignment. Factor
4503 // the alignment + offset into a new alignment. For example, if we know
4504 // the FI is 8 byte aligned, but the pointer is 4 off, we really have a
4505 // 4-byte alignment of the resultant pointer. Likewise align 4 + 4-byte
4506 // offset = 4-byte alignment, align 4 + 1-byte offset = align 1, etc.
4507 unsigned FIInfoAlign = MinAlign(MFI.getObjectAlignment(FrameIdx),
4508 FrameOffset);
4509 return std::max(Align, FIInfoAlign);
4510 }
4511 }
Chris Lattner00161a62008-01-25 07:20:16 +00004512
4513 return 0;
4514}
Chris Lattner448f2192006-11-11 00:39:41 +00004515
Dan Gohman475871a2008-07-27 21:46:04 +00004516SDValue DAGCombiner::visitLOAD(SDNode *N) {
Evan Cheng466685d2006-10-09 20:57:25 +00004517 LoadSDNode *LD = cast<LoadSDNode>(N);
Dan Gohman475871a2008-07-27 21:46:04 +00004518 SDValue Chain = LD->getChain();
4519 SDValue Ptr = LD->getBasePtr();
Chris Lattner00161a62008-01-25 07:20:16 +00004520
4521 // Try to infer better alignment information than the load already has.
Dan Gohmana2676512008-08-20 16:30:28 +00004522 if (!Fast && LD->isUnindexed()) {
Chris Lattner00161a62008-01-25 07:20:16 +00004523 if (unsigned Align = InferAlignment(Ptr, DAG)) {
4524 if (Align > LD->getAlignment())
4525 return DAG.getExtLoad(LD->getExtensionType(), LD->getValueType(0),
4526 Chain, Ptr, LD->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004527 LD->getSrcValueOffset(), LD->getMemoryVT(),
Chris Lattner00161a62008-01-25 07:20:16 +00004528 LD->isVolatile(), Align);
4529 }
4530 }
4531
Evan Cheng45a7ca92007-05-01 00:38:21 +00004532
4533 // If load is not volatile and there are no uses of the loaded value (and
4534 // the updated indexed value in case of indexed loads), change uses of the
4535 // chain value into uses of the chain input (i.e. delete the dead load).
4536 if (!LD->isVolatile()) {
Evan Cheng498f5592007-05-01 08:53:39 +00004537 if (N->getValueType(1) == MVT::Other) {
4538 // Unindexed loads.
Evan Cheng02c42852008-01-16 23:11:54 +00004539 if (N->hasNUsesOfValue(0, 0)) {
4540 // It's not safe to use the two value CombineTo variant here. e.g.
4541 // v1, chain2 = load chain1, loc
4542 // v2, chain3 = load chain2, loc
4543 // v3 = add v2, c
Chris Lattner125991a2008-01-24 07:57:06 +00004544 // Now we replace use of chain2 with chain1. This makes the second load
4545 // isomorphic to the one we are deleting, and thus makes this load live.
Evan Cheng02c42852008-01-16 23:11:54 +00004546 DOUT << "\nReplacing.6 "; DEBUG(N->dump(&DAG));
Gabor Greifba36cb52008-08-28 21:40:38 +00004547 DOUT << "\nWith chain: "; DEBUG(Chain.getNode()->dump(&DAG));
Chris Lattner125991a2008-01-24 07:57:06 +00004548 DOUT << "\n";
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004549 WorkListRemover DeadNodes(*this);
Dan Gohman475871a2008-07-27 21:46:04 +00004550 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Chain, &DeadNodes);
Chris Lattner125991a2008-01-24 07:57:06 +00004551 if (N->use_empty()) {
4552 removeFromWorkList(N);
4553 DAG.DeleteNode(N);
4554 }
Dan Gohman475871a2008-07-27 21:46:04 +00004555 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng02c42852008-01-16 23:11:54 +00004556 }
Evan Cheng498f5592007-05-01 08:53:39 +00004557 } else {
4558 // Indexed loads.
4559 assert(N->getValueType(2) == MVT::Other && "Malformed indexed loads?");
4560 if (N->hasNUsesOfValue(0, 0) && N->hasNUsesOfValue(0, 1)) {
Dan Gohman475871a2008-07-27 21:46:04 +00004561 SDValue Undef = DAG.getNode(ISD::UNDEF, N->getValueType(0));
Evan Cheng02c42852008-01-16 23:11:54 +00004562 DOUT << "\nReplacing.6 "; DEBUG(N->dump(&DAG));
Gabor Greifba36cb52008-08-28 21:40:38 +00004563 DOUT << "\nWith: "; DEBUG(Undef.getNode()->dump(&DAG));
Evan Cheng02c42852008-01-16 23:11:54 +00004564 DOUT << " and 2 other values\n";
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004565 WorkListRemover DeadNodes(*this);
Dan Gohman475871a2008-07-27 21:46:04 +00004566 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Undef, &DeadNodes);
4567 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1),
Chris Lattner4626b252008-01-17 07:20:38 +00004568 DAG.getNode(ISD::UNDEF, N->getValueType(1)),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004569 &DeadNodes);
Dan Gohman475871a2008-07-27 21:46:04 +00004570 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 2), Chain, &DeadNodes);
Evan Cheng02c42852008-01-16 23:11:54 +00004571 removeFromWorkList(N);
Evan Cheng02c42852008-01-16 23:11:54 +00004572 DAG.DeleteNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +00004573 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng45a7ca92007-05-01 00:38:21 +00004574 }
Evan Cheng45a7ca92007-05-01 00:38:21 +00004575 }
4576 }
Chris Lattner01a22022005-10-10 22:04:48 +00004577
4578 // If this load is directly stored, replace the load value with the stored
4579 // value.
4580 // TODO: Handle store large -> read small portion.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004581 // TODO: Handle TRUNCSTORE/LOADEXT
Dan Gohmanb061c4b2008-03-31 20:32:52 +00004582 if (LD->getExtensionType() == ISD::NON_EXTLOAD &&
4583 !LD->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004584 if (ISD::isNON_TRUNCStore(Chain.getNode())) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00004585 StoreSDNode *PrevST = cast<StoreSDNode>(Chain);
4586 if (PrevST->getBasePtr() == Ptr &&
4587 PrevST->getValue().getValueType() == N->getValueType(0))
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004588 return CombineTo(N, Chain.getOperand(1), Chain);
Evan Cheng8b2794a2006-10-13 21:14:26 +00004589 }
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004590 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00004591
Jim Laskey7ca56af2006-10-11 13:47:09 +00004592 if (CombinerAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00004593 // Walk up chain skipping non-aliasing memory nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00004594 SDValue BetterChain = FindBetterChain(N, Chain);
Jim Laskey279f0532006-09-25 16:29:54 +00004595
Jim Laskey6ff23e52006-10-04 16:53:27 +00004596 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00004597 if (Chain != BetterChain) {
Dan Gohman475871a2008-07-27 21:46:04 +00004598 SDValue ReplLoad;
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004599
Jim Laskey279f0532006-09-25 16:29:54 +00004600 // Replace the chain to void dependency.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004601 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
4602 ReplLoad = DAG.getLoad(N->getValueType(0), BetterChain, Ptr,
Duncan Sandsdc846502007-10-28 12:59:45 +00004603 LD->getSrcValue(), LD->getSrcValueOffset(),
4604 LD->isVolatile(), LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004605 } else {
4606 ReplLoad = DAG.getExtLoad(LD->getExtensionType(),
4607 LD->getValueType(0),
4608 BetterChain, Ptr, LD->getSrcValue(),
4609 LD->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004610 LD->getMemoryVT(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00004611 LD->isVolatile(),
4612 LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004613 }
Jim Laskey279f0532006-09-25 16:29:54 +00004614
Jim Laskey6ff23e52006-10-04 16:53:27 +00004615 // Create token factor to keep old chain connected.
Dan Gohman475871a2008-07-27 21:46:04 +00004616 SDValue Token = DAG.getNode(ISD::TokenFactor, MVT::Other,
Jim Laskey288af5e2006-09-25 19:32:58 +00004617 Chain, ReplLoad.getValue(1));
Jim Laskey6ff23e52006-10-04 16:53:27 +00004618
Jim Laskey274062c2006-10-13 23:32:28 +00004619 // Replace uses with load result and token factor. Don't add users
4620 // to work list.
4621 return CombineTo(N, ReplLoad.getValue(0), Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00004622 }
4623 }
4624
Evan Cheng7fc033a2006-11-03 03:06:21 +00004625 // Try transforming N to an indexed load.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00004626 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Dan Gohman475871a2008-07-27 21:46:04 +00004627 return SDValue(N, 0);
Evan Cheng7fc033a2006-11-03 03:06:21 +00004628
Dan Gohman475871a2008-07-27 21:46:04 +00004629 return SDValue();
Chris Lattner01a22022005-10-10 22:04:48 +00004630}
4631
Chris Lattner07649d92008-01-08 23:08:06 +00004632
Dan Gohman475871a2008-07-27 21:46:04 +00004633SDValue DAGCombiner::visitSTORE(SDNode *N) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00004634 StoreSDNode *ST = cast<StoreSDNode>(N);
Dan Gohman475871a2008-07-27 21:46:04 +00004635 SDValue Chain = ST->getChain();
4636 SDValue Value = ST->getValue();
4637 SDValue Ptr = ST->getBasePtr();
Jim Laskey7aed46c2006-10-11 18:55:16 +00004638
Chris Lattner00161a62008-01-25 07:20:16 +00004639 // Try to infer better alignment information than the store already has.
Dan Gohmana2676512008-08-20 16:30:28 +00004640 if (!Fast && ST->isUnindexed()) {
Chris Lattner00161a62008-01-25 07:20:16 +00004641 if (unsigned Align = InferAlignment(Ptr, DAG)) {
4642 if (Align > ST->getAlignment())
4643 return DAG.getTruncStore(Chain, Value, Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004644 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattner00161a62008-01-25 07:20:16 +00004645 ST->isVolatile(), Align);
4646 }
4647 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004648
Evan Cheng59d5b682007-05-07 21:27:48 +00004649 // If this is a store of a bit convert, store the input value if the
Evan Cheng2c4f9432007-05-09 21:49:47 +00004650 // resultant store does not need a higher alignment than the original.
Dale Johannesen98a6c622007-05-16 22:45:30 +00004651 if (Value.getOpcode() == ISD::BIT_CONVERT && !ST->isTruncatingStore() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00004652 ST->isUnindexed()) {
Evan Cheng59d5b682007-05-07 21:27:48 +00004653 unsigned Align = ST->getAlignment();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004654 MVT SVT = Value.getOperand(0).getValueType();
Dan Gohman6448d912008-09-04 15:39:15 +00004655 unsigned OrigAlign = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00004656 getABITypeAlignment(SVT.getTypeForMVT());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004657 if (Align <= OrigAlign &&
4658 ((!AfterLegalize && !ST->isVolatile()) ||
4659 TLI.isOperationLegal(ISD::STORE, SVT)))
Evan Cheng59d5b682007-05-07 21:27:48 +00004660 return DAG.getStore(Chain, Value.getOperand(0), Ptr, ST->getSrcValue(),
Dan Gohman77455612008-06-28 00:45:22 +00004661 ST->getSrcValueOffset(), ST->isVolatile(), OrigAlign);
Jim Laskey279f0532006-09-25 16:29:54 +00004662 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004663
Nate Begeman2cbba892006-12-11 02:23:46 +00004664 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Nate Begeman2cbba892006-12-11 02:23:46 +00004665 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Value)) {
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004666 // NOTE: If the original store is volatile, this transform must not increase
4667 // the number of stores. For example, on x86-32 an f64 can be stored in one
4668 // processor operation but an i64 (which is not legal) requires two. So the
4669 // transform should not be done in this case.
Evan Cheng25ece662006-12-11 17:25:19 +00004670 if (Value.getOpcode() != ISD::TargetConstantFP) {
Dan Gohman475871a2008-07-27 21:46:04 +00004671 SDValue Tmp;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004672 switch (CFP->getValueType(0).getSimpleVT()) {
Chris Lattner62be1a72006-12-12 04:16:14 +00004673 default: assert(0 && "Unknown FP type");
Dale Johannesenc7b21d52007-09-18 18:36:59 +00004674 case MVT::f80: // We don't do this for these yet.
4675 case MVT::f128:
4676 case MVT::ppcf128:
4677 break;
Chris Lattner62be1a72006-12-12 04:16:14 +00004678 case MVT::f32:
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004679 if ((!AfterLegalize && !ST->isVolatile()) ||
4680 TLI.isOperationLegal(ISD::STORE, MVT::i32)) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00004681 Tmp = DAG.getConstant((uint32_t)CFP->getValueAPF().
Dale Johannesen7111b022008-10-09 18:53:47 +00004682 bitcastToAPInt().getZExtValue(), MVT::i32);
Chris Lattner62be1a72006-12-12 04:16:14 +00004683 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004684 ST->getSrcValueOffset(), ST->isVolatile(),
4685 ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00004686 }
4687 break;
4688 case MVT::f64:
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004689 if ((!AfterLegalize && !ST->isVolatile()) ||
4690 TLI.isOperationLegal(ISD::STORE, MVT::i64)) {
Dale Johannesen7111b022008-10-09 18:53:47 +00004691 Tmp = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
Dale Johannesen9d5f4562007-09-12 03:30:33 +00004692 getZExtValue(), MVT::i64);
Chris Lattner62be1a72006-12-12 04:16:14 +00004693 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004694 ST->getSrcValueOffset(), ST->isVolatile(),
4695 ST->getAlignment());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004696 } else if (!ST->isVolatile() &&
4697 TLI.isOperationLegal(ISD::STORE, MVT::i32)) {
Duncan Sandsdc846502007-10-28 12:59:45 +00004698 // Many FP stores are not made apparent until after legalize, e.g. for
Chris Lattner62be1a72006-12-12 04:16:14 +00004699 // argument passing. Since this is so common, custom legalize the
4700 // 64-bit integer store into two 32-bit stores.
Dale Johannesen7111b022008-10-09 18:53:47 +00004701 uint64_t Val = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00004702 SDValue Lo = DAG.getConstant(Val & 0xFFFFFFFF, MVT::i32);
4703 SDValue Hi = DAG.getConstant(Val >> 32, MVT::i32);
Duncan Sands0753fc12008-02-11 10:37:04 +00004704 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner62be1a72006-12-12 04:16:14 +00004705
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004706 int SVOffset = ST->getSrcValueOffset();
4707 unsigned Alignment = ST->getAlignment();
4708 bool isVolatile = ST->isVolatile();
4709
Dan Gohman475871a2008-07-27 21:46:04 +00004710 SDValue St0 = DAG.getStore(Chain, Lo, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004711 ST->getSrcValueOffset(),
4712 isVolatile, ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00004713 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
4714 DAG.getConstant(4, Ptr.getValueType()));
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004715 SVOffset += 4;
Duncan Sandsdc846502007-10-28 12:59:45 +00004716 Alignment = MinAlign(Alignment, 4U);
Dan Gohman475871a2008-07-27 21:46:04 +00004717 SDValue St1 = DAG.getStore(Chain, Hi, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004718 SVOffset, isVolatile, Alignment);
Chris Lattner62be1a72006-12-12 04:16:14 +00004719 return DAG.getNode(ISD::TokenFactor, MVT::Other, St0, St1);
4720 }
4721 break;
Evan Cheng25ece662006-12-11 17:25:19 +00004722 }
Nate Begeman2cbba892006-12-11 02:23:46 +00004723 }
Nate Begeman2cbba892006-12-11 02:23:46 +00004724 }
4725
Jim Laskey279f0532006-09-25 16:29:54 +00004726 if (CombinerAA) {
4727 // Walk up chain skipping non-aliasing memory nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00004728 SDValue BetterChain = FindBetterChain(N, Chain);
Jim Laskey279f0532006-09-25 16:29:54 +00004729
Jim Laskey6ff23e52006-10-04 16:53:27 +00004730 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00004731 if (Chain != BetterChain) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00004732 // Replace the chain to avoid dependency.
Dan Gohman475871a2008-07-27 21:46:04 +00004733 SDValue ReplStore;
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004734 if (ST->isTruncatingStore()) {
4735 ReplStore = DAG.getTruncStore(BetterChain, Value, Ptr,
Chris Lattner4626b252008-01-17 07:20:38 +00004736 ST->getSrcValue(),ST->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004737 ST->getMemoryVT(),
Chris Lattner4626b252008-01-17 07:20:38 +00004738 ST->isVolatile(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004739 } else {
4740 ReplStore = DAG.getStore(BetterChain, Value, Ptr,
Chris Lattner4626b252008-01-17 07:20:38 +00004741 ST->getSrcValue(), ST->getSrcValueOffset(),
4742 ST->isVolatile(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004743 }
4744
Jim Laskey279f0532006-09-25 16:29:54 +00004745 // Create token to keep both nodes around.
Dan Gohman475871a2008-07-27 21:46:04 +00004746 SDValue Token =
Jim Laskey274062c2006-10-13 23:32:28 +00004747 DAG.getNode(ISD::TokenFactor, MVT::Other, Chain, ReplStore);
4748
4749 // Don't add users to work list.
4750 return CombineTo(N, Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00004751 }
Jim Laskeyd1aed7a2006-09-21 16:28:59 +00004752 }
Chris Lattnerc33baaa2005-12-23 05:48:07 +00004753
Evan Cheng33dbedc2006-11-05 09:31:14 +00004754 // Try transforming N to an indexed store.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00004755 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Dan Gohman475871a2008-07-27 21:46:04 +00004756 return SDValue(N, 0);
Evan Cheng33dbedc2006-11-05 09:31:14 +00004757
Chris Lattner3c872852007-12-29 06:26:16 +00004758 // FIXME: is there such a thing as a truncating indexed store?
Chris Lattnerddf89562008-01-17 19:59:44 +00004759 if (ST->isTruncatingStore() && ST->isUnindexed() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00004760 Value.getValueType().isInteger()) {
Chris Lattner2b4c2792007-10-13 06:35:54 +00004761 // See if we can simplify the input to this truncstore with knowledge that
4762 // only the low bits are being used. For example:
4763 // "truncstore (or (shl x, 8), y), i8" -> "truncstore y, i8"
Dan Gohman475871a2008-07-27 21:46:04 +00004764 SDValue Shorter =
Dan Gohman2e68b6f2008-02-25 21:11:39 +00004765 GetDemandedBits(Value,
4766 APInt::getLowBitsSet(Value.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +00004767 ST->getMemoryVT().getSizeInBits()));
Gabor Greifba36cb52008-08-28 21:40:38 +00004768 AddToWorkList(Value.getNode());
4769 if (Shorter.getNode())
Chris Lattner2b4c2792007-10-13 06:35:54 +00004770 return DAG.getTruncStore(Chain, Shorter, Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004771 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattner2b4c2792007-10-13 06:35:54 +00004772 ST->isVolatile(), ST->getAlignment());
Chris Lattnere33544c2007-10-13 06:58:48 +00004773
4774 // Otherwise, see if we can simplify the operation with
4775 // SimplifyDemandedBits, which only works if the value has a single use.
Dan Gohman7b8d4a92008-02-27 00:25:32 +00004776 if (SimplifyDemandedBits(Value,
4777 APInt::getLowBitsSet(
4778 Value.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +00004779 ST->getMemoryVT().getSizeInBits())))
Dan Gohman475871a2008-07-27 21:46:04 +00004780 return SDValue(N, 0);
Chris Lattner2b4c2792007-10-13 06:35:54 +00004781 }
4782
Chris Lattner3c872852007-12-29 06:26:16 +00004783 // If this is a load followed by a store to the same location, then the store
4784 // is dead/noop.
4785 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Value)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004786 if (Ld->getBasePtr() == Ptr && ST->getMemoryVT() == Ld->getMemoryVT() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00004787 ST->isUnindexed() && !ST->isVolatile() &&
Chris Lattner07649d92008-01-08 23:08:06 +00004788 // There can't be any side effects between the load and store, such as
4789 // a call or store.
Dan Gohman475871a2008-07-27 21:46:04 +00004790 Chain.reachesChainWithoutSideEffects(SDValue(Ld, 1))) {
Chris Lattner3c872852007-12-29 06:26:16 +00004791 // The store is dead, remove it.
4792 return Chain;
4793 }
4794 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004795
Chris Lattnerddf89562008-01-17 19:59:44 +00004796 // If this is an FP_ROUND or TRUNC followed by a store, fold this into a
4797 // truncating store. We can do this even if this is already a truncstore.
4798 if ((Value.getOpcode() == ISD::FP_ROUND || Value.getOpcode() == ISD::TRUNCATE)
Gabor Greifba36cb52008-08-28 21:40:38 +00004799 && Value.getNode()->hasOneUse() && ST->isUnindexed() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00004800 TLI.isTruncStoreLegal(Value.getOperand(0).getValueType(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004801 ST->getMemoryVT())) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004802 return DAG.getTruncStore(Chain, Value.getOperand(0), Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004803 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattnerddf89562008-01-17 19:59:44 +00004804 ST->isVolatile(), ST->getAlignment());
4805 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004806
Dan Gohman475871a2008-07-27 21:46:04 +00004807 return SDValue();
Chris Lattner87514ca2005-10-10 22:31:19 +00004808}
4809
Dan Gohman475871a2008-07-27 21:46:04 +00004810SDValue DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
4811 SDValue InVec = N->getOperand(0);
4812 SDValue InVal = N->getOperand(1);
4813 SDValue EltNo = N->getOperand(2);
Chris Lattnerca242442006-03-19 01:27:56 +00004814
4815 // If the invec is a BUILD_VECTOR and if EltNo is a constant, build a new
4816 // vector with the inserted element.
4817 if (InVec.getOpcode() == ISD::BUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004818 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Gabor Greif12632d22008-08-30 19:29:20 +00004819 SmallVector<SDValue, 8> Ops(InVec.getNode()->op_begin(),
4820 InVec.getNode()->op_end());
Chris Lattnerca242442006-03-19 01:27:56 +00004821 if (Elt < Ops.size())
4822 Ops[Elt] = InVal;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004823 return DAG.getNode(ISD::BUILD_VECTOR, InVec.getValueType(),
4824 &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00004825 }
4826
Dan Gohman475871a2008-07-27 21:46:04 +00004827 return SDValue();
Chris Lattnerca242442006-03-19 01:27:56 +00004828}
4829
Dan Gohman475871a2008-07-27 21:46:04 +00004830SDValue DAGCombiner::visitEXTRACT_VECTOR_ELT(SDNode *N) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004831 // (vextract (v4f32 load $addr), c) -> (f32 load $addr+c*size)
4832 // (vextract (v4f32 s2v (f32 load $addr)), c) -> (f32 load $addr+c*size)
4833 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), 0) -> (f32 load $addr)
4834
4835 // Perform only after legalization to ensure build_vector / vector_shuffle
4836 // optimizations have already been done.
Dan Gohman475871a2008-07-27 21:46:04 +00004837 if (!AfterLegalize) return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004838
Dan Gohman475871a2008-07-27 21:46:04 +00004839 SDValue InVec = N->getOperand(0);
4840 SDValue EltNo = N->getOperand(1);
Evan Cheng513da432007-10-06 08:19:55 +00004841
Evan Cheng513da432007-10-06 08:19:55 +00004842 if (isa<ConstantSDNode>(EltNo)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004843 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Evan Cheng513da432007-10-06 08:19:55 +00004844 bool NewLoad = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004845 MVT VT = InVec.getValueType();
4846 MVT EVT = VT.getVectorElementType();
4847 MVT LVT = EVT;
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004848 if (InVec.getOpcode() == ISD::BIT_CONVERT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004849 MVT BCVT = InVec.getOperand(0).getValueType();
Duncan Sands8e4eb092008-06-08 20:54:56 +00004850 if (!BCVT.isVector() || EVT.bitsGT(BCVT.getVectorElementType()))
Dan Gohman475871a2008-07-27 21:46:04 +00004851 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004852 InVec = InVec.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004853 EVT = BCVT.getVectorElementType();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004854 NewLoad = true;
4855 }
Evan Cheng513da432007-10-06 08:19:55 +00004856
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004857 LoadSDNode *LN0 = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00004858 if (ISD::isNormalLoad(InVec.getNode()))
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004859 LN0 = cast<LoadSDNode>(InVec);
4860 else if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR &&
4861 InVec.getOperand(0).getValueType() == EVT &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004862 ISD::isNormalLoad(InVec.getOperand(0).getNode())) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004863 LN0 = cast<LoadSDNode>(InVec.getOperand(0));
4864 } else if (InVec.getOpcode() == ISD::VECTOR_SHUFFLE) {
4865 // (vextract (vector_shuffle (load $addr), v2, <1, u, u, u>), 1)
4866 // =>
4867 // (load $addr+1*size)
4868 unsigned Idx = cast<ConstantSDNode>(InVec.getOperand(2).
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004869 getOperand(Elt))->getZExtValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004870 unsigned NumElems = InVec.getOperand(2).getNumOperands();
4871 InVec = (Idx < NumElems) ? InVec.getOperand(0) : InVec.getOperand(1);
4872 if (InVec.getOpcode() == ISD::BIT_CONVERT)
4873 InVec = InVec.getOperand(0);
Gabor Greifba36cb52008-08-28 21:40:38 +00004874 if (ISD::isNormalLoad(InVec.getNode())) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004875 LN0 = cast<LoadSDNode>(InVec);
4876 Elt = (Idx < NumElems) ? Idx : Idx - NumElems;
Evan Cheng513da432007-10-06 08:19:55 +00004877 }
4878 }
Duncan Sandsec87aa82008-06-15 20:12:31 +00004879 if (!LN0 || !LN0->hasOneUse() || LN0->isVolatile())
Dan Gohman475871a2008-07-27 21:46:04 +00004880 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004881
4882 unsigned Align = LN0->getAlignment();
4883 if (NewLoad) {
4884 // Check the resultant load doesn't need a higher alignment than the
4885 // original load.
Dan Gohman6448d912008-09-04 15:39:15 +00004886 unsigned NewAlign = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00004887 getABITypeAlignment(LVT.getTypeForMVT());
Duncan Sands184a8762008-06-14 17:48:34 +00004888 if (NewAlign > Align || !TLI.isOperationLegal(ISD::LOAD, LVT))
Dan Gohman475871a2008-07-27 21:46:04 +00004889 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004890 Align = NewAlign;
4891 }
4892
Dan Gohman475871a2008-07-27 21:46:04 +00004893 SDValue NewPtr = LN0->getBasePtr();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004894 if (Elt) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004895 unsigned PtrOff = LVT.getSizeInBits() * Elt / 8;
4896 MVT PtrType = NewPtr.getValueType();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004897 if (TLI.isBigEndian())
Duncan Sands83ec4b62008-06-06 12:08:01 +00004898 PtrOff = VT.getSizeInBits() / 8 - PtrOff;
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004899 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
4900 DAG.getConstant(PtrOff, PtrType));
4901 }
4902 return DAG.getLoad(LVT, LN0->getChain(), NewPtr,
4903 LN0->getSrcValue(), LN0->getSrcValueOffset(),
4904 LN0->isVolatile(), Align);
Evan Cheng513da432007-10-06 08:19:55 +00004905 }
Dan Gohman475871a2008-07-27 21:46:04 +00004906 return SDValue();
Evan Cheng513da432007-10-06 08:19:55 +00004907}
4908
4909
Dan Gohman475871a2008-07-27 21:46:04 +00004910SDValue DAGCombiner::visitBUILD_VECTOR(SDNode *N) {
Dan Gohman7f321562007-06-25 16:23:39 +00004911 unsigned NumInScalars = N->getNumOperands();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004912 MVT VT = N->getValueType(0);
4913 unsigned NumElts = VT.getVectorNumElements();
4914 MVT EltType = VT.getVectorElementType();
Chris Lattnerca242442006-03-19 01:27:56 +00004915
Dan Gohman7f321562007-06-25 16:23:39 +00004916 // Check to see if this is a BUILD_VECTOR of a bunch of EXTRACT_VECTOR_ELT
4917 // operations. If so, and if the EXTRACT_VECTOR_ELT vector inputs come from
4918 // at most two distinct vectors, turn this into a shuffle node.
Dan Gohman475871a2008-07-27 21:46:04 +00004919 SDValue VecIn1, VecIn2;
Chris Lattnerd7648c82006-03-28 20:28:38 +00004920 for (unsigned i = 0; i != NumInScalars; ++i) {
4921 // Ignore undef inputs.
4922 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
4923
Dan Gohman7f321562007-06-25 16:23:39 +00004924 // If this input is something other than a EXTRACT_VECTOR_ELT with a
Chris Lattnerd7648c82006-03-28 20:28:38 +00004925 // constant index, bail out.
Dan Gohman7f321562007-06-25 16:23:39 +00004926 if (N->getOperand(i).getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
Chris Lattnerd7648c82006-03-28 20:28:38 +00004927 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
Dan Gohman475871a2008-07-27 21:46:04 +00004928 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00004929 break;
4930 }
4931
Dan Gohman7f321562007-06-25 16:23:39 +00004932 // If the input vector type disagrees with the result of the build_vector,
Chris Lattnerd7648c82006-03-28 20:28:38 +00004933 // we can't make a shuffle.
Dan Gohman475871a2008-07-27 21:46:04 +00004934 SDValue ExtractedFromVec = N->getOperand(i).getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00004935 if (ExtractedFromVec.getValueType() != VT) {
Dan Gohman475871a2008-07-27 21:46:04 +00004936 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00004937 break;
4938 }
4939
4940 // Otherwise, remember this. We allow up to two distinct input vectors.
4941 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
4942 continue;
4943
Gabor Greifba36cb52008-08-28 21:40:38 +00004944 if (VecIn1.getNode() == 0) {
Chris Lattnerd7648c82006-03-28 20:28:38 +00004945 VecIn1 = ExtractedFromVec;
Gabor Greifba36cb52008-08-28 21:40:38 +00004946 } else if (VecIn2.getNode() == 0) {
Chris Lattnerd7648c82006-03-28 20:28:38 +00004947 VecIn2 = ExtractedFromVec;
4948 } else {
4949 // Too many inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00004950 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00004951 break;
4952 }
4953 }
4954
4955 // If everything is good, we can make a shuffle operation.
Gabor Greifba36cb52008-08-28 21:40:38 +00004956 if (VecIn1.getNode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004957 SmallVector<SDValue, 8> BuildVecIndices;
Chris Lattnerd7648c82006-03-28 20:28:38 +00004958 for (unsigned i = 0; i != NumInScalars; ++i) {
4959 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
Evan Cheng597a3bd2007-01-20 10:10:26 +00004960 BuildVecIndices.push_back(DAG.getNode(ISD::UNDEF, TLI.getPointerTy()));
Chris Lattnerd7648c82006-03-28 20:28:38 +00004961 continue;
4962 }
4963
Dan Gohman475871a2008-07-27 21:46:04 +00004964 SDValue Extract = N->getOperand(i);
Chris Lattnerd7648c82006-03-28 20:28:38 +00004965
4966 // If extracting from the first vector, just use the index directly.
4967 if (Extract.getOperand(0) == VecIn1) {
4968 BuildVecIndices.push_back(Extract.getOperand(1));
4969 continue;
4970 }
4971
4972 // Otherwise, use InIdx + VecSize
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004973 unsigned Idx =
4974 cast<ConstantSDNode>(Extract.getOperand(1))->getZExtValue();
Chris Lattner0bd48932008-01-17 07:00:52 +00004975 BuildVecIndices.push_back(DAG.getIntPtrConstant(Idx+NumInScalars));
Chris Lattnerd7648c82006-03-28 20:28:38 +00004976 }
4977
4978 // Add count and size info.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004979 MVT BuildVecVT = MVT::getVectorVT(TLI.getPointerTy(), NumElts);
Chris Lattnerd7648c82006-03-28 20:28:38 +00004980
Dan Gohman7f321562007-06-25 16:23:39 +00004981 // Return the new VECTOR_SHUFFLE node.
Dan Gohman475871a2008-07-27 21:46:04 +00004982 SDValue Ops[5];
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004983 Ops[0] = VecIn1;
Gabor Greifba36cb52008-08-28 21:40:38 +00004984 if (VecIn2.getNode()) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004985 Ops[1] = VecIn2;
Chris Lattnercef896e2006-03-28 22:19:47 +00004986 } else {
Dan Gohman7f321562007-06-25 16:23:39 +00004987 // Use an undef build_vector as input for the second operand.
Dan Gohman475871a2008-07-27 21:46:04 +00004988 std::vector<SDValue> UnOps(NumInScalars,
Chris Lattnercef896e2006-03-28 22:19:47 +00004989 DAG.getNode(ISD::UNDEF,
Dan Gohman7f321562007-06-25 16:23:39 +00004990 EltType));
4991 Ops[1] = DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004992 &UnOps[0], UnOps.size());
Gabor Greifba36cb52008-08-28 21:40:38 +00004993 AddToWorkList(Ops[1].getNode());
Chris Lattnercef896e2006-03-28 22:19:47 +00004994 }
Dan Gohman7f321562007-06-25 16:23:39 +00004995 Ops[2] = DAG.getNode(ISD::BUILD_VECTOR, BuildVecVT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004996 &BuildVecIndices[0], BuildVecIndices.size());
Dan Gohman7f321562007-06-25 16:23:39 +00004997 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Ops, 3);
Chris Lattnerd7648c82006-03-28 20:28:38 +00004998 }
4999
Dan Gohman475871a2008-07-27 21:46:04 +00005000 return SDValue();
Chris Lattnerd7648c82006-03-28 20:28:38 +00005001}
5002
Dan Gohman475871a2008-07-27 21:46:04 +00005003SDValue DAGCombiner::visitCONCAT_VECTORS(SDNode *N) {
Dan Gohman7f321562007-06-25 16:23:39 +00005004 // TODO: Check to see if this is a CONCAT_VECTORS of a bunch of
5005 // EXTRACT_SUBVECTOR operations. If so, and if the EXTRACT_SUBVECTOR vector
5006 // inputs come from at most two distinct vectors, turn this into a shuffle
5007 // node.
5008
5009 // If we only have one input vector, we don't need to do any concatenation.
5010 if (N->getNumOperands() == 1) {
5011 return N->getOperand(0);
5012 }
5013
Dan Gohman475871a2008-07-27 21:46:04 +00005014 return SDValue();
Dan Gohman7f321562007-06-25 16:23:39 +00005015}
5016
Dan Gohman475871a2008-07-27 21:46:04 +00005017SDValue DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
5018 SDValue ShufMask = N->getOperand(2);
Chris Lattnerf1d0c622006-03-31 22:16:43 +00005019 unsigned NumElts = ShufMask.getNumOperands();
5020
Mon P Wangaeb06d22008-11-10 04:46:22 +00005021 SDValue N0 = N->getOperand(0);
5022 SDValue N1 = N->getOperand(1);
5023
5024 assert(N0.getValueType().getVectorNumElements() == NumElts &&
5025 "Vector shuffle must be normalized in DAG");
5026
Chris Lattnerf1d0c622006-03-31 22:16:43 +00005027 // If the shuffle mask is an identity operation on the LHS, return the LHS.
5028 bool isIdentity = true;
5029 for (unsigned i = 0; i != NumElts; ++i) {
5030 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005031 cast<ConstantSDNode>(ShufMask.getOperand(i))->getZExtValue() != i) {
Chris Lattnerf1d0c622006-03-31 22:16:43 +00005032 isIdentity = false;
5033 break;
5034 }
5035 }
5036 if (isIdentity) return N->getOperand(0);
5037
5038 // If the shuffle mask is an identity operation on the RHS, return the RHS.
5039 isIdentity = true;
5040 for (unsigned i = 0; i != NumElts; ++i) {
5041 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005042 cast<ConstantSDNode>(ShufMask.getOperand(i))->getZExtValue() !=
5043 i+NumElts) {
Chris Lattnerf1d0c622006-03-31 22:16:43 +00005044 isIdentity = false;
5045 break;
5046 }
5047 }
5048 if (isIdentity) return N->getOperand(1);
Evan Chenge7bec0d2006-07-20 22:44:41 +00005049
5050 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
5051 // needed at all.
5052 bool isUnary = true;
Evan Cheng917ec982006-07-21 08:25:53 +00005053 bool isSplat = true;
Evan Chenge7bec0d2006-07-20 22:44:41 +00005054 int VecNum = -1;
Reid Spencer9160a6a2006-07-25 20:44:41 +00005055 unsigned BaseIdx = 0;
Evan Chenge7bec0d2006-07-20 22:44:41 +00005056 for (unsigned i = 0; i != NumElts; ++i)
5057 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005058 unsigned Idx=cast<ConstantSDNode>(ShufMask.getOperand(i))->getZExtValue();
Evan Chenge7bec0d2006-07-20 22:44:41 +00005059 int V = (Idx < NumElts) ? 0 : 1;
Evan Cheng917ec982006-07-21 08:25:53 +00005060 if (VecNum == -1) {
Evan Chenge7bec0d2006-07-20 22:44:41 +00005061 VecNum = V;
Evan Cheng917ec982006-07-21 08:25:53 +00005062 BaseIdx = Idx;
5063 } else {
5064 if (BaseIdx != Idx)
5065 isSplat = false;
5066 if (VecNum != V) {
5067 isUnary = false;
5068 break;
5069 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00005070 }
5071 }
5072
Evan Chenge7bec0d2006-07-20 22:44:41 +00005073 // Normalize unary shuffle so the RHS is undef.
5074 if (isUnary && VecNum == 1)
5075 std::swap(N0, N1);
5076
Evan Cheng917ec982006-07-21 08:25:53 +00005077 // If it is a splat, check if the argument vector is a build_vector with
5078 // all scalar elements the same.
5079 if (isSplat) {
Gabor Greifba36cb52008-08-28 21:40:38 +00005080 SDNode *V = N0.getNode();
Evan Cheng917ec982006-07-21 08:25:53 +00005081
Dan Gohman7f321562007-06-25 16:23:39 +00005082 // If this is a bit convert that changes the element type of the vector but
Evan Cheng59569222006-10-16 22:49:37 +00005083 // not the number of vector elements, look through it. Be careful not to
5084 // look though conversions that change things like v4f32 to v2f64.
Dan Gohman7f321562007-06-25 16:23:39 +00005085 if (V->getOpcode() == ISD::BIT_CONVERT) {
Dan Gohman475871a2008-07-27 21:46:04 +00005086 SDValue ConvInput = V->getOperand(0);
Evan Cheng29257862008-07-22 20:42:56 +00005087 if (ConvInput.getValueType().isVector() &&
5088 ConvInput.getValueType().getVectorNumElements() == NumElts)
Gabor Greifba36cb52008-08-28 21:40:38 +00005089 V = ConvInput.getNode();
Evan Cheng59569222006-10-16 22:49:37 +00005090 }
5091
Dan Gohman7f321562007-06-25 16:23:39 +00005092 if (V->getOpcode() == ISD::BUILD_VECTOR) {
5093 unsigned NumElems = V->getNumOperands();
Evan Cheng917ec982006-07-21 08:25:53 +00005094 if (NumElems > BaseIdx) {
Dan Gohman475871a2008-07-27 21:46:04 +00005095 SDValue Base;
Evan Cheng917ec982006-07-21 08:25:53 +00005096 bool AllSame = true;
5097 for (unsigned i = 0; i != NumElems; ++i) {
5098 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
5099 Base = V->getOperand(i);
5100 break;
5101 }
5102 }
5103 // Splat of <u, u, u, u>, return <u, u, u, u>
Gabor Greifba36cb52008-08-28 21:40:38 +00005104 if (!Base.getNode())
Evan Cheng917ec982006-07-21 08:25:53 +00005105 return N0;
5106 for (unsigned i = 0; i != NumElems; ++i) {
Evan Chenge0480d22007-09-18 21:54:37 +00005107 if (V->getOperand(i) != Base) {
Evan Cheng917ec982006-07-21 08:25:53 +00005108 AllSame = false;
5109 break;
5110 }
5111 }
5112 // Splat of <x, x, x, x>, return <x, x, x, x>
5113 if (AllSame)
5114 return N0;
5115 }
5116 }
5117 }
5118
Evan Chenge7bec0d2006-07-20 22:44:41 +00005119 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
5120 // into an undef.
5121 if (isUnary || N0 == N1) {
Chris Lattner17614ea2006-04-08 05:34:25 +00005122 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
5123 // first operand.
Dan Gohman475871a2008-07-27 21:46:04 +00005124 SmallVector<SDValue, 8> MappedOps;
Chris Lattner17614ea2006-04-08 05:34:25 +00005125 for (unsigned i = 0; i != NumElts; ++i) {
5126 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005127 cast<ConstantSDNode>(ShufMask.getOperand(i))->getZExtValue() <
5128 NumElts) {
Chris Lattner17614ea2006-04-08 05:34:25 +00005129 MappedOps.push_back(ShufMask.getOperand(i));
5130 } else {
5131 unsigned NewIdx =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005132 cast<ConstantSDNode>(ShufMask.getOperand(i))->getZExtValue() -
5133 NumElts;
Duncan Sandsd038e042008-07-21 10:20:31 +00005134 MappedOps.push_back(DAG.getConstant(NewIdx,
5135 ShufMask.getOperand(i).getValueType()));
Chris Lattner17614ea2006-04-08 05:34:25 +00005136 }
5137 }
Dan Gohman7f321562007-06-25 16:23:39 +00005138 ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMask.getValueType(),
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005139 &MappedOps[0], MappedOps.size());
Gabor Greifba36cb52008-08-28 21:40:38 +00005140 AddToWorkList(ShufMask.getNode());
Dan Gohman7f321562007-06-25 16:23:39 +00005141 return DAG.getNode(ISD::VECTOR_SHUFFLE, N->getValueType(0),
5142 N0,
5143 DAG.getNode(ISD::UNDEF, N->getValueType(0)),
5144 ShufMask);
Chris Lattner17614ea2006-04-08 05:34:25 +00005145 }
Dan Gohman7f321562007-06-25 16:23:39 +00005146
Dan Gohman475871a2008-07-27 21:46:04 +00005147 return SDValue();
Chris Lattnerf1d0c622006-03-31 22:16:43 +00005148}
5149
Bill Wendling1c55a9c2008-11-21 02:12:42 +00005150SDValue DAGCombiner::visitSADDO(SDNode *N) {
Bill Wendling7cdc3c82008-11-21 02:03:52 +00005151 SDValue LHS = N->getOperand(0);
5152 SDValue RHS = N->getOperand(1);
5153
5154 SDValue Sum = DAG.getNode(ISD::ADD, LHS.getValueType(), LHS, RHS);
5155 AddToWorkList(Sum.getNode());
5156 SDValue Cmp = DAG.getSetCC(MVT::i1, Sum, LHS, ISD::SETLT);
5157 AddToWorkList(Cmp.getNode());
5158
Bill Wendling6c63f622008-11-21 02:22:59 +00005159 MVT ValueVTs[] = { LHS.getValueType(), MVT::i1 };
5160 SDValue Ops[] = { Sum, Cmp };
Bill Wendling7cdc3c82008-11-21 02:03:52 +00005161
Bill Wendling6c63f622008-11-21 02:22:59 +00005162 SDValue Merge = DAG.getMergeValues(DAG.getVTList(&ValueVTs[0], 2),
5163 &Ops[0], 2);
Bill Wendling7cdc3c82008-11-21 02:03:52 +00005164 SDNode *MNode = Merge.getNode();
5165
5166 AddToWorkList(MNode);
5167 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), SDValue(MNode, 0));
5168 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SDValue(MNode, 1));
Bill Wendling7cdc3c82008-11-21 02:03:52 +00005169
5170 // Since the node is now dead, remove it from the graph.
5171 removeFromWorkList(N);
5172 DAG.DeleteNode(N);
5173 return SDValue(N, 0);
5174}
5175
Bill Wendling1c55a9c2008-11-21 02:12:42 +00005176SDValue DAGCombiner::visitUADDO(SDNode *N) {
5177 return SDValue();
5178}
5179
Evan Cheng44f1f092006-04-20 08:56:16 +00005180/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
Dan Gohman7f321562007-06-25 16:23:39 +00005181/// an AND to a vector_shuffle with the destination vector and a zero vector.
5182/// e.g. AND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
Evan Cheng44f1f092006-04-20 08:56:16 +00005183/// vector_shuffle V, Zero, <0, 4, 2, 4>
Dan Gohman475871a2008-07-27 21:46:04 +00005184SDValue DAGCombiner::XformToShuffleWithZero(SDNode *N) {
5185 SDValue LHS = N->getOperand(0);
5186 SDValue RHS = N->getOperand(1);
Dan Gohman7f321562007-06-25 16:23:39 +00005187 if (N->getOpcode() == ISD::AND) {
5188 if (RHS.getOpcode() == ISD::BIT_CONVERT)
Evan Cheng44f1f092006-04-20 08:56:16 +00005189 RHS = RHS.getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00005190 if (RHS.getOpcode() == ISD::BUILD_VECTOR) {
Dan Gohman475871a2008-07-27 21:46:04 +00005191 std::vector<SDValue> IdxOps;
Evan Cheng44f1f092006-04-20 08:56:16 +00005192 unsigned NumOps = RHS.getNumOperands();
Dan Gohman7f321562007-06-25 16:23:39 +00005193 unsigned NumElts = NumOps;
Evan Cheng44f1f092006-04-20 08:56:16 +00005194 for (unsigned i = 0; i != NumElts; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005195 SDValue Elt = RHS.getOperand(i);
Evan Cheng44f1f092006-04-20 08:56:16 +00005196 if (!isa<ConstantSDNode>(Elt))
Dan Gohman475871a2008-07-27 21:46:04 +00005197 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00005198 else if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
Duncan Sands94989ac2008-10-19 14:58:05 +00005199 IdxOps.push_back(DAG.getIntPtrConstant(i));
Evan Cheng44f1f092006-04-20 08:56:16 +00005200 else if (cast<ConstantSDNode>(Elt)->isNullValue())
Duncan Sands94989ac2008-10-19 14:58:05 +00005201 IdxOps.push_back(DAG.getIntPtrConstant(NumElts));
Evan Cheng44f1f092006-04-20 08:56:16 +00005202 else
Dan Gohman475871a2008-07-27 21:46:04 +00005203 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00005204 }
5205
5206 // Let's see if the target supports this vector_shuffle.
Duncan Sands94989ac2008-10-19 14:58:05 +00005207 if (!TLI.isVectorClearMaskLegal(IdxOps, TLI.getPointerTy(), DAG))
Dan Gohman475871a2008-07-27 21:46:04 +00005208 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00005209
Dan Gohman7f321562007-06-25 16:23:39 +00005210 // Return the new VECTOR_SHUFFLE node.
Duncan Sands94989ac2008-10-19 14:58:05 +00005211 MVT EVT = RHS.getValueType().getVectorElementType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005212 MVT VT = MVT::getVectorVT(EVT, NumElts);
Evan Cheng3eb57d52008-11-05 06:04:18 +00005213 MVT MaskVT = MVT::getVectorVT(TLI.getPointerTy(), NumElts);
Dan Gohman475871a2008-07-27 21:46:04 +00005214 std::vector<SDValue> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00005215 LHS = DAG.getNode(ISD::BIT_CONVERT, VT, LHS);
Evan Cheng44f1f092006-04-20 08:56:16 +00005216 Ops.push_back(LHS);
Gabor Greifba36cb52008-08-28 21:40:38 +00005217 AddToWorkList(LHS.getNode());
Dan Gohman475871a2008-07-27 21:46:04 +00005218 std::vector<SDValue> ZeroOps(NumElts, DAG.getConstant(0, EVT));
Dan Gohman7f321562007-06-25 16:23:39 +00005219 Ops.push_back(DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005220 &ZeroOps[0], ZeroOps.size()));
Evan Cheng3eb57d52008-11-05 06:04:18 +00005221 Ops.push_back(DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005222 &IdxOps[0], IdxOps.size()));
Dan Gohman475871a2008-07-27 21:46:04 +00005223 SDValue Result = DAG.getNode(ISD::VECTOR_SHUFFLE, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005224 &Ops[0], Ops.size());
Dan Gohman7a9a5af2008-07-16 16:13:58 +00005225 if (VT != N->getValueType(0))
5226 Result = DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Result);
Evan Cheng44f1f092006-04-20 08:56:16 +00005227 return Result;
5228 }
5229 }
Dan Gohman475871a2008-07-27 21:46:04 +00005230 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00005231}
5232
Dan Gohman7f321562007-06-25 16:23:39 +00005233/// SimplifyVBinOp - Visit a binary vector operation, like ADD.
Dan Gohman475871a2008-07-27 21:46:04 +00005234SDValue DAGCombiner::SimplifyVBinOp(SDNode *N) {
Dan Gohman7f321562007-06-25 16:23:39 +00005235 // After legalize, the target may be depending on adds and other
5236 // binary ops to provide legal ways to construct constants or other
5237 // things. Simplifying them may result in a loss of legality.
Dan Gohman475871a2008-07-27 21:46:04 +00005238 if (AfterLegalize) return SDValue();
Dan Gohman7f321562007-06-25 16:23:39 +00005239
Duncan Sands83ec4b62008-06-06 12:08:01 +00005240 MVT VT = N->getValueType(0);
5241 assert(VT.isVector() && "SimplifyVBinOp only works on vectors!");
Dan Gohman7f321562007-06-25 16:23:39 +00005242
Duncan Sands83ec4b62008-06-06 12:08:01 +00005243 MVT EltType = VT.getVectorElementType();
Dan Gohman475871a2008-07-27 21:46:04 +00005244 SDValue LHS = N->getOperand(0);
5245 SDValue RHS = N->getOperand(1);
5246 SDValue Shuffle = XformToShuffleWithZero(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00005247 if (Shuffle.getNode()) return Shuffle;
Evan Cheng44f1f092006-04-20 08:56:16 +00005248
Dan Gohman7f321562007-06-25 16:23:39 +00005249 // If the LHS and RHS are BUILD_VECTOR nodes, see if we can constant fold
Chris Lattneredab1b92006-04-02 03:25:57 +00005250 // this operation.
Dan Gohman7f321562007-06-25 16:23:39 +00005251 if (LHS.getOpcode() == ISD::BUILD_VECTOR &&
5252 RHS.getOpcode() == ISD::BUILD_VECTOR) {
Dan Gohman475871a2008-07-27 21:46:04 +00005253 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00005254 for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005255 SDValue LHSOp = LHS.getOperand(i);
5256 SDValue RHSOp = RHS.getOperand(i);
Chris Lattneredab1b92006-04-02 03:25:57 +00005257 // If these two elements can't be folded, bail out.
5258 if ((LHSOp.getOpcode() != ISD::UNDEF &&
5259 LHSOp.getOpcode() != ISD::Constant &&
5260 LHSOp.getOpcode() != ISD::ConstantFP) ||
5261 (RHSOp.getOpcode() != ISD::UNDEF &&
5262 RHSOp.getOpcode() != ISD::Constant &&
5263 RHSOp.getOpcode() != ISD::ConstantFP))
5264 break;
Evan Cheng7b336a82006-05-31 06:08:35 +00005265 // Can't fold divide by zero.
Dan Gohman7f321562007-06-25 16:23:39 +00005266 if (N->getOpcode() == ISD::SDIV || N->getOpcode() == ISD::UDIV ||
5267 N->getOpcode() == ISD::FDIV) {
Evan Cheng7b336a82006-05-31 06:08:35 +00005268 if ((RHSOp.getOpcode() == ISD::Constant &&
Gabor Greifba36cb52008-08-28 21:40:38 +00005269 cast<ConstantSDNode>(RHSOp.getNode())->isNullValue()) ||
Evan Cheng7b336a82006-05-31 06:08:35 +00005270 (RHSOp.getOpcode() == ISD::ConstantFP &&
Gabor Greifba36cb52008-08-28 21:40:38 +00005271 cast<ConstantFPSDNode>(RHSOp.getNode())->getValueAPF().isZero()))
Evan Cheng7b336a82006-05-31 06:08:35 +00005272 break;
5273 }
Dan Gohman7f321562007-06-25 16:23:39 +00005274 Ops.push_back(DAG.getNode(N->getOpcode(), EltType, LHSOp, RHSOp));
Gabor Greifba36cb52008-08-28 21:40:38 +00005275 AddToWorkList(Ops.back().getNode());
Chris Lattneredab1b92006-04-02 03:25:57 +00005276 assert((Ops.back().getOpcode() == ISD::UNDEF ||
5277 Ops.back().getOpcode() == ISD::Constant ||
5278 Ops.back().getOpcode() == ISD::ConstantFP) &&
5279 "Scalar binop didn't fold!");
5280 }
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00005281
Dan Gohman7f321562007-06-25 16:23:39 +00005282 if (Ops.size() == LHS.getNumOperands()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005283 MVT VT = LHS.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +00005284 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00005285 }
Chris Lattneredab1b92006-04-02 03:25:57 +00005286 }
5287
Dan Gohman475871a2008-07-27 21:46:04 +00005288 return SDValue();
Chris Lattneredab1b92006-04-02 03:25:57 +00005289}
5290
Dan Gohman475871a2008-07-27 21:46:04 +00005291SDValue DAGCombiner::SimplifySelect(SDValue N0, SDValue N1, SDValue N2){
Nate Begemanf845b452005-10-08 00:29:44 +00005292 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
5293
Dan Gohman475871a2008-07-27 21:46:04 +00005294 SDValue SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
Nate Begemanf845b452005-10-08 00:29:44 +00005295 cast<CondCodeSDNode>(N0.getOperand(2))->get());
5296 // If we got a simplified select_cc node back from SimplifySelectCC, then
5297 // break it down into a new SETCC node, and a new SELECT node, and then return
5298 // the SELECT node, since we were called with a SELECT node.
Gabor Greifba36cb52008-08-28 21:40:38 +00005299 if (SCC.getNode()) {
Nate Begemanf845b452005-10-08 00:29:44 +00005300 // Check to see if we got a select_cc back (to turn into setcc/select).
5301 // Otherwise, just return whatever node we got back, like fabs.
5302 if (SCC.getOpcode() == ISD::SELECT_CC) {
Dan Gohman475871a2008-07-27 21:46:04 +00005303 SDValue SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
Nate Begemanf845b452005-10-08 00:29:44 +00005304 SCC.getOperand(0), SCC.getOperand(1),
5305 SCC.getOperand(4));
Gabor Greifba36cb52008-08-28 21:40:38 +00005306 AddToWorkList(SETCC.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00005307 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
5308 SCC.getOperand(3), SETCC);
5309 }
5310 return SCC;
5311 }
Dan Gohman475871a2008-07-27 21:46:04 +00005312 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00005313}
5314
Chris Lattner40c62d52005-10-18 06:04:22 +00005315/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
5316/// are the two values being selected between, see if we can simplify the
Chris Lattner729c6d12006-05-27 00:43:02 +00005317/// select. Callers of this should assume that TheSelect is deleted if this
5318/// returns true. As such, they should return the appropriate thing (e.g. the
5319/// node) back to the top-level of the DAG combiner loop to avoid it being
5320/// looked at.
Chris Lattner40c62d52005-10-18 06:04:22 +00005321///
Dan Gohman475871a2008-07-27 21:46:04 +00005322bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDValue LHS,
5323 SDValue RHS) {
Chris Lattner40c62d52005-10-18 06:04:22 +00005324
5325 // If this is a select from two identical things, try to pull the operation
5326 // through the select.
5327 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
Chris Lattner40c62d52005-10-18 06:04:22 +00005328 // If this is a load and the token chain is identical, replace the select
5329 // of two loads with a load through a select of the address to load from.
5330 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
5331 // constants have been dropped into the constant pool.
Evan Cheng466685d2006-10-09 20:57:25 +00005332 if (LHS.getOpcode() == ISD::LOAD &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005333 // Do not let this transformation reduce the number of volatile loads.
5334 !cast<LoadSDNode>(LHS)->isVolatile() &&
5335 !cast<LoadSDNode>(RHS)->isVolatile() &&
Chris Lattner40c62d52005-10-18 06:04:22 +00005336 // Token chains must be identical.
Evan Cheng466685d2006-10-09 20:57:25 +00005337 LHS.getOperand(0) == RHS.getOperand(0)) {
5338 LoadSDNode *LLD = cast<LoadSDNode>(LHS);
5339 LoadSDNode *RLD = cast<LoadSDNode>(RHS);
5340
5341 // If this is an EXTLOAD, the VT's must match.
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005342 if (LLD->getMemoryVT() == RLD->getMemoryVT()) {
Evan Cheng466685d2006-10-09 20:57:25 +00005343 // FIXME: this conflates two src values, discarding one. This is not
5344 // the right thing to do, but nothing uses srcvalues now. When they do,
5345 // turn SrcValue into a list of locations.
Dan Gohman475871a2008-07-27 21:46:04 +00005346 SDValue Addr;
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005347 if (TheSelect->getOpcode() == ISD::SELECT) {
5348 // Check that the condition doesn't reach either load. If so, folding
5349 // this will induce a cycle into the DAG.
Gabor Greifba36cb52008-08-28 21:40:38 +00005350 if (!LLD->isPredecessorOf(TheSelect->getOperand(0).getNode()) &&
5351 !RLD->isPredecessorOf(TheSelect->getOperand(0).getNode())) {
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005352 Addr = DAG.getNode(ISD::SELECT, LLD->getBasePtr().getValueType(),
5353 TheSelect->getOperand(0), LLD->getBasePtr(),
5354 RLD->getBasePtr());
5355 }
5356 } else {
5357 // Check that the condition doesn't reach either load. If so, folding
5358 // this will induce a cycle into the DAG.
Gabor Greifba36cb52008-08-28 21:40:38 +00005359 if (!LLD->isPredecessorOf(TheSelect->getOperand(0).getNode()) &&
5360 !RLD->isPredecessorOf(TheSelect->getOperand(0).getNode()) &&
5361 !LLD->isPredecessorOf(TheSelect->getOperand(1).getNode()) &&
5362 !RLD->isPredecessorOf(TheSelect->getOperand(1).getNode())) {
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005363 Addr = DAG.getNode(ISD::SELECT_CC, LLD->getBasePtr().getValueType(),
Evan Cheng466685d2006-10-09 20:57:25 +00005364 TheSelect->getOperand(0),
5365 TheSelect->getOperand(1),
5366 LLD->getBasePtr(), RLD->getBasePtr(),
5367 TheSelect->getOperand(4));
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005368 }
Evan Cheng466685d2006-10-09 20:57:25 +00005369 }
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005370
Gabor Greifba36cb52008-08-28 21:40:38 +00005371 if (Addr.getNode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005372 SDValue Load;
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005373 if (LLD->getExtensionType() == ISD::NON_EXTLOAD)
5374 Load = DAG.getLoad(TheSelect->getValueType(0), LLD->getChain(),
5375 Addr,LLD->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00005376 LLD->getSrcValueOffset(),
5377 LLD->isVolatile(),
5378 LLD->getAlignment());
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005379 else {
5380 Load = DAG.getExtLoad(LLD->getExtensionType(),
5381 TheSelect->getValueType(0),
5382 LLD->getChain(), Addr, LLD->getSrcValue(),
5383 LLD->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005384 LLD->getMemoryVT(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00005385 LLD->isVolatile(),
5386 LLD->getAlignment());
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005387 }
5388 // Users of the select now use the result of the load.
5389 CombineTo(TheSelect, Load);
5390
5391 // Users of the old loads now use the new load's chain. We know the
5392 // old-load value is dead now.
Gabor Greifba36cb52008-08-28 21:40:38 +00005393 CombineTo(LHS.getNode(), Load.getValue(0), Load.getValue(1));
5394 CombineTo(RHS.getNode(), Load.getValue(0), Load.getValue(1));
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005395 return true;
5396 }
Evan Chengc5484282006-10-04 00:56:09 +00005397 }
Chris Lattner40c62d52005-10-18 06:04:22 +00005398 }
5399 }
5400
5401 return false;
5402}
5403
Dan Gohman475871a2008-07-27 21:46:04 +00005404SDValue DAGCombiner::SimplifySelectCC(SDValue N0, SDValue N1,
5405 SDValue N2, SDValue N3,
5406 ISD::CondCode CC, bool NotExtCompare) {
Nate Begemanf845b452005-10-08 00:29:44 +00005407
Duncan Sands83ec4b62008-06-06 12:08:01 +00005408 MVT VT = N2.getValueType();
Gabor Greifba36cb52008-08-28 21:40:38 +00005409 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
5410 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
5411 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00005412
5413 // Determine if the condition we're dealing with is constant
Dan Gohman475871a2008-07-27 21:46:04 +00005414 SDValue SCC = SimplifySetCC(TLI.getSetCCResultType(N0), N0, N1, CC, false);
Gabor Greifba36cb52008-08-28 21:40:38 +00005415 if (SCC.getNode()) AddToWorkList(SCC.getNode());
5416 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00005417
5418 // fold select_cc true, x, y -> x
Dan Gohman002e5d02008-03-13 22:13:53 +00005419 if (SCCC && !SCCC->isNullValue())
Nate Begemanf845b452005-10-08 00:29:44 +00005420 return N2;
5421 // fold select_cc false, x, y -> y
Dan Gohman002e5d02008-03-13 22:13:53 +00005422 if (SCCC && SCCC->isNullValue())
Nate Begemanf845b452005-10-08 00:29:44 +00005423 return N3;
5424
5425 // Check to see if we can simplify the select into an fabs node
5426 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
5427 // Allow either -0.0 or 0.0
Dale Johannesen87503a62007-08-25 22:10:57 +00005428 if (CFP->getValueAPF().isZero()) {
Nate Begemanf845b452005-10-08 00:29:44 +00005429 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
5430 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
5431 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
5432 N2 == N3.getOperand(0))
5433 return DAG.getNode(ISD::FABS, VT, N0);
5434
5435 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
5436 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
5437 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
5438 N2.getOperand(0) == N3)
5439 return DAG.getNode(ISD::FABS, VT, N3);
5440 }
5441 }
5442
5443 // Check to see if we can perform the "gzip trick", transforming
5444 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
Chris Lattnere3152e52006-09-20 06:41:35 +00005445 if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00005446 N0.getValueType().isInteger() &&
5447 N2.getValueType().isInteger() &&
Dan Gohman002e5d02008-03-13 22:13:53 +00005448 (N1C->isNullValue() || // (a < 0) ? b : 0
5449 (N1C->getAPIntValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0
Duncan Sands83ec4b62008-06-06 12:08:01 +00005450 MVT XType = N0.getValueType();
5451 MVT AType = N2.getValueType();
Duncan Sands8e4eb092008-06-08 20:54:56 +00005452 if (XType.bitsGE(AType)) {
Nate Begemanf845b452005-10-08 00:29:44 +00005453 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00005454 // single-bit constant.
Dan Gohman002e5d02008-03-13 22:13:53 +00005455 if (N2C && ((N2C->getAPIntValue() & (N2C->getAPIntValue()-1)) == 0)) {
5456 unsigned ShCtV = N2C->getAPIntValue().logBase2();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005457 ShCtV = XType.getSizeInBits()-ShCtV-1;
Dan Gohman475871a2008-07-27 21:46:04 +00005458 SDValue ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
5459 SDValue Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
Gabor Greifba36cb52008-08-28 21:40:38 +00005460 AddToWorkList(Shift.getNode());
Duncan Sands8e4eb092008-06-08 20:54:56 +00005461 if (XType.bitsGT(AType)) {
Nate Begemanf845b452005-10-08 00:29:44 +00005462 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00005463 AddToWorkList(Shift.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00005464 }
5465 return DAG.getNode(ISD::AND, AType, Shift, N2);
5466 }
Dan Gohman475871a2008-07-27 21:46:04 +00005467 SDValue Shift = DAG.getNode(ISD::SRA, XType, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005468 DAG.getConstant(XType.getSizeInBits()-1,
Nate Begemanf845b452005-10-08 00:29:44 +00005469 TLI.getShiftAmountTy()));
Gabor Greifba36cb52008-08-28 21:40:38 +00005470 AddToWorkList(Shift.getNode());
Duncan Sands8e4eb092008-06-08 20:54:56 +00005471 if (XType.bitsGT(AType)) {
Nate Begemanf845b452005-10-08 00:29:44 +00005472 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00005473 AddToWorkList(Shift.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00005474 }
5475 return DAG.getNode(ISD::AND, AType, Shift, N2);
5476 }
5477 }
Nate Begeman07ed4172005-10-10 21:26:48 +00005478
5479 // fold select C, 16, 0 -> shl C, 4
Dan Gohman002e5d02008-03-13 22:13:53 +00005480 if (N2C && N3C && N3C->isNullValue() && N2C->getAPIntValue().isPowerOf2() &&
Nate Begeman07ed4172005-10-10 21:26:48 +00005481 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) {
Chris Lattner1eba01e2007-04-11 06:50:51 +00005482
5483 // If the caller doesn't want us to simplify this into a zext of a compare,
5484 // don't do it.
Dan Gohman002e5d02008-03-13 22:13:53 +00005485 if (NotExtCompare && N2C->getAPIntValue() == 1)
Dan Gohman475871a2008-07-27 21:46:04 +00005486 return SDValue();
Chris Lattner1eba01e2007-04-11 06:50:51 +00005487
Nate Begeman07ed4172005-10-10 21:26:48 +00005488 // Get a SetCC of the condition
5489 // FIXME: Should probably make sure that setcc is legal if we ever have a
5490 // target where it isn't.
Dan Gohman475871a2008-07-27 21:46:04 +00005491 SDValue Temp, SCC;
Nate Begeman07ed4172005-10-10 21:26:48 +00005492 // cast from setcc result type to select result type
Nate Begemanb0d04a72006-02-18 02:40:58 +00005493 if (AfterLegalize) {
Scott Michel5b8f82e2008-03-10 15:42:14 +00005494 SCC = DAG.getSetCC(TLI.getSetCCResultType(N0), N0, N1, CC);
Duncan Sands8e4eb092008-06-08 20:54:56 +00005495 if (N2.getValueType().bitsLT(SCC.getValueType()))
Chris Lattner555d8d62006-12-07 22:36:47 +00005496 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
5497 else
5498 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00005499 } else {
5500 SCC = DAG.getSetCC(MVT::i1, N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00005501 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00005502 }
Gabor Greifba36cb52008-08-28 21:40:38 +00005503 AddToWorkList(SCC.getNode());
5504 AddToWorkList(Temp.getNode());
Chris Lattnerc56a81d2007-04-11 06:43:25 +00005505
Dan Gohman002e5d02008-03-13 22:13:53 +00005506 if (N2C->getAPIntValue() == 1)
Chris Lattnerc56a81d2007-04-11 06:43:25 +00005507 return Temp;
Nate Begeman07ed4172005-10-10 21:26:48 +00005508 // shl setcc result by log2 n2c
5509 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
Dan Gohman002e5d02008-03-13 22:13:53 +00005510 DAG.getConstant(N2C->getAPIntValue().logBase2(),
Nate Begeman07ed4172005-10-10 21:26:48 +00005511 TLI.getShiftAmountTy()));
5512 }
5513
Nate Begemanf845b452005-10-08 00:29:44 +00005514 // Check to see if this is the equivalent of setcc
5515 // FIXME: Turn all of these into setcc if setcc if setcc is legal
5516 // otherwise, go ahead with the folds.
Dan Gohman002e5d02008-03-13 22:13:53 +00005517 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getAPIntValue() == 1ULL)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005518 MVT XType = N0.getValueType();
Duncan Sands184a8762008-06-14 17:48:34 +00005519 if (!AfterLegalize ||
5520 TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultType(N0))) {
Dan Gohman475871a2008-07-27 21:46:04 +00005521 SDValue Res = DAG.getSetCC(TLI.getSetCCResultType(N0), N0, N1, CC);
Nate Begemanf845b452005-10-08 00:29:44 +00005522 if (Res.getValueType() != VT)
5523 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
5524 return Res;
5525 }
5526
5527 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
5528 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
Duncan Sands184a8762008-06-14 17:48:34 +00005529 (!AfterLegalize ||
5530 TLI.isOperationLegal(ISD::CTLZ, XType))) {
Dan Gohman475871a2008-07-27 21:46:04 +00005531 SDValue Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
Nate Begemanf845b452005-10-08 00:29:44 +00005532 return DAG.getNode(ISD::SRL, XType, Ctlz,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005533 DAG.getConstant(Log2_32(XType.getSizeInBits()),
Nate Begemanf845b452005-10-08 00:29:44 +00005534 TLI.getShiftAmountTy()));
5535 }
5536 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
5537 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
Dan Gohman475871a2008-07-27 21:46:04 +00005538 SDValue NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
Nate Begemanf845b452005-10-08 00:29:44 +00005539 N0);
Dan Gohman475871a2008-07-27 21:46:04 +00005540 SDValue NotN0 = DAG.getNode(ISD::XOR, XType, N0,
Nate Begemanf845b452005-10-08 00:29:44 +00005541 DAG.getConstant(~0ULL, XType));
5542 return DAG.getNode(ISD::SRL, XType,
5543 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
Duncan Sands83ec4b62008-06-06 12:08:01 +00005544 DAG.getConstant(XType.getSizeInBits()-1,
Nate Begemanf845b452005-10-08 00:29:44 +00005545 TLI.getShiftAmountTy()));
5546 }
5547 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
5548 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
Dan Gohman475871a2008-07-27 21:46:04 +00005549 SDValue Sign = DAG.getNode(ISD::SRL, XType, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005550 DAG.getConstant(XType.getSizeInBits()-1,
Nate Begemanf845b452005-10-08 00:29:44 +00005551 TLI.getShiftAmountTy()));
5552 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
5553 }
5554 }
5555
5556 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
5557 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
5558 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
Chris Lattner1982ef22007-04-11 05:11:38 +00005559 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1) &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00005560 N2.getOperand(0) == N1 && N0.getValueType().isInteger()) {
5561 MVT XType = N0.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00005562 SDValue Shift = DAG.getNode(ISD::SRA, XType, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005563 DAG.getConstant(XType.getSizeInBits()-1,
Chris Lattner1982ef22007-04-11 05:11:38 +00005564 TLI.getShiftAmountTy()));
Dan Gohman475871a2008-07-27 21:46:04 +00005565 SDValue Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00005566 AddToWorkList(Shift.getNode());
5567 AddToWorkList(Add.getNode());
Chris Lattner1982ef22007-04-11 05:11:38 +00005568 return DAG.getNode(ISD::XOR, XType, Add, Shift);
5569 }
5570 // Check to see if this is an integer abs. select_cc setgt X, -1, X, -X ->
5571 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
5572 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT &&
5573 N0 == N2 && N3.getOpcode() == ISD::SUB && N0 == N3.getOperand(1)) {
5574 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N3.getOperand(0))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005575 MVT XType = N0.getValueType();
5576 if (SubC->isNullValue() && XType.isInteger()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005577 SDValue Shift = DAG.getNode(ISD::SRA, XType, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005578 DAG.getConstant(XType.getSizeInBits()-1,
Chris Lattner1982ef22007-04-11 05:11:38 +00005579 TLI.getShiftAmountTy()));
Dan Gohman475871a2008-07-27 21:46:04 +00005580 SDValue Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00005581 AddToWorkList(Shift.getNode());
5582 AddToWorkList(Add.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00005583 return DAG.getNode(ISD::XOR, XType, Add, Shift);
5584 }
5585 }
5586 }
Chris Lattner1982ef22007-04-11 05:11:38 +00005587
Dan Gohman475871a2008-07-27 21:46:04 +00005588 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00005589}
5590
Evan Chengfa1eb272007-02-08 22:13:59 +00005591/// SimplifySetCC - This is a stub for TargetLowering::SimplifySetCC.
Dan Gohman475871a2008-07-27 21:46:04 +00005592SDValue DAGCombiner::SimplifySetCC(MVT VT, SDValue N0,
5593 SDValue N1, ISD::CondCode Cond,
5594 bool foldBooleans) {
Evan Chengfa1eb272007-02-08 22:13:59 +00005595 TargetLowering::DAGCombinerInfo
5596 DagCombineInfo(DAG, !AfterLegalize, false, this);
5597 return TLI.SimplifySetCC(VT, N0, N1, Cond, foldBooleans, DagCombineInfo);
Nate Begeman452d7beb2005-09-16 00:54:12 +00005598}
5599
Nate Begeman69575232005-10-20 02:15:44 +00005600/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
5601/// return a DAG expression to select that will generate the same value by
5602/// multiplying by a magic number. See:
5603/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Dan Gohman475871a2008-07-27 21:46:04 +00005604SDValue DAGCombiner::BuildSDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00005605 std::vector<SDNode*> Built;
Dan Gohman475871a2008-07-27 21:46:04 +00005606 SDValue S = TLI.BuildSDIV(N, DAG, &Built);
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005607
Andrew Lenharth232c9102006-06-12 16:07:18 +00005608 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005609 ii != ee; ++ii)
5610 AddToWorkList(*ii);
5611 return S;
Nate Begeman69575232005-10-20 02:15:44 +00005612}
5613
5614/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
5615/// return a DAG expression to select that will generate the same value by
5616/// multiplying by a magic number. See:
5617/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Dan Gohman475871a2008-07-27 21:46:04 +00005618SDValue DAGCombiner::BuildUDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00005619 std::vector<SDNode*> Built;
Dan Gohman475871a2008-07-27 21:46:04 +00005620 SDValue S = TLI.BuildUDIV(N, DAG, &Built);
Nate Begeman69575232005-10-20 02:15:44 +00005621
Andrew Lenharth232c9102006-06-12 16:07:18 +00005622 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005623 ii != ee; ++ii)
5624 AddToWorkList(*ii);
5625 return S;
Nate Begeman69575232005-10-20 02:15:44 +00005626}
5627
Jim Laskey71382342006-10-07 23:37:56 +00005628/// FindBaseOffset - Return true if base is known not to alias with anything
5629/// but itself. Provides base object and offset as results.
Dan Gohman475871a2008-07-27 21:46:04 +00005630static bool FindBaseOffset(SDValue Ptr, SDValue &Base, int64_t &Offset) {
Jim Laskey71382342006-10-07 23:37:56 +00005631 // Assume it is a primitive operation.
5632 Base = Ptr; Offset = 0;
5633
5634 // If it's an adding a simple constant then integrate the offset.
5635 if (Base.getOpcode() == ISD::ADD) {
5636 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Base.getOperand(1))) {
5637 Base = Base.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005638 Offset += C->getZExtValue();
Jim Laskey71382342006-10-07 23:37:56 +00005639 }
5640 }
5641
5642 // If it's any of the following then it can't alias with anything but itself.
5643 return isa<FrameIndexSDNode>(Base) ||
5644 isa<ConstantPoolSDNode>(Base) ||
5645 isa<GlobalAddressSDNode>(Base);
5646}
5647
5648/// isAlias - Return true if there is any possibility that the two addresses
5649/// overlap.
Dan Gohman475871a2008-07-27 21:46:04 +00005650bool DAGCombiner::isAlias(SDValue Ptr1, int64_t Size1,
Jim Laskey096c22e2006-10-18 12:29:57 +00005651 const Value *SrcValue1, int SrcValueOffset1,
Dan Gohman475871a2008-07-27 21:46:04 +00005652 SDValue Ptr2, int64_t Size2,
Jim Laskey096c22e2006-10-18 12:29:57 +00005653 const Value *SrcValue2, int SrcValueOffset2)
5654{
Jim Laskey71382342006-10-07 23:37:56 +00005655 // If they are the same then they must be aliases.
5656 if (Ptr1 == Ptr2) return true;
5657
5658 // Gather base node and offset information.
Dan Gohman475871a2008-07-27 21:46:04 +00005659 SDValue Base1, Base2;
Jim Laskey71382342006-10-07 23:37:56 +00005660 int64_t Offset1, Offset2;
5661 bool KnownBase1 = FindBaseOffset(Ptr1, Base1, Offset1);
5662 bool KnownBase2 = FindBaseOffset(Ptr2, Base2, Offset2);
5663
5664 // If they have a same base address then...
5665 if (Base1 == Base2) {
5666 // Check to see if the addresses overlap.
5667 return!((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
5668 }
5669
Jim Laskey096c22e2006-10-18 12:29:57 +00005670 // If we know both bases then they can't alias.
5671 if (KnownBase1 && KnownBase2) return false;
5672
Jim Laskey07a27092006-10-18 19:08:31 +00005673 if (CombinerGlobalAA) {
5674 // Use alias analysis information.
Dan Gohmane9c8fa02007-08-27 16:32:11 +00005675 int64_t MinOffset = std::min(SrcValueOffset1, SrcValueOffset2);
5676 int64_t Overlap1 = Size1 + SrcValueOffset1 - MinOffset;
5677 int64_t Overlap2 = Size2 + SrcValueOffset2 - MinOffset;
Jim Laskey07a27092006-10-18 19:08:31 +00005678 AliasAnalysis::AliasResult AAResult =
Jim Laskey096c22e2006-10-18 12:29:57 +00005679 AA.alias(SrcValue1, Overlap1, SrcValue2, Overlap2);
Jim Laskey07a27092006-10-18 19:08:31 +00005680 if (AAResult == AliasAnalysis::NoAlias)
5681 return false;
5682 }
Jim Laskey096c22e2006-10-18 12:29:57 +00005683
5684 // Otherwise we have to assume they alias.
5685 return true;
Jim Laskey71382342006-10-07 23:37:56 +00005686}
5687
5688/// FindAliasInfo - Extracts the relevant alias information from the memory
5689/// node. Returns true if the operand was a load.
Jim Laskey7ca56af2006-10-11 13:47:09 +00005690bool DAGCombiner::FindAliasInfo(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +00005691 SDValue &Ptr, int64_t &Size,
Jim Laskey096c22e2006-10-18 12:29:57 +00005692 const Value *&SrcValue, int &SrcValueOffset) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00005693 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
5694 Ptr = LD->getBasePtr();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005695 Size = LD->getMemoryVT().getSizeInBits() >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005696 SrcValue = LD->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00005697 SrcValueOffset = LD->getSrcValueOffset();
Jim Laskey71382342006-10-07 23:37:56 +00005698 return true;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005699 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00005700 Ptr = ST->getBasePtr();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005701 Size = ST->getMemoryVT().getSizeInBits() >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005702 SrcValue = ST->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00005703 SrcValueOffset = ST->getSrcValueOffset();
Jim Laskey7ca56af2006-10-11 13:47:09 +00005704 } else {
Jim Laskey71382342006-10-07 23:37:56 +00005705 assert(0 && "FindAliasInfo expected a memory operand");
Jim Laskey71382342006-10-07 23:37:56 +00005706 }
5707
5708 return false;
5709}
5710
Jim Laskey6ff23e52006-10-04 16:53:27 +00005711/// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
5712/// looking for aliasing nodes and adding them to the Aliases vector.
Dan Gohman475871a2008-07-27 21:46:04 +00005713void DAGCombiner::GatherAllAliases(SDNode *N, SDValue OriginalChain,
5714 SmallVector<SDValue, 8> &Aliases) {
5715 SmallVector<SDValue, 8> Chains; // List of chains to visit.
Jim Laskey6ff23e52006-10-04 16:53:27 +00005716 std::set<SDNode *> Visited; // Visited node set.
5717
Jim Laskey279f0532006-09-25 16:29:54 +00005718 // Get alias information for node.
Dan Gohman475871a2008-07-27 21:46:04 +00005719 SDValue Ptr;
Jim Laskey279f0532006-09-25 16:29:54 +00005720 int64_t Size;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005721 const Value *SrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00005722 int SrcValueOffset;
5723 bool IsLoad = FindAliasInfo(N, Ptr, Size, SrcValue, SrcValueOffset);
Jim Laskey279f0532006-09-25 16:29:54 +00005724
Jim Laskey6ff23e52006-10-04 16:53:27 +00005725 // Starting off.
Jim Laskeybc588b82006-10-05 15:07:25 +00005726 Chains.push_back(OriginalChain);
Jim Laskey6ff23e52006-10-04 16:53:27 +00005727
Jim Laskeybc588b82006-10-05 15:07:25 +00005728 // Look at each chain and determine if it is an alias. If so, add it to the
5729 // aliases list. If not, then continue up the chain looking for the next
5730 // candidate.
5731 while (!Chains.empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005732 SDValue Chain = Chains.back();
Jim Laskeybc588b82006-10-05 15:07:25 +00005733 Chains.pop_back();
Jim Laskey6ff23e52006-10-04 16:53:27 +00005734
Jim Laskeybc588b82006-10-05 15:07:25 +00005735 // Don't bother if we've been before.
Gabor Greifba36cb52008-08-28 21:40:38 +00005736 if (Visited.find(Chain.getNode()) != Visited.end()) continue;
5737 Visited.insert(Chain.getNode());
Jim Laskeybc588b82006-10-05 15:07:25 +00005738
5739 switch (Chain.getOpcode()) {
5740 case ISD::EntryToken:
5741 // Entry token is ideal chain operand, but handled in FindBetterChain.
5742 break;
Jim Laskey6ff23e52006-10-04 16:53:27 +00005743
Jim Laskeybc588b82006-10-05 15:07:25 +00005744 case ISD::LOAD:
5745 case ISD::STORE: {
5746 // Get alias information for Chain.
Dan Gohman475871a2008-07-27 21:46:04 +00005747 SDValue OpPtr;
Jim Laskeybc588b82006-10-05 15:07:25 +00005748 int64_t OpSize;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005749 const Value *OpSrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00005750 int OpSrcValueOffset;
Gabor Greifba36cb52008-08-28 21:40:38 +00005751 bool IsOpLoad = FindAliasInfo(Chain.getNode(), OpPtr, OpSize,
Jim Laskey096c22e2006-10-18 12:29:57 +00005752 OpSrcValue, OpSrcValueOffset);
Jim Laskeybc588b82006-10-05 15:07:25 +00005753
5754 // If chain is alias then stop here.
5755 if (!(IsLoad && IsOpLoad) &&
Jim Laskey096c22e2006-10-18 12:29:57 +00005756 isAlias(Ptr, Size, SrcValue, SrcValueOffset,
5757 OpPtr, OpSize, OpSrcValue, OpSrcValueOffset)) {
Jim Laskeybc588b82006-10-05 15:07:25 +00005758 Aliases.push_back(Chain);
5759 } else {
5760 // Look further up the chain.
5761 Chains.push_back(Chain.getOperand(0));
5762 // Clean up old chain.
Gabor Greifba36cb52008-08-28 21:40:38 +00005763 AddToWorkList(Chain.getNode());
Jim Laskey279f0532006-09-25 16:29:54 +00005764 }
Jim Laskeybc588b82006-10-05 15:07:25 +00005765 break;
5766 }
5767
5768 case ISD::TokenFactor:
5769 // We have to check each of the operands of the token factor, so we queue
5770 // then up. Adding the operands to the queue (stack) in reverse order
5771 // maintains the original order and increases the likelihood that getNode
5772 // will find a matching token factor (CSE.)
5773 for (unsigned n = Chain.getNumOperands(); n;)
5774 Chains.push_back(Chain.getOperand(--n));
5775 // Eliminate the token factor if we can.
Gabor Greifba36cb52008-08-28 21:40:38 +00005776 AddToWorkList(Chain.getNode());
Jim Laskeybc588b82006-10-05 15:07:25 +00005777 break;
5778
5779 default:
5780 // For all other instructions we will just have to take what we can get.
5781 Aliases.push_back(Chain);
5782 break;
Jim Laskey279f0532006-09-25 16:29:54 +00005783 }
5784 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00005785}
5786
5787/// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, looking
5788/// for a better chain (aliasing node.)
Dan Gohman475871a2008-07-27 21:46:04 +00005789SDValue DAGCombiner::FindBetterChain(SDNode *N, SDValue OldChain) {
5790 SmallVector<SDValue, 8> Aliases; // Ops for replacing token factor.
Jim Laskey279f0532006-09-25 16:29:54 +00005791
Jim Laskey6ff23e52006-10-04 16:53:27 +00005792 // Accumulate all the aliases to this node.
5793 GatherAllAliases(N, OldChain, Aliases);
5794
5795 if (Aliases.size() == 0) {
5796 // If no operands then chain to entry token.
5797 return DAG.getEntryNode();
5798 } else if (Aliases.size() == 1) {
5799 // If a single operand then chain to it. We don't need to revisit it.
5800 return Aliases[0];
5801 }
5802
5803 // Construct a custom tailored token factor.
Dan Gohman475871a2008-07-27 21:46:04 +00005804 SDValue NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
Jim Laskey6ff23e52006-10-04 16:53:27 +00005805 &Aliases[0], Aliases.size());
5806
5807 // Make sure the old chain gets cleaned up.
Gabor Greifba36cb52008-08-28 21:40:38 +00005808 if (NewChain != OldChain) AddToWorkList(OldChain.getNode());
Jim Laskey6ff23e52006-10-04 16:53:27 +00005809
5810 return NewChain;
Jim Laskey279f0532006-09-25 16:29:54 +00005811}
5812
Nate Begeman1d4d4142005-09-01 00:19:25 +00005813// SelectionDAG::Combine - This is the entry point for the file.
5814//
Dan Gohmana2676512008-08-20 16:30:28 +00005815void SelectionDAG::Combine(bool RunningAfterLegalize, AliasAnalysis &AA,
5816 bool Fast) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00005817 /// run - This is the main entry point to this class.
5818 ///
Dan Gohmana2676512008-08-20 16:30:28 +00005819 DAGCombiner(*this, AA, Fast).Run(RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +00005820}