blob: eb6481c996a77a2aa7ad8a3234f6a3fa2b9f6a3f [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,
Bill Wendlingae89bb12008-11-11 08:25:46 +0000895 SelectionDAG &DAG, const TargetLowering &TLI,
896 bool AfterLegalize) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000897 MVT VT = N->getValueType(0);
Evan Chengb13cdbd2007-06-21 07:39:16 +0000898 unsigned Opc = N->getOpcode();
899 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
Dan Gohman475871a2008-07-27 21:46:04 +0000900 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
901 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
Evan Chengb13cdbd2007-06-21 07:39:16 +0000902 ISD::CondCode CC = ISD::SETCC_INVALID;
Bill Wendlingae89bb12008-11-11 08:25:46 +0000903
904 if (isSlctCC) {
Evan Chengb13cdbd2007-06-21 07:39:16 +0000905 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
Bill Wendlingae89bb12008-11-11 08:25:46 +0000906 } else {
Dan Gohman475871a2008-07-27 21:46:04 +0000907 SDValue CCOp = Slct.getOperand(0);
Evan Chengb13cdbd2007-06-21 07:39:16 +0000908 if (CCOp.getOpcode() == ISD::SETCC)
909 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
910 }
911
912 bool DoXform = false;
913 bool InvCC = false;
914 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
915 "Bad input!");
Bill Wendlingae89bb12008-11-11 08:25:46 +0000916
Evan Chengb13cdbd2007-06-21 07:39:16 +0000917 if (LHS.getOpcode() == ISD::Constant &&
Bill Wendlingae89bb12008-11-11 08:25:46 +0000918 cast<ConstantSDNode>(LHS)->isNullValue()) {
Evan Chengb13cdbd2007-06-21 07:39:16 +0000919 DoXform = true;
Bill Wendlingae89bb12008-11-11 08:25:46 +0000920 } else if (CC != ISD::SETCC_INVALID &&
921 RHS.getOpcode() == ISD::Constant &&
922 cast<ConstantSDNode>(RHS)->isNullValue()) {
Evan Chengb13cdbd2007-06-21 07:39:16 +0000923 std::swap(LHS, RHS);
Dan Gohman475871a2008-07-27 21:46:04 +0000924 SDValue Op0 = Slct.getOperand(0);
Bill Wendlingae89bb12008-11-11 08:25:46 +0000925 MVT OpVT = isSlctCC ? Op0.getValueType() :
926 Op0.getOperand(0).getValueType();
927 bool isInt = OpVT.isInteger();
Evan Chengb13cdbd2007-06-21 07:39:16 +0000928 CC = ISD::getSetCCInverse(CC, isInt);
Bill Wendlingae89bb12008-11-11 08:25:46 +0000929
930 if (AfterLegalize && !TLI.isCondCodeLegal(CC, OpVT))
931 return SDValue(); // Inverse operator isn't legal.
932
Evan Chengb13cdbd2007-06-21 07:39:16 +0000933 DoXform = true;
934 InvCC = true;
935 }
936
937 if (DoXform) {
Dan Gohman475871a2008-07-27 21:46:04 +0000938 SDValue Result = DAG.getNode(Opc, VT, OtherOp, RHS);
Evan Chengb13cdbd2007-06-21 07:39:16 +0000939 if (isSlctCC)
940 return DAG.getSelectCC(OtherOp, Result,
941 Slct.getOperand(0), Slct.getOperand(1), CC);
Dan Gohman475871a2008-07-27 21:46:04 +0000942 SDValue CCOp = Slct.getOperand(0);
Evan Chengb13cdbd2007-06-21 07:39:16 +0000943 if (InvCC)
944 CCOp = DAG.getSetCC(CCOp.getValueType(), CCOp.getOperand(0),
945 CCOp.getOperand(1), CC);
946 return DAG.getNode(ISD::SELECT, VT, CCOp, OtherOp, Result);
947 }
Dan Gohman475871a2008-07-27 21:46:04 +0000948 return SDValue();
Evan Chengb13cdbd2007-06-21 07:39:16 +0000949}
950
Dan Gohman475871a2008-07-27 21:46:04 +0000951SDValue DAGCombiner::visitADD(SDNode *N) {
952 SDValue N0 = N->getOperand(0);
953 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000954 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
955 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000956 MVT VT = N0.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +0000957
958 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +0000959 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +0000960 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +0000961 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +0000962 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000963
Dan Gohman613e0d82007-07-03 14:03:57 +0000964 // fold (add x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +0000965 if (N0.getOpcode() == ISD::UNDEF)
966 return N0;
967 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +0000968 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000969 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000970 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +0000971 return DAG.FoldConstantArithmetic(ISD::ADD, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +0000972 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000973 if (N0C && !N1C)
974 return DAG.getNode(ISD::ADD, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000975 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000976 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000977 return N0;
Dan Gohman6520e202008-10-18 02:06:02 +0000978 // fold (add Sym, c) -> Sym+c
979 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0))
980 if (!AfterLegalize && TLI.isOffsetFoldingLegal(GA) && N1C &&
981 GA->getOpcode() == ISD::GlobalAddress)
982 return DAG.getGlobalAddress(GA->getGlobal(), VT,
983 GA->getOffset() +
984 (uint64_t)N1C->getSExtValue());
Chris Lattner4aafb4f2006-01-12 20:22:43 +0000985 // fold ((c1-A)+c2) -> (c1+c2)-A
986 if (N1C && N0.getOpcode() == ISD::SUB)
987 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
988 return DAG.getNode(ISD::SUB, VT,
Dan Gohman002e5d02008-03-13 22:13:53 +0000989 DAG.getConstant(N1C->getAPIntValue()+
990 N0C->getAPIntValue(), VT),
Chris Lattner4aafb4f2006-01-12 20:22:43 +0000991 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000992 // reassociate add
Dan Gohman475871a2008-07-27 21:46:04 +0000993 SDValue RADD = ReassociateOps(ISD::ADD, N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +0000994 if (RADD.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +0000995 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000996 // fold ((0-A) + B) -> B-A
997 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
998 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000999 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001000 // fold (A + (0-B)) -> A-B
1001 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
1002 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +00001003 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +00001004 // fold (A+(B-A)) -> B
1005 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001006 return N1.getOperand(0);
Chris Lattner947c2892006-03-13 06:51:27 +00001007
Dan Gohman475871a2008-07-27 21:46:04 +00001008 if (!VT.isVector() && SimplifyDemandedBits(SDValue(N, 0)))
1009 return SDValue(N, 0);
Chris Lattner947c2892006-03-13 06:51:27 +00001010
1011 // fold (a+b) -> (a|b) iff a and b share no bits.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001012 if (VT.isInteger() && !VT.isVector()) {
Dan Gohman948d8ea2008-02-20 16:33:30 +00001013 APInt LHSZero, LHSOne;
1014 APInt RHSZero, RHSOne;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001015 APInt Mask = APInt::getAllOnesValue(VT.getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00001016 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Dan Gohman948d8ea2008-02-20 16:33:30 +00001017 if (LHSZero.getBoolValue()) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001018 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
Chris Lattner947c2892006-03-13 06:51:27 +00001019
1020 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1021 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
1022 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
1023 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
1024 return DAG.getNode(ISD::OR, VT, N0, N1);
1025 }
1026 }
Evan Cheng3ef554d2006-11-06 08:14:30 +00001027
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001028 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
Gabor Greifba36cb52008-08-28 21:40:38 +00001029 if (N0.getOpcode() == ISD::SHL && N0.getNode()->hasOneUse()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001030 SDValue Result = combineShlAddConstant(N0, N1, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001031 if (Result.getNode()) return Result;
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001032 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001033 if (N1.getOpcode() == ISD::SHL && N1.getNode()->hasOneUse()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001034 SDValue Result = combineShlAddConstant(N1, N0, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001035 if (Result.getNode()) return Result;
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001036 }
1037
Evan Chengb13cdbd2007-06-21 07:39:16 +00001038 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
Gabor Greifba36cb52008-08-28 21:40:38 +00001039 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
Bill Wendlingae89bb12008-11-11 08:25:46 +00001040 SDValue Result = combineSelectAndUse(N, N0, N1, DAG, TLI, AfterLegalize);
Gabor Greifba36cb52008-08-28 21:40:38 +00001041 if (Result.getNode()) return Result;
Evan Chengb13cdbd2007-06-21 07:39:16 +00001042 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001043 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
Bill Wendlingae89bb12008-11-11 08:25:46 +00001044 SDValue Result = combineSelectAndUse(N, N1, N0, DAG, TLI, AfterLegalize);
Gabor Greifba36cb52008-08-28 21:40:38 +00001045 if (Result.getNode()) return Result;
Evan Chengb13cdbd2007-06-21 07:39:16 +00001046 }
1047
Dan Gohman475871a2008-07-27 21:46:04 +00001048 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001049}
1050
Dan Gohman475871a2008-07-27 21:46:04 +00001051SDValue DAGCombiner::visitADDC(SDNode *N) {
1052 SDValue N0 = N->getOperand(0);
1053 SDValue N1 = N->getOperand(1);
Chris Lattner91153682007-03-04 20:03:15 +00001054 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1055 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001056 MVT VT = N0.getValueType();
Chris Lattner91153682007-03-04 20:03:15 +00001057
1058 // If the flag result is dead, turn this into an ADD.
1059 if (N->hasNUsesOfValue(0, 1))
1060 return CombineTo(N, DAG.getNode(ISD::ADD, VT, N1, N0),
Chris Lattnerb6541762007-03-04 20:40:38 +00001061 DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
Chris Lattner91153682007-03-04 20:03:15 +00001062
1063 // canonicalize constant to RHS.
Dan Gohman0a4627d2008-06-23 15:29:14 +00001064 if (N0C && !N1C)
Dan Gohman56867522008-06-21 22:06:07 +00001065 return DAG.getNode(ISD::ADDC, N->getVTList(), N1, N0);
Chris Lattner91153682007-03-04 20:03:15 +00001066
Chris Lattnerb6541762007-03-04 20:40:38 +00001067 // fold (addc x, 0) -> x + no carry out
1068 if (N1C && N1C->isNullValue())
1069 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
1070
1071 // fold (addc a, b) -> (or a, b), CARRY_FALSE iff a and b share no bits.
Dan Gohman948d8ea2008-02-20 16:33:30 +00001072 APInt LHSZero, LHSOne;
1073 APInt RHSZero, RHSOne;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001074 APInt Mask = APInt::getAllOnesValue(VT.getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00001075 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Dan Gohman948d8ea2008-02-20 16:33:30 +00001076 if (LHSZero.getBoolValue()) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001077 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
Chris Lattnerb6541762007-03-04 20:40:38 +00001078
1079 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1080 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
1081 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
1082 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
1083 return CombineTo(N, DAG.getNode(ISD::OR, VT, N0, N1),
1084 DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
1085 }
Chris Lattner91153682007-03-04 20:03:15 +00001086
Dan Gohman475871a2008-07-27 21:46:04 +00001087 return SDValue();
Chris Lattner91153682007-03-04 20:03:15 +00001088}
1089
Dan Gohman475871a2008-07-27 21:46:04 +00001090SDValue DAGCombiner::visitADDE(SDNode *N) {
1091 SDValue N0 = N->getOperand(0);
1092 SDValue N1 = N->getOperand(1);
1093 SDValue CarryIn = N->getOperand(2);
Chris Lattner91153682007-03-04 20:03:15 +00001094 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1095 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001096 //MVT VT = N0.getValueType();
Chris Lattner91153682007-03-04 20:03:15 +00001097
1098 // canonicalize constant to RHS
Dan Gohman0a4627d2008-06-23 15:29:14 +00001099 if (N0C && !N1C)
Dan Gohman56867522008-06-21 22:06:07 +00001100 return DAG.getNode(ISD::ADDE, N->getVTList(), N1, N0, CarryIn);
Chris Lattner91153682007-03-04 20:03:15 +00001101
Chris Lattnerb6541762007-03-04 20:40:38 +00001102 // fold (adde x, y, false) -> (addc x, y)
Dan Gohman0a4627d2008-06-23 15:29:14 +00001103 if (CarryIn.getOpcode() == ISD::CARRY_FALSE)
Dan Gohman56867522008-06-21 22:06:07 +00001104 return DAG.getNode(ISD::ADDC, N->getVTList(), N1, N0);
Chris Lattner91153682007-03-04 20:03:15 +00001105
Dan Gohman475871a2008-07-27 21:46:04 +00001106 return SDValue();
Chris Lattner91153682007-03-04 20:03:15 +00001107}
1108
1109
1110
Dan Gohman475871a2008-07-27 21:46:04 +00001111SDValue DAGCombiner::visitSUB(SDNode *N) {
1112 SDValue N0 = N->getOperand(0);
1113 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001114 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1115 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Duncan Sands83ec4b62008-06-06 12:08:01 +00001116 MVT VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001117
Dan Gohman7f321562007-06-25 16:23:39 +00001118 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001119 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001120 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001121 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001122 }
Dan Gohman7f321562007-06-25 16:23:39 +00001123
Chris Lattner854077d2005-10-17 01:07:11 +00001124 // fold (sub x, x) -> 0
Evan Chengc8e3b142008-03-12 07:02:50 +00001125 if (N0 == N1)
Chris Lattner854077d2005-10-17 01:07:11 +00001126 return DAG.getConstant(0, N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001127 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001128 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001129 return DAG.FoldConstantArithmetic(ISD::SUB, VT, N0C, N1C);
Chris Lattner05b57432005-10-11 06:07:15 +00001130 // fold (sub x, c) -> (add x, -c)
1131 if (N1C)
Dan Gohman002e5d02008-03-13 22:13:53 +00001132 return DAG.getNode(ISD::ADD, VT, N0,
1133 DAG.getConstant(-N1C->getAPIntValue(), VT));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001134 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +00001135 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001136 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001137 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +00001138 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001139 return N0.getOperand(0);
Evan Chengb13cdbd2007-06-21 07:39:16 +00001140 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
Gabor Greifba36cb52008-08-28 21:40:38 +00001141 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
Bill Wendlingae89bb12008-11-11 08:25:46 +00001142 SDValue Result = combineSelectAndUse(N, N1, N0, DAG, TLI, AfterLegalize);
Gabor Greifba36cb52008-08-28 21:40:38 +00001143 if (Result.getNode()) return Result;
Evan Chengb13cdbd2007-06-21 07:39:16 +00001144 }
Dan Gohman613e0d82007-07-03 14:03:57 +00001145 // If either operand of a sub is undef, the result is undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00001146 if (N0.getOpcode() == ISD::UNDEF)
1147 return N0;
1148 if (N1.getOpcode() == ISD::UNDEF)
1149 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001150
Dan Gohman6520e202008-10-18 02:06:02 +00001151 // If the relocation model supports it, consider symbol offsets.
1152 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0))
1153 if (!AfterLegalize && TLI.isOffsetFoldingLegal(GA)) {
1154 // fold (sub Sym, c) -> Sym-c
1155 if (N1C && GA->getOpcode() == ISD::GlobalAddress)
1156 return DAG.getGlobalAddress(GA->getGlobal(), VT,
1157 GA->getOffset() -
1158 (uint64_t)N1C->getSExtValue());
1159 // fold (sub Sym+c1, Sym+c2) -> c1-c2
1160 if (GlobalAddressSDNode *GB = dyn_cast<GlobalAddressSDNode>(N1))
1161 if (GA->getGlobal() == GB->getGlobal())
1162 return DAG.getConstant((uint64_t)GA->getOffset() - GB->getOffset(),
1163 VT);
1164 }
1165
Dan Gohman475871a2008-07-27 21:46:04 +00001166 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001167}
1168
Dan Gohman475871a2008-07-27 21:46:04 +00001169SDValue DAGCombiner::visitMUL(SDNode *N) {
1170 SDValue N0 = N->getOperand(0);
1171 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001172 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1173 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001174 MVT VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001175
Dan Gohman7f321562007-06-25 16:23:39 +00001176 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001177 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001178 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001179 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001180 }
Dan Gohman7f321562007-06-25 16:23:39 +00001181
Dan Gohman613e0d82007-07-03 14:03:57 +00001182 // fold (mul x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001183 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001184 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001185 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001186 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001187 return DAG.FoldConstantArithmetic(ISD::MUL, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00001188 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001189 if (N0C && !N1C)
1190 return DAG.getNode(ISD::MUL, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001191 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001192 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001193 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001194 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +00001195 if (N1C && N1C->isAllOnesValue())
Nate Begeman405e3ec2005-10-21 00:02:42 +00001196 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001197 // fold (mul x, (1 << c)) -> x << c
Dan Gohman002e5d02008-03-13 22:13:53 +00001198 if (N1C && N1C->getAPIntValue().isPowerOf2())
Chris Lattner3e6099b2005-10-30 06:41:49 +00001199 return DAG.getNode(ISD::SHL, VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001200 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001201 TLI.getShiftAmountTy()));
Chris Lattner3e6099b2005-10-30 06:41:49 +00001202 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
Dan Gohman7810bfe2008-09-26 21:54:37 +00001203 if (N1C && isPowerOf2_64(-N1C->getSExtValue())) {
Chris Lattner3e6099b2005-10-30 06:41:49 +00001204 // FIXME: If the input is something that is easily negated (e.g. a
1205 // single-use add), we should put the negate there.
1206 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT),
1207 DAG.getNode(ISD::SHL, VT, N0,
Dan Gohman7810bfe2008-09-26 21:54:37 +00001208 DAG.getConstant(Log2_64(-N1C->getSExtValue()),
Chris Lattner3e6099b2005-10-30 06:41:49 +00001209 TLI.getShiftAmountTy())));
1210 }
Andrew Lenharth50a0d422006-04-02 21:42:45 +00001211
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001212 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
1213 if (N1C && N0.getOpcode() == ISD::SHL &&
1214 isa<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohman475871a2008-07-27 21:46:04 +00001215 SDValue C3 = DAG.getNode(ISD::SHL, VT, N1, N0.getOperand(1));
Gabor Greifba36cb52008-08-28 21:40:38 +00001216 AddToWorkList(C3.getNode());
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001217 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0), C3);
1218 }
1219
1220 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
1221 // use.
1222 {
Dan Gohman475871a2008-07-27 21:46:04 +00001223 SDValue Sh(0,0), Y(0,0);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001224 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
1225 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001226 N0.getNode()->hasOneUse()) {
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001227 Sh = N0; Y = N1;
1228 } else if (N1.getOpcode() == ISD::SHL &&
Gabor Greif12632d22008-08-30 19:29:20 +00001229 isa<ConstantSDNode>(N1.getOperand(1)) &&
1230 N1.getNode()->hasOneUse()) {
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001231 Sh = N1; Y = N0;
1232 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001233 if (Sh.getNode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001234 SDValue Mul = DAG.getNode(ISD::MUL, VT, Sh.getOperand(0), Y);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001235 return DAG.getNode(ISD::SHL, VT, Mul, Sh.getOperand(1));
1236 }
1237 }
Chris Lattnera1deca32006-03-04 23:33:26 +00001238 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
Gabor Greifba36cb52008-08-28 21:40:38 +00001239 if (N1C && N0.getOpcode() == ISD::ADD && N0.getNode()->hasOneUse() &&
Chris Lattnera1deca32006-03-04 23:33:26 +00001240 isa<ConstantSDNode>(N0.getOperand(1))) {
1241 return DAG.getNode(ISD::ADD, VT,
1242 DAG.getNode(ISD::MUL, VT, N0.getOperand(0), N1),
1243 DAG.getNode(ISD::MUL, VT, N0.getOperand(1), N1));
1244 }
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001245
Nate Begemancd4d58c2006-02-03 06:46:56 +00001246 // reassociate mul
Dan Gohman475871a2008-07-27 21:46:04 +00001247 SDValue RMUL = ReassociateOps(ISD::MUL, N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001248 if (RMUL.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001249 return RMUL;
Dan Gohman7f321562007-06-25 16:23:39 +00001250
Dan Gohman475871a2008-07-27 21:46:04 +00001251 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001252}
1253
Dan Gohman475871a2008-07-27 21:46:04 +00001254SDValue DAGCombiner::visitSDIV(SDNode *N) {
1255 SDValue N0 = N->getOperand(0);
1256 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001257 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1258 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Duncan Sands83ec4b62008-06-06 12:08:01 +00001259 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001260
Dan Gohman7f321562007-06-25 16:23:39 +00001261 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001262 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001263 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001264 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001265 }
Dan Gohman7f321562007-06-25 16:23:39 +00001266
Nate Begeman1d4d4142005-09-01 00:19:25 +00001267 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001268 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001269 return DAG.FoldConstantArithmetic(ISD::SDIV, VT, N0C, N1C);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001270 // fold (sdiv X, 1) -> X
Dan Gohman7810bfe2008-09-26 21:54:37 +00001271 if (N1C && N1C->getSExtValue() == 1LL)
Nate Begeman405e3ec2005-10-21 00:02:42 +00001272 return N0;
1273 // fold (sdiv X, -1) -> 0-X
1274 if (N1C && N1C->isAllOnesValue())
1275 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +00001276 // If we know the sign bits of both operands are zero, strength reduce to a
1277 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
Duncan Sands83ec4b62008-06-06 12:08:01 +00001278 if (!VT.isVector()) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001279 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Chris Lattnerf32aac32008-01-27 23:32:17 +00001280 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
1281 }
Nate Begemancd6a6ed2006-02-17 07:26:20 +00001282 // fold (sdiv X, pow2) -> simple ops after legalize
Dan Gohman002e5d02008-03-13 22:13:53 +00001283 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap() &&
Dan Gohman7810bfe2008-09-26 21:54:37 +00001284 (isPowerOf2_64(N1C->getSExtValue()) ||
1285 isPowerOf2_64(-N1C->getSExtValue()))) {
Nate Begeman405e3ec2005-10-21 00:02:42 +00001286 // If dividing by powers of two is cheap, then don't perform the following
1287 // fold.
1288 if (TLI.isPow2DivCheap())
Dan Gohman475871a2008-07-27 21:46:04 +00001289 return SDValue();
Dan Gohman7810bfe2008-09-26 21:54:37 +00001290 int64_t pow2 = N1C->getSExtValue();
Nate Begeman405e3ec2005-10-21 00:02:42 +00001291 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
Chris Lattner8f4880b2006-02-16 08:02:36 +00001292 unsigned lg2 = Log2_64(abs2);
1293 // Splat the sign bit into the register
Dan Gohman475871a2008-07-27 21:46:04 +00001294 SDValue SGN = DAG.getNode(ISD::SRA, VT, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001295 DAG.getConstant(VT.getSizeInBits()-1,
Nate Begeman405e3ec2005-10-21 00:02:42 +00001296 TLI.getShiftAmountTy()));
Gabor Greifba36cb52008-08-28 21:40:38 +00001297 AddToWorkList(SGN.getNode());
Chris Lattner8f4880b2006-02-16 08:02:36 +00001298 // Add (N0 < 0) ? abs2 - 1 : 0;
Dan Gohman475871a2008-07-27 21:46:04 +00001299 SDValue SRL = DAG.getNode(ISD::SRL, VT, SGN,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001300 DAG.getConstant(VT.getSizeInBits()-lg2,
Nate Begeman405e3ec2005-10-21 00:02:42 +00001301 TLI.getShiftAmountTy()));
Dan Gohman475871a2008-07-27 21:46:04 +00001302 SDValue ADD = DAG.getNode(ISD::ADD, VT, N0, SRL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001303 AddToWorkList(SRL.getNode());
1304 AddToWorkList(ADD.getNode()); // Divide by pow2
Dan Gohman475871a2008-07-27 21:46:04 +00001305 SDValue SRA = DAG.getNode(ISD::SRA, VT, ADD,
Chris Lattner8f4880b2006-02-16 08:02:36 +00001306 DAG.getConstant(lg2, TLI.getShiftAmountTy()));
Nate Begeman405e3ec2005-10-21 00:02:42 +00001307 // If we're dividing by a positive value, we're done. Otherwise, we must
1308 // negate the result.
1309 if (pow2 > 0)
1310 return SRA;
Gabor Greifba36cb52008-08-28 21:40:38 +00001311 AddToWorkList(SRA.getNode());
Nate Begeman405e3ec2005-10-21 00:02:42 +00001312 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), SRA);
1313 }
Nate Begeman69575232005-10-20 02:15:44 +00001314 // if integer divide is expensive and we satisfy the requirements, emit an
1315 // alternate sequence.
Dan Gohman7810bfe2008-09-26 21:54:37 +00001316 if (N1C && (N1C->getSExtValue() < -1 || N1C->getSExtValue() > 1) &&
Chris Lattnere9936d12005-10-22 18:50:15 +00001317 !TLI.isIntDivCheap()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001318 SDValue Op = BuildSDIV(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001319 if (Op.getNode()) return Op;
Nate Begeman69575232005-10-20 02:15:44 +00001320 }
Dan Gohman7f321562007-06-25 16:23:39 +00001321
Dan Gohman613e0d82007-07-03 14:03:57 +00001322 // undef / X -> 0
1323 if (N0.getOpcode() == ISD::UNDEF)
1324 return DAG.getConstant(0, VT);
1325 // X / undef -> undef
1326 if (N1.getOpcode() == ISD::UNDEF)
1327 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001328
Dan Gohman475871a2008-07-27 21:46:04 +00001329 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001330}
1331
Dan Gohman475871a2008-07-27 21:46:04 +00001332SDValue DAGCombiner::visitUDIV(SDNode *N) {
1333 SDValue N0 = N->getOperand(0);
1334 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001335 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1336 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Duncan Sands83ec4b62008-06-06 12:08:01 +00001337 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001338
Dan Gohman7f321562007-06-25 16:23:39 +00001339 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001340 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001341 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001342 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001343 }
Dan Gohman7f321562007-06-25 16:23:39 +00001344
Nate Begeman1d4d4142005-09-01 00:19:25 +00001345 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001346 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001347 return DAG.FoldConstantArithmetic(ISD::UDIV, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001348 // fold (udiv x, (1 << c)) -> x >>u c
Dan Gohman002e5d02008-03-13 22:13:53 +00001349 if (N1C && N1C->getAPIntValue().isPowerOf2())
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001350 return DAG.getNode(ISD::SRL, VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001351 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001352 TLI.getShiftAmountTy()));
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001353 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
1354 if (N1.getOpcode() == ISD::SHL) {
1355 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00001356 if (SHC->getAPIntValue().isPowerOf2()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001357 MVT ADDVT = N1.getOperand(1).getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00001358 SDValue Add = DAG.getNode(ISD::ADD, ADDVT, N1.getOperand(1),
Dan Gohman002e5d02008-03-13 22:13:53 +00001359 DAG.getConstant(SHC->getAPIntValue()
1360 .logBase2(),
Nate Begemanc031e332006-02-05 07:36:48 +00001361 ADDVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00001362 AddToWorkList(Add.getNode());
Nate Begemanc031e332006-02-05 07:36:48 +00001363 return DAG.getNode(ISD::SRL, VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001364 }
1365 }
1366 }
Nate Begeman69575232005-10-20 02:15:44 +00001367 // fold (udiv x, c) -> alternate
Dan Gohman002e5d02008-03-13 22:13:53 +00001368 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001369 SDValue Op = BuildUDIV(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001370 if (Op.getNode()) return Op;
Chris Lattnere9936d12005-10-22 18:50:15 +00001371 }
Dan Gohman7f321562007-06-25 16:23:39 +00001372
Dan Gohman613e0d82007-07-03 14:03:57 +00001373 // undef / X -> 0
1374 if (N0.getOpcode() == ISD::UNDEF)
1375 return DAG.getConstant(0, VT);
1376 // X / undef -> undef
1377 if (N1.getOpcode() == ISD::UNDEF)
1378 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001379
Dan Gohman475871a2008-07-27 21:46:04 +00001380 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001381}
1382
Dan Gohman475871a2008-07-27 21:46:04 +00001383SDValue DAGCombiner::visitSREM(SDNode *N) {
1384 SDValue N0 = N->getOperand(0);
1385 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001386 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1387 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001388 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001389
1390 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001391 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001392 return DAG.FoldConstantArithmetic(ISD::SREM, VT, N0C, N1C);
Nate Begeman07ed4172005-10-10 21:26:48 +00001393 // If we know the sign bits of both operands are zero, strength reduce to a
1394 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
Duncan Sands83ec4b62008-06-06 12:08:01 +00001395 if (!VT.isVector()) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001396 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Chris Lattneree339f42008-01-27 23:21:58 +00001397 return DAG.getNode(ISD::UREM, VT, N0, N1);
1398 }
Chris Lattner26d29902006-10-12 20:58:32 +00001399
Dan Gohman77003042007-11-26 23:46:11 +00001400 // If X/C can be simplified by the division-by-constant logic, lower
1401 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00001402 if (N1C && !N1C->isNullValue()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001403 SDValue Div = DAG.getNode(ISD::SDIV, VT, N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001404 AddToWorkList(Div.getNode());
1405 SDValue OptimizedDiv = combine(Div.getNode());
1406 if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001407 SDValue Mul = DAG.getNode(ISD::MUL, VT, OptimizedDiv, N1);
1408 SDValue Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
Gabor Greifba36cb52008-08-28 21:40:38 +00001409 AddToWorkList(Mul.getNode());
Dan Gohman77003042007-11-26 23:46:11 +00001410 return Sub;
1411 }
Chris Lattner26d29902006-10-12 20:58:32 +00001412 }
1413
Dan Gohman613e0d82007-07-03 14:03:57 +00001414 // undef % X -> 0
1415 if (N0.getOpcode() == ISD::UNDEF)
1416 return DAG.getConstant(0, VT);
1417 // X % undef -> undef
1418 if (N1.getOpcode() == ISD::UNDEF)
1419 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001420
Dan Gohman475871a2008-07-27 21:46:04 +00001421 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001422}
1423
Dan Gohman475871a2008-07-27 21:46:04 +00001424SDValue DAGCombiner::visitUREM(SDNode *N) {
1425 SDValue N0 = N->getOperand(0);
1426 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001427 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1428 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001429 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001430
1431 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001432 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001433 return DAG.FoldConstantArithmetic(ISD::UREM, VT, N0C, N1C);
Nate Begeman07ed4172005-10-10 21:26:48 +00001434 // fold (urem x, pow2) -> (and x, pow2-1)
Dan Gohman002e5d02008-03-13 22:13:53 +00001435 if (N1C && !N1C->isNullValue() && N1C->getAPIntValue().isPowerOf2())
1436 return DAG.getNode(ISD::AND, VT, N0,
1437 DAG.getConstant(N1C->getAPIntValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +00001438 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
1439 if (N1.getOpcode() == ISD::SHL) {
1440 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00001441 if (SHC->getAPIntValue().isPowerOf2()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001442 SDValue Add =
Dan Gohman002e5d02008-03-13 22:13:53 +00001443 DAG.getNode(ISD::ADD, VT, N1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001444 DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()),
Dan Gohman002e5d02008-03-13 22:13:53 +00001445 VT));
Gabor Greifba36cb52008-08-28 21:40:38 +00001446 AddToWorkList(Add.getNode());
Nate Begemanc031e332006-02-05 07:36:48 +00001447 return DAG.getNode(ISD::AND, VT, N0, Add);
1448 }
1449 }
1450 }
Chris Lattner26d29902006-10-12 20:58:32 +00001451
Dan Gohman77003042007-11-26 23:46:11 +00001452 // If X/C can be simplified by the division-by-constant logic, lower
1453 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00001454 if (N1C && !N1C->isNullValue()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001455 SDValue Div = DAG.getNode(ISD::UDIV, VT, N0, N1);
Dan Gohman942ca7f2008-09-08 16:59:01 +00001456 AddToWorkList(Div.getNode());
Gabor Greifba36cb52008-08-28 21:40:38 +00001457 SDValue OptimizedDiv = combine(Div.getNode());
1458 if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001459 SDValue Mul = DAG.getNode(ISD::MUL, VT, OptimizedDiv, N1);
1460 SDValue Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
Gabor Greifba36cb52008-08-28 21:40:38 +00001461 AddToWorkList(Mul.getNode());
Dan Gohman77003042007-11-26 23:46:11 +00001462 return Sub;
1463 }
Chris Lattner26d29902006-10-12 20:58:32 +00001464 }
1465
Dan Gohman613e0d82007-07-03 14:03:57 +00001466 // undef % X -> 0
1467 if (N0.getOpcode() == ISD::UNDEF)
1468 return DAG.getConstant(0, VT);
1469 // X % undef -> undef
1470 if (N1.getOpcode() == ISD::UNDEF)
1471 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001472
Dan Gohman475871a2008-07-27 21:46:04 +00001473 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001474}
1475
Dan Gohman475871a2008-07-27 21:46:04 +00001476SDValue DAGCombiner::visitMULHS(SDNode *N) {
1477 SDValue N0 = N->getOperand(0);
1478 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001479 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001480 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001481
1482 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001483 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001484 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001485 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Dan Gohman002e5d02008-03-13 22:13:53 +00001486 if (N1C && N1C->getAPIntValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001487 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001488 DAG.getConstant(N0.getValueType().getSizeInBits()-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001489 TLI.getShiftAmountTy()));
Dan Gohman613e0d82007-07-03 14:03:57 +00001490 // fold (mulhs x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001491 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001492 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00001493
Dan Gohman475871a2008-07-27 21:46:04 +00001494 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001495}
1496
Dan Gohman475871a2008-07-27 21:46:04 +00001497SDValue DAGCombiner::visitMULHU(SDNode *N) {
1498 SDValue N0 = N->getOperand(0);
1499 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001500 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001501 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001502
1503 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001504 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001505 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001506 // fold (mulhu x, 1) -> 0
Dan Gohman002e5d02008-03-13 22:13:53 +00001507 if (N1C && N1C->getAPIntValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001508 return DAG.getConstant(0, N0.getValueType());
Dan Gohman613e0d82007-07-03 14:03:57 +00001509 // fold (mulhu x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001510 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001511 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00001512
Dan Gohman475871a2008-07-27 21:46:04 +00001513 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001514}
1515
Dan Gohman389079b2007-10-08 17:57:15 +00001516/// SimplifyNodeWithTwoResults - Perform optimizations common to nodes that
1517/// compute two values. LoOp and HiOp give the opcodes for the two computations
1518/// that are being performed. Return true if a simplification was made.
1519///
Dan Gohman475871a2008-07-27 21:46:04 +00001520SDValue DAGCombiner::SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
1521 unsigned HiOp) {
Dan Gohman389079b2007-10-08 17:57:15 +00001522 // If the high half is not needed, just compute the low half.
Evan Cheng44711942007-11-08 09:25:29 +00001523 bool HiExists = N->hasAnyUseOfValue(1);
1524 if (!HiExists &&
Dan Gohman389079b2007-10-08 17:57:15 +00001525 (!AfterLegalize ||
1526 TLI.isOperationLegal(LoOp, N->getValueType(0)))) {
Dan Gohman475871a2008-07-27 21:46:04 +00001527 SDValue Res = DAG.getNode(LoOp, N->getValueType(0), N->op_begin(),
Chris Lattner5eee4272008-01-26 01:09:19 +00001528 N->getNumOperands());
1529 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00001530 }
1531
1532 // If the low half is not needed, just compute the high half.
Evan Cheng44711942007-11-08 09:25:29 +00001533 bool LoExists = N->hasAnyUseOfValue(0);
1534 if (!LoExists &&
Dan Gohman389079b2007-10-08 17:57:15 +00001535 (!AfterLegalize ||
1536 TLI.isOperationLegal(HiOp, N->getValueType(1)))) {
Dan Gohman475871a2008-07-27 21:46:04 +00001537 SDValue Res = DAG.getNode(HiOp, N->getValueType(1), N->op_begin(),
Chris Lattner5eee4272008-01-26 01:09:19 +00001538 N->getNumOperands());
1539 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00001540 }
1541
Evan Cheng44711942007-11-08 09:25:29 +00001542 // If both halves are used, return as it is.
1543 if (LoExists && HiExists)
Dan Gohman475871a2008-07-27 21:46:04 +00001544 return SDValue();
Evan Cheng44711942007-11-08 09:25:29 +00001545
1546 // If the two computed results can be simplified separately, separate them.
Evan Cheng44711942007-11-08 09:25:29 +00001547 if (LoExists) {
Dan Gohman475871a2008-07-27 21:46:04 +00001548 SDValue Lo = DAG.getNode(LoOp, N->getValueType(0),
Evan Cheng44711942007-11-08 09:25:29 +00001549 N->op_begin(), N->getNumOperands());
Gabor Greifba36cb52008-08-28 21:40:38 +00001550 AddToWorkList(Lo.getNode());
1551 SDValue LoOpt = combine(Lo.getNode());
1552 if (LoOpt.getNode() && LoOpt.getNode() != Lo.getNode() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001553 (!AfterLegalize ||
1554 TLI.isOperationLegal(LoOpt.getOpcode(), LoOpt.getValueType())))
Chris Lattner5eee4272008-01-26 01:09:19 +00001555 return CombineTo(N, LoOpt, LoOpt);
Dan Gohman389079b2007-10-08 17:57:15 +00001556 }
1557
Evan Cheng44711942007-11-08 09:25:29 +00001558 if (HiExists) {
Dan Gohman475871a2008-07-27 21:46:04 +00001559 SDValue Hi = DAG.getNode(HiOp, N->getValueType(1),
Evan Cheng44711942007-11-08 09:25:29 +00001560 N->op_begin(), N->getNumOperands());
Gabor Greifba36cb52008-08-28 21:40:38 +00001561 AddToWorkList(Hi.getNode());
1562 SDValue HiOpt = combine(Hi.getNode());
1563 if (HiOpt.getNode() && HiOpt != Hi &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001564 (!AfterLegalize ||
1565 TLI.isOperationLegal(HiOpt.getOpcode(), HiOpt.getValueType())))
Chris Lattner5eee4272008-01-26 01:09:19 +00001566 return CombineTo(N, HiOpt, HiOpt);
Evan Cheng44711942007-11-08 09:25:29 +00001567 }
Dan Gohman475871a2008-07-27 21:46:04 +00001568 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001569}
1570
Dan Gohman475871a2008-07-27 21:46:04 +00001571SDValue DAGCombiner::visitSMUL_LOHI(SDNode *N) {
1572 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHS);
Gabor Greifba36cb52008-08-28 21:40:38 +00001573 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001574
Dan Gohman475871a2008-07-27 21:46:04 +00001575 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001576}
1577
Dan Gohman475871a2008-07-27 21:46:04 +00001578SDValue DAGCombiner::visitUMUL_LOHI(SDNode *N) {
1579 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHU);
Gabor Greifba36cb52008-08-28 21:40:38 +00001580 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001581
Dan Gohman475871a2008-07-27 21:46:04 +00001582 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001583}
1584
Dan Gohman475871a2008-07-27 21:46:04 +00001585SDValue DAGCombiner::visitSDIVREM(SDNode *N) {
1586 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::SDIV, ISD::SREM);
Gabor Greifba36cb52008-08-28 21:40:38 +00001587 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001588
Dan Gohman475871a2008-07-27 21:46:04 +00001589 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001590}
1591
Dan Gohman475871a2008-07-27 21:46:04 +00001592SDValue DAGCombiner::visitUDIVREM(SDNode *N) {
1593 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::UDIV, ISD::UREM);
Gabor Greifba36cb52008-08-28 21:40:38 +00001594 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001595
Dan Gohman475871a2008-07-27 21:46:04 +00001596 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001597}
1598
Chris Lattner35e5c142006-05-05 05:51:50 +00001599/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
1600/// two operands of the same opcode, try to simplify it.
Dan Gohman475871a2008-07-27 21:46:04 +00001601SDValue DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
1602 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001603 MVT VT = N0.getValueType();
Chris Lattner35e5c142006-05-05 05:51:50 +00001604 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
1605
Chris Lattner540121f2006-05-05 06:31:05 +00001606 // For each of OP in AND/OR/XOR:
1607 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
1608 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
1609 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
Chris Lattner0d8dae72006-05-05 06:32:04 +00001610 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y))
Chris Lattner540121f2006-05-05 06:31:05 +00001611 if ((N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND||
Chris Lattner0d8dae72006-05-05 06:32:04 +00001612 N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::TRUNCATE) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001613 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001614 SDValue ORNode = DAG.getNode(N->getOpcode(),
Chris Lattner35e5c142006-05-05 05:51:50 +00001615 N0.getOperand(0).getValueType(),
1616 N0.getOperand(0), N1.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00001617 AddToWorkList(ORNode.getNode());
Chris Lattner540121f2006-05-05 06:31:05 +00001618 return DAG.getNode(N0.getOpcode(), VT, ORNode);
Chris Lattner35e5c142006-05-05 05:51:50 +00001619 }
1620
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001621 // For each of OP in SHL/SRL/SRA/AND...
1622 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
1623 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
1624 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
Chris Lattner35e5c142006-05-05 05:51:50 +00001625 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001626 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001627 N0.getOperand(1) == N1.getOperand(1)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001628 SDValue ORNode = DAG.getNode(N->getOpcode(),
Chris Lattner35e5c142006-05-05 05:51:50 +00001629 N0.getOperand(0).getValueType(),
1630 N0.getOperand(0), N1.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00001631 AddToWorkList(ORNode.getNode());
Chris Lattner35e5c142006-05-05 05:51:50 +00001632 return DAG.getNode(N0.getOpcode(), VT, ORNode, N0.getOperand(1));
1633 }
1634
Dan Gohman475871a2008-07-27 21:46:04 +00001635 return SDValue();
Chris Lattner35e5c142006-05-05 05:51:50 +00001636}
1637
Dan Gohman475871a2008-07-27 21:46:04 +00001638SDValue DAGCombiner::visitAND(SDNode *N) {
1639 SDValue N0 = N->getOperand(0);
1640 SDValue N1 = N->getOperand(1);
1641 SDValue LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001642 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1643 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001644 MVT VT = N1.getValueType();
1645 unsigned BitWidth = VT.getSizeInBits();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001646
Dan Gohman7f321562007-06-25 16:23:39 +00001647 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001648 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001649 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001650 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001651 }
Dan Gohman7f321562007-06-25 16:23:39 +00001652
Dan Gohman613e0d82007-07-03 14:03:57 +00001653 // fold (and x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001654 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001655 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001656 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001657 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001658 return DAG.FoldConstantArithmetic(ISD::AND, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00001659 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001660 if (N0C && !N1C)
1661 return DAG.getNode(ISD::AND, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001662 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001663 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001664 return N0;
1665 // if (and x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00001666 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001667 APInt::getAllOnesValue(BitWidth)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001668 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +00001669 // reassociate and
Dan Gohman475871a2008-07-27 21:46:04 +00001670 SDValue RAND = ReassociateOps(ISD::AND, N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001671 if (RAND.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001672 return RAND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001673 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
Nate Begeman5dc7e862005-11-02 18:42:59 +00001674 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001675 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohman002e5d02008-03-13 22:13:53 +00001676 if ((ORI->getAPIntValue() & N1C->getAPIntValue()) == N1C->getAPIntValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001677 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +00001678 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
1679 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Dan Gohman475871a2008-07-27 21:46:04 +00001680 SDValue N0Op0 = N0.getOperand(0);
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001681 APInt Mask = ~N1C->getAPIntValue();
1682 Mask.trunc(N0Op0.getValueSizeInBits());
1683 if (DAG.MaskedValueIsZero(N0Op0, Mask)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001684 SDValue Zext = DAG.getNode(ISD::ZERO_EXTEND, N0.getValueType(),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001685 N0Op0);
Chris Lattner1ec05d12006-03-01 21:47:21 +00001686
1687 // Replace uses of the AND with uses of the Zero extend node.
1688 CombineTo(N, Zext);
1689
Chris Lattner3603cd62006-02-02 07:17:31 +00001690 // We actually want to replace all uses of the any_extend with the
1691 // zero_extend, to avoid duplicating things. This will later cause this
1692 // AND to be folded.
Gabor Greifba36cb52008-08-28 21:40:38 +00001693 CombineTo(N0.getNode(), Zext);
Dan Gohman475871a2008-07-27 21:46:04 +00001694 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner3603cd62006-02-02 07:17:31 +00001695 }
1696 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001697 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
1698 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1699 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1700 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1701
1702 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00001703 LL.getValueType().isInteger()) {
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001704 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
Dan Gohman002e5d02008-03-13 22:13:53 +00001705 if (cast<ConstantSDNode>(LR)->isNullValue() && Op1 == ISD::SETEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00001706 SDValue ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001707 AddToWorkList(ORNode.getNode());
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001708 return DAG.getSetCC(VT, ORNode, LR, Op1);
1709 }
1710 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
1711 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00001712 SDValue ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001713 AddToWorkList(ANDNode.getNode());
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001714 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1715 }
1716 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
1717 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
Dan Gohman475871a2008-07-27 21:46:04 +00001718 SDValue ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001719 AddToWorkList(ORNode.getNode());
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001720 return DAG.getSetCC(VT, ORNode, LR, Op1);
1721 }
1722 }
1723 // canonicalize equivalent to ll == rl
1724 if (LL == RR && LR == RL) {
1725 Op1 = ISD::getSetCCSwappedOperands(Op1);
1726 std::swap(RL, RR);
1727 }
1728 if (LL == RL && LR == RR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001729 bool isInteger = LL.getValueType().isInteger();
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001730 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
Chris Lattner6e1c6232008-10-28 07:11:07 +00001731 if (Result != ISD::SETCC_INVALID &&
1732 (!AfterLegalize || TLI.isCondCodeLegal(Result, LL.getValueType())))
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001733 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1734 }
1735 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001736
1737 // Simplify: and (op x...), (op y...) -> (op (and x, y))
1738 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001739 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001740 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001741 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001742
Nate Begemande996292006-02-03 22:24:05 +00001743 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
1744 // fold (and (sra)) -> (and (srl)) when possible.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001745 if (!VT.isVector() &&
Dan Gohman475871a2008-07-27 21:46:04 +00001746 SimplifyDemandedBits(SDValue(N, 0)))
1747 return SDValue(N, 0);
Nate Begemanded49632005-10-13 03:11:28 +00001748 // fold (zext_inreg (extload x)) -> (zextload x)
Gabor Greifba36cb52008-08-28 21:40:38 +00001749 if (ISD::isEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode())) {
Evan Cheng466685d2006-10-09 20:57:25 +00001750 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001751 MVT EVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001752 // If we zero all the possible extended bits, then we can turn this into
1753 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001754 unsigned BitWidth = N1.getValueSizeInBits();
1755 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001756 BitWidth - EVT.getSizeInBits())) &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001757 ((!AfterLegalize && !LN0->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00001758 TLI.isLoadExtLegal(ISD::ZEXTLOAD, EVT))) {
Dan Gohman475871a2008-07-27 21:46:04 +00001759 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
Evan Cheng466685d2006-10-09 20:57:25 +00001760 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00001761 LN0->getSrcValueOffset(), EVT,
1762 LN0->isVolatile(),
1763 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00001764 AddToWorkList(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001765 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00001766 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001767 }
1768 }
1769 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Gabor Greifba36cb52008-08-28 21:40:38 +00001770 if (ISD::isSEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng83060c52007-03-07 08:07:03 +00001771 N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001772 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001773 MVT EVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001774 // If we zero all the possible extended bits, then we can turn this into
1775 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001776 unsigned BitWidth = N1.getValueSizeInBits();
1777 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001778 BitWidth - EVT.getSizeInBits())) &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001779 ((!AfterLegalize && !LN0->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00001780 TLI.isLoadExtLegal(ISD::ZEXTLOAD, EVT))) {
Dan Gohman475871a2008-07-27 21:46:04 +00001781 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
Evan Cheng466685d2006-10-09 20:57:25 +00001782 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00001783 LN0->getSrcValueOffset(), EVT,
1784 LN0->isVolatile(),
1785 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00001786 AddToWorkList(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001787 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00001788 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001789 }
1790 }
Chris Lattner15045b62006-02-28 06:35:35 +00001791
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001792 // fold (and (load x), 255) -> (zextload x, i8)
1793 // fold (and (extload x, i16), 255) -> (zextload x, i8)
Evan Cheng466685d2006-10-09 20:57:25 +00001794 if (N1C && N0.getOpcode() == ISD::LOAD) {
1795 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
1796 if (LN0->getExtensionType() != ISD::SEXTLOAD &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001797 LN0->isUnindexed() && N0.hasOneUse() &&
1798 // Do not change the width of a volatile load.
1799 !LN0->isVolatile()) {
Duncan Sands8eab8a22008-06-09 11:32:28 +00001800 MVT EVT = MVT::Other;
1801 uint32_t ActiveBits = N1C->getAPIntValue().getActiveBits();
1802 if (ActiveBits > 0 && APIntOps::isMask(ActiveBits, N1C->getAPIntValue()))
1803 EVT = MVT::getIntegerVT(ActiveBits);
1804
1805 MVT LoadedVT = LN0->getMemoryVT();
Duncan Sandsad205a72008-06-16 08:14:38 +00001806 // Do not generate loads of non-round integer types since these can
1807 // be expensive (and would be wrong if the type is not byte sized).
1808 if (EVT != MVT::Other && LoadedVT.bitsGT(EVT) && EVT.isRound() &&
Evan Cheng03294662008-10-14 21:26:46 +00001809 (!AfterLegalize || TLI.isLoadExtLegal(ISD::ZEXTLOAD, EVT))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001810 MVT PtrType = N0.getOperand(1).getValueType();
Evan Cheng466685d2006-10-09 20:57:25 +00001811 // For big endian targets, we need to add an offset to the pointer to
1812 // load the correct bytes. For little endian systems, we merely need to
1813 // read fewer bytes from the same pointer.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001814 unsigned LVTStoreBytes = LoadedVT.getStoreSizeInBits()/8;
1815 unsigned EVTStoreBytes = EVT.getStoreSizeInBits()/8;
Duncan Sandsc6fa1702007-11-09 08:57:19 +00001816 unsigned PtrOff = LVTStoreBytes - EVTStoreBytes;
Duncan Sandsdc846502007-10-28 12:59:45 +00001817 unsigned Alignment = LN0->getAlignment();
Dan Gohman475871a2008-07-27 21:46:04 +00001818 SDValue NewPtr = LN0->getBasePtr();
Duncan Sands0753fc12008-02-11 10:37:04 +00001819 if (TLI.isBigEndian()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001820 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
1821 DAG.getConstant(PtrOff, PtrType));
Duncan Sandsdc846502007-10-28 12:59:45 +00001822 Alignment = MinAlign(Alignment, PtrOff);
1823 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001824 AddToWorkList(NewPtr.getNode());
Dan Gohman475871a2008-07-27 21:46:04 +00001825 SDValue Load =
Evan Cheng466685d2006-10-09 20:57:25 +00001826 DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00001827 LN0->getSrcValue(), LN0->getSrcValueOffset(), EVT,
Duncan Sandsdc846502007-10-28 12:59:45 +00001828 LN0->isVolatile(), Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00001829 AddToWorkList(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001830 CombineTo(N0.getNode(), Load, Load.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00001831 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng466685d2006-10-09 20:57:25 +00001832 }
Chris Lattner15045b62006-02-28 06:35:35 +00001833 }
1834 }
1835
Dan Gohman475871a2008-07-27 21:46:04 +00001836 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001837}
1838
Dan Gohman475871a2008-07-27 21:46:04 +00001839SDValue DAGCombiner::visitOR(SDNode *N) {
1840 SDValue N0 = N->getOperand(0);
1841 SDValue N1 = N->getOperand(1);
1842 SDValue LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001843 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1844 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001845 MVT VT = N1.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001846
Dan Gohman7f321562007-06-25 16:23:39 +00001847 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001848 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001849 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001850 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001851 }
Dan Gohman7f321562007-06-25 16:23:39 +00001852
Dan Gohman613e0d82007-07-03 14:03:57 +00001853 // fold (or x, undef) -> -1
Dan Gohmand595b5f2007-07-10 14:20:37 +00001854 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Chris Lattnere094f542007-07-09 16:16:34 +00001855 return DAG.getConstant(~0ULL, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001856 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001857 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001858 return DAG.FoldConstantArithmetic(ISD::OR, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00001859 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001860 if (N0C && !N1C)
1861 return DAG.getNode(ISD::OR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001862 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001863 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001864 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001865 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001866 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001867 return N1;
1868 // fold (or x, c) -> c iff (x & ~c) == 0
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001869 if (N1C && DAG.MaskedValueIsZero(N0, ~N1C->getAPIntValue()))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001870 return N1;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001871 // reassociate or
Dan Gohman475871a2008-07-27 21:46:04 +00001872 SDValue ROR = ReassociateOps(ISD::OR, N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001873 if (ROR.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001874 return ROR;
1875 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
Gabor Greifba36cb52008-08-28 21:40:38 +00001876 if (N1C && N0.getOpcode() == ISD::AND && N0.getNode()->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00001877 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00001878 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
1879 return DAG.getNode(ISD::AND, VT, DAG.getNode(ISD::OR, VT, N0.getOperand(0),
1880 N1),
Dan Gohman002e5d02008-03-13 22:13:53 +00001881 DAG.getConstant(N1C->getAPIntValue() |
1882 C1->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00001883 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001884 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
1885 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1886 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1887 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1888
1889 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00001890 LL.getValueType().isInteger()) {
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001891 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
1892 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
Dan Gohman002e5d02008-03-13 22:13:53 +00001893 if (cast<ConstantSDNode>(LR)->isNullValue() &&
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001894 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001895 SDValue ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001896 AddToWorkList(ORNode.getNode());
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001897 return DAG.getSetCC(VT, ORNode, LR, Op1);
1898 }
1899 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
1900 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
1901 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
1902 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001903 SDValue ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001904 AddToWorkList(ANDNode.getNode());
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001905 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1906 }
1907 }
1908 // canonicalize equivalent to ll == rl
1909 if (LL == RR && LR == RL) {
1910 Op1 = ISD::getSetCCSwappedOperands(Op1);
1911 std::swap(RL, RR);
1912 }
1913 if (LL == RL && LR == RR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001914 bool isInteger = LL.getValueType().isInteger();
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001915 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
Chris Lattner6e1c6232008-10-28 07:11:07 +00001916 if (Result != ISD::SETCC_INVALID &&
1917 (!AfterLegalize || TLI.isCondCodeLegal(Result, LL.getValueType())))
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001918 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1919 }
1920 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001921
1922 // Simplify: or (op x...), (op y...) -> (op (or x, y))
1923 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001924 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001925 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001926 }
Chris Lattner516b9622006-09-14 20:50:57 +00001927
Chris Lattner1ec72732006-09-14 21:11:37 +00001928 // (X & C1) | (Y & C2) -> (X|Y) & C3 if possible.
1929 if (N0.getOpcode() == ISD::AND &&
1930 N1.getOpcode() == ISD::AND &&
1931 N0.getOperand(1).getOpcode() == ISD::Constant &&
1932 N1.getOperand(1).getOpcode() == ISD::Constant &&
1933 // Don't increase # computations.
Gabor Greifba36cb52008-08-28 21:40:38 +00001934 (N0.getNode()->hasOneUse() || N1.getNode()->hasOneUse())) {
Chris Lattner1ec72732006-09-14 21:11:37 +00001935 // We can only do this xform if we know that bits from X that are set in C2
1936 // but not in C1 are already zero. Likewise for Y.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001937 const APInt &LHSMask =
1938 cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
1939 const APInt &RHSMask =
1940 cast<ConstantSDNode>(N1.getOperand(1))->getAPIntValue();
Chris Lattner1ec72732006-09-14 21:11:37 +00001941
Dan Gohmanea859be2007-06-22 14:59:07 +00001942 if (DAG.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) &&
1943 DAG.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001944 SDValue X =DAG.getNode(ISD::OR, VT, N0.getOperand(0), N1.getOperand(0));
Chris Lattner1ec72732006-09-14 21:11:37 +00001945 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(LHSMask|RHSMask, VT));
1946 }
1947 }
1948
1949
Chris Lattner516b9622006-09-14 20:50:57 +00001950 // See if this is some rotate idiom.
1951 if (SDNode *Rot = MatchRotate(N0, N1))
Dan Gohman475871a2008-07-27 21:46:04 +00001952 return SDValue(Rot, 0);
Chris Lattner35e5c142006-05-05 05:51:50 +00001953
Dan Gohman475871a2008-07-27 21:46:04 +00001954 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001955}
1956
Chris Lattner516b9622006-09-14 20:50:57 +00001957
1958/// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present.
Dan Gohman475871a2008-07-27 21:46:04 +00001959static bool MatchRotateHalf(SDValue Op, SDValue &Shift, SDValue &Mask) {
Chris Lattner516b9622006-09-14 20:50:57 +00001960 if (Op.getOpcode() == ISD::AND) {
Reid Spencer3ed469c2006-11-02 20:25:50 +00001961 if (isa<ConstantSDNode>(Op.getOperand(1))) {
Chris Lattner516b9622006-09-14 20:50:57 +00001962 Mask = Op.getOperand(1);
1963 Op = Op.getOperand(0);
1964 } else {
1965 return false;
1966 }
1967 }
1968
1969 if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) {
1970 Shift = Op;
1971 return true;
1972 }
1973 return false;
1974}
1975
1976
1977// MatchRotate - Handle an 'or' of two operands. If this is one of the many
1978// idioms for rotate, and if the target supports rotation instructions, generate
1979// a rot[lr].
Dan Gohman475871a2008-07-27 21:46:04 +00001980SDNode *DAGCombiner::MatchRotate(SDValue LHS, SDValue RHS) {
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001981 // Must be a legal type. Expanded 'n promoted things won't work with rotates.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001982 MVT VT = LHS.getValueType();
Chris Lattner516b9622006-09-14 20:50:57 +00001983 if (!TLI.isTypeLegal(VT)) return 0;
1984
1985 // The target must have at least one rotate flavor.
1986 bool HasROTL = TLI.isOperationLegal(ISD::ROTL, VT);
1987 bool HasROTR = TLI.isOperationLegal(ISD::ROTR, VT);
1988 if (!HasROTL && !HasROTR) return 0;
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001989
Chris Lattner516b9622006-09-14 20:50:57 +00001990 // Match "(X shl/srl V1) & V2" where V2 may not be present.
Dan Gohman475871a2008-07-27 21:46:04 +00001991 SDValue LHSShift; // The shift.
1992 SDValue LHSMask; // AND value if any.
Chris Lattner516b9622006-09-14 20:50:57 +00001993 if (!MatchRotateHalf(LHS, LHSShift, LHSMask))
1994 return 0; // Not part of a rotate.
1995
Dan Gohman475871a2008-07-27 21:46:04 +00001996 SDValue RHSShift; // The shift.
1997 SDValue RHSMask; // AND value if any.
Chris Lattner516b9622006-09-14 20:50:57 +00001998 if (!MatchRotateHalf(RHS, RHSShift, RHSMask))
1999 return 0; // Not part of a rotate.
2000
2001 if (LHSShift.getOperand(0) != RHSShift.getOperand(0))
2002 return 0; // Not shifting the same value.
2003
2004 if (LHSShift.getOpcode() == RHSShift.getOpcode())
2005 return 0; // Shifts must disagree.
2006
2007 // Canonicalize shl to left side in a shl/srl pair.
2008 if (RHSShift.getOpcode() == ISD::SHL) {
2009 std::swap(LHS, RHS);
2010 std::swap(LHSShift, RHSShift);
2011 std::swap(LHSMask , RHSMask );
2012 }
2013
Duncan Sands83ec4b62008-06-06 12:08:01 +00002014 unsigned OpSizeInBits = VT.getSizeInBits();
Dan Gohman475871a2008-07-27 21:46:04 +00002015 SDValue LHSShiftArg = LHSShift.getOperand(0);
2016 SDValue LHSShiftAmt = LHSShift.getOperand(1);
2017 SDValue RHSShiftAmt = RHSShift.getOperand(1);
Chris Lattner516b9622006-09-14 20:50:57 +00002018
2019 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
2020 // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2)
Scott Michelc9dc1142007-04-02 21:36:32 +00002021 if (LHSShiftAmt.getOpcode() == ISD::Constant &&
2022 RHSShiftAmt.getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002023 uint64_t LShVal = cast<ConstantSDNode>(LHSShiftAmt)->getZExtValue();
2024 uint64_t RShVal = cast<ConstantSDNode>(RHSShiftAmt)->getZExtValue();
Chris Lattner516b9622006-09-14 20:50:57 +00002025 if ((LShVal + RShVal) != OpSizeInBits)
2026 return 0;
2027
Dan Gohman475871a2008-07-27 21:46:04 +00002028 SDValue Rot;
Chris Lattner516b9622006-09-14 20:50:57 +00002029 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00002030 Rot = DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00002031 else
Scott Michelc9dc1142007-04-02 21:36:32 +00002032 Rot = DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00002033
2034 // If there is an AND of either shifted operand, apply it to the result.
Gabor Greifba36cb52008-08-28 21:40:38 +00002035 if (LHSMask.getNode() || RHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00002036 APInt Mask = APInt::getAllOnesValue(OpSizeInBits);
Chris Lattner516b9622006-09-14 20:50:57 +00002037
Gabor Greifba36cb52008-08-28 21:40:38 +00002038 if (LHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00002039 APInt RHSBits = APInt::getLowBitsSet(OpSizeInBits, LShVal);
2040 Mask &= cast<ConstantSDNode>(LHSMask)->getAPIntValue() | RHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00002041 }
Gabor Greifba36cb52008-08-28 21:40:38 +00002042 if (RHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00002043 APInt LHSBits = APInt::getHighBitsSet(OpSizeInBits, RShVal);
2044 Mask &= cast<ConstantSDNode>(RHSMask)->getAPIntValue() | LHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00002045 }
2046
2047 Rot = DAG.getNode(ISD::AND, VT, Rot, DAG.getConstant(Mask, VT));
2048 }
2049
Gabor Greifba36cb52008-08-28 21:40:38 +00002050 return Rot.getNode();
Chris Lattner516b9622006-09-14 20:50:57 +00002051 }
2052
2053 // If there is a mask here, and we have a variable shift, we can't be sure
2054 // that we're masking out the right stuff.
Gabor Greifba36cb52008-08-28 21:40:38 +00002055 if (LHSMask.getNode() || RHSMask.getNode())
Chris Lattner516b9622006-09-14 20:50:57 +00002056 return 0;
2057
2058 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
2059 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotr x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00002060 if (RHSShiftAmt.getOpcode() == ISD::SUB &&
2061 LHSShiftAmt == RHSShiftAmt.getOperand(1)) {
Chris Lattner516b9622006-09-14 20:50:57 +00002062 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00002063 dyn_cast<ConstantSDNode>(RHSShiftAmt.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002064 if (SUBC->getAPIntValue() == OpSizeInBits) {
Chris Lattner516b9622006-09-14 20:50:57 +00002065 if (HasROTL)
Gabor Greifba36cb52008-08-28 21:40:38 +00002066 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).getNode();
Chris Lattner516b9622006-09-14 20:50:57 +00002067 else
Gabor Greifba36cb52008-08-28 21:40:38 +00002068 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).getNode();
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002069 }
Chris Lattner516b9622006-09-14 20:50:57 +00002070 }
2071 }
2072
2073 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
2074 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotl x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00002075 if (LHSShiftAmt.getOpcode() == ISD::SUB &&
2076 RHSShiftAmt == LHSShiftAmt.getOperand(1)) {
Chris Lattner516b9622006-09-14 20:50:57 +00002077 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00002078 dyn_cast<ConstantSDNode>(LHSShiftAmt.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002079 if (SUBC->getAPIntValue() == OpSizeInBits) {
Bill Wendling2692d592008-08-31 01:13:31 +00002080 if (HasROTR)
Gabor Greifba36cb52008-08-28 21:40:38 +00002081 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).getNode();
Bill Wendling2692d592008-08-31 01:13:31 +00002082 else
2083 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).getNode();
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002084 }
Scott Michelc9dc1142007-04-02 21:36:32 +00002085 }
2086 }
2087
Dan Gohman74feef22008-10-17 01:23:35 +00002088 // Look for sign/zext/any-extended or truncate cases:
Scott Michelc9dc1142007-04-02 21:36:32 +00002089 if ((LHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
2090 || LHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
Dan Gohman74feef22008-10-17 01:23:35 +00002091 || LHSShiftAmt.getOpcode() == ISD::ANY_EXTEND
2092 || LHSShiftAmt.getOpcode() == ISD::TRUNCATE) &&
Scott Michelc9dc1142007-04-02 21:36:32 +00002093 (RHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
2094 || RHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
Dan Gohman74feef22008-10-17 01:23:35 +00002095 || RHSShiftAmt.getOpcode() == ISD::ANY_EXTEND
2096 || RHSShiftAmt.getOpcode() == ISD::TRUNCATE)) {
Dan Gohman475871a2008-07-27 21:46:04 +00002097 SDValue LExtOp0 = LHSShiftAmt.getOperand(0);
2098 SDValue RExtOp0 = RHSShiftAmt.getOperand(0);
Scott Michelc9dc1142007-04-02 21:36:32 +00002099 if (RExtOp0.getOpcode() == ISD::SUB &&
2100 RExtOp0.getOperand(1) == LExtOp0) {
2101 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00002102 // (rotl x, y)
Scott Michelc9dc1142007-04-02 21:36:32 +00002103 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00002104 // (rotr x, (sub 32, y))
Dan Gohman74feef22008-10-17 01:23:35 +00002105 if (ConstantSDNode *SUBC =
2106 dyn_cast<ConstantSDNode>(RExtOp0.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002107 if (SUBC->getAPIntValue() == OpSizeInBits) {
Gabor Greif12632d22008-08-30 19:29:20 +00002108 return DAG.getNode(HasROTL ? ISD::ROTL : ISD::ROTR, VT, LHSShiftArg,
2109 HasROTL ? LHSShiftAmt : RHSShiftAmt).getNode();
Scott Michelc9dc1142007-04-02 21:36:32 +00002110 }
2111 }
2112 } else if (LExtOp0.getOpcode() == ISD::SUB &&
2113 RExtOp0 == LExtOp0.getOperand(1)) {
Bill Wendling353dea22008-08-31 01:04:56 +00002114 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext y))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00002115 // (rotr x, y)
Bill Wendling353dea22008-08-31 01:04:56 +00002116 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext y))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00002117 // (rotl x, (sub 32, y))
Dan Gohman74feef22008-10-17 01:23:35 +00002118 if (ConstantSDNode *SUBC =
2119 dyn_cast<ConstantSDNode>(LExtOp0.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002120 if (SUBC->getAPIntValue() == OpSizeInBits) {
Bill Wendling353dea22008-08-31 01:04:56 +00002121 return DAG.getNode(HasROTR ? ISD::ROTR : ISD::ROTL, VT, LHSShiftArg,
2122 HasROTR ? RHSShiftAmt : LHSShiftAmt).getNode();
Scott Michelc9dc1142007-04-02 21:36:32 +00002123 }
2124 }
Chris Lattner516b9622006-09-14 20:50:57 +00002125 }
2126 }
2127
2128 return 0;
2129}
2130
2131
Dan Gohman475871a2008-07-27 21:46:04 +00002132SDValue DAGCombiner::visitXOR(SDNode *N) {
2133 SDValue N0 = N->getOperand(0);
2134 SDValue N1 = N->getOperand(1);
2135 SDValue LHS, RHS, CC;
Nate Begeman646d7e22005-09-02 21:18:40 +00002136 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2137 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002138 MVT VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002139
Dan Gohman7f321562007-06-25 16:23:39 +00002140 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00002141 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002142 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002143 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00002144 }
Dan Gohman7f321562007-06-25 16:23:39 +00002145
Evan Cheng26471c42008-03-25 20:08:07 +00002146 // fold (xor undef, undef) -> 0. This is a common idiom (misuse).
2147 if (N0.getOpcode() == ISD::UNDEF && N1.getOpcode() == ISD::UNDEF)
2148 return DAG.getConstant(0, VT);
Dan Gohman613e0d82007-07-03 14:03:57 +00002149 // fold (xor x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00002150 if (N0.getOpcode() == ISD::UNDEF)
2151 return N0;
2152 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00002153 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002154 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002155 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002156 return DAG.FoldConstantArithmetic(ISD::XOR, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00002157 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00002158 if (N0C && !N1C)
2159 return DAG.getNode(ISD::XOR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002160 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002161 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002162 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00002163 // reassociate xor
Dan Gohman475871a2008-07-27 21:46:04 +00002164 SDValue RXOR = ReassociateOps(ISD::XOR, N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00002165 if (RXOR.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00002166 return RXOR;
Bill Wendlingae89bb12008-11-11 08:25:46 +00002167
Nate Begeman1d4d4142005-09-01 00:19:25 +00002168 // fold !(x cc y) -> (x !cc y)
Dan Gohman002e5d02008-03-13 22:13:53 +00002169 if (N1C && N1C->getAPIntValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002170 bool isInt = LHS.getValueType().isInteger();
Nate Begeman646d7e22005-09-02 21:18:40 +00002171 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
2172 isInt);
Bill Wendlingae89bb12008-11-11 08:25:46 +00002173
2174 if (!AfterLegalize || TLI.isCondCodeLegal(NotCC, LHS.getValueType())) {
2175 switch (N0.getOpcode()) {
2176 default:
2177 assert(0 && "Unhandled SetCC Equivalent!");
2178 abort();
2179 case ISD::SETCC:
2180 return DAG.getSetCC(VT, LHS, RHS, NotCC);
2181 case ISD::SELECT_CC:
2182 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),
2183 N0.getOperand(3), NotCC);
2184 }
2185 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00002186 }
Bill Wendlingae89bb12008-11-11 08:25:46 +00002187
Chris Lattner61c5ff42007-09-10 21:39:07 +00002188 // fold (not (zext (setcc x, y))) -> (zext (not (setcc x, y)))
Dan Gohman002e5d02008-03-13 22:13:53 +00002189 if (N1C && N1C->getAPIntValue() == 1 && N0.getOpcode() == ISD::ZERO_EXTEND &&
Gabor Greif12632d22008-08-30 19:29:20 +00002190 N0.getNode()->hasOneUse() &&
2191 isSetCCEquivalent(N0.getOperand(0), LHS, RHS, CC)){
Dan Gohman475871a2008-07-27 21:46:04 +00002192 SDValue V = N0.getOperand(0);
Chris Lattner61c5ff42007-09-10 21:39:07 +00002193 V = DAG.getNode(ISD::XOR, V.getValueType(), V,
Duncan Sands272dce02007-10-10 09:54:50 +00002194 DAG.getConstant(1, V.getValueType()));
Gabor Greifba36cb52008-08-28 21:40:38 +00002195 AddToWorkList(V.getNode());
Chris Lattner61c5ff42007-09-10 21:39:07 +00002196 return DAG.getNode(ISD::ZERO_EXTEND, VT, V);
2197 }
2198
Nate Begeman99801192005-09-07 23:25:52 +00002199 // fold !(x or y) -> (!x and !y) iff x or y are setcc
Dan Gohman002e5d02008-03-13 22:13:53 +00002200 if (N1C && N1C->getAPIntValue() == 1 && VT == MVT::i1 &&
Nate Begeman99801192005-09-07 23:25:52 +00002201 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Dan Gohman475871a2008-07-27 21:46:04 +00002202 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00002203 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
2204 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002205 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
2206 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Gabor Greifba36cb52008-08-28 21:40:38 +00002207 AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode());
Nate Begeman99801192005-09-07 23:25:52 +00002208 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002209 }
2210 }
Nate Begeman99801192005-09-07 23:25:52 +00002211 // fold !(x or y) -> (!x and !y) iff x or y are constants
2212 if (N1C && N1C->isAllOnesValue() &&
2213 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Dan Gohman475871a2008-07-27 21:46:04 +00002214 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00002215 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
2216 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002217 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
2218 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Gabor Greifba36cb52008-08-28 21:40:38 +00002219 AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode());
Nate Begeman99801192005-09-07 23:25:52 +00002220 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002221 }
2222 }
Nate Begeman223df222005-09-08 20:18:10 +00002223 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
2224 if (N1C && N0.getOpcode() == ISD::XOR) {
2225 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
2226 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2227 if (N00C)
2228 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
Dan Gohman002e5d02008-03-13 22:13:53 +00002229 DAG.getConstant(N1C->getAPIntValue()^
2230 N00C->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00002231 if (N01C)
2232 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
Dan Gohman002e5d02008-03-13 22:13:53 +00002233 DAG.getConstant(N1C->getAPIntValue()^
2234 N01C->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00002235 }
2236 // fold (xor x, x) -> 0
Chris Lattner4fbdd592006-03-28 19:11:05 +00002237 if (N0 == N1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002238 if (!VT.isVector()) {
Chris Lattner4fbdd592006-03-28 19:11:05 +00002239 return DAG.getConstant(0, VT);
2240 } else if (!AfterLegalize || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) {
2241 // Produce a vector of zeros.
Dan Gohman475871a2008-07-27 21:46:04 +00002242 SDValue El = DAG.getConstant(0, VT.getVectorElementType());
2243 std::vector<SDValue> Ops(VT.getVectorNumElements(), El);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002244 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4fbdd592006-03-28 19:11:05 +00002245 }
2246 }
Chris Lattner35e5c142006-05-05 05:51:50 +00002247
2248 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
2249 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002250 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002251 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002252 }
Chris Lattner35e5c142006-05-05 05:51:50 +00002253
Chris Lattner3e104b12006-04-08 04:15:24 +00002254 // Simplify the expression using non-local knowledge.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002255 if (!VT.isVector() &&
Dan Gohman475871a2008-07-27 21:46:04 +00002256 SimplifyDemandedBits(SDValue(N, 0)))
2257 return SDValue(N, 0);
Chris Lattner3e104b12006-04-08 04:15:24 +00002258
Dan Gohman475871a2008-07-27 21:46:04 +00002259 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002260}
2261
Chris Lattnere70da202007-12-06 07:33:36 +00002262/// visitShiftByConstant - Handle transforms common to the three shifts, when
2263/// the shift amount is a constant.
Dan Gohman475871a2008-07-27 21:46:04 +00002264SDValue DAGCombiner::visitShiftByConstant(SDNode *N, unsigned Amt) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002265 SDNode *LHS = N->getOperand(0).getNode();
Dan Gohman475871a2008-07-27 21:46:04 +00002266 if (!LHS->hasOneUse()) return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00002267
2268 // We want to pull some binops through shifts, so that we have (and (shift))
2269 // instead of (shift (and)), likewise for add, or, xor, etc. This sort of
2270 // thing happens with address calculations, so it's important to canonicalize
2271 // it.
2272 bool HighBitSet = false; // Can we transform this if the high bit is set?
2273
2274 switch (LHS->getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002275 default: return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00002276 case ISD::OR:
2277 case ISD::XOR:
2278 HighBitSet = false; // We can only transform sra if the high bit is clear.
2279 break;
2280 case ISD::AND:
2281 HighBitSet = true; // We can only transform sra if the high bit is set.
2282 break;
2283 case ISD::ADD:
2284 if (N->getOpcode() != ISD::SHL)
Dan Gohman475871a2008-07-27 21:46:04 +00002285 return SDValue(); // only shl(add) not sr[al](add).
Chris Lattnere70da202007-12-06 07:33:36 +00002286 HighBitSet = false; // We can only transform sra if the high bit is clear.
2287 break;
2288 }
2289
2290 // We require the RHS of the binop to be a constant as well.
2291 ConstantSDNode *BinOpCst = dyn_cast<ConstantSDNode>(LHS->getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00002292 if (!BinOpCst) return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00002293
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00002294
2295 // FIXME: disable this for unless the input to the binop is a shift by a
2296 // constant. If it is not a shift, it pessimizes some common cases like:
2297 //
2298 //void foo(int *X, int i) { X[i & 1235] = 1; }
2299 //int bar(int *X, int i) { return X[i & 255]; }
Gabor Greifba36cb52008-08-28 21:40:38 +00002300 SDNode *BinOpLHSVal = LHS->getOperand(0).getNode();
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00002301 if ((BinOpLHSVal->getOpcode() != ISD::SHL &&
2302 BinOpLHSVal->getOpcode() != ISD::SRA &&
2303 BinOpLHSVal->getOpcode() != ISD::SRL) ||
2304 !isa<ConstantSDNode>(BinOpLHSVal->getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00002305 return SDValue();
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00002306
Duncan Sands83ec4b62008-06-06 12:08:01 +00002307 MVT VT = N->getValueType(0);
Chris Lattnere70da202007-12-06 07:33:36 +00002308
2309 // If this is a signed shift right, and the high bit is modified
2310 // by the logical operation, do not perform the transformation.
2311 // The highBitSet boolean indicates the value of the high bit of
2312 // the constant which would cause it to be modified for this
2313 // operation.
2314 if (N->getOpcode() == ISD::SRA) {
Dan Gohman220a8232008-03-03 23:51:38 +00002315 bool BinOpRHSSignSet = BinOpCst->getAPIntValue().isNegative();
2316 if (BinOpRHSSignSet != HighBitSet)
Dan Gohman475871a2008-07-27 21:46:04 +00002317 return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00002318 }
2319
2320 // Fold the constants, shifting the binop RHS by the shift amount.
Dan Gohman475871a2008-07-27 21:46:04 +00002321 SDValue NewRHS = DAG.getNode(N->getOpcode(), N->getValueType(0),
Chris Lattnere70da202007-12-06 07:33:36 +00002322 LHS->getOperand(1), N->getOperand(1));
2323
2324 // Create the new shift.
Dan Gohman475871a2008-07-27 21:46:04 +00002325 SDValue NewShift = DAG.getNode(N->getOpcode(), VT, LHS->getOperand(0),
Chris Lattnere70da202007-12-06 07:33:36 +00002326 N->getOperand(1));
2327
2328 // Create the new binop.
2329 return DAG.getNode(LHS->getOpcode(), VT, NewShift, NewRHS);
2330}
2331
2332
Dan Gohman475871a2008-07-27 21:46:04 +00002333SDValue DAGCombiner::visitSHL(SDNode *N) {
2334 SDValue N0 = N->getOperand(0);
2335 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002336 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2337 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002338 MVT VT = N0.getValueType();
2339 unsigned OpSizeInBits = VT.getSizeInBits();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002340
2341 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002342 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002343 return DAG.FoldConstantArithmetic(ISD::SHL, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002344 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002345 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002346 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002347 // fold (shl x, c >= size(x)) -> undef
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002348 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002349 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002350 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002351 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002352 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002353 // if (shl x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00002354 if (DAG.MaskedValueIsZero(SDValue(N, 0),
Duncan Sands83ec4b62008-06-06 12:08:01 +00002355 APInt::getAllOnesValue(VT.getSizeInBits())))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002356 return DAG.getConstant(0, VT);
Evan Chengeb9f8922008-08-30 02:03:58 +00002357 // fold (shl x, (trunc (and y, c))) -> (shl x, (and (trunc y), c))
2358 // iff (trunc c) == c
2359 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00002360 N1.getOperand(0).getOpcode() == ISD::AND &&
2361 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00002362 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00002363 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Evan Chengeb9f8922008-08-30 02:03:58 +00002364 MVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00002365 SDValue N100 = N1.getOperand(0).getOperand(0);
2366 return DAG.getNode(ISD::SHL, VT, N0,
2367 DAG.getNode(ISD::AND, TruncVT,
2368 DAG.getNode(ISD::TRUNCATE, TruncVT, N100),
2369 DAG.getConstant(N101C->getZExtValue(),
2370 TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00002371 }
2372 }
2373
Dan Gohman475871a2008-07-27 21:46:04 +00002374 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
2375 return SDValue(N, 0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002376 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002377 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002378 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002379 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
2380 uint64_t c2 = N1C->getZExtValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002381 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002382 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002383 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002384 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002385 }
2386 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
2387 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002388 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002389 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002390 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
2391 uint64_t c2 = N1C->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00002392 SDValue Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman1d4d4142005-09-01 00:19:25 +00002393 DAG.getConstant(~0ULL << c1, VT));
2394 if (c2 > c1)
2395 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +00002396 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002397 else
Nate Begeman83e75ec2005-09-06 04:43:02 +00002398 return DAG.getNode(ISD::SRL, VT, Mask,
2399 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002400 }
2401 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00002402 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +00002403 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002404 DAG.getConstant(~0ULL << N1C->getZExtValue(), VT));
Chris Lattnere70da202007-12-06 07:33:36 +00002405
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002406 return N1C ? visitShiftByConstant(N, N1C->getZExtValue()) : SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002407}
2408
Dan Gohman475871a2008-07-27 21:46:04 +00002409SDValue DAGCombiner::visitSRA(SDNode *N) {
2410 SDValue N0 = N->getOperand(0);
2411 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002412 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2413 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002414 MVT VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002415
2416 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002417 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002418 return DAG.FoldConstantArithmetic(ISD::SRA, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002419 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002420 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002421 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002422 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00002423 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002424 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002425 // fold (sra x, c >= size(x)) -> undef
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002426 if (N1C && N1C->getZExtValue() >= VT.getSizeInBits())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002427 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002428 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002429 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002430 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00002431 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
2432 // sext_inreg.
2433 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002434 unsigned LowBits = VT.getSizeInBits() - (unsigned)N1C->getZExtValue();
Duncan Sands8eab8a22008-06-09 11:32:28 +00002435 MVT EVT = MVT::getIntegerVT(LowBits);
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002436 if (EVT.isSimple() && // TODO: remove when apint codegen support lands.
2437 (!AfterLegalize || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, EVT)))
Nate Begemanfb7217b2006-02-17 19:54:08 +00002438 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
2439 DAG.getValueType(EVT));
2440 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002441
Chris Lattner71d9ebc2006-02-28 06:23:04 +00002442 // fold (sra (sra x, c1), c2) -> (sra x, c1+c2)
2443 if (N1C && N0.getOpcode() == ISD::SRA) {
2444 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002445 unsigned Sum = N1C->getZExtValue() + C1->getZExtValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002446 if (Sum >= VT.getSizeInBits()) Sum = VT.getSizeInBits()-1;
Chris Lattner71d9ebc2006-02-28 06:23:04 +00002447 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0),
2448 DAG.getConstant(Sum, N1C->getValueType(0)));
2449 }
2450 }
Christopher Lamb15cbde32008-03-19 08:30:06 +00002451
2452 // fold sra (shl X, m), result_size - n
2453 // -> (sign_extend (trunc (shl X, result_size - n - m))) for
Christopher Lambb9b04282008-03-20 04:31:39 +00002454 // result_size - n != m.
2455 // If truncate is free for the target sext(shl) is likely to result in better
2456 // code.
Christopher Lamb15cbde32008-03-19 08:30:06 +00002457 if (N0.getOpcode() == ISD::SHL) {
2458 // Get the two constanst of the shifts, CN0 = m, CN = n.
2459 const ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2460 if (N01C && N1C) {
Christopher Lambb9b04282008-03-20 04:31:39 +00002461 // Determine what the truncate's result bitsize and type would be.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002462 unsigned VTValSize = VT.getSizeInBits();
2463 MVT TruncVT =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002464 MVT::getIntegerVT(VTValSize - N1C->getZExtValue());
Christopher Lambb9b04282008-03-20 04:31:39 +00002465 // Determine the residual right-shift amount.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002466 unsigned ShiftAmt = N1C->getZExtValue() - N01C->getZExtValue();
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002467
Christopher Lambb9b04282008-03-20 04:31:39 +00002468 // If the shift is not a no-op (in which case this should be just a sign
2469 // extend already), the truncated to type is legal, sign_extend is legal
Gabor Greif12632d22008-08-30 19:29:20 +00002470 // on that type, and the the truncate to that type is both legal and free,
Christopher Lambb9b04282008-03-20 04:31:39 +00002471 // perform the transform.
2472 if (ShiftAmt &&
Christopher Lambb9b04282008-03-20 04:31:39 +00002473 TLI.isOperationLegal(ISD::SIGN_EXTEND, TruncVT) &&
2474 TLI.isOperationLegal(ISD::TRUNCATE, VT) &&
Evan Cheng260e07e2008-03-20 02:18:41 +00002475 TLI.isTruncateFree(VT, TruncVT)) {
Christopher Lambb9b04282008-03-20 04:31:39 +00002476
Dan Gohman475871a2008-07-27 21:46:04 +00002477 SDValue Amt = DAG.getConstant(ShiftAmt, TLI.getShiftAmountTy());
2478 SDValue Shift = DAG.getNode(ISD::SRL, VT, N0.getOperand(0), Amt);
2479 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, TruncVT, Shift);
Christopher Lambb9b04282008-03-20 04:31:39 +00002480 return DAG.getNode(ISD::SIGN_EXTEND, N->getValueType(0), Trunc);
Christopher Lamb15cbde32008-03-19 08:30:06 +00002481 }
2482 }
2483 }
Chris Lattner71d9ebc2006-02-28 06:23:04 +00002484
Evan Chengeb9f8922008-08-30 02:03:58 +00002485 // fold (sra x, (trunc (and y, c))) -> (sra x, (and (trunc y), c))
2486 // iff (trunc c) == c
2487 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00002488 N1.getOperand(0).getOpcode() == ISD::AND &&
2489 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00002490 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00002491 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Evan Chengeb9f8922008-08-30 02:03:58 +00002492 MVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00002493 SDValue N100 = N1.getOperand(0).getOperand(0);
2494 return DAG.getNode(ISD::SRA, VT, N0,
2495 DAG.getNode(ISD::AND, TruncVT,
2496 DAG.getNode(ISD::TRUNCATE, TruncVT, N100),
2497 DAG.getConstant(N101C->getZExtValue(),
2498 TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00002499 }
2500 }
2501
Chris Lattnera8504462006-05-08 20:51:54 +00002502 // Simplify, based on bits shifted out of the LHS.
Dan Gohman475871a2008-07-27 21:46:04 +00002503 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
2504 return SDValue(N, 0);
Chris Lattnera8504462006-05-08 20:51:54 +00002505
2506
Nate Begeman1d4d4142005-09-01 00:19:25 +00002507 // If the sign bit is known to be zero, switch this to a SRL.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002508 if (DAG.SignBitIsZero(N0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002509 return DAG.getNode(ISD::SRL, VT, N0, N1);
Chris Lattnere70da202007-12-06 07:33:36 +00002510
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002511 return N1C ? visitShiftByConstant(N, N1C->getZExtValue()) : SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002512}
2513
Dan Gohman475871a2008-07-27 21:46:04 +00002514SDValue DAGCombiner::visitSRL(SDNode *N) {
2515 SDValue N0 = N->getOperand(0);
2516 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002517 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2518 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002519 MVT VT = N0.getValueType();
2520 unsigned OpSizeInBits = VT.getSizeInBits();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002521
2522 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002523 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002524 return DAG.FoldConstantArithmetic(ISD::SRL, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002525 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002526 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002527 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002528 // fold (srl x, c >= size(x)) -> undef
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002529 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002530 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002531 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002532 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002533 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002534 // if (srl x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00002535 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002536 APInt::getAllOnesValue(OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002537 return DAG.getConstant(0, VT);
Chris Lattnerec06e9a2007-04-18 03:05:22 +00002538
Nate Begeman1d4d4142005-09-01 00:19:25 +00002539 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002540 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002541 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002542 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
2543 uint64_t c2 = N1C->getZExtValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002544 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002545 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002546 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002547 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002548 }
Chris Lattner350bec02006-04-02 06:11:11 +00002549
Chris Lattner06afe072006-05-05 22:53:17 +00002550 // fold (srl (anyextend x), c) -> (anyextend (srl x, c))
2551 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
2552 // Shifting in all undef bits?
Duncan Sands83ec4b62008-06-06 12:08:01 +00002553 MVT SmallVT = N0.getOperand(0).getValueType();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002554 if (N1C->getZExtValue() >= SmallVT.getSizeInBits())
Chris Lattner06afe072006-05-05 22:53:17 +00002555 return DAG.getNode(ISD::UNDEF, VT);
2556
Dan Gohman475871a2008-07-27 21:46:04 +00002557 SDValue SmallShift = DAG.getNode(ISD::SRL, SmallVT, N0.getOperand(0), N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00002558 AddToWorkList(SmallShift.getNode());
Chris Lattner06afe072006-05-05 22:53:17 +00002559 return DAG.getNode(ISD::ANY_EXTEND, VT, SmallShift);
2560 }
2561
Chris Lattner3657ffe2006-10-12 20:23:19 +00002562 // fold (srl (sra X, Y), 31) -> (srl X, 31). This srl only looks at the sign
2563 // bit, which is unmodified by sra.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002564 if (N1C && N1C->getZExtValue()+1 == VT.getSizeInBits()) {
Chris Lattner3657ffe2006-10-12 20:23:19 +00002565 if (N0.getOpcode() == ISD::SRA)
2566 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0), N1);
2567 }
2568
Chris Lattner350bec02006-04-02 06:11:11 +00002569 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
2570 if (N1C && N0.getOpcode() == ISD::CTLZ &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002571 N1C->getAPIntValue() == Log2_32(VT.getSizeInBits())) {
Dan Gohman948d8ea2008-02-20 16:33:30 +00002572 APInt KnownZero, KnownOne;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002573 APInt Mask = APInt::getAllOnesValue(VT.getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00002574 DAG.ComputeMaskedBits(N0.getOperand(0), Mask, KnownZero, KnownOne);
Chris Lattner350bec02006-04-02 06:11:11 +00002575
2576 // If any of the input bits are KnownOne, then the input couldn't be all
2577 // zeros, thus the result of the srl will always be zero.
Dan Gohman948d8ea2008-02-20 16:33:30 +00002578 if (KnownOne.getBoolValue()) return DAG.getConstant(0, VT);
Chris Lattner350bec02006-04-02 06:11:11 +00002579
2580 // If all of the bits input the to ctlz node are known to be zero, then
2581 // the result of the ctlz is "32" and the result of the shift is one.
Dan Gohman948d8ea2008-02-20 16:33:30 +00002582 APInt UnknownBits = ~KnownZero & Mask;
Chris Lattner350bec02006-04-02 06:11:11 +00002583 if (UnknownBits == 0) return DAG.getConstant(1, VT);
2584
2585 // Otherwise, check to see if there is exactly one bit input to the ctlz.
2586 if ((UnknownBits & (UnknownBits-1)) == 0) {
2587 // Okay, we know that only that the single bit specified by UnknownBits
2588 // could be set on input to the CTLZ node. If this bit is set, the SRL
2589 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
2590 // to an SRL,XOR pair, which is likely to simplify more.
Dan Gohman948d8ea2008-02-20 16:33:30 +00002591 unsigned ShAmt = UnknownBits.countTrailingZeros();
Dan Gohman475871a2008-07-27 21:46:04 +00002592 SDValue Op = N0.getOperand(0);
Chris Lattner350bec02006-04-02 06:11:11 +00002593 if (ShAmt) {
2594 Op = DAG.getNode(ISD::SRL, VT, Op,
2595 DAG.getConstant(ShAmt, TLI.getShiftAmountTy()));
Gabor Greifba36cb52008-08-28 21:40:38 +00002596 AddToWorkList(Op.getNode());
Chris Lattner350bec02006-04-02 06:11:11 +00002597 }
2598 return DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(1, VT));
2599 }
2600 }
Evan Chengeb9f8922008-08-30 02:03:58 +00002601
2602 // fold (srl x, (trunc (and y, c))) -> (srl x, (and (trunc y), c))
2603 // iff (trunc c) == c
2604 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00002605 N1.getOperand(0).getOpcode() == ISD::AND &&
2606 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00002607 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00002608 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Evan Chengeb9f8922008-08-30 02:03:58 +00002609 MVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00002610 SDValue N100 = N1.getOperand(0).getOperand(0);
2611 return DAG.getNode(ISD::SRL, VT, N0,
2612 DAG.getNode(ISD::AND, TruncVT,
2613 DAG.getNode(ISD::TRUNCATE, TruncVT, N100),
2614 DAG.getConstant(N101C->getZExtValue(),
2615 TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00002616 }
2617 }
Chris Lattner61a4c072007-04-18 03:06:49 +00002618
2619 // fold operands of srl based on knowledge that the low bits are not
2620 // demanded.
Dan Gohman475871a2008-07-27 21:46:04 +00002621 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
2622 return SDValue(N, 0);
Chris Lattner61a4c072007-04-18 03:06:49 +00002623
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002624 return N1C ? visitShiftByConstant(N, N1C->getZExtValue()) : SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002625}
2626
Dan Gohman475871a2008-07-27 21:46:04 +00002627SDValue DAGCombiner::visitCTLZ(SDNode *N) {
2628 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002629 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002630
2631 // fold (ctlz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002632 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002633 return DAG.getNode(ISD::CTLZ, VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00002634 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002635}
2636
Dan Gohman475871a2008-07-27 21:46:04 +00002637SDValue DAGCombiner::visitCTTZ(SDNode *N) {
2638 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002639 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002640
2641 // fold (cttz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002642 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002643 return DAG.getNode(ISD::CTTZ, VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00002644 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002645}
2646
Dan Gohman475871a2008-07-27 21:46:04 +00002647SDValue DAGCombiner::visitCTPOP(SDNode *N) {
2648 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002649 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002650
2651 // fold (ctpop c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002652 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002653 return DAG.getNode(ISD::CTPOP, VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00002654 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002655}
2656
Dan Gohman475871a2008-07-27 21:46:04 +00002657SDValue DAGCombiner::visitSELECT(SDNode *N) {
2658 SDValue N0 = N->getOperand(0);
2659 SDValue N1 = N->getOperand(1);
2660 SDValue N2 = N->getOperand(2);
Nate Begeman452d7be2005-09-16 00:54:12 +00002661 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2662 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2663 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002664 MVT VT = N->getValueType(0);
2665 MVT VT0 = N0.getValueType();
Nate Begeman44728a72005-09-19 22:34:01 +00002666
Nate Begeman452d7be2005-09-16 00:54:12 +00002667 // fold select C, X, X -> X
2668 if (N1 == N2)
2669 return N1;
2670 // fold select true, X, Y -> X
2671 if (N0C && !N0C->isNullValue())
2672 return N1;
2673 // fold select false, X, Y -> Y
2674 if (N0C && N0C->isNullValue())
2675 return N2;
2676 // fold select C, 1, X -> C | X
Duncan Sands83ec4b62008-06-06 12:08:01 +00002677 if (VT == MVT::i1 && N1C && N1C->getAPIntValue() == 1)
Nate Begeman452d7be2005-09-16 00:54:12 +00002678 return DAG.getNode(ISD::OR, VT, N0, N2);
Evan Cheng571c4782007-08-18 05:57:05 +00002679 // fold select C, 0, 1 -> ~C
Duncan Sands83ec4b62008-06-06 12:08:01 +00002680 if (VT.isInteger() && VT0.isInteger() &&
Dan Gohman002e5d02008-03-13 22:13:53 +00002681 N1C && N2C && N1C->isNullValue() && N2C->getAPIntValue() == 1) {
Dan Gohman475871a2008-07-27 21:46:04 +00002682 SDValue XORNode = DAG.getNode(ISD::XOR, VT0, N0, DAG.getConstant(1, VT0));
Evan Cheng571c4782007-08-18 05:57:05 +00002683 if (VT == VT0)
2684 return XORNode;
Gabor Greifba36cb52008-08-28 21:40:38 +00002685 AddToWorkList(XORNode.getNode());
Duncan Sands8e4eb092008-06-08 20:54:56 +00002686 if (VT.bitsGT(VT0))
Evan Cheng571c4782007-08-18 05:57:05 +00002687 return DAG.getNode(ISD::ZERO_EXTEND, VT, XORNode);
2688 return DAG.getNode(ISD::TRUNCATE, VT, XORNode);
2689 }
Nate Begeman452d7be2005-09-16 00:54:12 +00002690 // fold select C, 0, X -> ~C & X
Dale Johannesene0e6fac2007-12-06 17:53:31 +00002691 if (VT == VT0 && VT == MVT::i1 && N1C && N1C->isNullValue()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002692 SDValue XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Gabor Greifba36cb52008-08-28 21:40:38 +00002693 AddToWorkList(XORNode.getNode());
Nate Begeman452d7be2005-09-16 00:54:12 +00002694 return DAG.getNode(ISD::AND, VT, XORNode, N2);
2695 }
2696 // fold select C, X, 1 -> ~C | X
Dan Gohman002e5d02008-03-13 22:13:53 +00002697 if (VT == VT0 && VT == MVT::i1 && N2C && N2C->getAPIntValue() == 1) {
Dan Gohman475871a2008-07-27 21:46:04 +00002698 SDValue XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Gabor Greifba36cb52008-08-28 21:40:38 +00002699 AddToWorkList(XORNode.getNode());
Nate Begeman452d7be2005-09-16 00:54:12 +00002700 return DAG.getNode(ISD::OR, VT, XORNode, N1);
2701 }
2702 // fold select C, X, 0 -> C & X
2703 // FIXME: this should check for C type == X type, not i1?
Duncan Sands83ec4b62008-06-06 12:08:01 +00002704 if (VT == MVT::i1 && N2C && N2C->isNullValue())
Nate Begeman452d7be2005-09-16 00:54:12 +00002705 return DAG.getNode(ISD::AND, VT, N0, N1);
2706 // fold X ? X : Y --> X ? 1 : Y --> X | Y
Duncan Sands83ec4b62008-06-06 12:08:01 +00002707 if (VT == MVT::i1 && N0 == N1)
Nate Begeman452d7be2005-09-16 00:54:12 +00002708 return DAG.getNode(ISD::OR, VT, N0, N2);
2709 // fold X ? Y : X --> X ? Y : 0 --> X & Y
Duncan Sands83ec4b62008-06-06 12:08:01 +00002710 if (VT == MVT::i1 && N0 == N2)
Nate Begeman452d7be2005-09-16 00:54:12 +00002711 return DAG.getNode(ISD::AND, VT, N0, N1);
Chris Lattner729c6d12006-05-27 00:43:02 +00002712
Chris Lattner40c62d52005-10-18 06:04:22 +00002713 // If we can fold this based on the true/false value, do so.
2714 if (SimplifySelectOps(N, N1, N2))
Dan Gohman475871a2008-07-27 21:46:04 +00002715 return SDValue(N, 0); // Don't revisit N.
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002716
Nate Begeman44728a72005-09-19 22:34:01 +00002717 // fold selects based on a setcc into other things, such as min/max/abs
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002718 if (N0.getOpcode() == ISD::SETCC) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00002719 // FIXME:
2720 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
2721 // having to say they don't support SELECT_CC on every type the DAG knows
2722 // about, since there is no way to mark an opcode illegal at all value types
2723 if (TLI.isOperationLegal(ISD::SELECT_CC, MVT::Other))
2724 return DAG.getNode(ISD::SELECT_CC, VT, N0.getOperand(0), N0.getOperand(1),
2725 N1, N2, N0.getOperand(2));
2726 else
2727 return SimplifySelect(N0, N1, N2);
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002728 }
Dan Gohman475871a2008-07-27 21:46:04 +00002729 return SDValue();
Nate Begeman452d7be2005-09-16 00:54:12 +00002730}
2731
Dan Gohman475871a2008-07-27 21:46:04 +00002732SDValue DAGCombiner::visitSELECT_CC(SDNode *N) {
2733 SDValue N0 = N->getOperand(0);
2734 SDValue N1 = N->getOperand(1);
2735 SDValue N2 = N->getOperand(2);
2736 SDValue N3 = N->getOperand(3);
2737 SDValue N4 = N->getOperand(4);
Nate Begeman44728a72005-09-19 22:34:01 +00002738 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
2739
Nate Begeman44728a72005-09-19 22:34:01 +00002740 // fold select_cc lhs, rhs, x, x, cc -> x
2741 if (N2 == N3)
2742 return N2;
Chris Lattner40c62d52005-10-18 06:04:22 +00002743
Chris Lattner5f42a242006-09-20 06:19:26 +00002744 // Determine if the condition we're dealing with is constant
Dan Gohman475871a2008-07-27 21:46:04 +00002745 SDValue SCC = SimplifySetCC(TLI.getSetCCResultType(N0), N0, N1, CC, false);
Gabor Greifba36cb52008-08-28 21:40:38 +00002746 if (SCC.getNode()) AddToWorkList(SCC.getNode());
Chris Lattner5f42a242006-09-20 06:19:26 +00002747
Gabor Greifba36cb52008-08-28 21:40:38 +00002748 if (ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.getNode())) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002749 if (!SCCC->isNullValue())
Chris Lattner5f42a242006-09-20 06:19:26 +00002750 return N2; // cond always true -> true val
2751 else
2752 return N3; // cond always false -> false val
2753 }
2754
2755 // Fold to a simpler select_cc
Gabor Greifba36cb52008-08-28 21:40:38 +00002756 if (SCC.getNode() && SCC.getOpcode() == ISD::SETCC)
Chris Lattner5f42a242006-09-20 06:19:26 +00002757 return DAG.getNode(ISD::SELECT_CC, N2.getValueType(),
2758 SCC.getOperand(0), SCC.getOperand(1), N2, N3,
2759 SCC.getOperand(2));
2760
Chris Lattner40c62d52005-10-18 06:04:22 +00002761 // If we can fold this based on the true/false value, do so.
2762 if (SimplifySelectOps(N, N2, N3))
Dan Gohman475871a2008-07-27 21:46:04 +00002763 return SDValue(N, 0); // Don't revisit N.
Chris Lattner40c62d52005-10-18 06:04:22 +00002764
Nate Begeman44728a72005-09-19 22:34:01 +00002765 // fold select_cc into other things, such as min/max/abs
2766 return SimplifySelectCC(N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00002767}
2768
Dan Gohman475871a2008-07-27 21:46:04 +00002769SDValue DAGCombiner::visitSETCC(SDNode *N) {
Nate Begeman452d7be2005-09-16 00:54:12 +00002770 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
2771 cast<CondCodeSDNode>(N->getOperand(2))->get());
2772}
2773
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002774// ExtendUsesToFormExtLoad - Trying to extend uses of a load to enable this:
2775// "fold ({s|z}ext (load x)) -> ({s|z}ext (truncate ({s|z}extload x)))"
2776// transformation. Returns true if extension are possible and the above
2777// mentioned transformation is profitable.
Dan Gohman475871a2008-07-27 21:46:04 +00002778static bool ExtendUsesToFormExtLoad(SDNode *N, SDValue N0,
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002779 unsigned ExtOpc,
2780 SmallVector<SDNode*, 4> &ExtendNodes,
2781 TargetLowering &TLI) {
2782 bool HasCopyToRegUses = false;
2783 bool isTruncFree = TLI.isTruncateFree(N->getValueType(0), N0.getValueType());
Gabor Greif12632d22008-08-30 19:29:20 +00002784 for (SDNode::use_iterator UI = N0.getNode()->use_begin(),
2785 UE = N0.getNode()->use_end();
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002786 UI != UE; ++UI) {
Dan Gohman89684502008-07-27 20:43:25 +00002787 SDNode *User = *UI;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002788 if (User == N)
2789 continue;
2790 // FIXME: Only extend SETCC N, N and SETCC N, c for now.
2791 if (User->getOpcode() == ISD::SETCC) {
2792 ISD::CondCode CC = cast<CondCodeSDNode>(User->getOperand(2))->get();
2793 if (ExtOpc == ISD::ZERO_EXTEND && ISD::isSignedIntSetCC(CC))
2794 // Sign bits will be lost after a zext.
2795 return false;
2796 bool Add = false;
2797 for (unsigned i = 0; i != 2; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002798 SDValue UseOp = User->getOperand(i);
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002799 if (UseOp == N0)
2800 continue;
2801 if (!isa<ConstantSDNode>(UseOp))
2802 return false;
2803 Add = true;
2804 }
2805 if (Add)
2806 ExtendNodes.push_back(User);
2807 } else {
2808 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002809 SDValue UseOp = User->getOperand(i);
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002810 if (UseOp == N0) {
2811 // If truncate from extended type to original load type is free
2812 // on this target, then it's ok to extend a CopyToReg.
2813 if (isTruncFree && User->getOpcode() == ISD::CopyToReg)
2814 HasCopyToRegUses = true;
2815 else
2816 return false;
2817 }
2818 }
2819 }
2820 }
2821
2822 if (HasCopyToRegUses) {
2823 bool BothLiveOut = false;
2824 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
2825 UI != UE; ++UI) {
Dan Gohman89684502008-07-27 20:43:25 +00002826 SDNode *User = *UI;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002827 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002828 SDValue UseOp = User->getOperand(i);
Gabor Greifba36cb52008-08-28 21:40:38 +00002829 if (UseOp.getNode() == N && UseOp.getResNo() == 0) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002830 BothLiveOut = true;
2831 break;
2832 }
2833 }
2834 }
2835 if (BothLiveOut)
2836 // Both unextended and extended values are live out. There had better be
2837 // good a reason for the transformation.
2838 return ExtendNodes.size();
2839 }
2840 return true;
2841}
2842
Dan Gohman475871a2008-07-27 21:46:04 +00002843SDValue DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
2844 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002845 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002846
Nate Begeman1d4d4142005-09-01 00:19:25 +00002847 // fold (sext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00002848 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002849 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0);
Chris Lattner310b5782006-05-06 23:06:26 +00002850
Nate Begeman1d4d4142005-09-01 00:19:25 +00002851 // fold (sext (sext x)) -> (sext x)
Chris Lattner310b5782006-05-06 23:06:26 +00002852 // fold (sext (aext x)) -> (sext x)
2853 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002854 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
Chris Lattner310b5782006-05-06 23:06:26 +00002855
Chris Lattner22558872007-02-26 03:13:59 +00002856 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman1fdfa6a2008-05-20 20:56:33 +00002857 // fold (sext (truncate (load x))) -> (sext (smaller load x))
2858 // fold (sext (truncate (srl (load x), c))) -> (sext (smaller load (x+c/n)))
Gabor Greifba36cb52008-08-28 21:40:38 +00002859 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
2860 if (NarrowLoad.getNode()) {
2861 if (NarrowLoad.getNode() != N0.getNode())
2862 CombineTo(N0.getNode(), NarrowLoad);
Evan Cheng0b063de2007-03-23 02:16:52 +00002863 return DAG.getNode(ISD::SIGN_EXTEND, VT, NarrowLoad);
2864 }
Evan Chengc88138f2007-03-22 01:54:19 +00002865
Dan Gohman1fdfa6a2008-05-20 20:56:33 +00002866 // See if the value being truncated is already sign extended. If so, just
2867 // eliminate the trunc/sext pair.
Dan Gohman475871a2008-07-27 21:46:04 +00002868 SDValue Op = N0.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002869 unsigned OpBits = Op.getValueType().getSizeInBits();
2870 unsigned MidBits = N0.getValueType().getSizeInBits();
2871 unsigned DestBits = VT.getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002872 unsigned NumSignBits = DAG.ComputeNumSignBits(Op);
Chris Lattner22558872007-02-26 03:13:59 +00002873
2874 if (OpBits == DestBits) {
2875 // Op is i32, Mid is i8, and Dest is i32. If Op has more than 24 sign
2876 // bits, it is already ready.
2877 if (NumSignBits > DestBits-MidBits)
2878 return Op;
2879 } else if (OpBits < DestBits) {
2880 // Op is i32, Mid is i8, and Dest is i64. If Op has more than 24 sign
2881 // bits, just sext from i32.
2882 if (NumSignBits > OpBits-MidBits)
2883 return DAG.getNode(ISD::SIGN_EXTEND, VT, Op);
2884 } else {
2885 // Op is i64, Mid is i8, and Dest is i32. If Op has more than 56 sign
2886 // bits, just truncate to i32.
2887 if (NumSignBits > OpBits-MidBits)
2888 return DAG.getNode(ISD::TRUNCATE, VT, Op);
Chris Lattner6007b842006-09-21 06:00:20 +00002889 }
Chris Lattner22558872007-02-26 03:13:59 +00002890
2891 // fold (sext (truncate x)) -> (sextinreg x).
2892 if (!AfterLegalize || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG,
2893 N0.getValueType())) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00002894 if (Op.getValueType().bitsLT(VT))
Chris Lattner22558872007-02-26 03:13:59 +00002895 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
Duncan Sands8e4eb092008-06-08 20:54:56 +00002896 else if (Op.getValueType().bitsGT(VT))
Chris Lattner22558872007-02-26 03:13:59 +00002897 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
2898 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, Op,
2899 DAG.getValueType(N0.getValueType()));
2900 }
Chris Lattner6007b842006-09-21 06:00:20 +00002901 }
Chris Lattner310b5782006-05-06 23:06:26 +00002902
Evan Cheng110dec22005-12-14 02:19:23 +00002903 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00002904 if (ISD::isNON_EXTLoad(N0.getNode()) &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002905 ((!AfterLegalize && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00002906 TLI.isLoadExtLegal(ISD::SEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002907 bool DoXform = true;
2908 SmallVector<SDNode*, 4> SetCCs;
2909 if (!N0.hasOneUse())
2910 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::SIGN_EXTEND, SetCCs, TLI);
2911 if (DoXform) {
2912 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman475871a2008-07-27 21:46:04 +00002913 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002914 LN0->getBasePtr(), LN0->getSrcValue(),
2915 LN0->getSrcValueOffset(),
2916 N0.getValueType(),
2917 LN0->isVolatile(),
2918 LN0->getAlignment());
2919 CombineTo(N, ExtLoad);
Dan Gohman475871a2008-07-27 21:46:04 +00002920 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00002921 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002922 // Extend SetCC uses if necessary.
2923 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
2924 SDNode *SetCC = SetCCs[i];
Dan Gohman475871a2008-07-27 21:46:04 +00002925 SmallVector<SDValue, 4> Ops;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002926 for (unsigned j = 0; j != 2; ++j) {
Dan Gohman475871a2008-07-27 21:46:04 +00002927 SDValue SOp = SetCC->getOperand(j);
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002928 if (SOp == Trunc)
2929 Ops.push_back(ExtLoad);
2930 else
2931 Ops.push_back(DAG.getNode(ISD::SIGN_EXTEND, VT, SOp));
2932 }
2933 Ops.push_back(SetCC->getOperand(2));
2934 CombineTo(SetCC, DAG.getNode(ISD::SETCC, SetCC->getValueType(0),
2935 &Ops[0], Ops.size()));
2936 }
Dan Gohman475871a2008-07-27 21:46:04 +00002937 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002938 }
Nate Begeman3df4d522005-10-12 20:40:40 +00002939 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002940
2941 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
2942 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00002943 if ((ISD::isSEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) &&
2944 ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002945 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002946 MVT EVT = LN0->getMemoryVT();
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002947 if ((!AfterLegalize && !LN0->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00002948 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT)) {
Dan Gohman475871a2008-07-27 21:46:04 +00002949 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00002950 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002951 LN0->getSrcValueOffset(), EVT,
2952 LN0->isVolatile(),
2953 LN0->getAlignment());
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00002954 CombineTo(N, ExtLoad);
Gabor Greif12632d22008-08-30 19:29:20 +00002955 CombineTo(N0.getNode(),
2956 DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00002957 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00002958 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00002959 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002960 }
2961
Chris Lattner20a35c32007-04-11 05:32:27 +00002962 // sext(setcc x,y,cc) -> select_cc x, y, -1, 0, cc
2963 if (N0.getOpcode() == ISD::SETCC) {
Dan Gohman475871a2008-07-27 21:46:04 +00002964 SDValue SCC =
Chris Lattner1eba01e2007-04-11 06:50:51 +00002965 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
2966 DAG.getConstant(~0ULL, VT), DAG.getConstant(0, VT),
2967 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00002968 if (SCC.getNode()) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00002969 }
2970
Dan Gohman8f0ad582008-04-28 16:58:24 +00002971 // fold (sext x) -> (zext x) if the sign bit is known zero.
Dan Gohman187db7b2008-04-28 18:47:17 +00002972 if ((!AfterLegalize || TLI.isOperationLegal(ISD::ZERO_EXTEND, VT)) &&
2973 DAG.SignBitIsZero(N0))
Dan Gohman8f0ad582008-04-28 16:58:24 +00002974 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
2975
Dan Gohman475871a2008-07-27 21:46:04 +00002976 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002977}
2978
Dan Gohman475871a2008-07-27 21:46:04 +00002979SDValue DAGCombiner::visitZERO_EXTEND(SDNode *N) {
2980 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002981 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002982
Nate Begeman1d4d4142005-09-01 00:19:25 +00002983 // fold (zext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00002984 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002985 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002986 // fold (zext (zext x)) -> (zext x)
Chris Lattner310b5782006-05-06 23:06:26 +00002987 // fold (zext (aext x)) -> (zext x)
2988 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002989 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
Chris Lattner6007b842006-09-21 06:00:20 +00002990
Evan Chengc88138f2007-03-22 01:54:19 +00002991 // fold (zext (truncate (load x))) -> (zext (smaller load x))
2992 // fold (zext (truncate (srl (load x), c))) -> (zext (small load (x+c/n)))
Dale Johannesen2041a0e2007-03-30 21:38:07 +00002993 if (N0.getOpcode() == ISD::TRUNCATE) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002994 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
2995 if (NarrowLoad.getNode()) {
2996 if (NarrowLoad.getNode() != N0.getNode())
2997 CombineTo(N0.getNode(), NarrowLoad);
Evan Cheng0b063de2007-03-23 02:16:52 +00002998 return DAG.getNode(ISD::ZERO_EXTEND, VT, NarrowLoad);
2999 }
Evan Chengc88138f2007-03-22 01:54:19 +00003000 }
3001
Chris Lattner6007b842006-09-21 06:00:20 +00003002 // fold (zext (truncate x)) -> (and x, mask)
3003 if (N0.getOpcode() == ISD::TRUNCATE &&
3004 (!AfterLegalize || TLI.isOperationLegal(ISD::AND, VT))) {
Dan Gohman475871a2008-07-27 21:46:04 +00003005 SDValue Op = N0.getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003006 if (Op.getValueType().bitsLT(VT)) {
Chris Lattner6007b842006-09-21 06:00:20 +00003007 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003008 } else if (Op.getValueType().bitsGT(VT)) {
Chris Lattner6007b842006-09-21 06:00:20 +00003009 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
3010 }
3011 return DAG.getZeroExtendInReg(Op, N0.getValueType());
3012 }
3013
Chris Lattner111c2282006-09-21 06:14:31 +00003014 // fold (zext (and (trunc x), cst)) -> (and x, cst).
3015 if (N0.getOpcode() == ISD::AND &&
3016 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
3017 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohman475871a2008-07-27 21:46:04 +00003018 SDValue X = N0.getOperand(0).getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003019 if (X.getValueType().bitsLT(VT)) {
Chris Lattner111c2282006-09-21 06:14:31 +00003020 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003021 } else if (X.getValueType().bitsGT(VT)) {
Chris Lattner111c2282006-09-21 06:14:31 +00003022 X = DAG.getNode(ISD::TRUNCATE, VT, X);
3023 }
Dan Gohman220a8232008-03-03 23:51:38 +00003024 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003025 Mask.zext(VT.getSizeInBits());
Chris Lattner111c2282006-09-21 06:14:31 +00003026 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
3027 }
3028
Evan Cheng110dec22005-12-14 02:19:23 +00003029 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00003030 if (ISD::isNON_EXTLoad(N0.getNode()) &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003031 ((!AfterLegalize && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00003032 TLI.isLoadExtLegal(ISD::ZEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003033 bool DoXform = true;
3034 SmallVector<SDNode*, 4> SetCCs;
3035 if (!N0.hasOneUse())
3036 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ZERO_EXTEND, SetCCs, TLI);
3037 if (DoXform) {
3038 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman475871a2008-07-27 21:46:04 +00003039 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003040 LN0->getBasePtr(), LN0->getSrcValue(),
3041 LN0->getSrcValueOffset(),
3042 N0.getValueType(),
3043 LN0->isVolatile(),
3044 LN0->getAlignment());
3045 CombineTo(N, ExtLoad);
Dan Gohman475871a2008-07-27 21:46:04 +00003046 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00003047 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003048 // Extend SetCC uses if necessary.
3049 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
3050 SDNode *SetCC = SetCCs[i];
Dan Gohman475871a2008-07-27 21:46:04 +00003051 SmallVector<SDValue, 4> Ops;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003052 for (unsigned j = 0; j != 2; ++j) {
Dan Gohman475871a2008-07-27 21:46:04 +00003053 SDValue SOp = SetCC->getOperand(j);
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003054 if (SOp == Trunc)
3055 Ops.push_back(ExtLoad);
3056 else
Evan Chengde1631b2007-10-30 20:11:21 +00003057 Ops.push_back(DAG.getNode(ISD::ZERO_EXTEND, VT, SOp));
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003058 }
3059 Ops.push_back(SetCC->getOperand(2));
3060 CombineTo(SetCC, DAG.getNode(ISD::SETCC, SetCC->getValueType(0),
3061 &Ops[0], Ops.size()));
3062 }
Dan Gohman475871a2008-07-27 21:46:04 +00003063 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003064 }
Evan Cheng110dec22005-12-14 02:19:23 +00003065 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00003066
3067 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
3068 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00003069 if ((ISD::isZEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) &&
3070 ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00003071 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003072 MVT EVT = LN0->getMemoryVT();
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003073 if ((!AfterLegalize && !LN0->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00003074 TLI.isLoadExtLegal(ISD::ZEXTLOAD, EVT)) {
Dan Gohman475871a2008-07-27 21:46:04 +00003075 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003076 LN0->getBasePtr(), LN0->getSrcValue(),
3077 LN0->getSrcValueOffset(), EVT,
3078 LN0->isVolatile(),
3079 LN0->getAlignment());
3080 CombineTo(N, ExtLoad);
Gabor Greif12632d22008-08-30 19:29:20 +00003081 CombineTo(N0.getNode(),
3082 DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003083 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003084 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003085 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00003086 }
Chris Lattner20a35c32007-04-11 05:32:27 +00003087
3088 // zext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
3089 if (N0.getOpcode() == ISD::SETCC) {
Dan Gohman475871a2008-07-27 21:46:04 +00003090 SDValue SCC =
Chris Lattner20a35c32007-04-11 05:32:27 +00003091 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
3092 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattner1eba01e2007-04-11 06:50:51 +00003093 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00003094 if (SCC.getNode()) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00003095 }
3096
Dan Gohman475871a2008-07-27 21:46:04 +00003097 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003098}
3099
Dan Gohman475871a2008-07-27 21:46:04 +00003100SDValue DAGCombiner::visitANY_EXTEND(SDNode *N) {
3101 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003102 MVT VT = N->getValueType(0);
Chris Lattner5ffc0662006-05-05 05:58:59 +00003103
3104 // fold (aext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00003105 if (isa<ConstantSDNode>(N0))
Chris Lattner5ffc0662006-05-05 05:58:59 +00003106 return DAG.getNode(ISD::ANY_EXTEND, VT, N0);
3107 // fold (aext (aext x)) -> (aext x)
3108 // fold (aext (zext x)) -> (zext x)
3109 // fold (aext (sext x)) -> (sext x)
3110 if (N0.getOpcode() == ISD::ANY_EXTEND ||
3111 N0.getOpcode() == ISD::ZERO_EXTEND ||
3112 N0.getOpcode() == ISD::SIGN_EXTEND)
3113 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
3114
Evan Chengc88138f2007-03-22 01:54:19 +00003115 // fold (aext (truncate (load x))) -> (aext (smaller load x))
3116 // fold (aext (truncate (srl (load x), c))) -> (aext (small load (x+c/n)))
3117 if (N0.getOpcode() == ISD::TRUNCATE) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003118 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
3119 if (NarrowLoad.getNode()) {
3120 if (NarrowLoad.getNode() != N0.getNode())
3121 CombineTo(N0.getNode(), NarrowLoad);
Evan Cheng0b063de2007-03-23 02:16:52 +00003122 return DAG.getNode(ISD::ANY_EXTEND, VT, NarrowLoad);
3123 }
Evan Chengc88138f2007-03-22 01:54:19 +00003124 }
3125
Chris Lattner84750582006-09-20 06:29:17 +00003126 // fold (aext (truncate x))
3127 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman475871a2008-07-27 21:46:04 +00003128 SDValue TruncOp = N0.getOperand(0);
Chris Lattner84750582006-09-20 06:29:17 +00003129 if (TruncOp.getValueType() == VT)
3130 return TruncOp; // x iff x size == zext size.
Duncan Sands8e4eb092008-06-08 20:54:56 +00003131 if (TruncOp.getValueType().bitsGT(VT))
Chris Lattner84750582006-09-20 06:29:17 +00003132 return DAG.getNode(ISD::TRUNCATE, VT, TruncOp);
3133 return DAG.getNode(ISD::ANY_EXTEND, VT, TruncOp);
3134 }
Chris Lattner0e4b9222006-09-21 06:40:43 +00003135
3136 // fold (aext (and (trunc x), cst)) -> (and x, cst).
3137 if (N0.getOpcode() == ISD::AND &&
3138 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
3139 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohman475871a2008-07-27 21:46:04 +00003140 SDValue X = N0.getOperand(0).getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003141 if (X.getValueType().bitsLT(VT)) {
Chris Lattner0e4b9222006-09-21 06:40:43 +00003142 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003143 } else if (X.getValueType().bitsGT(VT)) {
Chris Lattner0e4b9222006-09-21 06:40:43 +00003144 X = DAG.getNode(ISD::TRUNCATE, VT, X);
3145 }
Dan Gohman220a8232008-03-03 23:51:38 +00003146 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003147 Mask.zext(VT.getSizeInBits());
Chris Lattner0e4b9222006-09-21 06:40:43 +00003148 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
3149 }
3150
Chris Lattner5ffc0662006-05-05 05:58:59 +00003151 // fold (aext (load x)) -> (aext (truncate (extload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00003152 if (ISD::isNON_EXTLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003153 ((!AfterLegalize && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00003154 TLI.isLoadExtLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003155 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman475871a2008-07-27 21:46:04 +00003156 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
Evan Cheng466685d2006-10-09 20:57:25 +00003157 LN0->getBasePtr(), LN0->getSrcValue(),
3158 LN0->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003159 N0.getValueType(),
3160 LN0->isVolatile(),
3161 LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00003162 CombineTo(N, ExtLoad);
Dan Gohman75dcf082008-07-31 00:50:31 +00003163 // Redirect any chain users to the new load.
Evan Cheng45299662008-08-29 23:20:46 +00003164 DAG.ReplaceAllUsesOfValueWith(SDValue(LN0, 1),
3165 SDValue(ExtLoad.getNode(), 1));
Dan Gohman75dcf082008-07-31 00:50:31 +00003166 // If any node needs the original loaded value, recompute it.
3167 if (!LN0->use_empty())
3168 CombineTo(LN0, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
3169 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003170 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner5ffc0662006-05-05 05:58:59 +00003171 }
3172
3173 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
3174 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
3175 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00003176 if (N0.getOpcode() == ISD::LOAD &&
Gabor Greifba36cb52008-08-28 21:40:38 +00003177 !ISD::isNON_EXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng466685d2006-10-09 20:57:25 +00003178 N0.hasOneUse()) {
3179 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003180 MVT EVT = LN0->getMemoryVT();
Dan Gohman475871a2008-07-27 21:46:04 +00003181 SDValue ExtLoad = DAG.getExtLoad(LN0->getExtensionType(), VT,
Evan Cheng466685d2006-10-09 20:57:25 +00003182 LN0->getChain(), LN0->getBasePtr(),
3183 LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003184 LN0->getSrcValueOffset(), EVT,
3185 LN0->isVolatile(),
3186 LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00003187 CombineTo(N, ExtLoad);
Evan Cheng45299662008-08-29 23:20:46 +00003188 CombineTo(N0.getNode(),
3189 DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
Chris Lattner5ffc0662006-05-05 05:58:59 +00003190 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003191 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner5ffc0662006-05-05 05:58:59 +00003192 }
Chris Lattner20a35c32007-04-11 05:32:27 +00003193
3194 // aext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
3195 if (N0.getOpcode() == ISD::SETCC) {
Dan Gohman475871a2008-07-27 21:46:04 +00003196 SDValue SCC =
Chris Lattner1eba01e2007-04-11 06:50:51 +00003197 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
3198 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattnerc24bbad2007-04-11 16:51:53 +00003199 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00003200 if (SCC.getNode())
Chris Lattnerc56a81d2007-04-11 06:43:25 +00003201 return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00003202 }
3203
Dan Gohman475871a2008-07-27 21:46:04 +00003204 return SDValue();
Chris Lattner5ffc0662006-05-05 05:58:59 +00003205}
3206
Chris Lattner2b4c2792007-10-13 06:35:54 +00003207/// GetDemandedBits - See if the specified operand can be simplified with the
3208/// knowledge that only the bits specified by Mask are used. If so, return the
Dan Gohman475871a2008-07-27 21:46:04 +00003209/// simpler operand, otherwise return a null SDValue.
3210SDValue DAGCombiner::GetDemandedBits(SDValue V, const APInt &Mask) {
Chris Lattner2b4c2792007-10-13 06:35:54 +00003211 switch (V.getOpcode()) {
3212 default: break;
3213 case ISD::OR:
3214 case ISD::XOR:
3215 // If the LHS or RHS don't contribute bits to the or, drop them.
3216 if (DAG.MaskedValueIsZero(V.getOperand(0), Mask))
3217 return V.getOperand(1);
3218 if (DAG.MaskedValueIsZero(V.getOperand(1), Mask))
3219 return V.getOperand(0);
3220 break;
Chris Lattnere33544c2007-10-13 06:58:48 +00003221 case ISD::SRL:
3222 // Only look at single-use SRLs.
Gabor Greifba36cb52008-08-28 21:40:38 +00003223 if (!V.getNode()->hasOneUse())
Chris Lattnere33544c2007-10-13 06:58:48 +00003224 break;
3225 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
3226 // See if we can recursively simplify the LHS.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003227 unsigned Amt = RHSC->getZExtValue();
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003228 APInt NewMask = Mask << Amt;
Dan Gohman475871a2008-07-27 21:46:04 +00003229 SDValue SimplifyLHS = GetDemandedBits(V.getOperand(0), NewMask);
Gabor Greifba36cb52008-08-28 21:40:38 +00003230 if (SimplifyLHS.getNode()) {
Chris Lattnere33544c2007-10-13 06:58:48 +00003231 return DAG.getNode(ISD::SRL, V.getValueType(),
3232 SimplifyLHS, V.getOperand(1));
3233 }
3234 }
Chris Lattner2b4c2792007-10-13 06:35:54 +00003235 }
Dan Gohman475871a2008-07-27 21:46:04 +00003236 return SDValue();
Chris Lattner2b4c2792007-10-13 06:35:54 +00003237}
3238
Evan Chengc88138f2007-03-22 01:54:19 +00003239/// ReduceLoadWidth - If the result of a wider load is shifted to right of N
3240/// bits and then truncated to a narrower type and where N is a multiple
3241/// of number of bits of the narrower type, transform it to a narrower load
3242/// from address + N / num of bits of new type. If the result is to be
3243/// extended, also fold the extension to form a extending load.
Dan Gohman475871a2008-07-27 21:46:04 +00003244SDValue DAGCombiner::ReduceLoadWidth(SDNode *N) {
Evan Chengc88138f2007-03-22 01:54:19 +00003245 unsigned Opc = N->getOpcode();
3246 ISD::LoadExtType ExtType = ISD::NON_EXTLOAD;
Dan Gohman475871a2008-07-27 21:46:04 +00003247 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003248 MVT VT = N->getValueType(0);
3249 MVT EVT = N->getValueType(0);
Evan Chengc88138f2007-03-22 01:54:19 +00003250
Dan Gohman7f8613e2008-08-14 20:04:46 +00003251 // This transformation isn't valid for vector loads.
3252 if (VT.isVector())
3253 return SDValue();
3254
Evan Chenge177e302007-03-23 22:13:36 +00003255 // Special case: SIGN_EXTEND_INREG is basically truncating to EVT then
3256 // extended to VT.
Evan Chengc88138f2007-03-22 01:54:19 +00003257 if (Opc == ISD::SIGN_EXTEND_INREG) {
3258 ExtType = ISD::SEXTLOAD;
3259 EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Evan Cheng03294662008-10-14 21:26:46 +00003260 if (AfterLegalize && !TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))
Dan Gohman475871a2008-07-27 21:46:04 +00003261 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00003262 }
3263
Duncan Sands83ec4b62008-06-06 12:08:01 +00003264 unsigned EVTBits = EVT.getSizeInBits();
Evan Chengc88138f2007-03-22 01:54:19 +00003265 unsigned ShAmt = 0;
Evan Chengb37b80c2007-03-23 20:55:21 +00003266 bool CombineSRL = false;
Evan Chengc88138f2007-03-22 01:54:19 +00003267 if (N0.getOpcode() == ISD::SRL && N0.hasOneUse()) {
3268 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003269 ShAmt = N01->getZExtValue();
Evan Chengc88138f2007-03-22 01:54:19 +00003270 // Is the shift amount a multiple of size of VT?
3271 if ((ShAmt & (EVTBits-1)) == 0) {
3272 N0 = N0.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003273 if (N0.getValueType().getSizeInBits() <= EVTBits)
Dan Gohman475871a2008-07-27 21:46:04 +00003274 return SDValue();
Evan Chengb37b80c2007-03-23 20:55:21 +00003275 CombineSRL = true;
Evan Chengc88138f2007-03-22 01:54:19 +00003276 }
3277 }
3278 }
3279
Duncan Sandsad205a72008-06-16 08:14:38 +00003280 // Do not generate loads of non-round integer types since these can
3281 // be expensive (and would be wrong if the type is not byte sized).
Dan Gohman75dcf082008-07-31 00:50:31 +00003282 if (isa<LoadSDNode>(N0) && N0.hasOneUse() && VT.isRound() &&
3283 cast<LoadSDNode>(N0)->getMemoryVT().getSizeInBits() > EVTBits &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003284 // Do not change the width of a volatile load.
3285 !cast<LoadSDNode>(N0)->isVolatile()) {
Evan Chengc88138f2007-03-22 01:54:19 +00003286 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003287 MVT PtrType = N0.getOperand(1).getValueType();
Evan Chengdae54ce2007-03-24 00:02:43 +00003288 // For big endian targets, we need to adjust the offset to the pointer to
3289 // load the correct bytes.
Duncan Sands0753fc12008-02-11 10:37:04 +00003290 if (TLI.isBigEndian()) {
Dan Gohman75dcf082008-07-31 00:50:31 +00003291 unsigned LVTStoreBits = LN0->getMemoryVT().getStoreSizeInBits();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003292 unsigned EVTStoreBits = EVT.getStoreSizeInBits();
Duncan Sandsc6fa1702007-11-09 08:57:19 +00003293 ShAmt = LVTStoreBits - EVTStoreBits - ShAmt;
3294 }
Evan Chengdae54ce2007-03-24 00:02:43 +00003295 uint64_t PtrOff = ShAmt / 8;
Duncan Sandsdc846502007-10-28 12:59:45 +00003296 unsigned NewAlign = MinAlign(LN0->getAlignment(), PtrOff);
Dan Gohman475871a2008-07-27 21:46:04 +00003297 SDValue NewPtr = DAG.getNode(ISD::ADD, PtrType, LN0->getBasePtr(),
Evan Chengc88138f2007-03-22 01:54:19 +00003298 DAG.getConstant(PtrOff, PtrType));
Gabor Greifba36cb52008-08-28 21:40:38 +00003299 AddToWorkList(NewPtr.getNode());
Dan Gohman475871a2008-07-27 21:46:04 +00003300 SDValue Load = (ExtType == ISD::NON_EXTLOAD)
Evan Chengc88138f2007-03-22 01:54:19 +00003301 ? DAG.getLoad(VT, LN0->getChain(), NewPtr,
Dan Gohman75dcf082008-07-31 00:50:31 +00003302 LN0->getSrcValue(), LN0->getSrcValueOffset() + PtrOff,
Duncan Sandsdc846502007-10-28 12:59:45 +00003303 LN0->isVolatile(), NewAlign)
Evan Chengc88138f2007-03-22 01:54:19 +00003304 : DAG.getExtLoad(ExtType, VT, LN0->getChain(), NewPtr,
Dan Gohman75dcf082008-07-31 00:50:31 +00003305 LN0->getSrcValue(), LN0->getSrcValueOffset() + PtrOff,
3306 EVT, LN0->isVolatile(), NewAlign);
Evan Chengc88138f2007-03-22 01:54:19 +00003307 AddToWorkList(N);
Evan Chengb37b80c2007-03-23 20:55:21 +00003308 if (CombineSRL) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003309 WorkListRemover DeadNodes(*this);
3310 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), Load.getValue(1),
3311 &DeadNodes);
Gabor Greifba36cb52008-08-28 21:40:38 +00003312 CombineTo(N->getOperand(0).getNode(), Load);
Evan Chengb37b80c2007-03-23 20:55:21 +00003313 } else
Gabor Greifba36cb52008-08-28 21:40:38 +00003314 CombineTo(N0.getNode(), Load, Load.getValue(1));
Evan Cheng15213b72007-03-26 07:12:51 +00003315 if (ShAmt) {
3316 if (Opc == ISD::SIGN_EXTEND_INREG)
3317 return DAG.getNode(Opc, VT, Load, N->getOperand(1));
3318 else
3319 return DAG.getNode(Opc, VT, Load);
3320 }
Dan Gohman475871a2008-07-27 21:46:04 +00003321 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Chengc88138f2007-03-22 01:54:19 +00003322 }
3323
Dan Gohman475871a2008-07-27 21:46:04 +00003324 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00003325}
3326
Chris Lattner5ffc0662006-05-05 05:58:59 +00003327
Dan Gohman475871a2008-07-27 21:46:04 +00003328SDValue DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
3329 SDValue N0 = N->getOperand(0);
3330 SDValue N1 = N->getOperand(1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003331 MVT VT = N->getValueType(0);
3332 MVT EVT = cast<VTSDNode>(N1)->getVT();
3333 unsigned VTBits = VT.getSizeInBits();
3334 unsigned EVTBits = EVT.getSizeInBits();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003335
Nate Begeman1d4d4142005-09-01 00:19:25 +00003336 // fold (sext_in_reg c1) -> c1
Chris Lattnereaeda562006-05-08 20:59:41 +00003337 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
Chris Lattner310b5782006-05-06 23:06:26 +00003338 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0, N1);
Chris Lattneree4ea922006-05-06 09:30:03 +00003339
Chris Lattner541a24f2006-05-06 22:43:44 +00003340 // If the input is already sign extended, just drop the extension.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003341 if (DAG.ComputeNumSignBits(N0) >= VT.getSizeInBits()-EVTBits+1)
Chris Lattneree4ea922006-05-06 09:30:03 +00003342 return N0;
3343
Nate Begeman646d7e22005-09-02 21:18:40 +00003344 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
3345 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Duncan Sands8e4eb092008-06-08 20:54:56 +00003346 EVT.bitsLT(cast<VTSDNode>(N0.getOperand(1))->getVT())) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00003347 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00003348 }
Chris Lattner4b37e872006-05-08 21:18:59 +00003349
Dan Gohman75dcf082008-07-31 00:50:31 +00003350 // fold (sext_in_reg (sext x)) -> (sext x)
3351 // fold (sext_in_reg (aext x)) -> (sext x)
3352 // if x is small enough.
3353 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND) {
3354 SDValue N00 = N0.getOperand(0);
3355 if (N00.getValueType().getSizeInBits() < EVTBits)
3356 return DAG.getNode(ISD::SIGN_EXTEND, VT, N00, N1);
3357 }
3358
Chris Lattner95a5e052007-04-17 19:03:21 +00003359 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is known zero.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003360 if (DAG.MaskedValueIsZero(N0, APInt::getBitsSet(VTBits, EVTBits-1, EVTBits)))
Nate Begemande996292006-02-03 22:24:05 +00003361 return DAG.getZeroExtendInReg(N0, EVT);
Chris Lattner4b37e872006-05-08 21:18:59 +00003362
Chris Lattner95a5e052007-04-17 19:03:21 +00003363 // fold operands of sext_in_reg based on knowledge that the top bits are not
3364 // demanded.
Dan Gohman475871a2008-07-27 21:46:04 +00003365 if (SimplifyDemandedBits(SDValue(N, 0)))
3366 return SDValue(N, 0);
Chris Lattner95a5e052007-04-17 19:03:21 +00003367
Evan Chengc88138f2007-03-22 01:54:19 +00003368 // fold (sext_in_reg (load x)) -> (smaller sextload x)
3369 // fold (sext_in_reg (srl (load x), c)) -> (smaller sextload (x+c/evtbits))
Dan Gohman475871a2008-07-27 21:46:04 +00003370 SDValue NarrowLoad = ReduceLoadWidth(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003371 if (NarrowLoad.getNode())
Evan Chengc88138f2007-03-22 01:54:19 +00003372 return NarrowLoad;
3373
Chris Lattner4b37e872006-05-08 21:18:59 +00003374 // fold (sext_in_reg (srl X, 24), i8) -> sra X, 24
3375 // fold (sext_in_reg (srl X, 23), i8) -> sra X, 23 iff possible.
3376 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
3377 if (N0.getOpcode() == ISD::SRL) {
3378 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003379 if (ShAmt->getZExtValue()+EVTBits <= VT.getSizeInBits()) {
Chris Lattner4b37e872006-05-08 21:18:59 +00003380 // We can turn this into an SRA iff the input to the SRL is already sign
3381 // extended enough.
Dan Gohmanea859be2007-06-22 14:59:07 +00003382 unsigned InSignBits = DAG.ComputeNumSignBits(N0.getOperand(0));
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003383 if (VT.getSizeInBits()-(ShAmt->getZExtValue()+EVTBits) < InSignBits)
Chris Lattner4b37e872006-05-08 21:18:59 +00003384 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0), N0.getOperand(1));
3385 }
3386 }
Evan Chengc88138f2007-03-22 01:54:19 +00003387
Nate Begemanded49632005-10-13 03:11:28 +00003388 // fold (sext_inreg (extload x)) -> (sextload x)
Gabor Greifba36cb52008-08-28 21:40:38 +00003389 if (ISD::isEXTLoad(N0.getNode()) &&
3390 ISD::isUNINDEXEDLoad(N0.getNode()) &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003391 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003392 ((!AfterLegalize && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00003393 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003394 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman475871a2008-07-27 21:46:04 +00003395 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
Evan Cheng466685d2006-10-09 20:57:25 +00003396 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003397 LN0->getSrcValueOffset(), EVT,
3398 LN0->isVolatile(),
3399 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00003400 CombineTo(N, ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00003401 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003402 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00003403 }
3404 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Gabor Greifba36cb52008-08-28 21:40:38 +00003405 if (ISD::isZEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng83060c52007-03-07 08:07:03 +00003406 N0.hasOneUse() &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003407 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003408 ((!AfterLegalize && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00003409 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003410 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman475871a2008-07-27 21:46:04 +00003411 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
Evan Cheng466685d2006-10-09 20:57:25 +00003412 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003413 LN0->getSrcValueOffset(), EVT,
3414 LN0->isVolatile(),
3415 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00003416 CombineTo(N, ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00003417 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003418 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00003419 }
Dan Gohman475871a2008-07-27 21:46:04 +00003420 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003421}
3422
Dan Gohman475871a2008-07-27 21:46:04 +00003423SDValue DAGCombiner::visitTRUNCATE(SDNode *N) {
3424 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003425 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003426
3427 // noop truncate
3428 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00003429 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003430 // fold (truncate c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00003431 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00003432 return DAG.getNode(ISD::TRUNCATE, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003433 // fold (truncate (truncate x)) -> (truncate x)
3434 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00003435 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003436 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
Chris Lattnerb72773b2006-05-05 22:56:26 +00003437 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND||
3438 N0.getOpcode() == ISD::ANY_EXTEND) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00003439 if (N0.getOperand(0).getValueType().bitsLT(VT))
Nate Begeman1d4d4142005-09-01 00:19:25 +00003440 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00003441 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Duncan Sands8e4eb092008-06-08 20:54:56 +00003442 else if (N0.getOperand(0).getValueType().bitsGT(VT))
Nate Begeman1d4d4142005-09-01 00:19:25 +00003443 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00003444 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003445 else
3446 // if the source and dest are the same type, we can drop both the extend
3447 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00003448 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003449 }
Evan Cheng007b69e2007-03-21 20:14:05 +00003450
Chris Lattner2b4c2792007-10-13 06:35:54 +00003451 // See if we can simplify the input to this truncate through knowledge that
3452 // only the low bits are being used. For example "trunc (or (shl x, 8), y)"
3453 // -> trunc y
Dan Gohman475871a2008-07-27 21:46:04 +00003454 SDValue Shorter =
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003455 GetDemandedBits(N0, APInt::getLowBitsSet(N0.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +00003456 VT.getSizeInBits()));
Gabor Greifba36cb52008-08-28 21:40:38 +00003457 if (Shorter.getNode())
Chris Lattner2b4c2792007-10-13 06:35:54 +00003458 return DAG.getNode(ISD::TRUNCATE, VT, Shorter);
3459
Nate Begeman3df4d522005-10-12 20:40:40 +00003460 // fold (truncate (load x)) -> (smaller load x)
Evan Cheng007b69e2007-03-21 20:14:05 +00003461 // fold (truncate (srl (load x), c)) -> (smaller load (x+c/evtbits))
Evan Chengc88138f2007-03-22 01:54:19 +00003462 return ReduceLoadWidth(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003463}
3464
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003465static SDNode *getBuildPairElt(SDNode *N, unsigned i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003466 SDValue Elt = N->getOperand(i);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003467 if (Elt.getOpcode() != ISD::MERGE_VALUES)
Gabor Greifba36cb52008-08-28 21:40:38 +00003468 return Elt.getNode();
3469 return Elt.getOperand(Elt.getResNo()).getNode();
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003470}
3471
3472/// CombineConsecutiveLoads - build_pair (load, load) -> load
3473/// if load locations are consecutive.
Dan Gohman475871a2008-07-27 21:46:04 +00003474SDValue DAGCombiner::CombineConsecutiveLoads(SDNode *N, MVT VT) {
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003475 assert(N->getOpcode() == ISD::BUILD_PAIR);
3476
3477 SDNode *LD1 = getBuildPairElt(N, 0);
3478 if (!ISD::isNON_EXTLoad(LD1) || !LD1->hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00003479 return SDValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003480 MVT LD1VT = LD1->getValueType(0);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003481 SDNode *LD2 = getBuildPairElt(N, 1);
3482 const MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3483 if (ISD::isNON_EXTLoad(LD2) &&
3484 LD2->hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003485 // If both are volatile this would reduce the number of volatile loads.
3486 // If one is volatile it might be ok, but play conservative and bail out.
3487 !cast<LoadSDNode>(LD1)->isVolatile() &&
3488 !cast<LoadSDNode>(LD2)->isVolatile() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003489 TLI.isConsecutiveLoad(LD2, LD1, LD1VT.getSizeInBits()/8, 1, MFI)) {
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003490 LoadSDNode *LD = cast<LoadSDNode>(LD1);
3491 unsigned Align = LD->getAlignment();
Dan Gohman6448d912008-09-04 15:39:15 +00003492 unsigned NewAlign = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00003493 getABITypeAlignment(VT.getTypeForMVT());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003494 if (NewAlign <= Align &&
3495 (!AfterLegalize || TLI.isOperationLegal(ISD::LOAD, VT)))
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003496 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(),
3497 LD->getSrcValue(), LD->getSrcValueOffset(),
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003498 false, Align);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003499 }
Dan Gohman475871a2008-07-27 21:46:04 +00003500 return SDValue();
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003501}
3502
Dan Gohman475871a2008-07-27 21:46:04 +00003503SDValue DAGCombiner::visitBIT_CONVERT(SDNode *N) {
3504 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003505 MVT VT = N->getValueType(0);
Chris Lattner94683772005-12-23 05:30:37 +00003506
Dan Gohman7f321562007-06-25 16:23:39 +00003507 // If the input is a BUILD_VECTOR with all constant elements, fold this now.
3508 // Only do this before legalize, since afterward the target may be depending
3509 // on the bitconvert.
3510 // First check to see if this is all constant.
3511 if (!AfterLegalize &&
Gabor Greifba36cb52008-08-28 21:40:38 +00003512 N0.getOpcode() == ISD::BUILD_VECTOR && N0.getNode()->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003513 VT.isVector()) {
Dan Gohman7f321562007-06-25 16:23:39 +00003514 bool isSimple = true;
3515 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i)
3516 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
3517 N0.getOperand(i).getOpcode() != ISD::Constant &&
3518 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
3519 isSimple = false;
3520 break;
3521 }
3522
Duncan Sands83ec4b62008-06-06 12:08:01 +00003523 MVT DestEltVT = N->getValueType(0).getVectorElementType();
3524 assert(!DestEltVT.isVector() &&
Dan Gohman7f321562007-06-25 16:23:39 +00003525 "Element type of vector ValueType must not be vector!");
3526 if (isSimple) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003527 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(N0.getNode(), DestEltVT);
Dan Gohman7f321562007-06-25 16:23:39 +00003528 }
3529 }
3530
Dan Gohman3dd168d2008-09-05 01:58:21 +00003531 // If the input is a constant, let getNode fold it.
Chris Lattner94683772005-12-23 05:30:37 +00003532 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
Dan Gohman475871a2008-07-27 21:46:04 +00003533 SDValue Res = DAG.getNode(ISD::BIT_CONVERT, VT, N0);
Gabor Greifba36cb52008-08-28 21:40:38 +00003534 if (Res.getNode() != N) return Res;
Chris Lattner94683772005-12-23 05:30:37 +00003535 }
3536
Chris Lattnerc8547d82005-12-23 05:37:50 +00003537 if (N0.getOpcode() == ISD::BIT_CONVERT) // conv(conv(x,t1),t2) -> conv(x,t2)
3538 return DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
Chris Lattner6258fb22006-04-02 02:53:43 +00003539
Chris Lattner57104102005-12-23 05:44:41 +00003540 // fold (conv (load x)) -> (load (conv*)x)
Evan Cheng513da432007-10-06 08:19:55 +00003541 // If the resultant load doesn't need a higher alignment than the original!
Gabor Greifba36cb52008-08-28 21:40:38 +00003542 if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003543 // Do not change the width of a volatile load.
3544 !cast<LoadSDNode>(N0)->isVolatile() &&
3545 (!AfterLegalize || TLI.isOperationLegal(ISD::LOAD, VT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003546 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman6448d912008-09-04 15:39:15 +00003547 unsigned Align = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00003548 getABITypeAlignment(VT.getTypeForMVT());
Evan Cheng59d5b682007-05-07 21:27:48 +00003549 unsigned OrigAlign = LN0->getAlignment();
3550 if (Align <= OrigAlign) {
Dan Gohman475871a2008-07-27 21:46:04 +00003551 SDValue Load = DAG.getLoad(VT, LN0->getChain(), LN0->getBasePtr(),
Evan Cheng59d5b682007-05-07 21:27:48 +00003552 LN0->getSrcValue(), LN0->getSrcValueOffset(),
Dan Gohman77455612008-06-28 00:45:22 +00003553 LN0->isVolatile(), OrigAlign);
Evan Cheng59d5b682007-05-07 21:27:48 +00003554 AddToWorkList(N);
Gabor Greif12632d22008-08-30 19:29:20 +00003555 CombineTo(N0.getNode(),
3556 DAG.getNode(ISD::BIT_CONVERT, N0.getValueType(), Load),
Evan Cheng59d5b682007-05-07 21:27:48 +00003557 Load.getValue(1));
3558 return Load;
3559 }
Chris Lattner57104102005-12-23 05:44:41 +00003560 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003561
Chris Lattner3bd39d42008-01-27 17:42:27 +00003562 // Fold bitconvert(fneg(x)) -> xor(bitconvert(x), signbit)
3563 // Fold bitconvert(fabs(x)) -> and(bitconvert(x), ~signbit)
3564 // This often reduces constant pool loads.
3565 if ((N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FABS) &&
Gabor Greifba36cb52008-08-28 21:40:38 +00003566 N0.getNode()->hasOneUse() && VT.isInteger() && !VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003567 SDValue NewConv = DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00003568 AddToWorkList(NewConv.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003569
Duncan Sands83ec4b62008-06-06 12:08:01 +00003570 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003571 if (N0.getOpcode() == ISD::FNEG)
3572 return DAG.getNode(ISD::XOR, VT, NewConv, DAG.getConstant(SignBit, VT));
3573 assert(N0.getOpcode() == ISD::FABS);
3574 return DAG.getNode(ISD::AND, VT, NewConv, DAG.getConstant(~SignBit, VT));
3575 }
3576
3577 // Fold bitconvert(fcopysign(cst, x)) -> bitconvert(x)&sign | cst&~sign'
3578 // Note that we don't handle copysign(x,cst) because this can always be folded
3579 // to an fneg or fabs.
Gabor Greifba36cb52008-08-28 21:40:38 +00003580 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse() &&
Chris Lattnerf32aac32008-01-27 23:32:17 +00003581 isa<ConstantFPSDNode>(N0.getOperand(0)) &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003582 VT.isInteger() && !VT.isVector()) {
3583 unsigned OrigXWidth = N0.getOperand(1).getValueType().getSizeInBits();
Dan Gohman475871a2008-07-27 21:46:04 +00003584 SDValue X = DAG.getNode(ISD::BIT_CONVERT,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003585 MVT::getIntegerVT(OrigXWidth),
Chris Lattner3bd39d42008-01-27 17:42:27 +00003586 N0.getOperand(1));
Gabor Greifba36cb52008-08-28 21:40:38 +00003587 AddToWorkList(X.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003588
3589 // If X has a different width than the result/lhs, sext it or truncate it.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003590 unsigned VTWidth = VT.getSizeInBits();
Chris Lattner3bd39d42008-01-27 17:42:27 +00003591 if (OrigXWidth < VTWidth) {
3592 X = DAG.getNode(ISD::SIGN_EXTEND, VT, X);
Gabor Greifba36cb52008-08-28 21:40:38 +00003593 AddToWorkList(X.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003594 } else if (OrigXWidth > VTWidth) {
3595 // To get the sign bit in the right place, we have to shift it right
3596 // before truncating.
3597 X = DAG.getNode(ISD::SRL, X.getValueType(), X,
3598 DAG.getConstant(OrigXWidth-VTWidth, X.getValueType()));
Gabor Greifba36cb52008-08-28 21:40:38 +00003599 AddToWorkList(X.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003600 X = DAG.getNode(ISD::TRUNCATE, VT, X);
Gabor Greifba36cb52008-08-28 21:40:38 +00003601 AddToWorkList(X.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003602 }
3603
Duncan Sands83ec4b62008-06-06 12:08:01 +00003604 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003605 X = DAG.getNode(ISD::AND, VT, X, DAG.getConstant(SignBit, VT));
Gabor Greifba36cb52008-08-28 21:40:38 +00003606 AddToWorkList(X.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003607
Dan Gohman475871a2008-07-27 21:46:04 +00003608 SDValue Cst = DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
Chris Lattner3bd39d42008-01-27 17:42:27 +00003609 Cst = DAG.getNode(ISD::AND, VT, Cst, DAG.getConstant(~SignBit, VT));
Gabor Greifba36cb52008-08-28 21:40:38 +00003610 AddToWorkList(Cst.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003611
3612 return DAG.getNode(ISD::OR, VT, X, Cst);
3613 }
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003614
3615 // bitconvert(build_pair(ld, ld)) -> ld iff load locations are consecutive.
3616 if (N0.getOpcode() == ISD::BUILD_PAIR) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003617 SDValue CombineLD = CombineConsecutiveLoads(N0.getNode(), VT);
3618 if (CombineLD.getNode())
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003619 return CombineLD;
3620 }
Chris Lattner3bd39d42008-01-27 17:42:27 +00003621
Dan Gohman475871a2008-07-27 21:46:04 +00003622 return SDValue();
Chris Lattner94683772005-12-23 05:30:37 +00003623}
3624
Dan Gohman475871a2008-07-27 21:46:04 +00003625SDValue DAGCombiner::visitBUILD_PAIR(SDNode *N) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003626 MVT VT = N->getValueType(0);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003627 return CombineConsecutiveLoads(N, VT);
3628}
3629
Dan Gohman7f321562007-06-25 16:23:39 +00003630/// ConstantFoldBIT_CONVERTofBUILD_VECTOR - We know that BV is a build_vector
Chris Lattner6258fb22006-04-02 02:53:43 +00003631/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
3632/// destination element value type.
Dan Gohman475871a2008-07-27 21:46:04 +00003633SDValue DAGCombiner::
Duncan Sands83ec4b62008-06-06 12:08:01 +00003634ConstantFoldBIT_CONVERTofBUILD_VECTOR(SDNode *BV, MVT DstEltVT) {
3635 MVT SrcEltVT = BV->getOperand(0).getValueType();
Chris Lattner6258fb22006-04-02 02:53:43 +00003636
3637 // If this is already the right type, we're done.
Dan Gohman475871a2008-07-27 21:46:04 +00003638 if (SrcEltVT == DstEltVT) return SDValue(BV, 0);
Chris Lattner6258fb22006-04-02 02:53:43 +00003639
Duncan Sands83ec4b62008-06-06 12:08:01 +00003640 unsigned SrcBitSize = SrcEltVT.getSizeInBits();
3641 unsigned DstBitSize = DstEltVT.getSizeInBits();
Chris Lattner6258fb22006-04-02 02:53:43 +00003642
3643 // If this is a conversion of N elements of one type to N elements of another
3644 // type, convert each element. This handles FP<->INT cases.
3645 if (SrcBitSize == DstBitSize) {
Dan Gohman475871a2008-07-27 21:46:04 +00003646 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003647 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003648 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, DstEltVT, BV->getOperand(i)));
Gabor Greifba36cb52008-08-28 21:40:38 +00003649 AddToWorkList(Ops.back().getNode());
Chris Lattner3e104b12006-04-08 04:15:24 +00003650 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00003651 MVT VT = MVT::getVectorVT(DstEltVT,
3652 BV->getValueType(0).getVectorNumElements());
Dan Gohman7f321562007-06-25 16:23:39 +00003653 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003654 }
3655
3656 // Otherwise, we're growing or shrinking the elements. To avoid having to
3657 // handle annoying details of growing/shrinking FP values, we convert them to
3658 // int first.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003659 if (SrcEltVT.isFloatingPoint()) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003660 // Convert the input float vector to a int vector where the elements are the
3661 // same sizes.
3662 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
Duncan Sands8eab8a22008-06-09 11:32:28 +00003663 MVT IntVT = MVT::getIntegerVT(SrcEltVT.getSizeInBits());
Gabor Greifba36cb52008-08-28 21:40:38 +00003664 BV = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, IntVT).getNode();
Chris Lattner6258fb22006-04-02 02:53:43 +00003665 SrcEltVT = IntVT;
3666 }
3667
3668 // Now we know the input is an integer vector. If the output is a FP type,
3669 // convert to integer first, then to FP of the right size.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003670 if (DstEltVT.isFloatingPoint()) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003671 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
Duncan Sands8eab8a22008-06-09 11:32:28 +00003672 MVT TmpVT = MVT::getIntegerVT(DstEltVT.getSizeInBits());
Gabor Greifba36cb52008-08-28 21:40:38 +00003673 SDNode *Tmp = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, TmpVT).getNode();
Chris Lattner6258fb22006-04-02 02:53:43 +00003674
3675 // Next, convert to FP elements of the same size.
Dan Gohman7f321562007-06-25 16:23:39 +00003676 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(Tmp, DstEltVT);
Chris Lattner6258fb22006-04-02 02:53:43 +00003677 }
3678
3679 // Okay, we know the src/dst types are both integers of differing types.
3680 // Handling growing first.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003681 assert(SrcEltVT.isInteger() && DstEltVT.isInteger());
Chris Lattner6258fb22006-04-02 02:53:43 +00003682 if (SrcBitSize < DstBitSize) {
3683 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
3684
Dan Gohman475871a2008-07-27 21:46:04 +00003685 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003686 for (unsigned i = 0, e = BV->getNumOperands(); i != e;
Chris Lattner6258fb22006-04-02 02:53:43 +00003687 i += NumInputsPerOutput) {
3688 bool isLE = TLI.isLittleEndian();
Dan Gohman220a8232008-03-03 23:51:38 +00003689 APInt NewBits = APInt(DstBitSize, 0);
Chris Lattner6258fb22006-04-02 02:53:43 +00003690 bool EltIsUndef = true;
3691 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
3692 // Shift the previously computed bits over.
3693 NewBits <<= SrcBitSize;
Dan Gohman475871a2008-07-27 21:46:04 +00003694 SDValue Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
Chris Lattner6258fb22006-04-02 02:53:43 +00003695 if (Op.getOpcode() == ISD::UNDEF) continue;
3696 EltIsUndef = false;
3697
Dan Gohman220a8232008-03-03 23:51:38 +00003698 NewBits |=
3699 APInt(cast<ConstantSDNode>(Op)->getAPIntValue()).zext(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00003700 }
3701
3702 if (EltIsUndef)
3703 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
3704 else
3705 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
3706 }
3707
Duncan Sands83ec4b62008-06-06 12:08:01 +00003708 MVT VT = MVT::getVectorVT(DstEltVT, Ops.size());
Dan Gohman7f321562007-06-25 16:23:39 +00003709 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003710 }
3711
3712 // Finally, this must be the case where we are shrinking elements: each input
3713 // turns into multiple outputs.
Evan Chengefec7512008-02-18 23:04:32 +00003714 bool isS2V = ISD::isScalarToVector(BV);
Chris Lattner6258fb22006-04-02 02:53:43 +00003715 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003716 MVT VT = MVT::getVectorVT(DstEltVT, NumOutputsPerInput*BV->getNumOperands());
Dan Gohman475871a2008-07-27 21:46:04 +00003717 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003718 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003719 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
3720 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
3721 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
3722 continue;
3723 }
Dan Gohman220a8232008-03-03 23:51:38 +00003724 APInt OpVal = cast<ConstantSDNode>(BV->getOperand(i))->getAPIntValue();
Chris Lattner6258fb22006-04-02 02:53:43 +00003725 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
Dan Gohman220a8232008-03-03 23:51:38 +00003726 APInt ThisVal = APInt(OpVal).trunc(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00003727 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
Dan Gohman220a8232008-03-03 23:51:38 +00003728 if (isS2V && i == 0 && j == 0 && APInt(ThisVal).zext(SrcBitSize) == OpVal)
Evan Chengefec7512008-02-18 23:04:32 +00003729 // Simply turn this into a SCALAR_TO_VECTOR of the new type.
3730 return DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Ops[0]);
Dan Gohman220a8232008-03-03 23:51:38 +00003731 OpVal = OpVal.lshr(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00003732 }
3733
3734 // For big endian targets, swap the order of the pieces of each element.
Duncan Sands0753fc12008-02-11 10:37:04 +00003735 if (TLI.isBigEndian())
Chris Lattner6258fb22006-04-02 02:53:43 +00003736 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
3737 }
Dan Gohman7f321562007-06-25 16:23:39 +00003738 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003739}
3740
3741
3742
Dan Gohman475871a2008-07-27 21:46:04 +00003743SDValue DAGCombiner::visitFADD(SDNode *N) {
3744 SDValue N0 = N->getOperand(0);
3745 SDValue N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003746 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3747 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003748 MVT VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00003749
Dan Gohman7f321562007-06-25 16:23:39 +00003750 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003751 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003752 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003753 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00003754 }
Dan Gohman7f321562007-06-25 16:23:39 +00003755
Nate Begemana0e221d2005-10-18 00:28:13 +00003756 // fold (fadd c1, c2) -> c1+c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003757 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003758 return DAG.getNode(ISD::FADD, VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003759 // canonicalize constant to RHS
3760 if (N0CFP && !N1CFP)
3761 return DAG.getNode(ISD::FADD, VT, N1, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00003762 // fold (A + (-B)) -> A-B
Chris Lattner0254e702008-02-26 07:04:54 +00003763 if (isNegatibleForFree(N1, AfterLegalize) == 2)
3764 return DAG.getNode(ISD::FSUB, VT, N0,
3765 GetNegatedExpression(N1, DAG, AfterLegalize));
Chris Lattner01b3d732005-09-28 22:28:18 +00003766 // fold ((-A) + B) -> B-A
Chris Lattner0254e702008-02-26 07:04:54 +00003767 if (isNegatibleForFree(N0, AfterLegalize) == 2)
3768 return DAG.getNode(ISD::FSUB, VT, N1,
3769 GetNegatedExpression(N0, DAG, AfterLegalize));
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003770
3771 // If allowed, fold (fadd (fadd x, c1), c2) -> (fadd x, (fadd c1, c2))
3772 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FADD &&
Gabor Greifba36cb52008-08-28 21:40:38 +00003773 N0.getNode()->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003774 return DAG.getNode(ISD::FADD, VT, N0.getOperand(0),
3775 DAG.getNode(ISD::FADD, VT, N0.getOperand(1), N1));
3776
Dan Gohman475871a2008-07-27 21:46:04 +00003777 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00003778}
3779
Dan Gohman475871a2008-07-27 21:46:04 +00003780SDValue DAGCombiner::visitFSUB(SDNode *N) {
3781 SDValue N0 = N->getOperand(0);
3782 SDValue N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003783 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3784 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003785 MVT VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00003786
Dan Gohman7f321562007-06-25 16:23:39 +00003787 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003788 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003789 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003790 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00003791 }
Dan Gohman7f321562007-06-25 16:23:39 +00003792
Nate Begemana0e221d2005-10-18 00:28:13 +00003793 // fold (fsub c1, c2) -> c1-c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003794 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003795 return DAG.getNode(ISD::FSUB, VT, N0, N1);
Dan Gohman23ff1822007-07-02 15:48:56 +00003796 // fold (0-B) -> -B
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003797 if (UnsafeFPMath && N0CFP && N0CFP->getValueAPF().isZero()) {
Chris Lattner0254e702008-02-26 07:04:54 +00003798 if (isNegatibleForFree(N1, AfterLegalize))
3799 return GetNegatedExpression(N1, DAG, AfterLegalize);
Dan Gohman23ff1822007-07-02 15:48:56 +00003800 return DAG.getNode(ISD::FNEG, VT, N1);
3801 }
Chris Lattner01b3d732005-09-28 22:28:18 +00003802 // fold (A-(-B)) -> A+B
Chris Lattner0254e702008-02-26 07:04:54 +00003803 if (isNegatibleForFree(N1, AfterLegalize))
3804 return DAG.getNode(ISD::FADD, VT, N0,
3805 GetNegatedExpression(N1, DAG, AfterLegalize));
Chris Lattner29446522007-05-14 22:04:50 +00003806
Dan Gohman475871a2008-07-27 21:46:04 +00003807 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00003808}
3809
Dan Gohman475871a2008-07-27 21:46:04 +00003810SDValue DAGCombiner::visitFMUL(SDNode *N) {
3811 SDValue N0 = N->getOperand(0);
3812 SDValue N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003813 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3814 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003815 MVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00003816
Dan Gohman7f321562007-06-25 16:23:39 +00003817 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003818 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003819 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003820 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00003821 }
Dan Gohman7f321562007-06-25 16:23:39 +00003822
Nate Begeman11af4ea2005-10-17 20:40:11 +00003823 // fold (fmul c1, c2) -> c1*c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003824 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003825 return DAG.getNode(ISD::FMUL, VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003826 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00003827 if (N0CFP && !N1CFP)
3828 return DAG.getNode(ISD::FMUL, VT, N1, N0);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003829 // fold (fmul X, 2.0) -> (fadd X, X)
3830 if (N1CFP && N1CFP->isExactlyValue(+2.0))
3831 return DAG.getNode(ISD::FADD, VT, N0, N0);
Chris Lattner29446522007-05-14 22:04:50 +00003832 // fold (fmul X, -1.0) -> (fneg X)
3833 if (N1CFP && N1CFP->isExactlyValue(-1.0))
3834 return DAG.getNode(ISD::FNEG, VT, N0);
3835
3836 // -X * -Y -> X*Y
Chris Lattner0254e702008-02-26 07:04:54 +00003837 if (char LHSNeg = isNegatibleForFree(N0, AfterLegalize)) {
3838 if (char RHSNeg = isNegatibleForFree(N1, AfterLegalize)) {
Chris Lattner29446522007-05-14 22:04:50 +00003839 // Both can be negated for free, check to see if at least one is cheaper
3840 // negated.
3841 if (LHSNeg == 2 || RHSNeg == 2)
Chris Lattner0254e702008-02-26 07:04:54 +00003842 return DAG.getNode(ISD::FMUL, VT,
3843 GetNegatedExpression(N0, DAG, AfterLegalize),
3844 GetNegatedExpression(N1, DAG, AfterLegalize));
Chris Lattner29446522007-05-14 22:04:50 +00003845 }
3846 }
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003847
3848 // If allowed, fold (fmul (fmul x, c1), c2) -> (fmul x, (fmul c1, c2))
3849 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FMUL &&
Gabor Greifba36cb52008-08-28 21:40:38 +00003850 N0.getNode()->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003851 return DAG.getNode(ISD::FMUL, VT, N0.getOperand(0),
3852 DAG.getNode(ISD::FMUL, VT, N0.getOperand(1), N1));
3853
Dan Gohman475871a2008-07-27 21:46:04 +00003854 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00003855}
3856
Dan Gohman475871a2008-07-27 21:46:04 +00003857SDValue DAGCombiner::visitFDIV(SDNode *N) {
3858 SDValue N0 = N->getOperand(0);
3859 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003860 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3861 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003862 MVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00003863
Dan Gohman7f321562007-06-25 16:23:39 +00003864 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003865 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003866 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003867 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00003868 }
Dan Gohman7f321562007-06-25 16:23:39 +00003869
Nate Begemana148d982006-01-18 22:35:16 +00003870 // fold (fdiv c1, c2) -> c1/c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003871 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003872 return DAG.getNode(ISD::FDIV, VT, N0, N1);
Chris Lattner29446522007-05-14 22:04:50 +00003873
3874
3875 // -X / -Y -> X*Y
Chris Lattner0254e702008-02-26 07:04:54 +00003876 if (char LHSNeg = isNegatibleForFree(N0, AfterLegalize)) {
3877 if (char RHSNeg = isNegatibleForFree(N1, AfterLegalize)) {
Chris Lattner29446522007-05-14 22:04:50 +00003878 // Both can be negated for free, check to see if at least one is cheaper
3879 // negated.
3880 if (LHSNeg == 2 || RHSNeg == 2)
Chris Lattner0254e702008-02-26 07:04:54 +00003881 return DAG.getNode(ISD::FDIV, VT,
3882 GetNegatedExpression(N0, DAG, AfterLegalize),
3883 GetNegatedExpression(N1, DAG, AfterLegalize));
Chris Lattner29446522007-05-14 22:04:50 +00003884 }
3885 }
3886
Dan Gohman475871a2008-07-27 21:46:04 +00003887 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00003888}
3889
Dan Gohman475871a2008-07-27 21:46:04 +00003890SDValue DAGCombiner::visitFREM(SDNode *N) {
3891 SDValue N0 = N->getOperand(0);
3892 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003893 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3894 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003895 MVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00003896
Nate Begemana148d982006-01-18 22:35:16 +00003897 // fold (frem c1, c2) -> fmod(c1,c2)
Dale Johannesendb44bf82007-10-16 23:38:29 +00003898 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003899 return DAG.getNode(ISD::FREM, VT, N0, N1);
Dan Gohman7f321562007-06-25 16:23:39 +00003900
Dan Gohman475871a2008-07-27 21:46:04 +00003901 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00003902}
3903
Dan Gohman475871a2008-07-27 21:46:04 +00003904SDValue DAGCombiner::visitFCOPYSIGN(SDNode *N) {
3905 SDValue N0 = N->getOperand(0);
3906 SDValue N1 = N->getOperand(1);
Chris Lattner12d83032006-03-05 05:30:57 +00003907 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3908 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003909 MVT VT = N->getValueType(0);
Chris Lattner12d83032006-03-05 05:30:57 +00003910
Dale Johannesendb44bf82007-10-16 23:38:29 +00003911 if (N0CFP && N1CFP && VT != MVT::ppcf128) // Constant fold
Chris Lattner12d83032006-03-05 05:30:57 +00003912 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1);
3913
3914 if (N1CFP) {
Dale Johannesene6c17422007-08-26 01:18:27 +00003915 const APFloat& V = N1CFP->getValueAPF();
Chris Lattner12d83032006-03-05 05:30:57 +00003916 // copysign(x, c1) -> fabs(x) iff ispos(c1)
3917 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
Dale Johannesen87503a62007-08-25 22:10:57 +00003918 if (!V.isNegative())
Chris Lattner12d83032006-03-05 05:30:57 +00003919 return DAG.getNode(ISD::FABS, VT, N0);
3920 else
3921 return DAG.getNode(ISD::FNEG, VT, DAG.getNode(ISD::FABS, VT, N0));
3922 }
3923
3924 // copysign(fabs(x), y) -> copysign(x, y)
3925 // copysign(fneg(x), y) -> copysign(x, y)
3926 // copysign(copysign(x,z), y) -> copysign(x, y)
3927 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
3928 N0.getOpcode() == ISD::FCOPYSIGN)
3929 return DAG.getNode(ISD::FCOPYSIGN, VT, N0.getOperand(0), N1);
3930
3931 // copysign(x, abs(y)) -> abs(x)
3932 if (N1.getOpcode() == ISD::FABS)
3933 return DAG.getNode(ISD::FABS, VT, N0);
3934
3935 // copysign(x, copysign(y,z)) -> copysign(x, z)
3936 if (N1.getOpcode() == ISD::FCOPYSIGN)
3937 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(1));
3938
3939 // copysign(x, fp_extend(y)) -> copysign(x, y)
3940 // copysign(x, fp_round(y)) -> copysign(x, y)
3941 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
3942 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(0));
3943
Dan Gohman475871a2008-07-27 21:46:04 +00003944 return SDValue();
Chris Lattner12d83032006-03-05 05:30:57 +00003945}
3946
3947
Chris Lattner01b3d732005-09-28 22:28:18 +00003948
Dan Gohman475871a2008-07-27 21:46:04 +00003949SDValue DAGCombiner::visitSINT_TO_FP(SDNode *N) {
3950 SDValue N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003951 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003952 MVT VT = N->getValueType(0);
Chris Lattnercda88752008-06-26 00:16:49 +00003953 MVT OpVT = N0.getValueType();
3954
Nate Begeman1d4d4142005-09-01 00:19:25 +00003955 // fold (sint_to_fp c1) -> c1fp
Chris Lattnercda88752008-06-26 00:16:49 +00003956 if (N0C && OpVT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003957 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
Chris Lattnercda88752008-06-26 00:16:49 +00003958
3959 // If the input is a legal type, and SINT_TO_FP is not legal on this target,
3960 // but UINT_TO_FP is legal on this target, try to convert.
Chris Lattnerf77e46b2008-06-26 17:16:00 +00003961 if (!TLI.isOperationLegal(ISD::SINT_TO_FP, OpVT) &&
Chris Lattnercda88752008-06-26 00:16:49 +00003962 TLI.isOperationLegal(ISD::UINT_TO_FP, OpVT)) {
3963 // If the sign bit is known to be zero, we can change this to UINT_TO_FP.
3964 if (DAG.SignBitIsZero(N0))
3965 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
3966 }
3967
3968
Dan Gohman475871a2008-07-27 21:46:04 +00003969 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003970}
3971
Dan Gohman475871a2008-07-27 21:46:04 +00003972SDValue DAGCombiner::visitUINT_TO_FP(SDNode *N) {
3973 SDValue N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003974 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003975 MVT VT = N->getValueType(0);
Chris Lattnercda88752008-06-26 00:16:49 +00003976 MVT OpVT = N0.getValueType();
Nate Begemana148d982006-01-18 22:35:16 +00003977
Nate Begeman1d4d4142005-09-01 00:19:25 +00003978 // fold (uint_to_fp c1) -> c1fp
Chris Lattnercda88752008-06-26 00:16:49 +00003979 if (N0C && OpVT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003980 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
Chris Lattnercda88752008-06-26 00:16:49 +00003981
3982 // If the input is a legal type, and UINT_TO_FP is not legal on this target,
3983 // but SINT_TO_FP is legal on this target, try to convert.
Chris Lattnerf77e46b2008-06-26 17:16:00 +00003984 if (!TLI.isOperationLegal(ISD::UINT_TO_FP, OpVT) &&
Chris Lattnercda88752008-06-26 00:16:49 +00003985 TLI.isOperationLegal(ISD::SINT_TO_FP, OpVT)) {
3986 // If the sign bit is known to be zero, we can change this to SINT_TO_FP.
3987 if (DAG.SignBitIsZero(N0))
3988 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
3989 }
3990
Dan Gohman475871a2008-07-27 21:46:04 +00003991 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003992}
3993
Dan Gohman475871a2008-07-27 21:46:04 +00003994SDValue DAGCombiner::visitFP_TO_SINT(SDNode *N) {
3995 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00003996 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003997 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003998
3999 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00004000 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00004001 return DAG.getNode(ISD::FP_TO_SINT, VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00004002 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004003}
4004
Dan Gohman475871a2008-07-27 21:46:04 +00004005SDValue DAGCombiner::visitFP_TO_UINT(SDNode *N) {
4006 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00004007 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004008 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004009
4010 // fold (fp_to_uint c1fp) -> c1
Dale Johannesendb44bf82007-10-16 23:38:29 +00004011 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00004012 return DAG.getNode(ISD::FP_TO_UINT, VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00004013 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004014}
4015
Dan Gohman475871a2008-07-27 21:46:04 +00004016SDValue DAGCombiner::visitFP_ROUND(SDNode *N) {
4017 SDValue N0 = N->getOperand(0);
4018 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00004019 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004020 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004021
4022 // fold (fp_round c1fp) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00004023 if (N0CFP && N0.getValueType() != MVT::ppcf128)
Chris Lattner0bd48932008-01-17 07:00:52 +00004024 return DAG.getNode(ISD::FP_ROUND, VT, N0, N1);
Chris Lattner79dbea52006-03-13 06:26:26 +00004025
4026 // fold (fp_round (fp_extend x)) -> x
4027 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
4028 return N0.getOperand(0);
4029
Chris Lattner0aa5e6f2008-01-24 06:45:35 +00004030 // fold (fp_round (fp_round x)) -> (fp_round x)
4031 if (N0.getOpcode() == ISD::FP_ROUND) {
4032 // This is a value preserving truncation if both round's are.
4033 bool IsTrunc = N->getConstantOperandVal(1) == 1 &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004034 N0.getNode()->getConstantOperandVal(1) == 1;
Chris Lattner0aa5e6f2008-01-24 06:45:35 +00004035 return DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0),
4036 DAG.getIntPtrConstant(IsTrunc));
4037 }
4038
Chris Lattner79dbea52006-03-13 06:26:26 +00004039 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
Gabor Greifba36cb52008-08-28 21:40:38 +00004040 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004041 SDValue Tmp = DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0), N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00004042 AddToWorkList(Tmp.getNode());
Chris Lattner79dbea52006-03-13 06:26:26 +00004043 return DAG.getNode(ISD::FCOPYSIGN, VT, Tmp, N0.getOperand(1));
4044 }
4045
Dan Gohman475871a2008-07-27 21:46:04 +00004046 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004047}
4048
Dan Gohman475871a2008-07-27 21:46:04 +00004049SDValue DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
4050 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004051 MVT VT = N->getValueType(0);
4052 MVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00004053 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004054
Nate Begeman1d4d4142005-09-01 00:19:25 +00004055 // fold (fp_round_inreg c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00004056 if (N0CFP) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00004057 SDValue Round = DAG.getConstantFP(*N0CFP->getConstantFPValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00004058 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004059 }
Dan Gohman475871a2008-07-27 21:46:04 +00004060 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004061}
4062
Dan Gohman475871a2008-07-27 21:46:04 +00004063SDValue DAGCombiner::visitFP_EXTEND(SDNode *N) {
4064 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00004065 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004066 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004067
Chris Lattner5938bef2007-12-29 06:55:23 +00004068 // If this is fp_round(fpextend), don't fold it, allow ourselves to be folded.
Roman Levenstein9cac5252008-04-16 16:15:27 +00004069 if (N->hasOneUse() &&
Dan Gohman475871a2008-07-27 21:46:04 +00004070 N->use_begin().getUse().getSDValue().getOpcode() == ISD::FP_ROUND)
4071 return SDValue();
Chris Lattner0bd48932008-01-17 07:00:52 +00004072
Nate Begeman1d4d4142005-09-01 00:19:25 +00004073 // fold (fp_extend c1fp) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00004074 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00004075 return DAG.getNode(ISD::FP_EXTEND, VT, N0);
Chris Lattner0bd48932008-01-17 07:00:52 +00004076
4077 // Turn fp_extend(fp_round(X, 1)) -> x since the fp_round doesn't affect the
4078 // value of X.
Gabor Greif12632d22008-08-30 19:29:20 +00004079 if (N0.getOpcode() == ISD::FP_ROUND
4080 && N0.getNode()->getConstantOperandVal(1) == 1) {
Dan Gohman475871a2008-07-27 21:46:04 +00004081 SDValue In = N0.getOperand(0);
Chris Lattner0bd48932008-01-17 07:00:52 +00004082 if (In.getValueType() == VT) return In;
Duncan Sands8e4eb092008-06-08 20:54:56 +00004083 if (VT.bitsLT(In.getValueType()))
Chris Lattner0bd48932008-01-17 07:00:52 +00004084 return DAG.getNode(ISD::FP_ROUND, VT, In, N0.getOperand(1));
4085 return DAG.getNode(ISD::FP_EXTEND, VT, In);
4086 }
4087
4088 // fold (fpext (load x)) -> (fpext (fptrunc (extload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00004089 if (ISD::isNON_EXTLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004090 ((!AfterLegalize && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00004091 TLI.isLoadExtLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00004092 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman475871a2008-07-27 21:46:04 +00004093 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
Evan Cheng466685d2006-10-09 20:57:25 +00004094 LN0->getBasePtr(), LN0->getSrcValue(),
4095 LN0->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00004096 N0.getValueType(),
4097 LN0->isVolatile(),
4098 LN0->getAlignment());
Chris Lattnere564dbb2006-05-05 21:34:35 +00004099 CombineTo(N, ExtLoad);
Gabor Greif12632d22008-08-30 19:29:20 +00004100 CombineTo(N0.getNode(), DAG.getNode(ISD::FP_ROUND, N0.getValueType(),
4101 ExtLoad, DAG.getIntPtrConstant(1)),
Chris Lattnere564dbb2006-05-05 21:34:35 +00004102 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004103 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnere564dbb2006-05-05 21:34:35 +00004104 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004105
Dan Gohman475871a2008-07-27 21:46:04 +00004106 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004107}
4108
Dan Gohman475871a2008-07-27 21:46:04 +00004109SDValue DAGCombiner::visitFNEG(SDNode *N) {
4110 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00004111
Chris Lattner0254e702008-02-26 07:04:54 +00004112 if (isNegatibleForFree(N0, AfterLegalize))
4113 return GetNegatedExpression(N0, DAG, AfterLegalize);
Dan Gohman23ff1822007-07-02 15:48:56 +00004114
Chris Lattner3bd39d42008-01-27 17:42:27 +00004115 // Transform fneg(bitconvert(x)) -> bitconvert(x^sign) to avoid loading
4116 // constant pool values.
Gabor Greifba36cb52008-08-28 21:40:38 +00004117 if (N0.getOpcode() == ISD::BIT_CONVERT && N0.getNode()->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00004118 N0.getOperand(0).getValueType().isInteger() &&
4119 !N0.getOperand(0).getValueType().isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004120 SDValue Int = N0.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004121 MVT IntVT = Int.getValueType();
4122 if (IntVT.isInteger() && !IntVT.isVector()) {
Chris Lattner3bd39d42008-01-27 17:42:27 +00004123 Int = DAG.getNode(ISD::XOR, IntVT, Int,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004124 DAG.getConstant(IntVT.getIntegerVTSignBit(), IntVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00004125 AddToWorkList(Int.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00004126 return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Int);
4127 }
4128 }
4129
Dan Gohman475871a2008-07-27 21:46:04 +00004130 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004131}
4132
Dan Gohman475871a2008-07-27 21:46:04 +00004133SDValue DAGCombiner::visitFABS(SDNode *N) {
4134 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00004135 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004136 MVT VT = N->getValueType(0);
Nate Begemana148d982006-01-18 22:35:16 +00004137
Nate Begeman1d4d4142005-09-01 00:19:25 +00004138 // fold (fabs c1) -> fabs(c1)
Dale Johannesendb44bf82007-10-16 23:38:29 +00004139 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00004140 return DAG.getNode(ISD::FABS, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004141 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00004142 if (N0.getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00004143 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004144 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00004145 // fold (fabs (fcopysign x, y)) -> (fabs x)
4146 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
4147 return DAG.getNode(ISD::FABS, VT, N0.getOperand(0));
4148
Chris Lattner3bd39d42008-01-27 17:42:27 +00004149 // Transform fabs(bitconvert(x)) -> bitconvert(x&~sign) to avoid loading
4150 // constant pool values.
Gabor Greifba36cb52008-08-28 21:40:38 +00004151 if (N0.getOpcode() == ISD::BIT_CONVERT && N0.getNode()->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00004152 N0.getOperand(0).getValueType().isInteger() &&
4153 !N0.getOperand(0).getValueType().isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004154 SDValue Int = N0.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004155 MVT IntVT = Int.getValueType();
4156 if (IntVT.isInteger() && !IntVT.isVector()) {
Chris Lattner3bd39d42008-01-27 17:42:27 +00004157 Int = DAG.getNode(ISD::AND, IntVT, Int,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004158 DAG.getConstant(~IntVT.getIntegerVTSignBit(), IntVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00004159 AddToWorkList(Int.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00004160 return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Int);
4161 }
4162 }
4163
Dan Gohman475871a2008-07-27 21:46:04 +00004164 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004165}
4166
Dan Gohman475871a2008-07-27 21:46:04 +00004167SDValue DAGCombiner::visitBRCOND(SDNode *N) {
4168 SDValue Chain = N->getOperand(0);
4169 SDValue N1 = N->getOperand(1);
4170 SDValue N2 = N->getOperand(2);
Nate Begeman44728a72005-09-19 22:34:01 +00004171 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
4172
4173 // never taken branch, fold to chain
4174 if (N1C && N1C->isNullValue())
4175 return Chain;
4176 // unconditional branch
Dan Gohman002e5d02008-03-13 22:13:53 +00004177 if (N1C && N1C->getAPIntValue() == 1)
Nate Begeman44728a72005-09-19 22:34:01 +00004178 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
Nate Begeman750ac1b2006-02-01 07:19:44 +00004179 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
4180 // on the target.
4181 if (N1.getOpcode() == ISD::SETCC &&
4182 TLI.isOperationLegal(ISD::BR_CC, MVT::Other)) {
4183 return DAG.getNode(ISD::BR_CC, MVT::Other, Chain, N1.getOperand(2),
4184 N1.getOperand(0), N1.getOperand(1), N2);
4185 }
Dan Gohman475871a2008-07-27 21:46:04 +00004186 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00004187}
4188
Chris Lattner3ea0b472005-10-05 06:47:48 +00004189// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
4190//
Dan Gohman475871a2008-07-27 21:46:04 +00004191SDValue DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00004192 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004193 SDValue CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
Chris Lattner3ea0b472005-10-05 06:47:48 +00004194
Duncan Sands8eab8a22008-06-09 11:32:28 +00004195 // Use SimplifySetCC to simplify SETCC's.
Dan Gohman475871a2008-07-27 21:46:04 +00004196 SDValue Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false);
Gabor Greifba36cb52008-08-28 21:40:38 +00004197 if (Simp.getNode()) AddToWorkList(Simp.getNode());
Chris Lattner30f73e72006-10-14 03:52:46 +00004198
Gabor Greifba36cb52008-08-28 21:40:38 +00004199 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.getNode());
Nate Begemane17daeb2005-10-05 21:43:42 +00004200
4201 // fold br_cc true, dest -> br dest (unconditional branch)
Dan Gohman002e5d02008-03-13 22:13:53 +00004202 if (SCCC && !SCCC->isNullValue())
Nate Begemane17daeb2005-10-05 21:43:42 +00004203 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
4204 N->getOperand(4));
4205 // fold br_cc false, dest -> unconditional fall through
4206 if (SCCC && SCCC->isNullValue())
4207 return N->getOperand(0);
Chris Lattner30f73e72006-10-14 03:52:46 +00004208
Nate Begemane17daeb2005-10-05 21:43:42 +00004209 // fold to a simpler setcc
Gabor Greifba36cb52008-08-28 21:40:38 +00004210 if (Simp.getNode() && Simp.getOpcode() == ISD::SETCC)
Nate Begemane17daeb2005-10-05 21:43:42 +00004211 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
4212 Simp.getOperand(2), Simp.getOperand(0),
4213 Simp.getOperand(1), N->getOperand(4));
Dan Gohman475871a2008-07-27 21:46:04 +00004214 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00004215}
4216
Chris Lattner448f2192006-11-11 00:39:41 +00004217
Duncan Sandsec87aa82008-06-15 20:12:31 +00004218/// CombineToPreIndexedLoadStore - Try turning a load / store into a
4219/// pre-indexed load / store when the base pointer is an add or subtract
Chris Lattner448f2192006-11-11 00:39:41 +00004220/// and it has other uses besides the load / store. After the
4221/// transformation, the new indexed load / store has effectively folded
4222/// the add / subtract in and all of its other uses are redirected to the
4223/// new load / store.
4224bool DAGCombiner::CombineToPreIndexedLoadStore(SDNode *N) {
4225 if (!AfterLegalize)
4226 return false;
4227
4228 bool isLoad = true;
Dan Gohman475871a2008-07-27 21:46:04 +00004229 SDValue Ptr;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004230 MVT VT;
Chris Lattner448f2192006-11-11 00:39:41 +00004231 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004232 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004233 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004234 VT = LD->getMemoryVT();
Evan Cheng83060c52007-03-07 08:07:03 +00004235 if (!TLI.isIndexedLoadLegal(ISD::PRE_INC, VT) &&
Chris Lattner448f2192006-11-11 00:39:41 +00004236 !TLI.isIndexedLoadLegal(ISD::PRE_DEC, VT))
4237 return false;
4238 Ptr = LD->getBasePtr();
4239 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004240 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004241 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004242 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00004243 if (!TLI.isIndexedStoreLegal(ISD::PRE_INC, VT) &&
4244 !TLI.isIndexedStoreLegal(ISD::PRE_DEC, VT))
4245 return false;
4246 Ptr = ST->getBasePtr();
4247 isLoad = false;
4248 } else
4249 return false;
4250
Chris Lattner9f1794e2006-11-11 00:56:29 +00004251 // If the pointer is not an add/sub, or if it doesn't have multiple uses, bail
4252 // out. There is no reason to make this a preinc/predec.
4253 if ((Ptr.getOpcode() != ISD::ADD && Ptr.getOpcode() != ISD::SUB) ||
Gabor Greifba36cb52008-08-28 21:40:38 +00004254 Ptr.getNode()->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00004255 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00004256
Chris Lattner9f1794e2006-11-11 00:56:29 +00004257 // Ask the target to do addressing mode selection.
Dan Gohman475871a2008-07-27 21:46:04 +00004258 SDValue BasePtr;
4259 SDValue Offset;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004260 ISD::MemIndexedMode AM = ISD::UNINDEXED;
4261 if (!TLI.getPreIndexedAddressParts(N, BasePtr, Offset, AM, DAG))
4262 return false;
Evan Chenga7d4a042007-05-03 23:52:19 +00004263 // Don't create a indexed load / store with zero offset.
4264 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00004265 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Chenga7d4a042007-05-03 23:52:19 +00004266 return false;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004267
Chris Lattner41e53fd2006-11-11 01:00:15 +00004268 // Try turning it into a pre-indexed load / store except when:
Evan Chengc843abe2007-05-24 02:35:39 +00004269 // 1) The new base ptr is a frame index.
4270 // 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 +00004271 // predecessor of the value being stored.
Evan Chengc843abe2007-05-24 02:35:39 +00004272 // 3) Another use of old base ptr is a predecessor of N. If ptr is folded
Chris Lattner9f1794e2006-11-11 00:56:29 +00004273 // that would create a cycle.
Evan Chengc843abe2007-05-24 02:35:39 +00004274 // 4) All uses are load / store ops that use it as old base ptr.
Chris Lattner448f2192006-11-11 00:39:41 +00004275
Chris Lattner41e53fd2006-11-11 01:00:15 +00004276 // Check #1. Preinc'ing a frame index would require copying the stack pointer
4277 // (plus the implicit offset) to a register to preinc anyway.
4278 if (isa<FrameIndexSDNode>(BasePtr))
4279 return false;
4280
4281 // Check #2.
Chris Lattner9f1794e2006-11-11 00:56:29 +00004282 if (!isLoad) {
Dan Gohman475871a2008-07-27 21:46:04 +00004283 SDValue Val = cast<StoreSDNode>(N)->getValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00004284 if (Val == BasePtr || BasePtr.getNode()->isPredecessorOf(Val.getNode()))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004285 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00004286 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004287
Evan Chengc843abe2007-05-24 02:35:39 +00004288 // Now check for #3 and #4.
Chris Lattner9f1794e2006-11-11 00:56:29 +00004289 bool RealUse = false;
Gabor Greifba36cb52008-08-28 21:40:38 +00004290 for (SDNode::use_iterator I = Ptr.getNode()->use_begin(),
4291 E = Ptr.getNode()->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00004292 SDNode *Use = *I;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004293 if (Use == N)
4294 continue;
Evan Cheng917be682008-03-04 00:41:45 +00004295 if (Use->isPredecessorOf(N))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004296 return false;
4297
4298 if (!((Use->getOpcode() == ISD::LOAD &&
4299 cast<LoadSDNode>(Use)->getBasePtr() == Ptr) ||
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00004300 (Use->getOpcode() == ISD::STORE &&
4301 cast<StoreSDNode>(Use)->getBasePtr() == Ptr)))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004302 RealUse = true;
4303 }
4304 if (!RealUse)
4305 return false;
4306
Dan Gohman475871a2008-07-27 21:46:04 +00004307 SDValue Result;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004308 if (isLoad)
Dan Gohman475871a2008-07-27 21:46:04 +00004309 Result = DAG.getIndexedLoad(SDValue(N,0), BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004310 else
Dan Gohman475871a2008-07-27 21:46:04 +00004311 Result = DAG.getIndexedStore(SDValue(N,0), BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004312 ++PreIndexedNodes;
4313 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +00004314 DOUT << "\nReplacing.4 "; DEBUG(N->dump(&DAG));
Gabor Greifba36cb52008-08-28 21:40:38 +00004315 DOUT << "\nWith: "; DEBUG(Result.getNode()->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +00004316 DOUT << '\n';
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004317 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004318 if (isLoad) {
Dan Gohman475871a2008-07-27 21:46:04 +00004319 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004320 &DeadNodes);
Dan Gohman475871a2008-07-27 21:46:04 +00004321 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004322 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004323 } else {
Dan Gohman475871a2008-07-27 21:46:04 +00004324 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004325 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004326 }
4327
Chris Lattner9f1794e2006-11-11 00:56:29 +00004328 // Finally, since the node is now dead, remove it from the graph.
4329 DAG.DeleteNode(N);
4330
4331 // Replace the uses of Ptr with uses of the updated base value.
4332 DAG.ReplaceAllUsesOfValueWith(Ptr, Result.getValue(isLoad ? 1 : 0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004333 &DeadNodes);
Gabor Greifba36cb52008-08-28 21:40:38 +00004334 removeFromWorkList(Ptr.getNode());
4335 DAG.DeleteNode(Ptr.getNode());
Chris Lattner9f1794e2006-11-11 00:56:29 +00004336
4337 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00004338}
4339
Duncan Sandsec87aa82008-06-15 20:12:31 +00004340/// CombineToPostIndexedLoadStore - Try to combine a load / store with a
Chris Lattner448f2192006-11-11 00:39:41 +00004341/// add / sub of the base pointer node into a post-indexed load / store.
4342/// The transformation folded the add / subtract into the new indexed
4343/// load / store effectively and all of its uses are redirected to the
4344/// new load / store.
4345bool DAGCombiner::CombineToPostIndexedLoadStore(SDNode *N) {
4346 if (!AfterLegalize)
4347 return false;
4348
4349 bool isLoad = true;
Dan Gohman475871a2008-07-27 21:46:04 +00004350 SDValue Ptr;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004351 MVT VT;
Chris Lattner448f2192006-11-11 00:39:41 +00004352 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004353 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004354 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004355 VT = LD->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00004356 if (!TLI.isIndexedLoadLegal(ISD::POST_INC, VT) &&
4357 !TLI.isIndexedLoadLegal(ISD::POST_DEC, VT))
4358 return false;
4359 Ptr = LD->getBasePtr();
4360 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004361 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004362 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004363 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00004364 if (!TLI.isIndexedStoreLegal(ISD::POST_INC, VT) &&
4365 !TLI.isIndexedStoreLegal(ISD::POST_DEC, VT))
4366 return false;
4367 Ptr = ST->getBasePtr();
4368 isLoad = false;
4369 } else
4370 return false;
4371
Gabor Greifba36cb52008-08-28 21:40:38 +00004372 if (Ptr.getNode()->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00004373 return false;
4374
Gabor Greifba36cb52008-08-28 21:40:38 +00004375 for (SDNode::use_iterator I = Ptr.getNode()->use_begin(),
4376 E = Ptr.getNode()->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00004377 SDNode *Op = *I;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004378 if (Op == N ||
4379 (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB))
4380 continue;
4381
Dan Gohman475871a2008-07-27 21:46:04 +00004382 SDValue BasePtr;
4383 SDValue Offset;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004384 ISD::MemIndexedMode AM = ISD::UNINDEXED;
4385 if (TLI.getPostIndexedAddressParts(N, Op, BasePtr, Offset, AM, DAG)) {
4386 if (Ptr == Offset)
4387 std::swap(BasePtr, Offset);
4388 if (Ptr != BasePtr)
Chris Lattner448f2192006-11-11 00:39:41 +00004389 continue;
Evan Chenga7d4a042007-05-03 23:52:19 +00004390 // Don't create a indexed load / store with zero offset.
4391 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00004392 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Chenga7d4a042007-05-03 23:52:19 +00004393 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00004394
Chris Lattner9f1794e2006-11-11 00:56:29 +00004395 // Try turning it into a post-indexed load / store except when
4396 // 1) All uses are load / store ops that use it as base ptr.
4397 // 2) Op must be independent of N, i.e. Op is neither a predecessor
4398 // nor a successor of N. Otherwise, if Op is folded that would
4399 // create a cycle.
4400
4401 // Check for #1.
4402 bool TryNext = false;
Gabor Greifba36cb52008-08-28 21:40:38 +00004403 for (SDNode::use_iterator II = BasePtr.getNode()->use_begin(),
4404 EE = BasePtr.getNode()->use_end(); II != EE; ++II) {
Dan Gohman89684502008-07-27 20:43:25 +00004405 SDNode *Use = *II;
Gabor Greifba36cb52008-08-28 21:40:38 +00004406 if (Use == Ptr.getNode())
Chris Lattner448f2192006-11-11 00:39:41 +00004407 continue;
4408
Chris Lattner9f1794e2006-11-11 00:56:29 +00004409 // If all the uses are load / store addresses, then don't do the
4410 // transformation.
4411 if (Use->getOpcode() == ISD::ADD || Use->getOpcode() == ISD::SUB){
4412 bool RealUse = false;
4413 for (SDNode::use_iterator III = Use->use_begin(),
4414 EEE = Use->use_end(); III != EEE; ++III) {
Dan Gohman89684502008-07-27 20:43:25 +00004415 SDNode *UseUse = *III;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004416 if (!((UseUse->getOpcode() == ISD::LOAD &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004417 cast<LoadSDNode>(UseUse)->getBasePtr().getNode() == Use) ||
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00004418 (UseUse->getOpcode() == ISD::STORE &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004419 cast<StoreSDNode>(UseUse)->getBasePtr().getNode() == Use)))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004420 RealUse = true;
4421 }
Chris Lattner448f2192006-11-11 00:39:41 +00004422
Chris Lattner9f1794e2006-11-11 00:56:29 +00004423 if (!RealUse) {
4424 TryNext = true;
4425 break;
Chris Lattner448f2192006-11-11 00:39:41 +00004426 }
4427 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004428 }
4429 if (TryNext)
4430 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00004431
Chris Lattner9f1794e2006-11-11 00:56:29 +00004432 // Check for #2
Evan Cheng917be682008-03-04 00:41:45 +00004433 if (!Op->isPredecessorOf(N) && !N->isPredecessorOf(Op)) {
Dan Gohman475871a2008-07-27 21:46:04 +00004434 SDValue Result = isLoad
4435 ? DAG.getIndexedLoad(SDValue(N,0), BasePtr, Offset, AM)
4436 : DAG.getIndexedStore(SDValue(N,0), BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004437 ++PostIndexedNodes;
4438 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +00004439 DOUT << "\nReplacing.5 "; DEBUG(N->dump(&DAG));
Gabor Greifba36cb52008-08-28 21:40:38 +00004440 DOUT << "\nWith: "; DEBUG(Result.getNode()->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +00004441 DOUT << '\n';
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004442 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004443 if (isLoad) {
Dan Gohman475871a2008-07-27 21:46:04 +00004444 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004445 &DeadNodes);
Dan Gohman475871a2008-07-27 21:46:04 +00004446 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004447 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004448 } else {
Dan Gohman475871a2008-07-27 21:46:04 +00004449 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004450 &DeadNodes);
Chris Lattner448f2192006-11-11 00:39:41 +00004451 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004452
Chris Lattner9f1794e2006-11-11 00:56:29 +00004453 // Finally, since the node is now dead, remove it from the graph.
4454 DAG.DeleteNode(N);
4455
4456 // Replace the uses of Use with uses of the updated base value.
Dan Gohman475871a2008-07-27 21:46:04 +00004457 DAG.ReplaceAllUsesOfValueWith(SDValue(Op, 0),
Chris Lattner9f1794e2006-11-11 00:56:29 +00004458 Result.getValue(isLoad ? 1 : 0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004459 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004460 removeFromWorkList(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004461 DAG.DeleteNode(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004462 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00004463 }
4464 }
4465 }
4466 return false;
4467}
4468
Chris Lattner00161a62008-01-25 07:20:16 +00004469/// InferAlignment - If we can infer some alignment information from this
4470/// pointer, return it.
Dan Gohman475871a2008-07-27 21:46:04 +00004471static unsigned InferAlignment(SDValue Ptr, SelectionDAG &DAG) {
Chris Lattner00161a62008-01-25 07:20:16 +00004472 // If this is a direct reference to a stack slot, use information about the
4473 // stack slot's alignment.
Chris Lattner1329cb82008-01-26 19:45:50 +00004474 int FrameIdx = 1 << 31;
4475 int64_t FrameOffset = 0;
Chris Lattner00161a62008-01-25 07:20:16 +00004476 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
Chris Lattner1329cb82008-01-26 19:45:50 +00004477 FrameIdx = FI->getIndex();
4478 } else if (Ptr.getOpcode() == ISD::ADD &&
4479 isa<ConstantSDNode>(Ptr.getOperand(1)) &&
4480 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4481 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4482 FrameOffset = Ptr.getConstantOperandVal(1);
Chris Lattner00161a62008-01-25 07:20:16 +00004483 }
Chris Lattner1329cb82008-01-26 19:45:50 +00004484
4485 if (FrameIdx != (1 << 31)) {
4486 // FIXME: Handle FI+CST.
4487 const MachineFrameInfo &MFI = *DAG.getMachineFunction().getFrameInfo();
4488 if (MFI.isFixedObjectIndex(FrameIdx)) {
Dan Gohman8cea8ff2008-08-11 18:27:03 +00004489 int64_t ObjectOffset = MFI.getObjectOffset(FrameIdx) + FrameOffset;
Chris Lattner1329cb82008-01-26 19:45:50 +00004490
4491 // The alignment of the frame index can be determined from its offset from
4492 // the incoming frame position. If the frame object is at offset 32 and
4493 // the stack is guaranteed to be 16-byte aligned, then we know that the
4494 // object is 16-byte aligned.
4495 unsigned StackAlign = DAG.getTarget().getFrameInfo()->getStackAlignment();
4496 unsigned Align = MinAlign(ObjectOffset, StackAlign);
4497
4498 // Finally, the frame object itself may have a known alignment. Factor
4499 // the alignment + offset into a new alignment. For example, if we know
4500 // the FI is 8 byte aligned, but the pointer is 4 off, we really have a
4501 // 4-byte alignment of the resultant pointer. Likewise align 4 + 4-byte
4502 // offset = 4-byte alignment, align 4 + 1-byte offset = align 1, etc.
4503 unsigned FIInfoAlign = MinAlign(MFI.getObjectAlignment(FrameIdx),
4504 FrameOffset);
4505 return std::max(Align, FIInfoAlign);
4506 }
4507 }
Chris Lattner00161a62008-01-25 07:20:16 +00004508
4509 return 0;
4510}
Chris Lattner448f2192006-11-11 00:39:41 +00004511
Dan Gohman475871a2008-07-27 21:46:04 +00004512SDValue DAGCombiner::visitLOAD(SDNode *N) {
Evan Cheng466685d2006-10-09 20:57:25 +00004513 LoadSDNode *LD = cast<LoadSDNode>(N);
Dan Gohman475871a2008-07-27 21:46:04 +00004514 SDValue Chain = LD->getChain();
4515 SDValue Ptr = LD->getBasePtr();
Chris Lattner00161a62008-01-25 07:20:16 +00004516
4517 // Try to infer better alignment information than the load already has.
Dan Gohmana2676512008-08-20 16:30:28 +00004518 if (!Fast && LD->isUnindexed()) {
Chris Lattner00161a62008-01-25 07:20:16 +00004519 if (unsigned Align = InferAlignment(Ptr, DAG)) {
4520 if (Align > LD->getAlignment())
4521 return DAG.getExtLoad(LD->getExtensionType(), LD->getValueType(0),
4522 Chain, Ptr, LD->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004523 LD->getSrcValueOffset(), LD->getMemoryVT(),
Chris Lattner00161a62008-01-25 07:20:16 +00004524 LD->isVolatile(), Align);
4525 }
4526 }
4527
Evan Cheng45a7ca92007-05-01 00:38:21 +00004528
4529 // If load is not volatile and there are no uses of the loaded value (and
4530 // the updated indexed value in case of indexed loads), change uses of the
4531 // chain value into uses of the chain input (i.e. delete the dead load).
4532 if (!LD->isVolatile()) {
Evan Cheng498f5592007-05-01 08:53:39 +00004533 if (N->getValueType(1) == MVT::Other) {
4534 // Unindexed loads.
Evan Cheng02c42852008-01-16 23:11:54 +00004535 if (N->hasNUsesOfValue(0, 0)) {
4536 // It's not safe to use the two value CombineTo variant here. e.g.
4537 // v1, chain2 = load chain1, loc
4538 // v2, chain3 = load chain2, loc
4539 // v3 = add v2, c
Chris Lattner125991a2008-01-24 07:57:06 +00004540 // Now we replace use of chain2 with chain1. This makes the second load
4541 // isomorphic to the one we are deleting, and thus makes this load live.
Evan Cheng02c42852008-01-16 23:11:54 +00004542 DOUT << "\nReplacing.6 "; DEBUG(N->dump(&DAG));
Gabor Greifba36cb52008-08-28 21:40:38 +00004543 DOUT << "\nWith chain: "; DEBUG(Chain.getNode()->dump(&DAG));
Chris Lattner125991a2008-01-24 07:57:06 +00004544 DOUT << "\n";
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004545 WorkListRemover DeadNodes(*this);
Dan Gohman475871a2008-07-27 21:46:04 +00004546 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Chain, &DeadNodes);
Chris Lattner125991a2008-01-24 07:57:06 +00004547 if (N->use_empty()) {
4548 removeFromWorkList(N);
4549 DAG.DeleteNode(N);
4550 }
Dan Gohman475871a2008-07-27 21:46:04 +00004551 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng02c42852008-01-16 23:11:54 +00004552 }
Evan Cheng498f5592007-05-01 08:53:39 +00004553 } else {
4554 // Indexed loads.
4555 assert(N->getValueType(2) == MVT::Other && "Malformed indexed loads?");
4556 if (N->hasNUsesOfValue(0, 0) && N->hasNUsesOfValue(0, 1)) {
Dan Gohman475871a2008-07-27 21:46:04 +00004557 SDValue Undef = DAG.getNode(ISD::UNDEF, N->getValueType(0));
Evan Cheng02c42852008-01-16 23:11:54 +00004558 DOUT << "\nReplacing.6 "; DEBUG(N->dump(&DAG));
Gabor Greifba36cb52008-08-28 21:40:38 +00004559 DOUT << "\nWith: "; DEBUG(Undef.getNode()->dump(&DAG));
Evan Cheng02c42852008-01-16 23:11:54 +00004560 DOUT << " and 2 other values\n";
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004561 WorkListRemover DeadNodes(*this);
Dan Gohman475871a2008-07-27 21:46:04 +00004562 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Undef, &DeadNodes);
4563 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1),
Chris Lattner4626b252008-01-17 07:20:38 +00004564 DAG.getNode(ISD::UNDEF, N->getValueType(1)),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004565 &DeadNodes);
Dan Gohman475871a2008-07-27 21:46:04 +00004566 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 2), Chain, &DeadNodes);
Evan Cheng02c42852008-01-16 23:11:54 +00004567 removeFromWorkList(N);
Evan Cheng02c42852008-01-16 23:11:54 +00004568 DAG.DeleteNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +00004569 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng45a7ca92007-05-01 00:38:21 +00004570 }
Evan Cheng45a7ca92007-05-01 00:38:21 +00004571 }
4572 }
Chris Lattner01a22022005-10-10 22:04:48 +00004573
4574 // If this load is directly stored, replace the load value with the stored
4575 // value.
4576 // TODO: Handle store large -> read small portion.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004577 // TODO: Handle TRUNCSTORE/LOADEXT
Dan Gohmanb061c4b2008-03-31 20:32:52 +00004578 if (LD->getExtensionType() == ISD::NON_EXTLOAD &&
4579 !LD->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004580 if (ISD::isNON_TRUNCStore(Chain.getNode())) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00004581 StoreSDNode *PrevST = cast<StoreSDNode>(Chain);
4582 if (PrevST->getBasePtr() == Ptr &&
4583 PrevST->getValue().getValueType() == N->getValueType(0))
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004584 return CombineTo(N, Chain.getOperand(1), Chain);
Evan Cheng8b2794a2006-10-13 21:14:26 +00004585 }
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004586 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00004587
Jim Laskey7ca56af2006-10-11 13:47:09 +00004588 if (CombinerAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00004589 // Walk up chain skipping non-aliasing memory nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00004590 SDValue BetterChain = FindBetterChain(N, Chain);
Jim Laskey279f0532006-09-25 16:29:54 +00004591
Jim Laskey6ff23e52006-10-04 16:53:27 +00004592 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00004593 if (Chain != BetterChain) {
Dan Gohman475871a2008-07-27 21:46:04 +00004594 SDValue ReplLoad;
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004595
Jim Laskey279f0532006-09-25 16:29:54 +00004596 // Replace the chain to void dependency.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004597 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
4598 ReplLoad = DAG.getLoad(N->getValueType(0), BetterChain, Ptr,
Duncan Sandsdc846502007-10-28 12:59:45 +00004599 LD->getSrcValue(), LD->getSrcValueOffset(),
4600 LD->isVolatile(), LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004601 } else {
4602 ReplLoad = DAG.getExtLoad(LD->getExtensionType(),
4603 LD->getValueType(0),
4604 BetterChain, Ptr, LD->getSrcValue(),
4605 LD->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004606 LD->getMemoryVT(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00004607 LD->isVolatile(),
4608 LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004609 }
Jim Laskey279f0532006-09-25 16:29:54 +00004610
Jim Laskey6ff23e52006-10-04 16:53:27 +00004611 // Create token factor to keep old chain connected.
Dan Gohman475871a2008-07-27 21:46:04 +00004612 SDValue Token = DAG.getNode(ISD::TokenFactor, MVT::Other,
Jim Laskey288af5e2006-09-25 19:32:58 +00004613 Chain, ReplLoad.getValue(1));
Jim Laskey6ff23e52006-10-04 16:53:27 +00004614
Jim Laskey274062c2006-10-13 23:32:28 +00004615 // Replace uses with load result and token factor. Don't add users
4616 // to work list.
4617 return CombineTo(N, ReplLoad.getValue(0), Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00004618 }
4619 }
4620
Evan Cheng7fc033a2006-11-03 03:06:21 +00004621 // Try transforming N to an indexed load.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00004622 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Dan Gohman475871a2008-07-27 21:46:04 +00004623 return SDValue(N, 0);
Evan Cheng7fc033a2006-11-03 03:06:21 +00004624
Dan Gohman475871a2008-07-27 21:46:04 +00004625 return SDValue();
Chris Lattner01a22022005-10-10 22:04:48 +00004626}
4627
Chris Lattner07649d92008-01-08 23:08:06 +00004628
Dan Gohman475871a2008-07-27 21:46:04 +00004629SDValue DAGCombiner::visitSTORE(SDNode *N) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00004630 StoreSDNode *ST = cast<StoreSDNode>(N);
Dan Gohman475871a2008-07-27 21:46:04 +00004631 SDValue Chain = ST->getChain();
4632 SDValue Value = ST->getValue();
4633 SDValue Ptr = ST->getBasePtr();
Jim Laskey7aed46c2006-10-11 18:55:16 +00004634
Chris Lattner00161a62008-01-25 07:20:16 +00004635 // Try to infer better alignment information than the store already has.
Dan Gohmana2676512008-08-20 16:30:28 +00004636 if (!Fast && ST->isUnindexed()) {
Chris Lattner00161a62008-01-25 07:20:16 +00004637 if (unsigned Align = InferAlignment(Ptr, DAG)) {
4638 if (Align > ST->getAlignment())
4639 return DAG.getTruncStore(Chain, Value, Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004640 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattner00161a62008-01-25 07:20:16 +00004641 ST->isVolatile(), Align);
4642 }
4643 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004644
Evan Cheng59d5b682007-05-07 21:27:48 +00004645 // If this is a store of a bit convert, store the input value if the
Evan Cheng2c4f9432007-05-09 21:49:47 +00004646 // resultant store does not need a higher alignment than the original.
Dale Johannesen98a6c622007-05-16 22:45:30 +00004647 if (Value.getOpcode() == ISD::BIT_CONVERT && !ST->isTruncatingStore() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00004648 ST->isUnindexed()) {
Evan Cheng59d5b682007-05-07 21:27:48 +00004649 unsigned Align = ST->getAlignment();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004650 MVT SVT = Value.getOperand(0).getValueType();
Dan Gohman6448d912008-09-04 15:39:15 +00004651 unsigned OrigAlign = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00004652 getABITypeAlignment(SVT.getTypeForMVT());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004653 if (Align <= OrigAlign &&
4654 ((!AfterLegalize && !ST->isVolatile()) ||
4655 TLI.isOperationLegal(ISD::STORE, SVT)))
Evan Cheng59d5b682007-05-07 21:27:48 +00004656 return DAG.getStore(Chain, Value.getOperand(0), Ptr, ST->getSrcValue(),
Dan Gohman77455612008-06-28 00:45:22 +00004657 ST->getSrcValueOffset(), ST->isVolatile(), OrigAlign);
Jim Laskey279f0532006-09-25 16:29:54 +00004658 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004659
Nate Begeman2cbba892006-12-11 02:23:46 +00004660 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Nate Begeman2cbba892006-12-11 02:23:46 +00004661 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Value)) {
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004662 // NOTE: If the original store is volatile, this transform must not increase
4663 // the number of stores. For example, on x86-32 an f64 can be stored in one
4664 // processor operation but an i64 (which is not legal) requires two. So the
4665 // transform should not be done in this case.
Evan Cheng25ece662006-12-11 17:25:19 +00004666 if (Value.getOpcode() != ISD::TargetConstantFP) {
Dan Gohman475871a2008-07-27 21:46:04 +00004667 SDValue Tmp;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004668 switch (CFP->getValueType(0).getSimpleVT()) {
Chris Lattner62be1a72006-12-12 04:16:14 +00004669 default: assert(0 && "Unknown FP type");
Dale Johannesenc7b21d52007-09-18 18:36:59 +00004670 case MVT::f80: // We don't do this for these yet.
4671 case MVT::f128:
4672 case MVT::ppcf128:
4673 break;
Chris Lattner62be1a72006-12-12 04:16:14 +00004674 case MVT::f32:
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004675 if ((!AfterLegalize && !ST->isVolatile()) ||
4676 TLI.isOperationLegal(ISD::STORE, MVT::i32)) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00004677 Tmp = DAG.getConstant((uint32_t)CFP->getValueAPF().
Dale Johannesen7111b022008-10-09 18:53:47 +00004678 bitcastToAPInt().getZExtValue(), MVT::i32);
Chris Lattner62be1a72006-12-12 04:16:14 +00004679 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004680 ST->getSrcValueOffset(), ST->isVolatile(),
4681 ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00004682 }
4683 break;
4684 case MVT::f64:
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004685 if ((!AfterLegalize && !ST->isVolatile()) ||
4686 TLI.isOperationLegal(ISD::STORE, MVT::i64)) {
Dale Johannesen7111b022008-10-09 18:53:47 +00004687 Tmp = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
Dale Johannesen9d5f4562007-09-12 03:30:33 +00004688 getZExtValue(), MVT::i64);
Chris Lattner62be1a72006-12-12 04:16:14 +00004689 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004690 ST->getSrcValueOffset(), ST->isVolatile(),
4691 ST->getAlignment());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004692 } else if (!ST->isVolatile() &&
4693 TLI.isOperationLegal(ISD::STORE, MVT::i32)) {
Duncan Sandsdc846502007-10-28 12:59:45 +00004694 // Many FP stores are not made apparent until after legalize, e.g. for
Chris Lattner62be1a72006-12-12 04:16:14 +00004695 // argument passing. Since this is so common, custom legalize the
4696 // 64-bit integer store into two 32-bit stores.
Dale Johannesen7111b022008-10-09 18:53:47 +00004697 uint64_t Val = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00004698 SDValue Lo = DAG.getConstant(Val & 0xFFFFFFFF, MVT::i32);
4699 SDValue Hi = DAG.getConstant(Val >> 32, MVT::i32);
Duncan Sands0753fc12008-02-11 10:37:04 +00004700 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner62be1a72006-12-12 04:16:14 +00004701
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004702 int SVOffset = ST->getSrcValueOffset();
4703 unsigned Alignment = ST->getAlignment();
4704 bool isVolatile = ST->isVolatile();
4705
Dan Gohman475871a2008-07-27 21:46:04 +00004706 SDValue St0 = DAG.getStore(Chain, Lo, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004707 ST->getSrcValueOffset(),
4708 isVolatile, ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00004709 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
4710 DAG.getConstant(4, Ptr.getValueType()));
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004711 SVOffset += 4;
Duncan Sandsdc846502007-10-28 12:59:45 +00004712 Alignment = MinAlign(Alignment, 4U);
Dan Gohman475871a2008-07-27 21:46:04 +00004713 SDValue St1 = DAG.getStore(Chain, Hi, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004714 SVOffset, isVolatile, Alignment);
Chris Lattner62be1a72006-12-12 04:16:14 +00004715 return DAG.getNode(ISD::TokenFactor, MVT::Other, St0, St1);
4716 }
4717 break;
Evan Cheng25ece662006-12-11 17:25:19 +00004718 }
Nate Begeman2cbba892006-12-11 02:23:46 +00004719 }
Nate Begeman2cbba892006-12-11 02:23:46 +00004720 }
4721
Jim Laskey279f0532006-09-25 16:29:54 +00004722 if (CombinerAA) {
4723 // Walk up chain skipping non-aliasing memory nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00004724 SDValue BetterChain = FindBetterChain(N, Chain);
Jim Laskey279f0532006-09-25 16:29:54 +00004725
Jim Laskey6ff23e52006-10-04 16:53:27 +00004726 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00004727 if (Chain != BetterChain) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00004728 // Replace the chain to avoid dependency.
Dan Gohman475871a2008-07-27 21:46:04 +00004729 SDValue ReplStore;
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004730 if (ST->isTruncatingStore()) {
4731 ReplStore = DAG.getTruncStore(BetterChain, Value, Ptr,
Chris Lattner4626b252008-01-17 07:20:38 +00004732 ST->getSrcValue(),ST->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004733 ST->getMemoryVT(),
Chris Lattner4626b252008-01-17 07:20:38 +00004734 ST->isVolatile(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004735 } else {
4736 ReplStore = DAG.getStore(BetterChain, Value, Ptr,
Chris Lattner4626b252008-01-17 07:20:38 +00004737 ST->getSrcValue(), ST->getSrcValueOffset(),
4738 ST->isVolatile(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004739 }
4740
Jim Laskey279f0532006-09-25 16:29:54 +00004741 // Create token to keep both nodes around.
Dan Gohman475871a2008-07-27 21:46:04 +00004742 SDValue Token =
Jim Laskey274062c2006-10-13 23:32:28 +00004743 DAG.getNode(ISD::TokenFactor, MVT::Other, Chain, ReplStore);
4744
4745 // Don't add users to work list.
4746 return CombineTo(N, Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00004747 }
Jim Laskeyd1aed7a2006-09-21 16:28:59 +00004748 }
Chris Lattnerc33baaa2005-12-23 05:48:07 +00004749
Evan Cheng33dbedc2006-11-05 09:31:14 +00004750 // Try transforming N to an indexed store.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00004751 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Dan Gohman475871a2008-07-27 21:46:04 +00004752 return SDValue(N, 0);
Evan Cheng33dbedc2006-11-05 09:31:14 +00004753
Chris Lattner3c872852007-12-29 06:26:16 +00004754 // FIXME: is there such a thing as a truncating indexed store?
Chris Lattnerddf89562008-01-17 19:59:44 +00004755 if (ST->isTruncatingStore() && ST->isUnindexed() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00004756 Value.getValueType().isInteger()) {
Chris Lattner2b4c2792007-10-13 06:35:54 +00004757 // See if we can simplify the input to this truncstore with knowledge that
4758 // only the low bits are being used. For example:
4759 // "truncstore (or (shl x, 8), y), i8" -> "truncstore y, i8"
Dan Gohman475871a2008-07-27 21:46:04 +00004760 SDValue Shorter =
Dan Gohman2e68b6f2008-02-25 21:11:39 +00004761 GetDemandedBits(Value,
4762 APInt::getLowBitsSet(Value.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +00004763 ST->getMemoryVT().getSizeInBits()));
Gabor Greifba36cb52008-08-28 21:40:38 +00004764 AddToWorkList(Value.getNode());
4765 if (Shorter.getNode())
Chris Lattner2b4c2792007-10-13 06:35:54 +00004766 return DAG.getTruncStore(Chain, Shorter, Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004767 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattner2b4c2792007-10-13 06:35:54 +00004768 ST->isVolatile(), ST->getAlignment());
Chris Lattnere33544c2007-10-13 06:58:48 +00004769
4770 // Otherwise, see if we can simplify the operation with
4771 // SimplifyDemandedBits, which only works if the value has a single use.
Dan Gohman7b8d4a92008-02-27 00:25:32 +00004772 if (SimplifyDemandedBits(Value,
4773 APInt::getLowBitsSet(
4774 Value.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +00004775 ST->getMemoryVT().getSizeInBits())))
Dan Gohman475871a2008-07-27 21:46:04 +00004776 return SDValue(N, 0);
Chris Lattner2b4c2792007-10-13 06:35:54 +00004777 }
4778
Chris Lattner3c872852007-12-29 06:26:16 +00004779 // If this is a load followed by a store to the same location, then the store
4780 // is dead/noop.
4781 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Value)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004782 if (Ld->getBasePtr() == Ptr && ST->getMemoryVT() == Ld->getMemoryVT() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00004783 ST->isUnindexed() && !ST->isVolatile() &&
Chris Lattner07649d92008-01-08 23:08:06 +00004784 // There can't be any side effects between the load and store, such as
4785 // a call or store.
Dan Gohman475871a2008-07-27 21:46:04 +00004786 Chain.reachesChainWithoutSideEffects(SDValue(Ld, 1))) {
Chris Lattner3c872852007-12-29 06:26:16 +00004787 // The store is dead, remove it.
4788 return Chain;
4789 }
4790 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004791
Chris Lattnerddf89562008-01-17 19:59:44 +00004792 // If this is an FP_ROUND or TRUNC followed by a store, fold this into a
4793 // truncating store. We can do this even if this is already a truncstore.
4794 if ((Value.getOpcode() == ISD::FP_ROUND || Value.getOpcode() == ISD::TRUNCATE)
Gabor Greifba36cb52008-08-28 21:40:38 +00004795 && Value.getNode()->hasOneUse() && ST->isUnindexed() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00004796 TLI.isTruncStoreLegal(Value.getOperand(0).getValueType(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004797 ST->getMemoryVT())) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004798 return DAG.getTruncStore(Chain, Value.getOperand(0), Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004799 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattnerddf89562008-01-17 19:59:44 +00004800 ST->isVolatile(), ST->getAlignment());
4801 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004802
Dan Gohman475871a2008-07-27 21:46:04 +00004803 return SDValue();
Chris Lattner87514ca2005-10-10 22:31:19 +00004804}
4805
Dan Gohman475871a2008-07-27 21:46:04 +00004806SDValue DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
4807 SDValue InVec = N->getOperand(0);
4808 SDValue InVal = N->getOperand(1);
4809 SDValue EltNo = N->getOperand(2);
Chris Lattnerca242442006-03-19 01:27:56 +00004810
4811 // If the invec is a BUILD_VECTOR and if EltNo is a constant, build a new
4812 // vector with the inserted element.
4813 if (InVec.getOpcode() == ISD::BUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004814 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Gabor Greif12632d22008-08-30 19:29:20 +00004815 SmallVector<SDValue, 8> Ops(InVec.getNode()->op_begin(),
4816 InVec.getNode()->op_end());
Chris Lattnerca242442006-03-19 01:27:56 +00004817 if (Elt < Ops.size())
4818 Ops[Elt] = InVal;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004819 return DAG.getNode(ISD::BUILD_VECTOR, InVec.getValueType(),
4820 &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00004821 }
4822
Dan Gohman475871a2008-07-27 21:46:04 +00004823 return SDValue();
Chris Lattnerca242442006-03-19 01:27:56 +00004824}
4825
Dan Gohman475871a2008-07-27 21:46:04 +00004826SDValue DAGCombiner::visitEXTRACT_VECTOR_ELT(SDNode *N) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004827 // (vextract (v4f32 load $addr), c) -> (f32 load $addr+c*size)
4828 // (vextract (v4f32 s2v (f32 load $addr)), c) -> (f32 load $addr+c*size)
4829 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), 0) -> (f32 load $addr)
4830
4831 // Perform only after legalization to ensure build_vector / vector_shuffle
4832 // optimizations have already been done.
Dan Gohman475871a2008-07-27 21:46:04 +00004833 if (!AfterLegalize) return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004834
Dan Gohman475871a2008-07-27 21:46:04 +00004835 SDValue InVec = N->getOperand(0);
4836 SDValue EltNo = N->getOperand(1);
Evan Cheng513da432007-10-06 08:19:55 +00004837
Evan Cheng513da432007-10-06 08:19:55 +00004838 if (isa<ConstantSDNode>(EltNo)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004839 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Evan Cheng513da432007-10-06 08:19:55 +00004840 bool NewLoad = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004841 MVT VT = InVec.getValueType();
4842 MVT EVT = VT.getVectorElementType();
4843 MVT LVT = EVT;
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004844 if (InVec.getOpcode() == ISD::BIT_CONVERT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004845 MVT BCVT = InVec.getOperand(0).getValueType();
Duncan Sands8e4eb092008-06-08 20:54:56 +00004846 if (!BCVT.isVector() || EVT.bitsGT(BCVT.getVectorElementType()))
Dan Gohman475871a2008-07-27 21:46:04 +00004847 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004848 InVec = InVec.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004849 EVT = BCVT.getVectorElementType();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004850 NewLoad = true;
4851 }
Evan Cheng513da432007-10-06 08:19:55 +00004852
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004853 LoadSDNode *LN0 = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00004854 if (ISD::isNormalLoad(InVec.getNode()))
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004855 LN0 = cast<LoadSDNode>(InVec);
4856 else if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR &&
4857 InVec.getOperand(0).getValueType() == EVT &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004858 ISD::isNormalLoad(InVec.getOperand(0).getNode())) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004859 LN0 = cast<LoadSDNode>(InVec.getOperand(0));
4860 } else if (InVec.getOpcode() == ISD::VECTOR_SHUFFLE) {
4861 // (vextract (vector_shuffle (load $addr), v2, <1, u, u, u>), 1)
4862 // =>
4863 // (load $addr+1*size)
4864 unsigned Idx = cast<ConstantSDNode>(InVec.getOperand(2).
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004865 getOperand(Elt))->getZExtValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004866 unsigned NumElems = InVec.getOperand(2).getNumOperands();
4867 InVec = (Idx < NumElems) ? InVec.getOperand(0) : InVec.getOperand(1);
4868 if (InVec.getOpcode() == ISD::BIT_CONVERT)
4869 InVec = InVec.getOperand(0);
Gabor Greifba36cb52008-08-28 21:40:38 +00004870 if (ISD::isNormalLoad(InVec.getNode())) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004871 LN0 = cast<LoadSDNode>(InVec);
4872 Elt = (Idx < NumElems) ? Idx : Idx - NumElems;
Evan Cheng513da432007-10-06 08:19:55 +00004873 }
4874 }
Duncan Sandsec87aa82008-06-15 20:12:31 +00004875 if (!LN0 || !LN0->hasOneUse() || LN0->isVolatile())
Dan Gohman475871a2008-07-27 21:46:04 +00004876 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004877
4878 unsigned Align = LN0->getAlignment();
4879 if (NewLoad) {
4880 // Check the resultant load doesn't need a higher alignment than the
4881 // original load.
Dan Gohman6448d912008-09-04 15:39:15 +00004882 unsigned NewAlign = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00004883 getABITypeAlignment(LVT.getTypeForMVT());
Duncan Sands184a8762008-06-14 17:48:34 +00004884 if (NewAlign > Align || !TLI.isOperationLegal(ISD::LOAD, LVT))
Dan Gohman475871a2008-07-27 21:46:04 +00004885 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004886 Align = NewAlign;
4887 }
4888
Dan Gohman475871a2008-07-27 21:46:04 +00004889 SDValue NewPtr = LN0->getBasePtr();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004890 if (Elt) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004891 unsigned PtrOff = LVT.getSizeInBits() * Elt / 8;
4892 MVT PtrType = NewPtr.getValueType();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004893 if (TLI.isBigEndian())
Duncan Sands83ec4b62008-06-06 12:08:01 +00004894 PtrOff = VT.getSizeInBits() / 8 - PtrOff;
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004895 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
4896 DAG.getConstant(PtrOff, PtrType));
4897 }
4898 return DAG.getLoad(LVT, LN0->getChain(), NewPtr,
4899 LN0->getSrcValue(), LN0->getSrcValueOffset(),
4900 LN0->isVolatile(), Align);
Evan Cheng513da432007-10-06 08:19:55 +00004901 }
Dan Gohman475871a2008-07-27 21:46:04 +00004902 return SDValue();
Evan Cheng513da432007-10-06 08:19:55 +00004903}
4904
4905
Dan Gohman475871a2008-07-27 21:46:04 +00004906SDValue DAGCombiner::visitBUILD_VECTOR(SDNode *N) {
Dan Gohman7f321562007-06-25 16:23:39 +00004907 unsigned NumInScalars = N->getNumOperands();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004908 MVT VT = N->getValueType(0);
4909 unsigned NumElts = VT.getVectorNumElements();
4910 MVT EltType = VT.getVectorElementType();
Chris Lattnerca242442006-03-19 01:27:56 +00004911
Dan Gohman7f321562007-06-25 16:23:39 +00004912 // Check to see if this is a BUILD_VECTOR of a bunch of EXTRACT_VECTOR_ELT
4913 // operations. If so, and if the EXTRACT_VECTOR_ELT vector inputs come from
4914 // at most two distinct vectors, turn this into a shuffle node.
Dan Gohman475871a2008-07-27 21:46:04 +00004915 SDValue VecIn1, VecIn2;
Chris Lattnerd7648c82006-03-28 20:28:38 +00004916 for (unsigned i = 0; i != NumInScalars; ++i) {
4917 // Ignore undef inputs.
4918 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
4919
Dan Gohman7f321562007-06-25 16:23:39 +00004920 // If this input is something other than a EXTRACT_VECTOR_ELT with a
Chris Lattnerd7648c82006-03-28 20:28:38 +00004921 // constant index, bail out.
Dan Gohman7f321562007-06-25 16:23:39 +00004922 if (N->getOperand(i).getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
Chris Lattnerd7648c82006-03-28 20:28:38 +00004923 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
Dan Gohman475871a2008-07-27 21:46:04 +00004924 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00004925 break;
4926 }
4927
Dan Gohman7f321562007-06-25 16:23:39 +00004928 // If the input vector type disagrees with the result of the build_vector,
Chris Lattnerd7648c82006-03-28 20:28:38 +00004929 // we can't make a shuffle.
Dan Gohman475871a2008-07-27 21:46:04 +00004930 SDValue ExtractedFromVec = N->getOperand(i).getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00004931 if (ExtractedFromVec.getValueType() != VT) {
Dan Gohman475871a2008-07-27 21:46:04 +00004932 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00004933 break;
4934 }
4935
4936 // Otherwise, remember this. We allow up to two distinct input vectors.
4937 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
4938 continue;
4939
Gabor Greifba36cb52008-08-28 21:40:38 +00004940 if (VecIn1.getNode() == 0) {
Chris Lattnerd7648c82006-03-28 20:28:38 +00004941 VecIn1 = ExtractedFromVec;
Gabor Greifba36cb52008-08-28 21:40:38 +00004942 } else if (VecIn2.getNode() == 0) {
Chris Lattnerd7648c82006-03-28 20:28:38 +00004943 VecIn2 = ExtractedFromVec;
4944 } else {
4945 // Too many inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00004946 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00004947 break;
4948 }
4949 }
4950
4951 // If everything is good, we can make a shuffle operation.
Gabor Greifba36cb52008-08-28 21:40:38 +00004952 if (VecIn1.getNode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004953 SmallVector<SDValue, 8> BuildVecIndices;
Chris Lattnerd7648c82006-03-28 20:28:38 +00004954 for (unsigned i = 0; i != NumInScalars; ++i) {
4955 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
Evan Cheng597a3bd2007-01-20 10:10:26 +00004956 BuildVecIndices.push_back(DAG.getNode(ISD::UNDEF, TLI.getPointerTy()));
Chris Lattnerd7648c82006-03-28 20:28:38 +00004957 continue;
4958 }
4959
Dan Gohman475871a2008-07-27 21:46:04 +00004960 SDValue Extract = N->getOperand(i);
Chris Lattnerd7648c82006-03-28 20:28:38 +00004961
4962 // If extracting from the first vector, just use the index directly.
4963 if (Extract.getOperand(0) == VecIn1) {
4964 BuildVecIndices.push_back(Extract.getOperand(1));
4965 continue;
4966 }
4967
4968 // Otherwise, use InIdx + VecSize
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004969 unsigned Idx =
4970 cast<ConstantSDNode>(Extract.getOperand(1))->getZExtValue();
Chris Lattner0bd48932008-01-17 07:00:52 +00004971 BuildVecIndices.push_back(DAG.getIntPtrConstant(Idx+NumInScalars));
Chris Lattnerd7648c82006-03-28 20:28:38 +00004972 }
4973
4974 // Add count and size info.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004975 MVT BuildVecVT = MVT::getVectorVT(TLI.getPointerTy(), NumElts);
Chris Lattnerd7648c82006-03-28 20:28:38 +00004976
Dan Gohman7f321562007-06-25 16:23:39 +00004977 // Return the new VECTOR_SHUFFLE node.
Dan Gohman475871a2008-07-27 21:46:04 +00004978 SDValue Ops[5];
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004979 Ops[0] = VecIn1;
Gabor Greifba36cb52008-08-28 21:40:38 +00004980 if (VecIn2.getNode()) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004981 Ops[1] = VecIn2;
Chris Lattnercef896e2006-03-28 22:19:47 +00004982 } else {
Dan Gohman7f321562007-06-25 16:23:39 +00004983 // Use an undef build_vector as input for the second operand.
Dan Gohman475871a2008-07-27 21:46:04 +00004984 std::vector<SDValue> UnOps(NumInScalars,
Chris Lattnercef896e2006-03-28 22:19:47 +00004985 DAG.getNode(ISD::UNDEF,
Dan Gohman7f321562007-06-25 16:23:39 +00004986 EltType));
4987 Ops[1] = DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004988 &UnOps[0], UnOps.size());
Gabor Greifba36cb52008-08-28 21:40:38 +00004989 AddToWorkList(Ops[1].getNode());
Chris Lattnercef896e2006-03-28 22:19:47 +00004990 }
Dan Gohman7f321562007-06-25 16:23:39 +00004991 Ops[2] = DAG.getNode(ISD::BUILD_VECTOR, BuildVecVT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004992 &BuildVecIndices[0], BuildVecIndices.size());
Dan Gohman7f321562007-06-25 16:23:39 +00004993 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Ops, 3);
Chris Lattnerd7648c82006-03-28 20:28:38 +00004994 }
4995
Dan Gohman475871a2008-07-27 21:46:04 +00004996 return SDValue();
Chris Lattnerd7648c82006-03-28 20:28:38 +00004997}
4998
Dan Gohman475871a2008-07-27 21:46:04 +00004999SDValue DAGCombiner::visitCONCAT_VECTORS(SDNode *N) {
Dan Gohman7f321562007-06-25 16:23:39 +00005000 // TODO: Check to see if this is a CONCAT_VECTORS of a bunch of
5001 // EXTRACT_SUBVECTOR operations. If so, and if the EXTRACT_SUBVECTOR vector
5002 // inputs come from at most two distinct vectors, turn this into a shuffle
5003 // node.
5004
5005 // If we only have one input vector, we don't need to do any concatenation.
5006 if (N->getNumOperands() == 1) {
5007 return N->getOperand(0);
5008 }
5009
Dan Gohman475871a2008-07-27 21:46:04 +00005010 return SDValue();
Dan Gohman7f321562007-06-25 16:23:39 +00005011}
5012
Dan Gohman475871a2008-07-27 21:46:04 +00005013SDValue DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
5014 SDValue ShufMask = N->getOperand(2);
Chris Lattnerf1d0c622006-03-31 22:16:43 +00005015 unsigned NumElts = ShufMask.getNumOperands();
5016
Mon P Wangaeb06d22008-11-10 04:46:22 +00005017 SDValue N0 = N->getOperand(0);
5018 SDValue N1 = N->getOperand(1);
5019
5020 assert(N0.getValueType().getVectorNumElements() == NumElts &&
5021 "Vector shuffle must be normalized in DAG");
5022
Chris Lattnerf1d0c622006-03-31 22:16:43 +00005023 // If the shuffle mask is an identity operation on the LHS, return the LHS.
5024 bool isIdentity = true;
5025 for (unsigned i = 0; i != NumElts; ++i) {
5026 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005027 cast<ConstantSDNode>(ShufMask.getOperand(i))->getZExtValue() != i) {
Chris Lattnerf1d0c622006-03-31 22:16:43 +00005028 isIdentity = false;
5029 break;
5030 }
5031 }
5032 if (isIdentity) return N->getOperand(0);
5033
5034 // If the shuffle mask is an identity operation on the RHS, return the RHS.
5035 isIdentity = true;
5036 for (unsigned i = 0; i != NumElts; ++i) {
5037 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005038 cast<ConstantSDNode>(ShufMask.getOperand(i))->getZExtValue() !=
5039 i+NumElts) {
Chris Lattnerf1d0c622006-03-31 22:16:43 +00005040 isIdentity = false;
5041 break;
5042 }
5043 }
5044 if (isIdentity) return N->getOperand(1);
Evan Chenge7bec0d2006-07-20 22:44:41 +00005045
5046 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
5047 // needed at all.
5048 bool isUnary = true;
Evan Cheng917ec982006-07-21 08:25:53 +00005049 bool isSplat = true;
Evan Chenge7bec0d2006-07-20 22:44:41 +00005050 int VecNum = -1;
Reid Spencer9160a6a2006-07-25 20:44:41 +00005051 unsigned BaseIdx = 0;
Evan Chenge7bec0d2006-07-20 22:44:41 +00005052 for (unsigned i = 0; i != NumElts; ++i)
5053 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005054 unsigned Idx=cast<ConstantSDNode>(ShufMask.getOperand(i))->getZExtValue();
Evan Chenge7bec0d2006-07-20 22:44:41 +00005055 int V = (Idx < NumElts) ? 0 : 1;
Evan Cheng917ec982006-07-21 08:25:53 +00005056 if (VecNum == -1) {
Evan Chenge7bec0d2006-07-20 22:44:41 +00005057 VecNum = V;
Evan Cheng917ec982006-07-21 08:25:53 +00005058 BaseIdx = Idx;
5059 } else {
5060 if (BaseIdx != Idx)
5061 isSplat = false;
5062 if (VecNum != V) {
5063 isUnary = false;
5064 break;
5065 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00005066 }
5067 }
5068
Evan Chenge7bec0d2006-07-20 22:44:41 +00005069 // Normalize unary shuffle so the RHS is undef.
5070 if (isUnary && VecNum == 1)
5071 std::swap(N0, N1);
5072
Evan Cheng917ec982006-07-21 08:25:53 +00005073 // If it is a splat, check if the argument vector is a build_vector with
5074 // all scalar elements the same.
5075 if (isSplat) {
Gabor Greifba36cb52008-08-28 21:40:38 +00005076 SDNode *V = N0.getNode();
Evan Cheng917ec982006-07-21 08:25:53 +00005077
Dan Gohman7f321562007-06-25 16:23:39 +00005078 // If this is a bit convert that changes the element type of the vector but
Evan Cheng59569222006-10-16 22:49:37 +00005079 // not the number of vector elements, look through it. Be careful not to
5080 // look though conversions that change things like v4f32 to v2f64.
Dan Gohman7f321562007-06-25 16:23:39 +00005081 if (V->getOpcode() == ISD::BIT_CONVERT) {
Dan Gohman475871a2008-07-27 21:46:04 +00005082 SDValue ConvInput = V->getOperand(0);
Evan Cheng29257862008-07-22 20:42:56 +00005083 if (ConvInput.getValueType().isVector() &&
5084 ConvInput.getValueType().getVectorNumElements() == NumElts)
Gabor Greifba36cb52008-08-28 21:40:38 +00005085 V = ConvInput.getNode();
Evan Cheng59569222006-10-16 22:49:37 +00005086 }
5087
Dan Gohman7f321562007-06-25 16:23:39 +00005088 if (V->getOpcode() == ISD::BUILD_VECTOR) {
5089 unsigned NumElems = V->getNumOperands();
Evan Cheng917ec982006-07-21 08:25:53 +00005090 if (NumElems > BaseIdx) {
Dan Gohman475871a2008-07-27 21:46:04 +00005091 SDValue Base;
Evan Cheng917ec982006-07-21 08:25:53 +00005092 bool AllSame = true;
5093 for (unsigned i = 0; i != NumElems; ++i) {
5094 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
5095 Base = V->getOperand(i);
5096 break;
5097 }
5098 }
5099 // Splat of <u, u, u, u>, return <u, u, u, u>
Gabor Greifba36cb52008-08-28 21:40:38 +00005100 if (!Base.getNode())
Evan Cheng917ec982006-07-21 08:25:53 +00005101 return N0;
5102 for (unsigned i = 0; i != NumElems; ++i) {
Evan Chenge0480d22007-09-18 21:54:37 +00005103 if (V->getOperand(i) != Base) {
Evan Cheng917ec982006-07-21 08:25:53 +00005104 AllSame = false;
5105 break;
5106 }
5107 }
5108 // Splat of <x, x, x, x>, return <x, x, x, x>
5109 if (AllSame)
5110 return N0;
5111 }
5112 }
5113 }
5114
Evan Chenge7bec0d2006-07-20 22:44:41 +00005115 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
5116 // into an undef.
5117 if (isUnary || N0 == N1) {
Chris Lattner17614ea2006-04-08 05:34:25 +00005118 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
5119 // first operand.
Dan Gohman475871a2008-07-27 21:46:04 +00005120 SmallVector<SDValue, 8> MappedOps;
Chris Lattner17614ea2006-04-08 05:34:25 +00005121 for (unsigned i = 0; i != NumElts; ++i) {
5122 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005123 cast<ConstantSDNode>(ShufMask.getOperand(i))->getZExtValue() <
5124 NumElts) {
Chris Lattner17614ea2006-04-08 05:34:25 +00005125 MappedOps.push_back(ShufMask.getOperand(i));
5126 } else {
5127 unsigned NewIdx =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005128 cast<ConstantSDNode>(ShufMask.getOperand(i))->getZExtValue() -
5129 NumElts;
Duncan Sandsd038e042008-07-21 10:20:31 +00005130 MappedOps.push_back(DAG.getConstant(NewIdx,
5131 ShufMask.getOperand(i).getValueType()));
Chris Lattner17614ea2006-04-08 05:34:25 +00005132 }
5133 }
Dan Gohman7f321562007-06-25 16:23:39 +00005134 ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMask.getValueType(),
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005135 &MappedOps[0], MappedOps.size());
Gabor Greifba36cb52008-08-28 21:40:38 +00005136 AddToWorkList(ShufMask.getNode());
Dan Gohman7f321562007-06-25 16:23:39 +00005137 return DAG.getNode(ISD::VECTOR_SHUFFLE, N->getValueType(0),
5138 N0,
5139 DAG.getNode(ISD::UNDEF, N->getValueType(0)),
5140 ShufMask);
Chris Lattner17614ea2006-04-08 05:34:25 +00005141 }
Dan Gohman7f321562007-06-25 16:23:39 +00005142
Dan Gohman475871a2008-07-27 21:46:04 +00005143 return SDValue();
Chris Lattnerf1d0c622006-03-31 22:16:43 +00005144}
5145
Evan Cheng44f1f092006-04-20 08:56:16 +00005146/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
Dan Gohman7f321562007-06-25 16:23:39 +00005147/// an AND to a vector_shuffle with the destination vector and a zero vector.
5148/// e.g. AND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
Evan Cheng44f1f092006-04-20 08:56:16 +00005149/// vector_shuffle V, Zero, <0, 4, 2, 4>
Dan Gohman475871a2008-07-27 21:46:04 +00005150SDValue DAGCombiner::XformToShuffleWithZero(SDNode *N) {
5151 SDValue LHS = N->getOperand(0);
5152 SDValue RHS = N->getOperand(1);
Dan Gohman7f321562007-06-25 16:23:39 +00005153 if (N->getOpcode() == ISD::AND) {
5154 if (RHS.getOpcode() == ISD::BIT_CONVERT)
Evan Cheng44f1f092006-04-20 08:56:16 +00005155 RHS = RHS.getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00005156 if (RHS.getOpcode() == ISD::BUILD_VECTOR) {
Dan Gohman475871a2008-07-27 21:46:04 +00005157 std::vector<SDValue> IdxOps;
Evan Cheng44f1f092006-04-20 08:56:16 +00005158 unsigned NumOps = RHS.getNumOperands();
Dan Gohman7f321562007-06-25 16:23:39 +00005159 unsigned NumElts = NumOps;
Evan Cheng44f1f092006-04-20 08:56:16 +00005160 for (unsigned i = 0; i != NumElts; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005161 SDValue Elt = RHS.getOperand(i);
Evan Cheng44f1f092006-04-20 08:56:16 +00005162 if (!isa<ConstantSDNode>(Elt))
Dan Gohman475871a2008-07-27 21:46:04 +00005163 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00005164 else if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
Duncan Sands94989ac2008-10-19 14:58:05 +00005165 IdxOps.push_back(DAG.getIntPtrConstant(i));
Evan Cheng44f1f092006-04-20 08:56:16 +00005166 else if (cast<ConstantSDNode>(Elt)->isNullValue())
Duncan Sands94989ac2008-10-19 14:58:05 +00005167 IdxOps.push_back(DAG.getIntPtrConstant(NumElts));
Evan Cheng44f1f092006-04-20 08:56:16 +00005168 else
Dan Gohman475871a2008-07-27 21:46:04 +00005169 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00005170 }
5171
5172 // Let's see if the target supports this vector_shuffle.
Duncan Sands94989ac2008-10-19 14:58:05 +00005173 if (!TLI.isVectorClearMaskLegal(IdxOps, TLI.getPointerTy(), DAG))
Dan Gohman475871a2008-07-27 21:46:04 +00005174 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00005175
Dan Gohman7f321562007-06-25 16:23:39 +00005176 // Return the new VECTOR_SHUFFLE node.
Duncan Sands94989ac2008-10-19 14:58:05 +00005177 MVT EVT = RHS.getValueType().getVectorElementType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005178 MVT VT = MVT::getVectorVT(EVT, NumElts);
Evan Cheng3eb57d52008-11-05 06:04:18 +00005179 MVT MaskVT = MVT::getVectorVT(TLI.getPointerTy(), NumElts);
Dan Gohman475871a2008-07-27 21:46:04 +00005180 std::vector<SDValue> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00005181 LHS = DAG.getNode(ISD::BIT_CONVERT, VT, LHS);
Evan Cheng44f1f092006-04-20 08:56:16 +00005182 Ops.push_back(LHS);
Gabor Greifba36cb52008-08-28 21:40:38 +00005183 AddToWorkList(LHS.getNode());
Dan Gohman475871a2008-07-27 21:46:04 +00005184 std::vector<SDValue> ZeroOps(NumElts, DAG.getConstant(0, EVT));
Dan Gohman7f321562007-06-25 16:23:39 +00005185 Ops.push_back(DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005186 &ZeroOps[0], ZeroOps.size()));
Evan Cheng3eb57d52008-11-05 06:04:18 +00005187 Ops.push_back(DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005188 &IdxOps[0], IdxOps.size()));
Dan Gohman475871a2008-07-27 21:46:04 +00005189 SDValue Result = DAG.getNode(ISD::VECTOR_SHUFFLE, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005190 &Ops[0], Ops.size());
Dan Gohman7a9a5af2008-07-16 16:13:58 +00005191 if (VT != N->getValueType(0))
5192 Result = DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Result);
Evan Cheng44f1f092006-04-20 08:56:16 +00005193 return Result;
5194 }
5195 }
Dan Gohman475871a2008-07-27 21:46:04 +00005196 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00005197}
5198
Dan Gohman7f321562007-06-25 16:23:39 +00005199/// SimplifyVBinOp - Visit a binary vector operation, like ADD.
Dan Gohman475871a2008-07-27 21:46:04 +00005200SDValue DAGCombiner::SimplifyVBinOp(SDNode *N) {
Dan Gohman7f321562007-06-25 16:23:39 +00005201 // After legalize, the target may be depending on adds and other
5202 // binary ops to provide legal ways to construct constants or other
5203 // things. Simplifying them may result in a loss of legality.
Dan Gohman475871a2008-07-27 21:46:04 +00005204 if (AfterLegalize) return SDValue();
Dan Gohman7f321562007-06-25 16:23:39 +00005205
Duncan Sands83ec4b62008-06-06 12:08:01 +00005206 MVT VT = N->getValueType(0);
5207 assert(VT.isVector() && "SimplifyVBinOp only works on vectors!");
Dan Gohman7f321562007-06-25 16:23:39 +00005208
Duncan Sands83ec4b62008-06-06 12:08:01 +00005209 MVT EltType = VT.getVectorElementType();
Dan Gohman475871a2008-07-27 21:46:04 +00005210 SDValue LHS = N->getOperand(0);
5211 SDValue RHS = N->getOperand(1);
5212 SDValue Shuffle = XformToShuffleWithZero(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00005213 if (Shuffle.getNode()) return Shuffle;
Evan Cheng44f1f092006-04-20 08:56:16 +00005214
Dan Gohman7f321562007-06-25 16:23:39 +00005215 // If the LHS and RHS are BUILD_VECTOR nodes, see if we can constant fold
Chris Lattneredab1b92006-04-02 03:25:57 +00005216 // this operation.
Dan Gohman7f321562007-06-25 16:23:39 +00005217 if (LHS.getOpcode() == ISD::BUILD_VECTOR &&
5218 RHS.getOpcode() == ISD::BUILD_VECTOR) {
Dan Gohman475871a2008-07-27 21:46:04 +00005219 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00005220 for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005221 SDValue LHSOp = LHS.getOperand(i);
5222 SDValue RHSOp = RHS.getOperand(i);
Chris Lattneredab1b92006-04-02 03:25:57 +00005223 // If these two elements can't be folded, bail out.
5224 if ((LHSOp.getOpcode() != ISD::UNDEF &&
5225 LHSOp.getOpcode() != ISD::Constant &&
5226 LHSOp.getOpcode() != ISD::ConstantFP) ||
5227 (RHSOp.getOpcode() != ISD::UNDEF &&
5228 RHSOp.getOpcode() != ISD::Constant &&
5229 RHSOp.getOpcode() != ISD::ConstantFP))
5230 break;
Evan Cheng7b336a82006-05-31 06:08:35 +00005231 // Can't fold divide by zero.
Dan Gohman7f321562007-06-25 16:23:39 +00005232 if (N->getOpcode() == ISD::SDIV || N->getOpcode() == ISD::UDIV ||
5233 N->getOpcode() == ISD::FDIV) {
Evan Cheng7b336a82006-05-31 06:08:35 +00005234 if ((RHSOp.getOpcode() == ISD::Constant &&
Gabor Greifba36cb52008-08-28 21:40:38 +00005235 cast<ConstantSDNode>(RHSOp.getNode())->isNullValue()) ||
Evan Cheng7b336a82006-05-31 06:08:35 +00005236 (RHSOp.getOpcode() == ISD::ConstantFP &&
Gabor Greifba36cb52008-08-28 21:40:38 +00005237 cast<ConstantFPSDNode>(RHSOp.getNode())->getValueAPF().isZero()))
Evan Cheng7b336a82006-05-31 06:08:35 +00005238 break;
5239 }
Dan Gohman7f321562007-06-25 16:23:39 +00005240 Ops.push_back(DAG.getNode(N->getOpcode(), EltType, LHSOp, RHSOp));
Gabor Greifba36cb52008-08-28 21:40:38 +00005241 AddToWorkList(Ops.back().getNode());
Chris Lattneredab1b92006-04-02 03:25:57 +00005242 assert((Ops.back().getOpcode() == ISD::UNDEF ||
5243 Ops.back().getOpcode() == ISD::Constant ||
5244 Ops.back().getOpcode() == ISD::ConstantFP) &&
5245 "Scalar binop didn't fold!");
5246 }
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00005247
Dan Gohman7f321562007-06-25 16:23:39 +00005248 if (Ops.size() == LHS.getNumOperands()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005249 MVT VT = LHS.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +00005250 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00005251 }
Chris Lattneredab1b92006-04-02 03:25:57 +00005252 }
5253
Dan Gohman475871a2008-07-27 21:46:04 +00005254 return SDValue();
Chris Lattneredab1b92006-04-02 03:25:57 +00005255}
5256
Dan Gohman475871a2008-07-27 21:46:04 +00005257SDValue DAGCombiner::SimplifySelect(SDValue N0, SDValue N1, SDValue N2){
Nate Begemanf845b452005-10-08 00:29:44 +00005258 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
5259
Dan Gohman475871a2008-07-27 21:46:04 +00005260 SDValue SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
Nate Begemanf845b452005-10-08 00:29:44 +00005261 cast<CondCodeSDNode>(N0.getOperand(2))->get());
5262 // If we got a simplified select_cc node back from SimplifySelectCC, then
5263 // break it down into a new SETCC node, and a new SELECT node, and then return
5264 // the SELECT node, since we were called with a SELECT node.
Gabor Greifba36cb52008-08-28 21:40:38 +00005265 if (SCC.getNode()) {
Nate Begemanf845b452005-10-08 00:29:44 +00005266 // Check to see if we got a select_cc back (to turn into setcc/select).
5267 // Otherwise, just return whatever node we got back, like fabs.
5268 if (SCC.getOpcode() == ISD::SELECT_CC) {
Dan Gohman475871a2008-07-27 21:46:04 +00005269 SDValue SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
Nate Begemanf845b452005-10-08 00:29:44 +00005270 SCC.getOperand(0), SCC.getOperand(1),
5271 SCC.getOperand(4));
Gabor Greifba36cb52008-08-28 21:40:38 +00005272 AddToWorkList(SETCC.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00005273 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
5274 SCC.getOperand(3), SETCC);
5275 }
5276 return SCC;
5277 }
Dan Gohman475871a2008-07-27 21:46:04 +00005278 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00005279}
5280
Chris Lattner40c62d52005-10-18 06:04:22 +00005281/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
5282/// are the two values being selected between, see if we can simplify the
Chris Lattner729c6d12006-05-27 00:43:02 +00005283/// select. Callers of this should assume that TheSelect is deleted if this
5284/// returns true. As such, they should return the appropriate thing (e.g. the
5285/// node) back to the top-level of the DAG combiner loop to avoid it being
5286/// looked at.
Chris Lattner40c62d52005-10-18 06:04:22 +00005287///
Dan Gohman475871a2008-07-27 21:46:04 +00005288bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDValue LHS,
5289 SDValue RHS) {
Chris Lattner40c62d52005-10-18 06:04:22 +00005290
5291 // If this is a select from two identical things, try to pull the operation
5292 // through the select.
5293 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
Chris Lattner40c62d52005-10-18 06:04:22 +00005294 // If this is a load and the token chain is identical, replace the select
5295 // of two loads with a load through a select of the address to load from.
5296 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
5297 // constants have been dropped into the constant pool.
Evan Cheng466685d2006-10-09 20:57:25 +00005298 if (LHS.getOpcode() == ISD::LOAD &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005299 // Do not let this transformation reduce the number of volatile loads.
5300 !cast<LoadSDNode>(LHS)->isVolatile() &&
5301 !cast<LoadSDNode>(RHS)->isVolatile() &&
Chris Lattner40c62d52005-10-18 06:04:22 +00005302 // Token chains must be identical.
Evan Cheng466685d2006-10-09 20:57:25 +00005303 LHS.getOperand(0) == RHS.getOperand(0)) {
5304 LoadSDNode *LLD = cast<LoadSDNode>(LHS);
5305 LoadSDNode *RLD = cast<LoadSDNode>(RHS);
5306
5307 // If this is an EXTLOAD, the VT's must match.
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005308 if (LLD->getMemoryVT() == RLD->getMemoryVT()) {
Evan Cheng466685d2006-10-09 20:57:25 +00005309 // FIXME: this conflates two src values, discarding one. This is not
5310 // the right thing to do, but nothing uses srcvalues now. When they do,
5311 // turn SrcValue into a list of locations.
Dan Gohman475871a2008-07-27 21:46:04 +00005312 SDValue Addr;
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005313 if (TheSelect->getOpcode() == ISD::SELECT) {
5314 // Check that the condition doesn't reach either load. If so, folding
5315 // this will induce a cycle into the DAG.
Gabor Greifba36cb52008-08-28 21:40:38 +00005316 if (!LLD->isPredecessorOf(TheSelect->getOperand(0).getNode()) &&
5317 !RLD->isPredecessorOf(TheSelect->getOperand(0).getNode())) {
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005318 Addr = DAG.getNode(ISD::SELECT, LLD->getBasePtr().getValueType(),
5319 TheSelect->getOperand(0), LLD->getBasePtr(),
5320 RLD->getBasePtr());
5321 }
5322 } else {
5323 // Check that the condition doesn't reach either load. If so, folding
5324 // this will induce a cycle into the DAG.
Gabor Greifba36cb52008-08-28 21:40:38 +00005325 if (!LLD->isPredecessorOf(TheSelect->getOperand(0).getNode()) &&
5326 !RLD->isPredecessorOf(TheSelect->getOperand(0).getNode()) &&
5327 !LLD->isPredecessorOf(TheSelect->getOperand(1).getNode()) &&
5328 !RLD->isPredecessorOf(TheSelect->getOperand(1).getNode())) {
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005329 Addr = DAG.getNode(ISD::SELECT_CC, LLD->getBasePtr().getValueType(),
Evan Cheng466685d2006-10-09 20:57:25 +00005330 TheSelect->getOperand(0),
5331 TheSelect->getOperand(1),
5332 LLD->getBasePtr(), RLD->getBasePtr(),
5333 TheSelect->getOperand(4));
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005334 }
Evan Cheng466685d2006-10-09 20:57:25 +00005335 }
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005336
Gabor Greifba36cb52008-08-28 21:40:38 +00005337 if (Addr.getNode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005338 SDValue Load;
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005339 if (LLD->getExtensionType() == ISD::NON_EXTLOAD)
5340 Load = DAG.getLoad(TheSelect->getValueType(0), LLD->getChain(),
5341 Addr,LLD->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00005342 LLD->getSrcValueOffset(),
5343 LLD->isVolatile(),
5344 LLD->getAlignment());
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005345 else {
5346 Load = DAG.getExtLoad(LLD->getExtensionType(),
5347 TheSelect->getValueType(0),
5348 LLD->getChain(), Addr, LLD->getSrcValue(),
5349 LLD->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005350 LLD->getMemoryVT(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00005351 LLD->isVolatile(),
5352 LLD->getAlignment());
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005353 }
5354 // Users of the select now use the result of the load.
5355 CombineTo(TheSelect, Load);
5356
5357 // Users of the old loads now use the new load's chain. We know the
5358 // old-load value is dead now.
Gabor Greifba36cb52008-08-28 21:40:38 +00005359 CombineTo(LHS.getNode(), Load.getValue(0), Load.getValue(1));
5360 CombineTo(RHS.getNode(), Load.getValue(0), Load.getValue(1));
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005361 return true;
5362 }
Evan Chengc5484282006-10-04 00:56:09 +00005363 }
Chris Lattner40c62d52005-10-18 06:04:22 +00005364 }
5365 }
5366
5367 return false;
5368}
5369
Dan Gohman475871a2008-07-27 21:46:04 +00005370SDValue DAGCombiner::SimplifySelectCC(SDValue N0, SDValue N1,
5371 SDValue N2, SDValue N3,
5372 ISD::CondCode CC, bool NotExtCompare) {
Nate Begemanf845b452005-10-08 00:29:44 +00005373
Duncan Sands83ec4b62008-06-06 12:08:01 +00005374 MVT VT = N2.getValueType();
Gabor Greifba36cb52008-08-28 21:40:38 +00005375 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
5376 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
5377 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00005378
5379 // Determine if the condition we're dealing with is constant
Dan Gohman475871a2008-07-27 21:46:04 +00005380 SDValue SCC = SimplifySetCC(TLI.getSetCCResultType(N0), N0, N1, CC, false);
Gabor Greifba36cb52008-08-28 21:40:38 +00005381 if (SCC.getNode()) AddToWorkList(SCC.getNode());
5382 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00005383
5384 // fold select_cc true, x, y -> x
Dan Gohman002e5d02008-03-13 22:13:53 +00005385 if (SCCC && !SCCC->isNullValue())
Nate Begemanf845b452005-10-08 00:29:44 +00005386 return N2;
5387 // fold select_cc false, x, y -> y
Dan Gohman002e5d02008-03-13 22:13:53 +00005388 if (SCCC && SCCC->isNullValue())
Nate Begemanf845b452005-10-08 00:29:44 +00005389 return N3;
5390
5391 // Check to see if we can simplify the select into an fabs node
5392 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
5393 // Allow either -0.0 or 0.0
Dale Johannesen87503a62007-08-25 22:10:57 +00005394 if (CFP->getValueAPF().isZero()) {
Nate Begemanf845b452005-10-08 00:29:44 +00005395 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
5396 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
5397 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
5398 N2 == N3.getOperand(0))
5399 return DAG.getNode(ISD::FABS, VT, N0);
5400
5401 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
5402 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
5403 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
5404 N2.getOperand(0) == N3)
5405 return DAG.getNode(ISD::FABS, VT, N3);
5406 }
5407 }
5408
5409 // Check to see if we can perform the "gzip trick", transforming
5410 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
Chris Lattnere3152e52006-09-20 06:41:35 +00005411 if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00005412 N0.getValueType().isInteger() &&
5413 N2.getValueType().isInteger() &&
Dan Gohman002e5d02008-03-13 22:13:53 +00005414 (N1C->isNullValue() || // (a < 0) ? b : 0
5415 (N1C->getAPIntValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0
Duncan Sands83ec4b62008-06-06 12:08:01 +00005416 MVT XType = N0.getValueType();
5417 MVT AType = N2.getValueType();
Duncan Sands8e4eb092008-06-08 20:54:56 +00005418 if (XType.bitsGE(AType)) {
Nate Begemanf845b452005-10-08 00:29:44 +00005419 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00005420 // single-bit constant.
Dan Gohman002e5d02008-03-13 22:13:53 +00005421 if (N2C && ((N2C->getAPIntValue() & (N2C->getAPIntValue()-1)) == 0)) {
5422 unsigned ShCtV = N2C->getAPIntValue().logBase2();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005423 ShCtV = XType.getSizeInBits()-ShCtV-1;
Dan Gohman475871a2008-07-27 21:46:04 +00005424 SDValue ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
5425 SDValue Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
Gabor Greifba36cb52008-08-28 21:40:38 +00005426 AddToWorkList(Shift.getNode());
Duncan Sands8e4eb092008-06-08 20:54:56 +00005427 if (XType.bitsGT(AType)) {
Nate Begemanf845b452005-10-08 00:29:44 +00005428 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00005429 AddToWorkList(Shift.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00005430 }
5431 return DAG.getNode(ISD::AND, AType, Shift, N2);
5432 }
Dan Gohman475871a2008-07-27 21:46:04 +00005433 SDValue Shift = DAG.getNode(ISD::SRA, XType, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005434 DAG.getConstant(XType.getSizeInBits()-1,
Nate Begemanf845b452005-10-08 00:29:44 +00005435 TLI.getShiftAmountTy()));
Gabor Greifba36cb52008-08-28 21:40:38 +00005436 AddToWorkList(Shift.getNode());
Duncan Sands8e4eb092008-06-08 20:54:56 +00005437 if (XType.bitsGT(AType)) {
Nate Begemanf845b452005-10-08 00:29:44 +00005438 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00005439 AddToWorkList(Shift.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00005440 }
5441 return DAG.getNode(ISD::AND, AType, Shift, N2);
5442 }
5443 }
Nate Begeman07ed4172005-10-10 21:26:48 +00005444
5445 // fold select C, 16, 0 -> shl C, 4
Dan Gohman002e5d02008-03-13 22:13:53 +00005446 if (N2C && N3C && N3C->isNullValue() && N2C->getAPIntValue().isPowerOf2() &&
Nate Begeman07ed4172005-10-10 21:26:48 +00005447 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) {
Chris Lattner1eba01e2007-04-11 06:50:51 +00005448
5449 // If the caller doesn't want us to simplify this into a zext of a compare,
5450 // don't do it.
Dan Gohman002e5d02008-03-13 22:13:53 +00005451 if (NotExtCompare && N2C->getAPIntValue() == 1)
Dan Gohman475871a2008-07-27 21:46:04 +00005452 return SDValue();
Chris Lattner1eba01e2007-04-11 06:50:51 +00005453
Nate Begeman07ed4172005-10-10 21:26:48 +00005454 // Get a SetCC of the condition
5455 // FIXME: Should probably make sure that setcc is legal if we ever have a
5456 // target where it isn't.
Dan Gohman475871a2008-07-27 21:46:04 +00005457 SDValue Temp, SCC;
Nate Begeman07ed4172005-10-10 21:26:48 +00005458 // cast from setcc result type to select result type
Nate Begemanb0d04a72006-02-18 02:40:58 +00005459 if (AfterLegalize) {
Scott Michel5b8f82e2008-03-10 15:42:14 +00005460 SCC = DAG.getSetCC(TLI.getSetCCResultType(N0), N0, N1, CC);
Duncan Sands8e4eb092008-06-08 20:54:56 +00005461 if (N2.getValueType().bitsLT(SCC.getValueType()))
Chris Lattner555d8d62006-12-07 22:36:47 +00005462 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
5463 else
5464 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00005465 } else {
5466 SCC = DAG.getSetCC(MVT::i1, N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00005467 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00005468 }
Gabor Greifba36cb52008-08-28 21:40:38 +00005469 AddToWorkList(SCC.getNode());
5470 AddToWorkList(Temp.getNode());
Chris Lattnerc56a81d2007-04-11 06:43:25 +00005471
Dan Gohman002e5d02008-03-13 22:13:53 +00005472 if (N2C->getAPIntValue() == 1)
Chris Lattnerc56a81d2007-04-11 06:43:25 +00005473 return Temp;
Nate Begeman07ed4172005-10-10 21:26:48 +00005474 // shl setcc result by log2 n2c
5475 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
Dan Gohman002e5d02008-03-13 22:13:53 +00005476 DAG.getConstant(N2C->getAPIntValue().logBase2(),
Nate Begeman07ed4172005-10-10 21:26:48 +00005477 TLI.getShiftAmountTy()));
5478 }
5479
Nate Begemanf845b452005-10-08 00:29:44 +00005480 // Check to see if this is the equivalent of setcc
5481 // FIXME: Turn all of these into setcc if setcc if setcc is legal
5482 // otherwise, go ahead with the folds.
Dan Gohman002e5d02008-03-13 22:13:53 +00005483 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getAPIntValue() == 1ULL)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005484 MVT XType = N0.getValueType();
Duncan Sands184a8762008-06-14 17:48:34 +00005485 if (!AfterLegalize ||
5486 TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultType(N0))) {
Dan Gohman475871a2008-07-27 21:46:04 +00005487 SDValue Res = DAG.getSetCC(TLI.getSetCCResultType(N0), N0, N1, CC);
Nate Begemanf845b452005-10-08 00:29:44 +00005488 if (Res.getValueType() != VT)
5489 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
5490 return Res;
5491 }
5492
5493 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
5494 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
Duncan Sands184a8762008-06-14 17:48:34 +00005495 (!AfterLegalize ||
5496 TLI.isOperationLegal(ISD::CTLZ, XType))) {
Dan Gohman475871a2008-07-27 21:46:04 +00005497 SDValue Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
Nate Begemanf845b452005-10-08 00:29:44 +00005498 return DAG.getNode(ISD::SRL, XType, Ctlz,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005499 DAG.getConstant(Log2_32(XType.getSizeInBits()),
Nate Begemanf845b452005-10-08 00:29:44 +00005500 TLI.getShiftAmountTy()));
5501 }
5502 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
5503 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
Dan Gohman475871a2008-07-27 21:46:04 +00005504 SDValue NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
Nate Begemanf845b452005-10-08 00:29:44 +00005505 N0);
Dan Gohman475871a2008-07-27 21:46:04 +00005506 SDValue NotN0 = DAG.getNode(ISD::XOR, XType, N0,
Nate Begemanf845b452005-10-08 00:29:44 +00005507 DAG.getConstant(~0ULL, XType));
5508 return DAG.getNode(ISD::SRL, XType,
5509 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
Duncan Sands83ec4b62008-06-06 12:08:01 +00005510 DAG.getConstant(XType.getSizeInBits()-1,
Nate Begemanf845b452005-10-08 00:29:44 +00005511 TLI.getShiftAmountTy()));
5512 }
5513 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
5514 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
Dan Gohman475871a2008-07-27 21:46:04 +00005515 SDValue Sign = DAG.getNode(ISD::SRL, XType, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005516 DAG.getConstant(XType.getSizeInBits()-1,
Nate Begemanf845b452005-10-08 00:29:44 +00005517 TLI.getShiftAmountTy()));
5518 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
5519 }
5520 }
5521
5522 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
5523 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
5524 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
Chris Lattner1982ef22007-04-11 05:11:38 +00005525 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1) &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00005526 N2.getOperand(0) == N1 && N0.getValueType().isInteger()) {
5527 MVT XType = N0.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00005528 SDValue Shift = DAG.getNode(ISD::SRA, XType, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005529 DAG.getConstant(XType.getSizeInBits()-1,
Chris Lattner1982ef22007-04-11 05:11:38 +00005530 TLI.getShiftAmountTy()));
Dan Gohman475871a2008-07-27 21:46:04 +00005531 SDValue Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00005532 AddToWorkList(Shift.getNode());
5533 AddToWorkList(Add.getNode());
Chris Lattner1982ef22007-04-11 05:11:38 +00005534 return DAG.getNode(ISD::XOR, XType, Add, Shift);
5535 }
5536 // Check to see if this is an integer abs. select_cc setgt X, -1, X, -X ->
5537 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
5538 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT &&
5539 N0 == N2 && N3.getOpcode() == ISD::SUB && N0 == N3.getOperand(1)) {
5540 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N3.getOperand(0))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005541 MVT XType = N0.getValueType();
5542 if (SubC->isNullValue() && XType.isInteger()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005543 SDValue Shift = DAG.getNode(ISD::SRA, XType, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005544 DAG.getConstant(XType.getSizeInBits()-1,
Chris Lattner1982ef22007-04-11 05:11:38 +00005545 TLI.getShiftAmountTy()));
Dan Gohman475871a2008-07-27 21:46:04 +00005546 SDValue Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00005547 AddToWorkList(Shift.getNode());
5548 AddToWorkList(Add.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00005549 return DAG.getNode(ISD::XOR, XType, Add, Shift);
5550 }
5551 }
5552 }
Chris Lattner1982ef22007-04-11 05:11:38 +00005553
Dan Gohman475871a2008-07-27 21:46:04 +00005554 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00005555}
5556
Evan Chengfa1eb272007-02-08 22:13:59 +00005557/// SimplifySetCC - This is a stub for TargetLowering::SimplifySetCC.
Dan Gohman475871a2008-07-27 21:46:04 +00005558SDValue DAGCombiner::SimplifySetCC(MVT VT, SDValue N0,
5559 SDValue N1, ISD::CondCode Cond,
5560 bool foldBooleans) {
Evan Chengfa1eb272007-02-08 22:13:59 +00005561 TargetLowering::DAGCombinerInfo
5562 DagCombineInfo(DAG, !AfterLegalize, false, this);
5563 return TLI.SimplifySetCC(VT, N0, N1, Cond, foldBooleans, DagCombineInfo);
Nate Begeman452d7be2005-09-16 00:54:12 +00005564}
5565
Nate Begeman69575232005-10-20 02:15:44 +00005566/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
5567/// return a DAG expression to select that will generate the same value by
5568/// multiplying by a magic number. See:
5569/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Dan Gohman475871a2008-07-27 21:46:04 +00005570SDValue DAGCombiner::BuildSDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00005571 std::vector<SDNode*> Built;
Dan Gohman475871a2008-07-27 21:46:04 +00005572 SDValue S = TLI.BuildSDIV(N, DAG, &Built);
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005573
Andrew Lenharth232c9102006-06-12 16:07:18 +00005574 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005575 ii != ee; ++ii)
5576 AddToWorkList(*ii);
5577 return S;
Nate Begeman69575232005-10-20 02:15:44 +00005578}
5579
5580/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
5581/// return a DAG expression to select that will generate the same value by
5582/// multiplying by a magic number. See:
5583/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Dan Gohman475871a2008-07-27 21:46:04 +00005584SDValue DAGCombiner::BuildUDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00005585 std::vector<SDNode*> Built;
Dan Gohman475871a2008-07-27 21:46:04 +00005586 SDValue S = TLI.BuildUDIV(N, DAG, &Built);
Nate Begeman69575232005-10-20 02:15:44 +00005587
Andrew Lenharth232c9102006-06-12 16:07:18 +00005588 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005589 ii != ee; ++ii)
5590 AddToWorkList(*ii);
5591 return S;
Nate Begeman69575232005-10-20 02:15:44 +00005592}
5593
Jim Laskey71382342006-10-07 23:37:56 +00005594/// FindBaseOffset - Return true if base is known not to alias with anything
5595/// but itself. Provides base object and offset as results.
Dan Gohman475871a2008-07-27 21:46:04 +00005596static bool FindBaseOffset(SDValue Ptr, SDValue &Base, int64_t &Offset) {
Jim Laskey71382342006-10-07 23:37:56 +00005597 // Assume it is a primitive operation.
5598 Base = Ptr; Offset = 0;
5599
5600 // If it's an adding a simple constant then integrate the offset.
5601 if (Base.getOpcode() == ISD::ADD) {
5602 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Base.getOperand(1))) {
5603 Base = Base.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005604 Offset += C->getZExtValue();
Jim Laskey71382342006-10-07 23:37:56 +00005605 }
5606 }
5607
5608 // If it's any of the following then it can't alias with anything but itself.
5609 return isa<FrameIndexSDNode>(Base) ||
5610 isa<ConstantPoolSDNode>(Base) ||
5611 isa<GlobalAddressSDNode>(Base);
5612}
5613
5614/// isAlias - Return true if there is any possibility that the two addresses
5615/// overlap.
Dan Gohman475871a2008-07-27 21:46:04 +00005616bool DAGCombiner::isAlias(SDValue Ptr1, int64_t Size1,
Jim Laskey096c22e2006-10-18 12:29:57 +00005617 const Value *SrcValue1, int SrcValueOffset1,
Dan Gohman475871a2008-07-27 21:46:04 +00005618 SDValue Ptr2, int64_t Size2,
Jim Laskey096c22e2006-10-18 12:29:57 +00005619 const Value *SrcValue2, int SrcValueOffset2)
5620{
Jim Laskey71382342006-10-07 23:37:56 +00005621 // If they are the same then they must be aliases.
5622 if (Ptr1 == Ptr2) return true;
5623
5624 // Gather base node and offset information.
Dan Gohman475871a2008-07-27 21:46:04 +00005625 SDValue Base1, Base2;
Jim Laskey71382342006-10-07 23:37:56 +00005626 int64_t Offset1, Offset2;
5627 bool KnownBase1 = FindBaseOffset(Ptr1, Base1, Offset1);
5628 bool KnownBase2 = FindBaseOffset(Ptr2, Base2, Offset2);
5629
5630 // If they have a same base address then...
5631 if (Base1 == Base2) {
5632 // Check to see if the addresses overlap.
5633 return!((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
5634 }
5635
Jim Laskey096c22e2006-10-18 12:29:57 +00005636 // If we know both bases then they can't alias.
5637 if (KnownBase1 && KnownBase2) return false;
5638
Jim Laskey07a27092006-10-18 19:08:31 +00005639 if (CombinerGlobalAA) {
5640 // Use alias analysis information.
Dan Gohmane9c8fa02007-08-27 16:32:11 +00005641 int64_t MinOffset = std::min(SrcValueOffset1, SrcValueOffset2);
5642 int64_t Overlap1 = Size1 + SrcValueOffset1 - MinOffset;
5643 int64_t Overlap2 = Size2 + SrcValueOffset2 - MinOffset;
Jim Laskey07a27092006-10-18 19:08:31 +00005644 AliasAnalysis::AliasResult AAResult =
Jim Laskey096c22e2006-10-18 12:29:57 +00005645 AA.alias(SrcValue1, Overlap1, SrcValue2, Overlap2);
Jim Laskey07a27092006-10-18 19:08:31 +00005646 if (AAResult == AliasAnalysis::NoAlias)
5647 return false;
5648 }
Jim Laskey096c22e2006-10-18 12:29:57 +00005649
5650 // Otherwise we have to assume they alias.
5651 return true;
Jim Laskey71382342006-10-07 23:37:56 +00005652}
5653
5654/// FindAliasInfo - Extracts the relevant alias information from the memory
5655/// node. Returns true if the operand was a load.
Jim Laskey7ca56af2006-10-11 13:47:09 +00005656bool DAGCombiner::FindAliasInfo(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +00005657 SDValue &Ptr, int64_t &Size,
Jim Laskey096c22e2006-10-18 12:29:57 +00005658 const Value *&SrcValue, int &SrcValueOffset) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00005659 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
5660 Ptr = LD->getBasePtr();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005661 Size = LD->getMemoryVT().getSizeInBits() >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005662 SrcValue = LD->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00005663 SrcValueOffset = LD->getSrcValueOffset();
Jim Laskey71382342006-10-07 23:37:56 +00005664 return true;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005665 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00005666 Ptr = ST->getBasePtr();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005667 Size = ST->getMemoryVT().getSizeInBits() >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005668 SrcValue = ST->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00005669 SrcValueOffset = ST->getSrcValueOffset();
Jim Laskey7ca56af2006-10-11 13:47:09 +00005670 } else {
Jim Laskey71382342006-10-07 23:37:56 +00005671 assert(0 && "FindAliasInfo expected a memory operand");
Jim Laskey71382342006-10-07 23:37:56 +00005672 }
5673
5674 return false;
5675}
5676
Jim Laskey6ff23e52006-10-04 16:53:27 +00005677/// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
5678/// looking for aliasing nodes and adding them to the Aliases vector.
Dan Gohman475871a2008-07-27 21:46:04 +00005679void DAGCombiner::GatherAllAliases(SDNode *N, SDValue OriginalChain,
5680 SmallVector<SDValue, 8> &Aliases) {
5681 SmallVector<SDValue, 8> Chains; // List of chains to visit.
Jim Laskey6ff23e52006-10-04 16:53:27 +00005682 std::set<SDNode *> Visited; // Visited node set.
5683
Jim Laskey279f0532006-09-25 16:29:54 +00005684 // Get alias information for node.
Dan Gohman475871a2008-07-27 21:46:04 +00005685 SDValue Ptr;
Jim Laskey279f0532006-09-25 16:29:54 +00005686 int64_t Size;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005687 const Value *SrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00005688 int SrcValueOffset;
5689 bool IsLoad = FindAliasInfo(N, Ptr, Size, SrcValue, SrcValueOffset);
Jim Laskey279f0532006-09-25 16:29:54 +00005690
Jim Laskey6ff23e52006-10-04 16:53:27 +00005691 // Starting off.
Jim Laskeybc588b82006-10-05 15:07:25 +00005692 Chains.push_back(OriginalChain);
Jim Laskey6ff23e52006-10-04 16:53:27 +00005693
Jim Laskeybc588b82006-10-05 15:07:25 +00005694 // Look at each chain and determine if it is an alias. If so, add it to the
5695 // aliases list. If not, then continue up the chain looking for the next
5696 // candidate.
5697 while (!Chains.empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005698 SDValue Chain = Chains.back();
Jim Laskeybc588b82006-10-05 15:07:25 +00005699 Chains.pop_back();
Jim Laskey6ff23e52006-10-04 16:53:27 +00005700
Jim Laskeybc588b82006-10-05 15:07:25 +00005701 // Don't bother if we've been before.
Gabor Greifba36cb52008-08-28 21:40:38 +00005702 if (Visited.find(Chain.getNode()) != Visited.end()) continue;
5703 Visited.insert(Chain.getNode());
Jim Laskeybc588b82006-10-05 15:07:25 +00005704
5705 switch (Chain.getOpcode()) {
5706 case ISD::EntryToken:
5707 // Entry token is ideal chain operand, but handled in FindBetterChain.
5708 break;
Jim Laskey6ff23e52006-10-04 16:53:27 +00005709
Jim Laskeybc588b82006-10-05 15:07:25 +00005710 case ISD::LOAD:
5711 case ISD::STORE: {
5712 // Get alias information for Chain.
Dan Gohman475871a2008-07-27 21:46:04 +00005713 SDValue OpPtr;
Jim Laskeybc588b82006-10-05 15:07:25 +00005714 int64_t OpSize;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005715 const Value *OpSrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00005716 int OpSrcValueOffset;
Gabor Greifba36cb52008-08-28 21:40:38 +00005717 bool IsOpLoad = FindAliasInfo(Chain.getNode(), OpPtr, OpSize,
Jim Laskey096c22e2006-10-18 12:29:57 +00005718 OpSrcValue, OpSrcValueOffset);
Jim Laskeybc588b82006-10-05 15:07:25 +00005719
5720 // If chain is alias then stop here.
5721 if (!(IsLoad && IsOpLoad) &&
Jim Laskey096c22e2006-10-18 12:29:57 +00005722 isAlias(Ptr, Size, SrcValue, SrcValueOffset,
5723 OpPtr, OpSize, OpSrcValue, OpSrcValueOffset)) {
Jim Laskeybc588b82006-10-05 15:07:25 +00005724 Aliases.push_back(Chain);
5725 } else {
5726 // Look further up the chain.
5727 Chains.push_back(Chain.getOperand(0));
5728 // Clean up old chain.
Gabor Greifba36cb52008-08-28 21:40:38 +00005729 AddToWorkList(Chain.getNode());
Jim Laskey279f0532006-09-25 16:29:54 +00005730 }
Jim Laskeybc588b82006-10-05 15:07:25 +00005731 break;
5732 }
5733
5734 case ISD::TokenFactor:
5735 // We have to check each of the operands of the token factor, so we queue
5736 // then up. Adding the operands to the queue (stack) in reverse order
5737 // maintains the original order and increases the likelihood that getNode
5738 // will find a matching token factor (CSE.)
5739 for (unsigned n = Chain.getNumOperands(); n;)
5740 Chains.push_back(Chain.getOperand(--n));
5741 // Eliminate the token factor if we can.
Gabor Greifba36cb52008-08-28 21:40:38 +00005742 AddToWorkList(Chain.getNode());
Jim Laskeybc588b82006-10-05 15:07:25 +00005743 break;
5744
5745 default:
5746 // For all other instructions we will just have to take what we can get.
5747 Aliases.push_back(Chain);
5748 break;
Jim Laskey279f0532006-09-25 16:29:54 +00005749 }
5750 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00005751}
5752
5753/// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, looking
5754/// for a better chain (aliasing node.)
Dan Gohman475871a2008-07-27 21:46:04 +00005755SDValue DAGCombiner::FindBetterChain(SDNode *N, SDValue OldChain) {
5756 SmallVector<SDValue, 8> Aliases; // Ops for replacing token factor.
Jim Laskey279f0532006-09-25 16:29:54 +00005757
Jim Laskey6ff23e52006-10-04 16:53:27 +00005758 // Accumulate all the aliases to this node.
5759 GatherAllAliases(N, OldChain, Aliases);
5760
5761 if (Aliases.size() == 0) {
5762 // If no operands then chain to entry token.
5763 return DAG.getEntryNode();
5764 } else if (Aliases.size() == 1) {
5765 // If a single operand then chain to it. We don't need to revisit it.
5766 return Aliases[0];
5767 }
5768
5769 // Construct a custom tailored token factor.
Dan Gohman475871a2008-07-27 21:46:04 +00005770 SDValue NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
Jim Laskey6ff23e52006-10-04 16:53:27 +00005771 &Aliases[0], Aliases.size());
5772
5773 // Make sure the old chain gets cleaned up.
Gabor Greifba36cb52008-08-28 21:40:38 +00005774 if (NewChain != OldChain) AddToWorkList(OldChain.getNode());
Jim Laskey6ff23e52006-10-04 16:53:27 +00005775
5776 return NewChain;
Jim Laskey279f0532006-09-25 16:29:54 +00005777}
5778
Nate Begeman1d4d4142005-09-01 00:19:25 +00005779// SelectionDAG::Combine - This is the entry point for the file.
5780//
Dan Gohmana2676512008-08-20 16:30:28 +00005781void SelectionDAG::Combine(bool RunningAfterLegalize, AliasAnalysis &AA,
5782 bool Fast) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00005783 /// run - This is the main entry point to this class.
5784 ///
Dan Gohmana2676512008-08-20 16:30:28 +00005785 DAGCombiner(*this, AA, Fast).Run(RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +00005786}