blob: e2aad48a2c3ccb895377cdfe9dfc037fb862cf4d [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- DAGCombiner.cpp - Implement a DAG node combiner -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +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.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000012//
13//===----------------------------------------------------------------------===//
14
15#define DEBUG_TYPE "dagcombine"
16#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattner4e137af2008-01-25 07:20:16 +000017#include "llvm/CodeGen/MachineFunction.h"
18#include "llvm/CodeGen/MachineFrameInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000019#include "llvm/Analysis/AliasAnalysis.h"
20#include "llvm/Target/TargetData.h"
Chris Lattner1e3362f2008-01-26 19:45:50 +000021#include "llvm/Target/TargetFrameInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000022#include "llvm/Target/TargetLowering.h"
23#include "llvm/Target/TargetMachine.h"
24#include "llvm/Target/TargetOptions.h"
25#include "llvm/ADT/SmallPtrSet.h"
26#include "llvm/ADT/Statistic.h"
27#include "llvm/Support/Compiler.h"
28#include "llvm/Support/CommandLine.h"
29#include "llvm/Support/Debug.h"
30#include "llvm/Support/MathExtras.h"
31#include <algorithm>
Dan Gohmand408d392008-05-23 20:40:06 +000032#include <set>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000033using namespace llvm;
34
35STATISTIC(NodesCombined , "Number of dag nodes combined");
36STATISTIC(PreIndexedNodes , "Number of pre-indexed nodes created");
37STATISTIC(PostIndexedNodes, "Number of post-indexed nodes created");
38
39namespace {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000040 static cl::opt<bool>
41 CombinerAA("combiner-alias-analysis", cl::Hidden,
42 cl::desc("Turn on alias analysis during testing"));
43
44 static cl::opt<bool>
45 CombinerGlobalAA("combiner-global-alias-analysis", cl::Hidden,
46 cl::desc("Include global information in alias analysis"));
47
48//------------------------------ DAGCombiner ---------------------------------//
49
50 class VISIBILITY_HIDDEN DAGCombiner {
51 SelectionDAG &DAG;
52 TargetLowering &TLI;
53 bool AfterLegalize;
Dan Gohmanea12c0c2008-08-20 16:30:28 +000054 bool Fast;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000055
56 // Worklist of all of the nodes that need to be simplified.
Evan Cheng56550ae2008-08-29 22:21:44 +000057 std::vector<SDNode*> WorkList;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000058
59 // AA - Used for DAG load/store alias analysis.
60 AliasAnalysis &AA;
61
62 /// 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();
68 UI != UE; ++UI)
Dan Gohman0c97f1d2008-07-27 20:43:25 +000069 AddToWorkList(*UI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000070 }
71
Dan Gohman6c89ea72007-10-08 17:57:15 +000072 /// visit - call the node-specific routine that knows how to fold each
73 /// particular type of node.
Dan Gohman8181bd12008-07-27 21:46:04 +000074 SDValue visit(SDNode *N);
Dan Gohman6c89ea72007-10-08 17:57:15 +000075
Dan Gohmanf17a25c2007-07-18 16:29:46 +000076 public:
77 /// AddToWorkList - Add to the work list making sure it's instance is at the
78 /// the back (next to be processed.)
79 void AddToWorkList(SDNode *N) {
80 removeFromWorkList(N);
81 WorkList.push_back(N);
82 }
83
Chris Lattner7bcb18f2008-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());
Dan Gohmanf17a25c2007-07-18 16:29:46 +000089 }
90
Dan Gohman8181bd12008-07-27 21:46:04 +000091 SDValue CombineTo(SDNode *N, const SDValue *To, unsigned NumTo,
Chris Lattner7bcb18f2008-02-03 06:49:24 +000092 bool AddTo = true);
93
Dan Gohman8181bd12008-07-27 21:46:04 +000094 SDValue CombineTo(SDNode *N, SDValue Res, bool AddTo = true) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000095 return CombineTo(N, &Res, 1, AddTo);
96 }
97
Dan Gohman8181bd12008-07-27 21:46:04 +000098 SDValue CombineTo(SDNode *N, SDValue Res0, SDValue Res1,
Dan Gohmanf17a25c2007-07-18 16:29:46 +000099 bool AddTo = true) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000100 SDValue To[] = { Res0, Res1 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000101 return CombineTo(N, To, 2, AddTo);
102 }
Chris Lattner5872a362008-01-17 07:00:52 +0000103
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000104 private:
105
106 /// SimplifyDemandedBits - Check the specified integer node value to see if
107 /// it can be simplified or if things it uses can be simplified by bit
108 /// propagation. If so, return true.
Dan Gohman8181bd12008-07-27 21:46:04 +0000109 bool SimplifyDemandedBits(SDValue Op) {
Dan Gohman11607792008-02-27 00:25:32 +0000110 APInt Demanded = APInt::getAllOnesValue(Op.getValueSizeInBits());
111 return SimplifyDemandedBits(Op, Demanded);
112 }
113
Dan Gohman8181bd12008-07-27 21:46:04 +0000114 bool SimplifyDemandedBits(SDValue Op, const APInt &Demanded);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000115
116 bool CombineToPreIndexedLoadStore(SDNode *N);
117 bool CombineToPostIndexedLoadStore(SDNode *N);
118
119
Dan Gohman6c89ea72007-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 Gohman8181bd12008-07-27 21:46:04 +0000123 SDValue combine(SDNode *N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000124
125 // Visitation implementation - Implement dag node combining for different
126 // node types. The semantics are as follows:
127 // Return Value:
Evan Cheng56550ae2008-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.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000131 //
Dan Gohman8181bd12008-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 Wendlingea8b7c92008-11-21 02:12:42 +0000193 SDValue visitSADDO(SDNode *N);
194 SDValue visitUADDO(SDNode *N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000195
Dan Gohman8181bd12008-07-27 21:46:04 +0000196 SDValue XformToShuffleWithZero(SDNode *N);
197 SDValue ReassociateOps(unsigned Opc, SDValue LHS, SDValue RHS);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000198
Dan Gohman8181bd12008-07-27 21:46:04 +0000199 SDValue visitShiftByConstant(SDNode *N, unsigned Amt);
Chris Lattner91ed3c32007-12-06 07:33:36 +0000200
Dan Gohman8181bd12008-07-27 21:46:04 +0000201 bool SimplifySelectOps(SDNode *SELECT, SDValue LHS, SDValue RHS);
202 SDValue SimplifyBinOpWithSameOpcodeHands(SDNode *N);
203 SDValue SimplifySelect(SDValue N0, SDValue N1, SDValue N2);
204 SDValue SimplifySelectCC(SDValue N0, SDValue N1, SDValue N2,
205 SDValue N3, ISD::CondCode CC,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000206 bool NotExtCompare = false);
Dan Gohman8181bd12008-07-27 21:46:04 +0000207 SDValue SimplifySetCC(MVT VT, SDValue N0, SDValue N1,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000208 ISD::CondCode Cond, bool foldBooleans = true);
Dan Gohman8181bd12008-07-27 21:46:04 +0000209 SDValue SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
Chris Lattner4a7c8452008-01-26 01:09:19 +0000210 unsigned HiOp);
Dan Gohman8181bd12008-07-27 21:46:04 +0000211 SDValue CombineConsecutiveLoads(SDNode *N, MVT VT);
212 SDValue ConstantFoldBIT_CONVERTofBUILD_VECTOR(SDNode *, MVT);
213 SDValue BuildSDIV(SDNode *N);
214 SDValue BuildUDIV(SDNode *N);
215 SDNode *MatchRotate(SDValue LHS, SDValue RHS);
216 SDValue ReduceLoadWidth(SDNode *N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000217
Dan Gohman8181bd12008-07-27 21:46:04 +0000218 SDValue GetDemandedBits(SDValue V, const APInt &Mask);
Chris Lattnere8671c52007-10-13 06:35:54 +0000219
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000220 /// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
221 /// looking for aliasing nodes and adding them to the Aliases vector.
Dan Gohman8181bd12008-07-27 21:46:04 +0000222 void GatherAllAliases(SDNode *N, SDValue OriginalChain,
223 SmallVector<SDValue, 8> &Aliases);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000224
225 /// isAlias - Return true if there is any possibility that the two addresses
226 /// overlap.
Dan Gohman8181bd12008-07-27 21:46:04 +0000227 bool isAlias(SDValue Ptr1, int64_t Size1,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000228 const Value *SrcValue1, int SrcValueOffset1,
Dan Gohman8181bd12008-07-27 21:46:04 +0000229 SDValue Ptr2, int64_t Size2,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000230 const Value *SrcValue2, int SrcValueOffset2);
231
232 /// FindAliasInfo - Extracts the relevant alias information from the memory
233 /// node. Returns true if the operand was a load.
234 bool FindAliasInfo(SDNode *N,
Dan Gohman8181bd12008-07-27 21:46:04 +0000235 SDValue &Ptr, int64_t &Size,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000236 const Value *&SrcValue, int &SrcValueOffset);
237
238 /// FindBetterChain - Walk up chain skipping non-aliasing memory nodes,
239 /// looking for a better chain (aliasing node.)
Dan Gohman8181bd12008-07-27 21:46:04 +0000240 SDValue FindBetterChain(SDNode *N, SDValue Chain);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000241
242public:
Dan Gohmanea12c0c2008-08-20 16:30:28 +0000243 DAGCombiner(SelectionDAG &D, AliasAnalysis &A, bool fast)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000244 : DAG(D),
245 TLI(D.getTargetLoweringInfo()),
246 AfterLegalize(false),
Dan Gohmanea12c0c2008-08-20 16:30:28 +0000247 Fast(fast),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000248 AA(A) {}
249
250 /// Run - runs the dag combiner on all nodes in the work list
251 void Run(bool RunningAfterLegalize);
252 };
253}
254
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000255
256namespace {
257/// WorkListRemover - This class is a DAGUpdateListener that removes any deleted
258/// nodes from the worklist.
259class VISIBILITY_HIDDEN WorkListRemover :
260 public SelectionDAG::DAGUpdateListener {
261 DAGCombiner &DC;
262public:
Dan Gohmana789bff2008-02-20 16:44:09 +0000263 explicit WorkListRemover(DAGCombiner &dc) : DC(dc) {}
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000264
Duncan Sands3866b1c2008-06-11 11:42:12 +0000265 virtual void NodeDeleted(SDNode *N, SDNode *E) {
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000266 DC.removeFromWorkList(N);
267 }
268
269 virtual void NodeUpdated(SDNode *N) {
270 // Ignore updates.
271 }
272};
273}
274
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000275//===----------------------------------------------------------------------===//
276// TargetLowering::DAGCombinerInfo implementation
277//===----------------------------------------------------------------------===//
278
279void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) {
280 ((DAGCombiner*)DC)->AddToWorkList(N);
281}
282
Dan Gohman8181bd12008-07-27 21:46:04 +0000283SDValue TargetLowering::DAGCombinerInfo::
284CombineTo(SDNode *N, const std::vector<SDValue> &To) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000285 return ((DAGCombiner*)DC)->CombineTo(N, &To[0], To.size());
286}
287
Dan Gohman8181bd12008-07-27 21:46:04 +0000288SDValue TargetLowering::DAGCombinerInfo::
289CombineTo(SDNode *N, SDValue Res) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000290 return ((DAGCombiner*)DC)->CombineTo(N, Res);
291}
292
293
Dan Gohman8181bd12008-07-27 21:46:04 +0000294SDValue TargetLowering::DAGCombinerInfo::
295CombineTo(SDNode *N, SDValue Res0, SDValue Res1) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000296 return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1);
297}
298
299
300//===----------------------------------------------------------------------===//
301// Helper Functions
302//===----------------------------------------------------------------------===//
303
304/// isNegatibleForFree - Return 1 if we can compute the negated form of the
305/// specified expression for the same cost as the expression itself, or 2 if we
306/// can compute the negated form more cheaply than the expression itself.
Dan Gohman8181bd12008-07-27 21:46:04 +0000307static char isNegatibleForFree(SDValue Op, bool AfterLegalize,
Chris Lattnere0992b82008-02-26 07:04:54 +0000308 unsigned Depth = 0) {
Dale Johannesenb89072e2007-10-16 23:38:29 +0000309 // No compile time optimizations on this type.
310 if (Op.getValueType() == MVT::ppcf128)
311 return 0;
312
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000313 // fneg is removable even if it has multiple uses.
314 if (Op.getOpcode() == ISD::FNEG) return 2;
315
316 // Don't allow anything with multiple uses.
317 if (!Op.hasOneUse()) return 0;
318
319 // Don't recurse exponentially.
320 if (Depth > 6) return 0;
321
322 switch (Op.getOpcode()) {
323 default: return false;
324 case ISD::ConstantFP:
Chris Lattnere0992b82008-02-26 07:04:54 +0000325 // Don't invert constant FP values after legalize. The negated constant
326 // isn't necessarily legal.
327 return AfterLegalize ? 0 : 1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000328 case ISD::FADD:
329 // FIXME: determine better conditions for this xform.
330 if (!UnsafeFPMath) return 0;
331
332 // -(A+B) -> -A - B
Chris Lattnere0992b82008-02-26 07:04:54 +0000333 if (char V = isNegatibleForFree(Op.getOperand(0), AfterLegalize, Depth+1))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000334 return V;
335 // -(A+B) -> -B - A
Chris Lattnere0992b82008-02-26 07:04:54 +0000336 return isNegatibleForFree(Op.getOperand(1), AfterLegalize, Depth+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000337 case ISD::FSUB:
338 // We can't turn -(A-B) into B-A when we honor signed zeros.
339 if (!UnsafeFPMath) return 0;
340
341 // -(A-B) -> B-A
342 return 1;
343
344 case ISD::FMUL:
345 case ISD::FDIV:
346 if (HonorSignDependentRoundingFPMath()) return 0;
347
348 // -(X*Y) -> (-X * Y) or (X*-Y)
Chris Lattnere0992b82008-02-26 07:04:54 +0000349 if (char V = isNegatibleForFree(Op.getOperand(0), AfterLegalize, Depth+1))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000350 return V;
351
Chris Lattnere0992b82008-02-26 07:04:54 +0000352 return isNegatibleForFree(Op.getOperand(1), AfterLegalize, Depth+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000353
354 case ISD::FP_EXTEND:
355 case ISD::FP_ROUND:
356 case ISD::FSIN:
Chris Lattnere0992b82008-02-26 07:04:54 +0000357 return isNegatibleForFree(Op.getOperand(0), AfterLegalize, Depth+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000358 }
359}
360
361/// GetNegatedExpression - If isNegatibleForFree returns true, this function
362/// returns the newly negated expression.
Dan Gohman8181bd12008-07-27 21:46:04 +0000363static SDValue GetNegatedExpression(SDValue Op, SelectionDAG &DAG,
Chris Lattnere0992b82008-02-26 07:04:54 +0000364 bool AfterLegalize, unsigned Depth = 0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000365 // fneg is removable even if it has multiple uses.
366 if (Op.getOpcode() == ISD::FNEG) return Op.getOperand(0);
367
368 // Don't allow anything with multiple uses.
369 assert(Op.hasOneUse() && "Unknown reuse!");
370
371 assert(Depth <= 6 && "GetNegatedExpression doesn't match isNegatibleForFree");
372 switch (Op.getOpcode()) {
373 default: assert(0 && "Unknown code");
Dale Johannesen7604c1b2007-08-31 23:34:27 +0000374 case ISD::ConstantFP: {
375 APFloat V = cast<ConstantFPSDNode>(Op)->getValueAPF();
376 V.changeSign();
377 return DAG.getConstantFP(V, Op.getValueType());
378 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000379 case ISD::FADD:
380 // FIXME: determine better conditions for this xform.
381 assert(UnsafeFPMath);
382
383 // -(A+B) -> -A - B
Chris Lattnere0992b82008-02-26 07:04:54 +0000384 if (isNegatibleForFree(Op.getOperand(0), AfterLegalize, Depth+1))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000385 return DAG.getNode(ISD::FSUB, Op.getValueType(),
Chris Lattnere0992b82008-02-26 07:04:54 +0000386 GetNegatedExpression(Op.getOperand(0), DAG,
387 AfterLegalize, Depth+1),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000388 Op.getOperand(1));
389 // -(A+B) -> -B - A
390 return DAG.getNode(ISD::FSUB, Op.getValueType(),
Chris Lattnere0992b82008-02-26 07:04:54 +0000391 GetNegatedExpression(Op.getOperand(1), DAG,
392 AfterLegalize, Depth+1),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000393 Op.getOperand(0));
394 case ISD::FSUB:
395 // We can't turn -(A-B) into B-A when we honor signed zeros.
396 assert(UnsafeFPMath);
397
398 // -(0-B) -> B
399 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(Op.getOperand(0)))
Dale Johannesen7604c1b2007-08-31 23:34:27 +0000400 if (N0CFP->getValueAPF().isZero())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000401 return Op.getOperand(1);
402
403 // -(A-B) -> B-A
404 return DAG.getNode(ISD::FSUB, Op.getValueType(), Op.getOperand(1),
405 Op.getOperand(0));
406
407 case ISD::FMUL:
408 case ISD::FDIV:
409 assert(!HonorSignDependentRoundingFPMath());
410
411 // -(X*Y) -> -X * Y
Chris Lattner46360032008-02-26 17:09:59 +0000412 if (isNegatibleForFree(Op.getOperand(0), AfterLegalize, Depth+1))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000413 return DAG.getNode(Op.getOpcode(), Op.getValueType(),
Chris Lattnere0992b82008-02-26 07:04:54 +0000414 GetNegatedExpression(Op.getOperand(0), DAG,
415 AfterLegalize, Depth+1),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000416 Op.getOperand(1));
417
418 // -(X*Y) -> X * -Y
419 return DAG.getNode(Op.getOpcode(), Op.getValueType(),
420 Op.getOperand(0),
Chris Lattnere0992b82008-02-26 07:04:54 +0000421 GetNegatedExpression(Op.getOperand(1), DAG,
422 AfterLegalize, Depth+1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000423
424 case ISD::FP_EXTEND:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000425 case ISD::FSIN:
426 return DAG.getNode(Op.getOpcode(), Op.getValueType(),
Chris Lattnere0992b82008-02-26 07:04:54 +0000427 GetNegatedExpression(Op.getOperand(0), DAG,
428 AfterLegalize, Depth+1));
Chris Lattner5872a362008-01-17 07:00:52 +0000429 case ISD::FP_ROUND:
430 return DAG.getNode(ISD::FP_ROUND, Op.getValueType(),
Chris Lattnere0992b82008-02-26 07:04:54 +0000431 GetNegatedExpression(Op.getOperand(0), DAG,
432 AfterLegalize, Depth+1),
Chris Lattner5872a362008-01-17 07:00:52 +0000433 Op.getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000434 }
435}
436
437
438// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
439// that selects between the values 1 and 0, making it equivalent to a setcc.
440// Also, set the incoming LHS, RHS, and CC references to the appropriate
441// nodes based on the type of node we are checking. This simplifies life a
442// bit for the callers.
Dan Gohman8181bd12008-07-27 21:46:04 +0000443static bool isSetCCEquivalent(SDValue N, SDValue &LHS, SDValue &RHS,
444 SDValue &CC) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000445 if (N.getOpcode() == ISD::SETCC) {
446 LHS = N.getOperand(0);
447 RHS = N.getOperand(1);
448 CC = N.getOperand(2);
449 return true;
450 }
451 if (N.getOpcode() == ISD::SELECT_CC &&
452 N.getOperand(2).getOpcode() == ISD::Constant &&
453 N.getOperand(3).getOpcode() == ISD::Constant &&
Dan Gohman9d24dc72008-03-13 22:13:53 +0000454 cast<ConstantSDNode>(N.getOperand(2))->getAPIntValue() == 1 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000455 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
456 LHS = N.getOperand(0);
457 RHS = N.getOperand(1);
458 CC = N.getOperand(4);
459 return true;
460 }
461 return false;
462}
463
464// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
465// one use. If this is true, it allows the users to invert the operation for
466// free when it is profitable to do so.
Dan Gohman8181bd12008-07-27 21:46:04 +0000467static bool isOneUseSetCC(SDValue N) {
468 SDValue N0, N1, N2;
Gabor Greif1c80d112008-08-28 21:40:38 +0000469 if (isSetCCEquivalent(N, N0, N1, N2) && N.getNode()->hasOneUse())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000470 return true;
471 return false;
472}
473
Dan Gohman8181bd12008-07-27 21:46:04 +0000474SDValue DAGCombiner::ReassociateOps(unsigned Opc, SDValue N0, SDValue N1){
Duncan Sands92c43912008-06-06 12:08:01 +0000475 MVT VT = N0.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000476 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
477 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
478 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
479 if (isa<ConstantSDNode>(N1)) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000480 SDValue OpNode = DAG.getNode(Opc, VT, N0.getOperand(1), N1);
Gabor Greif1c80d112008-08-28 21:40:38 +0000481 AddToWorkList(OpNode.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000482 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(0));
483 } else if (N0.hasOneUse()) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000484 SDValue OpNode = DAG.getNode(Opc, VT, N0.getOperand(0), N1);
Gabor Greif1c80d112008-08-28 21:40:38 +0000485 AddToWorkList(OpNode.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000486 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(1));
487 }
488 }
489 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
490 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
491 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
492 if (isa<ConstantSDNode>(N0)) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000493 SDValue OpNode = DAG.getNode(Opc, VT, N1.getOperand(1), N0);
Gabor Greif1c80d112008-08-28 21:40:38 +0000494 AddToWorkList(OpNode.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000495 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(0));
496 } else if (N1.hasOneUse()) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000497 SDValue OpNode = DAG.getNode(Opc, VT, N1.getOperand(0), N0);
Gabor Greif1c80d112008-08-28 21:40:38 +0000498 AddToWorkList(OpNode.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000499 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(1));
500 }
501 }
Dan Gohman8181bd12008-07-27 21:46:04 +0000502 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000503}
504
Dan Gohman8181bd12008-07-27 21:46:04 +0000505SDValue DAGCombiner::CombineTo(SDNode *N, const SDValue *To, unsigned NumTo,
506 bool AddTo) {
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000507 assert(N->getNumValues() == NumTo && "Broken CombineTo call!");
508 ++NodesCombined;
509 DOUT << "\nReplacing.1 "; DEBUG(N->dump(&DAG));
Gabor Greif1c80d112008-08-28 21:40:38 +0000510 DOUT << "\nWith: "; DEBUG(To[0].getNode()->dump(&DAG));
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000511 DOUT << " and " << NumTo-1 << " other values\n";
512 WorkListRemover DeadNodes(*this);
513 DAG.ReplaceAllUsesWith(N, To, &DeadNodes);
514
515 if (AddTo) {
516 // Push the new nodes and any users onto the worklist
517 for (unsigned i = 0, e = NumTo; i != e; ++i) {
Gabor Greif1c80d112008-08-28 21:40:38 +0000518 AddToWorkList(To[i].getNode());
519 AddUsersToWorkList(To[i].getNode());
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000520 }
521 }
522
523 // Nodes can be reintroduced into the worklist. Make sure we do not
524 // process a node that has been replaced.
525 removeFromWorkList(N);
526
527 // Finally, since the node is now dead, remove it from the graph.
528 DAG.DeleteNode(N);
Dan Gohman8181bd12008-07-27 21:46:04 +0000529 return SDValue(N, 0);
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000530}
531
532/// SimplifyDemandedBits - Check the specified integer node value to see if
533/// it can be simplified or if things it uses can be simplified by bit
534/// propagation. If so, return true.
Dan Gohman8181bd12008-07-27 21:46:04 +0000535bool DAGCombiner::SimplifyDemandedBits(SDValue Op, const APInt &Demanded) {
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000536 TargetLowering::TargetLoweringOpt TLO(DAG, AfterLegalize);
Dan Gohman11607792008-02-27 00:25:32 +0000537 APInt KnownZero, KnownOne;
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000538 if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO))
539 return false;
540
541 // Revisit the node.
Gabor Greif1c80d112008-08-28 21:40:38 +0000542 AddToWorkList(Op.getNode());
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000543
544 // Replace the old value with the new one.
545 ++NodesCombined;
Gabor Greif1c80d112008-08-28 21:40:38 +0000546 DOUT << "\nReplacing.2 "; DEBUG(TLO.Old.getNode()->dump(&DAG));
547 DOUT << "\nWith: "; DEBUG(TLO.New.getNode()->dump(&DAG));
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000548 DOUT << '\n';
549
550 // Replace all uses. If any nodes become isomorphic to other nodes and
551 // are deleted, make sure to remove them from our worklist.
552 WorkListRemover DeadNodes(*this);
553 DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New, &DeadNodes);
554
555 // Push the new node and any (possibly new) users onto the worklist.
Gabor Greif1c80d112008-08-28 21:40:38 +0000556 AddToWorkList(TLO.New.getNode());
557 AddUsersToWorkList(TLO.New.getNode());
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000558
559 // Finally, if the node is now dead, remove it from the graph. The node
560 // may not be dead if the replacement process recursively simplified to
561 // something else needing this node.
Gabor Greif1c80d112008-08-28 21:40:38 +0000562 if (TLO.Old.getNode()->use_empty()) {
563 removeFromWorkList(TLO.Old.getNode());
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000564
565 // If the operands of this node are only used by the node, they will now
566 // be dead. Make sure to visit them first to delete dead nodes early.
Gabor Greif1c80d112008-08-28 21:40:38 +0000567 for (unsigned i = 0, e = TLO.Old.getNode()->getNumOperands(); i != e; ++i)
568 if (TLO.Old.getNode()->getOperand(i).getNode()->hasOneUse())
569 AddToWorkList(TLO.Old.getNode()->getOperand(i).getNode());
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000570
Gabor Greif1c80d112008-08-28 21:40:38 +0000571 DAG.DeleteNode(TLO.Old.getNode());
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000572 }
573 return true;
574}
575
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000576//===----------------------------------------------------------------------===//
577// Main DAG Combiner implementation
578//===----------------------------------------------------------------------===//
579
580void DAGCombiner::Run(bool RunningAfterLegalize) {
581 // set the instance variable, so that the various visit routines may use it.
582 AfterLegalize = RunningAfterLegalize;
583
Evan Cheng56550ae2008-08-29 22:21:44 +0000584 // Add all the dag nodes to the worklist.
585 WorkList.reserve(DAG.allnodes_size());
586 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
587 E = DAG.allnodes_end(); I != E; ++I)
588 WorkList.push_back(I);
589
590 // Create a dummy node (which is not added to allnodes), that adds a reference
591 // to the root node, preventing it from being deleted, and tracking any
592 // changes of the root.
593 HandleSDNode Dummy(DAG.getRoot());
594
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000595 // The root of the dag may dangle to deleted nodes until the dag combiner is
596 // done. Set it to null to avoid confusion.
Dan Gohman8181bd12008-07-27 21:46:04 +0000597 DAG.setRoot(SDValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000598
Evan Cheng56550ae2008-08-29 22:21:44 +0000599 // while the worklist isn't empty, inspect the node on the end of it and
600 // try and combine it.
601 while (!WorkList.empty()) {
602 SDNode *N = WorkList.back();
603 WorkList.pop_back();
604
605 // If N has no uses, it is dead. Make sure to revisit all N's operands once
606 // N is deleted from the DAG, since they too may now be dead or may have a
607 // reduced number of uses, allowing other xforms.
608 if (N->use_empty() && N != &Dummy) {
609 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
610 AddToWorkList(N->getOperand(i).getNode());
611
612 DAG.DeleteNode(N);
613 continue;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000614 }
Evan Cheng56550ae2008-08-29 22:21:44 +0000615
616 SDValue RV = combine(N);
617
618 if (RV.getNode() == 0)
619 continue;
620
621 ++NodesCombined;
622
623 // If we get back the same node we passed in, rather than a new node or
624 // zero, we know that the node must have defined multiple values and
625 // CombineTo was used. Since CombineTo takes care of the worklist
626 // mechanics for us, we have no work to do in this case.
627 if (RV.getNode() == N)
628 continue;
629
630 assert(N->getOpcode() != ISD::DELETED_NODE &&
631 RV.getNode()->getOpcode() != ISD::DELETED_NODE &&
632 "Node was deleted but visit returned new node!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000633
Evan Cheng56550ae2008-08-29 22:21:44 +0000634 DOUT << "\nReplacing.3 "; DEBUG(N->dump(&DAG));
635 DOUT << "\nWith: "; DEBUG(RV.getNode()->dump(&DAG));
636 DOUT << '\n';
637 WorkListRemover DeadNodes(*this);
638 if (N->getNumValues() == RV.getNode()->getNumValues())
639 DAG.ReplaceAllUsesWith(N, RV.getNode(), &DeadNodes);
640 else {
641 assert(N->getValueType(0) == RV.getValueType() &&
642 N->getNumValues() == 1 && "Type mismatch");
643 SDValue OpV = RV;
644 DAG.ReplaceAllUsesWith(N, &OpV, &DeadNodes);
645 }
646
647 // Push the new node and any users onto the worklist
648 AddToWorkList(RV.getNode());
649 AddUsersToWorkList(RV.getNode());
650
651 // Add any uses of the old node to the worklist in case this node is the
652 // last one that uses them. They may become dead after this node is
653 // deleted.
654 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
655 AddToWorkList(N->getOperand(i).getNode());
656
657 // Nodes can be reintroduced into the worklist. Make sure we do not
658 // process a node that has been replaced.
659 removeFromWorkList(N);
660
661 // Finally, since the node is now dead, remove it from the graph.
662 DAG.DeleteNode(N);
663 }
664
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000665 // If the root changed (e.g. it was a dead load, update the root).
666 DAG.setRoot(Dummy.getValue());
667}
668
Dan Gohman8181bd12008-07-27 21:46:04 +0000669SDValue DAGCombiner::visit(SDNode *N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000670 switch(N->getOpcode()) {
671 default: break;
672 case ISD::TokenFactor: return visitTokenFactor(N);
Chris Lattnerf32fa7f2008-02-13 07:25:05 +0000673 case ISD::MERGE_VALUES: return visitMERGE_VALUES(N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000674 case ISD::ADD: return visitADD(N);
675 case ISD::SUB: return visitSUB(N);
676 case ISD::ADDC: return visitADDC(N);
677 case ISD::ADDE: return visitADDE(N);
678 case ISD::MUL: return visitMUL(N);
679 case ISD::SDIV: return visitSDIV(N);
680 case ISD::UDIV: return visitUDIV(N);
681 case ISD::SREM: return visitSREM(N);
682 case ISD::UREM: return visitUREM(N);
683 case ISD::MULHU: return visitMULHU(N);
684 case ISD::MULHS: return visitMULHS(N);
Dan Gohman6c89ea72007-10-08 17:57:15 +0000685 case ISD::SMUL_LOHI: return visitSMUL_LOHI(N);
686 case ISD::UMUL_LOHI: return visitUMUL_LOHI(N);
687 case ISD::SDIVREM: return visitSDIVREM(N);
688 case ISD::UDIVREM: return visitUDIVREM(N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000689 case ISD::AND: return visitAND(N);
690 case ISD::OR: return visitOR(N);
691 case ISD::XOR: return visitXOR(N);
692 case ISD::SHL: return visitSHL(N);
693 case ISD::SRA: return visitSRA(N);
694 case ISD::SRL: return visitSRL(N);
695 case ISD::CTLZ: return visitCTLZ(N);
696 case ISD::CTTZ: return visitCTTZ(N);
697 case ISD::CTPOP: return visitCTPOP(N);
698 case ISD::SELECT: return visitSELECT(N);
699 case ISD::SELECT_CC: return visitSELECT_CC(N);
700 case ISD::SETCC: return visitSETCC(N);
701 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
702 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
703 case ISD::ANY_EXTEND: return visitANY_EXTEND(N);
704 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
705 case ISD::TRUNCATE: return visitTRUNCATE(N);
706 case ISD::BIT_CONVERT: return visitBIT_CONVERT(N);
Evan Chengb6290462008-05-12 23:04:07 +0000707 case ISD::BUILD_PAIR: return visitBUILD_PAIR(N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000708 case ISD::FADD: return visitFADD(N);
709 case ISD::FSUB: return visitFSUB(N);
710 case ISD::FMUL: return visitFMUL(N);
711 case ISD::FDIV: return visitFDIV(N);
712 case ISD::FREM: return visitFREM(N);
713 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
714 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
715 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
716 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
717 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
718 case ISD::FP_ROUND: return visitFP_ROUND(N);
719 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
720 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
721 case ISD::FNEG: return visitFNEG(N);
722 case ISD::FABS: return visitFABS(N);
723 case ISD::BRCOND: return visitBRCOND(N);
724 case ISD::BR_CC: return visitBR_CC(N);
725 case ISD::LOAD: return visitLOAD(N);
726 case ISD::STORE: return visitSTORE(N);
727 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
Evan Chengd7ba7ed2007-10-06 08:19:55 +0000728 case ISD::EXTRACT_VECTOR_ELT: return visitEXTRACT_VECTOR_ELT(N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000729 case ISD::BUILD_VECTOR: return visitBUILD_VECTOR(N);
730 case ISD::CONCAT_VECTORS: return visitCONCAT_VECTORS(N);
731 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
Bill Wendlingea8b7c92008-11-21 02:12:42 +0000732 case ISD::SADDO: return visitSADDO(N);
733 case ISD::UADDO: return visitUADDO(N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000734 }
Dan Gohman8181bd12008-07-27 21:46:04 +0000735 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000736}
737
Dan Gohman8181bd12008-07-27 21:46:04 +0000738SDValue DAGCombiner::combine(SDNode *N) {
Dan Gohman6c89ea72007-10-08 17:57:15 +0000739
Dan Gohman8181bd12008-07-27 21:46:04 +0000740 SDValue RV = visit(N);
Dan Gohman6c89ea72007-10-08 17:57:15 +0000741
742 // If nothing happened, try a target-specific DAG combine.
Gabor Greif1c80d112008-08-28 21:40:38 +0000743 if (RV.getNode() == 0) {
Dan Gohman6c89ea72007-10-08 17:57:15 +0000744 assert(N->getOpcode() != ISD::DELETED_NODE &&
745 "Node was deleted but visit returned NULL!");
746
747 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
748 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode())) {
749
750 // Expose the DAG combiner to the target combiner impls.
751 TargetLowering::DAGCombinerInfo
752 DagCombineInfo(DAG, !AfterLegalize, false, this);
753
754 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
755 }
756 }
757
Evan Chengd1113582008-03-22 01:55:50 +0000758 // If N is a commutative binary node, try commuting it to enable more
759 // sdisel CSE.
Gabor Greif1c80d112008-08-28 21:40:38 +0000760 if (RV.getNode() == 0 &&
Evan Chengd1113582008-03-22 01:55:50 +0000761 SelectionDAG::isCommutativeBinOp(N->getOpcode()) &&
762 N->getNumValues() == 1) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000763 SDValue N0 = N->getOperand(0);
764 SDValue N1 = N->getOperand(1);
Evan Chengd1113582008-03-22 01:55:50 +0000765 // Constant operands are canonicalized to RHS.
766 if (isa<ConstantSDNode>(N0) || !isa<ConstantSDNode>(N1)) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000767 SDValue Ops[] = { N1, N0 };
Evan Chengd1113582008-03-22 01:55:50 +0000768 SDNode *CSENode = DAG.getNodeIfExists(N->getOpcode(), N->getVTList(),
769 Ops, 2);
Evan Chenge40b51c2008-03-24 23:55:16 +0000770 if (CSENode)
Dan Gohman8181bd12008-07-27 21:46:04 +0000771 return SDValue(CSENode, 0);
Evan Chengd1113582008-03-22 01:55:50 +0000772 }
773 }
774
Dan Gohman6c89ea72007-10-08 17:57:15 +0000775 return RV;
776}
777
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000778/// getInputChainForNode - Given a node, return its input chain if it has one,
779/// otherwise return a null sd operand.
Dan Gohman8181bd12008-07-27 21:46:04 +0000780static SDValue getInputChainForNode(SDNode *N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000781 if (unsigned NumOps = N->getNumOperands()) {
782 if (N->getOperand(0).getValueType() == MVT::Other)
783 return N->getOperand(0);
784 else if (N->getOperand(NumOps-1).getValueType() == MVT::Other)
785 return N->getOperand(NumOps-1);
786 for (unsigned i = 1; i < NumOps-1; ++i)
787 if (N->getOperand(i).getValueType() == MVT::Other)
788 return N->getOperand(i);
789 }
Dan Gohman8181bd12008-07-27 21:46:04 +0000790 return SDValue(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000791}
792
Dan Gohman8181bd12008-07-27 21:46:04 +0000793SDValue DAGCombiner::visitTokenFactor(SDNode *N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000794 // If N has two operands, where one has an input chain equal to the other,
795 // the 'other' chain is redundant.
796 if (N->getNumOperands() == 2) {
Gabor Greif1c80d112008-08-28 21:40:38 +0000797 if (getInputChainForNode(N->getOperand(0).getNode()) == N->getOperand(1))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000798 return N->getOperand(0);
Gabor Greif1c80d112008-08-28 21:40:38 +0000799 if (getInputChainForNode(N->getOperand(1).getNode()) == N->getOperand(0))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000800 return N->getOperand(1);
801 }
802
803 SmallVector<SDNode *, 8> TFs; // List of token factors to visit.
Dan Gohman8181bd12008-07-27 21:46:04 +0000804 SmallVector<SDValue, 8> Ops; // Ops for replacing token factor.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000805 SmallPtrSet<SDNode*, 16> SeenOps;
806 bool Changed = false; // If we should replace this token factor.
807
808 // Start out with this token factor.
809 TFs.push_back(N);
810
811 // Iterate through token factors. The TFs grows when new token factors are
812 // encountered.
813 for (unsigned i = 0; i < TFs.size(); ++i) {
814 SDNode *TF = TFs[i];
815
816 // Check each of the operands.
817 for (unsigned i = 0, ie = TF->getNumOperands(); i != ie; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000818 SDValue Op = TF->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000819
820 switch (Op.getOpcode()) {
821 case ISD::EntryToken:
822 // Entry tokens don't need to be added to the list. They are
823 // rededundant.
824 Changed = true;
825 break;
826
827 case ISD::TokenFactor:
828 if ((CombinerAA || Op.hasOneUse()) &&
Gabor Greif1c80d112008-08-28 21:40:38 +0000829 std::find(TFs.begin(), TFs.end(), Op.getNode()) == TFs.end()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000830 // Queue up for processing.
Gabor Greif1c80d112008-08-28 21:40:38 +0000831 TFs.push_back(Op.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000832 // Clean up in case the token factor is removed.
Gabor Greif1c80d112008-08-28 21:40:38 +0000833 AddToWorkList(Op.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000834 Changed = true;
835 break;
836 }
837 // Fall thru
838
839 default:
840 // Only add if it isn't already in the list.
Gabor Greif1c80d112008-08-28 21:40:38 +0000841 if (SeenOps.insert(Op.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000842 Ops.push_back(Op);
843 else
844 Changed = true;
845 break;
846 }
847 }
848 }
849
Dan Gohman8181bd12008-07-27 21:46:04 +0000850 SDValue Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000851
852 // If we've change things around then replace token factor.
853 if (Changed) {
Dan Gohman301f4052008-01-29 13:02:09 +0000854 if (Ops.empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000855 // The entry token is the only possible outcome.
856 Result = DAG.getEntryNode();
857 } else {
858 // New and improved token factor.
859 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0], Ops.size());
860 }
861
862 // Don't add users to work list.
863 return CombineTo(N, Result, false);
864 }
865
866 return Result;
867}
868
Chris Lattnerf32fa7f2008-02-13 07:25:05 +0000869/// MERGE_VALUES can always be eliminated.
Dan Gohman8181bd12008-07-27 21:46:04 +0000870SDValue DAGCombiner::visitMERGE_VALUES(SDNode *N) {
Chris Lattnerf32fa7f2008-02-13 07:25:05 +0000871 WorkListRemover DeadNodes(*this);
872 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Dan Gohman8181bd12008-07-27 21:46:04 +0000873 DAG.ReplaceAllUsesOfValueWith(SDValue(N, i), N->getOperand(i),
Chris Lattnerf32fa7f2008-02-13 07:25:05 +0000874 &DeadNodes);
875 removeFromWorkList(N);
876 DAG.DeleteNode(N);
Dan Gohman8181bd12008-07-27 21:46:04 +0000877 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerf32fa7f2008-02-13 07:25:05 +0000878}
879
880
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000881static
Dan Gohman8181bd12008-07-27 21:46:04 +0000882SDValue combineShlAddConstant(SDValue N0, SDValue N1, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +0000883 MVT VT = N0.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +0000884 SDValue N00 = N0.getOperand(0);
885 SDValue N01 = N0.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000886 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N01);
Gabor Greif1c80d112008-08-28 21:40:38 +0000887 if (N01C && N00.getOpcode() == ISD::ADD && N00.getNode()->hasOneUse() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000888 isa<ConstantSDNode>(N00.getOperand(1))) {
889 N0 = DAG.getNode(ISD::ADD, VT,
890 DAG.getNode(ISD::SHL, VT, N00.getOperand(0), N01),
891 DAG.getNode(ISD::SHL, VT, N00.getOperand(1), N01));
892 return DAG.getNode(ISD::ADD, VT, N0, N1);
893 }
Dan Gohman8181bd12008-07-27 21:46:04 +0000894 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000895}
896
897static
Dan Gohman8181bd12008-07-27 21:46:04 +0000898SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
Bill Wendling0d810b82008-11-11 08:25:46 +0000899 SelectionDAG &DAG, const TargetLowering &TLI,
900 bool AfterLegalize) {
Duncan Sands92c43912008-06-06 12:08:01 +0000901 MVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000902 unsigned Opc = N->getOpcode();
903 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
Dan Gohman8181bd12008-07-27 21:46:04 +0000904 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
905 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000906 ISD::CondCode CC = ISD::SETCC_INVALID;
Bill Wendling0d810b82008-11-11 08:25:46 +0000907
908 if (isSlctCC) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000909 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
Bill Wendling0d810b82008-11-11 08:25:46 +0000910 } else {
Dan Gohman8181bd12008-07-27 21:46:04 +0000911 SDValue CCOp = Slct.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000912 if (CCOp.getOpcode() == ISD::SETCC)
913 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
914 }
915
916 bool DoXform = false;
917 bool InvCC = false;
918 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
919 "Bad input!");
Bill Wendling0d810b82008-11-11 08:25:46 +0000920
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000921 if (LHS.getOpcode() == ISD::Constant &&
Bill Wendling0d810b82008-11-11 08:25:46 +0000922 cast<ConstantSDNode>(LHS)->isNullValue()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000923 DoXform = true;
Bill Wendling0d810b82008-11-11 08:25:46 +0000924 } else if (CC != ISD::SETCC_INVALID &&
925 RHS.getOpcode() == ISD::Constant &&
926 cast<ConstantSDNode>(RHS)->isNullValue()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000927 std::swap(LHS, RHS);
Dan Gohman8181bd12008-07-27 21:46:04 +0000928 SDValue Op0 = Slct.getOperand(0);
Bill Wendling0d810b82008-11-11 08:25:46 +0000929 MVT OpVT = isSlctCC ? Op0.getValueType() :
930 Op0.getOperand(0).getValueType();
931 bool isInt = OpVT.isInteger();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000932 CC = ISD::getSetCCInverse(CC, isInt);
Bill Wendling0d810b82008-11-11 08:25:46 +0000933
934 if (AfterLegalize && !TLI.isCondCodeLegal(CC, OpVT))
935 return SDValue(); // Inverse operator isn't legal.
936
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000937 DoXform = true;
938 InvCC = true;
939 }
940
941 if (DoXform) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000942 SDValue Result = DAG.getNode(Opc, VT, OtherOp, RHS);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000943 if (isSlctCC)
944 return DAG.getSelectCC(OtherOp, Result,
945 Slct.getOperand(0), Slct.getOperand(1), CC);
Dan Gohman8181bd12008-07-27 21:46:04 +0000946 SDValue CCOp = Slct.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000947 if (InvCC)
948 CCOp = DAG.getSetCC(CCOp.getValueType(), CCOp.getOperand(0),
949 CCOp.getOperand(1), CC);
950 return DAG.getNode(ISD::SELECT, VT, CCOp, OtherOp, Result);
951 }
Dan Gohman8181bd12008-07-27 21:46:04 +0000952 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000953}
954
Dan Gohman8181bd12008-07-27 21:46:04 +0000955SDValue DAGCombiner::visitADD(SDNode *N) {
956 SDValue N0 = N->getOperand(0);
957 SDValue N1 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000958 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
959 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands92c43912008-06-06 12:08:01 +0000960 MVT VT = N0.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000961
962 // fold vector ops
Duncan Sands92c43912008-06-06 12:08:01 +0000963 if (VT.isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000964 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greif1c80d112008-08-28 21:40:38 +0000965 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000966 }
967
968 // fold (add x, undef) -> undef
969 if (N0.getOpcode() == ISD::UNDEF)
970 return N0;
971 if (N1.getOpcode() == ISD::UNDEF)
972 return N1;
973 // fold (add c1, c2) -> c1+c2
974 if (N0C && N1C)
Bill Wendling0445c4c2008-09-24 10:25:02 +0000975 return DAG.FoldConstantArithmetic(ISD::ADD, VT, N0C, N1C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000976 // canonicalize constant to RHS
977 if (N0C && !N1C)
978 return DAG.getNode(ISD::ADD, VT, N1, N0);
979 // fold (add x, 0) -> x
980 if (N1C && N1C->isNullValue())
981 return N0;
Dan Gohman36322c72008-10-18 02:06:02 +0000982 // fold (add Sym, c) -> Sym+c
983 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0))
984 if (!AfterLegalize && TLI.isOffsetFoldingLegal(GA) && N1C &&
985 GA->getOpcode() == ISD::GlobalAddress)
986 return DAG.getGlobalAddress(GA->getGlobal(), VT,
987 GA->getOffset() +
988 (uint64_t)N1C->getSExtValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000989 // fold ((c1-A)+c2) -> (c1+c2)-A
990 if (N1C && N0.getOpcode() == ISD::SUB)
991 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
992 return DAG.getNode(ISD::SUB, VT,
Dan Gohman9d24dc72008-03-13 22:13:53 +0000993 DAG.getConstant(N1C->getAPIntValue()+
994 N0C->getAPIntValue(), VT),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000995 N0.getOperand(1));
996 // reassociate add
Dan Gohman8181bd12008-07-27 21:46:04 +0000997 SDValue RADD = ReassociateOps(ISD::ADD, N0, N1);
Gabor Greif1c80d112008-08-28 21:40:38 +0000998 if (RADD.getNode() != 0)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000999 return RADD;
1000 // fold ((0-A) + B) -> B-A
1001 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
1002 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
1003 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
1004 // fold (A + (0-B)) -> A-B
1005 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
1006 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
1007 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
1008 // fold (A+(B-A)) -> B
1009 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
1010 return N1.getOperand(0);
1011
Dan Gohman8181bd12008-07-27 21:46:04 +00001012 if (!VT.isVector() && SimplifyDemandedBits(SDValue(N, 0)))
1013 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001014
1015 // fold (a+b) -> (a|b) iff a and b share no bits.
Duncan Sands92c43912008-06-06 12:08:01 +00001016 if (VT.isInteger() && !VT.isVector()) {
Dan Gohmanbea075f2008-02-20 16:33:30 +00001017 APInt LHSZero, LHSOne;
1018 APInt RHSZero, RHSOne;
Duncan Sands92c43912008-06-06 12:08:01 +00001019 APInt Mask = APInt::getAllOnesValue(VT.getSizeInBits());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001020 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Dan Gohmanbea075f2008-02-20 16:33:30 +00001021 if (LHSZero.getBoolValue()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001022 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
1023
1024 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1025 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
1026 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
1027 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
1028 return DAG.getNode(ISD::OR, VT, N0, N1);
1029 }
1030 }
1031
1032 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
Gabor Greif1c80d112008-08-28 21:40:38 +00001033 if (N0.getOpcode() == ISD::SHL && N0.getNode()->hasOneUse()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001034 SDValue Result = combineShlAddConstant(N0, N1, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001035 if (Result.getNode()) return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001036 }
Gabor Greif1c80d112008-08-28 21:40:38 +00001037 if (N1.getOpcode() == ISD::SHL && N1.getNode()->hasOneUse()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001038 SDValue Result = combineShlAddConstant(N1, N0, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001039 if (Result.getNode()) return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001040 }
1041
1042 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
Gabor Greif1c80d112008-08-28 21:40:38 +00001043 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
Bill Wendling0d810b82008-11-11 08:25:46 +00001044 SDValue Result = combineSelectAndUse(N, N0, N1, DAG, TLI, AfterLegalize);
Gabor Greif1c80d112008-08-28 21:40:38 +00001045 if (Result.getNode()) return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001046 }
Gabor Greif1c80d112008-08-28 21:40:38 +00001047 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
Bill Wendling0d810b82008-11-11 08:25:46 +00001048 SDValue Result = combineSelectAndUse(N, N1, N0, DAG, TLI, AfterLegalize);
Gabor Greif1c80d112008-08-28 21:40:38 +00001049 if (Result.getNode()) return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001050 }
1051
Dan Gohman8181bd12008-07-27 21:46:04 +00001052 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001053}
1054
Dan Gohman8181bd12008-07-27 21:46:04 +00001055SDValue DAGCombiner::visitADDC(SDNode *N) {
1056 SDValue N0 = N->getOperand(0);
1057 SDValue N1 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001058 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1059 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands92c43912008-06-06 12:08:01 +00001060 MVT VT = N0.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001061
1062 // If the flag result is dead, turn this into an ADD.
1063 if (N->hasNUsesOfValue(0, 1))
1064 return CombineTo(N, DAG.getNode(ISD::ADD, VT, N1, N0),
1065 DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
1066
1067 // canonicalize constant to RHS.
Dan Gohmanedb43e72008-06-23 15:29:14 +00001068 if (N0C && !N1C)
Dan Gohman6d4bb112008-06-21 22:06:07 +00001069 return DAG.getNode(ISD::ADDC, N->getVTList(), N1, N0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001070
1071 // fold (addc x, 0) -> x + no carry out
1072 if (N1C && N1C->isNullValue())
1073 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
1074
1075 // fold (addc a, b) -> (or a, b), CARRY_FALSE iff a and b share no bits.
Dan Gohmanbea075f2008-02-20 16:33:30 +00001076 APInt LHSZero, LHSOne;
1077 APInt RHSZero, RHSOne;
Duncan Sands92c43912008-06-06 12:08:01 +00001078 APInt Mask = APInt::getAllOnesValue(VT.getSizeInBits());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001079 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Dan Gohmanbea075f2008-02-20 16:33:30 +00001080 if (LHSZero.getBoolValue()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001081 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
1082
1083 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1084 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
1085 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
1086 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
1087 return CombineTo(N, DAG.getNode(ISD::OR, VT, N0, N1),
1088 DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
1089 }
1090
Dan Gohman8181bd12008-07-27 21:46:04 +00001091 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001092}
1093
Dan Gohman8181bd12008-07-27 21:46:04 +00001094SDValue DAGCombiner::visitADDE(SDNode *N) {
1095 SDValue N0 = N->getOperand(0);
1096 SDValue N1 = N->getOperand(1);
1097 SDValue CarryIn = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001098 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1099 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands92c43912008-06-06 12:08:01 +00001100 //MVT VT = N0.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001101
1102 // canonicalize constant to RHS
Dan Gohmanedb43e72008-06-23 15:29:14 +00001103 if (N0C && !N1C)
Dan Gohman6d4bb112008-06-21 22:06:07 +00001104 return DAG.getNode(ISD::ADDE, N->getVTList(), N1, N0, CarryIn);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001105
1106 // fold (adde x, y, false) -> (addc x, y)
Dan Gohmanedb43e72008-06-23 15:29:14 +00001107 if (CarryIn.getOpcode() == ISD::CARRY_FALSE)
Dan Gohman6d4bb112008-06-21 22:06:07 +00001108 return DAG.getNode(ISD::ADDC, N->getVTList(), N1, N0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001109
Dan Gohman8181bd12008-07-27 21:46:04 +00001110 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001111}
1112
1113
1114
Dan Gohman8181bd12008-07-27 21:46:04 +00001115SDValue DAGCombiner::visitSUB(SDNode *N) {
1116 SDValue N0 = N->getOperand(0);
1117 SDValue N1 = N->getOperand(1);
Gabor Greif1c80d112008-08-28 21:40:38 +00001118 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1119 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Duncan Sands92c43912008-06-06 12:08:01 +00001120 MVT VT = N0.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001121
1122 // fold vector ops
Duncan Sands92c43912008-06-06 12:08:01 +00001123 if (VT.isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001124 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00001125 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001126 }
1127
1128 // fold (sub x, x) -> 0
Evan Chenga15896e2008-03-12 07:02:50 +00001129 if (N0 == N1)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001130 return DAG.getConstant(0, N->getValueType(0));
1131 // fold (sub c1, c2) -> c1-c2
1132 if (N0C && N1C)
Bill Wendling0445c4c2008-09-24 10:25:02 +00001133 return DAG.FoldConstantArithmetic(ISD::SUB, VT, N0C, N1C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001134 // fold (sub x, c) -> (add x, -c)
1135 if (N1C)
Dan Gohman9d24dc72008-03-13 22:13:53 +00001136 return DAG.getNode(ISD::ADD, VT, N0,
1137 DAG.getConstant(-N1C->getAPIntValue(), VT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001138 // fold (A+B)-A -> B
1139 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
1140 return N0.getOperand(1);
1141 // fold (A+B)-B -> A
1142 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
1143 return N0.getOperand(0);
1144 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
Gabor Greif1c80d112008-08-28 21:40:38 +00001145 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
Bill Wendling0d810b82008-11-11 08:25:46 +00001146 SDValue Result = combineSelectAndUse(N, N1, N0, DAG, TLI, AfterLegalize);
Gabor Greif1c80d112008-08-28 21:40:38 +00001147 if (Result.getNode()) return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001148 }
1149 // If either operand of a sub is undef, the result is undef
1150 if (N0.getOpcode() == ISD::UNDEF)
1151 return N0;
1152 if (N1.getOpcode() == ISD::UNDEF)
1153 return N1;
1154
Dan Gohman36322c72008-10-18 02:06:02 +00001155 // If the relocation model supports it, consider symbol offsets.
1156 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0))
1157 if (!AfterLegalize && TLI.isOffsetFoldingLegal(GA)) {
1158 // fold (sub Sym, c) -> Sym-c
1159 if (N1C && GA->getOpcode() == ISD::GlobalAddress)
1160 return DAG.getGlobalAddress(GA->getGlobal(), VT,
1161 GA->getOffset() -
1162 (uint64_t)N1C->getSExtValue());
1163 // fold (sub Sym+c1, Sym+c2) -> c1-c2
1164 if (GlobalAddressSDNode *GB = dyn_cast<GlobalAddressSDNode>(N1))
1165 if (GA->getGlobal() == GB->getGlobal())
1166 return DAG.getConstant((uint64_t)GA->getOffset() - GB->getOffset(),
1167 VT);
1168 }
1169
Dan Gohman8181bd12008-07-27 21:46:04 +00001170 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001171}
1172
Dan Gohman8181bd12008-07-27 21:46:04 +00001173SDValue DAGCombiner::visitMUL(SDNode *N) {
1174 SDValue N0 = N->getOperand(0);
1175 SDValue N1 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001176 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1177 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands92c43912008-06-06 12:08:01 +00001178 MVT VT = N0.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001179
1180 // fold vector ops
Duncan Sands92c43912008-06-06 12:08:01 +00001181 if (VT.isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001182 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00001183 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001184 }
1185
1186 // fold (mul x, undef) -> 0
1187 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
1188 return DAG.getConstant(0, VT);
1189 // fold (mul c1, c2) -> c1*c2
1190 if (N0C && N1C)
Bill Wendling0445c4c2008-09-24 10:25:02 +00001191 return DAG.FoldConstantArithmetic(ISD::MUL, VT, N0C, N1C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001192 // canonicalize constant to RHS
1193 if (N0C && !N1C)
1194 return DAG.getNode(ISD::MUL, VT, N1, N0);
1195 // fold (mul x, 0) -> 0
1196 if (N1C && N1C->isNullValue())
1197 return N1;
1198 // fold (mul x, -1) -> 0-x
1199 if (N1C && N1C->isAllOnesValue())
1200 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
1201 // fold (mul x, (1 << c)) -> x << c
Dan Gohman9d24dc72008-03-13 22:13:53 +00001202 if (N1C && N1C->getAPIntValue().isPowerOf2())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001203 return DAG.getNode(ISD::SHL, VT, N0,
Dan Gohman9d24dc72008-03-13 22:13:53 +00001204 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001205 TLI.getShiftAmountTy()));
1206 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
Dan Gohman40686732008-09-26 21:54:37 +00001207 if (N1C && isPowerOf2_64(-N1C->getSExtValue())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001208 // FIXME: If the input is something that is easily negated (e.g. a
1209 // single-use add), we should put the negate there.
1210 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT),
1211 DAG.getNode(ISD::SHL, VT, N0,
Dan Gohman40686732008-09-26 21:54:37 +00001212 DAG.getConstant(Log2_64(-N1C->getSExtValue()),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001213 TLI.getShiftAmountTy())));
1214 }
1215
1216 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
1217 if (N1C && N0.getOpcode() == ISD::SHL &&
1218 isa<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001219 SDValue C3 = DAG.getNode(ISD::SHL, VT, N1, N0.getOperand(1));
Gabor Greif1c80d112008-08-28 21:40:38 +00001220 AddToWorkList(C3.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001221 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0), C3);
1222 }
1223
1224 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
1225 // use.
1226 {
Dan Gohman8181bd12008-07-27 21:46:04 +00001227 SDValue Sh(0,0), Y(0,0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001228 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
1229 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
Gabor Greif1c80d112008-08-28 21:40:38 +00001230 N0.getNode()->hasOneUse()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001231 Sh = N0; Y = N1;
1232 } else if (N1.getOpcode() == ISD::SHL &&
Gabor Greifb420b9d2008-08-30 19:29:20 +00001233 isa<ConstantSDNode>(N1.getOperand(1)) &&
1234 N1.getNode()->hasOneUse()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001235 Sh = N1; Y = N0;
1236 }
Gabor Greif1c80d112008-08-28 21:40:38 +00001237 if (Sh.getNode()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001238 SDValue Mul = DAG.getNode(ISD::MUL, VT, Sh.getOperand(0), Y);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001239 return DAG.getNode(ISD::SHL, VT, Mul, Sh.getOperand(1));
1240 }
1241 }
1242 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
Gabor Greif1c80d112008-08-28 21:40:38 +00001243 if (N1C && N0.getOpcode() == ISD::ADD && N0.getNode()->hasOneUse() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001244 isa<ConstantSDNode>(N0.getOperand(1))) {
1245 return DAG.getNode(ISD::ADD, VT,
1246 DAG.getNode(ISD::MUL, VT, N0.getOperand(0), N1),
1247 DAG.getNode(ISD::MUL, VT, N0.getOperand(1), N1));
1248 }
1249
1250 // reassociate mul
Dan Gohman8181bd12008-07-27 21:46:04 +00001251 SDValue RMUL = ReassociateOps(ISD::MUL, N0, N1);
Gabor Greif1c80d112008-08-28 21:40:38 +00001252 if (RMUL.getNode() != 0)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001253 return RMUL;
1254
Dan Gohman8181bd12008-07-27 21:46:04 +00001255 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001256}
1257
Dan Gohman8181bd12008-07-27 21:46:04 +00001258SDValue DAGCombiner::visitSDIV(SDNode *N) {
1259 SDValue N0 = N->getOperand(0);
1260 SDValue N1 = N->getOperand(1);
Gabor Greif1c80d112008-08-28 21:40:38 +00001261 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1262 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Duncan Sands92c43912008-06-06 12:08:01 +00001263 MVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001264
1265 // fold vector ops
Duncan Sands92c43912008-06-06 12:08:01 +00001266 if (VT.isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001267 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00001268 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001269 }
1270
1271 // fold (sdiv c1, c2) -> c1/c2
1272 if (N0C && N1C && !N1C->isNullValue())
Bill Wendling0445c4c2008-09-24 10:25:02 +00001273 return DAG.FoldConstantArithmetic(ISD::SDIV, VT, N0C, N1C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001274 // fold (sdiv X, 1) -> X
Dan Gohman40686732008-09-26 21:54:37 +00001275 if (N1C && N1C->getSExtValue() == 1LL)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001276 return N0;
1277 // fold (sdiv X, -1) -> 0-X
1278 if (N1C && N1C->isAllOnesValue())
1279 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
1280 // If we know the sign bits of both operands are zero, strength reduce to a
1281 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
Duncan Sands92c43912008-06-06 12:08:01 +00001282 if (!VT.isVector()) {
Dan Gohman07961cd2008-02-25 21:11:39 +00001283 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Chris Lattner336672f2008-01-27 23:32:17 +00001284 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
1285 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001286 // fold (sdiv X, pow2) -> simple ops after legalize
Dan Gohman9d24dc72008-03-13 22:13:53 +00001287 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap() &&
Dan Gohman40686732008-09-26 21:54:37 +00001288 (isPowerOf2_64(N1C->getSExtValue()) ||
1289 isPowerOf2_64(-N1C->getSExtValue()))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001290 // If dividing by powers of two is cheap, then don't perform the following
1291 // fold.
1292 if (TLI.isPow2DivCheap())
Dan Gohman8181bd12008-07-27 21:46:04 +00001293 return SDValue();
Dan Gohman40686732008-09-26 21:54:37 +00001294 int64_t pow2 = N1C->getSExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001295 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
1296 unsigned lg2 = Log2_64(abs2);
1297 // Splat the sign bit into the register
Dan Gohman8181bd12008-07-27 21:46:04 +00001298 SDValue SGN = DAG.getNode(ISD::SRA, VT, N0,
Duncan Sands92c43912008-06-06 12:08:01 +00001299 DAG.getConstant(VT.getSizeInBits()-1,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001300 TLI.getShiftAmountTy()));
Gabor Greif1c80d112008-08-28 21:40:38 +00001301 AddToWorkList(SGN.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001302 // Add (N0 < 0) ? abs2 - 1 : 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00001303 SDValue SRL = DAG.getNode(ISD::SRL, VT, SGN,
Duncan Sands92c43912008-06-06 12:08:01 +00001304 DAG.getConstant(VT.getSizeInBits()-lg2,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001305 TLI.getShiftAmountTy()));
Dan Gohman8181bd12008-07-27 21:46:04 +00001306 SDValue ADD = DAG.getNode(ISD::ADD, VT, N0, SRL);
Gabor Greif1c80d112008-08-28 21:40:38 +00001307 AddToWorkList(SRL.getNode());
1308 AddToWorkList(ADD.getNode()); // Divide by pow2
Dan Gohman8181bd12008-07-27 21:46:04 +00001309 SDValue SRA = DAG.getNode(ISD::SRA, VT, ADD,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001310 DAG.getConstant(lg2, TLI.getShiftAmountTy()));
1311 // If we're dividing by a positive value, we're done. Otherwise, we must
1312 // negate the result.
1313 if (pow2 > 0)
1314 return SRA;
Gabor Greif1c80d112008-08-28 21:40:38 +00001315 AddToWorkList(SRA.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001316 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), SRA);
1317 }
1318 // if integer divide is expensive and we satisfy the requirements, emit an
1319 // alternate sequence.
Dan Gohman40686732008-09-26 21:54:37 +00001320 if (N1C && (N1C->getSExtValue() < -1 || N1C->getSExtValue() > 1) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001321 !TLI.isIntDivCheap()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001322 SDValue Op = BuildSDIV(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00001323 if (Op.getNode()) return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001324 }
1325
1326 // undef / X -> 0
1327 if (N0.getOpcode() == ISD::UNDEF)
1328 return DAG.getConstant(0, VT);
1329 // X / undef -> undef
1330 if (N1.getOpcode() == ISD::UNDEF)
1331 return N1;
1332
Dan Gohman8181bd12008-07-27 21:46:04 +00001333 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001334}
1335
Dan Gohman8181bd12008-07-27 21:46:04 +00001336SDValue DAGCombiner::visitUDIV(SDNode *N) {
1337 SDValue N0 = N->getOperand(0);
1338 SDValue N1 = N->getOperand(1);
Gabor Greif1c80d112008-08-28 21:40:38 +00001339 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1340 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Duncan Sands92c43912008-06-06 12:08:01 +00001341 MVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001342
1343 // fold vector ops
Duncan Sands92c43912008-06-06 12:08:01 +00001344 if (VT.isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001345 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00001346 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001347 }
1348
1349 // fold (udiv c1, c2) -> c1/c2
1350 if (N0C && N1C && !N1C->isNullValue())
Bill Wendling0445c4c2008-09-24 10:25:02 +00001351 return DAG.FoldConstantArithmetic(ISD::UDIV, VT, N0C, N1C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001352 // fold (udiv x, (1 << c)) -> x >>u c
Dan Gohman9d24dc72008-03-13 22:13:53 +00001353 if (N1C && N1C->getAPIntValue().isPowerOf2())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001354 return DAG.getNode(ISD::SRL, VT, N0,
Dan Gohman9d24dc72008-03-13 22:13:53 +00001355 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001356 TLI.getShiftAmountTy()));
1357 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
1358 if (N1.getOpcode() == ISD::SHL) {
1359 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman9d24dc72008-03-13 22:13:53 +00001360 if (SHC->getAPIntValue().isPowerOf2()) {
Duncan Sands92c43912008-06-06 12:08:01 +00001361 MVT ADDVT = N1.getOperand(1).getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +00001362 SDValue Add = DAG.getNode(ISD::ADD, ADDVT, N1.getOperand(1),
Dan Gohman9d24dc72008-03-13 22:13:53 +00001363 DAG.getConstant(SHC->getAPIntValue()
1364 .logBase2(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001365 ADDVT));
Gabor Greif1c80d112008-08-28 21:40:38 +00001366 AddToWorkList(Add.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001367 return DAG.getNode(ISD::SRL, VT, N0, Add);
1368 }
1369 }
1370 }
1371 // fold (udiv x, c) -> alternate
Dan Gohman9d24dc72008-03-13 22:13:53 +00001372 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001373 SDValue Op = BuildUDIV(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00001374 if (Op.getNode()) return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001375 }
1376
1377 // undef / X -> 0
1378 if (N0.getOpcode() == ISD::UNDEF)
1379 return DAG.getConstant(0, VT);
1380 // X / undef -> undef
1381 if (N1.getOpcode() == ISD::UNDEF)
1382 return N1;
1383
Dan Gohman8181bd12008-07-27 21:46:04 +00001384 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001385}
1386
Dan Gohman8181bd12008-07-27 21:46:04 +00001387SDValue DAGCombiner::visitSREM(SDNode *N) {
1388 SDValue N0 = N->getOperand(0);
1389 SDValue N1 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001390 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1391 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands92c43912008-06-06 12:08:01 +00001392 MVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001393
1394 // fold (srem c1, c2) -> c1%c2
1395 if (N0C && N1C && !N1C->isNullValue())
Bill Wendling0445c4c2008-09-24 10:25:02 +00001396 return DAG.FoldConstantArithmetic(ISD::SREM, VT, N0C, N1C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001397 // If we know the sign bits of both operands are zero, strength reduce to a
1398 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
Duncan Sands92c43912008-06-06 12:08:01 +00001399 if (!VT.isVector()) {
Dan Gohman07961cd2008-02-25 21:11:39 +00001400 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Chris Lattnerce602f52008-01-27 23:21:58 +00001401 return DAG.getNode(ISD::UREM, VT, N0, N1);
1402 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001403
Dan Gohmanfdb31f12007-11-26 23:46:11 +00001404 // If X/C can be simplified by the division-by-constant logic, lower
1405 // X%C to the equivalent of X-X/C*C.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001406 if (N1C && !N1C->isNullValue()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001407 SDValue Div = DAG.getNode(ISD::SDIV, VT, N0, N1);
Gabor Greif1c80d112008-08-28 21:40:38 +00001408 AddToWorkList(Div.getNode());
1409 SDValue OptimizedDiv = combine(Div.getNode());
1410 if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001411 SDValue Mul = DAG.getNode(ISD::MUL, VT, OptimizedDiv, N1);
1412 SDValue Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
Gabor Greif1c80d112008-08-28 21:40:38 +00001413 AddToWorkList(Mul.getNode());
Dan Gohmanfdb31f12007-11-26 23:46:11 +00001414 return Sub;
1415 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001416 }
1417
1418 // undef % X -> 0
1419 if (N0.getOpcode() == ISD::UNDEF)
1420 return DAG.getConstant(0, VT);
1421 // X % undef -> undef
1422 if (N1.getOpcode() == ISD::UNDEF)
1423 return N1;
1424
Dan Gohman8181bd12008-07-27 21:46:04 +00001425 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001426}
1427
Dan Gohman8181bd12008-07-27 21:46:04 +00001428SDValue DAGCombiner::visitUREM(SDNode *N) {
1429 SDValue N0 = N->getOperand(0);
1430 SDValue N1 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001431 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1432 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands92c43912008-06-06 12:08:01 +00001433 MVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001434
1435 // fold (urem c1, c2) -> c1%c2
1436 if (N0C && N1C && !N1C->isNullValue())
Bill Wendling0445c4c2008-09-24 10:25:02 +00001437 return DAG.FoldConstantArithmetic(ISD::UREM, VT, N0C, N1C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001438 // fold (urem x, pow2) -> (and x, pow2-1)
Dan Gohman9d24dc72008-03-13 22:13:53 +00001439 if (N1C && !N1C->isNullValue() && N1C->getAPIntValue().isPowerOf2())
1440 return DAG.getNode(ISD::AND, VT, N0,
1441 DAG.getConstant(N1C->getAPIntValue()-1,VT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001442 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
1443 if (N1.getOpcode() == ISD::SHL) {
1444 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman9d24dc72008-03-13 22:13:53 +00001445 if (SHC->getAPIntValue().isPowerOf2()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001446 SDValue Add =
Dan Gohman9d24dc72008-03-13 22:13:53 +00001447 DAG.getNode(ISD::ADD, VT, N1,
Duncan Sands92c43912008-06-06 12:08:01 +00001448 DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()),
Dan Gohman9d24dc72008-03-13 22:13:53 +00001449 VT));
Gabor Greif1c80d112008-08-28 21:40:38 +00001450 AddToWorkList(Add.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001451 return DAG.getNode(ISD::AND, VT, N0, Add);
1452 }
1453 }
1454 }
1455
Dan Gohmanfdb31f12007-11-26 23:46:11 +00001456 // If X/C can be simplified by the division-by-constant logic, lower
1457 // X%C to the equivalent of X-X/C*C.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001458 if (N1C && !N1C->isNullValue()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001459 SDValue Div = DAG.getNode(ISD::UDIV, VT, N0, N1);
Dan Gohman2e1517f2008-09-08 16:59:01 +00001460 AddToWorkList(Div.getNode());
Gabor Greif1c80d112008-08-28 21:40:38 +00001461 SDValue OptimizedDiv = combine(Div.getNode());
1462 if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001463 SDValue Mul = DAG.getNode(ISD::MUL, VT, OptimizedDiv, N1);
1464 SDValue Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
Gabor Greif1c80d112008-08-28 21:40:38 +00001465 AddToWorkList(Mul.getNode());
Dan Gohmanfdb31f12007-11-26 23:46:11 +00001466 return Sub;
1467 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001468 }
1469
1470 // undef % X -> 0
1471 if (N0.getOpcode() == ISD::UNDEF)
1472 return DAG.getConstant(0, VT);
1473 // X % undef -> undef
1474 if (N1.getOpcode() == ISD::UNDEF)
1475 return N1;
1476
Dan Gohman8181bd12008-07-27 21:46:04 +00001477 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001478}
1479
Dan Gohman8181bd12008-07-27 21:46:04 +00001480SDValue DAGCombiner::visitMULHS(SDNode *N) {
1481 SDValue N0 = N->getOperand(0);
1482 SDValue N1 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001483 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands92c43912008-06-06 12:08:01 +00001484 MVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001485
1486 // fold (mulhs x, 0) -> 0
1487 if (N1C && N1C->isNullValue())
1488 return N1;
1489 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Dan Gohman9d24dc72008-03-13 22:13:53 +00001490 if (N1C && N1C->getAPIntValue() == 1)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001491 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
Duncan Sands92c43912008-06-06 12:08:01 +00001492 DAG.getConstant(N0.getValueType().getSizeInBits()-1,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001493 TLI.getShiftAmountTy()));
1494 // fold (mulhs x, undef) -> 0
1495 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
1496 return DAG.getConstant(0, VT);
1497
Dan Gohman8181bd12008-07-27 21:46:04 +00001498 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001499}
1500
Dan Gohman8181bd12008-07-27 21:46:04 +00001501SDValue DAGCombiner::visitMULHU(SDNode *N) {
1502 SDValue N0 = N->getOperand(0);
1503 SDValue N1 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001504 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands92c43912008-06-06 12:08:01 +00001505 MVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001506
1507 // fold (mulhu x, 0) -> 0
1508 if (N1C && N1C->isNullValue())
1509 return N1;
1510 // fold (mulhu x, 1) -> 0
Dan Gohman9d24dc72008-03-13 22:13:53 +00001511 if (N1C && N1C->getAPIntValue() == 1)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001512 return DAG.getConstant(0, N0.getValueType());
1513 // fold (mulhu x, undef) -> 0
1514 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
1515 return DAG.getConstant(0, VT);
1516
Dan Gohman8181bd12008-07-27 21:46:04 +00001517 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001518}
1519
Dan Gohman6c89ea72007-10-08 17:57:15 +00001520/// SimplifyNodeWithTwoResults - Perform optimizations common to nodes that
1521/// compute two values. LoOp and HiOp give the opcodes for the two computations
1522/// that are being performed. Return true if a simplification was made.
1523///
Dan Gohman8181bd12008-07-27 21:46:04 +00001524SDValue DAGCombiner::SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
1525 unsigned HiOp) {
Dan Gohman6c89ea72007-10-08 17:57:15 +00001526 // If the high half is not needed, just compute the low half.
Evan Chengddfa8c72007-11-08 09:25:29 +00001527 bool HiExists = N->hasAnyUseOfValue(1);
1528 if (!HiExists &&
Dan Gohman6c89ea72007-10-08 17:57:15 +00001529 (!AfterLegalize ||
1530 TLI.isOperationLegal(LoOp, N->getValueType(0)))) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001531 SDValue Res = DAG.getNode(LoOp, N->getValueType(0), N->op_begin(),
Chris Lattner4a7c8452008-01-26 01:09:19 +00001532 N->getNumOperands());
1533 return CombineTo(N, Res, Res);
Dan Gohman6c89ea72007-10-08 17:57:15 +00001534 }
1535
1536 // If the low half is not needed, just compute the high half.
Evan Chengddfa8c72007-11-08 09:25:29 +00001537 bool LoExists = N->hasAnyUseOfValue(0);
1538 if (!LoExists &&
Dan Gohman6c89ea72007-10-08 17:57:15 +00001539 (!AfterLegalize ||
1540 TLI.isOperationLegal(HiOp, N->getValueType(1)))) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001541 SDValue Res = DAG.getNode(HiOp, N->getValueType(1), N->op_begin(),
Chris Lattner4a7c8452008-01-26 01:09:19 +00001542 N->getNumOperands());
1543 return CombineTo(N, Res, Res);
Dan Gohman6c89ea72007-10-08 17:57:15 +00001544 }
1545
Evan Chengddfa8c72007-11-08 09:25:29 +00001546 // If both halves are used, return as it is.
1547 if (LoExists && HiExists)
Dan Gohman8181bd12008-07-27 21:46:04 +00001548 return SDValue();
Evan Chengddfa8c72007-11-08 09:25:29 +00001549
1550 // If the two computed results can be simplified separately, separate them.
Evan Chengddfa8c72007-11-08 09:25:29 +00001551 if (LoExists) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001552 SDValue Lo = DAG.getNode(LoOp, N->getValueType(0),
Evan Chengddfa8c72007-11-08 09:25:29 +00001553 N->op_begin(), N->getNumOperands());
Gabor Greif1c80d112008-08-28 21:40:38 +00001554 AddToWorkList(Lo.getNode());
1555 SDValue LoOpt = combine(Lo.getNode());
1556 if (LoOpt.getNode() && LoOpt.getNode() != Lo.getNode() &&
Duncan Sands2418bec2008-06-13 19:07:40 +00001557 (!AfterLegalize ||
1558 TLI.isOperationLegal(LoOpt.getOpcode(), LoOpt.getValueType())))
Chris Lattner4a7c8452008-01-26 01:09:19 +00001559 return CombineTo(N, LoOpt, LoOpt);
Dan Gohman6c89ea72007-10-08 17:57:15 +00001560 }
1561
Evan Chengddfa8c72007-11-08 09:25:29 +00001562 if (HiExists) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001563 SDValue Hi = DAG.getNode(HiOp, N->getValueType(1),
Evan Chengddfa8c72007-11-08 09:25:29 +00001564 N->op_begin(), N->getNumOperands());
Gabor Greif1c80d112008-08-28 21:40:38 +00001565 AddToWorkList(Hi.getNode());
1566 SDValue HiOpt = combine(Hi.getNode());
1567 if (HiOpt.getNode() && HiOpt != Hi &&
Duncan Sands2418bec2008-06-13 19:07:40 +00001568 (!AfterLegalize ||
1569 TLI.isOperationLegal(HiOpt.getOpcode(), HiOpt.getValueType())))
Chris Lattner4a7c8452008-01-26 01:09:19 +00001570 return CombineTo(N, HiOpt, HiOpt);
Evan Chengddfa8c72007-11-08 09:25:29 +00001571 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001572 return SDValue();
Dan Gohman6c89ea72007-10-08 17:57:15 +00001573}
1574
Dan Gohman8181bd12008-07-27 21:46:04 +00001575SDValue DAGCombiner::visitSMUL_LOHI(SDNode *N) {
1576 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHS);
Gabor Greif1c80d112008-08-28 21:40:38 +00001577 if (Res.getNode()) return Res;
Dan Gohman6c89ea72007-10-08 17:57:15 +00001578
Dan Gohman8181bd12008-07-27 21:46:04 +00001579 return SDValue();
Dan Gohman6c89ea72007-10-08 17:57:15 +00001580}
1581
Dan Gohman8181bd12008-07-27 21:46:04 +00001582SDValue DAGCombiner::visitUMUL_LOHI(SDNode *N) {
1583 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHU);
Gabor Greif1c80d112008-08-28 21:40:38 +00001584 if (Res.getNode()) return Res;
Dan Gohman6c89ea72007-10-08 17:57:15 +00001585
Dan Gohman8181bd12008-07-27 21:46:04 +00001586 return SDValue();
Dan Gohman6c89ea72007-10-08 17:57:15 +00001587}
1588
Dan Gohman8181bd12008-07-27 21:46:04 +00001589SDValue DAGCombiner::visitSDIVREM(SDNode *N) {
1590 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::SDIV, ISD::SREM);
Gabor Greif1c80d112008-08-28 21:40:38 +00001591 if (Res.getNode()) return Res;
Dan Gohman6c89ea72007-10-08 17:57:15 +00001592
Dan Gohman8181bd12008-07-27 21:46:04 +00001593 return SDValue();
Dan Gohman6c89ea72007-10-08 17:57:15 +00001594}
1595
Dan Gohman8181bd12008-07-27 21:46:04 +00001596SDValue DAGCombiner::visitUDIVREM(SDNode *N) {
1597 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::UDIV, ISD::UREM);
Gabor Greif1c80d112008-08-28 21:40:38 +00001598 if (Res.getNode()) return Res;
Dan Gohman6c89ea72007-10-08 17:57:15 +00001599
Dan Gohman8181bd12008-07-27 21:46:04 +00001600 return SDValue();
Dan Gohman6c89ea72007-10-08 17:57:15 +00001601}
1602
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001603/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
1604/// two operands of the same opcode, try to simplify it.
Dan Gohman8181bd12008-07-27 21:46:04 +00001605SDValue DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
1606 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Duncan Sands92c43912008-06-06 12:08:01 +00001607 MVT VT = N0.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001608 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
1609
1610 // For each of OP in AND/OR/XOR:
1611 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
1612 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
1613 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
1614 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y))
1615 if ((N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND||
1616 N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::TRUNCATE) &&
1617 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001618 SDValue ORNode = DAG.getNode(N->getOpcode(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001619 N0.getOperand(0).getValueType(),
1620 N0.getOperand(0), N1.getOperand(0));
Gabor Greif1c80d112008-08-28 21:40:38 +00001621 AddToWorkList(ORNode.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001622 return DAG.getNode(N0.getOpcode(), VT, ORNode);
1623 }
1624
1625 // For each of OP in SHL/SRL/SRA/AND...
1626 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
1627 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
1628 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
1629 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
1630 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
1631 N0.getOperand(1) == N1.getOperand(1)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001632 SDValue ORNode = DAG.getNode(N->getOpcode(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001633 N0.getOperand(0).getValueType(),
1634 N0.getOperand(0), N1.getOperand(0));
Gabor Greif1c80d112008-08-28 21:40:38 +00001635 AddToWorkList(ORNode.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001636 return DAG.getNode(N0.getOpcode(), VT, ORNode, N0.getOperand(1));
1637 }
1638
Dan Gohman8181bd12008-07-27 21:46:04 +00001639 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001640}
1641
Dan Gohman8181bd12008-07-27 21:46:04 +00001642SDValue DAGCombiner::visitAND(SDNode *N) {
1643 SDValue N0 = N->getOperand(0);
1644 SDValue N1 = N->getOperand(1);
1645 SDValue LL, LR, RL, RR, CC0, CC1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001646 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1647 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands92c43912008-06-06 12:08:01 +00001648 MVT VT = N1.getValueType();
1649 unsigned BitWidth = VT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001650
1651 // fold vector ops
Duncan Sands92c43912008-06-06 12:08:01 +00001652 if (VT.isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001653 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00001654 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001655 }
1656
1657 // fold (and x, undef) -> 0
1658 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
1659 return DAG.getConstant(0, VT);
1660 // fold (and c1, c2) -> c1&c2
1661 if (N0C && N1C)
Bill Wendling0445c4c2008-09-24 10:25:02 +00001662 return DAG.FoldConstantArithmetic(ISD::AND, VT, N0C, N1C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001663 // canonicalize constant to RHS
1664 if (N0C && !N1C)
1665 return DAG.getNode(ISD::AND, VT, N1, N0);
1666 // fold (and x, -1) -> x
1667 if (N1C && N1C->isAllOnesValue())
1668 return N0;
1669 // if (and x, c) is known to be zero, return 0
Dan Gohman8181bd12008-07-27 21:46:04 +00001670 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman07961cd2008-02-25 21:11:39 +00001671 APInt::getAllOnesValue(BitWidth)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001672 return DAG.getConstant(0, VT);
1673 // reassociate and
Dan Gohman8181bd12008-07-27 21:46:04 +00001674 SDValue RAND = ReassociateOps(ISD::AND, N0, N1);
Gabor Greif1c80d112008-08-28 21:40:38 +00001675 if (RAND.getNode() != 0)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001676 return RAND;
1677 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
1678 if (N1C && N0.getOpcode() == ISD::OR)
1679 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohman9d24dc72008-03-13 22:13:53 +00001680 if ((ORI->getAPIntValue() & N1C->getAPIntValue()) == N1C->getAPIntValue())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001681 return N1;
1682 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
1683 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001684 SDValue N0Op0 = N0.getOperand(0);
Dan Gohman07961cd2008-02-25 21:11:39 +00001685 APInt Mask = ~N1C->getAPIntValue();
1686 Mask.trunc(N0Op0.getValueSizeInBits());
1687 if (DAG.MaskedValueIsZero(N0Op0, Mask)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001688 SDValue Zext = DAG.getNode(ISD::ZERO_EXTEND, N0.getValueType(),
Dan Gohman07961cd2008-02-25 21:11:39 +00001689 N0Op0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001690
1691 // Replace uses of the AND with uses of the Zero extend node.
1692 CombineTo(N, Zext);
1693
1694 // We actually want to replace all uses of the any_extend with the
1695 // zero_extend, to avoid duplicating things. This will later cause this
1696 // AND to be folded.
Gabor Greif1c80d112008-08-28 21:40:38 +00001697 CombineTo(N0.getNode(), Zext);
Dan Gohman8181bd12008-07-27 21:46:04 +00001698 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001699 }
1700 }
1701 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
1702 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1703 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1704 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1705
1706 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands92c43912008-06-06 12:08:01 +00001707 LL.getValueType().isInteger()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001708 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
Dan Gohman9d24dc72008-03-13 22:13:53 +00001709 if (cast<ConstantSDNode>(LR)->isNullValue() && Op1 == ISD::SETEQ) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001710 SDValue ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Gabor Greif1c80d112008-08-28 21:40:38 +00001711 AddToWorkList(ORNode.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001712 return DAG.getSetCC(VT, ORNode, LR, Op1);
1713 }
1714 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
1715 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001716 SDValue ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Gabor Greif1c80d112008-08-28 21:40:38 +00001717 AddToWorkList(ANDNode.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001718 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1719 }
1720 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
1721 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001722 SDValue ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Gabor Greif1c80d112008-08-28 21:40:38 +00001723 AddToWorkList(ORNode.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001724 return DAG.getSetCC(VT, ORNode, LR, Op1);
1725 }
1726 }
1727 // canonicalize equivalent to ll == rl
1728 if (LL == RR && LR == RL) {
1729 Op1 = ISD::getSetCCSwappedOperands(Op1);
1730 std::swap(RL, RR);
1731 }
1732 if (LL == RL && LR == RR) {
Duncan Sands92c43912008-06-06 12:08:01 +00001733 bool isInteger = LL.getValueType().isInteger();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001734 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
Chris Lattner3ab74bd2008-10-28 07:11:07 +00001735 if (Result != ISD::SETCC_INVALID &&
1736 (!AfterLegalize || TLI.isCondCodeLegal(Result, LL.getValueType())))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001737 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1738 }
1739 }
1740
1741 // Simplify: and (op x...), (op y...) -> (op (and x, y))
1742 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001743 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00001744 if (Tmp.getNode()) return Tmp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001745 }
1746
1747 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
1748 // fold (and (sra)) -> (and (srl)) when possible.
Duncan Sands92c43912008-06-06 12:08:01 +00001749 if (!VT.isVector() &&
Dan Gohman8181bd12008-07-27 21:46:04 +00001750 SimplifyDemandedBits(SDValue(N, 0)))
1751 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001752 // fold (zext_inreg (extload x)) -> (zextload x)
Gabor Greif1c80d112008-08-28 21:40:38 +00001753 if (ISD::isEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001754 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands92c43912008-06-06 12:08:01 +00001755 MVT EVT = LN0->getMemoryVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001756 // If we zero all the possible extended bits, then we can turn this into
1757 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman07961cd2008-02-25 21:11:39 +00001758 unsigned BitWidth = N1.getValueSizeInBits();
1759 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Duncan Sands92c43912008-06-06 12:08:01 +00001760 BitWidth - EVT.getSizeInBits())) &&
Duncan Sands2418bec2008-06-13 19:07:40 +00001761 ((!AfterLegalize && !LN0->isVolatile()) ||
Evan Cheng08c171a2008-10-14 21:26:46 +00001762 TLI.isLoadExtLegal(ISD::ZEXTLOAD, EVT))) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001763 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001764 LN0->getBasePtr(), LN0->getSrcValue(),
1765 LN0->getSrcValueOffset(), EVT,
1766 LN0->isVolatile(),
1767 LN0->getAlignment());
1768 AddToWorkList(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00001769 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00001770 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001771 }
1772 }
1773 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Gabor Greif1c80d112008-08-28 21:40:38 +00001774 if (ISD::isSEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001775 N0.hasOneUse()) {
1776 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands92c43912008-06-06 12:08:01 +00001777 MVT EVT = LN0->getMemoryVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001778 // If we zero all the possible extended bits, then we can turn this into
1779 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman07961cd2008-02-25 21:11:39 +00001780 unsigned BitWidth = N1.getValueSizeInBits();
1781 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Duncan Sands92c43912008-06-06 12:08:01 +00001782 BitWidth - EVT.getSizeInBits())) &&
Duncan Sands2418bec2008-06-13 19:07:40 +00001783 ((!AfterLegalize && !LN0->isVolatile()) ||
Evan Cheng08c171a2008-10-14 21:26:46 +00001784 TLI.isLoadExtLegal(ISD::ZEXTLOAD, EVT))) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001785 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001786 LN0->getBasePtr(), LN0->getSrcValue(),
1787 LN0->getSrcValueOffset(), EVT,
1788 LN0->isVolatile(),
1789 LN0->getAlignment());
1790 AddToWorkList(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00001791 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00001792 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001793 }
1794 }
1795
1796 // fold (and (load x), 255) -> (zextload x, i8)
1797 // fold (and (extload x, i16), 255) -> (zextload x, i8)
1798 if (N1C && N0.getOpcode() == ISD::LOAD) {
1799 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
1800 if (LN0->getExtensionType() != ISD::SEXTLOAD &&
Duncan Sands2418bec2008-06-13 19:07:40 +00001801 LN0->isUnindexed() && N0.hasOneUse() &&
1802 // Do not change the width of a volatile load.
1803 !LN0->isVolatile()) {
Duncan Sands6a437fb2008-06-09 11:32:28 +00001804 MVT EVT = MVT::Other;
1805 uint32_t ActiveBits = N1C->getAPIntValue().getActiveBits();
1806 if (ActiveBits > 0 && APIntOps::isMask(ActiveBits, N1C->getAPIntValue()))
1807 EVT = MVT::getIntegerVT(ActiveBits);
1808
1809 MVT LoadedVT = LN0->getMemoryVT();
Duncan Sands3ea93352008-06-16 08:14:38 +00001810 // Do not generate loads of non-round integer types since these can
1811 // be expensive (and would be wrong if the type is not byte sized).
1812 if (EVT != MVT::Other && LoadedVT.bitsGT(EVT) && EVT.isRound() &&
Evan Cheng08c171a2008-10-14 21:26:46 +00001813 (!AfterLegalize || TLI.isLoadExtLegal(ISD::ZEXTLOAD, EVT))) {
Duncan Sands92c43912008-06-06 12:08:01 +00001814 MVT PtrType = N0.getOperand(1).getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001815 // For big endian targets, we need to add an offset to the pointer to
1816 // load the correct bytes. For little endian systems, we merely need to
1817 // read fewer bytes from the same pointer.
Duncan Sands92c43912008-06-06 12:08:01 +00001818 unsigned LVTStoreBytes = LoadedVT.getStoreSizeInBits()/8;
1819 unsigned EVTStoreBytes = EVT.getStoreSizeInBits()/8;
Duncan Sands4f18d4f2007-11-09 08:57:19 +00001820 unsigned PtrOff = LVTStoreBytes - EVTStoreBytes;
Duncan Sandsa3691432007-10-28 12:59:45 +00001821 unsigned Alignment = LN0->getAlignment();
Dan Gohman8181bd12008-07-27 21:46:04 +00001822 SDValue NewPtr = LN0->getBasePtr();
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00001823 if (TLI.isBigEndian()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001824 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
1825 DAG.getConstant(PtrOff, PtrType));
Duncan Sandsa3691432007-10-28 12:59:45 +00001826 Alignment = MinAlign(Alignment, PtrOff);
1827 }
Gabor Greif1c80d112008-08-28 21:40:38 +00001828 AddToWorkList(NewPtr.getNode());
Dan Gohman8181bd12008-07-27 21:46:04 +00001829 SDValue Load =
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001830 DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(), NewPtr,
1831 LN0->getSrcValue(), LN0->getSrcValueOffset(), EVT,
Duncan Sandsa3691432007-10-28 12:59:45 +00001832 LN0->isVolatile(), Alignment);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001833 AddToWorkList(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00001834 CombineTo(N0.getNode(), Load, Load.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00001835 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001836 }
1837 }
1838 }
1839
Dan Gohman8181bd12008-07-27 21:46:04 +00001840 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001841}
1842
Dan Gohman8181bd12008-07-27 21:46:04 +00001843SDValue DAGCombiner::visitOR(SDNode *N) {
1844 SDValue N0 = N->getOperand(0);
1845 SDValue N1 = N->getOperand(1);
1846 SDValue LL, LR, RL, RR, CC0, CC1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001847 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1848 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands92c43912008-06-06 12:08:01 +00001849 MVT VT = N1.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001850
1851 // fold vector ops
Duncan Sands92c43912008-06-06 12:08:01 +00001852 if (VT.isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001853 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00001854 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001855 }
1856
1857 // fold (or x, undef) -> -1
1858 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
1859 return DAG.getConstant(~0ULL, VT);
1860 // fold (or c1, c2) -> c1|c2
1861 if (N0C && N1C)
Bill Wendling0445c4c2008-09-24 10:25:02 +00001862 return DAG.FoldConstantArithmetic(ISD::OR, VT, N0C, N1C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001863 // canonicalize constant to RHS
1864 if (N0C && !N1C)
1865 return DAG.getNode(ISD::OR, VT, N1, N0);
1866 // fold (or x, 0) -> x
1867 if (N1C && N1C->isNullValue())
1868 return N0;
1869 // fold (or x, -1) -> -1
1870 if (N1C && N1C->isAllOnesValue())
1871 return N1;
1872 // fold (or x, c) -> c iff (x & ~c) == 0
Dan Gohman07961cd2008-02-25 21:11:39 +00001873 if (N1C && DAG.MaskedValueIsZero(N0, ~N1C->getAPIntValue()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001874 return N1;
1875 // reassociate or
Dan Gohman8181bd12008-07-27 21:46:04 +00001876 SDValue ROR = ReassociateOps(ISD::OR, N0, N1);
Gabor Greif1c80d112008-08-28 21:40:38 +00001877 if (ROR.getNode() != 0)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001878 return ROR;
1879 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
Gabor Greif1c80d112008-08-28 21:40:38 +00001880 if (N1C && N0.getOpcode() == ISD::AND && N0.getNode()->hasOneUse() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001881 isa<ConstantSDNode>(N0.getOperand(1))) {
1882 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
1883 return DAG.getNode(ISD::AND, VT, DAG.getNode(ISD::OR, VT, N0.getOperand(0),
1884 N1),
Dan Gohman9d24dc72008-03-13 22:13:53 +00001885 DAG.getConstant(N1C->getAPIntValue() |
1886 C1->getAPIntValue(), VT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001887 }
1888 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
1889 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1890 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1891 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1892
1893 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands92c43912008-06-06 12:08:01 +00001894 LL.getValueType().isInteger()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001895 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
1896 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
Dan Gohman9d24dc72008-03-13 22:13:53 +00001897 if (cast<ConstantSDNode>(LR)->isNullValue() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001898 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001899 SDValue ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Gabor Greif1c80d112008-08-28 21:40:38 +00001900 AddToWorkList(ORNode.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001901 return DAG.getSetCC(VT, ORNode, LR, Op1);
1902 }
1903 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
1904 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
1905 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
1906 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001907 SDValue ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Gabor Greif1c80d112008-08-28 21:40:38 +00001908 AddToWorkList(ANDNode.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001909 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1910 }
1911 }
1912 // canonicalize equivalent to ll == rl
1913 if (LL == RR && LR == RL) {
1914 Op1 = ISD::getSetCCSwappedOperands(Op1);
1915 std::swap(RL, RR);
1916 }
1917 if (LL == RL && LR == RR) {
Duncan Sands92c43912008-06-06 12:08:01 +00001918 bool isInteger = LL.getValueType().isInteger();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001919 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
Chris Lattner3ab74bd2008-10-28 07:11:07 +00001920 if (Result != ISD::SETCC_INVALID &&
1921 (!AfterLegalize || TLI.isCondCodeLegal(Result, LL.getValueType())))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001922 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1923 }
1924 }
1925
1926 // Simplify: or (op x...), (op y...) -> (op (or x, y))
1927 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001928 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00001929 if (Tmp.getNode()) return Tmp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001930 }
1931
1932 // (X & C1) | (Y & C2) -> (X|Y) & C3 if possible.
1933 if (N0.getOpcode() == ISD::AND &&
1934 N1.getOpcode() == ISD::AND &&
1935 N0.getOperand(1).getOpcode() == ISD::Constant &&
1936 N1.getOperand(1).getOpcode() == ISD::Constant &&
1937 // Don't increase # computations.
Gabor Greif1c80d112008-08-28 21:40:38 +00001938 (N0.getNode()->hasOneUse() || N1.getNode()->hasOneUse())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001939 // We can only do this xform if we know that bits from X that are set in C2
1940 // but not in C1 are already zero. Likewise for Y.
Dan Gohman07961cd2008-02-25 21:11:39 +00001941 const APInt &LHSMask =
1942 cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
1943 const APInt &RHSMask =
1944 cast<ConstantSDNode>(N1.getOperand(1))->getAPIntValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001945
1946 if (DAG.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) &&
1947 DAG.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001948 SDValue X =DAG.getNode(ISD::OR, VT, N0.getOperand(0), N1.getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001949 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(LHSMask|RHSMask, VT));
1950 }
1951 }
1952
1953
1954 // See if this is some rotate idiom.
1955 if (SDNode *Rot = MatchRotate(N0, N1))
Dan Gohman8181bd12008-07-27 21:46:04 +00001956 return SDValue(Rot, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001957
Dan Gohman8181bd12008-07-27 21:46:04 +00001958 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001959}
1960
1961
1962/// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present.
Dan Gohman8181bd12008-07-27 21:46:04 +00001963static bool MatchRotateHalf(SDValue Op, SDValue &Shift, SDValue &Mask) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001964 if (Op.getOpcode() == ISD::AND) {
1965 if (isa<ConstantSDNode>(Op.getOperand(1))) {
1966 Mask = Op.getOperand(1);
1967 Op = Op.getOperand(0);
1968 } else {
1969 return false;
1970 }
1971 }
1972
1973 if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) {
1974 Shift = Op;
1975 return true;
1976 }
1977 return false;
1978}
1979
1980
1981// MatchRotate - Handle an 'or' of two operands. If this is one of the many
1982// idioms for rotate, and if the target supports rotation instructions, generate
1983// a rot[lr].
Dan Gohman8181bd12008-07-27 21:46:04 +00001984SDNode *DAGCombiner::MatchRotate(SDValue LHS, SDValue RHS) {
Duncan Sands2418bec2008-06-13 19:07:40 +00001985 // Must be a legal type. Expanded 'n promoted things won't work with rotates.
Duncan Sands92c43912008-06-06 12:08:01 +00001986 MVT VT = LHS.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001987 if (!TLI.isTypeLegal(VT)) return 0;
1988
1989 // The target must have at least one rotate flavor.
1990 bool HasROTL = TLI.isOperationLegal(ISD::ROTL, VT);
1991 bool HasROTR = TLI.isOperationLegal(ISD::ROTR, VT);
1992 if (!HasROTL && !HasROTR) return 0;
Duncan Sands2418bec2008-06-13 19:07:40 +00001993
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001994 // Match "(X shl/srl V1) & V2" where V2 may not be present.
Dan Gohman8181bd12008-07-27 21:46:04 +00001995 SDValue LHSShift; // The shift.
1996 SDValue LHSMask; // AND value if any.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001997 if (!MatchRotateHalf(LHS, LHSShift, LHSMask))
1998 return 0; // Not part of a rotate.
1999
Dan Gohman8181bd12008-07-27 21:46:04 +00002000 SDValue RHSShift; // The shift.
2001 SDValue RHSMask; // AND value if any.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002002 if (!MatchRotateHalf(RHS, RHSShift, RHSMask))
2003 return 0; // Not part of a rotate.
2004
2005 if (LHSShift.getOperand(0) != RHSShift.getOperand(0))
2006 return 0; // Not shifting the same value.
2007
2008 if (LHSShift.getOpcode() == RHSShift.getOpcode())
2009 return 0; // Shifts must disagree.
2010
2011 // Canonicalize shl to left side in a shl/srl pair.
2012 if (RHSShift.getOpcode() == ISD::SHL) {
2013 std::swap(LHS, RHS);
2014 std::swap(LHSShift, RHSShift);
2015 std::swap(LHSMask , RHSMask );
2016 }
2017
Duncan Sands92c43912008-06-06 12:08:01 +00002018 unsigned OpSizeInBits = VT.getSizeInBits();
Dan Gohman8181bd12008-07-27 21:46:04 +00002019 SDValue LHSShiftArg = LHSShift.getOperand(0);
2020 SDValue LHSShiftAmt = LHSShift.getOperand(1);
2021 SDValue RHSShiftAmt = RHSShift.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002022
2023 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
2024 // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2)
2025 if (LHSShiftAmt.getOpcode() == ISD::Constant &&
2026 RHSShiftAmt.getOpcode() == ISD::Constant) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002027 uint64_t LShVal = cast<ConstantSDNode>(LHSShiftAmt)->getZExtValue();
2028 uint64_t RShVal = cast<ConstantSDNode>(RHSShiftAmt)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002029 if ((LShVal + RShVal) != OpSizeInBits)
2030 return 0;
2031
Dan Gohman8181bd12008-07-27 21:46:04 +00002032 SDValue Rot;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002033 if (HasROTL)
2034 Rot = DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt);
2035 else
2036 Rot = DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt);
2037
2038 // If there is an AND of either shifted operand, apply it to the result.
Gabor Greif1c80d112008-08-28 21:40:38 +00002039 if (LHSMask.getNode() || RHSMask.getNode()) {
Dan Gohmand047c3e2008-03-03 23:51:38 +00002040 APInt Mask = APInt::getAllOnesValue(OpSizeInBits);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002041
Gabor Greif1c80d112008-08-28 21:40:38 +00002042 if (LHSMask.getNode()) {
Dan Gohmand047c3e2008-03-03 23:51:38 +00002043 APInt RHSBits = APInt::getLowBitsSet(OpSizeInBits, LShVal);
2044 Mask &= cast<ConstantSDNode>(LHSMask)->getAPIntValue() | RHSBits;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002045 }
Gabor Greif1c80d112008-08-28 21:40:38 +00002046 if (RHSMask.getNode()) {
Dan Gohmand047c3e2008-03-03 23:51:38 +00002047 APInt LHSBits = APInt::getHighBitsSet(OpSizeInBits, RShVal);
2048 Mask &= cast<ConstantSDNode>(RHSMask)->getAPIntValue() | LHSBits;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002049 }
2050
2051 Rot = DAG.getNode(ISD::AND, VT, Rot, DAG.getConstant(Mask, VT));
2052 }
2053
Gabor Greif1c80d112008-08-28 21:40:38 +00002054 return Rot.getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002055 }
2056
2057 // If there is a mask here, and we have a variable shift, we can't be sure
2058 // that we're masking out the right stuff.
Gabor Greif1c80d112008-08-28 21:40:38 +00002059 if (LHSMask.getNode() || RHSMask.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002060 return 0;
2061
2062 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
2063 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotr x, (sub 32, y))
2064 if (RHSShiftAmt.getOpcode() == ISD::SUB &&
2065 LHSShiftAmt == RHSShiftAmt.getOperand(1)) {
2066 if (ConstantSDNode *SUBC =
2067 dyn_cast<ConstantSDNode>(RHSShiftAmt.getOperand(0))) {
Dan Gohman9d24dc72008-03-13 22:13:53 +00002068 if (SUBC->getAPIntValue() == OpSizeInBits) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002069 if (HasROTL)
Gabor Greif1c80d112008-08-28 21:40:38 +00002070 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002071 else
Gabor Greif1c80d112008-08-28 21:40:38 +00002072 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).getNode();
Anton Korobeynikov53422f62008-02-20 11:10:28 +00002073 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002074 }
2075 }
2076
2077 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
2078 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotl x, (sub 32, y))
2079 if (LHSShiftAmt.getOpcode() == ISD::SUB &&
2080 RHSShiftAmt == LHSShiftAmt.getOperand(1)) {
2081 if (ConstantSDNode *SUBC =
2082 dyn_cast<ConstantSDNode>(LHSShiftAmt.getOperand(0))) {
Dan Gohman9d24dc72008-03-13 22:13:53 +00002083 if (SUBC->getAPIntValue() == OpSizeInBits) {
Bill Wendling89f05f52008-08-31 01:13:31 +00002084 if (HasROTR)
Gabor Greif1c80d112008-08-28 21:40:38 +00002085 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).getNode();
Bill Wendling89f05f52008-08-31 01:13:31 +00002086 else
2087 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).getNode();
Anton Korobeynikov53422f62008-02-20 11:10:28 +00002088 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002089 }
2090 }
2091
Dan Gohman921581d2008-10-17 01:23:35 +00002092 // Look for sign/zext/any-extended or truncate cases:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002093 if ((LHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
2094 || LHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
Dan Gohman921581d2008-10-17 01:23:35 +00002095 || LHSShiftAmt.getOpcode() == ISD::ANY_EXTEND
2096 || LHSShiftAmt.getOpcode() == ISD::TRUNCATE) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002097 (RHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
2098 || RHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
Dan Gohman921581d2008-10-17 01:23:35 +00002099 || RHSShiftAmt.getOpcode() == ISD::ANY_EXTEND
2100 || RHSShiftAmt.getOpcode() == ISD::TRUNCATE)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002101 SDValue LExtOp0 = LHSShiftAmt.getOperand(0);
2102 SDValue RExtOp0 = RHSShiftAmt.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002103 if (RExtOp0.getOpcode() == ISD::SUB &&
2104 RExtOp0.getOperand(1) == LExtOp0) {
2105 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
Bill Wendlingf3dd7392008-08-31 00:37:27 +00002106 // (rotl x, y)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002107 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
Bill Wendlingf3dd7392008-08-31 00:37:27 +00002108 // (rotr x, (sub 32, y))
Dan Gohman921581d2008-10-17 01:23:35 +00002109 if (ConstantSDNode *SUBC =
2110 dyn_cast<ConstantSDNode>(RExtOp0.getOperand(0))) {
Dan Gohman9d24dc72008-03-13 22:13:53 +00002111 if (SUBC->getAPIntValue() == OpSizeInBits) {
Gabor Greifb420b9d2008-08-30 19:29:20 +00002112 return DAG.getNode(HasROTL ? ISD::ROTL : ISD::ROTR, VT, LHSShiftArg,
2113 HasROTL ? LHSShiftAmt : RHSShiftAmt).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002114 }
2115 }
2116 } else if (LExtOp0.getOpcode() == ISD::SUB &&
2117 RExtOp0 == LExtOp0.getOperand(1)) {
Bill Wendlinga70293d2008-08-31 01:04:56 +00002118 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext y))) ->
Bill Wendlingf3dd7392008-08-31 00:37:27 +00002119 // (rotr x, y)
Bill Wendlinga70293d2008-08-31 01:04:56 +00002120 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext y))) ->
Bill Wendlingf3dd7392008-08-31 00:37:27 +00002121 // (rotl x, (sub 32, y))
Dan Gohman921581d2008-10-17 01:23:35 +00002122 if (ConstantSDNode *SUBC =
2123 dyn_cast<ConstantSDNode>(LExtOp0.getOperand(0))) {
Dan Gohman9d24dc72008-03-13 22:13:53 +00002124 if (SUBC->getAPIntValue() == OpSizeInBits) {
Bill Wendlinga70293d2008-08-31 01:04:56 +00002125 return DAG.getNode(HasROTR ? ISD::ROTR : ISD::ROTL, VT, LHSShiftArg,
2126 HasROTR ? RHSShiftAmt : LHSShiftAmt).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002127 }
2128 }
2129 }
2130 }
2131
2132 return 0;
2133}
2134
2135
Dan Gohman8181bd12008-07-27 21:46:04 +00002136SDValue DAGCombiner::visitXOR(SDNode *N) {
2137 SDValue N0 = N->getOperand(0);
2138 SDValue N1 = N->getOperand(1);
2139 SDValue LHS, RHS, CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002140 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2141 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands92c43912008-06-06 12:08:01 +00002142 MVT VT = N0.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002143
2144 // fold vector ops
Duncan Sands92c43912008-06-06 12:08:01 +00002145 if (VT.isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002146 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00002147 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002148 }
2149
Evan Cheng5d00cb42008-03-25 20:08:07 +00002150 // fold (xor undef, undef) -> 0. This is a common idiom (misuse).
2151 if (N0.getOpcode() == ISD::UNDEF && N1.getOpcode() == ISD::UNDEF)
2152 return DAG.getConstant(0, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002153 // fold (xor x, undef) -> undef
2154 if (N0.getOpcode() == ISD::UNDEF)
2155 return N0;
2156 if (N1.getOpcode() == ISD::UNDEF)
2157 return N1;
2158 // fold (xor c1, c2) -> c1^c2
2159 if (N0C && N1C)
Bill Wendling0445c4c2008-09-24 10:25:02 +00002160 return DAG.FoldConstantArithmetic(ISD::XOR, VT, N0C, N1C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002161 // canonicalize constant to RHS
2162 if (N0C && !N1C)
2163 return DAG.getNode(ISD::XOR, VT, N1, N0);
2164 // fold (xor x, 0) -> x
2165 if (N1C && N1C->isNullValue())
2166 return N0;
2167 // reassociate xor
Dan Gohman8181bd12008-07-27 21:46:04 +00002168 SDValue RXOR = ReassociateOps(ISD::XOR, N0, N1);
Gabor Greif1c80d112008-08-28 21:40:38 +00002169 if (RXOR.getNode() != 0)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002170 return RXOR;
Bill Wendling0d810b82008-11-11 08:25:46 +00002171
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002172 // fold !(x cc y) -> (x !cc y)
Dan Gohman9d24dc72008-03-13 22:13:53 +00002173 if (N1C && N1C->getAPIntValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
Duncan Sands92c43912008-06-06 12:08:01 +00002174 bool isInt = LHS.getValueType().isInteger();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002175 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
2176 isInt);
Bill Wendling0d810b82008-11-11 08:25:46 +00002177
2178 if (!AfterLegalize || TLI.isCondCodeLegal(NotCC, LHS.getValueType())) {
2179 switch (N0.getOpcode()) {
2180 default:
2181 assert(0 && "Unhandled SetCC Equivalent!");
2182 abort();
2183 case ISD::SETCC:
2184 return DAG.getSetCC(VT, LHS, RHS, NotCC);
2185 case ISD::SELECT_CC:
2186 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),
2187 N0.getOperand(3), NotCC);
2188 }
2189 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002190 }
Bill Wendling0d810b82008-11-11 08:25:46 +00002191
Chris Lattnere27cd502007-09-10 21:39:07 +00002192 // fold (not (zext (setcc x, y))) -> (zext (not (setcc x, y)))
Dan Gohman9d24dc72008-03-13 22:13:53 +00002193 if (N1C && N1C->getAPIntValue() == 1 && N0.getOpcode() == ISD::ZERO_EXTEND &&
Gabor Greifb420b9d2008-08-30 19:29:20 +00002194 N0.getNode()->hasOneUse() &&
2195 isSetCCEquivalent(N0.getOperand(0), LHS, RHS, CC)){
Dan Gohman8181bd12008-07-27 21:46:04 +00002196 SDValue V = N0.getOperand(0);
Chris Lattnere27cd502007-09-10 21:39:07 +00002197 V = DAG.getNode(ISD::XOR, V.getValueType(), V,
Duncan Sandsbed21472007-10-10 09:54:50 +00002198 DAG.getConstant(1, V.getValueType()));
Gabor Greif1c80d112008-08-28 21:40:38 +00002199 AddToWorkList(V.getNode());
Chris Lattnere27cd502007-09-10 21:39:07 +00002200 return DAG.getNode(ISD::ZERO_EXTEND, VT, V);
2201 }
2202
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002203 // fold !(x or y) -> (!x and !y) iff x or y are setcc
Dan Gohman9d24dc72008-03-13 22:13:53 +00002204 if (N1C && N1C->getAPIntValue() == 1 && VT == MVT::i1 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002205 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002206 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002207 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
2208 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
2209 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
2210 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Gabor Greif1c80d112008-08-28 21:40:38 +00002211 AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002212 return DAG.getNode(NewOpcode, VT, LHS, RHS);
2213 }
2214 }
2215 // fold !(x or y) -> (!x and !y) iff x or y are constants
2216 if (N1C && N1C->isAllOnesValue() &&
2217 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002218 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002219 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
2220 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
2221 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
2222 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Gabor Greif1c80d112008-08-28 21:40:38 +00002223 AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002224 return DAG.getNode(NewOpcode, VT, LHS, RHS);
2225 }
2226 }
2227 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
2228 if (N1C && N0.getOpcode() == ISD::XOR) {
2229 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
2230 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2231 if (N00C)
2232 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
Dan Gohman9d24dc72008-03-13 22:13:53 +00002233 DAG.getConstant(N1C->getAPIntValue()^
2234 N00C->getAPIntValue(), VT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002235 if (N01C)
2236 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
Dan Gohman9d24dc72008-03-13 22:13:53 +00002237 DAG.getConstant(N1C->getAPIntValue()^
2238 N01C->getAPIntValue(), VT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002239 }
2240 // fold (xor x, x) -> 0
2241 if (N0 == N1) {
Duncan Sands92c43912008-06-06 12:08:01 +00002242 if (!VT.isVector()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002243 return DAG.getConstant(0, VT);
2244 } else if (!AfterLegalize || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) {
2245 // Produce a vector of zeros.
Dan Gohman8181bd12008-07-27 21:46:04 +00002246 SDValue El = DAG.getConstant(0, VT.getVectorElementType());
2247 std::vector<SDValue> Ops(VT.getVectorNumElements(), El);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002248 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
2249 }
2250 }
2251
2252 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
2253 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002254 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00002255 if (Tmp.getNode()) return Tmp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002256 }
2257
2258 // Simplify the expression using non-local knowledge.
Duncan Sands92c43912008-06-06 12:08:01 +00002259 if (!VT.isVector() &&
Dan Gohman8181bd12008-07-27 21:46:04 +00002260 SimplifyDemandedBits(SDValue(N, 0)))
2261 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002262
Dan Gohman8181bd12008-07-27 21:46:04 +00002263 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002264}
2265
Chris Lattner91ed3c32007-12-06 07:33:36 +00002266/// visitShiftByConstant - Handle transforms common to the three shifts, when
2267/// the shift amount is a constant.
Dan Gohman8181bd12008-07-27 21:46:04 +00002268SDValue DAGCombiner::visitShiftByConstant(SDNode *N, unsigned Amt) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002269 SDNode *LHS = N->getOperand(0).getNode();
Dan Gohman8181bd12008-07-27 21:46:04 +00002270 if (!LHS->hasOneUse()) return SDValue();
Chris Lattner91ed3c32007-12-06 07:33:36 +00002271
2272 // We want to pull some binops through shifts, so that we have (and (shift))
2273 // instead of (shift (and)), likewise for add, or, xor, etc. This sort of
2274 // thing happens with address calculations, so it's important to canonicalize
2275 // it.
2276 bool HighBitSet = false; // Can we transform this if the high bit is set?
2277
2278 switch (LHS->getOpcode()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002279 default: return SDValue();
Chris Lattner91ed3c32007-12-06 07:33:36 +00002280 case ISD::OR:
2281 case ISD::XOR:
2282 HighBitSet = false; // We can only transform sra if the high bit is clear.
2283 break;
2284 case ISD::AND:
2285 HighBitSet = true; // We can only transform sra if the high bit is set.
2286 break;
2287 case ISD::ADD:
2288 if (N->getOpcode() != ISD::SHL)
Dan Gohman8181bd12008-07-27 21:46:04 +00002289 return SDValue(); // only shl(add) not sr[al](add).
Chris Lattner91ed3c32007-12-06 07:33:36 +00002290 HighBitSet = false; // We can only transform sra if the high bit is clear.
2291 break;
2292 }
2293
2294 // We require the RHS of the binop to be a constant as well.
2295 ConstantSDNode *BinOpCst = dyn_cast<ConstantSDNode>(LHS->getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00002296 if (!BinOpCst) return SDValue();
Chris Lattner91ed3c32007-12-06 07:33:36 +00002297
Chris Lattnerdcd19762007-12-06 07:47:55 +00002298
2299 // FIXME: disable this for unless the input to the binop is a shift by a
2300 // constant. If it is not a shift, it pessimizes some common cases like:
2301 //
2302 //void foo(int *X, int i) { X[i & 1235] = 1; }
2303 //int bar(int *X, int i) { return X[i & 255]; }
Gabor Greif1c80d112008-08-28 21:40:38 +00002304 SDNode *BinOpLHSVal = LHS->getOperand(0).getNode();
Chris Lattnerdcd19762007-12-06 07:47:55 +00002305 if ((BinOpLHSVal->getOpcode() != ISD::SHL &&
2306 BinOpLHSVal->getOpcode() != ISD::SRA &&
2307 BinOpLHSVal->getOpcode() != ISD::SRL) ||
2308 !isa<ConstantSDNode>(BinOpLHSVal->getOperand(1)))
Dan Gohman8181bd12008-07-27 21:46:04 +00002309 return SDValue();
Chris Lattnerdcd19762007-12-06 07:47:55 +00002310
Duncan Sands92c43912008-06-06 12:08:01 +00002311 MVT VT = N->getValueType(0);
Chris Lattner91ed3c32007-12-06 07:33:36 +00002312
2313 // If this is a signed shift right, and the high bit is modified
2314 // by the logical operation, do not perform the transformation.
2315 // The highBitSet boolean indicates the value of the high bit of
2316 // the constant which would cause it to be modified for this
2317 // operation.
2318 if (N->getOpcode() == ISD::SRA) {
Dan Gohmand047c3e2008-03-03 23:51:38 +00002319 bool BinOpRHSSignSet = BinOpCst->getAPIntValue().isNegative();
2320 if (BinOpRHSSignSet != HighBitSet)
Dan Gohman8181bd12008-07-27 21:46:04 +00002321 return SDValue();
Chris Lattner91ed3c32007-12-06 07:33:36 +00002322 }
2323
2324 // Fold the constants, shifting the binop RHS by the shift amount.
Dan Gohman8181bd12008-07-27 21:46:04 +00002325 SDValue NewRHS = DAG.getNode(N->getOpcode(), N->getValueType(0),
Chris Lattner91ed3c32007-12-06 07:33:36 +00002326 LHS->getOperand(1), N->getOperand(1));
2327
2328 // Create the new shift.
Dan Gohman8181bd12008-07-27 21:46:04 +00002329 SDValue NewShift = DAG.getNode(N->getOpcode(), VT, LHS->getOperand(0),
Chris Lattner91ed3c32007-12-06 07:33:36 +00002330 N->getOperand(1));
2331
2332 // Create the new binop.
2333 return DAG.getNode(LHS->getOpcode(), VT, NewShift, NewRHS);
2334}
2335
2336
Dan Gohman8181bd12008-07-27 21:46:04 +00002337SDValue DAGCombiner::visitSHL(SDNode *N) {
2338 SDValue N0 = N->getOperand(0);
2339 SDValue N1 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002340 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2341 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands92c43912008-06-06 12:08:01 +00002342 MVT VT = N0.getValueType();
2343 unsigned OpSizeInBits = VT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002344
2345 // fold (shl c1, c2) -> c1<<c2
2346 if (N0C && N1C)
Bill Wendling0445c4c2008-09-24 10:25:02 +00002347 return DAG.FoldConstantArithmetic(ISD::SHL, VT, N0C, N1C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002348 // fold (shl 0, x) -> 0
2349 if (N0C && N0C->isNullValue())
2350 return N0;
2351 // fold (shl x, c >= size(x)) -> undef
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002352 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002353 return DAG.getNode(ISD::UNDEF, VT);
2354 // fold (shl x, 0) -> x
2355 if (N1C && N1C->isNullValue())
2356 return N0;
2357 // if (shl x, c) is known to be zero, return 0
Dan Gohman8181bd12008-07-27 21:46:04 +00002358 if (DAG.MaskedValueIsZero(SDValue(N, 0),
Duncan Sands92c43912008-06-06 12:08:01 +00002359 APInt::getAllOnesValue(VT.getSizeInBits())))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002360 return DAG.getConstant(0, VT);
Evan Cheng76a64c72008-08-30 02:03:58 +00002361 // fold (shl x, (trunc (and y, c))) -> (shl x, (and (trunc y), c))
2362 // iff (trunc c) == c
2363 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng11c34292008-09-22 18:19:24 +00002364 N1.getOperand(0).getOpcode() == ISD::AND &&
2365 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Cheng76a64c72008-08-30 02:03:58 +00002366 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng11c34292008-09-22 18:19:24 +00002367 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Evan Cheng76a64c72008-08-30 02:03:58 +00002368 MVT TruncVT = N1.getValueType();
Evan Cheng11c34292008-09-22 18:19:24 +00002369 SDValue N100 = N1.getOperand(0).getOperand(0);
2370 return DAG.getNode(ISD::SHL, VT, N0,
2371 DAG.getNode(ISD::AND, TruncVT,
2372 DAG.getNode(ISD::TRUNCATE, TruncVT, N100),
2373 DAG.getConstant(N101C->getZExtValue(),
2374 TruncVT)));
Evan Cheng76a64c72008-08-30 02:03:58 +00002375 }
2376 }
2377
Dan Gohman8181bd12008-07-27 21:46:04 +00002378 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
2379 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002380 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
2381 if (N1C && N0.getOpcode() == ISD::SHL &&
2382 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002383 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
2384 uint64_t c2 = N1C->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002385 if (c1 + c2 > OpSizeInBits)
2386 return DAG.getConstant(0, VT);
2387 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
2388 DAG.getConstant(c1 + c2, N1.getValueType()));
2389 }
2390 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
2391 // (srl (and x, -1 << c1), c1-c2)
2392 if (N1C && N0.getOpcode() == ISD::SRL &&
2393 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002394 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
2395 uint64_t c2 = N1C->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00002396 SDValue Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002397 DAG.getConstant(~0ULL << c1, VT));
2398 if (c2 > c1)
2399 return DAG.getNode(ISD::SHL, VT, Mask,
2400 DAG.getConstant(c2-c1, N1.getValueType()));
2401 else
2402 return DAG.getNode(ISD::SRL, VT, Mask,
2403 DAG.getConstant(c1-c2, N1.getValueType()));
2404 }
2405 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
2406 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
2407 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002408 DAG.getConstant(~0ULL << N1C->getZExtValue(), VT));
Chris Lattner91ed3c32007-12-06 07:33:36 +00002409
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002410 return N1C ? visitShiftByConstant(N, N1C->getZExtValue()) : SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002411}
2412
Dan Gohman8181bd12008-07-27 21:46:04 +00002413SDValue DAGCombiner::visitSRA(SDNode *N) {
2414 SDValue N0 = N->getOperand(0);
2415 SDValue N1 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002416 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2417 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands92c43912008-06-06 12:08:01 +00002418 MVT VT = N0.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002419
2420 // fold (sra c1, c2) -> c1>>c2
2421 if (N0C && N1C)
Bill Wendling0445c4c2008-09-24 10:25:02 +00002422 return DAG.FoldConstantArithmetic(ISD::SRA, VT, N0C, N1C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002423 // fold (sra 0, x) -> 0
2424 if (N0C && N0C->isNullValue())
2425 return N0;
2426 // fold (sra -1, x) -> -1
2427 if (N0C && N0C->isAllOnesValue())
2428 return N0;
2429 // fold (sra x, c >= size(x)) -> undef
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002430 if (N1C && N1C->getZExtValue() >= VT.getSizeInBits())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002431 return DAG.getNode(ISD::UNDEF, VT);
2432 // fold (sra x, 0) -> x
2433 if (N1C && N1C->isNullValue())
2434 return N0;
2435 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
2436 // sext_inreg.
2437 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002438 unsigned LowBits = VT.getSizeInBits() - (unsigned)N1C->getZExtValue();
Duncan Sands6a437fb2008-06-09 11:32:28 +00002439 MVT EVT = MVT::getIntegerVT(LowBits);
Duncan Sands2418bec2008-06-13 19:07:40 +00002440 if (EVT.isSimple() && // TODO: remove when apint codegen support lands.
2441 (!AfterLegalize || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, EVT)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002442 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
2443 DAG.getValueType(EVT));
2444 }
Duncan Sands2418bec2008-06-13 19:07:40 +00002445
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002446 // fold (sra (sra x, c1), c2) -> (sra x, c1+c2)
2447 if (N1C && N0.getOpcode() == ISD::SRA) {
2448 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002449 unsigned Sum = N1C->getZExtValue() + C1->getZExtValue();
Duncan Sands92c43912008-06-06 12:08:01 +00002450 if (Sum >= VT.getSizeInBits()) Sum = VT.getSizeInBits()-1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002451 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0),
2452 DAG.getConstant(Sum, N1C->getValueType(0)));
2453 }
2454 }
Christopher Lambfc5c1642008-03-19 08:30:06 +00002455
2456 // fold sra (shl X, m), result_size - n
2457 // -> (sign_extend (trunc (shl X, result_size - n - m))) for
Christopher Lamb21e8a952008-03-20 04:31:39 +00002458 // result_size - n != m.
2459 // If truncate is free for the target sext(shl) is likely to result in better
2460 // code.
Christopher Lambfc5c1642008-03-19 08:30:06 +00002461 if (N0.getOpcode() == ISD::SHL) {
2462 // Get the two constanst of the shifts, CN0 = m, CN = n.
2463 const ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2464 if (N01C && N1C) {
Christopher Lamb21e8a952008-03-20 04:31:39 +00002465 // Determine what the truncate's result bitsize and type would be.
Duncan Sands92c43912008-06-06 12:08:01 +00002466 unsigned VTValSize = VT.getSizeInBits();
2467 MVT TruncVT =
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002468 MVT::getIntegerVT(VTValSize - N1C->getZExtValue());
Christopher Lamb21e8a952008-03-20 04:31:39 +00002469 // Determine the residual right-shift amount.
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002470 unsigned ShiftAmt = N1C->getZExtValue() - N01C->getZExtValue();
Duncan Sands2418bec2008-06-13 19:07:40 +00002471
Christopher Lamb21e8a952008-03-20 04:31:39 +00002472 // If the shift is not a no-op (in which case this should be just a sign
2473 // extend already), the truncated to type is legal, sign_extend is legal
Gabor Greifb420b9d2008-08-30 19:29:20 +00002474 // on that type, and the the truncate to that type is both legal and free,
Christopher Lamb21e8a952008-03-20 04:31:39 +00002475 // perform the transform.
2476 if (ShiftAmt &&
Christopher Lamb21e8a952008-03-20 04:31:39 +00002477 TLI.isOperationLegal(ISD::SIGN_EXTEND, TruncVT) &&
2478 TLI.isOperationLegal(ISD::TRUNCATE, VT) &&
Evan Chengca0e80f2008-03-20 02:18:41 +00002479 TLI.isTruncateFree(VT, TruncVT)) {
Christopher Lamb21e8a952008-03-20 04:31:39 +00002480
Dan Gohman8181bd12008-07-27 21:46:04 +00002481 SDValue Amt = DAG.getConstant(ShiftAmt, TLI.getShiftAmountTy());
2482 SDValue Shift = DAG.getNode(ISD::SRL, VT, N0.getOperand(0), Amt);
2483 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, TruncVT, Shift);
Christopher Lamb21e8a952008-03-20 04:31:39 +00002484 return DAG.getNode(ISD::SIGN_EXTEND, N->getValueType(0), Trunc);
Christopher Lambfc5c1642008-03-19 08:30:06 +00002485 }
2486 }
2487 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002488
Evan Cheng76a64c72008-08-30 02:03:58 +00002489 // fold (sra x, (trunc (and y, c))) -> (sra x, (and (trunc y), c))
2490 // iff (trunc c) == c
2491 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng11c34292008-09-22 18:19:24 +00002492 N1.getOperand(0).getOpcode() == ISD::AND &&
2493 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Cheng76a64c72008-08-30 02:03:58 +00002494 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng11c34292008-09-22 18:19:24 +00002495 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Evan Cheng76a64c72008-08-30 02:03:58 +00002496 MVT TruncVT = N1.getValueType();
Evan Cheng11c34292008-09-22 18:19:24 +00002497 SDValue N100 = N1.getOperand(0).getOperand(0);
2498 return DAG.getNode(ISD::SRA, VT, N0,
2499 DAG.getNode(ISD::AND, TruncVT,
2500 DAG.getNode(ISD::TRUNCATE, TruncVT, N100),
2501 DAG.getConstant(N101C->getZExtValue(),
2502 TruncVT)));
Evan Cheng76a64c72008-08-30 02:03:58 +00002503 }
2504 }
2505
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002506 // Simplify, based on bits shifted out of the LHS.
Dan Gohman8181bd12008-07-27 21:46:04 +00002507 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
2508 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002509
2510
2511 // If the sign bit is known to be zero, switch this to a SRL.
Dan Gohman07961cd2008-02-25 21:11:39 +00002512 if (DAG.SignBitIsZero(N0))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002513 return DAG.getNode(ISD::SRL, VT, N0, N1);
Chris Lattner91ed3c32007-12-06 07:33:36 +00002514
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002515 return N1C ? visitShiftByConstant(N, N1C->getZExtValue()) : SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002516}
2517
Dan Gohman8181bd12008-07-27 21:46:04 +00002518SDValue DAGCombiner::visitSRL(SDNode *N) {
2519 SDValue N0 = N->getOperand(0);
2520 SDValue N1 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002521 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2522 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands92c43912008-06-06 12:08:01 +00002523 MVT VT = N0.getValueType();
2524 unsigned OpSizeInBits = VT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002525
2526 // fold (srl c1, c2) -> c1 >>u c2
2527 if (N0C && N1C)
Bill Wendling0445c4c2008-09-24 10:25:02 +00002528 return DAG.FoldConstantArithmetic(ISD::SRL, VT, N0C, N1C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002529 // fold (srl 0, x) -> 0
2530 if (N0C && N0C->isNullValue())
2531 return N0;
2532 // fold (srl x, c >= size(x)) -> undef
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002533 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002534 return DAG.getNode(ISD::UNDEF, VT);
2535 // fold (srl x, 0) -> x
2536 if (N1C && N1C->isNullValue())
2537 return N0;
2538 // if (srl x, c) is known to be zero, return 0
Dan Gohman8181bd12008-07-27 21:46:04 +00002539 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman07961cd2008-02-25 21:11:39 +00002540 APInt::getAllOnesValue(OpSizeInBits)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002541 return DAG.getConstant(0, VT);
2542
2543 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
2544 if (N1C && N0.getOpcode() == ISD::SRL &&
2545 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002546 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
2547 uint64_t c2 = N1C->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002548 if (c1 + c2 > OpSizeInBits)
2549 return DAG.getConstant(0, VT);
2550 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
2551 DAG.getConstant(c1 + c2, N1.getValueType()));
2552 }
2553
2554 // fold (srl (anyextend x), c) -> (anyextend (srl x, c))
2555 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
2556 // Shifting in all undef bits?
Duncan Sands92c43912008-06-06 12:08:01 +00002557 MVT SmallVT = N0.getOperand(0).getValueType();
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002558 if (N1C->getZExtValue() >= SmallVT.getSizeInBits())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002559 return DAG.getNode(ISD::UNDEF, VT);
2560
Dan Gohman8181bd12008-07-27 21:46:04 +00002561 SDValue SmallShift = DAG.getNode(ISD::SRL, SmallVT, N0.getOperand(0), N1);
Gabor Greif1c80d112008-08-28 21:40:38 +00002562 AddToWorkList(SmallShift.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002563 return DAG.getNode(ISD::ANY_EXTEND, VT, SmallShift);
2564 }
2565
2566 // fold (srl (sra X, Y), 31) -> (srl X, 31). This srl only looks at the sign
2567 // bit, which is unmodified by sra.
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002568 if (N1C && N1C->getZExtValue()+1 == VT.getSizeInBits()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002569 if (N0.getOpcode() == ISD::SRA)
2570 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0), N1);
2571 }
2572
2573 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
2574 if (N1C && N0.getOpcode() == ISD::CTLZ &&
Duncan Sands92c43912008-06-06 12:08:01 +00002575 N1C->getAPIntValue() == Log2_32(VT.getSizeInBits())) {
Dan Gohmanbea075f2008-02-20 16:33:30 +00002576 APInt KnownZero, KnownOne;
Duncan Sands92c43912008-06-06 12:08:01 +00002577 APInt Mask = APInt::getAllOnesValue(VT.getSizeInBits());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002578 DAG.ComputeMaskedBits(N0.getOperand(0), Mask, KnownZero, KnownOne);
2579
2580 // If any of the input bits are KnownOne, then the input couldn't be all
2581 // zeros, thus the result of the srl will always be zero.
Dan Gohmanbea075f2008-02-20 16:33:30 +00002582 if (KnownOne.getBoolValue()) return DAG.getConstant(0, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002583
2584 // If all of the bits input the to ctlz node are known to be zero, then
2585 // the result of the ctlz is "32" and the result of the shift is one.
Dan Gohmanbea075f2008-02-20 16:33:30 +00002586 APInt UnknownBits = ~KnownZero & Mask;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002587 if (UnknownBits == 0) return DAG.getConstant(1, VT);
2588
2589 // Otherwise, check to see if there is exactly one bit input to the ctlz.
2590 if ((UnknownBits & (UnknownBits-1)) == 0) {
2591 // Okay, we know that only that the single bit specified by UnknownBits
2592 // could be set on input to the CTLZ node. If this bit is set, the SRL
2593 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
2594 // to an SRL,XOR pair, which is likely to simplify more.
Dan Gohmanbea075f2008-02-20 16:33:30 +00002595 unsigned ShAmt = UnknownBits.countTrailingZeros();
Dan Gohman8181bd12008-07-27 21:46:04 +00002596 SDValue Op = N0.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002597 if (ShAmt) {
2598 Op = DAG.getNode(ISD::SRL, VT, Op,
2599 DAG.getConstant(ShAmt, TLI.getShiftAmountTy()));
Gabor Greif1c80d112008-08-28 21:40:38 +00002600 AddToWorkList(Op.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002601 }
2602 return DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(1, VT));
2603 }
2604 }
Evan Cheng76a64c72008-08-30 02:03:58 +00002605
2606 // fold (srl x, (trunc (and y, c))) -> (srl x, (and (trunc y), c))
2607 // iff (trunc c) == c
2608 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng11c34292008-09-22 18:19:24 +00002609 N1.getOperand(0).getOpcode() == ISD::AND &&
2610 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Cheng76a64c72008-08-30 02:03:58 +00002611 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng11c34292008-09-22 18:19:24 +00002612 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Evan Cheng76a64c72008-08-30 02:03:58 +00002613 MVT TruncVT = N1.getValueType();
Evan Cheng11c34292008-09-22 18:19:24 +00002614 SDValue N100 = N1.getOperand(0).getOperand(0);
2615 return DAG.getNode(ISD::SRL, VT, N0,
2616 DAG.getNode(ISD::AND, TruncVT,
2617 DAG.getNode(ISD::TRUNCATE, TruncVT, N100),
2618 DAG.getConstant(N101C->getZExtValue(),
2619 TruncVT)));
Evan Cheng76a64c72008-08-30 02:03:58 +00002620 }
2621 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002622
2623 // fold operands of srl based on knowledge that the low bits are not
2624 // demanded.
Dan Gohman8181bd12008-07-27 21:46:04 +00002625 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
2626 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002627
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002628 return N1C ? visitShiftByConstant(N, N1C->getZExtValue()) : SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002629}
2630
Dan Gohman8181bd12008-07-27 21:46:04 +00002631SDValue DAGCombiner::visitCTLZ(SDNode *N) {
2632 SDValue N0 = N->getOperand(0);
Duncan Sands92c43912008-06-06 12:08:01 +00002633 MVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002634
2635 // fold (ctlz c1) -> c2
2636 if (isa<ConstantSDNode>(N0))
2637 return DAG.getNode(ISD::CTLZ, VT, N0);
Dan Gohman8181bd12008-07-27 21:46:04 +00002638 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002639}
2640
Dan Gohman8181bd12008-07-27 21:46:04 +00002641SDValue DAGCombiner::visitCTTZ(SDNode *N) {
2642 SDValue N0 = N->getOperand(0);
Duncan Sands92c43912008-06-06 12:08:01 +00002643 MVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002644
2645 // fold (cttz c1) -> c2
2646 if (isa<ConstantSDNode>(N0))
2647 return DAG.getNode(ISD::CTTZ, VT, N0);
Dan Gohman8181bd12008-07-27 21:46:04 +00002648 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002649}
2650
Dan Gohman8181bd12008-07-27 21:46:04 +00002651SDValue DAGCombiner::visitCTPOP(SDNode *N) {
2652 SDValue N0 = N->getOperand(0);
Duncan Sands92c43912008-06-06 12:08:01 +00002653 MVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002654
2655 // fold (ctpop c1) -> c2
2656 if (isa<ConstantSDNode>(N0))
2657 return DAG.getNode(ISD::CTPOP, VT, N0);
Dan Gohman8181bd12008-07-27 21:46:04 +00002658 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002659}
2660
Dan Gohman8181bd12008-07-27 21:46:04 +00002661SDValue DAGCombiner::visitSELECT(SDNode *N) {
2662 SDValue N0 = N->getOperand(0);
2663 SDValue N1 = N->getOperand(1);
2664 SDValue N2 = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002665 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2666 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2667 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
Duncan Sands92c43912008-06-06 12:08:01 +00002668 MVT VT = N->getValueType(0);
2669 MVT VT0 = N0.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002670
2671 // fold select C, X, X -> X
2672 if (N1 == N2)
2673 return N1;
2674 // fold select true, X, Y -> X
2675 if (N0C && !N0C->isNullValue())
2676 return N1;
2677 // fold select false, X, Y -> Y
2678 if (N0C && N0C->isNullValue())
2679 return N2;
2680 // fold select C, 1, X -> C | X
Duncan Sands92c43912008-06-06 12:08:01 +00002681 if (VT == MVT::i1 && N1C && N1C->getAPIntValue() == 1)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002682 return DAG.getNode(ISD::OR, VT, N0, N2);
Evan Chengff601dc2007-08-18 05:57:05 +00002683 // fold select C, 0, 1 -> ~C
Duncan Sands92c43912008-06-06 12:08:01 +00002684 if (VT.isInteger() && VT0.isInteger() &&
Dan Gohman9d24dc72008-03-13 22:13:53 +00002685 N1C && N2C && N1C->isNullValue() && N2C->getAPIntValue() == 1) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002686 SDValue XORNode = DAG.getNode(ISD::XOR, VT0, N0, DAG.getConstant(1, VT0));
Evan Chengff601dc2007-08-18 05:57:05 +00002687 if (VT == VT0)
2688 return XORNode;
Gabor Greif1c80d112008-08-28 21:40:38 +00002689 AddToWorkList(XORNode.getNode());
Duncan Sandsec142ee2008-06-08 20:54:56 +00002690 if (VT.bitsGT(VT0))
Evan Chengff601dc2007-08-18 05:57:05 +00002691 return DAG.getNode(ISD::ZERO_EXTEND, VT, XORNode);
2692 return DAG.getNode(ISD::TRUNCATE, VT, XORNode);
2693 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002694 // fold select C, 0, X -> ~C & X
Dale Johannesen53e0ad72007-12-06 17:53:31 +00002695 if (VT == VT0 && VT == MVT::i1 && N1C && N1C->isNullValue()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002696 SDValue XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Gabor Greif1c80d112008-08-28 21:40:38 +00002697 AddToWorkList(XORNode.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002698 return DAG.getNode(ISD::AND, VT, XORNode, N2);
2699 }
2700 // fold select C, X, 1 -> ~C | X
Dan Gohman9d24dc72008-03-13 22:13:53 +00002701 if (VT == VT0 && VT == MVT::i1 && N2C && N2C->getAPIntValue() == 1) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002702 SDValue XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Gabor Greif1c80d112008-08-28 21:40:38 +00002703 AddToWorkList(XORNode.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002704 return DAG.getNode(ISD::OR, VT, XORNode, N1);
2705 }
2706 // fold select C, X, 0 -> C & X
2707 // FIXME: this should check for C type == X type, not i1?
Duncan Sands92c43912008-06-06 12:08:01 +00002708 if (VT == MVT::i1 && N2C && N2C->isNullValue())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002709 return DAG.getNode(ISD::AND, VT, N0, N1);
2710 // fold X ? X : Y --> X ? 1 : Y --> X | Y
Duncan Sands92c43912008-06-06 12:08:01 +00002711 if (VT == MVT::i1 && N0 == N1)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002712 return DAG.getNode(ISD::OR, VT, N0, N2);
2713 // fold X ? Y : X --> X ? Y : 0 --> X & Y
Duncan Sands92c43912008-06-06 12:08:01 +00002714 if (VT == MVT::i1 && N0 == N2)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002715 return DAG.getNode(ISD::AND, VT, N0, N1);
2716
2717 // If we can fold this based on the true/false value, do so.
2718 if (SimplifySelectOps(N, N1, N2))
Dan Gohman8181bd12008-07-27 21:46:04 +00002719 return SDValue(N, 0); // Don't revisit N.
Duncan Sands2418bec2008-06-13 19:07:40 +00002720
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002721 // fold selects based on a setcc into other things, such as min/max/abs
Anton Korobeynikov53422f62008-02-20 11:10:28 +00002722 if (N0.getOpcode() == ISD::SETCC) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002723 // FIXME:
2724 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
2725 // having to say they don't support SELECT_CC on every type the DAG knows
2726 // about, since there is no way to mark an opcode illegal at all value types
2727 if (TLI.isOperationLegal(ISD::SELECT_CC, MVT::Other))
2728 return DAG.getNode(ISD::SELECT_CC, VT, N0.getOperand(0), N0.getOperand(1),
2729 N1, N2, N0.getOperand(2));
2730 else
2731 return SimplifySelect(N0, N1, N2);
Anton Korobeynikov53422f62008-02-20 11:10:28 +00002732 }
Dan Gohman8181bd12008-07-27 21:46:04 +00002733 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002734}
2735
Dan Gohman8181bd12008-07-27 21:46:04 +00002736SDValue DAGCombiner::visitSELECT_CC(SDNode *N) {
2737 SDValue N0 = N->getOperand(0);
2738 SDValue N1 = N->getOperand(1);
2739 SDValue N2 = N->getOperand(2);
2740 SDValue N3 = N->getOperand(3);
2741 SDValue N4 = N->getOperand(4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002742 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
2743
2744 // fold select_cc lhs, rhs, x, x, cc -> x
2745 if (N2 == N3)
2746 return N2;
2747
2748 // Determine if the condition we're dealing with is constant
Dan Gohman8181bd12008-07-27 21:46:04 +00002749 SDValue SCC = SimplifySetCC(TLI.getSetCCResultType(N0), N0, N1, CC, false);
Gabor Greif1c80d112008-08-28 21:40:38 +00002750 if (SCC.getNode()) AddToWorkList(SCC.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002751
Gabor Greif1c80d112008-08-28 21:40:38 +00002752 if (ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.getNode())) {
Dan Gohman9d24dc72008-03-13 22:13:53 +00002753 if (!SCCC->isNullValue())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002754 return N2; // cond always true -> true val
2755 else
2756 return N3; // cond always false -> false val
2757 }
2758
2759 // Fold to a simpler select_cc
Gabor Greif1c80d112008-08-28 21:40:38 +00002760 if (SCC.getNode() && SCC.getOpcode() == ISD::SETCC)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002761 return DAG.getNode(ISD::SELECT_CC, N2.getValueType(),
2762 SCC.getOperand(0), SCC.getOperand(1), N2, N3,
2763 SCC.getOperand(2));
2764
2765 // If we can fold this based on the true/false value, do so.
2766 if (SimplifySelectOps(N, N2, N3))
Dan Gohman8181bd12008-07-27 21:46:04 +00002767 return SDValue(N, 0); // Don't revisit N.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002768
2769 // fold select_cc into other things, such as min/max/abs
2770 return SimplifySelectCC(N0, N1, N2, N3, CC);
2771}
2772
Dan Gohman8181bd12008-07-27 21:46:04 +00002773SDValue DAGCombiner::visitSETCC(SDNode *N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002774 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
2775 cast<CondCodeSDNode>(N->getOperand(2))->get());
2776}
2777
Evan Cheng9decb332007-10-29 19:58:20 +00002778// ExtendUsesToFormExtLoad - Trying to extend uses of a load to enable this:
2779// "fold ({s|z}ext (load x)) -> ({s|z}ext (truncate ({s|z}extload x)))"
2780// transformation. Returns true if extension are possible and the above
2781// mentioned transformation is profitable.
Dan Gohman8181bd12008-07-27 21:46:04 +00002782static bool ExtendUsesToFormExtLoad(SDNode *N, SDValue N0,
Evan Cheng9decb332007-10-29 19:58:20 +00002783 unsigned ExtOpc,
2784 SmallVector<SDNode*, 4> &ExtendNodes,
2785 TargetLowering &TLI) {
2786 bool HasCopyToRegUses = false;
2787 bool isTruncFree = TLI.isTruncateFree(N->getValueType(0), N0.getValueType());
Gabor Greifb420b9d2008-08-30 19:29:20 +00002788 for (SDNode::use_iterator UI = N0.getNode()->use_begin(),
2789 UE = N0.getNode()->use_end();
Evan Cheng9decb332007-10-29 19:58:20 +00002790 UI != UE; ++UI) {
Dan Gohman0c97f1d2008-07-27 20:43:25 +00002791 SDNode *User = *UI;
Evan Cheng9decb332007-10-29 19:58:20 +00002792 if (User == N)
2793 continue;
2794 // FIXME: Only extend SETCC N, N and SETCC N, c for now.
2795 if (User->getOpcode() == ISD::SETCC) {
2796 ISD::CondCode CC = cast<CondCodeSDNode>(User->getOperand(2))->get();
2797 if (ExtOpc == ISD::ZERO_EXTEND && ISD::isSignedIntSetCC(CC))
2798 // Sign bits will be lost after a zext.
2799 return false;
2800 bool Add = false;
2801 for (unsigned i = 0; i != 2; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002802 SDValue UseOp = User->getOperand(i);
Evan Cheng9decb332007-10-29 19:58:20 +00002803 if (UseOp == N0)
2804 continue;
2805 if (!isa<ConstantSDNode>(UseOp))
2806 return false;
2807 Add = true;
2808 }
2809 if (Add)
2810 ExtendNodes.push_back(User);
2811 } else {
2812 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002813 SDValue UseOp = User->getOperand(i);
Evan Cheng9decb332007-10-29 19:58:20 +00002814 if (UseOp == N0) {
2815 // If truncate from extended type to original load type is free
2816 // on this target, then it's ok to extend a CopyToReg.
2817 if (isTruncFree && User->getOpcode() == ISD::CopyToReg)
2818 HasCopyToRegUses = true;
2819 else
2820 return false;
2821 }
2822 }
2823 }
2824 }
2825
2826 if (HasCopyToRegUses) {
2827 bool BothLiveOut = false;
2828 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
2829 UI != UE; ++UI) {
Dan Gohman0c97f1d2008-07-27 20:43:25 +00002830 SDNode *User = *UI;
Evan Cheng9decb332007-10-29 19:58:20 +00002831 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002832 SDValue UseOp = User->getOperand(i);
Gabor Greif1c80d112008-08-28 21:40:38 +00002833 if (UseOp.getNode() == N && UseOp.getResNo() == 0) {
Evan Cheng9decb332007-10-29 19:58:20 +00002834 BothLiveOut = true;
2835 break;
2836 }
2837 }
2838 }
2839 if (BothLiveOut)
2840 // Both unextended and extended values are live out. There had better be
2841 // good a reason for the transformation.
2842 return ExtendNodes.size();
2843 }
2844 return true;
2845}
2846
Dan Gohman8181bd12008-07-27 21:46:04 +00002847SDValue DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
2848 SDValue N0 = N->getOperand(0);
Duncan Sands92c43912008-06-06 12:08:01 +00002849 MVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002850
2851 // fold (sext c1) -> c1
2852 if (isa<ConstantSDNode>(N0))
2853 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0);
2854
2855 // fold (sext (sext x)) -> (sext x)
2856 // fold (sext (aext x)) -> (sext x)
2857 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
2858 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
2859
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002860 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman2e0e0cf2008-05-20 20:56:33 +00002861 // fold (sext (truncate (load x))) -> (sext (smaller load x))
2862 // fold (sext (truncate (srl (load x), c))) -> (sext (smaller load (x+c/n)))
Gabor Greif1c80d112008-08-28 21:40:38 +00002863 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
2864 if (NarrowLoad.getNode()) {
2865 if (NarrowLoad.getNode() != N0.getNode())
2866 CombineTo(N0.getNode(), NarrowLoad);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002867 return DAG.getNode(ISD::SIGN_EXTEND, VT, NarrowLoad);
2868 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002869
Dan Gohman2e0e0cf2008-05-20 20:56:33 +00002870 // See if the value being truncated is already sign extended. If so, just
2871 // eliminate the trunc/sext pair.
Dan Gohman8181bd12008-07-27 21:46:04 +00002872 SDValue Op = N0.getOperand(0);
Duncan Sands92c43912008-06-06 12:08:01 +00002873 unsigned OpBits = Op.getValueType().getSizeInBits();
2874 unsigned MidBits = N0.getValueType().getSizeInBits();
2875 unsigned DestBits = VT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002876 unsigned NumSignBits = DAG.ComputeNumSignBits(Op);
2877
2878 if (OpBits == DestBits) {
2879 // Op is i32, Mid is i8, and Dest is i32. If Op has more than 24 sign
2880 // bits, it is already ready.
2881 if (NumSignBits > DestBits-MidBits)
2882 return Op;
2883 } else if (OpBits < DestBits) {
2884 // Op is i32, Mid is i8, and Dest is i64. If Op has more than 24 sign
2885 // bits, just sext from i32.
2886 if (NumSignBits > OpBits-MidBits)
2887 return DAG.getNode(ISD::SIGN_EXTEND, VT, Op);
2888 } else {
2889 // Op is i64, Mid is i8, and Dest is i32. If Op has more than 56 sign
2890 // bits, just truncate to i32.
2891 if (NumSignBits > OpBits-MidBits)
2892 return DAG.getNode(ISD::TRUNCATE, VT, Op);
2893 }
2894
2895 // fold (sext (truncate x)) -> (sextinreg x).
2896 if (!AfterLegalize || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG,
2897 N0.getValueType())) {
Duncan Sandsec142ee2008-06-08 20:54:56 +00002898 if (Op.getValueType().bitsLT(VT))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002899 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
Duncan Sandsec142ee2008-06-08 20:54:56 +00002900 else if (Op.getValueType().bitsGT(VT))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002901 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
2902 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, Op,
2903 DAG.getValueType(N0.getValueType()));
2904 }
2905 }
2906
2907 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Gabor Greif1c80d112008-08-28 21:40:38 +00002908 if (ISD::isNON_EXTLoad(N0.getNode()) &&
Duncan Sands2418bec2008-06-13 19:07:40 +00002909 ((!AfterLegalize && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng08c171a2008-10-14 21:26:46 +00002910 TLI.isLoadExtLegal(ISD::SEXTLOAD, N0.getValueType()))) {
Evan Cheng9decb332007-10-29 19:58:20 +00002911 bool DoXform = true;
2912 SmallVector<SDNode*, 4> SetCCs;
2913 if (!N0.hasOneUse())
2914 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::SIGN_EXTEND, SetCCs, TLI);
2915 if (DoXform) {
2916 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8181bd12008-07-27 21:46:04 +00002917 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
Evan Cheng9decb332007-10-29 19:58:20 +00002918 LN0->getBasePtr(), LN0->getSrcValue(),
2919 LN0->getSrcValueOffset(),
2920 N0.getValueType(),
2921 LN0->isVolatile(),
2922 LN0->getAlignment());
2923 CombineTo(N, ExtLoad);
Dan Gohman8181bd12008-07-27 21:46:04 +00002924 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad);
Gabor Greif1c80d112008-08-28 21:40:38 +00002925 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Evan Cheng9decb332007-10-29 19:58:20 +00002926 // Extend SetCC uses if necessary.
2927 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
2928 SDNode *SetCC = SetCCs[i];
Dan Gohman8181bd12008-07-27 21:46:04 +00002929 SmallVector<SDValue, 4> Ops;
Evan Cheng9decb332007-10-29 19:58:20 +00002930 for (unsigned j = 0; j != 2; ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002931 SDValue SOp = SetCC->getOperand(j);
Evan Cheng9decb332007-10-29 19:58:20 +00002932 if (SOp == Trunc)
2933 Ops.push_back(ExtLoad);
2934 else
2935 Ops.push_back(DAG.getNode(ISD::SIGN_EXTEND, VT, SOp));
2936 }
2937 Ops.push_back(SetCC->getOperand(2));
2938 CombineTo(SetCC, DAG.getNode(ISD::SETCC, SetCC->getValueType(0),
2939 &Ops[0], Ops.size()));
2940 }
Dan Gohman8181bd12008-07-27 21:46:04 +00002941 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng9decb332007-10-29 19:58:20 +00002942 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002943 }
2944
2945 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
2946 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
Gabor Greif1c80d112008-08-28 21:40:38 +00002947 if ((ISD::isSEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) &&
2948 ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002949 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands92c43912008-06-06 12:08:01 +00002950 MVT EVT = LN0->getMemoryVT();
Duncan Sands2418bec2008-06-13 19:07:40 +00002951 if ((!AfterLegalize && !LN0->isVolatile()) ||
Evan Cheng08c171a2008-10-14 21:26:46 +00002952 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002953 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002954 LN0->getBasePtr(), LN0->getSrcValue(),
2955 LN0->getSrcValueOffset(), EVT,
2956 LN0->isVolatile(),
2957 LN0->getAlignment());
2958 CombineTo(N, ExtLoad);
Gabor Greifb420b9d2008-08-30 19:29:20 +00002959 CombineTo(N0.getNode(),
2960 DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002961 ExtLoad.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00002962 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002963 }
2964 }
2965
2966 // sext(setcc x,y,cc) -> select_cc x, y, -1, 0, cc
2967 if (N0.getOpcode() == ISD::SETCC) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002968 SDValue SCC =
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002969 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
2970 DAG.getConstant(~0ULL, VT), DAG.getConstant(0, VT),
2971 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greif1c80d112008-08-28 21:40:38 +00002972 if (SCC.getNode()) return SCC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002973 }
2974
Dan Gohman415e13a2008-04-28 16:58:24 +00002975 // fold (sext x) -> (zext x) if the sign bit is known zero.
Dan Gohman5b37f9d2008-04-28 18:47:17 +00002976 if ((!AfterLegalize || TLI.isOperationLegal(ISD::ZERO_EXTEND, VT)) &&
2977 DAG.SignBitIsZero(N0))
Dan Gohman415e13a2008-04-28 16:58:24 +00002978 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
2979
Dan Gohman8181bd12008-07-27 21:46:04 +00002980 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002981}
2982
Dan Gohman8181bd12008-07-27 21:46:04 +00002983SDValue DAGCombiner::visitZERO_EXTEND(SDNode *N) {
2984 SDValue N0 = N->getOperand(0);
Duncan Sands92c43912008-06-06 12:08:01 +00002985 MVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002986
2987 // fold (zext c1) -> c1
2988 if (isa<ConstantSDNode>(N0))
2989 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
2990 // fold (zext (zext x)) -> (zext x)
2991 // fold (zext (aext x)) -> (zext x)
2992 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
2993 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
2994
2995 // fold (zext (truncate (load x))) -> (zext (smaller load x))
2996 // fold (zext (truncate (srl (load x), c))) -> (zext (small load (x+c/n)))
2997 if (N0.getOpcode() == ISD::TRUNCATE) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002998 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
2999 if (NarrowLoad.getNode()) {
3000 if (NarrowLoad.getNode() != N0.getNode())
3001 CombineTo(N0.getNode(), NarrowLoad);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003002 return DAG.getNode(ISD::ZERO_EXTEND, VT, NarrowLoad);
3003 }
3004 }
3005
3006 // fold (zext (truncate x)) -> (and x, mask)
3007 if (N0.getOpcode() == ISD::TRUNCATE &&
3008 (!AfterLegalize || TLI.isOperationLegal(ISD::AND, VT))) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003009 SDValue Op = N0.getOperand(0);
Duncan Sandsec142ee2008-06-08 20:54:56 +00003010 if (Op.getValueType().bitsLT(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003011 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
Duncan Sandsec142ee2008-06-08 20:54:56 +00003012 } else if (Op.getValueType().bitsGT(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003013 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
3014 }
3015 return DAG.getZeroExtendInReg(Op, N0.getValueType());
3016 }
3017
3018 // fold (zext (and (trunc x), cst)) -> (and x, cst).
3019 if (N0.getOpcode() == ISD::AND &&
3020 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
3021 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003022 SDValue X = N0.getOperand(0).getOperand(0);
Duncan Sandsec142ee2008-06-08 20:54:56 +00003023 if (X.getValueType().bitsLT(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003024 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
Duncan Sandsec142ee2008-06-08 20:54:56 +00003025 } else if (X.getValueType().bitsGT(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003026 X = DAG.getNode(ISD::TRUNCATE, VT, X);
3027 }
Dan Gohmand047c3e2008-03-03 23:51:38 +00003028 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Duncan Sands92c43912008-06-06 12:08:01 +00003029 Mask.zext(VT.getSizeInBits());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003030 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
3031 }
3032
3033 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Gabor Greif1c80d112008-08-28 21:40:38 +00003034 if (ISD::isNON_EXTLoad(N0.getNode()) &&
Duncan Sands2418bec2008-06-13 19:07:40 +00003035 ((!AfterLegalize && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng08c171a2008-10-14 21:26:46 +00003036 TLI.isLoadExtLegal(ISD::ZEXTLOAD, N0.getValueType()))) {
Evan Cheng9decb332007-10-29 19:58:20 +00003037 bool DoXform = true;
3038 SmallVector<SDNode*, 4> SetCCs;
3039 if (!N0.hasOneUse())
3040 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ZERO_EXTEND, SetCCs, TLI);
3041 if (DoXform) {
3042 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8181bd12008-07-27 21:46:04 +00003043 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
Evan Cheng9decb332007-10-29 19:58:20 +00003044 LN0->getBasePtr(), LN0->getSrcValue(),
3045 LN0->getSrcValueOffset(),
3046 N0.getValueType(),
3047 LN0->isVolatile(),
3048 LN0->getAlignment());
3049 CombineTo(N, ExtLoad);
Dan Gohman8181bd12008-07-27 21:46:04 +00003050 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad);
Gabor Greif1c80d112008-08-28 21:40:38 +00003051 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Evan Cheng9decb332007-10-29 19:58:20 +00003052 // Extend SetCC uses if necessary.
3053 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
3054 SDNode *SetCC = SetCCs[i];
Dan Gohman8181bd12008-07-27 21:46:04 +00003055 SmallVector<SDValue, 4> Ops;
Evan Cheng9decb332007-10-29 19:58:20 +00003056 for (unsigned j = 0; j != 2; ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003057 SDValue SOp = SetCC->getOperand(j);
Evan Cheng9decb332007-10-29 19:58:20 +00003058 if (SOp == Trunc)
3059 Ops.push_back(ExtLoad);
3060 else
Evan Cheng06aaf4c2007-10-30 20:11:21 +00003061 Ops.push_back(DAG.getNode(ISD::ZERO_EXTEND, VT, SOp));
Evan Cheng9decb332007-10-29 19:58:20 +00003062 }
3063 Ops.push_back(SetCC->getOperand(2));
3064 CombineTo(SetCC, DAG.getNode(ISD::SETCC, SetCC->getValueType(0),
3065 &Ops[0], Ops.size()));
3066 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003067 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng9decb332007-10-29 19:58:20 +00003068 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003069 }
3070
3071 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
3072 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
Gabor Greif1c80d112008-08-28 21:40:38 +00003073 if ((ISD::isZEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) &&
3074 ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003075 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands92c43912008-06-06 12:08:01 +00003076 MVT EVT = LN0->getMemoryVT();
Duncan Sands2418bec2008-06-13 19:07:40 +00003077 if ((!AfterLegalize && !LN0->isVolatile()) ||
Evan Cheng08c171a2008-10-14 21:26:46 +00003078 TLI.isLoadExtLegal(ISD::ZEXTLOAD, EVT)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003079 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
Duncan Sands2418bec2008-06-13 19:07:40 +00003080 LN0->getBasePtr(), LN0->getSrcValue(),
3081 LN0->getSrcValueOffset(), EVT,
3082 LN0->isVolatile(),
3083 LN0->getAlignment());
3084 CombineTo(N, ExtLoad);
Gabor Greifb420b9d2008-08-30 19:29:20 +00003085 CombineTo(N0.getNode(),
3086 DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
Duncan Sands2418bec2008-06-13 19:07:40 +00003087 ExtLoad.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00003088 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Duncan Sands2418bec2008-06-13 19:07:40 +00003089 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003090 }
3091
3092 // zext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
3093 if (N0.getOpcode() == ISD::SETCC) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003094 SDValue SCC =
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003095 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
3096 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
3097 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greif1c80d112008-08-28 21:40:38 +00003098 if (SCC.getNode()) return SCC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003099 }
3100
Dan Gohman8181bd12008-07-27 21:46:04 +00003101 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003102}
3103
Dan Gohman8181bd12008-07-27 21:46:04 +00003104SDValue DAGCombiner::visitANY_EXTEND(SDNode *N) {
3105 SDValue N0 = N->getOperand(0);
Duncan Sands92c43912008-06-06 12:08:01 +00003106 MVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003107
3108 // fold (aext c1) -> c1
3109 if (isa<ConstantSDNode>(N0))
3110 return DAG.getNode(ISD::ANY_EXTEND, VT, N0);
3111 // fold (aext (aext x)) -> (aext x)
3112 // fold (aext (zext x)) -> (zext x)
3113 // fold (aext (sext x)) -> (sext x)
3114 if (N0.getOpcode() == ISD::ANY_EXTEND ||
3115 N0.getOpcode() == ISD::ZERO_EXTEND ||
3116 N0.getOpcode() == ISD::SIGN_EXTEND)
3117 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
3118
3119 // fold (aext (truncate (load x))) -> (aext (smaller load x))
3120 // fold (aext (truncate (srl (load x), c))) -> (aext (small load (x+c/n)))
3121 if (N0.getOpcode() == ISD::TRUNCATE) {
Gabor Greif1c80d112008-08-28 21:40:38 +00003122 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
3123 if (NarrowLoad.getNode()) {
3124 if (NarrowLoad.getNode() != N0.getNode())
3125 CombineTo(N0.getNode(), NarrowLoad);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003126 return DAG.getNode(ISD::ANY_EXTEND, VT, NarrowLoad);
3127 }
3128 }
3129
3130 // fold (aext (truncate x))
3131 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003132 SDValue TruncOp = N0.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003133 if (TruncOp.getValueType() == VT)
3134 return TruncOp; // x iff x size == zext size.
Duncan Sandsec142ee2008-06-08 20:54:56 +00003135 if (TruncOp.getValueType().bitsGT(VT))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003136 return DAG.getNode(ISD::TRUNCATE, VT, TruncOp);
3137 return DAG.getNode(ISD::ANY_EXTEND, VT, TruncOp);
3138 }
3139
3140 // fold (aext (and (trunc x), cst)) -> (and x, cst).
3141 if (N0.getOpcode() == ISD::AND &&
3142 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
3143 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003144 SDValue X = N0.getOperand(0).getOperand(0);
Duncan Sandsec142ee2008-06-08 20:54:56 +00003145 if (X.getValueType().bitsLT(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003146 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
Duncan Sandsec142ee2008-06-08 20:54:56 +00003147 } else if (X.getValueType().bitsGT(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003148 X = DAG.getNode(ISD::TRUNCATE, VT, X);
3149 }
Dan Gohmand047c3e2008-03-03 23:51:38 +00003150 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Duncan Sands92c43912008-06-06 12:08:01 +00003151 Mask.zext(VT.getSizeInBits());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003152 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
3153 }
3154
3155 // fold (aext (load x)) -> (aext (truncate (extload x)))
Gabor Greif1c80d112008-08-28 21:40:38 +00003156 if (ISD::isNON_EXTLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sands2418bec2008-06-13 19:07:40 +00003157 ((!AfterLegalize && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng08c171a2008-10-14 21:26:46 +00003158 TLI.isLoadExtLegal(ISD::EXTLOAD, N0.getValueType()))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003159 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8181bd12008-07-27 21:46:04 +00003160 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003161 LN0->getBasePtr(), LN0->getSrcValue(),
3162 LN0->getSrcValueOffset(),
3163 N0.getValueType(),
3164 LN0->isVolatile(),
3165 LN0->getAlignment());
3166 CombineTo(N, ExtLoad);
Dan Gohman759ed292008-07-31 00:50:31 +00003167 // Redirect any chain users to the new load.
Evan Chengc296a302008-08-29 23:20:46 +00003168 DAG.ReplaceAllUsesOfValueWith(SDValue(LN0, 1),
3169 SDValue(ExtLoad.getNode(), 1));
Dan Gohman759ed292008-07-31 00:50:31 +00003170 // If any node needs the original loaded value, recompute it.
3171 if (!LN0->use_empty())
3172 CombineTo(LN0, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
3173 ExtLoad.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00003174 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003175 }
3176
3177 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
3178 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
3179 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
3180 if (N0.getOpcode() == ISD::LOAD &&
Gabor Greif1c80d112008-08-28 21:40:38 +00003181 !ISD::isNON_EXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003182 N0.hasOneUse()) {
3183 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands92c43912008-06-06 12:08:01 +00003184 MVT EVT = LN0->getMemoryVT();
Dan Gohman8181bd12008-07-27 21:46:04 +00003185 SDValue ExtLoad = DAG.getExtLoad(LN0->getExtensionType(), VT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003186 LN0->getChain(), LN0->getBasePtr(),
3187 LN0->getSrcValue(),
3188 LN0->getSrcValueOffset(), EVT,
3189 LN0->isVolatile(),
3190 LN0->getAlignment());
3191 CombineTo(N, ExtLoad);
Evan Chengc296a302008-08-29 23:20:46 +00003192 CombineTo(N0.getNode(),
3193 DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003194 ExtLoad.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00003195 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003196 }
3197
3198 // aext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
3199 if (N0.getOpcode() == ISD::SETCC) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003200 SDValue SCC =
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003201 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
3202 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
3203 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greif1c80d112008-08-28 21:40:38 +00003204 if (SCC.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003205 return SCC;
3206 }
3207
Dan Gohman8181bd12008-07-27 21:46:04 +00003208 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003209}
3210
Chris Lattnere8671c52007-10-13 06:35:54 +00003211/// GetDemandedBits - See if the specified operand can be simplified with the
3212/// knowledge that only the bits specified by Mask are used. If so, return the
Dan Gohman8181bd12008-07-27 21:46:04 +00003213/// simpler operand, otherwise return a null SDValue.
3214SDValue DAGCombiner::GetDemandedBits(SDValue V, const APInt &Mask) {
Chris Lattnere8671c52007-10-13 06:35:54 +00003215 switch (V.getOpcode()) {
3216 default: break;
3217 case ISD::OR:
3218 case ISD::XOR:
3219 // If the LHS or RHS don't contribute bits to the or, drop them.
3220 if (DAG.MaskedValueIsZero(V.getOperand(0), Mask))
3221 return V.getOperand(1);
3222 if (DAG.MaskedValueIsZero(V.getOperand(1), Mask))
3223 return V.getOperand(0);
3224 break;
Chris Lattnerb77ea552007-10-13 06:58:48 +00003225 case ISD::SRL:
3226 // Only look at single-use SRLs.
Gabor Greif1c80d112008-08-28 21:40:38 +00003227 if (!V.getNode()->hasOneUse())
Chris Lattnerb77ea552007-10-13 06:58:48 +00003228 break;
3229 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
3230 // See if we can recursively simplify the LHS.
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003231 unsigned Amt = RHSC->getZExtValue();
Dan Gohman07961cd2008-02-25 21:11:39 +00003232 APInt NewMask = Mask << Amt;
Dan Gohman8181bd12008-07-27 21:46:04 +00003233 SDValue SimplifyLHS = GetDemandedBits(V.getOperand(0), NewMask);
Gabor Greif1c80d112008-08-28 21:40:38 +00003234 if (SimplifyLHS.getNode()) {
Chris Lattnerb77ea552007-10-13 06:58:48 +00003235 return DAG.getNode(ISD::SRL, V.getValueType(),
3236 SimplifyLHS, V.getOperand(1));
3237 }
3238 }
Chris Lattnere8671c52007-10-13 06:35:54 +00003239 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003240 return SDValue();
Chris Lattnere8671c52007-10-13 06:35:54 +00003241}
3242
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003243/// ReduceLoadWidth - If the result of a wider load is shifted to right of N
3244/// bits and then truncated to a narrower type and where N is a multiple
3245/// of number of bits of the narrower type, transform it to a narrower load
3246/// from address + N / num of bits of new type. If the result is to be
3247/// extended, also fold the extension to form a extending load.
Dan Gohman8181bd12008-07-27 21:46:04 +00003248SDValue DAGCombiner::ReduceLoadWidth(SDNode *N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003249 unsigned Opc = N->getOpcode();
3250 ISD::LoadExtType ExtType = ISD::NON_EXTLOAD;
Dan Gohman8181bd12008-07-27 21:46:04 +00003251 SDValue N0 = N->getOperand(0);
Duncan Sands92c43912008-06-06 12:08:01 +00003252 MVT VT = N->getValueType(0);
3253 MVT EVT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003254
Dan Gohman29c3cef2008-08-14 20:04:46 +00003255 // This transformation isn't valid for vector loads.
3256 if (VT.isVector())
3257 return SDValue();
3258
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003259 // Special case: SIGN_EXTEND_INREG is basically truncating to EVT then
3260 // extended to VT.
3261 if (Opc == ISD::SIGN_EXTEND_INREG) {
3262 ExtType = ISD::SEXTLOAD;
3263 EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Evan Cheng08c171a2008-10-14 21:26:46 +00003264 if (AfterLegalize && !TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))
Dan Gohman8181bd12008-07-27 21:46:04 +00003265 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003266 }
3267
Duncan Sands92c43912008-06-06 12:08:01 +00003268 unsigned EVTBits = EVT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003269 unsigned ShAmt = 0;
3270 bool CombineSRL = false;
3271 if (N0.getOpcode() == ISD::SRL && N0.hasOneUse()) {
3272 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003273 ShAmt = N01->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003274 // Is the shift amount a multiple of size of VT?
3275 if ((ShAmt & (EVTBits-1)) == 0) {
3276 N0 = N0.getOperand(0);
Duncan Sands92c43912008-06-06 12:08:01 +00003277 if (N0.getValueType().getSizeInBits() <= EVTBits)
Dan Gohman8181bd12008-07-27 21:46:04 +00003278 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003279 CombineSRL = true;
3280 }
3281 }
3282 }
3283
Duncan Sands3ea93352008-06-16 08:14:38 +00003284 // Do not generate loads of non-round integer types since these can
3285 // be expensive (and would be wrong if the type is not byte sized).
Dan Gohman759ed292008-07-31 00:50:31 +00003286 if (isa<LoadSDNode>(N0) && N0.hasOneUse() && VT.isRound() &&
3287 cast<LoadSDNode>(N0)->getMemoryVT().getSizeInBits() > EVTBits &&
Duncan Sands2418bec2008-06-13 19:07:40 +00003288 // Do not change the width of a volatile load.
3289 !cast<LoadSDNode>(N0)->isVolatile()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003290 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands92c43912008-06-06 12:08:01 +00003291 MVT PtrType = N0.getOperand(1).getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003292 // For big endian targets, we need to adjust the offset to the pointer to
3293 // load the correct bytes.
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00003294 if (TLI.isBigEndian()) {
Dan Gohman759ed292008-07-31 00:50:31 +00003295 unsigned LVTStoreBits = LN0->getMemoryVT().getStoreSizeInBits();
Duncan Sands92c43912008-06-06 12:08:01 +00003296 unsigned EVTStoreBits = EVT.getStoreSizeInBits();
Duncan Sands4f18d4f2007-11-09 08:57:19 +00003297 ShAmt = LVTStoreBits - EVTStoreBits - ShAmt;
3298 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003299 uint64_t PtrOff = ShAmt / 8;
Duncan Sandsa3691432007-10-28 12:59:45 +00003300 unsigned NewAlign = MinAlign(LN0->getAlignment(), PtrOff);
Dan Gohman8181bd12008-07-27 21:46:04 +00003301 SDValue NewPtr = DAG.getNode(ISD::ADD, PtrType, LN0->getBasePtr(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003302 DAG.getConstant(PtrOff, PtrType));
Gabor Greif1c80d112008-08-28 21:40:38 +00003303 AddToWorkList(NewPtr.getNode());
Dan Gohman8181bd12008-07-27 21:46:04 +00003304 SDValue Load = (ExtType == ISD::NON_EXTLOAD)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003305 ? DAG.getLoad(VT, LN0->getChain(), NewPtr,
Dan Gohman759ed292008-07-31 00:50:31 +00003306 LN0->getSrcValue(), LN0->getSrcValueOffset() + PtrOff,
Duncan Sandsa3691432007-10-28 12:59:45 +00003307 LN0->isVolatile(), NewAlign)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003308 : DAG.getExtLoad(ExtType, VT, LN0->getChain(), NewPtr,
Dan Gohman759ed292008-07-31 00:50:31 +00003309 LN0->getSrcValue(), LN0->getSrcValueOffset() + PtrOff,
3310 EVT, LN0->isVolatile(), NewAlign);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003311 AddToWorkList(N);
3312 if (CombineSRL) {
Chris Lattner7bcb18f2008-02-03 06:49:24 +00003313 WorkListRemover DeadNodes(*this);
3314 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), Load.getValue(1),
3315 &DeadNodes);
Gabor Greif1c80d112008-08-28 21:40:38 +00003316 CombineTo(N->getOperand(0).getNode(), Load);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003317 } else
Gabor Greif1c80d112008-08-28 21:40:38 +00003318 CombineTo(N0.getNode(), Load, Load.getValue(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003319 if (ShAmt) {
3320 if (Opc == ISD::SIGN_EXTEND_INREG)
3321 return DAG.getNode(Opc, VT, Load, N->getOperand(1));
3322 else
3323 return DAG.getNode(Opc, VT, Load);
3324 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003325 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003326 }
3327
Dan Gohman8181bd12008-07-27 21:46:04 +00003328 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003329}
3330
3331
Dan Gohman8181bd12008-07-27 21:46:04 +00003332SDValue DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
3333 SDValue N0 = N->getOperand(0);
3334 SDValue N1 = N->getOperand(1);
Duncan Sands92c43912008-06-06 12:08:01 +00003335 MVT VT = N->getValueType(0);
3336 MVT EVT = cast<VTSDNode>(N1)->getVT();
3337 unsigned VTBits = VT.getSizeInBits();
3338 unsigned EVTBits = EVT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003339
3340 // fold (sext_in_reg c1) -> c1
3341 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
3342 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0, N1);
3343
3344 // If the input is already sign extended, just drop the extension.
Duncan Sands92c43912008-06-06 12:08:01 +00003345 if (DAG.ComputeNumSignBits(N0) >= VT.getSizeInBits()-EVTBits+1)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003346 return N0;
3347
3348 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
3349 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Duncan Sandsec142ee2008-06-08 20:54:56 +00003350 EVT.bitsLT(cast<VTSDNode>(N0.getOperand(1))->getVT())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003351 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
3352 }
3353
Dan Gohman759ed292008-07-31 00:50:31 +00003354 // fold (sext_in_reg (sext x)) -> (sext x)
3355 // fold (sext_in_reg (aext x)) -> (sext x)
3356 // if x is small enough.
3357 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND) {
3358 SDValue N00 = N0.getOperand(0);
3359 if (N00.getValueType().getSizeInBits() < EVTBits)
3360 return DAG.getNode(ISD::SIGN_EXTEND, VT, N00, N1);
3361 }
3362
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003363 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is known zero.
Dan Gohman07961cd2008-02-25 21:11:39 +00003364 if (DAG.MaskedValueIsZero(N0, APInt::getBitsSet(VTBits, EVTBits-1, EVTBits)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003365 return DAG.getZeroExtendInReg(N0, EVT);
3366
3367 // fold operands of sext_in_reg based on knowledge that the top bits are not
3368 // demanded.
Dan Gohman8181bd12008-07-27 21:46:04 +00003369 if (SimplifyDemandedBits(SDValue(N, 0)))
3370 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003371
3372 // fold (sext_in_reg (load x)) -> (smaller sextload x)
3373 // fold (sext_in_reg (srl (load x), c)) -> (smaller sextload (x+c/evtbits))
Dan Gohman8181bd12008-07-27 21:46:04 +00003374 SDValue NarrowLoad = ReduceLoadWidth(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00003375 if (NarrowLoad.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003376 return NarrowLoad;
3377
3378 // fold (sext_in_reg (srl X, 24), i8) -> sra X, 24
3379 // fold (sext_in_reg (srl X, 23), i8) -> sra X, 23 iff possible.
3380 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
3381 if (N0.getOpcode() == ISD::SRL) {
3382 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003383 if (ShAmt->getZExtValue()+EVTBits <= VT.getSizeInBits()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003384 // We can turn this into an SRA iff the input to the SRL is already sign
3385 // extended enough.
3386 unsigned InSignBits = DAG.ComputeNumSignBits(N0.getOperand(0));
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003387 if (VT.getSizeInBits()-(ShAmt->getZExtValue()+EVTBits) < InSignBits)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003388 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0), N0.getOperand(1));
3389 }
3390 }
3391
3392 // fold (sext_inreg (extload x)) -> (sextload x)
Gabor Greif1c80d112008-08-28 21:40:38 +00003393 if (ISD::isEXTLoad(N0.getNode()) &&
3394 ISD::isUNINDEXEDLoad(N0.getNode()) &&
Dan Gohman9a4c92c2008-01-30 00:15:11 +00003395 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sands2418bec2008-06-13 19:07:40 +00003396 ((!AfterLegalize && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng08c171a2008-10-14 21:26:46 +00003397 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003398 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8181bd12008-07-27 21:46:04 +00003399 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003400 LN0->getBasePtr(), LN0->getSrcValue(),
3401 LN0->getSrcValueOffset(), EVT,
3402 LN0->isVolatile(),
3403 LN0->getAlignment());
3404 CombineTo(N, ExtLoad);
Gabor Greif1c80d112008-08-28 21:40:38 +00003405 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00003406 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003407 }
3408 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Gabor Greif1c80d112008-08-28 21:40:38 +00003409 if (ISD::isZEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003410 N0.hasOneUse() &&
Dan Gohman9a4c92c2008-01-30 00:15:11 +00003411 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sands2418bec2008-06-13 19:07:40 +00003412 ((!AfterLegalize && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng08c171a2008-10-14 21:26:46 +00003413 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003414 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8181bd12008-07-27 21:46:04 +00003415 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003416 LN0->getBasePtr(), LN0->getSrcValue(),
3417 LN0->getSrcValueOffset(), EVT,
3418 LN0->isVolatile(),
3419 LN0->getAlignment());
3420 CombineTo(N, ExtLoad);
Gabor Greif1c80d112008-08-28 21:40:38 +00003421 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00003422 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003423 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003424 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003425}
3426
Dan Gohman8181bd12008-07-27 21:46:04 +00003427SDValue DAGCombiner::visitTRUNCATE(SDNode *N) {
3428 SDValue N0 = N->getOperand(0);
Duncan Sands92c43912008-06-06 12:08:01 +00003429 MVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003430
3431 // noop truncate
3432 if (N0.getValueType() == N->getValueType(0))
3433 return N0;
3434 // fold (truncate c1) -> c1
3435 if (isa<ConstantSDNode>(N0))
3436 return DAG.getNode(ISD::TRUNCATE, VT, N0);
3437 // fold (truncate (truncate x)) -> (truncate x)
3438 if (N0.getOpcode() == ISD::TRUNCATE)
3439 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
3440 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
3441 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND||
3442 N0.getOpcode() == ISD::ANY_EXTEND) {
Duncan Sandsec142ee2008-06-08 20:54:56 +00003443 if (N0.getOperand(0).getValueType().bitsLT(VT))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003444 // if the source is smaller than the dest, we still need an extend
3445 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Duncan Sandsec142ee2008-06-08 20:54:56 +00003446 else if (N0.getOperand(0).getValueType().bitsGT(VT))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003447 // if the source is larger than the dest, than we just need the truncate
3448 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
3449 else
3450 // if the source and dest are the same type, we can drop both the extend
3451 // and the truncate
3452 return N0.getOperand(0);
3453 }
3454
Chris Lattnere8671c52007-10-13 06:35:54 +00003455 // See if we can simplify the input to this truncate through knowledge that
3456 // only the low bits are being used. For example "trunc (or (shl x, 8), y)"
3457 // -> trunc y
Dan Gohman8181bd12008-07-27 21:46:04 +00003458 SDValue Shorter =
Dan Gohman07961cd2008-02-25 21:11:39 +00003459 GetDemandedBits(N0, APInt::getLowBitsSet(N0.getValueSizeInBits(),
Duncan Sands92c43912008-06-06 12:08:01 +00003460 VT.getSizeInBits()));
Gabor Greif1c80d112008-08-28 21:40:38 +00003461 if (Shorter.getNode())
Chris Lattnere8671c52007-10-13 06:35:54 +00003462 return DAG.getNode(ISD::TRUNCATE, VT, Shorter);
3463
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003464 // fold (truncate (load x)) -> (smaller load x)
3465 // fold (truncate (srl (load x), c)) -> (smaller load (x+c/evtbits))
3466 return ReduceLoadWidth(N);
3467}
3468
Evan Chengb6290462008-05-12 23:04:07 +00003469static SDNode *getBuildPairElt(SDNode *N, unsigned i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003470 SDValue Elt = N->getOperand(i);
Evan Chengb6290462008-05-12 23:04:07 +00003471 if (Elt.getOpcode() != ISD::MERGE_VALUES)
Gabor Greif1c80d112008-08-28 21:40:38 +00003472 return Elt.getNode();
3473 return Elt.getOperand(Elt.getResNo()).getNode();
Evan Chengb6290462008-05-12 23:04:07 +00003474}
3475
3476/// CombineConsecutiveLoads - build_pair (load, load) -> load
3477/// if load locations are consecutive.
Dan Gohman8181bd12008-07-27 21:46:04 +00003478SDValue DAGCombiner::CombineConsecutiveLoads(SDNode *N, MVT VT) {
Evan Chengb6290462008-05-12 23:04:07 +00003479 assert(N->getOpcode() == ISD::BUILD_PAIR);
3480
3481 SDNode *LD1 = getBuildPairElt(N, 0);
3482 if (!ISD::isNON_EXTLoad(LD1) || !LD1->hasOneUse())
Dan Gohman8181bd12008-07-27 21:46:04 +00003483 return SDValue();
Duncan Sands92c43912008-06-06 12:08:01 +00003484 MVT LD1VT = LD1->getValueType(0);
Evan Chengb6290462008-05-12 23:04:07 +00003485 SDNode *LD2 = getBuildPairElt(N, 1);
3486 const MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3487 if (ISD::isNON_EXTLoad(LD2) &&
3488 LD2->hasOneUse() &&
Duncan Sands2418bec2008-06-13 19:07:40 +00003489 // If both are volatile this would reduce the number of volatile loads.
3490 // If one is volatile it might be ok, but play conservative and bail out.
3491 !cast<LoadSDNode>(LD1)->isVolatile() &&
3492 !cast<LoadSDNode>(LD2)->isVolatile() &&
Duncan Sands92c43912008-06-06 12:08:01 +00003493 TLI.isConsecutiveLoad(LD2, LD1, LD1VT.getSizeInBits()/8, 1, MFI)) {
Evan Chengb6290462008-05-12 23:04:07 +00003494 LoadSDNode *LD = cast<LoadSDNode>(LD1);
3495 unsigned Align = LD->getAlignment();
Dan Gohman404e8542008-09-04 15:39:15 +00003496 unsigned NewAlign = TLI.getTargetData()->
Duncan Sands92c43912008-06-06 12:08:01 +00003497 getABITypeAlignment(VT.getTypeForMVT());
Duncan Sands2418bec2008-06-13 19:07:40 +00003498 if (NewAlign <= Align &&
3499 (!AfterLegalize || TLI.isOperationLegal(ISD::LOAD, VT)))
Evan Chengb6290462008-05-12 23:04:07 +00003500 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(),
3501 LD->getSrcValue(), LD->getSrcValueOffset(),
Duncan Sands2418bec2008-06-13 19:07:40 +00003502 false, Align);
Evan Chengb6290462008-05-12 23:04:07 +00003503 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003504 return SDValue();
Evan Chengb6290462008-05-12 23:04:07 +00003505}
3506
Dan Gohman8181bd12008-07-27 21:46:04 +00003507SDValue DAGCombiner::visitBIT_CONVERT(SDNode *N) {
3508 SDValue N0 = N->getOperand(0);
Duncan Sands92c43912008-06-06 12:08:01 +00003509 MVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003510
3511 // If the input is a BUILD_VECTOR with all constant elements, fold this now.
3512 // Only do this before legalize, since afterward the target may be depending
3513 // on the bitconvert.
3514 // First check to see if this is all constant.
3515 if (!AfterLegalize &&
Gabor Greif1c80d112008-08-28 21:40:38 +00003516 N0.getOpcode() == ISD::BUILD_VECTOR && N0.getNode()->hasOneUse() &&
Duncan Sands92c43912008-06-06 12:08:01 +00003517 VT.isVector()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003518 bool isSimple = true;
3519 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i)
3520 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
3521 N0.getOperand(i).getOpcode() != ISD::Constant &&
3522 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
3523 isSimple = false;
3524 break;
3525 }
3526
Duncan Sands92c43912008-06-06 12:08:01 +00003527 MVT DestEltVT = N->getValueType(0).getVectorElementType();
3528 assert(!DestEltVT.isVector() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003529 "Element type of vector ValueType must not be vector!");
3530 if (isSimple) {
Gabor Greif1c80d112008-08-28 21:40:38 +00003531 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(N0.getNode(), DestEltVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003532 }
3533 }
3534
Dan Gohman83c6e8a2008-09-05 01:58:21 +00003535 // If the input is a constant, let getNode fold it.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003536 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003537 SDValue Res = DAG.getNode(ISD::BIT_CONVERT, VT, N0);
Gabor Greif1c80d112008-08-28 21:40:38 +00003538 if (Res.getNode() != N) return Res;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003539 }
3540
3541 if (N0.getOpcode() == ISD::BIT_CONVERT) // conv(conv(x,t1),t2) -> conv(x,t2)
3542 return DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
3543
3544 // fold (conv (load x)) -> (load (conv*)x)
Evan Chengd7ba7ed2007-10-06 08:19:55 +00003545 // If the resultant load doesn't need a higher alignment than the original!
Gabor Greif1c80d112008-08-28 21:40:38 +00003546 if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sands2418bec2008-06-13 19:07:40 +00003547 // Do not change the width of a volatile load.
3548 !cast<LoadSDNode>(N0)->isVolatile() &&
3549 (!AfterLegalize || TLI.isOperationLegal(ISD::LOAD, VT))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003550 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman404e8542008-09-04 15:39:15 +00003551 unsigned Align = TLI.getTargetData()->
Duncan Sands92c43912008-06-06 12:08:01 +00003552 getABITypeAlignment(VT.getTypeForMVT());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003553 unsigned OrigAlign = LN0->getAlignment();
3554 if (Align <= OrigAlign) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003555 SDValue Load = DAG.getLoad(VT, LN0->getChain(), LN0->getBasePtr(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003556 LN0->getSrcValue(), LN0->getSrcValueOffset(),
Dan Gohman55a11de2008-06-28 00:45:22 +00003557 LN0->isVolatile(), OrigAlign);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003558 AddToWorkList(N);
Gabor Greifb420b9d2008-08-30 19:29:20 +00003559 CombineTo(N0.getNode(),
3560 DAG.getNode(ISD::BIT_CONVERT, N0.getValueType(), Load),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003561 Load.getValue(1));
3562 return Load;
3563 }
3564 }
Duncan Sands2418bec2008-06-13 19:07:40 +00003565
Chris Lattneref26cbc2008-01-27 17:42:27 +00003566 // Fold bitconvert(fneg(x)) -> xor(bitconvert(x), signbit)
3567 // Fold bitconvert(fabs(x)) -> and(bitconvert(x), ~signbit)
3568 // This often reduces constant pool loads.
3569 if ((N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FABS) &&
Gabor Greif1c80d112008-08-28 21:40:38 +00003570 N0.getNode()->hasOneUse() && VT.isInteger() && !VT.isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003571 SDValue NewConv = DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
Gabor Greif1c80d112008-08-28 21:40:38 +00003572 AddToWorkList(NewConv.getNode());
Chris Lattneref26cbc2008-01-27 17:42:27 +00003573
Duncan Sands92c43912008-06-06 12:08:01 +00003574 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
Chris Lattneref26cbc2008-01-27 17:42:27 +00003575 if (N0.getOpcode() == ISD::FNEG)
3576 return DAG.getNode(ISD::XOR, VT, NewConv, DAG.getConstant(SignBit, VT));
3577 assert(N0.getOpcode() == ISD::FABS);
3578 return DAG.getNode(ISD::AND, VT, NewConv, DAG.getConstant(~SignBit, VT));
3579 }
3580
3581 // Fold bitconvert(fcopysign(cst, x)) -> bitconvert(x)&sign | cst&~sign'
3582 // Note that we don't handle copysign(x,cst) because this can always be folded
3583 // to an fneg or fabs.
Gabor Greif1c80d112008-08-28 21:40:38 +00003584 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse() &&
Chris Lattner336672f2008-01-27 23:32:17 +00003585 isa<ConstantFPSDNode>(N0.getOperand(0)) &&
Duncan Sands92c43912008-06-06 12:08:01 +00003586 VT.isInteger() && !VT.isVector()) {
3587 unsigned OrigXWidth = N0.getOperand(1).getValueType().getSizeInBits();
Dan Gohman8181bd12008-07-27 21:46:04 +00003588 SDValue X = DAG.getNode(ISD::BIT_CONVERT,
Duncan Sands92c43912008-06-06 12:08:01 +00003589 MVT::getIntegerVT(OrigXWidth),
Chris Lattneref26cbc2008-01-27 17:42:27 +00003590 N0.getOperand(1));
Gabor Greif1c80d112008-08-28 21:40:38 +00003591 AddToWorkList(X.getNode());
Chris Lattneref26cbc2008-01-27 17:42:27 +00003592
3593 // If X has a different width than the result/lhs, sext it or truncate it.
Duncan Sands92c43912008-06-06 12:08:01 +00003594 unsigned VTWidth = VT.getSizeInBits();
Chris Lattneref26cbc2008-01-27 17:42:27 +00003595 if (OrigXWidth < VTWidth) {
3596 X = DAG.getNode(ISD::SIGN_EXTEND, VT, X);
Gabor Greif1c80d112008-08-28 21:40:38 +00003597 AddToWorkList(X.getNode());
Chris Lattneref26cbc2008-01-27 17:42:27 +00003598 } else if (OrigXWidth > VTWidth) {
3599 // To get the sign bit in the right place, we have to shift it right
3600 // before truncating.
3601 X = DAG.getNode(ISD::SRL, X.getValueType(), X,
3602 DAG.getConstant(OrigXWidth-VTWidth, X.getValueType()));
Gabor Greif1c80d112008-08-28 21:40:38 +00003603 AddToWorkList(X.getNode());
Chris Lattneref26cbc2008-01-27 17:42:27 +00003604 X = DAG.getNode(ISD::TRUNCATE, VT, X);
Gabor Greif1c80d112008-08-28 21:40:38 +00003605 AddToWorkList(X.getNode());
Chris Lattneref26cbc2008-01-27 17:42:27 +00003606 }
3607
Duncan Sands92c43912008-06-06 12:08:01 +00003608 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
Chris Lattneref26cbc2008-01-27 17:42:27 +00003609 X = DAG.getNode(ISD::AND, VT, X, DAG.getConstant(SignBit, VT));
Gabor Greif1c80d112008-08-28 21:40:38 +00003610 AddToWorkList(X.getNode());
Chris Lattneref26cbc2008-01-27 17:42:27 +00003611
Dan Gohman8181bd12008-07-27 21:46:04 +00003612 SDValue Cst = DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
Chris Lattneref26cbc2008-01-27 17:42:27 +00003613 Cst = DAG.getNode(ISD::AND, VT, Cst, DAG.getConstant(~SignBit, VT));
Gabor Greif1c80d112008-08-28 21:40:38 +00003614 AddToWorkList(Cst.getNode());
Chris Lattneref26cbc2008-01-27 17:42:27 +00003615
3616 return DAG.getNode(ISD::OR, VT, X, Cst);
3617 }
Evan Chengb6290462008-05-12 23:04:07 +00003618
3619 // bitconvert(build_pair(ld, ld)) -> ld iff load locations are consecutive.
3620 if (N0.getOpcode() == ISD::BUILD_PAIR) {
Gabor Greif1c80d112008-08-28 21:40:38 +00003621 SDValue CombineLD = CombineConsecutiveLoads(N0.getNode(), VT);
3622 if (CombineLD.getNode())
Evan Chengb6290462008-05-12 23:04:07 +00003623 return CombineLD;
3624 }
Chris Lattneref26cbc2008-01-27 17:42:27 +00003625
Dan Gohman8181bd12008-07-27 21:46:04 +00003626 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003627}
3628
Dan Gohman8181bd12008-07-27 21:46:04 +00003629SDValue DAGCombiner::visitBUILD_PAIR(SDNode *N) {
Duncan Sands92c43912008-06-06 12:08:01 +00003630 MVT VT = N->getValueType(0);
Evan Chengb6290462008-05-12 23:04:07 +00003631 return CombineConsecutiveLoads(N, VT);
3632}
3633
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003634/// ConstantFoldBIT_CONVERTofBUILD_VECTOR - We know that BV is a build_vector
3635/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
3636/// destination element value type.
Dan Gohman8181bd12008-07-27 21:46:04 +00003637SDValue DAGCombiner::
Duncan Sands92c43912008-06-06 12:08:01 +00003638ConstantFoldBIT_CONVERTofBUILD_VECTOR(SDNode *BV, MVT DstEltVT) {
3639 MVT SrcEltVT = BV->getOperand(0).getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003640
3641 // If this is already the right type, we're done.
Dan Gohman8181bd12008-07-27 21:46:04 +00003642 if (SrcEltVT == DstEltVT) return SDValue(BV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003643
Duncan Sands92c43912008-06-06 12:08:01 +00003644 unsigned SrcBitSize = SrcEltVT.getSizeInBits();
3645 unsigned DstBitSize = DstEltVT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003646
3647 // If this is a conversion of N elements of one type to N elements of another
3648 // type, convert each element. This handles FP<->INT cases.
3649 if (SrcBitSize == DstBitSize) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003650 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003651 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
3652 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, DstEltVT, BV->getOperand(i)));
Gabor Greif1c80d112008-08-28 21:40:38 +00003653 AddToWorkList(Ops.back().getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003654 }
Duncan Sands92c43912008-06-06 12:08:01 +00003655 MVT VT = MVT::getVectorVT(DstEltVT,
3656 BV->getValueType(0).getVectorNumElements());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003657 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
3658 }
3659
3660 // Otherwise, we're growing or shrinking the elements. To avoid having to
3661 // handle annoying details of growing/shrinking FP values, we convert them to
3662 // int first.
Duncan Sands92c43912008-06-06 12:08:01 +00003663 if (SrcEltVT.isFloatingPoint()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003664 // Convert the input float vector to a int vector where the elements are the
3665 // same sizes.
3666 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
Duncan Sands6a437fb2008-06-09 11:32:28 +00003667 MVT IntVT = MVT::getIntegerVT(SrcEltVT.getSizeInBits());
Gabor Greif1c80d112008-08-28 21:40:38 +00003668 BV = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, IntVT).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003669 SrcEltVT = IntVT;
3670 }
3671
3672 // Now we know the input is an integer vector. If the output is a FP type,
3673 // convert to integer first, then to FP of the right size.
Duncan Sands92c43912008-06-06 12:08:01 +00003674 if (DstEltVT.isFloatingPoint()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003675 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
Duncan Sands6a437fb2008-06-09 11:32:28 +00003676 MVT TmpVT = MVT::getIntegerVT(DstEltVT.getSizeInBits());
Gabor Greif1c80d112008-08-28 21:40:38 +00003677 SDNode *Tmp = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, TmpVT).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003678
3679 // Next, convert to FP elements of the same size.
3680 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(Tmp, DstEltVT);
3681 }
3682
3683 // Okay, we know the src/dst types are both integers of differing types.
3684 // Handling growing first.
Duncan Sands92c43912008-06-06 12:08:01 +00003685 assert(SrcEltVT.isInteger() && DstEltVT.isInteger());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003686 if (SrcBitSize < DstBitSize) {
3687 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
3688
Dan Gohman8181bd12008-07-27 21:46:04 +00003689 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003690 for (unsigned i = 0, e = BV->getNumOperands(); i != e;
3691 i += NumInputsPerOutput) {
3692 bool isLE = TLI.isLittleEndian();
Dan Gohmand047c3e2008-03-03 23:51:38 +00003693 APInt NewBits = APInt(DstBitSize, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003694 bool EltIsUndef = true;
3695 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
3696 // Shift the previously computed bits over.
3697 NewBits <<= SrcBitSize;
Dan Gohman8181bd12008-07-27 21:46:04 +00003698 SDValue Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003699 if (Op.getOpcode() == ISD::UNDEF) continue;
3700 EltIsUndef = false;
3701
Dan Gohmand047c3e2008-03-03 23:51:38 +00003702 NewBits |=
3703 APInt(cast<ConstantSDNode>(Op)->getAPIntValue()).zext(DstBitSize);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003704 }
3705
3706 if (EltIsUndef)
3707 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
3708 else
3709 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
3710 }
3711
Duncan Sands92c43912008-06-06 12:08:01 +00003712 MVT VT = MVT::getVectorVT(DstEltVT, Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003713 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
3714 }
3715
3716 // Finally, this must be the case where we are shrinking elements: each input
3717 // turns into multiple outputs.
Evan Chengd1045a62008-02-18 23:04:32 +00003718 bool isS2V = ISD::isScalarToVector(BV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003719 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
Duncan Sands92c43912008-06-06 12:08:01 +00003720 MVT VT = MVT::getVectorVT(DstEltVT, NumOutputsPerInput*BV->getNumOperands());
Dan Gohman8181bd12008-07-27 21:46:04 +00003721 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003722 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
3723 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
3724 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
3725 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
3726 continue;
3727 }
Dan Gohmand047c3e2008-03-03 23:51:38 +00003728 APInt OpVal = cast<ConstantSDNode>(BV->getOperand(i))->getAPIntValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003729 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
Dan Gohmand047c3e2008-03-03 23:51:38 +00003730 APInt ThisVal = APInt(OpVal).trunc(DstBitSize);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003731 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
Dan Gohmand047c3e2008-03-03 23:51:38 +00003732 if (isS2V && i == 0 && j == 0 && APInt(ThisVal).zext(SrcBitSize) == OpVal)
Evan Chengd1045a62008-02-18 23:04:32 +00003733 // Simply turn this into a SCALAR_TO_VECTOR of the new type.
3734 return DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Ops[0]);
Dan Gohmand047c3e2008-03-03 23:51:38 +00003735 OpVal = OpVal.lshr(DstBitSize);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003736 }
3737
3738 // For big endian targets, swap the order of the pieces of each element.
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00003739 if (TLI.isBigEndian())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003740 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
3741 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003742 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
3743}
3744
3745
3746
Dan Gohman8181bd12008-07-27 21:46:04 +00003747SDValue DAGCombiner::visitFADD(SDNode *N) {
3748 SDValue N0 = N->getOperand(0);
3749 SDValue N1 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003750 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3751 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands92c43912008-06-06 12:08:01 +00003752 MVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003753
3754 // fold vector ops
Duncan Sands92c43912008-06-06 12:08:01 +00003755 if (VT.isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003756 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00003757 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003758 }
3759
3760 // fold (fadd c1, c2) -> c1+c2
Dale Johannesenb89072e2007-10-16 23:38:29 +00003761 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003762 return DAG.getNode(ISD::FADD, VT, N0, N1);
3763 // canonicalize constant to RHS
3764 if (N0CFP && !N1CFP)
3765 return DAG.getNode(ISD::FADD, VT, N1, N0);
3766 // fold (A + (-B)) -> A-B
Chris Lattnere0992b82008-02-26 07:04:54 +00003767 if (isNegatibleForFree(N1, AfterLegalize) == 2)
3768 return DAG.getNode(ISD::FSUB, VT, N0,
3769 GetNegatedExpression(N1, DAG, AfterLegalize));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003770 // fold ((-A) + B) -> B-A
Chris Lattnere0992b82008-02-26 07:04:54 +00003771 if (isNegatibleForFree(N0, AfterLegalize) == 2)
3772 return DAG.getNode(ISD::FSUB, VT, N1,
3773 GetNegatedExpression(N0, DAG, AfterLegalize));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003774
3775 // If allowed, fold (fadd (fadd x, c1), c2) -> (fadd x, (fadd c1, c2))
3776 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FADD &&
Gabor Greif1c80d112008-08-28 21:40:38 +00003777 N0.getNode()->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003778 return DAG.getNode(ISD::FADD, VT, N0.getOperand(0),
3779 DAG.getNode(ISD::FADD, VT, N0.getOperand(1), N1));
3780
Dan Gohman8181bd12008-07-27 21:46:04 +00003781 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003782}
3783
Dan Gohman8181bd12008-07-27 21:46:04 +00003784SDValue DAGCombiner::visitFSUB(SDNode *N) {
3785 SDValue N0 = N->getOperand(0);
3786 SDValue N1 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003787 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3788 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands92c43912008-06-06 12:08:01 +00003789 MVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003790
3791 // fold vector ops
Duncan Sands92c43912008-06-06 12:08:01 +00003792 if (VT.isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003793 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00003794 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003795 }
3796
3797 // fold (fsub c1, c2) -> c1-c2
Dale Johannesenb89072e2007-10-16 23:38:29 +00003798 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003799 return DAG.getNode(ISD::FSUB, VT, N0, N1);
3800 // fold (0-B) -> -B
Dale Johannesen7604c1b2007-08-31 23:34:27 +00003801 if (UnsafeFPMath && N0CFP && N0CFP->getValueAPF().isZero()) {
Chris Lattnere0992b82008-02-26 07:04:54 +00003802 if (isNegatibleForFree(N1, AfterLegalize))
3803 return GetNegatedExpression(N1, DAG, AfterLegalize);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003804 return DAG.getNode(ISD::FNEG, VT, N1);
3805 }
3806 // fold (A-(-B)) -> A+B
Chris Lattnere0992b82008-02-26 07:04:54 +00003807 if (isNegatibleForFree(N1, AfterLegalize))
3808 return DAG.getNode(ISD::FADD, VT, N0,
3809 GetNegatedExpression(N1, DAG, AfterLegalize));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003810
Dan Gohman8181bd12008-07-27 21:46:04 +00003811 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003812}
3813
Dan Gohman8181bd12008-07-27 21:46:04 +00003814SDValue DAGCombiner::visitFMUL(SDNode *N) {
3815 SDValue N0 = N->getOperand(0);
3816 SDValue N1 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003817 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3818 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands92c43912008-06-06 12:08:01 +00003819 MVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003820
3821 // fold vector ops
Duncan Sands92c43912008-06-06 12:08:01 +00003822 if (VT.isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003823 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00003824 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003825 }
3826
3827 // fold (fmul c1, c2) -> c1*c2
Dale Johannesenb89072e2007-10-16 23:38:29 +00003828 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003829 return DAG.getNode(ISD::FMUL, VT, N0, N1);
3830 // canonicalize constant to RHS
3831 if (N0CFP && !N1CFP)
3832 return DAG.getNode(ISD::FMUL, VT, N1, N0);
3833 // fold (fmul X, 2.0) -> (fadd X, X)
3834 if (N1CFP && N1CFP->isExactlyValue(+2.0))
3835 return DAG.getNode(ISD::FADD, VT, N0, N0);
3836 // fold (fmul X, -1.0) -> (fneg X)
3837 if (N1CFP && N1CFP->isExactlyValue(-1.0))
3838 return DAG.getNode(ISD::FNEG, VT, N0);
3839
3840 // -X * -Y -> X*Y
Chris Lattnere0992b82008-02-26 07:04:54 +00003841 if (char LHSNeg = isNegatibleForFree(N0, AfterLegalize)) {
3842 if (char RHSNeg = isNegatibleForFree(N1, AfterLegalize)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003843 // Both can be negated for free, check to see if at least one is cheaper
3844 // negated.
3845 if (LHSNeg == 2 || RHSNeg == 2)
Chris Lattnere0992b82008-02-26 07:04:54 +00003846 return DAG.getNode(ISD::FMUL, VT,
3847 GetNegatedExpression(N0, DAG, AfterLegalize),
3848 GetNegatedExpression(N1, DAG, AfterLegalize));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003849 }
3850 }
3851
3852 // If allowed, fold (fmul (fmul x, c1), c2) -> (fmul x, (fmul c1, c2))
3853 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FMUL &&
Gabor Greif1c80d112008-08-28 21:40:38 +00003854 N0.getNode()->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003855 return DAG.getNode(ISD::FMUL, VT, N0.getOperand(0),
3856 DAG.getNode(ISD::FMUL, VT, N0.getOperand(1), N1));
3857
Dan Gohman8181bd12008-07-27 21:46:04 +00003858 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003859}
3860
Dan Gohman8181bd12008-07-27 21:46:04 +00003861SDValue DAGCombiner::visitFDIV(SDNode *N) {
3862 SDValue N0 = N->getOperand(0);
3863 SDValue N1 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003864 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3865 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands92c43912008-06-06 12:08:01 +00003866 MVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003867
3868 // fold vector ops
Duncan Sands92c43912008-06-06 12:08:01 +00003869 if (VT.isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003870 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00003871 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003872 }
3873
3874 // fold (fdiv c1, c2) -> c1/c2
Dale Johannesenb89072e2007-10-16 23:38:29 +00003875 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003876 return DAG.getNode(ISD::FDIV, VT, N0, N1);
3877
3878
3879 // -X / -Y -> X*Y
Chris Lattnere0992b82008-02-26 07:04:54 +00003880 if (char LHSNeg = isNegatibleForFree(N0, AfterLegalize)) {
3881 if (char RHSNeg = isNegatibleForFree(N1, AfterLegalize)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003882 // Both can be negated for free, check to see if at least one is cheaper
3883 // negated.
3884 if (LHSNeg == 2 || RHSNeg == 2)
Chris Lattnere0992b82008-02-26 07:04:54 +00003885 return DAG.getNode(ISD::FDIV, VT,
3886 GetNegatedExpression(N0, DAG, AfterLegalize),
3887 GetNegatedExpression(N1, DAG, AfterLegalize));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003888 }
3889 }
3890
Dan Gohman8181bd12008-07-27 21:46:04 +00003891 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003892}
3893
Dan Gohman8181bd12008-07-27 21:46:04 +00003894SDValue DAGCombiner::visitFREM(SDNode *N) {
3895 SDValue N0 = N->getOperand(0);
3896 SDValue N1 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003897 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3898 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands92c43912008-06-06 12:08:01 +00003899 MVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003900
3901 // fold (frem c1, c2) -> fmod(c1,c2)
Dale Johannesenb89072e2007-10-16 23:38:29 +00003902 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003903 return DAG.getNode(ISD::FREM, VT, N0, N1);
3904
Dan Gohman8181bd12008-07-27 21:46:04 +00003905 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003906}
3907
Dan Gohman8181bd12008-07-27 21:46:04 +00003908SDValue DAGCombiner::visitFCOPYSIGN(SDNode *N) {
3909 SDValue N0 = N->getOperand(0);
3910 SDValue N1 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003911 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3912 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands92c43912008-06-06 12:08:01 +00003913 MVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003914
Dale Johannesenb89072e2007-10-16 23:38:29 +00003915 if (N0CFP && N1CFP && VT != MVT::ppcf128) // Constant fold
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003916 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1);
3917
3918 if (N1CFP) {
Dale Johannesenc53301c2007-08-26 01:18:27 +00003919 const APFloat& V = N1CFP->getValueAPF();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003920 // copysign(x, c1) -> fabs(x) iff ispos(c1)
3921 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
Dale Johannesen7f2c1d12007-08-25 22:10:57 +00003922 if (!V.isNegative())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003923 return DAG.getNode(ISD::FABS, VT, N0);
3924 else
3925 return DAG.getNode(ISD::FNEG, VT, DAG.getNode(ISD::FABS, VT, N0));
3926 }
3927
3928 // copysign(fabs(x), y) -> copysign(x, y)
3929 // copysign(fneg(x), y) -> copysign(x, y)
3930 // copysign(copysign(x,z), y) -> copysign(x, y)
3931 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
3932 N0.getOpcode() == ISD::FCOPYSIGN)
3933 return DAG.getNode(ISD::FCOPYSIGN, VT, N0.getOperand(0), N1);
3934
3935 // copysign(x, abs(y)) -> abs(x)
3936 if (N1.getOpcode() == ISD::FABS)
3937 return DAG.getNode(ISD::FABS, VT, N0);
3938
3939 // copysign(x, copysign(y,z)) -> copysign(x, z)
3940 if (N1.getOpcode() == ISD::FCOPYSIGN)
3941 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(1));
3942
3943 // copysign(x, fp_extend(y)) -> copysign(x, y)
3944 // copysign(x, fp_round(y)) -> copysign(x, y)
3945 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
3946 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(0));
3947
Dan Gohman8181bd12008-07-27 21:46:04 +00003948 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003949}
3950
3951
3952
Dan Gohman8181bd12008-07-27 21:46:04 +00003953SDValue DAGCombiner::visitSINT_TO_FP(SDNode *N) {
3954 SDValue N0 = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003955 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Duncan Sands92c43912008-06-06 12:08:01 +00003956 MVT VT = N->getValueType(0);
Chris Lattner8e0e2bdc2008-06-26 00:16:49 +00003957 MVT OpVT = N0.getValueType();
3958
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003959 // fold (sint_to_fp c1) -> c1fp
Chris Lattner8e0e2bdc2008-06-26 00:16:49 +00003960 if (N0C && OpVT != MVT::ppcf128)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003961 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
Chris Lattner8e0e2bdc2008-06-26 00:16:49 +00003962
3963 // If the input is a legal type, and SINT_TO_FP is not legal on this target,
3964 // but UINT_TO_FP is legal on this target, try to convert.
Chris Lattner9532f022008-06-26 17:16:00 +00003965 if (!TLI.isOperationLegal(ISD::SINT_TO_FP, OpVT) &&
Chris Lattner8e0e2bdc2008-06-26 00:16:49 +00003966 TLI.isOperationLegal(ISD::UINT_TO_FP, OpVT)) {
3967 // If the sign bit is known to be zero, we can change this to UINT_TO_FP.
3968 if (DAG.SignBitIsZero(N0))
3969 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
3970 }
3971
3972
Dan Gohman8181bd12008-07-27 21:46:04 +00003973 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003974}
3975
Dan Gohman8181bd12008-07-27 21:46:04 +00003976SDValue DAGCombiner::visitUINT_TO_FP(SDNode *N) {
3977 SDValue N0 = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003978 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Duncan Sands92c43912008-06-06 12:08:01 +00003979 MVT VT = N->getValueType(0);
Chris Lattner8e0e2bdc2008-06-26 00:16:49 +00003980 MVT OpVT = N0.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003981
3982 // fold (uint_to_fp c1) -> c1fp
Chris Lattner8e0e2bdc2008-06-26 00:16:49 +00003983 if (N0C && OpVT != MVT::ppcf128)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003984 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
Chris Lattner8e0e2bdc2008-06-26 00:16:49 +00003985
3986 // If the input is a legal type, and UINT_TO_FP is not legal on this target,
3987 // but SINT_TO_FP is legal on this target, try to convert.
Chris Lattner9532f022008-06-26 17:16:00 +00003988 if (!TLI.isOperationLegal(ISD::UINT_TO_FP, OpVT) &&
Chris Lattner8e0e2bdc2008-06-26 00:16:49 +00003989 TLI.isOperationLegal(ISD::SINT_TO_FP, OpVT)) {
3990 // If the sign bit is known to be zero, we can change this to SINT_TO_FP.
3991 if (DAG.SignBitIsZero(N0))
3992 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
3993 }
3994
Dan Gohman8181bd12008-07-27 21:46:04 +00003995 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003996}
3997
Dan Gohman8181bd12008-07-27 21:46:04 +00003998SDValue DAGCombiner::visitFP_TO_SINT(SDNode *N) {
3999 SDValue N0 = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004000 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands92c43912008-06-06 12:08:01 +00004001 MVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004002
4003 // fold (fp_to_sint c1fp) -> c1
4004 if (N0CFP)
4005 return DAG.getNode(ISD::FP_TO_SINT, VT, N0);
Dan Gohman8181bd12008-07-27 21:46:04 +00004006 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004007}
4008
Dan Gohman8181bd12008-07-27 21:46:04 +00004009SDValue DAGCombiner::visitFP_TO_UINT(SDNode *N) {
4010 SDValue N0 = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004011 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands92c43912008-06-06 12:08:01 +00004012 MVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004013
4014 // fold (fp_to_uint c1fp) -> c1
Dale Johannesenb89072e2007-10-16 23:38:29 +00004015 if (N0CFP && VT != MVT::ppcf128)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004016 return DAG.getNode(ISD::FP_TO_UINT, VT, N0);
Dan Gohman8181bd12008-07-27 21:46:04 +00004017 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004018}
4019
Dan Gohman8181bd12008-07-27 21:46:04 +00004020SDValue DAGCombiner::visitFP_ROUND(SDNode *N) {
4021 SDValue N0 = N->getOperand(0);
4022 SDValue N1 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004023 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands92c43912008-06-06 12:08:01 +00004024 MVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004025
4026 // fold (fp_round c1fp) -> c1fp
Dale Johannesenb89072e2007-10-16 23:38:29 +00004027 if (N0CFP && N0.getValueType() != MVT::ppcf128)
Chris Lattner5872a362008-01-17 07:00:52 +00004028 return DAG.getNode(ISD::FP_ROUND, VT, N0, N1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004029
4030 // fold (fp_round (fp_extend x)) -> x
4031 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
4032 return N0.getOperand(0);
4033
Chris Lattner7afb8552008-01-24 06:45:35 +00004034 // fold (fp_round (fp_round x)) -> (fp_round x)
4035 if (N0.getOpcode() == ISD::FP_ROUND) {
4036 // This is a value preserving truncation if both round's are.
4037 bool IsTrunc = N->getConstantOperandVal(1) == 1 &&
Gabor Greif1c80d112008-08-28 21:40:38 +00004038 N0.getNode()->getConstantOperandVal(1) == 1;
Chris Lattner7afb8552008-01-24 06:45:35 +00004039 return DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0),
4040 DAG.getIntPtrConstant(IsTrunc));
4041 }
4042
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004043 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
Gabor Greif1c80d112008-08-28 21:40:38 +00004044 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004045 SDValue Tmp = DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0), N1);
Gabor Greif1c80d112008-08-28 21:40:38 +00004046 AddToWorkList(Tmp.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004047 return DAG.getNode(ISD::FCOPYSIGN, VT, Tmp, N0.getOperand(1));
4048 }
4049
Dan Gohman8181bd12008-07-27 21:46:04 +00004050 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004051}
4052
Dan Gohman8181bd12008-07-27 21:46:04 +00004053SDValue DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
4054 SDValue N0 = N->getOperand(0);
Duncan Sands92c43912008-06-06 12:08:01 +00004055 MVT VT = N->getValueType(0);
4056 MVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004057 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
4058
4059 // fold (fp_round_inreg c1fp) -> c1fp
4060 if (N0CFP) {
Dan Gohmanc1f3a072008-09-12 18:08:03 +00004061 SDValue Round = DAG.getConstantFP(*N0CFP->getConstantFPValue(), EVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004062 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
4063 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004064 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004065}
4066
Dan Gohman8181bd12008-07-27 21:46:04 +00004067SDValue DAGCombiner::visitFP_EXTEND(SDNode *N) {
4068 SDValue N0 = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004069 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands92c43912008-06-06 12:08:01 +00004070 MVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004071
Chris Lattner6f981fc2007-12-29 06:55:23 +00004072 // If this is fp_round(fpextend), don't fold it, allow ourselves to be folded.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00004073 if (N->hasOneUse() &&
Dan Gohman8181bd12008-07-27 21:46:04 +00004074 N->use_begin().getUse().getSDValue().getOpcode() == ISD::FP_ROUND)
4075 return SDValue();
Chris Lattner5872a362008-01-17 07:00:52 +00004076
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004077 // fold (fp_extend c1fp) -> c1fp
Dale Johannesenb89072e2007-10-16 23:38:29 +00004078 if (N0CFP && VT != MVT::ppcf128)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004079 return DAG.getNode(ISD::FP_EXTEND, VT, N0);
Chris Lattner5872a362008-01-17 07:00:52 +00004080
4081 // Turn fp_extend(fp_round(X, 1)) -> x since the fp_round doesn't affect the
4082 // value of X.
Gabor Greifb420b9d2008-08-30 19:29:20 +00004083 if (N0.getOpcode() == ISD::FP_ROUND
4084 && N0.getNode()->getConstantOperandVal(1) == 1) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004085 SDValue In = N0.getOperand(0);
Chris Lattner5872a362008-01-17 07:00:52 +00004086 if (In.getValueType() == VT) return In;
Duncan Sandsec142ee2008-06-08 20:54:56 +00004087 if (VT.bitsLT(In.getValueType()))
Chris Lattner5872a362008-01-17 07:00:52 +00004088 return DAG.getNode(ISD::FP_ROUND, VT, In, N0.getOperand(1));
4089 return DAG.getNode(ISD::FP_EXTEND, VT, In);
4090 }
4091
4092 // fold (fpext (load x)) -> (fpext (fptrunc (extload x)))
Gabor Greif1c80d112008-08-28 21:40:38 +00004093 if (ISD::isNON_EXTLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sands2418bec2008-06-13 19:07:40 +00004094 ((!AfterLegalize && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng08c171a2008-10-14 21:26:46 +00004095 TLI.isLoadExtLegal(ISD::EXTLOAD, N0.getValueType()))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004096 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8181bd12008-07-27 21:46:04 +00004097 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004098 LN0->getBasePtr(), LN0->getSrcValue(),
4099 LN0->getSrcValueOffset(),
4100 N0.getValueType(),
4101 LN0->isVolatile(),
4102 LN0->getAlignment());
4103 CombineTo(N, ExtLoad);
Gabor Greifb420b9d2008-08-30 19:29:20 +00004104 CombineTo(N0.getNode(), DAG.getNode(ISD::FP_ROUND, N0.getValueType(),
4105 ExtLoad, DAG.getIntPtrConstant(1)),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004106 ExtLoad.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004107 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004108 }
Duncan Sands2418bec2008-06-13 19:07:40 +00004109
Dan Gohman8181bd12008-07-27 21:46:04 +00004110 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004111}
4112
Dan Gohman8181bd12008-07-27 21:46:04 +00004113SDValue DAGCombiner::visitFNEG(SDNode *N) {
4114 SDValue N0 = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004115
Chris Lattnere0992b82008-02-26 07:04:54 +00004116 if (isNegatibleForFree(N0, AfterLegalize))
4117 return GetNegatedExpression(N0, DAG, AfterLegalize);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004118
Chris Lattneref26cbc2008-01-27 17:42:27 +00004119 // Transform fneg(bitconvert(x)) -> bitconvert(x^sign) to avoid loading
4120 // constant pool values.
Gabor Greif1c80d112008-08-28 21:40:38 +00004121 if (N0.getOpcode() == ISD::BIT_CONVERT && N0.getNode()->hasOneUse() &&
Duncan Sands92c43912008-06-06 12:08:01 +00004122 N0.getOperand(0).getValueType().isInteger() &&
4123 !N0.getOperand(0).getValueType().isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004124 SDValue Int = N0.getOperand(0);
Duncan Sands92c43912008-06-06 12:08:01 +00004125 MVT IntVT = Int.getValueType();
4126 if (IntVT.isInteger() && !IntVT.isVector()) {
Chris Lattneref26cbc2008-01-27 17:42:27 +00004127 Int = DAG.getNode(ISD::XOR, IntVT, Int,
Duncan Sands92c43912008-06-06 12:08:01 +00004128 DAG.getConstant(IntVT.getIntegerVTSignBit(), IntVT));
Gabor Greif1c80d112008-08-28 21:40:38 +00004129 AddToWorkList(Int.getNode());
Chris Lattneref26cbc2008-01-27 17:42:27 +00004130 return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Int);
4131 }
4132 }
4133
Dan Gohman8181bd12008-07-27 21:46:04 +00004134 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004135}
4136
Dan Gohman8181bd12008-07-27 21:46:04 +00004137SDValue DAGCombiner::visitFABS(SDNode *N) {
4138 SDValue N0 = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004139 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands92c43912008-06-06 12:08:01 +00004140 MVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004141
4142 // fold (fabs c1) -> fabs(c1)
Dale Johannesenb89072e2007-10-16 23:38:29 +00004143 if (N0CFP && VT != MVT::ppcf128)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004144 return DAG.getNode(ISD::FABS, VT, N0);
4145 // fold (fabs (fabs x)) -> (fabs x)
4146 if (N0.getOpcode() == ISD::FABS)
4147 return N->getOperand(0);
4148 // fold (fabs (fneg x)) -> (fabs x)
4149 // fold (fabs (fcopysign x, y)) -> (fabs x)
4150 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
4151 return DAG.getNode(ISD::FABS, VT, N0.getOperand(0));
4152
Chris Lattneref26cbc2008-01-27 17:42:27 +00004153 // Transform fabs(bitconvert(x)) -> bitconvert(x&~sign) to avoid loading
4154 // constant pool values.
Gabor Greif1c80d112008-08-28 21:40:38 +00004155 if (N0.getOpcode() == ISD::BIT_CONVERT && N0.getNode()->hasOneUse() &&
Duncan Sands92c43912008-06-06 12:08:01 +00004156 N0.getOperand(0).getValueType().isInteger() &&
4157 !N0.getOperand(0).getValueType().isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004158 SDValue Int = N0.getOperand(0);
Duncan Sands92c43912008-06-06 12:08:01 +00004159 MVT IntVT = Int.getValueType();
4160 if (IntVT.isInteger() && !IntVT.isVector()) {
Chris Lattneref26cbc2008-01-27 17:42:27 +00004161 Int = DAG.getNode(ISD::AND, IntVT, Int,
Duncan Sands92c43912008-06-06 12:08:01 +00004162 DAG.getConstant(~IntVT.getIntegerVTSignBit(), IntVT));
Gabor Greif1c80d112008-08-28 21:40:38 +00004163 AddToWorkList(Int.getNode());
Chris Lattneref26cbc2008-01-27 17:42:27 +00004164 return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Int);
4165 }
4166 }
4167
Dan Gohman8181bd12008-07-27 21:46:04 +00004168 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004169}
4170
Dan Gohman8181bd12008-07-27 21:46:04 +00004171SDValue DAGCombiner::visitBRCOND(SDNode *N) {
4172 SDValue Chain = N->getOperand(0);
4173 SDValue N1 = N->getOperand(1);
4174 SDValue N2 = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004175 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
4176
4177 // never taken branch, fold to chain
4178 if (N1C && N1C->isNullValue())
4179 return Chain;
4180 // unconditional branch
Dan Gohman9d24dc72008-03-13 22:13:53 +00004181 if (N1C && N1C->getAPIntValue() == 1)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004182 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
4183 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
4184 // on the target.
4185 if (N1.getOpcode() == ISD::SETCC &&
4186 TLI.isOperationLegal(ISD::BR_CC, MVT::Other)) {
4187 return DAG.getNode(ISD::BR_CC, MVT::Other, Chain, N1.getOperand(2),
4188 N1.getOperand(0), N1.getOperand(1), N2);
4189 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004190 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004191}
4192
4193// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
4194//
Dan Gohman8181bd12008-07-27 21:46:04 +00004195SDValue DAGCombiner::visitBR_CC(SDNode *N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004196 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004197 SDValue CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004198
Duncan Sands6a437fb2008-06-09 11:32:28 +00004199 // Use SimplifySetCC to simplify SETCC's.
Dan Gohman8181bd12008-07-27 21:46:04 +00004200 SDValue Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false);
Gabor Greif1c80d112008-08-28 21:40:38 +00004201 if (Simp.getNode()) AddToWorkList(Simp.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004202
Gabor Greif1c80d112008-08-28 21:40:38 +00004203 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004204
4205 // fold br_cc true, dest -> br dest (unconditional branch)
Dan Gohman9d24dc72008-03-13 22:13:53 +00004206 if (SCCC && !SCCC->isNullValue())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004207 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
4208 N->getOperand(4));
4209 // fold br_cc false, dest -> unconditional fall through
4210 if (SCCC && SCCC->isNullValue())
4211 return N->getOperand(0);
4212
4213 // fold to a simpler setcc
Gabor Greif1c80d112008-08-28 21:40:38 +00004214 if (Simp.getNode() && Simp.getOpcode() == ISD::SETCC)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004215 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
4216 Simp.getOperand(2), Simp.getOperand(0),
4217 Simp.getOperand(1), N->getOperand(4));
Dan Gohman8181bd12008-07-27 21:46:04 +00004218 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004219}
4220
4221
Duncan Sandsc218a5a2008-06-15 20:12:31 +00004222/// CombineToPreIndexedLoadStore - Try turning a load / store into a
4223/// pre-indexed load / store when the base pointer is an add or subtract
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004224/// and it has other uses besides the load / store. After the
4225/// transformation, the new indexed load / store has effectively folded
4226/// the add / subtract in and all of its other uses are redirected to the
4227/// new load / store.
4228bool DAGCombiner::CombineToPreIndexedLoadStore(SDNode *N) {
4229 if (!AfterLegalize)
4230 return false;
4231
4232 bool isLoad = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00004233 SDValue Ptr;
Duncan Sands92c43912008-06-06 12:08:01 +00004234 MVT VT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004235 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattner3bc08502008-01-17 19:59:44 +00004236 if (LD->isIndexed())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004237 return false;
Dan Gohman9a4c92c2008-01-30 00:15:11 +00004238 VT = LD->getMemoryVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004239 if (!TLI.isIndexedLoadLegal(ISD::PRE_INC, VT) &&
4240 !TLI.isIndexedLoadLegal(ISD::PRE_DEC, VT))
4241 return false;
4242 Ptr = LD->getBasePtr();
4243 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattner3bc08502008-01-17 19:59:44 +00004244 if (ST->isIndexed())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004245 return false;
Dan Gohman9a4c92c2008-01-30 00:15:11 +00004246 VT = ST->getMemoryVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004247 if (!TLI.isIndexedStoreLegal(ISD::PRE_INC, VT) &&
4248 !TLI.isIndexedStoreLegal(ISD::PRE_DEC, VT))
4249 return false;
4250 Ptr = ST->getBasePtr();
4251 isLoad = false;
4252 } else
4253 return false;
4254
4255 // If the pointer is not an add/sub, or if it doesn't have multiple uses, bail
4256 // out. There is no reason to make this a preinc/predec.
4257 if ((Ptr.getOpcode() != ISD::ADD && Ptr.getOpcode() != ISD::SUB) ||
Gabor Greif1c80d112008-08-28 21:40:38 +00004258 Ptr.getNode()->hasOneUse())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004259 return false;
4260
4261 // Ask the target to do addressing mode selection.
Dan Gohman8181bd12008-07-27 21:46:04 +00004262 SDValue BasePtr;
4263 SDValue Offset;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004264 ISD::MemIndexedMode AM = ISD::UNINDEXED;
4265 if (!TLI.getPreIndexedAddressParts(N, BasePtr, Offset, AM, DAG))
4266 return false;
4267 // Don't create a indexed load / store with zero offset.
4268 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman9d24dc72008-03-13 22:13:53 +00004269 cast<ConstantSDNode>(Offset)->isNullValue())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004270 return false;
4271
4272 // Try turning it into a pre-indexed load / store except when:
4273 // 1) The new base ptr is a frame index.
4274 // 2) If N is a store and the new base ptr is either the same as or is a
4275 // predecessor of the value being stored.
4276 // 3) Another use of old base ptr is a predecessor of N. If ptr is folded
4277 // that would create a cycle.
4278 // 4) All uses are load / store ops that use it as old base ptr.
4279
4280 // Check #1. Preinc'ing a frame index would require copying the stack pointer
4281 // (plus the implicit offset) to a register to preinc anyway.
4282 if (isa<FrameIndexSDNode>(BasePtr))
4283 return false;
4284
4285 // Check #2.
4286 if (!isLoad) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004287 SDValue Val = cast<StoreSDNode>(N)->getValue();
Gabor Greif1c80d112008-08-28 21:40:38 +00004288 if (Val == BasePtr || BasePtr.getNode()->isPredecessorOf(Val.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004289 return false;
4290 }
4291
4292 // Now check for #3 and #4.
4293 bool RealUse = false;
Gabor Greif1c80d112008-08-28 21:40:38 +00004294 for (SDNode::use_iterator I = Ptr.getNode()->use_begin(),
4295 E = Ptr.getNode()->use_end(); I != E; ++I) {
Dan Gohman0c97f1d2008-07-27 20:43:25 +00004296 SDNode *Use = *I;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004297 if (Use == N)
4298 continue;
Evan Chengd9387682008-03-04 00:41:45 +00004299 if (Use->isPredecessorOf(N))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004300 return false;
4301
4302 if (!((Use->getOpcode() == ISD::LOAD &&
4303 cast<LoadSDNode>(Use)->getBasePtr() == Ptr) ||
Anton Korobeynikov53422f62008-02-20 11:10:28 +00004304 (Use->getOpcode() == ISD::STORE &&
4305 cast<StoreSDNode>(Use)->getBasePtr() == Ptr)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004306 RealUse = true;
4307 }
4308 if (!RealUse)
4309 return false;
4310
Dan Gohman8181bd12008-07-27 21:46:04 +00004311 SDValue Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004312 if (isLoad)
Dan Gohman8181bd12008-07-27 21:46:04 +00004313 Result = DAG.getIndexedLoad(SDValue(N,0), BasePtr, Offset, AM);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004314 else
Dan Gohman8181bd12008-07-27 21:46:04 +00004315 Result = DAG.getIndexedStore(SDValue(N,0), BasePtr, Offset, AM);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004316 ++PreIndexedNodes;
4317 ++NodesCombined;
4318 DOUT << "\nReplacing.4 "; DEBUG(N->dump(&DAG));
Gabor Greif1c80d112008-08-28 21:40:38 +00004319 DOUT << "\nWith: "; DEBUG(Result.getNode()->dump(&DAG));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004320 DOUT << '\n';
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004321 WorkListRemover DeadNodes(*this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004322 if (isLoad) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004323 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0),
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004324 &DeadNodes);
Dan Gohman8181bd12008-07-27 21:46:04 +00004325 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2),
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004326 &DeadNodes);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004327 } else {
Dan Gohman8181bd12008-07-27 21:46:04 +00004328 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1),
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004329 &DeadNodes);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004330 }
4331
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004332 // Finally, since the node is now dead, remove it from the graph.
4333 DAG.DeleteNode(N);
4334
4335 // Replace the uses of Ptr with uses of the updated base value.
4336 DAG.ReplaceAllUsesOfValueWith(Ptr, Result.getValue(isLoad ? 1 : 0),
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004337 &DeadNodes);
Gabor Greif1c80d112008-08-28 21:40:38 +00004338 removeFromWorkList(Ptr.getNode());
4339 DAG.DeleteNode(Ptr.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004340
4341 return true;
4342}
4343
Duncan Sandsc218a5a2008-06-15 20:12:31 +00004344/// CombineToPostIndexedLoadStore - Try to combine a load / store with a
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004345/// add / sub of the base pointer node into a post-indexed load / store.
4346/// The transformation folded the add / subtract into the new indexed
4347/// load / store effectively and all of its uses are redirected to the
4348/// new load / store.
4349bool DAGCombiner::CombineToPostIndexedLoadStore(SDNode *N) {
4350 if (!AfterLegalize)
4351 return false;
4352
4353 bool isLoad = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00004354 SDValue Ptr;
Duncan Sands92c43912008-06-06 12:08:01 +00004355 MVT VT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004356 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattner3bc08502008-01-17 19:59:44 +00004357 if (LD->isIndexed())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004358 return false;
Dan Gohman9a4c92c2008-01-30 00:15:11 +00004359 VT = LD->getMemoryVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004360 if (!TLI.isIndexedLoadLegal(ISD::POST_INC, VT) &&
4361 !TLI.isIndexedLoadLegal(ISD::POST_DEC, VT))
4362 return false;
4363 Ptr = LD->getBasePtr();
4364 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattner3bc08502008-01-17 19:59:44 +00004365 if (ST->isIndexed())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004366 return false;
Dan Gohman9a4c92c2008-01-30 00:15:11 +00004367 VT = ST->getMemoryVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004368 if (!TLI.isIndexedStoreLegal(ISD::POST_INC, VT) &&
4369 !TLI.isIndexedStoreLegal(ISD::POST_DEC, VT))
4370 return false;
4371 Ptr = ST->getBasePtr();
4372 isLoad = false;
4373 } else
4374 return false;
4375
Gabor Greif1c80d112008-08-28 21:40:38 +00004376 if (Ptr.getNode()->hasOneUse())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004377 return false;
4378
Gabor Greif1c80d112008-08-28 21:40:38 +00004379 for (SDNode::use_iterator I = Ptr.getNode()->use_begin(),
4380 E = Ptr.getNode()->use_end(); I != E; ++I) {
Dan Gohman0c97f1d2008-07-27 20:43:25 +00004381 SDNode *Op = *I;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004382 if (Op == N ||
4383 (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB))
4384 continue;
4385
Dan Gohman8181bd12008-07-27 21:46:04 +00004386 SDValue BasePtr;
4387 SDValue Offset;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004388 ISD::MemIndexedMode AM = ISD::UNINDEXED;
4389 if (TLI.getPostIndexedAddressParts(N, Op, BasePtr, Offset, AM, DAG)) {
4390 if (Ptr == Offset)
4391 std::swap(BasePtr, Offset);
4392 if (Ptr != BasePtr)
4393 continue;
4394 // Don't create a indexed load / store with zero offset.
4395 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman9d24dc72008-03-13 22:13:53 +00004396 cast<ConstantSDNode>(Offset)->isNullValue())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004397 continue;
4398
4399 // Try turning it into a post-indexed load / store except when
4400 // 1) All uses are load / store ops that use it as base ptr.
4401 // 2) Op must be independent of N, i.e. Op is neither a predecessor
4402 // nor a successor of N. Otherwise, if Op is folded that would
4403 // create a cycle.
4404
4405 // Check for #1.
4406 bool TryNext = false;
Gabor Greif1c80d112008-08-28 21:40:38 +00004407 for (SDNode::use_iterator II = BasePtr.getNode()->use_begin(),
4408 EE = BasePtr.getNode()->use_end(); II != EE; ++II) {
Dan Gohman0c97f1d2008-07-27 20:43:25 +00004409 SDNode *Use = *II;
Gabor Greif1c80d112008-08-28 21:40:38 +00004410 if (Use == Ptr.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004411 continue;
4412
4413 // If all the uses are load / store addresses, then don't do the
4414 // transformation.
4415 if (Use->getOpcode() == ISD::ADD || Use->getOpcode() == ISD::SUB){
4416 bool RealUse = false;
4417 for (SDNode::use_iterator III = Use->use_begin(),
4418 EEE = Use->use_end(); III != EEE; ++III) {
Dan Gohman0c97f1d2008-07-27 20:43:25 +00004419 SDNode *UseUse = *III;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004420 if (!((UseUse->getOpcode() == ISD::LOAD &&
Gabor Greif1c80d112008-08-28 21:40:38 +00004421 cast<LoadSDNode>(UseUse)->getBasePtr().getNode() == Use) ||
Anton Korobeynikov53422f62008-02-20 11:10:28 +00004422 (UseUse->getOpcode() == ISD::STORE &&
Gabor Greif1c80d112008-08-28 21:40:38 +00004423 cast<StoreSDNode>(UseUse)->getBasePtr().getNode() == Use)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004424 RealUse = true;
4425 }
4426
4427 if (!RealUse) {
4428 TryNext = true;
4429 break;
4430 }
4431 }
4432 }
4433 if (TryNext)
4434 continue;
4435
4436 // Check for #2
Evan Chengd9387682008-03-04 00:41:45 +00004437 if (!Op->isPredecessorOf(N) && !N->isPredecessorOf(Op)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004438 SDValue Result = isLoad
4439 ? DAG.getIndexedLoad(SDValue(N,0), BasePtr, Offset, AM)
4440 : DAG.getIndexedStore(SDValue(N,0), BasePtr, Offset, AM);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004441 ++PostIndexedNodes;
4442 ++NodesCombined;
4443 DOUT << "\nReplacing.5 "; DEBUG(N->dump(&DAG));
Gabor Greif1c80d112008-08-28 21:40:38 +00004444 DOUT << "\nWith: "; DEBUG(Result.getNode()->dump(&DAG));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004445 DOUT << '\n';
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004446 WorkListRemover DeadNodes(*this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004447 if (isLoad) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004448 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0),
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004449 &DeadNodes);
Dan Gohman8181bd12008-07-27 21:46:04 +00004450 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2),
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004451 &DeadNodes);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004452 } else {
Dan Gohman8181bd12008-07-27 21:46:04 +00004453 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1),
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004454 &DeadNodes);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004455 }
4456
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004457 // Finally, since the node is now dead, remove it from the graph.
4458 DAG.DeleteNode(N);
4459
4460 // Replace the uses of Use with uses of the updated base value.
Dan Gohman8181bd12008-07-27 21:46:04 +00004461 DAG.ReplaceAllUsesOfValueWith(SDValue(Op, 0),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004462 Result.getValue(isLoad ? 1 : 0),
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004463 &DeadNodes);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004464 removeFromWorkList(Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004465 DAG.DeleteNode(Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004466 return true;
4467 }
4468 }
4469 }
4470 return false;
4471}
4472
Chris Lattner4e137af2008-01-25 07:20:16 +00004473/// InferAlignment - If we can infer some alignment information from this
4474/// pointer, return it.
Dan Gohman8181bd12008-07-27 21:46:04 +00004475static unsigned InferAlignment(SDValue Ptr, SelectionDAG &DAG) {
Chris Lattner4e137af2008-01-25 07:20:16 +00004476 // If this is a direct reference to a stack slot, use information about the
4477 // stack slot's alignment.
Chris Lattner1e3362f2008-01-26 19:45:50 +00004478 int FrameIdx = 1 << 31;
4479 int64_t FrameOffset = 0;
Chris Lattner4e137af2008-01-25 07:20:16 +00004480 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
Chris Lattner1e3362f2008-01-26 19:45:50 +00004481 FrameIdx = FI->getIndex();
4482 } else if (Ptr.getOpcode() == ISD::ADD &&
4483 isa<ConstantSDNode>(Ptr.getOperand(1)) &&
4484 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4485 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4486 FrameOffset = Ptr.getConstantOperandVal(1);
Chris Lattner4e137af2008-01-25 07:20:16 +00004487 }
Chris Lattner1e3362f2008-01-26 19:45:50 +00004488
4489 if (FrameIdx != (1 << 31)) {
4490 // FIXME: Handle FI+CST.
4491 const MachineFrameInfo &MFI = *DAG.getMachineFunction().getFrameInfo();
4492 if (MFI.isFixedObjectIndex(FrameIdx)) {
Dan Gohmanb0a2ff92008-08-11 18:27:03 +00004493 int64_t ObjectOffset = MFI.getObjectOffset(FrameIdx) + FrameOffset;
Chris Lattner1e3362f2008-01-26 19:45:50 +00004494
4495 // The alignment of the frame index can be determined from its offset from
4496 // the incoming frame position. If the frame object is at offset 32 and
4497 // the stack is guaranteed to be 16-byte aligned, then we know that the
4498 // object is 16-byte aligned.
4499 unsigned StackAlign = DAG.getTarget().getFrameInfo()->getStackAlignment();
4500 unsigned Align = MinAlign(ObjectOffset, StackAlign);
4501
4502 // Finally, the frame object itself may have a known alignment. Factor
4503 // the alignment + offset into a new alignment. For example, if we know
4504 // the FI is 8 byte aligned, but the pointer is 4 off, we really have a
4505 // 4-byte alignment of the resultant pointer. Likewise align 4 + 4-byte
4506 // offset = 4-byte alignment, align 4 + 1-byte offset = align 1, etc.
4507 unsigned FIInfoAlign = MinAlign(MFI.getObjectAlignment(FrameIdx),
4508 FrameOffset);
4509 return std::max(Align, FIInfoAlign);
4510 }
4511 }
Chris Lattner4e137af2008-01-25 07:20:16 +00004512
4513 return 0;
4514}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004515
Dan Gohman8181bd12008-07-27 21:46:04 +00004516SDValue DAGCombiner::visitLOAD(SDNode *N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004517 LoadSDNode *LD = cast<LoadSDNode>(N);
Dan Gohman8181bd12008-07-27 21:46:04 +00004518 SDValue Chain = LD->getChain();
4519 SDValue Ptr = LD->getBasePtr();
Chris Lattner4e137af2008-01-25 07:20:16 +00004520
4521 // Try to infer better alignment information than the load already has.
Dan Gohmanea12c0c2008-08-20 16:30:28 +00004522 if (!Fast && LD->isUnindexed()) {
Chris Lattner4e137af2008-01-25 07:20:16 +00004523 if (unsigned Align = InferAlignment(Ptr, DAG)) {
4524 if (Align > LD->getAlignment())
4525 return DAG.getExtLoad(LD->getExtensionType(), LD->getValueType(0),
4526 Chain, Ptr, LD->getSrcValue(),
Dan Gohman9a4c92c2008-01-30 00:15:11 +00004527 LD->getSrcValueOffset(), LD->getMemoryVT(),
Chris Lattner4e137af2008-01-25 07:20:16 +00004528 LD->isVolatile(), Align);
4529 }
4530 }
4531
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004532
4533 // If load is not volatile and there are no uses of the loaded value (and
4534 // the updated indexed value in case of indexed loads), change uses of the
4535 // chain value into uses of the chain input (i.e. delete the dead load).
4536 if (!LD->isVolatile()) {
4537 if (N->getValueType(1) == MVT::Other) {
4538 // Unindexed loads.
Evan Chenge8b886a2008-01-16 23:11:54 +00004539 if (N->hasNUsesOfValue(0, 0)) {
4540 // It's not safe to use the two value CombineTo variant here. e.g.
4541 // v1, chain2 = load chain1, loc
4542 // v2, chain3 = load chain2, loc
4543 // v3 = add v2, c
Chris Lattnerbb67c192008-01-24 07:57:06 +00004544 // Now we replace use of chain2 with chain1. This makes the second load
4545 // isomorphic to the one we are deleting, and thus makes this load live.
Evan Chenge8b886a2008-01-16 23:11:54 +00004546 DOUT << "\nReplacing.6 "; DEBUG(N->dump(&DAG));
Gabor Greif1c80d112008-08-28 21:40:38 +00004547 DOUT << "\nWith chain: "; DEBUG(Chain.getNode()->dump(&DAG));
Chris Lattnerbb67c192008-01-24 07:57:06 +00004548 DOUT << "\n";
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004549 WorkListRemover DeadNodes(*this);
Dan Gohman8181bd12008-07-27 21:46:04 +00004550 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Chain, &DeadNodes);
Chris Lattnerbb67c192008-01-24 07:57:06 +00004551 if (N->use_empty()) {
4552 removeFromWorkList(N);
4553 DAG.DeleteNode(N);
4554 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004555 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Chenge8b886a2008-01-16 23:11:54 +00004556 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004557 } else {
4558 // Indexed loads.
4559 assert(N->getValueType(2) == MVT::Other && "Malformed indexed loads?");
4560 if (N->hasNUsesOfValue(0, 0) && N->hasNUsesOfValue(0, 1)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004561 SDValue Undef = DAG.getNode(ISD::UNDEF, N->getValueType(0));
Evan Chenge8b886a2008-01-16 23:11:54 +00004562 DOUT << "\nReplacing.6 "; DEBUG(N->dump(&DAG));
Gabor Greif1c80d112008-08-28 21:40:38 +00004563 DOUT << "\nWith: "; DEBUG(Undef.getNode()->dump(&DAG));
Evan Chenge8b886a2008-01-16 23:11:54 +00004564 DOUT << " and 2 other values\n";
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004565 WorkListRemover DeadNodes(*this);
Dan Gohman8181bd12008-07-27 21:46:04 +00004566 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Undef, &DeadNodes);
4567 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1),
Chris Lattner667f9c12008-01-17 07:20:38 +00004568 DAG.getNode(ISD::UNDEF, N->getValueType(1)),
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004569 &DeadNodes);
Dan Gohman8181bd12008-07-27 21:46:04 +00004570 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 2), Chain, &DeadNodes);
Evan Chenge8b886a2008-01-16 23:11:54 +00004571 removeFromWorkList(N);
Evan Chenge8b886a2008-01-16 23:11:54 +00004572 DAG.DeleteNode(N);
Dan Gohman8181bd12008-07-27 21:46:04 +00004573 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004574 }
4575 }
4576 }
4577
4578 // If this load is directly stored, replace the load value with the stored
4579 // value.
4580 // TODO: Handle store large -> read small portion.
4581 // TODO: Handle TRUNCSTORE/LOADEXT
Dan Gohman729b5ff2008-03-31 20:32:52 +00004582 if (LD->getExtensionType() == ISD::NON_EXTLOAD &&
4583 !LD->isVolatile()) {
Gabor Greif1c80d112008-08-28 21:40:38 +00004584 if (ISD::isNON_TRUNCStore(Chain.getNode())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004585 StoreSDNode *PrevST = cast<StoreSDNode>(Chain);
4586 if (PrevST->getBasePtr() == Ptr &&
4587 PrevST->getValue().getValueType() == N->getValueType(0))
4588 return CombineTo(N, Chain.getOperand(1), Chain);
4589 }
4590 }
4591
4592 if (CombinerAA) {
4593 // Walk up chain skipping non-aliasing memory nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00004594 SDValue BetterChain = FindBetterChain(N, Chain);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004595
4596 // If there is a better chain.
4597 if (Chain != BetterChain) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004598 SDValue ReplLoad;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004599
4600 // Replace the chain to void dependency.
4601 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
4602 ReplLoad = DAG.getLoad(N->getValueType(0), BetterChain, Ptr,
Duncan Sandsa3691432007-10-28 12:59:45 +00004603 LD->getSrcValue(), LD->getSrcValueOffset(),
4604 LD->isVolatile(), LD->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004605 } else {
4606 ReplLoad = DAG.getExtLoad(LD->getExtensionType(),
4607 LD->getValueType(0),
4608 BetterChain, Ptr, LD->getSrcValue(),
4609 LD->getSrcValueOffset(),
Dan Gohman9a4c92c2008-01-30 00:15:11 +00004610 LD->getMemoryVT(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004611 LD->isVolatile(),
4612 LD->getAlignment());
4613 }
4614
4615 // Create token factor to keep old chain connected.
Dan Gohman8181bd12008-07-27 21:46:04 +00004616 SDValue Token = DAG.getNode(ISD::TokenFactor, MVT::Other,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004617 Chain, ReplLoad.getValue(1));
4618
4619 // Replace uses with load result and token factor. Don't add users
4620 // to work list.
4621 return CombineTo(N, ReplLoad.getValue(0), Token, false);
4622 }
4623 }
4624
4625 // Try transforming N to an indexed load.
4626 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Dan Gohman8181bd12008-07-27 21:46:04 +00004627 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004628
Dan Gohman8181bd12008-07-27 21:46:04 +00004629 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004630}
4631
Chris Lattner2e023772008-01-08 23:08:06 +00004632
Dan Gohman8181bd12008-07-27 21:46:04 +00004633SDValue DAGCombiner::visitSTORE(SDNode *N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004634 StoreSDNode *ST = cast<StoreSDNode>(N);
Dan Gohman8181bd12008-07-27 21:46:04 +00004635 SDValue Chain = ST->getChain();
4636 SDValue Value = ST->getValue();
4637 SDValue Ptr = ST->getBasePtr();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004638
Chris Lattner4e137af2008-01-25 07:20:16 +00004639 // Try to infer better alignment information than the store already has.
Dan Gohmanea12c0c2008-08-20 16:30:28 +00004640 if (!Fast && ST->isUnindexed()) {
Chris Lattner4e137af2008-01-25 07:20:16 +00004641 if (unsigned Align = InferAlignment(Ptr, DAG)) {
4642 if (Align > ST->getAlignment())
4643 return DAG.getTruncStore(Chain, Value, Ptr, ST->getSrcValue(),
Dan Gohman9a4c92c2008-01-30 00:15:11 +00004644 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattner4e137af2008-01-25 07:20:16 +00004645 ST->isVolatile(), Align);
4646 }
4647 }
Duncan Sands2418bec2008-06-13 19:07:40 +00004648
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004649 // If this is a store of a bit convert, store the input value if the
4650 // resultant store does not need a higher alignment than the original.
4651 if (Value.getOpcode() == ISD::BIT_CONVERT && !ST->isTruncatingStore() &&
Chris Lattner3bc08502008-01-17 19:59:44 +00004652 ST->isUnindexed()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004653 unsigned Align = ST->getAlignment();
Duncan Sands92c43912008-06-06 12:08:01 +00004654 MVT SVT = Value.getOperand(0).getValueType();
Dan Gohman404e8542008-09-04 15:39:15 +00004655 unsigned OrigAlign = TLI.getTargetData()->
Duncan Sands92c43912008-06-06 12:08:01 +00004656 getABITypeAlignment(SVT.getTypeForMVT());
Duncan Sands2418bec2008-06-13 19:07:40 +00004657 if (Align <= OrigAlign &&
4658 ((!AfterLegalize && !ST->isVolatile()) ||
4659 TLI.isOperationLegal(ISD::STORE, SVT)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004660 return DAG.getStore(Chain, Value.getOperand(0), Ptr, ST->getSrcValue(),
Dan Gohman55a11de2008-06-28 00:45:22 +00004661 ST->getSrcValueOffset(), ST->isVolatile(), OrigAlign);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004662 }
Duncan Sands2418bec2008-06-13 19:07:40 +00004663
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004664 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
4665 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Value)) {
Duncan Sands2418bec2008-06-13 19:07:40 +00004666 // NOTE: If the original store is volatile, this transform must not increase
4667 // the number of stores. For example, on x86-32 an f64 can be stored in one
4668 // processor operation but an i64 (which is not legal) requires two. So the
4669 // transform should not be done in this case.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004670 if (Value.getOpcode() != ISD::TargetConstantFP) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004671 SDValue Tmp;
Duncan Sands92c43912008-06-06 12:08:01 +00004672 switch (CFP->getValueType(0).getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004673 default: assert(0 && "Unknown FP type");
Dale Johannesen1b4181d2007-09-18 18:36:59 +00004674 case MVT::f80: // We don't do this for these yet.
4675 case MVT::f128:
4676 case MVT::ppcf128:
4677 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004678 case MVT::f32:
Duncan Sands2418bec2008-06-13 19:07:40 +00004679 if ((!AfterLegalize && !ST->isVolatile()) ||
4680 TLI.isOperationLegal(ISD::STORE, MVT::i32)) {
Dale Johannesenfbd9cda2007-09-12 03:30:33 +00004681 Tmp = DAG.getConstant((uint32_t)CFP->getValueAPF().
Dale Johannesen49cc7ce2008-10-09 18:53:47 +00004682 bitcastToAPInt().getZExtValue(), MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004683 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
4684 ST->getSrcValueOffset(), ST->isVolatile(),
4685 ST->getAlignment());
4686 }
4687 break;
4688 case MVT::f64:
Duncan Sands2418bec2008-06-13 19:07:40 +00004689 if ((!AfterLegalize && !ST->isVolatile()) ||
4690 TLI.isOperationLegal(ISD::STORE, MVT::i64)) {
Dale Johannesen49cc7ce2008-10-09 18:53:47 +00004691 Tmp = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
Dale Johannesenfbd9cda2007-09-12 03:30:33 +00004692 getZExtValue(), MVT::i64);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004693 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
4694 ST->getSrcValueOffset(), ST->isVolatile(),
4695 ST->getAlignment());
Duncan Sands2418bec2008-06-13 19:07:40 +00004696 } else if (!ST->isVolatile() &&
4697 TLI.isOperationLegal(ISD::STORE, MVT::i32)) {
Duncan Sandsa3691432007-10-28 12:59:45 +00004698 // Many FP stores are not made apparent until after legalize, e.g. for
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004699 // argument passing. Since this is so common, custom legalize the
4700 // 64-bit integer store into two 32-bit stores.
Dale Johannesen49cc7ce2008-10-09 18:53:47 +00004701 uint64_t Val = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00004702 SDValue Lo = DAG.getConstant(Val & 0xFFFFFFFF, MVT::i32);
4703 SDValue Hi = DAG.getConstant(Val >> 32, MVT::i32);
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00004704 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004705
4706 int SVOffset = ST->getSrcValueOffset();
4707 unsigned Alignment = ST->getAlignment();
4708 bool isVolatile = ST->isVolatile();
4709
Dan Gohman8181bd12008-07-27 21:46:04 +00004710 SDValue St0 = DAG.getStore(Chain, Lo, Ptr, ST->getSrcValue(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004711 ST->getSrcValueOffset(),
4712 isVolatile, ST->getAlignment());
4713 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
4714 DAG.getConstant(4, Ptr.getValueType()));
4715 SVOffset += 4;
Duncan Sandsa3691432007-10-28 12:59:45 +00004716 Alignment = MinAlign(Alignment, 4U);
Dan Gohman8181bd12008-07-27 21:46:04 +00004717 SDValue St1 = DAG.getStore(Chain, Hi, Ptr, ST->getSrcValue(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004718 SVOffset, isVolatile, Alignment);
4719 return DAG.getNode(ISD::TokenFactor, MVT::Other, St0, St1);
4720 }
4721 break;
4722 }
4723 }
4724 }
4725
4726 if (CombinerAA) {
4727 // Walk up chain skipping non-aliasing memory nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00004728 SDValue BetterChain = FindBetterChain(N, Chain);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004729
4730 // If there is a better chain.
4731 if (Chain != BetterChain) {
4732 // Replace the chain to avoid dependency.
Dan Gohman8181bd12008-07-27 21:46:04 +00004733 SDValue ReplStore;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004734 if (ST->isTruncatingStore()) {
4735 ReplStore = DAG.getTruncStore(BetterChain, Value, Ptr,
Chris Lattner667f9c12008-01-17 07:20:38 +00004736 ST->getSrcValue(),ST->getSrcValueOffset(),
Dan Gohman9a4c92c2008-01-30 00:15:11 +00004737 ST->getMemoryVT(),
Chris Lattner667f9c12008-01-17 07:20:38 +00004738 ST->isVolatile(), ST->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004739 } else {
4740 ReplStore = DAG.getStore(BetterChain, Value, Ptr,
Chris Lattner667f9c12008-01-17 07:20:38 +00004741 ST->getSrcValue(), ST->getSrcValueOffset(),
4742 ST->isVolatile(), ST->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004743 }
4744
4745 // Create token to keep both nodes around.
Dan Gohman8181bd12008-07-27 21:46:04 +00004746 SDValue Token =
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004747 DAG.getNode(ISD::TokenFactor, MVT::Other, Chain, ReplStore);
4748
4749 // Don't add users to work list.
4750 return CombineTo(N, Token, false);
4751 }
4752 }
4753
4754 // Try transforming N to an indexed store.
4755 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Dan Gohman8181bd12008-07-27 21:46:04 +00004756 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004757
Chris Lattner447d8e82007-12-29 06:26:16 +00004758 // FIXME: is there such a thing as a truncating indexed store?
Chris Lattner3bc08502008-01-17 19:59:44 +00004759 if (ST->isTruncatingStore() && ST->isUnindexed() &&
Duncan Sands92c43912008-06-06 12:08:01 +00004760 Value.getValueType().isInteger()) {
Chris Lattnere8671c52007-10-13 06:35:54 +00004761 // See if we can simplify the input to this truncstore with knowledge that
4762 // only the low bits are being used. For example:
4763 // "truncstore (or (shl x, 8), y), i8" -> "truncstore y, i8"
Dan Gohman8181bd12008-07-27 21:46:04 +00004764 SDValue Shorter =
Dan Gohman07961cd2008-02-25 21:11:39 +00004765 GetDemandedBits(Value,
4766 APInt::getLowBitsSet(Value.getValueSizeInBits(),
Duncan Sands92c43912008-06-06 12:08:01 +00004767 ST->getMemoryVT().getSizeInBits()));
Gabor Greif1c80d112008-08-28 21:40:38 +00004768 AddToWorkList(Value.getNode());
4769 if (Shorter.getNode())
Chris Lattnere8671c52007-10-13 06:35:54 +00004770 return DAG.getTruncStore(Chain, Shorter, Ptr, ST->getSrcValue(),
Dan Gohman9a4c92c2008-01-30 00:15:11 +00004771 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattnere8671c52007-10-13 06:35:54 +00004772 ST->isVolatile(), ST->getAlignment());
Chris Lattnerb77ea552007-10-13 06:58:48 +00004773
4774 // Otherwise, see if we can simplify the operation with
4775 // SimplifyDemandedBits, which only works if the value has a single use.
Dan Gohman11607792008-02-27 00:25:32 +00004776 if (SimplifyDemandedBits(Value,
4777 APInt::getLowBitsSet(
4778 Value.getValueSizeInBits(),
Duncan Sands92c43912008-06-06 12:08:01 +00004779 ST->getMemoryVT().getSizeInBits())))
Dan Gohman8181bd12008-07-27 21:46:04 +00004780 return SDValue(N, 0);
Chris Lattnere8671c52007-10-13 06:35:54 +00004781 }
4782
Chris Lattner447d8e82007-12-29 06:26:16 +00004783 // If this is a load followed by a store to the same location, then the store
4784 // is dead/noop.
4785 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Value)) {
Dan Gohman9a4c92c2008-01-30 00:15:11 +00004786 if (Ld->getBasePtr() == Ptr && ST->getMemoryVT() == Ld->getMemoryVT() &&
Chris Lattner3bc08502008-01-17 19:59:44 +00004787 ST->isUnindexed() && !ST->isVolatile() &&
Chris Lattner2e023772008-01-08 23:08:06 +00004788 // There can't be any side effects between the load and store, such as
4789 // a call or store.
Dan Gohman8181bd12008-07-27 21:46:04 +00004790 Chain.reachesChainWithoutSideEffects(SDValue(Ld, 1))) {
Chris Lattner447d8e82007-12-29 06:26:16 +00004791 // The store is dead, remove it.
4792 return Chain;
4793 }
4794 }
Duncan Sands2418bec2008-06-13 19:07:40 +00004795
Chris Lattner3bc08502008-01-17 19:59:44 +00004796 // If this is an FP_ROUND or TRUNC followed by a store, fold this into a
4797 // truncating store. We can do this even if this is already a truncstore.
4798 if ((Value.getOpcode() == ISD::FP_ROUND || Value.getOpcode() == ISD::TRUNCATE)
Gabor Greif1c80d112008-08-28 21:40:38 +00004799 && Value.getNode()->hasOneUse() && ST->isUnindexed() &&
Chris Lattner3bc08502008-01-17 19:59:44 +00004800 TLI.isTruncStoreLegal(Value.getOperand(0).getValueType(),
Dan Gohman9a4c92c2008-01-30 00:15:11 +00004801 ST->getMemoryVT())) {
Chris Lattner3bc08502008-01-17 19:59:44 +00004802 return DAG.getTruncStore(Chain, Value.getOperand(0), Ptr, ST->getSrcValue(),
Dan Gohman9a4c92c2008-01-30 00:15:11 +00004803 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattner3bc08502008-01-17 19:59:44 +00004804 ST->isVolatile(), ST->getAlignment());
4805 }
Duncan Sands2418bec2008-06-13 19:07:40 +00004806
Dan Gohman8181bd12008-07-27 21:46:04 +00004807 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004808}
4809
Dan Gohman8181bd12008-07-27 21:46:04 +00004810SDValue DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
4811 SDValue InVec = N->getOperand(0);
4812 SDValue InVal = N->getOperand(1);
4813 SDValue EltNo = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004814
4815 // If the invec is a BUILD_VECTOR and if EltNo is a constant, build a new
4816 // vector with the inserted element.
4817 if (InVec.getOpcode() == ISD::BUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004818 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Gabor Greifb420b9d2008-08-30 19:29:20 +00004819 SmallVector<SDValue, 8> Ops(InVec.getNode()->op_begin(),
4820 InVec.getNode()->op_end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004821 if (Elt < Ops.size())
4822 Ops[Elt] = InVal;
4823 return DAG.getNode(ISD::BUILD_VECTOR, InVec.getValueType(),
4824 &Ops[0], Ops.size());
4825 }
4826
Dan Gohman8181bd12008-07-27 21:46:04 +00004827 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004828}
4829
Dan Gohman8181bd12008-07-27 21:46:04 +00004830SDValue DAGCombiner::visitEXTRACT_VECTOR_ELT(SDNode *N) {
Evan Cheng411fc172008-05-13 08:35:03 +00004831 // (vextract (v4f32 load $addr), c) -> (f32 load $addr+c*size)
4832 // (vextract (v4f32 s2v (f32 load $addr)), c) -> (f32 load $addr+c*size)
4833 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), 0) -> (f32 load $addr)
4834
4835 // Perform only after legalization to ensure build_vector / vector_shuffle
4836 // optimizations have already been done.
Dan Gohman8181bd12008-07-27 21:46:04 +00004837 if (!AfterLegalize) return SDValue();
Evan Cheng411fc172008-05-13 08:35:03 +00004838
Dan Gohman8181bd12008-07-27 21:46:04 +00004839 SDValue InVec = N->getOperand(0);
4840 SDValue EltNo = N->getOperand(1);
Evan Chengd7ba7ed2007-10-06 08:19:55 +00004841
Evan Chengd7ba7ed2007-10-06 08:19:55 +00004842 if (isa<ConstantSDNode>(EltNo)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004843 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Evan Chengd7ba7ed2007-10-06 08:19:55 +00004844 bool NewLoad = false;
Duncan Sands92c43912008-06-06 12:08:01 +00004845 MVT VT = InVec.getValueType();
4846 MVT EVT = VT.getVectorElementType();
4847 MVT LVT = EVT;
Evan Cheng411fc172008-05-13 08:35:03 +00004848 if (InVec.getOpcode() == ISD::BIT_CONVERT) {
Duncan Sands92c43912008-06-06 12:08:01 +00004849 MVT BCVT = InVec.getOperand(0).getValueType();
Duncan Sandsec142ee2008-06-08 20:54:56 +00004850 if (!BCVT.isVector() || EVT.bitsGT(BCVT.getVectorElementType()))
Dan Gohman8181bd12008-07-27 21:46:04 +00004851 return SDValue();
Evan Cheng411fc172008-05-13 08:35:03 +00004852 InVec = InVec.getOperand(0);
Duncan Sands92c43912008-06-06 12:08:01 +00004853 EVT = BCVT.getVectorElementType();
Evan Cheng411fc172008-05-13 08:35:03 +00004854 NewLoad = true;
4855 }
Evan Chengd7ba7ed2007-10-06 08:19:55 +00004856
Evan Cheng411fc172008-05-13 08:35:03 +00004857 LoadSDNode *LN0 = NULL;
Gabor Greif1c80d112008-08-28 21:40:38 +00004858 if (ISD::isNormalLoad(InVec.getNode()))
Evan Cheng411fc172008-05-13 08:35:03 +00004859 LN0 = cast<LoadSDNode>(InVec);
4860 else if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR &&
4861 InVec.getOperand(0).getValueType() == EVT &&
Gabor Greif1c80d112008-08-28 21:40:38 +00004862 ISD::isNormalLoad(InVec.getOperand(0).getNode())) {
Evan Cheng411fc172008-05-13 08:35:03 +00004863 LN0 = cast<LoadSDNode>(InVec.getOperand(0));
4864 } else if (InVec.getOpcode() == ISD::VECTOR_SHUFFLE) {
4865 // (vextract (vector_shuffle (load $addr), v2, <1, u, u, u>), 1)
4866 // =>
4867 // (load $addr+1*size)
4868 unsigned Idx = cast<ConstantSDNode>(InVec.getOperand(2).
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004869 getOperand(Elt))->getZExtValue();
Evan Cheng411fc172008-05-13 08:35:03 +00004870 unsigned NumElems = InVec.getOperand(2).getNumOperands();
4871 InVec = (Idx < NumElems) ? InVec.getOperand(0) : InVec.getOperand(1);
4872 if (InVec.getOpcode() == ISD::BIT_CONVERT)
4873 InVec = InVec.getOperand(0);
Gabor Greif1c80d112008-08-28 21:40:38 +00004874 if (ISD::isNormalLoad(InVec.getNode())) {
Evan Cheng411fc172008-05-13 08:35:03 +00004875 LN0 = cast<LoadSDNode>(InVec);
4876 Elt = (Idx < NumElems) ? Idx : Idx - NumElems;
Evan Chengd7ba7ed2007-10-06 08:19:55 +00004877 }
4878 }
Duncan Sandsc218a5a2008-06-15 20:12:31 +00004879 if (!LN0 || !LN0->hasOneUse() || LN0->isVolatile())
Dan Gohman8181bd12008-07-27 21:46:04 +00004880 return SDValue();
Evan Cheng411fc172008-05-13 08:35:03 +00004881
4882 unsigned Align = LN0->getAlignment();
4883 if (NewLoad) {
4884 // Check the resultant load doesn't need a higher alignment than the
4885 // original load.
Dan Gohman404e8542008-09-04 15:39:15 +00004886 unsigned NewAlign = TLI.getTargetData()->
Duncan Sands92c43912008-06-06 12:08:01 +00004887 getABITypeAlignment(LVT.getTypeForMVT());
Duncan Sands6ae1a0632008-06-14 17:48:34 +00004888 if (NewAlign > Align || !TLI.isOperationLegal(ISD::LOAD, LVT))
Dan Gohman8181bd12008-07-27 21:46:04 +00004889 return SDValue();
Evan Cheng411fc172008-05-13 08:35:03 +00004890 Align = NewAlign;
4891 }
4892
Dan Gohman8181bd12008-07-27 21:46:04 +00004893 SDValue NewPtr = LN0->getBasePtr();
Evan Cheng411fc172008-05-13 08:35:03 +00004894 if (Elt) {
Duncan Sands92c43912008-06-06 12:08:01 +00004895 unsigned PtrOff = LVT.getSizeInBits() * Elt / 8;
4896 MVT PtrType = NewPtr.getValueType();
Evan Cheng411fc172008-05-13 08:35:03 +00004897 if (TLI.isBigEndian())
Duncan Sands92c43912008-06-06 12:08:01 +00004898 PtrOff = VT.getSizeInBits() / 8 - PtrOff;
Evan Cheng411fc172008-05-13 08:35:03 +00004899 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
4900 DAG.getConstant(PtrOff, PtrType));
4901 }
4902 return DAG.getLoad(LVT, LN0->getChain(), NewPtr,
4903 LN0->getSrcValue(), LN0->getSrcValueOffset(),
4904 LN0->isVolatile(), Align);
Evan Chengd7ba7ed2007-10-06 08:19:55 +00004905 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004906 return SDValue();
Evan Chengd7ba7ed2007-10-06 08:19:55 +00004907}
4908
4909
Dan Gohman8181bd12008-07-27 21:46:04 +00004910SDValue DAGCombiner::visitBUILD_VECTOR(SDNode *N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004911 unsigned NumInScalars = N->getNumOperands();
Duncan Sands92c43912008-06-06 12:08:01 +00004912 MVT VT = N->getValueType(0);
4913 unsigned NumElts = VT.getVectorNumElements();
4914 MVT EltType = VT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004915
4916 // Check to see if this is a BUILD_VECTOR of a bunch of EXTRACT_VECTOR_ELT
4917 // operations. If so, and if the EXTRACT_VECTOR_ELT vector inputs come from
4918 // at most two distinct vectors, turn this into a shuffle node.
Dan Gohman8181bd12008-07-27 21:46:04 +00004919 SDValue VecIn1, VecIn2;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004920 for (unsigned i = 0; i != NumInScalars; ++i) {
4921 // Ignore undef inputs.
4922 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
4923
4924 // If this input is something other than a EXTRACT_VECTOR_ELT with a
4925 // constant index, bail out.
4926 if (N->getOperand(i).getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
4927 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004928 VecIn1 = VecIn2 = SDValue(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004929 break;
4930 }
4931
4932 // If the input vector type disagrees with the result of the build_vector,
4933 // we can't make a shuffle.
Dan Gohman8181bd12008-07-27 21:46:04 +00004934 SDValue ExtractedFromVec = N->getOperand(i).getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004935 if (ExtractedFromVec.getValueType() != VT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004936 VecIn1 = VecIn2 = SDValue(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004937 break;
4938 }
4939
4940 // Otherwise, remember this. We allow up to two distinct input vectors.
4941 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
4942 continue;
4943
Gabor Greif1c80d112008-08-28 21:40:38 +00004944 if (VecIn1.getNode() == 0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004945 VecIn1 = ExtractedFromVec;
Gabor Greif1c80d112008-08-28 21:40:38 +00004946 } else if (VecIn2.getNode() == 0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004947 VecIn2 = ExtractedFromVec;
4948 } else {
4949 // Too many inputs.
Dan Gohman8181bd12008-07-27 21:46:04 +00004950 VecIn1 = VecIn2 = SDValue(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004951 break;
4952 }
4953 }
4954
4955 // If everything is good, we can make a shuffle operation.
Gabor Greif1c80d112008-08-28 21:40:38 +00004956 if (VecIn1.getNode()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004957 SmallVector<SDValue, 8> BuildVecIndices;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004958 for (unsigned i = 0; i != NumInScalars; ++i) {
4959 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
4960 BuildVecIndices.push_back(DAG.getNode(ISD::UNDEF, TLI.getPointerTy()));
4961 continue;
4962 }
4963
Dan Gohman8181bd12008-07-27 21:46:04 +00004964 SDValue Extract = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004965
4966 // If extracting from the first vector, just use the index directly.
4967 if (Extract.getOperand(0) == VecIn1) {
4968 BuildVecIndices.push_back(Extract.getOperand(1));
4969 continue;
4970 }
4971
4972 // Otherwise, use InIdx + VecSize
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004973 unsigned Idx =
4974 cast<ConstantSDNode>(Extract.getOperand(1))->getZExtValue();
Chris Lattner5872a362008-01-17 07:00:52 +00004975 BuildVecIndices.push_back(DAG.getIntPtrConstant(Idx+NumInScalars));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004976 }
4977
4978 // Add count and size info.
Duncan Sands92c43912008-06-06 12:08:01 +00004979 MVT BuildVecVT = MVT::getVectorVT(TLI.getPointerTy(), NumElts);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004980
4981 // Return the new VECTOR_SHUFFLE node.
Dan Gohman8181bd12008-07-27 21:46:04 +00004982 SDValue Ops[5];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004983 Ops[0] = VecIn1;
Gabor Greif1c80d112008-08-28 21:40:38 +00004984 if (VecIn2.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004985 Ops[1] = VecIn2;
4986 } else {
4987 // Use an undef build_vector as input for the second operand.
Dan Gohman8181bd12008-07-27 21:46:04 +00004988 std::vector<SDValue> UnOps(NumInScalars,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004989 DAG.getNode(ISD::UNDEF,
4990 EltType));
4991 Ops[1] = DAG.getNode(ISD::BUILD_VECTOR, VT,
4992 &UnOps[0], UnOps.size());
Gabor Greif1c80d112008-08-28 21:40:38 +00004993 AddToWorkList(Ops[1].getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004994 }
4995 Ops[2] = DAG.getNode(ISD::BUILD_VECTOR, BuildVecVT,
4996 &BuildVecIndices[0], BuildVecIndices.size());
4997 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Ops, 3);
4998 }
4999
Dan Gohman8181bd12008-07-27 21:46:04 +00005000 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005001}
5002
Dan Gohman8181bd12008-07-27 21:46:04 +00005003SDValue DAGCombiner::visitCONCAT_VECTORS(SDNode *N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005004 // TODO: Check to see if this is a CONCAT_VECTORS of a bunch of
5005 // EXTRACT_SUBVECTOR operations. If so, and if the EXTRACT_SUBVECTOR vector
5006 // inputs come from at most two distinct vectors, turn this into a shuffle
5007 // node.
5008
5009 // If we only have one input vector, we don't need to do any concatenation.
5010 if (N->getNumOperands() == 1) {
5011 return N->getOperand(0);
5012 }
5013
Dan Gohman8181bd12008-07-27 21:46:04 +00005014 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005015}
5016
Dan Gohman8181bd12008-07-27 21:46:04 +00005017SDValue DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
5018 SDValue ShufMask = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005019 unsigned NumElts = ShufMask.getNumOperands();
5020
Mon P Wangbff5d9c2008-11-10 04:46:22 +00005021 SDValue N0 = N->getOperand(0);
5022 SDValue N1 = N->getOperand(1);
5023
5024 assert(N0.getValueType().getVectorNumElements() == NumElts &&
5025 "Vector shuffle must be normalized in DAG");
5026
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005027 // If the shuffle mask is an identity operation on the LHS, return the LHS.
5028 bool isIdentity = true;
5029 for (unsigned i = 0; i != NumElts; ++i) {
5030 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005031 cast<ConstantSDNode>(ShufMask.getOperand(i))->getZExtValue() != i) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005032 isIdentity = false;
5033 break;
5034 }
5035 }
5036 if (isIdentity) return N->getOperand(0);
5037
5038 // If the shuffle mask is an identity operation on the RHS, return the RHS.
5039 isIdentity = true;
5040 for (unsigned i = 0; i != NumElts; ++i) {
5041 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005042 cast<ConstantSDNode>(ShufMask.getOperand(i))->getZExtValue() !=
5043 i+NumElts) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005044 isIdentity = false;
5045 break;
5046 }
5047 }
5048 if (isIdentity) return N->getOperand(1);
5049
5050 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
5051 // needed at all.
5052 bool isUnary = true;
5053 bool isSplat = true;
5054 int VecNum = -1;
5055 unsigned BaseIdx = 0;
5056 for (unsigned i = 0; i != NumElts; ++i)
5057 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005058 unsigned Idx=cast<ConstantSDNode>(ShufMask.getOperand(i))->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005059 int V = (Idx < NumElts) ? 0 : 1;
5060 if (VecNum == -1) {
5061 VecNum = V;
5062 BaseIdx = Idx;
5063 } else {
5064 if (BaseIdx != Idx)
5065 isSplat = false;
5066 if (VecNum != V) {
5067 isUnary = false;
5068 break;
5069 }
5070 }
5071 }
5072
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005073 // Normalize unary shuffle so the RHS is undef.
5074 if (isUnary && VecNum == 1)
5075 std::swap(N0, N1);
5076
5077 // If it is a splat, check if the argument vector is a build_vector with
5078 // all scalar elements the same.
5079 if (isSplat) {
Gabor Greif1c80d112008-08-28 21:40:38 +00005080 SDNode *V = N0.getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005081
5082 // If this is a bit convert that changes the element type of the vector but
5083 // not the number of vector elements, look through it. Be careful not to
5084 // look though conversions that change things like v4f32 to v2f64.
5085 if (V->getOpcode() == ISD::BIT_CONVERT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005086 SDValue ConvInput = V->getOperand(0);
Evan Cheng76b20d12008-07-22 20:42:56 +00005087 if (ConvInput.getValueType().isVector() &&
5088 ConvInput.getValueType().getVectorNumElements() == NumElts)
Gabor Greif1c80d112008-08-28 21:40:38 +00005089 V = ConvInput.getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005090 }
5091
5092 if (V->getOpcode() == ISD::BUILD_VECTOR) {
5093 unsigned NumElems = V->getNumOperands();
5094 if (NumElems > BaseIdx) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005095 SDValue Base;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005096 bool AllSame = true;
5097 for (unsigned i = 0; i != NumElems; ++i) {
5098 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
5099 Base = V->getOperand(i);
5100 break;
5101 }
5102 }
5103 // Splat of <u, u, u, u>, return <u, u, u, u>
Gabor Greif1c80d112008-08-28 21:40:38 +00005104 if (!Base.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005105 return N0;
5106 for (unsigned i = 0; i != NumElems; ++i) {
Evan Cheng8d68c2b2007-09-18 21:54:37 +00005107 if (V->getOperand(i) != Base) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005108 AllSame = false;
5109 break;
5110 }
5111 }
5112 // Splat of <x, x, x, x>, return <x, x, x, x>
5113 if (AllSame)
5114 return N0;
5115 }
5116 }
5117 }
5118
5119 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
5120 // into an undef.
5121 if (isUnary || N0 == N1) {
5122 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
5123 // first operand.
Dan Gohman8181bd12008-07-27 21:46:04 +00005124 SmallVector<SDValue, 8> MappedOps;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005125 for (unsigned i = 0; i != NumElts; ++i) {
5126 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005127 cast<ConstantSDNode>(ShufMask.getOperand(i))->getZExtValue() <
5128 NumElts) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005129 MappedOps.push_back(ShufMask.getOperand(i));
5130 } else {
5131 unsigned NewIdx =
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005132 cast<ConstantSDNode>(ShufMask.getOperand(i))->getZExtValue() -
5133 NumElts;
Duncan Sandsd3ace282008-07-21 10:20:31 +00005134 MappedOps.push_back(DAG.getConstant(NewIdx,
5135 ShufMask.getOperand(i).getValueType()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005136 }
5137 }
5138 ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMask.getValueType(),
5139 &MappedOps[0], MappedOps.size());
Gabor Greif1c80d112008-08-28 21:40:38 +00005140 AddToWorkList(ShufMask.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005141 return DAG.getNode(ISD::VECTOR_SHUFFLE, N->getValueType(0),
5142 N0,
5143 DAG.getNode(ISD::UNDEF, N->getValueType(0)),
5144 ShufMask);
5145 }
5146
Dan Gohman8181bd12008-07-27 21:46:04 +00005147 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005148}
5149
Bill Wendlingea8b7c92008-11-21 02:12:42 +00005150SDValue DAGCombiner::visitSADDO(SDNode *N) {
Bill Wendling5fc7e5c2008-11-21 02:03:52 +00005151 SDValue Chain = N->getOperand(2);
5152 SDValue LHS = N->getOperand(0);
5153 SDValue RHS = N->getOperand(1);
5154
5155 SDValue Sum = DAG.getNode(ISD::ADD, LHS.getValueType(), LHS, RHS);
5156 AddToWorkList(Sum.getNode());
5157 SDValue Cmp = DAG.getSetCC(MVT::i1, Sum, LHS, ISD::SETLT);
5158 AddToWorkList(Cmp.getNode());
5159
5160 MVT ValueVTs[] = { LHS.getValueType(), MVT::i1, MVT::Other };
5161 SDValue Ops[] = { Sum, Cmp, Chain };
5162
5163 SDValue Merge = DAG.getMergeValues(DAG.getVTList(&ValueVTs[0], 3),
5164 &Ops[0], 3);
5165 SDNode *MNode = Merge.getNode();
5166
5167 AddToWorkList(MNode);
5168 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), SDValue(MNode, 0));
5169 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SDValue(MNode, 1));
5170 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 2), SDValue(MNode, 2));
5171
5172 // Since the node is now dead, remove it from the graph.
5173 removeFromWorkList(N);
5174 DAG.DeleteNode(N);
5175 return SDValue(N, 0);
5176}
5177
Bill Wendlingea8b7c92008-11-21 02:12:42 +00005178SDValue DAGCombiner::visitUADDO(SDNode *N) {
5179 return SDValue();
5180}
5181
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005182/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
5183/// an AND to a vector_shuffle with the destination vector and a zero vector.
5184/// e.g. AND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
5185/// vector_shuffle V, Zero, <0, 4, 2, 4>
Dan Gohman8181bd12008-07-27 21:46:04 +00005186SDValue DAGCombiner::XformToShuffleWithZero(SDNode *N) {
5187 SDValue LHS = N->getOperand(0);
5188 SDValue RHS = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005189 if (N->getOpcode() == ISD::AND) {
5190 if (RHS.getOpcode() == ISD::BIT_CONVERT)
5191 RHS = RHS.getOperand(0);
5192 if (RHS.getOpcode() == ISD::BUILD_VECTOR) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005193 std::vector<SDValue> IdxOps;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005194 unsigned NumOps = RHS.getNumOperands();
5195 unsigned NumElts = NumOps;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005196 for (unsigned i = 0; i != NumElts; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005197 SDValue Elt = RHS.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005198 if (!isa<ConstantSDNode>(Elt))
Dan Gohman8181bd12008-07-27 21:46:04 +00005199 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005200 else if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
Duncan Sands193c2bd2008-10-19 14:58:05 +00005201 IdxOps.push_back(DAG.getIntPtrConstant(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005202 else if (cast<ConstantSDNode>(Elt)->isNullValue())
Duncan Sands193c2bd2008-10-19 14:58:05 +00005203 IdxOps.push_back(DAG.getIntPtrConstant(NumElts));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005204 else
Dan Gohman8181bd12008-07-27 21:46:04 +00005205 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005206 }
5207
5208 // Let's see if the target supports this vector_shuffle.
Duncan Sands193c2bd2008-10-19 14:58:05 +00005209 if (!TLI.isVectorClearMaskLegal(IdxOps, TLI.getPointerTy(), DAG))
Dan Gohman8181bd12008-07-27 21:46:04 +00005210 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005211
5212 // Return the new VECTOR_SHUFFLE node.
Duncan Sands193c2bd2008-10-19 14:58:05 +00005213 MVT EVT = RHS.getValueType().getVectorElementType();
Duncan Sands92c43912008-06-06 12:08:01 +00005214 MVT VT = MVT::getVectorVT(EVT, NumElts);
Evan Cheng4fdfdac2008-11-05 06:04:18 +00005215 MVT MaskVT = MVT::getVectorVT(TLI.getPointerTy(), NumElts);
Dan Gohman8181bd12008-07-27 21:46:04 +00005216 std::vector<SDValue> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005217 LHS = DAG.getNode(ISD::BIT_CONVERT, VT, LHS);
5218 Ops.push_back(LHS);
Gabor Greif1c80d112008-08-28 21:40:38 +00005219 AddToWorkList(LHS.getNode());
Dan Gohman8181bd12008-07-27 21:46:04 +00005220 std::vector<SDValue> ZeroOps(NumElts, DAG.getConstant(0, EVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005221 Ops.push_back(DAG.getNode(ISD::BUILD_VECTOR, VT,
5222 &ZeroOps[0], ZeroOps.size()));
Evan Cheng4fdfdac2008-11-05 06:04:18 +00005223 Ops.push_back(DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005224 &IdxOps[0], IdxOps.size()));
Dan Gohman8181bd12008-07-27 21:46:04 +00005225 SDValue Result = DAG.getNode(ISD::VECTOR_SHUFFLE, VT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005226 &Ops[0], Ops.size());
Dan Gohman4c219902008-07-16 16:13:58 +00005227 if (VT != N->getValueType(0))
5228 Result = DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Result);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005229 return Result;
5230 }
5231 }
Dan Gohman8181bd12008-07-27 21:46:04 +00005232 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005233}
5234
5235/// SimplifyVBinOp - Visit a binary vector operation, like ADD.
Dan Gohman8181bd12008-07-27 21:46:04 +00005236SDValue DAGCombiner::SimplifyVBinOp(SDNode *N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005237 // After legalize, the target may be depending on adds and other
5238 // binary ops to provide legal ways to construct constants or other
5239 // things. Simplifying them may result in a loss of legality.
Dan Gohman8181bd12008-07-27 21:46:04 +00005240 if (AfterLegalize) return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005241
Duncan Sands92c43912008-06-06 12:08:01 +00005242 MVT VT = N->getValueType(0);
5243 assert(VT.isVector() && "SimplifyVBinOp only works on vectors!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005244
Duncan Sands92c43912008-06-06 12:08:01 +00005245 MVT EltType = VT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00005246 SDValue LHS = N->getOperand(0);
5247 SDValue RHS = N->getOperand(1);
5248 SDValue Shuffle = XformToShuffleWithZero(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00005249 if (Shuffle.getNode()) return Shuffle;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005250
5251 // If the LHS and RHS are BUILD_VECTOR nodes, see if we can constant fold
5252 // this operation.
5253 if (LHS.getOpcode() == ISD::BUILD_VECTOR &&
5254 RHS.getOpcode() == ISD::BUILD_VECTOR) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005255 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005256 for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005257 SDValue LHSOp = LHS.getOperand(i);
5258 SDValue RHSOp = RHS.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005259 // If these two elements can't be folded, bail out.
5260 if ((LHSOp.getOpcode() != ISD::UNDEF &&
5261 LHSOp.getOpcode() != ISD::Constant &&
5262 LHSOp.getOpcode() != ISD::ConstantFP) ||
5263 (RHSOp.getOpcode() != ISD::UNDEF &&
5264 RHSOp.getOpcode() != ISD::Constant &&
5265 RHSOp.getOpcode() != ISD::ConstantFP))
5266 break;
5267 // Can't fold divide by zero.
5268 if (N->getOpcode() == ISD::SDIV || N->getOpcode() == ISD::UDIV ||
5269 N->getOpcode() == ISD::FDIV) {
5270 if ((RHSOp.getOpcode() == ISD::Constant &&
Gabor Greif1c80d112008-08-28 21:40:38 +00005271 cast<ConstantSDNode>(RHSOp.getNode())->isNullValue()) ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005272 (RHSOp.getOpcode() == ISD::ConstantFP &&
Gabor Greif1c80d112008-08-28 21:40:38 +00005273 cast<ConstantFPSDNode>(RHSOp.getNode())->getValueAPF().isZero()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005274 break;
5275 }
5276 Ops.push_back(DAG.getNode(N->getOpcode(), EltType, LHSOp, RHSOp));
Gabor Greif1c80d112008-08-28 21:40:38 +00005277 AddToWorkList(Ops.back().getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005278 assert((Ops.back().getOpcode() == ISD::UNDEF ||
5279 Ops.back().getOpcode() == ISD::Constant ||
5280 Ops.back().getOpcode() == ISD::ConstantFP) &&
5281 "Scalar binop didn't fold!");
5282 }
5283
5284 if (Ops.size() == LHS.getNumOperands()) {
Duncan Sands92c43912008-06-06 12:08:01 +00005285 MVT VT = LHS.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005286 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
5287 }
5288 }
5289
Dan Gohman8181bd12008-07-27 21:46:04 +00005290 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005291}
5292
Dan Gohman8181bd12008-07-27 21:46:04 +00005293SDValue DAGCombiner::SimplifySelect(SDValue N0, SDValue N1, SDValue N2){
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005294 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
5295
Dan Gohman8181bd12008-07-27 21:46:04 +00005296 SDValue SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005297 cast<CondCodeSDNode>(N0.getOperand(2))->get());
5298 // If we got a simplified select_cc node back from SimplifySelectCC, then
5299 // break it down into a new SETCC node, and a new SELECT node, and then return
5300 // the SELECT node, since we were called with a SELECT node.
Gabor Greif1c80d112008-08-28 21:40:38 +00005301 if (SCC.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005302 // Check to see if we got a select_cc back (to turn into setcc/select).
5303 // Otherwise, just return whatever node we got back, like fabs.
5304 if (SCC.getOpcode() == ISD::SELECT_CC) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005305 SDValue SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005306 SCC.getOperand(0), SCC.getOperand(1),
5307 SCC.getOperand(4));
Gabor Greif1c80d112008-08-28 21:40:38 +00005308 AddToWorkList(SETCC.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005309 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
5310 SCC.getOperand(3), SETCC);
5311 }
5312 return SCC;
5313 }
Dan Gohman8181bd12008-07-27 21:46:04 +00005314 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005315}
5316
5317/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
5318/// are the two values being selected between, see if we can simplify the
5319/// select. Callers of this should assume that TheSelect is deleted if this
5320/// returns true. As such, they should return the appropriate thing (e.g. the
5321/// node) back to the top-level of the DAG combiner loop to avoid it being
5322/// looked at.
5323///
Dan Gohman8181bd12008-07-27 21:46:04 +00005324bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDValue LHS,
5325 SDValue RHS) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005326
5327 // If this is a select from two identical things, try to pull the operation
5328 // through the select.
5329 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
5330 // If this is a load and the token chain is identical, replace the select
5331 // of two loads with a load through a select of the address to load from.
5332 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
5333 // constants have been dropped into the constant pool.
5334 if (LHS.getOpcode() == ISD::LOAD &&
Duncan Sands2418bec2008-06-13 19:07:40 +00005335 // Do not let this transformation reduce the number of volatile loads.
5336 !cast<LoadSDNode>(LHS)->isVolatile() &&
5337 !cast<LoadSDNode>(RHS)->isVolatile() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005338 // Token chains must be identical.
5339 LHS.getOperand(0) == RHS.getOperand(0)) {
5340 LoadSDNode *LLD = cast<LoadSDNode>(LHS);
5341 LoadSDNode *RLD = cast<LoadSDNode>(RHS);
5342
5343 // If this is an EXTLOAD, the VT's must match.
Dan Gohman9a4c92c2008-01-30 00:15:11 +00005344 if (LLD->getMemoryVT() == RLD->getMemoryVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005345 // FIXME: this conflates two src values, discarding one. This is not
5346 // the right thing to do, but nothing uses srcvalues now. When they do,
5347 // turn SrcValue into a list of locations.
Dan Gohman8181bd12008-07-27 21:46:04 +00005348 SDValue Addr;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005349 if (TheSelect->getOpcode() == ISD::SELECT) {
5350 // Check that the condition doesn't reach either load. If so, folding
5351 // this will induce a cycle into the DAG.
Gabor Greif1c80d112008-08-28 21:40:38 +00005352 if (!LLD->isPredecessorOf(TheSelect->getOperand(0).getNode()) &&
5353 !RLD->isPredecessorOf(TheSelect->getOperand(0).getNode())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005354 Addr = DAG.getNode(ISD::SELECT, LLD->getBasePtr().getValueType(),
5355 TheSelect->getOperand(0), LLD->getBasePtr(),
5356 RLD->getBasePtr());
5357 }
5358 } else {
5359 // Check that the condition doesn't reach either load. If so, folding
5360 // this will induce a cycle into the DAG.
Gabor Greif1c80d112008-08-28 21:40:38 +00005361 if (!LLD->isPredecessorOf(TheSelect->getOperand(0).getNode()) &&
5362 !RLD->isPredecessorOf(TheSelect->getOperand(0).getNode()) &&
5363 !LLD->isPredecessorOf(TheSelect->getOperand(1).getNode()) &&
5364 !RLD->isPredecessorOf(TheSelect->getOperand(1).getNode())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005365 Addr = DAG.getNode(ISD::SELECT_CC, LLD->getBasePtr().getValueType(),
5366 TheSelect->getOperand(0),
5367 TheSelect->getOperand(1),
5368 LLD->getBasePtr(), RLD->getBasePtr(),
5369 TheSelect->getOperand(4));
5370 }
5371 }
5372
Gabor Greif1c80d112008-08-28 21:40:38 +00005373 if (Addr.getNode()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005374 SDValue Load;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005375 if (LLD->getExtensionType() == ISD::NON_EXTLOAD)
5376 Load = DAG.getLoad(TheSelect->getValueType(0), LLD->getChain(),
5377 Addr,LLD->getSrcValue(),
5378 LLD->getSrcValueOffset(),
5379 LLD->isVolatile(),
5380 LLD->getAlignment());
5381 else {
5382 Load = DAG.getExtLoad(LLD->getExtensionType(),
5383 TheSelect->getValueType(0),
5384 LLD->getChain(), Addr, LLD->getSrcValue(),
5385 LLD->getSrcValueOffset(),
Dan Gohman9a4c92c2008-01-30 00:15:11 +00005386 LLD->getMemoryVT(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005387 LLD->isVolatile(),
5388 LLD->getAlignment());
5389 }
5390 // Users of the select now use the result of the load.
5391 CombineTo(TheSelect, Load);
5392
5393 // Users of the old loads now use the new load's chain. We know the
5394 // old-load value is dead now.
Gabor Greif1c80d112008-08-28 21:40:38 +00005395 CombineTo(LHS.getNode(), Load.getValue(0), Load.getValue(1));
5396 CombineTo(RHS.getNode(), Load.getValue(0), Load.getValue(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005397 return true;
5398 }
5399 }
5400 }
5401 }
5402
5403 return false;
5404}
5405
Dan Gohman8181bd12008-07-27 21:46:04 +00005406SDValue DAGCombiner::SimplifySelectCC(SDValue N0, SDValue N1,
5407 SDValue N2, SDValue N3,
5408 ISD::CondCode CC, bool NotExtCompare) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005409
Duncan Sands92c43912008-06-06 12:08:01 +00005410 MVT VT = N2.getValueType();
Gabor Greif1c80d112008-08-28 21:40:38 +00005411 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
5412 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
5413 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005414
5415 // Determine if the condition we're dealing with is constant
Dan Gohman8181bd12008-07-27 21:46:04 +00005416 SDValue SCC = SimplifySetCC(TLI.getSetCCResultType(N0), N0, N1, CC, false);
Gabor Greif1c80d112008-08-28 21:40:38 +00005417 if (SCC.getNode()) AddToWorkList(SCC.getNode());
5418 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005419
5420 // fold select_cc true, x, y -> x
Dan Gohman9d24dc72008-03-13 22:13:53 +00005421 if (SCCC && !SCCC->isNullValue())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005422 return N2;
5423 // fold select_cc false, x, y -> y
Dan Gohman9d24dc72008-03-13 22:13:53 +00005424 if (SCCC && SCCC->isNullValue())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005425 return N3;
5426
5427 // Check to see if we can simplify the select into an fabs node
5428 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
5429 // Allow either -0.0 or 0.0
Dale Johannesen7f2c1d12007-08-25 22:10:57 +00005430 if (CFP->getValueAPF().isZero()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005431 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
5432 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
5433 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
5434 N2 == N3.getOperand(0))
5435 return DAG.getNode(ISD::FABS, VT, N0);
5436
5437 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
5438 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
5439 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
5440 N2.getOperand(0) == N3)
5441 return DAG.getNode(ISD::FABS, VT, N3);
5442 }
5443 }
5444
5445 // Check to see if we can perform the "gzip trick", transforming
5446 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
5447 if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT &&
Duncan Sands92c43912008-06-06 12:08:01 +00005448 N0.getValueType().isInteger() &&
5449 N2.getValueType().isInteger() &&
Dan Gohman9d24dc72008-03-13 22:13:53 +00005450 (N1C->isNullValue() || // (a < 0) ? b : 0
5451 (N1C->getAPIntValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0
Duncan Sands92c43912008-06-06 12:08:01 +00005452 MVT XType = N0.getValueType();
5453 MVT AType = N2.getValueType();
Duncan Sandsec142ee2008-06-08 20:54:56 +00005454 if (XType.bitsGE(AType)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005455 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
5456 // single-bit constant.
Dan Gohman9d24dc72008-03-13 22:13:53 +00005457 if (N2C && ((N2C->getAPIntValue() & (N2C->getAPIntValue()-1)) == 0)) {
5458 unsigned ShCtV = N2C->getAPIntValue().logBase2();
Duncan Sands92c43912008-06-06 12:08:01 +00005459 ShCtV = XType.getSizeInBits()-ShCtV-1;
Dan Gohman8181bd12008-07-27 21:46:04 +00005460 SDValue ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
5461 SDValue Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
Gabor Greif1c80d112008-08-28 21:40:38 +00005462 AddToWorkList(Shift.getNode());
Duncan Sandsec142ee2008-06-08 20:54:56 +00005463 if (XType.bitsGT(AType)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005464 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Gabor Greif1c80d112008-08-28 21:40:38 +00005465 AddToWorkList(Shift.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005466 }
5467 return DAG.getNode(ISD::AND, AType, Shift, N2);
5468 }
Dan Gohman8181bd12008-07-27 21:46:04 +00005469 SDValue Shift = DAG.getNode(ISD::SRA, XType, N0,
Duncan Sands92c43912008-06-06 12:08:01 +00005470 DAG.getConstant(XType.getSizeInBits()-1,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005471 TLI.getShiftAmountTy()));
Gabor Greif1c80d112008-08-28 21:40:38 +00005472 AddToWorkList(Shift.getNode());
Duncan Sandsec142ee2008-06-08 20:54:56 +00005473 if (XType.bitsGT(AType)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005474 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Gabor Greif1c80d112008-08-28 21:40:38 +00005475 AddToWorkList(Shift.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005476 }
5477 return DAG.getNode(ISD::AND, AType, Shift, N2);
5478 }
5479 }
5480
5481 // fold select C, 16, 0 -> shl C, 4
Dan Gohman9d24dc72008-03-13 22:13:53 +00005482 if (N2C && N3C && N3C->isNullValue() && N2C->getAPIntValue().isPowerOf2() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005483 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) {
5484
5485 // If the caller doesn't want us to simplify this into a zext of a compare,
5486 // don't do it.
Dan Gohman9d24dc72008-03-13 22:13:53 +00005487 if (NotExtCompare && N2C->getAPIntValue() == 1)
Dan Gohman8181bd12008-07-27 21:46:04 +00005488 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005489
5490 // Get a SetCC of the condition
5491 // FIXME: Should probably make sure that setcc is legal if we ever have a
5492 // target where it isn't.
Dan Gohman8181bd12008-07-27 21:46:04 +00005493 SDValue Temp, SCC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005494 // cast from setcc result type to select result type
5495 if (AfterLegalize) {
Scott Michel502151f2008-03-10 15:42:14 +00005496 SCC = DAG.getSetCC(TLI.getSetCCResultType(N0), N0, N1, CC);
Duncan Sandsec142ee2008-06-08 20:54:56 +00005497 if (N2.getValueType().bitsLT(SCC.getValueType()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005498 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
5499 else
5500 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
5501 } else {
5502 SCC = DAG.getSetCC(MVT::i1, N0, N1, CC);
5503 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
5504 }
Gabor Greif1c80d112008-08-28 21:40:38 +00005505 AddToWorkList(SCC.getNode());
5506 AddToWorkList(Temp.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005507
Dan Gohman9d24dc72008-03-13 22:13:53 +00005508 if (N2C->getAPIntValue() == 1)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005509 return Temp;
5510 // shl setcc result by log2 n2c
5511 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
Dan Gohman9d24dc72008-03-13 22:13:53 +00005512 DAG.getConstant(N2C->getAPIntValue().logBase2(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005513 TLI.getShiftAmountTy()));
5514 }
5515
5516 // Check to see if this is the equivalent of setcc
5517 // FIXME: Turn all of these into setcc if setcc if setcc is legal
5518 // otherwise, go ahead with the folds.
Dan Gohman9d24dc72008-03-13 22:13:53 +00005519 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getAPIntValue() == 1ULL)) {
Duncan Sands92c43912008-06-06 12:08:01 +00005520 MVT XType = N0.getValueType();
Duncan Sands6ae1a0632008-06-14 17:48:34 +00005521 if (!AfterLegalize ||
5522 TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultType(N0))) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005523 SDValue Res = DAG.getSetCC(TLI.getSetCCResultType(N0), N0, N1, CC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005524 if (Res.getValueType() != VT)
5525 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
5526 return Res;
5527 }
5528
5529 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
5530 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
Duncan Sands6ae1a0632008-06-14 17:48:34 +00005531 (!AfterLegalize ||
5532 TLI.isOperationLegal(ISD::CTLZ, XType))) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005533 SDValue Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005534 return DAG.getNode(ISD::SRL, XType, Ctlz,
Duncan Sands92c43912008-06-06 12:08:01 +00005535 DAG.getConstant(Log2_32(XType.getSizeInBits()),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005536 TLI.getShiftAmountTy()));
5537 }
5538 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
5539 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005540 SDValue NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005541 N0);
Dan Gohman8181bd12008-07-27 21:46:04 +00005542 SDValue NotN0 = DAG.getNode(ISD::XOR, XType, N0,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005543 DAG.getConstant(~0ULL, XType));
5544 return DAG.getNode(ISD::SRL, XType,
5545 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
Duncan Sands92c43912008-06-06 12:08:01 +00005546 DAG.getConstant(XType.getSizeInBits()-1,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005547 TLI.getShiftAmountTy()));
5548 }
5549 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
5550 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005551 SDValue Sign = DAG.getNode(ISD::SRL, XType, N0,
Duncan Sands92c43912008-06-06 12:08:01 +00005552 DAG.getConstant(XType.getSizeInBits()-1,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005553 TLI.getShiftAmountTy()));
5554 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
5555 }
5556 }
5557
5558 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
5559 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
5560 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
5561 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1) &&
Duncan Sands92c43912008-06-06 12:08:01 +00005562 N2.getOperand(0) == N1 && N0.getValueType().isInteger()) {
5563 MVT XType = N0.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +00005564 SDValue Shift = DAG.getNode(ISD::SRA, XType, N0,
Duncan Sands92c43912008-06-06 12:08:01 +00005565 DAG.getConstant(XType.getSizeInBits()-1,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005566 TLI.getShiftAmountTy()));
Dan Gohman8181bd12008-07-27 21:46:04 +00005567 SDValue Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
Gabor Greif1c80d112008-08-28 21:40:38 +00005568 AddToWorkList(Shift.getNode());
5569 AddToWorkList(Add.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005570 return DAG.getNode(ISD::XOR, XType, Add, Shift);
5571 }
5572 // Check to see if this is an integer abs. select_cc setgt X, -1, X, -X ->
5573 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
5574 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT &&
5575 N0 == N2 && N3.getOpcode() == ISD::SUB && N0 == N3.getOperand(1)) {
5576 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N3.getOperand(0))) {
Duncan Sands92c43912008-06-06 12:08:01 +00005577 MVT XType = N0.getValueType();
5578 if (SubC->isNullValue() && XType.isInteger()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005579 SDValue Shift = DAG.getNode(ISD::SRA, XType, N0,
Duncan Sands92c43912008-06-06 12:08:01 +00005580 DAG.getConstant(XType.getSizeInBits()-1,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005581 TLI.getShiftAmountTy()));
Dan Gohman8181bd12008-07-27 21:46:04 +00005582 SDValue Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
Gabor Greif1c80d112008-08-28 21:40:38 +00005583 AddToWorkList(Shift.getNode());
5584 AddToWorkList(Add.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005585 return DAG.getNode(ISD::XOR, XType, Add, Shift);
5586 }
5587 }
5588 }
5589
Dan Gohman8181bd12008-07-27 21:46:04 +00005590 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005591}
5592
5593/// SimplifySetCC - This is a stub for TargetLowering::SimplifySetCC.
Dan Gohman8181bd12008-07-27 21:46:04 +00005594SDValue DAGCombiner::SimplifySetCC(MVT VT, SDValue N0,
5595 SDValue N1, ISD::CondCode Cond,
5596 bool foldBooleans) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005597 TargetLowering::DAGCombinerInfo
5598 DagCombineInfo(DAG, !AfterLegalize, false, this);
5599 return TLI.SimplifySetCC(VT, N0, N1, Cond, foldBooleans, DagCombineInfo);
5600}
5601
5602/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
5603/// return a DAG expression to select that will generate the same value by
5604/// multiplying by a magic number. See:
5605/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Dan Gohman8181bd12008-07-27 21:46:04 +00005606SDValue DAGCombiner::BuildSDIV(SDNode *N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005607 std::vector<SDNode*> Built;
Dan Gohman8181bd12008-07-27 21:46:04 +00005608 SDValue S = TLI.BuildSDIV(N, DAG, &Built);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005609
5610 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
5611 ii != ee; ++ii)
5612 AddToWorkList(*ii);
5613 return S;
5614}
5615
5616/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
5617/// return a DAG expression to select that will generate the same value by
5618/// multiplying by a magic number. See:
5619/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Dan Gohman8181bd12008-07-27 21:46:04 +00005620SDValue DAGCombiner::BuildUDIV(SDNode *N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005621 std::vector<SDNode*> Built;
Dan Gohman8181bd12008-07-27 21:46:04 +00005622 SDValue S = TLI.BuildUDIV(N, DAG, &Built);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005623
5624 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
5625 ii != ee; ++ii)
5626 AddToWorkList(*ii);
5627 return S;
5628}
5629
5630/// FindBaseOffset - Return true if base is known not to alias with anything
5631/// but itself. Provides base object and offset as results.
Dan Gohman8181bd12008-07-27 21:46:04 +00005632static bool FindBaseOffset(SDValue Ptr, SDValue &Base, int64_t &Offset) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005633 // Assume it is a primitive operation.
5634 Base = Ptr; Offset = 0;
5635
5636 // If it's an adding a simple constant then integrate the offset.
5637 if (Base.getOpcode() == ISD::ADD) {
5638 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Base.getOperand(1))) {
5639 Base = Base.getOperand(0);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005640 Offset += C->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005641 }
5642 }
5643
5644 // If it's any of the following then it can't alias with anything but itself.
5645 return isa<FrameIndexSDNode>(Base) ||
5646 isa<ConstantPoolSDNode>(Base) ||
5647 isa<GlobalAddressSDNode>(Base);
5648}
5649
5650/// isAlias - Return true if there is any possibility that the two addresses
5651/// overlap.
Dan Gohman8181bd12008-07-27 21:46:04 +00005652bool DAGCombiner::isAlias(SDValue Ptr1, int64_t Size1,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005653 const Value *SrcValue1, int SrcValueOffset1,
Dan Gohman8181bd12008-07-27 21:46:04 +00005654 SDValue Ptr2, int64_t Size2,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005655 const Value *SrcValue2, int SrcValueOffset2)
5656{
5657 // If they are the same then they must be aliases.
5658 if (Ptr1 == Ptr2) return true;
5659
5660 // Gather base node and offset information.
Dan Gohman8181bd12008-07-27 21:46:04 +00005661 SDValue Base1, Base2;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005662 int64_t Offset1, Offset2;
5663 bool KnownBase1 = FindBaseOffset(Ptr1, Base1, Offset1);
5664 bool KnownBase2 = FindBaseOffset(Ptr2, Base2, Offset2);
5665
5666 // If they have a same base address then...
5667 if (Base1 == Base2) {
5668 // Check to see if the addresses overlap.
5669 return!((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
5670 }
5671
5672 // If we know both bases then they can't alias.
5673 if (KnownBase1 && KnownBase2) return false;
5674
5675 if (CombinerGlobalAA) {
5676 // Use alias analysis information.
Dan Gohmane142c2e2007-08-27 16:32:11 +00005677 int64_t MinOffset = std::min(SrcValueOffset1, SrcValueOffset2);
5678 int64_t Overlap1 = Size1 + SrcValueOffset1 - MinOffset;
5679 int64_t Overlap2 = Size2 + SrcValueOffset2 - MinOffset;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005680 AliasAnalysis::AliasResult AAResult =
5681 AA.alias(SrcValue1, Overlap1, SrcValue2, Overlap2);
5682 if (AAResult == AliasAnalysis::NoAlias)
5683 return false;
5684 }
5685
5686 // Otherwise we have to assume they alias.
5687 return true;
5688}
5689
5690/// FindAliasInfo - Extracts the relevant alias information from the memory
5691/// node. Returns true if the operand was a load.
5692bool DAGCombiner::FindAliasInfo(SDNode *N,
Dan Gohman8181bd12008-07-27 21:46:04 +00005693 SDValue &Ptr, int64_t &Size,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005694 const Value *&SrcValue, int &SrcValueOffset) {
5695 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
5696 Ptr = LD->getBasePtr();
Duncan Sands92c43912008-06-06 12:08:01 +00005697 Size = LD->getMemoryVT().getSizeInBits() >> 3;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005698 SrcValue = LD->getSrcValue();
5699 SrcValueOffset = LD->getSrcValueOffset();
5700 return true;
5701 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
5702 Ptr = ST->getBasePtr();
Duncan Sands92c43912008-06-06 12:08:01 +00005703 Size = ST->getMemoryVT().getSizeInBits() >> 3;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005704 SrcValue = ST->getSrcValue();
5705 SrcValueOffset = ST->getSrcValueOffset();
5706 } else {
5707 assert(0 && "FindAliasInfo expected a memory operand");
5708 }
5709
5710 return false;
5711}
5712
5713/// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
5714/// looking for aliasing nodes and adding them to the Aliases vector.
Dan Gohman8181bd12008-07-27 21:46:04 +00005715void DAGCombiner::GatherAllAliases(SDNode *N, SDValue OriginalChain,
5716 SmallVector<SDValue, 8> &Aliases) {
5717 SmallVector<SDValue, 8> Chains; // List of chains to visit.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005718 std::set<SDNode *> Visited; // Visited node set.
5719
5720 // Get alias information for node.
Dan Gohman8181bd12008-07-27 21:46:04 +00005721 SDValue Ptr;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005722 int64_t Size;
5723 const Value *SrcValue;
5724 int SrcValueOffset;
5725 bool IsLoad = FindAliasInfo(N, Ptr, Size, SrcValue, SrcValueOffset);
5726
5727 // Starting off.
5728 Chains.push_back(OriginalChain);
5729
5730 // Look at each chain and determine if it is an alias. If so, add it to the
5731 // aliases list. If not, then continue up the chain looking for the next
5732 // candidate.
5733 while (!Chains.empty()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005734 SDValue Chain = Chains.back();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005735 Chains.pop_back();
5736
5737 // Don't bother if we've been before.
Gabor Greif1c80d112008-08-28 21:40:38 +00005738 if (Visited.find(Chain.getNode()) != Visited.end()) continue;
5739 Visited.insert(Chain.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005740
5741 switch (Chain.getOpcode()) {
5742 case ISD::EntryToken:
5743 // Entry token is ideal chain operand, but handled in FindBetterChain.
5744 break;
5745
5746 case ISD::LOAD:
5747 case ISD::STORE: {
5748 // Get alias information for Chain.
Dan Gohman8181bd12008-07-27 21:46:04 +00005749 SDValue OpPtr;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005750 int64_t OpSize;
5751 const Value *OpSrcValue;
5752 int OpSrcValueOffset;
Gabor Greif1c80d112008-08-28 21:40:38 +00005753 bool IsOpLoad = FindAliasInfo(Chain.getNode(), OpPtr, OpSize,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005754 OpSrcValue, OpSrcValueOffset);
5755
5756 // If chain is alias then stop here.
5757 if (!(IsLoad && IsOpLoad) &&
5758 isAlias(Ptr, Size, SrcValue, SrcValueOffset,
5759 OpPtr, OpSize, OpSrcValue, OpSrcValueOffset)) {
5760 Aliases.push_back(Chain);
5761 } else {
5762 // Look further up the chain.
5763 Chains.push_back(Chain.getOperand(0));
5764 // Clean up old chain.
Gabor Greif1c80d112008-08-28 21:40:38 +00005765 AddToWorkList(Chain.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005766 }
5767 break;
5768 }
5769
5770 case ISD::TokenFactor:
5771 // We have to check each of the operands of the token factor, so we queue
5772 // then up. Adding the operands to the queue (stack) in reverse order
5773 // maintains the original order and increases the likelihood that getNode
5774 // will find a matching token factor (CSE.)
5775 for (unsigned n = Chain.getNumOperands(); n;)
5776 Chains.push_back(Chain.getOperand(--n));
5777 // Eliminate the token factor if we can.
Gabor Greif1c80d112008-08-28 21:40:38 +00005778 AddToWorkList(Chain.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005779 break;
5780
5781 default:
5782 // For all other instructions we will just have to take what we can get.
5783 Aliases.push_back(Chain);
5784 break;
5785 }
5786 }
5787}
5788
5789/// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, looking
5790/// for a better chain (aliasing node.)
Dan Gohman8181bd12008-07-27 21:46:04 +00005791SDValue DAGCombiner::FindBetterChain(SDNode *N, SDValue OldChain) {
5792 SmallVector<SDValue, 8> Aliases; // Ops for replacing token factor.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005793
5794 // Accumulate all the aliases to this node.
5795 GatherAllAliases(N, OldChain, Aliases);
5796
5797 if (Aliases.size() == 0) {
5798 // If no operands then chain to entry token.
5799 return DAG.getEntryNode();
5800 } else if (Aliases.size() == 1) {
5801 // If a single operand then chain to it. We don't need to revisit it.
5802 return Aliases[0];
5803 }
5804
5805 // Construct a custom tailored token factor.
Dan Gohman8181bd12008-07-27 21:46:04 +00005806 SDValue NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005807 &Aliases[0], Aliases.size());
5808
5809 // Make sure the old chain gets cleaned up.
Gabor Greif1c80d112008-08-28 21:40:38 +00005810 if (NewChain != OldChain) AddToWorkList(OldChain.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005811
5812 return NewChain;
5813}
5814
5815// SelectionDAG::Combine - This is the entry point for the file.
5816//
Dan Gohmanea12c0c2008-08-20 16:30:28 +00005817void SelectionDAG::Combine(bool RunningAfterLegalize, AliasAnalysis &AA,
5818 bool Fast) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005819 /// run - This is the main entry point to this class.
5820 ///
Dan Gohmanea12c0c2008-08-20 16:30:28 +00005821 DAGCombiner(*this, AA, Fast).Run(RunningAfterLegalize);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005822}