blob: ab27925189e49853275e70e6cbc27e5c3eea0adc [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)
Nate Begemana148d982006-01-18 22:35:16 +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 Greifba36cb52008-08-28 21:40:38 +00001199 isa<ConstantSDNode>(N1.getOperand(1)) && N1.getNode()->hasOneUse()) {
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001200 Sh = N1; Y = N0;
1201 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001202 if (Sh.getNode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001203 SDValue Mul = DAG.getNode(ISD::MUL, VT, Sh.getOperand(0), Y);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001204 return DAG.getNode(ISD::SHL, VT, Mul, Sh.getOperand(1));
1205 }
1206 }
Chris Lattnera1deca32006-03-04 23:33:26 +00001207 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
Gabor Greifba36cb52008-08-28 21:40:38 +00001208 if (N1C && N0.getOpcode() == ISD::ADD && N0.getNode()->hasOneUse() &&
Chris Lattnera1deca32006-03-04 23:33:26 +00001209 isa<ConstantSDNode>(N0.getOperand(1))) {
1210 return DAG.getNode(ISD::ADD, VT,
1211 DAG.getNode(ISD::MUL, VT, N0.getOperand(0), N1),
1212 DAG.getNode(ISD::MUL, VT, N0.getOperand(1), N1));
1213 }
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001214
Nate Begemancd4d58c2006-02-03 06:46:56 +00001215 // reassociate mul
Dan Gohman475871a2008-07-27 21:46:04 +00001216 SDValue RMUL = ReassociateOps(ISD::MUL, N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001217 if (RMUL.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001218 return RMUL;
Dan Gohman7f321562007-06-25 16:23:39 +00001219
Dan Gohman475871a2008-07-27 21:46:04 +00001220 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001221}
1222
Dan Gohman475871a2008-07-27 21:46:04 +00001223SDValue DAGCombiner::visitSDIV(SDNode *N) {
1224 SDValue N0 = N->getOperand(0);
1225 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001226 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1227 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Duncan Sands83ec4b62008-06-06 12:08:01 +00001228 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001229
Dan Gohman7f321562007-06-25 16:23:39 +00001230 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001231 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001232 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001233 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001234 }
Dan Gohman7f321562007-06-25 16:23:39 +00001235
Nate Begeman1d4d4142005-09-01 00:19:25 +00001236 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001237 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001238 return DAG.getNode(ISD::SDIV, VT, N0, N1);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001239 // fold (sdiv X, 1) -> X
1240 if (N1C && N1C->getSignExtended() == 1LL)
1241 return N0;
1242 // fold (sdiv X, -1) -> 0-X
1243 if (N1C && N1C->isAllOnesValue())
1244 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +00001245 // If we know the sign bits of both operands are zero, strength reduce to a
1246 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
Duncan Sands83ec4b62008-06-06 12:08:01 +00001247 if (!VT.isVector()) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001248 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Chris Lattnerf32aac32008-01-27 23:32:17 +00001249 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
1250 }
Nate Begemancd6a6ed2006-02-17 07:26:20 +00001251 // fold (sdiv X, pow2) -> simple ops after legalize
Dan Gohman002e5d02008-03-13 22:13:53 +00001252 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap() &&
Nate Begeman405e3ec2005-10-21 00:02:42 +00001253 (isPowerOf2_64(N1C->getSignExtended()) ||
1254 isPowerOf2_64(-N1C->getSignExtended()))) {
1255 // If dividing by powers of two is cheap, then don't perform the following
1256 // fold.
1257 if (TLI.isPow2DivCheap())
Dan Gohman475871a2008-07-27 21:46:04 +00001258 return SDValue();
Nate Begeman405e3ec2005-10-21 00:02:42 +00001259 int64_t pow2 = N1C->getSignExtended();
1260 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
Chris Lattner8f4880b2006-02-16 08:02:36 +00001261 unsigned lg2 = Log2_64(abs2);
1262 // Splat the sign bit into the register
Dan Gohman475871a2008-07-27 21:46:04 +00001263 SDValue SGN = DAG.getNode(ISD::SRA, VT, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001264 DAG.getConstant(VT.getSizeInBits()-1,
Nate Begeman405e3ec2005-10-21 00:02:42 +00001265 TLI.getShiftAmountTy()));
Gabor Greifba36cb52008-08-28 21:40:38 +00001266 AddToWorkList(SGN.getNode());
Chris Lattner8f4880b2006-02-16 08:02:36 +00001267 // Add (N0 < 0) ? abs2 - 1 : 0;
Dan Gohman475871a2008-07-27 21:46:04 +00001268 SDValue SRL = DAG.getNode(ISD::SRL, VT, SGN,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001269 DAG.getConstant(VT.getSizeInBits()-lg2,
Nate Begeman405e3ec2005-10-21 00:02:42 +00001270 TLI.getShiftAmountTy()));
Dan Gohman475871a2008-07-27 21:46:04 +00001271 SDValue ADD = DAG.getNode(ISD::ADD, VT, N0, SRL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001272 AddToWorkList(SRL.getNode());
1273 AddToWorkList(ADD.getNode()); // Divide by pow2
Dan Gohman475871a2008-07-27 21:46:04 +00001274 SDValue SRA = DAG.getNode(ISD::SRA, VT, ADD,
Chris Lattner8f4880b2006-02-16 08:02:36 +00001275 DAG.getConstant(lg2, TLI.getShiftAmountTy()));
Nate Begeman405e3ec2005-10-21 00:02:42 +00001276 // If we're dividing by a positive value, we're done. Otherwise, we must
1277 // negate the result.
1278 if (pow2 > 0)
1279 return SRA;
Gabor Greifba36cb52008-08-28 21:40:38 +00001280 AddToWorkList(SRA.getNode());
Nate Begeman405e3ec2005-10-21 00:02:42 +00001281 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), SRA);
1282 }
Nate Begeman69575232005-10-20 02:15:44 +00001283 // if integer divide is expensive and we satisfy the requirements, emit an
1284 // alternate sequence.
Nate Begeman405e3ec2005-10-21 00:02:42 +00001285 if (N1C && (N1C->getSignExtended() < -1 || N1C->getSignExtended() > 1) &&
Chris Lattnere9936d12005-10-22 18:50:15 +00001286 !TLI.isIntDivCheap()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001287 SDValue Op = BuildSDIV(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001288 if (Op.getNode()) return Op;
Nate Begeman69575232005-10-20 02:15:44 +00001289 }
Dan Gohman7f321562007-06-25 16:23:39 +00001290
Dan Gohman613e0d82007-07-03 14:03:57 +00001291 // undef / X -> 0
1292 if (N0.getOpcode() == ISD::UNDEF)
1293 return DAG.getConstant(0, VT);
1294 // X / undef -> undef
1295 if (N1.getOpcode() == ISD::UNDEF)
1296 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001297
Dan Gohman475871a2008-07-27 21:46:04 +00001298 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001299}
1300
Dan Gohman475871a2008-07-27 21:46:04 +00001301SDValue DAGCombiner::visitUDIV(SDNode *N) {
1302 SDValue N0 = N->getOperand(0);
1303 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001304 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1305 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Duncan Sands83ec4b62008-06-06 12:08:01 +00001306 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001307
Dan Gohman7f321562007-06-25 16:23:39 +00001308 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001309 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001310 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001311 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001312 }
Dan Gohman7f321562007-06-25 16:23:39 +00001313
Nate Begeman1d4d4142005-09-01 00:19:25 +00001314 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001315 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001316 return DAG.getNode(ISD::UDIV, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001317 // fold (udiv x, (1 << c)) -> x >>u c
Dan Gohman002e5d02008-03-13 22:13:53 +00001318 if (N1C && N1C->getAPIntValue().isPowerOf2())
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001319 return DAG.getNode(ISD::SRL, VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001320 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001321 TLI.getShiftAmountTy()));
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001322 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
1323 if (N1.getOpcode() == ISD::SHL) {
1324 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00001325 if (SHC->getAPIntValue().isPowerOf2()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001326 MVT ADDVT = N1.getOperand(1).getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00001327 SDValue Add = DAG.getNode(ISD::ADD, ADDVT, N1.getOperand(1),
Dan Gohman002e5d02008-03-13 22:13:53 +00001328 DAG.getConstant(SHC->getAPIntValue()
1329 .logBase2(),
Nate Begemanc031e332006-02-05 07:36:48 +00001330 ADDVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00001331 AddToWorkList(Add.getNode());
Nate Begemanc031e332006-02-05 07:36:48 +00001332 return DAG.getNode(ISD::SRL, VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001333 }
1334 }
1335 }
Nate Begeman69575232005-10-20 02:15:44 +00001336 // fold (udiv x, c) -> alternate
Dan Gohman002e5d02008-03-13 22:13:53 +00001337 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001338 SDValue Op = BuildUDIV(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001339 if (Op.getNode()) return Op;
Chris Lattnere9936d12005-10-22 18:50:15 +00001340 }
Dan Gohman7f321562007-06-25 16:23:39 +00001341
Dan Gohman613e0d82007-07-03 14:03:57 +00001342 // undef / X -> 0
1343 if (N0.getOpcode() == ISD::UNDEF)
1344 return DAG.getConstant(0, VT);
1345 // X / undef -> undef
1346 if (N1.getOpcode() == ISD::UNDEF)
1347 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001348
Dan Gohman475871a2008-07-27 21:46:04 +00001349 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001350}
1351
Dan Gohman475871a2008-07-27 21:46:04 +00001352SDValue DAGCombiner::visitSREM(SDNode *N) {
1353 SDValue N0 = N->getOperand(0);
1354 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001355 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1356 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001357 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001358
1359 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001360 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001361 return DAG.getNode(ISD::SREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +00001362 // If we know the sign bits of both operands are zero, strength reduce to a
1363 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
Duncan Sands83ec4b62008-06-06 12:08:01 +00001364 if (!VT.isVector()) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001365 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Chris Lattneree339f42008-01-27 23:21:58 +00001366 return DAG.getNode(ISD::UREM, VT, N0, N1);
1367 }
Chris Lattner26d29902006-10-12 20:58:32 +00001368
Dan Gohman77003042007-11-26 23:46:11 +00001369 // If X/C can be simplified by the division-by-constant logic, lower
1370 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00001371 if (N1C && !N1C->isNullValue()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001372 SDValue Div = DAG.getNode(ISD::SDIV, VT, N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001373 AddToWorkList(Div.getNode());
1374 SDValue OptimizedDiv = combine(Div.getNode());
1375 if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001376 SDValue Mul = DAG.getNode(ISD::MUL, VT, OptimizedDiv, N1);
1377 SDValue Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
Gabor Greifba36cb52008-08-28 21:40:38 +00001378 AddToWorkList(Mul.getNode());
Dan Gohman77003042007-11-26 23:46:11 +00001379 return Sub;
1380 }
Chris Lattner26d29902006-10-12 20:58:32 +00001381 }
1382
Dan Gohman613e0d82007-07-03 14:03:57 +00001383 // undef % X -> 0
1384 if (N0.getOpcode() == ISD::UNDEF)
1385 return DAG.getConstant(0, VT);
1386 // X % undef -> undef
1387 if (N1.getOpcode() == ISD::UNDEF)
1388 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001389
Dan Gohman475871a2008-07-27 21:46:04 +00001390 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001391}
1392
Dan Gohman475871a2008-07-27 21:46:04 +00001393SDValue DAGCombiner::visitUREM(SDNode *N) {
1394 SDValue N0 = N->getOperand(0);
1395 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001396 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1397 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001398 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001399
1400 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001401 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001402 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +00001403 // fold (urem x, pow2) -> (and x, pow2-1)
Dan Gohman002e5d02008-03-13 22:13:53 +00001404 if (N1C && !N1C->isNullValue() && N1C->getAPIntValue().isPowerOf2())
1405 return DAG.getNode(ISD::AND, VT, N0,
1406 DAG.getConstant(N1C->getAPIntValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +00001407 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
1408 if (N1.getOpcode() == ISD::SHL) {
1409 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00001410 if (SHC->getAPIntValue().isPowerOf2()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001411 SDValue Add =
Dan Gohman002e5d02008-03-13 22:13:53 +00001412 DAG.getNode(ISD::ADD, VT, N1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001413 DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()),
Dan Gohman002e5d02008-03-13 22:13:53 +00001414 VT));
Gabor Greifba36cb52008-08-28 21:40:38 +00001415 AddToWorkList(Add.getNode());
Nate Begemanc031e332006-02-05 07:36:48 +00001416 return DAG.getNode(ISD::AND, VT, N0, Add);
1417 }
1418 }
1419 }
Chris Lattner26d29902006-10-12 20:58:32 +00001420
Dan Gohman77003042007-11-26 23:46:11 +00001421 // If X/C can be simplified by the division-by-constant logic, lower
1422 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00001423 if (N1C && !N1C->isNullValue()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001424 SDValue Div = DAG.getNode(ISD::UDIV, VT, N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001425 SDValue OptimizedDiv = combine(Div.getNode());
1426 if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001427 SDValue Mul = DAG.getNode(ISD::MUL, VT, OptimizedDiv, N1);
1428 SDValue Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
Gabor Greifba36cb52008-08-28 21:40:38 +00001429 AddToWorkList(Mul.getNode());
Dan Gohman77003042007-11-26 23:46:11 +00001430 return Sub;
1431 }
Chris Lattner26d29902006-10-12 20:58:32 +00001432 }
1433
Dan Gohman613e0d82007-07-03 14:03:57 +00001434 // undef % X -> 0
1435 if (N0.getOpcode() == ISD::UNDEF)
1436 return DAG.getConstant(0, VT);
1437 // X % undef -> undef
1438 if (N1.getOpcode() == ISD::UNDEF)
1439 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001440
Dan Gohman475871a2008-07-27 21:46:04 +00001441 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001442}
1443
Dan Gohman475871a2008-07-27 21:46:04 +00001444SDValue DAGCombiner::visitMULHS(SDNode *N) {
1445 SDValue N0 = N->getOperand(0);
1446 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001447 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001448 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001449
1450 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001451 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001452 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001453 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Dan Gohman002e5d02008-03-13 22:13:53 +00001454 if (N1C && N1C->getAPIntValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001455 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001456 DAG.getConstant(N0.getValueType().getSizeInBits()-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001457 TLI.getShiftAmountTy()));
Dan Gohman613e0d82007-07-03 14:03:57 +00001458 // fold (mulhs x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001459 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001460 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00001461
Dan Gohman475871a2008-07-27 21:46:04 +00001462 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001463}
1464
Dan Gohman475871a2008-07-27 21:46:04 +00001465SDValue DAGCombiner::visitMULHU(SDNode *N) {
1466 SDValue N0 = N->getOperand(0);
1467 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001468 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001469 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001470
1471 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001472 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001473 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001474 // fold (mulhu x, 1) -> 0
Dan Gohman002e5d02008-03-13 22:13:53 +00001475 if (N1C && N1C->getAPIntValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001476 return DAG.getConstant(0, N0.getValueType());
Dan Gohman613e0d82007-07-03 14:03:57 +00001477 // fold (mulhu x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001478 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001479 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00001480
Dan Gohman475871a2008-07-27 21:46:04 +00001481 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001482}
1483
Dan Gohman389079b2007-10-08 17:57:15 +00001484/// SimplifyNodeWithTwoResults - Perform optimizations common to nodes that
1485/// compute two values. LoOp and HiOp give the opcodes for the two computations
1486/// that are being performed. Return true if a simplification was made.
1487///
Dan Gohman475871a2008-07-27 21:46:04 +00001488SDValue DAGCombiner::SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
1489 unsigned HiOp) {
Dan Gohman389079b2007-10-08 17:57:15 +00001490 // If the high half is not needed, just compute the low half.
Evan Cheng44711942007-11-08 09:25:29 +00001491 bool HiExists = N->hasAnyUseOfValue(1);
1492 if (!HiExists &&
Dan Gohman389079b2007-10-08 17:57:15 +00001493 (!AfterLegalize ||
1494 TLI.isOperationLegal(LoOp, N->getValueType(0)))) {
Dan Gohman475871a2008-07-27 21:46:04 +00001495 SDValue Res = DAG.getNode(LoOp, N->getValueType(0), N->op_begin(),
Chris Lattner5eee4272008-01-26 01:09:19 +00001496 N->getNumOperands());
1497 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00001498 }
1499
1500 // If the low half is not needed, just compute the high half.
Evan Cheng44711942007-11-08 09:25:29 +00001501 bool LoExists = N->hasAnyUseOfValue(0);
1502 if (!LoExists &&
Dan Gohman389079b2007-10-08 17:57:15 +00001503 (!AfterLegalize ||
1504 TLI.isOperationLegal(HiOp, N->getValueType(1)))) {
Dan Gohman475871a2008-07-27 21:46:04 +00001505 SDValue Res = DAG.getNode(HiOp, N->getValueType(1), N->op_begin(),
Chris Lattner5eee4272008-01-26 01:09:19 +00001506 N->getNumOperands());
1507 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00001508 }
1509
Evan Cheng44711942007-11-08 09:25:29 +00001510 // If both halves are used, return as it is.
1511 if (LoExists && HiExists)
Dan Gohman475871a2008-07-27 21:46:04 +00001512 return SDValue();
Evan Cheng44711942007-11-08 09:25:29 +00001513
1514 // If the two computed results can be simplified separately, separate them.
Evan Cheng44711942007-11-08 09:25:29 +00001515 if (LoExists) {
Dan Gohman475871a2008-07-27 21:46:04 +00001516 SDValue Lo = DAG.getNode(LoOp, N->getValueType(0),
Evan Cheng44711942007-11-08 09:25:29 +00001517 N->op_begin(), N->getNumOperands());
Gabor Greifba36cb52008-08-28 21:40:38 +00001518 AddToWorkList(Lo.getNode());
1519 SDValue LoOpt = combine(Lo.getNode());
1520 if (LoOpt.getNode() && LoOpt.getNode() != Lo.getNode() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001521 (!AfterLegalize ||
1522 TLI.isOperationLegal(LoOpt.getOpcode(), LoOpt.getValueType())))
Chris Lattner5eee4272008-01-26 01:09:19 +00001523 return CombineTo(N, LoOpt, LoOpt);
Dan Gohman389079b2007-10-08 17:57:15 +00001524 }
1525
Evan Cheng44711942007-11-08 09:25:29 +00001526 if (HiExists) {
Dan Gohman475871a2008-07-27 21:46:04 +00001527 SDValue Hi = DAG.getNode(HiOp, N->getValueType(1),
Evan Cheng44711942007-11-08 09:25:29 +00001528 N->op_begin(), N->getNumOperands());
Gabor Greifba36cb52008-08-28 21:40:38 +00001529 AddToWorkList(Hi.getNode());
1530 SDValue HiOpt = combine(Hi.getNode());
1531 if (HiOpt.getNode() && HiOpt != Hi &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001532 (!AfterLegalize ||
1533 TLI.isOperationLegal(HiOpt.getOpcode(), HiOpt.getValueType())))
Chris Lattner5eee4272008-01-26 01:09:19 +00001534 return CombineTo(N, HiOpt, HiOpt);
Evan Cheng44711942007-11-08 09:25:29 +00001535 }
Dan Gohman475871a2008-07-27 21:46:04 +00001536 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001537}
1538
Dan Gohman475871a2008-07-27 21:46:04 +00001539SDValue DAGCombiner::visitSMUL_LOHI(SDNode *N) {
1540 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHS);
Gabor Greifba36cb52008-08-28 21:40:38 +00001541 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001542
Dan Gohman475871a2008-07-27 21:46:04 +00001543 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001544}
1545
Dan Gohman475871a2008-07-27 21:46:04 +00001546SDValue DAGCombiner::visitUMUL_LOHI(SDNode *N) {
1547 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHU);
Gabor Greifba36cb52008-08-28 21:40:38 +00001548 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001549
Dan Gohman475871a2008-07-27 21:46:04 +00001550 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001551}
1552
Dan Gohman475871a2008-07-27 21:46:04 +00001553SDValue DAGCombiner::visitSDIVREM(SDNode *N) {
1554 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::SDIV, ISD::SREM);
Gabor Greifba36cb52008-08-28 21:40:38 +00001555 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001556
Dan Gohman475871a2008-07-27 21:46:04 +00001557 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001558}
1559
Dan Gohman475871a2008-07-27 21:46:04 +00001560SDValue DAGCombiner::visitUDIVREM(SDNode *N) {
1561 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::UDIV, ISD::UREM);
Gabor Greifba36cb52008-08-28 21:40:38 +00001562 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001563
Dan Gohman475871a2008-07-27 21:46:04 +00001564 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001565}
1566
Chris Lattner35e5c142006-05-05 05:51:50 +00001567/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
1568/// two operands of the same opcode, try to simplify it.
Dan Gohman475871a2008-07-27 21:46:04 +00001569SDValue DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
1570 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001571 MVT VT = N0.getValueType();
Chris Lattner35e5c142006-05-05 05:51:50 +00001572 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
1573
Chris Lattner540121f2006-05-05 06:31:05 +00001574 // For each of OP in AND/OR/XOR:
1575 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
1576 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
1577 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
Chris Lattner0d8dae72006-05-05 06:32:04 +00001578 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y))
Chris Lattner540121f2006-05-05 06:31:05 +00001579 if ((N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND||
Chris Lattner0d8dae72006-05-05 06:32:04 +00001580 N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::TRUNCATE) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001581 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001582 SDValue ORNode = DAG.getNode(N->getOpcode(),
Chris Lattner35e5c142006-05-05 05:51:50 +00001583 N0.getOperand(0).getValueType(),
1584 N0.getOperand(0), N1.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00001585 AddToWorkList(ORNode.getNode());
Chris Lattner540121f2006-05-05 06:31:05 +00001586 return DAG.getNode(N0.getOpcode(), VT, ORNode);
Chris Lattner35e5c142006-05-05 05:51:50 +00001587 }
1588
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001589 // For each of OP in SHL/SRL/SRA/AND...
1590 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
1591 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
1592 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
Chris Lattner35e5c142006-05-05 05:51:50 +00001593 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001594 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001595 N0.getOperand(1) == N1.getOperand(1)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001596 SDValue ORNode = DAG.getNode(N->getOpcode(),
Chris Lattner35e5c142006-05-05 05:51:50 +00001597 N0.getOperand(0).getValueType(),
1598 N0.getOperand(0), N1.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00001599 AddToWorkList(ORNode.getNode());
Chris Lattner35e5c142006-05-05 05:51:50 +00001600 return DAG.getNode(N0.getOpcode(), VT, ORNode, N0.getOperand(1));
1601 }
1602
Dan Gohman475871a2008-07-27 21:46:04 +00001603 return SDValue();
Chris Lattner35e5c142006-05-05 05:51:50 +00001604}
1605
Dan Gohman475871a2008-07-27 21:46:04 +00001606SDValue DAGCombiner::visitAND(SDNode *N) {
1607 SDValue N0 = N->getOperand(0);
1608 SDValue N1 = N->getOperand(1);
1609 SDValue LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001610 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1611 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001612 MVT VT = N1.getValueType();
1613 unsigned BitWidth = VT.getSizeInBits();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001614
Dan Gohman7f321562007-06-25 16:23:39 +00001615 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001616 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001617 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001618 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001619 }
Dan Gohman7f321562007-06-25 16:23:39 +00001620
Dan Gohman613e0d82007-07-03 14:03:57 +00001621 // fold (and x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001622 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001623 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001624 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001625 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001626 return DAG.getNode(ISD::AND, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001627 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001628 if (N0C && !N1C)
1629 return DAG.getNode(ISD::AND, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001630 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001631 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001632 return N0;
1633 // if (and x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00001634 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001635 APInt::getAllOnesValue(BitWidth)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001636 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +00001637 // reassociate and
Dan Gohman475871a2008-07-27 21:46:04 +00001638 SDValue RAND = ReassociateOps(ISD::AND, N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001639 if (RAND.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001640 return RAND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001641 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
Nate Begeman5dc7e862005-11-02 18:42:59 +00001642 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001643 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohman002e5d02008-03-13 22:13:53 +00001644 if ((ORI->getAPIntValue() & N1C->getAPIntValue()) == N1C->getAPIntValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001645 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +00001646 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
1647 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Dan Gohman475871a2008-07-27 21:46:04 +00001648 SDValue N0Op0 = N0.getOperand(0);
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001649 APInt Mask = ~N1C->getAPIntValue();
1650 Mask.trunc(N0Op0.getValueSizeInBits());
1651 if (DAG.MaskedValueIsZero(N0Op0, Mask)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001652 SDValue Zext = DAG.getNode(ISD::ZERO_EXTEND, N0.getValueType(),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001653 N0Op0);
Chris Lattner1ec05d12006-03-01 21:47:21 +00001654
1655 // Replace uses of the AND with uses of the Zero extend node.
1656 CombineTo(N, Zext);
1657
Chris Lattner3603cd62006-02-02 07:17:31 +00001658 // We actually want to replace all uses of the any_extend with the
1659 // zero_extend, to avoid duplicating things. This will later cause this
1660 // AND to be folded.
Gabor Greifba36cb52008-08-28 21:40:38 +00001661 CombineTo(N0.getNode(), Zext);
Dan Gohman475871a2008-07-27 21:46:04 +00001662 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner3603cd62006-02-02 07:17:31 +00001663 }
1664 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001665 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
1666 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1667 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1668 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1669
1670 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00001671 LL.getValueType().isInteger()) {
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001672 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
Dan Gohman002e5d02008-03-13 22:13:53 +00001673 if (cast<ConstantSDNode>(LR)->isNullValue() && Op1 == ISD::SETEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00001674 SDValue ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001675 AddToWorkList(ORNode.getNode());
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001676 return DAG.getSetCC(VT, ORNode, LR, Op1);
1677 }
1678 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
1679 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00001680 SDValue ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001681 AddToWorkList(ANDNode.getNode());
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001682 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1683 }
1684 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
1685 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
Dan Gohman475871a2008-07-27 21:46:04 +00001686 SDValue ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001687 AddToWorkList(ORNode.getNode());
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001688 return DAG.getSetCC(VT, ORNode, LR, Op1);
1689 }
1690 }
1691 // canonicalize equivalent to ll == rl
1692 if (LL == RR && LR == RL) {
1693 Op1 = ISD::getSetCCSwappedOperands(Op1);
1694 std::swap(RL, RR);
1695 }
1696 if (LL == RL && LR == RR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001697 bool isInteger = LL.getValueType().isInteger();
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001698 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
1699 if (Result != ISD::SETCC_INVALID)
1700 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1701 }
1702 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001703
1704 // Simplify: and (op x...), (op y...) -> (op (and x, y))
1705 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001706 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001707 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001708 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001709
Nate Begemande996292006-02-03 22:24:05 +00001710 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
1711 // fold (and (sra)) -> (and (srl)) when possible.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001712 if (!VT.isVector() &&
Dan Gohman475871a2008-07-27 21:46:04 +00001713 SimplifyDemandedBits(SDValue(N, 0)))
1714 return SDValue(N, 0);
Nate Begemanded49632005-10-13 03:11:28 +00001715 // fold (zext_inreg (extload x)) -> (zextload x)
Gabor Greifba36cb52008-08-28 21:40:38 +00001716 if (ISD::isEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode())) {
Evan Cheng466685d2006-10-09 20:57:25 +00001717 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001718 MVT EVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001719 // If we zero all the possible extended bits, then we can turn this into
1720 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001721 unsigned BitWidth = N1.getValueSizeInBits();
1722 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001723 BitWidth - EVT.getSizeInBits())) &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001724 ((!AfterLegalize && !LN0->isVolatile()) ||
1725 TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Dan Gohman475871a2008-07-27 21:46:04 +00001726 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
Evan Cheng466685d2006-10-09 20:57:25 +00001727 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00001728 LN0->getSrcValueOffset(), EVT,
1729 LN0->isVolatile(),
1730 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00001731 AddToWorkList(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001732 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00001733 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001734 }
1735 }
1736 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Gabor Greifba36cb52008-08-28 21:40:38 +00001737 if (ISD::isSEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng83060c52007-03-07 08:07:03 +00001738 N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001739 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001740 MVT EVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001741 // If we zero all the possible extended bits, then we can turn this into
1742 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001743 unsigned BitWidth = N1.getValueSizeInBits();
1744 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001745 BitWidth - EVT.getSizeInBits())) &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001746 ((!AfterLegalize && !LN0->isVolatile()) ||
1747 TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Dan Gohman475871a2008-07-27 21:46:04 +00001748 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
Evan Cheng466685d2006-10-09 20:57:25 +00001749 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00001750 LN0->getSrcValueOffset(), EVT,
1751 LN0->isVolatile(),
1752 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00001753 AddToWorkList(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001754 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00001755 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001756 }
1757 }
Chris Lattner15045b62006-02-28 06:35:35 +00001758
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001759 // fold (and (load x), 255) -> (zextload x, i8)
1760 // fold (and (extload x, i16), 255) -> (zextload x, i8)
Evan Cheng466685d2006-10-09 20:57:25 +00001761 if (N1C && N0.getOpcode() == ISD::LOAD) {
1762 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
1763 if (LN0->getExtensionType() != ISD::SEXTLOAD &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001764 LN0->isUnindexed() && N0.hasOneUse() &&
1765 // Do not change the width of a volatile load.
1766 !LN0->isVolatile()) {
Duncan Sands8eab8a22008-06-09 11:32:28 +00001767 MVT EVT = MVT::Other;
1768 uint32_t ActiveBits = N1C->getAPIntValue().getActiveBits();
1769 if (ActiveBits > 0 && APIntOps::isMask(ActiveBits, N1C->getAPIntValue()))
1770 EVT = MVT::getIntegerVT(ActiveBits);
1771
1772 MVT LoadedVT = LN0->getMemoryVT();
Duncan Sandsad205a72008-06-16 08:14:38 +00001773 // Do not generate loads of non-round integer types since these can
1774 // be expensive (and would be wrong if the type is not byte sized).
1775 if (EVT != MVT::Other && LoadedVT.bitsGT(EVT) && EVT.isRound() &&
Evan Cheng466685d2006-10-09 20:57:25 +00001776 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001777 MVT PtrType = N0.getOperand(1).getValueType();
Evan Cheng466685d2006-10-09 20:57:25 +00001778 // For big endian targets, we need to add an offset to the pointer to
1779 // load the correct bytes. For little endian systems, we merely need to
1780 // read fewer bytes from the same pointer.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001781 unsigned LVTStoreBytes = LoadedVT.getStoreSizeInBits()/8;
1782 unsigned EVTStoreBytes = EVT.getStoreSizeInBits()/8;
Duncan Sandsc6fa1702007-11-09 08:57:19 +00001783 unsigned PtrOff = LVTStoreBytes - EVTStoreBytes;
Duncan Sandsdc846502007-10-28 12:59:45 +00001784 unsigned Alignment = LN0->getAlignment();
Dan Gohman475871a2008-07-27 21:46:04 +00001785 SDValue NewPtr = LN0->getBasePtr();
Duncan Sands0753fc12008-02-11 10:37:04 +00001786 if (TLI.isBigEndian()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001787 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
1788 DAG.getConstant(PtrOff, PtrType));
Duncan Sandsdc846502007-10-28 12:59:45 +00001789 Alignment = MinAlign(Alignment, PtrOff);
1790 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001791 AddToWorkList(NewPtr.getNode());
Dan Gohman475871a2008-07-27 21:46:04 +00001792 SDValue Load =
Evan Cheng466685d2006-10-09 20:57:25 +00001793 DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00001794 LN0->getSrcValue(), LN0->getSrcValueOffset(), EVT,
Duncan Sandsdc846502007-10-28 12:59:45 +00001795 LN0->isVolatile(), Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00001796 AddToWorkList(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001797 CombineTo(N0.getNode(), Load, Load.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00001798 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng466685d2006-10-09 20:57:25 +00001799 }
Chris Lattner15045b62006-02-28 06:35:35 +00001800 }
1801 }
1802
Dan Gohman475871a2008-07-27 21:46:04 +00001803 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001804}
1805
Dan Gohman475871a2008-07-27 21:46:04 +00001806SDValue DAGCombiner::visitOR(SDNode *N) {
1807 SDValue N0 = N->getOperand(0);
1808 SDValue N1 = N->getOperand(1);
1809 SDValue LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001810 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1811 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001812 MVT VT = N1.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001813
Dan Gohman7f321562007-06-25 16:23:39 +00001814 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001815 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001816 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001817 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001818 }
Dan Gohman7f321562007-06-25 16:23:39 +00001819
Dan Gohman613e0d82007-07-03 14:03:57 +00001820 // fold (or x, undef) -> -1
Dan Gohmand595b5f2007-07-10 14:20:37 +00001821 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Chris Lattnere094f542007-07-09 16:16:34 +00001822 return DAG.getConstant(~0ULL, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001823 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001824 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001825 return DAG.getNode(ISD::OR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001826 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001827 if (N0C && !N1C)
1828 return DAG.getNode(ISD::OR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001829 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001830 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001831 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001832 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001833 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001834 return N1;
1835 // fold (or x, c) -> c iff (x & ~c) == 0
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001836 if (N1C && DAG.MaskedValueIsZero(N0, ~N1C->getAPIntValue()))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001837 return N1;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001838 // reassociate or
Dan Gohman475871a2008-07-27 21:46:04 +00001839 SDValue ROR = ReassociateOps(ISD::OR, N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001840 if (ROR.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001841 return ROR;
1842 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
Gabor Greifba36cb52008-08-28 21:40:38 +00001843 if (N1C && N0.getOpcode() == ISD::AND && N0.getNode()->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00001844 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00001845 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
1846 return DAG.getNode(ISD::AND, VT, DAG.getNode(ISD::OR, VT, N0.getOperand(0),
1847 N1),
Dan Gohman002e5d02008-03-13 22:13:53 +00001848 DAG.getConstant(N1C->getAPIntValue() |
1849 C1->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00001850 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001851 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
1852 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1853 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1854 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1855
1856 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00001857 LL.getValueType().isInteger()) {
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001858 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
1859 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
Dan Gohman002e5d02008-03-13 22:13:53 +00001860 if (cast<ConstantSDNode>(LR)->isNullValue() &&
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001861 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001862 SDValue ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001863 AddToWorkList(ORNode.getNode());
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001864 return DAG.getSetCC(VT, ORNode, LR, Op1);
1865 }
1866 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
1867 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
1868 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
1869 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001870 SDValue ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001871 AddToWorkList(ANDNode.getNode());
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001872 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1873 }
1874 }
1875 // canonicalize equivalent to ll == rl
1876 if (LL == RR && LR == RL) {
1877 Op1 = ISD::getSetCCSwappedOperands(Op1);
1878 std::swap(RL, RR);
1879 }
1880 if (LL == RL && LR == RR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001881 bool isInteger = LL.getValueType().isInteger();
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001882 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
1883 if (Result != ISD::SETCC_INVALID)
1884 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1885 }
1886 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001887
1888 // Simplify: or (op x...), (op y...) -> (op (or x, y))
1889 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001890 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001891 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001892 }
Chris Lattner516b9622006-09-14 20:50:57 +00001893
Chris Lattner1ec72732006-09-14 21:11:37 +00001894 // (X & C1) | (Y & C2) -> (X|Y) & C3 if possible.
1895 if (N0.getOpcode() == ISD::AND &&
1896 N1.getOpcode() == ISD::AND &&
1897 N0.getOperand(1).getOpcode() == ISD::Constant &&
1898 N1.getOperand(1).getOpcode() == ISD::Constant &&
1899 // Don't increase # computations.
Gabor Greifba36cb52008-08-28 21:40:38 +00001900 (N0.getNode()->hasOneUse() || N1.getNode()->hasOneUse())) {
Chris Lattner1ec72732006-09-14 21:11:37 +00001901 // We can only do this xform if we know that bits from X that are set in C2
1902 // but not in C1 are already zero. Likewise for Y.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001903 const APInt &LHSMask =
1904 cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
1905 const APInt &RHSMask =
1906 cast<ConstantSDNode>(N1.getOperand(1))->getAPIntValue();
Chris Lattner1ec72732006-09-14 21:11:37 +00001907
Dan Gohmanea859be2007-06-22 14:59:07 +00001908 if (DAG.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) &&
1909 DAG.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001910 SDValue X =DAG.getNode(ISD::OR, VT, N0.getOperand(0), N1.getOperand(0));
Chris Lattner1ec72732006-09-14 21:11:37 +00001911 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(LHSMask|RHSMask, VT));
1912 }
1913 }
1914
1915
Chris Lattner516b9622006-09-14 20:50:57 +00001916 // See if this is some rotate idiom.
1917 if (SDNode *Rot = MatchRotate(N0, N1))
Dan Gohman475871a2008-07-27 21:46:04 +00001918 return SDValue(Rot, 0);
Chris Lattner35e5c142006-05-05 05:51:50 +00001919
Dan Gohman475871a2008-07-27 21:46:04 +00001920 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001921}
1922
Chris Lattner516b9622006-09-14 20:50:57 +00001923
1924/// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present.
Dan Gohman475871a2008-07-27 21:46:04 +00001925static bool MatchRotateHalf(SDValue Op, SDValue &Shift, SDValue &Mask) {
Chris Lattner516b9622006-09-14 20:50:57 +00001926 if (Op.getOpcode() == ISD::AND) {
Reid Spencer3ed469c2006-11-02 20:25:50 +00001927 if (isa<ConstantSDNode>(Op.getOperand(1))) {
Chris Lattner516b9622006-09-14 20:50:57 +00001928 Mask = Op.getOperand(1);
1929 Op = Op.getOperand(0);
1930 } else {
1931 return false;
1932 }
1933 }
1934
1935 if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) {
1936 Shift = Op;
1937 return true;
1938 }
1939 return false;
1940}
1941
1942
1943// MatchRotate - Handle an 'or' of two operands. If this is one of the many
1944// idioms for rotate, and if the target supports rotation instructions, generate
1945// a rot[lr].
Dan Gohman475871a2008-07-27 21:46:04 +00001946SDNode *DAGCombiner::MatchRotate(SDValue LHS, SDValue RHS) {
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001947 // Must be a legal type. Expanded 'n promoted things won't work with rotates.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001948 MVT VT = LHS.getValueType();
Chris Lattner516b9622006-09-14 20:50:57 +00001949 if (!TLI.isTypeLegal(VT)) return 0;
1950
1951 // The target must have at least one rotate flavor.
1952 bool HasROTL = TLI.isOperationLegal(ISD::ROTL, VT);
1953 bool HasROTR = TLI.isOperationLegal(ISD::ROTR, VT);
1954 if (!HasROTL && !HasROTR) return 0;
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001955
Chris Lattner516b9622006-09-14 20:50:57 +00001956 // Match "(X shl/srl V1) & V2" where V2 may not be present.
Dan Gohman475871a2008-07-27 21:46:04 +00001957 SDValue LHSShift; // The shift.
1958 SDValue LHSMask; // AND value if any.
Chris Lattner516b9622006-09-14 20:50:57 +00001959 if (!MatchRotateHalf(LHS, LHSShift, LHSMask))
1960 return 0; // Not part of a rotate.
1961
Dan Gohman475871a2008-07-27 21:46:04 +00001962 SDValue RHSShift; // The shift.
1963 SDValue RHSMask; // AND value if any.
Chris Lattner516b9622006-09-14 20:50:57 +00001964 if (!MatchRotateHalf(RHS, RHSShift, RHSMask))
1965 return 0; // Not part of a rotate.
1966
1967 if (LHSShift.getOperand(0) != RHSShift.getOperand(0))
1968 return 0; // Not shifting the same value.
1969
1970 if (LHSShift.getOpcode() == RHSShift.getOpcode())
1971 return 0; // Shifts must disagree.
1972
1973 // Canonicalize shl to left side in a shl/srl pair.
1974 if (RHSShift.getOpcode() == ISD::SHL) {
1975 std::swap(LHS, RHS);
1976 std::swap(LHSShift, RHSShift);
1977 std::swap(LHSMask , RHSMask );
1978 }
1979
Duncan Sands83ec4b62008-06-06 12:08:01 +00001980 unsigned OpSizeInBits = VT.getSizeInBits();
Dan Gohman475871a2008-07-27 21:46:04 +00001981 SDValue LHSShiftArg = LHSShift.getOperand(0);
1982 SDValue LHSShiftAmt = LHSShift.getOperand(1);
1983 SDValue RHSShiftAmt = RHSShift.getOperand(1);
Chris Lattner516b9622006-09-14 20:50:57 +00001984
1985 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
1986 // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2)
Scott Michelc9dc1142007-04-02 21:36:32 +00001987 if (LHSShiftAmt.getOpcode() == ISD::Constant &&
1988 RHSShiftAmt.getOpcode() == ISD::Constant) {
1989 uint64_t LShVal = cast<ConstantSDNode>(LHSShiftAmt)->getValue();
1990 uint64_t RShVal = cast<ConstantSDNode>(RHSShiftAmt)->getValue();
Chris Lattner516b9622006-09-14 20:50:57 +00001991 if ((LShVal + RShVal) != OpSizeInBits)
1992 return 0;
1993
Dan Gohman475871a2008-07-27 21:46:04 +00001994 SDValue Rot;
Chris Lattner516b9622006-09-14 20:50:57 +00001995 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00001996 Rot = DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00001997 else
Scott Michelc9dc1142007-04-02 21:36:32 +00001998 Rot = DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00001999
2000 // If there is an AND of either shifted operand, apply it to the result.
Gabor Greifba36cb52008-08-28 21:40:38 +00002001 if (LHSMask.getNode() || RHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00002002 APInt Mask = APInt::getAllOnesValue(OpSizeInBits);
Chris Lattner516b9622006-09-14 20:50:57 +00002003
Gabor Greifba36cb52008-08-28 21:40:38 +00002004 if (LHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00002005 APInt RHSBits = APInt::getLowBitsSet(OpSizeInBits, LShVal);
2006 Mask &= cast<ConstantSDNode>(LHSMask)->getAPIntValue() | RHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00002007 }
Gabor Greifba36cb52008-08-28 21:40:38 +00002008 if (RHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00002009 APInt LHSBits = APInt::getHighBitsSet(OpSizeInBits, RShVal);
2010 Mask &= cast<ConstantSDNode>(RHSMask)->getAPIntValue() | LHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00002011 }
2012
2013 Rot = DAG.getNode(ISD::AND, VT, Rot, DAG.getConstant(Mask, VT));
2014 }
2015
Gabor Greifba36cb52008-08-28 21:40:38 +00002016 return Rot.getNode();
Chris Lattner516b9622006-09-14 20:50:57 +00002017 }
2018
2019 // If there is a mask here, and we have a variable shift, we can't be sure
2020 // that we're masking out the right stuff.
Gabor Greifba36cb52008-08-28 21:40:38 +00002021 if (LHSMask.getNode() || RHSMask.getNode())
Chris Lattner516b9622006-09-14 20:50:57 +00002022 return 0;
2023
2024 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
2025 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotr x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00002026 if (RHSShiftAmt.getOpcode() == ISD::SUB &&
2027 LHSShiftAmt == RHSShiftAmt.getOperand(1)) {
Chris Lattner516b9622006-09-14 20:50:57 +00002028 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00002029 dyn_cast<ConstantSDNode>(RHSShiftAmt.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002030 if (SUBC->getAPIntValue() == OpSizeInBits) {
Chris Lattner516b9622006-09-14 20:50:57 +00002031 if (HasROTL)
Gabor Greifba36cb52008-08-28 21:40:38 +00002032 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).getNode();
Chris Lattner516b9622006-09-14 20:50:57 +00002033 else
Gabor Greifba36cb52008-08-28 21:40:38 +00002034 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).getNode();
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002035 }
Chris Lattner516b9622006-09-14 20:50:57 +00002036 }
2037 }
2038
2039 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
2040 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotl x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00002041 if (LHSShiftAmt.getOpcode() == ISD::SUB &&
2042 RHSShiftAmt == LHSShiftAmt.getOperand(1)) {
Chris Lattner516b9622006-09-14 20:50:57 +00002043 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00002044 dyn_cast<ConstantSDNode>(LHSShiftAmt.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002045 if (SUBC->getAPIntValue() == OpSizeInBits) {
Chris Lattner516b9622006-09-14 20:50:57 +00002046 if (HasROTL)
Gabor Greifba36cb52008-08-28 21:40:38 +00002047 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).getNode();
Chris Lattner516b9622006-09-14 20:50:57 +00002048 else
Gabor Greifba36cb52008-08-28 21:40:38 +00002049 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).getNode();
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002050 }
Scott Michelc9dc1142007-04-02 21:36:32 +00002051 }
2052 }
2053
2054 // Look for sign/zext/any-extended cases:
2055 if ((LHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
2056 || LHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
2057 || LHSShiftAmt.getOpcode() == ISD::ANY_EXTEND) &&
2058 (RHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
2059 || RHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
2060 || RHSShiftAmt.getOpcode() == ISD::ANY_EXTEND)) {
Dan Gohman475871a2008-07-27 21:46:04 +00002061 SDValue LExtOp0 = LHSShiftAmt.getOperand(0);
2062 SDValue RExtOp0 = RHSShiftAmt.getOperand(0);
Scott Michelc9dc1142007-04-02 21:36:32 +00002063 if (RExtOp0.getOpcode() == ISD::SUB &&
2064 RExtOp0.getOperand(1) == LExtOp0) {
2065 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
2066 // (rotr x, y)
2067 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
2068 // (rotl x, (sub 32, y))
2069 if (ConstantSDNode *SUBC = cast<ConstantSDNode>(RExtOp0.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002070 if (SUBC->getAPIntValue() == OpSizeInBits) {
Scott Michelc9dc1142007-04-02 21:36:32 +00002071 if (HasROTL)
Gabor Greifba36cb52008-08-28 21:40:38 +00002072 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).getNode();
Scott Michelc9dc1142007-04-02 21:36:32 +00002073 else
Gabor Greifba36cb52008-08-28 21:40:38 +00002074 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).getNode();
Scott Michelc9dc1142007-04-02 21:36:32 +00002075 }
2076 }
2077 } else if (LExtOp0.getOpcode() == ISD::SUB &&
2078 RExtOp0 == LExtOp0.getOperand(1)) {
2079 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext r))) ->
2080 // (rotl x, y)
2081 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext r))) ->
2082 // (rotr x, (sub 32, y))
2083 if (ConstantSDNode *SUBC = cast<ConstantSDNode>(LExtOp0.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002084 if (SUBC->getAPIntValue() == OpSizeInBits) {
Scott Michelc9dc1142007-04-02 21:36:32 +00002085 if (HasROTL)
Gabor Greifba36cb52008-08-28 21:40:38 +00002086 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, RHSShiftAmt).getNode();
Scott Michelc9dc1142007-04-02 21:36:32 +00002087 else
Gabor Greifba36cb52008-08-28 21:40:38 +00002088 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).getNode();
Scott Michelc9dc1142007-04-02 21:36:32 +00002089 }
2090 }
Chris Lattner516b9622006-09-14 20:50:57 +00002091 }
2092 }
2093
2094 return 0;
2095}
2096
2097
Dan Gohman475871a2008-07-27 21:46:04 +00002098SDValue DAGCombiner::visitXOR(SDNode *N) {
2099 SDValue N0 = N->getOperand(0);
2100 SDValue N1 = N->getOperand(1);
2101 SDValue LHS, RHS, CC;
Nate Begeman646d7e22005-09-02 21:18:40 +00002102 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2103 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002104 MVT VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002105
Dan Gohman7f321562007-06-25 16:23:39 +00002106 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00002107 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002108 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002109 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00002110 }
Dan Gohman7f321562007-06-25 16:23:39 +00002111
Evan Cheng26471c42008-03-25 20:08:07 +00002112 // fold (xor undef, undef) -> 0. This is a common idiom (misuse).
2113 if (N0.getOpcode() == ISD::UNDEF && N1.getOpcode() == ISD::UNDEF)
2114 return DAG.getConstant(0, VT);
Dan Gohman613e0d82007-07-03 14:03:57 +00002115 // fold (xor x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00002116 if (N0.getOpcode() == ISD::UNDEF)
2117 return N0;
2118 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00002119 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002120 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002121 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002122 return DAG.getNode(ISD::XOR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00002123 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00002124 if (N0C && !N1C)
2125 return DAG.getNode(ISD::XOR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002126 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002127 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002128 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00002129 // reassociate xor
Dan Gohman475871a2008-07-27 21:46:04 +00002130 SDValue RXOR = ReassociateOps(ISD::XOR, N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00002131 if (RXOR.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00002132 return RXOR;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002133 // fold !(x cc y) -> (x !cc y)
Dan Gohman002e5d02008-03-13 22:13:53 +00002134 if (N1C && N1C->getAPIntValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002135 bool isInt = LHS.getValueType().isInteger();
Nate Begeman646d7e22005-09-02 21:18:40 +00002136 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
2137 isInt);
2138 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002139 return DAG.getSetCC(VT, LHS, RHS, NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00002140 if (N0.getOpcode() == ISD::SELECT_CC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002141 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00002142 assert(0 && "Unhandled SetCC Equivalent!");
2143 abort();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002144 }
Chris Lattner61c5ff42007-09-10 21:39:07 +00002145 // fold (not (zext (setcc x, y))) -> (zext (not (setcc x, y)))
Dan Gohman002e5d02008-03-13 22:13:53 +00002146 if (N1C && N1C->getAPIntValue() == 1 && N0.getOpcode() == ISD::ZERO_EXTEND &&
Gabor Greifba36cb52008-08-28 21:40:38 +00002147 N0.getNode()->hasOneUse() && isSetCCEquivalent(N0.getOperand(0), LHS, RHS, CC)){
Dan Gohman475871a2008-07-27 21:46:04 +00002148 SDValue V = N0.getOperand(0);
Chris Lattner61c5ff42007-09-10 21:39:07 +00002149 V = DAG.getNode(ISD::XOR, V.getValueType(), V,
Duncan Sands272dce02007-10-10 09:54:50 +00002150 DAG.getConstant(1, V.getValueType()));
Gabor Greifba36cb52008-08-28 21:40:38 +00002151 AddToWorkList(V.getNode());
Chris Lattner61c5ff42007-09-10 21:39:07 +00002152 return DAG.getNode(ISD::ZERO_EXTEND, VT, V);
2153 }
2154
Nate Begeman99801192005-09-07 23:25:52 +00002155 // fold !(x or y) -> (!x and !y) iff x or y are setcc
Dan Gohman002e5d02008-03-13 22:13:53 +00002156 if (N1C && N1C->getAPIntValue() == 1 && VT == MVT::i1 &&
Nate Begeman99801192005-09-07 23:25:52 +00002157 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Dan Gohman475871a2008-07-27 21:46:04 +00002158 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00002159 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
2160 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002161 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
2162 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Gabor Greifba36cb52008-08-28 21:40:38 +00002163 AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode());
Nate Begeman99801192005-09-07 23:25:52 +00002164 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002165 }
2166 }
Nate Begeman99801192005-09-07 23:25:52 +00002167 // fold !(x or y) -> (!x and !y) iff x or y are constants
2168 if (N1C && N1C->isAllOnesValue() &&
2169 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Dan Gohman475871a2008-07-27 21:46:04 +00002170 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00002171 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
2172 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002173 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
2174 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Gabor Greifba36cb52008-08-28 21:40:38 +00002175 AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode());
Nate Begeman99801192005-09-07 23:25:52 +00002176 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002177 }
2178 }
Nate Begeman223df222005-09-08 20:18:10 +00002179 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
2180 if (N1C && N0.getOpcode() == ISD::XOR) {
2181 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
2182 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2183 if (N00C)
2184 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
Dan Gohman002e5d02008-03-13 22:13:53 +00002185 DAG.getConstant(N1C->getAPIntValue()^
2186 N00C->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00002187 if (N01C)
2188 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
Dan Gohman002e5d02008-03-13 22:13:53 +00002189 DAG.getConstant(N1C->getAPIntValue()^
2190 N01C->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00002191 }
2192 // fold (xor x, x) -> 0
Chris Lattner4fbdd592006-03-28 19:11:05 +00002193 if (N0 == N1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002194 if (!VT.isVector()) {
Chris Lattner4fbdd592006-03-28 19:11:05 +00002195 return DAG.getConstant(0, VT);
2196 } else if (!AfterLegalize || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) {
2197 // Produce a vector of zeros.
Dan Gohman475871a2008-07-27 21:46:04 +00002198 SDValue El = DAG.getConstant(0, VT.getVectorElementType());
2199 std::vector<SDValue> Ops(VT.getVectorNumElements(), El);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002200 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4fbdd592006-03-28 19:11:05 +00002201 }
2202 }
Chris Lattner35e5c142006-05-05 05:51:50 +00002203
2204 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
2205 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002206 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002207 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002208 }
Chris Lattner35e5c142006-05-05 05:51:50 +00002209
Chris Lattner3e104b12006-04-08 04:15:24 +00002210 // Simplify the expression using non-local knowledge.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002211 if (!VT.isVector() &&
Dan Gohman475871a2008-07-27 21:46:04 +00002212 SimplifyDemandedBits(SDValue(N, 0)))
2213 return SDValue(N, 0);
Chris Lattner3e104b12006-04-08 04:15:24 +00002214
Dan Gohman475871a2008-07-27 21:46:04 +00002215 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002216}
2217
Chris Lattnere70da202007-12-06 07:33:36 +00002218/// visitShiftByConstant - Handle transforms common to the three shifts, when
2219/// the shift amount is a constant.
Dan Gohman475871a2008-07-27 21:46:04 +00002220SDValue DAGCombiner::visitShiftByConstant(SDNode *N, unsigned Amt) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002221 SDNode *LHS = N->getOperand(0).getNode();
Dan Gohman475871a2008-07-27 21:46:04 +00002222 if (!LHS->hasOneUse()) return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00002223
2224 // We want to pull some binops through shifts, so that we have (and (shift))
2225 // instead of (shift (and)), likewise for add, or, xor, etc. This sort of
2226 // thing happens with address calculations, so it's important to canonicalize
2227 // it.
2228 bool HighBitSet = false; // Can we transform this if the high bit is set?
2229
2230 switch (LHS->getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002231 default: return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00002232 case ISD::OR:
2233 case ISD::XOR:
2234 HighBitSet = false; // We can only transform sra if the high bit is clear.
2235 break;
2236 case ISD::AND:
2237 HighBitSet = true; // We can only transform sra if the high bit is set.
2238 break;
2239 case ISD::ADD:
2240 if (N->getOpcode() != ISD::SHL)
Dan Gohman475871a2008-07-27 21:46:04 +00002241 return SDValue(); // only shl(add) not sr[al](add).
Chris Lattnere70da202007-12-06 07:33:36 +00002242 HighBitSet = false; // We can only transform sra if the high bit is clear.
2243 break;
2244 }
2245
2246 // We require the RHS of the binop to be a constant as well.
2247 ConstantSDNode *BinOpCst = dyn_cast<ConstantSDNode>(LHS->getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00002248 if (!BinOpCst) return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00002249
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00002250
2251 // FIXME: disable this for unless the input to the binop is a shift by a
2252 // constant. If it is not a shift, it pessimizes some common cases like:
2253 //
2254 //void foo(int *X, int i) { X[i & 1235] = 1; }
2255 //int bar(int *X, int i) { return X[i & 255]; }
Gabor Greifba36cb52008-08-28 21:40:38 +00002256 SDNode *BinOpLHSVal = LHS->getOperand(0).getNode();
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00002257 if ((BinOpLHSVal->getOpcode() != ISD::SHL &&
2258 BinOpLHSVal->getOpcode() != ISD::SRA &&
2259 BinOpLHSVal->getOpcode() != ISD::SRL) ||
2260 !isa<ConstantSDNode>(BinOpLHSVal->getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00002261 return SDValue();
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00002262
Duncan Sands83ec4b62008-06-06 12:08:01 +00002263 MVT VT = N->getValueType(0);
Chris Lattnere70da202007-12-06 07:33:36 +00002264
2265 // If this is a signed shift right, and the high bit is modified
2266 // by the logical operation, do not perform the transformation.
2267 // The highBitSet boolean indicates the value of the high bit of
2268 // the constant which would cause it to be modified for this
2269 // operation.
2270 if (N->getOpcode() == ISD::SRA) {
Dan Gohman220a8232008-03-03 23:51:38 +00002271 bool BinOpRHSSignSet = BinOpCst->getAPIntValue().isNegative();
2272 if (BinOpRHSSignSet != HighBitSet)
Dan Gohman475871a2008-07-27 21:46:04 +00002273 return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00002274 }
2275
2276 // Fold the constants, shifting the binop RHS by the shift amount.
Dan Gohman475871a2008-07-27 21:46:04 +00002277 SDValue NewRHS = DAG.getNode(N->getOpcode(), N->getValueType(0),
Chris Lattnere70da202007-12-06 07:33:36 +00002278 LHS->getOperand(1), N->getOperand(1));
2279
2280 // Create the new shift.
Dan Gohman475871a2008-07-27 21:46:04 +00002281 SDValue NewShift = DAG.getNode(N->getOpcode(), VT, LHS->getOperand(0),
Chris Lattnere70da202007-12-06 07:33:36 +00002282 N->getOperand(1));
2283
2284 // Create the new binop.
2285 return DAG.getNode(LHS->getOpcode(), VT, NewShift, NewRHS);
2286}
2287
2288
Dan Gohman475871a2008-07-27 21:46:04 +00002289SDValue DAGCombiner::visitSHL(SDNode *N) {
2290 SDValue N0 = N->getOperand(0);
2291 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002292 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2293 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002294 MVT VT = N0.getValueType();
2295 unsigned OpSizeInBits = VT.getSizeInBits();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002296
2297 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002298 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002299 return DAG.getNode(ISD::SHL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002300 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002301 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002302 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002303 // fold (shl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00002304 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002305 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002306 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002307 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002308 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002309 // if (shl x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00002310 if (DAG.MaskedValueIsZero(SDValue(N, 0),
Duncan Sands83ec4b62008-06-06 12:08:01 +00002311 APInt::getAllOnesValue(VT.getSizeInBits())))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002312 return DAG.getConstant(0, VT);
Dan Gohman475871a2008-07-27 21:46:04 +00002313 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
2314 return SDValue(N, 0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002315 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002316 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002317 N0.getOperand(1).getOpcode() == ISD::Constant) {
2318 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00002319 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002320 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002321 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002322 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002323 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002324 }
2325 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
2326 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002327 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002328 N0.getOperand(1).getOpcode() == ISD::Constant) {
2329 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00002330 uint64_t c2 = N1C->getValue();
Dan Gohman475871a2008-07-27 21:46:04 +00002331 SDValue Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman1d4d4142005-09-01 00:19:25 +00002332 DAG.getConstant(~0ULL << c1, VT));
2333 if (c2 > c1)
2334 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +00002335 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002336 else
Nate Begeman83e75ec2005-09-06 04:43:02 +00002337 return DAG.getNode(ISD::SRL, VT, Mask,
2338 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002339 }
2340 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00002341 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +00002342 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002343 DAG.getConstant(~0ULL << N1C->getValue(), VT));
Chris Lattnere70da202007-12-06 07:33:36 +00002344
Dan Gohman475871a2008-07-27 21:46:04 +00002345 return N1C ? visitShiftByConstant(N, N1C->getValue()) : SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002346}
2347
Dan Gohman475871a2008-07-27 21:46:04 +00002348SDValue DAGCombiner::visitSRA(SDNode *N) {
2349 SDValue N0 = N->getOperand(0);
2350 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002351 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2352 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002353 MVT VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002354
2355 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002356 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002357 return DAG.getNode(ISD::SRA, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002358 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002359 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002360 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002361 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00002362 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002363 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002364 // fold (sra x, c >= size(x)) -> undef
Duncan Sands83ec4b62008-06-06 12:08:01 +00002365 if (N1C && N1C->getValue() >= VT.getSizeInBits())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002366 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002367 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002368 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002369 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00002370 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
2371 // sext_inreg.
2372 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002373 unsigned LowBits = VT.getSizeInBits() - (unsigned)N1C->getValue();
Duncan Sands8eab8a22008-06-09 11:32:28 +00002374 MVT EVT = MVT::getIntegerVT(LowBits);
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002375 if (EVT.isSimple() && // TODO: remove when apint codegen support lands.
2376 (!AfterLegalize || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, EVT)))
Nate Begemanfb7217b2006-02-17 19:54:08 +00002377 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
2378 DAG.getValueType(EVT));
2379 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002380
Chris Lattner71d9ebc2006-02-28 06:23:04 +00002381 // fold (sra (sra x, c1), c2) -> (sra x, c1+c2)
2382 if (N1C && N0.getOpcode() == ISD::SRA) {
2383 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2384 unsigned Sum = N1C->getValue() + C1->getValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002385 if (Sum >= VT.getSizeInBits()) Sum = VT.getSizeInBits()-1;
Chris Lattner71d9ebc2006-02-28 06:23:04 +00002386 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0),
2387 DAG.getConstant(Sum, N1C->getValueType(0)));
2388 }
2389 }
Christopher Lamb15cbde32008-03-19 08:30:06 +00002390
2391 // fold sra (shl X, m), result_size - n
2392 // -> (sign_extend (trunc (shl X, result_size - n - m))) for
Christopher Lambb9b04282008-03-20 04:31:39 +00002393 // result_size - n != m.
2394 // If truncate is free for the target sext(shl) is likely to result in better
2395 // code.
Christopher Lamb15cbde32008-03-19 08:30:06 +00002396 if (N0.getOpcode() == ISD::SHL) {
2397 // Get the two constanst of the shifts, CN0 = m, CN = n.
2398 const ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2399 if (N01C && N1C) {
Christopher Lambb9b04282008-03-20 04:31:39 +00002400 // Determine what the truncate's result bitsize and type would be.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002401 unsigned VTValSize = VT.getSizeInBits();
2402 MVT TruncVT =
2403 MVT::getIntegerVT(VTValSize - N1C->getValue());
Christopher Lambb9b04282008-03-20 04:31:39 +00002404 // Determine the residual right-shift amount.
Christopher Lamb15cbde32008-03-19 08:30:06 +00002405 unsigned ShiftAmt = N1C->getValue() - N01C->getValue();
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002406
Christopher Lambb9b04282008-03-20 04:31:39 +00002407 // If the shift is not a no-op (in which case this should be just a sign
2408 // extend already), the truncated to type is legal, sign_extend is legal
2409 // on that type, and the the truncate to that type is both legal and free,
2410 // perform the transform.
2411 if (ShiftAmt &&
Christopher Lambb9b04282008-03-20 04:31:39 +00002412 TLI.isOperationLegal(ISD::SIGN_EXTEND, TruncVT) &&
2413 TLI.isOperationLegal(ISD::TRUNCATE, VT) &&
Evan Cheng260e07e2008-03-20 02:18:41 +00002414 TLI.isTruncateFree(VT, TruncVT)) {
Christopher Lambb9b04282008-03-20 04:31:39 +00002415
Dan Gohman475871a2008-07-27 21:46:04 +00002416 SDValue Amt = DAG.getConstant(ShiftAmt, TLI.getShiftAmountTy());
2417 SDValue Shift = DAG.getNode(ISD::SRL, VT, N0.getOperand(0), Amt);
2418 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, TruncVT, Shift);
Christopher Lambb9b04282008-03-20 04:31:39 +00002419 return DAG.getNode(ISD::SIGN_EXTEND, N->getValueType(0), Trunc);
Christopher Lamb15cbde32008-03-19 08:30:06 +00002420 }
2421 }
2422 }
Chris Lattner71d9ebc2006-02-28 06:23:04 +00002423
Chris Lattnera8504462006-05-08 20:51:54 +00002424 // Simplify, based on bits shifted out of the LHS.
Dan Gohman475871a2008-07-27 21:46:04 +00002425 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
2426 return SDValue(N, 0);
Chris Lattnera8504462006-05-08 20:51:54 +00002427
2428
Nate Begeman1d4d4142005-09-01 00:19:25 +00002429 // If the sign bit is known to be zero, switch this to a SRL.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002430 if (DAG.SignBitIsZero(N0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002431 return DAG.getNode(ISD::SRL, VT, N0, N1);
Chris Lattnere70da202007-12-06 07:33:36 +00002432
Dan Gohman475871a2008-07-27 21:46:04 +00002433 return N1C ? visitShiftByConstant(N, N1C->getValue()) : SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002434}
2435
Dan Gohman475871a2008-07-27 21:46:04 +00002436SDValue DAGCombiner::visitSRL(SDNode *N) {
2437 SDValue N0 = N->getOperand(0);
2438 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002439 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2440 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002441 MVT VT = N0.getValueType();
2442 unsigned OpSizeInBits = VT.getSizeInBits();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002443
2444 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002445 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002446 return DAG.getNode(ISD::SRL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002447 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002448 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002449 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002450 // fold (srl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00002451 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002452 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002453 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002454 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002455 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002456 // if (srl x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00002457 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002458 APInt::getAllOnesValue(OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002459 return DAG.getConstant(0, VT);
Chris Lattnerec06e9a2007-04-18 03:05:22 +00002460
Nate Begeman1d4d4142005-09-01 00:19:25 +00002461 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002462 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002463 N0.getOperand(1).getOpcode() == ISD::Constant) {
2464 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00002465 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002466 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002467 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002468 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002469 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002470 }
Chris Lattner350bec02006-04-02 06:11:11 +00002471
Chris Lattner06afe072006-05-05 22:53:17 +00002472 // fold (srl (anyextend x), c) -> (anyextend (srl x, c))
2473 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
2474 // Shifting in all undef bits?
Duncan Sands83ec4b62008-06-06 12:08:01 +00002475 MVT SmallVT = N0.getOperand(0).getValueType();
2476 if (N1C->getValue() >= SmallVT.getSizeInBits())
Chris Lattner06afe072006-05-05 22:53:17 +00002477 return DAG.getNode(ISD::UNDEF, VT);
2478
Dan Gohman475871a2008-07-27 21:46:04 +00002479 SDValue SmallShift = DAG.getNode(ISD::SRL, SmallVT, N0.getOperand(0), N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00002480 AddToWorkList(SmallShift.getNode());
Chris Lattner06afe072006-05-05 22:53:17 +00002481 return DAG.getNode(ISD::ANY_EXTEND, VT, SmallShift);
2482 }
2483
Chris Lattner3657ffe2006-10-12 20:23:19 +00002484 // fold (srl (sra X, Y), 31) -> (srl X, 31). This srl only looks at the sign
2485 // bit, which is unmodified by sra.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002486 if (N1C && N1C->getValue()+1 == VT.getSizeInBits()) {
Chris Lattner3657ffe2006-10-12 20:23:19 +00002487 if (N0.getOpcode() == ISD::SRA)
2488 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0), N1);
2489 }
2490
Chris Lattner350bec02006-04-02 06:11:11 +00002491 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
2492 if (N1C && N0.getOpcode() == ISD::CTLZ &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002493 N1C->getAPIntValue() == Log2_32(VT.getSizeInBits())) {
Dan Gohman948d8ea2008-02-20 16:33:30 +00002494 APInt KnownZero, KnownOne;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002495 APInt Mask = APInt::getAllOnesValue(VT.getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00002496 DAG.ComputeMaskedBits(N0.getOperand(0), Mask, KnownZero, KnownOne);
Chris Lattner350bec02006-04-02 06:11:11 +00002497
2498 // If any of the input bits are KnownOne, then the input couldn't be all
2499 // zeros, thus the result of the srl will always be zero.
Dan Gohman948d8ea2008-02-20 16:33:30 +00002500 if (KnownOne.getBoolValue()) return DAG.getConstant(0, VT);
Chris Lattner350bec02006-04-02 06:11:11 +00002501
2502 // If all of the bits input the to ctlz node are known to be zero, then
2503 // the result of the ctlz is "32" and the result of the shift is one.
Dan Gohman948d8ea2008-02-20 16:33:30 +00002504 APInt UnknownBits = ~KnownZero & Mask;
Chris Lattner350bec02006-04-02 06:11:11 +00002505 if (UnknownBits == 0) return DAG.getConstant(1, VT);
2506
2507 // Otherwise, check to see if there is exactly one bit input to the ctlz.
2508 if ((UnknownBits & (UnknownBits-1)) == 0) {
2509 // Okay, we know that only that the single bit specified by UnknownBits
2510 // could be set on input to the CTLZ node. If this bit is set, the SRL
2511 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
2512 // to an SRL,XOR pair, which is likely to simplify more.
Dan Gohman948d8ea2008-02-20 16:33:30 +00002513 unsigned ShAmt = UnknownBits.countTrailingZeros();
Dan Gohman475871a2008-07-27 21:46:04 +00002514 SDValue Op = N0.getOperand(0);
Chris Lattner350bec02006-04-02 06:11:11 +00002515 if (ShAmt) {
2516 Op = DAG.getNode(ISD::SRL, VT, Op,
2517 DAG.getConstant(ShAmt, TLI.getShiftAmountTy()));
Gabor Greifba36cb52008-08-28 21:40:38 +00002518 AddToWorkList(Op.getNode());
Chris Lattner350bec02006-04-02 06:11:11 +00002519 }
2520 return DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(1, VT));
2521 }
2522 }
Chris Lattner61a4c072007-04-18 03:06:49 +00002523
2524 // fold operands of srl based on knowledge that the low bits are not
2525 // demanded.
Dan Gohman475871a2008-07-27 21:46:04 +00002526 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
2527 return SDValue(N, 0);
Chris Lattner61a4c072007-04-18 03:06:49 +00002528
Dan Gohman475871a2008-07-27 21:46:04 +00002529 return N1C ? visitShiftByConstant(N, N1C->getValue()) : SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002530}
2531
Dan Gohman475871a2008-07-27 21:46:04 +00002532SDValue DAGCombiner::visitCTLZ(SDNode *N) {
2533 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002534 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002535
2536 // fold (ctlz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002537 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002538 return DAG.getNode(ISD::CTLZ, VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00002539 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002540}
2541
Dan Gohman475871a2008-07-27 21:46:04 +00002542SDValue DAGCombiner::visitCTTZ(SDNode *N) {
2543 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002544 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002545
2546 // fold (cttz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002547 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002548 return DAG.getNode(ISD::CTTZ, VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00002549 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002550}
2551
Dan Gohman475871a2008-07-27 21:46:04 +00002552SDValue DAGCombiner::visitCTPOP(SDNode *N) {
2553 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002554 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002555
2556 // fold (ctpop c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002557 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002558 return DAG.getNode(ISD::CTPOP, VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00002559 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002560}
2561
Dan Gohman475871a2008-07-27 21:46:04 +00002562SDValue DAGCombiner::visitSELECT(SDNode *N) {
2563 SDValue N0 = N->getOperand(0);
2564 SDValue N1 = N->getOperand(1);
2565 SDValue N2 = N->getOperand(2);
Nate Begeman452d7be2005-09-16 00:54:12 +00002566 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2567 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2568 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002569 MVT VT = N->getValueType(0);
2570 MVT VT0 = N0.getValueType();
Nate Begeman44728a72005-09-19 22:34:01 +00002571
Nate Begeman452d7be2005-09-16 00:54:12 +00002572 // fold select C, X, X -> X
2573 if (N1 == N2)
2574 return N1;
2575 // fold select true, X, Y -> X
2576 if (N0C && !N0C->isNullValue())
2577 return N1;
2578 // fold select false, X, Y -> Y
2579 if (N0C && N0C->isNullValue())
2580 return N2;
2581 // fold select C, 1, X -> C | X
Duncan Sands83ec4b62008-06-06 12:08:01 +00002582 if (VT == MVT::i1 && N1C && N1C->getAPIntValue() == 1)
Nate Begeman452d7be2005-09-16 00:54:12 +00002583 return DAG.getNode(ISD::OR, VT, N0, N2);
Evan Cheng571c4782007-08-18 05:57:05 +00002584 // fold select C, 0, 1 -> ~C
Duncan Sands83ec4b62008-06-06 12:08:01 +00002585 if (VT.isInteger() && VT0.isInteger() &&
Dan Gohman002e5d02008-03-13 22:13:53 +00002586 N1C && N2C && N1C->isNullValue() && N2C->getAPIntValue() == 1) {
Dan Gohman475871a2008-07-27 21:46:04 +00002587 SDValue XORNode = DAG.getNode(ISD::XOR, VT0, N0, DAG.getConstant(1, VT0));
Evan Cheng571c4782007-08-18 05:57:05 +00002588 if (VT == VT0)
2589 return XORNode;
Gabor Greifba36cb52008-08-28 21:40:38 +00002590 AddToWorkList(XORNode.getNode());
Duncan Sands8e4eb092008-06-08 20:54:56 +00002591 if (VT.bitsGT(VT0))
Evan Cheng571c4782007-08-18 05:57:05 +00002592 return DAG.getNode(ISD::ZERO_EXTEND, VT, XORNode);
2593 return DAG.getNode(ISD::TRUNCATE, VT, XORNode);
2594 }
Nate Begeman452d7be2005-09-16 00:54:12 +00002595 // fold select C, 0, X -> ~C & X
Dale Johannesene0e6fac2007-12-06 17:53:31 +00002596 if (VT == VT0 && VT == MVT::i1 && N1C && N1C->isNullValue()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002597 SDValue XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Gabor Greifba36cb52008-08-28 21:40:38 +00002598 AddToWorkList(XORNode.getNode());
Nate Begeman452d7be2005-09-16 00:54:12 +00002599 return DAG.getNode(ISD::AND, VT, XORNode, N2);
2600 }
2601 // fold select C, X, 1 -> ~C | X
Dan Gohman002e5d02008-03-13 22:13:53 +00002602 if (VT == VT0 && VT == MVT::i1 && N2C && N2C->getAPIntValue() == 1) {
Dan Gohman475871a2008-07-27 21:46:04 +00002603 SDValue XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Gabor Greifba36cb52008-08-28 21:40:38 +00002604 AddToWorkList(XORNode.getNode());
Nate Begeman452d7be2005-09-16 00:54:12 +00002605 return DAG.getNode(ISD::OR, VT, XORNode, N1);
2606 }
2607 // fold select C, X, 0 -> C & X
2608 // FIXME: this should check for C type == X type, not i1?
Duncan Sands83ec4b62008-06-06 12:08:01 +00002609 if (VT == MVT::i1 && N2C && N2C->isNullValue())
Nate Begeman452d7be2005-09-16 00:54:12 +00002610 return DAG.getNode(ISD::AND, VT, N0, N1);
2611 // fold X ? X : Y --> X ? 1 : Y --> X | Y
Duncan Sands83ec4b62008-06-06 12:08:01 +00002612 if (VT == MVT::i1 && N0 == N1)
Nate Begeman452d7be2005-09-16 00:54:12 +00002613 return DAG.getNode(ISD::OR, VT, N0, N2);
2614 // fold X ? Y : X --> X ? Y : 0 --> X & Y
Duncan Sands83ec4b62008-06-06 12:08:01 +00002615 if (VT == MVT::i1 && N0 == N2)
Nate Begeman452d7be2005-09-16 00:54:12 +00002616 return DAG.getNode(ISD::AND, VT, N0, N1);
Chris Lattner729c6d12006-05-27 00:43:02 +00002617
Chris Lattner40c62d52005-10-18 06:04:22 +00002618 // If we can fold this based on the true/false value, do so.
2619 if (SimplifySelectOps(N, N1, N2))
Dan Gohman475871a2008-07-27 21:46:04 +00002620 return SDValue(N, 0); // Don't revisit N.
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002621
Nate Begeman44728a72005-09-19 22:34:01 +00002622 // fold selects based on a setcc into other things, such as min/max/abs
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002623 if (N0.getOpcode() == ISD::SETCC) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00002624 // FIXME:
2625 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
2626 // having to say they don't support SELECT_CC on every type the DAG knows
2627 // about, since there is no way to mark an opcode illegal at all value types
2628 if (TLI.isOperationLegal(ISD::SELECT_CC, MVT::Other))
2629 return DAG.getNode(ISD::SELECT_CC, VT, N0.getOperand(0), N0.getOperand(1),
2630 N1, N2, N0.getOperand(2));
2631 else
2632 return SimplifySelect(N0, N1, N2);
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002633 }
Dan Gohman475871a2008-07-27 21:46:04 +00002634 return SDValue();
Nate Begeman452d7be2005-09-16 00:54:12 +00002635}
2636
Dan Gohman475871a2008-07-27 21:46:04 +00002637SDValue DAGCombiner::visitSELECT_CC(SDNode *N) {
2638 SDValue N0 = N->getOperand(0);
2639 SDValue N1 = N->getOperand(1);
2640 SDValue N2 = N->getOperand(2);
2641 SDValue N3 = N->getOperand(3);
2642 SDValue N4 = N->getOperand(4);
Nate Begeman44728a72005-09-19 22:34:01 +00002643 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
2644
Nate Begeman44728a72005-09-19 22:34:01 +00002645 // fold select_cc lhs, rhs, x, x, cc -> x
2646 if (N2 == N3)
2647 return N2;
Chris Lattner40c62d52005-10-18 06:04:22 +00002648
Chris Lattner5f42a242006-09-20 06:19:26 +00002649 // Determine if the condition we're dealing with is constant
Dan Gohman475871a2008-07-27 21:46:04 +00002650 SDValue SCC = SimplifySetCC(TLI.getSetCCResultType(N0), N0, N1, CC, false);
Gabor Greifba36cb52008-08-28 21:40:38 +00002651 if (SCC.getNode()) AddToWorkList(SCC.getNode());
Chris Lattner5f42a242006-09-20 06:19:26 +00002652
Gabor Greifba36cb52008-08-28 21:40:38 +00002653 if (ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.getNode())) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002654 if (!SCCC->isNullValue())
Chris Lattner5f42a242006-09-20 06:19:26 +00002655 return N2; // cond always true -> true val
2656 else
2657 return N3; // cond always false -> false val
2658 }
2659
2660 // Fold to a simpler select_cc
Gabor Greifba36cb52008-08-28 21:40:38 +00002661 if (SCC.getNode() && SCC.getOpcode() == ISD::SETCC)
Chris Lattner5f42a242006-09-20 06:19:26 +00002662 return DAG.getNode(ISD::SELECT_CC, N2.getValueType(),
2663 SCC.getOperand(0), SCC.getOperand(1), N2, N3,
2664 SCC.getOperand(2));
2665
Chris Lattner40c62d52005-10-18 06:04:22 +00002666 // If we can fold this based on the true/false value, do so.
2667 if (SimplifySelectOps(N, N2, N3))
Dan Gohman475871a2008-07-27 21:46:04 +00002668 return SDValue(N, 0); // Don't revisit N.
Chris Lattner40c62d52005-10-18 06:04:22 +00002669
Nate Begeman44728a72005-09-19 22:34:01 +00002670 // fold select_cc into other things, such as min/max/abs
2671 return SimplifySelectCC(N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00002672}
2673
Dan Gohman475871a2008-07-27 21:46:04 +00002674SDValue DAGCombiner::visitSETCC(SDNode *N) {
Nate Begeman452d7be2005-09-16 00:54:12 +00002675 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
2676 cast<CondCodeSDNode>(N->getOperand(2))->get());
2677}
2678
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002679// ExtendUsesToFormExtLoad - Trying to extend uses of a load to enable this:
2680// "fold ({s|z}ext (load x)) -> ({s|z}ext (truncate ({s|z}extload x)))"
2681// transformation. Returns true if extension are possible and the above
2682// mentioned transformation is profitable.
Dan Gohman475871a2008-07-27 21:46:04 +00002683static bool ExtendUsesToFormExtLoad(SDNode *N, SDValue N0,
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002684 unsigned ExtOpc,
2685 SmallVector<SDNode*, 4> &ExtendNodes,
2686 TargetLowering &TLI) {
2687 bool HasCopyToRegUses = false;
2688 bool isTruncFree = TLI.isTruncateFree(N->getValueType(0), N0.getValueType());
Gabor Greifba36cb52008-08-28 21:40:38 +00002689 for (SDNode::use_iterator UI = N0.getNode()->use_begin(), UE = N0.getNode()->use_end();
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002690 UI != UE; ++UI) {
Dan Gohman89684502008-07-27 20:43:25 +00002691 SDNode *User = *UI;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002692 if (User == N)
2693 continue;
2694 // FIXME: Only extend SETCC N, N and SETCC N, c for now.
2695 if (User->getOpcode() == ISD::SETCC) {
2696 ISD::CondCode CC = cast<CondCodeSDNode>(User->getOperand(2))->get();
2697 if (ExtOpc == ISD::ZERO_EXTEND && ISD::isSignedIntSetCC(CC))
2698 // Sign bits will be lost after a zext.
2699 return false;
2700 bool Add = false;
2701 for (unsigned i = 0; i != 2; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002702 SDValue UseOp = User->getOperand(i);
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002703 if (UseOp == N0)
2704 continue;
2705 if (!isa<ConstantSDNode>(UseOp))
2706 return false;
2707 Add = true;
2708 }
2709 if (Add)
2710 ExtendNodes.push_back(User);
2711 } else {
2712 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002713 SDValue UseOp = User->getOperand(i);
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002714 if (UseOp == N0) {
2715 // If truncate from extended type to original load type is free
2716 // on this target, then it's ok to extend a CopyToReg.
2717 if (isTruncFree && User->getOpcode() == ISD::CopyToReg)
2718 HasCopyToRegUses = true;
2719 else
2720 return false;
2721 }
2722 }
2723 }
2724 }
2725
2726 if (HasCopyToRegUses) {
2727 bool BothLiveOut = false;
2728 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
2729 UI != UE; ++UI) {
Dan Gohman89684502008-07-27 20:43:25 +00002730 SDNode *User = *UI;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002731 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002732 SDValue UseOp = User->getOperand(i);
Gabor Greifba36cb52008-08-28 21:40:38 +00002733 if (UseOp.getNode() == N && UseOp.getResNo() == 0) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002734 BothLiveOut = true;
2735 break;
2736 }
2737 }
2738 }
2739 if (BothLiveOut)
2740 // Both unextended and extended values are live out. There had better be
2741 // good a reason for the transformation.
2742 return ExtendNodes.size();
2743 }
2744 return true;
2745}
2746
Dan Gohman475871a2008-07-27 21:46:04 +00002747SDValue DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
2748 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002749 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002750
Nate Begeman1d4d4142005-09-01 00:19:25 +00002751 // fold (sext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00002752 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002753 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0);
Chris Lattner310b5782006-05-06 23:06:26 +00002754
Nate Begeman1d4d4142005-09-01 00:19:25 +00002755 // fold (sext (sext x)) -> (sext x)
Chris Lattner310b5782006-05-06 23:06:26 +00002756 // fold (sext (aext x)) -> (sext x)
2757 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002758 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
Chris Lattner310b5782006-05-06 23:06:26 +00002759
Chris Lattner22558872007-02-26 03:13:59 +00002760 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman1fdfa6a2008-05-20 20:56:33 +00002761 // fold (sext (truncate (load x))) -> (sext (smaller load x))
2762 // fold (sext (truncate (srl (load x), c))) -> (sext (smaller load (x+c/n)))
Gabor Greifba36cb52008-08-28 21:40:38 +00002763 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
2764 if (NarrowLoad.getNode()) {
2765 if (NarrowLoad.getNode() != N0.getNode())
2766 CombineTo(N0.getNode(), NarrowLoad);
Evan Cheng0b063de2007-03-23 02:16:52 +00002767 return DAG.getNode(ISD::SIGN_EXTEND, VT, NarrowLoad);
2768 }
Evan Chengc88138f2007-03-22 01:54:19 +00002769
Dan Gohman1fdfa6a2008-05-20 20:56:33 +00002770 // See if the value being truncated is already sign extended. If so, just
2771 // eliminate the trunc/sext pair.
Dan Gohman475871a2008-07-27 21:46:04 +00002772 SDValue Op = N0.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002773 unsigned OpBits = Op.getValueType().getSizeInBits();
2774 unsigned MidBits = N0.getValueType().getSizeInBits();
2775 unsigned DestBits = VT.getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002776 unsigned NumSignBits = DAG.ComputeNumSignBits(Op);
Chris Lattner22558872007-02-26 03:13:59 +00002777
2778 if (OpBits == DestBits) {
2779 // Op is i32, Mid is i8, and Dest is i32. If Op has more than 24 sign
2780 // bits, it is already ready.
2781 if (NumSignBits > DestBits-MidBits)
2782 return Op;
2783 } else if (OpBits < DestBits) {
2784 // Op is i32, Mid is i8, and Dest is i64. If Op has more than 24 sign
2785 // bits, just sext from i32.
2786 if (NumSignBits > OpBits-MidBits)
2787 return DAG.getNode(ISD::SIGN_EXTEND, VT, Op);
2788 } else {
2789 // Op is i64, Mid is i8, and Dest is i32. If Op has more than 56 sign
2790 // bits, just truncate to i32.
2791 if (NumSignBits > OpBits-MidBits)
2792 return DAG.getNode(ISD::TRUNCATE, VT, Op);
Chris Lattner6007b842006-09-21 06:00:20 +00002793 }
Chris Lattner22558872007-02-26 03:13:59 +00002794
2795 // fold (sext (truncate x)) -> (sextinreg x).
2796 if (!AfterLegalize || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG,
2797 N0.getValueType())) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00002798 if (Op.getValueType().bitsLT(VT))
Chris Lattner22558872007-02-26 03:13:59 +00002799 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
Duncan Sands8e4eb092008-06-08 20:54:56 +00002800 else if (Op.getValueType().bitsGT(VT))
Chris Lattner22558872007-02-26 03:13:59 +00002801 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
2802 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, Op,
2803 DAG.getValueType(N0.getValueType()));
2804 }
Chris Lattner6007b842006-09-21 06:00:20 +00002805 }
Chris Lattner310b5782006-05-06 23:06:26 +00002806
Evan Cheng110dec22005-12-14 02:19:23 +00002807 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00002808 if (ISD::isNON_EXTLoad(N0.getNode()) &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002809 ((!AfterLegalize && !cast<LoadSDNode>(N0)->isVolatile()) ||
2810 TLI.isLoadXLegal(ISD::SEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002811 bool DoXform = true;
2812 SmallVector<SDNode*, 4> SetCCs;
2813 if (!N0.hasOneUse())
2814 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::SIGN_EXTEND, SetCCs, TLI);
2815 if (DoXform) {
2816 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman475871a2008-07-27 21:46:04 +00002817 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002818 LN0->getBasePtr(), LN0->getSrcValue(),
2819 LN0->getSrcValueOffset(),
2820 N0.getValueType(),
2821 LN0->isVolatile(),
2822 LN0->getAlignment());
2823 CombineTo(N, ExtLoad);
Dan Gohman475871a2008-07-27 21:46:04 +00002824 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00002825 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002826 // Extend SetCC uses if necessary.
2827 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
2828 SDNode *SetCC = SetCCs[i];
Dan Gohman475871a2008-07-27 21:46:04 +00002829 SmallVector<SDValue, 4> Ops;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002830 for (unsigned j = 0; j != 2; ++j) {
Dan Gohman475871a2008-07-27 21:46:04 +00002831 SDValue SOp = SetCC->getOperand(j);
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002832 if (SOp == Trunc)
2833 Ops.push_back(ExtLoad);
2834 else
2835 Ops.push_back(DAG.getNode(ISD::SIGN_EXTEND, VT, SOp));
2836 }
2837 Ops.push_back(SetCC->getOperand(2));
2838 CombineTo(SetCC, DAG.getNode(ISD::SETCC, SetCC->getValueType(0),
2839 &Ops[0], Ops.size()));
2840 }
Dan Gohman475871a2008-07-27 21:46:04 +00002841 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002842 }
Nate Begeman3df4d522005-10-12 20:40:40 +00002843 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002844
2845 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
2846 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00002847 if ((ISD::isSEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) &&
2848 ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002849 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002850 MVT EVT = LN0->getMemoryVT();
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002851 if ((!AfterLegalize && !LN0->isVolatile()) ||
2852 TLI.isLoadXLegal(ISD::SEXTLOAD, EVT)) {
Dan Gohman475871a2008-07-27 21:46:04 +00002853 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00002854 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002855 LN0->getSrcValueOffset(), EVT,
2856 LN0->isVolatile(),
2857 LN0->getAlignment());
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00002858 CombineTo(N, ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00002859 CombineTo(N0.getNode(), DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00002860 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00002861 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00002862 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002863 }
2864
Chris Lattner20a35c32007-04-11 05:32:27 +00002865 // sext(setcc x,y,cc) -> select_cc x, y, -1, 0, cc
2866 if (N0.getOpcode() == ISD::SETCC) {
Dan Gohman475871a2008-07-27 21:46:04 +00002867 SDValue SCC =
Chris Lattner1eba01e2007-04-11 06:50:51 +00002868 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
2869 DAG.getConstant(~0ULL, VT), DAG.getConstant(0, VT),
2870 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00002871 if (SCC.getNode()) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00002872 }
2873
Dan Gohman8f0ad582008-04-28 16:58:24 +00002874 // fold (sext x) -> (zext x) if the sign bit is known zero.
Dan Gohman187db7b2008-04-28 18:47:17 +00002875 if ((!AfterLegalize || TLI.isOperationLegal(ISD::ZERO_EXTEND, VT)) &&
2876 DAG.SignBitIsZero(N0))
Dan Gohman8f0ad582008-04-28 16:58:24 +00002877 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
2878
Dan Gohman475871a2008-07-27 21:46:04 +00002879 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002880}
2881
Dan Gohman475871a2008-07-27 21:46:04 +00002882SDValue DAGCombiner::visitZERO_EXTEND(SDNode *N) {
2883 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002884 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002885
Nate Begeman1d4d4142005-09-01 00:19:25 +00002886 // fold (zext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00002887 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002888 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002889 // fold (zext (zext x)) -> (zext x)
Chris Lattner310b5782006-05-06 23:06:26 +00002890 // fold (zext (aext x)) -> (zext x)
2891 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002892 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
Chris Lattner6007b842006-09-21 06:00:20 +00002893
Evan Chengc88138f2007-03-22 01:54:19 +00002894 // fold (zext (truncate (load x))) -> (zext (smaller load x))
2895 // fold (zext (truncate (srl (load x), c))) -> (zext (small load (x+c/n)))
Dale Johannesen2041a0e2007-03-30 21:38:07 +00002896 if (N0.getOpcode() == ISD::TRUNCATE) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002897 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
2898 if (NarrowLoad.getNode()) {
2899 if (NarrowLoad.getNode() != N0.getNode())
2900 CombineTo(N0.getNode(), NarrowLoad);
Evan Cheng0b063de2007-03-23 02:16:52 +00002901 return DAG.getNode(ISD::ZERO_EXTEND, VT, NarrowLoad);
2902 }
Evan Chengc88138f2007-03-22 01:54:19 +00002903 }
2904
Chris Lattner6007b842006-09-21 06:00:20 +00002905 // fold (zext (truncate x)) -> (and x, mask)
2906 if (N0.getOpcode() == ISD::TRUNCATE &&
2907 (!AfterLegalize || TLI.isOperationLegal(ISD::AND, VT))) {
Dan Gohman475871a2008-07-27 21:46:04 +00002908 SDValue Op = N0.getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00002909 if (Op.getValueType().bitsLT(VT)) {
Chris Lattner6007b842006-09-21 06:00:20 +00002910 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
Duncan Sands8e4eb092008-06-08 20:54:56 +00002911 } else if (Op.getValueType().bitsGT(VT)) {
Chris Lattner6007b842006-09-21 06:00:20 +00002912 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
2913 }
2914 return DAG.getZeroExtendInReg(Op, N0.getValueType());
2915 }
2916
Chris Lattner111c2282006-09-21 06:14:31 +00002917 // fold (zext (and (trunc x), cst)) -> (and x, cst).
2918 if (N0.getOpcode() == ISD::AND &&
2919 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
2920 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohman475871a2008-07-27 21:46:04 +00002921 SDValue X = N0.getOperand(0).getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00002922 if (X.getValueType().bitsLT(VT)) {
Chris Lattner111c2282006-09-21 06:14:31 +00002923 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
Duncan Sands8e4eb092008-06-08 20:54:56 +00002924 } else if (X.getValueType().bitsGT(VT)) {
Chris Lattner111c2282006-09-21 06:14:31 +00002925 X = DAG.getNode(ISD::TRUNCATE, VT, X);
2926 }
Dan Gohman220a8232008-03-03 23:51:38 +00002927 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002928 Mask.zext(VT.getSizeInBits());
Chris Lattner111c2282006-09-21 06:14:31 +00002929 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
2930 }
2931
Evan Cheng110dec22005-12-14 02:19:23 +00002932 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00002933 if (ISD::isNON_EXTLoad(N0.getNode()) &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002934 ((!AfterLegalize && !cast<LoadSDNode>(N0)->isVolatile()) ||
2935 TLI.isLoadXLegal(ISD::ZEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002936 bool DoXform = true;
2937 SmallVector<SDNode*, 4> SetCCs;
2938 if (!N0.hasOneUse())
2939 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ZERO_EXTEND, SetCCs, TLI);
2940 if (DoXform) {
2941 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman475871a2008-07-27 21:46:04 +00002942 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002943 LN0->getBasePtr(), LN0->getSrcValue(),
2944 LN0->getSrcValueOffset(),
2945 N0.getValueType(),
2946 LN0->isVolatile(),
2947 LN0->getAlignment());
2948 CombineTo(N, ExtLoad);
Dan Gohman475871a2008-07-27 21:46:04 +00002949 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00002950 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002951 // Extend SetCC uses if necessary.
2952 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
2953 SDNode *SetCC = SetCCs[i];
Dan Gohman475871a2008-07-27 21:46:04 +00002954 SmallVector<SDValue, 4> Ops;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002955 for (unsigned j = 0; j != 2; ++j) {
Dan Gohman475871a2008-07-27 21:46:04 +00002956 SDValue SOp = SetCC->getOperand(j);
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002957 if (SOp == Trunc)
2958 Ops.push_back(ExtLoad);
2959 else
Evan Chengde1631b2007-10-30 20:11:21 +00002960 Ops.push_back(DAG.getNode(ISD::ZERO_EXTEND, VT, SOp));
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002961 }
2962 Ops.push_back(SetCC->getOperand(2));
2963 CombineTo(SetCC, DAG.getNode(ISD::SETCC, SetCC->getValueType(0),
2964 &Ops[0], Ops.size()));
2965 }
Dan Gohman475871a2008-07-27 21:46:04 +00002966 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002967 }
Evan Cheng110dec22005-12-14 02:19:23 +00002968 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002969
2970 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
2971 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00002972 if ((ISD::isZEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) &&
2973 ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002974 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002975 MVT EVT = LN0->getMemoryVT();
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002976 if ((!AfterLegalize && !LN0->isVolatile()) ||
2977 TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT)) {
Dan Gohman475871a2008-07-27 21:46:04 +00002978 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002979 LN0->getBasePtr(), LN0->getSrcValue(),
2980 LN0->getSrcValueOffset(), EVT,
2981 LN0->isVolatile(),
2982 LN0->getAlignment());
2983 CombineTo(N, ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00002984 CombineTo(N0.getNode(), DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002985 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00002986 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002987 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002988 }
Chris Lattner20a35c32007-04-11 05:32:27 +00002989
2990 // zext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
2991 if (N0.getOpcode() == ISD::SETCC) {
Dan Gohman475871a2008-07-27 21:46:04 +00002992 SDValue SCC =
Chris Lattner20a35c32007-04-11 05:32:27 +00002993 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
2994 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattner1eba01e2007-04-11 06:50:51 +00002995 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00002996 if (SCC.getNode()) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00002997 }
2998
Dan Gohman475871a2008-07-27 21:46:04 +00002999 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003000}
3001
Dan Gohman475871a2008-07-27 21:46:04 +00003002SDValue DAGCombiner::visitANY_EXTEND(SDNode *N) {
3003 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003004 MVT VT = N->getValueType(0);
Chris Lattner5ffc0662006-05-05 05:58:59 +00003005
3006 // fold (aext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00003007 if (isa<ConstantSDNode>(N0))
Chris Lattner5ffc0662006-05-05 05:58:59 +00003008 return DAG.getNode(ISD::ANY_EXTEND, VT, N0);
3009 // fold (aext (aext x)) -> (aext x)
3010 // fold (aext (zext x)) -> (zext x)
3011 // fold (aext (sext x)) -> (sext x)
3012 if (N0.getOpcode() == ISD::ANY_EXTEND ||
3013 N0.getOpcode() == ISD::ZERO_EXTEND ||
3014 N0.getOpcode() == ISD::SIGN_EXTEND)
3015 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
3016
Evan Chengc88138f2007-03-22 01:54:19 +00003017 // fold (aext (truncate (load x))) -> (aext (smaller load x))
3018 // fold (aext (truncate (srl (load x), c))) -> (aext (small load (x+c/n)))
3019 if (N0.getOpcode() == ISD::TRUNCATE) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003020 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
3021 if (NarrowLoad.getNode()) {
3022 if (NarrowLoad.getNode() != N0.getNode())
3023 CombineTo(N0.getNode(), NarrowLoad);
Evan Cheng0b063de2007-03-23 02:16:52 +00003024 return DAG.getNode(ISD::ANY_EXTEND, VT, NarrowLoad);
3025 }
Evan Chengc88138f2007-03-22 01:54:19 +00003026 }
3027
Chris Lattner84750582006-09-20 06:29:17 +00003028 // fold (aext (truncate x))
3029 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman475871a2008-07-27 21:46:04 +00003030 SDValue TruncOp = N0.getOperand(0);
Chris Lattner84750582006-09-20 06:29:17 +00003031 if (TruncOp.getValueType() == VT)
3032 return TruncOp; // x iff x size == zext size.
Duncan Sands8e4eb092008-06-08 20:54:56 +00003033 if (TruncOp.getValueType().bitsGT(VT))
Chris Lattner84750582006-09-20 06:29:17 +00003034 return DAG.getNode(ISD::TRUNCATE, VT, TruncOp);
3035 return DAG.getNode(ISD::ANY_EXTEND, VT, TruncOp);
3036 }
Chris Lattner0e4b9222006-09-21 06:40:43 +00003037
3038 // fold (aext (and (trunc x), cst)) -> (and x, cst).
3039 if (N0.getOpcode() == ISD::AND &&
3040 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
3041 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohman475871a2008-07-27 21:46:04 +00003042 SDValue X = N0.getOperand(0).getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003043 if (X.getValueType().bitsLT(VT)) {
Chris Lattner0e4b9222006-09-21 06:40:43 +00003044 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003045 } else if (X.getValueType().bitsGT(VT)) {
Chris Lattner0e4b9222006-09-21 06:40:43 +00003046 X = DAG.getNode(ISD::TRUNCATE, VT, X);
3047 }
Dan Gohman220a8232008-03-03 23:51:38 +00003048 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003049 Mask.zext(VT.getSizeInBits());
Chris Lattner0e4b9222006-09-21 06:40:43 +00003050 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
3051 }
3052
Chris Lattner5ffc0662006-05-05 05:58:59 +00003053 // fold (aext (load x)) -> (aext (truncate (extload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00003054 if (ISD::isNON_EXTLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003055 ((!AfterLegalize && !cast<LoadSDNode>(N0)->isVolatile()) ||
3056 TLI.isLoadXLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003057 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman475871a2008-07-27 21:46:04 +00003058 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
Evan Cheng466685d2006-10-09 20:57:25 +00003059 LN0->getBasePtr(), LN0->getSrcValue(),
3060 LN0->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003061 N0.getValueType(),
3062 LN0->isVolatile(),
3063 LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00003064 CombineTo(N, ExtLoad);
Dan Gohman75dcf082008-07-31 00:50:31 +00003065 // Redirect any chain users to the new load.
Gabor Greifba36cb52008-08-28 21:40:38 +00003066 DAG.ReplaceAllUsesOfValueWith(SDValue(LN0, 1), SDValue(ExtLoad.getNode(), 1));
Dan Gohman75dcf082008-07-31 00:50:31 +00003067 // If any node needs the original loaded value, recompute it.
3068 if (!LN0->use_empty())
3069 CombineTo(LN0, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
3070 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003071 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner5ffc0662006-05-05 05:58:59 +00003072 }
3073
3074 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
3075 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
3076 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00003077 if (N0.getOpcode() == ISD::LOAD &&
Gabor Greifba36cb52008-08-28 21:40:38 +00003078 !ISD::isNON_EXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng466685d2006-10-09 20:57:25 +00003079 N0.hasOneUse()) {
3080 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003081 MVT EVT = LN0->getMemoryVT();
Dan Gohman475871a2008-07-27 21:46:04 +00003082 SDValue ExtLoad = DAG.getExtLoad(LN0->getExtensionType(), VT,
Evan Cheng466685d2006-10-09 20:57:25 +00003083 LN0->getChain(), LN0->getBasePtr(),
3084 LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003085 LN0->getSrcValueOffset(), EVT,
3086 LN0->isVolatile(),
3087 LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00003088 CombineTo(N, ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00003089 CombineTo(N0.getNode(), DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
Chris Lattner5ffc0662006-05-05 05:58:59 +00003090 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003091 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner5ffc0662006-05-05 05:58:59 +00003092 }
Chris Lattner20a35c32007-04-11 05:32:27 +00003093
3094 // aext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
3095 if (N0.getOpcode() == ISD::SETCC) {
Dan Gohman475871a2008-07-27 21:46:04 +00003096 SDValue SCC =
Chris Lattner1eba01e2007-04-11 06:50:51 +00003097 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
3098 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattnerc24bbad2007-04-11 16:51:53 +00003099 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00003100 if (SCC.getNode())
Chris Lattnerc56a81d2007-04-11 06:43:25 +00003101 return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00003102 }
3103
Dan Gohman475871a2008-07-27 21:46:04 +00003104 return SDValue();
Chris Lattner5ffc0662006-05-05 05:58:59 +00003105}
3106
Chris Lattner2b4c2792007-10-13 06:35:54 +00003107/// GetDemandedBits - See if the specified operand can be simplified with the
3108/// knowledge that only the bits specified by Mask are used. If so, return the
Dan Gohman475871a2008-07-27 21:46:04 +00003109/// simpler operand, otherwise return a null SDValue.
3110SDValue DAGCombiner::GetDemandedBits(SDValue V, const APInt &Mask) {
Chris Lattner2b4c2792007-10-13 06:35:54 +00003111 switch (V.getOpcode()) {
3112 default: break;
3113 case ISD::OR:
3114 case ISD::XOR:
3115 // If the LHS or RHS don't contribute bits to the or, drop them.
3116 if (DAG.MaskedValueIsZero(V.getOperand(0), Mask))
3117 return V.getOperand(1);
3118 if (DAG.MaskedValueIsZero(V.getOperand(1), Mask))
3119 return V.getOperand(0);
3120 break;
Chris Lattnere33544c2007-10-13 06:58:48 +00003121 case ISD::SRL:
3122 // Only look at single-use SRLs.
Gabor Greifba36cb52008-08-28 21:40:38 +00003123 if (!V.getNode()->hasOneUse())
Chris Lattnere33544c2007-10-13 06:58:48 +00003124 break;
3125 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
3126 // See if we can recursively simplify the LHS.
3127 unsigned Amt = RHSC->getValue();
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003128 APInt NewMask = Mask << Amt;
Dan Gohman475871a2008-07-27 21:46:04 +00003129 SDValue SimplifyLHS = GetDemandedBits(V.getOperand(0), NewMask);
Gabor Greifba36cb52008-08-28 21:40:38 +00003130 if (SimplifyLHS.getNode()) {
Chris Lattnere33544c2007-10-13 06:58:48 +00003131 return DAG.getNode(ISD::SRL, V.getValueType(),
3132 SimplifyLHS, V.getOperand(1));
3133 }
3134 }
Chris Lattner2b4c2792007-10-13 06:35:54 +00003135 }
Dan Gohman475871a2008-07-27 21:46:04 +00003136 return SDValue();
Chris Lattner2b4c2792007-10-13 06:35:54 +00003137}
3138
Evan Chengc88138f2007-03-22 01:54:19 +00003139/// ReduceLoadWidth - If the result of a wider load is shifted to right of N
3140/// bits and then truncated to a narrower type and where N is a multiple
3141/// of number of bits of the narrower type, transform it to a narrower load
3142/// from address + N / num of bits of new type. If the result is to be
3143/// extended, also fold the extension to form a extending load.
Dan Gohman475871a2008-07-27 21:46:04 +00003144SDValue DAGCombiner::ReduceLoadWidth(SDNode *N) {
Evan Chengc88138f2007-03-22 01:54:19 +00003145 unsigned Opc = N->getOpcode();
3146 ISD::LoadExtType ExtType = ISD::NON_EXTLOAD;
Dan Gohman475871a2008-07-27 21:46:04 +00003147 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003148 MVT VT = N->getValueType(0);
3149 MVT EVT = N->getValueType(0);
Evan Chengc88138f2007-03-22 01:54:19 +00003150
Dan Gohman7f8613e2008-08-14 20:04:46 +00003151 // This transformation isn't valid for vector loads.
3152 if (VT.isVector())
3153 return SDValue();
3154
Evan Chenge177e302007-03-23 22:13:36 +00003155 // Special case: SIGN_EXTEND_INREG is basically truncating to EVT then
3156 // extended to VT.
Evan Chengc88138f2007-03-22 01:54:19 +00003157 if (Opc == ISD::SIGN_EXTEND_INREG) {
3158 ExtType = ISD::SEXTLOAD;
3159 EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Evan Chenge177e302007-03-23 22:13:36 +00003160 if (AfterLegalize && !TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))
Dan Gohman475871a2008-07-27 21:46:04 +00003161 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00003162 }
3163
Duncan Sands83ec4b62008-06-06 12:08:01 +00003164 unsigned EVTBits = EVT.getSizeInBits();
Evan Chengc88138f2007-03-22 01:54:19 +00003165 unsigned ShAmt = 0;
Evan Chengb37b80c2007-03-23 20:55:21 +00003166 bool CombineSRL = false;
Evan Chengc88138f2007-03-22 01:54:19 +00003167 if (N0.getOpcode() == ISD::SRL && N0.hasOneUse()) {
3168 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
3169 ShAmt = N01->getValue();
3170 // Is the shift amount a multiple of size of VT?
3171 if ((ShAmt & (EVTBits-1)) == 0) {
3172 N0 = N0.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003173 if (N0.getValueType().getSizeInBits() <= EVTBits)
Dan Gohman475871a2008-07-27 21:46:04 +00003174 return SDValue();
Evan Chengb37b80c2007-03-23 20:55:21 +00003175 CombineSRL = true;
Evan Chengc88138f2007-03-22 01:54:19 +00003176 }
3177 }
3178 }
3179
Duncan Sandsad205a72008-06-16 08:14:38 +00003180 // Do not generate loads of non-round integer types since these can
3181 // be expensive (and would be wrong if the type is not byte sized).
Dan Gohman75dcf082008-07-31 00:50:31 +00003182 if (isa<LoadSDNode>(N0) && N0.hasOneUse() && VT.isRound() &&
3183 cast<LoadSDNode>(N0)->getMemoryVT().getSizeInBits() > EVTBits &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003184 // Do not change the width of a volatile load.
3185 !cast<LoadSDNode>(N0)->isVolatile()) {
Evan Chengc88138f2007-03-22 01:54:19 +00003186 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003187 MVT PtrType = N0.getOperand(1).getValueType();
Evan Chengdae54ce2007-03-24 00:02:43 +00003188 // For big endian targets, we need to adjust the offset to the pointer to
3189 // load the correct bytes.
Duncan Sands0753fc12008-02-11 10:37:04 +00003190 if (TLI.isBigEndian()) {
Dan Gohman75dcf082008-07-31 00:50:31 +00003191 unsigned LVTStoreBits = LN0->getMemoryVT().getStoreSizeInBits();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003192 unsigned EVTStoreBits = EVT.getStoreSizeInBits();
Duncan Sandsc6fa1702007-11-09 08:57:19 +00003193 ShAmt = LVTStoreBits - EVTStoreBits - ShAmt;
3194 }
Evan Chengdae54ce2007-03-24 00:02:43 +00003195 uint64_t PtrOff = ShAmt / 8;
Duncan Sandsdc846502007-10-28 12:59:45 +00003196 unsigned NewAlign = MinAlign(LN0->getAlignment(), PtrOff);
Dan Gohman475871a2008-07-27 21:46:04 +00003197 SDValue NewPtr = DAG.getNode(ISD::ADD, PtrType, LN0->getBasePtr(),
Evan Chengc88138f2007-03-22 01:54:19 +00003198 DAG.getConstant(PtrOff, PtrType));
Gabor Greifba36cb52008-08-28 21:40:38 +00003199 AddToWorkList(NewPtr.getNode());
Dan Gohman475871a2008-07-27 21:46:04 +00003200 SDValue Load = (ExtType == ISD::NON_EXTLOAD)
Evan Chengc88138f2007-03-22 01:54:19 +00003201 ? DAG.getLoad(VT, LN0->getChain(), NewPtr,
Dan Gohman75dcf082008-07-31 00:50:31 +00003202 LN0->getSrcValue(), LN0->getSrcValueOffset() + PtrOff,
Duncan Sandsdc846502007-10-28 12:59:45 +00003203 LN0->isVolatile(), NewAlign)
Evan Chengc88138f2007-03-22 01:54:19 +00003204 : DAG.getExtLoad(ExtType, VT, LN0->getChain(), NewPtr,
Dan Gohman75dcf082008-07-31 00:50:31 +00003205 LN0->getSrcValue(), LN0->getSrcValueOffset() + PtrOff,
3206 EVT, LN0->isVolatile(), NewAlign);
Evan Chengc88138f2007-03-22 01:54:19 +00003207 AddToWorkList(N);
Evan Chengb37b80c2007-03-23 20:55:21 +00003208 if (CombineSRL) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003209 WorkListRemover DeadNodes(*this);
3210 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), Load.getValue(1),
3211 &DeadNodes);
Gabor Greifba36cb52008-08-28 21:40:38 +00003212 CombineTo(N->getOperand(0).getNode(), Load);
Evan Chengb37b80c2007-03-23 20:55:21 +00003213 } else
Gabor Greifba36cb52008-08-28 21:40:38 +00003214 CombineTo(N0.getNode(), Load, Load.getValue(1));
Evan Cheng15213b72007-03-26 07:12:51 +00003215 if (ShAmt) {
3216 if (Opc == ISD::SIGN_EXTEND_INREG)
3217 return DAG.getNode(Opc, VT, Load, N->getOperand(1));
3218 else
3219 return DAG.getNode(Opc, VT, Load);
3220 }
Dan Gohman475871a2008-07-27 21:46:04 +00003221 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Chengc88138f2007-03-22 01:54:19 +00003222 }
3223
Dan Gohman475871a2008-07-27 21:46:04 +00003224 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00003225}
3226
Chris Lattner5ffc0662006-05-05 05:58:59 +00003227
Dan Gohman475871a2008-07-27 21:46:04 +00003228SDValue DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
3229 SDValue N0 = N->getOperand(0);
3230 SDValue N1 = N->getOperand(1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003231 MVT VT = N->getValueType(0);
3232 MVT EVT = cast<VTSDNode>(N1)->getVT();
3233 unsigned VTBits = VT.getSizeInBits();
3234 unsigned EVTBits = EVT.getSizeInBits();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003235
Nate Begeman1d4d4142005-09-01 00:19:25 +00003236 // fold (sext_in_reg c1) -> c1
Chris Lattnereaeda562006-05-08 20:59:41 +00003237 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
Chris Lattner310b5782006-05-06 23:06:26 +00003238 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0, N1);
Chris Lattneree4ea922006-05-06 09:30:03 +00003239
Chris Lattner541a24f2006-05-06 22:43:44 +00003240 // If the input is already sign extended, just drop the extension.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003241 if (DAG.ComputeNumSignBits(N0) >= VT.getSizeInBits()-EVTBits+1)
Chris Lattneree4ea922006-05-06 09:30:03 +00003242 return N0;
3243
Nate Begeman646d7e22005-09-02 21:18:40 +00003244 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
3245 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Duncan Sands8e4eb092008-06-08 20:54:56 +00003246 EVT.bitsLT(cast<VTSDNode>(N0.getOperand(1))->getVT())) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00003247 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00003248 }
Chris Lattner4b37e872006-05-08 21:18:59 +00003249
Dan Gohman75dcf082008-07-31 00:50:31 +00003250 // fold (sext_in_reg (sext x)) -> (sext x)
3251 // fold (sext_in_reg (aext x)) -> (sext x)
3252 // if x is small enough.
3253 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND) {
3254 SDValue N00 = N0.getOperand(0);
3255 if (N00.getValueType().getSizeInBits() < EVTBits)
3256 return DAG.getNode(ISD::SIGN_EXTEND, VT, N00, N1);
3257 }
3258
Chris Lattner95a5e052007-04-17 19:03:21 +00003259 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is known zero.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003260 if (DAG.MaskedValueIsZero(N0, APInt::getBitsSet(VTBits, EVTBits-1, EVTBits)))
Nate Begemande996292006-02-03 22:24:05 +00003261 return DAG.getZeroExtendInReg(N0, EVT);
Chris Lattner4b37e872006-05-08 21:18:59 +00003262
Chris Lattner95a5e052007-04-17 19:03:21 +00003263 // fold operands of sext_in_reg based on knowledge that the top bits are not
3264 // demanded.
Dan Gohman475871a2008-07-27 21:46:04 +00003265 if (SimplifyDemandedBits(SDValue(N, 0)))
3266 return SDValue(N, 0);
Chris Lattner95a5e052007-04-17 19:03:21 +00003267
Evan Chengc88138f2007-03-22 01:54:19 +00003268 // fold (sext_in_reg (load x)) -> (smaller sextload x)
3269 // fold (sext_in_reg (srl (load x), c)) -> (smaller sextload (x+c/evtbits))
Dan Gohman475871a2008-07-27 21:46:04 +00003270 SDValue NarrowLoad = ReduceLoadWidth(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003271 if (NarrowLoad.getNode())
Evan Chengc88138f2007-03-22 01:54:19 +00003272 return NarrowLoad;
3273
Chris Lattner4b37e872006-05-08 21:18:59 +00003274 // fold (sext_in_reg (srl X, 24), i8) -> sra X, 24
3275 // fold (sext_in_reg (srl X, 23), i8) -> sra X, 23 iff possible.
3276 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
3277 if (N0.getOpcode() == ISD::SRL) {
3278 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Duncan Sands83ec4b62008-06-06 12:08:01 +00003279 if (ShAmt->getValue()+EVTBits <= VT.getSizeInBits()) {
Chris Lattner4b37e872006-05-08 21:18:59 +00003280 // We can turn this into an SRA iff the input to the SRL is already sign
3281 // extended enough.
Dan Gohmanea859be2007-06-22 14:59:07 +00003282 unsigned InSignBits = DAG.ComputeNumSignBits(N0.getOperand(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00003283 if (VT.getSizeInBits()-(ShAmt->getValue()+EVTBits) < InSignBits)
Chris Lattner4b37e872006-05-08 21:18:59 +00003284 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0), N0.getOperand(1));
3285 }
3286 }
Evan Chengc88138f2007-03-22 01:54:19 +00003287
Nate Begemanded49632005-10-13 03:11:28 +00003288 // fold (sext_inreg (extload x)) -> (sextload x)
Gabor Greifba36cb52008-08-28 21:40:38 +00003289 if (ISD::isEXTLoad(N0.getNode()) &&
3290 ISD::isUNINDEXEDLoad(N0.getNode()) &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003291 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003292 ((!AfterLegalize && !cast<LoadSDNode>(N0)->isVolatile()) ||
3293 TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003294 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman475871a2008-07-27 21:46:04 +00003295 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
Evan Cheng466685d2006-10-09 20:57:25 +00003296 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003297 LN0->getSrcValueOffset(), EVT,
3298 LN0->isVolatile(),
3299 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00003300 CombineTo(N, ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00003301 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003302 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00003303 }
3304 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Gabor Greifba36cb52008-08-28 21:40:38 +00003305 if (ISD::isZEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng83060c52007-03-07 08:07:03 +00003306 N0.hasOneUse() &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003307 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003308 ((!AfterLegalize && !cast<LoadSDNode>(N0)->isVolatile()) ||
3309 TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003310 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman475871a2008-07-27 21:46:04 +00003311 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
Evan Cheng466685d2006-10-09 20:57:25 +00003312 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003313 LN0->getSrcValueOffset(), EVT,
3314 LN0->isVolatile(),
3315 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00003316 CombineTo(N, ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00003317 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003318 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00003319 }
Dan Gohman475871a2008-07-27 21:46:04 +00003320 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003321}
3322
Dan Gohman475871a2008-07-27 21:46:04 +00003323SDValue DAGCombiner::visitTRUNCATE(SDNode *N) {
3324 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003325 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003326
3327 // noop truncate
3328 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00003329 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003330 // fold (truncate c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00003331 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00003332 return DAG.getNode(ISD::TRUNCATE, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003333 // fold (truncate (truncate x)) -> (truncate x)
3334 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00003335 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003336 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
Chris Lattnerb72773b2006-05-05 22:56:26 +00003337 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND||
3338 N0.getOpcode() == ISD::ANY_EXTEND) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00003339 if (N0.getOperand(0).getValueType().bitsLT(VT))
Nate Begeman1d4d4142005-09-01 00:19:25 +00003340 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00003341 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Duncan Sands8e4eb092008-06-08 20:54:56 +00003342 else if (N0.getOperand(0).getValueType().bitsGT(VT))
Nate Begeman1d4d4142005-09-01 00:19:25 +00003343 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00003344 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003345 else
3346 // if the source and dest are the same type, we can drop both the extend
3347 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00003348 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003349 }
Evan Cheng007b69e2007-03-21 20:14:05 +00003350
Chris Lattner2b4c2792007-10-13 06:35:54 +00003351 // See if we can simplify the input to this truncate through knowledge that
3352 // only the low bits are being used. For example "trunc (or (shl x, 8), y)"
3353 // -> trunc y
Dan Gohman475871a2008-07-27 21:46:04 +00003354 SDValue Shorter =
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003355 GetDemandedBits(N0, APInt::getLowBitsSet(N0.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +00003356 VT.getSizeInBits()));
Gabor Greifba36cb52008-08-28 21:40:38 +00003357 if (Shorter.getNode())
Chris Lattner2b4c2792007-10-13 06:35:54 +00003358 return DAG.getNode(ISD::TRUNCATE, VT, Shorter);
3359
Nate Begeman3df4d522005-10-12 20:40:40 +00003360 // fold (truncate (load x)) -> (smaller load x)
Evan Cheng007b69e2007-03-21 20:14:05 +00003361 // fold (truncate (srl (load x), c)) -> (smaller load (x+c/evtbits))
Evan Chengc88138f2007-03-22 01:54:19 +00003362 return ReduceLoadWidth(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003363}
3364
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003365static SDNode *getBuildPairElt(SDNode *N, unsigned i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003366 SDValue Elt = N->getOperand(i);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003367 if (Elt.getOpcode() != ISD::MERGE_VALUES)
Gabor Greifba36cb52008-08-28 21:40:38 +00003368 return Elt.getNode();
3369 return Elt.getOperand(Elt.getResNo()).getNode();
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003370}
3371
3372/// CombineConsecutiveLoads - build_pair (load, load) -> load
3373/// if load locations are consecutive.
Dan Gohman475871a2008-07-27 21:46:04 +00003374SDValue DAGCombiner::CombineConsecutiveLoads(SDNode *N, MVT VT) {
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003375 assert(N->getOpcode() == ISD::BUILD_PAIR);
3376
3377 SDNode *LD1 = getBuildPairElt(N, 0);
3378 if (!ISD::isNON_EXTLoad(LD1) || !LD1->hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00003379 return SDValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003380 MVT LD1VT = LD1->getValueType(0);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003381 SDNode *LD2 = getBuildPairElt(N, 1);
3382 const MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3383 if (ISD::isNON_EXTLoad(LD2) &&
3384 LD2->hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003385 // If both are volatile this would reduce the number of volatile loads.
3386 // If one is volatile it might be ok, but play conservative and bail out.
3387 !cast<LoadSDNode>(LD1)->isVolatile() &&
3388 !cast<LoadSDNode>(LD2)->isVolatile() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003389 TLI.isConsecutiveLoad(LD2, LD1, LD1VT.getSizeInBits()/8, 1, MFI)) {
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003390 LoadSDNode *LD = cast<LoadSDNode>(LD1);
3391 unsigned Align = LD->getAlignment();
3392 unsigned NewAlign = TLI.getTargetMachine().getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00003393 getABITypeAlignment(VT.getTypeForMVT());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003394 if (NewAlign <= Align &&
3395 (!AfterLegalize || TLI.isOperationLegal(ISD::LOAD, VT)))
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003396 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(),
3397 LD->getSrcValue(), LD->getSrcValueOffset(),
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003398 false, Align);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003399 }
Dan Gohman475871a2008-07-27 21:46:04 +00003400 return SDValue();
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003401}
3402
Dan Gohman475871a2008-07-27 21:46:04 +00003403SDValue DAGCombiner::visitBIT_CONVERT(SDNode *N) {
3404 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003405 MVT VT = N->getValueType(0);
Chris Lattner94683772005-12-23 05:30:37 +00003406
Dan Gohman7f321562007-06-25 16:23:39 +00003407 // If the input is a BUILD_VECTOR with all constant elements, fold this now.
3408 // Only do this before legalize, since afterward the target may be depending
3409 // on the bitconvert.
3410 // First check to see if this is all constant.
3411 if (!AfterLegalize &&
Gabor Greifba36cb52008-08-28 21:40:38 +00003412 N0.getOpcode() == ISD::BUILD_VECTOR && N0.getNode()->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003413 VT.isVector()) {
Dan Gohman7f321562007-06-25 16:23:39 +00003414 bool isSimple = true;
3415 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i)
3416 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
3417 N0.getOperand(i).getOpcode() != ISD::Constant &&
3418 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
3419 isSimple = false;
3420 break;
3421 }
3422
Duncan Sands83ec4b62008-06-06 12:08:01 +00003423 MVT DestEltVT = N->getValueType(0).getVectorElementType();
3424 assert(!DestEltVT.isVector() &&
Dan Gohman7f321562007-06-25 16:23:39 +00003425 "Element type of vector ValueType must not be vector!");
3426 if (isSimple) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003427 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(N0.getNode(), DestEltVT);
Dan Gohman7f321562007-06-25 16:23:39 +00003428 }
3429 }
3430
Evan Cheng17a568b2008-08-29 22:21:44 +00003431 // If the input is a constant, let Val fold it.
Chris Lattner94683772005-12-23 05:30:37 +00003432 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
Dan Gohman475871a2008-07-27 21:46:04 +00003433 SDValue Res = DAG.getNode(ISD::BIT_CONVERT, VT, N0);
Gabor Greifba36cb52008-08-28 21:40:38 +00003434 if (Res.getNode() != N) return Res;
Chris Lattner94683772005-12-23 05:30:37 +00003435 }
3436
Chris Lattnerc8547d82005-12-23 05:37:50 +00003437 if (N0.getOpcode() == ISD::BIT_CONVERT) // conv(conv(x,t1),t2) -> conv(x,t2)
3438 return DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
Chris Lattner6258fb22006-04-02 02:53:43 +00003439
Chris Lattner57104102005-12-23 05:44:41 +00003440 // fold (conv (load x)) -> (load (conv*)x)
Evan Cheng513da432007-10-06 08:19:55 +00003441 // If the resultant load doesn't need a higher alignment than the original!
Gabor Greifba36cb52008-08-28 21:40:38 +00003442 if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003443 // Do not change the width of a volatile load.
3444 !cast<LoadSDNode>(N0)->isVolatile() &&
3445 (!AfterLegalize || TLI.isOperationLegal(ISD::LOAD, VT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003446 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng59d5b682007-05-07 21:27:48 +00003447 unsigned Align = TLI.getTargetMachine().getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00003448 getABITypeAlignment(VT.getTypeForMVT());
Evan Cheng59d5b682007-05-07 21:27:48 +00003449 unsigned OrigAlign = LN0->getAlignment();
3450 if (Align <= OrigAlign) {
Dan Gohman475871a2008-07-27 21:46:04 +00003451 SDValue Load = DAG.getLoad(VT, LN0->getChain(), LN0->getBasePtr(),
Evan Cheng59d5b682007-05-07 21:27:48 +00003452 LN0->getSrcValue(), LN0->getSrcValueOffset(),
Dan Gohman77455612008-06-28 00:45:22 +00003453 LN0->isVolatile(), OrigAlign);
Evan Cheng59d5b682007-05-07 21:27:48 +00003454 AddToWorkList(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003455 CombineTo(N0.getNode(), DAG.getNode(ISD::BIT_CONVERT, N0.getValueType(), Load),
Evan Cheng59d5b682007-05-07 21:27:48 +00003456 Load.getValue(1));
3457 return Load;
3458 }
Chris Lattner57104102005-12-23 05:44:41 +00003459 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003460
Chris Lattner3bd39d42008-01-27 17:42:27 +00003461 // Fold bitconvert(fneg(x)) -> xor(bitconvert(x), signbit)
3462 // Fold bitconvert(fabs(x)) -> and(bitconvert(x), ~signbit)
3463 // This often reduces constant pool loads.
3464 if ((N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FABS) &&
Gabor Greifba36cb52008-08-28 21:40:38 +00003465 N0.getNode()->hasOneUse() && VT.isInteger() && !VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003466 SDValue NewConv = DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00003467 AddToWorkList(NewConv.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003468
Duncan Sands83ec4b62008-06-06 12:08:01 +00003469 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003470 if (N0.getOpcode() == ISD::FNEG)
3471 return DAG.getNode(ISD::XOR, VT, NewConv, DAG.getConstant(SignBit, VT));
3472 assert(N0.getOpcode() == ISD::FABS);
3473 return DAG.getNode(ISD::AND, VT, NewConv, DAG.getConstant(~SignBit, VT));
3474 }
3475
3476 // Fold bitconvert(fcopysign(cst, x)) -> bitconvert(x)&sign | cst&~sign'
3477 // Note that we don't handle copysign(x,cst) because this can always be folded
3478 // to an fneg or fabs.
Gabor Greifba36cb52008-08-28 21:40:38 +00003479 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse() &&
Chris Lattnerf32aac32008-01-27 23:32:17 +00003480 isa<ConstantFPSDNode>(N0.getOperand(0)) &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003481 VT.isInteger() && !VT.isVector()) {
3482 unsigned OrigXWidth = N0.getOperand(1).getValueType().getSizeInBits();
Dan Gohman475871a2008-07-27 21:46:04 +00003483 SDValue X = DAG.getNode(ISD::BIT_CONVERT,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003484 MVT::getIntegerVT(OrigXWidth),
Chris Lattner3bd39d42008-01-27 17:42:27 +00003485 N0.getOperand(1));
Gabor Greifba36cb52008-08-28 21:40:38 +00003486 AddToWorkList(X.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003487
3488 // If X has a different width than the result/lhs, sext it or truncate it.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003489 unsigned VTWidth = VT.getSizeInBits();
Chris Lattner3bd39d42008-01-27 17:42:27 +00003490 if (OrigXWidth < VTWidth) {
3491 X = DAG.getNode(ISD::SIGN_EXTEND, VT, X);
Gabor Greifba36cb52008-08-28 21:40:38 +00003492 AddToWorkList(X.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003493 } else if (OrigXWidth > VTWidth) {
3494 // To get the sign bit in the right place, we have to shift it right
3495 // before truncating.
3496 X = DAG.getNode(ISD::SRL, X.getValueType(), X,
3497 DAG.getConstant(OrigXWidth-VTWidth, X.getValueType()));
Gabor Greifba36cb52008-08-28 21:40:38 +00003498 AddToWorkList(X.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003499 X = DAG.getNode(ISD::TRUNCATE, VT, X);
Gabor Greifba36cb52008-08-28 21:40:38 +00003500 AddToWorkList(X.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003501 }
3502
Duncan Sands83ec4b62008-06-06 12:08:01 +00003503 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003504 X = DAG.getNode(ISD::AND, VT, X, DAG.getConstant(SignBit, VT));
Gabor Greifba36cb52008-08-28 21:40:38 +00003505 AddToWorkList(X.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003506
Dan Gohman475871a2008-07-27 21:46:04 +00003507 SDValue Cst = DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
Chris Lattner3bd39d42008-01-27 17:42:27 +00003508 Cst = DAG.getNode(ISD::AND, VT, Cst, DAG.getConstant(~SignBit, VT));
Gabor Greifba36cb52008-08-28 21:40:38 +00003509 AddToWorkList(Cst.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003510
3511 return DAG.getNode(ISD::OR, VT, X, Cst);
3512 }
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003513
3514 // bitconvert(build_pair(ld, ld)) -> ld iff load locations are consecutive.
3515 if (N0.getOpcode() == ISD::BUILD_PAIR) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003516 SDValue CombineLD = CombineConsecutiveLoads(N0.getNode(), VT);
3517 if (CombineLD.getNode())
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003518 return CombineLD;
3519 }
Chris Lattner3bd39d42008-01-27 17:42:27 +00003520
Dan Gohman475871a2008-07-27 21:46:04 +00003521 return SDValue();
Chris Lattner94683772005-12-23 05:30:37 +00003522}
3523
Dan Gohman475871a2008-07-27 21:46:04 +00003524SDValue DAGCombiner::visitBUILD_PAIR(SDNode *N) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003525 MVT VT = N->getValueType(0);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003526 return CombineConsecutiveLoads(N, VT);
3527}
3528
Dan Gohman7f321562007-06-25 16:23:39 +00003529/// ConstantFoldBIT_CONVERTofBUILD_VECTOR - We know that BV is a build_vector
Chris Lattner6258fb22006-04-02 02:53:43 +00003530/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
3531/// destination element value type.
Dan Gohman475871a2008-07-27 21:46:04 +00003532SDValue DAGCombiner::
Duncan Sands83ec4b62008-06-06 12:08:01 +00003533ConstantFoldBIT_CONVERTofBUILD_VECTOR(SDNode *BV, MVT DstEltVT) {
3534 MVT SrcEltVT = BV->getOperand(0).getValueType();
Chris Lattner6258fb22006-04-02 02:53:43 +00003535
3536 // If this is already the right type, we're done.
Dan Gohman475871a2008-07-27 21:46:04 +00003537 if (SrcEltVT == DstEltVT) return SDValue(BV, 0);
Chris Lattner6258fb22006-04-02 02:53:43 +00003538
Duncan Sands83ec4b62008-06-06 12:08:01 +00003539 unsigned SrcBitSize = SrcEltVT.getSizeInBits();
3540 unsigned DstBitSize = DstEltVT.getSizeInBits();
Chris Lattner6258fb22006-04-02 02:53:43 +00003541
3542 // If this is a conversion of N elements of one type to N elements of another
3543 // type, convert each element. This handles FP<->INT cases.
3544 if (SrcBitSize == DstBitSize) {
Dan Gohman475871a2008-07-27 21:46:04 +00003545 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003546 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003547 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, DstEltVT, BV->getOperand(i)));
Gabor Greifba36cb52008-08-28 21:40:38 +00003548 AddToWorkList(Ops.back().getNode());
Chris Lattner3e104b12006-04-08 04:15:24 +00003549 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00003550 MVT VT = MVT::getVectorVT(DstEltVT,
3551 BV->getValueType(0).getVectorNumElements());
Dan Gohman7f321562007-06-25 16:23:39 +00003552 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003553 }
3554
3555 // Otherwise, we're growing or shrinking the elements. To avoid having to
3556 // handle annoying details of growing/shrinking FP values, we convert them to
3557 // int first.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003558 if (SrcEltVT.isFloatingPoint()) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003559 // Convert the input float vector to a int vector where the elements are the
3560 // same sizes.
3561 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
Duncan Sands8eab8a22008-06-09 11:32:28 +00003562 MVT IntVT = MVT::getIntegerVT(SrcEltVT.getSizeInBits());
Gabor Greifba36cb52008-08-28 21:40:38 +00003563 BV = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, IntVT).getNode();
Chris Lattner6258fb22006-04-02 02:53:43 +00003564 SrcEltVT = IntVT;
3565 }
3566
3567 // Now we know the input is an integer vector. If the output is a FP type,
3568 // convert to integer first, then to FP of the right size.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003569 if (DstEltVT.isFloatingPoint()) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003570 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
Duncan Sands8eab8a22008-06-09 11:32:28 +00003571 MVT TmpVT = MVT::getIntegerVT(DstEltVT.getSizeInBits());
Gabor Greifba36cb52008-08-28 21:40:38 +00003572 SDNode *Tmp = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, TmpVT).getNode();
Chris Lattner6258fb22006-04-02 02:53:43 +00003573
3574 // Next, convert to FP elements of the same size.
Dan Gohman7f321562007-06-25 16:23:39 +00003575 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(Tmp, DstEltVT);
Chris Lattner6258fb22006-04-02 02:53:43 +00003576 }
3577
3578 // Okay, we know the src/dst types are both integers of differing types.
3579 // Handling growing first.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003580 assert(SrcEltVT.isInteger() && DstEltVT.isInteger());
Chris Lattner6258fb22006-04-02 02:53:43 +00003581 if (SrcBitSize < DstBitSize) {
3582 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
3583
Dan Gohman475871a2008-07-27 21:46:04 +00003584 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003585 for (unsigned i = 0, e = BV->getNumOperands(); i != e;
Chris Lattner6258fb22006-04-02 02:53:43 +00003586 i += NumInputsPerOutput) {
3587 bool isLE = TLI.isLittleEndian();
Dan Gohman220a8232008-03-03 23:51:38 +00003588 APInt NewBits = APInt(DstBitSize, 0);
Chris Lattner6258fb22006-04-02 02:53:43 +00003589 bool EltIsUndef = true;
3590 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
3591 // Shift the previously computed bits over.
3592 NewBits <<= SrcBitSize;
Dan Gohman475871a2008-07-27 21:46:04 +00003593 SDValue Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
Chris Lattner6258fb22006-04-02 02:53:43 +00003594 if (Op.getOpcode() == ISD::UNDEF) continue;
3595 EltIsUndef = false;
3596
Dan Gohman220a8232008-03-03 23:51:38 +00003597 NewBits |=
3598 APInt(cast<ConstantSDNode>(Op)->getAPIntValue()).zext(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00003599 }
3600
3601 if (EltIsUndef)
3602 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
3603 else
3604 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
3605 }
3606
Duncan Sands83ec4b62008-06-06 12:08:01 +00003607 MVT VT = MVT::getVectorVT(DstEltVT, Ops.size());
Dan Gohman7f321562007-06-25 16:23:39 +00003608 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003609 }
3610
3611 // Finally, this must be the case where we are shrinking elements: each input
3612 // turns into multiple outputs.
Evan Chengefec7512008-02-18 23:04:32 +00003613 bool isS2V = ISD::isScalarToVector(BV);
Chris Lattner6258fb22006-04-02 02:53:43 +00003614 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003615 MVT VT = MVT::getVectorVT(DstEltVT, NumOutputsPerInput*BV->getNumOperands());
Dan Gohman475871a2008-07-27 21:46:04 +00003616 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003617 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003618 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
3619 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
3620 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
3621 continue;
3622 }
Dan Gohman220a8232008-03-03 23:51:38 +00003623 APInt OpVal = cast<ConstantSDNode>(BV->getOperand(i))->getAPIntValue();
Chris Lattner6258fb22006-04-02 02:53:43 +00003624 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
Dan Gohman220a8232008-03-03 23:51:38 +00003625 APInt ThisVal = APInt(OpVal).trunc(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00003626 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
Dan Gohman220a8232008-03-03 23:51:38 +00003627 if (isS2V && i == 0 && j == 0 && APInt(ThisVal).zext(SrcBitSize) == OpVal)
Evan Chengefec7512008-02-18 23:04:32 +00003628 // Simply turn this into a SCALAR_TO_VECTOR of the new type.
3629 return DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Ops[0]);
Dan Gohman220a8232008-03-03 23:51:38 +00003630 OpVal = OpVal.lshr(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00003631 }
3632
3633 // For big endian targets, swap the order of the pieces of each element.
Duncan Sands0753fc12008-02-11 10:37:04 +00003634 if (TLI.isBigEndian())
Chris Lattner6258fb22006-04-02 02:53:43 +00003635 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
3636 }
Dan Gohman7f321562007-06-25 16:23:39 +00003637 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003638}
3639
3640
3641
Dan Gohman475871a2008-07-27 21:46:04 +00003642SDValue DAGCombiner::visitFADD(SDNode *N) {
3643 SDValue N0 = N->getOperand(0);
3644 SDValue N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003645 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3646 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003647 MVT VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00003648
Dan Gohman7f321562007-06-25 16:23:39 +00003649 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003650 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003651 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003652 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00003653 }
Dan Gohman7f321562007-06-25 16:23:39 +00003654
Nate Begemana0e221d2005-10-18 00:28:13 +00003655 // fold (fadd c1, c2) -> c1+c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003656 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003657 return DAG.getNode(ISD::FADD, VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003658 // canonicalize constant to RHS
3659 if (N0CFP && !N1CFP)
3660 return DAG.getNode(ISD::FADD, VT, N1, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00003661 // fold (A + (-B)) -> A-B
Chris Lattner0254e702008-02-26 07:04:54 +00003662 if (isNegatibleForFree(N1, AfterLegalize) == 2)
3663 return DAG.getNode(ISD::FSUB, VT, N0,
3664 GetNegatedExpression(N1, DAG, AfterLegalize));
Chris Lattner01b3d732005-09-28 22:28:18 +00003665 // fold ((-A) + B) -> B-A
Chris Lattner0254e702008-02-26 07:04:54 +00003666 if (isNegatibleForFree(N0, AfterLegalize) == 2)
3667 return DAG.getNode(ISD::FSUB, VT, N1,
3668 GetNegatedExpression(N0, DAG, AfterLegalize));
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003669
3670 // If allowed, fold (fadd (fadd x, c1), c2) -> (fadd x, (fadd c1, c2))
3671 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FADD &&
Gabor Greifba36cb52008-08-28 21:40:38 +00003672 N0.getNode()->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003673 return DAG.getNode(ISD::FADD, VT, N0.getOperand(0),
3674 DAG.getNode(ISD::FADD, VT, N0.getOperand(1), N1));
3675
Dan Gohman475871a2008-07-27 21:46:04 +00003676 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00003677}
3678
Dan Gohman475871a2008-07-27 21:46:04 +00003679SDValue DAGCombiner::visitFSUB(SDNode *N) {
3680 SDValue N0 = N->getOperand(0);
3681 SDValue N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003682 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3683 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003684 MVT VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00003685
Dan Gohman7f321562007-06-25 16:23:39 +00003686 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003687 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003688 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003689 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00003690 }
Dan Gohman7f321562007-06-25 16:23:39 +00003691
Nate Begemana0e221d2005-10-18 00:28:13 +00003692 // fold (fsub c1, c2) -> c1-c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003693 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003694 return DAG.getNode(ISD::FSUB, VT, N0, N1);
Dan Gohman23ff1822007-07-02 15:48:56 +00003695 // fold (0-B) -> -B
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003696 if (UnsafeFPMath && N0CFP && N0CFP->getValueAPF().isZero()) {
Chris Lattner0254e702008-02-26 07:04:54 +00003697 if (isNegatibleForFree(N1, AfterLegalize))
3698 return GetNegatedExpression(N1, DAG, AfterLegalize);
Dan Gohman23ff1822007-07-02 15:48:56 +00003699 return DAG.getNode(ISD::FNEG, VT, N1);
3700 }
Chris Lattner01b3d732005-09-28 22:28:18 +00003701 // fold (A-(-B)) -> A+B
Chris Lattner0254e702008-02-26 07:04:54 +00003702 if (isNegatibleForFree(N1, AfterLegalize))
3703 return DAG.getNode(ISD::FADD, VT, N0,
3704 GetNegatedExpression(N1, DAG, AfterLegalize));
Chris Lattner29446522007-05-14 22:04:50 +00003705
Dan Gohman475871a2008-07-27 21:46:04 +00003706 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00003707}
3708
Dan Gohman475871a2008-07-27 21:46:04 +00003709SDValue DAGCombiner::visitFMUL(SDNode *N) {
3710 SDValue N0 = N->getOperand(0);
3711 SDValue N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003712 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3713 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003714 MVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00003715
Dan Gohman7f321562007-06-25 16:23:39 +00003716 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003717 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003718 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003719 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00003720 }
Dan Gohman7f321562007-06-25 16:23:39 +00003721
Nate Begeman11af4ea2005-10-17 20:40:11 +00003722 // fold (fmul c1, c2) -> c1*c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003723 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003724 return DAG.getNode(ISD::FMUL, VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003725 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00003726 if (N0CFP && !N1CFP)
3727 return DAG.getNode(ISD::FMUL, VT, N1, N0);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003728 // fold (fmul X, 2.0) -> (fadd X, X)
3729 if (N1CFP && N1CFP->isExactlyValue(+2.0))
3730 return DAG.getNode(ISD::FADD, VT, N0, N0);
Chris Lattner29446522007-05-14 22:04:50 +00003731 // fold (fmul X, -1.0) -> (fneg X)
3732 if (N1CFP && N1CFP->isExactlyValue(-1.0))
3733 return DAG.getNode(ISD::FNEG, VT, N0);
3734
3735 // -X * -Y -> X*Y
Chris Lattner0254e702008-02-26 07:04:54 +00003736 if (char LHSNeg = isNegatibleForFree(N0, AfterLegalize)) {
3737 if (char RHSNeg = isNegatibleForFree(N1, AfterLegalize)) {
Chris Lattner29446522007-05-14 22:04:50 +00003738 // Both can be negated for free, check to see if at least one is cheaper
3739 // negated.
3740 if (LHSNeg == 2 || RHSNeg == 2)
Chris Lattner0254e702008-02-26 07:04:54 +00003741 return DAG.getNode(ISD::FMUL, VT,
3742 GetNegatedExpression(N0, DAG, AfterLegalize),
3743 GetNegatedExpression(N1, DAG, AfterLegalize));
Chris Lattner29446522007-05-14 22:04:50 +00003744 }
3745 }
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003746
3747 // If allowed, fold (fmul (fmul x, c1), c2) -> (fmul x, (fmul c1, c2))
3748 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FMUL &&
Gabor Greifba36cb52008-08-28 21:40:38 +00003749 N0.getNode()->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003750 return DAG.getNode(ISD::FMUL, VT, N0.getOperand(0),
3751 DAG.getNode(ISD::FMUL, VT, N0.getOperand(1), N1));
3752
Dan Gohman475871a2008-07-27 21:46:04 +00003753 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00003754}
3755
Dan Gohman475871a2008-07-27 21:46:04 +00003756SDValue DAGCombiner::visitFDIV(SDNode *N) {
3757 SDValue N0 = N->getOperand(0);
3758 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003759 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3760 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003761 MVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00003762
Dan Gohman7f321562007-06-25 16:23:39 +00003763 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003764 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003765 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003766 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00003767 }
Dan Gohman7f321562007-06-25 16:23:39 +00003768
Nate Begemana148d982006-01-18 22:35:16 +00003769 // fold (fdiv c1, c2) -> c1/c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003770 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003771 return DAG.getNode(ISD::FDIV, VT, N0, N1);
Chris Lattner29446522007-05-14 22:04:50 +00003772
3773
3774 // -X / -Y -> X*Y
Chris Lattner0254e702008-02-26 07:04:54 +00003775 if (char LHSNeg = isNegatibleForFree(N0, AfterLegalize)) {
3776 if (char RHSNeg = isNegatibleForFree(N1, AfterLegalize)) {
Chris Lattner29446522007-05-14 22:04:50 +00003777 // Both can be negated for free, check to see if at least one is cheaper
3778 // negated.
3779 if (LHSNeg == 2 || RHSNeg == 2)
Chris Lattner0254e702008-02-26 07:04:54 +00003780 return DAG.getNode(ISD::FDIV, VT,
3781 GetNegatedExpression(N0, DAG, AfterLegalize),
3782 GetNegatedExpression(N1, DAG, AfterLegalize));
Chris Lattner29446522007-05-14 22:04:50 +00003783 }
3784 }
3785
Dan Gohman475871a2008-07-27 21:46:04 +00003786 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00003787}
3788
Dan Gohman475871a2008-07-27 21:46:04 +00003789SDValue DAGCombiner::visitFREM(SDNode *N) {
3790 SDValue N0 = N->getOperand(0);
3791 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003792 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3793 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003794 MVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00003795
Nate Begemana148d982006-01-18 22:35:16 +00003796 // fold (frem c1, c2) -> fmod(c1,c2)
Dale Johannesendb44bf82007-10-16 23:38:29 +00003797 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003798 return DAG.getNode(ISD::FREM, VT, N0, N1);
Dan Gohman7f321562007-06-25 16:23:39 +00003799
Dan Gohman475871a2008-07-27 21:46:04 +00003800 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00003801}
3802
Dan Gohman475871a2008-07-27 21:46:04 +00003803SDValue DAGCombiner::visitFCOPYSIGN(SDNode *N) {
3804 SDValue N0 = N->getOperand(0);
3805 SDValue N1 = N->getOperand(1);
Chris Lattner12d83032006-03-05 05:30:57 +00003806 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3807 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003808 MVT VT = N->getValueType(0);
Chris Lattner12d83032006-03-05 05:30:57 +00003809
Dale Johannesendb44bf82007-10-16 23:38:29 +00003810 if (N0CFP && N1CFP && VT != MVT::ppcf128) // Constant fold
Chris Lattner12d83032006-03-05 05:30:57 +00003811 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1);
3812
3813 if (N1CFP) {
Dale Johannesene6c17422007-08-26 01:18:27 +00003814 const APFloat& V = N1CFP->getValueAPF();
Chris Lattner12d83032006-03-05 05:30:57 +00003815 // copysign(x, c1) -> fabs(x) iff ispos(c1)
3816 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
Dale Johannesen87503a62007-08-25 22:10:57 +00003817 if (!V.isNegative())
Chris Lattner12d83032006-03-05 05:30:57 +00003818 return DAG.getNode(ISD::FABS, VT, N0);
3819 else
3820 return DAG.getNode(ISD::FNEG, VT, DAG.getNode(ISD::FABS, VT, N0));
3821 }
3822
3823 // copysign(fabs(x), y) -> copysign(x, y)
3824 // copysign(fneg(x), y) -> copysign(x, y)
3825 // copysign(copysign(x,z), y) -> copysign(x, y)
3826 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
3827 N0.getOpcode() == ISD::FCOPYSIGN)
3828 return DAG.getNode(ISD::FCOPYSIGN, VT, N0.getOperand(0), N1);
3829
3830 // copysign(x, abs(y)) -> abs(x)
3831 if (N1.getOpcode() == ISD::FABS)
3832 return DAG.getNode(ISD::FABS, VT, N0);
3833
3834 // copysign(x, copysign(y,z)) -> copysign(x, z)
3835 if (N1.getOpcode() == ISD::FCOPYSIGN)
3836 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(1));
3837
3838 // copysign(x, fp_extend(y)) -> copysign(x, y)
3839 // copysign(x, fp_round(y)) -> copysign(x, y)
3840 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
3841 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(0));
3842
Dan Gohman475871a2008-07-27 21:46:04 +00003843 return SDValue();
Chris Lattner12d83032006-03-05 05:30:57 +00003844}
3845
3846
Chris Lattner01b3d732005-09-28 22:28:18 +00003847
Dan Gohman475871a2008-07-27 21:46:04 +00003848SDValue DAGCombiner::visitSINT_TO_FP(SDNode *N) {
3849 SDValue N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003850 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003851 MVT VT = N->getValueType(0);
Chris Lattnercda88752008-06-26 00:16:49 +00003852 MVT OpVT = N0.getValueType();
3853
Nate Begeman1d4d4142005-09-01 00:19:25 +00003854 // fold (sint_to_fp c1) -> c1fp
Chris Lattnercda88752008-06-26 00:16:49 +00003855 if (N0C && OpVT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003856 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
Chris Lattnercda88752008-06-26 00:16:49 +00003857
3858 // If the input is a legal type, and SINT_TO_FP is not legal on this target,
3859 // but UINT_TO_FP is legal on this target, try to convert.
Chris Lattnerf77e46b2008-06-26 17:16:00 +00003860 if (!TLI.isOperationLegal(ISD::SINT_TO_FP, OpVT) &&
Chris Lattnercda88752008-06-26 00:16:49 +00003861 TLI.isOperationLegal(ISD::UINT_TO_FP, OpVT)) {
3862 // If the sign bit is known to be zero, we can change this to UINT_TO_FP.
3863 if (DAG.SignBitIsZero(N0))
3864 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
3865 }
3866
3867
Dan Gohman475871a2008-07-27 21:46:04 +00003868 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003869}
3870
Dan Gohman475871a2008-07-27 21:46:04 +00003871SDValue DAGCombiner::visitUINT_TO_FP(SDNode *N) {
3872 SDValue N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003873 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003874 MVT VT = N->getValueType(0);
Chris Lattnercda88752008-06-26 00:16:49 +00003875 MVT OpVT = N0.getValueType();
Nate Begemana148d982006-01-18 22:35:16 +00003876
Nate Begeman1d4d4142005-09-01 00:19:25 +00003877 // fold (uint_to_fp c1) -> c1fp
Chris Lattnercda88752008-06-26 00:16:49 +00003878 if (N0C && OpVT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003879 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
Chris Lattnercda88752008-06-26 00:16:49 +00003880
3881 // If the input is a legal type, and UINT_TO_FP is not legal on this target,
3882 // but SINT_TO_FP is legal on this target, try to convert.
Chris Lattnerf77e46b2008-06-26 17:16:00 +00003883 if (!TLI.isOperationLegal(ISD::UINT_TO_FP, OpVT) &&
Chris Lattnercda88752008-06-26 00:16:49 +00003884 TLI.isOperationLegal(ISD::SINT_TO_FP, OpVT)) {
3885 // If the sign bit is known to be zero, we can change this to SINT_TO_FP.
3886 if (DAG.SignBitIsZero(N0))
3887 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
3888 }
3889
Dan Gohman475871a2008-07-27 21:46:04 +00003890 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003891}
3892
Dan Gohman475871a2008-07-27 21:46:04 +00003893SDValue DAGCombiner::visitFP_TO_SINT(SDNode *N) {
3894 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00003895 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003896 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003897
3898 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00003899 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00003900 return DAG.getNode(ISD::FP_TO_SINT, VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00003901 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003902}
3903
Dan Gohman475871a2008-07-27 21:46:04 +00003904SDValue DAGCombiner::visitFP_TO_UINT(SDNode *N) {
3905 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00003906 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003907 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003908
3909 // fold (fp_to_uint c1fp) -> c1
Dale Johannesendb44bf82007-10-16 23:38:29 +00003910 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003911 return DAG.getNode(ISD::FP_TO_UINT, VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00003912 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003913}
3914
Dan Gohman475871a2008-07-27 21:46:04 +00003915SDValue DAGCombiner::visitFP_ROUND(SDNode *N) {
3916 SDValue N0 = N->getOperand(0);
3917 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003918 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003919 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003920
3921 // fold (fp_round c1fp) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00003922 if (N0CFP && N0.getValueType() != MVT::ppcf128)
Chris Lattner0bd48932008-01-17 07:00:52 +00003923 return DAG.getNode(ISD::FP_ROUND, VT, N0, N1);
Chris Lattner79dbea52006-03-13 06:26:26 +00003924
3925 // fold (fp_round (fp_extend x)) -> x
3926 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
3927 return N0.getOperand(0);
3928
Chris Lattner0aa5e6f2008-01-24 06:45:35 +00003929 // fold (fp_round (fp_round x)) -> (fp_round x)
3930 if (N0.getOpcode() == ISD::FP_ROUND) {
3931 // This is a value preserving truncation if both round's are.
3932 bool IsTrunc = N->getConstantOperandVal(1) == 1 &&
Gabor Greifba36cb52008-08-28 21:40:38 +00003933 N0.getNode()->getConstantOperandVal(1) == 1;
Chris Lattner0aa5e6f2008-01-24 06:45:35 +00003934 return DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0),
3935 DAG.getIntPtrConstant(IsTrunc));
3936 }
3937
Chris Lattner79dbea52006-03-13 06:26:26 +00003938 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
Gabor Greifba36cb52008-08-28 21:40:38 +00003939 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003940 SDValue Tmp = DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0), N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00003941 AddToWorkList(Tmp.getNode());
Chris Lattner79dbea52006-03-13 06:26:26 +00003942 return DAG.getNode(ISD::FCOPYSIGN, VT, Tmp, N0.getOperand(1));
3943 }
3944
Dan Gohman475871a2008-07-27 21:46:04 +00003945 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003946}
3947
Dan Gohman475871a2008-07-27 21:46:04 +00003948SDValue DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
3949 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003950 MVT VT = N->getValueType(0);
3951 MVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00003952 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003953
Nate Begeman1d4d4142005-09-01 00:19:25 +00003954 // fold (fp_round_inreg c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00003955 if (N0CFP) {
Dan Gohman475871a2008-07-27 21:46:04 +00003956 SDValue Round = DAG.getConstantFP(N0CFP->getValueAPF(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003957 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003958 }
Dan Gohman475871a2008-07-27 21:46:04 +00003959 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003960}
3961
Dan Gohman475871a2008-07-27 21:46:04 +00003962SDValue DAGCombiner::visitFP_EXTEND(SDNode *N) {
3963 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00003964 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003965 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003966
Chris Lattner5938bef2007-12-29 06:55:23 +00003967 // If this is fp_round(fpextend), don't fold it, allow ourselves to be folded.
Roman Levenstein9cac5252008-04-16 16:15:27 +00003968 if (N->hasOneUse() &&
Dan Gohman475871a2008-07-27 21:46:04 +00003969 N->use_begin().getUse().getSDValue().getOpcode() == ISD::FP_ROUND)
3970 return SDValue();
Chris Lattner0bd48932008-01-17 07:00:52 +00003971
Nate Begeman1d4d4142005-09-01 00:19:25 +00003972 // fold (fp_extend c1fp) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00003973 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003974 return DAG.getNode(ISD::FP_EXTEND, VT, N0);
Chris Lattner0bd48932008-01-17 07:00:52 +00003975
3976 // Turn fp_extend(fp_round(X, 1)) -> x since the fp_round doesn't affect the
3977 // value of X.
Gabor Greifba36cb52008-08-28 21:40:38 +00003978 if (N0.getOpcode() == ISD::FP_ROUND && N0.getNode()->getConstantOperandVal(1) == 1){
Dan Gohman475871a2008-07-27 21:46:04 +00003979 SDValue In = N0.getOperand(0);
Chris Lattner0bd48932008-01-17 07:00:52 +00003980 if (In.getValueType() == VT) return In;
Duncan Sands8e4eb092008-06-08 20:54:56 +00003981 if (VT.bitsLT(In.getValueType()))
Chris Lattner0bd48932008-01-17 07:00:52 +00003982 return DAG.getNode(ISD::FP_ROUND, VT, In, N0.getOperand(1));
3983 return DAG.getNode(ISD::FP_EXTEND, VT, In);
3984 }
3985
3986 // fold (fpext (load x)) -> (fpext (fptrunc (extload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00003987 if (ISD::isNON_EXTLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003988 ((!AfterLegalize && !cast<LoadSDNode>(N0)->isVolatile()) ||
3989 TLI.isLoadXLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003990 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman475871a2008-07-27 21:46:04 +00003991 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
Evan Cheng466685d2006-10-09 20:57:25 +00003992 LN0->getBasePtr(), LN0->getSrcValue(),
3993 LN0->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003994 N0.getValueType(),
3995 LN0->isVolatile(),
3996 LN0->getAlignment());
Chris Lattnere564dbb2006-05-05 21:34:35 +00003997 CombineTo(N, ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00003998 CombineTo(N0.getNode(), DAG.getNode(ISD::FP_ROUND, N0.getValueType(), ExtLoad,
Chris Lattner0bd48932008-01-17 07:00:52 +00003999 DAG.getIntPtrConstant(1)),
Chris Lattnere564dbb2006-05-05 21:34:35 +00004000 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004001 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnere564dbb2006-05-05 21:34:35 +00004002 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004003
Dan Gohman475871a2008-07-27 21:46:04 +00004004 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004005}
4006
Dan Gohman475871a2008-07-27 21:46:04 +00004007SDValue DAGCombiner::visitFNEG(SDNode *N) {
4008 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00004009
Chris Lattner0254e702008-02-26 07:04:54 +00004010 if (isNegatibleForFree(N0, AfterLegalize))
4011 return GetNegatedExpression(N0, DAG, AfterLegalize);
Dan Gohman23ff1822007-07-02 15:48:56 +00004012
Chris Lattner3bd39d42008-01-27 17:42:27 +00004013 // Transform fneg(bitconvert(x)) -> bitconvert(x^sign) to avoid loading
4014 // constant pool values.
Gabor Greifba36cb52008-08-28 21:40:38 +00004015 if (N0.getOpcode() == ISD::BIT_CONVERT && N0.getNode()->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00004016 N0.getOperand(0).getValueType().isInteger() &&
4017 !N0.getOperand(0).getValueType().isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004018 SDValue Int = N0.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004019 MVT IntVT = Int.getValueType();
4020 if (IntVT.isInteger() && !IntVT.isVector()) {
Chris Lattner3bd39d42008-01-27 17:42:27 +00004021 Int = DAG.getNode(ISD::XOR, IntVT, Int,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004022 DAG.getConstant(IntVT.getIntegerVTSignBit(), IntVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00004023 AddToWorkList(Int.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00004024 return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Int);
4025 }
4026 }
4027
Dan Gohman475871a2008-07-27 21:46:04 +00004028 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004029}
4030
Dan Gohman475871a2008-07-27 21:46:04 +00004031SDValue DAGCombiner::visitFABS(SDNode *N) {
4032 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00004033 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004034 MVT VT = N->getValueType(0);
Nate Begemana148d982006-01-18 22:35:16 +00004035
Nate Begeman1d4d4142005-09-01 00:19:25 +00004036 // fold (fabs c1) -> fabs(c1)
Dale Johannesendb44bf82007-10-16 23:38:29 +00004037 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00004038 return DAG.getNode(ISD::FABS, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004039 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00004040 if (N0.getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00004041 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004042 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00004043 // fold (fabs (fcopysign x, y)) -> (fabs x)
4044 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
4045 return DAG.getNode(ISD::FABS, VT, N0.getOperand(0));
4046
Chris Lattner3bd39d42008-01-27 17:42:27 +00004047 // Transform fabs(bitconvert(x)) -> bitconvert(x&~sign) to avoid loading
4048 // constant pool values.
Gabor Greifba36cb52008-08-28 21:40:38 +00004049 if (N0.getOpcode() == ISD::BIT_CONVERT && N0.getNode()->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00004050 N0.getOperand(0).getValueType().isInteger() &&
4051 !N0.getOperand(0).getValueType().isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004052 SDValue Int = N0.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004053 MVT IntVT = Int.getValueType();
4054 if (IntVT.isInteger() && !IntVT.isVector()) {
Chris Lattner3bd39d42008-01-27 17:42:27 +00004055 Int = DAG.getNode(ISD::AND, IntVT, Int,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004056 DAG.getConstant(~IntVT.getIntegerVTSignBit(), IntVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00004057 AddToWorkList(Int.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00004058 return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Int);
4059 }
4060 }
4061
Dan Gohman475871a2008-07-27 21:46:04 +00004062 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004063}
4064
Dan Gohman475871a2008-07-27 21:46:04 +00004065SDValue DAGCombiner::visitBRCOND(SDNode *N) {
4066 SDValue Chain = N->getOperand(0);
4067 SDValue N1 = N->getOperand(1);
4068 SDValue N2 = N->getOperand(2);
Nate Begeman44728a72005-09-19 22:34:01 +00004069 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
4070
4071 // never taken branch, fold to chain
4072 if (N1C && N1C->isNullValue())
4073 return Chain;
4074 // unconditional branch
Dan Gohman002e5d02008-03-13 22:13:53 +00004075 if (N1C && N1C->getAPIntValue() == 1)
Nate Begeman44728a72005-09-19 22:34:01 +00004076 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
Nate Begeman750ac1b2006-02-01 07:19:44 +00004077 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
4078 // on the target.
4079 if (N1.getOpcode() == ISD::SETCC &&
4080 TLI.isOperationLegal(ISD::BR_CC, MVT::Other)) {
4081 return DAG.getNode(ISD::BR_CC, MVT::Other, Chain, N1.getOperand(2),
4082 N1.getOperand(0), N1.getOperand(1), N2);
4083 }
Dan Gohman475871a2008-07-27 21:46:04 +00004084 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00004085}
4086
Chris Lattner3ea0b472005-10-05 06:47:48 +00004087// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
4088//
Dan Gohman475871a2008-07-27 21:46:04 +00004089SDValue DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00004090 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004091 SDValue CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
Chris Lattner3ea0b472005-10-05 06:47:48 +00004092
Duncan Sands8eab8a22008-06-09 11:32:28 +00004093 // Use SimplifySetCC to simplify SETCC's.
Dan Gohman475871a2008-07-27 21:46:04 +00004094 SDValue Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false);
Gabor Greifba36cb52008-08-28 21:40:38 +00004095 if (Simp.getNode()) AddToWorkList(Simp.getNode());
Chris Lattner30f73e72006-10-14 03:52:46 +00004096
Gabor Greifba36cb52008-08-28 21:40:38 +00004097 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.getNode());
Nate Begemane17daeb2005-10-05 21:43:42 +00004098
4099 // fold br_cc true, dest -> br dest (unconditional branch)
Dan Gohman002e5d02008-03-13 22:13:53 +00004100 if (SCCC && !SCCC->isNullValue())
Nate Begemane17daeb2005-10-05 21:43:42 +00004101 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
4102 N->getOperand(4));
4103 // fold br_cc false, dest -> unconditional fall through
4104 if (SCCC && SCCC->isNullValue())
4105 return N->getOperand(0);
Chris Lattner30f73e72006-10-14 03:52:46 +00004106
Nate Begemane17daeb2005-10-05 21:43:42 +00004107 // fold to a simpler setcc
Gabor Greifba36cb52008-08-28 21:40:38 +00004108 if (Simp.getNode() && Simp.getOpcode() == ISD::SETCC)
Nate Begemane17daeb2005-10-05 21:43:42 +00004109 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
4110 Simp.getOperand(2), Simp.getOperand(0),
4111 Simp.getOperand(1), N->getOperand(4));
Dan Gohman475871a2008-07-27 21:46:04 +00004112 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00004113}
4114
Chris Lattner448f2192006-11-11 00:39:41 +00004115
Duncan Sandsec87aa82008-06-15 20:12:31 +00004116/// CombineToPreIndexedLoadStore - Try turning a load / store into a
4117/// pre-indexed load / store when the base pointer is an add or subtract
Chris Lattner448f2192006-11-11 00:39:41 +00004118/// and it has other uses besides the load / store. After the
4119/// transformation, the new indexed load / store has effectively folded
4120/// the add / subtract in and all of its other uses are redirected to the
4121/// new load / store.
4122bool DAGCombiner::CombineToPreIndexedLoadStore(SDNode *N) {
4123 if (!AfterLegalize)
4124 return false;
4125
4126 bool isLoad = true;
Dan Gohman475871a2008-07-27 21:46:04 +00004127 SDValue Ptr;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004128 MVT VT;
Chris Lattner448f2192006-11-11 00:39:41 +00004129 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004130 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004131 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004132 VT = LD->getMemoryVT();
Evan Cheng83060c52007-03-07 08:07:03 +00004133 if (!TLI.isIndexedLoadLegal(ISD::PRE_INC, VT) &&
Chris Lattner448f2192006-11-11 00:39:41 +00004134 !TLI.isIndexedLoadLegal(ISD::PRE_DEC, VT))
4135 return false;
4136 Ptr = LD->getBasePtr();
4137 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004138 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004139 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004140 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00004141 if (!TLI.isIndexedStoreLegal(ISD::PRE_INC, VT) &&
4142 !TLI.isIndexedStoreLegal(ISD::PRE_DEC, VT))
4143 return false;
4144 Ptr = ST->getBasePtr();
4145 isLoad = false;
4146 } else
4147 return false;
4148
Chris Lattner9f1794e2006-11-11 00:56:29 +00004149 // If the pointer is not an add/sub, or if it doesn't have multiple uses, bail
4150 // out. There is no reason to make this a preinc/predec.
4151 if ((Ptr.getOpcode() != ISD::ADD && Ptr.getOpcode() != ISD::SUB) ||
Gabor Greifba36cb52008-08-28 21:40:38 +00004152 Ptr.getNode()->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00004153 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00004154
Chris Lattner9f1794e2006-11-11 00:56:29 +00004155 // Ask the target to do addressing mode selection.
Dan Gohman475871a2008-07-27 21:46:04 +00004156 SDValue BasePtr;
4157 SDValue Offset;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004158 ISD::MemIndexedMode AM = ISD::UNINDEXED;
4159 if (!TLI.getPreIndexedAddressParts(N, BasePtr, Offset, AM, DAG))
4160 return false;
Evan Chenga7d4a042007-05-03 23:52:19 +00004161 // Don't create a indexed load / store with zero offset.
4162 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00004163 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Chenga7d4a042007-05-03 23:52:19 +00004164 return false;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004165
Chris Lattner41e53fd2006-11-11 01:00:15 +00004166 // Try turning it into a pre-indexed load / store except when:
Evan Chengc843abe2007-05-24 02:35:39 +00004167 // 1) The new base ptr is a frame index.
4168 // 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 +00004169 // predecessor of the value being stored.
Evan Chengc843abe2007-05-24 02:35:39 +00004170 // 3) Another use of old base ptr is a predecessor of N. If ptr is folded
Chris Lattner9f1794e2006-11-11 00:56:29 +00004171 // that would create a cycle.
Evan Chengc843abe2007-05-24 02:35:39 +00004172 // 4) All uses are load / store ops that use it as old base ptr.
Chris Lattner448f2192006-11-11 00:39:41 +00004173
Chris Lattner41e53fd2006-11-11 01:00:15 +00004174 // Check #1. Preinc'ing a frame index would require copying the stack pointer
4175 // (plus the implicit offset) to a register to preinc anyway.
4176 if (isa<FrameIndexSDNode>(BasePtr))
4177 return false;
4178
4179 // Check #2.
Chris Lattner9f1794e2006-11-11 00:56:29 +00004180 if (!isLoad) {
Dan Gohman475871a2008-07-27 21:46:04 +00004181 SDValue Val = cast<StoreSDNode>(N)->getValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00004182 if (Val == BasePtr || BasePtr.getNode()->isPredecessorOf(Val.getNode()))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004183 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00004184 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004185
Evan Chengc843abe2007-05-24 02:35:39 +00004186 // Now check for #3 and #4.
Chris Lattner9f1794e2006-11-11 00:56:29 +00004187 bool RealUse = false;
Gabor Greifba36cb52008-08-28 21:40:38 +00004188 for (SDNode::use_iterator I = Ptr.getNode()->use_begin(),
4189 E = Ptr.getNode()->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00004190 SDNode *Use = *I;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004191 if (Use == N)
4192 continue;
Evan Cheng917be682008-03-04 00:41:45 +00004193 if (Use->isPredecessorOf(N))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004194 return false;
4195
4196 if (!((Use->getOpcode() == ISD::LOAD &&
4197 cast<LoadSDNode>(Use)->getBasePtr() == Ptr) ||
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00004198 (Use->getOpcode() == ISD::STORE &&
4199 cast<StoreSDNode>(Use)->getBasePtr() == Ptr)))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004200 RealUse = true;
4201 }
4202 if (!RealUse)
4203 return false;
4204
Dan Gohman475871a2008-07-27 21:46:04 +00004205 SDValue Result;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004206 if (isLoad)
Dan Gohman475871a2008-07-27 21:46:04 +00004207 Result = DAG.getIndexedLoad(SDValue(N,0), BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004208 else
Dan Gohman475871a2008-07-27 21:46:04 +00004209 Result = DAG.getIndexedStore(SDValue(N,0), BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004210 ++PreIndexedNodes;
4211 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +00004212 DOUT << "\nReplacing.4 "; DEBUG(N->dump(&DAG));
Gabor Greifba36cb52008-08-28 21:40:38 +00004213 DOUT << "\nWith: "; DEBUG(Result.getNode()->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +00004214 DOUT << '\n';
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004215 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004216 if (isLoad) {
Dan Gohman475871a2008-07-27 21:46:04 +00004217 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004218 &DeadNodes);
Dan Gohman475871a2008-07-27 21:46:04 +00004219 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004220 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004221 } else {
Dan Gohman475871a2008-07-27 21:46:04 +00004222 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004223 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004224 }
4225
Chris Lattner9f1794e2006-11-11 00:56:29 +00004226 // Finally, since the node is now dead, remove it from the graph.
4227 DAG.DeleteNode(N);
4228
4229 // Replace the uses of Ptr with uses of the updated base value.
4230 DAG.ReplaceAllUsesOfValueWith(Ptr, Result.getValue(isLoad ? 1 : 0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004231 &DeadNodes);
Gabor Greifba36cb52008-08-28 21:40:38 +00004232 removeFromWorkList(Ptr.getNode());
4233 DAG.DeleteNode(Ptr.getNode());
Chris Lattner9f1794e2006-11-11 00:56:29 +00004234
4235 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00004236}
4237
Duncan Sandsec87aa82008-06-15 20:12:31 +00004238/// CombineToPostIndexedLoadStore - Try to combine a load / store with a
Chris Lattner448f2192006-11-11 00:39:41 +00004239/// add / sub of the base pointer node into a post-indexed load / store.
4240/// The transformation folded the add / subtract into the new indexed
4241/// load / store effectively and all of its uses are redirected to the
4242/// new load / store.
4243bool DAGCombiner::CombineToPostIndexedLoadStore(SDNode *N) {
4244 if (!AfterLegalize)
4245 return false;
4246
4247 bool isLoad = true;
Dan Gohman475871a2008-07-27 21:46:04 +00004248 SDValue Ptr;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004249 MVT VT;
Chris Lattner448f2192006-11-11 00:39:41 +00004250 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004251 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004252 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004253 VT = LD->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00004254 if (!TLI.isIndexedLoadLegal(ISD::POST_INC, VT) &&
4255 !TLI.isIndexedLoadLegal(ISD::POST_DEC, VT))
4256 return false;
4257 Ptr = LD->getBasePtr();
4258 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004259 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004260 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004261 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00004262 if (!TLI.isIndexedStoreLegal(ISD::POST_INC, VT) &&
4263 !TLI.isIndexedStoreLegal(ISD::POST_DEC, VT))
4264 return false;
4265 Ptr = ST->getBasePtr();
4266 isLoad = false;
4267 } else
4268 return false;
4269
Gabor Greifba36cb52008-08-28 21:40:38 +00004270 if (Ptr.getNode()->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00004271 return false;
4272
Gabor Greifba36cb52008-08-28 21:40:38 +00004273 for (SDNode::use_iterator I = Ptr.getNode()->use_begin(),
4274 E = Ptr.getNode()->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00004275 SDNode *Op = *I;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004276 if (Op == N ||
4277 (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB))
4278 continue;
4279
Dan Gohman475871a2008-07-27 21:46:04 +00004280 SDValue BasePtr;
4281 SDValue Offset;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004282 ISD::MemIndexedMode AM = ISD::UNINDEXED;
4283 if (TLI.getPostIndexedAddressParts(N, Op, BasePtr, Offset, AM, DAG)) {
4284 if (Ptr == Offset)
4285 std::swap(BasePtr, Offset);
4286 if (Ptr != BasePtr)
Chris Lattner448f2192006-11-11 00:39:41 +00004287 continue;
Evan Chenga7d4a042007-05-03 23:52:19 +00004288 // Don't create a indexed load / store with zero offset.
4289 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00004290 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Chenga7d4a042007-05-03 23:52:19 +00004291 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00004292
Chris Lattner9f1794e2006-11-11 00:56:29 +00004293 // Try turning it into a post-indexed load / store except when
4294 // 1) All uses are load / store ops that use it as base ptr.
4295 // 2) Op must be independent of N, i.e. Op is neither a predecessor
4296 // nor a successor of N. Otherwise, if Op is folded that would
4297 // create a cycle.
4298
4299 // Check for #1.
4300 bool TryNext = false;
Gabor Greifba36cb52008-08-28 21:40:38 +00004301 for (SDNode::use_iterator II = BasePtr.getNode()->use_begin(),
4302 EE = BasePtr.getNode()->use_end(); II != EE; ++II) {
Dan Gohman89684502008-07-27 20:43:25 +00004303 SDNode *Use = *II;
Gabor Greifba36cb52008-08-28 21:40:38 +00004304 if (Use == Ptr.getNode())
Chris Lattner448f2192006-11-11 00:39:41 +00004305 continue;
4306
Chris Lattner9f1794e2006-11-11 00:56:29 +00004307 // If all the uses are load / store addresses, then don't do the
4308 // transformation.
4309 if (Use->getOpcode() == ISD::ADD || Use->getOpcode() == ISD::SUB){
4310 bool RealUse = false;
4311 for (SDNode::use_iterator III = Use->use_begin(),
4312 EEE = Use->use_end(); III != EEE; ++III) {
Dan Gohman89684502008-07-27 20:43:25 +00004313 SDNode *UseUse = *III;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004314 if (!((UseUse->getOpcode() == ISD::LOAD &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004315 cast<LoadSDNode>(UseUse)->getBasePtr().getNode() == Use) ||
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00004316 (UseUse->getOpcode() == ISD::STORE &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004317 cast<StoreSDNode>(UseUse)->getBasePtr().getNode() == Use)))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004318 RealUse = true;
4319 }
Chris Lattner448f2192006-11-11 00:39:41 +00004320
Chris Lattner9f1794e2006-11-11 00:56:29 +00004321 if (!RealUse) {
4322 TryNext = true;
4323 break;
Chris Lattner448f2192006-11-11 00:39:41 +00004324 }
4325 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004326 }
4327 if (TryNext)
4328 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00004329
Chris Lattner9f1794e2006-11-11 00:56:29 +00004330 // Check for #2
Evan Cheng917be682008-03-04 00:41:45 +00004331 if (!Op->isPredecessorOf(N) && !N->isPredecessorOf(Op)) {
Dan Gohman475871a2008-07-27 21:46:04 +00004332 SDValue Result = isLoad
4333 ? DAG.getIndexedLoad(SDValue(N,0), BasePtr, Offset, AM)
4334 : DAG.getIndexedStore(SDValue(N,0), BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004335 ++PostIndexedNodes;
4336 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +00004337 DOUT << "\nReplacing.5 "; DEBUG(N->dump(&DAG));
Gabor Greifba36cb52008-08-28 21:40:38 +00004338 DOUT << "\nWith: "; DEBUG(Result.getNode()->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +00004339 DOUT << '\n';
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004340 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004341 if (isLoad) {
Dan Gohman475871a2008-07-27 21:46:04 +00004342 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004343 &DeadNodes);
Dan Gohman475871a2008-07-27 21:46:04 +00004344 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004345 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004346 } else {
Dan Gohman475871a2008-07-27 21:46:04 +00004347 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004348 &DeadNodes);
Chris Lattner448f2192006-11-11 00:39:41 +00004349 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004350
Chris Lattner9f1794e2006-11-11 00:56:29 +00004351 // Finally, since the node is now dead, remove it from the graph.
4352 DAG.DeleteNode(N);
4353
4354 // Replace the uses of Use with uses of the updated base value.
Dan Gohman475871a2008-07-27 21:46:04 +00004355 DAG.ReplaceAllUsesOfValueWith(SDValue(Op, 0),
Chris Lattner9f1794e2006-11-11 00:56:29 +00004356 Result.getValue(isLoad ? 1 : 0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004357 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004358 removeFromWorkList(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004359 DAG.DeleteNode(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004360 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00004361 }
4362 }
4363 }
4364 return false;
4365}
4366
Chris Lattner00161a62008-01-25 07:20:16 +00004367/// InferAlignment - If we can infer some alignment information from this
4368/// pointer, return it.
Dan Gohman475871a2008-07-27 21:46:04 +00004369static unsigned InferAlignment(SDValue Ptr, SelectionDAG &DAG) {
Chris Lattner00161a62008-01-25 07:20:16 +00004370 // If this is a direct reference to a stack slot, use information about the
4371 // stack slot's alignment.
Chris Lattner1329cb82008-01-26 19:45:50 +00004372 int FrameIdx = 1 << 31;
4373 int64_t FrameOffset = 0;
Chris Lattner00161a62008-01-25 07:20:16 +00004374 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
Chris Lattner1329cb82008-01-26 19:45:50 +00004375 FrameIdx = FI->getIndex();
4376 } else if (Ptr.getOpcode() == ISD::ADD &&
4377 isa<ConstantSDNode>(Ptr.getOperand(1)) &&
4378 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4379 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4380 FrameOffset = Ptr.getConstantOperandVal(1);
Chris Lattner00161a62008-01-25 07:20:16 +00004381 }
Chris Lattner1329cb82008-01-26 19:45:50 +00004382
4383 if (FrameIdx != (1 << 31)) {
4384 // FIXME: Handle FI+CST.
4385 const MachineFrameInfo &MFI = *DAG.getMachineFunction().getFrameInfo();
4386 if (MFI.isFixedObjectIndex(FrameIdx)) {
Dan Gohman8cea8ff2008-08-11 18:27:03 +00004387 int64_t ObjectOffset = MFI.getObjectOffset(FrameIdx) + FrameOffset;
Chris Lattner1329cb82008-01-26 19:45:50 +00004388
4389 // The alignment of the frame index can be determined from its offset from
4390 // the incoming frame position. If the frame object is at offset 32 and
4391 // the stack is guaranteed to be 16-byte aligned, then we know that the
4392 // object is 16-byte aligned.
4393 unsigned StackAlign = DAG.getTarget().getFrameInfo()->getStackAlignment();
4394 unsigned Align = MinAlign(ObjectOffset, StackAlign);
4395
4396 // Finally, the frame object itself may have a known alignment. Factor
4397 // the alignment + offset into a new alignment. For example, if we know
4398 // the FI is 8 byte aligned, but the pointer is 4 off, we really have a
4399 // 4-byte alignment of the resultant pointer. Likewise align 4 + 4-byte
4400 // offset = 4-byte alignment, align 4 + 1-byte offset = align 1, etc.
4401 unsigned FIInfoAlign = MinAlign(MFI.getObjectAlignment(FrameIdx),
4402 FrameOffset);
4403 return std::max(Align, FIInfoAlign);
4404 }
4405 }
Chris Lattner00161a62008-01-25 07:20:16 +00004406
4407 return 0;
4408}
Chris Lattner448f2192006-11-11 00:39:41 +00004409
Dan Gohman475871a2008-07-27 21:46:04 +00004410SDValue DAGCombiner::visitLOAD(SDNode *N) {
Evan Cheng466685d2006-10-09 20:57:25 +00004411 LoadSDNode *LD = cast<LoadSDNode>(N);
Dan Gohman475871a2008-07-27 21:46:04 +00004412 SDValue Chain = LD->getChain();
4413 SDValue Ptr = LD->getBasePtr();
Chris Lattner00161a62008-01-25 07:20:16 +00004414
4415 // Try to infer better alignment information than the load already has.
Dan Gohmana2676512008-08-20 16:30:28 +00004416 if (!Fast && LD->isUnindexed()) {
Chris Lattner00161a62008-01-25 07:20:16 +00004417 if (unsigned Align = InferAlignment(Ptr, DAG)) {
4418 if (Align > LD->getAlignment())
4419 return DAG.getExtLoad(LD->getExtensionType(), LD->getValueType(0),
4420 Chain, Ptr, LD->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004421 LD->getSrcValueOffset(), LD->getMemoryVT(),
Chris Lattner00161a62008-01-25 07:20:16 +00004422 LD->isVolatile(), Align);
4423 }
4424 }
4425
Evan Cheng45a7ca92007-05-01 00:38:21 +00004426
4427 // If load is not volatile and there are no uses of the loaded value (and
4428 // the updated indexed value in case of indexed loads), change uses of the
4429 // chain value into uses of the chain input (i.e. delete the dead load).
4430 if (!LD->isVolatile()) {
Evan Cheng498f5592007-05-01 08:53:39 +00004431 if (N->getValueType(1) == MVT::Other) {
4432 // Unindexed loads.
Evan Cheng02c42852008-01-16 23:11:54 +00004433 if (N->hasNUsesOfValue(0, 0)) {
4434 // It's not safe to use the two value CombineTo variant here. e.g.
4435 // v1, chain2 = load chain1, loc
4436 // v2, chain3 = load chain2, loc
4437 // v3 = add v2, c
Chris Lattner125991a2008-01-24 07:57:06 +00004438 // Now we replace use of chain2 with chain1. This makes the second load
4439 // isomorphic to the one we are deleting, and thus makes this load live.
Evan Cheng02c42852008-01-16 23:11:54 +00004440 DOUT << "\nReplacing.6 "; DEBUG(N->dump(&DAG));
Gabor Greifba36cb52008-08-28 21:40:38 +00004441 DOUT << "\nWith chain: "; DEBUG(Chain.getNode()->dump(&DAG));
Chris Lattner125991a2008-01-24 07:57:06 +00004442 DOUT << "\n";
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004443 WorkListRemover DeadNodes(*this);
Dan Gohman475871a2008-07-27 21:46:04 +00004444 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Chain, &DeadNodes);
Chris Lattner125991a2008-01-24 07:57:06 +00004445 if (N->use_empty()) {
4446 removeFromWorkList(N);
4447 DAG.DeleteNode(N);
4448 }
Dan Gohman475871a2008-07-27 21:46:04 +00004449 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng02c42852008-01-16 23:11:54 +00004450 }
Evan Cheng498f5592007-05-01 08:53:39 +00004451 } else {
4452 // Indexed loads.
4453 assert(N->getValueType(2) == MVT::Other && "Malformed indexed loads?");
4454 if (N->hasNUsesOfValue(0, 0) && N->hasNUsesOfValue(0, 1)) {
Dan Gohman475871a2008-07-27 21:46:04 +00004455 SDValue Undef = DAG.getNode(ISD::UNDEF, N->getValueType(0));
Evan Cheng02c42852008-01-16 23:11:54 +00004456 DOUT << "\nReplacing.6 "; DEBUG(N->dump(&DAG));
Gabor Greifba36cb52008-08-28 21:40:38 +00004457 DOUT << "\nWith: "; DEBUG(Undef.getNode()->dump(&DAG));
Evan Cheng02c42852008-01-16 23:11:54 +00004458 DOUT << " and 2 other values\n";
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004459 WorkListRemover DeadNodes(*this);
Dan Gohman475871a2008-07-27 21:46:04 +00004460 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Undef, &DeadNodes);
4461 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1),
Chris Lattner4626b252008-01-17 07:20:38 +00004462 DAG.getNode(ISD::UNDEF, N->getValueType(1)),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004463 &DeadNodes);
Dan Gohman475871a2008-07-27 21:46:04 +00004464 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 2), Chain, &DeadNodes);
Evan Cheng02c42852008-01-16 23:11:54 +00004465 removeFromWorkList(N);
Evan Cheng02c42852008-01-16 23:11:54 +00004466 DAG.DeleteNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +00004467 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng45a7ca92007-05-01 00:38:21 +00004468 }
Evan Cheng45a7ca92007-05-01 00:38:21 +00004469 }
4470 }
Chris Lattner01a22022005-10-10 22:04:48 +00004471
4472 // If this load is directly stored, replace the load value with the stored
4473 // value.
4474 // TODO: Handle store large -> read small portion.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004475 // TODO: Handle TRUNCSTORE/LOADEXT
Dan Gohmanb061c4b2008-03-31 20:32:52 +00004476 if (LD->getExtensionType() == ISD::NON_EXTLOAD &&
4477 !LD->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004478 if (ISD::isNON_TRUNCStore(Chain.getNode())) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00004479 StoreSDNode *PrevST = cast<StoreSDNode>(Chain);
4480 if (PrevST->getBasePtr() == Ptr &&
4481 PrevST->getValue().getValueType() == N->getValueType(0))
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004482 return CombineTo(N, Chain.getOperand(1), Chain);
Evan Cheng8b2794a2006-10-13 21:14:26 +00004483 }
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004484 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00004485
Jim Laskey7ca56af2006-10-11 13:47:09 +00004486 if (CombinerAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00004487 // Walk up chain skipping non-aliasing memory nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00004488 SDValue BetterChain = FindBetterChain(N, Chain);
Jim Laskey279f0532006-09-25 16:29:54 +00004489
Jim Laskey6ff23e52006-10-04 16:53:27 +00004490 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00004491 if (Chain != BetterChain) {
Dan Gohman475871a2008-07-27 21:46:04 +00004492 SDValue ReplLoad;
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004493
Jim Laskey279f0532006-09-25 16:29:54 +00004494 // Replace the chain to void dependency.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004495 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
4496 ReplLoad = DAG.getLoad(N->getValueType(0), BetterChain, Ptr,
Duncan Sandsdc846502007-10-28 12:59:45 +00004497 LD->getSrcValue(), LD->getSrcValueOffset(),
4498 LD->isVolatile(), LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004499 } else {
4500 ReplLoad = DAG.getExtLoad(LD->getExtensionType(),
4501 LD->getValueType(0),
4502 BetterChain, Ptr, LD->getSrcValue(),
4503 LD->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004504 LD->getMemoryVT(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00004505 LD->isVolatile(),
4506 LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004507 }
Jim Laskey279f0532006-09-25 16:29:54 +00004508
Jim Laskey6ff23e52006-10-04 16:53:27 +00004509 // Create token factor to keep old chain connected.
Dan Gohman475871a2008-07-27 21:46:04 +00004510 SDValue Token = DAG.getNode(ISD::TokenFactor, MVT::Other,
Jim Laskey288af5e2006-09-25 19:32:58 +00004511 Chain, ReplLoad.getValue(1));
Jim Laskey6ff23e52006-10-04 16:53:27 +00004512
Jim Laskey274062c2006-10-13 23:32:28 +00004513 // Replace uses with load result and token factor. Don't add users
4514 // to work list.
4515 return CombineTo(N, ReplLoad.getValue(0), Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00004516 }
4517 }
4518
Evan Cheng7fc033a2006-11-03 03:06:21 +00004519 // Try transforming N to an indexed load.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00004520 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Dan Gohman475871a2008-07-27 21:46:04 +00004521 return SDValue(N, 0);
Evan Cheng7fc033a2006-11-03 03:06:21 +00004522
Dan Gohman475871a2008-07-27 21:46:04 +00004523 return SDValue();
Chris Lattner01a22022005-10-10 22:04:48 +00004524}
4525
Chris Lattner07649d92008-01-08 23:08:06 +00004526
Dan Gohman475871a2008-07-27 21:46:04 +00004527SDValue DAGCombiner::visitSTORE(SDNode *N) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00004528 StoreSDNode *ST = cast<StoreSDNode>(N);
Dan Gohman475871a2008-07-27 21:46:04 +00004529 SDValue Chain = ST->getChain();
4530 SDValue Value = ST->getValue();
4531 SDValue Ptr = ST->getBasePtr();
Jim Laskey7aed46c2006-10-11 18:55:16 +00004532
Chris Lattner00161a62008-01-25 07:20:16 +00004533 // Try to infer better alignment information than the store already has.
Dan Gohmana2676512008-08-20 16:30:28 +00004534 if (!Fast && ST->isUnindexed()) {
Chris Lattner00161a62008-01-25 07:20:16 +00004535 if (unsigned Align = InferAlignment(Ptr, DAG)) {
4536 if (Align > ST->getAlignment())
4537 return DAG.getTruncStore(Chain, Value, Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004538 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattner00161a62008-01-25 07:20:16 +00004539 ST->isVolatile(), Align);
4540 }
4541 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004542
Evan Cheng59d5b682007-05-07 21:27:48 +00004543 // If this is a store of a bit convert, store the input value if the
Evan Cheng2c4f9432007-05-09 21:49:47 +00004544 // resultant store does not need a higher alignment than the original.
Dale Johannesen98a6c622007-05-16 22:45:30 +00004545 if (Value.getOpcode() == ISD::BIT_CONVERT && !ST->isTruncatingStore() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00004546 ST->isUnindexed()) {
Evan Cheng59d5b682007-05-07 21:27:48 +00004547 unsigned Align = ST->getAlignment();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004548 MVT SVT = Value.getOperand(0).getValueType();
Evan Cheng59d5b682007-05-07 21:27:48 +00004549 unsigned OrigAlign = TLI.getTargetMachine().getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00004550 getABITypeAlignment(SVT.getTypeForMVT());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004551 if (Align <= OrigAlign &&
4552 ((!AfterLegalize && !ST->isVolatile()) ||
4553 TLI.isOperationLegal(ISD::STORE, SVT)))
Evan Cheng59d5b682007-05-07 21:27:48 +00004554 return DAG.getStore(Chain, Value.getOperand(0), Ptr, ST->getSrcValue(),
Dan Gohman77455612008-06-28 00:45:22 +00004555 ST->getSrcValueOffset(), ST->isVolatile(), OrigAlign);
Jim Laskey279f0532006-09-25 16:29:54 +00004556 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004557
Nate Begeman2cbba892006-12-11 02:23:46 +00004558 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Nate Begeman2cbba892006-12-11 02:23:46 +00004559 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Value)) {
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004560 // NOTE: If the original store is volatile, this transform must not increase
4561 // the number of stores. For example, on x86-32 an f64 can be stored in one
4562 // processor operation but an i64 (which is not legal) requires two. So the
4563 // transform should not be done in this case.
Evan Cheng25ece662006-12-11 17:25:19 +00004564 if (Value.getOpcode() != ISD::TargetConstantFP) {
Dan Gohman475871a2008-07-27 21:46:04 +00004565 SDValue Tmp;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004566 switch (CFP->getValueType(0).getSimpleVT()) {
Chris Lattner62be1a72006-12-12 04:16:14 +00004567 default: assert(0 && "Unknown FP type");
Dale Johannesenc7b21d52007-09-18 18:36:59 +00004568 case MVT::f80: // We don't do this for these yet.
4569 case MVT::f128:
4570 case MVT::ppcf128:
4571 break;
Chris Lattner62be1a72006-12-12 04:16:14 +00004572 case MVT::f32:
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004573 if ((!AfterLegalize && !ST->isVolatile()) ||
4574 TLI.isOperationLegal(ISD::STORE, MVT::i32)) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00004575 Tmp = DAG.getConstant((uint32_t)CFP->getValueAPF().
4576 convertToAPInt().getZExtValue(), MVT::i32);
Chris Lattner62be1a72006-12-12 04:16:14 +00004577 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004578 ST->getSrcValueOffset(), ST->isVolatile(),
4579 ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00004580 }
4581 break;
4582 case MVT::f64:
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004583 if ((!AfterLegalize && !ST->isVolatile()) ||
4584 TLI.isOperationLegal(ISD::STORE, MVT::i64)) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00004585 Tmp = DAG.getConstant(CFP->getValueAPF().convertToAPInt().
4586 getZExtValue(), MVT::i64);
Chris Lattner62be1a72006-12-12 04:16:14 +00004587 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004588 ST->getSrcValueOffset(), ST->isVolatile(),
4589 ST->getAlignment());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004590 } else if (!ST->isVolatile() &&
4591 TLI.isOperationLegal(ISD::STORE, MVT::i32)) {
Duncan Sandsdc846502007-10-28 12:59:45 +00004592 // Many FP stores are not made apparent until after legalize, e.g. for
Chris Lattner62be1a72006-12-12 04:16:14 +00004593 // argument passing. Since this is so common, custom legalize the
4594 // 64-bit integer store into two 32-bit stores.
Dale Johannesen9d5f4562007-09-12 03:30:33 +00004595 uint64_t Val = CFP->getValueAPF().convertToAPInt().getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00004596 SDValue Lo = DAG.getConstant(Val & 0xFFFFFFFF, MVT::i32);
4597 SDValue Hi = DAG.getConstant(Val >> 32, MVT::i32);
Duncan Sands0753fc12008-02-11 10:37:04 +00004598 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner62be1a72006-12-12 04:16:14 +00004599
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004600 int SVOffset = ST->getSrcValueOffset();
4601 unsigned Alignment = ST->getAlignment();
4602 bool isVolatile = ST->isVolatile();
4603
Dan Gohman475871a2008-07-27 21:46:04 +00004604 SDValue St0 = DAG.getStore(Chain, Lo, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004605 ST->getSrcValueOffset(),
4606 isVolatile, ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00004607 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
4608 DAG.getConstant(4, Ptr.getValueType()));
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004609 SVOffset += 4;
Duncan Sandsdc846502007-10-28 12:59:45 +00004610 Alignment = MinAlign(Alignment, 4U);
Dan Gohman475871a2008-07-27 21:46:04 +00004611 SDValue St1 = DAG.getStore(Chain, Hi, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004612 SVOffset, isVolatile, Alignment);
Chris Lattner62be1a72006-12-12 04:16:14 +00004613 return DAG.getNode(ISD::TokenFactor, MVT::Other, St0, St1);
4614 }
4615 break;
Evan Cheng25ece662006-12-11 17:25:19 +00004616 }
Nate Begeman2cbba892006-12-11 02:23:46 +00004617 }
Nate Begeman2cbba892006-12-11 02:23:46 +00004618 }
4619
Jim Laskey279f0532006-09-25 16:29:54 +00004620 if (CombinerAA) {
4621 // Walk up chain skipping non-aliasing memory nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00004622 SDValue BetterChain = FindBetterChain(N, Chain);
Jim Laskey279f0532006-09-25 16:29:54 +00004623
Jim Laskey6ff23e52006-10-04 16:53:27 +00004624 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00004625 if (Chain != BetterChain) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00004626 // Replace the chain to avoid dependency.
Dan Gohman475871a2008-07-27 21:46:04 +00004627 SDValue ReplStore;
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004628 if (ST->isTruncatingStore()) {
4629 ReplStore = DAG.getTruncStore(BetterChain, Value, Ptr,
Chris Lattner4626b252008-01-17 07:20:38 +00004630 ST->getSrcValue(),ST->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004631 ST->getMemoryVT(),
Chris Lattner4626b252008-01-17 07:20:38 +00004632 ST->isVolatile(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004633 } else {
4634 ReplStore = DAG.getStore(BetterChain, Value, Ptr,
Chris Lattner4626b252008-01-17 07:20:38 +00004635 ST->getSrcValue(), ST->getSrcValueOffset(),
4636 ST->isVolatile(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004637 }
4638
Jim Laskey279f0532006-09-25 16:29:54 +00004639 // Create token to keep both nodes around.
Dan Gohman475871a2008-07-27 21:46:04 +00004640 SDValue Token =
Jim Laskey274062c2006-10-13 23:32:28 +00004641 DAG.getNode(ISD::TokenFactor, MVT::Other, Chain, ReplStore);
4642
4643 // Don't add users to work list.
4644 return CombineTo(N, Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00004645 }
Jim Laskeyd1aed7a2006-09-21 16:28:59 +00004646 }
Chris Lattnerc33baaa2005-12-23 05:48:07 +00004647
Evan Cheng33dbedc2006-11-05 09:31:14 +00004648 // Try transforming N to an indexed store.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00004649 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Dan Gohman475871a2008-07-27 21:46:04 +00004650 return SDValue(N, 0);
Evan Cheng33dbedc2006-11-05 09:31:14 +00004651
Chris Lattner3c872852007-12-29 06:26:16 +00004652 // FIXME: is there such a thing as a truncating indexed store?
Chris Lattnerddf89562008-01-17 19:59:44 +00004653 if (ST->isTruncatingStore() && ST->isUnindexed() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00004654 Value.getValueType().isInteger()) {
Chris Lattner2b4c2792007-10-13 06:35:54 +00004655 // See if we can simplify the input to this truncstore with knowledge that
4656 // only the low bits are being used. For example:
4657 // "truncstore (or (shl x, 8), y), i8" -> "truncstore y, i8"
Dan Gohman475871a2008-07-27 21:46:04 +00004658 SDValue Shorter =
Dan Gohman2e68b6f2008-02-25 21:11:39 +00004659 GetDemandedBits(Value,
4660 APInt::getLowBitsSet(Value.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +00004661 ST->getMemoryVT().getSizeInBits()));
Gabor Greifba36cb52008-08-28 21:40:38 +00004662 AddToWorkList(Value.getNode());
4663 if (Shorter.getNode())
Chris Lattner2b4c2792007-10-13 06:35:54 +00004664 return DAG.getTruncStore(Chain, Shorter, Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004665 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattner2b4c2792007-10-13 06:35:54 +00004666 ST->isVolatile(), ST->getAlignment());
Chris Lattnere33544c2007-10-13 06:58:48 +00004667
4668 // Otherwise, see if we can simplify the operation with
4669 // SimplifyDemandedBits, which only works if the value has a single use.
Dan Gohman7b8d4a92008-02-27 00:25:32 +00004670 if (SimplifyDemandedBits(Value,
4671 APInt::getLowBitsSet(
4672 Value.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +00004673 ST->getMemoryVT().getSizeInBits())))
Dan Gohman475871a2008-07-27 21:46:04 +00004674 return SDValue(N, 0);
Chris Lattner2b4c2792007-10-13 06:35:54 +00004675 }
4676
Chris Lattner3c872852007-12-29 06:26:16 +00004677 // If this is a load followed by a store to the same location, then the store
4678 // is dead/noop.
4679 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Value)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004680 if (Ld->getBasePtr() == Ptr && ST->getMemoryVT() == Ld->getMemoryVT() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00004681 ST->isUnindexed() && !ST->isVolatile() &&
Chris Lattner07649d92008-01-08 23:08:06 +00004682 // There can't be any side effects between the load and store, such as
4683 // a call or store.
Dan Gohman475871a2008-07-27 21:46:04 +00004684 Chain.reachesChainWithoutSideEffects(SDValue(Ld, 1))) {
Chris Lattner3c872852007-12-29 06:26:16 +00004685 // The store is dead, remove it.
4686 return Chain;
4687 }
4688 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004689
Chris Lattnerddf89562008-01-17 19:59:44 +00004690 // If this is an FP_ROUND or TRUNC followed by a store, fold this into a
4691 // truncating store. We can do this even if this is already a truncstore.
4692 if ((Value.getOpcode() == ISD::FP_ROUND || Value.getOpcode() == ISD::TRUNCATE)
Gabor Greifba36cb52008-08-28 21:40:38 +00004693 && Value.getNode()->hasOneUse() && ST->isUnindexed() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00004694 TLI.isTruncStoreLegal(Value.getOperand(0).getValueType(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004695 ST->getMemoryVT())) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004696 return DAG.getTruncStore(Chain, Value.getOperand(0), Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004697 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattnerddf89562008-01-17 19:59:44 +00004698 ST->isVolatile(), ST->getAlignment());
4699 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004700
Dan Gohman475871a2008-07-27 21:46:04 +00004701 return SDValue();
Chris Lattner87514ca2005-10-10 22:31:19 +00004702}
4703
Dan Gohman475871a2008-07-27 21:46:04 +00004704SDValue DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
4705 SDValue InVec = N->getOperand(0);
4706 SDValue InVal = N->getOperand(1);
4707 SDValue EltNo = N->getOperand(2);
Chris Lattnerca242442006-03-19 01:27:56 +00004708
4709 // If the invec is a BUILD_VECTOR and if EltNo is a constant, build a new
4710 // vector with the inserted element.
4711 if (InVec.getOpcode() == ISD::BUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
4712 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00004713 SmallVector<SDValue, 8> Ops(InVec.getNode()->op_begin(), InVec.getNode()->op_end());
Chris Lattnerca242442006-03-19 01:27:56 +00004714 if (Elt < Ops.size())
4715 Ops[Elt] = InVal;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004716 return DAG.getNode(ISD::BUILD_VECTOR, InVec.getValueType(),
4717 &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00004718 }
4719
Dan Gohman475871a2008-07-27 21:46:04 +00004720 return SDValue();
Chris Lattnerca242442006-03-19 01:27:56 +00004721}
4722
Dan Gohman475871a2008-07-27 21:46:04 +00004723SDValue DAGCombiner::visitEXTRACT_VECTOR_ELT(SDNode *N) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004724 // (vextract (v4f32 load $addr), c) -> (f32 load $addr+c*size)
4725 // (vextract (v4f32 s2v (f32 load $addr)), c) -> (f32 load $addr+c*size)
4726 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), 0) -> (f32 load $addr)
4727
4728 // Perform only after legalization to ensure build_vector / vector_shuffle
4729 // optimizations have already been done.
Dan Gohman475871a2008-07-27 21:46:04 +00004730 if (!AfterLegalize) return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004731
Dan Gohman475871a2008-07-27 21:46:04 +00004732 SDValue InVec = N->getOperand(0);
4733 SDValue EltNo = N->getOperand(1);
Evan Cheng513da432007-10-06 08:19:55 +00004734
Evan Cheng513da432007-10-06 08:19:55 +00004735 if (isa<ConstantSDNode>(EltNo)) {
4736 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
4737 bool NewLoad = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004738 MVT VT = InVec.getValueType();
4739 MVT EVT = VT.getVectorElementType();
4740 MVT LVT = EVT;
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004741 if (InVec.getOpcode() == ISD::BIT_CONVERT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004742 MVT BCVT = InVec.getOperand(0).getValueType();
Duncan Sands8e4eb092008-06-08 20:54:56 +00004743 if (!BCVT.isVector() || EVT.bitsGT(BCVT.getVectorElementType()))
Dan Gohman475871a2008-07-27 21:46:04 +00004744 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004745 InVec = InVec.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004746 EVT = BCVT.getVectorElementType();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004747 NewLoad = true;
4748 }
Evan Cheng513da432007-10-06 08:19:55 +00004749
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004750 LoadSDNode *LN0 = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00004751 if (ISD::isNormalLoad(InVec.getNode()))
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004752 LN0 = cast<LoadSDNode>(InVec);
4753 else if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR &&
4754 InVec.getOperand(0).getValueType() == EVT &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004755 ISD::isNormalLoad(InVec.getOperand(0).getNode())) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004756 LN0 = cast<LoadSDNode>(InVec.getOperand(0));
4757 } else if (InVec.getOpcode() == ISD::VECTOR_SHUFFLE) {
4758 // (vextract (vector_shuffle (load $addr), v2, <1, u, u, u>), 1)
4759 // =>
4760 // (load $addr+1*size)
4761 unsigned Idx = cast<ConstantSDNode>(InVec.getOperand(2).
4762 getOperand(Elt))->getValue();
4763 unsigned NumElems = InVec.getOperand(2).getNumOperands();
4764 InVec = (Idx < NumElems) ? InVec.getOperand(0) : InVec.getOperand(1);
4765 if (InVec.getOpcode() == ISD::BIT_CONVERT)
4766 InVec = InVec.getOperand(0);
Gabor Greifba36cb52008-08-28 21:40:38 +00004767 if (ISD::isNormalLoad(InVec.getNode())) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004768 LN0 = cast<LoadSDNode>(InVec);
4769 Elt = (Idx < NumElems) ? Idx : Idx - NumElems;
Evan Cheng513da432007-10-06 08:19:55 +00004770 }
4771 }
Duncan Sandsec87aa82008-06-15 20:12:31 +00004772 if (!LN0 || !LN0->hasOneUse() || LN0->isVolatile())
Dan Gohman475871a2008-07-27 21:46:04 +00004773 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004774
4775 unsigned Align = LN0->getAlignment();
4776 if (NewLoad) {
4777 // Check the resultant load doesn't need a higher alignment than the
4778 // original load.
4779 unsigned NewAlign = TLI.getTargetMachine().getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00004780 getABITypeAlignment(LVT.getTypeForMVT());
Duncan Sands184a8762008-06-14 17:48:34 +00004781 if (NewAlign > Align || !TLI.isOperationLegal(ISD::LOAD, LVT))
Dan Gohman475871a2008-07-27 21:46:04 +00004782 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004783 Align = NewAlign;
4784 }
4785
Dan Gohman475871a2008-07-27 21:46:04 +00004786 SDValue NewPtr = LN0->getBasePtr();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004787 if (Elt) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004788 unsigned PtrOff = LVT.getSizeInBits() * Elt / 8;
4789 MVT PtrType = NewPtr.getValueType();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004790 if (TLI.isBigEndian())
Duncan Sands83ec4b62008-06-06 12:08:01 +00004791 PtrOff = VT.getSizeInBits() / 8 - PtrOff;
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004792 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
4793 DAG.getConstant(PtrOff, PtrType));
4794 }
4795 return DAG.getLoad(LVT, LN0->getChain(), NewPtr,
4796 LN0->getSrcValue(), LN0->getSrcValueOffset(),
4797 LN0->isVolatile(), Align);
Evan Cheng513da432007-10-06 08:19:55 +00004798 }
Dan Gohman475871a2008-07-27 21:46:04 +00004799 return SDValue();
Evan Cheng513da432007-10-06 08:19:55 +00004800}
4801
4802
Dan Gohman475871a2008-07-27 21:46:04 +00004803SDValue DAGCombiner::visitBUILD_VECTOR(SDNode *N) {
Dan Gohman7f321562007-06-25 16:23:39 +00004804 unsigned NumInScalars = N->getNumOperands();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004805 MVT VT = N->getValueType(0);
4806 unsigned NumElts = VT.getVectorNumElements();
4807 MVT EltType = VT.getVectorElementType();
Chris Lattnerca242442006-03-19 01:27:56 +00004808
Dan Gohman7f321562007-06-25 16:23:39 +00004809 // Check to see if this is a BUILD_VECTOR of a bunch of EXTRACT_VECTOR_ELT
4810 // operations. If so, and if the EXTRACT_VECTOR_ELT vector inputs come from
4811 // at most two distinct vectors, turn this into a shuffle node.
Dan Gohman475871a2008-07-27 21:46:04 +00004812 SDValue VecIn1, VecIn2;
Chris Lattnerd7648c82006-03-28 20:28:38 +00004813 for (unsigned i = 0; i != NumInScalars; ++i) {
4814 // Ignore undef inputs.
4815 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
4816
Dan Gohman7f321562007-06-25 16:23:39 +00004817 // If this input is something other than a EXTRACT_VECTOR_ELT with a
Chris Lattnerd7648c82006-03-28 20:28:38 +00004818 // constant index, bail out.
Dan Gohman7f321562007-06-25 16:23:39 +00004819 if (N->getOperand(i).getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
Chris Lattnerd7648c82006-03-28 20:28:38 +00004820 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
Dan Gohman475871a2008-07-27 21:46:04 +00004821 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00004822 break;
4823 }
4824
Dan Gohman7f321562007-06-25 16:23:39 +00004825 // If the input vector type disagrees with the result of the build_vector,
Chris Lattnerd7648c82006-03-28 20:28:38 +00004826 // we can't make a shuffle.
Dan Gohman475871a2008-07-27 21:46:04 +00004827 SDValue ExtractedFromVec = N->getOperand(i).getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00004828 if (ExtractedFromVec.getValueType() != VT) {
Dan Gohman475871a2008-07-27 21:46:04 +00004829 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00004830 break;
4831 }
4832
4833 // Otherwise, remember this. We allow up to two distinct input vectors.
4834 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
4835 continue;
4836
Gabor Greifba36cb52008-08-28 21:40:38 +00004837 if (VecIn1.getNode() == 0) {
Chris Lattnerd7648c82006-03-28 20:28:38 +00004838 VecIn1 = ExtractedFromVec;
Gabor Greifba36cb52008-08-28 21:40:38 +00004839 } else if (VecIn2.getNode() == 0) {
Chris Lattnerd7648c82006-03-28 20:28:38 +00004840 VecIn2 = ExtractedFromVec;
4841 } else {
4842 // Too many inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00004843 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00004844 break;
4845 }
4846 }
4847
4848 // If everything is good, we can make a shuffle operation.
Gabor Greifba36cb52008-08-28 21:40:38 +00004849 if (VecIn1.getNode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004850 SmallVector<SDValue, 8> BuildVecIndices;
Chris Lattnerd7648c82006-03-28 20:28:38 +00004851 for (unsigned i = 0; i != NumInScalars; ++i) {
4852 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
Evan Cheng597a3bd2007-01-20 10:10:26 +00004853 BuildVecIndices.push_back(DAG.getNode(ISD::UNDEF, TLI.getPointerTy()));
Chris Lattnerd7648c82006-03-28 20:28:38 +00004854 continue;
4855 }
4856
Dan Gohman475871a2008-07-27 21:46:04 +00004857 SDValue Extract = N->getOperand(i);
Chris Lattnerd7648c82006-03-28 20:28:38 +00004858
4859 // If extracting from the first vector, just use the index directly.
4860 if (Extract.getOperand(0) == VecIn1) {
4861 BuildVecIndices.push_back(Extract.getOperand(1));
4862 continue;
4863 }
4864
4865 // Otherwise, use InIdx + VecSize
4866 unsigned Idx = cast<ConstantSDNode>(Extract.getOperand(1))->getValue();
Chris Lattner0bd48932008-01-17 07:00:52 +00004867 BuildVecIndices.push_back(DAG.getIntPtrConstant(Idx+NumInScalars));
Chris Lattnerd7648c82006-03-28 20:28:38 +00004868 }
4869
4870 // Add count and size info.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004871 MVT BuildVecVT = MVT::getVectorVT(TLI.getPointerTy(), NumElts);
Chris Lattnerd7648c82006-03-28 20:28:38 +00004872
Dan Gohman7f321562007-06-25 16:23:39 +00004873 // Return the new VECTOR_SHUFFLE node.
Dan Gohman475871a2008-07-27 21:46:04 +00004874 SDValue Ops[5];
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004875 Ops[0] = VecIn1;
Gabor Greifba36cb52008-08-28 21:40:38 +00004876 if (VecIn2.getNode()) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004877 Ops[1] = VecIn2;
Chris Lattnercef896e2006-03-28 22:19:47 +00004878 } else {
Dan Gohman7f321562007-06-25 16:23:39 +00004879 // Use an undef build_vector as input for the second operand.
Dan Gohman475871a2008-07-27 21:46:04 +00004880 std::vector<SDValue> UnOps(NumInScalars,
Chris Lattnercef896e2006-03-28 22:19:47 +00004881 DAG.getNode(ISD::UNDEF,
Dan Gohman7f321562007-06-25 16:23:39 +00004882 EltType));
4883 Ops[1] = DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004884 &UnOps[0], UnOps.size());
Gabor Greifba36cb52008-08-28 21:40:38 +00004885 AddToWorkList(Ops[1].getNode());
Chris Lattnercef896e2006-03-28 22:19:47 +00004886 }
Dan Gohman7f321562007-06-25 16:23:39 +00004887 Ops[2] = DAG.getNode(ISD::BUILD_VECTOR, BuildVecVT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004888 &BuildVecIndices[0], BuildVecIndices.size());
Dan Gohman7f321562007-06-25 16:23:39 +00004889 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Ops, 3);
Chris Lattnerd7648c82006-03-28 20:28:38 +00004890 }
4891
Dan Gohman475871a2008-07-27 21:46:04 +00004892 return SDValue();
Chris Lattnerd7648c82006-03-28 20:28:38 +00004893}
4894
Dan Gohman475871a2008-07-27 21:46:04 +00004895SDValue DAGCombiner::visitCONCAT_VECTORS(SDNode *N) {
Dan Gohman7f321562007-06-25 16:23:39 +00004896 // TODO: Check to see if this is a CONCAT_VECTORS of a bunch of
4897 // EXTRACT_SUBVECTOR operations. If so, and if the EXTRACT_SUBVECTOR vector
4898 // inputs come from at most two distinct vectors, turn this into a shuffle
4899 // node.
4900
4901 // If we only have one input vector, we don't need to do any concatenation.
4902 if (N->getNumOperands() == 1) {
4903 return N->getOperand(0);
4904 }
4905
Dan Gohman475871a2008-07-27 21:46:04 +00004906 return SDValue();
Dan Gohman7f321562007-06-25 16:23:39 +00004907}
4908
Dan Gohman475871a2008-07-27 21:46:04 +00004909SDValue DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
4910 SDValue ShufMask = N->getOperand(2);
Chris Lattnerf1d0c622006-03-31 22:16:43 +00004911 unsigned NumElts = ShufMask.getNumOperands();
4912
4913 // If the shuffle mask is an identity operation on the LHS, return the LHS.
4914 bool isIdentity = true;
4915 for (unsigned i = 0; i != NumElts; ++i) {
4916 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
4917 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
4918 isIdentity = false;
4919 break;
4920 }
4921 }
4922 if (isIdentity) return N->getOperand(0);
4923
4924 // If the shuffle mask is an identity operation on the RHS, return the RHS.
4925 isIdentity = true;
4926 for (unsigned i = 0; i != NumElts; ++i) {
4927 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
4928 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
4929 isIdentity = false;
4930 break;
4931 }
4932 }
4933 if (isIdentity) return N->getOperand(1);
Evan Chenge7bec0d2006-07-20 22:44:41 +00004934
4935 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
4936 // needed at all.
4937 bool isUnary = true;
Evan Cheng917ec982006-07-21 08:25:53 +00004938 bool isSplat = true;
Evan Chenge7bec0d2006-07-20 22:44:41 +00004939 int VecNum = -1;
Reid Spencer9160a6a2006-07-25 20:44:41 +00004940 unsigned BaseIdx = 0;
Evan Chenge7bec0d2006-07-20 22:44:41 +00004941 for (unsigned i = 0; i != NumElts; ++i)
4942 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
4943 unsigned Idx = cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue();
4944 int V = (Idx < NumElts) ? 0 : 1;
Evan Cheng917ec982006-07-21 08:25:53 +00004945 if (VecNum == -1) {
Evan Chenge7bec0d2006-07-20 22:44:41 +00004946 VecNum = V;
Evan Cheng917ec982006-07-21 08:25:53 +00004947 BaseIdx = Idx;
4948 } else {
4949 if (BaseIdx != Idx)
4950 isSplat = false;
4951 if (VecNum != V) {
4952 isUnary = false;
4953 break;
4954 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00004955 }
4956 }
4957
Dan Gohman475871a2008-07-27 21:46:04 +00004958 SDValue N0 = N->getOperand(0);
4959 SDValue N1 = N->getOperand(1);
Evan Chenge7bec0d2006-07-20 22:44:41 +00004960 // Normalize unary shuffle so the RHS is undef.
4961 if (isUnary && VecNum == 1)
4962 std::swap(N0, N1);
4963
Evan Cheng917ec982006-07-21 08:25:53 +00004964 // If it is a splat, check if the argument vector is a build_vector with
4965 // all scalar elements the same.
4966 if (isSplat) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004967 SDNode *V = N0.getNode();
Evan Cheng917ec982006-07-21 08:25:53 +00004968
Dan Gohman7f321562007-06-25 16:23:39 +00004969 // If this is a bit convert that changes the element type of the vector but
Evan Cheng59569222006-10-16 22:49:37 +00004970 // not the number of vector elements, look through it. Be careful not to
4971 // look though conversions that change things like v4f32 to v2f64.
Dan Gohman7f321562007-06-25 16:23:39 +00004972 if (V->getOpcode() == ISD::BIT_CONVERT) {
Dan Gohman475871a2008-07-27 21:46:04 +00004973 SDValue ConvInput = V->getOperand(0);
Evan Cheng29257862008-07-22 20:42:56 +00004974 if (ConvInput.getValueType().isVector() &&
4975 ConvInput.getValueType().getVectorNumElements() == NumElts)
Gabor Greifba36cb52008-08-28 21:40:38 +00004976 V = ConvInput.getNode();
Evan Cheng59569222006-10-16 22:49:37 +00004977 }
4978
Dan Gohman7f321562007-06-25 16:23:39 +00004979 if (V->getOpcode() == ISD::BUILD_VECTOR) {
4980 unsigned NumElems = V->getNumOperands();
Evan Cheng917ec982006-07-21 08:25:53 +00004981 if (NumElems > BaseIdx) {
Dan Gohman475871a2008-07-27 21:46:04 +00004982 SDValue Base;
Evan Cheng917ec982006-07-21 08:25:53 +00004983 bool AllSame = true;
4984 for (unsigned i = 0; i != NumElems; ++i) {
4985 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
4986 Base = V->getOperand(i);
4987 break;
4988 }
4989 }
4990 // Splat of <u, u, u, u>, return <u, u, u, u>
Gabor Greifba36cb52008-08-28 21:40:38 +00004991 if (!Base.getNode())
Evan Cheng917ec982006-07-21 08:25:53 +00004992 return N0;
4993 for (unsigned i = 0; i != NumElems; ++i) {
Evan Chenge0480d22007-09-18 21:54:37 +00004994 if (V->getOperand(i) != Base) {
Evan Cheng917ec982006-07-21 08:25:53 +00004995 AllSame = false;
4996 break;
4997 }
4998 }
4999 // Splat of <x, x, x, x>, return <x, x, x, x>
5000 if (AllSame)
5001 return N0;
5002 }
5003 }
5004 }
5005
Evan Chenge7bec0d2006-07-20 22:44:41 +00005006 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
5007 // into an undef.
5008 if (isUnary || N0 == N1) {
Chris Lattner17614ea2006-04-08 05:34:25 +00005009 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
5010 // first operand.
Dan Gohman475871a2008-07-27 21:46:04 +00005011 SmallVector<SDValue, 8> MappedOps;
Chris Lattner17614ea2006-04-08 05:34:25 +00005012 for (unsigned i = 0; i != NumElts; ++i) {
5013 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
5014 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) {
5015 MappedOps.push_back(ShufMask.getOperand(i));
5016 } else {
5017 unsigned NewIdx =
5018 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
Duncan Sandsd038e042008-07-21 10:20:31 +00005019 MappedOps.push_back(DAG.getConstant(NewIdx,
5020 ShufMask.getOperand(i).getValueType()));
Chris Lattner17614ea2006-04-08 05:34:25 +00005021 }
5022 }
Dan Gohman7f321562007-06-25 16:23:39 +00005023 ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMask.getValueType(),
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005024 &MappedOps[0], MappedOps.size());
Gabor Greifba36cb52008-08-28 21:40:38 +00005025 AddToWorkList(ShufMask.getNode());
Dan Gohman7f321562007-06-25 16:23:39 +00005026 return DAG.getNode(ISD::VECTOR_SHUFFLE, N->getValueType(0),
5027 N0,
5028 DAG.getNode(ISD::UNDEF, N->getValueType(0)),
5029 ShufMask);
Chris Lattner17614ea2006-04-08 05:34:25 +00005030 }
Dan Gohman7f321562007-06-25 16:23:39 +00005031
Dan Gohman475871a2008-07-27 21:46:04 +00005032 return SDValue();
Chris Lattnerf1d0c622006-03-31 22:16:43 +00005033}
5034
Evan Cheng44f1f092006-04-20 08:56:16 +00005035/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
Dan Gohman7f321562007-06-25 16:23:39 +00005036/// an AND to a vector_shuffle with the destination vector and a zero vector.
5037/// e.g. AND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
Evan Cheng44f1f092006-04-20 08:56:16 +00005038/// vector_shuffle V, Zero, <0, 4, 2, 4>
Dan Gohman475871a2008-07-27 21:46:04 +00005039SDValue DAGCombiner::XformToShuffleWithZero(SDNode *N) {
5040 SDValue LHS = N->getOperand(0);
5041 SDValue RHS = N->getOperand(1);
Dan Gohman7f321562007-06-25 16:23:39 +00005042 if (N->getOpcode() == ISD::AND) {
5043 if (RHS.getOpcode() == ISD::BIT_CONVERT)
Evan Cheng44f1f092006-04-20 08:56:16 +00005044 RHS = RHS.getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00005045 if (RHS.getOpcode() == ISD::BUILD_VECTOR) {
Dan Gohman475871a2008-07-27 21:46:04 +00005046 std::vector<SDValue> IdxOps;
Evan Cheng44f1f092006-04-20 08:56:16 +00005047 unsigned NumOps = RHS.getNumOperands();
Dan Gohman7f321562007-06-25 16:23:39 +00005048 unsigned NumElts = NumOps;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005049 MVT EVT = RHS.getValueType().getVectorElementType();
Evan Cheng44f1f092006-04-20 08:56:16 +00005050 for (unsigned i = 0; i != NumElts; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005051 SDValue Elt = RHS.getOperand(i);
Evan Cheng44f1f092006-04-20 08:56:16 +00005052 if (!isa<ConstantSDNode>(Elt))
Dan Gohman475871a2008-07-27 21:46:04 +00005053 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00005054 else if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
Duncan Sands77926da2008-07-18 20:12:05 +00005055 IdxOps.push_back(DAG.getConstant(i, EVT));
Evan Cheng44f1f092006-04-20 08:56:16 +00005056 else if (cast<ConstantSDNode>(Elt)->isNullValue())
Duncan Sands77926da2008-07-18 20:12:05 +00005057 IdxOps.push_back(DAG.getConstant(NumElts, EVT));
Evan Cheng44f1f092006-04-20 08:56:16 +00005058 else
Dan Gohman475871a2008-07-27 21:46:04 +00005059 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00005060 }
5061
5062 // Let's see if the target supports this vector_shuffle.
5063 if (!TLI.isVectorClearMaskLegal(IdxOps, EVT, DAG))
Dan Gohman475871a2008-07-27 21:46:04 +00005064 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00005065
Dan Gohman7f321562007-06-25 16:23:39 +00005066 // Return the new VECTOR_SHUFFLE node.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005067 MVT VT = MVT::getVectorVT(EVT, NumElts);
Dan Gohman475871a2008-07-27 21:46:04 +00005068 std::vector<SDValue> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00005069 LHS = DAG.getNode(ISD::BIT_CONVERT, VT, LHS);
Evan Cheng44f1f092006-04-20 08:56:16 +00005070 Ops.push_back(LHS);
Gabor Greifba36cb52008-08-28 21:40:38 +00005071 AddToWorkList(LHS.getNode());
Dan Gohman475871a2008-07-27 21:46:04 +00005072 std::vector<SDValue> ZeroOps(NumElts, DAG.getConstant(0, EVT));
Dan Gohman7f321562007-06-25 16:23:39 +00005073 Ops.push_back(DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005074 &ZeroOps[0], ZeroOps.size()));
Duncan Sands77926da2008-07-18 20:12:05 +00005075 Ops.push_back(DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005076 &IdxOps[0], IdxOps.size()));
Dan Gohman475871a2008-07-27 21:46:04 +00005077 SDValue Result = DAG.getNode(ISD::VECTOR_SHUFFLE, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005078 &Ops[0], Ops.size());
Dan Gohman7a9a5af2008-07-16 16:13:58 +00005079 if (VT != N->getValueType(0))
5080 Result = DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Result);
Evan Cheng44f1f092006-04-20 08:56:16 +00005081 return Result;
5082 }
5083 }
Dan Gohman475871a2008-07-27 21:46:04 +00005084 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00005085}
5086
Dan Gohman7f321562007-06-25 16:23:39 +00005087/// SimplifyVBinOp - Visit a binary vector operation, like ADD.
Dan Gohman475871a2008-07-27 21:46:04 +00005088SDValue DAGCombiner::SimplifyVBinOp(SDNode *N) {
Dan Gohman7f321562007-06-25 16:23:39 +00005089 // After legalize, the target may be depending on adds and other
5090 // binary ops to provide legal ways to construct constants or other
5091 // things. Simplifying them may result in a loss of legality.
Dan Gohman475871a2008-07-27 21:46:04 +00005092 if (AfterLegalize) return SDValue();
Dan Gohman7f321562007-06-25 16:23:39 +00005093
Duncan Sands83ec4b62008-06-06 12:08:01 +00005094 MVT VT = N->getValueType(0);
5095 assert(VT.isVector() && "SimplifyVBinOp only works on vectors!");
Dan Gohman7f321562007-06-25 16:23:39 +00005096
Duncan Sands83ec4b62008-06-06 12:08:01 +00005097 MVT EltType = VT.getVectorElementType();
Dan Gohman475871a2008-07-27 21:46:04 +00005098 SDValue LHS = N->getOperand(0);
5099 SDValue RHS = N->getOperand(1);
5100 SDValue Shuffle = XformToShuffleWithZero(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00005101 if (Shuffle.getNode()) return Shuffle;
Evan Cheng44f1f092006-04-20 08:56:16 +00005102
Dan Gohman7f321562007-06-25 16:23:39 +00005103 // If the LHS and RHS are BUILD_VECTOR nodes, see if we can constant fold
Chris Lattneredab1b92006-04-02 03:25:57 +00005104 // this operation.
Dan Gohman7f321562007-06-25 16:23:39 +00005105 if (LHS.getOpcode() == ISD::BUILD_VECTOR &&
5106 RHS.getOpcode() == ISD::BUILD_VECTOR) {
Dan Gohman475871a2008-07-27 21:46:04 +00005107 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00005108 for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005109 SDValue LHSOp = LHS.getOperand(i);
5110 SDValue RHSOp = RHS.getOperand(i);
Chris Lattneredab1b92006-04-02 03:25:57 +00005111 // If these two elements can't be folded, bail out.
5112 if ((LHSOp.getOpcode() != ISD::UNDEF &&
5113 LHSOp.getOpcode() != ISD::Constant &&
5114 LHSOp.getOpcode() != ISD::ConstantFP) ||
5115 (RHSOp.getOpcode() != ISD::UNDEF &&
5116 RHSOp.getOpcode() != ISD::Constant &&
5117 RHSOp.getOpcode() != ISD::ConstantFP))
5118 break;
Evan Cheng7b336a82006-05-31 06:08:35 +00005119 // Can't fold divide by zero.
Dan Gohman7f321562007-06-25 16:23:39 +00005120 if (N->getOpcode() == ISD::SDIV || N->getOpcode() == ISD::UDIV ||
5121 N->getOpcode() == ISD::FDIV) {
Evan Cheng7b336a82006-05-31 06:08:35 +00005122 if ((RHSOp.getOpcode() == ISD::Constant &&
Gabor Greifba36cb52008-08-28 21:40:38 +00005123 cast<ConstantSDNode>(RHSOp.getNode())->isNullValue()) ||
Evan Cheng7b336a82006-05-31 06:08:35 +00005124 (RHSOp.getOpcode() == ISD::ConstantFP &&
Gabor Greifba36cb52008-08-28 21:40:38 +00005125 cast<ConstantFPSDNode>(RHSOp.getNode())->getValueAPF().isZero()))
Evan Cheng7b336a82006-05-31 06:08:35 +00005126 break;
5127 }
Dan Gohman7f321562007-06-25 16:23:39 +00005128 Ops.push_back(DAG.getNode(N->getOpcode(), EltType, LHSOp, RHSOp));
Gabor Greifba36cb52008-08-28 21:40:38 +00005129 AddToWorkList(Ops.back().getNode());
Chris Lattneredab1b92006-04-02 03:25:57 +00005130 assert((Ops.back().getOpcode() == ISD::UNDEF ||
5131 Ops.back().getOpcode() == ISD::Constant ||
5132 Ops.back().getOpcode() == ISD::ConstantFP) &&
5133 "Scalar binop didn't fold!");
5134 }
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00005135
Dan Gohman7f321562007-06-25 16:23:39 +00005136 if (Ops.size() == LHS.getNumOperands()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005137 MVT VT = LHS.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +00005138 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00005139 }
Chris Lattneredab1b92006-04-02 03:25:57 +00005140 }
5141
Dan Gohman475871a2008-07-27 21:46:04 +00005142 return SDValue();
Chris Lattneredab1b92006-04-02 03:25:57 +00005143}
5144
Dan Gohman475871a2008-07-27 21:46:04 +00005145SDValue DAGCombiner::SimplifySelect(SDValue N0, SDValue N1, SDValue N2){
Nate Begemanf845b452005-10-08 00:29:44 +00005146 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
5147
Dan Gohman475871a2008-07-27 21:46:04 +00005148 SDValue SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
Nate Begemanf845b452005-10-08 00:29:44 +00005149 cast<CondCodeSDNode>(N0.getOperand(2))->get());
5150 // If we got a simplified select_cc node back from SimplifySelectCC, then
5151 // break it down into a new SETCC node, and a new SELECT node, and then return
5152 // the SELECT node, since we were called with a SELECT node.
Gabor Greifba36cb52008-08-28 21:40:38 +00005153 if (SCC.getNode()) {
Nate Begemanf845b452005-10-08 00:29:44 +00005154 // Check to see if we got a select_cc back (to turn into setcc/select).
5155 // Otherwise, just return whatever node we got back, like fabs.
5156 if (SCC.getOpcode() == ISD::SELECT_CC) {
Dan Gohman475871a2008-07-27 21:46:04 +00005157 SDValue SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
Nate Begemanf845b452005-10-08 00:29:44 +00005158 SCC.getOperand(0), SCC.getOperand(1),
5159 SCC.getOperand(4));
Gabor Greifba36cb52008-08-28 21:40:38 +00005160 AddToWorkList(SETCC.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00005161 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
5162 SCC.getOperand(3), SETCC);
5163 }
5164 return SCC;
5165 }
Dan Gohman475871a2008-07-27 21:46:04 +00005166 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00005167}
5168
Chris Lattner40c62d52005-10-18 06:04:22 +00005169/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
5170/// are the two values being selected between, see if we can simplify the
Chris Lattner729c6d12006-05-27 00:43:02 +00005171/// select. Callers of this should assume that TheSelect is deleted if this
5172/// returns true. As such, they should return the appropriate thing (e.g. the
5173/// node) back to the top-level of the DAG combiner loop to avoid it being
5174/// looked at.
Chris Lattner40c62d52005-10-18 06:04:22 +00005175///
Dan Gohman475871a2008-07-27 21:46:04 +00005176bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDValue LHS,
5177 SDValue RHS) {
Chris Lattner40c62d52005-10-18 06:04:22 +00005178
5179 // If this is a select from two identical things, try to pull the operation
5180 // through the select.
5181 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
Chris Lattner40c62d52005-10-18 06:04:22 +00005182 // If this is a load and the token chain is identical, replace the select
5183 // of two loads with a load through a select of the address to load from.
5184 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
5185 // constants have been dropped into the constant pool.
Evan Cheng466685d2006-10-09 20:57:25 +00005186 if (LHS.getOpcode() == ISD::LOAD &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005187 // Do not let this transformation reduce the number of volatile loads.
5188 !cast<LoadSDNode>(LHS)->isVolatile() &&
5189 !cast<LoadSDNode>(RHS)->isVolatile() &&
Chris Lattner40c62d52005-10-18 06:04:22 +00005190 // Token chains must be identical.
Evan Cheng466685d2006-10-09 20:57:25 +00005191 LHS.getOperand(0) == RHS.getOperand(0)) {
5192 LoadSDNode *LLD = cast<LoadSDNode>(LHS);
5193 LoadSDNode *RLD = cast<LoadSDNode>(RHS);
5194
5195 // If this is an EXTLOAD, the VT's must match.
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005196 if (LLD->getMemoryVT() == RLD->getMemoryVT()) {
Evan Cheng466685d2006-10-09 20:57:25 +00005197 // FIXME: this conflates two src values, discarding one. This is not
5198 // the right thing to do, but nothing uses srcvalues now. When they do,
5199 // turn SrcValue into a list of locations.
Dan Gohman475871a2008-07-27 21:46:04 +00005200 SDValue Addr;
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005201 if (TheSelect->getOpcode() == ISD::SELECT) {
5202 // Check that the condition doesn't reach either load. If so, folding
5203 // this will induce a cycle into the DAG.
Gabor Greifba36cb52008-08-28 21:40:38 +00005204 if (!LLD->isPredecessorOf(TheSelect->getOperand(0).getNode()) &&
5205 !RLD->isPredecessorOf(TheSelect->getOperand(0).getNode())) {
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005206 Addr = DAG.getNode(ISD::SELECT, LLD->getBasePtr().getValueType(),
5207 TheSelect->getOperand(0), LLD->getBasePtr(),
5208 RLD->getBasePtr());
5209 }
5210 } else {
5211 // Check that the condition doesn't reach either load. If so, folding
5212 // this will induce a cycle into the DAG.
Gabor Greifba36cb52008-08-28 21:40:38 +00005213 if (!LLD->isPredecessorOf(TheSelect->getOperand(0).getNode()) &&
5214 !RLD->isPredecessorOf(TheSelect->getOperand(0).getNode()) &&
5215 !LLD->isPredecessorOf(TheSelect->getOperand(1).getNode()) &&
5216 !RLD->isPredecessorOf(TheSelect->getOperand(1).getNode())) {
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005217 Addr = DAG.getNode(ISD::SELECT_CC, LLD->getBasePtr().getValueType(),
Evan Cheng466685d2006-10-09 20:57:25 +00005218 TheSelect->getOperand(0),
5219 TheSelect->getOperand(1),
5220 LLD->getBasePtr(), RLD->getBasePtr(),
5221 TheSelect->getOperand(4));
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005222 }
Evan Cheng466685d2006-10-09 20:57:25 +00005223 }
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005224
Gabor Greifba36cb52008-08-28 21:40:38 +00005225 if (Addr.getNode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005226 SDValue Load;
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005227 if (LLD->getExtensionType() == ISD::NON_EXTLOAD)
5228 Load = DAG.getLoad(TheSelect->getValueType(0), LLD->getChain(),
5229 Addr,LLD->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00005230 LLD->getSrcValueOffset(),
5231 LLD->isVolatile(),
5232 LLD->getAlignment());
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005233 else {
5234 Load = DAG.getExtLoad(LLD->getExtensionType(),
5235 TheSelect->getValueType(0),
5236 LLD->getChain(), Addr, LLD->getSrcValue(),
5237 LLD->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005238 LLD->getMemoryVT(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00005239 LLD->isVolatile(),
5240 LLD->getAlignment());
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005241 }
5242 // Users of the select now use the result of the load.
5243 CombineTo(TheSelect, Load);
5244
5245 // Users of the old loads now use the new load's chain. We know the
5246 // old-load value is dead now.
Gabor Greifba36cb52008-08-28 21:40:38 +00005247 CombineTo(LHS.getNode(), Load.getValue(0), Load.getValue(1));
5248 CombineTo(RHS.getNode(), Load.getValue(0), Load.getValue(1));
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005249 return true;
5250 }
Evan Chengc5484282006-10-04 00:56:09 +00005251 }
Chris Lattner40c62d52005-10-18 06:04:22 +00005252 }
5253 }
5254
5255 return false;
5256}
5257
Dan Gohman475871a2008-07-27 21:46:04 +00005258SDValue DAGCombiner::SimplifySelectCC(SDValue N0, SDValue N1,
5259 SDValue N2, SDValue N3,
5260 ISD::CondCode CC, bool NotExtCompare) {
Nate Begemanf845b452005-10-08 00:29:44 +00005261
Duncan Sands83ec4b62008-06-06 12:08:01 +00005262 MVT VT = N2.getValueType();
Gabor Greifba36cb52008-08-28 21:40:38 +00005263 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
5264 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
5265 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00005266
5267 // Determine if the condition we're dealing with is constant
Dan Gohman475871a2008-07-27 21:46:04 +00005268 SDValue SCC = SimplifySetCC(TLI.getSetCCResultType(N0), N0, N1, CC, false);
Gabor Greifba36cb52008-08-28 21:40:38 +00005269 if (SCC.getNode()) AddToWorkList(SCC.getNode());
5270 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00005271
5272 // fold select_cc true, x, y -> x
Dan Gohman002e5d02008-03-13 22:13:53 +00005273 if (SCCC && !SCCC->isNullValue())
Nate Begemanf845b452005-10-08 00:29:44 +00005274 return N2;
5275 // fold select_cc false, x, y -> y
Dan Gohman002e5d02008-03-13 22:13:53 +00005276 if (SCCC && SCCC->isNullValue())
Nate Begemanf845b452005-10-08 00:29:44 +00005277 return N3;
5278
5279 // Check to see if we can simplify the select into an fabs node
5280 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
5281 // Allow either -0.0 or 0.0
Dale Johannesen87503a62007-08-25 22:10:57 +00005282 if (CFP->getValueAPF().isZero()) {
Nate Begemanf845b452005-10-08 00:29:44 +00005283 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
5284 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
5285 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
5286 N2 == N3.getOperand(0))
5287 return DAG.getNode(ISD::FABS, VT, N0);
5288
5289 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
5290 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
5291 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
5292 N2.getOperand(0) == N3)
5293 return DAG.getNode(ISD::FABS, VT, N3);
5294 }
5295 }
5296
5297 // Check to see if we can perform the "gzip trick", transforming
5298 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
Chris Lattnere3152e52006-09-20 06:41:35 +00005299 if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00005300 N0.getValueType().isInteger() &&
5301 N2.getValueType().isInteger() &&
Dan Gohman002e5d02008-03-13 22:13:53 +00005302 (N1C->isNullValue() || // (a < 0) ? b : 0
5303 (N1C->getAPIntValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0
Duncan Sands83ec4b62008-06-06 12:08:01 +00005304 MVT XType = N0.getValueType();
5305 MVT AType = N2.getValueType();
Duncan Sands8e4eb092008-06-08 20:54:56 +00005306 if (XType.bitsGE(AType)) {
Nate Begemanf845b452005-10-08 00:29:44 +00005307 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00005308 // single-bit constant.
Dan Gohman002e5d02008-03-13 22:13:53 +00005309 if (N2C && ((N2C->getAPIntValue() & (N2C->getAPIntValue()-1)) == 0)) {
5310 unsigned ShCtV = N2C->getAPIntValue().logBase2();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005311 ShCtV = XType.getSizeInBits()-ShCtV-1;
Dan Gohman475871a2008-07-27 21:46:04 +00005312 SDValue ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
5313 SDValue Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
Gabor Greifba36cb52008-08-28 21:40:38 +00005314 AddToWorkList(Shift.getNode());
Duncan Sands8e4eb092008-06-08 20:54:56 +00005315 if (XType.bitsGT(AType)) {
Nate Begemanf845b452005-10-08 00:29:44 +00005316 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00005317 AddToWorkList(Shift.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00005318 }
5319 return DAG.getNode(ISD::AND, AType, Shift, N2);
5320 }
Dan Gohman475871a2008-07-27 21:46:04 +00005321 SDValue Shift = DAG.getNode(ISD::SRA, XType, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005322 DAG.getConstant(XType.getSizeInBits()-1,
Nate Begemanf845b452005-10-08 00:29:44 +00005323 TLI.getShiftAmountTy()));
Gabor Greifba36cb52008-08-28 21:40:38 +00005324 AddToWorkList(Shift.getNode());
Duncan Sands8e4eb092008-06-08 20:54:56 +00005325 if (XType.bitsGT(AType)) {
Nate Begemanf845b452005-10-08 00:29:44 +00005326 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00005327 AddToWorkList(Shift.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00005328 }
5329 return DAG.getNode(ISD::AND, AType, Shift, N2);
5330 }
5331 }
Nate Begeman07ed4172005-10-10 21:26:48 +00005332
5333 // fold select C, 16, 0 -> shl C, 4
Dan Gohman002e5d02008-03-13 22:13:53 +00005334 if (N2C && N3C && N3C->isNullValue() && N2C->getAPIntValue().isPowerOf2() &&
Nate Begeman07ed4172005-10-10 21:26:48 +00005335 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) {
Chris Lattner1eba01e2007-04-11 06:50:51 +00005336
5337 // If the caller doesn't want us to simplify this into a zext of a compare,
5338 // don't do it.
Dan Gohman002e5d02008-03-13 22:13:53 +00005339 if (NotExtCompare && N2C->getAPIntValue() == 1)
Dan Gohman475871a2008-07-27 21:46:04 +00005340 return SDValue();
Chris Lattner1eba01e2007-04-11 06:50:51 +00005341
Nate Begeman07ed4172005-10-10 21:26:48 +00005342 // Get a SetCC of the condition
5343 // FIXME: Should probably make sure that setcc is legal if we ever have a
5344 // target where it isn't.
Dan Gohman475871a2008-07-27 21:46:04 +00005345 SDValue Temp, SCC;
Nate Begeman07ed4172005-10-10 21:26:48 +00005346 // cast from setcc result type to select result type
Nate Begemanb0d04a72006-02-18 02:40:58 +00005347 if (AfterLegalize) {
Scott Michel5b8f82e2008-03-10 15:42:14 +00005348 SCC = DAG.getSetCC(TLI.getSetCCResultType(N0), N0, N1, CC);
Duncan Sands8e4eb092008-06-08 20:54:56 +00005349 if (N2.getValueType().bitsLT(SCC.getValueType()))
Chris Lattner555d8d62006-12-07 22:36:47 +00005350 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
5351 else
5352 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00005353 } else {
5354 SCC = DAG.getSetCC(MVT::i1, N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00005355 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00005356 }
Gabor Greifba36cb52008-08-28 21:40:38 +00005357 AddToWorkList(SCC.getNode());
5358 AddToWorkList(Temp.getNode());
Chris Lattnerc56a81d2007-04-11 06:43:25 +00005359
Dan Gohman002e5d02008-03-13 22:13:53 +00005360 if (N2C->getAPIntValue() == 1)
Chris Lattnerc56a81d2007-04-11 06:43:25 +00005361 return Temp;
Nate Begeman07ed4172005-10-10 21:26:48 +00005362 // shl setcc result by log2 n2c
5363 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
Dan Gohman002e5d02008-03-13 22:13:53 +00005364 DAG.getConstant(N2C->getAPIntValue().logBase2(),
Nate Begeman07ed4172005-10-10 21:26:48 +00005365 TLI.getShiftAmountTy()));
5366 }
5367
Nate Begemanf845b452005-10-08 00:29:44 +00005368 // Check to see if this is the equivalent of setcc
5369 // FIXME: Turn all of these into setcc if setcc if setcc is legal
5370 // otherwise, go ahead with the folds.
Dan Gohman002e5d02008-03-13 22:13:53 +00005371 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getAPIntValue() == 1ULL)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005372 MVT XType = N0.getValueType();
Duncan Sands184a8762008-06-14 17:48:34 +00005373 if (!AfterLegalize ||
5374 TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultType(N0))) {
Dan Gohman475871a2008-07-27 21:46:04 +00005375 SDValue Res = DAG.getSetCC(TLI.getSetCCResultType(N0), N0, N1, CC);
Nate Begemanf845b452005-10-08 00:29:44 +00005376 if (Res.getValueType() != VT)
5377 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
5378 return Res;
5379 }
5380
5381 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
5382 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
Duncan Sands184a8762008-06-14 17:48:34 +00005383 (!AfterLegalize ||
5384 TLI.isOperationLegal(ISD::CTLZ, XType))) {
Dan Gohman475871a2008-07-27 21:46:04 +00005385 SDValue Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
Nate Begemanf845b452005-10-08 00:29:44 +00005386 return DAG.getNode(ISD::SRL, XType, Ctlz,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005387 DAG.getConstant(Log2_32(XType.getSizeInBits()),
Nate Begemanf845b452005-10-08 00:29:44 +00005388 TLI.getShiftAmountTy()));
5389 }
5390 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
5391 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
Dan Gohman475871a2008-07-27 21:46:04 +00005392 SDValue NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
Nate Begemanf845b452005-10-08 00:29:44 +00005393 N0);
Dan Gohman475871a2008-07-27 21:46:04 +00005394 SDValue NotN0 = DAG.getNode(ISD::XOR, XType, N0,
Nate Begemanf845b452005-10-08 00:29:44 +00005395 DAG.getConstant(~0ULL, XType));
5396 return DAG.getNode(ISD::SRL, XType,
5397 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
Duncan Sands83ec4b62008-06-06 12:08:01 +00005398 DAG.getConstant(XType.getSizeInBits()-1,
Nate Begemanf845b452005-10-08 00:29:44 +00005399 TLI.getShiftAmountTy()));
5400 }
5401 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
5402 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
Dan Gohman475871a2008-07-27 21:46:04 +00005403 SDValue Sign = DAG.getNode(ISD::SRL, XType, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005404 DAG.getConstant(XType.getSizeInBits()-1,
Nate Begemanf845b452005-10-08 00:29:44 +00005405 TLI.getShiftAmountTy()));
5406 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
5407 }
5408 }
5409
5410 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
5411 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
5412 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
Chris Lattner1982ef22007-04-11 05:11:38 +00005413 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1) &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00005414 N2.getOperand(0) == N1 && N0.getValueType().isInteger()) {
5415 MVT XType = N0.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00005416 SDValue Shift = DAG.getNode(ISD::SRA, XType, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005417 DAG.getConstant(XType.getSizeInBits()-1,
Chris Lattner1982ef22007-04-11 05:11:38 +00005418 TLI.getShiftAmountTy()));
Dan Gohman475871a2008-07-27 21:46:04 +00005419 SDValue Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00005420 AddToWorkList(Shift.getNode());
5421 AddToWorkList(Add.getNode());
Chris Lattner1982ef22007-04-11 05:11:38 +00005422 return DAG.getNode(ISD::XOR, XType, Add, Shift);
5423 }
5424 // Check to see if this is an integer abs. select_cc setgt X, -1, X, -X ->
5425 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
5426 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT &&
5427 N0 == N2 && N3.getOpcode() == ISD::SUB && N0 == N3.getOperand(1)) {
5428 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N3.getOperand(0))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005429 MVT XType = N0.getValueType();
5430 if (SubC->isNullValue() && XType.isInteger()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005431 SDValue Shift = DAG.getNode(ISD::SRA, XType, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005432 DAG.getConstant(XType.getSizeInBits()-1,
Chris Lattner1982ef22007-04-11 05:11:38 +00005433 TLI.getShiftAmountTy()));
Dan Gohman475871a2008-07-27 21:46:04 +00005434 SDValue Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00005435 AddToWorkList(Shift.getNode());
5436 AddToWorkList(Add.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00005437 return DAG.getNode(ISD::XOR, XType, Add, Shift);
5438 }
5439 }
5440 }
Chris Lattner1982ef22007-04-11 05:11:38 +00005441
Dan Gohman475871a2008-07-27 21:46:04 +00005442 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00005443}
5444
Evan Chengfa1eb272007-02-08 22:13:59 +00005445/// SimplifySetCC - This is a stub for TargetLowering::SimplifySetCC.
Dan Gohman475871a2008-07-27 21:46:04 +00005446SDValue DAGCombiner::SimplifySetCC(MVT VT, SDValue N0,
5447 SDValue N1, ISD::CondCode Cond,
5448 bool foldBooleans) {
Evan Chengfa1eb272007-02-08 22:13:59 +00005449 TargetLowering::DAGCombinerInfo
5450 DagCombineInfo(DAG, !AfterLegalize, false, this);
5451 return TLI.SimplifySetCC(VT, N0, N1, Cond, foldBooleans, DagCombineInfo);
Nate Begeman452d7be2005-09-16 00:54:12 +00005452}
5453
Nate Begeman69575232005-10-20 02:15:44 +00005454/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
5455/// return a DAG expression to select that will generate the same value by
5456/// multiplying by a magic number. See:
5457/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Dan Gohman475871a2008-07-27 21:46:04 +00005458SDValue DAGCombiner::BuildSDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00005459 std::vector<SDNode*> Built;
Dan Gohman475871a2008-07-27 21:46:04 +00005460 SDValue S = TLI.BuildSDIV(N, DAG, &Built);
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005461
Andrew Lenharth232c9102006-06-12 16:07:18 +00005462 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005463 ii != ee; ++ii)
5464 AddToWorkList(*ii);
5465 return S;
Nate Begeman69575232005-10-20 02:15:44 +00005466}
5467
5468/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
5469/// return a DAG expression to select that will generate the same value by
5470/// multiplying by a magic number. See:
5471/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Dan Gohman475871a2008-07-27 21:46:04 +00005472SDValue DAGCombiner::BuildUDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00005473 std::vector<SDNode*> Built;
Dan Gohman475871a2008-07-27 21:46:04 +00005474 SDValue S = TLI.BuildUDIV(N, DAG, &Built);
Nate Begeman69575232005-10-20 02:15:44 +00005475
Andrew Lenharth232c9102006-06-12 16:07:18 +00005476 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005477 ii != ee; ++ii)
5478 AddToWorkList(*ii);
5479 return S;
Nate Begeman69575232005-10-20 02:15:44 +00005480}
5481
Jim Laskey71382342006-10-07 23:37:56 +00005482/// FindBaseOffset - Return true if base is known not to alias with anything
5483/// but itself. Provides base object and offset as results.
Dan Gohman475871a2008-07-27 21:46:04 +00005484static bool FindBaseOffset(SDValue Ptr, SDValue &Base, int64_t &Offset) {
Jim Laskey71382342006-10-07 23:37:56 +00005485 // Assume it is a primitive operation.
5486 Base = Ptr; Offset = 0;
5487
5488 // If it's an adding a simple constant then integrate the offset.
5489 if (Base.getOpcode() == ISD::ADD) {
5490 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Base.getOperand(1))) {
5491 Base = Base.getOperand(0);
5492 Offset += C->getValue();
5493 }
5494 }
5495
5496 // If it's any of the following then it can't alias with anything but itself.
5497 return isa<FrameIndexSDNode>(Base) ||
5498 isa<ConstantPoolSDNode>(Base) ||
5499 isa<GlobalAddressSDNode>(Base);
5500}
5501
5502/// isAlias - Return true if there is any possibility that the two addresses
5503/// overlap.
Dan Gohman475871a2008-07-27 21:46:04 +00005504bool DAGCombiner::isAlias(SDValue Ptr1, int64_t Size1,
Jim Laskey096c22e2006-10-18 12:29:57 +00005505 const Value *SrcValue1, int SrcValueOffset1,
Dan Gohman475871a2008-07-27 21:46:04 +00005506 SDValue Ptr2, int64_t Size2,
Jim Laskey096c22e2006-10-18 12:29:57 +00005507 const Value *SrcValue2, int SrcValueOffset2)
5508{
Jim Laskey71382342006-10-07 23:37:56 +00005509 // If they are the same then they must be aliases.
5510 if (Ptr1 == Ptr2) return true;
5511
5512 // Gather base node and offset information.
Dan Gohman475871a2008-07-27 21:46:04 +00005513 SDValue Base1, Base2;
Jim Laskey71382342006-10-07 23:37:56 +00005514 int64_t Offset1, Offset2;
5515 bool KnownBase1 = FindBaseOffset(Ptr1, Base1, Offset1);
5516 bool KnownBase2 = FindBaseOffset(Ptr2, Base2, Offset2);
5517
5518 // If they have a same base address then...
5519 if (Base1 == Base2) {
5520 // Check to see if the addresses overlap.
5521 return!((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
5522 }
5523
Jim Laskey096c22e2006-10-18 12:29:57 +00005524 // If we know both bases then they can't alias.
5525 if (KnownBase1 && KnownBase2) return false;
5526
Jim Laskey07a27092006-10-18 19:08:31 +00005527 if (CombinerGlobalAA) {
5528 // Use alias analysis information.
Dan Gohmane9c8fa02007-08-27 16:32:11 +00005529 int64_t MinOffset = std::min(SrcValueOffset1, SrcValueOffset2);
5530 int64_t Overlap1 = Size1 + SrcValueOffset1 - MinOffset;
5531 int64_t Overlap2 = Size2 + SrcValueOffset2 - MinOffset;
Jim Laskey07a27092006-10-18 19:08:31 +00005532 AliasAnalysis::AliasResult AAResult =
Jim Laskey096c22e2006-10-18 12:29:57 +00005533 AA.alias(SrcValue1, Overlap1, SrcValue2, Overlap2);
Jim Laskey07a27092006-10-18 19:08:31 +00005534 if (AAResult == AliasAnalysis::NoAlias)
5535 return false;
5536 }
Jim Laskey096c22e2006-10-18 12:29:57 +00005537
5538 // Otherwise we have to assume they alias.
5539 return true;
Jim Laskey71382342006-10-07 23:37:56 +00005540}
5541
5542/// FindAliasInfo - Extracts the relevant alias information from the memory
5543/// node. Returns true if the operand was a load.
Jim Laskey7ca56af2006-10-11 13:47:09 +00005544bool DAGCombiner::FindAliasInfo(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +00005545 SDValue &Ptr, int64_t &Size,
Jim Laskey096c22e2006-10-18 12:29:57 +00005546 const Value *&SrcValue, int &SrcValueOffset) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00005547 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
5548 Ptr = LD->getBasePtr();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005549 Size = LD->getMemoryVT().getSizeInBits() >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005550 SrcValue = LD->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00005551 SrcValueOffset = LD->getSrcValueOffset();
Jim Laskey71382342006-10-07 23:37:56 +00005552 return true;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005553 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00005554 Ptr = ST->getBasePtr();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005555 Size = ST->getMemoryVT().getSizeInBits() >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005556 SrcValue = ST->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00005557 SrcValueOffset = ST->getSrcValueOffset();
Jim Laskey7ca56af2006-10-11 13:47:09 +00005558 } else {
Jim Laskey71382342006-10-07 23:37:56 +00005559 assert(0 && "FindAliasInfo expected a memory operand");
Jim Laskey71382342006-10-07 23:37:56 +00005560 }
5561
5562 return false;
5563}
5564
Jim Laskey6ff23e52006-10-04 16:53:27 +00005565/// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
5566/// looking for aliasing nodes and adding them to the Aliases vector.
Dan Gohman475871a2008-07-27 21:46:04 +00005567void DAGCombiner::GatherAllAliases(SDNode *N, SDValue OriginalChain,
5568 SmallVector<SDValue, 8> &Aliases) {
5569 SmallVector<SDValue, 8> Chains; // List of chains to visit.
Jim Laskey6ff23e52006-10-04 16:53:27 +00005570 std::set<SDNode *> Visited; // Visited node set.
5571
Jim Laskey279f0532006-09-25 16:29:54 +00005572 // Get alias information for node.
Dan Gohman475871a2008-07-27 21:46:04 +00005573 SDValue Ptr;
Jim Laskey279f0532006-09-25 16:29:54 +00005574 int64_t Size;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005575 const Value *SrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00005576 int SrcValueOffset;
5577 bool IsLoad = FindAliasInfo(N, Ptr, Size, SrcValue, SrcValueOffset);
Jim Laskey279f0532006-09-25 16:29:54 +00005578
Jim Laskey6ff23e52006-10-04 16:53:27 +00005579 // Starting off.
Jim Laskeybc588b82006-10-05 15:07:25 +00005580 Chains.push_back(OriginalChain);
Jim Laskey6ff23e52006-10-04 16:53:27 +00005581
Jim Laskeybc588b82006-10-05 15:07:25 +00005582 // Look at each chain and determine if it is an alias. If so, add it to the
5583 // aliases list. If not, then continue up the chain looking for the next
5584 // candidate.
5585 while (!Chains.empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005586 SDValue Chain = Chains.back();
Jim Laskeybc588b82006-10-05 15:07:25 +00005587 Chains.pop_back();
Jim Laskey6ff23e52006-10-04 16:53:27 +00005588
Jim Laskeybc588b82006-10-05 15:07:25 +00005589 // Don't bother if we've been before.
Gabor Greifba36cb52008-08-28 21:40:38 +00005590 if (Visited.find(Chain.getNode()) != Visited.end()) continue;
5591 Visited.insert(Chain.getNode());
Jim Laskeybc588b82006-10-05 15:07:25 +00005592
5593 switch (Chain.getOpcode()) {
5594 case ISD::EntryToken:
5595 // Entry token is ideal chain operand, but handled in FindBetterChain.
5596 break;
Jim Laskey6ff23e52006-10-04 16:53:27 +00005597
Jim Laskeybc588b82006-10-05 15:07:25 +00005598 case ISD::LOAD:
5599 case ISD::STORE: {
5600 // Get alias information for Chain.
Dan Gohman475871a2008-07-27 21:46:04 +00005601 SDValue OpPtr;
Jim Laskeybc588b82006-10-05 15:07:25 +00005602 int64_t OpSize;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005603 const Value *OpSrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00005604 int OpSrcValueOffset;
Gabor Greifba36cb52008-08-28 21:40:38 +00005605 bool IsOpLoad = FindAliasInfo(Chain.getNode(), OpPtr, OpSize,
Jim Laskey096c22e2006-10-18 12:29:57 +00005606 OpSrcValue, OpSrcValueOffset);
Jim Laskeybc588b82006-10-05 15:07:25 +00005607
5608 // If chain is alias then stop here.
5609 if (!(IsLoad && IsOpLoad) &&
Jim Laskey096c22e2006-10-18 12:29:57 +00005610 isAlias(Ptr, Size, SrcValue, SrcValueOffset,
5611 OpPtr, OpSize, OpSrcValue, OpSrcValueOffset)) {
Jim Laskeybc588b82006-10-05 15:07:25 +00005612 Aliases.push_back(Chain);
5613 } else {
5614 // Look further up the chain.
5615 Chains.push_back(Chain.getOperand(0));
5616 // Clean up old chain.
Gabor Greifba36cb52008-08-28 21:40:38 +00005617 AddToWorkList(Chain.getNode());
Jim Laskey279f0532006-09-25 16:29:54 +00005618 }
Jim Laskeybc588b82006-10-05 15:07:25 +00005619 break;
5620 }
5621
5622 case ISD::TokenFactor:
5623 // We have to check each of the operands of the token factor, so we queue
5624 // then up. Adding the operands to the queue (stack) in reverse order
5625 // maintains the original order and increases the likelihood that getNode
5626 // will find a matching token factor (CSE.)
5627 for (unsigned n = Chain.getNumOperands(); n;)
5628 Chains.push_back(Chain.getOperand(--n));
5629 // Eliminate the token factor if we can.
Gabor Greifba36cb52008-08-28 21:40:38 +00005630 AddToWorkList(Chain.getNode());
Jim Laskeybc588b82006-10-05 15:07:25 +00005631 break;
5632
5633 default:
5634 // For all other instructions we will just have to take what we can get.
5635 Aliases.push_back(Chain);
5636 break;
Jim Laskey279f0532006-09-25 16:29:54 +00005637 }
5638 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00005639}
5640
5641/// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, looking
5642/// for a better chain (aliasing node.)
Dan Gohman475871a2008-07-27 21:46:04 +00005643SDValue DAGCombiner::FindBetterChain(SDNode *N, SDValue OldChain) {
5644 SmallVector<SDValue, 8> Aliases; // Ops for replacing token factor.
Jim Laskey279f0532006-09-25 16:29:54 +00005645
Jim Laskey6ff23e52006-10-04 16:53:27 +00005646 // Accumulate all the aliases to this node.
5647 GatherAllAliases(N, OldChain, Aliases);
5648
5649 if (Aliases.size() == 0) {
5650 // If no operands then chain to entry token.
5651 return DAG.getEntryNode();
5652 } else if (Aliases.size() == 1) {
5653 // If a single operand then chain to it. We don't need to revisit it.
5654 return Aliases[0];
5655 }
5656
5657 // Construct a custom tailored token factor.
Dan Gohman475871a2008-07-27 21:46:04 +00005658 SDValue NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
Jim Laskey6ff23e52006-10-04 16:53:27 +00005659 &Aliases[0], Aliases.size());
5660
5661 // Make sure the old chain gets cleaned up.
Gabor Greifba36cb52008-08-28 21:40:38 +00005662 if (NewChain != OldChain) AddToWorkList(OldChain.getNode());
Jim Laskey6ff23e52006-10-04 16:53:27 +00005663
5664 return NewChain;
Jim Laskey279f0532006-09-25 16:29:54 +00005665}
5666
Nate Begeman1d4d4142005-09-01 00:19:25 +00005667// SelectionDAG::Combine - This is the entry point for the file.
5668//
Dan Gohmana2676512008-08-20 16:30:28 +00005669void SelectionDAG::Combine(bool RunningAfterLegalize, AliasAnalysis &AA,
5670 bool Fast) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00005671 /// run - This is the main entry point to this class.
5672 ///
Dan Gohmana2676512008-08-20 16:30:28 +00005673 DAGCombiner(*this, AA, Fast).Run(RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +00005674}