blob: cbb6506b65b69ee93e4bfcc67abc8592831dabf2 [file] [log] [blame]
Nate Begeman4ebd8052005-09-01 23:24:04 +00001//===-- DAGCombiner.cpp - Implement a DAG node combiner -------------------===//
Nate Begeman1d4d4142005-09-01 00:19:25 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Nate Begeman1d4d4142005-09-01 00:19:25 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This pass combines dag nodes to form fewer, simpler DAG nodes. It can be run
11// both before and after the DAG is legalized.
Nate Begeman1d4d4142005-09-01 00:19:25 +000012//
13//===----------------------------------------------------------------------===//
14
15#define DEBUG_TYPE "dagcombine"
Nate Begeman1d4d4142005-09-01 00:19:25 +000016#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattner00161a62008-01-25 07:20:16 +000017#include "llvm/CodeGen/MachineFunction.h"
18#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattnerc76d4412007-05-16 06:37:59 +000019#include "llvm/Analysis/AliasAnalysis.h"
Evan Cheng59d5b682007-05-07 21:27:48 +000020#include "llvm/Target/TargetData.h"
Chris Lattner1329cb82008-01-26 19:45:50 +000021#include "llvm/Target/TargetFrameInfo.h"
Nate Begeman1d4d4142005-09-01 00:19:25 +000022#include "llvm/Target/TargetLowering.h"
Evan Cheng59d5b682007-05-07 21:27:48 +000023#include "llvm/Target/TargetMachine.h"
Chris Lattnerddae4bd2007-01-08 23:04:05 +000024#include "llvm/Target/TargetOptions.h"
Chris Lattnerc76d4412007-05-16 06:37:59 +000025#include "llvm/ADT/SmallPtrSet.h"
26#include "llvm/ADT/Statistic.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000027#include "llvm/Support/Compiler.h"
Jim Laskeyd1aed7a2006-09-21 16:28:59 +000028#include "llvm/Support/CommandLine.h"
Chris Lattnerc76d4412007-05-16 06:37:59 +000029#include "llvm/Support/Debug.h"
30#include "llvm/Support/MathExtras.h"
Chris Lattnera500fc62005-09-09 23:53:39 +000031#include <algorithm>
Dan Gohmanee335e32008-05-23 20:40:06 +000032#include <set>
Nate Begeman1d4d4142005-09-01 00:19:25 +000033using namespace llvm;
34
Chris Lattnercd3245a2006-12-19 22:41:21 +000035STATISTIC(NodesCombined , "Number of dag nodes combined");
36STATISTIC(PreIndexedNodes , "Number of pre-indexed nodes created");
37STATISTIC(PostIndexedNodes, "Number of post-indexed nodes created");
38
Nate Begeman1d4d4142005-09-01 00:19:25 +000039namespace {
Jim Laskey71382342006-10-07 23:37:56 +000040 static cl::opt<bool>
41 CombinerAA("combiner-alias-analysis", cl::Hidden,
Jim Laskey26f7fa72006-10-17 19:33:52 +000042 cl::desc("Turn on alias analysis during testing"));
Jim Laskey3ad175b2006-10-12 15:22:24 +000043
Jim Laskey07a27092006-10-18 19:08:31 +000044 static cl::opt<bool>
45 CombinerGlobalAA("combiner-global-alias-analysis", cl::Hidden,
46 cl::desc("Include global information in alias analysis"));
47
Jim Laskeybc588b82006-10-05 15:07:25 +000048//------------------------------ DAGCombiner ---------------------------------//
49
Jim Laskey71382342006-10-07 23:37:56 +000050 class VISIBILITY_HIDDEN DAGCombiner {
Nate Begeman1d4d4142005-09-01 00:19:25 +000051 SelectionDAG &DAG;
52 TargetLowering &TLI;
Nate Begeman4ebd8052005-09-01 23:24:04 +000053 bool AfterLegalize;
Dan Gohmana2676512008-08-20 16:30:28 +000054 bool Fast;
Nate Begeman1d4d4142005-09-01 00:19:25 +000055
56 // Worklist of all of the nodes that need to be simplified.
Evan Cheng17a568b2008-08-29 22:21:44 +000057 std::vector<SDNode*> WorkList;
Nate Begeman1d4d4142005-09-01 00:19:25 +000058
Jim Laskeyc7c3f112006-10-16 20:52:31 +000059 // AA - Used for DAG load/store alias analysis.
60 AliasAnalysis &AA;
61
Nate Begeman1d4d4142005-09-01 00:19:25 +000062 /// AddUsersToWorkList - When an instruction is simplified, add all users of
63 /// the instruction to the work lists because they might get more simplified
64 /// now.
65 ///
66 void AddUsersToWorkList(SDNode *N) {
67 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
Nate Begeman4ebd8052005-09-01 23:24:04 +000068 UI != UE; ++UI)
Dan Gohman89684502008-07-27 20:43:25 +000069 AddToWorkList(*UI);
Nate Begeman1d4d4142005-09-01 00:19:25 +000070 }
71
Dan Gohman389079b2007-10-08 17:57:15 +000072 /// visit - call the node-specific routine that knows how to fold each
73 /// particular type of node.
Dan Gohman475871a2008-07-27 21:46:04 +000074 SDValue visit(SDNode *N);
Dan Gohman389079b2007-10-08 17:57:15 +000075
Chris Lattner24664722006-03-01 04:53:38 +000076 public:
Jim Laskey6ff23e52006-10-04 16:53:27 +000077 /// AddToWorkList - Add to the work list making sure it's instance is at the
78 /// the back (next to be processed.)
Chris Lattner5750df92006-03-01 04:03:14 +000079 void AddToWorkList(SDNode *N) {
Jim Laskey6ff23e52006-10-04 16:53:27 +000080 removeFromWorkList(N);
Chris Lattner5750df92006-03-01 04:03:14 +000081 WorkList.push_back(N);
82 }
Jim Laskey6ff23e52006-10-04 16:53:27 +000083
Chris Lattnerf8dc0612008-02-03 06:49:24 +000084 /// removeFromWorkList - remove all instances of N from the worklist.
85 ///
86 void removeFromWorkList(SDNode *N) {
87 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), N),
88 WorkList.end());
Chris Lattner01a22022005-10-10 22:04:48 +000089 }
Nate Begeman368e18d2006-02-16 21:11:51 +000090
Dan Gohman475871a2008-07-27 21:46:04 +000091 SDValue CombineTo(SDNode *N, const SDValue *To, unsigned NumTo,
Chris Lattnerf8dc0612008-02-03 06:49:24 +000092 bool AddTo = true);
93
Dan Gohman475871a2008-07-27 21:46:04 +000094 SDValue CombineTo(SDNode *N, SDValue Res, bool AddTo = true) {
Jim Laskey274062c2006-10-13 23:32:28 +000095 return CombineTo(N, &Res, 1, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +000096 }
97
Dan Gohman475871a2008-07-27 21:46:04 +000098 SDValue CombineTo(SDNode *N, SDValue Res0, SDValue Res1,
Jim Laskey274062c2006-10-13 23:32:28 +000099 bool AddTo = true) {
Dan Gohman475871a2008-07-27 21:46:04 +0000100 SDValue To[] = { Res0, Res1 };
Jim Laskey274062c2006-10-13 23:32:28 +0000101 return CombineTo(N, To, 2, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000102 }
Chris Lattner0bd48932008-01-17 07:00:52 +0000103
Chris Lattner24664722006-03-01 04:53:38 +0000104 private:
105
Chris Lattner012f2412006-02-17 21:58:01 +0000106 /// SimplifyDemandedBits - Check the specified integer node value to see if
Chris Lattnerb2742f42006-03-01 19:55:35 +0000107 /// it can be simplified or if things it uses can be simplified by bit
Chris Lattner012f2412006-02-17 21:58:01 +0000108 /// propagation. If so, return true.
Dan Gohman475871a2008-07-27 21:46:04 +0000109 bool SimplifyDemandedBits(SDValue Op) {
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000110 APInt Demanded = APInt::getAllOnesValue(Op.getValueSizeInBits());
111 return SimplifyDemandedBits(Op, Demanded);
112 }
113
Dan Gohman475871a2008-07-27 21:46:04 +0000114 bool SimplifyDemandedBits(SDValue Op, const APInt &Demanded);
Chris Lattner87514ca2005-10-10 22:31:19 +0000115
Chris Lattner448f2192006-11-11 00:39:41 +0000116 bool CombineToPreIndexedLoadStore(SDNode *N);
117 bool CombineToPostIndexedLoadStore(SDNode *N);
118
119
Dan Gohman389079b2007-10-08 17:57:15 +0000120 /// combine - call the node-specific routine that knows how to fold each
121 /// particular type of node. If that doesn't do anything, try the
122 /// target-specific DAG combines.
Dan Gohman475871a2008-07-27 21:46:04 +0000123 SDValue combine(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000124
125 // Visitation implementation - Implement dag node combining for different
126 // node types. The semantics are as follows:
127 // Return Value:
Evan Cheng17a568b2008-08-29 22:21:44 +0000128 // SDValue.getNode() == 0 - No change was made
129 // SDValue.getNode() == N - N was replaced, is dead and has been handled.
130 // otherwise - N should be replaced by the returned Operand.
Nate Begeman1d4d4142005-09-01 00:19:25 +0000131 //
Dan Gohman475871a2008-07-27 21:46:04 +0000132 SDValue visitTokenFactor(SDNode *N);
133 SDValue visitMERGE_VALUES(SDNode *N);
134 SDValue visitADD(SDNode *N);
135 SDValue visitSUB(SDNode *N);
136 SDValue visitADDC(SDNode *N);
137 SDValue visitADDE(SDNode *N);
138 SDValue visitMUL(SDNode *N);
139 SDValue visitSDIV(SDNode *N);
140 SDValue visitUDIV(SDNode *N);
141 SDValue visitSREM(SDNode *N);
142 SDValue visitUREM(SDNode *N);
143 SDValue visitMULHU(SDNode *N);
144 SDValue visitMULHS(SDNode *N);
145 SDValue visitSMUL_LOHI(SDNode *N);
146 SDValue visitUMUL_LOHI(SDNode *N);
147 SDValue visitSDIVREM(SDNode *N);
148 SDValue visitUDIVREM(SDNode *N);
149 SDValue visitAND(SDNode *N);
150 SDValue visitOR(SDNode *N);
151 SDValue visitXOR(SDNode *N);
152 SDValue SimplifyVBinOp(SDNode *N);
153 SDValue visitSHL(SDNode *N);
154 SDValue visitSRA(SDNode *N);
155 SDValue visitSRL(SDNode *N);
156 SDValue visitCTLZ(SDNode *N);
157 SDValue visitCTTZ(SDNode *N);
158 SDValue visitCTPOP(SDNode *N);
159 SDValue visitSELECT(SDNode *N);
160 SDValue visitSELECT_CC(SDNode *N);
161 SDValue visitSETCC(SDNode *N);
162 SDValue visitSIGN_EXTEND(SDNode *N);
163 SDValue visitZERO_EXTEND(SDNode *N);
164 SDValue visitANY_EXTEND(SDNode *N);
165 SDValue visitSIGN_EXTEND_INREG(SDNode *N);
166 SDValue visitTRUNCATE(SDNode *N);
167 SDValue visitBIT_CONVERT(SDNode *N);
168 SDValue visitBUILD_PAIR(SDNode *N);
169 SDValue visitFADD(SDNode *N);
170 SDValue visitFSUB(SDNode *N);
171 SDValue visitFMUL(SDNode *N);
172 SDValue visitFDIV(SDNode *N);
173 SDValue visitFREM(SDNode *N);
174 SDValue visitFCOPYSIGN(SDNode *N);
175 SDValue visitSINT_TO_FP(SDNode *N);
176 SDValue visitUINT_TO_FP(SDNode *N);
177 SDValue visitFP_TO_SINT(SDNode *N);
178 SDValue visitFP_TO_UINT(SDNode *N);
179 SDValue visitFP_ROUND(SDNode *N);
180 SDValue visitFP_ROUND_INREG(SDNode *N);
181 SDValue visitFP_EXTEND(SDNode *N);
182 SDValue visitFNEG(SDNode *N);
183 SDValue visitFABS(SDNode *N);
184 SDValue visitBRCOND(SDNode *N);
185 SDValue visitBR_CC(SDNode *N);
186 SDValue visitLOAD(SDNode *N);
187 SDValue visitSTORE(SDNode *N);
188 SDValue visitINSERT_VECTOR_ELT(SDNode *N);
189 SDValue visitEXTRACT_VECTOR_ELT(SDNode *N);
190 SDValue visitBUILD_VECTOR(SDNode *N);
191 SDValue visitCONCAT_VECTORS(SDNode *N);
192 SDValue visitVECTOR_SHUFFLE(SDNode *N);
Bill Wendling7cdc3c82008-11-21 02:03:52 +0000193 SDValue visitADDO(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000194
Dan Gohman475871a2008-07-27 21:46:04 +0000195 SDValue XformToShuffleWithZero(SDNode *N);
196 SDValue ReassociateOps(unsigned Opc, SDValue LHS, SDValue RHS);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000197
Dan Gohman475871a2008-07-27 21:46:04 +0000198 SDValue visitShiftByConstant(SDNode *N, unsigned Amt);
Chris Lattnere70da202007-12-06 07:33:36 +0000199
Dan Gohman475871a2008-07-27 21:46:04 +0000200 bool SimplifySelectOps(SDNode *SELECT, SDValue LHS, SDValue RHS);
201 SDValue SimplifyBinOpWithSameOpcodeHands(SDNode *N);
202 SDValue SimplifySelect(SDValue N0, SDValue N1, SDValue N2);
203 SDValue SimplifySelectCC(SDValue N0, SDValue N1, SDValue N2,
204 SDValue N3, ISD::CondCode CC,
Chris Lattner1eba01e2007-04-11 06:50:51 +0000205 bool NotExtCompare = false);
Dan Gohman475871a2008-07-27 21:46:04 +0000206 SDValue SimplifySetCC(MVT VT, SDValue N0, SDValue N1,
Nate Begemane17daeb2005-10-05 21:43:42 +0000207 ISD::CondCode Cond, bool foldBooleans = true);
Dan Gohman475871a2008-07-27 21:46:04 +0000208 SDValue SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
Chris Lattner5eee4272008-01-26 01:09:19 +0000209 unsigned HiOp);
Dan Gohman475871a2008-07-27 21:46:04 +0000210 SDValue CombineConsecutiveLoads(SDNode *N, MVT VT);
211 SDValue ConstantFoldBIT_CONVERTofBUILD_VECTOR(SDNode *, MVT);
212 SDValue BuildSDIV(SDNode *N);
213 SDValue BuildUDIV(SDNode *N);
214 SDNode *MatchRotate(SDValue LHS, SDValue RHS);
215 SDValue ReduceLoadWidth(SDNode *N);
Jim Laskey279f0532006-09-25 16:29:54 +0000216
Dan Gohman475871a2008-07-27 21:46:04 +0000217 SDValue GetDemandedBits(SDValue V, const APInt &Mask);
Chris Lattner2b4c2792007-10-13 06:35:54 +0000218
Jim Laskey6ff23e52006-10-04 16:53:27 +0000219 /// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
220 /// looking for aliasing nodes and adding them to the Aliases vector.
Dan Gohman475871a2008-07-27 21:46:04 +0000221 void GatherAllAliases(SDNode *N, SDValue OriginalChain,
222 SmallVector<SDValue, 8> &Aliases);
Jim Laskey6ff23e52006-10-04 16:53:27 +0000223
Jim Laskey096c22e2006-10-18 12:29:57 +0000224 /// isAlias - Return true if there is any possibility that the two addresses
225 /// overlap.
Dan Gohman475871a2008-07-27 21:46:04 +0000226 bool isAlias(SDValue Ptr1, int64_t Size1,
Jim Laskey096c22e2006-10-18 12:29:57 +0000227 const Value *SrcValue1, int SrcValueOffset1,
Dan Gohman475871a2008-07-27 21:46:04 +0000228 SDValue Ptr2, int64_t Size2,
Jeff Cohend41b30d2006-11-05 19:31:28 +0000229 const Value *SrcValue2, int SrcValueOffset2);
Jim Laskey096c22e2006-10-18 12:29:57 +0000230
Jim Laskey7ca56af2006-10-11 13:47:09 +0000231 /// FindAliasInfo - Extracts the relevant alias information from the memory
232 /// node. Returns true if the operand was a load.
233 bool FindAliasInfo(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +0000234 SDValue &Ptr, int64_t &Size,
Jim Laskey096c22e2006-10-18 12:29:57 +0000235 const Value *&SrcValue, int &SrcValueOffset);
Jim Laskey7ca56af2006-10-11 13:47:09 +0000236
Jim Laskey279f0532006-09-25 16:29:54 +0000237 /// FindBetterChain - Walk up chain skipping non-aliasing memory nodes,
Jim Laskey6ff23e52006-10-04 16:53:27 +0000238 /// looking for a better chain (aliasing node.)
Dan Gohman475871a2008-07-27 21:46:04 +0000239 SDValue FindBetterChain(SDNode *N, SDValue Chain);
Jim Laskey279f0532006-09-25 16:29:54 +0000240
Nate Begeman1d4d4142005-09-01 00:19:25 +0000241public:
Dan Gohmana2676512008-08-20 16:30:28 +0000242 DAGCombiner(SelectionDAG &D, AliasAnalysis &A, bool fast)
Jim Laskeyc7c3f112006-10-16 20:52:31 +0000243 : DAG(D),
244 TLI(D.getTargetLoweringInfo()),
245 AfterLegalize(false),
Dan Gohmana2676512008-08-20 16:30:28 +0000246 Fast(fast),
Jim Laskeyc7c3f112006-10-16 20:52:31 +0000247 AA(A) {}
Nate Begeman1d4d4142005-09-01 00:19:25 +0000248
249 /// Run - runs the dag combiner on all nodes in the work list
Nate Begeman4ebd8052005-09-01 23:24:04 +0000250 void Run(bool RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000251 };
252}
253
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000254
255namespace {
256/// WorkListRemover - This class is a DAGUpdateListener that removes any deleted
257/// nodes from the worklist.
258class VISIBILITY_HIDDEN WorkListRemover :
259 public SelectionDAG::DAGUpdateListener {
260 DAGCombiner &DC;
261public:
Dan Gohmanb5660dc2008-02-20 16:44:09 +0000262 explicit WorkListRemover(DAGCombiner &dc) : DC(dc) {}
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000263
Duncan Sandsedfcf592008-06-11 11:42:12 +0000264 virtual void NodeDeleted(SDNode *N, SDNode *E) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000265 DC.removeFromWorkList(N);
266 }
267
268 virtual void NodeUpdated(SDNode *N) {
269 // Ignore updates.
270 }
271};
272}
273
Chris Lattner24664722006-03-01 04:53:38 +0000274//===----------------------------------------------------------------------===//
275// TargetLowering::DAGCombinerInfo implementation
276//===----------------------------------------------------------------------===//
277
278void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) {
279 ((DAGCombiner*)DC)->AddToWorkList(N);
280}
281
Dan Gohman475871a2008-07-27 21:46:04 +0000282SDValue TargetLowering::DAGCombinerInfo::
283CombineTo(SDNode *N, const std::vector<SDValue> &To) {
Chris Lattner3577e382006-08-11 17:56:38 +0000284 return ((DAGCombiner*)DC)->CombineTo(N, &To[0], To.size());
Chris Lattner24664722006-03-01 04:53:38 +0000285}
286
Dan Gohman475871a2008-07-27 21:46:04 +0000287SDValue TargetLowering::DAGCombinerInfo::
288CombineTo(SDNode *N, SDValue Res) {
Chris Lattner24664722006-03-01 04:53:38 +0000289 return ((DAGCombiner*)DC)->CombineTo(N, Res);
290}
291
292
Dan Gohman475871a2008-07-27 21:46:04 +0000293SDValue TargetLowering::DAGCombinerInfo::
294CombineTo(SDNode *N, SDValue Res0, SDValue Res1) {
Chris Lattner24664722006-03-01 04:53:38 +0000295 return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1);
296}
297
298
Chris Lattner24664722006-03-01 04:53:38 +0000299//===----------------------------------------------------------------------===//
Chris Lattner29446522007-05-14 22:04:50 +0000300// Helper Functions
301//===----------------------------------------------------------------------===//
302
303/// isNegatibleForFree - Return 1 if we can compute the negated form of the
304/// specified expression for the same cost as the expression itself, or 2 if we
305/// can compute the negated form more cheaply than the expression itself.
Dan Gohman475871a2008-07-27 21:46:04 +0000306static char isNegatibleForFree(SDValue Op, bool AfterLegalize,
Chris Lattner0254e702008-02-26 07:04:54 +0000307 unsigned Depth = 0) {
Dale Johannesendb44bf82007-10-16 23:38:29 +0000308 // No compile time optimizations on this type.
309 if (Op.getValueType() == MVT::ppcf128)
310 return 0;
311
Chris Lattner29446522007-05-14 22:04:50 +0000312 // fneg is removable even if it has multiple uses.
313 if (Op.getOpcode() == ISD::FNEG) return 2;
314
315 // Don't allow anything with multiple uses.
316 if (!Op.hasOneUse()) return 0;
317
Chris Lattner3adf9512007-05-25 02:19:06 +0000318 // Don't recurse exponentially.
319 if (Depth > 6) return 0;
320
Chris Lattner29446522007-05-14 22:04:50 +0000321 switch (Op.getOpcode()) {
322 default: return false;
323 case ISD::ConstantFP:
Chris Lattner0254e702008-02-26 07:04:54 +0000324 // Don't invert constant FP values after legalize. The negated constant
325 // isn't necessarily legal.
326 return AfterLegalize ? 0 : 1;
Chris Lattner29446522007-05-14 22:04:50 +0000327 case ISD::FADD:
328 // FIXME: determine better conditions for this xform.
329 if (!UnsafeFPMath) return 0;
330
331 // -(A+B) -> -A - B
Chris Lattner0254e702008-02-26 07:04:54 +0000332 if (char V = isNegatibleForFree(Op.getOperand(0), AfterLegalize, Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000333 return V;
334 // -(A+B) -> -B - A
Chris Lattner0254e702008-02-26 07:04:54 +0000335 return isNegatibleForFree(Op.getOperand(1), AfterLegalize, Depth+1);
Chris Lattner29446522007-05-14 22:04:50 +0000336 case ISD::FSUB:
337 // We can't turn -(A-B) into B-A when we honor signed zeros.
338 if (!UnsafeFPMath) return 0;
339
340 // -(A-B) -> B-A
341 return 1;
342
343 case ISD::FMUL:
344 case ISD::FDIV:
345 if (HonorSignDependentRoundingFPMath()) return 0;
346
347 // -(X*Y) -> (-X * Y) or (X*-Y)
Chris Lattner0254e702008-02-26 07:04:54 +0000348 if (char V = isNegatibleForFree(Op.getOperand(0), AfterLegalize, Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000349 return V;
350
Chris Lattner0254e702008-02-26 07:04:54 +0000351 return isNegatibleForFree(Op.getOperand(1), AfterLegalize, Depth+1);
Chris Lattner29446522007-05-14 22:04:50 +0000352
353 case ISD::FP_EXTEND:
354 case ISD::FP_ROUND:
355 case ISD::FSIN:
Chris Lattner0254e702008-02-26 07:04:54 +0000356 return isNegatibleForFree(Op.getOperand(0), AfterLegalize, Depth+1);
Chris Lattner29446522007-05-14 22:04:50 +0000357 }
358}
359
360/// GetNegatedExpression - If isNegatibleForFree returns true, this function
361/// returns the newly negated expression.
Dan Gohman475871a2008-07-27 21:46:04 +0000362static SDValue GetNegatedExpression(SDValue Op, SelectionDAG &DAG,
Chris Lattner0254e702008-02-26 07:04:54 +0000363 bool AfterLegalize, unsigned Depth = 0) {
Chris Lattner29446522007-05-14 22:04:50 +0000364 // fneg is removable even if it has multiple uses.
365 if (Op.getOpcode() == ISD::FNEG) return Op.getOperand(0);
366
367 // Don't allow anything with multiple uses.
368 assert(Op.hasOneUse() && "Unknown reuse!");
369
Chris Lattner3adf9512007-05-25 02:19:06 +0000370 assert(Depth <= 6 && "GetNegatedExpression doesn't match isNegatibleForFree");
Chris Lattner29446522007-05-14 22:04:50 +0000371 switch (Op.getOpcode()) {
372 default: assert(0 && "Unknown code");
Dale Johannesenc4dd3c32007-08-31 23:34:27 +0000373 case ISD::ConstantFP: {
374 APFloat V = cast<ConstantFPSDNode>(Op)->getValueAPF();
375 V.changeSign();
376 return DAG.getConstantFP(V, Op.getValueType());
377 }
Chris Lattner29446522007-05-14 22:04:50 +0000378 case ISD::FADD:
379 // FIXME: determine better conditions for this xform.
380 assert(UnsafeFPMath);
381
382 // -(A+B) -> -A - B
Chris Lattner0254e702008-02-26 07:04:54 +0000383 if (isNegatibleForFree(Op.getOperand(0), AfterLegalize, Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000384 return DAG.getNode(ISD::FSUB, Op.getValueType(),
Chris Lattner0254e702008-02-26 07:04:54 +0000385 GetNegatedExpression(Op.getOperand(0), DAG,
386 AfterLegalize, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000387 Op.getOperand(1));
388 // -(A+B) -> -B - A
389 return DAG.getNode(ISD::FSUB, Op.getValueType(),
Chris Lattner0254e702008-02-26 07:04:54 +0000390 GetNegatedExpression(Op.getOperand(1), DAG,
391 AfterLegalize, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000392 Op.getOperand(0));
393 case ISD::FSUB:
394 // We can't turn -(A-B) into B-A when we honor signed zeros.
395 assert(UnsafeFPMath);
Dan Gohman23ff1822007-07-02 15:48:56 +0000396
397 // -(0-B) -> B
398 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(Op.getOperand(0)))
Dale Johannesenc4dd3c32007-08-31 23:34:27 +0000399 if (N0CFP->getValueAPF().isZero())
Dan Gohman23ff1822007-07-02 15:48:56 +0000400 return Op.getOperand(1);
Chris Lattner29446522007-05-14 22:04:50 +0000401
402 // -(A-B) -> B-A
403 return DAG.getNode(ISD::FSUB, Op.getValueType(), Op.getOperand(1),
404 Op.getOperand(0));
405
406 case ISD::FMUL:
407 case ISD::FDIV:
408 assert(!HonorSignDependentRoundingFPMath());
409
410 // -(X*Y) -> -X * Y
Chris Lattneraeecb6c2008-02-26 17:09:59 +0000411 if (isNegatibleForFree(Op.getOperand(0), AfterLegalize, Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000412 return DAG.getNode(Op.getOpcode(), Op.getValueType(),
Chris Lattner0254e702008-02-26 07:04:54 +0000413 GetNegatedExpression(Op.getOperand(0), DAG,
414 AfterLegalize, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000415 Op.getOperand(1));
416
417 // -(X*Y) -> X * -Y
418 return DAG.getNode(Op.getOpcode(), Op.getValueType(),
419 Op.getOperand(0),
Chris Lattner0254e702008-02-26 07:04:54 +0000420 GetNegatedExpression(Op.getOperand(1), DAG,
421 AfterLegalize, Depth+1));
Chris Lattner29446522007-05-14 22:04:50 +0000422
423 case ISD::FP_EXTEND:
Chris Lattner29446522007-05-14 22:04:50 +0000424 case ISD::FSIN:
425 return DAG.getNode(Op.getOpcode(), Op.getValueType(),
Chris Lattner0254e702008-02-26 07:04:54 +0000426 GetNegatedExpression(Op.getOperand(0), DAG,
427 AfterLegalize, Depth+1));
Chris Lattner0bd48932008-01-17 07:00:52 +0000428 case ISD::FP_ROUND:
429 return DAG.getNode(ISD::FP_ROUND, Op.getValueType(),
Chris Lattner0254e702008-02-26 07:04:54 +0000430 GetNegatedExpression(Op.getOperand(0), DAG,
431 AfterLegalize, Depth+1),
Chris Lattner0bd48932008-01-17 07:00:52 +0000432 Op.getOperand(1));
Chris Lattner29446522007-05-14 22:04:50 +0000433 }
434}
Chris Lattner24664722006-03-01 04:53:38 +0000435
436
Nate Begeman4ebd8052005-09-01 23:24:04 +0000437// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
438// that selects between the values 1 and 0, making it equivalent to a setcc.
Nate Begeman646d7e22005-09-02 21:18:40 +0000439// Also, set the incoming LHS, RHS, and CC references to the appropriate
440// nodes based on the type of node we are checking. This simplifies life a
441// bit for the callers.
Dan Gohman475871a2008-07-27 21:46:04 +0000442static bool isSetCCEquivalent(SDValue N, SDValue &LHS, SDValue &RHS,
443 SDValue &CC) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000444 if (N.getOpcode() == ISD::SETCC) {
445 LHS = N.getOperand(0);
446 RHS = N.getOperand(1);
447 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000448 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000449 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000450 if (N.getOpcode() == ISD::SELECT_CC &&
451 N.getOperand(2).getOpcode() == ISD::Constant &&
452 N.getOperand(3).getOpcode() == ISD::Constant &&
Dan Gohman002e5d02008-03-13 22:13:53 +0000453 cast<ConstantSDNode>(N.getOperand(2))->getAPIntValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000454 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
455 LHS = N.getOperand(0);
456 RHS = N.getOperand(1);
457 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000458 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000459 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000460 return false;
461}
462
Nate Begeman99801192005-09-07 23:25:52 +0000463// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
464// one use. If this is true, it allows the users to invert the operation for
465// free when it is profitable to do so.
Dan Gohman475871a2008-07-27 21:46:04 +0000466static bool isOneUseSetCC(SDValue N) {
467 SDValue N0, N1, N2;
Gabor Greifba36cb52008-08-28 21:40:38 +0000468 if (isSetCCEquivalent(N, N0, N1, N2) && N.getNode()->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000469 return true;
470 return false;
471}
472
Dan Gohman475871a2008-07-27 21:46:04 +0000473SDValue DAGCombiner::ReassociateOps(unsigned Opc, SDValue N0, SDValue N1){
Duncan Sands83ec4b62008-06-06 12:08:01 +0000474 MVT VT = N0.getValueType();
Nate Begemancd4d58c2006-02-03 06:46:56 +0000475 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
476 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
477 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
478 if (isa<ConstantSDNode>(N1)) {
Dan Gohman475871a2008-07-27 21:46:04 +0000479 SDValue OpNode = DAG.getNode(Opc, VT, N0.getOperand(1), N1);
Gabor Greifba36cb52008-08-28 21:40:38 +0000480 AddToWorkList(OpNode.getNode());
Nate Begemancd4d58c2006-02-03 06:46:56 +0000481 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(0));
482 } else if (N0.hasOneUse()) {
Dan Gohman475871a2008-07-27 21:46:04 +0000483 SDValue OpNode = DAG.getNode(Opc, VT, N0.getOperand(0), N1);
Gabor Greifba36cb52008-08-28 21:40:38 +0000484 AddToWorkList(OpNode.getNode());
Nate Begemancd4d58c2006-02-03 06:46:56 +0000485 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(1));
486 }
487 }
488 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
489 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
490 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
491 if (isa<ConstantSDNode>(N0)) {
Dan Gohman475871a2008-07-27 21:46:04 +0000492 SDValue OpNode = DAG.getNode(Opc, VT, N1.getOperand(1), N0);
Gabor Greifba36cb52008-08-28 21:40:38 +0000493 AddToWorkList(OpNode.getNode());
Nate Begemancd4d58c2006-02-03 06:46:56 +0000494 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(0));
495 } else if (N1.hasOneUse()) {
Dan Gohman475871a2008-07-27 21:46:04 +0000496 SDValue OpNode = DAG.getNode(Opc, VT, N1.getOperand(0), N0);
Gabor Greifba36cb52008-08-28 21:40:38 +0000497 AddToWorkList(OpNode.getNode());
Nate Begemancd4d58c2006-02-03 06:46:56 +0000498 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(1));
499 }
500 }
Dan Gohman475871a2008-07-27 21:46:04 +0000501 return SDValue();
Nate Begemancd4d58c2006-02-03 06:46:56 +0000502}
503
Dan Gohman475871a2008-07-27 21:46:04 +0000504SDValue DAGCombiner::CombineTo(SDNode *N, const SDValue *To, unsigned NumTo,
505 bool AddTo) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000506 assert(N->getNumValues() == NumTo && "Broken CombineTo call!");
507 ++NodesCombined;
508 DOUT << "\nReplacing.1 "; DEBUG(N->dump(&DAG));
Gabor Greifba36cb52008-08-28 21:40:38 +0000509 DOUT << "\nWith: "; DEBUG(To[0].getNode()->dump(&DAG));
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000510 DOUT << " and " << NumTo-1 << " other values\n";
511 WorkListRemover DeadNodes(*this);
512 DAG.ReplaceAllUsesWith(N, To, &DeadNodes);
513
514 if (AddTo) {
515 // Push the new nodes and any users onto the worklist
516 for (unsigned i = 0, e = NumTo; i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000517 AddToWorkList(To[i].getNode());
518 AddUsersToWorkList(To[i].getNode());
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000519 }
520 }
521
522 // Nodes can be reintroduced into the worklist. Make sure we do not
523 // process a node that has been replaced.
524 removeFromWorkList(N);
525
526 // Finally, since the node is now dead, remove it from the graph.
527 DAG.DeleteNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +0000528 return SDValue(N, 0);
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000529}
530
531/// SimplifyDemandedBits - Check the specified integer node value to see if
532/// it can be simplified or if things it uses can be simplified by bit
533/// propagation. If so, return true.
Dan Gohman475871a2008-07-27 21:46:04 +0000534bool DAGCombiner::SimplifyDemandedBits(SDValue Op, const APInt &Demanded) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000535 TargetLowering::TargetLoweringOpt TLO(DAG, AfterLegalize);
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000536 APInt KnownZero, KnownOne;
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000537 if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO))
538 return false;
539
540 // Revisit the node.
Gabor Greifba36cb52008-08-28 21:40:38 +0000541 AddToWorkList(Op.getNode());
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000542
543 // Replace the old value with the new one.
544 ++NodesCombined;
Gabor Greifba36cb52008-08-28 21:40:38 +0000545 DOUT << "\nReplacing.2 "; DEBUG(TLO.Old.getNode()->dump(&DAG));
546 DOUT << "\nWith: "; DEBUG(TLO.New.getNode()->dump(&DAG));
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000547 DOUT << '\n';
548
549 // Replace all uses. If any nodes become isomorphic to other nodes and
550 // are deleted, make sure to remove them from our worklist.
551 WorkListRemover DeadNodes(*this);
552 DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New, &DeadNodes);
553
554 // Push the new node and any (possibly new) users onto the worklist.
Gabor Greifba36cb52008-08-28 21:40:38 +0000555 AddToWorkList(TLO.New.getNode());
556 AddUsersToWorkList(TLO.New.getNode());
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000557
558 // Finally, if the node is now dead, remove it from the graph. The node
559 // may not be dead if the replacement process recursively simplified to
560 // something else needing this node.
Gabor Greifba36cb52008-08-28 21:40:38 +0000561 if (TLO.Old.getNode()->use_empty()) {
562 removeFromWorkList(TLO.Old.getNode());
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000563
564 // If the operands of this node are only used by the node, they will now
565 // be dead. Make sure to visit them first to delete dead nodes early.
Gabor Greifba36cb52008-08-28 21:40:38 +0000566 for (unsigned i = 0, e = TLO.Old.getNode()->getNumOperands(); i != e; ++i)
567 if (TLO.Old.getNode()->getOperand(i).getNode()->hasOneUse())
568 AddToWorkList(TLO.Old.getNode()->getOperand(i).getNode());
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000569
Gabor Greifba36cb52008-08-28 21:40:38 +0000570 DAG.DeleteNode(TLO.Old.getNode());
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000571 }
572 return true;
573}
574
Chris Lattner29446522007-05-14 22:04:50 +0000575//===----------------------------------------------------------------------===//
576// Main DAG Combiner implementation
577//===----------------------------------------------------------------------===//
578
Nate Begeman4ebd8052005-09-01 23:24:04 +0000579void DAGCombiner::Run(bool RunningAfterLegalize) {
580 // set the instance variable, so that the various visit routines may use it.
581 AfterLegalize = RunningAfterLegalize;
582
Evan Cheng17a568b2008-08-29 22:21:44 +0000583 // Add all the dag nodes to the worklist.
584 WorkList.reserve(DAG.allnodes_size());
585 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
586 E = DAG.allnodes_end(); I != E; ++I)
587 WorkList.push_back(I);
588
589 // Create a dummy node (which is not added to allnodes), that adds a reference
590 // to the root node, preventing it from being deleted, and tracking any
591 // changes of the root.
592 HandleSDNode Dummy(DAG.getRoot());
593
Jim Laskey26f7fa72006-10-17 19:33:52 +0000594 // The root of the dag may dangle to deleted nodes until the dag combiner is
595 // done. Set it to null to avoid confusion.
Dan Gohman475871a2008-07-27 21:46:04 +0000596 DAG.setRoot(SDValue());
Chris Lattner24664722006-03-01 04:53:38 +0000597
Evan Cheng17a568b2008-08-29 22:21:44 +0000598 // while the worklist isn't empty, inspect the node on the end of it and
599 // try and combine it.
600 while (!WorkList.empty()) {
601 SDNode *N = WorkList.back();
602 WorkList.pop_back();
603
604 // If N has no uses, it is dead. Make sure to revisit all N's operands once
605 // N is deleted from the DAG, since they too may now be dead or may have a
606 // reduced number of uses, allowing other xforms.
607 if (N->use_empty() && N != &Dummy) {
608 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
609 AddToWorkList(N->getOperand(i).getNode());
610
611 DAG.DeleteNode(N);
612 continue;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000613 }
Evan Cheng17a568b2008-08-29 22:21:44 +0000614
615 SDValue RV = combine(N);
616
617 if (RV.getNode() == 0)
618 continue;
619
620 ++NodesCombined;
621
622 // If we get back the same node we passed in, rather than a new node or
623 // zero, we know that the node must have defined multiple values and
624 // CombineTo was used. Since CombineTo takes care of the worklist
625 // mechanics for us, we have no work to do in this case.
626 if (RV.getNode() == N)
627 continue;
628
629 assert(N->getOpcode() != ISD::DELETED_NODE &&
630 RV.getNode()->getOpcode() != ISD::DELETED_NODE &&
631 "Node was deleted but visit returned new node!");
Chris Lattner729c6d12006-05-27 00:43:02 +0000632
Evan Cheng17a568b2008-08-29 22:21:44 +0000633 DOUT << "\nReplacing.3 "; DEBUG(N->dump(&DAG));
634 DOUT << "\nWith: "; DEBUG(RV.getNode()->dump(&DAG));
635 DOUT << '\n';
636 WorkListRemover DeadNodes(*this);
637 if (N->getNumValues() == RV.getNode()->getNumValues())
638 DAG.ReplaceAllUsesWith(N, RV.getNode(), &DeadNodes);
639 else {
640 assert(N->getValueType(0) == RV.getValueType() &&
641 N->getNumValues() == 1 && "Type mismatch");
642 SDValue OpV = RV;
643 DAG.ReplaceAllUsesWith(N, &OpV, &DeadNodes);
644 }
645
646 // Push the new node and any users onto the worklist
647 AddToWorkList(RV.getNode());
648 AddUsersToWorkList(RV.getNode());
649
650 // Add any uses of the old node to the worklist in case this node is the
651 // last one that uses them. They may become dead after this node is
652 // deleted.
653 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
654 AddToWorkList(N->getOperand(i).getNode());
655
656 // Nodes can be reintroduced into the worklist. Make sure we do not
657 // process a node that has been replaced.
658 removeFromWorkList(N);
659
660 // Finally, since the node is now dead, remove it from the graph.
661 DAG.DeleteNode(N);
662 }
663
Chris Lattner95038592005-10-05 06:35:28 +0000664 // If the root changed (e.g. it was a dead load, update the root).
665 DAG.setRoot(Dummy.getValue());
Nate Begeman1d4d4142005-09-01 00:19:25 +0000666}
667
Dan Gohman475871a2008-07-27 21:46:04 +0000668SDValue DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000669 switch(N->getOpcode()) {
670 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +0000671 case ISD::TokenFactor: return visitTokenFactor(N);
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000672 case ISD::MERGE_VALUES: return visitMERGE_VALUES(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000673 case ISD::ADD: return visitADD(N);
674 case ISD::SUB: return visitSUB(N);
Chris Lattner91153682007-03-04 20:03:15 +0000675 case ISD::ADDC: return visitADDC(N);
676 case ISD::ADDE: return visitADDE(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000677 case ISD::MUL: return visitMUL(N);
678 case ISD::SDIV: return visitSDIV(N);
679 case ISD::UDIV: return visitUDIV(N);
680 case ISD::SREM: return visitSREM(N);
681 case ISD::UREM: return visitUREM(N);
682 case ISD::MULHU: return visitMULHU(N);
683 case ISD::MULHS: return visitMULHS(N);
Dan Gohman389079b2007-10-08 17:57:15 +0000684 case ISD::SMUL_LOHI: return visitSMUL_LOHI(N);
685 case ISD::UMUL_LOHI: return visitUMUL_LOHI(N);
686 case ISD::SDIVREM: return visitSDIVREM(N);
687 case ISD::UDIVREM: return visitUDIVREM(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000688 case ISD::AND: return visitAND(N);
689 case ISD::OR: return visitOR(N);
690 case ISD::XOR: return visitXOR(N);
691 case ISD::SHL: return visitSHL(N);
692 case ISD::SRA: return visitSRA(N);
693 case ISD::SRL: return visitSRL(N);
694 case ISD::CTLZ: return visitCTLZ(N);
695 case ISD::CTTZ: return visitCTTZ(N);
696 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7beb2005-09-16 00:54:12 +0000697 case ISD::SELECT: return visitSELECT(N);
698 case ISD::SELECT_CC: return visitSELECT_CC(N);
699 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000700 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
701 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000702 case ISD::ANY_EXTEND: return visitANY_EXTEND(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000703 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
704 case ISD::TRUNCATE: return visitTRUNCATE(N);
Chris Lattner94683772005-12-23 05:30:37 +0000705 case ISD::BIT_CONVERT: return visitBIT_CONVERT(N);
Evan Cheng9bfa03c2008-05-12 23:04:07 +0000706 case ISD::BUILD_PAIR: return visitBUILD_PAIR(N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000707 case ISD::FADD: return visitFADD(N);
708 case ISD::FSUB: return visitFSUB(N);
709 case ISD::FMUL: return visitFMUL(N);
710 case ISD::FDIV: return visitFDIV(N);
711 case ISD::FREM: return visitFREM(N);
Chris Lattner12d83032006-03-05 05:30:57 +0000712 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000713 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
714 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
715 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
716 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
717 case ISD::FP_ROUND: return visitFP_ROUND(N);
718 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
719 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
720 case ISD::FNEG: return visitFNEG(N);
721 case ISD::FABS: return visitFABS(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000722 case ISD::BRCOND: return visitBRCOND(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000723 case ISD::BR_CC: return visitBR_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000724 case ISD::LOAD: return visitLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000725 case ISD::STORE: return visitSTORE(N);
Chris Lattnerca242442006-03-19 01:27:56 +0000726 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
Evan Cheng513da432007-10-06 08:19:55 +0000727 case ISD::EXTRACT_VECTOR_ELT: return visitEXTRACT_VECTOR_ELT(N);
Dan Gohman7f321562007-06-25 16:23:39 +0000728 case ISD::BUILD_VECTOR: return visitBUILD_VECTOR(N);
729 case ISD::CONCAT_VECTORS: return visitCONCAT_VECTORS(N);
Chris Lattner66445d32006-03-28 22:11:53 +0000730 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
Bill Wendling7cdc3c82008-11-21 02:03:52 +0000731 case ISD::ADDO: return visitADDO(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000732 }
Dan Gohman475871a2008-07-27 21:46:04 +0000733 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000734}
735
Dan Gohman475871a2008-07-27 21:46:04 +0000736SDValue DAGCombiner::combine(SDNode *N) {
Dan Gohman389079b2007-10-08 17:57:15 +0000737
Dan Gohman475871a2008-07-27 21:46:04 +0000738 SDValue RV = visit(N);
Dan Gohman389079b2007-10-08 17:57:15 +0000739
740 // If nothing happened, try a target-specific DAG combine.
Gabor Greifba36cb52008-08-28 21:40:38 +0000741 if (RV.getNode() == 0) {
Dan Gohman389079b2007-10-08 17:57:15 +0000742 assert(N->getOpcode() != ISD::DELETED_NODE &&
743 "Node was deleted but visit returned NULL!");
744
745 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
746 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode())) {
747
748 // Expose the DAG combiner to the target combiner impls.
749 TargetLowering::DAGCombinerInfo
750 DagCombineInfo(DAG, !AfterLegalize, false, this);
751
752 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
753 }
754 }
755
Evan Cheng08b11732008-03-22 01:55:50 +0000756 // If N is a commutative binary node, try commuting it to enable more
757 // sdisel CSE.
Gabor Greifba36cb52008-08-28 21:40:38 +0000758 if (RV.getNode() == 0 &&
Evan Cheng08b11732008-03-22 01:55:50 +0000759 SelectionDAG::isCommutativeBinOp(N->getOpcode()) &&
760 N->getNumValues() == 1) {
Dan Gohman475871a2008-07-27 21:46:04 +0000761 SDValue N0 = N->getOperand(0);
762 SDValue N1 = N->getOperand(1);
Evan Cheng08b11732008-03-22 01:55:50 +0000763 // Constant operands are canonicalized to RHS.
764 if (isa<ConstantSDNode>(N0) || !isa<ConstantSDNode>(N1)) {
Dan Gohman475871a2008-07-27 21:46:04 +0000765 SDValue Ops[] = { N1, N0 };
Evan Cheng08b11732008-03-22 01:55:50 +0000766 SDNode *CSENode = DAG.getNodeIfExists(N->getOpcode(), N->getVTList(),
767 Ops, 2);
Evan Chengea100462008-03-24 23:55:16 +0000768 if (CSENode)
Dan Gohman475871a2008-07-27 21:46:04 +0000769 return SDValue(CSENode, 0);
Evan Cheng08b11732008-03-22 01:55:50 +0000770 }
771 }
772
Dan Gohman389079b2007-10-08 17:57:15 +0000773 return RV;
774}
775
Chris Lattner6270f682006-10-08 22:57:01 +0000776/// getInputChainForNode - Given a node, return its input chain if it has one,
777/// otherwise return a null sd operand.
Dan Gohman475871a2008-07-27 21:46:04 +0000778static SDValue getInputChainForNode(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +0000779 if (unsigned NumOps = N->getNumOperands()) {
780 if (N->getOperand(0).getValueType() == MVT::Other)
781 return N->getOperand(0);
782 else if (N->getOperand(NumOps-1).getValueType() == MVT::Other)
783 return N->getOperand(NumOps-1);
784 for (unsigned i = 1; i < NumOps-1; ++i)
785 if (N->getOperand(i).getValueType() == MVT::Other)
786 return N->getOperand(i);
787 }
Dan Gohman475871a2008-07-27 21:46:04 +0000788 return SDValue(0, 0);
Chris Lattner6270f682006-10-08 22:57:01 +0000789}
790
Dan Gohman475871a2008-07-27 21:46:04 +0000791SDValue DAGCombiner::visitTokenFactor(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +0000792 // If N has two operands, where one has an input chain equal to the other,
793 // the 'other' chain is redundant.
794 if (N->getNumOperands() == 2) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000795 if (getInputChainForNode(N->getOperand(0).getNode()) == N->getOperand(1))
Chris Lattner6270f682006-10-08 22:57:01 +0000796 return N->getOperand(0);
Gabor Greifba36cb52008-08-28 21:40:38 +0000797 if (getInputChainForNode(N->getOperand(1).getNode()) == N->getOperand(0))
Chris Lattner6270f682006-10-08 22:57:01 +0000798 return N->getOperand(1);
799 }
800
Chris Lattnerc76d4412007-05-16 06:37:59 +0000801 SmallVector<SDNode *, 8> TFs; // List of token factors to visit.
Dan Gohman475871a2008-07-27 21:46:04 +0000802 SmallVector<SDValue, 8> Ops; // Ops for replacing token factor.
Chris Lattnerc76d4412007-05-16 06:37:59 +0000803 SmallPtrSet<SDNode*, 16> SeenOps;
804 bool Changed = false; // If we should replace this token factor.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000805
806 // Start out with this token factor.
Jim Laskey279f0532006-09-25 16:29:54 +0000807 TFs.push_back(N);
Jim Laskey279f0532006-09-25 16:29:54 +0000808
Jim Laskey71382342006-10-07 23:37:56 +0000809 // Iterate through token factors. The TFs grows when new token factors are
Jim Laskeybc588b82006-10-05 15:07:25 +0000810 // encountered.
811 for (unsigned i = 0; i < TFs.size(); ++i) {
812 SDNode *TF = TFs[i];
813
Jim Laskey6ff23e52006-10-04 16:53:27 +0000814 // Check each of the operands.
815 for (unsigned i = 0, ie = TF->getNumOperands(); i != ie; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +0000816 SDValue Op = TF->getOperand(i);
Jim Laskey279f0532006-09-25 16:29:54 +0000817
Jim Laskey6ff23e52006-10-04 16:53:27 +0000818 switch (Op.getOpcode()) {
819 case ISD::EntryToken:
Jim Laskeybc588b82006-10-05 15:07:25 +0000820 // Entry tokens don't need to be added to the list. They are
821 // rededundant.
822 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000823 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000824
Jim Laskey6ff23e52006-10-04 16:53:27 +0000825 case ISD::TokenFactor:
Jim Laskeybc588b82006-10-05 15:07:25 +0000826 if ((CombinerAA || Op.hasOneUse()) &&
Gabor Greifba36cb52008-08-28 21:40:38 +0000827 std::find(TFs.begin(), TFs.end(), Op.getNode()) == TFs.end()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +0000828 // Queue up for processing.
Gabor Greifba36cb52008-08-28 21:40:38 +0000829 TFs.push_back(Op.getNode());
Jim Laskey6ff23e52006-10-04 16:53:27 +0000830 // Clean up in case the token factor is removed.
Gabor Greifba36cb52008-08-28 21:40:38 +0000831 AddToWorkList(Op.getNode());
Jim Laskey6ff23e52006-10-04 16:53:27 +0000832 Changed = true;
833 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000834 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000835 // Fall thru
836
837 default:
Chris Lattnerc76d4412007-05-16 06:37:59 +0000838 // Only add if it isn't already in the list.
Gabor Greifba36cb52008-08-28 21:40:38 +0000839 if (SeenOps.insert(Op.getNode()))
Jim Laskeybc588b82006-10-05 15:07:25 +0000840 Ops.push_back(Op);
Chris Lattnerc76d4412007-05-16 06:37:59 +0000841 else
842 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000843 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000844 }
845 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000846 }
847
Dan Gohman475871a2008-07-27 21:46:04 +0000848 SDValue Result;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000849
850 // If we've change things around then replace token factor.
851 if (Changed) {
Dan Gohman30359592008-01-29 13:02:09 +0000852 if (Ops.empty()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +0000853 // The entry token is the only possible outcome.
854 Result = DAG.getEntryNode();
855 } else {
856 // New and improved token factor.
857 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0], Ops.size());
Nate Begemanded49632005-10-13 03:11:28 +0000858 }
Jim Laskey274062c2006-10-13 23:32:28 +0000859
860 // Don't add users to work list.
861 return CombineTo(N, Result, false);
Nate Begemanded49632005-10-13 03:11:28 +0000862 }
Jim Laskey279f0532006-09-25 16:29:54 +0000863
Jim Laskey6ff23e52006-10-04 16:53:27 +0000864 return Result;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000865}
866
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000867/// MERGE_VALUES can always be eliminated.
Dan Gohman475871a2008-07-27 21:46:04 +0000868SDValue DAGCombiner::visitMERGE_VALUES(SDNode *N) {
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000869 WorkListRemover DeadNodes(*this);
870 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Dan Gohman475871a2008-07-27 21:46:04 +0000871 DAG.ReplaceAllUsesOfValueWith(SDValue(N, i), N->getOperand(i),
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000872 &DeadNodes);
873 removeFromWorkList(N);
874 DAG.DeleteNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +0000875 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000876}
877
878
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000879static
Dan Gohman475871a2008-07-27 21:46:04 +0000880SDValue combineShlAddConstant(SDValue N0, SDValue N1, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000881 MVT VT = N0.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +0000882 SDValue N00 = N0.getOperand(0);
883 SDValue N01 = N0.getOperand(1);
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000884 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N01);
Gabor Greifba36cb52008-08-28 21:40:38 +0000885 if (N01C && N00.getOpcode() == ISD::ADD && N00.getNode()->hasOneUse() &&
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000886 isa<ConstantSDNode>(N00.getOperand(1))) {
887 N0 = DAG.getNode(ISD::ADD, VT,
888 DAG.getNode(ISD::SHL, VT, N00.getOperand(0), N01),
889 DAG.getNode(ISD::SHL, VT, N00.getOperand(1), N01));
890 return DAG.getNode(ISD::ADD, VT, N0, N1);
891 }
Dan Gohman475871a2008-07-27 21:46:04 +0000892 return SDValue();
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000893}
894
Evan Chengb13cdbd2007-06-21 07:39:16 +0000895static
Dan Gohman475871a2008-07-27 21:46:04 +0000896SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
Bill Wendlingae89bb12008-11-11 08:25:46 +0000897 SelectionDAG &DAG, const TargetLowering &TLI,
898 bool AfterLegalize) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000899 MVT VT = N->getValueType(0);
Evan Chengb13cdbd2007-06-21 07:39:16 +0000900 unsigned Opc = N->getOpcode();
901 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
Dan Gohman475871a2008-07-27 21:46:04 +0000902 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
903 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
Evan Chengb13cdbd2007-06-21 07:39:16 +0000904 ISD::CondCode CC = ISD::SETCC_INVALID;
Bill Wendlingae89bb12008-11-11 08:25:46 +0000905
906 if (isSlctCC) {
Evan Chengb13cdbd2007-06-21 07:39:16 +0000907 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
Bill Wendlingae89bb12008-11-11 08:25:46 +0000908 } else {
Dan Gohman475871a2008-07-27 21:46:04 +0000909 SDValue CCOp = Slct.getOperand(0);
Evan Chengb13cdbd2007-06-21 07:39:16 +0000910 if (CCOp.getOpcode() == ISD::SETCC)
911 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
912 }
913
914 bool DoXform = false;
915 bool InvCC = false;
916 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
917 "Bad input!");
Bill Wendlingae89bb12008-11-11 08:25:46 +0000918
Evan Chengb13cdbd2007-06-21 07:39:16 +0000919 if (LHS.getOpcode() == ISD::Constant &&
Bill Wendlingae89bb12008-11-11 08:25:46 +0000920 cast<ConstantSDNode>(LHS)->isNullValue()) {
Evan Chengb13cdbd2007-06-21 07:39:16 +0000921 DoXform = true;
Bill Wendlingae89bb12008-11-11 08:25:46 +0000922 } else if (CC != ISD::SETCC_INVALID &&
923 RHS.getOpcode() == ISD::Constant &&
924 cast<ConstantSDNode>(RHS)->isNullValue()) {
Evan Chengb13cdbd2007-06-21 07:39:16 +0000925 std::swap(LHS, RHS);
Dan Gohman475871a2008-07-27 21:46:04 +0000926 SDValue Op0 = Slct.getOperand(0);
Bill Wendlingae89bb12008-11-11 08:25:46 +0000927 MVT OpVT = isSlctCC ? Op0.getValueType() :
928 Op0.getOperand(0).getValueType();
929 bool isInt = OpVT.isInteger();
Evan Chengb13cdbd2007-06-21 07:39:16 +0000930 CC = ISD::getSetCCInverse(CC, isInt);
Bill Wendlingae89bb12008-11-11 08:25:46 +0000931
932 if (AfterLegalize && !TLI.isCondCodeLegal(CC, OpVT))
933 return SDValue(); // Inverse operator isn't legal.
934
Evan Chengb13cdbd2007-06-21 07:39:16 +0000935 DoXform = true;
936 InvCC = true;
937 }
938
939 if (DoXform) {
Dan Gohman475871a2008-07-27 21:46:04 +0000940 SDValue Result = DAG.getNode(Opc, VT, OtherOp, RHS);
Evan Chengb13cdbd2007-06-21 07:39:16 +0000941 if (isSlctCC)
942 return DAG.getSelectCC(OtherOp, Result,
943 Slct.getOperand(0), Slct.getOperand(1), CC);
Dan Gohman475871a2008-07-27 21:46:04 +0000944 SDValue CCOp = Slct.getOperand(0);
Evan Chengb13cdbd2007-06-21 07:39:16 +0000945 if (InvCC)
946 CCOp = DAG.getSetCC(CCOp.getValueType(), CCOp.getOperand(0),
947 CCOp.getOperand(1), CC);
948 return DAG.getNode(ISD::SELECT, VT, CCOp, OtherOp, Result);
949 }
Dan Gohman475871a2008-07-27 21:46:04 +0000950 return SDValue();
Evan Chengb13cdbd2007-06-21 07:39:16 +0000951}
952
Dan Gohman475871a2008-07-27 21:46:04 +0000953SDValue DAGCombiner::visitADD(SDNode *N) {
954 SDValue N0 = N->getOperand(0);
955 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000956 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
957 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000958 MVT VT = N0.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +0000959
960 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +0000961 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +0000962 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +0000963 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +0000964 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000965
Dan Gohman613e0d82007-07-03 14:03:57 +0000966 // fold (add x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +0000967 if (N0.getOpcode() == ISD::UNDEF)
968 return N0;
969 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +0000970 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000971 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000972 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +0000973 return DAG.FoldConstantArithmetic(ISD::ADD, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +0000974 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000975 if (N0C && !N1C)
976 return DAG.getNode(ISD::ADD, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000977 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000978 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000979 return N0;
Dan Gohman6520e202008-10-18 02:06:02 +0000980 // fold (add Sym, c) -> Sym+c
981 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0))
982 if (!AfterLegalize && TLI.isOffsetFoldingLegal(GA) && N1C &&
983 GA->getOpcode() == ISD::GlobalAddress)
984 return DAG.getGlobalAddress(GA->getGlobal(), VT,
985 GA->getOffset() +
986 (uint64_t)N1C->getSExtValue());
Chris Lattner4aafb4f2006-01-12 20:22:43 +0000987 // fold ((c1-A)+c2) -> (c1+c2)-A
988 if (N1C && N0.getOpcode() == ISD::SUB)
989 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
990 return DAG.getNode(ISD::SUB, VT,
Dan Gohman002e5d02008-03-13 22:13:53 +0000991 DAG.getConstant(N1C->getAPIntValue()+
992 N0C->getAPIntValue(), VT),
Chris Lattner4aafb4f2006-01-12 20:22:43 +0000993 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000994 // reassociate add
Dan Gohman475871a2008-07-27 21:46:04 +0000995 SDValue RADD = ReassociateOps(ISD::ADD, N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +0000996 if (RADD.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +0000997 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000998 // fold ((0-A) + B) -> B-A
999 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
1000 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +00001001 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001002 // fold (A + (0-B)) -> A-B
1003 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
1004 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +00001005 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +00001006 // fold (A+(B-A)) -> B
1007 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001008 return N1.getOperand(0);
Chris Lattner947c2892006-03-13 06:51:27 +00001009
Dan Gohman475871a2008-07-27 21:46:04 +00001010 if (!VT.isVector() && SimplifyDemandedBits(SDValue(N, 0)))
1011 return SDValue(N, 0);
Chris Lattner947c2892006-03-13 06:51:27 +00001012
1013 // fold (a+b) -> (a|b) iff a and b share no bits.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001014 if (VT.isInteger() && !VT.isVector()) {
Dan Gohman948d8ea2008-02-20 16:33:30 +00001015 APInt LHSZero, LHSOne;
1016 APInt RHSZero, RHSOne;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001017 APInt Mask = APInt::getAllOnesValue(VT.getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00001018 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Dan Gohman948d8ea2008-02-20 16:33:30 +00001019 if (LHSZero.getBoolValue()) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001020 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
Chris Lattner947c2892006-03-13 06:51:27 +00001021
1022 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1023 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
1024 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
1025 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
1026 return DAG.getNode(ISD::OR, VT, N0, N1);
1027 }
1028 }
Evan Cheng3ef554d2006-11-06 08:14:30 +00001029
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001030 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
Gabor Greifba36cb52008-08-28 21:40:38 +00001031 if (N0.getOpcode() == ISD::SHL && N0.getNode()->hasOneUse()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001032 SDValue Result = combineShlAddConstant(N0, N1, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001033 if (Result.getNode()) return Result;
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001034 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001035 if (N1.getOpcode() == ISD::SHL && N1.getNode()->hasOneUse()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001036 SDValue Result = combineShlAddConstant(N1, N0, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001037 if (Result.getNode()) return Result;
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001038 }
1039
Evan Chengb13cdbd2007-06-21 07:39:16 +00001040 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
Gabor Greifba36cb52008-08-28 21:40:38 +00001041 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
Bill Wendlingae89bb12008-11-11 08:25:46 +00001042 SDValue Result = combineSelectAndUse(N, N0, N1, DAG, TLI, AfterLegalize);
Gabor Greifba36cb52008-08-28 21:40:38 +00001043 if (Result.getNode()) return Result;
Evan Chengb13cdbd2007-06-21 07:39:16 +00001044 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001045 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
Bill Wendlingae89bb12008-11-11 08:25:46 +00001046 SDValue Result = combineSelectAndUse(N, N1, N0, DAG, TLI, AfterLegalize);
Gabor Greifba36cb52008-08-28 21:40:38 +00001047 if (Result.getNode()) return Result;
Evan Chengb13cdbd2007-06-21 07:39:16 +00001048 }
1049
Dan Gohman475871a2008-07-27 21:46:04 +00001050 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001051}
1052
Dan Gohman475871a2008-07-27 21:46:04 +00001053SDValue DAGCombiner::visitADDC(SDNode *N) {
1054 SDValue N0 = N->getOperand(0);
1055 SDValue N1 = N->getOperand(1);
Chris Lattner91153682007-03-04 20:03:15 +00001056 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1057 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001058 MVT VT = N0.getValueType();
Chris Lattner91153682007-03-04 20:03:15 +00001059
1060 // If the flag result is dead, turn this into an ADD.
1061 if (N->hasNUsesOfValue(0, 1))
1062 return CombineTo(N, DAG.getNode(ISD::ADD, VT, N1, N0),
Chris Lattnerb6541762007-03-04 20:40:38 +00001063 DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
Chris Lattner91153682007-03-04 20:03:15 +00001064
1065 // canonicalize constant to RHS.
Dan Gohman0a4627d2008-06-23 15:29:14 +00001066 if (N0C && !N1C)
Dan Gohman56867522008-06-21 22:06:07 +00001067 return DAG.getNode(ISD::ADDC, N->getVTList(), N1, N0);
Chris Lattner91153682007-03-04 20:03:15 +00001068
Chris Lattnerb6541762007-03-04 20:40:38 +00001069 // fold (addc x, 0) -> x + no carry out
1070 if (N1C && N1C->isNullValue())
1071 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
1072
1073 // fold (addc a, b) -> (or a, b), CARRY_FALSE iff a and b share no bits.
Dan Gohman948d8ea2008-02-20 16:33:30 +00001074 APInt LHSZero, LHSOne;
1075 APInt RHSZero, RHSOne;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001076 APInt Mask = APInt::getAllOnesValue(VT.getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00001077 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Dan Gohman948d8ea2008-02-20 16:33:30 +00001078 if (LHSZero.getBoolValue()) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001079 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
Chris Lattnerb6541762007-03-04 20:40:38 +00001080
1081 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1082 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
1083 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
1084 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
1085 return CombineTo(N, DAG.getNode(ISD::OR, VT, N0, N1),
1086 DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
1087 }
Chris Lattner91153682007-03-04 20:03:15 +00001088
Dan Gohman475871a2008-07-27 21:46:04 +00001089 return SDValue();
Chris Lattner91153682007-03-04 20:03:15 +00001090}
1091
Dan Gohman475871a2008-07-27 21:46:04 +00001092SDValue DAGCombiner::visitADDE(SDNode *N) {
1093 SDValue N0 = N->getOperand(0);
1094 SDValue N1 = N->getOperand(1);
1095 SDValue CarryIn = N->getOperand(2);
Chris Lattner91153682007-03-04 20:03:15 +00001096 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1097 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001098 //MVT VT = N0.getValueType();
Chris Lattner91153682007-03-04 20:03:15 +00001099
1100 // canonicalize constant to RHS
Dan Gohman0a4627d2008-06-23 15:29:14 +00001101 if (N0C && !N1C)
Dan Gohman56867522008-06-21 22:06:07 +00001102 return DAG.getNode(ISD::ADDE, N->getVTList(), N1, N0, CarryIn);
Chris Lattner91153682007-03-04 20:03:15 +00001103
Chris Lattnerb6541762007-03-04 20:40:38 +00001104 // fold (adde x, y, false) -> (addc x, y)
Dan Gohman0a4627d2008-06-23 15:29:14 +00001105 if (CarryIn.getOpcode() == ISD::CARRY_FALSE)
Dan Gohman56867522008-06-21 22:06:07 +00001106 return DAG.getNode(ISD::ADDC, N->getVTList(), N1, N0);
Chris Lattner91153682007-03-04 20:03:15 +00001107
Dan Gohman475871a2008-07-27 21:46:04 +00001108 return SDValue();
Chris Lattner91153682007-03-04 20:03:15 +00001109}
1110
1111
1112
Dan Gohman475871a2008-07-27 21:46:04 +00001113SDValue DAGCombiner::visitSUB(SDNode *N) {
1114 SDValue N0 = N->getOperand(0);
1115 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001116 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1117 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Duncan Sands83ec4b62008-06-06 12:08:01 +00001118 MVT VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001119
Dan Gohman7f321562007-06-25 16:23:39 +00001120 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001121 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001122 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001123 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001124 }
Dan Gohman7f321562007-06-25 16:23:39 +00001125
Chris Lattner854077d2005-10-17 01:07:11 +00001126 // fold (sub x, x) -> 0
Evan Chengc8e3b142008-03-12 07:02:50 +00001127 if (N0 == N1)
Chris Lattner854077d2005-10-17 01:07:11 +00001128 return DAG.getConstant(0, N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001129 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001130 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001131 return DAG.FoldConstantArithmetic(ISD::SUB, VT, N0C, N1C);
Chris Lattner05b57432005-10-11 06:07:15 +00001132 // fold (sub x, c) -> (add x, -c)
1133 if (N1C)
Dan Gohman002e5d02008-03-13 22:13:53 +00001134 return DAG.getNode(ISD::ADD, VT, N0,
1135 DAG.getConstant(-N1C->getAPIntValue(), VT));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001136 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +00001137 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001138 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001139 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +00001140 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001141 return N0.getOperand(0);
Evan Chengb13cdbd2007-06-21 07:39:16 +00001142 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
Gabor Greifba36cb52008-08-28 21:40:38 +00001143 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
Bill Wendlingae89bb12008-11-11 08:25:46 +00001144 SDValue Result = combineSelectAndUse(N, N1, N0, DAG, TLI, AfterLegalize);
Gabor Greifba36cb52008-08-28 21:40:38 +00001145 if (Result.getNode()) return Result;
Evan Chengb13cdbd2007-06-21 07:39:16 +00001146 }
Dan Gohman613e0d82007-07-03 14:03:57 +00001147 // If either operand of a sub is undef, the result is undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00001148 if (N0.getOpcode() == ISD::UNDEF)
1149 return N0;
1150 if (N1.getOpcode() == ISD::UNDEF)
1151 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001152
Dan Gohman6520e202008-10-18 02:06:02 +00001153 // If the relocation model supports it, consider symbol offsets.
1154 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0))
1155 if (!AfterLegalize && TLI.isOffsetFoldingLegal(GA)) {
1156 // fold (sub Sym, c) -> Sym-c
1157 if (N1C && GA->getOpcode() == ISD::GlobalAddress)
1158 return DAG.getGlobalAddress(GA->getGlobal(), VT,
1159 GA->getOffset() -
1160 (uint64_t)N1C->getSExtValue());
1161 // fold (sub Sym+c1, Sym+c2) -> c1-c2
1162 if (GlobalAddressSDNode *GB = dyn_cast<GlobalAddressSDNode>(N1))
1163 if (GA->getGlobal() == GB->getGlobal())
1164 return DAG.getConstant((uint64_t)GA->getOffset() - GB->getOffset(),
1165 VT);
1166 }
1167
Dan Gohman475871a2008-07-27 21:46:04 +00001168 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001169}
1170
Dan Gohman475871a2008-07-27 21:46:04 +00001171SDValue DAGCombiner::visitMUL(SDNode *N) {
1172 SDValue N0 = N->getOperand(0);
1173 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001174 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1175 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001176 MVT VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001177
Dan Gohman7f321562007-06-25 16:23:39 +00001178 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001179 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001180 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001181 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001182 }
Dan Gohman7f321562007-06-25 16:23:39 +00001183
Dan Gohman613e0d82007-07-03 14:03:57 +00001184 // fold (mul x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001185 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001186 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001187 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001188 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001189 return DAG.FoldConstantArithmetic(ISD::MUL, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00001190 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001191 if (N0C && !N1C)
1192 return DAG.getNode(ISD::MUL, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001193 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001194 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001195 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001196 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +00001197 if (N1C && N1C->isAllOnesValue())
Nate Begeman405e3ec2005-10-21 00:02:42 +00001198 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001199 // fold (mul x, (1 << c)) -> x << c
Dan Gohman002e5d02008-03-13 22:13:53 +00001200 if (N1C && N1C->getAPIntValue().isPowerOf2())
Chris Lattner3e6099b2005-10-30 06:41:49 +00001201 return DAG.getNode(ISD::SHL, VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001202 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001203 TLI.getShiftAmountTy()));
Chris Lattner3e6099b2005-10-30 06:41:49 +00001204 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
Dan Gohman7810bfe2008-09-26 21:54:37 +00001205 if (N1C && isPowerOf2_64(-N1C->getSExtValue())) {
Chris Lattner3e6099b2005-10-30 06:41:49 +00001206 // FIXME: If the input is something that is easily negated (e.g. a
1207 // single-use add), we should put the negate there.
1208 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT),
1209 DAG.getNode(ISD::SHL, VT, N0,
Dan Gohman7810bfe2008-09-26 21:54:37 +00001210 DAG.getConstant(Log2_64(-N1C->getSExtValue()),
Chris Lattner3e6099b2005-10-30 06:41:49 +00001211 TLI.getShiftAmountTy())));
1212 }
Andrew Lenharth50a0d422006-04-02 21:42:45 +00001213
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001214 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
1215 if (N1C && N0.getOpcode() == ISD::SHL &&
1216 isa<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohman475871a2008-07-27 21:46:04 +00001217 SDValue C3 = DAG.getNode(ISD::SHL, VT, N1, N0.getOperand(1));
Gabor Greifba36cb52008-08-28 21:40:38 +00001218 AddToWorkList(C3.getNode());
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001219 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0), C3);
1220 }
1221
1222 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
1223 // use.
1224 {
Dan Gohman475871a2008-07-27 21:46:04 +00001225 SDValue Sh(0,0), Y(0,0);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001226 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
1227 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001228 N0.getNode()->hasOneUse()) {
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001229 Sh = N0; Y = N1;
1230 } else if (N1.getOpcode() == ISD::SHL &&
Gabor Greif12632d22008-08-30 19:29:20 +00001231 isa<ConstantSDNode>(N1.getOperand(1)) &&
1232 N1.getNode()->hasOneUse()) {
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001233 Sh = N1; Y = N0;
1234 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001235 if (Sh.getNode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001236 SDValue Mul = DAG.getNode(ISD::MUL, VT, Sh.getOperand(0), Y);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001237 return DAG.getNode(ISD::SHL, VT, Mul, Sh.getOperand(1));
1238 }
1239 }
Chris Lattnera1deca32006-03-04 23:33:26 +00001240 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
Gabor Greifba36cb52008-08-28 21:40:38 +00001241 if (N1C && N0.getOpcode() == ISD::ADD && N0.getNode()->hasOneUse() &&
Chris Lattnera1deca32006-03-04 23:33:26 +00001242 isa<ConstantSDNode>(N0.getOperand(1))) {
1243 return DAG.getNode(ISD::ADD, VT,
1244 DAG.getNode(ISD::MUL, VT, N0.getOperand(0), N1),
1245 DAG.getNode(ISD::MUL, VT, N0.getOperand(1), N1));
1246 }
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001247
Nate Begemancd4d58c2006-02-03 06:46:56 +00001248 // reassociate mul
Dan Gohman475871a2008-07-27 21:46:04 +00001249 SDValue RMUL = ReassociateOps(ISD::MUL, N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001250 if (RMUL.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001251 return RMUL;
Dan Gohman7f321562007-06-25 16:23:39 +00001252
Dan Gohman475871a2008-07-27 21:46:04 +00001253 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001254}
1255
Dan Gohman475871a2008-07-27 21:46:04 +00001256SDValue DAGCombiner::visitSDIV(SDNode *N) {
1257 SDValue N0 = N->getOperand(0);
1258 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001259 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1260 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Duncan Sands83ec4b62008-06-06 12:08:01 +00001261 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001262
Dan Gohman7f321562007-06-25 16:23:39 +00001263 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001264 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001265 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001266 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001267 }
Dan Gohman7f321562007-06-25 16:23:39 +00001268
Nate Begeman1d4d4142005-09-01 00:19:25 +00001269 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001270 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001271 return DAG.FoldConstantArithmetic(ISD::SDIV, VT, N0C, N1C);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001272 // fold (sdiv X, 1) -> X
Dan Gohman7810bfe2008-09-26 21:54:37 +00001273 if (N1C && N1C->getSExtValue() == 1LL)
Nate Begeman405e3ec2005-10-21 00:02:42 +00001274 return N0;
1275 // fold (sdiv X, -1) -> 0-X
1276 if (N1C && N1C->isAllOnesValue())
1277 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +00001278 // If we know the sign bits of both operands are zero, strength reduce to a
1279 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
Duncan Sands83ec4b62008-06-06 12:08:01 +00001280 if (!VT.isVector()) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001281 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Chris Lattnerf32aac32008-01-27 23:32:17 +00001282 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
1283 }
Nate Begemancd6a6ed2006-02-17 07:26:20 +00001284 // fold (sdiv X, pow2) -> simple ops after legalize
Dan Gohman002e5d02008-03-13 22:13:53 +00001285 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap() &&
Dan Gohman7810bfe2008-09-26 21:54:37 +00001286 (isPowerOf2_64(N1C->getSExtValue()) ||
1287 isPowerOf2_64(-N1C->getSExtValue()))) {
Nate Begeman405e3ec2005-10-21 00:02:42 +00001288 // If dividing by powers of two is cheap, then don't perform the following
1289 // fold.
1290 if (TLI.isPow2DivCheap())
Dan Gohman475871a2008-07-27 21:46:04 +00001291 return SDValue();
Dan Gohman7810bfe2008-09-26 21:54:37 +00001292 int64_t pow2 = N1C->getSExtValue();
Nate Begeman405e3ec2005-10-21 00:02:42 +00001293 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
Chris Lattner8f4880b2006-02-16 08:02:36 +00001294 unsigned lg2 = Log2_64(abs2);
1295 // Splat the sign bit into the register
Dan Gohman475871a2008-07-27 21:46:04 +00001296 SDValue SGN = DAG.getNode(ISD::SRA, VT, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001297 DAG.getConstant(VT.getSizeInBits()-1,
Nate Begeman405e3ec2005-10-21 00:02:42 +00001298 TLI.getShiftAmountTy()));
Gabor Greifba36cb52008-08-28 21:40:38 +00001299 AddToWorkList(SGN.getNode());
Chris Lattner8f4880b2006-02-16 08:02:36 +00001300 // Add (N0 < 0) ? abs2 - 1 : 0;
Dan Gohman475871a2008-07-27 21:46:04 +00001301 SDValue SRL = DAG.getNode(ISD::SRL, VT, SGN,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001302 DAG.getConstant(VT.getSizeInBits()-lg2,
Nate Begeman405e3ec2005-10-21 00:02:42 +00001303 TLI.getShiftAmountTy()));
Dan Gohman475871a2008-07-27 21:46:04 +00001304 SDValue ADD = DAG.getNode(ISD::ADD, VT, N0, SRL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001305 AddToWorkList(SRL.getNode());
1306 AddToWorkList(ADD.getNode()); // Divide by pow2
Dan Gohman475871a2008-07-27 21:46:04 +00001307 SDValue SRA = DAG.getNode(ISD::SRA, VT, ADD,
Chris Lattner8f4880b2006-02-16 08:02:36 +00001308 DAG.getConstant(lg2, TLI.getShiftAmountTy()));
Nate Begeman405e3ec2005-10-21 00:02:42 +00001309 // If we're dividing by a positive value, we're done. Otherwise, we must
1310 // negate the result.
1311 if (pow2 > 0)
1312 return SRA;
Gabor Greifba36cb52008-08-28 21:40:38 +00001313 AddToWorkList(SRA.getNode());
Nate Begeman405e3ec2005-10-21 00:02:42 +00001314 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), SRA);
1315 }
Nate Begeman69575232005-10-20 02:15:44 +00001316 // if integer divide is expensive and we satisfy the requirements, emit an
1317 // alternate sequence.
Dan Gohman7810bfe2008-09-26 21:54:37 +00001318 if (N1C && (N1C->getSExtValue() < -1 || N1C->getSExtValue() > 1) &&
Chris Lattnere9936d12005-10-22 18:50:15 +00001319 !TLI.isIntDivCheap()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001320 SDValue Op = BuildSDIV(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001321 if (Op.getNode()) return Op;
Nate Begeman69575232005-10-20 02:15:44 +00001322 }
Dan Gohman7f321562007-06-25 16:23:39 +00001323
Dan Gohman613e0d82007-07-03 14:03:57 +00001324 // undef / X -> 0
1325 if (N0.getOpcode() == ISD::UNDEF)
1326 return DAG.getConstant(0, VT);
1327 // X / undef -> undef
1328 if (N1.getOpcode() == ISD::UNDEF)
1329 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001330
Dan Gohman475871a2008-07-27 21:46:04 +00001331 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001332}
1333
Dan Gohman475871a2008-07-27 21:46:04 +00001334SDValue DAGCombiner::visitUDIV(SDNode *N) {
1335 SDValue N0 = N->getOperand(0);
1336 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001337 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1338 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Duncan Sands83ec4b62008-06-06 12:08:01 +00001339 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001340
Dan Gohman7f321562007-06-25 16:23:39 +00001341 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001342 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001343 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001344 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001345 }
Dan Gohman7f321562007-06-25 16:23:39 +00001346
Nate Begeman1d4d4142005-09-01 00:19:25 +00001347 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001348 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001349 return DAG.FoldConstantArithmetic(ISD::UDIV, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001350 // fold (udiv x, (1 << c)) -> x >>u c
Dan Gohman002e5d02008-03-13 22:13:53 +00001351 if (N1C && N1C->getAPIntValue().isPowerOf2())
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001352 return DAG.getNode(ISD::SRL, VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001353 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001354 TLI.getShiftAmountTy()));
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001355 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
1356 if (N1.getOpcode() == ISD::SHL) {
1357 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00001358 if (SHC->getAPIntValue().isPowerOf2()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001359 MVT ADDVT = N1.getOperand(1).getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00001360 SDValue Add = DAG.getNode(ISD::ADD, ADDVT, N1.getOperand(1),
Dan Gohman002e5d02008-03-13 22:13:53 +00001361 DAG.getConstant(SHC->getAPIntValue()
1362 .logBase2(),
Nate Begemanc031e332006-02-05 07:36:48 +00001363 ADDVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00001364 AddToWorkList(Add.getNode());
Nate Begemanc031e332006-02-05 07:36:48 +00001365 return DAG.getNode(ISD::SRL, VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001366 }
1367 }
1368 }
Nate Begeman69575232005-10-20 02:15:44 +00001369 // fold (udiv x, c) -> alternate
Dan Gohman002e5d02008-03-13 22:13:53 +00001370 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001371 SDValue Op = BuildUDIV(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001372 if (Op.getNode()) return Op;
Chris Lattnere9936d12005-10-22 18:50:15 +00001373 }
Dan Gohman7f321562007-06-25 16:23:39 +00001374
Dan Gohman613e0d82007-07-03 14:03:57 +00001375 // undef / X -> 0
1376 if (N0.getOpcode() == ISD::UNDEF)
1377 return DAG.getConstant(0, VT);
1378 // X / undef -> undef
1379 if (N1.getOpcode() == ISD::UNDEF)
1380 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001381
Dan Gohman475871a2008-07-27 21:46:04 +00001382 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001383}
1384
Dan Gohman475871a2008-07-27 21:46:04 +00001385SDValue DAGCombiner::visitSREM(SDNode *N) {
1386 SDValue N0 = N->getOperand(0);
1387 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001388 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1389 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001390 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001391
1392 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001393 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001394 return DAG.FoldConstantArithmetic(ISD::SREM, VT, N0C, N1C);
Nate Begeman07ed4172005-10-10 21:26:48 +00001395 // If we know the sign bits of both operands are zero, strength reduce to a
1396 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
Duncan Sands83ec4b62008-06-06 12:08:01 +00001397 if (!VT.isVector()) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001398 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Chris Lattneree339f42008-01-27 23:21:58 +00001399 return DAG.getNode(ISD::UREM, VT, N0, N1);
1400 }
Chris Lattner26d29902006-10-12 20:58:32 +00001401
Dan Gohman77003042007-11-26 23:46:11 +00001402 // If X/C can be simplified by the division-by-constant logic, lower
1403 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00001404 if (N1C && !N1C->isNullValue()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001405 SDValue Div = DAG.getNode(ISD::SDIV, VT, N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001406 AddToWorkList(Div.getNode());
1407 SDValue OptimizedDiv = combine(Div.getNode());
1408 if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001409 SDValue Mul = DAG.getNode(ISD::MUL, VT, OptimizedDiv, N1);
1410 SDValue Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
Gabor Greifba36cb52008-08-28 21:40:38 +00001411 AddToWorkList(Mul.getNode());
Dan Gohman77003042007-11-26 23:46:11 +00001412 return Sub;
1413 }
Chris Lattner26d29902006-10-12 20:58:32 +00001414 }
1415
Dan Gohman613e0d82007-07-03 14:03:57 +00001416 // undef % X -> 0
1417 if (N0.getOpcode() == ISD::UNDEF)
1418 return DAG.getConstant(0, VT);
1419 // X % undef -> undef
1420 if (N1.getOpcode() == ISD::UNDEF)
1421 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001422
Dan Gohman475871a2008-07-27 21:46:04 +00001423 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001424}
1425
Dan Gohman475871a2008-07-27 21:46:04 +00001426SDValue DAGCombiner::visitUREM(SDNode *N) {
1427 SDValue N0 = N->getOperand(0);
1428 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001429 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1430 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001431 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001432
1433 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001434 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001435 return DAG.FoldConstantArithmetic(ISD::UREM, VT, N0C, N1C);
Nate Begeman07ed4172005-10-10 21:26:48 +00001436 // fold (urem x, pow2) -> (and x, pow2-1)
Dan Gohman002e5d02008-03-13 22:13:53 +00001437 if (N1C && !N1C->isNullValue() && N1C->getAPIntValue().isPowerOf2())
1438 return DAG.getNode(ISD::AND, VT, N0,
1439 DAG.getConstant(N1C->getAPIntValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +00001440 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
1441 if (N1.getOpcode() == ISD::SHL) {
1442 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00001443 if (SHC->getAPIntValue().isPowerOf2()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001444 SDValue Add =
Dan Gohman002e5d02008-03-13 22:13:53 +00001445 DAG.getNode(ISD::ADD, VT, N1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001446 DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()),
Dan Gohman002e5d02008-03-13 22:13:53 +00001447 VT));
Gabor Greifba36cb52008-08-28 21:40:38 +00001448 AddToWorkList(Add.getNode());
Nate Begemanc031e332006-02-05 07:36:48 +00001449 return DAG.getNode(ISD::AND, VT, N0, Add);
1450 }
1451 }
1452 }
Chris Lattner26d29902006-10-12 20:58:32 +00001453
Dan Gohman77003042007-11-26 23:46:11 +00001454 // If X/C can be simplified by the division-by-constant logic, lower
1455 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00001456 if (N1C && !N1C->isNullValue()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001457 SDValue Div = DAG.getNode(ISD::UDIV, VT, N0, N1);
Dan Gohman942ca7f2008-09-08 16:59:01 +00001458 AddToWorkList(Div.getNode());
Gabor Greifba36cb52008-08-28 21:40:38 +00001459 SDValue OptimizedDiv = combine(Div.getNode());
1460 if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001461 SDValue Mul = DAG.getNode(ISD::MUL, VT, OptimizedDiv, N1);
1462 SDValue Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
Gabor Greifba36cb52008-08-28 21:40:38 +00001463 AddToWorkList(Mul.getNode());
Dan Gohman77003042007-11-26 23:46:11 +00001464 return Sub;
1465 }
Chris Lattner26d29902006-10-12 20:58:32 +00001466 }
1467
Dan Gohman613e0d82007-07-03 14:03:57 +00001468 // undef % X -> 0
1469 if (N0.getOpcode() == ISD::UNDEF)
1470 return DAG.getConstant(0, VT);
1471 // X % undef -> undef
1472 if (N1.getOpcode() == ISD::UNDEF)
1473 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001474
Dan Gohman475871a2008-07-27 21:46:04 +00001475 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001476}
1477
Dan Gohman475871a2008-07-27 21:46:04 +00001478SDValue DAGCombiner::visitMULHS(SDNode *N) {
1479 SDValue N0 = N->getOperand(0);
1480 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001481 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001482 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001483
1484 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001485 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001486 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001487 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Dan Gohman002e5d02008-03-13 22:13:53 +00001488 if (N1C && N1C->getAPIntValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001489 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001490 DAG.getConstant(N0.getValueType().getSizeInBits()-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001491 TLI.getShiftAmountTy()));
Dan Gohman613e0d82007-07-03 14:03:57 +00001492 // fold (mulhs x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001493 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001494 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00001495
Dan Gohman475871a2008-07-27 21:46:04 +00001496 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001497}
1498
Dan Gohman475871a2008-07-27 21:46:04 +00001499SDValue DAGCombiner::visitMULHU(SDNode *N) {
1500 SDValue N0 = N->getOperand(0);
1501 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001502 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001503 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001504
1505 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001506 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001507 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001508 // fold (mulhu x, 1) -> 0
Dan Gohman002e5d02008-03-13 22:13:53 +00001509 if (N1C && N1C->getAPIntValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001510 return DAG.getConstant(0, N0.getValueType());
Dan Gohman613e0d82007-07-03 14:03:57 +00001511 // fold (mulhu x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001512 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001513 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00001514
Dan Gohman475871a2008-07-27 21:46:04 +00001515 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001516}
1517
Dan Gohman389079b2007-10-08 17:57:15 +00001518/// SimplifyNodeWithTwoResults - Perform optimizations common to nodes that
1519/// compute two values. LoOp and HiOp give the opcodes for the two computations
1520/// that are being performed. Return true if a simplification was made.
1521///
Dan Gohman475871a2008-07-27 21:46:04 +00001522SDValue DAGCombiner::SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
1523 unsigned HiOp) {
Dan Gohman389079b2007-10-08 17:57:15 +00001524 // If the high half is not needed, just compute the low half.
Evan Cheng44711942007-11-08 09:25:29 +00001525 bool HiExists = N->hasAnyUseOfValue(1);
1526 if (!HiExists &&
Dan Gohman389079b2007-10-08 17:57:15 +00001527 (!AfterLegalize ||
1528 TLI.isOperationLegal(LoOp, N->getValueType(0)))) {
Dan Gohman475871a2008-07-27 21:46:04 +00001529 SDValue Res = DAG.getNode(LoOp, N->getValueType(0), N->op_begin(),
Chris Lattner5eee4272008-01-26 01:09:19 +00001530 N->getNumOperands());
1531 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00001532 }
1533
1534 // If the low half is not needed, just compute the high half.
Evan Cheng44711942007-11-08 09:25:29 +00001535 bool LoExists = N->hasAnyUseOfValue(0);
1536 if (!LoExists &&
Dan Gohman389079b2007-10-08 17:57:15 +00001537 (!AfterLegalize ||
1538 TLI.isOperationLegal(HiOp, N->getValueType(1)))) {
Dan Gohman475871a2008-07-27 21:46:04 +00001539 SDValue Res = DAG.getNode(HiOp, N->getValueType(1), N->op_begin(),
Chris Lattner5eee4272008-01-26 01:09:19 +00001540 N->getNumOperands());
1541 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00001542 }
1543
Evan Cheng44711942007-11-08 09:25:29 +00001544 // If both halves are used, return as it is.
1545 if (LoExists && HiExists)
Dan Gohman475871a2008-07-27 21:46:04 +00001546 return SDValue();
Evan Cheng44711942007-11-08 09:25:29 +00001547
1548 // If the two computed results can be simplified separately, separate them.
Evan Cheng44711942007-11-08 09:25:29 +00001549 if (LoExists) {
Dan Gohman475871a2008-07-27 21:46:04 +00001550 SDValue Lo = DAG.getNode(LoOp, N->getValueType(0),
Evan Cheng44711942007-11-08 09:25:29 +00001551 N->op_begin(), N->getNumOperands());
Gabor Greifba36cb52008-08-28 21:40:38 +00001552 AddToWorkList(Lo.getNode());
1553 SDValue LoOpt = combine(Lo.getNode());
1554 if (LoOpt.getNode() && LoOpt.getNode() != Lo.getNode() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001555 (!AfterLegalize ||
1556 TLI.isOperationLegal(LoOpt.getOpcode(), LoOpt.getValueType())))
Chris Lattner5eee4272008-01-26 01:09:19 +00001557 return CombineTo(N, LoOpt, LoOpt);
Dan Gohman389079b2007-10-08 17:57:15 +00001558 }
1559
Evan Cheng44711942007-11-08 09:25:29 +00001560 if (HiExists) {
Dan Gohman475871a2008-07-27 21:46:04 +00001561 SDValue Hi = DAG.getNode(HiOp, N->getValueType(1),
Evan Cheng44711942007-11-08 09:25:29 +00001562 N->op_begin(), N->getNumOperands());
Gabor Greifba36cb52008-08-28 21:40:38 +00001563 AddToWorkList(Hi.getNode());
1564 SDValue HiOpt = combine(Hi.getNode());
1565 if (HiOpt.getNode() && HiOpt != Hi &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001566 (!AfterLegalize ||
1567 TLI.isOperationLegal(HiOpt.getOpcode(), HiOpt.getValueType())))
Chris Lattner5eee4272008-01-26 01:09:19 +00001568 return CombineTo(N, HiOpt, HiOpt);
Evan Cheng44711942007-11-08 09:25:29 +00001569 }
Dan Gohman475871a2008-07-27 21:46:04 +00001570 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001571}
1572
Dan Gohman475871a2008-07-27 21:46:04 +00001573SDValue DAGCombiner::visitSMUL_LOHI(SDNode *N) {
1574 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHS);
Gabor Greifba36cb52008-08-28 21:40:38 +00001575 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001576
Dan Gohman475871a2008-07-27 21:46:04 +00001577 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001578}
1579
Dan Gohman475871a2008-07-27 21:46:04 +00001580SDValue DAGCombiner::visitUMUL_LOHI(SDNode *N) {
1581 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHU);
Gabor Greifba36cb52008-08-28 21:40:38 +00001582 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001583
Dan Gohman475871a2008-07-27 21:46:04 +00001584 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001585}
1586
Dan Gohman475871a2008-07-27 21:46:04 +00001587SDValue DAGCombiner::visitSDIVREM(SDNode *N) {
1588 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::SDIV, ISD::SREM);
Gabor Greifba36cb52008-08-28 21:40:38 +00001589 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001590
Dan Gohman475871a2008-07-27 21:46:04 +00001591 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001592}
1593
Dan Gohman475871a2008-07-27 21:46:04 +00001594SDValue DAGCombiner::visitUDIVREM(SDNode *N) {
1595 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::UDIV, ISD::UREM);
Gabor Greifba36cb52008-08-28 21:40:38 +00001596 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001597
Dan Gohman475871a2008-07-27 21:46:04 +00001598 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001599}
1600
Chris Lattner35e5c142006-05-05 05:51:50 +00001601/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
1602/// two operands of the same opcode, try to simplify it.
Dan Gohman475871a2008-07-27 21:46:04 +00001603SDValue DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
1604 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001605 MVT VT = N0.getValueType();
Chris Lattner35e5c142006-05-05 05:51:50 +00001606 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
1607
Chris Lattner540121f2006-05-05 06:31:05 +00001608 // For each of OP in AND/OR/XOR:
1609 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
1610 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
1611 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
Chris Lattner0d8dae72006-05-05 06:32:04 +00001612 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y))
Chris Lattner540121f2006-05-05 06:31:05 +00001613 if ((N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND||
Chris Lattner0d8dae72006-05-05 06:32:04 +00001614 N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::TRUNCATE) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001615 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001616 SDValue ORNode = DAG.getNode(N->getOpcode(),
Chris Lattner35e5c142006-05-05 05:51:50 +00001617 N0.getOperand(0).getValueType(),
1618 N0.getOperand(0), N1.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00001619 AddToWorkList(ORNode.getNode());
Chris Lattner540121f2006-05-05 06:31:05 +00001620 return DAG.getNode(N0.getOpcode(), VT, ORNode);
Chris Lattner35e5c142006-05-05 05:51:50 +00001621 }
1622
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001623 // For each of OP in SHL/SRL/SRA/AND...
1624 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
1625 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
1626 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
Chris Lattner35e5c142006-05-05 05:51:50 +00001627 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001628 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001629 N0.getOperand(1) == N1.getOperand(1)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001630 SDValue ORNode = DAG.getNode(N->getOpcode(),
Chris Lattner35e5c142006-05-05 05:51:50 +00001631 N0.getOperand(0).getValueType(),
1632 N0.getOperand(0), N1.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00001633 AddToWorkList(ORNode.getNode());
Chris Lattner35e5c142006-05-05 05:51:50 +00001634 return DAG.getNode(N0.getOpcode(), VT, ORNode, N0.getOperand(1));
1635 }
1636
Dan Gohman475871a2008-07-27 21:46:04 +00001637 return SDValue();
Chris Lattner35e5c142006-05-05 05:51:50 +00001638}
1639
Dan Gohman475871a2008-07-27 21:46:04 +00001640SDValue DAGCombiner::visitAND(SDNode *N) {
1641 SDValue N0 = N->getOperand(0);
1642 SDValue N1 = N->getOperand(1);
1643 SDValue LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001644 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1645 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001646 MVT VT = N1.getValueType();
1647 unsigned BitWidth = VT.getSizeInBits();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001648
Dan Gohman7f321562007-06-25 16:23:39 +00001649 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001650 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001651 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001652 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001653 }
Dan Gohman7f321562007-06-25 16:23:39 +00001654
Dan Gohman613e0d82007-07-03 14:03:57 +00001655 // fold (and x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001656 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001657 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001658 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001659 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001660 return DAG.FoldConstantArithmetic(ISD::AND, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00001661 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001662 if (N0C && !N1C)
1663 return DAG.getNode(ISD::AND, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001664 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001665 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001666 return N0;
1667 // if (and x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00001668 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001669 APInt::getAllOnesValue(BitWidth)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001670 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +00001671 // reassociate and
Dan Gohman475871a2008-07-27 21:46:04 +00001672 SDValue RAND = ReassociateOps(ISD::AND, N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001673 if (RAND.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001674 return RAND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001675 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
Nate Begeman5dc7e862005-11-02 18:42:59 +00001676 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001677 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohman002e5d02008-03-13 22:13:53 +00001678 if ((ORI->getAPIntValue() & N1C->getAPIntValue()) == N1C->getAPIntValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001679 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +00001680 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
1681 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Dan Gohman475871a2008-07-27 21:46:04 +00001682 SDValue N0Op0 = N0.getOperand(0);
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001683 APInt Mask = ~N1C->getAPIntValue();
1684 Mask.trunc(N0Op0.getValueSizeInBits());
1685 if (DAG.MaskedValueIsZero(N0Op0, Mask)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001686 SDValue Zext = DAG.getNode(ISD::ZERO_EXTEND, N0.getValueType(),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001687 N0Op0);
Chris Lattner1ec05d12006-03-01 21:47:21 +00001688
1689 // Replace uses of the AND with uses of the Zero extend node.
1690 CombineTo(N, Zext);
1691
Chris Lattner3603cd62006-02-02 07:17:31 +00001692 // We actually want to replace all uses of the any_extend with the
1693 // zero_extend, to avoid duplicating things. This will later cause this
1694 // AND to be folded.
Gabor Greifba36cb52008-08-28 21:40:38 +00001695 CombineTo(N0.getNode(), Zext);
Dan Gohman475871a2008-07-27 21:46:04 +00001696 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner3603cd62006-02-02 07:17:31 +00001697 }
1698 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001699 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
1700 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1701 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1702 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1703
1704 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00001705 LL.getValueType().isInteger()) {
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001706 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
Dan Gohman002e5d02008-03-13 22:13:53 +00001707 if (cast<ConstantSDNode>(LR)->isNullValue() && Op1 == ISD::SETEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00001708 SDValue ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001709 AddToWorkList(ORNode.getNode());
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001710 return DAG.getSetCC(VT, ORNode, LR, Op1);
1711 }
1712 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
1713 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00001714 SDValue ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001715 AddToWorkList(ANDNode.getNode());
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001716 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1717 }
1718 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
1719 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
Dan Gohman475871a2008-07-27 21:46:04 +00001720 SDValue ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001721 AddToWorkList(ORNode.getNode());
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001722 return DAG.getSetCC(VT, ORNode, LR, Op1);
1723 }
1724 }
1725 // canonicalize equivalent to ll == rl
1726 if (LL == RR && LR == RL) {
1727 Op1 = ISD::getSetCCSwappedOperands(Op1);
1728 std::swap(RL, RR);
1729 }
1730 if (LL == RL && LR == RR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001731 bool isInteger = LL.getValueType().isInteger();
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001732 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
Chris Lattner6e1c6232008-10-28 07:11:07 +00001733 if (Result != ISD::SETCC_INVALID &&
1734 (!AfterLegalize || TLI.isCondCodeLegal(Result, LL.getValueType())))
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001735 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1736 }
1737 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001738
1739 // Simplify: and (op x...), (op y...) -> (op (and x, y))
1740 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001741 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001742 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001743 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001744
Nate Begemande996292006-02-03 22:24:05 +00001745 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
1746 // fold (and (sra)) -> (and (srl)) when possible.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001747 if (!VT.isVector() &&
Dan Gohman475871a2008-07-27 21:46:04 +00001748 SimplifyDemandedBits(SDValue(N, 0)))
1749 return SDValue(N, 0);
Nate Begemanded49632005-10-13 03:11:28 +00001750 // fold (zext_inreg (extload x)) -> (zextload x)
Gabor Greifba36cb52008-08-28 21:40:38 +00001751 if (ISD::isEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode())) {
Evan Cheng466685d2006-10-09 20:57:25 +00001752 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001753 MVT EVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001754 // If we zero all the possible extended bits, then we can turn this into
1755 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001756 unsigned BitWidth = N1.getValueSizeInBits();
1757 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001758 BitWidth - EVT.getSizeInBits())) &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001759 ((!AfterLegalize && !LN0->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00001760 TLI.isLoadExtLegal(ISD::ZEXTLOAD, EVT))) {
Dan Gohman475871a2008-07-27 21:46:04 +00001761 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
Evan Cheng466685d2006-10-09 20:57:25 +00001762 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00001763 LN0->getSrcValueOffset(), EVT,
1764 LN0->isVolatile(),
1765 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00001766 AddToWorkList(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001767 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00001768 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001769 }
1770 }
1771 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Gabor Greifba36cb52008-08-28 21:40:38 +00001772 if (ISD::isSEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng83060c52007-03-07 08:07:03 +00001773 N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001774 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001775 MVT EVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001776 // If we zero all the possible extended bits, then we can turn this into
1777 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001778 unsigned BitWidth = N1.getValueSizeInBits();
1779 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001780 BitWidth - EVT.getSizeInBits())) &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001781 ((!AfterLegalize && !LN0->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00001782 TLI.isLoadExtLegal(ISD::ZEXTLOAD, EVT))) {
Dan Gohman475871a2008-07-27 21:46:04 +00001783 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
Evan Cheng466685d2006-10-09 20:57:25 +00001784 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00001785 LN0->getSrcValueOffset(), EVT,
1786 LN0->isVolatile(),
1787 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00001788 AddToWorkList(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001789 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00001790 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001791 }
1792 }
Chris Lattner15045b62006-02-28 06:35:35 +00001793
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001794 // fold (and (load x), 255) -> (zextload x, i8)
1795 // fold (and (extload x, i16), 255) -> (zextload x, i8)
Evan Cheng466685d2006-10-09 20:57:25 +00001796 if (N1C && N0.getOpcode() == ISD::LOAD) {
1797 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
1798 if (LN0->getExtensionType() != ISD::SEXTLOAD &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001799 LN0->isUnindexed() && N0.hasOneUse() &&
1800 // Do not change the width of a volatile load.
1801 !LN0->isVolatile()) {
Duncan Sands8eab8a22008-06-09 11:32:28 +00001802 MVT EVT = MVT::Other;
1803 uint32_t ActiveBits = N1C->getAPIntValue().getActiveBits();
1804 if (ActiveBits > 0 && APIntOps::isMask(ActiveBits, N1C->getAPIntValue()))
1805 EVT = MVT::getIntegerVT(ActiveBits);
1806
1807 MVT LoadedVT = LN0->getMemoryVT();
Duncan Sandsad205a72008-06-16 08:14:38 +00001808 // Do not generate loads of non-round integer types since these can
1809 // be expensive (and would be wrong if the type is not byte sized).
1810 if (EVT != MVT::Other && LoadedVT.bitsGT(EVT) && EVT.isRound() &&
Evan Cheng03294662008-10-14 21:26:46 +00001811 (!AfterLegalize || TLI.isLoadExtLegal(ISD::ZEXTLOAD, EVT))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001812 MVT PtrType = N0.getOperand(1).getValueType();
Evan Cheng466685d2006-10-09 20:57:25 +00001813 // For big endian targets, we need to add an offset to the pointer to
1814 // load the correct bytes. For little endian systems, we merely need to
1815 // read fewer bytes from the same pointer.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001816 unsigned LVTStoreBytes = LoadedVT.getStoreSizeInBits()/8;
1817 unsigned EVTStoreBytes = EVT.getStoreSizeInBits()/8;
Duncan Sandsc6fa1702007-11-09 08:57:19 +00001818 unsigned PtrOff = LVTStoreBytes - EVTStoreBytes;
Duncan Sandsdc846502007-10-28 12:59:45 +00001819 unsigned Alignment = LN0->getAlignment();
Dan Gohman475871a2008-07-27 21:46:04 +00001820 SDValue NewPtr = LN0->getBasePtr();
Duncan Sands0753fc12008-02-11 10:37:04 +00001821 if (TLI.isBigEndian()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001822 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
1823 DAG.getConstant(PtrOff, PtrType));
Duncan Sandsdc846502007-10-28 12:59:45 +00001824 Alignment = MinAlign(Alignment, PtrOff);
1825 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001826 AddToWorkList(NewPtr.getNode());
Dan Gohman475871a2008-07-27 21:46:04 +00001827 SDValue Load =
Evan Cheng466685d2006-10-09 20:57:25 +00001828 DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00001829 LN0->getSrcValue(), LN0->getSrcValueOffset(), EVT,
Duncan Sandsdc846502007-10-28 12:59:45 +00001830 LN0->isVolatile(), Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00001831 AddToWorkList(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001832 CombineTo(N0.getNode(), Load, Load.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00001833 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng466685d2006-10-09 20:57:25 +00001834 }
Chris Lattner15045b62006-02-28 06:35:35 +00001835 }
1836 }
1837
Dan Gohman475871a2008-07-27 21:46:04 +00001838 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001839}
1840
Dan Gohman475871a2008-07-27 21:46:04 +00001841SDValue DAGCombiner::visitOR(SDNode *N) {
1842 SDValue N0 = N->getOperand(0);
1843 SDValue N1 = N->getOperand(1);
1844 SDValue LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001845 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1846 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001847 MVT VT = N1.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001848
Dan Gohman7f321562007-06-25 16:23:39 +00001849 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001850 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001851 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001852 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001853 }
Dan Gohman7f321562007-06-25 16:23:39 +00001854
Dan Gohman613e0d82007-07-03 14:03:57 +00001855 // fold (or x, undef) -> -1
Dan Gohmand595b5f2007-07-10 14:20:37 +00001856 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Chris Lattnere094f542007-07-09 16:16:34 +00001857 return DAG.getConstant(~0ULL, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001858 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001859 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001860 return DAG.FoldConstantArithmetic(ISD::OR, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00001861 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001862 if (N0C && !N1C)
1863 return DAG.getNode(ISD::OR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001864 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001865 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001866 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001867 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001868 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001869 return N1;
1870 // fold (or x, c) -> c iff (x & ~c) == 0
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001871 if (N1C && DAG.MaskedValueIsZero(N0, ~N1C->getAPIntValue()))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001872 return N1;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001873 // reassociate or
Dan Gohman475871a2008-07-27 21:46:04 +00001874 SDValue ROR = ReassociateOps(ISD::OR, N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001875 if (ROR.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001876 return ROR;
1877 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
Gabor Greifba36cb52008-08-28 21:40:38 +00001878 if (N1C && N0.getOpcode() == ISD::AND && N0.getNode()->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00001879 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00001880 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
1881 return DAG.getNode(ISD::AND, VT, DAG.getNode(ISD::OR, VT, N0.getOperand(0),
1882 N1),
Dan Gohman002e5d02008-03-13 22:13:53 +00001883 DAG.getConstant(N1C->getAPIntValue() |
1884 C1->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00001885 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001886 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
1887 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1888 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1889 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1890
1891 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00001892 LL.getValueType().isInteger()) {
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001893 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
1894 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
Dan Gohman002e5d02008-03-13 22:13:53 +00001895 if (cast<ConstantSDNode>(LR)->isNullValue() &&
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001896 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001897 SDValue ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001898 AddToWorkList(ORNode.getNode());
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001899 return DAG.getSetCC(VT, ORNode, LR, Op1);
1900 }
1901 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
1902 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
1903 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
1904 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001905 SDValue ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001906 AddToWorkList(ANDNode.getNode());
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001907 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1908 }
1909 }
1910 // canonicalize equivalent to ll == rl
1911 if (LL == RR && LR == RL) {
1912 Op1 = ISD::getSetCCSwappedOperands(Op1);
1913 std::swap(RL, RR);
1914 }
1915 if (LL == RL && LR == RR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001916 bool isInteger = LL.getValueType().isInteger();
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001917 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
Chris Lattner6e1c6232008-10-28 07:11:07 +00001918 if (Result != ISD::SETCC_INVALID &&
1919 (!AfterLegalize || TLI.isCondCodeLegal(Result, LL.getValueType())))
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001920 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1921 }
1922 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001923
1924 // Simplify: or (op x...), (op y...) -> (op (or x, y))
1925 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001926 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001927 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001928 }
Chris Lattner516b9622006-09-14 20:50:57 +00001929
Chris Lattner1ec72732006-09-14 21:11:37 +00001930 // (X & C1) | (Y & C2) -> (X|Y) & C3 if possible.
1931 if (N0.getOpcode() == ISD::AND &&
1932 N1.getOpcode() == ISD::AND &&
1933 N0.getOperand(1).getOpcode() == ISD::Constant &&
1934 N1.getOperand(1).getOpcode() == ISD::Constant &&
1935 // Don't increase # computations.
Gabor Greifba36cb52008-08-28 21:40:38 +00001936 (N0.getNode()->hasOneUse() || N1.getNode()->hasOneUse())) {
Chris Lattner1ec72732006-09-14 21:11:37 +00001937 // We can only do this xform if we know that bits from X that are set in C2
1938 // but not in C1 are already zero. Likewise for Y.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001939 const APInt &LHSMask =
1940 cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
1941 const APInt &RHSMask =
1942 cast<ConstantSDNode>(N1.getOperand(1))->getAPIntValue();
Chris Lattner1ec72732006-09-14 21:11:37 +00001943
Dan Gohmanea859be2007-06-22 14:59:07 +00001944 if (DAG.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) &&
1945 DAG.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001946 SDValue X =DAG.getNode(ISD::OR, VT, N0.getOperand(0), N1.getOperand(0));
Chris Lattner1ec72732006-09-14 21:11:37 +00001947 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(LHSMask|RHSMask, VT));
1948 }
1949 }
1950
1951
Chris Lattner516b9622006-09-14 20:50:57 +00001952 // See if this is some rotate idiom.
1953 if (SDNode *Rot = MatchRotate(N0, N1))
Dan Gohman475871a2008-07-27 21:46:04 +00001954 return SDValue(Rot, 0);
Chris Lattner35e5c142006-05-05 05:51:50 +00001955
Dan Gohman475871a2008-07-27 21:46:04 +00001956 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001957}
1958
Chris Lattner516b9622006-09-14 20:50:57 +00001959
1960/// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present.
Dan Gohman475871a2008-07-27 21:46:04 +00001961static bool MatchRotateHalf(SDValue Op, SDValue &Shift, SDValue &Mask) {
Chris Lattner516b9622006-09-14 20:50:57 +00001962 if (Op.getOpcode() == ISD::AND) {
Reid Spencer3ed469c2006-11-02 20:25:50 +00001963 if (isa<ConstantSDNode>(Op.getOperand(1))) {
Chris Lattner516b9622006-09-14 20:50:57 +00001964 Mask = Op.getOperand(1);
1965 Op = Op.getOperand(0);
1966 } else {
1967 return false;
1968 }
1969 }
1970
1971 if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) {
1972 Shift = Op;
1973 return true;
1974 }
1975 return false;
1976}
1977
1978
1979// MatchRotate - Handle an 'or' of two operands. If this is one of the many
1980// idioms for rotate, and if the target supports rotation instructions, generate
1981// a rot[lr].
Dan Gohman475871a2008-07-27 21:46:04 +00001982SDNode *DAGCombiner::MatchRotate(SDValue LHS, SDValue RHS) {
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001983 // Must be a legal type. Expanded 'n promoted things won't work with rotates.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001984 MVT VT = LHS.getValueType();
Chris Lattner516b9622006-09-14 20:50:57 +00001985 if (!TLI.isTypeLegal(VT)) return 0;
1986
1987 // The target must have at least one rotate flavor.
1988 bool HasROTL = TLI.isOperationLegal(ISD::ROTL, VT);
1989 bool HasROTR = TLI.isOperationLegal(ISD::ROTR, VT);
1990 if (!HasROTL && !HasROTR) return 0;
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001991
Chris Lattner516b9622006-09-14 20:50:57 +00001992 // Match "(X shl/srl V1) & V2" where V2 may not be present.
Dan Gohman475871a2008-07-27 21:46:04 +00001993 SDValue LHSShift; // The shift.
1994 SDValue LHSMask; // AND value if any.
Chris Lattner516b9622006-09-14 20:50:57 +00001995 if (!MatchRotateHalf(LHS, LHSShift, LHSMask))
1996 return 0; // Not part of a rotate.
1997
Dan Gohman475871a2008-07-27 21:46:04 +00001998 SDValue RHSShift; // The shift.
1999 SDValue RHSMask; // AND value if any.
Chris Lattner516b9622006-09-14 20:50:57 +00002000 if (!MatchRotateHalf(RHS, RHSShift, RHSMask))
2001 return 0; // Not part of a rotate.
2002
2003 if (LHSShift.getOperand(0) != RHSShift.getOperand(0))
2004 return 0; // Not shifting the same value.
2005
2006 if (LHSShift.getOpcode() == RHSShift.getOpcode())
2007 return 0; // Shifts must disagree.
2008
2009 // Canonicalize shl to left side in a shl/srl pair.
2010 if (RHSShift.getOpcode() == ISD::SHL) {
2011 std::swap(LHS, RHS);
2012 std::swap(LHSShift, RHSShift);
2013 std::swap(LHSMask , RHSMask );
2014 }
2015
Duncan Sands83ec4b62008-06-06 12:08:01 +00002016 unsigned OpSizeInBits = VT.getSizeInBits();
Dan Gohman475871a2008-07-27 21:46:04 +00002017 SDValue LHSShiftArg = LHSShift.getOperand(0);
2018 SDValue LHSShiftAmt = LHSShift.getOperand(1);
2019 SDValue RHSShiftAmt = RHSShift.getOperand(1);
Chris Lattner516b9622006-09-14 20:50:57 +00002020
2021 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
2022 // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2)
Scott Michelc9dc1142007-04-02 21:36:32 +00002023 if (LHSShiftAmt.getOpcode() == ISD::Constant &&
2024 RHSShiftAmt.getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002025 uint64_t LShVal = cast<ConstantSDNode>(LHSShiftAmt)->getZExtValue();
2026 uint64_t RShVal = cast<ConstantSDNode>(RHSShiftAmt)->getZExtValue();
Chris Lattner516b9622006-09-14 20:50:57 +00002027 if ((LShVal + RShVal) != OpSizeInBits)
2028 return 0;
2029
Dan Gohman475871a2008-07-27 21:46:04 +00002030 SDValue Rot;
Chris Lattner516b9622006-09-14 20:50:57 +00002031 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00002032 Rot = DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00002033 else
Scott Michelc9dc1142007-04-02 21:36:32 +00002034 Rot = DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00002035
2036 // If there is an AND of either shifted operand, apply it to the result.
Gabor Greifba36cb52008-08-28 21:40:38 +00002037 if (LHSMask.getNode() || RHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00002038 APInt Mask = APInt::getAllOnesValue(OpSizeInBits);
Chris Lattner516b9622006-09-14 20:50:57 +00002039
Gabor Greifba36cb52008-08-28 21:40:38 +00002040 if (LHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00002041 APInt RHSBits = APInt::getLowBitsSet(OpSizeInBits, LShVal);
2042 Mask &= cast<ConstantSDNode>(LHSMask)->getAPIntValue() | RHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00002043 }
Gabor Greifba36cb52008-08-28 21:40:38 +00002044 if (RHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00002045 APInt LHSBits = APInt::getHighBitsSet(OpSizeInBits, RShVal);
2046 Mask &= cast<ConstantSDNode>(RHSMask)->getAPIntValue() | LHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00002047 }
2048
2049 Rot = DAG.getNode(ISD::AND, VT, Rot, DAG.getConstant(Mask, VT));
2050 }
2051
Gabor Greifba36cb52008-08-28 21:40:38 +00002052 return Rot.getNode();
Chris Lattner516b9622006-09-14 20:50:57 +00002053 }
2054
2055 // If there is a mask here, and we have a variable shift, we can't be sure
2056 // that we're masking out the right stuff.
Gabor Greifba36cb52008-08-28 21:40:38 +00002057 if (LHSMask.getNode() || RHSMask.getNode())
Chris Lattner516b9622006-09-14 20:50:57 +00002058 return 0;
2059
2060 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
2061 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotr x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00002062 if (RHSShiftAmt.getOpcode() == ISD::SUB &&
2063 LHSShiftAmt == RHSShiftAmt.getOperand(1)) {
Chris Lattner516b9622006-09-14 20:50:57 +00002064 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00002065 dyn_cast<ConstantSDNode>(RHSShiftAmt.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002066 if (SUBC->getAPIntValue() == OpSizeInBits) {
Chris Lattner516b9622006-09-14 20:50:57 +00002067 if (HasROTL)
Gabor Greifba36cb52008-08-28 21:40:38 +00002068 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).getNode();
Chris Lattner516b9622006-09-14 20:50:57 +00002069 else
Gabor Greifba36cb52008-08-28 21:40:38 +00002070 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).getNode();
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002071 }
Chris Lattner516b9622006-09-14 20:50:57 +00002072 }
2073 }
2074
2075 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
2076 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotl x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00002077 if (LHSShiftAmt.getOpcode() == ISD::SUB &&
2078 RHSShiftAmt == LHSShiftAmt.getOperand(1)) {
Chris Lattner516b9622006-09-14 20:50:57 +00002079 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00002080 dyn_cast<ConstantSDNode>(LHSShiftAmt.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002081 if (SUBC->getAPIntValue() == OpSizeInBits) {
Bill Wendling2692d592008-08-31 01:13:31 +00002082 if (HasROTR)
Gabor Greifba36cb52008-08-28 21:40:38 +00002083 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).getNode();
Bill Wendling2692d592008-08-31 01:13:31 +00002084 else
2085 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).getNode();
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002086 }
Scott Michelc9dc1142007-04-02 21:36:32 +00002087 }
2088 }
2089
Dan Gohman74feef22008-10-17 01:23:35 +00002090 // Look for sign/zext/any-extended or truncate cases:
Scott Michelc9dc1142007-04-02 21:36:32 +00002091 if ((LHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
2092 || LHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
Dan Gohman74feef22008-10-17 01:23:35 +00002093 || LHSShiftAmt.getOpcode() == ISD::ANY_EXTEND
2094 || LHSShiftAmt.getOpcode() == ISD::TRUNCATE) &&
Scott Michelc9dc1142007-04-02 21:36:32 +00002095 (RHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
2096 || RHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
Dan Gohman74feef22008-10-17 01:23:35 +00002097 || RHSShiftAmt.getOpcode() == ISD::ANY_EXTEND
2098 || RHSShiftAmt.getOpcode() == ISD::TRUNCATE)) {
Dan Gohman475871a2008-07-27 21:46:04 +00002099 SDValue LExtOp0 = LHSShiftAmt.getOperand(0);
2100 SDValue RExtOp0 = RHSShiftAmt.getOperand(0);
Scott Michelc9dc1142007-04-02 21:36:32 +00002101 if (RExtOp0.getOpcode() == ISD::SUB &&
2102 RExtOp0.getOperand(1) == LExtOp0) {
2103 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00002104 // (rotl x, y)
Scott Michelc9dc1142007-04-02 21:36:32 +00002105 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00002106 // (rotr x, (sub 32, y))
Dan Gohman74feef22008-10-17 01:23:35 +00002107 if (ConstantSDNode *SUBC =
2108 dyn_cast<ConstantSDNode>(RExtOp0.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002109 if (SUBC->getAPIntValue() == OpSizeInBits) {
Gabor Greif12632d22008-08-30 19:29:20 +00002110 return DAG.getNode(HasROTL ? ISD::ROTL : ISD::ROTR, VT, LHSShiftArg,
2111 HasROTL ? LHSShiftAmt : RHSShiftAmt).getNode();
Scott Michelc9dc1142007-04-02 21:36:32 +00002112 }
2113 }
2114 } else if (LExtOp0.getOpcode() == ISD::SUB &&
2115 RExtOp0 == LExtOp0.getOperand(1)) {
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 // (rotr x, y)
Bill Wendling353dea22008-08-31 01:04:56 +00002118 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext y))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00002119 // (rotl x, (sub 32, y))
Dan Gohman74feef22008-10-17 01:23:35 +00002120 if (ConstantSDNode *SUBC =
2121 dyn_cast<ConstantSDNode>(LExtOp0.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002122 if (SUBC->getAPIntValue() == OpSizeInBits) {
Bill Wendling353dea22008-08-31 01:04:56 +00002123 return DAG.getNode(HasROTR ? ISD::ROTR : ISD::ROTL, VT, LHSShiftArg,
2124 HasROTR ? RHSShiftAmt : LHSShiftAmt).getNode();
Scott Michelc9dc1142007-04-02 21:36:32 +00002125 }
2126 }
Chris Lattner516b9622006-09-14 20:50:57 +00002127 }
2128 }
2129
2130 return 0;
2131}
2132
2133
Dan Gohman475871a2008-07-27 21:46:04 +00002134SDValue DAGCombiner::visitXOR(SDNode *N) {
2135 SDValue N0 = N->getOperand(0);
2136 SDValue N1 = N->getOperand(1);
2137 SDValue LHS, RHS, CC;
Nate Begeman646d7e22005-09-02 21:18:40 +00002138 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2139 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002140 MVT VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002141
Dan Gohman7f321562007-06-25 16:23:39 +00002142 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00002143 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002144 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002145 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00002146 }
Dan Gohman7f321562007-06-25 16:23:39 +00002147
Evan Cheng26471c42008-03-25 20:08:07 +00002148 // fold (xor undef, undef) -> 0. This is a common idiom (misuse).
2149 if (N0.getOpcode() == ISD::UNDEF && N1.getOpcode() == ISD::UNDEF)
2150 return DAG.getConstant(0, VT);
Dan Gohman613e0d82007-07-03 14:03:57 +00002151 // fold (xor x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00002152 if (N0.getOpcode() == ISD::UNDEF)
2153 return N0;
2154 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00002155 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002156 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002157 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002158 return DAG.FoldConstantArithmetic(ISD::XOR, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00002159 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00002160 if (N0C && !N1C)
2161 return DAG.getNode(ISD::XOR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002162 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002163 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002164 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00002165 // reassociate xor
Dan Gohman475871a2008-07-27 21:46:04 +00002166 SDValue RXOR = ReassociateOps(ISD::XOR, N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00002167 if (RXOR.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00002168 return RXOR;
Bill Wendlingae89bb12008-11-11 08:25:46 +00002169
Nate Begeman1d4d4142005-09-01 00:19:25 +00002170 // fold !(x cc y) -> (x !cc y)
Dan Gohman002e5d02008-03-13 22:13:53 +00002171 if (N1C && N1C->getAPIntValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002172 bool isInt = LHS.getValueType().isInteger();
Nate Begeman646d7e22005-09-02 21:18:40 +00002173 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
2174 isInt);
Bill Wendlingae89bb12008-11-11 08:25:46 +00002175
2176 if (!AfterLegalize || TLI.isCondCodeLegal(NotCC, LHS.getValueType())) {
2177 switch (N0.getOpcode()) {
2178 default:
2179 assert(0 && "Unhandled SetCC Equivalent!");
2180 abort();
2181 case ISD::SETCC:
2182 return DAG.getSetCC(VT, LHS, RHS, NotCC);
2183 case ISD::SELECT_CC:
2184 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),
2185 N0.getOperand(3), NotCC);
2186 }
2187 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00002188 }
Bill Wendlingae89bb12008-11-11 08:25:46 +00002189
Chris Lattner61c5ff42007-09-10 21:39:07 +00002190 // fold (not (zext (setcc x, y))) -> (zext (not (setcc x, y)))
Dan Gohman002e5d02008-03-13 22:13:53 +00002191 if (N1C && N1C->getAPIntValue() == 1 && N0.getOpcode() == ISD::ZERO_EXTEND &&
Gabor Greif12632d22008-08-30 19:29:20 +00002192 N0.getNode()->hasOneUse() &&
2193 isSetCCEquivalent(N0.getOperand(0), LHS, RHS, CC)){
Dan Gohman475871a2008-07-27 21:46:04 +00002194 SDValue V = N0.getOperand(0);
Chris Lattner61c5ff42007-09-10 21:39:07 +00002195 V = DAG.getNode(ISD::XOR, V.getValueType(), V,
Duncan Sands272dce02007-10-10 09:54:50 +00002196 DAG.getConstant(1, V.getValueType()));
Gabor Greifba36cb52008-08-28 21:40:38 +00002197 AddToWorkList(V.getNode());
Chris Lattner61c5ff42007-09-10 21:39:07 +00002198 return DAG.getNode(ISD::ZERO_EXTEND, VT, V);
2199 }
2200
Nate Begeman99801192005-09-07 23:25:52 +00002201 // fold !(x or y) -> (!x and !y) iff x or y are setcc
Dan Gohman002e5d02008-03-13 22:13:53 +00002202 if (N1C && N1C->getAPIntValue() == 1 && VT == MVT::i1 &&
Nate Begeman99801192005-09-07 23:25:52 +00002203 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Dan Gohman475871a2008-07-27 21:46:04 +00002204 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00002205 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
2206 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002207 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
2208 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Gabor Greifba36cb52008-08-28 21:40:38 +00002209 AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode());
Nate Begeman99801192005-09-07 23:25:52 +00002210 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002211 }
2212 }
Nate Begeman99801192005-09-07 23:25:52 +00002213 // fold !(x or y) -> (!x and !y) iff x or y are constants
2214 if (N1C && N1C->isAllOnesValue() &&
2215 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Dan Gohman475871a2008-07-27 21:46:04 +00002216 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00002217 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
2218 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002219 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
2220 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Gabor Greifba36cb52008-08-28 21:40:38 +00002221 AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode());
Nate Begeman99801192005-09-07 23:25:52 +00002222 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002223 }
2224 }
Nate Begeman223df222005-09-08 20:18:10 +00002225 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
2226 if (N1C && N0.getOpcode() == ISD::XOR) {
2227 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
2228 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2229 if (N00C)
2230 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
Dan Gohman002e5d02008-03-13 22:13:53 +00002231 DAG.getConstant(N1C->getAPIntValue()^
2232 N00C->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00002233 if (N01C)
2234 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
Dan Gohman002e5d02008-03-13 22:13:53 +00002235 DAG.getConstant(N1C->getAPIntValue()^
2236 N01C->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00002237 }
2238 // fold (xor x, x) -> 0
Chris Lattner4fbdd592006-03-28 19:11:05 +00002239 if (N0 == N1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002240 if (!VT.isVector()) {
Chris Lattner4fbdd592006-03-28 19:11:05 +00002241 return DAG.getConstant(0, VT);
2242 } else if (!AfterLegalize || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) {
2243 // Produce a vector of zeros.
Dan Gohman475871a2008-07-27 21:46:04 +00002244 SDValue El = DAG.getConstant(0, VT.getVectorElementType());
2245 std::vector<SDValue> Ops(VT.getVectorNumElements(), El);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002246 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4fbdd592006-03-28 19:11:05 +00002247 }
2248 }
Chris Lattner35e5c142006-05-05 05:51:50 +00002249
2250 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
2251 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002252 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002253 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002254 }
Chris Lattner35e5c142006-05-05 05:51:50 +00002255
Chris Lattner3e104b12006-04-08 04:15:24 +00002256 // Simplify the expression using non-local knowledge.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002257 if (!VT.isVector() &&
Dan Gohman475871a2008-07-27 21:46:04 +00002258 SimplifyDemandedBits(SDValue(N, 0)))
2259 return SDValue(N, 0);
Chris Lattner3e104b12006-04-08 04:15:24 +00002260
Dan Gohman475871a2008-07-27 21:46:04 +00002261 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002262}
2263
Chris Lattnere70da202007-12-06 07:33:36 +00002264/// visitShiftByConstant - Handle transforms common to the three shifts, when
2265/// the shift amount is a constant.
Dan Gohman475871a2008-07-27 21:46:04 +00002266SDValue DAGCombiner::visitShiftByConstant(SDNode *N, unsigned Amt) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002267 SDNode *LHS = N->getOperand(0).getNode();
Dan Gohman475871a2008-07-27 21:46:04 +00002268 if (!LHS->hasOneUse()) return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00002269
2270 // We want to pull some binops through shifts, so that we have (and (shift))
2271 // instead of (shift (and)), likewise for add, or, xor, etc. This sort of
2272 // thing happens with address calculations, so it's important to canonicalize
2273 // it.
2274 bool HighBitSet = false; // Can we transform this if the high bit is set?
2275
2276 switch (LHS->getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002277 default: return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00002278 case ISD::OR:
2279 case ISD::XOR:
2280 HighBitSet = false; // We can only transform sra if the high bit is clear.
2281 break;
2282 case ISD::AND:
2283 HighBitSet = true; // We can only transform sra if the high bit is set.
2284 break;
2285 case ISD::ADD:
2286 if (N->getOpcode() != ISD::SHL)
Dan Gohman475871a2008-07-27 21:46:04 +00002287 return SDValue(); // only shl(add) not sr[al](add).
Chris Lattnere70da202007-12-06 07:33:36 +00002288 HighBitSet = false; // We can only transform sra if the high bit is clear.
2289 break;
2290 }
2291
2292 // We require the RHS of the binop to be a constant as well.
2293 ConstantSDNode *BinOpCst = dyn_cast<ConstantSDNode>(LHS->getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00002294 if (!BinOpCst) return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00002295
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00002296
2297 // FIXME: disable this for unless the input to the binop is a shift by a
2298 // constant. If it is not a shift, it pessimizes some common cases like:
2299 //
2300 //void foo(int *X, int i) { X[i & 1235] = 1; }
2301 //int bar(int *X, int i) { return X[i & 255]; }
Gabor Greifba36cb52008-08-28 21:40:38 +00002302 SDNode *BinOpLHSVal = LHS->getOperand(0).getNode();
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00002303 if ((BinOpLHSVal->getOpcode() != ISD::SHL &&
2304 BinOpLHSVal->getOpcode() != ISD::SRA &&
2305 BinOpLHSVal->getOpcode() != ISD::SRL) ||
2306 !isa<ConstantSDNode>(BinOpLHSVal->getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00002307 return SDValue();
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00002308
Duncan Sands83ec4b62008-06-06 12:08:01 +00002309 MVT VT = N->getValueType(0);
Chris Lattnere70da202007-12-06 07:33:36 +00002310
2311 // If this is a signed shift right, and the high bit is modified
2312 // by the logical operation, do not perform the transformation.
2313 // The highBitSet boolean indicates the value of the high bit of
2314 // the constant which would cause it to be modified for this
2315 // operation.
2316 if (N->getOpcode() == ISD::SRA) {
Dan Gohman220a8232008-03-03 23:51:38 +00002317 bool BinOpRHSSignSet = BinOpCst->getAPIntValue().isNegative();
2318 if (BinOpRHSSignSet != HighBitSet)
Dan Gohman475871a2008-07-27 21:46:04 +00002319 return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00002320 }
2321
2322 // Fold the constants, shifting the binop RHS by the shift amount.
Dan Gohman475871a2008-07-27 21:46:04 +00002323 SDValue NewRHS = DAG.getNode(N->getOpcode(), N->getValueType(0),
Chris Lattnere70da202007-12-06 07:33:36 +00002324 LHS->getOperand(1), N->getOperand(1));
2325
2326 // Create the new shift.
Dan Gohman475871a2008-07-27 21:46:04 +00002327 SDValue NewShift = DAG.getNode(N->getOpcode(), VT, LHS->getOperand(0),
Chris Lattnere70da202007-12-06 07:33:36 +00002328 N->getOperand(1));
2329
2330 // Create the new binop.
2331 return DAG.getNode(LHS->getOpcode(), VT, NewShift, NewRHS);
2332}
2333
2334
Dan Gohman475871a2008-07-27 21:46:04 +00002335SDValue DAGCombiner::visitSHL(SDNode *N) {
2336 SDValue N0 = N->getOperand(0);
2337 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002338 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2339 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002340 MVT VT = N0.getValueType();
2341 unsigned OpSizeInBits = VT.getSizeInBits();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002342
2343 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002344 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002345 return DAG.FoldConstantArithmetic(ISD::SHL, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002346 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002347 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002348 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002349 // fold (shl x, c >= size(x)) -> undef
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002350 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002351 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002352 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002353 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002354 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002355 // if (shl x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00002356 if (DAG.MaskedValueIsZero(SDValue(N, 0),
Duncan Sands83ec4b62008-06-06 12:08:01 +00002357 APInt::getAllOnesValue(VT.getSizeInBits())))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002358 return DAG.getConstant(0, VT);
Evan Chengeb9f8922008-08-30 02:03:58 +00002359 // fold (shl x, (trunc (and y, c))) -> (shl x, (and (trunc y), c))
2360 // iff (trunc c) == c
2361 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00002362 N1.getOperand(0).getOpcode() == ISD::AND &&
2363 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00002364 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00002365 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Evan Chengeb9f8922008-08-30 02:03:58 +00002366 MVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00002367 SDValue N100 = N1.getOperand(0).getOperand(0);
2368 return DAG.getNode(ISD::SHL, VT, N0,
2369 DAG.getNode(ISD::AND, TruncVT,
2370 DAG.getNode(ISD::TRUNCATE, TruncVT, N100),
2371 DAG.getConstant(N101C->getZExtValue(),
2372 TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00002373 }
2374 }
2375
Dan Gohman475871a2008-07-27 21:46:04 +00002376 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
2377 return SDValue(N, 0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002378 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002379 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002380 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002381 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
2382 uint64_t c2 = N1C->getZExtValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002383 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002384 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002385 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002386 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002387 }
2388 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
2389 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002390 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002391 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002392 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
2393 uint64_t c2 = N1C->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00002394 SDValue Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman1d4d4142005-09-01 00:19:25 +00002395 DAG.getConstant(~0ULL << c1, VT));
2396 if (c2 > c1)
2397 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +00002398 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002399 else
Nate Begeman83e75ec2005-09-06 04:43:02 +00002400 return DAG.getNode(ISD::SRL, VT, Mask,
2401 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002402 }
2403 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00002404 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +00002405 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002406 DAG.getConstant(~0ULL << N1C->getZExtValue(), VT));
Chris Lattnere70da202007-12-06 07:33:36 +00002407
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002408 return N1C ? visitShiftByConstant(N, N1C->getZExtValue()) : SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002409}
2410
Dan Gohman475871a2008-07-27 21:46:04 +00002411SDValue DAGCombiner::visitSRA(SDNode *N) {
2412 SDValue N0 = N->getOperand(0);
2413 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002414 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2415 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002416 MVT VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002417
2418 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002419 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002420 return DAG.FoldConstantArithmetic(ISD::SRA, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002421 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002422 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002423 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002424 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00002425 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002426 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002427 // fold (sra x, c >= size(x)) -> undef
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002428 if (N1C && N1C->getZExtValue() >= VT.getSizeInBits())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002429 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002430 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002431 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002432 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00002433 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
2434 // sext_inreg.
2435 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002436 unsigned LowBits = VT.getSizeInBits() - (unsigned)N1C->getZExtValue();
Duncan Sands8eab8a22008-06-09 11:32:28 +00002437 MVT EVT = MVT::getIntegerVT(LowBits);
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002438 if (EVT.isSimple() && // TODO: remove when apint codegen support lands.
2439 (!AfterLegalize || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, EVT)))
Nate Begemanfb7217b2006-02-17 19:54:08 +00002440 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
2441 DAG.getValueType(EVT));
2442 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002443
Chris Lattner71d9ebc2006-02-28 06:23:04 +00002444 // fold (sra (sra x, c1), c2) -> (sra x, c1+c2)
2445 if (N1C && N0.getOpcode() == ISD::SRA) {
2446 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002447 unsigned Sum = N1C->getZExtValue() + C1->getZExtValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002448 if (Sum >= VT.getSizeInBits()) Sum = VT.getSizeInBits()-1;
Chris Lattner71d9ebc2006-02-28 06:23:04 +00002449 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0),
2450 DAG.getConstant(Sum, N1C->getValueType(0)));
2451 }
2452 }
Christopher Lamb15cbde32008-03-19 08:30:06 +00002453
2454 // fold sra (shl X, m), result_size - n
2455 // -> (sign_extend (trunc (shl X, result_size - n - m))) for
Christopher Lambb9b04282008-03-20 04:31:39 +00002456 // result_size - n != m.
2457 // If truncate is free for the target sext(shl) is likely to result in better
2458 // code.
Christopher Lamb15cbde32008-03-19 08:30:06 +00002459 if (N0.getOpcode() == ISD::SHL) {
2460 // Get the two constanst of the shifts, CN0 = m, CN = n.
2461 const ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2462 if (N01C && N1C) {
Christopher Lambb9b04282008-03-20 04:31:39 +00002463 // Determine what the truncate's result bitsize and type would be.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002464 unsigned VTValSize = VT.getSizeInBits();
2465 MVT TruncVT =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002466 MVT::getIntegerVT(VTValSize - N1C->getZExtValue());
Christopher Lambb9b04282008-03-20 04:31:39 +00002467 // Determine the residual right-shift amount.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002468 unsigned ShiftAmt = N1C->getZExtValue() - N01C->getZExtValue();
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002469
Christopher Lambb9b04282008-03-20 04:31:39 +00002470 // If the shift is not a no-op (in which case this should be just a sign
2471 // extend already), the truncated to type is legal, sign_extend is legal
Gabor Greif12632d22008-08-30 19:29:20 +00002472 // on that type, and the the truncate to that type is both legal and free,
Christopher Lambb9b04282008-03-20 04:31:39 +00002473 // perform the transform.
2474 if (ShiftAmt &&
Christopher Lambb9b04282008-03-20 04:31:39 +00002475 TLI.isOperationLegal(ISD::SIGN_EXTEND, TruncVT) &&
2476 TLI.isOperationLegal(ISD::TRUNCATE, VT) &&
Evan Cheng260e07e2008-03-20 02:18:41 +00002477 TLI.isTruncateFree(VT, TruncVT)) {
Christopher Lambb9b04282008-03-20 04:31:39 +00002478
Dan Gohman475871a2008-07-27 21:46:04 +00002479 SDValue Amt = DAG.getConstant(ShiftAmt, TLI.getShiftAmountTy());
2480 SDValue Shift = DAG.getNode(ISD::SRL, VT, N0.getOperand(0), Amt);
2481 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, TruncVT, Shift);
Christopher Lambb9b04282008-03-20 04:31:39 +00002482 return DAG.getNode(ISD::SIGN_EXTEND, N->getValueType(0), Trunc);
Christopher Lamb15cbde32008-03-19 08:30:06 +00002483 }
2484 }
2485 }
Chris Lattner71d9ebc2006-02-28 06:23:04 +00002486
Evan Chengeb9f8922008-08-30 02:03:58 +00002487 // fold (sra x, (trunc (and y, c))) -> (sra x, (and (trunc y), c))
2488 // iff (trunc c) == c
2489 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00002490 N1.getOperand(0).getOpcode() == ISD::AND &&
2491 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00002492 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00002493 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Evan Chengeb9f8922008-08-30 02:03:58 +00002494 MVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00002495 SDValue N100 = N1.getOperand(0).getOperand(0);
2496 return DAG.getNode(ISD::SRA, VT, N0,
2497 DAG.getNode(ISD::AND, TruncVT,
2498 DAG.getNode(ISD::TRUNCATE, TruncVT, N100),
2499 DAG.getConstant(N101C->getZExtValue(),
2500 TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00002501 }
2502 }
2503
Chris Lattnera8504462006-05-08 20:51:54 +00002504 // Simplify, based on bits shifted out of the LHS.
Dan Gohman475871a2008-07-27 21:46:04 +00002505 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
2506 return SDValue(N, 0);
Chris Lattnera8504462006-05-08 20:51:54 +00002507
2508
Nate Begeman1d4d4142005-09-01 00:19:25 +00002509 // If the sign bit is known to be zero, switch this to a SRL.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002510 if (DAG.SignBitIsZero(N0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002511 return DAG.getNode(ISD::SRL, VT, N0, N1);
Chris Lattnere70da202007-12-06 07:33:36 +00002512
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002513 return N1C ? visitShiftByConstant(N, N1C->getZExtValue()) : SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002514}
2515
Dan Gohman475871a2008-07-27 21:46:04 +00002516SDValue DAGCombiner::visitSRL(SDNode *N) {
2517 SDValue N0 = N->getOperand(0);
2518 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002519 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2520 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002521 MVT VT = N0.getValueType();
2522 unsigned OpSizeInBits = VT.getSizeInBits();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002523
2524 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002525 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002526 return DAG.FoldConstantArithmetic(ISD::SRL, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002527 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002528 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002529 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002530 // fold (srl x, c >= size(x)) -> undef
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002531 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002532 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002533 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002534 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002535 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002536 // if (srl x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00002537 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002538 APInt::getAllOnesValue(OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002539 return DAG.getConstant(0, VT);
Chris Lattnerec06e9a2007-04-18 03:05:22 +00002540
Nate Begeman1d4d4142005-09-01 00:19:25 +00002541 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002542 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002543 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002544 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
2545 uint64_t c2 = N1C->getZExtValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002546 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002547 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002548 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002549 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002550 }
Chris Lattner350bec02006-04-02 06:11:11 +00002551
Chris Lattner06afe072006-05-05 22:53:17 +00002552 // fold (srl (anyextend x), c) -> (anyextend (srl x, c))
2553 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
2554 // Shifting in all undef bits?
Duncan Sands83ec4b62008-06-06 12:08:01 +00002555 MVT SmallVT = N0.getOperand(0).getValueType();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002556 if (N1C->getZExtValue() >= SmallVT.getSizeInBits())
Chris Lattner06afe072006-05-05 22:53:17 +00002557 return DAG.getNode(ISD::UNDEF, VT);
2558
Dan Gohman475871a2008-07-27 21:46:04 +00002559 SDValue SmallShift = DAG.getNode(ISD::SRL, SmallVT, N0.getOperand(0), N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00002560 AddToWorkList(SmallShift.getNode());
Chris Lattner06afe072006-05-05 22:53:17 +00002561 return DAG.getNode(ISD::ANY_EXTEND, VT, SmallShift);
2562 }
2563
Chris Lattner3657ffe2006-10-12 20:23:19 +00002564 // fold (srl (sra X, Y), 31) -> (srl X, 31). This srl only looks at the sign
2565 // bit, which is unmodified by sra.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002566 if (N1C && N1C->getZExtValue()+1 == VT.getSizeInBits()) {
Chris Lattner3657ffe2006-10-12 20:23:19 +00002567 if (N0.getOpcode() == ISD::SRA)
2568 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0), N1);
2569 }
2570
Chris Lattner350bec02006-04-02 06:11:11 +00002571 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
2572 if (N1C && N0.getOpcode() == ISD::CTLZ &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002573 N1C->getAPIntValue() == Log2_32(VT.getSizeInBits())) {
Dan Gohman948d8ea2008-02-20 16:33:30 +00002574 APInt KnownZero, KnownOne;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002575 APInt Mask = APInt::getAllOnesValue(VT.getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00002576 DAG.ComputeMaskedBits(N0.getOperand(0), Mask, KnownZero, KnownOne);
Chris Lattner350bec02006-04-02 06:11:11 +00002577
2578 // If any of the input bits are KnownOne, then the input couldn't be all
2579 // zeros, thus the result of the srl will always be zero.
Dan Gohman948d8ea2008-02-20 16:33:30 +00002580 if (KnownOne.getBoolValue()) return DAG.getConstant(0, VT);
Chris Lattner350bec02006-04-02 06:11:11 +00002581
2582 // If all of the bits input the to ctlz node are known to be zero, then
2583 // the result of the ctlz is "32" and the result of the shift is one.
Dan Gohman948d8ea2008-02-20 16:33:30 +00002584 APInt UnknownBits = ~KnownZero & Mask;
Chris Lattner350bec02006-04-02 06:11:11 +00002585 if (UnknownBits == 0) return DAG.getConstant(1, VT);
2586
2587 // Otherwise, check to see if there is exactly one bit input to the ctlz.
2588 if ((UnknownBits & (UnknownBits-1)) == 0) {
2589 // Okay, we know that only that the single bit specified by UnknownBits
2590 // could be set on input to the CTLZ node. If this bit is set, the SRL
2591 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
2592 // to an SRL,XOR pair, which is likely to simplify more.
Dan Gohman948d8ea2008-02-20 16:33:30 +00002593 unsigned ShAmt = UnknownBits.countTrailingZeros();
Dan Gohman475871a2008-07-27 21:46:04 +00002594 SDValue Op = N0.getOperand(0);
Chris Lattner350bec02006-04-02 06:11:11 +00002595 if (ShAmt) {
2596 Op = DAG.getNode(ISD::SRL, VT, Op,
2597 DAG.getConstant(ShAmt, TLI.getShiftAmountTy()));
Gabor Greifba36cb52008-08-28 21:40:38 +00002598 AddToWorkList(Op.getNode());
Chris Lattner350bec02006-04-02 06:11:11 +00002599 }
2600 return DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(1, VT));
2601 }
2602 }
Evan Chengeb9f8922008-08-30 02:03:58 +00002603
2604 // fold (srl x, (trunc (and y, c))) -> (srl x, (and (trunc y), c))
2605 // iff (trunc c) == c
2606 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00002607 N1.getOperand(0).getOpcode() == ISD::AND &&
2608 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00002609 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00002610 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Evan Chengeb9f8922008-08-30 02:03:58 +00002611 MVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00002612 SDValue N100 = N1.getOperand(0).getOperand(0);
2613 return DAG.getNode(ISD::SRL, VT, N0,
2614 DAG.getNode(ISD::AND, TruncVT,
2615 DAG.getNode(ISD::TRUNCATE, TruncVT, N100),
2616 DAG.getConstant(N101C->getZExtValue(),
2617 TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00002618 }
2619 }
Chris Lattner61a4c072007-04-18 03:06:49 +00002620
2621 // fold operands of srl based on knowledge that the low bits are not
2622 // demanded.
Dan Gohman475871a2008-07-27 21:46:04 +00002623 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
2624 return SDValue(N, 0);
Chris Lattner61a4c072007-04-18 03:06:49 +00002625
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002626 return N1C ? visitShiftByConstant(N, N1C->getZExtValue()) : SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002627}
2628
Dan Gohman475871a2008-07-27 21:46:04 +00002629SDValue DAGCombiner::visitCTLZ(SDNode *N) {
2630 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002631 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002632
2633 // fold (ctlz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002634 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002635 return DAG.getNode(ISD::CTLZ, VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00002636 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002637}
2638
Dan Gohman475871a2008-07-27 21:46:04 +00002639SDValue DAGCombiner::visitCTTZ(SDNode *N) {
2640 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002641 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002642
2643 // fold (cttz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002644 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002645 return DAG.getNode(ISD::CTTZ, VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00002646 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002647}
2648
Dan Gohman475871a2008-07-27 21:46:04 +00002649SDValue DAGCombiner::visitCTPOP(SDNode *N) {
2650 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002651 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002652
2653 // fold (ctpop c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002654 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002655 return DAG.getNode(ISD::CTPOP, VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00002656 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002657}
2658
Dan Gohman475871a2008-07-27 21:46:04 +00002659SDValue DAGCombiner::visitSELECT(SDNode *N) {
2660 SDValue N0 = N->getOperand(0);
2661 SDValue N1 = N->getOperand(1);
2662 SDValue N2 = N->getOperand(2);
Nate Begeman452d7beb2005-09-16 00:54:12 +00002663 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2664 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2665 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002666 MVT VT = N->getValueType(0);
2667 MVT VT0 = N0.getValueType();
Nate Begeman44728a72005-09-19 22:34:01 +00002668
Nate Begeman452d7beb2005-09-16 00:54:12 +00002669 // fold select C, X, X -> X
2670 if (N1 == N2)
2671 return N1;
2672 // fold select true, X, Y -> X
2673 if (N0C && !N0C->isNullValue())
2674 return N1;
2675 // fold select false, X, Y -> Y
2676 if (N0C && N0C->isNullValue())
2677 return N2;
2678 // fold select C, 1, X -> C | X
Duncan Sands83ec4b62008-06-06 12:08:01 +00002679 if (VT == MVT::i1 && N1C && N1C->getAPIntValue() == 1)
Nate Begeman452d7beb2005-09-16 00:54:12 +00002680 return DAG.getNode(ISD::OR, VT, N0, N2);
Evan Cheng571c4782007-08-18 05:57:05 +00002681 // fold select C, 0, 1 -> ~C
Duncan Sands83ec4b62008-06-06 12:08:01 +00002682 if (VT.isInteger() && VT0.isInteger() &&
Dan Gohman002e5d02008-03-13 22:13:53 +00002683 N1C && N2C && N1C->isNullValue() && N2C->getAPIntValue() == 1) {
Dan Gohman475871a2008-07-27 21:46:04 +00002684 SDValue XORNode = DAG.getNode(ISD::XOR, VT0, N0, DAG.getConstant(1, VT0));
Evan Cheng571c4782007-08-18 05:57:05 +00002685 if (VT == VT0)
2686 return XORNode;
Gabor Greifba36cb52008-08-28 21:40:38 +00002687 AddToWorkList(XORNode.getNode());
Duncan Sands8e4eb092008-06-08 20:54:56 +00002688 if (VT.bitsGT(VT0))
Evan Cheng571c4782007-08-18 05:57:05 +00002689 return DAG.getNode(ISD::ZERO_EXTEND, VT, XORNode);
2690 return DAG.getNode(ISD::TRUNCATE, VT, XORNode);
2691 }
Nate Begeman452d7beb2005-09-16 00:54:12 +00002692 // fold select C, 0, X -> ~C & X
Dale Johannesene0e6fac2007-12-06 17:53:31 +00002693 if (VT == VT0 && VT == MVT::i1 && N1C && N1C->isNullValue()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002694 SDValue XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Gabor Greifba36cb52008-08-28 21:40:38 +00002695 AddToWorkList(XORNode.getNode());
Nate Begeman452d7beb2005-09-16 00:54:12 +00002696 return DAG.getNode(ISD::AND, VT, XORNode, N2);
2697 }
2698 // fold select C, X, 1 -> ~C | X
Dan Gohman002e5d02008-03-13 22:13:53 +00002699 if (VT == VT0 && VT == MVT::i1 && N2C && N2C->getAPIntValue() == 1) {
Dan Gohman475871a2008-07-27 21:46:04 +00002700 SDValue XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Gabor Greifba36cb52008-08-28 21:40:38 +00002701 AddToWorkList(XORNode.getNode());
Nate Begeman452d7beb2005-09-16 00:54:12 +00002702 return DAG.getNode(ISD::OR, VT, XORNode, N1);
2703 }
2704 // fold select C, X, 0 -> C & X
2705 // FIXME: this should check for C type == X type, not i1?
Duncan Sands83ec4b62008-06-06 12:08:01 +00002706 if (VT == MVT::i1 && N2C && N2C->isNullValue())
Nate Begeman452d7beb2005-09-16 00:54:12 +00002707 return DAG.getNode(ISD::AND, VT, N0, N1);
2708 // fold X ? X : Y --> X ? 1 : Y --> X | Y
Duncan Sands83ec4b62008-06-06 12:08:01 +00002709 if (VT == MVT::i1 && N0 == N1)
Nate Begeman452d7beb2005-09-16 00:54:12 +00002710 return DAG.getNode(ISD::OR, VT, N0, N2);
2711 // fold X ? Y : X --> X ? Y : 0 --> X & Y
Duncan Sands83ec4b62008-06-06 12:08:01 +00002712 if (VT == MVT::i1 && N0 == N2)
Nate Begeman452d7beb2005-09-16 00:54:12 +00002713 return DAG.getNode(ISD::AND, VT, N0, N1);
Chris Lattner729c6d12006-05-27 00:43:02 +00002714
Chris Lattner40c62d52005-10-18 06:04:22 +00002715 // If we can fold this based on the true/false value, do so.
2716 if (SimplifySelectOps(N, N1, N2))
Dan Gohman475871a2008-07-27 21:46:04 +00002717 return SDValue(N, 0); // Don't revisit N.
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002718
Nate Begeman44728a72005-09-19 22:34:01 +00002719 // fold selects based on a setcc into other things, such as min/max/abs
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002720 if (N0.getOpcode() == ISD::SETCC) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00002721 // FIXME:
2722 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
2723 // having to say they don't support SELECT_CC on every type the DAG knows
2724 // about, since there is no way to mark an opcode illegal at all value types
2725 if (TLI.isOperationLegal(ISD::SELECT_CC, MVT::Other))
2726 return DAG.getNode(ISD::SELECT_CC, VT, N0.getOperand(0), N0.getOperand(1),
2727 N1, N2, N0.getOperand(2));
2728 else
2729 return SimplifySelect(N0, N1, N2);
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002730 }
Dan Gohman475871a2008-07-27 21:46:04 +00002731 return SDValue();
Nate Begeman452d7beb2005-09-16 00:54:12 +00002732}
2733
Dan Gohman475871a2008-07-27 21:46:04 +00002734SDValue DAGCombiner::visitSELECT_CC(SDNode *N) {
2735 SDValue N0 = N->getOperand(0);
2736 SDValue N1 = N->getOperand(1);
2737 SDValue N2 = N->getOperand(2);
2738 SDValue N3 = N->getOperand(3);
2739 SDValue N4 = N->getOperand(4);
Nate Begeman44728a72005-09-19 22:34:01 +00002740 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
2741
Nate Begeman44728a72005-09-19 22:34:01 +00002742 // fold select_cc lhs, rhs, x, x, cc -> x
2743 if (N2 == N3)
2744 return N2;
Chris Lattner40c62d52005-10-18 06:04:22 +00002745
Chris Lattner5f42a242006-09-20 06:19:26 +00002746 // Determine if the condition we're dealing with is constant
Dan Gohman475871a2008-07-27 21:46:04 +00002747 SDValue SCC = SimplifySetCC(TLI.getSetCCResultType(N0), N0, N1, CC, false);
Gabor Greifba36cb52008-08-28 21:40:38 +00002748 if (SCC.getNode()) AddToWorkList(SCC.getNode());
Chris Lattner5f42a242006-09-20 06:19:26 +00002749
Gabor Greifba36cb52008-08-28 21:40:38 +00002750 if (ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.getNode())) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002751 if (!SCCC->isNullValue())
Chris Lattner5f42a242006-09-20 06:19:26 +00002752 return N2; // cond always true -> true val
2753 else
2754 return N3; // cond always false -> false val
2755 }
2756
2757 // Fold to a simpler select_cc
Gabor Greifba36cb52008-08-28 21:40:38 +00002758 if (SCC.getNode() && SCC.getOpcode() == ISD::SETCC)
Chris Lattner5f42a242006-09-20 06:19:26 +00002759 return DAG.getNode(ISD::SELECT_CC, N2.getValueType(),
2760 SCC.getOperand(0), SCC.getOperand(1), N2, N3,
2761 SCC.getOperand(2));
2762
Chris Lattner40c62d52005-10-18 06:04:22 +00002763 // If we can fold this based on the true/false value, do so.
2764 if (SimplifySelectOps(N, N2, N3))
Dan Gohman475871a2008-07-27 21:46:04 +00002765 return SDValue(N, 0); // Don't revisit N.
Chris Lattner40c62d52005-10-18 06:04:22 +00002766
Nate Begeman44728a72005-09-19 22:34:01 +00002767 // fold select_cc into other things, such as min/max/abs
2768 return SimplifySelectCC(N0, N1, N2, N3, CC);
Nate Begeman452d7beb2005-09-16 00:54:12 +00002769}
2770
Dan Gohman475871a2008-07-27 21:46:04 +00002771SDValue DAGCombiner::visitSETCC(SDNode *N) {
Nate Begeman452d7beb2005-09-16 00:54:12 +00002772 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
2773 cast<CondCodeSDNode>(N->getOperand(2))->get());
2774}
2775
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002776// ExtendUsesToFormExtLoad - Trying to extend uses of a load to enable this:
2777// "fold ({s|z}ext (load x)) -> ({s|z}ext (truncate ({s|z}extload x)))"
2778// transformation. Returns true if extension are possible and the above
2779// mentioned transformation is profitable.
Dan Gohman475871a2008-07-27 21:46:04 +00002780static bool ExtendUsesToFormExtLoad(SDNode *N, SDValue N0,
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002781 unsigned ExtOpc,
2782 SmallVector<SDNode*, 4> &ExtendNodes,
2783 TargetLowering &TLI) {
2784 bool HasCopyToRegUses = false;
2785 bool isTruncFree = TLI.isTruncateFree(N->getValueType(0), N0.getValueType());
Gabor Greif12632d22008-08-30 19:29:20 +00002786 for (SDNode::use_iterator UI = N0.getNode()->use_begin(),
2787 UE = N0.getNode()->use_end();
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002788 UI != UE; ++UI) {
Dan Gohman89684502008-07-27 20:43:25 +00002789 SDNode *User = *UI;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002790 if (User == N)
2791 continue;
2792 // FIXME: Only extend SETCC N, N and SETCC N, c for now.
2793 if (User->getOpcode() == ISD::SETCC) {
2794 ISD::CondCode CC = cast<CondCodeSDNode>(User->getOperand(2))->get();
2795 if (ExtOpc == ISD::ZERO_EXTEND && ISD::isSignedIntSetCC(CC))
2796 // Sign bits will be lost after a zext.
2797 return false;
2798 bool Add = false;
2799 for (unsigned i = 0; i != 2; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002800 SDValue UseOp = User->getOperand(i);
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002801 if (UseOp == N0)
2802 continue;
2803 if (!isa<ConstantSDNode>(UseOp))
2804 return false;
2805 Add = true;
2806 }
2807 if (Add)
2808 ExtendNodes.push_back(User);
2809 } else {
2810 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002811 SDValue UseOp = User->getOperand(i);
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002812 if (UseOp == N0) {
2813 // If truncate from extended type to original load type is free
2814 // on this target, then it's ok to extend a CopyToReg.
2815 if (isTruncFree && User->getOpcode() == ISD::CopyToReg)
2816 HasCopyToRegUses = true;
2817 else
2818 return false;
2819 }
2820 }
2821 }
2822 }
2823
2824 if (HasCopyToRegUses) {
2825 bool BothLiveOut = false;
2826 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
2827 UI != UE; ++UI) {
Dan Gohman89684502008-07-27 20:43:25 +00002828 SDNode *User = *UI;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002829 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002830 SDValue UseOp = User->getOperand(i);
Gabor Greifba36cb52008-08-28 21:40:38 +00002831 if (UseOp.getNode() == N && UseOp.getResNo() == 0) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002832 BothLiveOut = true;
2833 break;
2834 }
2835 }
2836 }
2837 if (BothLiveOut)
2838 // Both unextended and extended values are live out. There had better be
2839 // good a reason for the transformation.
2840 return ExtendNodes.size();
2841 }
2842 return true;
2843}
2844
Dan Gohman475871a2008-07-27 21:46:04 +00002845SDValue DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
2846 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002847 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002848
Nate Begeman1d4d4142005-09-01 00:19:25 +00002849 // fold (sext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00002850 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002851 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0);
Chris Lattner310b5782006-05-06 23:06:26 +00002852
Nate Begeman1d4d4142005-09-01 00:19:25 +00002853 // fold (sext (sext x)) -> (sext x)
Chris Lattner310b5782006-05-06 23:06:26 +00002854 // fold (sext (aext x)) -> (sext x)
2855 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002856 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
Chris Lattner310b5782006-05-06 23:06:26 +00002857
Chris Lattner22558872007-02-26 03:13:59 +00002858 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman1fdfa6a2008-05-20 20:56:33 +00002859 // fold (sext (truncate (load x))) -> (sext (smaller load x))
2860 // fold (sext (truncate (srl (load x), c))) -> (sext (smaller load (x+c/n)))
Gabor Greifba36cb52008-08-28 21:40:38 +00002861 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
2862 if (NarrowLoad.getNode()) {
2863 if (NarrowLoad.getNode() != N0.getNode())
2864 CombineTo(N0.getNode(), NarrowLoad);
Evan Cheng0b063de2007-03-23 02:16:52 +00002865 return DAG.getNode(ISD::SIGN_EXTEND, VT, NarrowLoad);
2866 }
Evan Chengc88138f2007-03-22 01:54:19 +00002867
Dan Gohman1fdfa6a2008-05-20 20:56:33 +00002868 // See if the value being truncated is already sign extended. If so, just
2869 // eliminate the trunc/sext pair.
Dan Gohman475871a2008-07-27 21:46:04 +00002870 SDValue Op = N0.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002871 unsigned OpBits = Op.getValueType().getSizeInBits();
2872 unsigned MidBits = N0.getValueType().getSizeInBits();
2873 unsigned DestBits = VT.getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002874 unsigned NumSignBits = DAG.ComputeNumSignBits(Op);
Chris Lattner22558872007-02-26 03:13:59 +00002875
2876 if (OpBits == DestBits) {
2877 // Op is i32, Mid is i8, and Dest is i32. If Op has more than 24 sign
2878 // bits, it is already ready.
2879 if (NumSignBits > DestBits-MidBits)
2880 return Op;
2881 } else if (OpBits < DestBits) {
2882 // Op is i32, Mid is i8, and Dest is i64. If Op has more than 24 sign
2883 // bits, just sext from i32.
2884 if (NumSignBits > OpBits-MidBits)
2885 return DAG.getNode(ISD::SIGN_EXTEND, VT, Op);
2886 } else {
2887 // Op is i64, Mid is i8, and Dest is i32. If Op has more than 56 sign
2888 // bits, just truncate to i32.
2889 if (NumSignBits > OpBits-MidBits)
2890 return DAG.getNode(ISD::TRUNCATE, VT, Op);
Chris Lattner6007b842006-09-21 06:00:20 +00002891 }
Chris Lattner22558872007-02-26 03:13:59 +00002892
2893 // fold (sext (truncate x)) -> (sextinreg x).
2894 if (!AfterLegalize || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG,
2895 N0.getValueType())) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00002896 if (Op.getValueType().bitsLT(VT))
Chris Lattner22558872007-02-26 03:13:59 +00002897 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
Duncan Sands8e4eb092008-06-08 20:54:56 +00002898 else if (Op.getValueType().bitsGT(VT))
Chris Lattner22558872007-02-26 03:13:59 +00002899 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
2900 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, Op,
2901 DAG.getValueType(N0.getValueType()));
2902 }
Chris Lattner6007b842006-09-21 06:00:20 +00002903 }
Chris Lattner310b5782006-05-06 23:06:26 +00002904
Evan Cheng110dec22005-12-14 02:19:23 +00002905 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00002906 if (ISD::isNON_EXTLoad(N0.getNode()) &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002907 ((!AfterLegalize && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00002908 TLI.isLoadExtLegal(ISD::SEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002909 bool DoXform = true;
2910 SmallVector<SDNode*, 4> SetCCs;
2911 if (!N0.hasOneUse())
2912 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::SIGN_EXTEND, SetCCs, TLI);
2913 if (DoXform) {
2914 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman475871a2008-07-27 21:46:04 +00002915 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002916 LN0->getBasePtr(), LN0->getSrcValue(),
2917 LN0->getSrcValueOffset(),
2918 N0.getValueType(),
2919 LN0->isVolatile(),
2920 LN0->getAlignment());
2921 CombineTo(N, ExtLoad);
Dan Gohman475871a2008-07-27 21:46:04 +00002922 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00002923 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002924 // Extend SetCC uses if necessary.
2925 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
2926 SDNode *SetCC = SetCCs[i];
Dan Gohman475871a2008-07-27 21:46:04 +00002927 SmallVector<SDValue, 4> Ops;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002928 for (unsigned j = 0; j != 2; ++j) {
Dan Gohman475871a2008-07-27 21:46:04 +00002929 SDValue SOp = SetCC->getOperand(j);
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002930 if (SOp == Trunc)
2931 Ops.push_back(ExtLoad);
2932 else
2933 Ops.push_back(DAG.getNode(ISD::SIGN_EXTEND, VT, SOp));
2934 }
2935 Ops.push_back(SetCC->getOperand(2));
2936 CombineTo(SetCC, DAG.getNode(ISD::SETCC, SetCC->getValueType(0),
2937 &Ops[0], Ops.size()));
2938 }
Dan Gohman475871a2008-07-27 21:46:04 +00002939 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002940 }
Nate Begeman3df4d522005-10-12 20:40:40 +00002941 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002942
2943 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
2944 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00002945 if ((ISD::isSEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) &&
2946 ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002947 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002948 MVT EVT = LN0->getMemoryVT();
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002949 if ((!AfterLegalize && !LN0->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00002950 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT)) {
Dan Gohman475871a2008-07-27 21:46:04 +00002951 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00002952 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002953 LN0->getSrcValueOffset(), EVT,
2954 LN0->isVolatile(),
2955 LN0->getAlignment());
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00002956 CombineTo(N, ExtLoad);
Gabor Greif12632d22008-08-30 19:29:20 +00002957 CombineTo(N0.getNode(),
2958 DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00002959 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00002960 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00002961 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002962 }
2963
Chris Lattner20a35c32007-04-11 05:32:27 +00002964 // sext(setcc x,y,cc) -> select_cc x, y, -1, 0, cc
2965 if (N0.getOpcode() == ISD::SETCC) {
Dan Gohman475871a2008-07-27 21:46:04 +00002966 SDValue SCC =
Chris Lattner1eba01e2007-04-11 06:50:51 +00002967 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
2968 DAG.getConstant(~0ULL, VT), DAG.getConstant(0, VT),
2969 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00002970 if (SCC.getNode()) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00002971 }
2972
Dan Gohman8f0ad582008-04-28 16:58:24 +00002973 // fold (sext x) -> (zext x) if the sign bit is known zero.
Dan Gohman187db7b2008-04-28 18:47:17 +00002974 if ((!AfterLegalize || TLI.isOperationLegal(ISD::ZERO_EXTEND, VT)) &&
2975 DAG.SignBitIsZero(N0))
Dan Gohman8f0ad582008-04-28 16:58:24 +00002976 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
2977
Dan Gohman475871a2008-07-27 21:46:04 +00002978 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002979}
2980
Dan Gohman475871a2008-07-27 21:46:04 +00002981SDValue DAGCombiner::visitZERO_EXTEND(SDNode *N) {
2982 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002983 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002984
Nate Begeman1d4d4142005-09-01 00:19:25 +00002985 // fold (zext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00002986 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002987 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002988 // fold (zext (zext x)) -> (zext x)
Chris Lattner310b5782006-05-06 23:06:26 +00002989 // fold (zext (aext x)) -> (zext x)
2990 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002991 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
Chris Lattner6007b842006-09-21 06:00:20 +00002992
Evan Chengc88138f2007-03-22 01:54:19 +00002993 // fold (zext (truncate (load x))) -> (zext (smaller load x))
2994 // fold (zext (truncate (srl (load x), c))) -> (zext (small load (x+c/n)))
Dale Johannesen2041a0e2007-03-30 21:38:07 +00002995 if (N0.getOpcode() == ISD::TRUNCATE) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002996 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
2997 if (NarrowLoad.getNode()) {
2998 if (NarrowLoad.getNode() != N0.getNode())
2999 CombineTo(N0.getNode(), NarrowLoad);
Evan Cheng0b063de2007-03-23 02:16:52 +00003000 return DAG.getNode(ISD::ZERO_EXTEND, VT, NarrowLoad);
3001 }
Evan Chengc88138f2007-03-22 01:54:19 +00003002 }
3003
Chris Lattner6007b842006-09-21 06:00:20 +00003004 // fold (zext (truncate x)) -> (and x, mask)
3005 if (N0.getOpcode() == ISD::TRUNCATE &&
3006 (!AfterLegalize || TLI.isOperationLegal(ISD::AND, VT))) {
Dan Gohman475871a2008-07-27 21:46:04 +00003007 SDValue Op = N0.getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003008 if (Op.getValueType().bitsLT(VT)) {
Chris Lattner6007b842006-09-21 06:00:20 +00003009 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003010 } else if (Op.getValueType().bitsGT(VT)) {
Chris Lattner6007b842006-09-21 06:00:20 +00003011 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
3012 }
3013 return DAG.getZeroExtendInReg(Op, N0.getValueType());
3014 }
3015
Chris Lattner111c2282006-09-21 06:14:31 +00003016 // fold (zext (and (trunc x), cst)) -> (and x, cst).
3017 if (N0.getOpcode() == ISD::AND &&
3018 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
3019 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohman475871a2008-07-27 21:46:04 +00003020 SDValue X = N0.getOperand(0).getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003021 if (X.getValueType().bitsLT(VT)) {
Chris Lattner111c2282006-09-21 06:14:31 +00003022 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003023 } else if (X.getValueType().bitsGT(VT)) {
Chris Lattner111c2282006-09-21 06:14:31 +00003024 X = DAG.getNode(ISD::TRUNCATE, VT, X);
3025 }
Dan Gohman220a8232008-03-03 23:51:38 +00003026 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003027 Mask.zext(VT.getSizeInBits());
Chris Lattner111c2282006-09-21 06:14:31 +00003028 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
3029 }
3030
Evan Cheng110dec22005-12-14 02:19:23 +00003031 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00003032 if (ISD::isNON_EXTLoad(N0.getNode()) &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003033 ((!AfterLegalize && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00003034 TLI.isLoadExtLegal(ISD::ZEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003035 bool DoXform = true;
3036 SmallVector<SDNode*, 4> SetCCs;
3037 if (!N0.hasOneUse())
3038 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ZERO_EXTEND, SetCCs, TLI);
3039 if (DoXform) {
3040 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman475871a2008-07-27 21:46:04 +00003041 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003042 LN0->getBasePtr(), LN0->getSrcValue(),
3043 LN0->getSrcValueOffset(),
3044 N0.getValueType(),
3045 LN0->isVolatile(),
3046 LN0->getAlignment());
3047 CombineTo(N, ExtLoad);
Dan Gohman475871a2008-07-27 21:46:04 +00003048 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00003049 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003050 // Extend SetCC uses if necessary.
3051 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
3052 SDNode *SetCC = SetCCs[i];
Dan Gohman475871a2008-07-27 21:46:04 +00003053 SmallVector<SDValue, 4> Ops;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003054 for (unsigned j = 0; j != 2; ++j) {
Dan Gohman475871a2008-07-27 21:46:04 +00003055 SDValue SOp = SetCC->getOperand(j);
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003056 if (SOp == Trunc)
3057 Ops.push_back(ExtLoad);
3058 else
Evan Chengde1631b2007-10-30 20:11:21 +00003059 Ops.push_back(DAG.getNode(ISD::ZERO_EXTEND, VT, SOp));
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003060 }
3061 Ops.push_back(SetCC->getOperand(2));
3062 CombineTo(SetCC, DAG.getNode(ISD::SETCC, SetCC->getValueType(0),
3063 &Ops[0], Ops.size()));
3064 }
Dan Gohman475871a2008-07-27 21:46:04 +00003065 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003066 }
Evan Cheng110dec22005-12-14 02:19:23 +00003067 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00003068
3069 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
3070 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00003071 if ((ISD::isZEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) &&
3072 ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00003073 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003074 MVT EVT = LN0->getMemoryVT();
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003075 if ((!AfterLegalize && !LN0->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00003076 TLI.isLoadExtLegal(ISD::ZEXTLOAD, EVT)) {
Dan Gohman475871a2008-07-27 21:46:04 +00003077 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003078 LN0->getBasePtr(), LN0->getSrcValue(),
3079 LN0->getSrcValueOffset(), EVT,
3080 LN0->isVolatile(),
3081 LN0->getAlignment());
3082 CombineTo(N, ExtLoad);
Gabor Greif12632d22008-08-30 19:29:20 +00003083 CombineTo(N0.getNode(),
3084 DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003085 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003086 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003087 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00003088 }
Chris Lattner20a35c32007-04-11 05:32:27 +00003089
3090 // zext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
3091 if (N0.getOpcode() == ISD::SETCC) {
Dan Gohman475871a2008-07-27 21:46:04 +00003092 SDValue SCC =
Chris Lattner20a35c32007-04-11 05:32:27 +00003093 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
3094 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattner1eba01e2007-04-11 06:50:51 +00003095 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00003096 if (SCC.getNode()) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00003097 }
3098
Dan Gohman475871a2008-07-27 21:46:04 +00003099 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003100}
3101
Dan Gohman475871a2008-07-27 21:46:04 +00003102SDValue DAGCombiner::visitANY_EXTEND(SDNode *N) {
3103 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003104 MVT VT = N->getValueType(0);
Chris Lattner5ffc0662006-05-05 05:58:59 +00003105
3106 // fold (aext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00003107 if (isa<ConstantSDNode>(N0))
Chris Lattner5ffc0662006-05-05 05:58:59 +00003108 return DAG.getNode(ISD::ANY_EXTEND, VT, N0);
3109 // fold (aext (aext x)) -> (aext x)
3110 // fold (aext (zext x)) -> (zext x)
3111 // fold (aext (sext x)) -> (sext x)
3112 if (N0.getOpcode() == ISD::ANY_EXTEND ||
3113 N0.getOpcode() == ISD::ZERO_EXTEND ||
3114 N0.getOpcode() == ISD::SIGN_EXTEND)
3115 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
3116
Evan Chengc88138f2007-03-22 01:54:19 +00003117 // fold (aext (truncate (load x))) -> (aext (smaller load x))
3118 // fold (aext (truncate (srl (load x), c))) -> (aext (small load (x+c/n)))
3119 if (N0.getOpcode() == ISD::TRUNCATE) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003120 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
3121 if (NarrowLoad.getNode()) {
3122 if (NarrowLoad.getNode() != N0.getNode())
3123 CombineTo(N0.getNode(), NarrowLoad);
Evan Cheng0b063de2007-03-23 02:16:52 +00003124 return DAG.getNode(ISD::ANY_EXTEND, VT, NarrowLoad);
3125 }
Evan Chengc88138f2007-03-22 01:54:19 +00003126 }
3127
Chris Lattner84750582006-09-20 06:29:17 +00003128 // fold (aext (truncate x))
3129 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman475871a2008-07-27 21:46:04 +00003130 SDValue TruncOp = N0.getOperand(0);
Chris Lattner84750582006-09-20 06:29:17 +00003131 if (TruncOp.getValueType() == VT)
3132 return TruncOp; // x iff x size == zext size.
Duncan Sands8e4eb092008-06-08 20:54:56 +00003133 if (TruncOp.getValueType().bitsGT(VT))
Chris Lattner84750582006-09-20 06:29:17 +00003134 return DAG.getNode(ISD::TRUNCATE, VT, TruncOp);
3135 return DAG.getNode(ISD::ANY_EXTEND, VT, TruncOp);
3136 }
Chris Lattner0e4b9222006-09-21 06:40:43 +00003137
3138 // fold (aext (and (trunc x), cst)) -> (and x, cst).
3139 if (N0.getOpcode() == ISD::AND &&
3140 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
3141 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohman475871a2008-07-27 21:46:04 +00003142 SDValue X = N0.getOperand(0).getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003143 if (X.getValueType().bitsLT(VT)) {
Chris Lattner0e4b9222006-09-21 06:40:43 +00003144 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003145 } else if (X.getValueType().bitsGT(VT)) {
Chris Lattner0e4b9222006-09-21 06:40:43 +00003146 X = DAG.getNode(ISD::TRUNCATE, VT, X);
3147 }
Dan Gohman220a8232008-03-03 23:51:38 +00003148 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003149 Mask.zext(VT.getSizeInBits());
Chris Lattner0e4b9222006-09-21 06:40:43 +00003150 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
3151 }
3152
Chris Lattner5ffc0662006-05-05 05:58:59 +00003153 // fold (aext (load x)) -> (aext (truncate (extload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00003154 if (ISD::isNON_EXTLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003155 ((!AfterLegalize && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00003156 TLI.isLoadExtLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003157 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman475871a2008-07-27 21:46:04 +00003158 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
Evan Cheng466685d2006-10-09 20:57:25 +00003159 LN0->getBasePtr(), LN0->getSrcValue(),
3160 LN0->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003161 N0.getValueType(),
3162 LN0->isVolatile(),
3163 LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00003164 CombineTo(N, ExtLoad);
Dan Gohman75dcf082008-07-31 00:50:31 +00003165 // Redirect any chain users to the new load.
Evan Cheng45299662008-08-29 23:20:46 +00003166 DAG.ReplaceAllUsesOfValueWith(SDValue(LN0, 1),
3167 SDValue(ExtLoad.getNode(), 1));
Dan Gohman75dcf082008-07-31 00:50:31 +00003168 // If any node needs the original loaded value, recompute it.
3169 if (!LN0->use_empty())
3170 CombineTo(LN0, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
3171 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003172 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner5ffc0662006-05-05 05:58:59 +00003173 }
3174
3175 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
3176 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
3177 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00003178 if (N0.getOpcode() == ISD::LOAD &&
Gabor Greifba36cb52008-08-28 21:40:38 +00003179 !ISD::isNON_EXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng466685d2006-10-09 20:57:25 +00003180 N0.hasOneUse()) {
3181 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003182 MVT EVT = LN0->getMemoryVT();
Dan Gohman475871a2008-07-27 21:46:04 +00003183 SDValue ExtLoad = DAG.getExtLoad(LN0->getExtensionType(), VT,
Evan Cheng466685d2006-10-09 20:57:25 +00003184 LN0->getChain(), LN0->getBasePtr(),
3185 LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003186 LN0->getSrcValueOffset(), EVT,
3187 LN0->isVolatile(),
3188 LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00003189 CombineTo(N, ExtLoad);
Evan Cheng45299662008-08-29 23:20:46 +00003190 CombineTo(N0.getNode(),
3191 DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
Chris Lattner5ffc0662006-05-05 05:58:59 +00003192 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003193 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner5ffc0662006-05-05 05:58:59 +00003194 }
Chris Lattner20a35c32007-04-11 05:32:27 +00003195
3196 // aext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
3197 if (N0.getOpcode() == ISD::SETCC) {
Dan Gohman475871a2008-07-27 21:46:04 +00003198 SDValue SCC =
Chris Lattner1eba01e2007-04-11 06:50:51 +00003199 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
3200 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattnerc24bbad2007-04-11 16:51:53 +00003201 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00003202 if (SCC.getNode())
Chris Lattnerc56a81d2007-04-11 06:43:25 +00003203 return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00003204 }
3205
Dan Gohman475871a2008-07-27 21:46:04 +00003206 return SDValue();
Chris Lattner5ffc0662006-05-05 05:58:59 +00003207}
3208
Chris Lattner2b4c2792007-10-13 06:35:54 +00003209/// GetDemandedBits - See if the specified operand can be simplified with the
3210/// knowledge that only the bits specified by Mask are used. If so, return the
Dan Gohman475871a2008-07-27 21:46:04 +00003211/// simpler operand, otherwise return a null SDValue.
3212SDValue DAGCombiner::GetDemandedBits(SDValue V, const APInt &Mask) {
Chris Lattner2b4c2792007-10-13 06:35:54 +00003213 switch (V.getOpcode()) {
3214 default: break;
3215 case ISD::OR:
3216 case ISD::XOR:
3217 // If the LHS or RHS don't contribute bits to the or, drop them.
3218 if (DAG.MaskedValueIsZero(V.getOperand(0), Mask))
3219 return V.getOperand(1);
3220 if (DAG.MaskedValueIsZero(V.getOperand(1), Mask))
3221 return V.getOperand(0);
3222 break;
Chris Lattnere33544c2007-10-13 06:58:48 +00003223 case ISD::SRL:
3224 // Only look at single-use SRLs.
Gabor Greifba36cb52008-08-28 21:40:38 +00003225 if (!V.getNode()->hasOneUse())
Chris Lattnere33544c2007-10-13 06:58:48 +00003226 break;
3227 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
3228 // See if we can recursively simplify the LHS.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003229 unsigned Amt = RHSC->getZExtValue();
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003230 APInt NewMask = Mask << Amt;
Dan Gohman475871a2008-07-27 21:46:04 +00003231 SDValue SimplifyLHS = GetDemandedBits(V.getOperand(0), NewMask);
Gabor Greifba36cb52008-08-28 21:40:38 +00003232 if (SimplifyLHS.getNode()) {
Chris Lattnere33544c2007-10-13 06:58:48 +00003233 return DAG.getNode(ISD::SRL, V.getValueType(),
3234 SimplifyLHS, V.getOperand(1));
3235 }
3236 }
Chris Lattner2b4c2792007-10-13 06:35:54 +00003237 }
Dan Gohman475871a2008-07-27 21:46:04 +00003238 return SDValue();
Chris Lattner2b4c2792007-10-13 06:35:54 +00003239}
3240
Evan Chengc88138f2007-03-22 01:54:19 +00003241/// ReduceLoadWidth - If the result of a wider load is shifted to right of N
3242/// bits and then truncated to a narrower type and where N is a multiple
3243/// of number of bits of the narrower type, transform it to a narrower load
3244/// from address + N / num of bits of new type. If the result is to be
3245/// extended, also fold the extension to form a extending load.
Dan Gohman475871a2008-07-27 21:46:04 +00003246SDValue DAGCombiner::ReduceLoadWidth(SDNode *N) {
Evan Chengc88138f2007-03-22 01:54:19 +00003247 unsigned Opc = N->getOpcode();
3248 ISD::LoadExtType ExtType = ISD::NON_EXTLOAD;
Dan Gohman475871a2008-07-27 21:46:04 +00003249 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003250 MVT VT = N->getValueType(0);
3251 MVT EVT = N->getValueType(0);
Evan Chengc88138f2007-03-22 01:54:19 +00003252
Dan Gohman7f8613e2008-08-14 20:04:46 +00003253 // This transformation isn't valid for vector loads.
3254 if (VT.isVector())
3255 return SDValue();
3256
Evan Chenge177e302007-03-23 22:13:36 +00003257 // Special case: SIGN_EXTEND_INREG is basically truncating to EVT then
3258 // extended to VT.
Evan Chengc88138f2007-03-22 01:54:19 +00003259 if (Opc == ISD::SIGN_EXTEND_INREG) {
3260 ExtType = ISD::SEXTLOAD;
3261 EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Evan Cheng03294662008-10-14 21:26:46 +00003262 if (AfterLegalize && !TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))
Dan Gohman475871a2008-07-27 21:46:04 +00003263 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00003264 }
3265
Duncan Sands83ec4b62008-06-06 12:08:01 +00003266 unsigned EVTBits = EVT.getSizeInBits();
Evan Chengc88138f2007-03-22 01:54:19 +00003267 unsigned ShAmt = 0;
Evan Chengb37b80c2007-03-23 20:55:21 +00003268 bool CombineSRL = false;
Evan Chengc88138f2007-03-22 01:54:19 +00003269 if (N0.getOpcode() == ISD::SRL && N0.hasOneUse()) {
3270 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003271 ShAmt = N01->getZExtValue();
Evan Chengc88138f2007-03-22 01:54:19 +00003272 // Is the shift amount a multiple of size of VT?
3273 if ((ShAmt & (EVTBits-1)) == 0) {
3274 N0 = N0.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003275 if (N0.getValueType().getSizeInBits() <= EVTBits)
Dan Gohman475871a2008-07-27 21:46:04 +00003276 return SDValue();
Evan Chengb37b80c2007-03-23 20:55:21 +00003277 CombineSRL = true;
Evan Chengc88138f2007-03-22 01:54:19 +00003278 }
3279 }
3280 }
3281
Duncan Sandsad205a72008-06-16 08:14:38 +00003282 // Do not generate loads of non-round integer types since these can
3283 // be expensive (and would be wrong if the type is not byte sized).
Dan Gohman75dcf082008-07-31 00:50:31 +00003284 if (isa<LoadSDNode>(N0) && N0.hasOneUse() && VT.isRound() &&
3285 cast<LoadSDNode>(N0)->getMemoryVT().getSizeInBits() > EVTBits &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003286 // Do not change the width of a volatile load.
3287 !cast<LoadSDNode>(N0)->isVolatile()) {
Evan Chengc88138f2007-03-22 01:54:19 +00003288 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003289 MVT PtrType = N0.getOperand(1).getValueType();
Evan Chengdae54ce2007-03-24 00:02:43 +00003290 // For big endian targets, we need to adjust the offset to the pointer to
3291 // load the correct bytes.
Duncan Sands0753fc12008-02-11 10:37:04 +00003292 if (TLI.isBigEndian()) {
Dan Gohman75dcf082008-07-31 00:50:31 +00003293 unsigned LVTStoreBits = LN0->getMemoryVT().getStoreSizeInBits();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003294 unsigned EVTStoreBits = EVT.getStoreSizeInBits();
Duncan Sandsc6fa1702007-11-09 08:57:19 +00003295 ShAmt = LVTStoreBits - EVTStoreBits - ShAmt;
3296 }
Evan Chengdae54ce2007-03-24 00:02:43 +00003297 uint64_t PtrOff = ShAmt / 8;
Duncan Sandsdc846502007-10-28 12:59:45 +00003298 unsigned NewAlign = MinAlign(LN0->getAlignment(), PtrOff);
Dan Gohman475871a2008-07-27 21:46:04 +00003299 SDValue NewPtr = DAG.getNode(ISD::ADD, PtrType, LN0->getBasePtr(),
Evan Chengc88138f2007-03-22 01:54:19 +00003300 DAG.getConstant(PtrOff, PtrType));
Gabor Greifba36cb52008-08-28 21:40:38 +00003301 AddToWorkList(NewPtr.getNode());
Dan Gohman475871a2008-07-27 21:46:04 +00003302 SDValue Load = (ExtType == ISD::NON_EXTLOAD)
Evan Chengc88138f2007-03-22 01:54:19 +00003303 ? DAG.getLoad(VT, LN0->getChain(), NewPtr,
Dan Gohman75dcf082008-07-31 00:50:31 +00003304 LN0->getSrcValue(), LN0->getSrcValueOffset() + PtrOff,
Duncan Sandsdc846502007-10-28 12:59:45 +00003305 LN0->isVolatile(), NewAlign)
Evan Chengc88138f2007-03-22 01:54:19 +00003306 : DAG.getExtLoad(ExtType, VT, LN0->getChain(), NewPtr,
Dan Gohman75dcf082008-07-31 00:50:31 +00003307 LN0->getSrcValue(), LN0->getSrcValueOffset() + PtrOff,
3308 EVT, LN0->isVolatile(), NewAlign);
Evan Chengc88138f2007-03-22 01:54:19 +00003309 AddToWorkList(N);
Evan Chengb37b80c2007-03-23 20:55:21 +00003310 if (CombineSRL) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003311 WorkListRemover DeadNodes(*this);
3312 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), Load.getValue(1),
3313 &DeadNodes);
Gabor Greifba36cb52008-08-28 21:40:38 +00003314 CombineTo(N->getOperand(0).getNode(), Load);
Evan Chengb37b80c2007-03-23 20:55:21 +00003315 } else
Gabor Greifba36cb52008-08-28 21:40:38 +00003316 CombineTo(N0.getNode(), Load, Load.getValue(1));
Evan Cheng15213b72007-03-26 07:12:51 +00003317 if (ShAmt) {
3318 if (Opc == ISD::SIGN_EXTEND_INREG)
3319 return DAG.getNode(Opc, VT, Load, N->getOperand(1));
3320 else
3321 return DAG.getNode(Opc, VT, Load);
3322 }
Dan Gohman475871a2008-07-27 21:46:04 +00003323 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Chengc88138f2007-03-22 01:54:19 +00003324 }
3325
Dan Gohman475871a2008-07-27 21:46:04 +00003326 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00003327}
3328
Chris Lattner5ffc0662006-05-05 05:58:59 +00003329
Dan Gohman475871a2008-07-27 21:46:04 +00003330SDValue DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
3331 SDValue N0 = N->getOperand(0);
3332 SDValue N1 = N->getOperand(1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003333 MVT VT = N->getValueType(0);
3334 MVT EVT = cast<VTSDNode>(N1)->getVT();
3335 unsigned VTBits = VT.getSizeInBits();
3336 unsigned EVTBits = EVT.getSizeInBits();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003337
Nate Begeman1d4d4142005-09-01 00:19:25 +00003338 // fold (sext_in_reg c1) -> c1
Chris Lattnereaeda562006-05-08 20:59:41 +00003339 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
Chris Lattner310b5782006-05-06 23:06:26 +00003340 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0, N1);
Chris Lattneree4ea922006-05-06 09:30:03 +00003341
Chris Lattner541a24f2006-05-06 22:43:44 +00003342 // If the input is already sign extended, just drop the extension.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003343 if (DAG.ComputeNumSignBits(N0) >= VT.getSizeInBits()-EVTBits+1)
Chris Lattneree4ea922006-05-06 09:30:03 +00003344 return N0;
3345
Nate Begeman646d7e22005-09-02 21:18:40 +00003346 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
3347 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Duncan Sands8e4eb092008-06-08 20:54:56 +00003348 EVT.bitsLT(cast<VTSDNode>(N0.getOperand(1))->getVT())) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00003349 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00003350 }
Chris Lattner4b37e872006-05-08 21:18:59 +00003351
Dan Gohman75dcf082008-07-31 00:50:31 +00003352 // fold (sext_in_reg (sext x)) -> (sext x)
3353 // fold (sext_in_reg (aext x)) -> (sext x)
3354 // if x is small enough.
3355 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND) {
3356 SDValue N00 = N0.getOperand(0);
3357 if (N00.getValueType().getSizeInBits() < EVTBits)
3358 return DAG.getNode(ISD::SIGN_EXTEND, VT, N00, N1);
3359 }
3360
Chris Lattner95a5e052007-04-17 19:03:21 +00003361 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is known zero.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003362 if (DAG.MaskedValueIsZero(N0, APInt::getBitsSet(VTBits, EVTBits-1, EVTBits)))
Nate Begemande996292006-02-03 22:24:05 +00003363 return DAG.getZeroExtendInReg(N0, EVT);
Chris Lattner4b37e872006-05-08 21:18:59 +00003364
Chris Lattner95a5e052007-04-17 19:03:21 +00003365 // fold operands of sext_in_reg based on knowledge that the top bits are not
3366 // demanded.
Dan Gohman475871a2008-07-27 21:46:04 +00003367 if (SimplifyDemandedBits(SDValue(N, 0)))
3368 return SDValue(N, 0);
Chris Lattner95a5e052007-04-17 19:03:21 +00003369
Evan Chengc88138f2007-03-22 01:54:19 +00003370 // fold (sext_in_reg (load x)) -> (smaller sextload x)
3371 // fold (sext_in_reg (srl (load x), c)) -> (smaller sextload (x+c/evtbits))
Dan Gohman475871a2008-07-27 21:46:04 +00003372 SDValue NarrowLoad = ReduceLoadWidth(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003373 if (NarrowLoad.getNode())
Evan Chengc88138f2007-03-22 01:54:19 +00003374 return NarrowLoad;
3375
Chris Lattner4b37e872006-05-08 21:18:59 +00003376 // fold (sext_in_reg (srl X, 24), i8) -> sra X, 24
3377 // fold (sext_in_reg (srl X, 23), i8) -> sra X, 23 iff possible.
3378 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
3379 if (N0.getOpcode() == ISD::SRL) {
3380 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003381 if (ShAmt->getZExtValue()+EVTBits <= VT.getSizeInBits()) {
Chris Lattner4b37e872006-05-08 21:18:59 +00003382 // We can turn this into an SRA iff the input to the SRL is already sign
3383 // extended enough.
Dan Gohmanea859be2007-06-22 14:59:07 +00003384 unsigned InSignBits = DAG.ComputeNumSignBits(N0.getOperand(0));
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003385 if (VT.getSizeInBits()-(ShAmt->getZExtValue()+EVTBits) < InSignBits)
Chris Lattner4b37e872006-05-08 21:18:59 +00003386 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0), N0.getOperand(1));
3387 }
3388 }
Evan Chengc88138f2007-03-22 01:54:19 +00003389
Nate Begemanded49632005-10-13 03:11:28 +00003390 // fold (sext_inreg (extload x)) -> (sextload x)
Gabor Greifba36cb52008-08-28 21:40:38 +00003391 if (ISD::isEXTLoad(N0.getNode()) &&
3392 ISD::isUNINDEXEDLoad(N0.getNode()) &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003393 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003394 ((!AfterLegalize && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00003395 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003396 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman475871a2008-07-27 21:46:04 +00003397 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
Evan Cheng466685d2006-10-09 20:57:25 +00003398 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003399 LN0->getSrcValueOffset(), EVT,
3400 LN0->isVolatile(),
3401 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00003402 CombineTo(N, ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00003403 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003404 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00003405 }
3406 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Gabor Greifba36cb52008-08-28 21:40:38 +00003407 if (ISD::isZEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng83060c52007-03-07 08:07:03 +00003408 N0.hasOneUse() &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003409 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003410 ((!AfterLegalize && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00003411 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003412 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman475871a2008-07-27 21:46:04 +00003413 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
Evan Cheng466685d2006-10-09 20:57:25 +00003414 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003415 LN0->getSrcValueOffset(), EVT,
3416 LN0->isVolatile(),
3417 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00003418 CombineTo(N, ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00003419 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003420 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00003421 }
Dan Gohman475871a2008-07-27 21:46:04 +00003422 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003423}
3424
Dan Gohman475871a2008-07-27 21:46:04 +00003425SDValue DAGCombiner::visitTRUNCATE(SDNode *N) {
3426 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003427 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003428
3429 // noop truncate
3430 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00003431 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003432 // fold (truncate c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00003433 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00003434 return DAG.getNode(ISD::TRUNCATE, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003435 // fold (truncate (truncate x)) -> (truncate x)
3436 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00003437 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003438 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
Chris Lattnerb72773b2006-05-05 22:56:26 +00003439 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND||
3440 N0.getOpcode() == ISD::ANY_EXTEND) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00003441 if (N0.getOperand(0).getValueType().bitsLT(VT))
Nate Begeman1d4d4142005-09-01 00:19:25 +00003442 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00003443 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Duncan Sands8e4eb092008-06-08 20:54:56 +00003444 else if (N0.getOperand(0).getValueType().bitsGT(VT))
Nate Begeman1d4d4142005-09-01 00:19:25 +00003445 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00003446 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003447 else
3448 // if the source and dest are the same type, we can drop both the extend
3449 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00003450 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003451 }
Evan Cheng007b69e2007-03-21 20:14:05 +00003452
Chris Lattner2b4c2792007-10-13 06:35:54 +00003453 // See if we can simplify the input to this truncate through knowledge that
3454 // only the low bits are being used. For example "trunc (or (shl x, 8), y)"
3455 // -> trunc y
Dan Gohman475871a2008-07-27 21:46:04 +00003456 SDValue Shorter =
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003457 GetDemandedBits(N0, APInt::getLowBitsSet(N0.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +00003458 VT.getSizeInBits()));
Gabor Greifba36cb52008-08-28 21:40:38 +00003459 if (Shorter.getNode())
Chris Lattner2b4c2792007-10-13 06:35:54 +00003460 return DAG.getNode(ISD::TRUNCATE, VT, Shorter);
3461
Nate Begeman3df4d522005-10-12 20:40:40 +00003462 // fold (truncate (load x)) -> (smaller load x)
Evan Cheng007b69e2007-03-21 20:14:05 +00003463 // fold (truncate (srl (load x), c)) -> (smaller load (x+c/evtbits))
Evan Chengc88138f2007-03-22 01:54:19 +00003464 return ReduceLoadWidth(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003465}
3466
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003467static SDNode *getBuildPairElt(SDNode *N, unsigned i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003468 SDValue Elt = N->getOperand(i);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003469 if (Elt.getOpcode() != ISD::MERGE_VALUES)
Gabor Greifba36cb52008-08-28 21:40:38 +00003470 return Elt.getNode();
3471 return Elt.getOperand(Elt.getResNo()).getNode();
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003472}
3473
3474/// CombineConsecutiveLoads - build_pair (load, load) -> load
3475/// if load locations are consecutive.
Dan Gohman475871a2008-07-27 21:46:04 +00003476SDValue DAGCombiner::CombineConsecutiveLoads(SDNode *N, MVT VT) {
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003477 assert(N->getOpcode() == ISD::BUILD_PAIR);
3478
3479 SDNode *LD1 = getBuildPairElt(N, 0);
3480 if (!ISD::isNON_EXTLoad(LD1) || !LD1->hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00003481 return SDValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003482 MVT LD1VT = LD1->getValueType(0);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003483 SDNode *LD2 = getBuildPairElt(N, 1);
3484 const MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3485 if (ISD::isNON_EXTLoad(LD2) &&
3486 LD2->hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003487 // If both are volatile this would reduce the number of volatile loads.
3488 // If one is volatile it might be ok, but play conservative and bail out.
3489 !cast<LoadSDNode>(LD1)->isVolatile() &&
3490 !cast<LoadSDNode>(LD2)->isVolatile() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003491 TLI.isConsecutiveLoad(LD2, LD1, LD1VT.getSizeInBits()/8, 1, MFI)) {
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003492 LoadSDNode *LD = cast<LoadSDNode>(LD1);
3493 unsigned Align = LD->getAlignment();
Dan Gohman6448d912008-09-04 15:39:15 +00003494 unsigned NewAlign = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00003495 getABITypeAlignment(VT.getTypeForMVT());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003496 if (NewAlign <= Align &&
3497 (!AfterLegalize || TLI.isOperationLegal(ISD::LOAD, VT)))
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003498 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(),
3499 LD->getSrcValue(), LD->getSrcValueOffset(),
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003500 false, Align);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003501 }
Dan Gohman475871a2008-07-27 21:46:04 +00003502 return SDValue();
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003503}
3504
Dan Gohman475871a2008-07-27 21:46:04 +00003505SDValue DAGCombiner::visitBIT_CONVERT(SDNode *N) {
3506 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003507 MVT VT = N->getValueType(0);
Chris Lattner94683772005-12-23 05:30:37 +00003508
Dan Gohman7f321562007-06-25 16:23:39 +00003509 // If the input is a BUILD_VECTOR with all constant elements, fold this now.
3510 // Only do this before legalize, since afterward the target may be depending
3511 // on the bitconvert.
3512 // First check to see if this is all constant.
3513 if (!AfterLegalize &&
Gabor Greifba36cb52008-08-28 21:40:38 +00003514 N0.getOpcode() == ISD::BUILD_VECTOR && N0.getNode()->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003515 VT.isVector()) {
Dan Gohman7f321562007-06-25 16:23:39 +00003516 bool isSimple = true;
3517 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i)
3518 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
3519 N0.getOperand(i).getOpcode() != ISD::Constant &&
3520 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
3521 isSimple = false;
3522 break;
3523 }
3524
Duncan Sands83ec4b62008-06-06 12:08:01 +00003525 MVT DestEltVT = N->getValueType(0).getVectorElementType();
3526 assert(!DestEltVT.isVector() &&
Dan Gohman7f321562007-06-25 16:23:39 +00003527 "Element type of vector ValueType must not be vector!");
3528 if (isSimple) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003529 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(N0.getNode(), DestEltVT);
Dan Gohman7f321562007-06-25 16:23:39 +00003530 }
3531 }
3532
Dan Gohman3dd168d2008-09-05 01:58:21 +00003533 // If the input is a constant, let getNode fold it.
Chris Lattner94683772005-12-23 05:30:37 +00003534 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
Dan Gohman475871a2008-07-27 21:46:04 +00003535 SDValue Res = DAG.getNode(ISD::BIT_CONVERT, VT, N0);
Gabor Greifba36cb52008-08-28 21:40:38 +00003536 if (Res.getNode() != N) return Res;
Chris Lattner94683772005-12-23 05:30:37 +00003537 }
3538
Chris Lattnerc8547d82005-12-23 05:37:50 +00003539 if (N0.getOpcode() == ISD::BIT_CONVERT) // conv(conv(x,t1),t2) -> conv(x,t2)
3540 return DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
Chris Lattner6258fb22006-04-02 02:53:43 +00003541
Chris Lattner57104102005-12-23 05:44:41 +00003542 // fold (conv (load x)) -> (load (conv*)x)
Evan Cheng513da432007-10-06 08:19:55 +00003543 // If the resultant load doesn't need a higher alignment than the original!
Gabor Greifba36cb52008-08-28 21:40:38 +00003544 if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003545 // Do not change the width of a volatile load.
3546 !cast<LoadSDNode>(N0)->isVolatile() &&
3547 (!AfterLegalize || TLI.isOperationLegal(ISD::LOAD, VT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003548 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman6448d912008-09-04 15:39:15 +00003549 unsigned Align = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00003550 getABITypeAlignment(VT.getTypeForMVT());
Evan Cheng59d5b682007-05-07 21:27:48 +00003551 unsigned OrigAlign = LN0->getAlignment();
3552 if (Align <= OrigAlign) {
Dan Gohman475871a2008-07-27 21:46:04 +00003553 SDValue Load = DAG.getLoad(VT, LN0->getChain(), LN0->getBasePtr(),
Evan Cheng59d5b682007-05-07 21:27:48 +00003554 LN0->getSrcValue(), LN0->getSrcValueOffset(),
Dan Gohman77455612008-06-28 00:45:22 +00003555 LN0->isVolatile(), OrigAlign);
Evan Cheng59d5b682007-05-07 21:27:48 +00003556 AddToWorkList(N);
Gabor Greif12632d22008-08-30 19:29:20 +00003557 CombineTo(N0.getNode(),
3558 DAG.getNode(ISD::BIT_CONVERT, N0.getValueType(), Load),
Evan Cheng59d5b682007-05-07 21:27:48 +00003559 Load.getValue(1));
3560 return Load;
3561 }
Chris Lattner57104102005-12-23 05:44:41 +00003562 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003563
Chris Lattner3bd39d42008-01-27 17:42:27 +00003564 // Fold bitconvert(fneg(x)) -> xor(bitconvert(x), signbit)
3565 // Fold bitconvert(fabs(x)) -> and(bitconvert(x), ~signbit)
3566 // This often reduces constant pool loads.
3567 if ((N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FABS) &&
Gabor Greifba36cb52008-08-28 21:40:38 +00003568 N0.getNode()->hasOneUse() && VT.isInteger() && !VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003569 SDValue NewConv = DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00003570 AddToWorkList(NewConv.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003571
Duncan Sands83ec4b62008-06-06 12:08:01 +00003572 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003573 if (N0.getOpcode() == ISD::FNEG)
3574 return DAG.getNode(ISD::XOR, VT, NewConv, DAG.getConstant(SignBit, VT));
3575 assert(N0.getOpcode() == ISD::FABS);
3576 return DAG.getNode(ISD::AND, VT, NewConv, DAG.getConstant(~SignBit, VT));
3577 }
3578
3579 // Fold bitconvert(fcopysign(cst, x)) -> bitconvert(x)&sign | cst&~sign'
3580 // Note that we don't handle copysign(x,cst) because this can always be folded
3581 // to an fneg or fabs.
Gabor Greifba36cb52008-08-28 21:40:38 +00003582 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse() &&
Chris Lattnerf32aac32008-01-27 23:32:17 +00003583 isa<ConstantFPSDNode>(N0.getOperand(0)) &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003584 VT.isInteger() && !VT.isVector()) {
3585 unsigned OrigXWidth = N0.getOperand(1).getValueType().getSizeInBits();
Dan Gohman475871a2008-07-27 21:46:04 +00003586 SDValue X = DAG.getNode(ISD::BIT_CONVERT,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003587 MVT::getIntegerVT(OrigXWidth),
Chris Lattner3bd39d42008-01-27 17:42:27 +00003588 N0.getOperand(1));
Gabor Greifba36cb52008-08-28 21:40:38 +00003589 AddToWorkList(X.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003590
3591 // If X has a different width than the result/lhs, sext it or truncate it.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003592 unsigned VTWidth = VT.getSizeInBits();
Chris Lattner3bd39d42008-01-27 17:42:27 +00003593 if (OrigXWidth < VTWidth) {
3594 X = DAG.getNode(ISD::SIGN_EXTEND, VT, X);
Gabor Greifba36cb52008-08-28 21:40:38 +00003595 AddToWorkList(X.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003596 } else if (OrigXWidth > VTWidth) {
3597 // To get the sign bit in the right place, we have to shift it right
3598 // before truncating.
3599 X = DAG.getNode(ISD::SRL, X.getValueType(), X,
3600 DAG.getConstant(OrigXWidth-VTWidth, X.getValueType()));
Gabor Greifba36cb52008-08-28 21:40:38 +00003601 AddToWorkList(X.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003602 X = DAG.getNode(ISD::TRUNCATE, VT, X);
Gabor Greifba36cb52008-08-28 21:40:38 +00003603 AddToWorkList(X.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003604 }
3605
Duncan Sands83ec4b62008-06-06 12:08:01 +00003606 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003607 X = DAG.getNode(ISD::AND, VT, X, DAG.getConstant(SignBit, VT));
Gabor Greifba36cb52008-08-28 21:40:38 +00003608 AddToWorkList(X.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003609
Dan Gohman475871a2008-07-27 21:46:04 +00003610 SDValue Cst = DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
Chris Lattner3bd39d42008-01-27 17:42:27 +00003611 Cst = DAG.getNode(ISD::AND, VT, Cst, DAG.getConstant(~SignBit, VT));
Gabor Greifba36cb52008-08-28 21:40:38 +00003612 AddToWorkList(Cst.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003613
3614 return DAG.getNode(ISD::OR, VT, X, Cst);
3615 }
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003616
3617 // bitconvert(build_pair(ld, ld)) -> ld iff load locations are consecutive.
3618 if (N0.getOpcode() == ISD::BUILD_PAIR) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003619 SDValue CombineLD = CombineConsecutiveLoads(N0.getNode(), VT);
3620 if (CombineLD.getNode())
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003621 return CombineLD;
3622 }
Chris Lattner3bd39d42008-01-27 17:42:27 +00003623
Dan Gohman475871a2008-07-27 21:46:04 +00003624 return SDValue();
Chris Lattner94683772005-12-23 05:30:37 +00003625}
3626
Dan Gohman475871a2008-07-27 21:46:04 +00003627SDValue DAGCombiner::visitBUILD_PAIR(SDNode *N) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003628 MVT VT = N->getValueType(0);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003629 return CombineConsecutiveLoads(N, VT);
3630}
3631
Dan Gohman7f321562007-06-25 16:23:39 +00003632/// ConstantFoldBIT_CONVERTofBUILD_VECTOR - We know that BV is a build_vector
Chris Lattner6258fb22006-04-02 02:53:43 +00003633/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
3634/// destination element value type.
Dan Gohman475871a2008-07-27 21:46:04 +00003635SDValue DAGCombiner::
Duncan Sands83ec4b62008-06-06 12:08:01 +00003636ConstantFoldBIT_CONVERTofBUILD_VECTOR(SDNode *BV, MVT DstEltVT) {
3637 MVT SrcEltVT = BV->getOperand(0).getValueType();
Chris Lattner6258fb22006-04-02 02:53:43 +00003638
3639 // If this is already the right type, we're done.
Dan Gohman475871a2008-07-27 21:46:04 +00003640 if (SrcEltVT == DstEltVT) return SDValue(BV, 0);
Chris Lattner6258fb22006-04-02 02:53:43 +00003641
Duncan Sands83ec4b62008-06-06 12:08:01 +00003642 unsigned SrcBitSize = SrcEltVT.getSizeInBits();
3643 unsigned DstBitSize = DstEltVT.getSizeInBits();
Chris Lattner6258fb22006-04-02 02:53:43 +00003644
3645 // If this is a conversion of N elements of one type to N elements of another
3646 // type, convert each element. This handles FP<->INT cases.
3647 if (SrcBitSize == DstBitSize) {
Dan Gohman475871a2008-07-27 21:46:04 +00003648 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003649 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003650 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, DstEltVT, BV->getOperand(i)));
Gabor Greifba36cb52008-08-28 21:40:38 +00003651 AddToWorkList(Ops.back().getNode());
Chris Lattner3e104b12006-04-08 04:15:24 +00003652 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00003653 MVT VT = MVT::getVectorVT(DstEltVT,
3654 BV->getValueType(0).getVectorNumElements());
Dan Gohman7f321562007-06-25 16:23:39 +00003655 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003656 }
3657
3658 // Otherwise, we're growing or shrinking the elements. To avoid having to
3659 // handle annoying details of growing/shrinking FP values, we convert them to
3660 // int first.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003661 if (SrcEltVT.isFloatingPoint()) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003662 // Convert the input float vector to a int vector where the elements are the
3663 // same sizes.
3664 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
Duncan Sands8eab8a22008-06-09 11:32:28 +00003665 MVT IntVT = MVT::getIntegerVT(SrcEltVT.getSizeInBits());
Gabor Greifba36cb52008-08-28 21:40:38 +00003666 BV = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, IntVT).getNode();
Chris Lattner6258fb22006-04-02 02:53:43 +00003667 SrcEltVT = IntVT;
3668 }
3669
3670 // Now we know the input is an integer vector. If the output is a FP type,
3671 // convert to integer first, then to FP of the right size.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003672 if (DstEltVT.isFloatingPoint()) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003673 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
Duncan Sands8eab8a22008-06-09 11:32:28 +00003674 MVT TmpVT = MVT::getIntegerVT(DstEltVT.getSizeInBits());
Gabor Greifba36cb52008-08-28 21:40:38 +00003675 SDNode *Tmp = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, TmpVT).getNode();
Chris Lattner6258fb22006-04-02 02:53:43 +00003676
3677 // Next, convert to FP elements of the same size.
Dan Gohman7f321562007-06-25 16:23:39 +00003678 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(Tmp, DstEltVT);
Chris Lattner6258fb22006-04-02 02:53:43 +00003679 }
3680
3681 // Okay, we know the src/dst types are both integers of differing types.
3682 // Handling growing first.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003683 assert(SrcEltVT.isInteger() && DstEltVT.isInteger());
Chris Lattner6258fb22006-04-02 02:53:43 +00003684 if (SrcBitSize < DstBitSize) {
3685 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
3686
Dan Gohman475871a2008-07-27 21:46:04 +00003687 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003688 for (unsigned i = 0, e = BV->getNumOperands(); i != e;
Chris Lattner6258fb22006-04-02 02:53:43 +00003689 i += NumInputsPerOutput) {
3690 bool isLE = TLI.isLittleEndian();
Dan Gohman220a8232008-03-03 23:51:38 +00003691 APInt NewBits = APInt(DstBitSize, 0);
Chris Lattner6258fb22006-04-02 02:53:43 +00003692 bool EltIsUndef = true;
3693 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
3694 // Shift the previously computed bits over.
3695 NewBits <<= SrcBitSize;
Dan Gohman475871a2008-07-27 21:46:04 +00003696 SDValue Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
Chris Lattner6258fb22006-04-02 02:53:43 +00003697 if (Op.getOpcode() == ISD::UNDEF) continue;
3698 EltIsUndef = false;
3699
Dan Gohman220a8232008-03-03 23:51:38 +00003700 NewBits |=
3701 APInt(cast<ConstantSDNode>(Op)->getAPIntValue()).zext(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00003702 }
3703
3704 if (EltIsUndef)
3705 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
3706 else
3707 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
3708 }
3709
Duncan Sands83ec4b62008-06-06 12:08:01 +00003710 MVT VT = MVT::getVectorVT(DstEltVT, Ops.size());
Dan Gohman7f321562007-06-25 16:23:39 +00003711 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003712 }
3713
3714 // Finally, this must be the case where we are shrinking elements: each input
3715 // turns into multiple outputs.
Evan Chengefec7512008-02-18 23:04:32 +00003716 bool isS2V = ISD::isScalarToVector(BV);
Chris Lattner6258fb22006-04-02 02:53:43 +00003717 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003718 MVT VT = MVT::getVectorVT(DstEltVT, NumOutputsPerInput*BV->getNumOperands());
Dan Gohman475871a2008-07-27 21:46:04 +00003719 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003720 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003721 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
3722 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
3723 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
3724 continue;
3725 }
Dan Gohman220a8232008-03-03 23:51:38 +00003726 APInt OpVal = cast<ConstantSDNode>(BV->getOperand(i))->getAPIntValue();
Chris Lattner6258fb22006-04-02 02:53:43 +00003727 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
Dan Gohman220a8232008-03-03 23:51:38 +00003728 APInt ThisVal = APInt(OpVal).trunc(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00003729 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
Dan Gohman220a8232008-03-03 23:51:38 +00003730 if (isS2V && i == 0 && j == 0 && APInt(ThisVal).zext(SrcBitSize) == OpVal)
Evan Chengefec7512008-02-18 23:04:32 +00003731 // Simply turn this into a SCALAR_TO_VECTOR of the new type.
3732 return DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Ops[0]);
Dan Gohman220a8232008-03-03 23:51:38 +00003733 OpVal = OpVal.lshr(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00003734 }
3735
3736 // For big endian targets, swap the order of the pieces of each element.
Duncan Sands0753fc12008-02-11 10:37:04 +00003737 if (TLI.isBigEndian())
Chris Lattner6258fb22006-04-02 02:53:43 +00003738 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
3739 }
Dan Gohman7f321562007-06-25 16:23:39 +00003740 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003741}
3742
3743
3744
Dan Gohman475871a2008-07-27 21:46:04 +00003745SDValue DAGCombiner::visitFADD(SDNode *N) {
3746 SDValue N0 = N->getOperand(0);
3747 SDValue N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003748 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3749 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003750 MVT VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00003751
Dan Gohman7f321562007-06-25 16:23:39 +00003752 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003753 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003754 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003755 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00003756 }
Dan Gohman7f321562007-06-25 16:23:39 +00003757
Nate Begemana0e221d2005-10-18 00:28:13 +00003758 // fold (fadd c1, c2) -> c1+c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003759 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003760 return DAG.getNode(ISD::FADD, VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003761 // canonicalize constant to RHS
3762 if (N0CFP && !N1CFP)
3763 return DAG.getNode(ISD::FADD, VT, N1, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00003764 // fold (A + (-B)) -> A-B
Chris Lattner0254e702008-02-26 07:04:54 +00003765 if (isNegatibleForFree(N1, AfterLegalize) == 2)
3766 return DAG.getNode(ISD::FSUB, VT, N0,
3767 GetNegatedExpression(N1, DAG, AfterLegalize));
Chris Lattner01b3d732005-09-28 22:28:18 +00003768 // fold ((-A) + B) -> B-A
Chris Lattner0254e702008-02-26 07:04:54 +00003769 if (isNegatibleForFree(N0, AfterLegalize) == 2)
3770 return DAG.getNode(ISD::FSUB, VT, N1,
3771 GetNegatedExpression(N0, DAG, AfterLegalize));
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003772
3773 // If allowed, fold (fadd (fadd x, c1), c2) -> (fadd x, (fadd c1, c2))
3774 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FADD &&
Gabor Greifba36cb52008-08-28 21:40:38 +00003775 N0.getNode()->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003776 return DAG.getNode(ISD::FADD, VT, N0.getOperand(0),
3777 DAG.getNode(ISD::FADD, VT, N0.getOperand(1), N1));
3778
Dan Gohman475871a2008-07-27 21:46:04 +00003779 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00003780}
3781
Dan Gohman475871a2008-07-27 21:46:04 +00003782SDValue DAGCombiner::visitFSUB(SDNode *N) {
3783 SDValue N0 = N->getOperand(0);
3784 SDValue N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003785 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3786 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003787 MVT VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00003788
Dan Gohman7f321562007-06-25 16:23:39 +00003789 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003790 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003791 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003792 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00003793 }
Dan Gohman7f321562007-06-25 16:23:39 +00003794
Nate Begemana0e221d2005-10-18 00:28:13 +00003795 // fold (fsub c1, c2) -> c1-c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003796 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003797 return DAG.getNode(ISD::FSUB, VT, N0, N1);
Dan Gohman23ff1822007-07-02 15:48:56 +00003798 // fold (0-B) -> -B
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003799 if (UnsafeFPMath && N0CFP && N0CFP->getValueAPF().isZero()) {
Chris Lattner0254e702008-02-26 07:04:54 +00003800 if (isNegatibleForFree(N1, AfterLegalize))
3801 return GetNegatedExpression(N1, DAG, AfterLegalize);
Dan Gohman23ff1822007-07-02 15:48:56 +00003802 return DAG.getNode(ISD::FNEG, VT, N1);
3803 }
Chris Lattner01b3d732005-09-28 22:28:18 +00003804 // fold (A-(-B)) -> A+B
Chris Lattner0254e702008-02-26 07:04:54 +00003805 if (isNegatibleForFree(N1, AfterLegalize))
3806 return DAG.getNode(ISD::FADD, VT, N0,
3807 GetNegatedExpression(N1, DAG, AfterLegalize));
Chris Lattner29446522007-05-14 22:04:50 +00003808
Dan Gohman475871a2008-07-27 21:46:04 +00003809 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00003810}
3811
Dan Gohman475871a2008-07-27 21:46:04 +00003812SDValue DAGCombiner::visitFMUL(SDNode *N) {
3813 SDValue N0 = N->getOperand(0);
3814 SDValue N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003815 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3816 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003817 MVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00003818
Dan Gohman7f321562007-06-25 16:23:39 +00003819 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003820 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003821 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003822 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00003823 }
Dan Gohman7f321562007-06-25 16:23:39 +00003824
Nate Begeman11af4ea2005-10-17 20:40:11 +00003825 // fold (fmul c1, c2) -> c1*c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003826 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003827 return DAG.getNode(ISD::FMUL, VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003828 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00003829 if (N0CFP && !N1CFP)
3830 return DAG.getNode(ISD::FMUL, VT, N1, N0);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003831 // fold (fmul X, 2.0) -> (fadd X, X)
3832 if (N1CFP && N1CFP->isExactlyValue(+2.0))
3833 return DAG.getNode(ISD::FADD, VT, N0, N0);
Chris Lattner29446522007-05-14 22:04:50 +00003834 // fold (fmul X, -1.0) -> (fneg X)
3835 if (N1CFP && N1CFP->isExactlyValue(-1.0))
3836 return DAG.getNode(ISD::FNEG, VT, N0);
3837
3838 // -X * -Y -> X*Y
Chris Lattner0254e702008-02-26 07:04:54 +00003839 if (char LHSNeg = isNegatibleForFree(N0, AfterLegalize)) {
3840 if (char RHSNeg = isNegatibleForFree(N1, AfterLegalize)) {
Chris Lattner29446522007-05-14 22:04:50 +00003841 // Both can be negated for free, check to see if at least one is cheaper
3842 // negated.
3843 if (LHSNeg == 2 || RHSNeg == 2)
Chris Lattner0254e702008-02-26 07:04:54 +00003844 return DAG.getNode(ISD::FMUL, VT,
3845 GetNegatedExpression(N0, DAG, AfterLegalize),
3846 GetNegatedExpression(N1, DAG, AfterLegalize));
Chris Lattner29446522007-05-14 22:04:50 +00003847 }
3848 }
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003849
3850 // If allowed, fold (fmul (fmul x, c1), c2) -> (fmul x, (fmul c1, c2))
3851 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FMUL &&
Gabor Greifba36cb52008-08-28 21:40:38 +00003852 N0.getNode()->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003853 return DAG.getNode(ISD::FMUL, VT, N0.getOperand(0),
3854 DAG.getNode(ISD::FMUL, VT, N0.getOperand(1), N1));
3855
Dan Gohman475871a2008-07-27 21:46:04 +00003856 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00003857}
3858
Dan Gohman475871a2008-07-27 21:46:04 +00003859SDValue DAGCombiner::visitFDIV(SDNode *N) {
3860 SDValue N0 = N->getOperand(0);
3861 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003862 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3863 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003864 MVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00003865
Dan Gohman7f321562007-06-25 16:23:39 +00003866 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003867 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003868 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003869 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00003870 }
Dan Gohman7f321562007-06-25 16:23:39 +00003871
Nate Begemana148d982006-01-18 22:35:16 +00003872 // fold (fdiv c1, c2) -> c1/c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003873 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003874 return DAG.getNode(ISD::FDIV, VT, N0, N1);
Chris Lattner29446522007-05-14 22:04:50 +00003875
3876
3877 // -X / -Y -> X*Y
Chris Lattner0254e702008-02-26 07:04:54 +00003878 if (char LHSNeg = isNegatibleForFree(N0, AfterLegalize)) {
3879 if (char RHSNeg = isNegatibleForFree(N1, AfterLegalize)) {
Chris Lattner29446522007-05-14 22:04:50 +00003880 // Both can be negated for free, check to see if at least one is cheaper
3881 // negated.
3882 if (LHSNeg == 2 || RHSNeg == 2)
Chris Lattner0254e702008-02-26 07:04:54 +00003883 return DAG.getNode(ISD::FDIV, VT,
3884 GetNegatedExpression(N0, DAG, AfterLegalize),
3885 GetNegatedExpression(N1, DAG, AfterLegalize));
Chris Lattner29446522007-05-14 22:04:50 +00003886 }
3887 }
3888
Dan Gohman475871a2008-07-27 21:46:04 +00003889 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00003890}
3891
Dan Gohman475871a2008-07-27 21:46:04 +00003892SDValue DAGCombiner::visitFREM(SDNode *N) {
3893 SDValue N0 = N->getOperand(0);
3894 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003895 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3896 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003897 MVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00003898
Nate Begemana148d982006-01-18 22:35:16 +00003899 // fold (frem c1, c2) -> fmod(c1,c2)
Dale Johannesendb44bf82007-10-16 23:38:29 +00003900 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003901 return DAG.getNode(ISD::FREM, VT, N0, N1);
Dan Gohman7f321562007-06-25 16:23:39 +00003902
Dan Gohman475871a2008-07-27 21:46:04 +00003903 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00003904}
3905
Dan Gohman475871a2008-07-27 21:46:04 +00003906SDValue DAGCombiner::visitFCOPYSIGN(SDNode *N) {
3907 SDValue N0 = N->getOperand(0);
3908 SDValue N1 = N->getOperand(1);
Chris Lattner12d83032006-03-05 05:30:57 +00003909 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3910 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003911 MVT VT = N->getValueType(0);
Chris Lattner12d83032006-03-05 05:30:57 +00003912
Dale Johannesendb44bf82007-10-16 23:38:29 +00003913 if (N0CFP && N1CFP && VT != MVT::ppcf128) // Constant fold
Chris Lattner12d83032006-03-05 05:30:57 +00003914 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1);
3915
3916 if (N1CFP) {
Dale Johannesene6c17422007-08-26 01:18:27 +00003917 const APFloat& V = N1CFP->getValueAPF();
Chris Lattner12d83032006-03-05 05:30:57 +00003918 // copysign(x, c1) -> fabs(x) iff ispos(c1)
3919 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
Dale Johannesen87503a62007-08-25 22:10:57 +00003920 if (!V.isNegative())
Chris Lattner12d83032006-03-05 05:30:57 +00003921 return DAG.getNode(ISD::FABS, VT, N0);
3922 else
3923 return DAG.getNode(ISD::FNEG, VT, DAG.getNode(ISD::FABS, VT, N0));
3924 }
3925
3926 // copysign(fabs(x), y) -> copysign(x, y)
3927 // copysign(fneg(x), y) -> copysign(x, y)
3928 // copysign(copysign(x,z), y) -> copysign(x, y)
3929 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
3930 N0.getOpcode() == ISD::FCOPYSIGN)
3931 return DAG.getNode(ISD::FCOPYSIGN, VT, N0.getOperand(0), N1);
3932
3933 // copysign(x, abs(y)) -> abs(x)
3934 if (N1.getOpcode() == ISD::FABS)
3935 return DAG.getNode(ISD::FABS, VT, N0);
3936
3937 // copysign(x, copysign(y,z)) -> copysign(x, z)
3938 if (N1.getOpcode() == ISD::FCOPYSIGN)
3939 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(1));
3940
3941 // copysign(x, fp_extend(y)) -> copysign(x, y)
3942 // copysign(x, fp_round(y)) -> copysign(x, y)
3943 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
3944 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(0));
3945
Dan Gohman475871a2008-07-27 21:46:04 +00003946 return SDValue();
Chris Lattner12d83032006-03-05 05:30:57 +00003947}
3948
3949
Chris Lattner01b3d732005-09-28 22:28:18 +00003950
Dan Gohman475871a2008-07-27 21:46:04 +00003951SDValue DAGCombiner::visitSINT_TO_FP(SDNode *N) {
3952 SDValue N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003953 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003954 MVT VT = N->getValueType(0);
Chris Lattnercda88752008-06-26 00:16:49 +00003955 MVT OpVT = N0.getValueType();
3956
Nate Begeman1d4d4142005-09-01 00:19:25 +00003957 // fold (sint_to_fp c1) -> c1fp
Chris Lattnercda88752008-06-26 00:16:49 +00003958 if (N0C && OpVT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003959 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
Chris Lattnercda88752008-06-26 00:16:49 +00003960
3961 // If the input is a legal type, and SINT_TO_FP is not legal on this target,
3962 // but UINT_TO_FP is legal on this target, try to convert.
Chris Lattnerf77e46b2008-06-26 17:16:00 +00003963 if (!TLI.isOperationLegal(ISD::SINT_TO_FP, OpVT) &&
Chris Lattnercda88752008-06-26 00:16:49 +00003964 TLI.isOperationLegal(ISD::UINT_TO_FP, OpVT)) {
3965 // If the sign bit is known to be zero, we can change this to UINT_TO_FP.
3966 if (DAG.SignBitIsZero(N0))
3967 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
3968 }
3969
3970
Dan Gohman475871a2008-07-27 21:46:04 +00003971 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003972}
3973
Dan Gohman475871a2008-07-27 21:46:04 +00003974SDValue DAGCombiner::visitUINT_TO_FP(SDNode *N) {
3975 SDValue N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003976 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003977 MVT VT = N->getValueType(0);
Chris Lattnercda88752008-06-26 00:16:49 +00003978 MVT OpVT = N0.getValueType();
Nate Begemana148d982006-01-18 22:35:16 +00003979
Nate Begeman1d4d4142005-09-01 00:19:25 +00003980 // fold (uint_to_fp c1) -> c1fp
Chris Lattnercda88752008-06-26 00:16:49 +00003981 if (N0C && OpVT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003982 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
Chris Lattnercda88752008-06-26 00:16:49 +00003983
3984 // If the input is a legal type, and UINT_TO_FP is not legal on this target,
3985 // but SINT_TO_FP is legal on this target, try to convert.
Chris Lattnerf77e46b2008-06-26 17:16:00 +00003986 if (!TLI.isOperationLegal(ISD::UINT_TO_FP, OpVT) &&
Chris Lattnercda88752008-06-26 00:16:49 +00003987 TLI.isOperationLegal(ISD::SINT_TO_FP, OpVT)) {
3988 // If the sign bit is known to be zero, we can change this to SINT_TO_FP.
3989 if (DAG.SignBitIsZero(N0))
3990 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
3991 }
3992
Dan Gohman475871a2008-07-27 21:46:04 +00003993 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003994}
3995
Dan Gohman475871a2008-07-27 21:46:04 +00003996SDValue DAGCombiner::visitFP_TO_SINT(SDNode *N) {
3997 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00003998 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003999 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004000
4001 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00004002 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00004003 return DAG.getNode(ISD::FP_TO_SINT, VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00004004 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004005}
4006
Dan Gohman475871a2008-07-27 21:46:04 +00004007SDValue DAGCombiner::visitFP_TO_UINT(SDNode *N) {
4008 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00004009 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004010 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004011
4012 // fold (fp_to_uint c1fp) -> c1
Dale Johannesendb44bf82007-10-16 23:38:29 +00004013 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00004014 return DAG.getNode(ISD::FP_TO_UINT, VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00004015 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004016}
4017
Dan Gohman475871a2008-07-27 21:46:04 +00004018SDValue DAGCombiner::visitFP_ROUND(SDNode *N) {
4019 SDValue N0 = N->getOperand(0);
4020 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00004021 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004022 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004023
4024 // fold (fp_round c1fp) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00004025 if (N0CFP && N0.getValueType() != MVT::ppcf128)
Chris Lattner0bd48932008-01-17 07:00:52 +00004026 return DAG.getNode(ISD::FP_ROUND, VT, N0, N1);
Chris Lattner79dbea52006-03-13 06:26:26 +00004027
4028 // fold (fp_round (fp_extend x)) -> x
4029 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
4030 return N0.getOperand(0);
4031
Chris Lattner0aa5e6f2008-01-24 06:45:35 +00004032 // fold (fp_round (fp_round x)) -> (fp_round x)
4033 if (N0.getOpcode() == ISD::FP_ROUND) {
4034 // This is a value preserving truncation if both round's are.
4035 bool IsTrunc = N->getConstantOperandVal(1) == 1 &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004036 N0.getNode()->getConstantOperandVal(1) == 1;
Chris Lattner0aa5e6f2008-01-24 06:45:35 +00004037 return DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0),
4038 DAG.getIntPtrConstant(IsTrunc));
4039 }
4040
Chris Lattner79dbea52006-03-13 06:26:26 +00004041 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
Gabor Greifba36cb52008-08-28 21:40:38 +00004042 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004043 SDValue Tmp = DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0), N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00004044 AddToWorkList(Tmp.getNode());
Chris Lattner79dbea52006-03-13 06:26:26 +00004045 return DAG.getNode(ISD::FCOPYSIGN, VT, Tmp, N0.getOperand(1));
4046 }
4047
Dan Gohman475871a2008-07-27 21:46:04 +00004048 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004049}
4050
Dan Gohman475871a2008-07-27 21:46:04 +00004051SDValue DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
4052 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004053 MVT VT = N->getValueType(0);
4054 MVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00004055 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004056
Nate Begeman1d4d4142005-09-01 00:19:25 +00004057 // fold (fp_round_inreg c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00004058 if (N0CFP) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00004059 SDValue Round = DAG.getConstantFP(*N0CFP->getConstantFPValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00004060 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004061 }
Dan Gohman475871a2008-07-27 21:46:04 +00004062 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004063}
4064
Dan Gohman475871a2008-07-27 21:46:04 +00004065SDValue DAGCombiner::visitFP_EXTEND(SDNode *N) {
4066 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00004067 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004068 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004069
Chris Lattner5938bef2007-12-29 06:55:23 +00004070 // If this is fp_round(fpextend), don't fold it, allow ourselves to be folded.
Roman Levenstein9cac5252008-04-16 16:15:27 +00004071 if (N->hasOneUse() &&
Dan Gohman475871a2008-07-27 21:46:04 +00004072 N->use_begin().getUse().getSDValue().getOpcode() == ISD::FP_ROUND)
4073 return SDValue();
Chris Lattner0bd48932008-01-17 07:00:52 +00004074
Nate Begeman1d4d4142005-09-01 00:19:25 +00004075 // fold (fp_extend c1fp) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00004076 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00004077 return DAG.getNode(ISD::FP_EXTEND, VT, N0);
Chris Lattner0bd48932008-01-17 07:00:52 +00004078
4079 // Turn fp_extend(fp_round(X, 1)) -> x since the fp_round doesn't affect the
4080 // value of X.
Gabor Greif12632d22008-08-30 19:29:20 +00004081 if (N0.getOpcode() == ISD::FP_ROUND
4082 && N0.getNode()->getConstantOperandVal(1) == 1) {
Dan Gohman475871a2008-07-27 21:46:04 +00004083 SDValue In = N0.getOperand(0);
Chris Lattner0bd48932008-01-17 07:00:52 +00004084 if (In.getValueType() == VT) return In;
Duncan Sands8e4eb092008-06-08 20:54:56 +00004085 if (VT.bitsLT(In.getValueType()))
Chris Lattner0bd48932008-01-17 07:00:52 +00004086 return DAG.getNode(ISD::FP_ROUND, VT, In, N0.getOperand(1));
4087 return DAG.getNode(ISD::FP_EXTEND, VT, In);
4088 }
4089
4090 // fold (fpext (load x)) -> (fpext (fptrunc (extload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00004091 if (ISD::isNON_EXTLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004092 ((!AfterLegalize && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00004093 TLI.isLoadExtLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00004094 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman475871a2008-07-27 21:46:04 +00004095 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
Evan Cheng466685d2006-10-09 20:57:25 +00004096 LN0->getBasePtr(), LN0->getSrcValue(),
4097 LN0->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00004098 N0.getValueType(),
4099 LN0->isVolatile(),
4100 LN0->getAlignment());
Chris Lattnere564dbb2006-05-05 21:34:35 +00004101 CombineTo(N, ExtLoad);
Gabor Greif12632d22008-08-30 19:29:20 +00004102 CombineTo(N0.getNode(), DAG.getNode(ISD::FP_ROUND, N0.getValueType(),
4103 ExtLoad, DAG.getIntPtrConstant(1)),
Chris Lattnere564dbb2006-05-05 21:34:35 +00004104 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004105 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnere564dbb2006-05-05 21:34:35 +00004106 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004107
Dan Gohman475871a2008-07-27 21:46:04 +00004108 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004109}
4110
Dan Gohman475871a2008-07-27 21:46:04 +00004111SDValue DAGCombiner::visitFNEG(SDNode *N) {
4112 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00004113
Chris Lattner0254e702008-02-26 07:04:54 +00004114 if (isNegatibleForFree(N0, AfterLegalize))
4115 return GetNegatedExpression(N0, DAG, AfterLegalize);
Dan Gohman23ff1822007-07-02 15:48:56 +00004116
Chris Lattner3bd39d42008-01-27 17:42:27 +00004117 // Transform fneg(bitconvert(x)) -> bitconvert(x^sign) to avoid loading
4118 // constant pool values.
Gabor Greifba36cb52008-08-28 21:40:38 +00004119 if (N0.getOpcode() == ISD::BIT_CONVERT && N0.getNode()->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00004120 N0.getOperand(0).getValueType().isInteger() &&
4121 !N0.getOperand(0).getValueType().isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004122 SDValue Int = N0.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004123 MVT IntVT = Int.getValueType();
4124 if (IntVT.isInteger() && !IntVT.isVector()) {
Chris Lattner3bd39d42008-01-27 17:42:27 +00004125 Int = DAG.getNode(ISD::XOR, IntVT, Int,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004126 DAG.getConstant(IntVT.getIntegerVTSignBit(), IntVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00004127 AddToWorkList(Int.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00004128 return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Int);
4129 }
4130 }
4131
Dan Gohman475871a2008-07-27 21:46:04 +00004132 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004133}
4134
Dan Gohman475871a2008-07-27 21:46:04 +00004135SDValue DAGCombiner::visitFABS(SDNode *N) {
4136 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00004137 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004138 MVT VT = N->getValueType(0);
Nate Begemana148d982006-01-18 22:35:16 +00004139
Nate Begeman1d4d4142005-09-01 00:19:25 +00004140 // fold (fabs c1) -> fabs(c1)
Dale Johannesendb44bf82007-10-16 23:38:29 +00004141 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00004142 return DAG.getNode(ISD::FABS, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004143 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00004144 if (N0.getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00004145 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004146 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00004147 // fold (fabs (fcopysign x, y)) -> (fabs x)
4148 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
4149 return DAG.getNode(ISD::FABS, VT, N0.getOperand(0));
4150
Chris Lattner3bd39d42008-01-27 17:42:27 +00004151 // Transform fabs(bitconvert(x)) -> bitconvert(x&~sign) to avoid loading
4152 // constant pool values.
Gabor Greifba36cb52008-08-28 21:40:38 +00004153 if (N0.getOpcode() == ISD::BIT_CONVERT && N0.getNode()->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00004154 N0.getOperand(0).getValueType().isInteger() &&
4155 !N0.getOperand(0).getValueType().isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004156 SDValue Int = N0.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004157 MVT IntVT = Int.getValueType();
4158 if (IntVT.isInteger() && !IntVT.isVector()) {
Chris Lattner3bd39d42008-01-27 17:42:27 +00004159 Int = DAG.getNode(ISD::AND, IntVT, Int,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004160 DAG.getConstant(~IntVT.getIntegerVTSignBit(), IntVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00004161 AddToWorkList(Int.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00004162 return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Int);
4163 }
4164 }
4165
Dan Gohman475871a2008-07-27 21:46:04 +00004166 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004167}
4168
Dan Gohman475871a2008-07-27 21:46:04 +00004169SDValue DAGCombiner::visitBRCOND(SDNode *N) {
4170 SDValue Chain = N->getOperand(0);
4171 SDValue N1 = N->getOperand(1);
4172 SDValue N2 = N->getOperand(2);
Nate Begeman44728a72005-09-19 22:34:01 +00004173 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
4174
4175 // never taken branch, fold to chain
4176 if (N1C && N1C->isNullValue())
4177 return Chain;
4178 // unconditional branch
Dan Gohman002e5d02008-03-13 22:13:53 +00004179 if (N1C && N1C->getAPIntValue() == 1)
Nate Begeman44728a72005-09-19 22:34:01 +00004180 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
Nate Begeman750ac1b2006-02-01 07:19:44 +00004181 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
4182 // on the target.
4183 if (N1.getOpcode() == ISD::SETCC &&
4184 TLI.isOperationLegal(ISD::BR_CC, MVT::Other)) {
4185 return DAG.getNode(ISD::BR_CC, MVT::Other, Chain, N1.getOperand(2),
4186 N1.getOperand(0), N1.getOperand(1), N2);
4187 }
Dan Gohman475871a2008-07-27 21:46:04 +00004188 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00004189}
4190
Chris Lattner3ea0b472005-10-05 06:47:48 +00004191// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
4192//
Dan Gohman475871a2008-07-27 21:46:04 +00004193SDValue DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00004194 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004195 SDValue CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
Chris Lattner3ea0b472005-10-05 06:47:48 +00004196
Duncan Sands8eab8a22008-06-09 11:32:28 +00004197 // Use SimplifySetCC to simplify SETCC's.
Dan Gohman475871a2008-07-27 21:46:04 +00004198 SDValue Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false);
Gabor Greifba36cb52008-08-28 21:40:38 +00004199 if (Simp.getNode()) AddToWorkList(Simp.getNode());
Chris Lattner30f73e72006-10-14 03:52:46 +00004200
Gabor Greifba36cb52008-08-28 21:40:38 +00004201 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.getNode());
Nate Begemane17daeb2005-10-05 21:43:42 +00004202
4203 // fold br_cc true, dest -> br dest (unconditional branch)
Dan Gohman002e5d02008-03-13 22:13:53 +00004204 if (SCCC && !SCCC->isNullValue())
Nate Begemane17daeb2005-10-05 21:43:42 +00004205 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
4206 N->getOperand(4));
4207 // fold br_cc false, dest -> unconditional fall through
4208 if (SCCC && SCCC->isNullValue())
4209 return N->getOperand(0);
Chris Lattner30f73e72006-10-14 03:52:46 +00004210
Nate Begemane17daeb2005-10-05 21:43:42 +00004211 // fold to a simpler setcc
Gabor Greifba36cb52008-08-28 21:40:38 +00004212 if (Simp.getNode() && Simp.getOpcode() == ISD::SETCC)
Nate Begemane17daeb2005-10-05 21:43:42 +00004213 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
4214 Simp.getOperand(2), Simp.getOperand(0),
4215 Simp.getOperand(1), N->getOperand(4));
Dan Gohman475871a2008-07-27 21:46:04 +00004216 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00004217}
4218
Chris Lattner448f2192006-11-11 00:39:41 +00004219
Duncan Sandsec87aa82008-06-15 20:12:31 +00004220/// CombineToPreIndexedLoadStore - Try turning a load / store into a
4221/// pre-indexed load / store when the base pointer is an add or subtract
Chris Lattner448f2192006-11-11 00:39:41 +00004222/// and it has other uses besides the load / store. After the
4223/// transformation, the new indexed load / store has effectively folded
4224/// the add / subtract in and all of its other uses are redirected to the
4225/// new load / store.
4226bool DAGCombiner::CombineToPreIndexedLoadStore(SDNode *N) {
4227 if (!AfterLegalize)
4228 return false;
4229
4230 bool isLoad = true;
Dan Gohman475871a2008-07-27 21:46:04 +00004231 SDValue Ptr;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004232 MVT VT;
Chris Lattner448f2192006-11-11 00:39:41 +00004233 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004234 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004235 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004236 VT = LD->getMemoryVT();
Evan Cheng83060c52007-03-07 08:07:03 +00004237 if (!TLI.isIndexedLoadLegal(ISD::PRE_INC, VT) &&
Chris Lattner448f2192006-11-11 00:39:41 +00004238 !TLI.isIndexedLoadLegal(ISD::PRE_DEC, VT))
4239 return false;
4240 Ptr = LD->getBasePtr();
4241 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004242 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004243 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004244 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00004245 if (!TLI.isIndexedStoreLegal(ISD::PRE_INC, VT) &&
4246 !TLI.isIndexedStoreLegal(ISD::PRE_DEC, VT))
4247 return false;
4248 Ptr = ST->getBasePtr();
4249 isLoad = false;
4250 } else
4251 return false;
4252
Chris Lattner9f1794e2006-11-11 00:56:29 +00004253 // If the pointer is not an add/sub, or if it doesn't have multiple uses, bail
4254 // out. There is no reason to make this a preinc/predec.
4255 if ((Ptr.getOpcode() != ISD::ADD && Ptr.getOpcode() != ISD::SUB) ||
Gabor Greifba36cb52008-08-28 21:40:38 +00004256 Ptr.getNode()->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00004257 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00004258
Chris Lattner9f1794e2006-11-11 00:56:29 +00004259 // Ask the target to do addressing mode selection.
Dan Gohman475871a2008-07-27 21:46:04 +00004260 SDValue BasePtr;
4261 SDValue Offset;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004262 ISD::MemIndexedMode AM = ISD::UNINDEXED;
4263 if (!TLI.getPreIndexedAddressParts(N, BasePtr, Offset, AM, DAG))
4264 return false;
Evan Chenga7d4a042007-05-03 23:52:19 +00004265 // Don't create a indexed load / store with zero offset.
4266 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00004267 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Chenga7d4a042007-05-03 23:52:19 +00004268 return false;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004269
Chris Lattner41e53fd2006-11-11 01:00:15 +00004270 // Try turning it into a pre-indexed load / store except when:
Evan Chengc843abe2007-05-24 02:35:39 +00004271 // 1) The new base ptr is a frame index.
4272 // 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 +00004273 // predecessor of the value being stored.
Evan Chengc843abe2007-05-24 02:35:39 +00004274 // 3) Another use of old base ptr is a predecessor of N. If ptr is folded
Chris Lattner9f1794e2006-11-11 00:56:29 +00004275 // that would create a cycle.
Evan Chengc843abe2007-05-24 02:35:39 +00004276 // 4) All uses are load / store ops that use it as old base ptr.
Chris Lattner448f2192006-11-11 00:39:41 +00004277
Chris Lattner41e53fd2006-11-11 01:00:15 +00004278 // Check #1. Preinc'ing a frame index would require copying the stack pointer
4279 // (plus the implicit offset) to a register to preinc anyway.
4280 if (isa<FrameIndexSDNode>(BasePtr))
4281 return false;
4282
4283 // Check #2.
Chris Lattner9f1794e2006-11-11 00:56:29 +00004284 if (!isLoad) {
Dan Gohman475871a2008-07-27 21:46:04 +00004285 SDValue Val = cast<StoreSDNode>(N)->getValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00004286 if (Val == BasePtr || BasePtr.getNode()->isPredecessorOf(Val.getNode()))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004287 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00004288 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004289
Evan Chengc843abe2007-05-24 02:35:39 +00004290 // Now check for #3 and #4.
Chris Lattner9f1794e2006-11-11 00:56:29 +00004291 bool RealUse = false;
Gabor Greifba36cb52008-08-28 21:40:38 +00004292 for (SDNode::use_iterator I = Ptr.getNode()->use_begin(),
4293 E = Ptr.getNode()->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00004294 SDNode *Use = *I;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004295 if (Use == N)
4296 continue;
Evan Cheng917be682008-03-04 00:41:45 +00004297 if (Use->isPredecessorOf(N))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004298 return false;
4299
4300 if (!((Use->getOpcode() == ISD::LOAD &&
4301 cast<LoadSDNode>(Use)->getBasePtr() == Ptr) ||
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00004302 (Use->getOpcode() == ISD::STORE &&
4303 cast<StoreSDNode>(Use)->getBasePtr() == Ptr)))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004304 RealUse = true;
4305 }
4306 if (!RealUse)
4307 return false;
4308
Dan Gohman475871a2008-07-27 21:46:04 +00004309 SDValue Result;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004310 if (isLoad)
Dan Gohman475871a2008-07-27 21:46:04 +00004311 Result = DAG.getIndexedLoad(SDValue(N,0), BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004312 else
Dan Gohman475871a2008-07-27 21:46:04 +00004313 Result = DAG.getIndexedStore(SDValue(N,0), BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004314 ++PreIndexedNodes;
4315 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +00004316 DOUT << "\nReplacing.4 "; DEBUG(N->dump(&DAG));
Gabor Greifba36cb52008-08-28 21:40:38 +00004317 DOUT << "\nWith: "; DEBUG(Result.getNode()->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +00004318 DOUT << '\n';
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004319 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004320 if (isLoad) {
Dan Gohman475871a2008-07-27 21:46:04 +00004321 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004322 &DeadNodes);
Dan Gohman475871a2008-07-27 21:46:04 +00004323 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004324 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004325 } else {
Dan Gohman475871a2008-07-27 21:46:04 +00004326 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004327 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004328 }
4329
Chris Lattner9f1794e2006-11-11 00:56:29 +00004330 // Finally, since the node is now dead, remove it from the graph.
4331 DAG.DeleteNode(N);
4332
4333 // Replace the uses of Ptr with uses of the updated base value.
4334 DAG.ReplaceAllUsesOfValueWith(Ptr, Result.getValue(isLoad ? 1 : 0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004335 &DeadNodes);
Gabor Greifba36cb52008-08-28 21:40:38 +00004336 removeFromWorkList(Ptr.getNode());
4337 DAG.DeleteNode(Ptr.getNode());
Chris Lattner9f1794e2006-11-11 00:56:29 +00004338
4339 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00004340}
4341
Duncan Sandsec87aa82008-06-15 20:12:31 +00004342/// CombineToPostIndexedLoadStore - Try to combine a load / store with a
Chris Lattner448f2192006-11-11 00:39:41 +00004343/// add / sub of the base pointer node into a post-indexed load / store.
4344/// The transformation folded the add / subtract into the new indexed
4345/// load / store effectively and all of its uses are redirected to the
4346/// new load / store.
4347bool DAGCombiner::CombineToPostIndexedLoadStore(SDNode *N) {
4348 if (!AfterLegalize)
4349 return false;
4350
4351 bool isLoad = true;
Dan Gohman475871a2008-07-27 21:46:04 +00004352 SDValue Ptr;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004353 MVT VT;
Chris Lattner448f2192006-11-11 00:39:41 +00004354 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004355 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004356 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004357 VT = LD->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00004358 if (!TLI.isIndexedLoadLegal(ISD::POST_INC, VT) &&
4359 !TLI.isIndexedLoadLegal(ISD::POST_DEC, VT))
4360 return false;
4361 Ptr = LD->getBasePtr();
4362 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004363 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004364 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004365 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00004366 if (!TLI.isIndexedStoreLegal(ISD::POST_INC, VT) &&
4367 !TLI.isIndexedStoreLegal(ISD::POST_DEC, VT))
4368 return false;
4369 Ptr = ST->getBasePtr();
4370 isLoad = false;
4371 } else
4372 return false;
4373
Gabor Greifba36cb52008-08-28 21:40:38 +00004374 if (Ptr.getNode()->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00004375 return false;
4376
Gabor Greifba36cb52008-08-28 21:40:38 +00004377 for (SDNode::use_iterator I = Ptr.getNode()->use_begin(),
4378 E = Ptr.getNode()->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00004379 SDNode *Op = *I;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004380 if (Op == N ||
4381 (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB))
4382 continue;
4383
Dan Gohman475871a2008-07-27 21:46:04 +00004384 SDValue BasePtr;
4385 SDValue Offset;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004386 ISD::MemIndexedMode AM = ISD::UNINDEXED;
4387 if (TLI.getPostIndexedAddressParts(N, Op, BasePtr, Offset, AM, DAG)) {
4388 if (Ptr == Offset)
4389 std::swap(BasePtr, Offset);
4390 if (Ptr != BasePtr)
Chris Lattner448f2192006-11-11 00:39:41 +00004391 continue;
Evan Chenga7d4a042007-05-03 23:52:19 +00004392 // Don't create a indexed load / store with zero offset.
4393 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00004394 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Chenga7d4a042007-05-03 23:52:19 +00004395 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00004396
Chris Lattner9f1794e2006-11-11 00:56:29 +00004397 // Try turning it into a post-indexed load / store except when
4398 // 1) All uses are load / store ops that use it as base ptr.
4399 // 2) Op must be independent of N, i.e. Op is neither a predecessor
4400 // nor a successor of N. Otherwise, if Op is folded that would
4401 // create a cycle.
4402
4403 // Check for #1.
4404 bool TryNext = false;
Gabor Greifba36cb52008-08-28 21:40:38 +00004405 for (SDNode::use_iterator II = BasePtr.getNode()->use_begin(),
4406 EE = BasePtr.getNode()->use_end(); II != EE; ++II) {
Dan Gohman89684502008-07-27 20:43:25 +00004407 SDNode *Use = *II;
Gabor Greifba36cb52008-08-28 21:40:38 +00004408 if (Use == Ptr.getNode())
Chris Lattner448f2192006-11-11 00:39:41 +00004409 continue;
4410
Chris Lattner9f1794e2006-11-11 00:56:29 +00004411 // If all the uses are load / store addresses, then don't do the
4412 // transformation.
4413 if (Use->getOpcode() == ISD::ADD || Use->getOpcode() == ISD::SUB){
4414 bool RealUse = false;
4415 for (SDNode::use_iterator III = Use->use_begin(),
4416 EEE = Use->use_end(); III != EEE; ++III) {
Dan Gohman89684502008-07-27 20:43:25 +00004417 SDNode *UseUse = *III;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004418 if (!((UseUse->getOpcode() == ISD::LOAD &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004419 cast<LoadSDNode>(UseUse)->getBasePtr().getNode() == Use) ||
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00004420 (UseUse->getOpcode() == ISD::STORE &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004421 cast<StoreSDNode>(UseUse)->getBasePtr().getNode() == Use)))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004422 RealUse = true;
4423 }
Chris Lattner448f2192006-11-11 00:39:41 +00004424
Chris Lattner9f1794e2006-11-11 00:56:29 +00004425 if (!RealUse) {
4426 TryNext = true;
4427 break;
Chris Lattner448f2192006-11-11 00:39:41 +00004428 }
4429 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004430 }
4431 if (TryNext)
4432 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00004433
Chris Lattner9f1794e2006-11-11 00:56:29 +00004434 // Check for #2
Evan Cheng917be682008-03-04 00:41:45 +00004435 if (!Op->isPredecessorOf(N) && !N->isPredecessorOf(Op)) {
Dan Gohman475871a2008-07-27 21:46:04 +00004436 SDValue Result = isLoad
4437 ? DAG.getIndexedLoad(SDValue(N,0), BasePtr, Offset, AM)
4438 : DAG.getIndexedStore(SDValue(N,0), BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004439 ++PostIndexedNodes;
4440 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +00004441 DOUT << "\nReplacing.5 "; DEBUG(N->dump(&DAG));
Gabor Greifba36cb52008-08-28 21:40:38 +00004442 DOUT << "\nWith: "; DEBUG(Result.getNode()->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +00004443 DOUT << '\n';
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004444 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004445 if (isLoad) {
Dan Gohman475871a2008-07-27 21:46:04 +00004446 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004447 &DeadNodes);
Dan Gohman475871a2008-07-27 21:46:04 +00004448 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004449 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004450 } else {
Dan Gohman475871a2008-07-27 21:46:04 +00004451 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004452 &DeadNodes);
Chris Lattner448f2192006-11-11 00:39:41 +00004453 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004454
Chris Lattner9f1794e2006-11-11 00:56:29 +00004455 // Finally, since the node is now dead, remove it from the graph.
4456 DAG.DeleteNode(N);
4457
4458 // Replace the uses of Use with uses of the updated base value.
Dan Gohman475871a2008-07-27 21:46:04 +00004459 DAG.ReplaceAllUsesOfValueWith(SDValue(Op, 0),
Chris Lattner9f1794e2006-11-11 00:56:29 +00004460 Result.getValue(isLoad ? 1 : 0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004461 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004462 removeFromWorkList(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004463 DAG.DeleteNode(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004464 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00004465 }
4466 }
4467 }
4468 return false;
4469}
4470
Chris Lattner00161a62008-01-25 07:20:16 +00004471/// InferAlignment - If we can infer some alignment information from this
4472/// pointer, return it.
Dan Gohman475871a2008-07-27 21:46:04 +00004473static unsigned InferAlignment(SDValue Ptr, SelectionDAG &DAG) {
Chris Lattner00161a62008-01-25 07:20:16 +00004474 // If this is a direct reference to a stack slot, use information about the
4475 // stack slot's alignment.
Chris Lattner1329cb82008-01-26 19:45:50 +00004476 int FrameIdx = 1 << 31;
4477 int64_t FrameOffset = 0;
Chris Lattner00161a62008-01-25 07:20:16 +00004478 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
Chris Lattner1329cb82008-01-26 19:45:50 +00004479 FrameIdx = FI->getIndex();
4480 } else if (Ptr.getOpcode() == ISD::ADD &&
4481 isa<ConstantSDNode>(Ptr.getOperand(1)) &&
4482 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4483 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4484 FrameOffset = Ptr.getConstantOperandVal(1);
Chris Lattner00161a62008-01-25 07:20:16 +00004485 }
Chris Lattner1329cb82008-01-26 19:45:50 +00004486
4487 if (FrameIdx != (1 << 31)) {
4488 // FIXME: Handle FI+CST.
4489 const MachineFrameInfo &MFI = *DAG.getMachineFunction().getFrameInfo();
4490 if (MFI.isFixedObjectIndex(FrameIdx)) {
Dan Gohman8cea8ff2008-08-11 18:27:03 +00004491 int64_t ObjectOffset = MFI.getObjectOffset(FrameIdx) + FrameOffset;
Chris Lattner1329cb82008-01-26 19:45:50 +00004492
4493 // The alignment of the frame index can be determined from its offset from
4494 // the incoming frame position. If the frame object is at offset 32 and
4495 // the stack is guaranteed to be 16-byte aligned, then we know that the
4496 // object is 16-byte aligned.
4497 unsigned StackAlign = DAG.getTarget().getFrameInfo()->getStackAlignment();
4498 unsigned Align = MinAlign(ObjectOffset, StackAlign);
4499
4500 // Finally, the frame object itself may have a known alignment. Factor
4501 // the alignment + offset into a new alignment. For example, if we know
4502 // the FI is 8 byte aligned, but the pointer is 4 off, we really have a
4503 // 4-byte alignment of the resultant pointer. Likewise align 4 + 4-byte
4504 // offset = 4-byte alignment, align 4 + 1-byte offset = align 1, etc.
4505 unsigned FIInfoAlign = MinAlign(MFI.getObjectAlignment(FrameIdx),
4506 FrameOffset);
4507 return std::max(Align, FIInfoAlign);
4508 }
4509 }
Chris Lattner00161a62008-01-25 07:20:16 +00004510
4511 return 0;
4512}
Chris Lattner448f2192006-11-11 00:39:41 +00004513
Dan Gohman475871a2008-07-27 21:46:04 +00004514SDValue DAGCombiner::visitLOAD(SDNode *N) {
Evan Cheng466685d2006-10-09 20:57:25 +00004515 LoadSDNode *LD = cast<LoadSDNode>(N);
Dan Gohman475871a2008-07-27 21:46:04 +00004516 SDValue Chain = LD->getChain();
4517 SDValue Ptr = LD->getBasePtr();
Chris Lattner00161a62008-01-25 07:20:16 +00004518
4519 // Try to infer better alignment information than the load already has.
Dan Gohmana2676512008-08-20 16:30:28 +00004520 if (!Fast && LD->isUnindexed()) {
Chris Lattner00161a62008-01-25 07:20:16 +00004521 if (unsigned Align = InferAlignment(Ptr, DAG)) {
4522 if (Align > LD->getAlignment())
4523 return DAG.getExtLoad(LD->getExtensionType(), LD->getValueType(0),
4524 Chain, Ptr, LD->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004525 LD->getSrcValueOffset(), LD->getMemoryVT(),
Chris Lattner00161a62008-01-25 07:20:16 +00004526 LD->isVolatile(), Align);
4527 }
4528 }
4529
Evan Cheng45a7ca92007-05-01 00:38:21 +00004530
4531 // If load is not volatile and there are no uses of the loaded value (and
4532 // the updated indexed value in case of indexed loads), change uses of the
4533 // chain value into uses of the chain input (i.e. delete the dead load).
4534 if (!LD->isVolatile()) {
Evan Cheng498f5592007-05-01 08:53:39 +00004535 if (N->getValueType(1) == MVT::Other) {
4536 // Unindexed loads.
Evan Cheng02c42852008-01-16 23:11:54 +00004537 if (N->hasNUsesOfValue(0, 0)) {
4538 // It's not safe to use the two value CombineTo variant here. e.g.
4539 // v1, chain2 = load chain1, loc
4540 // v2, chain3 = load chain2, loc
4541 // v3 = add v2, c
Chris Lattner125991a2008-01-24 07:57:06 +00004542 // Now we replace use of chain2 with chain1. This makes the second load
4543 // isomorphic to the one we are deleting, and thus makes this load live.
Evan Cheng02c42852008-01-16 23:11:54 +00004544 DOUT << "\nReplacing.6 "; DEBUG(N->dump(&DAG));
Gabor Greifba36cb52008-08-28 21:40:38 +00004545 DOUT << "\nWith chain: "; DEBUG(Chain.getNode()->dump(&DAG));
Chris Lattner125991a2008-01-24 07:57:06 +00004546 DOUT << "\n";
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004547 WorkListRemover DeadNodes(*this);
Dan Gohman475871a2008-07-27 21:46:04 +00004548 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Chain, &DeadNodes);
Chris Lattner125991a2008-01-24 07:57:06 +00004549 if (N->use_empty()) {
4550 removeFromWorkList(N);
4551 DAG.DeleteNode(N);
4552 }
Dan Gohman475871a2008-07-27 21:46:04 +00004553 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng02c42852008-01-16 23:11:54 +00004554 }
Evan Cheng498f5592007-05-01 08:53:39 +00004555 } else {
4556 // Indexed loads.
4557 assert(N->getValueType(2) == MVT::Other && "Malformed indexed loads?");
4558 if (N->hasNUsesOfValue(0, 0) && N->hasNUsesOfValue(0, 1)) {
Dan Gohman475871a2008-07-27 21:46:04 +00004559 SDValue Undef = DAG.getNode(ISD::UNDEF, N->getValueType(0));
Evan Cheng02c42852008-01-16 23:11:54 +00004560 DOUT << "\nReplacing.6 "; DEBUG(N->dump(&DAG));
Gabor Greifba36cb52008-08-28 21:40:38 +00004561 DOUT << "\nWith: "; DEBUG(Undef.getNode()->dump(&DAG));
Evan Cheng02c42852008-01-16 23:11:54 +00004562 DOUT << " and 2 other values\n";
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004563 WorkListRemover DeadNodes(*this);
Dan Gohman475871a2008-07-27 21:46:04 +00004564 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Undef, &DeadNodes);
4565 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1),
Chris Lattner4626b252008-01-17 07:20:38 +00004566 DAG.getNode(ISD::UNDEF, N->getValueType(1)),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004567 &DeadNodes);
Dan Gohman475871a2008-07-27 21:46:04 +00004568 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 2), Chain, &DeadNodes);
Evan Cheng02c42852008-01-16 23:11:54 +00004569 removeFromWorkList(N);
Evan Cheng02c42852008-01-16 23:11:54 +00004570 DAG.DeleteNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +00004571 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng45a7ca92007-05-01 00:38:21 +00004572 }
Evan Cheng45a7ca92007-05-01 00:38:21 +00004573 }
4574 }
Chris Lattner01a22022005-10-10 22:04:48 +00004575
4576 // If this load is directly stored, replace the load value with the stored
4577 // value.
4578 // TODO: Handle store large -> read small portion.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004579 // TODO: Handle TRUNCSTORE/LOADEXT
Dan Gohmanb061c4b2008-03-31 20:32:52 +00004580 if (LD->getExtensionType() == ISD::NON_EXTLOAD &&
4581 !LD->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004582 if (ISD::isNON_TRUNCStore(Chain.getNode())) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00004583 StoreSDNode *PrevST = cast<StoreSDNode>(Chain);
4584 if (PrevST->getBasePtr() == Ptr &&
4585 PrevST->getValue().getValueType() == N->getValueType(0))
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004586 return CombineTo(N, Chain.getOperand(1), Chain);
Evan Cheng8b2794a2006-10-13 21:14:26 +00004587 }
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004588 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00004589
Jim Laskey7ca56af2006-10-11 13:47:09 +00004590 if (CombinerAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00004591 // Walk up chain skipping non-aliasing memory nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00004592 SDValue BetterChain = FindBetterChain(N, Chain);
Jim Laskey279f0532006-09-25 16:29:54 +00004593
Jim Laskey6ff23e52006-10-04 16:53:27 +00004594 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00004595 if (Chain != BetterChain) {
Dan Gohman475871a2008-07-27 21:46:04 +00004596 SDValue ReplLoad;
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004597
Jim Laskey279f0532006-09-25 16:29:54 +00004598 // Replace the chain to void dependency.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004599 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
4600 ReplLoad = DAG.getLoad(N->getValueType(0), BetterChain, Ptr,
Duncan Sandsdc846502007-10-28 12:59:45 +00004601 LD->getSrcValue(), LD->getSrcValueOffset(),
4602 LD->isVolatile(), LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004603 } else {
4604 ReplLoad = DAG.getExtLoad(LD->getExtensionType(),
4605 LD->getValueType(0),
4606 BetterChain, Ptr, LD->getSrcValue(),
4607 LD->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004608 LD->getMemoryVT(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00004609 LD->isVolatile(),
4610 LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004611 }
Jim Laskey279f0532006-09-25 16:29:54 +00004612
Jim Laskey6ff23e52006-10-04 16:53:27 +00004613 // Create token factor to keep old chain connected.
Dan Gohman475871a2008-07-27 21:46:04 +00004614 SDValue Token = DAG.getNode(ISD::TokenFactor, MVT::Other,
Jim Laskey288af5e2006-09-25 19:32:58 +00004615 Chain, ReplLoad.getValue(1));
Jim Laskey6ff23e52006-10-04 16:53:27 +00004616
Jim Laskey274062c2006-10-13 23:32:28 +00004617 // Replace uses with load result and token factor. Don't add users
4618 // to work list.
4619 return CombineTo(N, ReplLoad.getValue(0), Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00004620 }
4621 }
4622
Evan Cheng7fc033a2006-11-03 03:06:21 +00004623 // Try transforming N to an indexed load.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00004624 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Dan Gohman475871a2008-07-27 21:46:04 +00004625 return SDValue(N, 0);
Evan Cheng7fc033a2006-11-03 03:06:21 +00004626
Dan Gohman475871a2008-07-27 21:46:04 +00004627 return SDValue();
Chris Lattner01a22022005-10-10 22:04:48 +00004628}
4629
Chris Lattner07649d92008-01-08 23:08:06 +00004630
Dan Gohman475871a2008-07-27 21:46:04 +00004631SDValue DAGCombiner::visitSTORE(SDNode *N) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00004632 StoreSDNode *ST = cast<StoreSDNode>(N);
Dan Gohman475871a2008-07-27 21:46:04 +00004633 SDValue Chain = ST->getChain();
4634 SDValue Value = ST->getValue();
4635 SDValue Ptr = ST->getBasePtr();
Jim Laskey7aed46c2006-10-11 18:55:16 +00004636
Chris Lattner00161a62008-01-25 07:20:16 +00004637 // Try to infer better alignment information than the store already has.
Dan Gohmana2676512008-08-20 16:30:28 +00004638 if (!Fast && ST->isUnindexed()) {
Chris Lattner00161a62008-01-25 07:20:16 +00004639 if (unsigned Align = InferAlignment(Ptr, DAG)) {
4640 if (Align > ST->getAlignment())
4641 return DAG.getTruncStore(Chain, Value, Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004642 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattner00161a62008-01-25 07:20:16 +00004643 ST->isVolatile(), Align);
4644 }
4645 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004646
Evan Cheng59d5b682007-05-07 21:27:48 +00004647 // If this is a store of a bit convert, store the input value if the
Evan Cheng2c4f9432007-05-09 21:49:47 +00004648 // resultant store does not need a higher alignment than the original.
Dale Johannesen98a6c622007-05-16 22:45:30 +00004649 if (Value.getOpcode() == ISD::BIT_CONVERT && !ST->isTruncatingStore() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00004650 ST->isUnindexed()) {
Evan Cheng59d5b682007-05-07 21:27:48 +00004651 unsigned Align = ST->getAlignment();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004652 MVT SVT = Value.getOperand(0).getValueType();
Dan Gohman6448d912008-09-04 15:39:15 +00004653 unsigned OrigAlign = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00004654 getABITypeAlignment(SVT.getTypeForMVT());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004655 if (Align <= OrigAlign &&
4656 ((!AfterLegalize && !ST->isVolatile()) ||
4657 TLI.isOperationLegal(ISD::STORE, SVT)))
Evan Cheng59d5b682007-05-07 21:27:48 +00004658 return DAG.getStore(Chain, Value.getOperand(0), Ptr, ST->getSrcValue(),
Dan Gohman77455612008-06-28 00:45:22 +00004659 ST->getSrcValueOffset(), ST->isVolatile(), OrigAlign);
Jim Laskey279f0532006-09-25 16:29:54 +00004660 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004661
Nate Begeman2cbba892006-12-11 02:23:46 +00004662 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Nate Begeman2cbba892006-12-11 02:23:46 +00004663 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Value)) {
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004664 // NOTE: If the original store is volatile, this transform must not increase
4665 // the number of stores. For example, on x86-32 an f64 can be stored in one
4666 // processor operation but an i64 (which is not legal) requires two. So the
4667 // transform should not be done in this case.
Evan Cheng25ece662006-12-11 17:25:19 +00004668 if (Value.getOpcode() != ISD::TargetConstantFP) {
Dan Gohman475871a2008-07-27 21:46:04 +00004669 SDValue Tmp;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004670 switch (CFP->getValueType(0).getSimpleVT()) {
Chris Lattner62be1a72006-12-12 04:16:14 +00004671 default: assert(0 && "Unknown FP type");
Dale Johannesenc7b21d52007-09-18 18:36:59 +00004672 case MVT::f80: // We don't do this for these yet.
4673 case MVT::f128:
4674 case MVT::ppcf128:
4675 break;
Chris Lattner62be1a72006-12-12 04:16:14 +00004676 case MVT::f32:
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004677 if ((!AfterLegalize && !ST->isVolatile()) ||
4678 TLI.isOperationLegal(ISD::STORE, MVT::i32)) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00004679 Tmp = DAG.getConstant((uint32_t)CFP->getValueAPF().
Dale Johannesen7111b022008-10-09 18:53:47 +00004680 bitcastToAPInt().getZExtValue(), MVT::i32);
Chris Lattner62be1a72006-12-12 04:16:14 +00004681 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004682 ST->getSrcValueOffset(), ST->isVolatile(),
4683 ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00004684 }
4685 break;
4686 case MVT::f64:
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004687 if ((!AfterLegalize && !ST->isVolatile()) ||
4688 TLI.isOperationLegal(ISD::STORE, MVT::i64)) {
Dale Johannesen7111b022008-10-09 18:53:47 +00004689 Tmp = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
Dale Johannesen9d5f4562007-09-12 03:30:33 +00004690 getZExtValue(), MVT::i64);
Chris Lattner62be1a72006-12-12 04:16:14 +00004691 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004692 ST->getSrcValueOffset(), ST->isVolatile(),
4693 ST->getAlignment());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004694 } else if (!ST->isVolatile() &&
4695 TLI.isOperationLegal(ISD::STORE, MVT::i32)) {
Duncan Sandsdc846502007-10-28 12:59:45 +00004696 // Many FP stores are not made apparent until after legalize, e.g. for
Chris Lattner62be1a72006-12-12 04:16:14 +00004697 // argument passing. Since this is so common, custom legalize the
4698 // 64-bit integer store into two 32-bit stores.
Dale Johannesen7111b022008-10-09 18:53:47 +00004699 uint64_t Val = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00004700 SDValue Lo = DAG.getConstant(Val & 0xFFFFFFFF, MVT::i32);
4701 SDValue Hi = DAG.getConstant(Val >> 32, MVT::i32);
Duncan Sands0753fc12008-02-11 10:37:04 +00004702 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner62be1a72006-12-12 04:16:14 +00004703
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004704 int SVOffset = ST->getSrcValueOffset();
4705 unsigned Alignment = ST->getAlignment();
4706 bool isVolatile = ST->isVolatile();
4707
Dan Gohman475871a2008-07-27 21:46:04 +00004708 SDValue St0 = DAG.getStore(Chain, Lo, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004709 ST->getSrcValueOffset(),
4710 isVolatile, ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00004711 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
4712 DAG.getConstant(4, Ptr.getValueType()));
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004713 SVOffset += 4;
Duncan Sandsdc846502007-10-28 12:59:45 +00004714 Alignment = MinAlign(Alignment, 4U);
Dan Gohman475871a2008-07-27 21:46:04 +00004715 SDValue St1 = DAG.getStore(Chain, Hi, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004716 SVOffset, isVolatile, Alignment);
Chris Lattner62be1a72006-12-12 04:16:14 +00004717 return DAG.getNode(ISD::TokenFactor, MVT::Other, St0, St1);
4718 }
4719 break;
Evan Cheng25ece662006-12-11 17:25:19 +00004720 }
Nate Begeman2cbba892006-12-11 02:23:46 +00004721 }
Nate Begeman2cbba892006-12-11 02:23:46 +00004722 }
4723
Jim Laskey279f0532006-09-25 16:29:54 +00004724 if (CombinerAA) {
4725 // Walk up chain skipping non-aliasing memory nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00004726 SDValue BetterChain = FindBetterChain(N, Chain);
Jim Laskey279f0532006-09-25 16:29:54 +00004727
Jim Laskey6ff23e52006-10-04 16:53:27 +00004728 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00004729 if (Chain != BetterChain) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00004730 // Replace the chain to avoid dependency.
Dan Gohman475871a2008-07-27 21:46:04 +00004731 SDValue ReplStore;
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004732 if (ST->isTruncatingStore()) {
4733 ReplStore = DAG.getTruncStore(BetterChain, Value, Ptr,
Chris Lattner4626b252008-01-17 07:20:38 +00004734 ST->getSrcValue(),ST->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004735 ST->getMemoryVT(),
Chris Lattner4626b252008-01-17 07:20:38 +00004736 ST->isVolatile(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004737 } else {
4738 ReplStore = DAG.getStore(BetterChain, Value, Ptr,
Chris Lattner4626b252008-01-17 07:20:38 +00004739 ST->getSrcValue(), ST->getSrcValueOffset(),
4740 ST->isVolatile(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004741 }
4742
Jim Laskey279f0532006-09-25 16:29:54 +00004743 // Create token to keep both nodes around.
Dan Gohman475871a2008-07-27 21:46:04 +00004744 SDValue Token =
Jim Laskey274062c2006-10-13 23:32:28 +00004745 DAG.getNode(ISD::TokenFactor, MVT::Other, Chain, ReplStore);
4746
4747 // Don't add users to work list.
4748 return CombineTo(N, Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00004749 }
Jim Laskeyd1aed7a2006-09-21 16:28:59 +00004750 }
Chris Lattnerc33baaa2005-12-23 05:48:07 +00004751
Evan Cheng33dbedc2006-11-05 09:31:14 +00004752 // Try transforming N to an indexed store.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00004753 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Dan Gohman475871a2008-07-27 21:46:04 +00004754 return SDValue(N, 0);
Evan Cheng33dbedc2006-11-05 09:31:14 +00004755
Chris Lattner3c872852007-12-29 06:26:16 +00004756 // FIXME: is there such a thing as a truncating indexed store?
Chris Lattnerddf89562008-01-17 19:59:44 +00004757 if (ST->isTruncatingStore() && ST->isUnindexed() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00004758 Value.getValueType().isInteger()) {
Chris Lattner2b4c2792007-10-13 06:35:54 +00004759 // See if we can simplify the input to this truncstore with knowledge that
4760 // only the low bits are being used. For example:
4761 // "truncstore (or (shl x, 8), y), i8" -> "truncstore y, i8"
Dan Gohman475871a2008-07-27 21:46:04 +00004762 SDValue Shorter =
Dan Gohman2e68b6f2008-02-25 21:11:39 +00004763 GetDemandedBits(Value,
4764 APInt::getLowBitsSet(Value.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +00004765 ST->getMemoryVT().getSizeInBits()));
Gabor Greifba36cb52008-08-28 21:40:38 +00004766 AddToWorkList(Value.getNode());
4767 if (Shorter.getNode())
Chris Lattner2b4c2792007-10-13 06:35:54 +00004768 return DAG.getTruncStore(Chain, Shorter, Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004769 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattner2b4c2792007-10-13 06:35:54 +00004770 ST->isVolatile(), ST->getAlignment());
Chris Lattnere33544c2007-10-13 06:58:48 +00004771
4772 // Otherwise, see if we can simplify the operation with
4773 // SimplifyDemandedBits, which only works if the value has a single use.
Dan Gohman7b8d4a92008-02-27 00:25:32 +00004774 if (SimplifyDemandedBits(Value,
4775 APInt::getLowBitsSet(
4776 Value.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +00004777 ST->getMemoryVT().getSizeInBits())))
Dan Gohman475871a2008-07-27 21:46:04 +00004778 return SDValue(N, 0);
Chris Lattner2b4c2792007-10-13 06:35:54 +00004779 }
4780
Chris Lattner3c872852007-12-29 06:26:16 +00004781 // If this is a load followed by a store to the same location, then the store
4782 // is dead/noop.
4783 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Value)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004784 if (Ld->getBasePtr() == Ptr && ST->getMemoryVT() == Ld->getMemoryVT() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00004785 ST->isUnindexed() && !ST->isVolatile() &&
Chris Lattner07649d92008-01-08 23:08:06 +00004786 // There can't be any side effects between the load and store, such as
4787 // a call or store.
Dan Gohman475871a2008-07-27 21:46:04 +00004788 Chain.reachesChainWithoutSideEffects(SDValue(Ld, 1))) {
Chris Lattner3c872852007-12-29 06:26:16 +00004789 // The store is dead, remove it.
4790 return Chain;
4791 }
4792 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004793
Chris Lattnerddf89562008-01-17 19:59:44 +00004794 // If this is an FP_ROUND or TRUNC followed by a store, fold this into a
4795 // truncating store. We can do this even if this is already a truncstore.
4796 if ((Value.getOpcode() == ISD::FP_ROUND || Value.getOpcode() == ISD::TRUNCATE)
Gabor Greifba36cb52008-08-28 21:40:38 +00004797 && Value.getNode()->hasOneUse() && ST->isUnindexed() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00004798 TLI.isTruncStoreLegal(Value.getOperand(0).getValueType(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004799 ST->getMemoryVT())) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004800 return DAG.getTruncStore(Chain, Value.getOperand(0), Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004801 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattnerddf89562008-01-17 19:59:44 +00004802 ST->isVolatile(), ST->getAlignment());
4803 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004804
Dan Gohman475871a2008-07-27 21:46:04 +00004805 return SDValue();
Chris Lattner87514ca2005-10-10 22:31:19 +00004806}
4807
Dan Gohman475871a2008-07-27 21:46:04 +00004808SDValue DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
4809 SDValue InVec = N->getOperand(0);
4810 SDValue InVal = N->getOperand(1);
4811 SDValue EltNo = N->getOperand(2);
Chris Lattnerca242442006-03-19 01:27:56 +00004812
4813 // If the invec is a BUILD_VECTOR and if EltNo is a constant, build a new
4814 // vector with the inserted element.
4815 if (InVec.getOpcode() == ISD::BUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004816 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Gabor Greif12632d22008-08-30 19:29:20 +00004817 SmallVector<SDValue, 8> Ops(InVec.getNode()->op_begin(),
4818 InVec.getNode()->op_end());
Chris Lattnerca242442006-03-19 01:27:56 +00004819 if (Elt < Ops.size())
4820 Ops[Elt] = InVal;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004821 return DAG.getNode(ISD::BUILD_VECTOR, InVec.getValueType(),
4822 &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00004823 }
4824
Dan Gohman475871a2008-07-27 21:46:04 +00004825 return SDValue();
Chris Lattnerca242442006-03-19 01:27:56 +00004826}
4827
Dan Gohman475871a2008-07-27 21:46:04 +00004828SDValue DAGCombiner::visitEXTRACT_VECTOR_ELT(SDNode *N) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004829 // (vextract (v4f32 load $addr), c) -> (f32 load $addr+c*size)
4830 // (vextract (v4f32 s2v (f32 load $addr)), c) -> (f32 load $addr+c*size)
4831 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), 0) -> (f32 load $addr)
4832
4833 // Perform only after legalization to ensure build_vector / vector_shuffle
4834 // optimizations have already been done.
Dan Gohman475871a2008-07-27 21:46:04 +00004835 if (!AfterLegalize) return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004836
Dan Gohman475871a2008-07-27 21:46:04 +00004837 SDValue InVec = N->getOperand(0);
4838 SDValue EltNo = N->getOperand(1);
Evan Cheng513da432007-10-06 08:19:55 +00004839
Evan Cheng513da432007-10-06 08:19:55 +00004840 if (isa<ConstantSDNode>(EltNo)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004841 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Evan Cheng513da432007-10-06 08:19:55 +00004842 bool NewLoad = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004843 MVT VT = InVec.getValueType();
4844 MVT EVT = VT.getVectorElementType();
4845 MVT LVT = EVT;
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004846 if (InVec.getOpcode() == ISD::BIT_CONVERT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004847 MVT BCVT = InVec.getOperand(0).getValueType();
Duncan Sands8e4eb092008-06-08 20:54:56 +00004848 if (!BCVT.isVector() || EVT.bitsGT(BCVT.getVectorElementType()))
Dan Gohman475871a2008-07-27 21:46:04 +00004849 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004850 InVec = InVec.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004851 EVT = BCVT.getVectorElementType();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004852 NewLoad = true;
4853 }
Evan Cheng513da432007-10-06 08:19:55 +00004854
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004855 LoadSDNode *LN0 = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00004856 if (ISD::isNormalLoad(InVec.getNode()))
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004857 LN0 = cast<LoadSDNode>(InVec);
4858 else if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR &&
4859 InVec.getOperand(0).getValueType() == EVT &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004860 ISD::isNormalLoad(InVec.getOperand(0).getNode())) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004861 LN0 = cast<LoadSDNode>(InVec.getOperand(0));
4862 } else if (InVec.getOpcode() == ISD::VECTOR_SHUFFLE) {
4863 // (vextract (vector_shuffle (load $addr), v2, <1, u, u, u>), 1)
4864 // =>
4865 // (load $addr+1*size)
4866 unsigned Idx = cast<ConstantSDNode>(InVec.getOperand(2).
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004867 getOperand(Elt))->getZExtValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004868 unsigned NumElems = InVec.getOperand(2).getNumOperands();
4869 InVec = (Idx < NumElems) ? InVec.getOperand(0) : InVec.getOperand(1);
4870 if (InVec.getOpcode() == ISD::BIT_CONVERT)
4871 InVec = InVec.getOperand(0);
Gabor Greifba36cb52008-08-28 21:40:38 +00004872 if (ISD::isNormalLoad(InVec.getNode())) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004873 LN0 = cast<LoadSDNode>(InVec);
4874 Elt = (Idx < NumElems) ? Idx : Idx - NumElems;
Evan Cheng513da432007-10-06 08:19:55 +00004875 }
4876 }
Duncan Sandsec87aa82008-06-15 20:12:31 +00004877 if (!LN0 || !LN0->hasOneUse() || LN0->isVolatile())
Dan Gohman475871a2008-07-27 21:46:04 +00004878 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004879
4880 unsigned Align = LN0->getAlignment();
4881 if (NewLoad) {
4882 // Check the resultant load doesn't need a higher alignment than the
4883 // original load.
Dan Gohman6448d912008-09-04 15:39:15 +00004884 unsigned NewAlign = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00004885 getABITypeAlignment(LVT.getTypeForMVT());
Duncan Sands184a8762008-06-14 17:48:34 +00004886 if (NewAlign > Align || !TLI.isOperationLegal(ISD::LOAD, LVT))
Dan Gohman475871a2008-07-27 21:46:04 +00004887 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004888 Align = NewAlign;
4889 }
4890
Dan Gohman475871a2008-07-27 21:46:04 +00004891 SDValue NewPtr = LN0->getBasePtr();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004892 if (Elt) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004893 unsigned PtrOff = LVT.getSizeInBits() * Elt / 8;
4894 MVT PtrType = NewPtr.getValueType();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004895 if (TLI.isBigEndian())
Duncan Sands83ec4b62008-06-06 12:08:01 +00004896 PtrOff = VT.getSizeInBits() / 8 - PtrOff;
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004897 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
4898 DAG.getConstant(PtrOff, PtrType));
4899 }
4900 return DAG.getLoad(LVT, LN0->getChain(), NewPtr,
4901 LN0->getSrcValue(), LN0->getSrcValueOffset(),
4902 LN0->isVolatile(), Align);
Evan Cheng513da432007-10-06 08:19:55 +00004903 }
Dan Gohman475871a2008-07-27 21:46:04 +00004904 return SDValue();
Evan Cheng513da432007-10-06 08:19:55 +00004905}
4906
4907
Dan Gohman475871a2008-07-27 21:46:04 +00004908SDValue DAGCombiner::visitBUILD_VECTOR(SDNode *N) {
Dan Gohman7f321562007-06-25 16:23:39 +00004909 unsigned NumInScalars = N->getNumOperands();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004910 MVT VT = N->getValueType(0);
4911 unsigned NumElts = VT.getVectorNumElements();
4912 MVT EltType = VT.getVectorElementType();
Chris Lattnerca242442006-03-19 01:27:56 +00004913
Dan Gohman7f321562007-06-25 16:23:39 +00004914 // Check to see if this is a BUILD_VECTOR of a bunch of EXTRACT_VECTOR_ELT
4915 // operations. If so, and if the EXTRACT_VECTOR_ELT vector inputs come from
4916 // at most two distinct vectors, turn this into a shuffle node.
Dan Gohman475871a2008-07-27 21:46:04 +00004917 SDValue VecIn1, VecIn2;
Chris Lattnerd7648c82006-03-28 20:28:38 +00004918 for (unsigned i = 0; i != NumInScalars; ++i) {
4919 // Ignore undef inputs.
4920 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
4921
Dan Gohman7f321562007-06-25 16:23:39 +00004922 // If this input is something other than a EXTRACT_VECTOR_ELT with a
Chris Lattnerd7648c82006-03-28 20:28:38 +00004923 // constant index, bail out.
Dan Gohman7f321562007-06-25 16:23:39 +00004924 if (N->getOperand(i).getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
Chris Lattnerd7648c82006-03-28 20:28:38 +00004925 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
Dan Gohman475871a2008-07-27 21:46:04 +00004926 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00004927 break;
4928 }
4929
Dan Gohman7f321562007-06-25 16:23:39 +00004930 // If the input vector type disagrees with the result of the build_vector,
Chris Lattnerd7648c82006-03-28 20:28:38 +00004931 // we can't make a shuffle.
Dan Gohman475871a2008-07-27 21:46:04 +00004932 SDValue ExtractedFromVec = N->getOperand(i).getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00004933 if (ExtractedFromVec.getValueType() != VT) {
Dan Gohman475871a2008-07-27 21:46:04 +00004934 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00004935 break;
4936 }
4937
4938 // Otherwise, remember this. We allow up to two distinct input vectors.
4939 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
4940 continue;
4941
Gabor Greifba36cb52008-08-28 21:40:38 +00004942 if (VecIn1.getNode() == 0) {
Chris Lattnerd7648c82006-03-28 20:28:38 +00004943 VecIn1 = ExtractedFromVec;
Gabor Greifba36cb52008-08-28 21:40:38 +00004944 } else if (VecIn2.getNode() == 0) {
Chris Lattnerd7648c82006-03-28 20:28:38 +00004945 VecIn2 = ExtractedFromVec;
4946 } else {
4947 // Too many inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00004948 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00004949 break;
4950 }
4951 }
4952
4953 // If everything is good, we can make a shuffle operation.
Gabor Greifba36cb52008-08-28 21:40:38 +00004954 if (VecIn1.getNode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004955 SmallVector<SDValue, 8> BuildVecIndices;
Chris Lattnerd7648c82006-03-28 20:28:38 +00004956 for (unsigned i = 0; i != NumInScalars; ++i) {
4957 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
Evan Cheng597a3bd2007-01-20 10:10:26 +00004958 BuildVecIndices.push_back(DAG.getNode(ISD::UNDEF, TLI.getPointerTy()));
Chris Lattnerd7648c82006-03-28 20:28:38 +00004959 continue;
4960 }
4961
Dan Gohman475871a2008-07-27 21:46:04 +00004962 SDValue Extract = N->getOperand(i);
Chris Lattnerd7648c82006-03-28 20:28:38 +00004963
4964 // If extracting from the first vector, just use the index directly.
4965 if (Extract.getOperand(0) == VecIn1) {
4966 BuildVecIndices.push_back(Extract.getOperand(1));
4967 continue;
4968 }
4969
4970 // Otherwise, use InIdx + VecSize
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004971 unsigned Idx =
4972 cast<ConstantSDNode>(Extract.getOperand(1))->getZExtValue();
Chris Lattner0bd48932008-01-17 07:00:52 +00004973 BuildVecIndices.push_back(DAG.getIntPtrConstant(Idx+NumInScalars));
Chris Lattnerd7648c82006-03-28 20:28:38 +00004974 }
4975
4976 // Add count and size info.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004977 MVT BuildVecVT = MVT::getVectorVT(TLI.getPointerTy(), NumElts);
Chris Lattnerd7648c82006-03-28 20:28:38 +00004978
Dan Gohman7f321562007-06-25 16:23:39 +00004979 // Return the new VECTOR_SHUFFLE node.
Dan Gohman475871a2008-07-27 21:46:04 +00004980 SDValue Ops[5];
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004981 Ops[0] = VecIn1;
Gabor Greifba36cb52008-08-28 21:40:38 +00004982 if (VecIn2.getNode()) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004983 Ops[1] = VecIn2;
Chris Lattnercef896e2006-03-28 22:19:47 +00004984 } else {
Dan Gohman7f321562007-06-25 16:23:39 +00004985 // Use an undef build_vector as input for the second operand.
Dan Gohman475871a2008-07-27 21:46:04 +00004986 std::vector<SDValue> UnOps(NumInScalars,
Chris Lattnercef896e2006-03-28 22:19:47 +00004987 DAG.getNode(ISD::UNDEF,
Dan Gohman7f321562007-06-25 16:23:39 +00004988 EltType));
4989 Ops[1] = DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004990 &UnOps[0], UnOps.size());
Gabor Greifba36cb52008-08-28 21:40:38 +00004991 AddToWorkList(Ops[1].getNode());
Chris Lattnercef896e2006-03-28 22:19:47 +00004992 }
Dan Gohman7f321562007-06-25 16:23:39 +00004993 Ops[2] = DAG.getNode(ISD::BUILD_VECTOR, BuildVecVT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004994 &BuildVecIndices[0], BuildVecIndices.size());
Dan Gohman7f321562007-06-25 16:23:39 +00004995 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Ops, 3);
Chris Lattnerd7648c82006-03-28 20:28:38 +00004996 }
4997
Dan Gohman475871a2008-07-27 21:46:04 +00004998 return SDValue();
Chris Lattnerd7648c82006-03-28 20:28:38 +00004999}
5000
Dan Gohman475871a2008-07-27 21:46:04 +00005001SDValue DAGCombiner::visitCONCAT_VECTORS(SDNode *N) {
Dan Gohman7f321562007-06-25 16:23:39 +00005002 // TODO: Check to see if this is a CONCAT_VECTORS of a bunch of
5003 // EXTRACT_SUBVECTOR operations. If so, and if the EXTRACT_SUBVECTOR vector
5004 // inputs come from at most two distinct vectors, turn this into a shuffle
5005 // node.
5006
5007 // If we only have one input vector, we don't need to do any concatenation.
5008 if (N->getNumOperands() == 1) {
5009 return N->getOperand(0);
5010 }
5011
Dan Gohman475871a2008-07-27 21:46:04 +00005012 return SDValue();
Dan Gohman7f321562007-06-25 16:23:39 +00005013}
5014
Dan Gohman475871a2008-07-27 21:46:04 +00005015SDValue DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
5016 SDValue ShufMask = N->getOperand(2);
Chris Lattnerf1d0c622006-03-31 22:16:43 +00005017 unsigned NumElts = ShufMask.getNumOperands();
5018
Mon P Wangaeb06d22008-11-10 04:46:22 +00005019 SDValue N0 = N->getOperand(0);
5020 SDValue N1 = N->getOperand(1);
5021
5022 assert(N0.getValueType().getVectorNumElements() == NumElts &&
5023 "Vector shuffle must be normalized in DAG");
5024
Chris Lattnerf1d0c622006-03-31 22:16:43 +00005025 // If the shuffle mask is an identity operation on the LHS, return the LHS.
5026 bool isIdentity = true;
5027 for (unsigned i = 0; i != NumElts; ++i) {
5028 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005029 cast<ConstantSDNode>(ShufMask.getOperand(i))->getZExtValue() != i) {
Chris Lattnerf1d0c622006-03-31 22:16:43 +00005030 isIdentity = false;
5031 break;
5032 }
5033 }
5034 if (isIdentity) return N->getOperand(0);
5035
5036 // If the shuffle mask is an identity operation on the RHS, return the RHS.
5037 isIdentity = true;
5038 for (unsigned i = 0; i != NumElts; ++i) {
5039 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005040 cast<ConstantSDNode>(ShufMask.getOperand(i))->getZExtValue() !=
5041 i+NumElts) {
Chris Lattnerf1d0c622006-03-31 22:16:43 +00005042 isIdentity = false;
5043 break;
5044 }
5045 }
5046 if (isIdentity) return N->getOperand(1);
Evan Chenge7bec0d2006-07-20 22:44:41 +00005047
5048 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
5049 // needed at all.
5050 bool isUnary = true;
Evan Cheng917ec982006-07-21 08:25:53 +00005051 bool isSplat = true;
Evan Chenge7bec0d2006-07-20 22:44:41 +00005052 int VecNum = -1;
Reid Spencer9160a6a2006-07-25 20:44:41 +00005053 unsigned BaseIdx = 0;
Evan Chenge7bec0d2006-07-20 22:44:41 +00005054 for (unsigned i = 0; i != NumElts; ++i)
5055 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005056 unsigned Idx=cast<ConstantSDNode>(ShufMask.getOperand(i))->getZExtValue();
Evan Chenge7bec0d2006-07-20 22:44:41 +00005057 int V = (Idx < NumElts) ? 0 : 1;
Evan Cheng917ec982006-07-21 08:25:53 +00005058 if (VecNum == -1) {
Evan Chenge7bec0d2006-07-20 22:44:41 +00005059 VecNum = V;
Evan Cheng917ec982006-07-21 08:25:53 +00005060 BaseIdx = Idx;
5061 } else {
5062 if (BaseIdx != Idx)
5063 isSplat = false;
5064 if (VecNum != V) {
5065 isUnary = false;
5066 break;
5067 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00005068 }
5069 }
5070
Evan Chenge7bec0d2006-07-20 22:44:41 +00005071 // Normalize unary shuffle so the RHS is undef.
5072 if (isUnary && VecNum == 1)
5073 std::swap(N0, N1);
5074
Evan Cheng917ec982006-07-21 08:25:53 +00005075 // If it is a splat, check if the argument vector is a build_vector with
5076 // all scalar elements the same.
5077 if (isSplat) {
Gabor Greifba36cb52008-08-28 21:40:38 +00005078 SDNode *V = N0.getNode();
Evan Cheng917ec982006-07-21 08:25:53 +00005079
Dan Gohman7f321562007-06-25 16:23:39 +00005080 // If this is a bit convert that changes the element type of the vector but
Evan Cheng59569222006-10-16 22:49:37 +00005081 // not the number of vector elements, look through it. Be careful not to
5082 // look though conversions that change things like v4f32 to v2f64.
Dan Gohman7f321562007-06-25 16:23:39 +00005083 if (V->getOpcode() == ISD::BIT_CONVERT) {
Dan Gohman475871a2008-07-27 21:46:04 +00005084 SDValue ConvInput = V->getOperand(0);
Evan Cheng29257862008-07-22 20:42:56 +00005085 if (ConvInput.getValueType().isVector() &&
5086 ConvInput.getValueType().getVectorNumElements() == NumElts)
Gabor Greifba36cb52008-08-28 21:40:38 +00005087 V = ConvInput.getNode();
Evan Cheng59569222006-10-16 22:49:37 +00005088 }
5089
Dan Gohman7f321562007-06-25 16:23:39 +00005090 if (V->getOpcode() == ISD::BUILD_VECTOR) {
5091 unsigned NumElems = V->getNumOperands();
Evan Cheng917ec982006-07-21 08:25:53 +00005092 if (NumElems > BaseIdx) {
Dan Gohman475871a2008-07-27 21:46:04 +00005093 SDValue Base;
Evan Cheng917ec982006-07-21 08:25:53 +00005094 bool AllSame = true;
5095 for (unsigned i = 0; i != NumElems; ++i) {
5096 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
5097 Base = V->getOperand(i);
5098 break;
5099 }
5100 }
5101 // Splat of <u, u, u, u>, return <u, u, u, u>
Gabor Greifba36cb52008-08-28 21:40:38 +00005102 if (!Base.getNode())
Evan Cheng917ec982006-07-21 08:25:53 +00005103 return N0;
5104 for (unsigned i = 0; i != NumElems; ++i) {
Evan Chenge0480d22007-09-18 21:54:37 +00005105 if (V->getOperand(i) != Base) {
Evan Cheng917ec982006-07-21 08:25:53 +00005106 AllSame = false;
5107 break;
5108 }
5109 }
5110 // Splat of <x, x, x, x>, return <x, x, x, x>
5111 if (AllSame)
5112 return N0;
5113 }
5114 }
5115 }
5116
Evan Chenge7bec0d2006-07-20 22:44:41 +00005117 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
5118 // into an undef.
5119 if (isUnary || N0 == N1) {
Chris Lattner17614ea2006-04-08 05:34:25 +00005120 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
5121 // first operand.
Dan Gohman475871a2008-07-27 21:46:04 +00005122 SmallVector<SDValue, 8> MappedOps;
Chris Lattner17614ea2006-04-08 05:34:25 +00005123 for (unsigned i = 0; i != NumElts; ++i) {
5124 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005125 cast<ConstantSDNode>(ShufMask.getOperand(i))->getZExtValue() <
5126 NumElts) {
Chris Lattner17614ea2006-04-08 05:34:25 +00005127 MappedOps.push_back(ShufMask.getOperand(i));
5128 } else {
5129 unsigned NewIdx =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005130 cast<ConstantSDNode>(ShufMask.getOperand(i))->getZExtValue() -
5131 NumElts;
Duncan Sandsd038e042008-07-21 10:20:31 +00005132 MappedOps.push_back(DAG.getConstant(NewIdx,
5133 ShufMask.getOperand(i).getValueType()));
Chris Lattner17614ea2006-04-08 05:34:25 +00005134 }
5135 }
Dan Gohman7f321562007-06-25 16:23:39 +00005136 ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMask.getValueType(),
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005137 &MappedOps[0], MappedOps.size());
Gabor Greifba36cb52008-08-28 21:40:38 +00005138 AddToWorkList(ShufMask.getNode());
Dan Gohman7f321562007-06-25 16:23:39 +00005139 return DAG.getNode(ISD::VECTOR_SHUFFLE, N->getValueType(0),
5140 N0,
5141 DAG.getNode(ISD::UNDEF, N->getValueType(0)),
5142 ShufMask);
Chris Lattner17614ea2006-04-08 05:34:25 +00005143 }
Dan Gohman7f321562007-06-25 16:23:39 +00005144
Dan Gohman475871a2008-07-27 21:46:04 +00005145 return SDValue();
Chris Lattnerf1d0c622006-03-31 22:16:43 +00005146}
5147
Bill Wendling7cdc3c82008-11-21 02:03:52 +00005148SDValue DAGCombiner::visitADDO(SDNode *N) {
5149 SDValue Chain = N->getOperand(2);
5150 SDValue LHS = N->getOperand(0);
5151 SDValue RHS = N->getOperand(1);
5152
5153 SDValue Sum = DAG.getNode(ISD::ADD, LHS.getValueType(), LHS, RHS);
5154 AddToWorkList(Sum.getNode());
5155 SDValue Cmp = DAG.getSetCC(MVT::i1, Sum, LHS, ISD::SETLT);
5156 AddToWorkList(Cmp.getNode());
5157
5158 MVT ValueVTs[] = { LHS.getValueType(), MVT::i1, MVT::Other };
5159 SDValue Ops[] = { Sum, Cmp, Chain };
5160
5161 SDValue Merge = DAG.getMergeValues(DAG.getVTList(&ValueVTs[0], 3),
5162 &Ops[0], 3);
5163 SDNode *MNode = Merge.getNode();
5164
5165 AddToWorkList(MNode);
5166 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), SDValue(MNode, 0));
5167 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SDValue(MNode, 1));
5168 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 2), SDValue(MNode, 2));
5169
5170 // Since the node is now dead, remove it from the graph.
5171 removeFromWorkList(N);
5172 DAG.DeleteNode(N);
5173 return SDValue(N, 0);
5174}
5175
Evan Cheng44f1f092006-04-20 08:56:16 +00005176/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
Dan Gohman7f321562007-06-25 16:23:39 +00005177/// an AND to a vector_shuffle with the destination vector and a zero vector.
5178/// e.g. AND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
Evan Cheng44f1f092006-04-20 08:56:16 +00005179/// vector_shuffle V, Zero, <0, 4, 2, 4>
Dan Gohman475871a2008-07-27 21:46:04 +00005180SDValue DAGCombiner::XformToShuffleWithZero(SDNode *N) {
5181 SDValue LHS = N->getOperand(0);
5182 SDValue RHS = N->getOperand(1);
Dan Gohman7f321562007-06-25 16:23:39 +00005183 if (N->getOpcode() == ISD::AND) {
5184 if (RHS.getOpcode() == ISD::BIT_CONVERT)
Evan Cheng44f1f092006-04-20 08:56:16 +00005185 RHS = RHS.getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00005186 if (RHS.getOpcode() == ISD::BUILD_VECTOR) {
Dan Gohman475871a2008-07-27 21:46:04 +00005187 std::vector<SDValue> IdxOps;
Evan Cheng44f1f092006-04-20 08:56:16 +00005188 unsigned NumOps = RHS.getNumOperands();
Dan Gohman7f321562007-06-25 16:23:39 +00005189 unsigned NumElts = NumOps;
Evan Cheng44f1f092006-04-20 08:56:16 +00005190 for (unsigned i = 0; i != NumElts; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005191 SDValue Elt = RHS.getOperand(i);
Evan Cheng44f1f092006-04-20 08:56:16 +00005192 if (!isa<ConstantSDNode>(Elt))
Dan Gohman475871a2008-07-27 21:46:04 +00005193 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00005194 else if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
Duncan Sands94989ac2008-10-19 14:58:05 +00005195 IdxOps.push_back(DAG.getIntPtrConstant(i));
Evan Cheng44f1f092006-04-20 08:56:16 +00005196 else if (cast<ConstantSDNode>(Elt)->isNullValue())
Duncan Sands94989ac2008-10-19 14:58:05 +00005197 IdxOps.push_back(DAG.getIntPtrConstant(NumElts));
Evan Cheng44f1f092006-04-20 08:56:16 +00005198 else
Dan Gohman475871a2008-07-27 21:46:04 +00005199 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00005200 }
5201
5202 // Let's see if the target supports this vector_shuffle.
Duncan Sands94989ac2008-10-19 14:58:05 +00005203 if (!TLI.isVectorClearMaskLegal(IdxOps, TLI.getPointerTy(), DAG))
Dan Gohman475871a2008-07-27 21:46:04 +00005204 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00005205
Dan Gohman7f321562007-06-25 16:23:39 +00005206 // Return the new VECTOR_SHUFFLE node.
Duncan Sands94989ac2008-10-19 14:58:05 +00005207 MVT EVT = RHS.getValueType().getVectorElementType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005208 MVT VT = MVT::getVectorVT(EVT, NumElts);
Evan Cheng3eb57d52008-11-05 06:04:18 +00005209 MVT MaskVT = MVT::getVectorVT(TLI.getPointerTy(), NumElts);
Dan Gohman475871a2008-07-27 21:46:04 +00005210 std::vector<SDValue> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00005211 LHS = DAG.getNode(ISD::BIT_CONVERT, VT, LHS);
Evan Cheng44f1f092006-04-20 08:56:16 +00005212 Ops.push_back(LHS);
Gabor Greifba36cb52008-08-28 21:40:38 +00005213 AddToWorkList(LHS.getNode());
Dan Gohman475871a2008-07-27 21:46:04 +00005214 std::vector<SDValue> ZeroOps(NumElts, DAG.getConstant(0, EVT));
Dan Gohman7f321562007-06-25 16:23:39 +00005215 Ops.push_back(DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005216 &ZeroOps[0], ZeroOps.size()));
Evan Cheng3eb57d52008-11-05 06:04:18 +00005217 Ops.push_back(DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005218 &IdxOps[0], IdxOps.size()));
Dan Gohman475871a2008-07-27 21:46:04 +00005219 SDValue Result = DAG.getNode(ISD::VECTOR_SHUFFLE, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005220 &Ops[0], Ops.size());
Dan Gohman7a9a5af2008-07-16 16:13:58 +00005221 if (VT != N->getValueType(0))
5222 Result = DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Result);
Evan Cheng44f1f092006-04-20 08:56:16 +00005223 return Result;
5224 }
5225 }
Dan Gohman475871a2008-07-27 21:46:04 +00005226 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00005227}
5228
Dan Gohman7f321562007-06-25 16:23:39 +00005229/// SimplifyVBinOp - Visit a binary vector operation, like ADD.
Dan Gohman475871a2008-07-27 21:46:04 +00005230SDValue DAGCombiner::SimplifyVBinOp(SDNode *N) {
Dan Gohman7f321562007-06-25 16:23:39 +00005231 // After legalize, the target may be depending on adds and other
5232 // binary ops to provide legal ways to construct constants or other
5233 // things. Simplifying them may result in a loss of legality.
Dan Gohman475871a2008-07-27 21:46:04 +00005234 if (AfterLegalize) return SDValue();
Dan Gohman7f321562007-06-25 16:23:39 +00005235
Duncan Sands83ec4b62008-06-06 12:08:01 +00005236 MVT VT = N->getValueType(0);
5237 assert(VT.isVector() && "SimplifyVBinOp only works on vectors!");
Dan Gohman7f321562007-06-25 16:23:39 +00005238
Duncan Sands83ec4b62008-06-06 12:08:01 +00005239 MVT EltType = VT.getVectorElementType();
Dan Gohman475871a2008-07-27 21:46:04 +00005240 SDValue LHS = N->getOperand(0);
5241 SDValue RHS = N->getOperand(1);
5242 SDValue Shuffle = XformToShuffleWithZero(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00005243 if (Shuffle.getNode()) return Shuffle;
Evan Cheng44f1f092006-04-20 08:56:16 +00005244
Dan Gohman7f321562007-06-25 16:23:39 +00005245 // If the LHS and RHS are BUILD_VECTOR nodes, see if we can constant fold
Chris Lattneredab1b92006-04-02 03:25:57 +00005246 // this operation.
Dan Gohman7f321562007-06-25 16:23:39 +00005247 if (LHS.getOpcode() == ISD::BUILD_VECTOR &&
5248 RHS.getOpcode() == ISD::BUILD_VECTOR) {
Dan Gohman475871a2008-07-27 21:46:04 +00005249 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00005250 for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005251 SDValue LHSOp = LHS.getOperand(i);
5252 SDValue RHSOp = RHS.getOperand(i);
Chris Lattneredab1b92006-04-02 03:25:57 +00005253 // If these two elements can't be folded, bail out.
5254 if ((LHSOp.getOpcode() != ISD::UNDEF &&
5255 LHSOp.getOpcode() != ISD::Constant &&
5256 LHSOp.getOpcode() != ISD::ConstantFP) ||
5257 (RHSOp.getOpcode() != ISD::UNDEF &&
5258 RHSOp.getOpcode() != ISD::Constant &&
5259 RHSOp.getOpcode() != ISD::ConstantFP))
5260 break;
Evan Cheng7b336a82006-05-31 06:08:35 +00005261 // Can't fold divide by zero.
Dan Gohman7f321562007-06-25 16:23:39 +00005262 if (N->getOpcode() == ISD::SDIV || N->getOpcode() == ISD::UDIV ||
5263 N->getOpcode() == ISD::FDIV) {
Evan Cheng7b336a82006-05-31 06:08:35 +00005264 if ((RHSOp.getOpcode() == ISD::Constant &&
Gabor Greifba36cb52008-08-28 21:40:38 +00005265 cast<ConstantSDNode>(RHSOp.getNode())->isNullValue()) ||
Evan Cheng7b336a82006-05-31 06:08:35 +00005266 (RHSOp.getOpcode() == ISD::ConstantFP &&
Gabor Greifba36cb52008-08-28 21:40:38 +00005267 cast<ConstantFPSDNode>(RHSOp.getNode())->getValueAPF().isZero()))
Evan Cheng7b336a82006-05-31 06:08:35 +00005268 break;
5269 }
Dan Gohman7f321562007-06-25 16:23:39 +00005270 Ops.push_back(DAG.getNode(N->getOpcode(), EltType, LHSOp, RHSOp));
Gabor Greifba36cb52008-08-28 21:40:38 +00005271 AddToWorkList(Ops.back().getNode());
Chris Lattneredab1b92006-04-02 03:25:57 +00005272 assert((Ops.back().getOpcode() == ISD::UNDEF ||
5273 Ops.back().getOpcode() == ISD::Constant ||
5274 Ops.back().getOpcode() == ISD::ConstantFP) &&
5275 "Scalar binop didn't fold!");
5276 }
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00005277
Dan Gohman7f321562007-06-25 16:23:39 +00005278 if (Ops.size() == LHS.getNumOperands()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005279 MVT VT = LHS.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +00005280 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00005281 }
Chris Lattneredab1b92006-04-02 03:25:57 +00005282 }
5283
Dan Gohman475871a2008-07-27 21:46:04 +00005284 return SDValue();
Chris Lattneredab1b92006-04-02 03:25:57 +00005285}
5286
Dan Gohman475871a2008-07-27 21:46:04 +00005287SDValue DAGCombiner::SimplifySelect(SDValue N0, SDValue N1, SDValue N2){
Nate Begemanf845b452005-10-08 00:29:44 +00005288 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
5289
Dan Gohman475871a2008-07-27 21:46:04 +00005290 SDValue SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
Nate Begemanf845b452005-10-08 00:29:44 +00005291 cast<CondCodeSDNode>(N0.getOperand(2))->get());
5292 // If we got a simplified select_cc node back from SimplifySelectCC, then
5293 // break it down into a new SETCC node, and a new SELECT node, and then return
5294 // the SELECT node, since we were called with a SELECT node.
Gabor Greifba36cb52008-08-28 21:40:38 +00005295 if (SCC.getNode()) {
Nate Begemanf845b452005-10-08 00:29:44 +00005296 // Check to see if we got a select_cc back (to turn into setcc/select).
5297 // Otherwise, just return whatever node we got back, like fabs.
5298 if (SCC.getOpcode() == ISD::SELECT_CC) {
Dan Gohman475871a2008-07-27 21:46:04 +00005299 SDValue SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
Nate Begemanf845b452005-10-08 00:29:44 +00005300 SCC.getOperand(0), SCC.getOperand(1),
5301 SCC.getOperand(4));
Gabor Greifba36cb52008-08-28 21:40:38 +00005302 AddToWorkList(SETCC.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00005303 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
5304 SCC.getOperand(3), SETCC);
5305 }
5306 return SCC;
5307 }
Dan Gohman475871a2008-07-27 21:46:04 +00005308 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00005309}
5310
Chris Lattner40c62d52005-10-18 06:04:22 +00005311/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
5312/// are the two values being selected between, see if we can simplify the
Chris Lattner729c6d12006-05-27 00:43:02 +00005313/// select. Callers of this should assume that TheSelect is deleted if this
5314/// returns true. As such, they should return the appropriate thing (e.g. the
5315/// node) back to the top-level of the DAG combiner loop to avoid it being
5316/// looked at.
Chris Lattner40c62d52005-10-18 06:04:22 +00005317///
Dan Gohman475871a2008-07-27 21:46:04 +00005318bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDValue LHS,
5319 SDValue RHS) {
Chris Lattner40c62d52005-10-18 06:04:22 +00005320
5321 // If this is a select from two identical things, try to pull the operation
5322 // through the select.
5323 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
Chris Lattner40c62d52005-10-18 06:04:22 +00005324 // If this is a load and the token chain is identical, replace the select
5325 // of two loads with a load through a select of the address to load from.
5326 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
5327 // constants have been dropped into the constant pool.
Evan Cheng466685d2006-10-09 20:57:25 +00005328 if (LHS.getOpcode() == ISD::LOAD &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005329 // Do not let this transformation reduce the number of volatile loads.
5330 !cast<LoadSDNode>(LHS)->isVolatile() &&
5331 !cast<LoadSDNode>(RHS)->isVolatile() &&
Chris Lattner40c62d52005-10-18 06:04:22 +00005332 // Token chains must be identical.
Evan Cheng466685d2006-10-09 20:57:25 +00005333 LHS.getOperand(0) == RHS.getOperand(0)) {
5334 LoadSDNode *LLD = cast<LoadSDNode>(LHS);
5335 LoadSDNode *RLD = cast<LoadSDNode>(RHS);
5336
5337 // If this is an EXTLOAD, the VT's must match.
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005338 if (LLD->getMemoryVT() == RLD->getMemoryVT()) {
Evan Cheng466685d2006-10-09 20:57:25 +00005339 // FIXME: this conflates two src values, discarding one. This is not
5340 // the right thing to do, but nothing uses srcvalues now. When they do,
5341 // turn SrcValue into a list of locations.
Dan Gohman475871a2008-07-27 21:46:04 +00005342 SDValue Addr;
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005343 if (TheSelect->getOpcode() == ISD::SELECT) {
5344 // Check that the condition doesn't reach either load. If so, folding
5345 // this will induce a cycle into the DAG.
Gabor Greifba36cb52008-08-28 21:40:38 +00005346 if (!LLD->isPredecessorOf(TheSelect->getOperand(0).getNode()) &&
5347 !RLD->isPredecessorOf(TheSelect->getOperand(0).getNode())) {
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005348 Addr = DAG.getNode(ISD::SELECT, LLD->getBasePtr().getValueType(),
5349 TheSelect->getOperand(0), LLD->getBasePtr(),
5350 RLD->getBasePtr());
5351 }
5352 } else {
5353 // Check that the condition doesn't reach either load. If so, folding
5354 // this will induce a cycle into the DAG.
Gabor Greifba36cb52008-08-28 21:40:38 +00005355 if (!LLD->isPredecessorOf(TheSelect->getOperand(0).getNode()) &&
5356 !RLD->isPredecessorOf(TheSelect->getOperand(0).getNode()) &&
5357 !LLD->isPredecessorOf(TheSelect->getOperand(1).getNode()) &&
5358 !RLD->isPredecessorOf(TheSelect->getOperand(1).getNode())) {
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005359 Addr = DAG.getNode(ISD::SELECT_CC, LLD->getBasePtr().getValueType(),
Evan Cheng466685d2006-10-09 20:57:25 +00005360 TheSelect->getOperand(0),
5361 TheSelect->getOperand(1),
5362 LLD->getBasePtr(), RLD->getBasePtr(),
5363 TheSelect->getOperand(4));
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005364 }
Evan Cheng466685d2006-10-09 20:57:25 +00005365 }
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005366
Gabor Greifba36cb52008-08-28 21:40:38 +00005367 if (Addr.getNode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005368 SDValue Load;
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005369 if (LLD->getExtensionType() == ISD::NON_EXTLOAD)
5370 Load = DAG.getLoad(TheSelect->getValueType(0), LLD->getChain(),
5371 Addr,LLD->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00005372 LLD->getSrcValueOffset(),
5373 LLD->isVolatile(),
5374 LLD->getAlignment());
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005375 else {
5376 Load = DAG.getExtLoad(LLD->getExtensionType(),
5377 TheSelect->getValueType(0),
5378 LLD->getChain(), Addr, LLD->getSrcValue(),
5379 LLD->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005380 LLD->getMemoryVT(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00005381 LLD->isVolatile(),
5382 LLD->getAlignment());
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005383 }
5384 // Users of the select now use the result of the load.
5385 CombineTo(TheSelect, Load);
5386
5387 // Users of the old loads now use the new load's chain. We know the
5388 // old-load value is dead now.
Gabor Greifba36cb52008-08-28 21:40:38 +00005389 CombineTo(LHS.getNode(), Load.getValue(0), Load.getValue(1));
5390 CombineTo(RHS.getNode(), Load.getValue(0), Load.getValue(1));
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005391 return true;
5392 }
Evan Chengc5484282006-10-04 00:56:09 +00005393 }
Chris Lattner40c62d52005-10-18 06:04:22 +00005394 }
5395 }
5396
5397 return false;
5398}
5399
Dan Gohman475871a2008-07-27 21:46:04 +00005400SDValue DAGCombiner::SimplifySelectCC(SDValue N0, SDValue N1,
5401 SDValue N2, SDValue N3,
5402 ISD::CondCode CC, bool NotExtCompare) {
Nate Begemanf845b452005-10-08 00:29:44 +00005403
Duncan Sands83ec4b62008-06-06 12:08:01 +00005404 MVT VT = N2.getValueType();
Gabor Greifba36cb52008-08-28 21:40:38 +00005405 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
5406 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
5407 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00005408
5409 // Determine if the condition we're dealing with is constant
Dan Gohman475871a2008-07-27 21:46:04 +00005410 SDValue SCC = SimplifySetCC(TLI.getSetCCResultType(N0), N0, N1, CC, false);
Gabor Greifba36cb52008-08-28 21:40:38 +00005411 if (SCC.getNode()) AddToWorkList(SCC.getNode());
5412 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00005413
5414 // fold select_cc true, x, y -> x
Dan Gohman002e5d02008-03-13 22:13:53 +00005415 if (SCCC && !SCCC->isNullValue())
Nate Begemanf845b452005-10-08 00:29:44 +00005416 return N2;
5417 // fold select_cc false, x, y -> y
Dan Gohman002e5d02008-03-13 22:13:53 +00005418 if (SCCC && SCCC->isNullValue())
Nate Begemanf845b452005-10-08 00:29:44 +00005419 return N3;
5420
5421 // Check to see if we can simplify the select into an fabs node
5422 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
5423 // Allow either -0.0 or 0.0
Dale Johannesen87503a62007-08-25 22:10:57 +00005424 if (CFP->getValueAPF().isZero()) {
Nate Begemanf845b452005-10-08 00:29:44 +00005425 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
5426 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
5427 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
5428 N2 == N3.getOperand(0))
5429 return DAG.getNode(ISD::FABS, VT, N0);
5430
5431 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
5432 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
5433 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
5434 N2.getOperand(0) == N3)
5435 return DAG.getNode(ISD::FABS, VT, N3);
5436 }
5437 }
5438
5439 // Check to see if we can perform the "gzip trick", transforming
5440 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
Chris Lattnere3152e52006-09-20 06:41:35 +00005441 if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00005442 N0.getValueType().isInteger() &&
5443 N2.getValueType().isInteger() &&
Dan Gohman002e5d02008-03-13 22:13:53 +00005444 (N1C->isNullValue() || // (a < 0) ? b : 0
5445 (N1C->getAPIntValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0
Duncan Sands83ec4b62008-06-06 12:08:01 +00005446 MVT XType = N0.getValueType();
5447 MVT AType = N2.getValueType();
Duncan Sands8e4eb092008-06-08 20:54:56 +00005448 if (XType.bitsGE(AType)) {
Nate Begemanf845b452005-10-08 00:29:44 +00005449 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00005450 // single-bit constant.
Dan Gohman002e5d02008-03-13 22:13:53 +00005451 if (N2C && ((N2C->getAPIntValue() & (N2C->getAPIntValue()-1)) == 0)) {
5452 unsigned ShCtV = N2C->getAPIntValue().logBase2();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005453 ShCtV = XType.getSizeInBits()-ShCtV-1;
Dan Gohman475871a2008-07-27 21:46:04 +00005454 SDValue ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
5455 SDValue Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
Gabor Greifba36cb52008-08-28 21:40:38 +00005456 AddToWorkList(Shift.getNode());
Duncan Sands8e4eb092008-06-08 20:54:56 +00005457 if (XType.bitsGT(AType)) {
Nate Begemanf845b452005-10-08 00:29:44 +00005458 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00005459 AddToWorkList(Shift.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00005460 }
5461 return DAG.getNode(ISD::AND, AType, Shift, N2);
5462 }
Dan Gohman475871a2008-07-27 21:46:04 +00005463 SDValue Shift = DAG.getNode(ISD::SRA, XType, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005464 DAG.getConstant(XType.getSizeInBits()-1,
Nate Begemanf845b452005-10-08 00:29:44 +00005465 TLI.getShiftAmountTy()));
Gabor Greifba36cb52008-08-28 21:40:38 +00005466 AddToWorkList(Shift.getNode());
Duncan Sands8e4eb092008-06-08 20:54:56 +00005467 if (XType.bitsGT(AType)) {
Nate Begemanf845b452005-10-08 00:29:44 +00005468 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00005469 AddToWorkList(Shift.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00005470 }
5471 return DAG.getNode(ISD::AND, AType, Shift, N2);
5472 }
5473 }
Nate Begeman07ed4172005-10-10 21:26:48 +00005474
5475 // fold select C, 16, 0 -> shl C, 4
Dan Gohman002e5d02008-03-13 22:13:53 +00005476 if (N2C && N3C && N3C->isNullValue() && N2C->getAPIntValue().isPowerOf2() &&
Nate Begeman07ed4172005-10-10 21:26:48 +00005477 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) {
Chris Lattner1eba01e2007-04-11 06:50:51 +00005478
5479 // If the caller doesn't want us to simplify this into a zext of a compare,
5480 // don't do it.
Dan Gohman002e5d02008-03-13 22:13:53 +00005481 if (NotExtCompare && N2C->getAPIntValue() == 1)
Dan Gohman475871a2008-07-27 21:46:04 +00005482 return SDValue();
Chris Lattner1eba01e2007-04-11 06:50:51 +00005483
Nate Begeman07ed4172005-10-10 21:26:48 +00005484 // Get a SetCC of the condition
5485 // FIXME: Should probably make sure that setcc is legal if we ever have a
5486 // target where it isn't.
Dan Gohman475871a2008-07-27 21:46:04 +00005487 SDValue Temp, SCC;
Nate Begeman07ed4172005-10-10 21:26:48 +00005488 // cast from setcc result type to select result type
Nate Begemanb0d04a72006-02-18 02:40:58 +00005489 if (AfterLegalize) {
Scott Michel5b8f82e2008-03-10 15:42:14 +00005490 SCC = DAG.getSetCC(TLI.getSetCCResultType(N0), N0, N1, CC);
Duncan Sands8e4eb092008-06-08 20:54:56 +00005491 if (N2.getValueType().bitsLT(SCC.getValueType()))
Chris Lattner555d8d62006-12-07 22:36:47 +00005492 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
5493 else
5494 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00005495 } else {
5496 SCC = DAG.getSetCC(MVT::i1, N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00005497 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00005498 }
Gabor Greifba36cb52008-08-28 21:40:38 +00005499 AddToWorkList(SCC.getNode());
5500 AddToWorkList(Temp.getNode());
Chris Lattnerc56a81d2007-04-11 06:43:25 +00005501
Dan Gohman002e5d02008-03-13 22:13:53 +00005502 if (N2C->getAPIntValue() == 1)
Chris Lattnerc56a81d2007-04-11 06:43:25 +00005503 return Temp;
Nate Begeman07ed4172005-10-10 21:26:48 +00005504 // shl setcc result by log2 n2c
5505 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
Dan Gohman002e5d02008-03-13 22:13:53 +00005506 DAG.getConstant(N2C->getAPIntValue().logBase2(),
Nate Begeman07ed4172005-10-10 21:26:48 +00005507 TLI.getShiftAmountTy()));
5508 }
5509
Nate Begemanf845b452005-10-08 00:29:44 +00005510 // Check to see if this is the equivalent of setcc
5511 // FIXME: Turn all of these into setcc if setcc if setcc is legal
5512 // otherwise, go ahead with the folds.
Dan Gohman002e5d02008-03-13 22:13:53 +00005513 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getAPIntValue() == 1ULL)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005514 MVT XType = N0.getValueType();
Duncan Sands184a8762008-06-14 17:48:34 +00005515 if (!AfterLegalize ||
5516 TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultType(N0))) {
Dan Gohman475871a2008-07-27 21:46:04 +00005517 SDValue Res = DAG.getSetCC(TLI.getSetCCResultType(N0), N0, N1, CC);
Nate Begemanf845b452005-10-08 00:29:44 +00005518 if (Res.getValueType() != VT)
5519 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
5520 return Res;
5521 }
5522
5523 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
5524 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
Duncan Sands184a8762008-06-14 17:48:34 +00005525 (!AfterLegalize ||
5526 TLI.isOperationLegal(ISD::CTLZ, XType))) {
Dan Gohman475871a2008-07-27 21:46:04 +00005527 SDValue Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
Nate Begemanf845b452005-10-08 00:29:44 +00005528 return DAG.getNode(ISD::SRL, XType, Ctlz,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005529 DAG.getConstant(Log2_32(XType.getSizeInBits()),
Nate Begemanf845b452005-10-08 00:29:44 +00005530 TLI.getShiftAmountTy()));
5531 }
5532 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
5533 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
Dan Gohman475871a2008-07-27 21:46:04 +00005534 SDValue NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
Nate Begemanf845b452005-10-08 00:29:44 +00005535 N0);
Dan Gohman475871a2008-07-27 21:46:04 +00005536 SDValue NotN0 = DAG.getNode(ISD::XOR, XType, N0,
Nate Begemanf845b452005-10-08 00:29:44 +00005537 DAG.getConstant(~0ULL, XType));
5538 return DAG.getNode(ISD::SRL, XType,
5539 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
Duncan Sands83ec4b62008-06-06 12:08:01 +00005540 DAG.getConstant(XType.getSizeInBits()-1,
Nate Begemanf845b452005-10-08 00:29:44 +00005541 TLI.getShiftAmountTy()));
5542 }
5543 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
5544 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
Dan Gohman475871a2008-07-27 21:46:04 +00005545 SDValue Sign = DAG.getNode(ISD::SRL, XType, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005546 DAG.getConstant(XType.getSizeInBits()-1,
Nate Begemanf845b452005-10-08 00:29:44 +00005547 TLI.getShiftAmountTy()));
5548 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
5549 }
5550 }
5551
5552 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
5553 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
5554 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
Chris Lattner1982ef22007-04-11 05:11:38 +00005555 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1) &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00005556 N2.getOperand(0) == N1 && N0.getValueType().isInteger()) {
5557 MVT XType = N0.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00005558 SDValue Shift = DAG.getNode(ISD::SRA, XType, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005559 DAG.getConstant(XType.getSizeInBits()-1,
Chris Lattner1982ef22007-04-11 05:11:38 +00005560 TLI.getShiftAmountTy()));
Dan Gohman475871a2008-07-27 21:46:04 +00005561 SDValue Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00005562 AddToWorkList(Shift.getNode());
5563 AddToWorkList(Add.getNode());
Chris Lattner1982ef22007-04-11 05:11:38 +00005564 return DAG.getNode(ISD::XOR, XType, Add, Shift);
5565 }
5566 // Check to see if this is an integer abs. select_cc setgt X, -1, X, -X ->
5567 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
5568 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT &&
5569 N0 == N2 && N3.getOpcode() == ISD::SUB && N0 == N3.getOperand(1)) {
5570 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N3.getOperand(0))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005571 MVT XType = N0.getValueType();
5572 if (SubC->isNullValue() && XType.isInteger()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005573 SDValue Shift = DAG.getNode(ISD::SRA, XType, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005574 DAG.getConstant(XType.getSizeInBits()-1,
Chris Lattner1982ef22007-04-11 05:11:38 +00005575 TLI.getShiftAmountTy()));
Dan Gohman475871a2008-07-27 21:46:04 +00005576 SDValue Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00005577 AddToWorkList(Shift.getNode());
5578 AddToWorkList(Add.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00005579 return DAG.getNode(ISD::XOR, XType, Add, Shift);
5580 }
5581 }
5582 }
Chris Lattner1982ef22007-04-11 05:11:38 +00005583
Dan Gohman475871a2008-07-27 21:46:04 +00005584 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00005585}
5586
Evan Chengfa1eb272007-02-08 22:13:59 +00005587/// SimplifySetCC - This is a stub for TargetLowering::SimplifySetCC.
Dan Gohman475871a2008-07-27 21:46:04 +00005588SDValue DAGCombiner::SimplifySetCC(MVT VT, SDValue N0,
5589 SDValue N1, ISD::CondCode Cond,
5590 bool foldBooleans) {
Evan Chengfa1eb272007-02-08 22:13:59 +00005591 TargetLowering::DAGCombinerInfo
5592 DagCombineInfo(DAG, !AfterLegalize, false, this);
5593 return TLI.SimplifySetCC(VT, N0, N1, Cond, foldBooleans, DagCombineInfo);
Nate Begeman452d7beb2005-09-16 00:54:12 +00005594}
5595
Nate Begeman69575232005-10-20 02:15:44 +00005596/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
5597/// return a DAG expression to select that will generate the same value by
5598/// multiplying by a magic number. See:
5599/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Dan Gohman475871a2008-07-27 21:46:04 +00005600SDValue DAGCombiner::BuildSDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00005601 std::vector<SDNode*> Built;
Dan Gohman475871a2008-07-27 21:46:04 +00005602 SDValue S = TLI.BuildSDIV(N, DAG, &Built);
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005603
Andrew Lenharth232c9102006-06-12 16:07:18 +00005604 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005605 ii != ee; ++ii)
5606 AddToWorkList(*ii);
5607 return S;
Nate Begeman69575232005-10-20 02:15:44 +00005608}
5609
5610/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
5611/// return a DAG expression to select that will generate the same value by
5612/// multiplying by a magic number. See:
5613/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Dan Gohman475871a2008-07-27 21:46:04 +00005614SDValue DAGCombiner::BuildUDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00005615 std::vector<SDNode*> Built;
Dan Gohman475871a2008-07-27 21:46:04 +00005616 SDValue S = TLI.BuildUDIV(N, DAG, &Built);
Nate Begeman69575232005-10-20 02:15:44 +00005617
Andrew Lenharth232c9102006-06-12 16:07:18 +00005618 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005619 ii != ee; ++ii)
5620 AddToWorkList(*ii);
5621 return S;
Nate Begeman69575232005-10-20 02:15:44 +00005622}
5623
Jim Laskey71382342006-10-07 23:37:56 +00005624/// FindBaseOffset - Return true if base is known not to alias with anything
5625/// but itself. Provides base object and offset as results.
Dan Gohman475871a2008-07-27 21:46:04 +00005626static bool FindBaseOffset(SDValue Ptr, SDValue &Base, int64_t &Offset) {
Jim Laskey71382342006-10-07 23:37:56 +00005627 // Assume it is a primitive operation.
5628 Base = Ptr; Offset = 0;
5629
5630 // If it's an adding a simple constant then integrate the offset.
5631 if (Base.getOpcode() == ISD::ADD) {
5632 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Base.getOperand(1))) {
5633 Base = Base.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005634 Offset += C->getZExtValue();
Jim Laskey71382342006-10-07 23:37:56 +00005635 }
5636 }
5637
5638 // If it's any of the following then it can't alias with anything but itself.
5639 return isa<FrameIndexSDNode>(Base) ||
5640 isa<ConstantPoolSDNode>(Base) ||
5641 isa<GlobalAddressSDNode>(Base);
5642}
5643
5644/// isAlias - Return true if there is any possibility that the two addresses
5645/// overlap.
Dan Gohman475871a2008-07-27 21:46:04 +00005646bool DAGCombiner::isAlias(SDValue Ptr1, int64_t Size1,
Jim Laskey096c22e2006-10-18 12:29:57 +00005647 const Value *SrcValue1, int SrcValueOffset1,
Dan Gohman475871a2008-07-27 21:46:04 +00005648 SDValue Ptr2, int64_t Size2,
Jim Laskey096c22e2006-10-18 12:29:57 +00005649 const Value *SrcValue2, int SrcValueOffset2)
5650{
Jim Laskey71382342006-10-07 23:37:56 +00005651 // If they are the same then they must be aliases.
5652 if (Ptr1 == Ptr2) return true;
5653
5654 // Gather base node and offset information.
Dan Gohman475871a2008-07-27 21:46:04 +00005655 SDValue Base1, Base2;
Jim Laskey71382342006-10-07 23:37:56 +00005656 int64_t Offset1, Offset2;
5657 bool KnownBase1 = FindBaseOffset(Ptr1, Base1, Offset1);
5658 bool KnownBase2 = FindBaseOffset(Ptr2, Base2, Offset2);
5659
5660 // If they have a same base address then...
5661 if (Base1 == Base2) {
5662 // Check to see if the addresses overlap.
5663 return!((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
5664 }
5665
Jim Laskey096c22e2006-10-18 12:29:57 +00005666 // If we know both bases then they can't alias.
5667 if (KnownBase1 && KnownBase2) return false;
5668
Jim Laskey07a27092006-10-18 19:08:31 +00005669 if (CombinerGlobalAA) {
5670 // Use alias analysis information.
Dan Gohmane9c8fa02007-08-27 16:32:11 +00005671 int64_t MinOffset = std::min(SrcValueOffset1, SrcValueOffset2);
5672 int64_t Overlap1 = Size1 + SrcValueOffset1 - MinOffset;
5673 int64_t Overlap2 = Size2 + SrcValueOffset2 - MinOffset;
Jim Laskey07a27092006-10-18 19:08:31 +00005674 AliasAnalysis::AliasResult AAResult =
Jim Laskey096c22e2006-10-18 12:29:57 +00005675 AA.alias(SrcValue1, Overlap1, SrcValue2, Overlap2);
Jim Laskey07a27092006-10-18 19:08:31 +00005676 if (AAResult == AliasAnalysis::NoAlias)
5677 return false;
5678 }
Jim Laskey096c22e2006-10-18 12:29:57 +00005679
5680 // Otherwise we have to assume they alias.
5681 return true;
Jim Laskey71382342006-10-07 23:37:56 +00005682}
5683
5684/// FindAliasInfo - Extracts the relevant alias information from the memory
5685/// node. Returns true if the operand was a load.
Jim Laskey7ca56af2006-10-11 13:47:09 +00005686bool DAGCombiner::FindAliasInfo(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +00005687 SDValue &Ptr, int64_t &Size,
Jim Laskey096c22e2006-10-18 12:29:57 +00005688 const Value *&SrcValue, int &SrcValueOffset) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00005689 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
5690 Ptr = LD->getBasePtr();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005691 Size = LD->getMemoryVT().getSizeInBits() >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005692 SrcValue = LD->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00005693 SrcValueOffset = LD->getSrcValueOffset();
Jim Laskey71382342006-10-07 23:37:56 +00005694 return true;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005695 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00005696 Ptr = ST->getBasePtr();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005697 Size = ST->getMemoryVT().getSizeInBits() >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005698 SrcValue = ST->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00005699 SrcValueOffset = ST->getSrcValueOffset();
Jim Laskey7ca56af2006-10-11 13:47:09 +00005700 } else {
Jim Laskey71382342006-10-07 23:37:56 +00005701 assert(0 && "FindAliasInfo expected a memory operand");
Jim Laskey71382342006-10-07 23:37:56 +00005702 }
5703
5704 return false;
5705}
5706
Jim Laskey6ff23e52006-10-04 16:53:27 +00005707/// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
5708/// looking for aliasing nodes and adding them to the Aliases vector.
Dan Gohman475871a2008-07-27 21:46:04 +00005709void DAGCombiner::GatherAllAliases(SDNode *N, SDValue OriginalChain,
5710 SmallVector<SDValue, 8> &Aliases) {
5711 SmallVector<SDValue, 8> Chains; // List of chains to visit.
Jim Laskey6ff23e52006-10-04 16:53:27 +00005712 std::set<SDNode *> Visited; // Visited node set.
5713
Jim Laskey279f0532006-09-25 16:29:54 +00005714 // Get alias information for node.
Dan Gohman475871a2008-07-27 21:46:04 +00005715 SDValue Ptr;
Jim Laskey279f0532006-09-25 16:29:54 +00005716 int64_t Size;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005717 const Value *SrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00005718 int SrcValueOffset;
5719 bool IsLoad = FindAliasInfo(N, Ptr, Size, SrcValue, SrcValueOffset);
Jim Laskey279f0532006-09-25 16:29:54 +00005720
Jim Laskey6ff23e52006-10-04 16:53:27 +00005721 // Starting off.
Jim Laskeybc588b82006-10-05 15:07:25 +00005722 Chains.push_back(OriginalChain);
Jim Laskey6ff23e52006-10-04 16:53:27 +00005723
Jim Laskeybc588b82006-10-05 15:07:25 +00005724 // Look at each chain and determine if it is an alias. If so, add it to the
5725 // aliases list. If not, then continue up the chain looking for the next
5726 // candidate.
5727 while (!Chains.empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005728 SDValue Chain = Chains.back();
Jim Laskeybc588b82006-10-05 15:07:25 +00005729 Chains.pop_back();
Jim Laskey6ff23e52006-10-04 16:53:27 +00005730
Jim Laskeybc588b82006-10-05 15:07:25 +00005731 // Don't bother if we've been before.
Gabor Greifba36cb52008-08-28 21:40:38 +00005732 if (Visited.find(Chain.getNode()) != Visited.end()) continue;
5733 Visited.insert(Chain.getNode());
Jim Laskeybc588b82006-10-05 15:07:25 +00005734
5735 switch (Chain.getOpcode()) {
5736 case ISD::EntryToken:
5737 // Entry token is ideal chain operand, but handled in FindBetterChain.
5738 break;
Jim Laskey6ff23e52006-10-04 16:53:27 +00005739
Jim Laskeybc588b82006-10-05 15:07:25 +00005740 case ISD::LOAD:
5741 case ISD::STORE: {
5742 // Get alias information for Chain.
Dan Gohman475871a2008-07-27 21:46:04 +00005743 SDValue OpPtr;
Jim Laskeybc588b82006-10-05 15:07:25 +00005744 int64_t OpSize;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005745 const Value *OpSrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00005746 int OpSrcValueOffset;
Gabor Greifba36cb52008-08-28 21:40:38 +00005747 bool IsOpLoad = FindAliasInfo(Chain.getNode(), OpPtr, OpSize,
Jim Laskey096c22e2006-10-18 12:29:57 +00005748 OpSrcValue, OpSrcValueOffset);
Jim Laskeybc588b82006-10-05 15:07:25 +00005749
5750 // If chain is alias then stop here.
5751 if (!(IsLoad && IsOpLoad) &&
Jim Laskey096c22e2006-10-18 12:29:57 +00005752 isAlias(Ptr, Size, SrcValue, SrcValueOffset,
5753 OpPtr, OpSize, OpSrcValue, OpSrcValueOffset)) {
Jim Laskeybc588b82006-10-05 15:07:25 +00005754 Aliases.push_back(Chain);
5755 } else {
5756 // Look further up the chain.
5757 Chains.push_back(Chain.getOperand(0));
5758 // Clean up old chain.
Gabor Greifba36cb52008-08-28 21:40:38 +00005759 AddToWorkList(Chain.getNode());
Jim Laskey279f0532006-09-25 16:29:54 +00005760 }
Jim Laskeybc588b82006-10-05 15:07:25 +00005761 break;
5762 }
5763
5764 case ISD::TokenFactor:
5765 // We have to check each of the operands of the token factor, so we queue
5766 // then up. Adding the operands to the queue (stack) in reverse order
5767 // maintains the original order and increases the likelihood that getNode
5768 // will find a matching token factor (CSE.)
5769 for (unsigned n = Chain.getNumOperands(); n;)
5770 Chains.push_back(Chain.getOperand(--n));
5771 // Eliminate the token factor if we can.
Gabor Greifba36cb52008-08-28 21:40:38 +00005772 AddToWorkList(Chain.getNode());
Jim Laskeybc588b82006-10-05 15:07:25 +00005773 break;
5774
5775 default:
5776 // For all other instructions we will just have to take what we can get.
5777 Aliases.push_back(Chain);
5778 break;
Jim Laskey279f0532006-09-25 16:29:54 +00005779 }
5780 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00005781}
5782
5783/// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, looking
5784/// for a better chain (aliasing node.)
Dan Gohman475871a2008-07-27 21:46:04 +00005785SDValue DAGCombiner::FindBetterChain(SDNode *N, SDValue OldChain) {
5786 SmallVector<SDValue, 8> Aliases; // Ops for replacing token factor.
Jim Laskey279f0532006-09-25 16:29:54 +00005787
Jim Laskey6ff23e52006-10-04 16:53:27 +00005788 // Accumulate all the aliases to this node.
5789 GatherAllAliases(N, OldChain, Aliases);
5790
5791 if (Aliases.size() == 0) {
5792 // If no operands then chain to entry token.
5793 return DAG.getEntryNode();
5794 } else if (Aliases.size() == 1) {
5795 // If a single operand then chain to it. We don't need to revisit it.
5796 return Aliases[0];
5797 }
5798
5799 // Construct a custom tailored token factor.
Dan Gohman475871a2008-07-27 21:46:04 +00005800 SDValue NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
Jim Laskey6ff23e52006-10-04 16:53:27 +00005801 &Aliases[0], Aliases.size());
5802
5803 // Make sure the old chain gets cleaned up.
Gabor Greifba36cb52008-08-28 21:40:38 +00005804 if (NewChain != OldChain) AddToWorkList(OldChain.getNode());
Jim Laskey6ff23e52006-10-04 16:53:27 +00005805
5806 return NewChain;
Jim Laskey279f0532006-09-25 16:29:54 +00005807}
5808
Nate Begeman1d4d4142005-09-01 00:19:25 +00005809// SelectionDAG::Combine - This is the entry point for the file.
5810//
Dan Gohmana2676512008-08-20 16:30:28 +00005811void SelectionDAG::Combine(bool RunningAfterLegalize, AliasAnalysis &AA,
5812 bool Fast) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00005813 /// run - This is the main entry point to this class.
5814 ///
Dan Gohmana2676512008-08-20 16:30:28 +00005815 DAGCombiner(*this, AA, Fast).Run(RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +00005816}