blob: d79dc7a3b58bb106f61455cba3868ddecf2784ab [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);
Chris Lattner01a22022005-10-10 22:04:48 +0000193
Dan Gohman475871a2008-07-27 21:46:04 +0000194 SDValue XformToShuffleWithZero(SDNode *N);
195 SDValue ReassociateOps(unsigned Opc, SDValue LHS, SDValue RHS);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000196
Dan Gohman475871a2008-07-27 21:46:04 +0000197 SDValue visitShiftByConstant(SDNode *N, unsigned Amt);
Chris Lattnere70da202007-12-06 07:33:36 +0000198
Dan Gohman475871a2008-07-27 21:46:04 +0000199 bool SimplifySelectOps(SDNode *SELECT, SDValue LHS, SDValue RHS);
200 SDValue SimplifyBinOpWithSameOpcodeHands(SDNode *N);
201 SDValue SimplifySelect(SDValue N0, SDValue N1, SDValue N2);
202 SDValue SimplifySelectCC(SDValue N0, SDValue N1, SDValue N2,
203 SDValue N3, ISD::CondCode CC,
Chris Lattner1eba01e2007-04-11 06:50:51 +0000204 bool NotExtCompare = false);
Dan Gohman475871a2008-07-27 21:46:04 +0000205 SDValue SimplifySetCC(MVT VT, SDValue N0, SDValue N1,
Nate Begemane17daeb2005-10-05 21:43:42 +0000206 ISD::CondCode Cond, bool foldBooleans = true);
Dan Gohman475871a2008-07-27 21:46:04 +0000207 SDValue SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
Chris Lattner5eee4272008-01-26 01:09:19 +0000208 unsigned HiOp);
Dan Gohman475871a2008-07-27 21:46:04 +0000209 SDValue CombineConsecutiveLoads(SDNode *N, MVT VT);
210 SDValue ConstantFoldBIT_CONVERTofBUILD_VECTOR(SDNode *, MVT);
211 SDValue BuildSDIV(SDNode *N);
212 SDValue BuildUDIV(SDNode *N);
213 SDNode *MatchRotate(SDValue LHS, SDValue RHS);
214 SDValue ReduceLoadWidth(SDNode *N);
Jim Laskey279f0532006-09-25 16:29:54 +0000215
Dan Gohman475871a2008-07-27 21:46:04 +0000216 SDValue GetDemandedBits(SDValue V, const APInt &Mask);
Chris Lattner2b4c2792007-10-13 06:35:54 +0000217
Jim Laskey6ff23e52006-10-04 16:53:27 +0000218 /// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
219 /// looking for aliasing nodes and adding them to the Aliases vector.
Dan Gohman475871a2008-07-27 21:46:04 +0000220 void GatherAllAliases(SDNode *N, SDValue OriginalChain,
221 SmallVector<SDValue, 8> &Aliases);
Jim Laskey6ff23e52006-10-04 16:53:27 +0000222
Jim Laskey096c22e2006-10-18 12:29:57 +0000223 /// isAlias - Return true if there is any possibility that the two addresses
224 /// overlap.
Dan Gohman475871a2008-07-27 21:46:04 +0000225 bool isAlias(SDValue Ptr1, int64_t Size1,
Jim Laskey096c22e2006-10-18 12:29:57 +0000226 const Value *SrcValue1, int SrcValueOffset1,
Dan Gohman475871a2008-07-27 21:46:04 +0000227 SDValue Ptr2, int64_t Size2,
Jeff Cohend41b30d2006-11-05 19:31:28 +0000228 const Value *SrcValue2, int SrcValueOffset2);
Jim Laskey096c22e2006-10-18 12:29:57 +0000229
Jim Laskey7ca56af2006-10-11 13:47:09 +0000230 /// FindAliasInfo - Extracts the relevant alias information from the memory
231 /// node. Returns true if the operand was a load.
232 bool FindAliasInfo(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +0000233 SDValue &Ptr, int64_t &Size,
Jim Laskey096c22e2006-10-18 12:29:57 +0000234 const Value *&SrcValue, int &SrcValueOffset);
Jim Laskey7ca56af2006-10-11 13:47:09 +0000235
Jim Laskey279f0532006-09-25 16:29:54 +0000236 /// FindBetterChain - Walk up chain skipping non-aliasing memory nodes,
Jim Laskey6ff23e52006-10-04 16:53:27 +0000237 /// looking for a better chain (aliasing node.)
Dan Gohman475871a2008-07-27 21:46:04 +0000238 SDValue FindBetterChain(SDNode *N, SDValue Chain);
Jim Laskey279f0532006-09-25 16:29:54 +0000239
Nate Begeman1d4d4142005-09-01 00:19:25 +0000240public:
Dan Gohmana2676512008-08-20 16:30:28 +0000241 DAGCombiner(SelectionDAG &D, AliasAnalysis &A, bool fast)
Jim Laskeyc7c3f112006-10-16 20:52:31 +0000242 : DAG(D),
243 TLI(D.getTargetLoweringInfo()),
244 AfterLegalize(false),
Dan Gohmana2676512008-08-20 16:30:28 +0000245 Fast(fast),
Jim Laskeyc7c3f112006-10-16 20:52:31 +0000246 AA(A) {}
Nate Begeman1d4d4142005-09-01 00:19:25 +0000247
248 /// Run - runs the dag combiner on all nodes in the work list
Nate Begeman4ebd8052005-09-01 23:24:04 +0000249 void Run(bool RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000250 };
251}
252
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000253
254namespace {
255/// WorkListRemover - This class is a DAGUpdateListener that removes any deleted
256/// nodes from the worklist.
257class VISIBILITY_HIDDEN WorkListRemover :
258 public SelectionDAG::DAGUpdateListener {
259 DAGCombiner &DC;
260public:
Dan Gohmanb5660dc2008-02-20 16:44:09 +0000261 explicit WorkListRemover(DAGCombiner &dc) : DC(dc) {}
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000262
Duncan Sandsedfcf592008-06-11 11:42:12 +0000263 virtual void NodeDeleted(SDNode *N, SDNode *E) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000264 DC.removeFromWorkList(N);
265 }
266
267 virtual void NodeUpdated(SDNode *N) {
268 // Ignore updates.
269 }
270};
271}
272
Chris Lattner24664722006-03-01 04:53:38 +0000273//===----------------------------------------------------------------------===//
274// TargetLowering::DAGCombinerInfo implementation
275//===----------------------------------------------------------------------===//
276
277void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) {
278 ((DAGCombiner*)DC)->AddToWorkList(N);
279}
280
Dan Gohman475871a2008-07-27 21:46:04 +0000281SDValue TargetLowering::DAGCombinerInfo::
282CombineTo(SDNode *N, const std::vector<SDValue> &To) {
Chris Lattner3577e382006-08-11 17:56:38 +0000283 return ((DAGCombiner*)DC)->CombineTo(N, &To[0], To.size());
Chris Lattner24664722006-03-01 04:53:38 +0000284}
285
Dan Gohman475871a2008-07-27 21:46:04 +0000286SDValue TargetLowering::DAGCombinerInfo::
287CombineTo(SDNode *N, SDValue Res) {
Chris Lattner24664722006-03-01 04:53:38 +0000288 return ((DAGCombiner*)DC)->CombineTo(N, Res);
289}
290
291
Dan Gohman475871a2008-07-27 21:46:04 +0000292SDValue TargetLowering::DAGCombinerInfo::
293CombineTo(SDNode *N, SDValue Res0, SDValue Res1) {
Chris Lattner24664722006-03-01 04:53:38 +0000294 return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1);
295}
296
297
Chris Lattner24664722006-03-01 04:53:38 +0000298//===----------------------------------------------------------------------===//
Chris Lattner29446522007-05-14 22:04:50 +0000299// Helper Functions
300//===----------------------------------------------------------------------===//
301
302/// isNegatibleForFree - Return 1 if we can compute the negated form of the
303/// specified expression for the same cost as the expression itself, or 2 if we
304/// can compute the negated form more cheaply than the expression itself.
Dan Gohman475871a2008-07-27 21:46:04 +0000305static char isNegatibleForFree(SDValue Op, bool AfterLegalize,
Chris Lattner0254e702008-02-26 07:04:54 +0000306 unsigned Depth = 0) {
Dale Johannesendb44bf82007-10-16 23:38:29 +0000307 // No compile time optimizations on this type.
308 if (Op.getValueType() == MVT::ppcf128)
309 return 0;
310
Chris Lattner29446522007-05-14 22:04:50 +0000311 // fneg is removable even if it has multiple uses.
312 if (Op.getOpcode() == ISD::FNEG) return 2;
313
314 // Don't allow anything with multiple uses.
315 if (!Op.hasOneUse()) return 0;
316
Chris Lattner3adf9512007-05-25 02:19:06 +0000317 // Don't recurse exponentially.
318 if (Depth > 6) return 0;
319
Chris Lattner29446522007-05-14 22:04:50 +0000320 switch (Op.getOpcode()) {
321 default: return false;
322 case ISD::ConstantFP:
Chris Lattner0254e702008-02-26 07:04:54 +0000323 // Don't invert constant FP values after legalize. The negated constant
324 // isn't necessarily legal.
325 return AfterLegalize ? 0 : 1;
Chris Lattner29446522007-05-14 22:04:50 +0000326 case ISD::FADD:
327 // FIXME: determine better conditions for this xform.
328 if (!UnsafeFPMath) return 0;
329
330 // -(A+B) -> -A - B
Chris Lattner0254e702008-02-26 07:04:54 +0000331 if (char V = isNegatibleForFree(Op.getOperand(0), AfterLegalize, Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000332 return V;
333 // -(A+B) -> -B - A
Chris Lattner0254e702008-02-26 07:04:54 +0000334 return isNegatibleForFree(Op.getOperand(1), AfterLegalize, Depth+1);
Chris Lattner29446522007-05-14 22:04:50 +0000335 case ISD::FSUB:
336 // We can't turn -(A-B) into B-A when we honor signed zeros.
337 if (!UnsafeFPMath) return 0;
338
339 // -(A-B) -> B-A
340 return 1;
341
342 case ISD::FMUL:
343 case ISD::FDIV:
344 if (HonorSignDependentRoundingFPMath()) return 0;
345
346 // -(X*Y) -> (-X * Y) or (X*-Y)
Chris Lattner0254e702008-02-26 07:04:54 +0000347 if (char V = isNegatibleForFree(Op.getOperand(0), AfterLegalize, Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000348 return V;
349
Chris Lattner0254e702008-02-26 07:04:54 +0000350 return isNegatibleForFree(Op.getOperand(1), AfterLegalize, Depth+1);
Chris Lattner29446522007-05-14 22:04:50 +0000351
352 case ISD::FP_EXTEND:
353 case ISD::FP_ROUND:
354 case ISD::FSIN:
Chris Lattner0254e702008-02-26 07:04:54 +0000355 return isNegatibleForFree(Op.getOperand(0), AfterLegalize, Depth+1);
Chris Lattner29446522007-05-14 22:04:50 +0000356 }
357}
358
359/// GetNegatedExpression - If isNegatibleForFree returns true, this function
360/// returns the newly negated expression.
Dan Gohman475871a2008-07-27 21:46:04 +0000361static SDValue GetNegatedExpression(SDValue Op, SelectionDAG &DAG,
Chris Lattner0254e702008-02-26 07:04:54 +0000362 bool AfterLegalize, unsigned Depth = 0) {
Chris Lattner29446522007-05-14 22:04:50 +0000363 // fneg is removable even if it has multiple uses.
364 if (Op.getOpcode() == ISD::FNEG) return Op.getOperand(0);
365
366 // Don't allow anything with multiple uses.
367 assert(Op.hasOneUse() && "Unknown reuse!");
368
Chris Lattner3adf9512007-05-25 02:19:06 +0000369 assert(Depth <= 6 && "GetNegatedExpression doesn't match isNegatibleForFree");
Chris Lattner29446522007-05-14 22:04:50 +0000370 switch (Op.getOpcode()) {
371 default: assert(0 && "Unknown code");
Dale Johannesenc4dd3c32007-08-31 23:34:27 +0000372 case ISD::ConstantFP: {
373 APFloat V = cast<ConstantFPSDNode>(Op)->getValueAPF();
374 V.changeSign();
375 return DAG.getConstantFP(V, Op.getValueType());
376 }
Chris Lattner29446522007-05-14 22:04:50 +0000377 case ISD::FADD:
378 // FIXME: determine better conditions for this xform.
379 assert(UnsafeFPMath);
380
381 // -(A+B) -> -A - B
Chris Lattner0254e702008-02-26 07:04:54 +0000382 if (isNegatibleForFree(Op.getOperand(0), AfterLegalize, Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000383 return DAG.getNode(ISD::FSUB, Op.getValueType(),
Chris Lattner0254e702008-02-26 07:04:54 +0000384 GetNegatedExpression(Op.getOperand(0), DAG,
385 AfterLegalize, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000386 Op.getOperand(1));
387 // -(A+B) -> -B - A
388 return DAG.getNode(ISD::FSUB, Op.getValueType(),
Chris Lattner0254e702008-02-26 07:04:54 +0000389 GetNegatedExpression(Op.getOperand(1), DAG,
390 AfterLegalize, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000391 Op.getOperand(0));
392 case ISD::FSUB:
393 // We can't turn -(A-B) into B-A when we honor signed zeros.
394 assert(UnsafeFPMath);
Dan Gohman23ff1822007-07-02 15:48:56 +0000395
396 // -(0-B) -> B
397 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(Op.getOperand(0)))
Dale Johannesenc4dd3c32007-08-31 23:34:27 +0000398 if (N0CFP->getValueAPF().isZero())
Dan Gohman23ff1822007-07-02 15:48:56 +0000399 return Op.getOperand(1);
Chris Lattner29446522007-05-14 22:04:50 +0000400
401 // -(A-B) -> B-A
402 return DAG.getNode(ISD::FSUB, Op.getValueType(), Op.getOperand(1),
403 Op.getOperand(0));
404
405 case ISD::FMUL:
406 case ISD::FDIV:
407 assert(!HonorSignDependentRoundingFPMath());
408
409 // -(X*Y) -> -X * Y
Chris Lattneraeecb6c2008-02-26 17:09:59 +0000410 if (isNegatibleForFree(Op.getOperand(0), AfterLegalize, Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000411 return DAG.getNode(Op.getOpcode(), Op.getValueType(),
Chris Lattner0254e702008-02-26 07:04:54 +0000412 GetNegatedExpression(Op.getOperand(0), DAG,
413 AfterLegalize, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000414 Op.getOperand(1));
415
416 // -(X*Y) -> X * -Y
417 return DAG.getNode(Op.getOpcode(), Op.getValueType(),
418 Op.getOperand(0),
Chris Lattner0254e702008-02-26 07:04:54 +0000419 GetNegatedExpression(Op.getOperand(1), DAG,
420 AfterLegalize, Depth+1));
Chris Lattner29446522007-05-14 22:04:50 +0000421
422 case ISD::FP_EXTEND:
Chris Lattner29446522007-05-14 22:04:50 +0000423 case ISD::FSIN:
424 return DAG.getNode(Op.getOpcode(), Op.getValueType(),
Chris Lattner0254e702008-02-26 07:04:54 +0000425 GetNegatedExpression(Op.getOperand(0), DAG,
426 AfterLegalize, Depth+1));
Chris Lattner0bd48932008-01-17 07:00:52 +0000427 case ISD::FP_ROUND:
428 return DAG.getNode(ISD::FP_ROUND, Op.getValueType(),
Chris Lattner0254e702008-02-26 07:04:54 +0000429 GetNegatedExpression(Op.getOperand(0), DAG,
430 AfterLegalize, Depth+1),
Chris Lattner0bd48932008-01-17 07:00:52 +0000431 Op.getOperand(1));
Chris Lattner29446522007-05-14 22:04:50 +0000432 }
433}
Chris Lattner24664722006-03-01 04:53:38 +0000434
435
Nate Begeman4ebd8052005-09-01 23:24:04 +0000436// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
437// that selects between the values 1 and 0, making it equivalent to a setcc.
Nate Begeman646d7e22005-09-02 21:18:40 +0000438// Also, set the incoming LHS, RHS, and CC references to the appropriate
439// nodes based on the type of node we are checking. This simplifies life a
440// bit for the callers.
Dan Gohman475871a2008-07-27 21:46:04 +0000441static bool isSetCCEquivalent(SDValue N, SDValue &LHS, SDValue &RHS,
442 SDValue &CC) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000443 if (N.getOpcode() == ISD::SETCC) {
444 LHS = N.getOperand(0);
445 RHS = N.getOperand(1);
446 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000447 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000448 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000449 if (N.getOpcode() == ISD::SELECT_CC &&
450 N.getOperand(2).getOpcode() == ISD::Constant &&
451 N.getOperand(3).getOpcode() == ISD::Constant &&
Dan Gohman002e5d02008-03-13 22:13:53 +0000452 cast<ConstantSDNode>(N.getOperand(2))->getAPIntValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000453 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
454 LHS = N.getOperand(0);
455 RHS = N.getOperand(1);
456 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000457 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000458 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000459 return false;
460}
461
Nate Begeman99801192005-09-07 23:25:52 +0000462// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
463// one use. If this is true, it allows the users to invert the operation for
464// free when it is profitable to do so.
Dan Gohman475871a2008-07-27 21:46:04 +0000465static bool isOneUseSetCC(SDValue N) {
466 SDValue N0, N1, N2;
Gabor Greifba36cb52008-08-28 21:40:38 +0000467 if (isSetCCEquivalent(N, N0, N1, N2) && N.getNode()->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000468 return true;
469 return false;
470}
471
Dan Gohman475871a2008-07-27 21:46:04 +0000472SDValue DAGCombiner::ReassociateOps(unsigned Opc, SDValue N0, SDValue N1){
Duncan Sands83ec4b62008-06-06 12:08:01 +0000473 MVT VT = N0.getValueType();
Nate Begemancd4d58c2006-02-03 06:46:56 +0000474 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
475 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
476 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
477 if (isa<ConstantSDNode>(N1)) {
Dan Gohman475871a2008-07-27 21:46:04 +0000478 SDValue OpNode = DAG.getNode(Opc, VT, N0.getOperand(1), N1);
Gabor Greifba36cb52008-08-28 21:40:38 +0000479 AddToWorkList(OpNode.getNode());
Nate Begemancd4d58c2006-02-03 06:46:56 +0000480 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(0));
481 } else if (N0.hasOneUse()) {
Dan Gohman475871a2008-07-27 21:46:04 +0000482 SDValue OpNode = DAG.getNode(Opc, VT, N0.getOperand(0), N1);
Gabor Greifba36cb52008-08-28 21:40:38 +0000483 AddToWorkList(OpNode.getNode());
Nate Begemancd4d58c2006-02-03 06:46:56 +0000484 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(1));
485 }
486 }
487 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
488 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
489 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
490 if (isa<ConstantSDNode>(N0)) {
Dan Gohman475871a2008-07-27 21:46:04 +0000491 SDValue OpNode = DAG.getNode(Opc, VT, N1.getOperand(1), N0);
Gabor Greifba36cb52008-08-28 21:40:38 +0000492 AddToWorkList(OpNode.getNode());
Nate Begemancd4d58c2006-02-03 06:46:56 +0000493 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(0));
494 } else if (N1.hasOneUse()) {
Dan Gohman475871a2008-07-27 21:46:04 +0000495 SDValue OpNode = DAG.getNode(Opc, VT, N1.getOperand(0), N0);
Gabor Greifba36cb52008-08-28 21:40:38 +0000496 AddToWorkList(OpNode.getNode());
Nate Begemancd4d58c2006-02-03 06:46:56 +0000497 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(1));
498 }
499 }
Dan Gohman475871a2008-07-27 21:46:04 +0000500 return SDValue();
Nate Begemancd4d58c2006-02-03 06:46:56 +0000501}
502
Dan Gohman475871a2008-07-27 21:46:04 +0000503SDValue DAGCombiner::CombineTo(SDNode *N, const SDValue *To, unsigned NumTo,
504 bool AddTo) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000505 assert(N->getNumValues() == NumTo && "Broken CombineTo call!");
506 ++NodesCombined;
507 DOUT << "\nReplacing.1 "; DEBUG(N->dump(&DAG));
Gabor Greifba36cb52008-08-28 21:40:38 +0000508 DOUT << "\nWith: "; DEBUG(To[0].getNode()->dump(&DAG));
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000509 DOUT << " and " << NumTo-1 << " other values\n";
510 WorkListRemover DeadNodes(*this);
511 DAG.ReplaceAllUsesWith(N, To, &DeadNodes);
512
513 if (AddTo) {
514 // Push the new nodes and any users onto the worklist
515 for (unsigned i = 0, e = NumTo; i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000516 AddToWorkList(To[i].getNode());
517 AddUsersToWorkList(To[i].getNode());
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000518 }
519 }
520
521 // Nodes can be reintroduced into the worklist. Make sure we do not
522 // process a node that has been replaced.
523 removeFromWorkList(N);
524
525 // Finally, since the node is now dead, remove it from the graph.
526 DAG.DeleteNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +0000527 return SDValue(N, 0);
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000528}
529
530/// SimplifyDemandedBits - Check the specified integer node value to see if
531/// it can be simplified or if things it uses can be simplified by bit
532/// propagation. If so, return true.
Dan Gohman475871a2008-07-27 21:46:04 +0000533bool DAGCombiner::SimplifyDemandedBits(SDValue Op, const APInt &Demanded) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000534 TargetLowering::TargetLoweringOpt TLO(DAG, AfterLegalize);
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000535 APInt KnownZero, KnownOne;
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000536 if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO))
537 return false;
538
539 // Revisit the node.
Gabor Greifba36cb52008-08-28 21:40:38 +0000540 AddToWorkList(Op.getNode());
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000541
542 // Replace the old value with the new one.
543 ++NodesCombined;
Gabor Greifba36cb52008-08-28 21:40:38 +0000544 DOUT << "\nReplacing.2 "; DEBUG(TLO.Old.getNode()->dump(&DAG));
545 DOUT << "\nWith: "; DEBUG(TLO.New.getNode()->dump(&DAG));
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000546 DOUT << '\n';
547
548 // Replace all uses. If any nodes become isomorphic to other nodes and
549 // are deleted, make sure to remove them from our worklist.
550 WorkListRemover DeadNodes(*this);
551 DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New, &DeadNodes);
552
553 // Push the new node and any (possibly new) users onto the worklist.
Gabor Greifba36cb52008-08-28 21:40:38 +0000554 AddToWorkList(TLO.New.getNode());
555 AddUsersToWorkList(TLO.New.getNode());
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000556
557 // Finally, if the node is now dead, remove it from the graph. The node
558 // may not be dead if the replacement process recursively simplified to
559 // something else needing this node.
Gabor Greifba36cb52008-08-28 21:40:38 +0000560 if (TLO.Old.getNode()->use_empty()) {
561 removeFromWorkList(TLO.Old.getNode());
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000562
563 // If the operands of this node are only used by the node, they will now
564 // be dead. Make sure to visit them first to delete dead nodes early.
Gabor Greifba36cb52008-08-28 21:40:38 +0000565 for (unsigned i = 0, e = TLO.Old.getNode()->getNumOperands(); i != e; ++i)
566 if (TLO.Old.getNode()->getOperand(i).getNode()->hasOneUse())
567 AddToWorkList(TLO.Old.getNode()->getOperand(i).getNode());
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000568
Gabor Greifba36cb52008-08-28 21:40:38 +0000569 DAG.DeleteNode(TLO.Old.getNode());
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000570 }
571 return true;
572}
573
Chris Lattner29446522007-05-14 22:04:50 +0000574//===----------------------------------------------------------------------===//
575// Main DAG Combiner implementation
576//===----------------------------------------------------------------------===//
577
Nate Begeman4ebd8052005-09-01 23:24:04 +0000578void DAGCombiner::Run(bool RunningAfterLegalize) {
579 // set the instance variable, so that the various visit routines may use it.
580 AfterLegalize = RunningAfterLegalize;
581
Evan Cheng17a568b2008-08-29 22:21:44 +0000582 // Add all the dag nodes to the worklist.
583 WorkList.reserve(DAG.allnodes_size());
584 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
585 E = DAG.allnodes_end(); I != E; ++I)
586 WorkList.push_back(I);
587
588 // Create a dummy node (which is not added to allnodes), that adds a reference
589 // to the root node, preventing it from being deleted, and tracking any
590 // changes of the root.
591 HandleSDNode Dummy(DAG.getRoot());
592
Jim Laskey26f7fa72006-10-17 19:33:52 +0000593 // The root of the dag may dangle to deleted nodes until the dag combiner is
594 // done. Set it to null to avoid confusion.
Dan Gohman475871a2008-07-27 21:46:04 +0000595 DAG.setRoot(SDValue());
Chris Lattner24664722006-03-01 04:53:38 +0000596
Evan Cheng17a568b2008-08-29 22:21:44 +0000597 // while the worklist isn't empty, inspect the node on the end of it and
598 // try and combine it.
599 while (!WorkList.empty()) {
600 SDNode *N = WorkList.back();
601 WorkList.pop_back();
602
603 // If N has no uses, it is dead. Make sure to revisit all N's operands once
604 // N is deleted from the DAG, since they too may now be dead or may have a
605 // reduced number of uses, allowing other xforms.
606 if (N->use_empty() && N != &Dummy) {
607 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
608 AddToWorkList(N->getOperand(i).getNode());
609
610 DAG.DeleteNode(N);
611 continue;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000612 }
Evan Cheng17a568b2008-08-29 22:21:44 +0000613
614 SDValue RV = combine(N);
615
616 if (RV.getNode() == 0)
617 continue;
618
619 ++NodesCombined;
620
621 // If we get back the same node we passed in, rather than a new node or
622 // zero, we know that the node must have defined multiple values and
623 // CombineTo was used. Since CombineTo takes care of the worklist
624 // mechanics for us, we have no work to do in this case.
625 if (RV.getNode() == N)
626 continue;
627
628 assert(N->getOpcode() != ISD::DELETED_NODE &&
629 RV.getNode()->getOpcode() != ISD::DELETED_NODE &&
630 "Node was deleted but visit returned new node!");
Chris Lattner729c6d12006-05-27 00:43:02 +0000631
Evan Cheng17a568b2008-08-29 22:21:44 +0000632 DOUT << "\nReplacing.3 "; DEBUG(N->dump(&DAG));
633 DOUT << "\nWith: "; DEBUG(RV.getNode()->dump(&DAG));
634 DOUT << '\n';
635 WorkListRemover DeadNodes(*this);
636 if (N->getNumValues() == RV.getNode()->getNumValues())
637 DAG.ReplaceAllUsesWith(N, RV.getNode(), &DeadNodes);
638 else {
639 assert(N->getValueType(0) == RV.getValueType() &&
640 N->getNumValues() == 1 && "Type mismatch");
641 SDValue OpV = RV;
642 DAG.ReplaceAllUsesWith(N, &OpV, &DeadNodes);
643 }
644
645 // Push the new node and any users onto the worklist
646 AddToWorkList(RV.getNode());
647 AddUsersToWorkList(RV.getNode());
648
649 // Add any uses of the old node to the worklist in case this node is the
650 // last one that uses them. They may become dead after this node is
651 // deleted.
652 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
653 AddToWorkList(N->getOperand(i).getNode());
654
655 // Nodes can be reintroduced into the worklist. Make sure we do not
656 // process a node that has been replaced.
657 removeFromWorkList(N);
658
659 // Finally, since the node is now dead, remove it from the graph.
660 DAG.DeleteNode(N);
661 }
662
Chris Lattner95038592005-10-05 06:35:28 +0000663 // If the root changed (e.g. it was a dead load, update the root).
664 DAG.setRoot(Dummy.getValue());
Nate Begeman1d4d4142005-09-01 00:19:25 +0000665}
666
Dan Gohman475871a2008-07-27 21:46:04 +0000667SDValue DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000668 switch(N->getOpcode()) {
669 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +0000670 case ISD::TokenFactor: return visitTokenFactor(N);
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000671 case ISD::MERGE_VALUES: return visitMERGE_VALUES(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000672 case ISD::ADD: return visitADD(N);
673 case ISD::SUB: return visitSUB(N);
Chris Lattner91153682007-03-04 20:03:15 +0000674 case ISD::ADDC: return visitADDC(N);
675 case ISD::ADDE: return visitADDE(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000676 case ISD::MUL: return visitMUL(N);
677 case ISD::SDIV: return visitSDIV(N);
678 case ISD::UDIV: return visitUDIV(N);
679 case ISD::SREM: return visitSREM(N);
680 case ISD::UREM: return visitUREM(N);
681 case ISD::MULHU: return visitMULHU(N);
682 case ISD::MULHS: return visitMULHS(N);
Dan Gohman389079b2007-10-08 17:57:15 +0000683 case ISD::SMUL_LOHI: return visitSMUL_LOHI(N);
684 case ISD::UMUL_LOHI: return visitUMUL_LOHI(N);
685 case ISD::SDIVREM: return visitSDIVREM(N);
686 case ISD::UDIVREM: return visitUDIVREM(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000687 case ISD::AND: return visitAND(N);
688 case ISD::OR: return visitOR(N);
689 case ISD::XOR: return visitXOR(N);
690 case ISD::SHL: return visitSHL(N);
691 case ISD::SRA: return visitSRA(N);
692 case ISD::SRL: return visitSRL(N);
693 case ISD::CTLZ: return visitCTLZ(N);
694 case ISD::CTTZ: return visitCTTZ(N);
695 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000696 case ISD::SELECT: return visitSELECT(N);
697 case ISD::SELECT_CC: return visitSELECT_CC(N);
698 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000699 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
700 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000701 case ISD::ANY_EXTEND: return visitANY_EXTEND(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000702 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
703 case ISD::TRUNCATE: return visitTRUNCATE(N);
Chris Lattner94683772005-12-23 05:30:37 +0000704 case ISD::BIT_CONVERT: return visitBIT_CONVERT(N);
Evan Cheng9bfa03c2008-05-12 23:04:07 +0000705 case ISD::BUILD_PAIR: return visitBUILD_PAIR(N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000706 case ISD::FADD: return visitFADD(N);
707 case ISD::FSUB: return visitFSUB(N);
708 case ISD::FMUL: return visitFMUL(N);
709 case ISD::FDIV: return visitFDIV(N);
710 case ISD::FREM: return visitFREM(N);
Chris Lattner12d83032006-03-05 05:30:57 +0000711 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000712 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
713 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
714 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
715 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
716 case ISD::FP_ROUND: return visitFP_ROUND(N);
717 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
718 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
719 case ISD::FNEG: return visitFNEG(N);
720 case ISD::FABS: return visitFABS(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000721 case ISD::BRCOND: return visitBRCOND(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000722 case ISD::BR_CC: return visitBR_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000723 case ISD::LOAD: return visitLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000724 case ISD::STORE: return visitSTORE(N);
Chris Lattnerca242442006-03-19 01:27:56 +0000725 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
Evan Cheng513da432007-10-06 08:19:55 +0000726 case ISD::EXTRACT_VECTOR_ELT: return visitEXTRACT_VECTOR_ELT(N);
Dan Gohman7f321562007-06-25 16:23:39 +0000727 case ISD::BUILD_VECTOR: return visitBUILD_VECTOR(N);
728 case ISD::CONCAT_VECTORS: return visitCONCAT_VECTORS(N);
Chris Lattner66445d32006-03-28 22:11:53 +0000729 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000730 }
Dan Gohman475871a2008-07-27 21:46:04 +0000731 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000732}
733
Dan Gohman475871a2008-07-27 21:46:04 +0000734SDValue DAGCombiner::combine(SDNode *N) {
Dan Gohman389079b2007-10-08 17:57:15 +0000735
Dan Gohman475871a2008-07-27 21:46:04 +0000736 SDValue RV = visit(N);
Dan Gohman389079b2007-10-08 17:57:15 +0000737
738 // If nothing happened, try a target-specific DAG combine.
Gabor Greifba36cb52008-08-28 21:40:38 +0000739 if (RV.getNode() == 0) {
Dan Gohman389079b2007-10-08 17:57:15 +0000740 assert(N->getOpcode() != ISD::DELETED_NODE &&
741 "Node was deleted but visit returned NULL!");
742
743 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
744 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode())) {
745
746 // Expose the DAG combiner to the target combiner impls.
747 TargetLowering::DAGCombinerInfo
748 DagCombineInfo(DAG, !AfterLegalize, false, this);
749
750 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
751 }
752 }
753
Evan Cheng08b11732008-03-22 01:55:50 +0000754 // If N is a commutative binary node, try commuting it to enable more
755 // sdisel CSE.
Gabor Greifba36cb52008-08-28 21:40:38 +0000756 if (RV.getNode() == 0 &&
Evan Cheng08b11732008-03-22 01:55:50 +0000757 SelectionDAG::isCommutativeBinOp(N->getOpcode()) &&
758 N->getNumValues() == 1) {
Dan Gohman475871a2008-07-27 21:46:04 +0000759 SDValue N0 = N->getOperand(0);
760 SDValue N1 = N->getOperand(1);
Evan Cheng08b11732008-03-22 01:55:50 +0000761 // Constant operands are canonicalized to RHS.
762 if (isa<ConstantSDNode>(N0) || !isa<ConstantSDNode>(N1)) {
Dan Gohman475871a2008-07-27 21:46:04 +0000763 SDValue Ops[] = { N1, N0 };
Evan Cheng08b11732008-03-22 01:55:50 +0000764 SDNode *CSENode = DAG.getNodeIfExists(N->getOpcode(), N->getVTList(),
765 Ops, 2);
Evan Chengea100462008-03-24 23:55:16 +0000766 if (CSENode)
Dan Gohman475871a2008-07-27 21:46:04 +0000767 return SDValue(CSENode, 0);
Evan Cheng08b11732008-03-22 01:55:50 +0000768 }
769 }
770
Dan Gohman389079b2007-10-08 17:57:15 +0000771 return RV;
772}
773
Chris Lattner6270f682006-10-08 22:57:01 +0000774/// getInputChainForNode - Given a node, return its input chain if it has one,
775/// otherwise return a null sd operand.
Dan Gohman475871a2008-07-27 21:46:04 +0000776static SDValue getInputChainForNode(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +0000777 if (unsigned NumOps = N->getNumOperands()) {
778 if (N->getOperand(0).getValueType() == MVT::Other)
779 return N->getOperand(0);
780 else if (N->getOperand(NumOps-1).getValueType() == MVT::Other)
781 return N->getOperand(NumOps-1);
782 for (unsigned i = 1; i < NumOps-1; ++i)
783 if (N->getOperand(i).getValueType() == MVT::Other)
784 return N->getOperand(i);
785 }
Dan Gohman475871a2008-07-27 21:46:04 +0000786 return SDValue(0, 0);
Chris Lattner6270f682006-10-08 22:57:01 +0000787}
788
Dan Gohman475871a2008-07-27 21:46:04 +0000789SDValue DAGCombiner::visitTokenFactor(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +0000790 // If N has two operands, where one has an input chain equal to the other,
791 // the 'other' chain is redundant.
792 if (N->getNumOperands() == 2) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000793 if (getInputChainForNode(N->getOperand(0).getNode()) == N->getOperand(1))
Chris Lattner6270f682006-10-08 22:57:01 +0000794 return N->getOperand(0);
Gabor Greifba36cb52008-08-28 21:40:38 +0000795 if (getInputChainForNode(N->getOperand(1).getNode()) == N->getOperand(0))
Chris Lattner6270f682006-10-08 22:57:01 +0000796 return N->getOperand(1);
797 }
798
Chris Lattnerc76d4412007-05-16 06:37:59 +0000799 SmallVector<SDNode *, 8> TFs; // List of token factors to visit.
Dan Gohman475871a2008-07-27 21:46:04 +0000800 SmallVector<SDValue, 8> Ops; // Ops for replacing token factor.
Chris Lattnerc76d4412007-05-16 06:37:59 +0000801 SmallPtrSet<SDNode*, 16> SeenOps;
802 bool Changed = false; // If we should replace this token factor.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000803
804 // Start out with this token factor.
Jim Laskey279f0532006-09-25 16:29:54 +0000805 TFs.push_back(N);
Jim Laskey279f0532006-09-25 16:29:54 +0000806
Jim Laskey71382342006-10-07 23:37:56 +0000807 // Iterate through token factors. The TFs grows when new token factors are
Jim Laskeybc588b82006-10-05 15:07:25 +0000808 // encountered.
809 for (unsigned i = 0; i < TFs.size(); ++i) {
810 SDNode *TF = TFs[i];
811
Jim Laskey6ff23e52006-10-04 16:53:27 +0000812 // Check each of the operands.
813 for (unsigned i = 0, ie = TF->getNumOperands(); i != ie; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +0000814 SDValue Op = TF->getOperand(i);
Jim Laskey279f0532006-09-25 16:29:54 +0000815
Jim Laskey6ff23e52006-10-04 16:53:27 +0000816 switch (Op.getOpcode()) {
817 case ISD::EntryToken:
Jim Laskeybc588b82006-10-05 15:07:25 +0000818 // Entry tokens don't need to be added to the list. They are
819 // rededundant.
820 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000821 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000822
Jim Laskey6ff23e52006-10-04 16:53:27 +0000823 case ISD::TokenFactor:
Jim Laskeybc588b82006-10-05 15:07:25 +0000824 if ((CombinerAA || Op.hasOneUse()) &&
Gabor Greifba36cb52008-08-28 21:40:38 +0000825 std::find(TFs.begin(), TFs.end(), Op.getNode()) == TFs.end()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +0000826 // Queue up for processing.
Gabor Greifba36cb52008-08-28 21:40:38 +0000827 TFs.push_back(Op.getNode());
Jim Laskey6ff23e52006-10-04 16:53:27 +0000828 // Clean up in case the token factor is removed.
Gabor Greifba36cb52008-08-28 21:40:38 +0000829 AddToWorkList(Op.getNode());
Jim Laskey6ff23e52006-10-04 16:53:27 +0000830 Changed = true;
831 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000832 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000833 // Fall thru
834
835 default:
Chris Lattnerc76d4412007-05-16 06:37:59 +0000836 // Only add if it isn't already in the list.
Gabor Greifba36cb52008-08-28 21:40:38 +0000837 if (SeenOps.insert(Op.getNode()))
Jim Laskeybc588b82006-10-05 15:07:25 +0000838 Ops.push_back(Op);
Chris Lattnerc76d4412007-05-16 06:37:59 +0000839 else
840 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000841 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000842 }
843 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000844 }
845
Dan Gohman475871a2008-07-27 21:46:04 +0000846 SDValue Result;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000847
848 // If we've change things around then replace token factor.
849 if (Changed) {
Dan Gohman30359592008-01-29 13:02:09 +0000850 if (Ops.empty()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +0000851 // The entry token is the only possible outcome.
852 Result = DAG.getEntryNode();
853 } else {
854 // New and improved token factor.
855 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0], Ops.size());
Nate Begemanded49632005-10-13 03:11:28 +0000856 }
Jim Laskey274062c2006-10-13 23:32:28 +0000857
858 // Don't add users to work list.
859 return CombineTo(N, Result, false);
Nate Begemanded49632005-10-13 03:11:28 +0000860 }
Jim Laskey279f0532006-09-25 16:29:54 +0000861
Jim Laskey6ff23e52006-10-04 16:53:27 +0000862 return Result;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000863}
864
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000865/// MERGE_VALUES can always be eliminated.
Dan Gohman475871a2008-07-27 21:46:04 +0000866SDValue DAGCombiner::visitMERGE_VALUES(SDNode *N) {
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000867 WorkListRemover DeadNodes(*this);
868 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Dan Gohman475871a2008-07-27 21:46:04 +0000869 DAG.ReplaceAllUsesOfValueWith(SDValue(N, i), N->getOperand(i),
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000870 &DeadNodes);
871 removeFromWorkList(N);
872 DAG.DeleteNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +0000873 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000874}
875
876
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000877static
Dan Gohman475871a2008-07-27 21:46:04 +0000878SDValue combineShlAddConstant(SDValue N0, SDValue N1, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000879 MVT VT = N0.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +0000880 SDValue N00 = N0.getOperand(0);
881 SDValue N01 = N0.getOperand(1);
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000882 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N01);
Gabor Greifba36cb52008-08-28 21:40:38 +0000883 if (N01C && N00.getOpcode() == ISD::ADD && N00.getNode()->hasOneUse() &&
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000884 isa<ConstantSDNode>(N00.getOperand(1))) {
885 N0 = DAG.getNode(ISD::ADD, VT,
886 DAG.getNode(ISD::SHL, VT, N00.getOperand(0), N01),
887 DAG.getNode(ISD::SHL, VT, N00.getOperand(1), N01));
888 return DAG.getNode(ISD::ADD, VT, N0, N1);
889 }
Dan Gohman475871a2008-07-27 21:46:04 +0000890 return SDValue();
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000891}
892
Evan Chengb13cdbd2007-06-21 07:39:16 +0000893static
Dan Gohman475871a2008-07-27 21:46:04 +0000894SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
895 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000896 MVT VT = N->getValueType(0);
Evan Chengb13cdbd2007-06-21 07:39:16 +0000897 unsigned Opc = N->getOpcode();
898 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
Dan Gohman475871a2008-07-27 21:46:04 +0000899 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
900 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
Evan Chengb13cdbd2007-06-21 07:39:16 +0000901 ISD::CondCode CC = ISD::SETCC_INVALID;
902 if (isSlctCC)
903 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
904 else {
Dan Gohman475871a2008-07-27 21:46:04 +0000905 SDValue CCOp = Slct.getOperand(0);
Evan Chengb13cdbd2007-06-21 07:39:16 +0000906 if (CCOp.getOpcode() == ISD::SETCC)
907 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
908 }
909
910 bool DoXform = false;
911 bool InvCC = false;
912 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
913 "Bad input!");
914 if (LHS.getOpcode() == ISD::Constant &&
915 cast<ConstantSDNode>(LHS)->isNullValue())
916 DoXform = true;
917 else if (CC != ISD::SETCC_INVALID &&
918 RHS.getOpcode() == ISD::Constant &&
919 cast<ConstantSDNode>(RHS)->isNullValue()) {
920 std::swap(LHS, RHS);
Dan Gohman475871a2008-07-27 21:46:04 +0000921 SDValue Op0 = Slct.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000922 bool isInt = (isSlctCC ? Op0.getValueType() :
923 Op0.getOperand(0).getValueType()).isInteger();
Evan Chengb13cdbd2007-06-21 07:39:16 +0000924 CC = ISD::getSetCCInverse(CC, isInt);
925 DoXform = true;
926 InvCC = true;
927 }
928
929 if (DoXform) {
Dan Gohman475871a2008-07-27 21:46:04 +0000930 SDValue Result = DAG.getNode(Opc, VT, OtherOp, RHS);
Evan Chengb13cdbd2007-06-21 07:39:16 +0000931 if (isSlctCC)
932 return DAG.getSelectCC(OtherOp, Result,
933 Slct.getOperand(0), Slct.getOperand(1), CC);
Dan Gohman475871a2008-07-27 21:46:04 +0000934 SDValue CCOp = Slct.getOperand(0);
Evan Chengb13cdbd2007-06-21 07:39:16 +0000935 if (InvCC)
936 CCOp = DAG.getSetCC(CCOp.getValueType(), CCOp.getOperand(0),
937 CCOp.getOperand(1), CC);
938 return DAG.getNode(ISD::SELECT, VT, CCOp, OtherOp, Result);
939 }
Dan Gohman475871a2008-07-27 21:46:04 +0000940 return SDValue();
Evan Chengb13cdbd2007-06-21 07:39:16 +0000941}
942
Dan Gohman475871a2008-07-27 21:46:04 +0000943SDValue DAGCombiner::visitADD(SDNode *N) {
944 SDValue N0 = N->getOperand(0);
945 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000946 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
947 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000948 MVT VT = N0.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +0000949
950 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +0000951 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +0000952 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +0000953 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +0000954 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000955
Dan Gohman613e0d82007-07-03 14:03:57 +0000956 // fold (add x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +0000957 if (N0.getOpcode() == ISD::UNDEF)
958 return N0;
959 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +0000960 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000961 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000962 if (N0C && N1C)
Dan Gohman002e5d02008-03-13 22:13:53 +0000963 return DAG.getConstant(N0C->getAPIntValue() + N1C->getAPIntValue(), VT);
Nate Begeman99801192005-09-07 23:25:52 +0000964 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000965 if (N0C && !N1C)
966 return DAG.getNode(ISD::ADD, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000967 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000968 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000969 return N0;
Chris Lattner4aafb4f2006-01-12 20:22:43 +0000970 // fold ((c1-A)+c2) -> (c1+c2)-A
971 if (N1C && N0.getOpcode() == ISD::SUB)
972 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
973 return DAG.getNode(ISD::SUB, VT,
Dan Gohman002e5d02008-03-13 22:13:53 +0000974 DAG.getConstant(N1C->getAPIntValue()+
975 N0C->getAPIntValue(), VT),
Chris Lattner4aafb4f2006-01-12 20:22:43 +0000976 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000977 // reassociate add
Dan Gohman475871a2008-07-27 21:46:04 +0000978 SDValue RADD = ReassociateOps(ISD::ADD, N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +0000979 if (RADD.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +0000980 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000981 // fold ((0-A) + B) -> B-A
982 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
983 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000984 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000985 // fold (A + (0-B)) -> A-B
986 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
987 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000988 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +0000989 // fold (A+(B-A)) -> B
990 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000991 return N1.getOperand(0);
Chris Lattner947c2892006-03-13 06:51:27 +0000992
Dan Gohman475871a2008-07-27 21:46:04 +0000993 if (!VT.isVector() && SimplifyDemandedBits(SDValue(N, 0)))
994 return SDValue(N, 0);
Chris Lattner947c2892006-03-13 06:51:27 +0000995
996 // fold (a+b) -> (a|b) iff a and b share no bits.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000997 if (VT.isInteger() && !VT.isVector()) {
Dan Gohman948d8ea2008-02-20 16:33:30 +0000998 APInt LHSZero, LHSOne;
999 APInt RHSZero, RHSOne;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001000 APInt Mask = APInt::getAllOnesValue(VT.getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00001001 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Dan Gohman948d8ea2008-02-20 16:33:30 +00001002 if (LHSZero.getBoolValue()) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001003 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
Chris Lattner947c2892006-03-13 06:51:27 +00001004
1005 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1006 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
1007 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
1008 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
1009 return DAG.getNode(ISD::OR, VT, N0, N1);
1010 }
1011 }
Evan Cheng3ef554d2006-11-06 08:14:30 +00001012
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001013 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
Gabor Greifba36cb52008-08-28 21:40:38 +00001014 if (N0.getOpcode() == ISD::SHL && N0.getNode()->hasOneUse()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001015 SDValue Result = combineShlAddConstant(N0, N1, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001016 if (Result.getNode()) return Result;
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001017 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001018 if (N1.getOpcode() == ISD::SHL && N1.getNode()->hasOneUse()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001019 SDValue Result = combineShlAddConstant(N1, N0, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001020 if (Result.getNode()) return Result;
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001021 }
1022
Evan Chengb13cdbd2007-06-21 07:39:16 +00001023 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
Gabor Greifba36cb52008-08-28 21:40:38 +00001024 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001025 SDValue Result = combineSelectAndUse(N, N0, N1, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001026 if (Result.getNode()) return Result;
Evan Chengb13cdbd2007-06-21 07:39:16 +00001027 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001028 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001029 SDValue Result = combineSelectAndUse(N, N1, N0, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001030 if (Result.getNode()) return Result;
Evan Chengb13cdbd2007-06-21 07:39:16 +00001031 }
1032
Dan Gohman475871a2008-07-27 21:46:04 +00001033 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001034}
1035
Dan Gohman475871a2008-07-27 21:46:04 +00001036SDValue DAGCombiner::visitADDC(SDNode *N) {
1037 SDValue N0 = N->getOperand(0);
1038 SDValue N1 = N->getOperand(1);
Chris Lattner91153682007-03-04 20:03:15 +00001039 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1040 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001041 MVT VT = N0.getValueType();
Chris Lattner91153682007-03-04 20:03:15 +00001042
1043 // If the flag result is dead, turn this into an ADD.
1044 if (N->hasNUsesOfValue(0, 1))
1045 return CombineTo(N, DAG.getNode(ISD::ADD, VT, N1, N0),
Chris Lattnerb6541762007-03-04 20:40:38 +00001046 DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
Chris Lattner91153682007-03-04 20:03:15 +00001047
1048 // canonicalize constant to RHS.
Dan Gohman0a4627d2008-06-23 15:29:14 +00001049 if (N0C && !N1C)
Dan Gohman56867522008-06-21 22:06:07 +00001050 return DAG.getNode(ISD::ADDC, N->getVTList(), N1, N0);
Chris Lattner91153682007-03-04 20:03:15 +00001051
Chris Lattnerb6541762007-03-04 20:40:38 +00001052 // fold (addc x, 0) -> x + no carry out
1053 if (N1C && N1C->isNullValue())
1054 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
1055
1056 // fold (addc a, b) -> (or a, b), CARRY_FALSE iff a and b share no bits.
Dan Gohman948d8ea2008-02-20 16:33:30 +00001057 APInt LHSZero, LHSOne;
1058 APInt RHSZero, RHSOne;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001059 APInt Mask = APInt::getAllOnesValue(VT.getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00001060 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Dan Gohman948d8ea2008-02-20 16:33:30 +00001061 if (LHSZero.getBoolValue()) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001062 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
Chris Lattnerb6541762007-03-04 20:40:38 +00001063
1064 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1065 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
1066 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
1067 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
1068 return CombineTo(N, DAG.getNode(ISD::OR, VT, N0, N1),
1069 DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
1070 }
Chris Lattner91153682007-03-04 20:03:15 +00001071
Dan Gohman475871a2008-07-27 21:46:04 +00001072 return SDValue();
Chris Lattner91153682007-03-04 20:03:15 +00001073}
1074
Dan Gohman475871a2008-07-27 21:46:04 +00001075SDValue DAGCombiner::visitADDE(SDNode *N) {
1076 SDValue N0 = N->getOperand(0);
1077 SDValue N1 = N->getOperand(1);
1078 SDValue CarryIn = N->getOperand(2);
Chris Lattner91153682007-03-04 20:03:15 +00001079 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1080 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001081 //MVT VT = N0.getValueType();
Chris Lattner91153682007-03-04 20:03:15 +00001082
1083 // canonicalize constant to RHS
Dan Gohman0a4627d2008-06-23 15:29:14 +00001084 if (N0C && !N1C)
Dan Gohman56867522008-06-21 22:06:07 +00001085 return DAG.getNode(ISD::ADDE, N->getVTList(), N1, N0, CarryIn);
Chris Lattner91153682007-03-04 20:03:15 +00001086
Chris Lattnerb6541762007-03-04 20:40:38 +00001087 // fold (adde x, y, false) -> (addc x, y)
Dan Gohman0a4627d2008-06-23 15:29:14 +00001088 if (CarryIn.getOpcode() == ISD::CARRY_FALSE)
Dan Gohman56867522008-06-21 22:06:07 +00001089 return DAG.getNode(ISD::ADDC, N->getVTList(), N1, N0);
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
1094
1095
Dan Gohman475871a2008-07-27 21:46:04 +00001096SDValue DAGCombiner::visitSUB(SDNode *N) {
1097 SDValue N0 = N->getOperand(0);
1098 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001099 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1100 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Duncan Sands83ec4b62008-06-06 12:08:01 +00001101 MVT VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001102
Dan Gohman7f321562007-06-25 16:23:39 +00001103 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001104 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001105 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001106 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001107 }
Dan Gohman7f321562007-06-25 16:23:39 +00001108
Chris Lattner854077d2005-10-17 01:07:11 +00001109 // fold (sub x, x) -> 0
Evan Chengc8e3b142008-03-12 07:02:50 +00001110 if (N0 == N1)
Chris Lattner854077d2005-10-17 01:07:11 +00001111 return DAG.getConstant(0, N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001112 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001113 if (N0C && N1C)
Bill Wendling9a0d1ba2008-09-08 01:56:32 +00001114 return DAG.getNode(ISD::SUB, VT, N0, N1);
Chris Lattner05b57432005-10-11 06:07:15 +00001115 // fold (sub x, c) -> (add x, -c)
1116 if (N1C)
Dan Gohman002e5d02008-03-13 22:13:53 +00001117 return DAG.getNode(ISD::ADD, VT, N0,
1118 DAG.getConstant(-N1C->getAPIntValue(), VT));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001119 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +00001120 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001121 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001122 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +00001123 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001124 return N0.getOperand(0);
Evan Chengb13cdbd2007-06-21 07:39:16 +00001125 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
Gabor Greifba36cb52008-08-28 21:40:38 +00001126 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001127 SDValue Result = combineSelectAndUse(N, N1, N0, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001128 if (Result.getNode()) return Result;
Evan Chengb13cdbd2007-06-21 07:39:16 +00001129 }
Dan Gohman613e0d82007-07-03 14:03:57 +00001130 // If either operand of a sub is undef, the result is undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00001131 if (N0.getOpcode() == ISD::UNDEF)
1132 return N0;
1133 if (N1.getOpcode() == ISD::UNDEF)
1134 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001135
Dan Gohman475871a2008-07-27 21:46:04 +00001136 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001137}
1138
Dan Gohman475871a2008-07-27 21:46:04 +00001139SDValue DAGCombiner::visitMUL(SDNode *N) {
1140 SDValue N0 = N->getOperand(0);
1141 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001142 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1143 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001144 MVT VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001145
Dan Gohman7f321562007-06-25 16:23:39 +00001146 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001147 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001148 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001149 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001150 }
Dan Gohman7f321562007-06-25 16:23:39 +00001151
Dan Gohman613e0d82007-07-03 14:03:57 +00001152 // fold (mul x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001153 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001154 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001155 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001156 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001157 return DAG.getNode(ISD::MUL, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001158 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001159 if (N0C && !N1C)
1160 return DAG.getNode(ISD::MUL, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001161 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001162 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001163 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001164 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +00001165 if (N1C && N1C->isAllOnesValue())
Nate Begeman405e3ec2005-10-21 00:02:42 +00001166 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001167 // fold (mul x, (1 << c)) -> x << c
Dan Gohman002e5d02008-03-13 22:13:53 +00001168 if (N1C && N1C->getAPIntValue().isPowerOf2())
Chris Lattner3e6099b2005-10-30 06:41:49 +00001169 return DAG.getNode(ISD::SHL, VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001170 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001171 TLI.getShiftAmountTy()));
Chris Lattner3e6099b2005-10-30 06:41:49 +00001172 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
1173 if (N1C && isPowerOf2_64(-N1C->getSignExtended())) {
1174 // FIXME: If the input is something that is easily negated (e.g. a
1175 // single-use add), we should put the negate there.
1176 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT),
1177 DAG.getNode(ISD::SHL, VT, N0,
1178 DAG.getConstant(Log2_64(-N1C->getSignExtended()),
1179 TLI.getShiftAmountTy())));
1180 }
Andrew Lenharth50a0d422006-04-02 21:42:45 +00001181
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001182 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
1183 if (N1C && N0.getOpcode() == ISD::SHL &&
1184 isa<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohman475871a2008-07-27 21:46:04 +00001185 SDValue C3 = DAG.getNode(ISD::SHL, VT, N1, N0.getOperand(1));
Gabor Greifba36cb52008-08-28 21:40:38 +00001186 AddToWorkList(C3.getNode());
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001187 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0), C3);
1188 }
1189
1190 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
1191 // use.
1192 {
Dan Gohman475871a2008-07-27 21:46:04 +00001193 SDValue Sh(0,0), Y(0,0);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001194 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
1195 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001196 N0.getNode()->hasOneUse()) {
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001197 Sh = N0; Y = N1;
1198 } else if (N1.getOpcode() == ISD::SHL &&
Gabor Greif12632d22008-08-30 19:29:20 +00001199 isa<ConstantSDNode>(N1.getOperand(1)) &&
1200 N1.getNode()->hasOneUse()) {
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001201 Sh = N1; Y = N0;
1202 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001203 if (Sh.getNode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001204 SDValue Mul = DAG.getNode(ISD::MUL, VT, Sh.getOperand(0), Y);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001205 return DAG.getNode(ISD::SHL, VT, Mul, Sh.getOperand(1));
1206 }
1207 }
Chris Lattnera1deca32006-03-04 23:33:26 +00001208 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
Gabor Greifba36cb52008-08-28 21:40:38 +00001209 if (N1C && N0.getOpcode() == ISD::ADD && N0.getNode()->hasOneUse() &&
Chris Lattnera1deca32006-03-04 23:33:26 +00001210 isa<ConstantSDNode>(N0.getOperand(1))) {
1211 return DAG.getNode(ISD::ADD, VT,
1212 DAG.getNode(ISD::MUL, VT, N0.getOperand(0), N1),
1213 DAG.getNode(ISD::MUL, VT, N0.getOperand(1), N1));
1214 }
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001215
Nate Begemancd4d58c2006-02-03 06:46:56 +00001216 // reassociate mul
Dan Gohman475871a2008-07-27 21:46:04 +00001217 SDValue RMUL = ReassociateOps(ISD::MUL, N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001218 if (RMUL.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001219 return RMUL;
Dan Gohman7f321562007-06-25 16:23:39 +00001220
Dan Gohman475871a2008-07-27 21:46:04 +00001221 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001222}
1223
Dan Gohman475871a2008-07-27 21:46:04 +00001224SDValue DAGCombiner::visitSDIV(SDNode *N) {
1225 SDValue N0 = N->getOperand(0);
1226 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001227 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1228 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Duncan Sands83ec4b62008-06-06 12:08:01 +00001229 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001230
Dan Gohman7f321562007-06-25 16:23:39 +00001231 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001232 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001233 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001234 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001235 }
Dan Gohman7f321562007-06-25 16:23:39 +00001236
Nate Begeman1d4d4142005-09-01 00:19:25 +00001237 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001238 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001239 return DAG.getNode(ISD::SDIV, VT, N0, N1);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001240 // fold (sdiv X, 1) -> X
1241 if (N1C && N1C->getSignExtended() == 1LL)
1242 return N0;
1243 // fold (sdiv X, -1) -> 0-X
1244 if (N1C && N1C->isAllOnesValue())
1245 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +00001246 // If we know the sign bits of both operands are zero, strength reduce to a
1247 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
Duncan Sands83ec4b62008-06-06 12:08:01 +00001248 if (!VT.isVector()) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001249 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Chris Lattnerf32aac32008-01-27 23:32:17 +00001250 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
1251 }
Nate Begemancd6a6ed2006-02-17 07:26:20 +00001252 // fold (sdiv X, pow2) -> simple ops after legalize
Dan Gohman002e5d02008-03-13 22:13:53 +00001253 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap() &&
Nate Begeman405e3ec2005-10-21 00:02:42 +00001254 (isPowerOf2_64(N1C->getSignExtended()) ||
1255 isPowerOf2_64(-N1C->getSignExtended()))) {
1256 // If dividing by powers of two is cheap, then don't perform the following
1257 // fold.
1258 if (TLI.isPow2DivCheap())
Dan Gohman475871a2008-07-27 21:46:04 +00001259 return SDValue();
Nate Begeman405e3ec2005-10-21 00:02:42 +00001260 int64_t pow2 = N1C->getSignExtended();
1261 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
Chris Lattner8f4880b2006-02-16 08:02:36 +00001262 unsigned lg2 = Log2_64(abs2);
1263 // Splat the sign bit into the register
Dan Gohman475871a2008-07-27 21:46:04 +00001264 SDValue SGN = DAG.getNode(ISD::SRA, VT, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001265 DAG.getConstant(VT.getSizeInBits()-1,
Nate Begeman405e3ec2005-10-21 00:02:42 +00001266 TLI.getShiftAmountTy()));
Gabor Greifba36cb52008-08-28 21:40:38 +00001267 AddToWorkList(SGN.getNode());
Chris Lattner8f4880b2006-02-16 08:02:36 +00001268 // Add (N0 < 0) ? abs2 - 1 : 0;
Dan Gohman475871a2008-07-27 21:46:04 +00001269 SDValue SRL = DAG.getNode(ISD::SRL, VT, SGN,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001270 DAG.getConstant(VT.getSizeInBits()-lg2,
Nate Begeman405e3ec2005-10-21 00:02:42 +00001271 TLI.getShiftAmountTy()));
Dan Gohman475871a2008-07-27 21:46:04 +00001272 SDValue ADD = DAG.getNode(ISD::ADD, VT, N0, SRL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001273 AddToWorkList(SRL.getNode());
1274 AddToWorkList(ADD.getNode()); // Divide by pow2
Dan Gohman475871a2008-07-27 21:46:04 +00001275 SDValue SRA = DAG.getNode(ISD::SRA, VT, ADD,
Chris Lattner8f4880b2006-02-16 08:02:36 +00001276 DAG.getConstant(lg2, TLI.getShiftAmountTy()));
Nate Begeman405e3ec2005-10-21 00:02:42 +00001277 // If we're dividing by a positive value, we're done. Otherwise, we must
1278 // negate the result.
1279 if (pow2 > 0)
1280 return SRA;
Gabor Greifba36cb52008-08-28 21:40:38 +00001281 AddToWorkList(SRA.getNode());
Nate Begeman405e3ec2005-10-21 00:02:42 +00001282 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), SRA);
1283 }
Nate Begeman69575232005-10-20 02:15:44 +00001284 // if integer divide is expensive and we satisfy the requirements, emit an
1285 // alternate sequence.
Nate Begeman405e3ec2005-10-21 00:02:42 +00001286 if (N1C && (N1C->getSignExtended() < -1 || N1C->getSignExtended() > 1) &&
Chris Lattnere9936d12005-10-22 18:50:15 +00001287 !TLI.isIntDivCheap()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001288 SDValue Op = BuildSDIV(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001289 if (Op.getNode()) return Op;
Nate Begeman69575232005-10-20 02:15:44 +00001290 }
Dan Gohman7f321562007-06-25 16:23:39 +00001291
Dan Gohman613e0d82007-07-03 14:03:57 +00001292 // undef / X -> 0
1293 if (N0.getOpcode() == ISD::UNDEF)
1294 return DAG.getConstant(0, VT);
1295 // X / undef -> undef
1296 if (N1.getOpcode() == ISD::UNDEF)
1297 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001298
Dan Gohman475871a2008-07-27 21:46:04 +00001299 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001300}
1301
Dan Gohman475871a2008-07-27 21:46:04 +00001302SDValue DAGCombiner::visitUDIV(SDNode *N) {
1303 SDValue N0 = N->getOperand(0);
1304 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001305 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1306 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Duncan Sands83ec4b62008-06-06 12:08:01 +00001307 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001308
Dan Gohman7f321562007-06-25 16:23:39 +00001309 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001310 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001311 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001312 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001313 }
Dan Gohman7f321562007-06-25 16:23:39 +00001314
Nate Begeman1d4d4142005-09-01 00:19:25 +00001315 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001316 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001317 return DAG.getNode(ISD::UDIV, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001318 // fold (udiv x, (1 << c)) -> x >>u c
Dan Gohman002e5d02008-03-13 22:13:53 +00001319 if (N1C && N1C->getAPIntValue().isPowerOf2())
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001320 return DAG.getNode(ISD::SRL, VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001321 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001322 TLI.getShiftAmountTy()));
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001323 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
1324 if (N1.getOpcode() == ISD::SHL) {
1325 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00001326 if (SHC->getAPIntValue().isPowerOf2()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001327 MVT ADDVT = N1.getOperand(1).getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00001328 SDValue Add = DAG.getNode(ISD::ADD, ADDVT, N1.getOperand(1),
Dan Gohman002e5d02008-03-13 22:13:53 +00001329 DAG.getConstant(SHC->getAPIntValue()
1330 .logBase2(),
Nate Begemanc031e332006-02-05 07:36:48 +00001331 ADDVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00001332 AddToWorkList(Add.getNode());
Nate Begemanc031e332006-02-05 07:36:48 +00001333 return DAG.getNode(ISD::SRL, VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001334 }
1335 }
1336 }
Nate Begeman69575232005-10-20 02:15:44 +00001337 // fold (udiv x, c) -> alternate
Dan Gohman002e5d02008-03-13 22:13:53 +00001338 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001339 SDValue Op = BuildUDIV(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001340 if (Op.getNode()) return Op;
Chris Lattnere9936d12005-10-22 18:50:15 +00001341 }
Dan Gohman7f321562007-06-25 16:23:39 +00001342
Dan Gohman613e0d82007-07-03 14:03:57 +00001343 // undef / X -> 0
1344 if (N0.getOpcode() == ISD::UNDEF)
1345 return DAG.getConstant(0, VT);
1346 // X / undef -> undef
1347 if (N1.getOpcode() == ISD::UNDEF)
1348 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001349
Dan Gohman475871a2008-07-27 21:46:04 +00001350 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001351}
1352
Dan Gohman475871a2008-07-27 21:46:04 +00001353SDValue DAGCombiner::visitSREM(SDNode *N) {
1354 SDValue N0 = N->getOperand(0);
1355 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001356 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1357 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001358 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001359
1360 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001361 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001362 return DAG.getNode(ISD::SREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +00001363 // If we know the sign bits of both operands are zero, strength reduce to a
1364 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
Duncan Sands83ec4b62008-06-06 12:08:01 +00001365 if (!VT.isVector()) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001366 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Chris Lattneree339f42008-01-27 23:21:58 +00001367 return DAG.getNode(ISD::UREM, VT, N0, N1);
1368 }
Chris Lattner26d29902006-10-12 20:58:32 +00001369
Dan Gohman77003042007-11-26 23:46:11 +00001370 // If X/C can be simplified by the division-by-constant logic, lower
1371 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00001372 if (N1C && !N1C->isNullValue()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001373 SDValue Div = DAG.getNode(ISD::SDIV, VT, N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001374 AddToWorkList(Div.getNode());
1375 SDValue OptimizedDiv = combine(Div.getNode());
1376 if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001377 SDValue Mul = DAG.getNode(ISD::MUL, VT, OptimizedDiv, N1);
1378 SDValue Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
Gabor Greifba36cb52008-08-28 21:40:38 +00001379 AddToWorkList(Mul.getNode());
Dan Gohman77003042007-11-26 23:46:11 +00001380 return Sub;
1381 }
Chris Lattner26d29902006-10-12 20:58:32 +00001382 }
1383
Dan Gohman613e0d82007-07-03 14:03:57 +00001384 // undef % X -> 0
1385 if (N0.getOpcode() == ISD::UNDEF)
1386 return DAG.getConstant(0, VT);
1387 // X % undef -> undef
1388 if (N1.getOpcode() == ISD::UNDEF)
1389 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001390
Dan Gohman475871a2008-07-27 21:46:04 +00001391 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001392}
1393
Dan Gohman475871a2008-07-27 21:46:04 +00001394SDValue DAGCombiner::visitUREM(SDNode *N) {
1395 SDValue N0 = N->getOperand(0);
1396 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001397 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1398 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001399 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001400
1401 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001402 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001403 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +00001404 // fold (urem x, pow2) -> (and x, pow2-1)
Dan Gohman002e5d02008-03-13 22:13:53 +00001405 if (N1C && !N1C->isNullValue() && N1C->getAPIntValue().isPowerOf2())
1406 return DAG.getNode(ISD::AND, VT, N0,
1407 DAG.getConstant(N1C->getAPIntValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +00001408 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
1409 if (N1.getOpcode() == ISD::SHL) {
1410 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00001411 if (SHC->getAPIntValue().isPowerOf2()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001412 SDValue Add =
Dan Gohman002e5d02008-03-13 22:13:53 +00001413 DAG.getNode(ISD::ADD, VT, N1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001414 DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()),
Dan Gohman002e5d02008-03-13 22:13:53 +00001415 VT));
Gabor Greifba36cb52008-08-28 21:40:38 +00001416 AddToWorkList(Add.getNode());
Nate Begemanc031e332006-02-05 07:36:48 +00001417 return DAG.getNode(ISD::AND, VT, N0, Add);
1418 }
1419 }
1420 }
Chris Lattner26d29902006-10-12 20:58:32 +00001421
Dan Gohman77003042007-11-26 23:46:11 +00001422 // If X/C can be simplified by the division-by-constant logic, lower
1423 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00001424 if (N1C && !N1C->isNullValue()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001425 SDValue Div = DAG.getNode(ISD::UDIV, VT, N0, N1);
Dan Gohman942ca7f2008-09-08 16:59:01 +00001426 AddToWorkList(Div.getNode());
Gabor Greifba36cb52008-08-28 21:40:38 +00001427 SDValue OptimizedDiv = combine(Div.getNode());
1428 if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001429 SDValue Mul = DAG.getNode(ISD::MUL, VT, OptimizedDiv, N1);
1430 SDValue Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
Gabor Greifba36cb52008-08-28 21:40:38 +00001431 AddToWorkList(Mul.getNode());
Dan Gohman77003042007-11-26 23:46:11 +00001432 return Sub;
1433 }
Chris Lattner26d29902006-10-12 20:58:32 +00001434 }
1435
Dan Gohman613e0d82007-07-03 14:03:57 +00001436 // undef % X -> 0
1437 if (N0.getOpcode() == ISD::UNDEF)
1438 return DAG.getConstant(0, VT);
1439 // X % undef -> undef
1440 if (N1.getOpcode() == ISD::UNDEF)
1441 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001442
Dan Gohman475871a2008-07-27 21:46:04 +00001443 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001444}
1445
Dan Gohman475871a2008-07-27 21:46:04 +00001446SDValue DAGCombiner::visitMULHS(SDNode *N) {
1447 SDValue N0 = N->getOperand(0);
1448 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001449 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001450 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001451
1452 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001453 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001454 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001455 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Dan Gohman002e5d02008-03-13 22:13:53 +00001456 if (N1C && N1C->getAPIntValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001457 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001458 DAG.getConstant(N0.getValueType().getSizeInBits()-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001459 TLI.getShiftAmountTy()));
Dan Gohman613e0d82007-07-03 14:03:57 +00001460 // fold (mulhs x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001461 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001462 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00001463
Dan Gohman475871a2008-07-27 21:46:04 +00001464 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001465}
1466
Dan Gohman475871a2008-07-27 21:46:04 +00001467SDValue DAGCombiner::visitMULHU(SDNode *N) {
1468 SDValue N0 = N->getOperand(0);
1469 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001470 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001471 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001472
1473 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001474 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001475 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001476 // fold (mulhu x, 1) -> 0
Dan Gohman002e5d02008-03-13 22:13:53 +00001477 if (N1C && N1C->getAPIntValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001478 return DAG.getConstant(0, N0.getValueType());
Dan Gohman613e0d82007-07-03 14:03:57 +00001479 // fold (mulhu x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001480 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001481 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00001482
Dan Gohman475871a2008-07-27 21:46:04 +00001483 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001484}
1485
Dan Gohman389079b2007-10-08 17:57:15 +00001486/// SimplifyNodeWithTwoResults - Perform optimizations common to nodes that
1487/// compute two values. LoOp and HiOp give the opcodes for the two computations
1488/// that are being performed. Return true if a simplification was made.
1489///
Dan Gohman475871a2008-07-27 21:46:04 +00001490SDValue DAGCombiner::SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
1491 unsigned HiOp) {
Dan Gohman389079b2007-10-08 17:57:15 +00001492 // If the high half is not needed, just compute the low half.
Evan Cheng44711942007-11-08 09:25:29 +00001493 bool HiExists = N->hasAnyUseOfValue(1);
1494 if (!HiExists &&
Dan Gohman389079b2007-10-08 17:57:15 +00001495 (!AfterLegalize ||
1496 TLI.isOperationLegal(LoOp, N->getValueType(0)))) {
Dan Gohman475871a2008-07-27 21:46:04 +00001497 SDValue Res = DAG.getNode(LoOp, N->getValueType(0), N->op_begin(),
Chris Lattner5eee4272008-01-26 01:09:19 +00001498 N->getNumOperands());
1499 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00001500 }
1501
1502 // If the low half is not needed, just compute the high half.
Evan Cheng44711942007-11-08 09:25:29 +00001503 bool LoExists = N->hasAnyUseOfValue(0);
1504 if (!LoExists &&
Dan Gohman389079b2007-10-08 17:57:15 +00001505 (!AfterLegalize ||
1506 TLI.isOperationLegal(HiOp, N->getValueType(1)))) {
Dan Gohman475871a2008-07-27 21:46:04 +00001507 SDValue Res = DAG.getNode(HiOp, N->getValueType(1), N->op_begin(),
Chris Lattner5eee4272008-01-26 01:09:19 +00001508 N->getNumOperands());
1509 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00001510 }
1511
Evan Cheng44711942007-11-08 09:25:29 +00001512 // If both halves are used, return as it is.
1513 if (LoExists && HiExists)
Dan Gohman475871a2008-07-27 21:46:04 +00001514 return SDValue();
Evan Cheng44711942007-11-08 09:25:29 +00001515
1516 // If the two computed results can be simplified separately, separate them.
Evan Cheng44711942007-11-08 09:25:29 +00001517 if (LoExists) {
Dan Gohman475871a2008-07-27 21:46:04 +00001518 SDValue Lo = DAG.getNode(LoOp, N->getValueType(0),
Evan Cheng44711942007-11-08 09:25:29 +00001519 N->op_begin(), N->getNumOperands());
Gabor Greifba36cb52008-08-28 21:40:38 +00001520 AddToWorkList(Lo.getNode());
1521 SDValue LoOpt = combine(Lo.getNode());
1522 if (LoOpt.getNode() && LoOpt.getNode() != Lo.getNode() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001523 (!AfterLegalize ||
1524 TLI.isOperationLegal(LoOpt.getOpcode(), LoOpt.getValueType())))
Chris Lattner5eee4272008-01-26 01:09:19 +00001525 return CombineTo(N, LoOpt, LoOpt);
Dan Gohman389079b2007-10-08 17:57:15 +00001526 }
1527
Evan Cheng44711942007-11-08 09:25:29 +00001528 if (HiExists) {
Dan Gohman475871a2008-07-27 21:46:04 +00001529 SDValue Hi = DAG.getNode(HiOp, N->getValueType(1),
Evan Cheng44711942007-11-08 09:25:29 +00001530 N->op_begin(), N->getNumOperands());
Gabor Greifba36cb52008-08-28 21:40:38 +00001531 AddToWorkList(Hi.getNode());
1532 SDValue HiOpt = combine(Hi.getNode());
1533 if (HiOpt.getNode() && HiOpt != Hi &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001534 (!AfterLegalize ||
1535 TLI.isOperationLegal(HiOpt.getOpcode(), HiOpt.getValueType())))
Chris Lattner5eee4272008-01-26 01:09:19 +00001536 return CombineTo(N, HiOpt, HiOpt);
Evan Cheng44711942007-11-08 09:25:29 +00001537 }
Dan Gohman475871a2008-07-27 21:46:04 +00001538 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001539}
1540
Dan Gohman475871a2008-07-27 21:46:04 +00001541SDValue DAGCombiner::visitSMUL_LOHI(SDNode *N) {
1542 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHS);
Gabor Greifba36cb52008-08-28 21:40:38 +00001543 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001544
Dan Gohman475871a2008-07-27 21:46:04 +00001545 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001546}
1547
Dan Gohman475871a2008-07-27 21:46:04 +00001548SDValue DAGCombiner::visitUMUL_LOHI(SDNode *N) {
1549 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHU);
Gabor Greifba36cb52008-08-28 21:40:38 +00001550 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001551
Dan Gohman475871a2008-07-27 21:46:04 +00001552 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001553}
1554
Dan Gohman475871a2008-07-27 21:46:04 +00001555SDValue DAGCombiner::visitSDIVREM(SDNode *N) {
1556 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::SDIV, ISD::SREM);
Gabor Greifba36cb52008-08-28 21:40:38 +00001557 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001558
Dan Gohman475871a2008-07-27 21:46:04 +00001559 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001560}
1561
Dan Gohman475871a2008-07-27 21:46:04 +00001562SDValue DAGCombiner::visitUDIVREM(SDNode *N) {
1563 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::UDIV, ISD::UREM);
Gabor Greifba36cb52008-08-28 21:40:38 +00001564 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001565
Dan Gohman475871a2008-07-27 21:46:04 +00001566 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001567}
1568
Chris Lattner35e5c142006-05-05 05:51:50 +00001569/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
1570/// two operands of the same opcode, try to simplify it.
Dan Gohman475871a2008-07-27 21:46:04 +00001571SDValue DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
1572 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001573 MVT VT = N0.getValueType();
Chris Lattner35e5c142006-05-05 05:51:50 +00001574 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
1575
Chris Lattner540121f2006-05-05 06:31:05 +00001576 // For each of OP in AND/OR/XOR:
1577 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
1578 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
1579 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
Chris Lattner0d8dae72006-05-05 06:32:04 +00001580 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y))
Chris Lattner540121f2006-05-05 06:31:05 +00001581 if ((N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND||
Chris Lattner0d8dae72006-05-05 06:32:04 +00001582 N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::TRUNCATE) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001583 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001584 SDValue ORNode = DAG.getNode(N->getOpcode(),
Chris Lattner35e5c142006-05-05 05:51:50 +00001585 N0.getOperand(0).getValueType(),
1586 N0.getOperand(0), N1.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00001587 AddToWorkList(ORNode.getNode());
Chris Lattner540121f2006-05-05 06:31:05 +00001588 return DAG.getNode(N0.getOpcode(), VT, ORNode);
Chris Lattner35e5c142006-05-05 05:51:50 +00001589 }
1590
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001591 // For each of OP in SHL/SRL/SRA/AND...
1592 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
1593 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
1594 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
Chris Lattner35e5c142006-05-05 05:51:50 +00001595 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001596 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001597 N0.getOperand(1) == N1.getOperand(1)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001598 SDValue ORNode = DAG.getNode(N->getOpcode(),
Chris Lattner35e5c142006-05-05 05:51:50 +00001599 N0.getOperand(0).getValueType(),
1600 N0.getOperand(0), N1.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00001601 AddToWorkList(ORNode.getNode());
Chris Lattner35e5c142006-05-05 05:51:50 +00001602 return DAG.getNode(N0.getOpcode(), VT, ORNode, N0.getOperand(1));
1603 }
1604
Dan Gohman475871a2008-07-27 21:46:04 +00001605 return SDValue();
Chris Lattner35e5c142006-05-05 05:51:50 +00001606}
1607
Dan Gohman475871a2008-07-27 21:46:04 +00001608SDValue DAGCombiner::visitAND(SDNode *N) {
1609 SDValue N0 = N->getOperand(0);
1610 SDValue N1 = N->getOperand(1);
1611 SDValue LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001612 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1613 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001614 MVT VT = N1.getValueType();
1615 unsigned BitWidth = VT.getSizeInBits();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001616
Dan Gohman7f321562007-06-25 16:23:39 +00001617 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001618 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001619 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001620 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001621 }
Dan Gohman7f321562007-06-25 16:23:39 +00001622
Dan Gohman613e0d82007-07-03 14:03:57 +00001623 // fold (and x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001624 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001625 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001626 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001627 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001628 return DAG.getNode(ISD::AND, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001629 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001630 if (N0C && !N1C)
1631 return DAG.getNode(ISD::AND, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001632 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001633 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001634 return N0;
1635 // if (and x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00001636 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001637 APInt::getAllOnesValue(BitWidth)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001638 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +00001639 // reassociate and
Dan Gohman475871a2008-07-27 21:46:04 +00001640 SDValue RAND = ReassociateOps(ISD::AND, N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001641 if (RAND.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001642 return RAND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001643 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
Nate Begeman5dc7e862005-11-02 18:42:59 +00001644 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001645 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohman002e5d02008-03-13 22:13:53 +00001646 if ((ORI->getAPIntValue() & N1C->getAPIntValue()) == N1C->getAPIntValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001647 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +00001648 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
1649 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Dan Gohman475871a2008-07-27 21:46:04 +00001650 SDValue N0Op0 = N0.getOperand(0);
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001651 APInt Mask = ~N1C->getAPIntValue();
1652 Mask.trunc(N0Op0.getValueSizeInBits());
1653 if (DAG.MaskedValueIsZero(N0Op0, Mask)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001654 SDValue Zext = DAG.getNode(ISD::ZERO_EXTEND, N0.getValueType(),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001655 N0Op0);
Chris Lattner1ec05d12006-03-01 21:47:21 +00001656
1657 // Replace uses of the AND with uses of the Zero extend node.
1658 CombineTo(N, Zext);
1659
Chris Lattner3603cd62006-02-02 07:17:31 +00001660 // We actually want to replace all uses of the any_extend with the
1661 // zero_extend, to avoid duplicating things. This will later cause this
1662 // AND to be folded.
Gabor Greifba36cb52008-08-28 21:40:38 +00001663 CombineTo(N0.getNode(), Zext);
Dan Gohman475871a2008-07-27 21:46:04 +00001664 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner3603cd62006-02-02 07:17:31 +00001665 }
1666 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001667 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
1668 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1669 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1670 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1671
1672 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00001673 LL.getValueType().isInteger()) {
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001674 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
Dan Gohman002e5d02008-03-13 22:13:53 +00001675 if (cast<ConstantSDNode>(LR)->isNullValue() && Op1 == ISD::SETEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00001676 SDValue ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001677 AddToWorkList(ORNode.getNode());
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001678 return DAG.getSetCC(VT, ORNode, LR, Op1);
1679 }
1680 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
1681 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00001682 SDValue ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001683 AddToWorkList(ANDNode.getNode());
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001684 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1685 }
1686 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
1687 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
Dan Gohman475871a2008-07-27 21:46:04 +00001688 SDValue ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001689 AddToWorkList(ORNode.getNode());
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001690 return DAG.getSetCC(VT, ORNode, LR, Op1);
1691 }
1692 }
1693 // canonicalize equivalent to ll == rl
1694 if (LL == RR && LR == RL) {
1695 Op1 = ISD::getSetCCSwappedOperands(Op1);
1696 std::swap(RL, RR);
1697 }
1698 if (LL == RL && LR == RR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001699 bool isInteger = LL.getValueType().isInteger();
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001700 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
1701 if (Result != ISD::SETCC_INVALID)
1702 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1703 }
1704 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001705
1706 // Simplify: and (op x...), (op y...) -> (op (and x, y))
1707 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001708 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001709 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001710 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001711
Nate Begemande996292006-02-03 22:24:05 +00001712 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
1713 // fold (and (sra)) -> (and (srl)) when possible.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001714 if (!VT.isVector() &&
Dan Gohman475871a2008-07-27 21:46:04 +00001715 SimplifyDemandedBits(SDValue(N, 0)))
1716 return SDValue(N, 0);
Nate Begemanded49632005-10-13 03:11:28 +00001717 // fold (zext_inreg (extload x)) -> (zextload x)
Gabor Greifba36cb52008-08-28 21:40:38 +00001718 if (ISD::isEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode())) {
Evan Cheng466685d2006-10-09 20:57:25 +00001719 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001720 MVT EVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001721 // If we zero all the possible extended bits, then we can turn this into
1722 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001723 unsigned BitWidth = N1.getValueSizeInBits();
1724 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001725 BitWidth - EVT.getSizeInBits())) &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001726 ((!AfterLegalize && !LN0->isVolatile()) ||
1727 TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Dan Gohman475871a2008-07-27 21:46:04 +00001728 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
Evan Cheng466685d2006-10-09 20:57:25 +00001729 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00001730 LN0->getSrcValueOffset(), EVT,
1731 LN0->isVolatile(),
1732 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00001733 AddToWorkList(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001734 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00001735 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001736 }
1737 }
1738 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Gabor Greifba36cb52008-08-28 21:40:38 +00001739 if (ISD::isSEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng83060c52007-03-07 08:07:03 +00001740 N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001741 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001742 MVT EVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001743 // If we zero all the possible extended bits, then we can turn this into
1744 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001745 unsigned BitWidth = N1.getValueSizeInBits();
1746 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001747 BitWidth - EVT.getSizeInBits())) &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001748 ((!AfterLegalize && !LN0->isVolatile()) ||
1749 TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Dan Gohman475871a2008-07-27 21:46:04 +00001750 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
Evan Cheng466685d2006-10-09 20:57:25 +00001751 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00001752 LN0->getSrcValueOffset(), EVT,
1753 LN0->isVolatile(),
1754 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00001755 AddToWorkList(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001756 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00001757 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001758 }
1759 }
Chris Lattner15045b62006-02-28 06:35:35 +00001760
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001761 // fold (and (load x), 255) -> (zextload x, i8)
1762 // fold (and (extload x, i16), 255) -> (zextload x, i8)
Evan Cheng466685d2006-10-09 20:57:25 +00001763 if (N1C && N0.getOpcode() == ISD::LOAD) {
1764 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
1765 if (LN0->getExtensionType() != ISD::SEXTLOAD &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001766 LN0->isUnindexed() && N0.hasOneUse() &&
1767 // Do not change the width of a volatile load.
1768 !LN0->isVolatile()) {
Duncan Sands8eab8a22008-06-09 11:32:28 +00001769 MVT EVT = MVT::Other;
1770 uint32_t ActiveBits = N1C->getAPIntValue().getActiveBits();
1771 if (ActiveBits > 0 && APIntOps::isMask(ActiveBits, N1C->getAPIntValue()))
1772 EVT = MVT::getIntegerVT(ActiveBits);
1773
1774 MVT LoadedVT = LN0->getMemoryVT();
Duncan Sandsad205a72008-06-16 08:14:38 +00001775 // Do not generate loads of non-round integer types since these can
1776 // be expensive (and would be wrong if the type is not byte sized).
1777 if (EVT != MVT::Other && LoadedVT.bitsGT(EVT) && EVT.isRound() &&
Evan Cheng466685d2006-10-09 20:57:25 +00001778 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001779 MVT PtrType = N0.getOperand(1).getValueType();
Evan Cheng466685d2006-10-09 20:57:25 +00001780 // For big endian targets, we need to add an offset to the pointer to
1781 // load the correct bytes. For little endian systems, we merely need to
1782 // read fewer bytes from the same pointer.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001783 unsigned LVTStoreBytes = LoadedVT.getStoreSizeInBits()/8;
1784 unsigned EVTStoreBytes = EVT.getStoreSizeInBits()/8;
Duncan Sandsc6fa1702007-11-09 08:57:19 +00001785 unsigned PtrOff = LVTStoreBytes - EVTStoreBytes;
Duncan Sandsdc846502007-10-28 12:59:45 +00001786 unsigned Alignment = LN0->getAlignment();
Dan Gohman475871a2008-07-27 21:46:04 +00001787 SDValue NewPtr = LN0->getBasePtr();
Duncan Sands0753fc12008-02-11 10:37:04 +00001788 if (TLI.isBigEndian()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001789 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
1790 DAG.getConstant(PtrOff, PtrType));
Duncan Sandsdc846502007-10-28 12:59:45 +00001791 Alignment = MinAlign(Alignment, PtrOff);
1792 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001793 AddToWorkList(NewPtr.getNode());
Dan Gohman475871a2008-07-27 21:46:04 +00001794 SDValue Load =
Evan Cheng466685d2006-10-09 20:57:25 +00001795 DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00001796 LN0->getSrcValue(), LN0->getSrcValueOffset(), EVT,
Duncan Sandsdc846502007-10-28 12:59:45 +00001797 LN0->isVolatile(), Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00001798 AddToWorkList(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001799 CombineTo(N0.getNode(), Load, Load.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00001800 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng466685d2006-10-09 20:57:25 +00001801 }
Chris Lattner15045b62006-02-28 06:35:35 +00001802 }
1803 }
1804
Dan Gohman475871a2008-07-27 21:46:04 +00001805 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001806}
1807
Dan Gohman475871a2008-07-27 21:46:04 +00001808SDValue DAGCombiner::visitOR(SDNode *N) {
1809 SDValue N0 = N->getOperand(0);
1810 SDValue N1 = N->getOperand(1);
1811 SDValue LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001812 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1813 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001814 MVT VT = N1.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001815
Dan Gohman7f321562007-06-25 16:23:39 +00001816 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001817 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001818 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001819 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001820 }
Dan Gohman7f321562007-06-25 16:23:39 +00001821
Dan Gohman613e0d82007-07-03 14:03:57 +00001822 // fold (or x, undef) -> -1
Dan Gohmand595b5f2007-07-10 14:20:37 +00001823 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Chris Lattnere094f542007-07-09 16:16:34 +00001824 return DAG.getConstant(~0ULL, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001825 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001826 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001827 return DAG.getNode(ISD::OR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001828 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001829 if (N0C && !N1C)
1830 return DAG.getNode(ISD::OR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001831 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001832 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001833 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001834 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001835 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001836 return N1;
1837 // fold (or x, c) -> c iff (x & ~c) == 0
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001838 if (N1C && DAG.MaskedValueIsZero(N0, ~N1C->getAPIntValue()))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001839 return N1;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001840 // reassociate or
Dan Gohman475871a2008-07-27 21:46:04 +00001841 SDValue ROR = ReassociateOps(ISD::OR, N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001842 if (ROR.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001843 return ROR;
1844 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
Gabor Greifba36cb52008-08-28 21:40:38 +00001845 if (N1C && N0.getOpcode() == ISD::AND && N0.getNode()->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00001846 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00001847 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
1848 return DAG.getNode(ISD::AND, VT, DAG.getNode(ISD::OR, VT, N0.getOperand(0),
1849 N1),
Dan Gohman002e5d02008-03-13 22:13:53 +00001850 DAG.getConstant(N1C->getAPIntValue() |
1851 C1->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00001852 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001853 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
1854 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1855 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1856 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1857
1858 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00001859 LL.getValueType().isInteger()) {
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001860 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
1861 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
Dan Gohman002e5d02008-03-13 22:13:53 +00001862 if (cast<ConstantSDNode>(LR)->isNullValue() &&
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001863 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001864 SDValue ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001865 AddToWorkList(ORNode.getNode());
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001866 return DAG.getSetCC(VT, ORNode, LR, Op1);
1867 }
1868 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
1869 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
1870 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
1871 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001872 SDValue ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001873 AddToWorkList(ANDNode.getNode());
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001874 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1875 }
1876 }
1877 // canonicalize equivalent to ll == rl
1878 if (LL == RR && LR == RL) {
1879 Op1 = ISD::getSetCCSwappedOperands(Op1);
1880 std::swap(RL, RR);
1881 }
1882 if (LL == RL && LR == RR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001883 bool isInteger = LL.getValueType().isInteger();
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001884 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
1885 if (Result != ISD::SETCC_INVALID)
1886 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1887 }
1888 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001889
1890 // Simplify: or (op x...), (op y...) -> (op (or x, y))
1891 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001892 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001893 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001894 }
Chris Lattner516b9622006-09-14 20:50:57 +00001895
Chris Lattner1ec72732006-09-14 21:11:37 +00001896 // (X & C1) | (Y & C2) -> (X|Y) & C3 if possible.
1897 if (N0.getOpcode() == ISD::AND &&
1898 N1.getOpcode() == ISD::AND &&
1899 N0.getOperand(1).getOpcode() == ISD::Constant &&
1900 N1.getOperand(1).getOpcode() == ISD::Constant &&
1901 // Don't increase # computations.
Gabor Greifba36cb52008-08-28 21:40:38 +00001902 (N0.getNode()->hasOneUse() || N1.getNode()->hasOneUse())) {
Chris Lattner1ec72732006-09-14 21:11:37 +00001903 // We can only do this xform if we know that bits from X that are set in C2
1904 // but not in C1 are already zero. Likewise for Y.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001905 const APInt &LHSMask =
1906 cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
1907 const APInt &RHSMask =
1908 cast<ConstantSDNode>(N1.getOperand(1))->getAPIntValue();
Chris Lattner1ec72732006-09-14 21:11:37 +00001909
Dan Gohmanea859be2007-06-22 14:59:07 +00001910 if (DAG.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) &&
1911 DAG.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001912 SDValue X =DAG.getNode(ISD::OR, VT, N0.getOperand(0), N1.getOperand(0));
Chris Lattner1ec72732006-09-14 21:11:37 +00001913 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(LHSMask|RHSMask, VT));
1914 }
1915 }
1916
1917
Chris Lattner516b9622006-09-14 20:50:57 +00001918 // See if this is some rotate idiom.
1919 if (SDNode *Rot = MatchRotate(N0, N1))
Dan Gohman475871a2008-07-27 21:46:04 +00001920 return SDValue(Rot, 0);
Chris Lattner35e5c142006-05-05 05:51:50 +00001921
Dan Gohman475871a2008-07-27 21:46:04 +00001922 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001923}
1924
Chris Lattner516b9622006-09-14 20:50:57 +00001925
1926/// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present.
Dan Gohman475871a2008-07-27 21:46:04 +00001927static bool MatchRotateHalf(SDValue Op, SDValue &Shift, SDValue &Mask) {
Chris Lattner516b9622006-09-14 20:50:57 +00001928 if (Op.getOpcode() == ISD::AND) {
Reid Spencer3ed469c2006-11-02 20:25:50 +00001929 if (isa<ConstantSDNode>(Op.getOperand(1))) {
Chris Lattner516b9622006-09-14 20:50:57 +00001930 Mask = Op.getOperand(1);
1931 Op = Op.getOperand(0);
1932 } else {
1933 return false;
1934 }
1935 }
1936
1937 if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) {
1938 Shift = Op;
1939 return true;
1940 }
1941 return false;
1942}
1943
1944
1945// MatchRotate - Handle an 'or' of two operands. If this is one of the many
1946// idioms for rotate, and if the target supports rotation instructions, generate
1947// a rot[lr].
Dan Gohman475871a2008-07-27 21:46:04 +00001948SDNode *DAGCombiner::MatchRotate(SDValue LHS, SDValue RHS) {
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001949 // Must be a legal type. Expanded 'n promoted things won't work with rotates.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001950 MVT VT = LHS.getValueType();
Chris Lattner516b9622006-09-14 20:50:57 +00001951 if (!TLI.isTypeLegal(VT)) return 0;
1952
1953 // The target must have at least one rotate flavor.
1954 bool HasROTL = TLI.isOperationLegal(ISD::ROTL, VT);
1955 bool HasROTR = TLI.isOperationLegal(ISD::ROTR, VT);
1956 if (!HasROTL && !HasROTR) return 0;
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001957
Chris Lattner516b9622006-09-14 20:50:57 +00001958 // Match "(X shl/srl V1) & V2" where V2 may not be present.
Dan Gohman475871a2008-07-27 21:46:04 +00001959 SDValue LHSShift; // The shift.
1960 SDValue LHSMask; // AND value if any.
Chris Lattner516b9622006-09-14 20:50:57 +00001961 if (!MatchRotateHalf(LHS, LHSShift, LHSMask))
1962 return 0; // Not part of a rotate.
1963
Dan Gohman475871a2008-07-27 21:46:04 +00001964 SDValue RHSShift; // The shift.
1965 SDValue RHSMask; // AND value if any.
Chris Lattner516b9622006-09-14 20:50:57 +00001966 if (!MatchRotateHalf(RHS, RHSShift, RHSMask))
1967 return 0; // Not part of a rotate.
1968
1969 if (LHSShift.getOperand(0) != RHSShift.getOperand(0))
1970 return 0; // Not shifting the same value.
1971
1972 if (LHSShift.getOpcode() == RHSShift.getOpcode())
1973 return 0; // Shifts must disagree.
1974
1975 // Canonicalize shl to left side in a shl/srl pair.
1976 if (RHSShift.getOpcode() == ISD::SHL) {
1977 std::swap(LHS, RHS);
1978 std::swap(LHSShift, RHSShift);
1979 std::swap(LHSMask , RHSMask );
1980 }
1981
Duncan Sands83ec4b62008-06-06 12:08:01 +00001982 unsigned OpSizeInBits = VT.getSizeInBits();
Dan Gohman475871a2008-07-27 21:46:04 +00001983 SDValue LHSShiftArg = LHSShift.getOperand(0);
1984 SDValue LHSShiftAmt = LHSShift.getOperand(1);
1985 SDValue RHSShiftAmt = RHSShift.getOperand(1);
Chris Lattner516b9622006-09-14 20:50:57 +00001986
1987 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
1988 // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2)
Scott Michelc9dc1142007-04-02 21:36:32 +00001989 if (LHSShiftAmt.getOpcode() == ISD::Constant &&
1990 RHSShiftAmt.getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001991 uint64_t LShVal = cast<ConstantSDNode>(LHSShiftAmt)->getZExtValue();
1992 uint64_t RShVal = cast<ConstantSDNode>(RHSShiftAmt)->getZExtValue();
Chris Lattner516b9622006-09-14 20:50:57 +00001993 if ((LShVal + RShVal) != OpSizeInBits)
1994 return 0;
1995
Dan Gohman475871a2008-07-27 21:46:04 +00001996 SDValue Rot;
Chris Lattner516b9622006-09-14 20:50:57 +00001997 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00001998 Rot = DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00001999 else
Scott Michelc9dc1142007-04-02 21:36:32 +00002000 Rot = DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00002001
2002 // If there is an AND of either shifted operand, apply it to the result.
Gabor Greifba36cb52008-08-28 21:40:38 +00002003 if (LHSMask.getNode() || RHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00002004 APInt Mask = APInt::getAllOnesValue(OpSizeInBits);
Chris Lattner516b9622006-09-14 20:50:57 +00002005
Gabor Greifba36cb52008-08-28 21:40:38 +00002006 if (LHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00002007 APInt RHSBits = APInt::getLowBitsSet(OpSizeInBits, LShVal);
2008 Mask &= cast<ConstantSDNode>(LHSMask)->getAPIntValue() | RHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00002009 }
Gabor Greifba36cb52008-08-28 21:40:38 +00002010 if (RHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00002011 APInt LHSBits = APInt::getHighBitsSet(OpSizeInBits, RShVal);
2012 Mask &= cast<ConstantSDNode>(RHSMask)->getAPIntValue() | LHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00002013 }
2014
2015 Rot = DAG.getNode(ISD::AND, VT, Rot, DAG.getConstant(Mask, VT));
2016 }
2017
Gabor Greifba36cb52008-08-28 21:40:38 +00002018 return Rot.getNode();
Chris Lattner516b9622006-09-14 20:50:57 +00002019 }
2020
2021 // If there is a mask here, and we have a variable shift, we can't be sure
2022 // that we're masking out the right stuff.
Gabor Greifba36cb52008-08-28 21:40:38 +00002023 if (LHSMask.getNode() || RHSMask.getNode())
Chris Lattner516b9622006-09-14 20:50:57 +00002024 return 0;
2025
2026 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
2027 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotr x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00002028 if (RHSShiftAmt.getOpcode() == ISD::SUB &&
2029 LHSShiftAmt == RHSShiftAmt.getOperand(1)) {
Chris Lattner516b9622006-09-14 20:50:57 +00002030 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00002031 dyn_cast<ConstantSDNode>(RHSShiftAmt.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002032 if (SUBC->getAPIntValue() == OpSizeInBits) {
Chris Lattner516b9622006-09-14 20:50:57 +00002033 if (HasROTL)
Gabor Greifba36cb52008-08-28 21:40:38 +00002034 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).getNode();
Chris Lattner516b9622006-09-14 20:50:57 +00002035 else
Gabor Greifba36cb52008-08-28 21:40:38 +00002036 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).getNode();
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002037 }
Chris Lattner516b9622006-09-14 20:50:57 +00002038 }
2039 }
2040
2041 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
2042 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotl x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00002043 if (LHSShiftAmt.getOpcode() == ISD::SUB &&
2044 RHSShiftAmt == LHSShiftAmt.getOperand(1)) {
Chris Lattner516b9622006-09-14 20:50:57 +00002045 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00002046 dyn_cast<ConstantSDNode>(LHSShiftAmt.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002047 if (SUBC->getAPIntValue() == OpSizeInBits) {
Bill Wendling2692d592008-08-31 01:13:31 +00002048 if (HasROTR)
Gabor Greifba36cb52008-08-28 21:40:38 +00002049 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).getNode();
Bill Wendling2692d592008-08-31 01:13:31 +00002050 else
2051 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).getNode();
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002052 }
Scott Michelc9dc1142007-04-02 21:36:32 +00002053 }
2054 }
2055
2056 // Look for sign/zext/any-extended cases:
2057 if ((LHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
2058 || LHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
2059 || LHSShiftAmt.getOpcode() == ISD::ANY_EXTEND) &&
2060 (RHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
2061 || RHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
2062 || RHSShiftAmt.getOpcode() == ISD::ANY_EXTEND)) {
Dan Gohman475871a2008-07-27 21:46:04 +00002063 SDValue LExtOp0 = LHSShiftAmt.getOperand(0);
2064 SDValue RExtOp0 = RHSShiftAmt.getOperand(0);
Scott Michelc9dc1142007-04-02 21:36:32 +00002065 if (RExtOp0.getOpcode() == ISD::SUB &&
2066 RExtOp0.getOperand(1) == LExtOp0) {
2067 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00002068 // (rotl x, y)
Scott Michelc9dc1142007-04-02 21:36:32 +00002069 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00002070 // (rotr x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00002071 if (ConstantSDNode *SUBC = cast<ConstantSDNode>(RExtOp0.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002072 if (SUBC->getAPIntValue() == OpSizeInBits) {
Gabor Greif12632d22008-08-30 19:29:20 +00002073 return DAG.getNode(HasROTL ? ISD::ROTL : ISD::ROTR, VT, LHSShiftArg,
2074 HasROTL ? LHSShiftAmt : RHSShiftAmt).getNode();
Scott Michelc9dc1142007-04-02 21:36:32 +00002075 }
2076 }
2077 } else if (LExtOp0.getOpcode() == ISD::SUB &&
2078 RExtOp0 == LExtOp0.getOperand(1)) {
Bill Wendling353dea22008-08-31 01:04:56 +00002079 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext y))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00002080 // (rotr x, y)
Bill Wendling353dea22008-08-31 01:04:56 +00002081 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext y))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00002082 // (rotl x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00002083 if (ConstantSDNode *SUBC = cast<ConstantSDNode>(LExtOp0.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002084 if (SUBC->getAPIntValue() == OpSizeInBits) {
Bill Wendling353dea22008-08-31 01:04:56 +00002085 return DAG.getNode(HasROTR ? ISD::ROTR : ISD::ROTL, VT, LHSShiftArg,
2086 HasROTR ? RHSShiftAmt : LHSShiftAmt).getNode();
Scott Michelc9dc1142007-04-02 21:36:32 +00002087 }
2088 }
Chris Lattner516b9622006-09-14 20:50:57 +00002089 }
2090 }
2091
2092 return 0;
2093}
2094
2095
Dan Gohman475871a2008-07-27 21:46:04 +00002096SDValue DAGCombiner::visitXOR(SDNode *N) {
2097 SDValue N0 = N->getOperand(0);
2098 SDValue N1 = N->getOperand(1);
2099 SDValue LHS, RHS, CC;
Nate Begeman646d7e22005-09-02 21:18:40 +00002100 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2101 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002102 MVT VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002103
Dan Gohman7f321562007-06-25 16:23:39 +00002104 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00002105 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002106 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002107 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00002108 }
Dan Gohman7f321562007-06-25 16:23:39 +00002109
Evan Cheng26471c42008-03-25 20:08:07 +00002110 // fold (xor undef, undef) -> 0. This is a common idiom (misuse).
2111 if (N0.getOpcode() == ISD::UNDEF && N1.getOpcode() == ISD::UNDEF)
2112 return DAG.getConstant(0, VT);
Dan Gohman613e0d82007-07-03 14:03:57 +00002113 // fold (xor x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00002114 if (N0.getOpcode() == ISD::UNDEF)
2115 return N0;
2116 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00002117 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002118 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002119 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002120 return DAG.getNode(ISD::XOR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00002121 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00002122 if (N0C && !N1C)
2123 return DAG.getNode(ISD::XOR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002124 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002125 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002126 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00002127 // reassociate xor
Dan Gohman475871a2008-07-27 21:46:04 +00002128 SDValue RXOR = ReassociateOps(ISD::XOR, N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00002129 if (RXOR.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00002130 return RXOR;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002131 // fold !(x cc y) -> (x !cc y)
Dan Gohman002e5d02008-03-13 22:13:53 +00002132 if (N1C && N1C->getAPIntValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002133 bool isInt = LHS.getValueType().isInteger();
Nate Begeman646d7e22005-09-02 21:18:40 +00002134 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
2135 isInt);
2136 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002137 return DAG.getSetCC(VT, LHS, RHS, NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00002138 if (N0.getOpcode() == ISD::SELECT_CC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002139 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00002140 assert(0 && "Unhandled SetCC Equivalent!");
2141 abort();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002142 }
Chris Lattner61c5ff42007-09-10 21:39:07 +00002143 // fold (not (zext (setcc x, y))) -> (zext (not (setcc x, y)))
Dan Gohman002e5d02008-03-13 22:13:53 +00002144 if (N1C && N1C->getAPIntValue() == 1 && N0.getOpcode() == ISD::ZERO_EXTEND &&
Gabor Greif12632d22008-08-30 19:29:20 +00002145 N0.getNode()->hasOneUse() &&
2146 isSetCCEquivalent(N0.getOperand(0), LHS, RHS, CC)){
Dan Gohman475871a2008-07-27 21:46:04 +00002147 SDValue V = N0.getOperand(0);
Chris Lattner61c5ff42007-09-10 21:39:07 +00002148 V = DAG.getNode(ISD::XOR, V.getValueType(), V,
Duncan Sands272dce02007-10-10 09:54:50 +00002149 DAG.getConstant(1, V.getValueType()));
Gabor Greifba36cb52008-08-28 21:40:38 +00002150 AddToWorkList(V.getNode());
Chris Lattner61c5ff42007-09-10 21:39:07 +00002151 return DAG.getNode(ISD::ZERO_EXTEND, VT, V);
2152 }
2153
Nate Begeman99801192005-09-07 23:25:52 +00002154 // fold !(x or y) -> (!x and !y) iff x or y are setcc
Dan Gohman002e5d02008-03-13 22:13:53 +00002155 if (N1C && N1C->getAPIntValue() == 1 && VT == MVT::i1 &&
Nate Begeman99801192005-09-07 23:25:52 +00002156 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Dan Gohman475871a2008-07-27 21:46:04 +00002157 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00002158 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
2159 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002160 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
2161 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Gabor Greifba36cb52008-08-28 21:40:38 +00002162 AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode());
Nate Begeman99801192005-09-07 23:25:52 +00002163 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002164 }
2165 }
Nate Begeman99801192005-09-07 23:25:52 +00002166 // fold !(x or y) -> (!x and !y) iff x or y are constants
2167 if (N1C && N1C->isAllOnesValue() &&
2168 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Dan Gohman475871a2008-07-27 21:46:04 +00002169 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00002170 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
2171 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002172 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
2173 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Gabor Greifba36cb52008-08-28 21:40:38 +00002174 AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode());
Nate Begeman99801192005-09-07 23:25:52 +00002175 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002176 }
2177 }
Nate Begeman223df222005-09-08 20:18:10 +00002178 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
2179 if (N1C && N0.getOpcode() == ISD::XOR) {
2180 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
2181 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2182 if (N00C)
2183 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
Dan Gohman002e5d02008-03-13 22:13:53 +00002184 DAG.getConstant(N1C->getAPIntValue()^
2185 N00C->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00002186 if (N01C)
2187 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
Dan Gohman002e5d02008-03-13 22:13:53 +00002188 DAG.getConstant(N1C->getAPIntValue()^
2189 N01C->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00002190 }
2191 // fold (xor x, x) -> 0
Chris Lattner4fbdd592006-03-28 19:11:05 +00002192 if (N0 == N1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002193 if (!VT.isVector()) {
Chris Lattner4fbdd592006-03-28 19:11:05 +00002194 return DAG.getConstant(0, VT);
2195 } else if (!AfterLegalize || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) {
2196 // Produce a vector of zeros.
Dan Gohman475871a2008-07-27 21:46:04 +00002197 SDValue El = DAG.getConstant(0, VT.getVectorElementType());
2198 std::vector<SDValue> Ops(VT.getVectorNumElements(), El);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002199 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4fbdd592006-03-28 19:11:05 +00002200 }
2201 }
Chris Lattner35e5c142006-05-05 05:51:50 +00002202
2203 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
2204 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002205 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002206 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002207 }
Chris Lattner35e5c142006-05-05 05:51:50 +00002208
Chris Lattner3e104b12006-04-08 04:15:24 +00002209 // Simplify the expression using non-local knowledge.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002210 if (!VT.isVector() &&
Dan Gohman475871a2008-07-27 21:46:04 +00002211 SimplifyDemandedBits(SDValue(N, 0)))
2212 return SDValue(N, 0);
Chris Lattner3e104b12006-04-08 04:15:24 +00002213
Dan Gohman475871a2008-07-27 21:46:04 +00002214 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002215}
2216
Chris Lattnere70da202007-12-06 07:33:36 +00002217/// visitShiftByConstant - Handle transforms common to the three shifts, when
2218/// the shift amount is a constant.
Dan Gohman475871a2008-07-27 21:46:04 +00002219SDValue DAGCombiner::visitShiftByConstant(SDNode *N, unsigned Amt) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002220 SDNode *LHS = N->getOperand(0).getNode();
Dan Gohman475871a2008-07-27 21:46:04 +00002221 if (!LHS->hasOneUse()) return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00002222
2223 // We want to pull some binops through shifts, so that we have (and (shift))
2224 // instead of (shift (and)), likewise for add, or, xor, etc. This sort of
2225 // thing happens with address calculations, so it's important to canonicalize
2226 // it.
2227 bool HighBitSet = false; // Can we transform this if the high bit is set?
2228
2229 switch (LHS->getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002230 default: return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00002231 case ISD::OR:
2232 case ISD::XOR:
2233 HighBitSet = false; // We can only transform sra if the high bit is clear.
2234 break;
2235 case ISD::AND:
2236 HighBitSet = true; // We can only transform sra if the high bit is set.
2237 break;
2238 case ISD::ADD:
2239 if (N->getOpcode() != ISD::SHL)
Dan Gohman475871a2008-07-27 21:46:04 +00002240 return SDValue(); // only shl(add) not sr[al](add).
Chris Lattnere70da202007-12-06 07:33:36 +00002241 HighBitSet = false; // We can only transform sra if the high bit is clear.
2242 break;
2243 }
2244
2245 // We require the RHS of the binop to be a constant as well.
2246 ConstantSDNode *BinOpCst = dyn_cast<ConstantSDNode>(LHS->getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00002247 if (!BinOpCst) return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00002248
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00002249
2250 // FIXME: disable this for unless the input to the binop is a shift by a
2251 // constant. If it is not a shift, it pessimizes some common cases like:
2252 //
2253 //void foo(int *X, int i) { X[i & 1235] = 1; }
2254 //int bar(int *X, int i) { return X[i & 255]; }
Gabor Greifba36cb52008-08-28 21:40:38 +00002255 SDNode *BinOpLHSVal = LHS->getOperand(0).getNode();
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00002256 if ((BinOpLHSVal->getOpcode() != ISD::SHL &&
2257 BinOpLHSVal->getOpcode() != ISD::SRA &&
2258 BinOpLHSVal->getOpcode() != ISD::SRL) ||
2259 !isa<ConstantSDNode>(BinOpLHSVal->getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00002260 return SDValue();
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00002261
Duncan Sands83ec4b62008-06-06 12:08:01 +00002262 MVT VT = N->getValueType(0);
Chris Lattnere70da202007-12-06 07:33:36 +00002263
2264 // If this is a signed shift right, and the high bit is modified
2265 // by the logical operation, do not perform the transformation.
2266 // The highBitSet boolean indicates the value of the high bit of
2267 // the constant which would cause it to be modified for this
2268 // operation.
2269 if (N->getOpcode() == ISD::SRA) {
Dan Gohman220a8232008-03-03 23:51:38 +00002270 bool BinOpRHSSignSet = BinOpCst->getAPIntValue().isNegative();
2271 if (BinOpRHSSignSet != HighBitSet)
Dan Gohman475871a2008-07-27 21:46:04 +00002272 return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00002273 }
2274
2275 // Fold the constants, shifting the binop RHS by the shift amount.
Dan Gohman475871a2008-07-27 21:46:04 +00002276 SDValue NewRHS = DAG.getNode(N->getOpcode(), N->getValueType(0),
Chris Lattnere70da202007-12-06 07:33:36 +00002277 LHS->getOperand(1), N->getOperand(1));
2278
2279 // Create the new shift.
Dan Gohman475871a2008-07-27 21:46:04 +00002280 SDValue NewShift = DAG.getNode(N->getOpcode(), VT, LHS->getOperand(0),
Chris Lattnere70da202007-12-06 07:33:36 +00002281 N->getOperand(1));
2282
2283 // Create the new binop.
2284 return DAG.getNode(LHS->getOpcode(), VT, NewShift, NewRHS);
2285}
2286
2287
Dan Gohman475871a2008-07-27 21:46:04 +00002288SDValue DAGCombiner::visitSHL(SDNode *N) {
2289 SDValue N0 = N->getOperand(0);
2290 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002291 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2292 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002293 MVT VT = N0.getValueType();
2294 unsigned OpSizeInBits = VT.getSizeInBits();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002295
2296 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002297 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002298 return DAG.getNode(ISD::SHL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002299 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002300 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002301 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002302 // fold (shl x, c >= size(x)) -> undef
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002303 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002304 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002305 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002306 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002307 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002308 // if (shl x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00002309 if (DAG.MaskedValueIsZero(SDValue(N, 0),
Duncan Sands83ec4b62008-06-06 12:08:01 +00002310 APInt::getAllOnesValue(VT.getSizeInBits())))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002311 return DAG.getConstant(0, VT);
Evan Chengeb9f8922008-08-30 02:03:58 +00002312 // fold (shl x, (trunc (and y, c))) -> (shl x, (and (trunc y), c))
2313 // iff (trunc c) == c
2314 if (N1.getOpcode() == ISD::TRUNCATE &&
2315 N1.getOperand(0).getOpcode() == ISD::AND) {
2316 SDValue N101 = N1.getOperand(0).getOperand(1);
2317 ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101);
2318 if (N101C) {
2319 MVT TruncVT = N1.getValueType();
2320 unsigned TruncBitSize = TruncVT.getSizeInBits();
2321 APInt ShAmt = N101C->getAPIntValue();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002322 if (ShAmt.trunc(TruncBitSize).getZExtValue() == N101C->getZExtValue()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00002323 SDValue N100 = N1.getOperand(0).getOperand(0);
2324 return DAG.getNode(ISD::SHL, VT, N0,
2325 DAG.getNode(ISD::AND, TruncVT,
2326 DAG.getNode(ISD::TRUNCATE, TruncVT, N100),
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002327 DAG.getConstant(N101C->getZExtValue(),
2328 TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00002329 }
2330 }
2331 }
2332
Dan Gohman475871a2008-07-27 21:46:04 +00002333 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
2334 return SDValue(N, 0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002335 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002336 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002337 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002338 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
2339 uint64_t c2 = N1C->getZExtValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002340 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002341 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002342 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002343 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002344 }
2345 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
2346 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002347 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002348 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002349 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
2350 uint64_t c2 = N1C->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00002351 SDValue Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman1d4d4142005-09-01 00:19:25 +00002352 DAG.getConstant(~0ULL << c1, VT));
2353 if (c2 > c1)
2354 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +00002355 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002356 else
Nate Begeman83e75ec2005-09-06 04:43:02 +00002357 return DAG.getNode(ISD::SRL, VT, Mask,
2358 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002359 }
2360 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00002361 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +00002362 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002363 DAG.getConstant(~0ULL << N1C->getZExtValue(), VT));
Chris Lattnere70da202007-12-06 07:33:36 +00002364
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002365 return N1C ? visitShiftByConstant(N, N1C->getZExtValue()) : SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002366}
2367
Dan Gohman475871a2008-07-27 21:46:04 +00002368SDValue DAGCombiner::visitSRA(SDNode *N) {
2369 SDValue N0 = N->getOperand(0);
2370 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002371 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2372 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002373 MVT VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002374
2375 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002376 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002377 return DAG.getNode(ISD::SRA, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002378 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002379 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002380 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002381 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00002382 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002383 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002384 // fold (sra x, c >= size(x)) -> undef
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002385 if (N1C && N1C->getZExtValue() >= VT.getSizeInBits())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002386 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002387 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002388 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002389 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00002390 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
2391 // sext_inreg.
2392 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002393 unsigned LowBits = VT.getSizeInBits() - (unsigned)N1C->getZExtValue();
Duncan Sands8eab8a22008-06-09 11:32:28 +00002394 MVT EVT = MVT::getIntegerVT(LowBits);
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002395 if (EVT.isSimple() && // TODO: remove when apint codegen support lands.
2396 (!AfterLegalize || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, EVT)))
Nate Begemanfb7217b2006-02-17 19:54:08 +00002397 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
2398 DAG.getValueType(EVT));
2399 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002400
Chris Lattner71d9ebc2006-02-28 06:23:04 +00002401 // fold (sra (sra x, c1), c2) -> (sra x, c1+c2)
2402 if (N1C && N0.getOpcode() == ISD::SRA) {
2403 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002404 unsigned Sum = N1C->getZExtValue() + C1->getZExtValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002405 if (Sum >= VT.getSizeInBits()) Sum = VT.getSizeInBits()-1;
Chris Lattner71d9ebc2006-02-28 06:23:04 +00002406 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0),
2407 DAG.getConstant(Sum, N1C->getValueType(0)));
2408 }
2409 }
Christopher Lamb15cbde32008-03-19 08:30:06 +00002410
2411 // fold sra (shl X, m), result_size - n
2412 // -> (sign_extend (trunc (shl X, result_size - n - m))) for
Christopher Lambb9b04282008-03-20 04:31:39 +00002413 // result_size - n != m.
2414 // If truncate is free for the target sext(shl) is likely to result in better
2415 // code.
Christopher Lamb15cbde32008-03-19 08:30:06 +00002416 if (N0.getOpcode() == ISD::SHL) {
2417 // Get the two constanst of the shifts, CN0 = m, CN = n.
2418 const ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2419 if (N01C && N1C) {
Christopher Lambb9b04282008-03-20 04:31:39 +00002420 // Determine what the truncate's result bitsize and type would be.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002421 unsigned VTValSize = VT.getSizeInBits();
2422 MVT TruncVT =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002423 MVT::getIntegerVT(VTValSize - N1C->getZExtValue());
Christopher Lambb9b04282008-03-20 04:31:39 +00002424 // Determine the residual right-shift amount.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002425 unsigned ShiftAmt = N1C->getZExtValue() - N01C->getZExtValue();
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002426
Christopher Lambb9b04282008-03-20 04:31:39 +00002427 // If the shift is not a no-op (in which case this should be just a sign
2428 // extend already), the truncated to type is legal, sign_extend is legal
Gabor Greif12632d22008-08-30 19:29:20 +00002429 // on that type, and the the truncate to that type is both legal and free,
Christopher Lambb9b04282008-03-20 04:31:39 +00002430 // perform the transform.
2431 if (ShiftAmt &&
Christopher Lambb9b04282008-03-20 04:31:39 +00002432 TLI.isOperationLegal(ISD::SIGN_EXTEND, TruncVT) &&
2433 TLI.isOperationLegal(ISD::TRUNCATE, VT) &&
Evan Cheng260e07e2008-03-20 02:18:41 +00002434 TLI.isTruncateFree(VT, TruncVT)) {
Christopher Lambb9b04282008-03-20 04:31:39 +00002435
Dan Gohman475871a2008-07-27 21:46:04 +00002436 SDValue Amt = DAG.getConstant(ShiftAmt, TLI.getShiftAmountTy());
2437 SDValue Shift = DAG.getNode(ISD::SRL, VT, N0.getOperand(0), Amt);
2438 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, TruncVT, Shift);
Christopher Lambb9b04282008-03-20 04:31:39 +00002439 return DAG.getNode(ISD::SIGN_EXTEND, N->getValueType(0), Trunc);
Christopher Lamb15cbde32008-03-19 08:30:06 +00002440 }
2441 }
2442 }
Chris Lattner71d9ebc2006-02-28 06:23:04 +00002443
Evan Chengeb9f8922008-08-30 02:03:58 +00002444 // fold (sra x, (trunc (and y, c))) -> (sra x, (and (trunc y), c))
2445 // iff (trunc c) == c
2446 if (N1.getOpcode() == ISD::TRUNCATE &&
2447 N1.getOperand(0).getOpcode() == ISD::AND) {
2448 SDValue N101 = N1.getOperand(0).getOperand(1);
2449 ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101);
2450 if (N101C) {
2451 MVT TruncVT = N1.getValueType();
2452 unsigned TruncBitSize = TruncVT.getSizeInBits();
2453 APInt ShAmt = N101C->getAPIntValue();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002454 if (ShAmt.trunc(TruncBitSize).getZExtValue() == N101C->getZExtValue()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00002455 SDValue N100 = N1.getOperand(0).getOperand(0);
2456 return DAG.getNode(ISD::SRA, VT, N0,
2457 DAG.getNode(ISD::AND, TruncVT,
2458 DAG.getNode(ISD::TRUNCATE, TruncVT, N100),
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002459 DAG.getConstant(N101C->getZExtValue(),
2460 TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00002461 }
2462 }
2463 }
2464
Chris Lattnera8504462006-05-08 20:51:54 +00002465 // Simplify, based on bits shifted out of the LHS.
Dan Gohman475871a2008-07-27 21:46:04 +00002466 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
2467 return SDValue(N, 0);
Chris Lattnera8504462006-05-08 20:51:54 +00002468
2469
Nate Begeman1d4d4142005-09-01 00:19:25 +00002470 // If the sign bit is known to be zero, switch this to a SRL.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002471 if (DAG.SignBitIsZero(N0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002472 return DAG.getNode(ISD::SRL, VT, N0, N1);
Chris Lattnere70da202007-12-06 07:33:36 +00002473
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002474 return N1C ? visitShiftByConstant(N, N1C->getZExtValue()) : SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002475}
2476
Dan Gohman475871a2008-07-27 21:46:04 +00002477SDValue DAGCombiner::visitSRL(SDNode *N) {
2478 SDValue N0 = N->getOperand(0);
2479 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002480 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2481 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002482 MVT VT = N0.getValueType();
2483 unsigned OpSizeInBits = VT.getSizeInBits();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002484
2485 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002486 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002487 return DAG.getNode(ISD::SRL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002488 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002489 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002490 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002491 // fold (srl x, c >= size(x)) -> undef
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002492 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002493 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002494 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002495 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002496 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002497 // if (srl x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00002498 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002499 APInt::getAllOnesValue(OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002500 return DAG.getConstant(0, VT);
Chris Lattnerec06e9a2007-04-18 03:05:22 +00002501
Nate Begeman1d4d4142005-09-01 00:19:25 +00002502 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002503 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002504 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002505 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
2506 uint64_t c2 = N1C->getZExtValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002507 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002508 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002509 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002510 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002511 }
Chris Lattner350bec02006-04-02 06:11:11 +00002512
Chris Lattner06afe072006-05-05 22:53:17 +00002513 // fold (srl (anyextend x), c) -> (anyextend (srl x, c))
2514 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
2515 // Shifting in all undef bits?
Duncan Sands83ec4b62008-06-06 12:08:01 +00002516 MVT SmallVT = N0.getOperand(0).getValueType();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002517 if (N1C->getZExtValue() >= SmallVT.getSizeInBits())
Chris Lattner06afe072006-05-05 22:53:17 +00002518 return DAG.getNode(ISD::UNDEF, VT);
2519
Dan Gohman475871a2008-07-27 21:46:04 +00002520 SDValue SmallShift = DAG.getNode(ISD::SRL, SmallVT, N0.getOperand(0), N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00002521 AddToWorkList(SmallShift.getNode());
Chris Lattner06afe072006-05-05 22:53:17 +00002522 return DAG.getNode(ISD::ANY_EXTEND, VT, SmallShift);
2523 }
2524
Chris Lattner3657ffe2006-10-12 20:23:19 +00002525 // fold (srl (sra X, Y), 31) -> (srl X, 31). This srl only looks at the sign
2526 // bit, which is unmodified by sra.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002527 if (N1C && N1C->getZExtValue()+1 == VT.getSizeInBits()) {
Chris Lattner3657ffe2006-10-12 20:23:19 +00002528 if (N0.getOpcode() == ISD::SRA)
2529 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0), N1);
2530 }
2531
Chris Lattner350bec02006-04-02 06:11:11 +00002532 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
2533 if (N1C && N0.getOpcode() == ISD::CTLZ &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002534 N1C->getAPIntValue() == Log2_32(VT.getSizeInBits())) {
Dan Gohman948d8ea2008-02-20 16:33:30 +00002535 APInt KnownZero, KnownOne;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002536 APInt Mask = APInt::getAllOnesValue(VT.getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00002537 DAG.ComputeMaskedBits(N0.getOperand(0), Mask, KnownZero, KnownOne);
Chris Lattner350bec02006-04-02 06:11:11 +00002538
2539 // If any of the input bits are KnownOne, then the input couldn't be all
2540 // zeros, thus the result of the srl will always be zero.
Dan Gohman948d8ea2008-02-20 16:33:30 +00002541 if (KnownOne.getBoolValue()) return DAG.getConstant(0, VT);
Chris Lattner350bec02006-04-02 06:11:11 +00002542
2543 // If all of the bits input the to ctlz node are known to be zero, then
2544 // the result of the ctlz is "32" and the result of the shift is one.
Dan Gohman948d8ea2008-02-20 16:33:30 +00002545 APInt UnknownBits = ~KnownZero & Mask;
Chris Lattner350bec02006-04-02 06:11:11 +00002546 if (UnknownBits == 0) return DAG.getConstant(1, VT);
2547
2548 // Otherwise, check to see if there is exactly one bit input to the ctlz.
2549 if ((UnknownBits & (UnknownBits-1)) == 0) {
2550 // Okay, we know that only that the single bit specified by UnknownBits
2551 // could be set on input to the CTLZ node. If this bit is set, the SRL
2552 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
2553 // to an SRL,XOR pair, which is likely to simplify more.
Dan Gohman948d8ea2008-02-20 16:33:30 +00002554 unsigned ShAmt = UnknownBits.countTrailingZeros();
Dan Gohman475871a2008-07-27 21:46:04 +00002555 SDValue Op = N0.getOperand(0);
Chris Lattner350bec02006-04-02 06:11:11 +00002556 if (ShAmt) {
2557 Op = DAG.getNode(ISD::SRL, VT, Op,
2558 DAG.getConstant(ShAmt, TLI.getShiftAmountTy()));
Gabor Greifba36cb52008-08-28 21:40:38 +00002559 AddToWorkList(Op.getNode());
Chris Lattner350bec02006-04-02 06:11:11 +00002560 }
2561 return DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(1, VT));
2562 }
2563 }
Evan Chengeb9f8922008-08-30 02:03:58 +00002564
2565 // fold (srl x, (trunc (and y, c))) -> (srl x, (and (trunc y), c))
2566 // iff (trunc c) == c
2567 if (N1.getOpcode() == ISD::TRUNCATE &&
2568 N1.getOperand(0).getOpcode() == ISD::AND) {
2569 SDValue N101 = N1.getOperand(0).getOperand(1);
2570 ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101);
2571 if (N101C) {
2572 MVT TruncVT = N1.getValueType();
2573 unsigned TruncBitSize = TruncVT.getSizeInBits();
2574 APInt ShAmt = N101C->getAPIntValue();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002575 if (ShAmt.trunc(TruncBitSize).getZExtValue() == N101C->getZExtValue()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00002576 SDValue N100 = N1.getOperand(0).getOperand(0);
2577 return DAG.getNode(ISD::SRL, VT, N0,
2578 DAG.getNode(ISD::AND, TruncVT,
2579 DAG.getNode(ISD::TRUNCATE, TruncVT, N100),
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002580 DAG.getConstant(N101C->getZExtValue(),
2581 TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00002582 }
2583 }
2584 }
Chris Lattner61a4c072007-04-18 03:06:49 +00002585
2586 // fold operands of srl based on knowledge that the low bits are not
2587 // demanded.
Dan Gohman475871a2008-07-27 21:46:04 +00002588 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
2589 return SDValue(N, 0);
Chris Lattner61a4c072007-04-18 03:06:49 +00002590
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002591 return N1C ? visitShiftByConstant(N, N1C->getZExtValue()) : SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002592}
2593
Dan Gohman475871a2008-07-27 21:46:04 +00002594SDValue DAGCombiner::visitCTLZ(SDNode *N) {
2595 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002596 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002597
2598 // fold (ctlz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002599 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002600 return DAG.getNode(ISD::CTLZ, VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00002601 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002602}
2603
Dan Gohman475871a2008-07-27 21:46:04 +00002604SDValue DAGCombiner::visitCTTZ(SDNode *N) {
2605 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002606 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002607
2608 // fold (cttz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002609 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002610 return DAG.getNode(ISD::CTTZ, VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00002611 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002612}
2613
Dan Gohman475871a2008-07-27 21:46:04 +00002614SDValue DAGCombiner::visitCTPOP(SDNode *N) {
2615 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002616 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002617
2618 // fold (ctpop c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002619 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002620 return DAG.getNode(ISD::CTPOP, VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00002621 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002622}
2623
Dan Gohman475871a2008-07-27 21:46:04 +00002624SDValue DAGCombiner::visitSELECT(SDNode *N) {
2625 SDValue N0 = N->getOperand(0);
2626 SDValue N1 = N->getOperand(1);
2627 SDValue N2 = N->getOperand(2);
Nate Begeman452d7be2005-09-16 00:54:12 +00002628 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2629 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2630 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002631 MVT VT = N->getValueType(0);
2632 MVT VT0 = N0.getValueType();
Nate Begeman44728a72005-09-19 22:34:01 +00002633
Nate Begeman452d7be2005-09-16 00:54:12 +00002634 // fold select C, X, X -> X
2635 if (N1 == N2)
2636 return N1;
2637 // fold select true, X, Y -> X
2638 if (N0C && !N0C->isNullValue())
2639 return N1;
2640 // fold select false, X, Y -> Y
2641 if (N0C && N0C->isNullValue())
2642 return N2;
2643 // fold select C, 1, X -> C | X
Duncan Sands83ec4b62008-06-06 12:08:01 +00002644 if (VT == MVT::i1 && N1C && N1C->getAPIntValue() == 1)
Nate Begeman452d7be2005-09-16 00:54:12 +00002645 return DAG.getNode(ISD::OR, VT, N0, N2);
Evan Cheng571c4782007-08-18 05:57:05 +00002646 // fold select C, 0, 1 -> ~C
Duncan Sands83ec4b62008-06-06 12:08:01 +00002647 if (VT.isInteger() && VT0.isInteger() &&
Dan Gohman002e5d02008-03-13 22:13:53 +00002648 N1C && N2C && N1C->isNullValue() && N2C->getAPIntValue() == 1) {
Dan Gohman475871a2008-07-27 21:46:04 +00002649 SDValue XORNode = DAG.getNode(ISD::XOR, VT0, N0, DAG.getConstant(1, VT0));
Evan Cheng571c4782007-08-18 05:57:05 +00002650 if (VT == VT0)
2651 return XORNode;
Gabor Greifba36cb52008-08-28 21:40:38 +00002652 AddToWorkList(XORNode.getNode());
Duncan Sands8e4eb092008-06-08 20:54:56 +00002653 if (VT.bitsGT(VT0))
Evan Cheng571c4782007-08-18 05:57:05 +00002654 return DAG.getNode(ISD::ZERO_EXTEND, VT, XORNode);
2655 return DAG.getNode(ISD::TRUNCATE, VT, XORNode);
2656 }
Nate Begeman452d7be2005-09-16 00:54:12 +00002657 // fold select C, 0, X -> ~C & X
Dale Johannesene0e6fac2007-12-06 17:53:31 +00002658 if (VT == VT0 && VT == MVT::i1 && N1C && N1C->isNullValue()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002659 SDValue XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Gabor Greifba36cb52008-08-28 21:40:38 +00002660 AddToWorkList(XORNode.getNode());
Nate Begeman452d7be2005-09-16 00:54:12 +00002661 return DAG.getNode(ISD::AND, VT, XORNode, N2);
2662 }
2663 // fold select C, X, 1 -> ~C | X
Dan Gohman002e5d02008-03-13 22:13:53 +00002664 if (VT == VT0 && VT == MVT::i1 && N2C && N2C->getAPIntValue() == 1) {
Dan Gohman475871a2008-07-27 21:46:04 +00002665 SDValue XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Gabor Greifba36cb52008-08-28 21:40:38 +00002666 AddToWorkList(XORNode.getNode());
Nate Begeman452d7be2005-09-16 00:54:12 +00002667 return DAG.getNode(ISD::OR, VT, XORNode, N1);
2668 }
2669 // fold select C, X, 0 -> C & X
2670 // FIXME: this should check for C type == X type, not i1?
Duncan Sands83ec4b62008-06-06 12:08:01 +00002671 if (VT == MVT::i1 && N2C && N2C->isNullValue())
Nate Begeman452d7be2005-09-16 00:54:12 +00002672 return DAG.getNode(ISD::AND, VT, N0, N1);
2673 // fold X ? X : Y --> X ? 1 : Y --> X | Y
Duncan Sands83ec4b62008-06-06 12:08:01 +00002674 if (VT == MVT::i1 && N0 == N1)
Nate Begeman452d7be2005-09-16 00:54:12 +00002675 return DAG.getNode(ISD::OR, VT, N0, N2);
2676 // fold X ? Y : X --> X ? Y : 0 --> X & Y
Duncan Sands83ec4b62008-06-06 12:08:01 +00002677 if (VT == MVT::i1 && N0 == N2)
Nate Begeman452d7be2005-09-16 00:54:12 +00002678 return DAG.getNode(ISD::AND, VT, N0, N1);
Chris Lattner729c6d12006-05-27 00:43:02 +00002679
Chris Lattner40c62d52005-10-18 06:04:22 +00002680 // If we can fold this based on the true/false value, do so.
2681 if (SimplifySelectOps(N, N1, N2))
Dan Gohman475871a2008-07-27 21:46:04 +00002682 return SDValue(N, 0); // Don't revisit N.
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002683
Nate Begeman44728a72005-09-19 22:34:01 +00002684 // fold selects based on a setcc into other things, such as min/max/abs
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002685 if (N0.getOpcode() == ISD::SETCC) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00002686 // FIXME:
2687 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
2688 // having to say they don't support SELECT_CC on every type the DAG knows
2689 // about, since there is no way to mark an opcode illegal at all value types
2690 if (TLI.isOperationLegal(ISD::SELECT_CC, MVT::Other))
2691 return DAG.getNode(ISD::SELECT_CC, VT, N0.getOperand(0), N0.getOperand(1),
2692 N1, N2, N0.getOperand(2));
2693 else
2694 return SimplifySelect(N0, N1, N2);
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002695 }
Dan Gohman475871a2008-07-27 21:46:04 +00002696 return SDValue();
Nate Begeman452d7be2005-09-16 00:54:12 +00002697}
2698
Dan Gohman475871a2008-07-27 21:46:04 +00002699SDValue DAGCombiner::visitSELECT_CC(SDNode *N) {
2700 SDValue N0 = N->getOperand(0);
2701 SDValue N1 = N->getOperand(1);
2702 SDValue N2 = N->getOperand(2);
2703 SDValue N3 = N->getOperand(3);
2704 SDValue N4 = N->getOperand(4);
Nate Begeman44728a72005-09-19 22:34:01 +00002705 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
2706
Nate Begeman44728a72005-09-19 22:34:01 +00002707 // fold select_cc lhs, rhs, x, x, cc -> x
2708 if (N2 == N3)
2709 return N2;
Chris Lattner40c62d52005-10-18 06:04:22 +00002710
Chris Lattner5f42a242006-09-20 06:19:26 +00002711 // Determine if the condition we're dealing with is constant
Dan Gohman475871a2008-07-27 21:46:04 +00002712 SDValue SCC = SimplifySetCC(TLI.getSetCCResultType(N0), N0, N1, CC, false);
Gabor Greifba36cb52008-08-28 21:40:38 +00002713 if (SCC.getNode()) AddToWorkList(SCC.getNode());
Chris Lattner5f42a242006-09-20 06:19:26 +00002714
Gabor Greifba36cb52008-08-28 21:40:38 +00002715 if (ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.getNode())) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002716 if (!SCCC->isNullValue())
Chris Lattner5f42a242006-09-20 06:19:26 +00002717 return N2; // cond always true -> true val
2718 else
2719 return N3; // cond always false -> false val
2720 }
2721
2722 // Fold to a simpler select_cc
Gabor Greifba36cb52008-08-28 21:40:38 +00002723 if (SCC.getNode() && SCC.getOpcode() == ISD::SETCC)
Chris Lattner5f42a242006-09-20 06:19:26 +00002724 return DAG.getNode(ISD::SELECT_CC, N2.getValueType(),
2725 SCC.getOperand(0), SCC.getOperand(1), N2, N3,
2726 SCC.getOperand(2));
2727
Chris Lattner40c62d52005-10-18 06:04:22 +00002728 // If we can fold this based on the true/false value, do so.
2729 if (SimplifySelectOps(N, N2, N3))
Dan Gohman475871a2008-07-27 21:46:04 +00002730 return SDValue(N, 0); // Don't revisit N.
Chris Lattner40c62d52005-10-18 06:04:22 +00002731
Nate Begeman44728a72005-09-19 22:34:01 +00002732 // fold select_cc into other things, such as min/max/abs
2733 return SimplifySelectCC(N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00002734}
2735
Dan Gohman475871a2008-07-27 21:46:04 +00002736SDValue DAGCombiner::visitSETCC(SDNode *N) {
Nate Begeman452d7be2005-09-16 00:54:12 +00002737 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
2738 cast<CondCodeSDNode>(N->getOperand(2))->get());
2739}
2740
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002741// ExtendUsesToFormExtLoad - Trying to extend uses of a load to enable this:
2742// "fold ({s|z}ext (load x)) -> ({s|z}ext (truncate ({s|z}extload x)))"
2743// transformation. Returns true if extension are possible and the above
2744// mentioned transformation is profitable.
Dan Gohman475871a2008-07-27 21:46:04 +00002745static bool ExtendUsesToFormExtLoad(SDNode *N, SDValue N0,
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002746 unsigned ExtOpc,
2747 SmallVector<SDNode*, 4> &ExtendNodes,
2748 TargetLowering &TLI) {
2749 bool HasCopyToRegUses = false;
2750 bool isTruncFree = TLI.isTruncateFree(N->getValueType(0), N0.getValueType());
Gabor Greif12632d22008-08-30 19:29:20 +00002751 for (SDNode::use_iterator UI = N0.getNode()->use_begin(),
2752 UE = N0.getNode()->use_end();
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002753 UI != UE; ++UI) {
Dan Gohman89684502008-07-27 20:43:25 +00002754 SDNode *User = *UI;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002755 if (User == N)
2756 continue;
2757 // FIXME: Only extend SETCC N, N and SETCC N, c for now.
2758 if (User->getOpcode() == ISD::SETCC) {
2759 ISD::CondCode CC = cast<CondCodeSDNode>(User->getOperand(2))->get();
2760 if (ExtOpc == ISD::ZERO_EXTEND && ISD::isSignedIntSetCC(CC))
2761 // Sign bits will be lost after a zext.
2762 return false;
2763 bool Add = false;
2764 for (unsigned i = 0; i != 2; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002765 SDValue UseOp = User->getOperand(i);
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002766 if (UseOp == N0)
2767 continue;
2768 if (!isa<ConstantSDNode>(UseOp))
2769 return false;
2770 Add = true;
2771 }
2772 if (Add)
2773 ExtendNodes.push_back(User);
2774 } else {
2775 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002776 SDValue UseOp = User->getOperand(i);
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002777 if (UseOp == N0) {
2778 // If truncate from extended type to original load type is free
2779 // on this target, then it's ok to extend a CopyToReg.
2780 if (isTruncFree && User->getOpcode() == ISD::CopyToReg)
2781 HasCopyToRegUses = true;
2782 else
2783 return false;
2784 }
2785 }
2786 }
2787 }
2788
2789 if (HasCopyToRegUses) {
2790 bool BothLiveOut = false;
2791 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
2792 UI != UE; ++UI) {
Dan Gohman89684502008-07-27 20:43:25 +00002793 SDNode *User = *UI;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002794 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002795 SDValue UseOp = User->getOperand(i);
Gabor Greifba36cb52008-08-28 21:40:38 +00002796 if (UseOp.getNode() == N && UseOp.getResNo() == 0) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002797 BothLiveOut = true;
2798 break;
2799 }
2800 }
2801 }
2802 if (BothLiveOut)
2803 // Both unextended and extended values are live out. There had better be
2804 // good a reason for the transformation.
2805 return ExtendNodes.size();
2806 }
2807 return true;
2808}
2809
Dan Gohman475871a2008-07-27 21:46:04 +00002810SDValue DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
2811 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002812 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002813
Nate Begeman1d4d4142005-09-01 00:19:25 +00002814 // fold (sext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00002815 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002816 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0);
Chris Lattner310b5782006-05-06 23:06:26 +00002817
Nate Begeman1d4d4142005-09-01 00:19:25 +00002818 // fold (sext (sext x)) -> (sext x)
Chris Lattner310b5782006-05-06 23:06:26 +00002819 // fold (sext (aext x)) -> (sext x)
2820 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002821 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
Chris Lattner310b5782006-05-06 23:06:26 +00002822
Chris Lattner22558872007-02-26 03:13:59 +00002823 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman1fdfa6a2008-05-20 20:56:33 +00002824 // fold (sext (truncate (load x))) -> (sext (smaller load x))
2825 // fold (sext (truncate (srl (load x), c))) -> (sext (smaller load (x+c/n)))
Gabor Greifba36cb52008-08-28 21:40:38 +00002826 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
2827 if (NarrowLoad.getNode()) {
2828 if (NarrowLoad.getNode() != N0.getNode())
2829 CombineTo(N0.getNode(), NarrowLoad);
Evan Cheng0b063de2007-03-23 02:16:52 +00002830 return DAG.getNode(ISD::SIGN_EXTEND, VT, NarrowLoad);
2831 }
Evan Chengc88138f2007-03-22 01:54:19 +00002832
Dan Gohman1fdfa6a2008-05-20 20:56:33 +00002833 // See if the value being truncated is already sign extended. If so, just
2834 // eliminate the trunc/sext pair.
Dan Gohman475871a2008-07-27 21:46:04 +00002835 SDValue Op = N0.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002836 unsigned OpBits = Op.getValueType().getSizeInBits();
2837 unsigned MidBits = N0.getValueType().getSizeInBits();
2838 unsigned DestBits = VT.getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002839 unsigned NumSignBits = DAG.ComputeNumSignBits(Op);
Chris Lattner22558872007-02-26 03:13:59 +00002840
2841 if (OpBits == DestBits) {
2842 // Op is i32, Mid is i8, and Dest is i32. If Op has more than 24 sign
2843 // bits, it is already ready.
2844 if (NumSignBits > DestBits-MidBits)
2845 return Op;
2846 } else if (OpBits < DestBits) {
2847 // Op is i32, Mid is i8, and Dest is i64. If Op has more than 24 sign
2848 // bits, just sext from i32.
2849 if (NumSignBits > OpBits-MidBits)
2850 return DAG.getNode(ISD::SIGN_EXTEND, VT, Op);
2851 } else {
2852 // Op is i64, Mid is i8, and Dest is i32. If Op has more than 56 sign
2853 // bits, just truncate to i32.
2854 if (NumSignBits > OpBits-MidBits)
2855 return DAG.getNode(ISD::TRUNCATE, VT, Op);
Chris Lattner6007b842006-09-21 06:00:20 +00002856 }
Chris Lattner22558872007-02-26 03:13:59 +00002857
2858 // fold (sext (truncate x)) -> (sextinreg x).
2859 if (!AfterLegalize || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG,
2860 N0.getValueType())) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00002861 if (Op.getValueType().bitsLT(VT))
Chris Lattner22558872007-02-26 03:13:59 +00002862 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
Duncan Sands8e4eb092008-06-08 20:54:56 +00002863 else if (Op.getValueType().bitsGT(VT))
Chris Lattner22558872007-02-26 03:13:59 +00002864 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
2865 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, Op,
2866 DAG.getValueType(N0.getValueType()));
2867 }
Chris Lattner6007b842006-09-21 06:00:20 +00002868 }
Chris Lattner310b5782006-05-06 23:06:26 +00002869
Evan Cheng110dec22005-12-14 02:19:23 +00002870 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00002871 if (ISD::isNON_EXTLoad(N0.getNode()) &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002872 ((!AfterLegalize && !cast<LoadSDNode>(N0)->isVolatile()) ||
2873 TLI.isLoadXLegal(ISD::SEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002874 bool DoXform = true;
2875 SmallVector<SDNode*, 4> SetCCs;
2876 if (!N0.hasOneUse())
2877 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::SIGN_EXTEND, SetCCs, TLI);
2878 if (DoXform) {
2879 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman475871a2008-07-27 21:46:04 +00002880 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002881 LN0->getBasePtr(), LN0->getSrcValue(),
2882 LN0->getSrcValueOffset(),
2883 N0.getValueType(),
2884 LN0->isVolatile(),
2885 LN0->getAlignment());
2886 CombineTo(N, ExtLoad);
Dan Gohman475871a2008-07-27 21:46:04 +00002887 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00002888 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002889 // Extend SetCC uses if necessary.
2890 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
2891 SDNode *SetCC = SetCCs[i];
Dan Gohman475871a2008-07-27 21:46:04 +00002892 SmallVector<SDValue, 4> Ops;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002893 for (unsigned j = 0; j != 2; ++j) {
Dan Gohman475871a2008-07-27 21:46:04 +00002894 SDValue SOp = SetCC->getOperand(j);
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002895 if (SOp == Trunc)
2896 Ops.push_back(ExtLoad);
2897 else
2898 Ops.push_back(DAG.getNode(ISD::SIGN_EXTEND, VT, SOp));
2899 }
2900 Ops.push_back(SetCC->getOperand(2));
2901 CombineTo(SetCC, DAG.getNode(ISD::SETCC, SetCC->getValueType(0),
2902 &Ops[0], Ops.size()));
2903 }
Dan Gohman475871a2008-07-27 21:46:04 +00002904 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002905 }
Nate Begeman3df4d522005-10-12 20:40:40 +00002906 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002907
2908 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
2909 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00002910 if ((ISD::isSEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) &&
2911 ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002912 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002913 MVT EVT = LN0->getMemoryVT();
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002914 if ((!AfterLegalize && !LN0->isVolatile()) ||
2915 TLI.isLoadXLegal(ISD::SEXTLOAD, EVT)) {
Dan Gohman475871a2008-07-27 21:46:04 +00002916 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00002917 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002918 LN0->getSrcValueOffset(), EVT,
2919 LN0->isVolatile(),
2920 LN0->getAlignment());
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00002921 CombineTo(N, ExtLoad);
Gabor Greif12632d22008-08-30 19:29:20 +00002922 CombineTo(N0.getNode(),
2923 DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00002924 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00002925 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00002926 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002927 }
2928
Chris Lattner20a35c32007-04-11 05:32:27 +00002929 // sext(setcc x,y,cc) -> select_cc x, y, -1, 0, cc
2930 if (N0.getOpcode() == ISD::SETCC) {
Dan Gohman475871a2008-07-27 21:46:04 +00002931 SDValue SCC =
Chris Lattner1eba01e2007-04-11 06:50:51 +00002932 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
2933 DAG.getConstant(~0ULL, VT), DAG.getConstant(0, VT),
2934 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00002935 if (SCC.getNode()) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00002936 }
2937
Dan Gohman8f0ad582008-04-28 16:58:24 +00002938 // fold (sext x) -> (zext x) if the sign bit is known zero.
Dan Gohman187db7b2008-04-28 18:47:17 +00002939 if ((!AfterLegalize || TLI.isOperationLegal(ISD::ZERO_EXTEND, VT)) &&
2940 DAG.SignBitIsZero(N0))
Dan Gohman8f0ad582008-04-28 16:58:24 +00002941 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
2942
Dan Gohman475871a2008-07-27 21:46:04 +00002943 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002944}
2945
Dan Gohman475871a2008-07-27 21:46:04 +00002946SDValue DAGCombiner::visitZERO_EXTEND(SDNode *N) {
2947 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002948 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002949
Nate Begeman1d4d4142005-09-01 00:19:25 +00002950 // fold (zext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00002951 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002952 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002953 // fold (zext (zext x)) -> (zext x)
Chris Lattner310b5782006-05-06 23:06:26 +00002954 // fold (zext (aext x)) -> (zext x)
2955 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002956 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
Chris Lattner6007b842006-09-21 06:00:20 +00002957
Evan Chengc88138f2007-03-22 01:54:19 +00002958 // fold (zext (truncate (load x))) -> (zext (smaller load x))
2959 // fold (zext (truncate (srl (load x), c))) -> (zext (small load (x+c/n)))
Dale Johannesen2041a0e2007-03-30 21:38:07 +00002960 if (N0.getOpcode() == ISD::TRUNCATE) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002961 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
2962 if (NarrowLoad.getNode()) {
2963 if (NarrowLoad.getNode() != N0.getNode())
2964 CombineTo(N0.getNode(), NarrowLoad);
Evan Cheng0b063de2007-03-23 02:16:52 +00002965 return DAG.getNode(ISD::ZERO_EXTEND, VT, NarrowLoad);
2966 }
Evan Chengc88138f2007-03-22 01:54:19 +00002967 }
2968
Chris Lattner6007b842006-09-21 06:00:20 +00002969 // fold (zext (truncate x)) -> (and x, mask)
2970 if (N0.getOpcode() == ISD::TRUNCATE &&
2971 (!AfterLegalize || TLI.isOperationLegal(ISD::AND, VT))) {
Dan Gohman475871a2008-07-27 21:46:04 +00002972 SDValue Op = N0.getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00002973 if (Op.getValueType().bitsLT(VT)) {
Chris Lattner6007b842006-09-21 06:00:20 +00002974 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
Duncan Sands8e4eb092008-06-08 20:54:56 +00002975 } else if (Op.getValueType().bitsGT(VT)) {
Chris Lattner6007b842006-09-21 06:00:20 +00002976 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
2977 }
2978 return DAG.getZeroExtendInReg(Op, N0.getValueType());
2979 }
2980
Chris Lattner111c2282006-09-21 06:14:31 +00002981 // fold (zext (and (trunc x), cst)) -> (and x, cst).
2982 if (N0.getOpcode() == ISD::AND &&
2983 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
2984 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohman475871a2008-07-27 21:46:04 +00002985 SDValue X = N0.getOperand(0).getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00002986 if (X.getValueType().bitsLT(VT)) {
Chris Lattner111c2282006-09-21 06:14:31 +00002987 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
Duncan Sands8e4eb092008-06-08 20:54:56 +00002988 } else if (X.getValueType().bitsGT(VT)) {
Chris Lattner111c2282006-09-21 06:14:31 +00002989 X = DAG.getNode(ISD::TRUNCATE, VT, X);
2990 }
Dan Gohman220a8232008-03-03 23:51:38 +00002991 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002992 Mask.zext(VT.getSizeInBits());
Chris Lattner111c2282006-09-21 06:14:31 +00002993 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
2994 }
2995
Evan Cheng110dec22005-12-14 02:19:23 +00002996 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00002997 if (ISD::isNON_EXTLoad(N0.getNode()) &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002998 ((!AfterLegalize && !cast<LoadSDNode>(N0)->isVolatile()) ||
2999 TLI.isLoadXLegal(ISD::ZEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003000 bool DoXform = true;
3001 SmallVector<SDNode*, 4> SetCCs;
3002 if (!N0.hasOneUse())
3003 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ZERO_EXTEND, SetCCs, TLI);
3004 if (DoXform) {
3005 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman475871a2008-07-27 21:46:04 +00003006 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003007 LN0->getBasePtr(), LN0->getSrcValue(),
3008 LN0->getSrcValueOffset(),
3009 N0.getValueType(),
3010 LN0->isVolatile(),
3011 LN0->getAlignment());
3012 CombineTo(N, ExtLoad);
Dan Gohman475871a2008-07-27 21:46:04 +00003013 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00003014 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003015 // Extend SetCC uses if necessary.
3016 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
3017 SDNode *SetCC = SetCCs[i];
Dan Gohman475871a2008-07-27 21:46:04 +00003018 SmallVector<SDValue, 4> Ops;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003019 for (unsigned j = 0; j != 2; ++j) {
Dan Gohman475871a2008-07-27 21:46:04 +00003020 SDValue SOp = SetCC->getOperand(j);
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003021 if (SOp == Trunc)
3022 Ops.push_back(ExtLoad);
3023 else
Evan Chengde1631b2007-10-30 20:11:21 +00003024 Ops.push_back(DAG.getNode(ISD::ZERO_EXTEND, VT, SOp));
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003025 }
3026 Ops.push_back(SetCC->getOperand(2));
3027 CombineTo(SetCC, DAG.getNode(ISD::SETCC, SetCC->getValueType(0),
3028 &Ops[0], Ops.size()));
3029 }
Dan Gohman475871a2008-07-27 21:46:04 +00003030 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003031 }
Evan Cheng110dec22005-12-14 02:19:23 +00003032 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00003033
3034 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
3035 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00003036 if ((ISD::isZEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) &&
3037 ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00003038 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003039 MVT EVT = LN0->getMemoryVT();
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003040 if ((!AfterLegalize && !LN0->isVolatile()) ||
3041 TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT)) {
Dan Gohman475871a2008-07-27 21:46:04 +00003042 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003043 LN0->getBasePtr(), LN0->getSrcValue(),
3044 LN0->getSrcValueOffset(), EVT,
3045 LN0->isVolatile(),
3046 LN0->getAlignment());
3047 CombineTo(N, ExtLoad);
Gabor Greif12632d22008-08-30 19:29:20 +00003048 CombineTo(N0.getNode(),
3049 DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003050 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003051 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003052 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00003053 }
Chris Lattner20a35c32007-04-11 05:32:27 +00003054
3055 // zext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
3056 if (N0.getOpcode() == ISD::SETCC) {
Dan Gohman475871a2008-07-27 21:46:04 +00003057 SDValue SCC =
Chris Lattner20a35c32007-04-11 05:32:27 +00003058 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
3059 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattner1eba01e2007-04-11 06:50:51 +00003060 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00003061 if (SCC.getNode()) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00003062 }
3063
Dan Gohman475871a2008-07-27 21:46:04 +00003064 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003065}
3066
Dan Gohman475871a2008-07-27 21:46:04 +00003067SDValue DAGCombiner::visitANY_EXTEND(SDNode *N) {
3068 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003069 MVT VT = N->getValueType(0);
Chris Lattner5ffc0662006-05-05 05:58:59 +00003070
3071 // fold (aext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00003072 if (isa<ConstantSDNode>(N0))
Chris Lattner5ffc0662006-05-05 05:58:59 +00003073 return DAG.getNode(ISD::ANY_EXTEND, VT, N0);
3074 // fold (aext (aext x)) -> (aext x)
3075 // fold (aext (zext x)) -> (zext x)
3076 // fold (aext (sext x)) -> (sext x)
3077 if (N0.getOpcode() == ISD::ANY_EXTEND ||
3078 N0.getOpcode() == ISD::ZERO_EXTEND ||
3079 N0.getOpcode() == ISD::SIGN_EXTEND)
3080 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
3081
Evan Chengc88138f2007-03-22 01:54:19 +00003082 // fold (aext (truncate (load x))) -> (aext (smaller load x))
3083 // fold (aext (truncate (srl (load x), c))) -> (aext (small load (x+c/n)))
3084 if (N0.getOpcode() == ISD::TRUNCATE) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003085 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
3086 if (NarrowLoad.getNode()) {
3087 if (NarrowLoad.getNode() != N0.getNode())
3088 CombineTo(N0.getNode(), NarrowLoad);
Evan Cheng0b063de2007-03-23 02:16:52 +00003089 return DAG.getNode(ISD::ANY_EXTEND, VT, NarrowLoad);
3090 }
Evan Chengc88138f2007-03-22 01:54:19 +00003091 }
3092
Chris Lattner84750582006-09-20 06:29:17 +00003093 // fold (aext (truncate x))
3094 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman475871a2008-07-27 21:46:04 +00003095 SDValue TruncOp = N0.getOperand(0);
Chris Lattner84750582006-09-20 06:29:17 +00003096 if (TruncOp.getValueType() == VT)
3097 return TruncOp; // x iff x size == zext size.
Duncan Sands8e4eb092008-06-08 20:54:56 +00003098 if (TruncOp.getValueType().bitsGT(VT))
Chris Lattner84750582006-09-20 06:29:17 +00003099 return DAG.getNode(ISD::TRUNCATE, VT, TruncOp);
3100 return DAG.getNode(ISD::ANY_EXTEND, VT, TruncOp);
3101 }
Chris Lattner0e4b9222006-09-21 06:40:43 +00003102
3103 // fold (aext (and (trunc x), cst)) -> (and x, cst).
3104 if (N0.getOpcode() == ISD::AND &&
3105 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
3106 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohman475871a2008-07-27 21:46:04 +00003107 SDValue X = N0.getOperand(0).getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003108 if (X.getValueType().bitsLT(VT)) {
Chris Lattner0e4b9222006-09-21 06:40:43 +00003109 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003110 } else if (X.getValueType().bitsGT(VT)) {
Chris Lattner0e4b9222006-09-21 06:40:43 +00003111 X = DAG.getNode(ISD::TRUNCATE, VT, X);
3112 }
Dan Gohman220a8232008-03-03 23:51:38 +00003113 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003114 Mask.zext(VT.getSizeInBits());
Chris Lattner0e4b9222006-09-21 06:40:43 +00003115 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
3116 }
3117
Chris Lattner5ffc0662006-05-05 05:58:59 +00003118 // fold (aext (load x)) -> (aext (truncate (extload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00003119 if (ISD::isNON_EXTLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003120 ((!AfterLegalize && !cast<LoadSDNode>(N0)->isVolatile()) ||
3121 TLI.isLoadXLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003122 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman475871a2008-07-27 21:46:04 +00003123 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
Evan Cheng466685d2006-10-09 20:57:25 +00003124 LN0->getBasePtr(), LN0->getSrcValue(),
3125 LN0->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003126 N0.getValueType(),
3127 LN0->isVolatile(),
3128 LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00003129 CombineTo(N, ExtLoad);
Dan Gohman75dcf082008-07-31 00:50:31 +00003130 // Redirect any chain users to the new load.
Evan Cheng45299662008-08-29 23:20:46 +00003131 DAG.ReplaceAllUsesOfValueWith(SDValue(LN0, 1),
3132 SDValue(ExtLoad.getNode(), 1));
Dan Gohman75dcf082008-07-31 00:50:31 +00003133 // If any node needs the original loaded value, recompute it.
3134 if (!LN0->use_empty())
3135 CombineTo(LN0, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
3136 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003137 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner5ffc0662006-05-05 05:58:59 +00003138 }
3139
3140 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
3141 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
3142 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00003143 if (N0.getOpcode() == ISD::LOAD &&
Gabor Greifba36cb52008-08-28 21:40:38 +00003144 !ISD::isNON_EXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng466685d2006-10-09 20:57:25 +00003145 N0.hasOneUse()) {
3146 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003147 MVT EVT = LN0->getMemoryVT();
Dan Gohman475871a2008-07-27 21:46:04 +00003148 SDValue ExtLoad = DAG.getExtLoad(LN0->getExtensionType(), VT,
Evan Cheng466685d2006-10-09 20:57:25 +00003149 LN0->getChain(), LN0->getBasePtr(),
3150 LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003151 LN0->getSrcValueOffset(), EVT,
3152 LN0->isVolatile(),
3153 LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00003154 CombineTo(N, ExtLoad);
Evan Cheng45299662008-08-29 23:20:46 +00003155 CombineTo(N0.getNode(),
3156 DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
Chris Lattner5ffc0662006-05-05 05:58:59 +00003157 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003158 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner5ffc0662006-05-05 05:58:59 +00003159 }
Chris Lattner20a35c32007-04-11 05:32:27 +00003160
3161 // aext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
3162 if (N0.getOpcode() == ISD::SETCC) {
Dan Gohman475871a2008-07-27 21:46:04 +00003163 SDValue SCC =
Chris Lattner1eba01e2007-04-11 06:50:51 +00003164 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
3165 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattnerc24bbad2007-04-11 16:51:53 +00003166 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00003167 if (SCC.getNode())
Chris Lattnerc56a81d2007-04-11 06:43:25 +00003168 return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00003169 }
3170
Dan Gohman475871a2008-07-27 21:46:04 +00003171 return SDValue();
Chris Lattner5ffc0662006-05-05 05:58:59 +00003172}
3173
Chris Lattner2b4c2792007-10-13 06:35:54 +00003174/// GetDemandedBits - See if the specified operand can be simplified with the
3175/// knowledge that only the bits specified by Mask are used. If so, return the
Dan Gohman475871a2008-07-27 21:46:04 +00003176/// simpler operand, otherwise return a null SDValue.
3177SDValue DAGCombiner::GetDemandedBits(SDValue V, const APInt &Mask) {
Chris Lattner2b4c2792007-10-13 06:35:54 +00003178 switch (V.getOpcode()) {
3179 default: break;
3180 case ISD::OR:
3181 case ISD::XOR:
3182 // If the LHS or RHS don't contribute bits to the or, drop them.
3183 if (DAG.MaskedValueIsZero(V.getOperand(0), Mask))
3184 return V.getOperand(1);
3185 if (DAG.MaskedValueIsZero(V.getOperand(1), Mask))
3186 return V.getOperand(0);
3187 break;
Chris Lattnere33544c2007-10-13 06:58:48 +00003188 case ISD::SRL:
3189 // Only look at single-use SRLs.
Gabor Greifba36cb52008-08-28 21:40:38 +00003190 if (!V.getNode()->hasOneUse())
Chris Lattnere33544c2007-10-13 06:58:48 +00003191 break;
3192 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
3193 // See if we can recursively simplify the LHS.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003194 unsigned Amt = RHSC->getZExtValue();
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003195 APInt NewMask = Mask << Amt;
Dan Gohman475871a2008-07-27 21:46:04 +00003196 SDValue SimplifyLHS = GetDemandedBits(V.getOperand(0), NewMask);
Gabor Greifba36cb52008-08-28 21:40:38 +00003197 if (SimplifyLHS.getNode()) {
Chris Lattnere33544c2007-10-13 06:58:48 +00003198 return DAG.getNode(ISD::SRL, V.getValueType(),
3199 SimplifyLHS, V.getOperand(1));
3200 }
3201 }
Chris Lattner2b4c2792007-10-13 06:35:54 +00003202 }
Dan Gohman475871a2008-07-27 21:46:04 +00003203 return SDValue();
Chris Lattner2b4c2792007-10-13 06:35:54 +00003204}
3205
Evan Chengc88138f2007-03-22 01:54:19 +00003206/// ReduceLoadWidth - If the result of a wider load is shifted to right of N
3207/// bits and then truncated to a narrower type and where N is a multiple
3208/// of number of bits of the narrower type, transform it to a narrower load
3209/// from address + N / num of bits of new type. If the result is to be
3210/// extended, also fold the extension to form a extending load.
Dan Gohman475871a2008-07-27 21:46:04 +00003211SDValue DAGCombiner::ReduceLoadWidth(SDNode *N) {
Evan Chengc88138f2007-03-22 01:54:19 +00003212 unsigned Opc = N->getOpcode();
3213 ISD::LoadExtType ExtType = ISD::NON_EXTLOAD;
Dan Gohman475871a2008-07-27 21:46:04 +00003214 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003215 MVT VT = N->getValueType(0);
3216 MVT EVT = N->getValueType(0);
Evan Chengc88138f2007-03-22 01:54:19 +00003217
Dan Gohman7f8613e2008-08-14 20:04:46 +00003218 // This transformation isn't valid for vector loads.
3219 if (VT.isVector())
3220 return SDValue();
3221
Evan Chenge177e302007-03-23 22:13:36 +00003222 // Special case: SIGN_EXTEND_INREG is basically truncating to EVT then
3223 // extended to VT.
Evan Chengc88138f2007-03-22 01:54:19 +00003224 if (Opc == ISD::SIGN_EXTEND_INREG) {
3225 ExtType = ISD::SEXTLOAD;
3226 EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Evan Chenge177e302007-03-23 22:13:36 +00003227 if (AfterLegalize && !TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))
Dan Gohman475871a2008-07-27 21:46:04 +00003228 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00003229 }
3230
Duncan Sands83ec4b62008-06-06 12:08:01 +00003231 unsigned EVTBits = EVT.getSizeInBits();
Evan Chengc88138f2007-03-22 01:54:19 +00003232 unsigned ShAmt = 0;
Evan Chengb37b80c2007-03-23 20:55:21 +00003233 bool CombineSRL = false;
Evan Chengc88138f2007-03-22 01:54:19 +00003234 if (N0.getOpcode() == ISD::SRL && N0.hasOneUse()) {
3235 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003236 ShAmt = N01->getZExtValue();
Evan Chengc88138f2007-03-22 01:54:19 +00003237 // Is the shift amount a multiple of size of VT?
3238 if ((ShAmt & (EVTBits-1)) == 0) {
3239 N0 = N0.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003240 if (N0.getValueType().getSizeInBits() <= EVTBits)
Dan Gohman475871a2008-07-27 21:46:04 +00003241 return SDValue();
Evan Chengb37b80c2007-03-23 20:55:21 +00003242 CombineSRL = true;
Evan Chengc88138f2007-03-22 01:54:19 +00003243 }
3244 }
3245 }
3246
Duncan Sandsad205a72008-06-16 08:14:38 +00003247 // Do not generate loads of non-round integer types since these can
3248 // be expensive (and would be wrong if the type is not byte sized).
Dan Gohman75dcf082008-07-31 00:50:31 +00003249 if (isa<LoadSDNode>(N0) && N0.hasOneUse() && VT.isRound() &&
3250 cast<LoadSDNode>(N0)->getMemoryVT().getSizeInBits() > EVTBits &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003251 // Do not change the width of a volatile load.
3252 !cast<LoadSDNode>(N0)->isVolatile()) {
Evan Chengc88138f2007-03-22 01:54:19 +00003253 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003254 MVT PtrType = N0.getOperand(1).getValueType();
Evan Chengdae54ce2007-03-24 00:02:43 +00003255 // For big endian targets, we need to adjust the offset to the pointer to
3256 // load the correct bytes.
Duncan Sands0753fc12008-02-11 10:37:04 +00003257 if (TLI.isBigEndian()) {
Dan Gohman75dcf082008-07-31 00:50:31 +00003258 unsigned LVTStoreBits = LN0->getMemoryVT().getStoreSizeInBits();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003259 unsigned EVTStoreBits = EVT.getStoreSizeInBits();
Duncan Sandsc6fa1702007-11-09 08:57:19 +00003260 ShAmt = LVTStoreBits - EVTStoreBits - ShAmt;
3261 }
Evan Chengdae54ce2007-03-24 00:02:43 +00003262 uint64_t PtrOff = ShAmt / 8;
Duncan Sandsdc846502007-10-28 12:59:45 +00003263 unsigned NewAlign = MinAlign(LN0->getAlignment(), PtrOff);
Dan Gohman475871a2008-07-27 21:46:04 +00003264 SDValue NewPtr = DAG.getNode(ISD::ADD, PtrType, LN0->getBasePtr(),
Evan Chengc88138f2007-03-22 01:54:19 +00003265 DAG.getConstant(PtrOff, PtrType));
Gabor Greifba36cb52008-08-28 21:40:38 +00003266 AddToWorkList(NewPtr.getNode());
Dan Gohman475871a2008-07-27 21:46:04 +00003267 SDValue Load = (ExtType == ISD::NON_EXTLOAD)
Evan Chengc88138f2007-03-22 01:54:19 +00003268 ? DAG.getLoad(VT, LN0->getChain(), NewPtr,
Dan Gohman75dcf082008-07-31 00:50:31 +00003269 LN0->getSrcValue(), LN0->getSrcValueOffset() + PtrOff,
Duncan Sandsdc846502007-10-28 12:59:45 +00003270 LN0->isVolatile(), NewAlign)
Evan Chengc88138f2007-03-22 01:54:19 +00003271 : DAG.getExtLoad(ExtType, VT, LN0->getChain(), NewPtr,
Dan Gohman75dcf082008-07-31 00:50:31 +00003272 LN0->getSrcValue(), LN0->getSrcValueOffset() + PtrOff,
3273 EVT, LN0->isVolatile(), NewAlign);
Evan Chengc88138f2007-03-22 01:54:19 +00003274 AddToWorkList(N);
Evan Chengb37b80c2007-03-23 20:55:21 +00003275 if (CombineSRL) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003276 WorkListRemover DeadNodes(*this);
3277 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), Load.getValue(1),
3278 &DeadNodes);
Gabor Greifba36cb52008-08-28 21:40:38 +00003279 CombineTo(N->getOperand(0).getNode(), Load);
Evan Chengb37b80c2007-03-23 20:55:21 +00003280 } else
Gabor Greifba36cb52008-08-28 21:40:38 +00003281 CombineTo(N0.getNode(), Load, Load.getValue(1));
Evan Cheng15213b72007-03-26 07:12:51 +00003282 if (ShAmt) {
3283 if (Opc == ISD::SIGN_EXTEND_INREG)
3284 return DAG.getNode(Opc, VT, Load, N->getOperand(1));
3285 else
3286 return DAG.getNode(Opc, VT, Load);
3287 }
Dan Gohman475871a2008-07-27 21:46:04 +00003288 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Chengc88138f2007-03-22 01:54:19 +00003289 }
3290
Dan Gohman475871a2008-07-27 21:46:04 +00003291 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00003292}
3293
Chris Lattner5ffc0662006-05-05 05:58:59 +00003294
Dan Gohman475871a2008-07-27 21:46:04 +00003295SDValue DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
3296 SDValue N0 = N->getOperand(0);
3297 SDValue N1 = N->getOperand(1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003298 MVT VT = N->getValueType(0);
3299 MVT EVT = cast<VTSDNode>(N1)->getVT();
3300 unsigned VTBits = VT.getSizeInBits();
3301 unsigned EVTBits = EVT.getSizeInBits();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003302
Nate Begeman1d4d4142005-09-01 00:19:25 +00003303 // fold (sext_in_reg c1) -> c1
Chris Lattnereaeda562006-05-08 20:59:41 +00003304 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
Chris Lattner310b5782006-05-06 23:06:26 +00003305 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0, N1);
Chris Lattneree4ea922006-05-06 09:30:03 +00003306
Chris Lattner541a24f2006-05-06 22:43:44 +00003307 // If the input is already sign extended, just drop the extension.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003308 if (DAG.ComputeNumSignBits(N0) >= VT.getSizeInBits()-EVTBits+1)
Chris Lattneree4ea922006-05-06 09:30:03 +00003309 return N0;
3310
Nate Begeman646d7e22005-09-02 21:18:40 +00003311 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
3312 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Duncan Sands8e4eb092008-06-08 20:54:56 +00003313 EVT.bitsLT(cast<VTSDNode>(N0.getOperand(1))->getVT())) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00003314 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00003315 }
Chris Lattner4b37e872006-05-08 21:18:59 +00003316
Dan Gohman75dcf082008-07-31 00:50:31 +00003317 // fold (sext_in_reg (sext x)) -> (sext x)
3318 // fold (sext_in_reg (aext x)) -> (sext x)
3319 // if x is small enough.
3320 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND) {
3321 SDValue N00 = N0.getOperand(0);
3322 if (N00.getValueType().getSizeInBits() < EVTBits)
3323 return DAG.getNode(ISD::SIGN_EXTEND, VT, N00, N1);
3324 }
3325
Chris Lattner95a5e052007-04-17 19:03:21 +00003326 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is known zero.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003327 if (DAG.MaskedValueIsZero(N0, APInt::getBitsSet(VTBits, EVTBits-1, EVTBits)))
Nate Begemande996292006-02-03 22:24:05 +00003328 return DAG.getZeroExtendInReg(N0, EVT);
Chris Lattner4b37e872006-05-08 21:18:59 +00003329
Chris Lattner95a5e052007-04-17 19:03:21 +00003330 // fold operands of sext_in_reg based on knowledge that the top bits are not
3331 // demanded.
Dan Gohman475871a2008-07-27 21:46:04 +00003332 if (SimplifyDemandedBits(SDValue(N, 0)))
3333 return SDValue(N, 0);
Chris Lattner95a5e052007-04-17 19:03:21 +00003334
Evan Chengc88138f2007-03-22 01:54:19 +00003335 // fold (sext_in_reg (load x)) -> (smaller sextload x)
3336 // fold (sext_in_reg (srl (load x), c)) -> (smaller sextload (x+c/evtbits))
Dan Gohman475871a2008-07-27 21:46:04 +00003337 SDValue NarrowLoad = ReduceLoadWidth(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003338 if (NarrowLoad.getNode())
Evan Chengc88138f2007-03-22 01:54:19 +00003339 return NarrowLoad;
3340
Chris Lattner4b37e872006-05-08 21:18:59 +00003341 // fold (sext_in_reg (srl X, 24), i8) -> sra X, 24
3342 // fold (sext_in_reg (srl X, 23), i8) -> sra X, 23 iff possible.
3343 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
3344 if (N0.getOpcode() == ISD::SRL) {
3345 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003346 if (ShAmt->getZExtValue()+EVTBits <= VT.getSizeInBits()) {
Chris Lattner4b37e872006-05-08 21:18:59 +00003347 // We can turn this into an SRA iff the input to the SRL is already sign
3348 // extended enough.
Dan Gohmanea859be2007-06-22 14:59:07 +00003349 unsigned InSignBits = DAG.ComputeNumSignBits(N0.getOperand(0));
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003350 if (VT.getSizeInBits()-(ShAmt->getZExtValue()+EVTBits) < InSignBits)
Chris Lattner4b37e872006-05-08 21:18:59 +00003351 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0), N0.getOperand(1));
3352 }
3353 }
Evan Chengc88138f2007-03-22 01:54:19 +00003354
Nate Begemanded49632005-10-13 03:11:28 +00003355 // fold (sext_inreg (extload x)) -> (sextload x)
Gabor Greifba36cb52008-08-28 21:40:38 +00003356 if (ISD::isEXTLoad(N0.getNode()) &&
3357 ISD::isUNINDEXEDLoad(N0.getNode()) &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003358 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003359 ((!AfterLegalize && !cast<LoadSDNode>(N0)->isVolatile()) ||
3360 TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003361 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman475871a2008-07-27 21:46:04 +00003362 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
Evan Cheng466685d2006-10-09 20:57:25 +00003363 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003364 LN0->getSrcValueOffset(), EVT,
3365 LN0->isVolatile(),
3366 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00003367 CombineTo(N, ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00003368 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003369 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00003370 }
3371 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Gabor Greifba36cb52008-08-28 21:40:38 +00003372 if (ISD::isZEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng83060c52007-03-07 08:07:03 +00003373 N0.hasOneUse() &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003374 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003375 ((!AfterLegalize && !cast<LoadSDNode>(N0)->isVolatile()) ||
3376 TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003377 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman475871a2008-07-27 21:46:04 +00003378 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
Evan Cheng466685d2006-10-09 20:57:25 +00003379 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003380 LN0->getSrcValueOffset(), EVT,
3381 LN0->isVolatile(),
3382 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00003383 CombineTo(N, ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00003384 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003385 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00003386 }
Dan Gohman475871a2008-07-27 21:46:04 +00003387 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003388}
3389
Dan Gohman475871a2008-07-27 21:46:04 +00003390SDValue DAGCombiner::visitTRUNCATE(SDNode *N) {
3391 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003392 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003393
3394 // noop truncate
3395 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00003396 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003397 // fold (truncate c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00003398 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00003399 return DAG.getNode(ISD::TRUNCATE, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003400 // fold (truncate (truncate x)) -> (truncate x)
3401 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00003402 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003403 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
Chris Lattnerb72773b2006-05-05 22:56:26 +00003404 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND||
3405 N0.getOpcode() == ISD::ANY_EXTEND) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00003406 if (N0.getOperand(0).getValueType().bitsLT(VT))
Nate Begeman1d4d4142005-09-01 00:19:25 +00003407 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00003408 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Duncan Sands8e4eb092008-06-08 20:54:56 +00003409 else if (N0.getOperand(0).getValueType().bitsGT(VT))
Nate Begeman1d4d4142005-09-01 00:19:25 +00003410 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00003411 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003412 else
3413 // if the source and dest are the same type, we can drop both the extend
3414 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00003415 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003416 }
Evan Cheng007b69e2007-03-21 20:14:05 +00003417
Chris Lattner2b4c2792007-10-13 06:35:54 +00003418 // See if we can simplify the input to this truncate through knowledge that
3419 // only the low bits are being used. For example "trunc (or (shl x, 8), y)"
3420 // -> trunc y
Dan Gohman475871a2008-07-27 21:46:04 +00003421 SDValue Shorter =
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003422 GetDemandedBits(N0, APInt::getLowBitsSet(N0.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +00003423 VT.getSizeInBits()));
Gabor Greifba36cb52008-08-28 21:40:38 +00003424 if (Shorter.getNode())
Chris Lattner2b4c2792007-10-13 06:35:54 +00003425 return DAG.getNode(ISD::TRUNCATE, VT, Shorter);
3426
Nate Begeman3df4d522005-10-12 20:40:40 +00003427 // fold (truncate (load x)) -> (smaller load x)
Evan Cheng007b69e2007-03-21 20:14:05 +00003428 // fold (truncate (srl (load x), c)) -> (smaller load (x+c/evtbits))
Evan Chengc88138f2007-03-22 01:54:19 +00003429 return ReduceLoadWidth(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003430}
3431
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003432static SDNode *getBuildPairElt(SDNode *N, unsigned i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003433 SDValue Elt = N->getOperand(i);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003434 if (Elt.getOpcode() != ISD::MERGE_VALUES)
Gabor Greifba36cb52008-08-28 21:40:38 +00003435 return Elt.getNode();
3436 return Elt.getOperand(Elt.getResNo()).getNode();
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003437}
3438
3439/// CombineConsecutiveLoads - build_pair (load, load) -> load
3440/// if load locations are consecutive.
Dan Gohman475871a2008-07-27 21:46:04 +00003441SDValue DAGCombiner::CombineConsecutiveLoads(SDNode *N, MVT VT) {
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003442 assert(N->getOpcode() == ISD::BUILD_PAIR);
3443
3444 SDNode *LD1 = getBuildPairElt(N, 0);
3445 if (!ISD::isNON_EXTLoad(LD1) || !LD1->hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00003446 return SDValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003447 MVT LD1VT = LD1->getValueType(0);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003448 SDNode *LD2 = getBuildPairElt(N, 1);
3449 const MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3450 if (ISD::isNON_EXTLoad(LD2) &&
3451 LD2->hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003452 // If both are volatile this would reduce the number of volatile loads.
3453 // If one is volatile it might be ok, but play conservative and bail out.
3454 !cast<LoadSDNode>(LD1)->isVolatile() &&
3455 !cast<LoadSDNode>(LD2)->isVolatile() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003456 TLI.isConsecutiveLoad(LD2, LD1, LD1VT.getSizeInBits()/8, 1, MFI)) {
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003457 LoadSDNode *LD = cast<LoadSDNode>(LD1);
3458 unsigned Align = LD->getAlignment();
Dan Gohman6448d912008-09-04 15:39:15 +00003459 unsigned NewAlign = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00003460 getABITypeAlignment(VT.getTypeForMVT());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003461 if (NewAlign <= Align &&
3462 (!AfterLegalize || TLI.isOperationLegal(ISD::LOAD, VT)))
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003463 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(),
3464 LD->getSrcValue(), LD->getSrcValueOffset(),
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003465 false, Align);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003466 }
Dan Gohman475871a2008-07-27 21:46:04 +00003467 return SDValue();
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003468}
3469
Dan Gohman475871a2008-07-27 21:46:04 +00003470SDValue DAGCombiner::visitBIT_CONVERT(SDNode *N) {
3471 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003472 MVT VT = N->getValueType(0);
Chris Lattner94683772005-12-23 05:30:37 +00003473
Dan Gohman7f321562007-06-25 16:23:39 +00003474 // If the input is a BUILD_VECTOR with all constant elements, fold this now.
3475 // Only do this before legalize, since afterward the target may be depending
3476 // on the bitconvert.
3477 // First check to see if this is all constant.
3478 if (!AfterLegalize &&
Gabor Greifba36cb52008-08-28 21:40:38 +00003479 N0.getOpcode() == ISD::BUILD_VECTOR && N0.getNode()->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003480 VT.isVector()) {
Dan Gohman7f321562007-06-25 16:23:39 +00003481 bool isSimple = true;
3482 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i)
3483 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
3484 N0.getOperand(i).getOpcode() != ISD::Constant &&
3485 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
3486 isSimple = false;
3487 break;
3488 }
3489
Duncan Sands83ec4b62008-06-06 12:08:01 +00003490 MVT DestEltVT = N->getValueType(0).getVectorElementType();
3491 assert(!DestEltVT.isVector() &&
Dan Gohman7f321562007-06-25 16:23:39 +00003492 "Element type of vector ValueType must not be vector!");
3493 if (isSimple) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003494 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(N0.getNode(), DestEltVT);
Dan Gohman7f321562007-06-25 16:23:39 +00003495 }
3496 }
3497
Dan Gohman3dd168d2008-09-05 01:58:21 +00003498 // If the input is a constant, let getNode fold it.
Chris Lattner94683772005-12-23 05:30:37 +00003499 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
Dan Gohman475871a2008-07-27 21:46:04 +00003500 SDValue Res = DAG.getNode(ISD::BIT_CONVERT, VT, N0);
Gabor Greifba36cb52008-08-28 21:40:38 +00003501 if (Res.getNode() != N) return Res;
Chris Lattner94683772005-12-23 05:30:37 +00003502 }
3503
Chris Lattnerc8547d82005-12-23 05:37:50 +00003504 if (N0.getOpcode() == ISD::BIT_CONVERT) // conv(conv(x,t1),t2) -> conv(x,t2)
3505 return DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
Chris Lattner6258fb22006-04-02 02:53:43 +00003506
Chris Lattner57104102005-12-23 05:44:41 +00003507 // fold (conv (load x)) -> (load (conv*)x)
Evan Cheng513da432007-10-06 08:19:55 +00003508 // If the resultant load doesn't need a higher alignment than the original!
Gabor Greifba36cb52008-08-28 21:40:38 +00003509 if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003510 // Do not change the width of a volatile load.
3511 !cast<LoadSDNode>(N0)->isVolatile() &&
3512 (!AfterLegalize || TLI.isOperationLegal(ISD::LOAD, VT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003513 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman6448d912008-09-04 15:39:15 +00003514 unsigned Align = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00003515 getABITypeAlignment(VT.getTypeForMVT());
Evan Cheng59d5b682007-05-07 21:27:48 +00003516 unsigned OrigAlign = LN0->getAlignment();
3517 if (Align <= OrigAlign) {
Dan Gohman475871a2008-07-27 21:46:04 +00003518 SDValue Load = DAG.getLoad(VT, LN0->getChain(), LN0->getBasePtr(),
Evan Cheng59d5b682007-05-07 21:27:48 +00003519 LN0->getSrcValue(), LN0->getSrcValueOffset(),
Dan Gohman77455612008-06-28 00:45:22 +00003520 LN0->isVolatile(), OrigAlign);
Evan Cheng59d5b682007-05-07 21:27:48 +00003521 AddToWorkList(N);
Gabor Greif12632d22008-08-30 19:29:20 +00003522 CombineTo(N0.getNode(),
3523 DAG.getNode(ISD::BIT_CONVERT, N0.getValueType(), Load),
Evan Cheng59d5b682007-05-07 21:27:48 +00003524 Load.getValue(1));
3525 return Load;
3526 }
Chris Lattner57104102005-12-23 05:44:41 +00003527 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003528
Chris Lattner3bd39d42008-01-27 17:42:27 +00003529 // Fold bitconvert(fneg(x)) -> xor(bitconvert(x), signbit)
3530 // Fold bitconvert(fabs(x)) -> and(bitconvert(x), ~signbit)
3531 // This often reduces constant pool loads.
3532 if ((N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FABS) &&
Gabor Greifba36cb52008-08-28 21:40:38 +00003533 N0.getNode()->hasOneUse() && VT.isInteger() && !VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003534 SDValue NewConv = DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00003535 AddToWorkList(NewConv.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003536
Duncan Sands83ec4b62008-06-06 12:08:01 +00003537 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003538 if (N0.getOpcode() == ISD::FNEG)
3539 return DAG.getNode(ISD::XOR, VT, NewConv, DAG.getConstant(SignBit, VT));
3540 assert(N0.getOpcode() == ISD::FABS);
3541 return DAG.getNode(ISD::AND, VT, NewConv, DAG.getConstant(~SignBit, VT));
3542 }
3543
3544 // Fold bitconvert(fcopysign(cst, x)) -> bitconvert(x)&sign | cst&~sign'
3545 // Note that we don't handle copysign(x,cst) because this can always be folded
3546 // to an fneg or fabs.
Gabor Greifba36cb52008-08-28 21:40:38 +00003547 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse() &&
Chris Lattnerf32aac32008-01-27 23:32:17 +00003548 isa<ConstantFPSDNode>(N0.getOperand(0)) &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003549 VT.isInteger() && !VT.isVector()) {
3550 unsigned OrigXWidth = N0.getOperand(1).getValueType().getSizeInBits();
Dan Gohman475871a2008-07-27 21:46:04 +00003551 SDValue X = DAG.getNode(ISD::BIT_CONVERT,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003552 MVT::getIntegerVT(OrigXWidth),
Chris Lattner3bd39d42008-01-27 17:42:27 +00003553 N0.getOperand(1));
Gabor Greifba36cb52008-08-28 21:40:38 +00003554 AddToWorkList(X.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003555
3556 // If X has a different width than the result/lhs, sext it or truncate it.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003557 unsigned VTWidth = VT.getSizeInBits();
Chris Lattner3bd39d42008-01-27 17:42:27 +00003558 if (OrigXWidth < VTWidth) {
3559 X = DAG.getNode(ISD::SIGN_EXTEND, VT, X);
Gabor Greifba36cb52008-08-28 21:40:38 +00003560 AddToWorkList(X.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003561 } else if (OrigXWidth > VTWidth) {
3562 // To get the sign bit in the right place, we have to shift it right
3563 // before truncating.
3564 X = DAG.getNode(ISD::SRL, X.getValueType(), X,
3565 DAG.getConstant(OrigXWidth-VTWidth, X.getValueType()));
Gabor Greifba36cb52008-08-28 21:40:38 +00003566 AddToWorkList(X.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003567 X = DAG.getNode(ISD::TRUNCATE, VT, X);
Gabor Greifba36cb52008-08-28 21:40:38 +00003568 AddToWorkList(X.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003569 }
3570
Duncan Sands83ec4b62008-06-06 12:08:01 +00003571 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003572 X = DAG.getNode(ISD::AND, VT, X, DAG.getConstant(SignBit, VT));
Gabor Greifba36cb52008-08-28 21:40:38 +00003573 AddToWorkList(X.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003574
Dan Gohman475871a2008-07-27 21:46:04 +00003575 SDValue Cst = DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
Chris Lattner3bd39d42008-01-27 17:42:27 +00003576 Cst = DAG.getNode(ISD::AND, VT, Cst, DAG.getConstant(~SignBit, VT));
Gabor Greifba36cb52008-08-28 21:40:38 +00003577 AddToWorkList(Cst.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003578
3579 return DAG.getNode(ISD::OR, VT, X, Cst);
3580 }
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003581
3582 // bitconvert(build_pair(ld, ld)) -> ld iff load locations are consecutive.
3583 if (N0.getOpcode() == ISD::BUILD_PAIR) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003584 SDValue CombineLD = CombineConsecutiveLoads(N0.getNode(), VT);
3585 if (CombineLD.getNode())
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003586 return CombineLD;
3587 }
Chris Lattner3bd39d42008-01-27 17:42:27 +00003588
Dan Gohman475871a2008-07-27 21:46:04 +00003589 return SDValue();
Chris Lattner94683772005-12-23 05:30:37 +00003590}
3591
Dan Gohman475871a2008-07-27 21:46:04 +00003592SDValue DAGCombiner::visitBUILD_PAIR(SDNode *N) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003593 MVT VT = N->getValueType(0);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003594 return CombineConsecutiveLoads(N, VT);
3595}
3596
Dan Gohman7f321562007-06-25 16:23:39 +00003597/// ConstantFoldBIT_CONVERTofBUILD_VECTOR - We know that BV is a build_vector
Chris Lattner6258fb22006-04-02 02:53:43 +00003598/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
3599/// destination element value type.
Dan Gohman475871a2008-07-27 21:46:04 +00003600SDValue DAGCombiner::
Duncan Sands83ec4b62008-06-06 12:08:01 +00003601ConstantFoldBIT_CONVERTofBUILD_VECTOR(SDNode *BV, MVT DstEltVT) {
3602 MVT SrcEltVT = BV->getOperand(0).getValueType();
Chris Lattner6258fb22006-04-02 02:53:43 +00003603
3604 // If this is already the right type, we're done.
Dan Gohman475871a2008-07-27 21:46:04 +00003605 if (SrcEltVT == DstEltVT) return SDValue(BV, 0);
Chris Lattner6258fb22006-04-02 02:53:43 +00003606
Duncan Sands83ec4b62008-06-06 12:08:01 +00003607 unsigned SrcBitSize = SrcEltVT.getSizeInBits();
3608 unsigned DstBitSize = DstEltVT.getSizeInBits();
Chris Lattner6258fb22006-04-02 02:53:43 +00003609
3610 // If this is a conversion of N elements of one type to N elements of another
3611 // type, convert each element. This handles FP<->INT cases.
3612 if (SrcBitSize == DstBitSize) {
Dan Gohman475871a2008-07-27 21:46:04 +00003613 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003614 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003615 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, DstEltVT, BV->getOperand(i)));
Gabor Greifba36cb52008-08-28 21:40:38 +00003616 AddToWorkList(Ops.back().getNode());
Chris Lattner3e104b12006-04-08 04:15:24 +00003617 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00003618 MVT VT = MVT::getVectorVT(DstEltVT,
3619 BV->getValueType(0).getVectorNumElements());
Dan Gohman7f321562007-06-25 16:23:39 +00003620 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003621 }
3622
3623 // Otherwise, we're growing or shrinking the elements. To avoid having to
3624 // handle annoying details of growing/shrinking FP values, we convert them to
3625 // int first.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003626 if (SrcEltVT.isFloatingPoint()) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003627 // Convert the input float vector to a int vector where the elements are the
3628 // same sizes.
3629 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
Duncan Sands8eab8a22008-06-09 11:32:28 +00003630 MVT IntVT = MVT::getIntegerVT(SrcEltVT.getSizeInBits());
Gabor Greifba36cb52008-08-28 21:40:38 +00003631 BV = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, IntVT).getNode();
Chris Lattner6258fb22006-04-02 02:53:43 +00003632 SrcEltVT = IntVT;
3633 }
3634
3635 // Now we know the input is an integer vector. If the output is a FP type,
3636 // convert to integer first, then to FP of the right size.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003637 if (DstEltVT.isFloatingPoint()) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003638 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
Duncan Sands8eab8a22008-06-09 11:32:28 +00003639 MVT TmpVT = MVT::getIntegerVT(DstEltVT.getSizeInBits());
Gabor Greifba36cb52008-08-28 21:40:38 +00003640 SDNode *Tmp = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, TmpVT).getNode();
Chris Lattner6258fb22006-04-02 02:53:43 +00003641
3642 // Next, convert to FP elements of the same size.
Dan Gohman7f321562007-06-25 16:23:39 +00003643 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(Tmp, DstEltVT);
Chris Lattner6258fb22006-04-02 02:53:43 +00003644 }
3645
3646 // Okay, we know the src/dst types are both integers of differing types.
3647 // Handling growing first.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003648 assert(SrcEltVT.isInteger() && DstEltVT.isInteger());
Chris Lattner6258fb22006-04-02 02:53:43 +00003649 if (SrcBitSize < DstBitSize) {
3650 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
3651
Dan Gohman475871a2008-07-27 21:46:04 +00003652 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003653 for (unsigned i = 0, e = BV->getNumOperands(); i != e;
Chris Lattner6258fb22006-04-02 02:53:43 +00003654 i += NumInputsPerOutput) {
3655 bool isLE = TLI.isLittleEndian();
Dan Gohman220a8232008-03-03 23:51:38 +00003656 APInt NewBits = APInt(DstBitSize, 0);
Chris Lattner6258fb22006-04-02 02:53:43 +00003657 bool EltIsUndef = true;
3658 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
3659 // Shift the previously computed bits over.
3660 NewBits <<= SrcBitSize;
Dan Gohman475871a2008-07-27 21:46:04 +00003661 SDValue Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
Chris Lattner6258fb22006-04-02 02:53:43 +00003662 if (Op.getOpcode() == ISD::UNDEF) continue;
3663 EltIsUndef = false;
3664
Dan Gohman220a8232008-03-03 23:51:38 +00003665 NewBits |=
3666 APInt(cast<ConstantSDNode>(Op)->getAPIntValue()).zext(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00003667 }
3668
3669 if (EltIsUndef)
3670 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
3671 else
3672 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
3673 }
3674
Duncan Sands83ec4b62008-06-06 12:08:01 +00003675 MVT VT = MVT::getVectorVT(DstEltVT, Ops.size());
Dan Gohman7f321562007-06-25 16:23:39 +00003676 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003677 }
3678
3679 // Finally, this must be the case where we are shrinking elements: each input
3680 // turns into multiple outputs.
Evan Chengefec7512008-02-18 23:04:32 +00003681 bool isS2V = ISD::isScalarToVector(BV);
Chris Lattner6258fb22006-04-02 02:53:43 +00003682 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003683 MVT VT = MVT::getVectorVT(DstEltVT, NumOutputsPerInput*BV->getNumOperands());
Dan Gohman475871a2008-07-27 21:46:04 +00003684 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003685 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003686 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
3687 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
3688 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
3689 continue;
3690 }
Dan Gohman220a8232008-03-03 23:51:38 +00003691 APInt OpVal = cast<ConstantSDNode>(BV->getOperand(i))->getAPIntValue();
Chris Lattner6258fb22006-04-02 02:53:43 +00003692 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
Dan Gohman220a8232008-03-03 23:51:38 +00003693 APInt ThisVal = APInt(OpVal).trunc(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00003694 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
Dan Gohman220a8232008-03-03 23:51:38 +00003695 if (isS2V && i == 0 && j == 0 && APInt(ThisVal).zext(SrcBitSize) == OpVal)
Evan Chengefec7512008-02-18 23:04:32 +00003696 // Simply turn this into a SCALAR_TO_VECTOR of the new type.
3697 return DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Ops[0]);
Dan Gohman220a8232008-03-03 23:51:38 +00003698 OpVal = OpVal.lshr(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00003699 }
3700
3701 // For big endian targets, swap the order of the pieces of each element.
Duncan Sands0753fc12008-02-11 10:37:04 +00003702 if (TLI.isBigEndian())
Chris Lattner6258fb22006-04-02 02:53:43 +00003703 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
3704 }
Dan Gohman7f321562007-06-25 16:23:39 +00003705 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003706}
3707
3708
3709
Dan Gohman475871a2008-07-27 21:46:04 +00003710SDValue DAGCombiner::visitFADD(SDNode *N) {
3711 SDValue N0 = N->getOperand(0);
3712 SDValue N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003713 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3714 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003715 MVT VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00003716
Dan Gohman7f321562007-06-25 16:23:39 +00003717 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003718 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003719 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003720 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00003721 }
Dan Gohman7f321562007-06-25 16:23:39 +00003722
Nate Begemana0e221d2005-10-18 00:28:13 +00003723 // fold (fadd c1, c2) -> c1+c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003724 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003725 return DAG.getNode(ISD::FADD, VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003726 // canonicalize constant to RHS
3727 if (N0CFP && !N1CFP)
3728 return DAG.getNode(ISD::FADD, VT, N1, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00003729 // fold (A + (-B)) -> A-B
Chris Lattner0254e702008-02-26 07:04:54 +00003730 if (isNegatibleForFree(N1, AfterLegalize) == 2)
3731 return DAG.getNode(ISD::FSUB, VT, N0,
3732 GetNegatedExpression(N1, DAG, AfterLegalize));
Chris Lattner01b3d732005-09-28 22:28:18 +00003733 // fold ((-A) + B) -> B-A
Chris Lattner0254e702008-02-26 07:04:54 +00003734 if (isNegatibleForFree(N0, AfterLegalize) == 2)
3735 return DAG.getNode(ISD::FSUB, VT, N1,
3736 GetNegatedExpression(N0, DAG, AfterLegalize));
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003737
3738 // If allowed, fold (fadd (fadd x, c1), c2) -> (fadd x, (fadd c1, c2))
3739 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FADD &&
Gabor Greifba36cb52008-08-28 21:40:38 +00003740 N0.getNode()->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003741 return DAG.getNode(ISD::FADD, VT, N0.getOperand(0),
3742 DAG.getNode(ISD::FADD, VT, N0.getOperand(1), N1));
3743
Dan Gohman475871a2008-07-27 21:46:04 +00003744 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00003745}
3746
Dan Gohman475871a2008-07-27 21:46:04 +00003747SDValue DAGCombiner::visitFSUB(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 (fsub 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::FSUB, VT, N0, N1);
Dan Gohman23ff1822007-07-02 15:48:56 +00003763 // fold (0-B) -> -B
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003764 if (UnsafeFPMath && N0CFP && N0CFP->getValueAPF().isZero()) {
Chris Lattner0254e702008-02-26 07:04:54 +00003765 if (isNegatibleForFree(N1, AfterLegalize))
3766 return GetNegatedExpression(N1, DAG, AfterLegalize);
Dan Gohman23ff1822007-07-02 15:48:56 +00003767 return DAG.getNode(ISD::FNEG, VT, N1);
3768 }
Chris Lattner01b3d732005-09-28 22:28:18 +00003769 // fold (A-(-B)) -> A+B
Chris Lattner0254e702008-02-26 07:04:54 +00003770 if (isNegatibleForFree(N1, AfterLegalize))
3771 return DAG.getNode(ISD::FADD, VT, N0,
3772 GetNegatedExpression(N1, DAG, AfterLegalize));
Chris Lattner29446522007-05-14 22:04:50 +00003773
Dan Gohman475871a2008-07-27 21:46:04 +00003774 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00003775}
3776
Dan Gohman475871a2008-07-27 21:46:04 +00003777SDValue DAGCombiner::visitFMUL(SDNode *N) {
3778 SDValue N0 = N->getOperand(0);
3779 SDValue N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003780 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3781 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003782 MVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00003783
Dan Gohman7f321562007-06-25 16:23:39 +00003784 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003785 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003786 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003787 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00003788 }
Dan Gohman7f321562007-06-25 16:23:39 +00003789
Nate Begeman11af4ea2005-10-17 20:40:11 +00003790 // fold (fmul c1, c2) -> c1*c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003791 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003792 return DAG.getNode(ISD::FMUL, VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003793 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00003794 if (N0CFP && !N1CFP)
3795 return DAG.getNode(ISD::FMUL, VT, N1, N0);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003796 // fold (fmul X, 2.0) -> (fadd X, X)
3797 if (N1CFP && N1CFP->isExactlyValue(+2.0))
3798 return DAG.getNode(ISD::FADD, VT, N0, N0);
Chris Lattner29446522007-05-14 22:04:50 +00003799 // fold (fmul X, -1.0) -> (fneg X)
3800 if (N1CFP && N1CFP->isExactlyValue(-1.0))
3801 return DAG.getNode(ISD::FNEG, VT, N0);
3802
3803 // -X * -Y -> X*Y
Chris Lattner0254e702008-02-26 07:04:54 +00003804 if (char LHSNeg = isNegatibleForFree(N0, AfterLegalize)) {
3805 if (char RHSNeg = isNegatibleForFree(N1, AfterLegalize)) {
Chris Lattner29446522007-05-14 22:04:50 +00003806 // Both can be negated for free, check to see if at least one is cheaper
3807 // negated.
3808 if (LHSNeg == 2 || RHSNeg == 2)
Chris Lattner0254e702008-02-26 07:04:54 +00003809 return DAG.getNode(ISD::FMUL, VT,
3810 GetNegatedExpression(N0, DAG, AfterLegalize),
3811 GetNegatedExpression(N1, DAG, AfterLegalize));
Chris Lattner29446522007-05-14 22:04:50 +00003812 }
3813 }
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003814
3815 // If allowed, fold (fmul (fmul x, c1), c2) -> (fmul x, (fmul c1, c2))
3816 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FMUL &&
Gabor Greifba36cb52008-08-28 21:40:38 +00003817 N0.getNode()->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003818 return DAG.getNode(ISD::FMUL, VT, N0.getOperand(0),
3819 DAG.getNode(ISD::FMUL, VT, N0.getOperand(1), N1));
3820
Dan Gohman475871a2008-07-27 21:46:04 +00003821 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00003822}
3823
Dan Gohman475871a2008-07-27 21:46:04 +00003824SDValue DAGCombiner::visitFDIV(SDNode *N) {
3825 SDValue N0 = N->getOperand(0);
3826 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003827 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3828 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003829 MVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00003830
Dan Gohman7f321562007-06-25 16:23:39 +00003831 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003832 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003833 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003834 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00003835 }
Dan Gohman7f321562007-06-25 16:23:39 +00003836
Nate Begemana148d982006-01-18 22:35:16 +00003837 // fold (fdiv c1, c2) -> c1/c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003838 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003839 return DAG.getNode(ISD::FDIV, VT, N0, N1);
Chris Lattner29446522007-05-14 22:04:50 +00003840
3841
3842 // -X / -Y -> X*Y
Chris Lattner0254e702008-02-26 07:04:54 +00003843 if (char LHSNeg = isNegatibleForFree(N0, AfterLegalize)) {
3844 if (char RHSNeg = isNegatibleForFree(N1, AfterLegalize)) {
Chris Lattner29446522007-05-14 22:04:50 +00003845 // Both can be negated for free, check to see if at least one is cheaper
3846 // negated.
3847 if (LHSNeg == 2 || RHSNeg == 2)
Chris Lattner0254e702008-02-26 07:04:54 +00003848 return DAG.getNode(ISD::FDIV, VT,
3849 GetNegatedExpression(N0, DAG, AfterLegalize),
3850 GetNegatedExpression(N1, DAG, AfterLegalize));
Chris Lattner29446522007-05-14 22:04:50 +00003851 }
3852 }
3853
Dan Gohman475871a2008-07-27 21:46:04 +00003854 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00003855}
3856
Dan Gohman475871a2008-07-27 21:46:04 +00003857SDValue DAGCombiner::visitFREM(SDNode *N) {
3858 SDValue N0 = N->getOperand(0);
3859 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003860 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3861 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003862 MVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00003863
Nate Begemana148d982006-01-18 22:35:16 +00003864 // fold (frem c1, c2) -> fmod(c1,c2)
Dale Johannesendb44bf82007-10-16 23:38:29 +00003865 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003866 return DAG.getNode(ISD::FREM, VT, N0, N1);
Dan Gohman7f321562007-06-25 16:23:39 +00003867
Dan Gohman475871a2008-07-27 21:46:04 +00003868 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00003869}
3870
Dan Gohman475871a2008-07-27 21:46:04 +00003871SDValue DAGCombiner::visitFCOPYSIGN(SDNode *N) {
3872 SDValue N0 = N->getOperand(0);
3873 SDValue N1 = N->getOperand(1);
Chris Lattner12d83032006-03-05 05:30:57 +00003874 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3875 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003876 MVT VT = N->getValueType(0);
Chris Lattner12d83032006-03-05 05:30:57 +00003877
Dale Johannesendb44bf82007-10-16 23:38:29 +00003878 if (N0CFP && N1CFP && VT != MVT::ppcf128) // Constant fold
Chris Lattner12d83032006-03-05 05:30:57 +00003879 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1);
3880
3881 if (N1CFP) {
Dale Johannesene6c17422007-08-26 01:18:27 +00003882 const APFloat& V = N1CFP->getValueAPF();
Chris Lattner12d83032006-03-05 05:30:57 +00003883 // copysign(x, c1) -> fabs(x) iff ispos(c1)
3884 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
Dale Johannesen87503a62007-08-25 22:10:57 +00003885 if (!V.isNegative())
Chris Lattner12d83032006-03-05 05:30:57 +00003886 return DAG.getNode(ISD::FABS, VT, N0);
3887 else
3888 return DAG.getNode(ISD::FNEG, VT, DAG.getNode(ISD::FABS, VT, N0));
3889 }
3890
3891 // copysign(fabs(x), y) -> copysign(x, y)
3892 // copysign(fneg(x), y) -> copysign(x, y)
3893 // copysign(copysign(x,z), y) -> copysign(x, y)
3894 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
3895 N0.getOpcode() == ISD::FCOPYSIGN)
3896 return DAG.getNode(ISD::FCOPYSIGN, VT, N0.getOperand(0), N1);
3897
3898 // copysign(x, abs(y)) -> abs(x)
3899 if (N1.getOpcode() == ISD::FABS)
3900 return DAG.getNode(ISD::FABS, VT, N0);
3901
3902 // copysign(x, copysign(y,z)) -> copysign(x, z)
3903 if (N1.getOpcode() == ISD::FCOPYSIGN)
3904 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(1));
3905
3906 // copysign(x, fp_extend(y)) -> copysign(x, y)
3907 // copysign(x, fp_round(y)) -> copysign(x, y)
3908 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
3909 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(0));
3910
Dan Gohman475871a2008-07-27 21:46:04 +00003911 return SDValue();
Chris Lattner12d83032006-03-05 05:30:57 +00003912}
3913
3914
Chris Lattner01b3d732005-09-28 22:28:18 +00003915
Dan Gohman475871a2008-07-27 21:46:04 +00003916SDValue DAGCombiner::visitSINT_TO_FP(SDNode *N) {
3917 SDValue N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003918 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003919 MVT VT = N->getValueType(0);
Chris Lattnercda88752008-06-26 00:16:49 +00003920 MVT OpVT = N0.getValueType();
3921
Nate Begeman1d4d4142005-09-01 00:19:25 +00003922 // fold (sint_to_fp c1) -> c1fp
Chris Lattnercda88752008-06-26 00:16:49 +00003923 if (N0C && OpVT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003924 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
Chris Lattnercda88752008-06-26 00:16:49 +00003925
3926 // If the input is a legal type, and SINT_TO_FP is not legal on this target,
3927 // but UINT_TO_FP is legal on this target, try to convert.
Chris Lattnerf77e46b2008-06-26 17:16:00 +00003928 if (!TLI.isOperationLegal(ISD::SINT_TO_FP, OpVT) &&
Chris Lattnercda88752008-06-26 00:16:49 +00003929 TLI.isOperationLegal(ISD::UINT_TO_FP, OpVT)) {
3930 // If the sign bit is known to be zero, we can change this to UINT_TO_FP.
3931 if (DAG.SignBitIsZero(N0))
3932 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
3933 }
3934
3935
Dan Gohman475871a2008-07-27 21:46:04 +00003936 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003937}
3938
Dan Gohman475871a2008-07-27 21:46:04 +00003939SDValue DAGCombiner::visitUINT_TO_FP(SDNode *N) {
3940 SDValue N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003941 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003942 MVT VT = N->getValueType(0);
Chris Lattnercda88752008-06-26 00:16:49 +00003943 MVT OpVT = N0.getValueType();
Nate Begemana148d982006-01-18 22:35:16 +00003944
Nate Begeman1d4d4142005-09-01 00:19:25 +00003945 // fold (uint_to_fp c1) -> c1fp
Chris Lattnercda88752008-06-26 00:16:49 +00003946 if (N0C && OpVT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003947 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
Chris Lattnercda88752008-06-26 00:16:49 +00003948
3949 // If the input is a legal type, and UINT_TO_FP is not legal on this target,
3950 // but SINT_TO_FP is legal on this target, try to convert.
Chris Lattnerf77e46b2008-06-26 17:16:00 +00003951 if (!TLI.isOperationLegal(ISD::UINT_TO_FP, OpVT) &&
Chris Lattnercda88752008-06-26 00:16:49 +00003952 TLI.isOperationLegal(ISD::SINT_TO_FP, OpVT)) {
3953 // If the sign bit is known to be zero, we can change this to SINT_TO_FP.
3954 if (DAG.SignBitIsZero(N0))
3955 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
3956 }
3957
Dan Gohman475871a2008-07-27 21:46:04 +00003958 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003959}
3960
Dan Gohman475871a2008-07-27 21:46:04 +00003961SDValue DAGCombiner::visitFP_TO_SINT(SDNode *N) {
3962 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00003963 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003964 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003965
3966 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00003967 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00003968 return DAG.getNode(ISD::FP_TO_SINT, VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00003969 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003970}
3971
Dan Gohman475871a2008-07-27 21:46:04 +00003972SDValue DAGCombiner::visitFP_TO_UINT(SDNode *N) {
3973 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00003974 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003975 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003976
3977 // fold (fp_to_uint c1fp) -> c1
Dale Johannesendb44bf82007-10-16 23:38:29 +00003978 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003979 return DAG.getNode(ISD::FP_TO_UINT, VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00003980 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003981}
3982
Dan Gohman475871a2008-07-27 21:46:04 +00003983SDValue DAGCombiner::visitFP_ROUND(SDNode *N) {
3984 SDValue N0 = N->getOperand(0);
3985 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003986 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003987 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003988
3989 // fold (fp_round c1fp) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00003990 if (N0CFP && N0.getValueType() != MVT::ppcf128)
Chris Lattner0bd48932008-01-17 07:00:52 +00003991 return DAG.getNode(ISD::FP_ROUND, VT, N0, N1);
Chris Lattner79dbea52006-03-13 06:26:26 +00003992
3993 // fold (fp_round (fp_extend x)) -> x
3994 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
3995 return N0.getOperand(0);
3996
Chris Lattner0aa5e6f2008-01-24 06:45:35 +00003997 // fold (fp_round (fp_round x)) -> (fp_round x)
3998 if (N0.getOpcode() == ISD::FP_ROUND) {
3999 // This is a value preserving truncation if both round's are.
4000 bool IsTrunc = N->getConstantOperandVal(1) == 1 &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004001 N0.getNode()->getConstantOperandVal(1) == 1;
Chris Lattner0aa5e6f2008-01-24 06:45:35 +00004002 return DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0),
4003 DAG.getIntPtrConstant(IsTrunc));
4004 }
4005
Chris Lattner79dbea52006-03-13 06:26:26 +00004006 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
Gabor Greifba36cb52008-08-28 21:40:38 +00004007 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004008 SDValue Tmp = DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0), N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00004009 AddToWorkList(Tmp.getNode());
Chris Lattner79dbea52006-03-13 06:26:26 +00004010 return DAG.getNode(ISD::FCOPYSIGN, VT, Tmp, N0.getOperand(1));
4011 }
4012
Dan Gohman475871a2008-07-27 21:46:04 +00004013 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004014}
4015
Dan Gohman475871a2008-07-27 21:46:04 +00004016SDValue DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
4017 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004018 MVT VT = N->getValueType(0);
4019 MVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00004020 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004021
Nate Begeman1d4d4142005-09-01 00:19:25 +00004022 // fold (fp_round_inreg c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00004023 if (N0CFP) {
Dan Gohman475871a2008-07-27 21:46:04 +00004024 SDValue Round = DAG.getConstantFP(N0CFP->getValueAPF(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00004025 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004026 }
Dan Gohman475871a2008-07-27 21:46:04 +00004027 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004028}
4029
Dan Gohman475871a2008-07-27 21:46:04 +00004030SDValue DAGCombiner::visitFP_EXTEND(SDNode *N) {
4031 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00004032 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004033 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004034
Chris Lattner5938bef2007-12-29 06:55:23 +00004035 // If this is fp_round(fpextend), don't fold it, allow ourselves to be folded.
Roman Levenstein9cac5252008-04-16 16:15:27 +00004036 if (N->hasOneUse() &&
Dan Gohman475871a2008-07-27 21:46:04 +00004037 N->use_begin().getUse().getSDValue().getOpcode() == ISD::FP_ROUND)
4038 return SDValue();
Chris Lattner0bd48932008-01-17 07:00:52 +00004039
Nate Begeman1d4d4142005-09-01 00:19:25 +00004040 // fold (fp_extend c1fp) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00004041 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00004042 return DAG.getNode(ISD::FP_EXTEND, VT, N0);
Chris Lattner0bd48932008-01-17 07:00:52 +00004043
4044 // Turn fp_extend(fp_round(X, 1)) -> x since the fp_round doesn't affect the
4045 // value of X.
Gabor Greif12632d22008-08-30 19:29:20 +00004046 if (N0.getOpcode() == ISD::FP_ROUND
4047 && N0.getNode()->getConstantOperandVal(1) == 1) {
Dan Gohman475871a2008-07-27 21:46:04 +00004048 SDValue In = N0.getOperand(0);
Chris Lattner0bd48932008-01-17 07:00:52 +00004049 if (In.getValueType() == VT) return In;
Duncan Sands8e4eb092008-06-08 20:54:56 +00004050 if (VT.bitsLT(In.getValueType()))
Chris Lattner0bd48932008-01-17 07:00:52 +00004051 return DAG.getNode(ISD::FP_ROUND, VT, In, N0.getOperand(1));
4052 return DAG.getNode(ISD::FP_EXTEND, VT, In);
4053 }
4054
4055 // fold (fpext (load x)) -> (fpext (fptrunc (extload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00004056 if (ISD::isNON_EXTLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004057 ((!AfterLegalize && !cast<LoadSDNode>(N0)->isVolatile()) ||
4058 TLI.isLoadXLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00004059 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman475871a2008-07-27 21:46:04 +00004060 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
Evan Cheng466685d2006-10-09 20:57:25 +00004061 LN0->getBasePtr(), LN0->getSrcValue(),
4062 LN0->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00004063 N0.getValueType(),
4064 LN0->isVolatile(),
4065 LN0->getAlignment());
Chris Lattnere564dbb2006-05-05 21:34:35 +00004066 CombineTo(N, ExtLoad);
Gabor Greif12632d22008-08-30 19:29:20 +00004067 CombineTo(N0.getNode(), DAG.getNode(ISD::FP_ROUND, N0.getValueType(),
4068 ExtLoad, DAG.getIntPtrConstant(1)),
Chris Lattnere564dbb2006-05-05 21:34:35 +00004069 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004070 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnere564dbb2006-05-05 21:34:35 +00004071 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004072
Dan Gohman475871a2008-07-27 21:46:04 +00004073 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004074}
4075
Dan Gohman475871a2008-07-27 21:46:04 +00004076SDValue DAGCombiner::visitFNEG(SDNode *N) {
4077 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00004078
Chris Lattner0254e702008-02-26 07:04:54 +00004079 if (isNegatibleForFree(N0, AfterLegalize))
4080 return GetNegatedExpression(N0, DAG, AfterLegalize);
Dan Gohman23ff1822007-07-02 15:48:56 +00004081
Chris Lattner3bd39d42008-01-27 17:42:27 +00004082 // Transform fneg(bitconvert(x)) -> bitconvert(x^sign) to avoid loading
4083 // constant pool values.
Gabor Greifba36cb52008-08-28 21:40:38 +00004084 if (N0.getOpcode() == ISD::BIT_CONVERT && N0.getNode()->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00004085 N0.getOperand(0).getValueType().isInteger() &&
4086 !N0.getOperand(0).getValueType().isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004087 SDValue Int = N0.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004088 MVT IntVT = Int.getValueType();
4089 if (IntVT.isInteger() && !IntVT.isVector()) {
Chris Lattner3bd39d42008-01-27 17:42:27 +00004090 Int = DAG.getNode(ISD::XOR, IntVT, Int,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004091 DAG.getConstant(IntVT.getIntegerVTSignBit(), IntVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00004092 AddToWorkList(Int.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00004093 return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Int);
4094 }
4095 }
4096
Dan Gohman475871a2008-07-27 21:46:04 +00004097 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004098}
4099
Dan Gohman475871a2008-07-27 21:46:04 +00004100SDValue DAGCombiner::visitFABS(SDNode *N) {
4101 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00004102 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004103 MVT VT = N->getValueType(0);
Nate Begemana148d982006-01-18 22:35:16 +00004104
Nate Begeman1d4d4142005-09-01 00:19:25 +00004105 // fold (fabs c1) -> fabs(c1)
Dale Johannesendb44bf82007-10-16 23:38:29 +00004106 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00004107 return DAG.getNode(ISD::FABS, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004108 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00004109 if (N0.getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00004110 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004111 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00004112 // fold (fabs (fcopysign x, y)) -> (fabs x)
4113 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
4114 return DAG.getNode(ISD::FABS, VT, N0.getOperand(0));
4115
Chris Lattner3bd39d42008-01-27 17:42:27 +00004116 // Transform fabs(bitconvert(x)) -> bitconvert(x&~sign) to avoid loading
4117 // constant pool values.
Gabor Greifba36cb52008-08-28 21:40:38 +00004118 if (N0.getOpcode() == ISD::BIT_CONVERT && N0.getNode()->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00004119 N0.getOperand(0).getValueType().isInteger() &&
4120 !N0.getOperand(0).getValueType().isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004121 SDValue Int = N0.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004122 MVT IntVT = Int.getValueType();
4123 if (IntVT.isInteger() && !IntVT.isVector()) {
Chris Lattner3bd39d42008-01-27 17:42:27 +00004124 Int = DAG.getNode(ISD::AND, IntVT, Int,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004125 DAG.getConstant(~IntVT.getIntegerVTSignBit(), IntVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00004126 AddToWorkList(Int.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00004127 return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Int);
4128 }
4129 }
4130
Dan Gohman475871a2008-07-27 21:46:04 +00004131 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004132}
4133
Dan Gohman475871a2008-07-27 21:46:04 +00004134SDValue DAGCombiner::visitBRCOND(SDNode *N) {
4135 SDValue Chain = N->getOperand(0);
4136 SDValue N1 = N->getOperand(1);
4137 SDValue N2 = N->getOperand(2);
Nate Begeman44728a72005-09-19 22:34:01 +00004138 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
4139
4140 // never taken branch, fold to chain
4141 if (N1C && N1C->isNullValue())
4142 return Chain;
4143 // unconditional branch
Dan Gohman002e5d02008-03-13 22:13:53 +00004144 if (N1C && N1C->getAPIntValue() == 1)
Nate Begeman44728a72005-09-19 22:34:01 +00004145 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
Nate Begeman750ac1b2006-02-01 07:19:44 +00004146 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
4147 // on the target.
4148 if (N1.getOpcode() == ISD::SETCC &&
4149 TLI.isOperationLegal(ISD::BR_CC, MVT::Other)) {
4150 return DAG.getNode(ISD::BR_CC, MVT::Other, Chain, N1.getOperand(2),
4151 N1.getOperand(0), N1.getOperand(1), N2);
4152 }
Dan Gohman475871a2008-07-27 21:46:04 +00004153 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00004154}
4155
Chris Lattner3ea0b472005-10-05 06:47:48 +00004156// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
4157//
Dan Gohman475871a2008-07-27 21:46:04 +00004158SDValue DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00004159 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004160 SDValue CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
Chris Lattner3ea0b472005-10-05 06:47:48 +00004161
Duncan Sands8eab8a22008-06-09 11:32:28 +00004162 // Use SimplifySetCC to simplify SETCC's.
Dan Gohman475871a2008-07-27 21:46:04 +00004163 SDValue Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false);
Gabor Greifba36cb52008-08-28 21:40:38 +00004164 if (Simp.getNode()) AddToWorkList(Simp.getNode());
Chris Lattner30f73e72006-10-14 03:52:46 +00004165
Gabor Greifba36cb52008-08-28 21:40:38 +00004166 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.getNode());
Nate Begemane17daeb2005-10-05 21:43:42 +00004167
4168 // fold br_cc true, dest -> br dest (unconditional branch)
Dan Gohman002e5d02008-03-13 22:13:53 +00004169 if (SCCC && !SCCC->isNullValue())
Nate Begemane17daeb2005-10-05 21:43:42 +00004170 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
4171 N->getOperand(4));
4172 // fold br_cc false, dest -> unconditional fall through
4173 if (SCCC && SCCC->isNullValue())
4174 return N->getOperand(0);
Chris Lattner30f73e72006-10-14 03:52:46 +00004175
Nate Begemane17daeb2005-10-05 21:43:42 +00004176 // fold to a simpler setcc
Gabor Greifba36cb52008-08-28 21:40:38 +00004177 if (Simp.getNode() && Simp.getOpcode() == ISD::SETCC)
Nate Begemane17daeb2005-10-05 21:43:42 +00004178 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
4179 Simp.getOperand(2), Simp.getOperand(0),
4180 Simp.getOperand(1), N->getOperand(4));
Dan Gohman475871a2008-07-27 21:46:04 +00004181 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00004182}
4183
Chris Lattner448f2192006-11-11 00:39:41 +00004184
Duncan Sandsec87aa82008-06-15 20:12:31 +00004185/// CombineToPreIndexedLoadStore - Try turning a load / store into a
4186/// pre-indexed load / store when the base pointer is an add or subtract
Chris Lattner448f2192006-11-11 00:39:41 +00004187/// and it has other uses besides the load / store. After the
4188/// transformation, the new indexed load / store has effectively folded
4189/// the add / subtract in and all of its other uses are redirected to the
4190/// new load / store.
4191bool DAGCombiner::CombineToPreIndexedLoadStore(SDNode *N) {
4192 if (!AfterLegalize)
4193 return false;
4194
4195 bool isLoad = true;
Dan Gohman475871a2008-07-27 21:46:04 +00004196 SDValue Ptr;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004197 MVT VT;
Chris Lattner448f2192006-11-11 00:39:41 +00004198 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004199 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004200 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004201 VT = LD->getMemoryVT();
Evan Cheng83060c52007-03-07 08:07:03 +00004202 if (!TLI.isIndexedLoadLegal(ISD::PRE_INC, VT) &&
Chris Lattner448f2192006-11-11 00:39:41 +00004203 !TLI.isIndexedLoadLegal(ISD::PRE_DEC, VT))
4204 return false;
4205 Ptr = LD->getBasePtr();
4206 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004207 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004208 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004209 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00004210 if (!TLI.isIndexedStoreLegal(ISD::PRE_INC, VT) &&
4211 !TLI.isIndexedStoreLegal(ISD::PRE_DEC, VT))
4212 return false;
4213 Ptr = ST->getBasePtr();
4214 isLoad = false;
4215 } else
4216 return false;
4217
Chris Lattner9f1794e2006-11-11 00:56:29 +00004218 // If the pointer is not an add/sub, or if it doesn't have multiple uses, bail
4219 // out. There is no reason to make this a preinc/predec.
4220 if ((Ptr.getOpcode() != ISD::ADD && Ptr.getOpcode() != ISD::SUB) ||
Gabor Greifba36cb52008-08-28 21:40:38 +00004221 Ptr.getNode()->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00004222 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00004223
Chris Lattner9f1794e2006-11-11 00:56:29 +00004224 // Ask the target to do addressing mode selection.
Dan Gohman475871a2008-07-27 21:46:04 +00004225 SDValue BasePtr;
4226 SDValue Offset;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004227 ISD::MemIndexedMode AM = ISD::UNINDEXED;
4228 if (!TLI.getPreIndexedAddressParts(N, BasePtr, Offset, AM, DAG))
4229 return false;
Evan Chenga7d4a042007-05-03 23:52:19 +00004230 // Don't create a indexed load / store with zero offset.
4231 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00004232 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Chenga7d4a042007-05-03 23:52:19 +00004233 return false;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004234
Chris Lattner41e53fd2006-11-11 01:00:15 +00004235 // Try turning it into a pre-indexed load / store except when:
Evan Chengc843abe2007-05-24 02:35:39 +00004236 // 1) The new base ptr is a frame index.
4237 // 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 +00004238 // predecessor of the value being stored.
Evan Chengc843abe2007-05-24 02:35:39 +00004239 // 3) Another use of old base ptr is a predecessor of N. If ptr is folded
Chris Lattner9f1794e2006-11-11 00:56:29 +00004240 // that would create a cycle.
Evan Chengc843abe2007-05-24 02:35:39 +00004241 // 4) All uses are load / store ops that use it as old base ptr.
Chris Lattner448f2192006-11-11 00:39:41 +00004242
Chris Lattner41e53fd2006-11-11 01:00:15 +00004243 // Check #1. Preinc'ing a frame index would require copying the stack pointer
4244 // (plus the implicit offset) to a register to preinc anyway.
4245 if (isa<FrameIndexSDNode>(BasePtr))
4246 return false;
4247
4248 // Check #2.
Chris Lattner9f1794e2006-11-11 00:56:29 +00004249 if (!isLoad) {
Dan Gohman475871a2008-07-27 21:46:04 +00004250 SDValue Val = cast<StoreSDNode>(N)->getValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00004251 if (Val == BasePtr || BasePtr.getNode()->isPredecessorOf(Val.getNode()))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004252 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00004253 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004254
Evan Chengc843abe2007-05-24 02:35:39 +00004255 // Now check for #3 and #4.
Chris Lattner9f1794e2006-11-11 00:56:29 +00004256 bool RealUse = false;
Gabor Greifba36cb52008-08-28 21:40:38 +00004257 for (SDNode::use_iterator I = Ptr.getNode()->use_begin(),
4258 E = Ptr.getNode()->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00004259 SDNode *Use = *I;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004260 if (Use == N)
4261 continue;
Evan Cheng917be682008-03-04 00:41:45 +00004262 if (Use->isPredecessorOf(N))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004263 return false;
4264
4265 if (!((Use->getOpcode() == ISD::LOAD &&
4266 cast<LoadSDNode>(Use)->getBasePtr() == Ptr) ||
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00004267 (Use->getOpcode() == ISD::STORE &&
4268 cast<StoreSDNode>(Use)->getBasePtr() == Ptr)))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004269 RealUse = true;
4270 }
4271 if (!RealUse)
4272 return false;
4273
Dan Gohman475871a2008-07-27 21:46:04 +00004274 SDValue Result;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004275 if (isLoad)
Dan Gohman475871a2008-07-27 21:46:04 +00004276 Result = DAG.getIndexedLoad(SDValue(N,0), BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004277 else
Dan Gohman475871a2008-07-27 21:46:04 +00004278 Result = DAG.getIndexedStore(SDValue(N,0), BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004279 ++PreIndexedNodes;
4280 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +00004281 DOUT << "\nReplacing.4 "; DEBUG(N->dump(&DAG));
Gabor Greifba36cb52008-08-28 21:40:38 +00004282 DOUT << "\nWith: "; DEBUG(Result.getNode()->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +00004283 DOUT << '\n';
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004284 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004285 if (isLoad) {
Dan Gohman475871a2008-07-27 21:46:04 +00004286 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004287 &DeadNodes);
Dan Gohman475871a2008-07-27 21:46:04 +00004288 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004289 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004290 } else {
Dan Gohman475871a2008-07-27 21:46:04 +00004291 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004292 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004293 }
4294
Chris Lattner9f1794e2006-11-11 00:56:29 +00004295 // Finally, since the node is now dead, remove it from the graph.
4296 DAG.DeleteNode(N);
4297
4298 // Replace the uses of Ptr with uses of the updated base value.
4299 DAG.ReplaceAllUsesOfValueWith(Ptr, Result.getValue(isLoad ? 1 : 0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004300 &DeadNodes);
Gabor Greifba36cb52008-08-28 21:40:38 +00004301 removeFromWorkList(Ptr.getNode());
4302 DAG.DeleteNode(Ptr.getNode());
Chris Lattner9f1794e2006-11-11 00:56:29 +00004303
4304 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00004305}
4306
Duncan Sandsec87aa82008-06-15 20:12:31 +00004307/// CombineToPostIndexedLoadStore - Try to combine a load / store with a
Chris Lattner448f2192006-11-11 00:39:41 +00004308/// add / sub of the base pointer node into a post-indexed load / store.
4309/// The transformation folded the add / subtract into the new indexed
4310/// load / store effectively and all of its uses are redirected to the
4311/// new load / store.
4312bool DAGCombiner::CombineToPostIndexedLoadStore(SDNode *N) {
4313 if (!AfterLegalize)
4314 return false;
4315
4316 bool isLoad = true;
Dan Gohman475871a2008-07-27 21:46:04 +00004317 SDValue Ptr;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004318 MVT VT;
Chris Lattner448f2192006-11-11 00:39:41 +00004319 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004320 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004321 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004322 VT = LD->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00004323 if (!TLI.isIndexedLoadLegal(ISD::POST_INC, VT) &&
4324 !TLI.isIndexedLoadLegal(ISD::POST_DEC, VT))
4325 return false;
4326 Ptr = LD->getBasePtr();
4327 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004328 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004329 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004330 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00004331 if (!TLI.isIndexedStoreLegal(ISD::POST_INC, VT) &&
4332 !TLI.isIndexedStoreLegal(ISD::POST_DEC, VT))
4333 return false;
4334 Ptr = ST->getBasePtr();
4335 isLoad = false;
4336 } else
4337 return false;
4338
Gabor Greifba36cb52008-08-28 21:40:38 +00004339 if (Ptr.getNode()->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00004340 return false;
4341
Gabor Greifba36cb52008-08-28 21:40:38 +00004342 for (SDNode::use_iterator I = Ptr.getNode()->use_begin(),
4343 E = Ptr.getNode()->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00004344 SDNode *Op = *I;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004345 if (Op == N ||
4346 (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB))
4347 continue;
4348
Dan Gohman475871a2008-07-27 21:46:04 +00004349 SDValue BasePtr;
4350 SDValue Offset;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004351 ISD::MemIndexedMode AM = ISD::UNINDEXED;
4352 if (TLI.getPostIndexedAddressParts(N, Op, BasePtr, Offset, AM, DAG)) {
4353 if (Ptr == Offset)
4354 std::swap(BasePtr, Offset);
4355 if (Ptr != BasePtr)
Chris Lattner448f2192006-11-11 00:39:41 +00004356 continue;
Evan Chenga7d4a042007-05-03 23:52:19 +00004357 // Don't create a indexed load / store with zero offset.
4358 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00004359 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Chenga7d4a042007-05-03 23:52:19 +00004360 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00004361
Chris Lattner9f1794e2006-11-11 00:56:29 +00004362 // Try turning it into a post-indexed load / store except when
4363 // 1) All uses are load / store ops that use it as base ptr.
4364 // 2) Op must be independent of N, i.e. Op is neither a predecessor
4365 // nor a successor of N. Otherwise, if Op is folded that would
4366 // create a cycle.
4367
4368 // Check for #1.
4369 bool TryNext = false;
Gabor Greifba36cb52008-08-28 21:40:38 +00004370 for (SDNode::use_iterator II = BasePtr.getNode()->use_begin(),
4371 EE = BasePtr.getNode()->use_end(); II != EE; ++II) {
Dan Gohman89684502008-07-27 20:43:25 +00004372 SDNode *Use = *II;
Gabor Greifba36cb52008-08-28 21:40:38 +00004373 if (Use == Ptr.getNode())
Chris Lattner448f2192006-11-11 00:39:41 +00004374 continue;
4375
Chris Lattner9f1794e2006-11-11 00:56:29 +00004376 // If all the uses are load / store addresses, then don't do the
4377 // transformation.
4378 if (Use->getOpcode() == ISD::ADD || Use->getOpcode() == ISD::SUB){
4379 bool RealUse = false;
4380 for (SDNode::use_iterator III = Use->use_begin(),
4381 EEE = Use->use_end(); III != EEE; ++III) {
Dan Gohman89684502008-07-27 20:43:25 +00004382 SDNode *UseUse = *III;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004383 if (!((UseUse->getOpcode() == ISD::LOAD &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004384 cast<LoadSDNode>(UseUse)->getBasePtr().getNode() == Use) ||
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00004385 (UseUse->getOpcode() == ISD::STORE &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004386 cast<StoreSDNode>(UseUse)->getBasePtr().getNode() == Use)))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004387 RealUse = true;
4388 }
Chris Lattner448f2192006-11-11 00:39:41 +00004389
Chris Lattner9f1794e2006-11-11 00:56:29 +00004390 if (!RealUse) {
4391 TryNext = true;
4392 break;
Chris Lattner448f2192006-11-11 00:39:41 +00004393 }
4394 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004395 }
4396 if (TryNext)
4397 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00004398
Chris Lattner9f1794e2006-11-11 00:56:29 +00004399 // Check for #2
Evan Cheng917be682008-03-04 00:41:45 +00004400 if (!Op->isPredecessorOf(N) && !N->isPredecessorOf(Op)) {
Dan Gohman475871a2008-07-27 21:46:04 +00004401 SDValue Result = isLoad
4402 ? DAG.getIndexedLoad(SDValue(N,0), BasePtr, Offset, AM)
4403 : DAG.getIndexedStore(SDValue(N,0), BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004404 ++PostIndexedNodes;
4405 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +00004406 DOUT << "\nReplacing.5 "; DEBUG(N->dump(&DAG));
Gabor Greifba36cb52008-08-28 21:40:38 +00004407 DOUT << "\nWith: "; DEBUG(Result.getNode()->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +00004408 DOUT << '\n';
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004409 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004410 if (isLoad) {
Dan Gohman475871a2008-07-27 21:46:04 +00004411 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004412 &DeadNodes);
Dan Gohman475871a2008-07-27 21:46:04 +00004413 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004414 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004415 } else {
Dan Gohman475871a2008-07-27 21:46:04 +00004416 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004417 &DeadNodes);
Chris Lattner448f2192006-11-11 00:39:41 +00004418 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004419
Chris Lattner9f1794e2006-11-11 00:56:29 +00004420 // Finally, since the node is now dead, remove it from the graph.
4421 DAG.DeleteNode(N);
4422
4423 // Replace the uses of Use with uses of the updated base value.
Dan Gohman475871a2008-07-27 21:46:04 +00004424 DAG.ReplaceAllUsesOfValueWith(SDValue(Op, 0),
Chris Lattner9f1794e2006-11-11 00:56:29 +00004425 Result.getValue(isLoad ? 1 : 0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004426 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004427 removeFromWorkList(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004428 DAG.DeleteNode(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004429 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00004430 }
4431 }
4432 }
4433 return false;
4434}
4435
Chris Lattner00161a62008-01-25 07:20:16 +00004436/// InferAlignment - If we can infer some alignment information from this
4437/// pointer, return it.
Dan Gohman475871a2008-07-27 21:46:04 +00004438static unsigned InferAlignment(SDValue Ptr, SelectionDAG &DAG) {
Chris Lattner00161a62008-01-25 07:20:16 +00004439 // If this is a direct reference to a stack slot, use information about the
4440 // stack slot's alignment.
Chris Lattner1329cb82008-01-26 19:45:50 +00004441 int FrameIdx = 1 << 31;
4442 int64_t FrameOffset = 0;
Chris Lattner00161a62008-01-25 07:20:16 +00004443 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
Chris Lattner1329cb82008-01-26 19:45:50 +00004444 FrameIdx = FI->getIndex();
4445 } else if (Ptr.getOpcode() == ISD::ADD &&
4446 isa<ConstantSDNode>(Ptr.getOperand(1)) &&
4447 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4448 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4449 FrameOffset = Ptr.getConstantOperandVal(1);
Chris Lattner00161a62008-01-25 07:20:16 +00004450 }
Chris Lattner1329cb82008-01-26 19:45:50 +00004451
4452 if (FrameIdx != (1 << 31)) {
4453 // FIXME: Handle FI+CST.
4454 const MachineFrameInfo &MFI = *DAG.getMachineFunction().getFrameInfo();
4455 if (MFI.isFixedObjectIndex(FrameIdx)) {
Dan Gohman8cea8ff2008-08-11 18:27:03 +00004456 int64_t ObjectOffset = MFI.getObjectOffset(FrameIdx) + FrameOffset;
Chris Lattner1329cb82008-01-26 19:45:50 +00004457
4458 // The alignment of the frame index can be determined from its offset from
4459 // the incoming frame position. If the frame object is at offset 32 and
4460 // the stack is guaranteed to be 16-byte aligned, then we know that the
4461 // object is 16-byte aligned.
4462 unsigned StackAlign = DAG.getTarget().getFrameInfo()->getStackAlignment();
4463 unsigned Align = MinAlign(ObjectOffset, StackAlign);
4464
4465 // Finally, the frame object itself may have a known alignment. Factor
4466 // the alignment + offset into a new alignment. For example, if we know
4467 // the FI is 8 byte aligned, but the pointer is 4 off, we really have a
4468 // 4-byte alignment of the resultant pointer. Likewise align 4 + 4-byte
4469 // offset = 4-byte alignment, align 4 + 1-byte offset = align 1, etc.
4470 unsigned FIInfoAlign = MinAlign(MFI.getObjectAlignment(FrameIdx),
4471 FrameOffset);
4472 return std::max(Align, FIInfoAlign);
4473 }
4474 }
Chris Lattner00161a62008-01-25 07:20:16 +00004475
4476 return 0;
4477}
Chris Lattner448f2192006-11-11 00:39:41 +00004478
Dan Gohman475871a2008-07-27 21:46:04 +00004479SDValue DAGCombiner::visitLOAD(SDNode *N) {
Evan Cheng466685d2006-10-09 20:57:25 +00004480 LoadSDNode *LD = cast<LoadSDNode>(N);
Dan Gohman475871a2008-07-27 21:46:04 +00004481 SDValue Chain = LD->getChain();
4482 SDValue Ptr = LD->getBasePtr();
Chris Lattner00161a62008-01-25 07:20:16 +00004483
4484 // Try to infer better alignment information than the load already has.
Dan Gohmana2676512008-08-20 16:30:28 +00004485 if (!Fast && LD->isUnindexed()) {
Chris Lattner00161a62008-01-25 07:20:16 +00004486 if (unsigned Align = InferAlignment(Ptr, DAG)) {
4487 if (Align > LD->getAlignment())
4488 return DAG.getExtLoad(LD->getExtensionType(), LD->getValueType(0),
4489 Chain, Ptr, LD->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004490 LD->getSrcValueOffset(), LD->getMemoryVT(),
Chris Lattner00161a62008-01-25 07:20:16 +00004491 LD->isVolatile(), Align);
4492 }
4493 }
4494
Evan Cheng45a7ca92007-05-01 00:38:21 +00004495
4496 // If load is not volatile and there are no uses of the loaded value (and
4497 // the updated indexed value in case of indexed loads), change uses of the
4498 // chain value into uses of the chain input (i.e. delete the dead load).
4499 if (!LD->isVolatile()) {
Evan Cheng498f5592007-05-01 08:53:39 +00004500 if (N->getValueType(1) == MVT::Other) {
4501 // Unindexed loads.
Evan Cheng02c42852008-01-16 23:11:54 +00004502 if (N->hasNUsesOfValue(0, 0)) {
4503 // It's not safe to use the two value CombineTo variant here. e.g.
4504 // v1, chain2 = load chain1, loc
4505 // v2, chain3 = load chain2, loc
4506 // v3 = add v2, c
Chris Lattner125991a2008-01-24 07:57:06 +00004507 // Now we replace use of chain2 with chain1. This makes the second load
4508 // isomorphic to the one we are deleting, and thus makes this load live.
Evan Cheng02c42852008-01-16 23:11:54 +00004509 DOUT << "\nReplacing.6 "; DEBUG(N->dump(&DAG));
Gabor Greifba36cb52008-08-28 21:40:38 +00004510 DOUT << "\nWith chain: "; DEBUG(Chain.getNode()->dump(&DAG));
Chris Lattner125991a2008-01-24 07:57:06 +00004511 DOUT << "\n";
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004512 WorkListRemover DeadNodes(*this);
Dan Gohman475871a2008-07-27 21:46:04 +00004513 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Chain, &DeadNodes);
Chris Lattner125991a2008-01-24 07:57:06 +00004514 if (N->use_empty()) {
4515 removeFromWorkList(N);
4516 DAG.DeleteNode(N);
4517 }
Dan Gohman475871a2008-07-27 21:46:04 +00004518 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng02c42852008-01-16 23:11:54 +00004519 }
Evan Cheng498f5592007-05-01 08:53:39 +00004520 } else {
4521 // Indexed loads.
4522 assert(N->getValueType(2) == MVT::Other && "Malformed indexed loads?");
4523 if (N->hasNUsesOfValue(0, 0) && N->hasNUsesOfValue(0, 1)) {
Dan Gohman475871a2008-07-27 21:46:04 +00004524 SDValue Undef = DAG.getNode(ISD::UNDEF, N->getValueType(0));
Evan Cheng02c42852008-01-16 23:11:54 +00004525 DOUT << "\nReplacing.6 "; DEBUG(N->dump(&DAG));
Gabor Greifba36cb52008-08-28 21:40:38 +00004526 DOUT << "\nWith: "; DEBUG(Undef.getNode()->dump(&DAG));
Evan Cheng02c42852008-01-16 23:11:54 +00004527 DOUT << " and 2 other values\n";
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004528 WorkListRemover DeadNodes(*this);
Dan Gohman475871a2008-07-27 21:46:04 +00004529 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Undef, &DeadNodes);
4530 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1),
Chris Lattner4626b252008-01-17 07:20:38 +00004531 DAG.getNode(ISD::UNDEF, N->getValueType(1)),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004532 &DeadNodes);
Dan Gohman475871a2008-07-27 21:46:04 +00004533 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 2), Chain, &DeadNodes);
Evan Cheng02c42852008-01-16 23:11:54 +00004534 removeFromWorkList(N);
Evan Cheng02c42852008-01-16 23:11:54 +00004535 DAG.DeleteNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +00004536 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng45a7ca92007-05-01 00:38:21 +00004537 }
Evan Cheng45a7ca92007-05-01 00:38:21 +00004538 }
4539 }
Chris Lattner01a22022005-10-10 22:04:48 +00004540
4541 // If this load is directly stored, replace the load value with the stored
4542 // value.
4543 // TODO: Handle store large -> read small portion.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004544 // TODO: Handle TRUNCSTORE/LOADEXT
Dan Gohmanb061c4b2008-03-31 20:32:52 +00004545 if (LD->getExtensionType() == ISD::NON_EXTLOAD &&
4546 !LD->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004547 if (ISD::isNON_TRUNCStore(Chain.getNode())) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00004548 StoreSDNode *PrevST = cast<StoreSDNode>(Chain);
4549 if (PrevST->getBasePtr() == Ptr &&
4550 PrevST->getValue().getValueType() == N->getValueType(0))
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004551 return CombineTo(N, Chain.getOperand(1), Chain);
Evan Cheng8b2794a2006-10-13 21:14:26 +00004552 }
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004553 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00004554
Jim Laskey7ca56af2006-10-11 13:47:09 +00004555 if (CombinerAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00004556 // Walk up chain skipping non-aliasing memory nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00004557 SDValue BetterChain = FindBetterChain(N, Chain);
Jim Laskey279f0532006-09-25 16:29:54 +00004558
Jim Laskey6ff23e52006-10-04 16:53:27 +00004559 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00004560 if (Chain != BetterChain) {
Dan Gohman475871a2008-07-27 21:46:04 +00004561 SDValue ReplLoad;
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004562
Jim Laskey279f0532006-09-25 16:29:54 +00004563 // Replace the chain to void dependency.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004564 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
4565 ReplLoad = DAG.getLoad(N->getValueType(0), BetterChain, Ptr,
Duncan Sandsdc846502007-10-28 12:59:45 +00004566 LD->getSrcValue(), LD->getSrcValueOffset(),
4567 LD->isVolatile(), LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004568 } else {
4569 ReplLoad = DAG.getExtLoad(LD->getExtensionType(),
4570 LD->getValueType(0),
4571 BetterChain, Ptr, LD->getSrcValue(),
4572 LD->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004573 LD->getMemoryVT(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00004574 LD->isVolatile(),
4575 LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004576 }
Jim Laskey279f0532006-09-25 16:29:54 +00004577
Jim Laskey6ff23e52006-10-04 16:53:27 +00004578 // Create token factor to keep old chain connected.
Dan Gohman475871a2008-07-27 21:46:04 +00004579 SDValue Token = DAG.getNode(ISD::TokenFactor, MVT::Other,
Jim Laskey288af5e2006-09-25 19:32:58 +00004580 Chain, ReplLoad.getValue(1));
Jim Laskey6ff23e52006-10-04 16:53:27 +00004581
Jim Laskey274062c2006-10-13 23:32:28 +00004582 // Replace uses with load result and token factor. Don't add users
4583 // to work list.
4584 return CombineTo(N, ReplLoad.getValue(0), Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00004585 }
4586 }
4587
Evan Cheng7fc033a2006-11-03 03:06:21 +00004588 // Try transforming N to an indexed load.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00004589 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Dan Gohman475871a2008-07-27 21:46:04 +00004590 return SDValue(N, 0);
Evan Cheng7fc033a2006-11-03 03:06:21 +00004591
Dan Gohman475871a2008-07-27 21:46:04 +00004592 return SDValue();
Chris Lattner01a22022005-10-10 22:04:48 +00004593}
4594
Chris Lattner07649d92008-01-08 23:08:06 +00004595
Dan Gohman475871a2008-07-27 21:46:04 +00004596SDValue DAGCombiner::visitSTORE(SDNode *N) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00004597 StoreSDNode *ST = cast<StoreSDNode>(N);
Dan Gohman475871a2008-07-27 21:46:04 +00004598 SDValue Chain = ST->getChain();
4599 SDValue Value = ST->getValue();
4600 SDValue Ptr = ST->getBasePtr();
Jim Laskey7aed46c2006-10-11 18:55:16 +00004601
Chris Lattner00161a62008-01-25 07:20:16 +00004602 // Try to infer better alignment information than the store already has.
Dan Gohmana2676512008-08-20 16:30:28 +00004603 if (!Fast && ST->isUnindexed()) {
Chris Lattner00161a62008-01-25 07:20:16 +00004604 if (unsigned Align = InferAlignment(Ptr, DAG)) {
4605 if (Align > ST->getAlignment())
4606 return DAG.getTruncStore(Chain, Value, Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004607 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattner00161a62008-01-25 07:20:16 +00004608 ST->isVolatile(), Align);
4609 }
4610 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004611
Evan Cheng59d5b682007-05-07 21:27:48 +00004612 // If this is a store of a bit convert, store the input value if the
Evan Cheng2c4f9432007-05-09 21:49:47 +00004613 // resultant store does not need a higher alignment than the original.
Dale Johannesen98a6c622007-05-16 22:45:30 +00004614 if (Value.getOpcode() == ISD::BIT_CONVERT && !ST->isTruncatingStore() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00004615 ST->isUnindexed()) {
Evan Cheng59d5b682007-05-07 21:27:48 +00004616 unsigned Align = ST->getAlignment();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004617 MVT SVT = Value.getOperand(0).getValueType();
Dan Gohman6448d912008-09-04 15:39:15 +00004618 unsigned OrigAlign = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00004619 getABITypeAlignment(SVT.getTypeForMVT());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004620 if (Align <= OrigAlign &&
4621 ((!AfterLegalize && !ST->isVolatile()) ||
4622 TLI.isOperationLegal(ISD::STORE, SVT)))
Evan Cheng59d5b682007-05-07 21:27:48 +00004623 return DAG.getStore(Chain, Value.getOperand(0), Ptr, ST->getSrcValue(),
Dan Gohman77455612008-06-28 00:45:22 +00004624 ST->getSrcValueOffset(), ST->isVolatile(), OrigAlign);
Jim Laskey279f0532006-09-25 16:29:54 +00004625 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004626
Nate Begeman2cbba892006-12-11 02:23:46 +00004627 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Nate Begeman2cbba892006-12-11 02:23:46 +00004628 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Value)) {
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004629 // NOTE: If the original store is volatile, this transform must not increase
4630 // the number of stores. For example, on x86-32 an f64 can be stored in one
4631 // processor operation but an i64 (which is not legal) requires two. So the
4632 // transform should not be done in this case.
Evan Cheng25ece662006-12-11 17:25:19 +00004633 if (Value.getOpcode() != ISD::TargetConstantFP) {
Dan Gohman475871a2008-07-27 21:46:04 +00004634 SDValue Tmp;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004635 switch (CFP->getValueType(0).getSimpleVT()) {
Chris Lattner62be1a72006-12-12 04:16:14 +00004636 default: assert(0 && "Unknown FP type");
Dale Johannesenc7b21d52007-09-18 18:36:59 +00004637 case MVT::f80: // We don't do this for these yet.
4638 case MVT::f128:
4639 case MVT::ppcf128:
4640 break;
Chris Lattner62be1a72006-12-12 04:16:14 +00004641 case MVT::f32:
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004642 if ((!AfterLegalize && !ST->isVolatile()) ||
4643 TLI.isOperationLegal(ISD::STORE, MVT::i32)) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00004644 Tmp = DAG.getConstant((uint32_t)CFP->getValueAPF().
4645 convertToAPInt().getZExtValue(), MVT::i32);
Chris Lattner62be1a72006-12-12 04:16:14 +00004646 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004647 ST->getSrcValueOffset(), ST->isVolatile(),
4648 ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00004649 }
4650 break;
4651 case MVT::f64:
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004652 if ((!AfterLegalize && !ST->isVolatile()) ||
4653 TLI.isOperationLegal(ISD::STORE, MVT::i64)) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00004654 Tmp = DAG.getConstant(CFP->getValueAPF().convertToAPInt().
4655 getZExtValue(), MVT::i64);
Chris Lattner62be1a72006-12-12 04:16:14 +00004656 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004657 ST->getSrcValueOffset(), ST->isVolatile(),
4658 ST->getAlignment());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004659 } else if (!ST->isVolatile() &&
4660 TLI.isOperationLegal(ISD::STORE, MVT::i32)) {
Duncan Sandsdc846502007-10-28 12:59:45 +00004661 // Many FP stores are not made apparent until after legalize, e.g. for
Chris Lattner62be1a72006-12-12 04:16:14 +00004662 // argument passing. Since this is so common, custom legalize the
4663 // 64-bit integer store into two 32-bit stores.
Dale Johannesen9d5f4562007-09-12 03:30:33 +00004664 uint64_t Val = CFP->getValueAPF().convertToAPInt().getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00004665 SDValue Lo = DAG.getConstant(Val & 0xFFFFFFFF, MVT::i32);
4666 SDValue Hi = DAG.getConstant(Val >> 32, MVT::i32);
Duncan Sands0753fc12008-02-11 10:37:04 +00004667 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner62be1a72006-12-12 04:16:14 +00004668
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004669 int SVOffset = ST->getSrcValueOffset();
4670 unsigned Alignment = ST->getAlignment();
4671 bool isVolatile = ST->isVolatile();
4672
Dan Gohman475871a2008-07-27 21:46:04 +00004673 SDValue St0 = DAG.getStore(Chain, Lo, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004674 ST->getSrcValueOffset(),
4675 isVolatile, ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00004676 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
4677 DAG.getConstant(4, Ptr.getValueType()));
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004678 SVOffset += 4;
Duncan Sandsdc846502007-10-28 12:59:45 +00004679 Alignment = MinAlign(Alignment, 4U);
Dan Gohman475871a2008-07-27 21:46:04 +00004680 SDValue St1 = DAG.getStore(Chain, Hi, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004681 SVOffset, isVolatile, Alignment);
Chris Lattner62be1a72006-12-12 04:16:14 +00004682 return DAG.getNode(ISD::TokenFactor, MVT::Other, St0, St1);
4683 }
4684 break;
Evan Cheng25ece662006-12-11 17:25:19 +00004685 }
Nate Begeman2cbba892006-12-11 02:23:46 +00004686 }
Nate Begeman2cbba892006-12-11 02:23:46 +00004687 }
4688
Jim Laskey279f0532006-09-25 16:29:54 +00004689 if (CombinerAA) {
4690 // Walk up chain skipping non-aliasing memory nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00004691 SDValue BetterChain = FindBetterChain(N, Chain);
Jim Laskey279f0532006-09-25 16:29:54 +00004692
Jim Laskey6ff23e52006-10-04 16:53:27 +00004693 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00004694 if (Chain != BetterChain) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00004695 // Replace the chain to avoid dependency.
Dan Gohman475871a2008-07-27 21:46:04 +00004696 SDValue ReplStore;
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004697 if (ST->isTruncatingStore()) {
4698 ReplStore = DAG.getTruncStore(BetterChain, Value, Ptr,
Chris Lattner4626b252008-01-17 07:20:38 +00004699 ST->getSrcValue(),ST->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004700 ST->getMemoryVT(),
Chris Lattner4626b252008-01-17 07:20:38 +00004701 ST->isVolatile(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004702 } else {
4703 ReplStore = DAG.getStore(BetterChain, Value, Ptr,
Chris Lattner4626b252008-01-17 07:20:38 +00004704 ST->getSrcValue(), ST->getSrcValueOffset(),
4705 ST->isVolatile(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004706 }
4707
Jim Laskey279f0532006-09-25 16:29:54 +00004708 // Create token to keep both nodes around.
Dan Gohman475871a2008-07-27 21:46:04 +00004709 SDValue Token =
Jim Laskey274062c2006-10-13 23:32:28 +00004710 DAG.getNode(ISD::TokenFactor, MVT::Other, Chain, ReplStore);
4711
4712 // Don't add users to work list.
4713 return CombineTo(N, Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00004714 }
Jim Laskeyd1aed7a2006-09-21 16:28:59 +00004715 }
Chris Lattnerc33baaa2005-12-23 05:48:07 +00004716
Evan Cheng33dbedc2006-11-05 09:31:14 +00004717 // Try transforming N to an indexed store.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00004718 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Dan Gohman475871a2008-07-27 21:46:04 +00004719 return SDValue(N, 0);
Evan Cheng33dbedc2006-11-05 09:31:14 +00004720
Chris Lattner3c872852007-12-29 06:26:16 +00004721 // FIXME: is there such a thing as a truncating indexed store?
Chris Lattnerddf89562008-01-17 19:59:44 +00004722 if (ST->isTruncatingStore() && ST->isUnindexed() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00004723 Value.getValueType().isInteger()) {
Chris Lattner2b4c2792007-10-13 06:35:54 +00004724 // See if we can simplify the input to this truncstore with knowledge that
4725 // only the low bits are being used. For example:
4726 // "truncstore (or (shl x, 8), y), i8" -> "truncstore y, i8"
Dan Gohman475871a2008-07-27 21:46:04 +00004727 SDValue Shorter =
Dan Gohman2e68b6f2008-02-25 21:11:39 +00004728 GetDemandedBits(Value,
4729 APInt::getLowBitsSet(Value.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +00004730 ST->getMemoryVT().getSizeInBits()));
Gabor Greifba36cb52008-08-28 21:40:38 +00004731 AddToWorkList(Value.getNode());
4732 if (Shorter.getNode())
Chris Lattner2b4c2792007-10-13 06:35:54 +00004733 return DAG.getTruncStore(Chain, Shorter, Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004734 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattner2b4c2792007-10-13 06:35:54 +00004735 ST->isVolatile(), ST->getAlignment());
Chris Lattnere33544c2007-10-13 06:58:48 +00004736
4737 // Otherwise, see if we can simplify the operation with
4738 // SimplifyDemandedBits, which only works if the value has a single use.
Dan Gohman7b8d4a92008-02-27 00:25:32 +00004739 if (SimplifyDemandedBits(Value,
4740 APInt::getLowBitsSet(
4741 Value.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +00004742 ST->getMemoryVT().getSizeInBits())))
Dan Gohman475871a2008-07-27 21:46:04 +00004743 return SDValue(N, 0);
Chris Lattner2b4c2792007-10-13 06:35:54 +00004744 }
4745
Chris Lattner3c872852007-12-29 06:26:16 +00004746 // If this is a load followed by a store to the same location, then the store
4747 // is dead/noop.
4748 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Value)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004749 if (Ld->getBasePtr() == Ptr && ST->getMemoryVT() == Ld->getMemoryVT() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00004750 ST->isUnindexed() && !ST->isVolatile() &&
Chris Lattner07649d92008-01-08 23:08:06 +00004751 // There can't be any side effects between the load and store, such as
4752 // a call or store.
Dan Gohman475871a2008-07-27 21:46:04 +00004753 Chain.reachesChainWithoutSideEffects(SDValue(Ld, 1))) {
Chris Lattner3c872852007-12-29 06:26:16 +00004754 // The store is dead, remove it.
4755 return Chain;
4756 }
4757 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004758
Chris Lattnerddf89562008-01-17 19:59:44 +00004759 // If this is an FP_ROUND or TRUNC followed by a store, fold this into a
4760 // truncating store. We can do this even if this is already a truncstore.
4761 if ((Value.getOpcode() == ISD::FP_ROUND || Value.getOpcode() == ISD::TRUNCATE)
Gabor Greifba36cb52008-08-28 21:40:38 +00004762 && Value.getNode()->hasOneUse() && ST->isUnindexed() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00004763 TLI.isTruncStoreLegal(Value.getOperand(0).getValueType(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004764 ST->getMemoryVT())) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004765 return DAG.getTruncStore(Chain, Value.getOperand(0), Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004766 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattnerddf89562008-01-17 19:59:44 +00004767 ST->isVolatile(), ST->getAlignment());
4768 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004769
Dan Gohman475871a2008-07-27 21:46:04 +00004770 return SDValue();
Chris Lattner87514ca2005-10-10 22:31:19 +00004771}
4772
Dan Gohman475871a2008-07-27 21:46:04 +00004773SDValue DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
4774 SDValue InVec = N->getOperand(0);
4775 SDValue InVal = N->getOperand(1);
4776 SDValue EltNo = N->getOperand(2);
Chris Lattnerca242442006-03-19 01:27:56 +00004777
4778 // If the invec is a BUILD_VECTOR and if EltNo is a constant, build a new
4779 // vector with the inserted element.
4780 if (InVec.getOpcode() == ISD::BUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004781 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Gabor Greif12632d22008-08-30 19:29:20 +00004782 SmallVector<SDValue, 8> Ops(InVec.getNode()->op_begin(),
4783 InVec.getNode()->op_end());
Chris Lattnerca242442006-03-19 01:27:56 +00004784 if (Elt < Ops.size())
4785 Ops[Elt] = InVal;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004786 return DAG.getNode(ISD::BUILD_VECTOR, InVec.getValueType(),
4787 &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00004788 }
4789
Dan Gohman475871a2008-07-27 21:46:04 +00004790 return SDValue();
Chris Lattnerca242442006-03-19 01:27:56 +00004791}
4792
Dan Gohman475871a2008-07-27 21:46:04 +00004793SDValue DAGCombiner::visitEXTRACT_VECTOR_ELT(SDNode *N) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004794 // (vextract (v4f32 load $addr), c) -> (f32 load $addr+c*size)
4795 // (vextract (v4f32 s2v (f32 load $addr)), c) -> (f32 load $addr+c*size)
4796 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), 0) -> (f32 load $addr)
4797
4798 // Perform only after legalization to ensure build_vector / vector_shuffle
4799 // optimizations have already been done.
Dan Gohman475871a2008-07-27 21:46:04 +00004800 if (!AfterLegalize) return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004801
Dan Gohman475871a2008-07-27 21:46:04 +00004802 SDValue InVec = N->getOperand(0);
4803 SDValue EltNo = N->getOperand(1);
Evan Cheng513da432007-10-06 08:19:55 +00004804
Evan Cheng513da432007-10-06 08:19:55 +00004805 if (isa<ConstantSDNode>(EltNo)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004806 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Evan Cheng513da432007-10-06 08:19:55 +00004807 bool NewLoad = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004808 MVT VT = InVec.getValueType();
4809 MVT EVT = VT.getVectorElementType();
4810 MVT LVT = EVT;
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004811 if (InVec.getOpcode() == ISD::BIT_CONVERT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004812 MVT BCVT = InVec.getOperand(0).getValueType();
Duncan Sands8e4eb092008-06-08 20:54:56 +00004813 if (!BCVT.isVector() || EVT.bitsGT(BCVT.getVectorElementType()))
Dan Gohman475871a2008-07-27 21:46:04 +00004814 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004815 InVec = InVec.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004816 EVT = BCVT.getVectorElementType();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004817 NewLoad = true;
4818 }
Evan Cheng513da432007-10-06 08:19:55 +00004819
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004820 LoadSDNode *LN0 = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00004821 if (ISD::isNormalLoad(InVec.getNode()))
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004822 LN0 = cast<LoadSDNode>(InVec);
4823 else if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR &&
4824 InVec.getOperand(0).getValueType() == EVT &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004825 ISD::isNormalLoad(InVec.getOperand(0).getNode())) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004826 LN0 = cast<LoadSDNode>(InVec.getOperand(0));
4827 } else if (InVec.getOpcode() == ISD::VECTOR_SHUFFLE) {
4828 // (vextract (vector_shuffle (load $addr), v2, <1, u, u, u>), 1)
4829 // =>
4830 // (load $addr+1*size)
4831 unsigned Idx = cast<ConstantSDNode>(InVec.getOperand(2).
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004832 getOperand(Elt))->getZExtValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004833 unsigned NumElems = InVec.getOperand(2).getNumOperands();
4834 InVec = (Idx < NumElems) ? InVec.getOperand(0) : InVec.getOperand(1);
4835 if (InVec.getOpcode() == ISD::BIT_CONVERT)
4836 InVec = InVec.getOperand(0);
Gabor Greifba36cb52008-08-28 21:40:38 +00004837 if (ISD::isNormalLoad(InVec.getNode())) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004838 LN0 = cast<LoadSDNode>(InVec);
4839 Elt = (Idx < NumElems) ? Idx : Idx - NumElems;
Evan Cheng513da432007-10-06 08:19:55 +00004840 }
4841 }
Duncan Sandsec87aa82008-06-15 20:12:31 +00004842 if (!LN0 || !LN0->hasOneUse() || LN0->isVolatile())
Dan Gohman475871a2008-07-27 21:46:04 +00004843 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004844
4845 unsigned Align = LN0->getAlignment();
4846 if (NewLoad) {
4847 // Check the resultant load doesn't need a higher alignment than the
4848 // original load.
Dan Gohman6448d912008-09-04 15:39:15 +00004849 unsigned NewAlign = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00004850 getABITypeAlignment(LVT.getTypeForMVT());
Duncan Sands184a8762008-06-14 17:48:34 +00004851 if (NewAlign > Align || !TLI.isOperationLegal(ISD::LOAD, LVT))
Dan Gohman475871a2008-07-27 21:46:04 +00004852 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004853 Align = NewAlign;
4854 }
4855
Dan Gohman475871a2008-07-27 21:46:04 +00004856 SDValue NewPtr = LN0->getBasePtr();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004857 if (Elt) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004858 unsigned PtrOff = LVT.getSizeInBits() * Elt / 8;
4859 MVT PtrType = NewPtr.getValueType();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004860 if (TLI.isBigEndian())
Duncan Sands83ec4b62008-06-06 12:08:01 +00004861 PtrOff = VT.getSizeInBits() / 8 - PtrOff;
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004862 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
4863 DAG.getConstant(PtrOff, PtrType));
4864 }
4865 return DAG.getLoad(LVT, LN0->getChain(), NewPtr,
4866 LN0->getSrcValue(), LN0->getSrcValueOffset(),
4867 LN0->isVolatile(), Align);
Evan Cheng513da432007-10-06 08:19:55 +00004868 }
Dan Gohman475871a2008-07-27 21:46:04 +00004869 return SDValue();
Evan Cheng513da432007-10-06 08:19:55 +00004870}
4871
4872
Dan Gohman475871a2008-07-27 21:46:04 +00004873SDValue DAGCombiner::visitBUILD_VECTOR(SDNode *N) {
Dan Gohman7f321562007-06-25 16:23:39 +00004874 unsigned NumInScalars = N->getNumOperands();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004875 MVT VT = N->getValueType(0);
4876 unsigned NumElts = VT.getVectorNumElements();
4877 MVT EltType = VT.getVectorElementType();
Chris Lattnerca242442006-03-19 01:27:56 +00004878
Dan Gohman7f321562007-06-25 16:23:39 +00004879 // Check to see if this is a BUILD_VECTOR of a bunch of EXTRACT_VECTOR_ELT
4880 // operations. If so, and if the EXTRACT_VECTOR_ELT vector inputs come from
4881 // at most two distinct vectors, turn this into a shuffle node.
Dan Gohman475871a2008-07-27 21:46:04 +00004882 SDValue VecIn1, VecIn2;
Chris Lattnerd7648c82006-03-28 20:28:38 +00004883 for (unsigned i = 0; i != NumInScalars; ++i) {
4884 // Ignore undef inputs.
4885 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
4886
Dan Gohman7f321562007-06-25 16:23:39 +00004887 // If this input is something other than a EXTRACT_VECTOR_ELT with a
Chris Lattnerd7648c82006-03-28 20:28:38 +00004888 // constant index, bail out.
Dan Gohman7f321562007-06-25 16:23:39 +00004889 if (N->getOperand(i).getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
Chris Lattnerd7648c82006-03-28 20:28:38 +00004890 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
Dan Gohman475871a2008-07-27 21:46:04 +00004891 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00004892 break;
4893 }
4894
Dan Gohman7f321562007-06-25 16:23:39 +00004895 // If the input vector type disagrees with the result of the build_vector,
Chris Lattnerd7648c82006-03-28 20:28:38 +00004896 // we can't make a shuffle.
Dan Gohman475871a2008-07-27 21:46:04 +00004897 SDValue ExtractedFromVec = N->getOperand(i).getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00004898 if (ExtractedFromVec.getValueType() != VT) {
Dan Gohman475871a2008-07-27 21:46:04 +00004899 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00004900 break;
4901 }
4902
4903 // Otherwise, remember this. We allow up to two distinct input vectors.
4904 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
4905 continue;
4906
Gabor Greifba36cb52008-08-28 21:40:38 +00004907 if (VecIn1.getNode() == 0) {
Chris Lattnerd7648c82006-03-28 20:28:38 +00004908 VecIn1 = ExtractedFromVec;
Gabor Greifba36cb52008-08-28 21:40:38 +00004909 } else if (VecIn2.getNode() == 0) {
Chris Lattnerd7648c82006-03-28 20:28:38 +00004910 VecIn2 = ExtractedFromVec;
4911 } else {
4912 // Too many inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00004913 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00004914 break;
4915 }
4916 }
4917
4918 // If everything is good, we can make a shuffle operation.
Gabor Greifba36cb52008-08-28 21:40:38 +00004919 if (VecIn1.getNode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004920 SmallVector<SDValue, 8> BuildVecIndices;
Chris Lattnerd7648c82006-03-28 20:28:38 +00004921 for (unsigned i = 0; i != NumInScalars; ++i) {
4922 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
Evan Cheng597a3bd2007-01-20 10:10:26 +00004923 BuildVecIndices.push_back(DAG.getNode(ISD::UNDEF, TLI.getPointerTy()));
Chris Lattnerd7648c82006-03-28 20:28:38 +00004924 continue;
4925 }
4926
Dan Gohman475871a2008-07-27 21:46:04 +00004927 SDValue Extract = N->getOperand(i);
Chris Lattnerd7648c82006-03-28 20:28:38 +00004928
4929 // If extracting from the first vector, just use the index directly.
4930 if (Extract.getOperand(0) == VecIn1) {
4931 BuildVecIndices.push_back(Extract.getOperand(1));
4932 continue;
4933 }
4934
4935 // Otherwise, use InIdx + VecSize
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004936 unsigned Idx =
4937 cast<ConstantSDNode>(Extract.getOperand(1))->getZExtValue();
Chris Lattner0bd48932008-01-17 07:00:52 +00004938 BuildVecIndices.push_back(DAG.getIntPtrConstant(Idx+NumInScalars));
Chris Lattnerd7648c82006-03-28 20:28:38 +00004939 }
4940
4941 // Add count and size info.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004942 MVT BuildVecVT = MVT::getVectorVT(TLI.getPointerTy(), NumElts);
Chris Lattnerd7648c82006-03-28 20:28:38 +00004943
Dan Gohman7f321562007-06-25 16:23:39 +00004944 // Return the new VECTOR_SHUFFLE node.
Dan Gohman475871a2008-07-27 21:46:04 +00004945 SDValue Ops[5];
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004946 Ops[0] = VecIn1;
Gabor Greifba36cb52008-08-28 21:40:38 +00004947 if (VecIn2.getNode()) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004948 Ops[1] = VecIn2;
Chris Lattnercef896e2006-03-28 22:19:47 +00004949 } else {
Dan Gohman7f321562007-06-25 16:23:39 +00004950 // Use an undef build_vector as input for the second operand.
Dan Gohman475871a2008-07-27 21:46:04 +00004951 std::vector<SDValue> UnOps(NumInScalars,
Chris Lattnercef896e2006-03-28 22:19:47 +00004952 DAG.getNode(ISD::UNDEF,
Dan Gohman7f321562007-06-25 16:23:39 +00004953 EltType));
4954 Ops[1] = DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004955 &UnOps[0], UnOps.size());
Gabor Greifba36cb52008-08-28 21:40:38 +00004956 AddToWorkList(Ops[1].getNode());
Chris Lattnercef896e2006-03-28 22:19:47 +00004957 }
Dan Gohman7f321562007-06-25 16:23:39 +00004958 Ops[2] = DAG.getNode(ISD::BUILD_VECTOR, BuildVecVT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004959 &BuildVecIndices[0], BuildVecIndices.size());
Dan Gohman7f321562007-06-25 16:23:39 +00004960 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Ops, 3);
Chris Lattnerd7648c82006-03-28 20:28:38 +00004961 }
4962
Dan Gohman475871a2008-07-27 21:46:04 +00004963 return SDValue();
Chris Lattnerd7648c82006-03-28 20:28:38 +00004964}
4965
Dan Gohman475871a2008-07-27 21:46:04 +00004966SDValue DAGCombiner::visitCONCAT_VECTORS(SDNode *N) {
Dan Gohman7f321562007-06-25 16:23:39 +00004967 // TODO: Check to see if this is a CONCAT_VECTORS of a bunch of
4968 // EXTRACT_SUBVECTOR operations. If so, and if the EXTRACT_SUBVECTOR vector
4969 // inputs come from at most two distinct vectors, turn this into a shuffle
4970 // node.
4971
4972 // If we only have one input vector, we don't need to do any concatenation.
4973 if (N->getNumOperands() == 1) {
4974 return N->getOperand(0);
4975 }
4976
Dan Gohman475871a2008-07-27 21:46:04 +00004977 return SDValue();
Dan Gohman7f321562007-06-25 16:23:39 +00004978}
4979
Dan Gohman475871a2008-07-27 21:46:04 +00004980SDValue DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
4981 SDValue ShufMask = N->getOperand(2);
Chris Lattnerf1d0c622006-03-31 22:16:43 +00004982 unsigned NumElts = ShufMask.getNumOperands();
4983
4984 // If the shuffle mask is an identity operation on the LHS, return the LHS.
4985 bool isIdentity = true;
4986 for (unsigned i = 0; i != NumElts; ++i) {
4987 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004988 cast<ConstantSDNode>(ShufMask.getOperand(i))->getZExtValue() != i) {
Chris Lattnerf1d0c622006-03-31 22:16:43 +00004989 isIdentity = false;
4990 break;
4991 }
4992 }
4993 if (isIdentity) return N->getOperand(0);
4994
4995 // If the shuffle mask is an identity operation on the RHS, return the RHS.
4996 isIdentity = true;
4997 for (unsigned i = 0; i != NumElts; ++i) {
4998 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004999 cast<ConstantSDNode>(ShufMask.getOperand(i))->getZExtValue() !=
5000 i+NumElts) {
Chris Lattnerf1d0c622006-03-31 22:16:43 +00005001 isIdentity = false;
5002 break;
5003 }
5004 }
5005 if (isIdentity) return N->getOperand(1);
Evan Chenge7bec0d2006-07-20 22:44:41 +00005006
5007 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
5008 // needed at all.
5009 bool isUnary = true;
Evan Cheng917ec982006-07-21 08:25:53 +00005010 bool isSplat = true;
Evan Chenge7bec0d2006-07-20 22:44:41 +00005011 int VecNum = -1;
Reid Spencer9160a6a2006-07-25 20:44:41 +00005012 unsigned BaseIdx = 0;
Evan Chenge7bec0d2006-07-20 22:44:41 +00005013 for (unsigned i = 0; i != NumElts; ++i)
5014 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005015 unsigned Idx=cast<ConstantSDNode>(ShufMask.getOperand(i))->getZExtValue();
Evan Chenge7bec0d2006-07-20 22:44:41 +00005016 int V = (Idx < NumElts) ? 0 : 1;
Evan Cheng917ec982006-07-21 08:25:53 +00005017 if (VecNum == -1) {
Evan Chenge7bec0d2006-07-20 22:44:41 +00005018 VecNum = V;
Evan Cheng917ec982006-07-21 08:25:53 +00005019 BaseIdx = Idx;
5020 } else {
5021 if (BaseIdx != Idx)
5022 isSplat = false;
5023 if (VecNum != V) {
5024 isUnary = false;
5025 break;
5026 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00005027 }
5028 }
5029
Dan Gohman475871a2008-07-27 21:46:04 +00005030 SDValue N0 = N->getOperand(0);
5031 SDValue N1 = N->getOperand(1);
Evan Chenge7bec0d2006-07-20 22:44:41 +00005032 // Normalize unary shuffle so the RHS is undef.
5033 if (isUnary && VecNum == 1)
5034 std::swap(N0, N1);
5035
Evan Cheng917ec982006-07-21 08:25:53 +00005036 // If it is a splat, check if the argument vector is a build_vector with
5037 // all scalar elements the same.
5038 if (isSplat) {
Gabor Greifba36cb52008-08-28 21:40:38 +00005039 SDNode *V = N0.getNode();
Evan Cheng917ec982006-07-21 08:25:53 +00005040
Dan Gohman7f321562007-06-25 16:23:39 +00005041 // If this is a bit convert that changes the element type of the vector but
Evan Cheng59569222006-10-16 22:49:37 +00005042 // not the number of vector elements, look through it. Be careful not to
5043 // look though conversions that change things like v4f32 to v2f64.
Dan Gohman7f321562007-06-25 16:23:39 +00005044 if (V->getOpcode() == ISD::BIT_CONVERT) {
Dan Gohman475871a2008-07-27 21:46:04 +00005045 SDValue ConvInput = V->getOperand(0);
Evan Cheng29257862008-07-22 20:42:56 +00005046 if (ConvInput.getValueType().isVector() &&
5047 ConvInput.getValueType().getVectorNumElements() == NumElts)
Gabor Greifba36cb52008-08-28 21:40:38 +00005048 V = ConvInput.getNode();
Evan Cheng59569222006-10-16 22:49:37 +00005049 }
5050
Dan Gohman7f321562007-06-25 16:23:39 +00005051 if (V->getOpcode() == ISD::BUILD_VECTOR) {
5052 unsigned NumElems = V->getNumOperands();
Evan Cheng917ec982006-07-21 08:25:53 +00005053 if (NumElems > BaseIdx) {
Dan Gohman475871a2008-07-27 21:46:04 +00005054 SDValue Base;
Evan Cheng917ec982006-07-21 08:25:53 +00005055 bool AllSame = true;
5056 for (unsigned i = 0; i != NumElems; ++i) {
5057 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
5058 Base = V->getOperand(i);
5059 break;
5060 }
5061 }
5062 // Splat of <u, u, u, u>, return <u, u, u, u>
Gabor Greifba36cb52008-08-28 21:40:38 +00005063 if (!Base.getNode())
Evan Cheng917ec982006-07-21 08:25:53 +00005064 return N0;
5065 for (unsigned i = 0; i != NumElems; ++i) {
Evan Chenge0480d22007-09-18 21:54:37 +00005066 if (V->getOperand(i) != Base) {
Evan Cheng917ec982006-07-21 08:25:53 +00005067 AllSame = false;
5068 break;
5069 }
5070 }
5071 // Splat of <x, x, x, x>, return <x, x, x, x>
5072 if (AllSame)
5073 return N0;
5074 }
5075 }
5076 }
5077
Evan Chenge7bec0d2006-07-20 22:44:41 +00005078 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
5079 // into an undef.
5080 if (isUnary || N0 == N1) {
Chris Lattner17614ea2006-04-08 05:34:25 +00005081 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
5082 // first operand.
Dan Gohman475871a2008-07-27 21:46:04 +00005083 SmallVector<SDValue, 8> MappedOps;
Chris Lattner17614ea2006-04-08 05:34:25 +00005084 for (unsigned i = 0; i != NumElts; ++i) {
5085 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005086 cast<ConstantSDNode>(ShufMask.getOperand(i))->getZExtValue() <
5087 NumElts) {
Chris Lattner17614ea2006-04-08 05:34:25 +00005088 MappedOps.push_back(ShufMask.getOperand(i));
5089 } else {
5090 unsigned NewIdx =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005091 cast<ConstantSDNode>(ShufMask.getOperand(i))->getZExtValue() -
5092 NumElts;
Duncan Sandsd038e042008-07-21 10:20:31 +00005093 MappedOps.push_back(DAG.getConstant(NewIdx,
5094 ShufMask.getOperand(i).getValueType()));
Chris Lattner17614ea2006-04-08 05:34:25 +00005095 }
5096 }
Dan Gohman7f321562007-06-25 16:23:39 +00005097 ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMask.getValueType(),
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005098 &MappedOps[0], MappedOps.size());
Gabor Greifba36cb52008-08-28 21:40:38 +00005099 AddToWorkList(ShufMask.getNode());
Dan Gohman7f321562007-06-25 16:23:39 +00005100 return DAG.getNode(ISD::VECTOR_SHUFFLE, N->getValueType(0),
5101 N0,
5102 DAG.getNode(ISD::UNDEF, N->getValueType(0)),
5103 ShufMask);
Chris Lattner17614ea2006-04-08 05:34:25 +00005104 }
Dan Gohman7f321562007-06-25 16:23:39 +00005105
Dan Gohman475871a2008-07-27 21:46:04 +00005106 return SDValue();
Chris Lattnerf1d0c622006-03-31 22:16:43 +00005107}
5108
Evan Cheng44f1f092006-04-20 08:56:16 +00005109/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
Dan Gohman7f321562007-06-25 16:23:39 +00005110/// an AND to a vector_shuffle with the destination vector and a zero vector.
5111/// e.g. AND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
Evan Cheng44f1f092006-04-20 08:56:16 +00005112/// vector_shuffle V, Zero, <0, 4, 2, 4>
Dan Gohman475871a2008-07-27 21:46:04 +00005113SDValue DAGCombiner::XformToShuffleWithZero(SDNode *N) {
5114 SDValue LHS = N->getOperand(0);
5115 SDValue RHS = N->getOperand(1);
Dan Gohman7f321562007-06-25 16:23:39 +00005116 if (N->getOpcode() == ISD::AND) {
5117 if (RHS.getOpcode() == ISD::BIT_CONVERT)
Evan Cheng44f1f092006-04-20 08:56:16 +00005118 RHS = RHS.getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00005119 if (RHS.getOpcode() == ISD::BUILD_VECTOR) {
Dan Gohman475871a2008-07-27 21:46:04 +00005120 std::vector<SDValue> IdxOps;
Evan Cheng44f1f092006-04-20 08:56:16 +00005121 unsigned NumOps = RHS.getNumOperands();
Dan Gohman7f321562007-06-25 16:23:39 +00005122 unsigned NumElts = NumOps;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005123 MVT EVT = RHS.getValueType().getVectorElementType();
Evan Cheng44f1f092006-04-20 08:56:16 +00005124 for (unsigned i = 0; i != NumElts; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005125 SDValue Elt = RHS.getOperand(i);
Evan Cheng44f1f092006-04-20 08:56:16 +00005126 if (!isa<ConstantSDNode>(Elt))
Dan Gohman475871a2008-07-27 21:46:04 +00005127 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00005128 else if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
Duncan Sands77926da2008-07-18 20:12:05 +00005129 IdxOps.push_back(DAG.getConstant(i, EVT));
Evan Cheng44f1f092006-04-20 08:56:16 +00005130 else if (cast<ConstantSDNode>(Elt)->isNullValue())
Duncan Sands77926da2008-07-18 20:12:05 +00005131 IdxOps.push_back(DAG.getConstant(NumElts, EVT));
Evan Cheng44f1f092006-04-20 08:56:16 +00005132 else
Dan Gohman475871a2008-07-27 21:46:04 +00005133 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00005134 }
5135
5136 // Let's see if the target supports this vector_shuffle.
5137 if (!TLI.isVectorClearMaskLegal(IdxOps, EVT, DAG))
Dan Gohman475871a2008-07-27 21:46:04 +00005138 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00005139
Dan Gohman7f321562007-06-25 16:23:39 +00005140 // Return the new VECTOR_SHUFFLE node.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005141 MVT VT = MVT::getVectorVT(EVT, NumElts);
Dan Gohman475871a2008-07-27 21:46:04 +00005142 std::vector<SDValue> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00005143 LHS = DAG.getNode(ISD::BIT_CONVERT, VT, LHS);
Evan Cheng44f1f092006-04-20 08:56:16 +00005144 Ops.push_back(LHS);
Gabor Greifba36cb52008-08-28 21:40:38 +00005145 AddToWorkList(LHS.getNode());
Dan Gohman475871a2008-07-27 21:46:04 +00005146 std::vector<SDValue> ZeroOps(NumElts, DAG.getConstant(0, EVT));
Dan Gohman7f321562007-06-25 16:23:39 +00005147 Ops.push_back(DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005148 &ZeroOps[0], ZeroOps.size()));
Duncan Sands77926da2008-07-18 20:12:05 +00005149 Ops.push_back(DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005150 &IdxOps[0], IdxOps.size()));
Dan Gohman475871a2008-07-27 21:46:04 +00005151 SDValue Result = DAG.getNode(ISD::VECTOR_SHUFFLE, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005152 &Ops[0], Ops.size());
Dan Gohman7a9a5af2008-07-16 16:13:58 +00005153 if (VT != N->getValueType(0))
5154 Result = DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Result);
Evan Cheng44f1f092006-04-20 08:56:16 +00005155 return Result;
5156 }
5157 }
Dan Gohman475871a2008-07-27 21:46:04 +00005158 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00005159}
5160
Dan Gohman7f321562007-06-25 16:23:39 +00005161/// SimplifyVBinOp - Visit a binary vector operation, like ADD.
Dan Gohman475871a2008-07-27 21:46:04 +00005162SDValue DAGCombiner::SimplifyVBinOp(SDNode *N) {
Dan Gohman7f321562007-06-25 16:23:39 +00005163 // After legalize, the target may be depending on adds and other
5164 // binary ops to provide legal ways to construct constants or other
5165 // things. Simplifying them may result in a loss of legality.
Dan Gohman475871a2008-07-27 21:46:04 +00005166 if (AfterLegalize) return SDValue();
Dan Gohman7f321562007-06-25 16:23:39 +00005167
Duncan Sands83ec4b62008-06-06 12:08:01 +00005168 MVT VT = N->getValueType(0);
5169 assert(VT.isVector() && "SimplifyVBinOp only works on vectors!");
Dan Gohman7f321562007-06-25 16:23:39 +00005170
Duncan Sands83ec4b62008-06-06 12:08:01 +00005171 MVT EltType = VT.getVectorElementType();
Dan Gohman475871a2008-07-27 21:46:04 +00005172 SDValue LHS = N->getOperand(0);
5173 SDValue RHS = N->getOperand(1);
5174 SDValue Shuffle = XformToShuffleWithZero(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00005175 if (Shuffle.getNode()) return Shuffle;
Evan Cheng44f1f092006-04-20 08:56:16 +00005176
Dan Gohman7f321562007-06-25 16:23:39 +00005177 // If the LHS and RHS are BUILD_VECTOR nodes, see if we can constant fold
Chris Lattneredab1b92006-04-02 03:25:57 +00005178 // this operation.
Dan Gohman7f321562007-06-25 16:23:39 +00005179 if (LHS.getOpcode() == ISD::BUILD_VECTOR &&
5180 RHS.getOpcode() == ISD::BUILD_VECTOR) {
Dan Gohman475871a2008-07-27 21:46:04 +00005181 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00005182 for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005183 SDValue LHSOp = LHS.getOperand(i);
5184 SDValue RHSOp = RHS.getOperand(i);
Chris Lattneredab1b92006-04-02 03:25:57 +00005185 // If these two elements can't be folded, bail out.
5186 if ((LHSOp.getOpcode() != ISD::UNDEF &&
5187 LHSOp.getOpcode() != ISD::Constant &&
5188 LHSOp.getOpcode() != ISD::ConstantFP) ||
5189 (RHSOp.getOpcode() != ISD::UNDEF &&
5190 RHSOp.getOpcode() != ISD::Constant &&
5191 RHSOp.getOpcode() != ISD::ConstantFP))
5192 break;
Evan Cheng7b336a82006-05-31 06:08:35 +00005193 // Can't fold divide by zero.
Dan Gohman7f321562007-06-25 16:23:39 +00005194 if (N->getOpcode() == ISD::SDIV || N->getOpcode() == ISD::UDIV ||
5195 N->getOpcode() == ISD::FDIV) {
Evan Cheng7b336a82006-05-31 06:08:35 +00005196 if ((RHSOp.getOpcode() == ISD::Constant &&
Gabor Greifba36cb52008-08-28 21:40:38 +00005197 cast<ConstantSDNode>(RHSOp.getNode())->isNullValue()) ||
Evan Cheng7b336a82006-05-31 06:08:35 +00005198 (RHSOp.getOpcode() == ISD::ConstantFP &&
Gabor Greifba36cb52008-08-28 21:40:38 +00005199 cast<ConstantFPSDNode>(RHSOp.getNode())->getValueAPF().isZero()))
Evan Cheng7b336a82006-05-31 06:08:35 +00005200 break;
5201 }
Dan Gohman7f321562007-06-25 16:23:39 +00005202 Ops.push_back(DAG.getNode(N->getOpcode(), EltType, LHSOp, RHSOp));
Gabor Greifba36cb52008-08-28 21:40:38 +00005203 AddToWorkList(Ops.back().getNode());
Chris Lattneredab1b92006-04-02 03:25:57 +00005204 assert((Ops.back().getOpcode() == ISD::UNDEF ||
5205 Ops.back().getOpcode() == ISD::Constant ||
5206 Ops.back().getOpcode() == ISD::ConstantFP) &&
5207 "Scalar binop didn't fold!");
5208 }
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00005209
Dan Gohman7f321562007-06-25 16:23:39 +00005210 if (Ops.size() == LHS.getNumOperands()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005211 MVT VT = LHS.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +00005212 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00005213 }
Chris Lattneredab1b92006-04-02 03:25:57 +00005214 }
5215
Dan Gohman475871a2008-07-27 21:46:04 +00005216 return SDValue();
Chris Lattneredab1b92006-04-02 03:25:57 +00005217}
5218
Dan Gohman475871a2008-07-27 21:46:04 +00005219SDValue DAGCombiner::SimplifySelect(SDValue N0, SDValue N1, SDValue N2){
Nate Begemanf845b452005-10-08 00:29:44 +00005220 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
5221
Dan Gohman475871a2008-07-27 21:46:04 +00005222 SDValue SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
Nate Begemanf845b452005-10-08 00:29:44 +00005223 cast<CondCodeSDNode>(N0.getOperand(2))->get());
5224 // If we got a simplified select_cc node back from SimplifySelectCC, then
5225 // break it down into a new SETCC node, and a new SELECT node, and then return
5226 // the SELECT node, since we were called with a SELECT node.
Gabor Greifba36cb52008-08-28 21:40:38 +00005227 if (SCC.getNode()) {
Nate Begemanf845b452005-10-08 00:29:44 +00005228 // Check to see if we got a select_cc back (to turn into setcc/select).
5229 // Otherwise, just return whatever node we got back, like fabs.
5230 if (SCC.getOpcode() == ISD::SELECT_CC) {
Dan Gohman475871a2008-07-27 21:46:04 +00005231 SDValue SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
Nate Begemanf845b452005-10-08 00:29:44 +00005232 SCC.getOperand(0), SCC.getOperand(1),
5233 SCC.getOperand(4));
Gabor Greifba36cb52008-08-28 21:40:38 +00005234 AddToWorkList(SETCC.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00005235 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
5236 SCC.getOperand(3), SETCC);
5237 }
5238 return SCC;
5239 }
Dan Gohman475871a2008-07-27 21:46:04 +00005240 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00005241}
5242
Chris Lattner40c62d52005-10-18 06:04:22 +00005243/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
5244/// are the two values being selected between, see if we can simplify the
Chris Lattner729c6d12006-05-27 00:43:02 +00005245/// select. Callers of this should assume that TheSelect is deleted if this
5246/// returns true. As such, they should return the appropriate thing (e.g. the
5247/// node) back to the top-level of the DAG combiner loop to avoid it being
5248/// looked at.
Chris Lattner40c62d52005-10-18 06:04:22 +00005249///
Dan Gohman475871a2008-07-27 21:46:04 +00005250bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDValue LHS,
5251 SDValue RHS) {
Chris Lattner40c62d52005-10-18 06:04:22 +00005252
5253 // If this is a select from two identical things, try to pull the operation
5254 // through the select.
5255 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
Chris Lattner40c62d52005-10-18 06:04:22 +00005256 // If this is a load and the token chain is identical, replace the select
5257 // of two loads with a load through a select of the address to load from.
5258 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
5259 // constants have been dropped into the constant pool.
Evan Cheng466685d2006-10-09 20:57:25 +00005260 if (LHS.getOpcode() == ISD::LOAD &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005261 // Do not let this transformation reduce the number of volatile loads.
5262 !cast<LoadSDNode>(LHS)->isVolatile() &&
5263 !cast<LoadSDNode>(RHS)->isVolatile() &&
Chris Lattner40c62d52005-10-18 06:04:22 +00005264 // Token chains must be identical.
Evan Cheng466685d2006-10-09 20:57:25 +00005265 LHS.getOperand(0) == RHS.getOperand(0)) {
5266 LoadSDNode *LLD = cast<LoadSDNode>(LHS);
5267 LoadSDNode *RLD = cast<LoadSDNode>(RHS);
5268
5269 // If this is an EXTLOAD, the VT's must match.
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005270 if (LLD->getMemoryVT() == RLD->getMemoryVT()) {
Evan Cheng466685d2006-10-09 20:57:25 +00005271 // FIXME: this conflates two src values, discarding one. This is not
5272 // the right thing to do, but nothing uses srcvalues now. When they do,
5273 // turn SrcValue into a list of locations.
Dan Gohman475871a2008-07-27 21:46:04 +00005274 SDValue Addr;
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005275 if (TheSelect->getOpcode() == ISD::SELECT) {
5276 // Check that the condition doesn't reach either load. If so, folding
5277 // this will induce a cycle into the DAG.
Gabor Greifba36cb52008-08-28 21:40:38 +00005278 if (!LLD->isPredecessorOf(TheSelect->getOperand(0).getNode()) &&
5279 !RLD->isPredecessorOf(TheSelect->getOperand(0).getNode())) {
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005280 Addr = DAG.getNode(ISD::SELECT, LLD->getBasePtr().getValueType(),
5281 TheSelect->getOperand(0), LLD->getBasePtr(),
5282 RLD->getBasePtr());
5283 }
5284 } else {
5285 // Check that the condition doesn't reach either load. If so, folding
5286 // this will induce a cycle into the DAG.
Gabor Greifba36cb52008-08-28 21:40:38 +00005287 if (!LLD->isPredecessorOf(TheSelect->getOperand(0).getNode()) &&
5288 !RLD->isPredecessorOf(TheSelect->getOperand(0).getNode()) &&
5289 !LLD->isPredecessorOf(TheSelect->getOperand(1).getNode()) &&
5290 !RLD->isPredecessorOf(TheSelect->getOperand(1).getNode())) {
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005291 Addr = DAG.getNode(ISD::SELECT_CC, LLD->getBasePtr().getValueType(),
Evan Cheng466685d2006-10-09 20:57:25 +00005292 TheSelect->getOperand(0),
5293 TheSelect->getOperand(1),
5294 LLD->getBasePtr(), RLD->getBasePtr(),
5295 TheSelect->getOperand(4));
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005296 }
Evan Cheng466685d2006-10-09 20:57:25 +00005297 }
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005298
Gabor Greifba36cb52008-08-28 21:40:38 +00005299 if (Addr.getNode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005300 SDValue Load;
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005301 if (LLD->getExtensionType() == ISD::NON_EXTLOAD)
5302 Load = DAG.getLoad(TheSelect->getValueType(0), LLD->getChain(),
5303 Addr,LLD->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00005304 LLD->getSrcValueOffset(),
5305 LLD->isVolatile(),
5306 LLD->getAlignment());
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005307 else {
5308 Load = DAG.getExtLoad(LLD->getExtensionType(),
5309 TheSelect->getValueType(0),
5310 LLD->getChain(), Addr, LLD->getSrcValue(),
5311 LLD->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005312 LLD->getMemoryVT(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00005313 LLD->isVolatile(),
5314 LLD->getAlignment());
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005315 }
5316 // Users of the select now use the result of the load.
5317 CombineTo(TheSelect, Load);
5318
5319 // Users of the old loads now use the new load's chain. We know the
5320 // old-load value is dead now.
Gabor Greifba36cb52008-08-28 21:40:38 +00005321 CombineTo(LHS.getNode(), Load.getValue(0), Load.getValue(1));
5322 CombineTo(RHS.getNode(), Load.getValue(0), Load.getValue(1));
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005323 return true;
5324 }
Evan Chengc5484282006-10-04 00:56:09 +00005325 }
Chris Lattner40c62d52005-10-18 06:04:22 +00005326 }
5327 }
5328
5329 return false;
5330}
5331
Dan Gohman475871a2008-07-27 21:46:04 +00005332SDValue DAGCombiner::SimplifySelectCC(SDValue N0, SDValue N1,
5333 SDValue N2, SDValue N3,
5334 ISD::CondCode CC, bool NotExtCompare) {
Nate Begemanf845b452005-10-08 00:29:44 +00005335
Duncan Sands83ec4b62008-06-06 12:08:01 +00005336 MVT VT = N2.getValueType();
Gabor Greifba36cb52008-08-28 21:40:38 +00005337 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
5338 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
5339 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00005340
5341 // Determine if the condition we're dealing with is constant
Dan Gohman475871a2008-07-27 21:46:04 +00005342 SDValue SCC = SimplifySetCC(TLI.getSetCCResultType(N0), N0, N1, CC, false);
Gabor Greifba36cb52008-08-28 21:40:38 +00005343 if (SCC.getNode()) AddToWorkList(SCC.getNode());
5344 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00005345
5346 // fold select_cc true, x, y -> x
Dan Gohman002e5d02008-03-13 22:13:53 +00005347 if (SCCC && !SCCC->isNullValue())
Nate Begemanf845b452005-10-08 00:29:44 +00005348 return N2;
5349 // fold select_cc false, x, y -> y
Dan Gohman002e5d02008-03-13 22:13:53 +00005350 if (SCCC && SCCC->isNullValue())
Nate Begemanf845b452005-10-08 00:29:44 +00005351 return N3;
5352
5353 // Check to see if we can simplify the select into an fabs node
5354 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
5355 // Allow either -0.0 or 0.0
Dale Johannesen87503a62007-08-25 22:10:57 +00005356 if (CFP->getValueAPF().isZero()) {
Nate Begemanf845b452005-10-08 00:29:44 +00005357 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
5358 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
5359 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
5360 N2 == N3.getOperand(0))
5361 return DAG.getNode(ISD::FABS, VT, N0);
5362
5363 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
5364 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
5365 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
5366 N2.getOperand(0) == N3)
5367 return DAG.getNode(ISD::FABS, VT, N3);
5368 }
5369 }
5370
5371 // Check to see if we can perform the "gzip trick", transforming
5372 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
Chris Lattnere3152e52006-09-20 06:41:35 +00005373 if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00005374 N0.getValueType().isInteger() &&
5375 N2.getValueType().isInteger() &&
Dan Gohman002e5d02008-03-13 22:13:53 +00005376 (N1C->isNullValue() || // (a < 0) ? b : 0
5377 (N1C->getAPIntValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0
Duncan Sands83ec4b62008-06-06 12:08:01 +00005378 MVT XType = N0.getValueType();
5379 MVT AType = N2.getValueType();
Duncan Sands8e4eb092008-06-08 20:54:56 +00005380 if (XType.bitsGE(AType)) {
Nate Begemanf845b452005-10-08 00:29:44 +00005381 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00005382 // single-bit constant.
Dan Gohman002e5d02008-03-13 22:13:53 +00005383 if (N2C && ((N2C->getAPIntValue() & (N2C->getAPIntValue()-1)) == 0)) {
5384 unsigned ShCtV = N2C->getAPIntValue().logBase2();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005385 ShCtV = XType.getSizeInBits()-ShCtV-1;
Dan Gohman475871a2008-07-27 21:46:04 +00005386 SDValue ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
5387 SDValue Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
Gabor Greifba36cb52008-08-28 21:40:38 +00005388 AddToWorkList(Shift.getNode());
Duncan Sands8e4eb092008-06-08 20:54:56 +00005389 if (XType.bitsGT(AType)) {
Nate Begemanf845b452005-10-08 00:29:44 +00005390 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00005391 AddToWorkList(Shift.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00005392 }
5393 return DAG.getNode(ISD::AND, AType, Shift, N2);
5394 }
Dan Gohman475871a2008-07-27 21:46:04 +00005395 SDValue Shift = DAG.getNode(ISD::SRA, XType, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005396 DAG.getConstant(XType.getSizeInBits()-1,
Nate Begemanf845b452005-10-08 00:29:44 +00005397 TLI.getShiftAmountTy()));
Gabor Greifba36cb52008-08-28 21:40:38 +00005398 AddToWorkList(Shift.getNode());
Duncan Sands8e4eb092008-06-08 20:54:56 +00005399 if (XType.bitsGT(AType)) {
Nate Begemanf845b452005-10-08 00:29:44 +00005400 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00005401 AddToWorkList(Shift.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00005402 }
5403 return DAG.getNode(ISD::AND, AType, Shift, N2);
5404 }
5405 }
Nate Begeman07ed4172005-10-10 21:26:48 +00005406
5407 // fold select C, 16, 0 -> shl C, 4
Dan Gohman002e5d02008-03-13 22:13:53 +00005408 if (N2C && N3C && N3C->isNullValue() && N2C->getAPIntValue().isPowerOf2() &&
Nate Begeman07ed4172005-10-10 21:26:48 +00005409 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) {
Chris Lattner1eba01e2007-04-11 06:50:51 +00005410
5411 // If the caller doesn't want us to simplify this into a zext of a compare,
5412 // don't do it.
Dan Gohman002e5d02008-03-13 22:13:53 +00005413 if (NotExtCompare && N2C->getAPIntValue() == 1)
Dan Gohman475871a2008-07-27 21:46:04 +00005414 return SDValue();
Chris Lattner1eba01e2007-04-11 06:50:51 +00005415
Nate Begeman07ed4172005-10-10 21:26:48 +00005416 // Get a SetCC of the condition
5417 // FIXME: Should probably make sure that setcc is legal if we ever have a
5418 // target where it isn't.
Dan Gohman475871a2008-07-27 21:46:04 +00005419 SDValue Temp, SCC;
Nate Begeman07ed4172005-10-10 21:26:48 +00005420 // cast from setcc result type to select result type
Nate Begemanb0d04a72006-02-18 02:40:58 +00005421 if (AfterLegalize) {
Scott Michel5b8f82e2008-03-10 15:42:14 +00005422 SCC = DAG.getSetCC(TLI.getSetCCResultType(N0), N0, N1, CC);
Duncan Sands8e4eb092008-06-08 20:54:56 +00005423 if (N2.getValueType().bitsLT(SCC.getValueType()))
Chris Lattner555d8d62006-12-07 22:36:47 +00005424 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
5425 else
5426 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00005427 } else {
5428 SCC = DAG.getSetCC(MVT::i1, N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00005429 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00005430 }
Gabor Greifba36cb52008-08-28 21:40:38 +00005431 AddToWorkList(SCC.getNode());
5432 AddToWorkList(Temp.getNode());
Chris Lattnerc56a81d2007-04-11 06:43:25 +00005433
Dan Gohman002e5d02008-03-13 22:13:53 +00005434 if (N2C->getAPIntValue() == 1)
Chris Lattnerc56a81d2007-04-11 06:43:25 +00005435 return Temp;
Nate Begeman07ed4172005-10-10 21:26:48 +00005436 // shl setcc result by log2 n2c
5437 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
Dan Gohman002e5d02008-03-13 22:13:53 +00005438 DAG.getConstant(N2C->getAPIntValue().logBase2(),
Nate Begeman07ed4172005-10-10 21:26:48 +00005439 TLI.getShiftAmountTy()));
5440 }
5441
Nate Begemanf845b452005-10-08 00:29:44 +00005442 // Check to see if this is the equivalent of setcc
5443 // FIXME: Turn all of these into setcc if setcc if setcc is legal
5444 // otherwise, go ahead with the folds.
Dan Gohman002e5d02008-03-13 22:13:53 +00005445 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getAPIntValue() == 1ULL)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005446 MVT XType = N0.getValueType();
Duncan Sands184a8762008-06-14 17:48:34 +00005447 if (!AfterLegalize ||
5448 TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultType(N0))) {
Dan Gohman475871a2008-07-27 21:46:04 +00005449 SDValue Res = DAG.getSetCC(TLI.getSetCCResultType(N0), N0, N1, CC);
Nate Begemanf845b452005-10-08 00:29:44 +00005450 if (Res.getValueType() != VT)
5451 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
5452 return Res;
5453 }
5454
5455 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
5456 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
Duncan Sands184a8762008-06-14 17:48:34 +00005457 (!AfterLegalize ||
5458 TLI.isOperationLegal(ISD::CTLZ, XType))) {
Dan Gohman475871a2008-07-27 21:46:04 +00005459 SDValue Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
Nate Begemanf845b452005-10-08 00:29:44 +00005460 return DAG.getNode(ISD::SRL, XType, Ctlz,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005461 DAG.getConstant(Log2_32(XType.getSizeInBits()),
Nate Begemanf845b452005-10-08 00:29:44 +00005462 TLI.getShiftAmountTy()));
5463 }
5464 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
5465 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
Dan Gohman475871a2008-07-27 21:46:04 +00005466 SDValue NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
Nate Begemanf845b452005-10-08 00:29:44 +00005467 N0);
Dan Gohman475871a2008-07-27 21:46:04 +00005468 SDValue NotN0 = DAG.getNode(ISD::XOR, XType, N0,
Nate Begemanf845b452005-10-08 00:29:44 +00005469 DAG.getConstant(~0ULL, XType));
5470 return DAG.getNode(ISD::SRL, XType,
5471 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
Duncan Sands83ec4b62008-06-06 12:08:01 +00005472 DAG.getConstant(XType.getSizeInBits()-1,
Nate Begemanf845b452005-10-08 00:29:44 +00005473 TLI.getShiftAmountTy()));
5474 }
5475 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
5476 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
Dan Gohman475871a2008-07-27 21:46:04 +00005477 SDValue Sign = DAG.getNode(ISD::SRL, XType, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005478 DAG.getConstant(XType.getSizeInBits()-1,
Nate Begemanf845b452005-10-08 00:29:44 +00005479 TLI.getShiftAmountTy()));
5480 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
5481 }
5482 }
5483
5484 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
5485 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
5486 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
Chris Lattner1982ef22007-04-11 05:11:38 +00005487 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1) &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00005488 N2.getOperand(0) == N1 && N0.getValueType().isInteger()) {
5489 MVT XType = N0.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00005490 SDValue Shift = DAG.getNode(ISD::SRA, XType, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005491 DAG.getConstant(XType.getSizeInBits()-1,
Chris Lattner1982ef22007-04-11 05:11:38 +00005492 TLI.getShiftAmountTy()));
Dan Gohman475871a2008-07-27 21:46:04 +00005493 SDValue Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00005494 AddToWorkList(Shift.getNode());
5495 AddToWorkList(Add.getNode());
Chris Lattner1982ef22007-04-11 05:11:38 +00005496 return DAG.getNode(ISD::XOR, XType, Add, Shift);
5497 }
5498 // Check to see if this is an integer abs. select_cc setgt X, -1, X, -X ->
5499 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
5500 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT &&
5501 N0 == N2 && N3.getOpcode() == ISD::SUB && N0 == N3.getOperand(1)) {
5502 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N3.getOperand(0))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005503 MVT XType = N0.getValueType();
5504 if (SubC->isNullValue() && XType.isInteger()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005505 SDValue Shift = DAG.getNode(ISD::SRA, XType, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005506 DAG.getConstant(XType.getSizeInBits()-1,
Chris Lattner1982ef22007-04-11 05:11:38 +00005507 TLI.getShiftAmountTy()));
Dan Gohman475871a2008-07-27 21:46:04 +00005508 SDValue Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00005509 AddToWorkList(Shift.getNode());
5510 AddToWorkList(Add.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00005511 return DAG.getNode(ISD::XOR, XType, Add, Shift);
5512 }
5513 }
5514 }
Chris Lattner1982ef22007-04-11 05:11:38 +00005515
Dan Gohman475871a2008-07-27 21:46:04 +00005516 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00005517}
5518
Evan Chengfa1eb272007-02-08 22:13:59 +00005519/// SimplifySetCC - This is a stub for TargetLowering::SimplifySetCC.
Dan Gohman475871a2008-07-27 21:46:04 +00005520SDValue DAGCombiner::SimplifySetCC(MVT VT, SDValue N0,
5521 SDValue N1, ISD::CondCode Cond,
5522 bool foldBooleans) {
Evan Chengfa1eb272007-02-08 22:13:59 +00005523 TargetLowering::DAGCombinerInfo
5524 DagCombineInfo(DAG, !AfterLegalize, false, this);
5525 return TLI.SimplifySetCC(VT, N0, N1, Cond, foldBooleans, DagCombineInfo);
Nate Begeman452d7be2005-09-16 00:54:12 +00005526}
5527
Nate Begeman69575232005-10-20 02:15:44 +00005528/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
5529/// return a DAG expression to select that will generate the same value by
5530/// multiplying by a magic number. See:
5531/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Dan Gohman475871a2008-07-27 21:46:04 +00005532SDValue DAGCombiner::BuildSDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00005533 std::vector<SDNode*> Built;
Dan Gohman475871a2008-07-27 21:46:04 +00005534 SDValue S = TLI.BuildSDIV(N, DAG, &Built);
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005535
Andrew Lenharth232c9102006-06-12 16:07:18 +00005536 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005537 ii != ee; ++ii)
5538 AddToWorkList(*ii);
5539 return S;
Nate Begeman69575232005-10-20 02:15:44 +00005540}
5541
5542/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
5543/// return a DAG expression to select that will generate the same value by
5544/// multiplying by a magic number. See:
5545/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Dan Gohman475871a2008-07-27 21:46:04 +00005546SDValue DAGCombiner::BuildUDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00005547 std::vector<SDNode*> Built;
Dan Gohman475871a2008-07-27 21:46:04 +00005548 SDValue S = TLI.BuildUDIV(N, DAG, &Built);
Nate Begeman69575232005-10-20 02:15:44 +00005549
Andrew Lenharth232c9102006-06-12 16:07:18 +00005550 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005551 ii != ee; ++ii)
5552 AddToWorkList(*ii);
5553 return S;
Nate Begeman69575232005-10-20 02:15:44 +00005554}
5555
Jim Laskey71382342006-10-07 23:37:56 +00005556/// FindBaseOffset - Return true if base is known not to alias with anything
5557/// but itself. Provides base object and offset as results.
Dan Gohman475871a2008-07-27 21:46:04 +00005558static bool FindBaseOffset(SDValue Ptr, SDValue &Base, int64_t &Offset) {
Jim Laskey71382342006-10-07 23:37:56 +00005559 // Assume it is a primitive operation.
5560 Base = Ptr; Offset = 0;
5561
5562 // If it's an adding a simple constant then integrate the offset.
5563 if (Base.getOpcode() == ISD::ADD) {
5564 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Base.getOperand(1))) {
5565 Base = Base.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005566 Offset += C->getZExtValue();
Jim Laskey71382342006-10-07 23:37:56 +00005567 }
5568 }
5569
5570 // If it's any of the following then it can't alias with anything but itself.
5571 return isa<FrameIndexSDNode>(Base) ||
5572 isa<ConstantPoolSDNode>(Base) ||
5573 isa<GlobalAddressSDNode>(Base);
5574}
5575
5576/// isAlias - Return true if there is any possibility that the two addresses
5577/// overlap.
Dan Gohman475871a2008-07-27 21:46:04 +00005578bool DAGCombiner::isAlias(SDValue Ptr1, int64_t Size1,
Jim Laskey096c22e2006-10-18 12:29:57 +00005579 const Value *SrcValue1, int SrcValueOffset1,
Dan Gohman475871a2008-07-27 21:46:04 +00005580 SDValue Ptr2, int64_t Size2,
Jim Laskey096c22e2006-10-18 12:29:57 +00005581 const Value *SrcValue2, int SrcValueOffset2)
5582{
Jim Laskey71382342006-10-07 23:37:56 +00005583 // If they are the same then they must be aliases.
5584 if (Ptr1 == Ptr2) return true;
5585
5586 // Gather base node and offset information.
Dan Gohman475871a2008-07-27 21:46:04 +00005587 SDValue Base1, Base2;
Jim Laskey71382342006-10-07 23:37:56 +00005588 int64_t Offset1, Offset2;
5589 bool KnownBase1 = FindBaseOffset(Ptr1, Base1, Offset1);
5590 bool KnownBase2 = FindBaseOffset(Ptr2, Base2, Offset2);
5591
5592 // If they have a same base address then...
5593 if (Base1 == Base2) {
5594 // Check to see if the addresses overlap.
5595 return!((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
5596 }
5597
Jim Laskey096c22e2006-10-18 12:29:57 +00005598 // If we know both bases then they can't alias.
5599 if (KnownBase1 && KnownBase2) return false;
5600
Jim Laskey07a27092006-10-18 19:08:31 +00005601 if (CombinerGlobalAA) {
5602 // Use alias analysis information.
Dan Gohmane9c8fa02007-08-27 16:32:11 +00005603 int64_t MinOffset = std::min(SrcValueOffset1, SrcValueOffset2);
5604 int64_t Overlap1 = Size1 + SrcValueOffset1 - MinOffset;
5605 int64_t Overlap2 = Size2 + SrcValueOffset2 - MinOffset;
Jim Laskey07a27092006-10-18 19:08:31 +00005606 AliasAnalysis::AliasResult AAResult =
Jim Laskey096c22e2006-10-18 12:29:57 +00005607 AA.alias(SrcValue1, Overlap1, SrcValue2, Overlap2);
Jim Laskey07a27092006-10-18 19:08:31 +00005608 if (AAResult == AliasAnalysis::NoAlias)
5609 return false;
5610 }
Jim Laskey096c22e2006-10-18 12:29:57 +00005611
5612 // Otherwise we have to assume they alias.
5613 return true;
Jim Laskey71382342006-10-07 23:37:56 +00005614}
5615
5616/// FindAliasInfo - Extracts the relevant alias information from the memory
5617/// node. Returns true if the operand was a load.
Jim Laskey7ca56af2006-10-11 13:47:09 +00005618bool DAGCombiner::FindAliasInfo(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +00005619 SDValue &Ptr, int64_t &Size,
Jim Laskey096c22e2006-10-18 12:29:57 +00005620 const Value *&SrcValue, int &SrcValueOffset) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00005621 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
5622 Ptr = LD->getBasePtr();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005623 Size = LD->getMemoryVT().getSizeInBits() >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005624 SrcValue = LD->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00005625 SrcValueOffset = LD->getSrcValueOffset();
Jim Laskey71382342006-10-07 23:37:56 +00005626 return true;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005627 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00005628 Ptr = ST->getBasePtr();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005629 Size = ST->getMemoryVT().getSizeInBits() >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005630 SrcValue = ST->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00005631 SrcValueOffset = ST->getSrcValueOffset();
Jim Laskey7ca56af2006-10-11 13:47:09 +00005632 } else {
Jim Laskey71382342006-10-07 23:37:56 +00005633 assert(0 && "FindAliasInfo expected a memory operand");
Jim Laskey71382342006-10-07 23:37:56 +00005634 }
5635
5636 return false;
5637}
5638
Jim Laskey6ff23e52006-10-04 16:53:27 +00005639/// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
5640/// looking for aliasing nodes and adding them to the Aliases vector.
Dan Gohman475871a2008-07-27 21:46:04 +00005641void DAGCombiner::GatherAllAliases(SDNode *N, SDValue OriginalChain,
5642 SmallVector<SDValue, 8> &Aliases) {
5643 SmallVector<SDValue, 8> Chains; // List of chains to visit.
Jim Laskey6ff23e52006-10-04 16:53:27 +00005644 std::set<SDNode *> Visited; // Visited node set.
5645
Jim Laskey279f0532006-09-25 16:29:54 +00005646 // Get alias information for node.
Dan Gohman475871a2008-07-27 21:46:04 +00005647 SDValue Ptr;
Jim Laskey279f0532006-09-25 16:29:54 +00005648 int64_t Size;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005649 const Value *SrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00005650 int SrcValueOffset;
5651 bool IsLoad = FindAliasInfo(N, Ptr, Size, SrcValue, SrcValueOffset);
Jim Laskey279f0532006-09-25 16:29:54 +00005652
Jim Laskey6ff23e52006-10-04 16:53:27 +00005653 // Starting off.
Jim Laskeybc588b82006-10-05 15:07:25 +00005654 Chains.push_back(OriginalChain);
Jim Laskey6ff23e52006-10-04 16:53:27 +00005655
Jim Laskeybc588b82006-10-05 15:07:25 +00005656 // Look at each chain and determine if it is an alias. If so, add it to the
5657 // aliases list. If not, then continue up the chain looking for the next
5658 // candidate.
5659 while (!Chains.empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005660 SDValue Chain = Chains.back();
Jim Laskeybc588b82006-10-05 15:07:25 +00005661 Chains.pop_back();
Jim Laskey6ff23e52006-10-04 16:53:27 +00005662
Jim Laskeybc588b82006-10-05 15:07:25 +00005663 // Don't bother if we've been before.
Gabor Greifba36cb52008-08-28 21:40:38 +00005664 if (Visited.find(Chain.getNode()) != Visited.end()) continue;
5665 Visited.insert(Chain.getNode());
Jim Laskeybc588b82006-10-05 15:07:25 +00005666
5667 switch (Chain.getOpcode()) {
5668 case ISD::EntryToken:
5669 // Entry token is ideal chain operand, but handled in FindBetterChain.
5670 break;
Jim Laskey6ff23e52006-10-04 16:53:27 +00005671
Jim Laskeybc588b82006-10-05 15:07:25 +00005672 case ISD::LOAD:
5673 case ISD::STORE: {
5674 // Get alias information for Chain.
Dan Gohman475871a2008-07-27 21:46:04 +00005675 SDValue OpPtr;
Jim Laskeybc588b82006-10-05 15:07:25 +00005676 int64_t OpSize;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005677 const Value *OpSrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00005678 int OpSrcValueOffset;
Gabor Greifba36cb52008-08-28 21:40:38 +00005679 bool IsOpLoad = FindAliasInfo(Chain.getNode(), OpPtr, OpSize,
Jim Laskey096c22e2006-10-18 12:29:57 +00005680 OpSrcValue, OpSrcValueOffset);
Jim Laskeybc588b82006-10-05 15:07:25 +00005681
5682 // If chain is alias then stop here.
5683 if (!(IsLoad && IsOpLoad) &&
Jim Laskey096c22e2006-10-18 12:29:57 +00005684 isAlias(Ptr, Size, SrcValue, SrcValueOffset,
5685 OpPtr, OpSize, OpSrcValue, OpSrcValueOffset)) {
Jim Laskeybc588b82006-10-05 15:07:25 +00005686 Aliases.push_back(Chain);
5687 } else {
5688 // Look further up the chain.
5689 Chains.push_back(Chain.getOperand(0));
5690 // Clean up old chain.
Gabor Greifba36cb52008-08-28 21:40:38 +00005691 AddToWorkList(Chain.getNode());
Jim Laskey279f0532006-09-25 16:29:54 +00005692 }
Jim Laskeybc588b82006-10-05 15:07:25 +00005693 break;
5694 }
5695
5696 case ISD::TokenFactor:
5697 // We have to check each of the operands of the token factor, so we queue
5698 // then up. Adding the operands to the queue (stack) in reverse order
5699 // maintains the original order and increases the likelihood that getNode
5700 // will find a matching token factor (CSE.)
5701 for (unsigned n = Chain.getNumOperands(); n;)
5702 Chains.push_back(Chain.getOperand(--n));
5703 // Eliminate the token factor if we can.
Gabor Greifba36cb52008-08-28 21:40:38 +00005704 AddToWorkList(Chain.getNode());
Jim Laskeybc588b82006-10-05 15:07:25 +00005705 break;
5706
5707 default:
5708 // For all other instructions we will just have to take what we can get.
5709 Aliases.push_back(Chain);
5710 break;
Jim Laskey279f0532006-09-25 16:29:54 +00005711 }
5712 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00005713}
5714
5715/// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, looking
5716/// for a better chain (aliasing node.)
Dan Gohman475871a2008-07-27 21:46:04 +00005717SDValue DAGCombiner::FindBetterChain(SDNode *N, SDValue OldChain) {
5718 SmallVector<SDValue, 8> Aliases; // Ops for replacing token factor.
Jim Laskey279f0532006-09-25 16:29:54 +00005719
Jim Laskey6ff23e52006-10-04 16:53:27 +00005720 // Accumulate all the aliases to this node.
5721 GatherAllAliases(N, OldChain, Aliases);
5722
5723 if (Aliases.size() == 0) {
5724 // If no operands then chain to entry token.
5725 return DAG.getEntryNode();
5726 } else if (Aliases.size() == 1) {
5727 // If a single operand then chain to it. We don't need to revisit it.
5728 return Aliases[0];
5729 }
5730
5731 // Construct a custom tailored token factor.
Dan Gohman475871a2008-07-27 21:46:04 +00005732 SDValue NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
Jim Laskey6ff23e52006-10-04 16:53:27 +00005733 &Aliases[0], Aliases.size());
5734
5735 // Make sure the old chain gets cleaned up.
Gabor Greifba36cb52008-08-28 21:40:38 +00005736 if (NewChain != OldChain) AddToWorkList(OldChain.getNode());
Jim Laskey6ff23e52006-10-04 16:53:27 +00005737
5738 return NewChain;
Jim Laskey279f0532006-09-25 16:29:54 +00005739}
5740
Nate Begeman1d4d4142005-09-01 00:19:25 +00005741// SelectionDAG::Combine - This is the entry point for the file.
5742//
Dan Gohmana2676512008-08-20 16:30:28 +00005743void SelectionDAG::Combine(bool RunningAfterLegalize, AliasAnalysis &AA,
5744 bool Fast) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00005745 /// run - This is the main entry point to this class.
5746 ///
Dan Gohmana2676512008-08-20 16:30:28 +00005747 DAGCombiner(*this, AA, Fast).Run(RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +00005748}