blob: a41d2a1a5a6c8225f9f46e6d3a406161488b607c [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;
Dan Gohman96eb47a2009-01-15 19:20:50 +000052 const TargetLowering &TLI;
Duncan Sandsa3e2cd02008-11-24 14:53:14 +000053 CombineLevel Level;
54 bool LegalOperations;
55 bool LegalTypes;
Dan Gohmanea12c0c2008-08-20 16:30:28 +000056 bool Fast;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000057
58 // Worklist of all of the nodes that need to be simplified.
Evan Cheng56550ae2008-08-29 22:21:44 +000059 std::vector<SDNode*> WorkList;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000060
61 // AA - Used for DAG load/store alias analysis.
62 AliasAnalysis &AA;
63
64 /// AddUsersToWorkList - When an instruction is simplified, add all users of
65 /// the instruction to the work lists because they might get more simplified
66 /// now.
67 ///
68 void AddUsersToWorkList(SDNode *N) {
69 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
70 UI != UE; ++UI)
Dan Gohman0c97f1d2008-07-27 20:43:25 +000071 AddToWorkList(*UI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000072 }
73
Dan Gohman6c89ea72007-10-08 17:57:15 +000074 /// visit - call the node-specific routine that knows how to fold each
75 /// particular type of node.
Dan Gohman8181bd12008-07-27 21:46:04 +000076 SDValue visit(SDNode *N);
Dan Gohman6c89ea72007-10-08 17:57:15 +000077
Dan Gohmanf17a25c2007-07-18 16:29:46 +000078 public:
79 /// AddToWorkList - Add to the work list making sure it's instance is at the
80 /// the back (next to be processed.)
81 void AddToWorkList(SDNode *N) {
82 removeFromWorkList(N);
83 WorkList.push_back(N);
84 }
85
Chris Lattner7bcb18f2008-02-03 06:49:24 +000086 /// removeFromWorkList - remove all instances of N from the worklist.
87 ///
88 void removeFromWorkList(SDNode *N) {
89 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), N),
90 WorkList.end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +000091 }
92
Dan Gohman8181bd12008-07-27 21:46:04 +000093 SDValue CombineTo(SDNode *N, const SDValue *To, unsigned NumTo,
Chris Lattner7bcb18f2008-02-03 06:49:24 +000094 bool AddTo = true);
95
Dan Gohman8181bd12008-07-27 21:46:04 +000096 SDValue CombineTo(SDNode *N, SDValue Res, bool AddTo = true) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000097 return CombineTo(N, &Res, 1, AddTo);
98 }
99
Dan Gohman8181bd12008-07-27 21:46:04 +0000100 SDValue CombineTo(SDNode *N, SDValue Res0, SDValue Res1,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000101 bool AddTo = true) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000102 SDValue To[] = { Res0, Res1 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000103 return CombineTo(N, To, 2, AddTo);
104 }
Dan Gohman22cefb02009-01-29 01:59:02 +0000105
106 void CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO);
Chris Lattner5872a362008-01-17 07:00:52 +0000107
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000108 private:
109
110 /// SimplifyDemandedBits - Check the specified integer node value to see if
111 /// it can be simplified or if things it uses can be simplified by bit
112 /// propagation. If so, return true.
Dan Gohman8181bd12008-07-27 21:46:04 +0000113 bool SimplifyDemandedBits(SDValue Op) {
Dan Gohman11607792008-02-27 00:25:32 +0000114 APInt Demanded = APInt::getAllOnesValue(Op.getValueSizeInBits());
115 return SimplifyDemandedBits(Op, Demanded);
116 }
117
Dan Gohman8181bd12008-07-27 21:46:04 +0000118 bool SimplifyDemandedBits(SDValue Op, const APInt &Demanded);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000119
120 bool CombineToPreIndexedLoadStore(SDNode *N);
121 bool CombineToPostIndexedLoadStore(SDNode *N);
122
123
Dan Gohman6c89ea72007-10-08 17:57:15 +0000124 /// combine - call the node-specific routine that knows how to fold each
125 /// particular type of node. If that doesn't do anything, try the
126 /// target-specific DAG combines.
Dan Gohman8181bd12008-07-27 21:46:04 +0000127 SDValue combine(SDNode *N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000128
129 // Visitation implementation - Implement dag node combining for different
130 // node types. The semantics are as follows:
131 // Return Value:
Evan Cheng56550ae2008-08-29 22:21:44 +0000132 // SDValue.getNode() == 0 - No change was made
133 // SDValue.getNode() == N - N was replaced, is dead and has been handled.
134 // otherwise - N should be replaced by the returned Operand.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000135 //
Dan Gohman8181bd12008-07-27 21:46:04 +0000136 SDValue visitTokenFactor(SDNode *N);
137 SDValue visitMERGE_VALUES(SDNode *N);
138 SDValue visitADD(SDNode *N);
139 SDValue visitSUB(SDNode *N);
140 SDValue visitADDC(SDNode *N);
141 SDValue visitADDE(SDNode *N);
142 SDValue visitMUL(SDNode *N);
143 SDValue visitSDIV(SDNode *N);
144 SDValue visitUDIV(SDNode *N);
145 SDValue visitSREM(SDNode *N);
146 SDValue visitUREM(SDNode *N);
147 SDValue visitMULHU(SDNode *N);
148 SDValue visitMULHS(SDNode *N);
149 SDValue visitSMUL_LOHI(SDNode *N);
150 SDValue visitUMUL_LOHI(SDNode *N);
151 SDValue visitSDIVREM(SDNode *N);
152 SDValue visitUDIVREM(SDNode *N);
153 SDValue visitAND(SDNode *N);
154 SDValue visitOR(SDNode *N);
155 SDValue visitXOR(SDNode *N);
156 SDValue SimplifyVBinOp(SDNode *N);
157 SDValue visitSHL(SDNode *N);
158 SDValue visitSRA(SDNode *N);
159 SDValue visitSRL(SDNode *N);
160 SDValue visitCTLZ(SDNode *N);
161 SDValue visitCTTZ(SDNode *N);
162 SDValue visitCTPOP(SDNode *N);
163 SDValue visitSELECT(SDNode *N);
164 SDValue visitSELECT_CC(SDNode *N);
165 SDValue visitSETCC(SDNode *N);
166 SDValue visitSIGN_EXTEND(SDNode *N);
167 SDValue visitZERO_EXTEND(SDNode *N);
168 SDValue visitANY_EXTEND(SDNode *N);
169 SDValue visitSIGN_EXTEND_INREG(SDNode *N);
170 SDValue visitTRUNCATE(SDNode *N);
171 SDValue visitBIT_CONVERT(SDNode *N);
172 SDValue visitBUILD_PAIR(SDNode *N);
173 SDValue visitFADD(SDNode *N);
174 SDValue visitFSUB(SDNode *N);
175 SDValue visitFMUL(SDNode *N);
176 SDValue visitFDIV(SDNode *N);
177 SDValue visitFREM(SDNode *N);
178 SDValue visitFCOPYSIGN(SDNode *N);
179 SDValue visitSINT_TO_FP(SDNode *N);
180 SDValue visitUINT_TO_FP(SDNode *N);
181 SDValue visitFP_TO_SINT(SDNode *N);
182 SDValue visitFP_TO_UINT(SDNode *N);
183 SDValue visitFP_ROUND(SDNode *N);
184 SDValue visitFP_ROUND_INREG(SDNode *N);
185 SDValue visitFP_EXTEND(SDNode *N);
186 SDValue visitFNEG(SDNode *N);
187 SDValue visitFABS(SDNode *N);
188 SDValue visitBRCOND(SDNode *N);
189 SDValue visitBR_CC(SDNode *N);
190 SDValue visitLOAD(SDNode *N);
191 SDValue visitSTORE(SDNode *N);
192 SDValue visitINSERT_VECTOR_ELT(SDNode *N);
193 SDValue visitEXTRACT_VECTOR_ELT(SDNode *N);
194 SDValue visitBUILD_VECTOR(SDNode *N);
195 SDValue visitCONCAT_VECTORS(SDNode *N);
196 SDValue visitVECTOR_SHUFFLE(SDNode *N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000197
Dan Gohman8181bd12008-07-27 21:46:04 +0000198 SDValue XformToShuffleWithZero(SDNode *N);
Bill Wendlingabb33a22009-01-30 00:45:56 +0000199 SDValue ReassociateOps(unsigned Opc, DebugLoc DL, SDValue LHS, SDValue RHS);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000200
Dan Gohman8181bd12008-07-27 21:46:04 +0000201 SDValue visitShiftByConstant(SDNode *N, unsigned Amt);
Chris Lattner91ed3c32007-12-06 07:33:36 +0000202
Dan Gohman8181bd12008-07-27 21:46:04 +0000203 bool SimplifySelectOps(SDNode *SELECT, SDValue LHS, SDValue RHS);
204 SDValue SimplifyBinOpWithSameOpcodeHands(SDNode *N);
205 SDValue SimplifySelect(SDValue N0, SDValue N1, SDValue N2);
206 SDValue SimplifySelectCC(SDValue N0, SDValue N1, SDValue N2,
207 SDValue N3, ISD::CondCode CC,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000208 bool NotExtCompare = false);
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000209 SDValue SimplifySetCC(MVT VT, SDValue N0, SDValue N1, ISD::CondCode Cond,
210 bool foldBooleans = true);
Dan Gohman8181bd12008-07-27 21:46:04 +0000211 SDValue SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
Chris Lattner4a7c8452008-01-26 01:09:19 +0000212 unsigned HiOp);
Dan Gohman8181bd12008-07-27 21:46:04 +0000213 SDValue CombineConsecutiveLoads(SDNode *N, MVT VT);
214 SDValue ConstantFoldBIT_CONVERTofBUILD_VECTOR(SDNode *, MVT);
215 SDValue BuildSDIV(SDNode *N);
216 SDValue BuildUDIV(SDNode *N);
Bill Wendling2e1865c2009-01-30 21:14:50 +0000217 SDNode *MatchRotate(SDValue LHS, SDValue RHS, DebugLoc DL);
Dan Gohman8181bd12008-07-27 21:46:04 +0000218 SDValue ReduceLoadWidth(SDNode *N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000219
Dan Gohman8181bd12008-07-27 21:46:04 +0000220 SDValue GetDemandedBits(SDValue V, const APInt &Mask);
Chris Lattnere8671c52007-10-13 06:35:54 +0000221
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000222 /// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
223 /// looking for aliasing nodes and adding them to the Aliases vector.
Dan Gohman8181bd12008-07-27 21:46:04 +0000224 void GatherAllAliases(SDNode *N, SDValue OriginalChain,
225 SmallVector<SDValue, 8> &Aliases);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000226
227 /// isAlias - Return true if there is any possibility that the two addresses
228 /// overlap.
Dan Gohman8181bd12008-07-27 21:46:04 +0000229 bool isAlias(SDValue Ptr1, int64_t Size1,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000230 const Value *SrcValue1, int SrcValueOffset1,
Dan Gohman8181bd12008-07-27 21:46:04 +0000231 SDValue Ptr2, int64_t Size2,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000232 const Value *SrcValue2, int SrcValueOffset2);
233
234 /// FindAliasInfo - Extracts the relevant alias information from the memory
235 /// node. Returns true if the operand was a load.
236 bool FindAliasInfo(SDNode *N,
Dan Gohman8181bd12008-07-27 21:46:04 +0000237 SDValue &Ptr, int64_t &Size,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000238 const Value *&SrcValue, int &SrcValueOffset);
239
240 /// FindBetterChain - Walk up chain skipping non-aliasing memory nodes,
241 /// looking for a better chain (aliasing node.)
Dan Gohman8181bd12008-07-27 21:46:04 +0000242 SDValue FindBetterChain(SDNode *N, SDValue Chain);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000243
244public:
Dan Gohmanea12c0c2008-08-20 16:30:28 +0000245 DAGCombiner(SelectionDAG &D, AliasAnalysis &A, bool fast)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000246 : DAG(D),
247 TLI(D.getTargetLoweringInfo()),
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000248 Level(Unrestricted),
249 LegalOperations(false),
250 LegalTypes(false),
Dan Gohmanea12c0c2008-08-20 16:30:28 +0000251 Fast(fast),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000252 AA(A) {}
253
254 /// Run - runs the dag combiner on all nodes in the work list
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000255 void Run(CombineLevel AtLevel);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000256 };
257}
258
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000259
260namespace {
261/// WorkListRemover - This class is a DAGUpdateListener that removes any deleted
262/// nodes from the worklist.
263class VISIBILITY_HIDDEN WorkListRemover :
264 public SelectionDAG::DAGUpdateListener {
265 DAGCombiner &DC;
266public:
Dan Gohmana789bff2008-02-20 16:44:09 +0000267 explicit WorkListRemover(DAGCombiner &dc) : DC(dc) {}
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000268
Duncan Sands3866b1c2008-06-11 11:42:12 +0000269 virtual void NodeDeleted(SDNode *N, SDNode *E) {
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000270 DC.removeFromWorkList(N);
271 }
272
273 virtual void NodeUpdated(SDNode *N) {
274 // Ignore updates.
275 }
276};
277}
278
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000279//===----------------------------------------------------------------------===//
280// TargetLowering::DAGCombinerInfo implementation
281//===----------------------------------------------------------------------===//
282
283void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) {
284 ((DAGCombiner*)DC)->AddToWorkList(N);
285}
286
Dan Gohman8181bd12008-07-27 21:46:04 +0000287SDValue TargetLowering::DAGCombinerInfo::
288CombineTo(SDNode *N, const std::vector<SDValue> &To) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000289 return ((DAGCombiner*)DC)->CombineTo(N, &To[0], To.size());
290}
291
Dan Gohman8181bd12008-07-27 21:46:04 +0000292SDValue TargetLowering::DAGCombinerInfo::
293CombineTo(SDNode *N, SDValue Res) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000294 return ((DAGCombiner*)DC)->CombineTo(N, Res);
295}
296
297
Dan Gohman8181bd12008-07-27 21:46:04 +0000298SDValue TargetLowering::DAGCombinerInfo::
299CombineTo(SDNode *N, SDValue Res0, SDValue Res1) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000300 return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1);
301}
302
Dan Gohman22cefb02009-01-29 01:59:02 +0000303void TargetLowering::DAGCombinerInfo::
304CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO) {
305 return ((DAGCombiner*)DC)->CommitTargetLoweringOpt(TLO);
306}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000307
308//===----------------------------------------------------------------------===//
309// Helper Functions
310//===----------------------------------------------------------------------===//
311
312/// isNegatibleForFree - Return 1 if we can compute the negated form of the
313/// specified expression for the same cost as the expression itself, or 2 if we
314/// can compute the negated form more cheaply than the expression itself.
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000315static char isNegatibleForFree(SDValue Op, bool LegalOperations,
Chris Lattnere0992b82008-02-26 07:04:54 +0000316 unsigned Depth = 0) {
Dale Johannesenb89072e2007-10-16 23:38:29 +0000317 // No compile time optimizations on this type.
318 if (Op.getValueType() == MVT::ppcf128)
319 return 0;
320
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000321 // fneg is removable even if it has multiple uses.
322 if (Op.getOpcode() == ISD::FNEG) return 2;
323
324 // Don't allow anything with multiple uses.
325 if (!Op.hasOneUse()) return 0;
326
327 // Don't recurse exponentially.
328 if (Depth > 6) return 0;
329
330 switch (Op.getOpcode()) {
331 default: return false;
332 case ISD::ConstantFP:
Chris Lattnere0992b82008-02-26 07:04:54 +0000333 // Don't invert constant FP values after legalize. The negated constant
334 // isn't necessarily legal.
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000335 return LegalOperations ? 0 : 1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000336 case ISD::FADD:
337 // FIXME: determine better conditions for this xform.
338 if (!UnsafeFPMath) return 0;
339
340 // -(A+B) -> -A - B
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000341 if (char V = isNegatibleForFree(Op.getOperand(0), LegalOperations, Depth+1))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000342 return V;
343 // -(A+B) -> -B - A
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000344 return isNegatibleForFree(Op.getOperand(1), LegalOperations, Depth+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000345 case ISD::FSUB:
346 // We can't turn -(A-B) into B-A when we honor signed zeros.
347 if (!UnsafeFPMath) return 0;
348
349 // -(A-B) -> B-A
350 return 1;
351
352 case ISD::FMUL:
353 case ISD::FDIV:
354 if (HonorSignDependentRoundingFPMath()) return 0;
355
356 // -(X*Y) -> (-X * Y) or (X*-Y)
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000357 if (char V = isNegatibleForFree(Op.getOperand(0), LegalOperations, Depth+1))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000358 return V;
359
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000360 return isNegatibleForFree(Op.getOperand(1), LegalOperations, Depth+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000361
362 case ISD::FP_EXTEND:
363 case ISD::FP_ROUND:
364 case ISD::FSIN:
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000365 return isNegatibleForFree(Op.getOperand(0), LegalOperations, Depth+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000366 }
367}
368
369/// GetNegatedExpression - If isNegatibleForFree returns true, this function
370/// returns the newly negated expression.
Dan Gohman8181bd12008-07-27 21:46:04 +0000371static SDValue GetNegatedExpression(SDValue Op, SelectionDAG &DAG,
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000372 bool LegalOperations, unsigned Depth = 0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000373 // fneg is removable even if it has multiple uses.
374 if (Op.getOpcode() == ISD::FNEG) return Op.getOperand(0);
375
376 // Don't allow anything with multiple uses.
377 assert(Op.hasOneUse() && "Unknown reuse!");
378
379 assert(Depth <= 6 && "GetNegatedExpression doesn't match isNegatibleForFree");
380 switch (Op.getOpcode()) {
381 default: assert(0 && "Unknown code");
Dale Johannesen7604c1b2007-08-31 23:34:27 +0000382 case ISD::ConstantFP: {
383 APFloat V = cast<ConstantFPSDNode>(Op)->getValueAPF();
384 V.changeSign();
385 return DAG.getConstantFP(V, Op.getValueType());
386 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000387 case ISD::FADD:
388 // FIXME: determine better conditions for this xform.
389 assert(UnsafeFPMath);
390
391 // -(A+B) -> -A - B
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000392 if (isNegatibleForFree(Op.getOperand(0), LegalOperations, Depth+1))
Bill Wendlingabb33a22009-01-30 00:45:56 +0000393 return DAG.getNode(ISD::FSUB, Op.getDebugLoc(), Op.getValueType(),
Chris Lattnere0992b82008-02-26 07:04:54 +0000394 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000395 LegalOperations, Depth+1),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000396 Op.getOperand(1));
397 // -(A+B) -> -B - A
Bill Wendlingabb33a22009-01-30 00:45:56 +0000398 return DAG.getNode(ISD::FSUB, Op.getDebugLoc(), Op.getValueType(),
Chris Lattnere0992b82008-02-26 07:04:54 +0000399 GetNegatedExpression(Op.getOperand(1), DAG,
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000400 LegalOperations, Depth+1),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000401 Op.getOperand(0));
402 case ISD::FSUB:
403 // We can't turn -(A-B) into B-A when we honor signed zeros.
404 assert(UnsafeFPMath);
405
406 // -(0-B) -> B
407 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(Op.getOperand(0)))
Dale Johannesen7604c1b2007-08-31 23:34:27 +0000408 if (N0CFP->getValueAPF().isZero())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000409 return Op.getOperand(1);
410
411 // -(A-B) -> B-A
Bill Wendlingabb33a22009-01-30 00:45:56 +0000412 return DAG.getNode(ISD::FSUB, Op.getDebugLoc(), Op.getValueType(),
413 Op.getOperand(1), Op.getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000414
415 case ISD::FMUL:
416 case ISD::FDIV:
417 assert(!HonorSignDependentRoundingFPMath());
418
419 // -(X*Y) -> -X * Y
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000420 if (isNegatibleForFree(Op.getOperand(0), LegalOperations, Depth+1))
Bill Wendlingabb33a22009-01-30 00:45:56 +0000421 return DAG.getNode(Op.getOpcode(), Op.getDebugLoc(), Op.getValueType(),
Chris Lattnere0992b82008-02-26 07:04:54 +0000422 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000423 LegalOperations, Depth+1),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000424 Op.getOperand(1));
425
426 // -(X*Y) -> X * -Y
Bill Wendlingabb33a22009-01-30 00:45:56 +0000427 return DAG.getNode(Op.getOpcode(), Op.getDebugLoc(), Op.getValueType(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000428 Op.getOperand(0),
Chris Lattnere0992b82008-02-26 07:04:54 +0000429 GetNegatedExpression(Op.getOperand(1), DAG,
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000430 LegalOperations, Depth+1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000431
432 case ISD::FP_EXTEND:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000433 case ISD::FSIN:
Bill Wendlingabb33a22009-01-30 00:45:56 +0000434 return DAG.getNode(Op.getOpcode(), Op.getDebugLoc(), Op.getValueType(),
Chris Lattnere0992b82008-02-26 07:04:54 +0000435 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000436 LegalOperations, Depth+1));
Chris Lattner5872a362008-01-17 07:00:52 +0000437 case ISD::FP_ROUND:
Bill Wendlingabb33a22009-01-30 00:45:56 +0000438 return DAG.getNode(ISD::FP_ROUND, Op.getDebugLoc(), Op.getValueType(),
Chris Lattnere0992b82008-02-26 07:04:54 +0000439 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000440 LegalOperations, Depth+1),
Chris Lattner5872a362008-01-17 07:00:52 +0000441 Op.getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000442 }
443}
444
445
446// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
447// that selects between the values 1 and 0, making it equivalent to a setcc.
448// Also, set the incoming LHS, RHS, and CC references to the appropriate
449// nodes based on the type of node we are checking. This simplifies life a
450// bit for the callers.
Dan Gohman8181bd12008-07-27 21:46:04 +0000451static bool isSetCCEquivalent(SDValue N, SDValue &LHS, SDValue &RHS,
452 SDValue &CC) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000453 if (N.getOpcode() == ISD::SETCC) {
454 LHS = N.getOperand(0);
455 RHS = N.getOperand(1);
456 CC = N.getOperand(2);
457 return true;
458 }
459 if (N.getOpcode() == ISD::SELECT_CC &&
460 N.getOperand(2).getOpcode() == ISD::Constant &&
461 N.getOperand(3).getOpcode() == ISD::Constant &&
Dan Gohman9d24dc72008-03-13 22:13:53 +0000462 cast<ConstantSDNode>(N.getOperand(2))->getAPIntValue() == 1 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000463 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
464 LHS = N.getOperand(0);
465 RHS = N.getOperand(1);
466 CC = N.getOperand(4);
467 return true;
468 }
469 return false;
470}
471
472// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
473// one use. If this is true, it allows the users to invert the operation for
474// free when it is profitable to do so.
Dan Gohman8181bd12008-07-27 21:46:04 +0000475static bool isOneUseSetCC(SDValue N) {
476 SDValue N0, N1, N2;
Gabor Greif1c80d112008-08-28 21:40:38 +0000477 if (isSetCCEquivalent(N, N0, N1, N2) && N.getNode()->hasOneUse())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000478 return true;
479 return false;
480}
481
Bill Wendlingabb33a22009-01-30 00:45:56 +0000482SDValue DAGCombiner::ReassociateOps(unsigned Opc, DebugLoc DL,
483 SDValue N0, SDValue N1) {
Duncan Sands92c43912008-06-06 12:08:01 +0000484 MVT VT = N0.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000485 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
486 if (isa<ConstantSDNode>(N1)) {
Bill Wendlingabb33a22009-01-30 00:45:56 +0000487 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
Bill Wendlingf8bc7842009-01-30 20:50:00 +0000488 SDValue OpNode =
489 DAG.FoldConstantArithmetic(Opc, VT,
490 cast<ConstantSDNode>(N0.getOperand(1)),
491 cast<ConstantSDNode>(N1));
Bill Wendlinge165f5a2009-01-30 02:23:43 +0000492 return DAG.getNode(Opc, DL, VT, N0.getOperand(0), OpNode);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000493 } else if (N0.hasOneUse()) {
Bill Wendlingabb33a22009-01-30 00:45:56 +0000494 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
495 SDValue OpNode = DAG.getNode(Opc, N0.getDebugLoc(), VT,
496 N0.getOperand(0), N1);
Gabor Greif1c80d112008-08-28 21:40:38 +0000497 AddToWorkList(OpNode.getNode());
Bill Wendlingabb33a22009-01-30 00:45:56 +0000498 return DAG.getNode(Opc, DL, VT, OpNode, N0.getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000499 }
500 }
Bill Wendlingabb33a22009-01-30 00:45:56 +0000501
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000502 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
503 if (isa<ConstantSDNode>(N0)) {
Bill Wendlingabb33a22009-01-30 00:45:56 +0000504 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
Bill Wendlingf8bc7842009-01-30 20:50:00 +0000505 SDValue OpNode =
506 DAG.FoldConstantArithmetic(Opc, VT,
507 cast<ConstantSDNode>(N1.getOperand(1)),
508 cast<ConstantSDNode>(N0));
Bill Wendlinge165f5a2009-01-30 02:23:43 +0000509 return DAG.getNode(Opc, DL, VT, N1.getOperand(0), OpNode);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000510 } else if (N1.hasOneUse()) {
Bill Wendlingabb33a22009-01-30 00:45:56 +0000511 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
Bill Wendlinge165f5a2009-01-30 02:23:43 +0000512 SDValue OpNode = DAG.getNode(Opc, N0.getDebugLoc(), VT,
Bill Wendlingabb33a22009-01-30 00:45:56 +0000513 N1.getOperand(0), N0);
Gabor Greif1c80d112008-08-28 21:40:38 +0000514 AddToWorkList(OpNode.getNode());
Bill Wendlingabb33a22009-01-30 00:45:56 +0000515 return DAG.getNode(Opc, DL, VT, OpNode, N1.getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000516 }
517 }
Bill Wendlingabb33a22009-01-30 00:45:56 +0000518
Dan Gohman8181bd12008-07-27 21:46:04 +0000519 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000520}
521
Dan Gohman8181bd12008-07-27 21:46:04 +0000522SDValue DAGCombiner::CombineTo(SDNode *N, const SDValue *To, unsigned NumTo,
523 bool AddTo) {
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000524 assert(N->getNumValues() == NumTo && "Broken CombineTo call!");
525 ++NodesCombined;
526 DOUT << "\nReplacing.1 "; DEBUG(N->dump(&DAG));
Gabor Greif1c80d112008-08-28 21:40:38 +0000527 DOUT << "\nWith: "; DEBUG(To[0].getNode()->dump(&DAG));
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000528 DOUT << " and " << NumTo-1 << " other values\n";
Dan Gohman05452202009-01-21 15:17:51 +0000529 DEBUG(for (unsigned i = 0, e = NumTo; i != e; ++i)
530 assert(N->getValueType(i) == To[i].getValueType() &&
531 "Cannot combine value to value of different type!"));
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000532 WorkListRemover DeadNodes(*this);
533 DAG.ReplaceAllUsesWith(N, To, &DeadNodes);
534
535 if (AddTo) {
536 // Push the new nodes and any users onto the worklist
537 for (unsigned i = 0, e = NumTo; i != e; ++i) {
Gabor Greif1c80d112008-08-28 21:40:38 +0000538 AddToWorkList(To[i].getNode());
539 AddUsersToWorkList(To[i].getNode());
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000540 }
541 }
542
Dan Gohman86bf1392009-01-19 21:44:21 +0000543 // Finally, if the node is now dead, remove it from the graph. The node
544 // may not be dead if the replacement process recursively simplified to
545 // something else needing this node.
546 if (N->use_empty()) {
547 // Nodes can be reintroduced into the worklist. Make sure we do not
548 // process a node that has been replaced.
549 removeFromWorkList(N);
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000550
Dan Gohman86bf1392009-01-19 21:44:21 +0000551 // Finally, since the node is now dead, remove it from the graph.
552 DAG.DeleteNode(N);
553 }
Dan Gohman8181bd12008-07-27 21:46:04 +0000554 return SDValue(N, 0);
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000555}
556
Dan Gohman22cefb02009-01-29 01:59:02 +0000557void
558DAGCombiner::CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &
559 TLO) {
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000560 // Replace all uses. If any nodes become isomorphic to other nodes and
561 // are deleted, make sure to remove them from our worklist.
562 WorkListRemover DeadNodes(*this);
563 DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New, &DeadNodes);
Dan Gohman22cefb02009-01-29 01:59:02 +0000564
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000565 // Push the new node and any (possibly new) users onto the worklist.
Gabor Greif1c80d112008-08-28 21:40:38 +0000566 AddToWorkList(TLO.New.getNode());
567 AddUsersToWorkList(TLO.New.getNode());
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000568
569 // Finally, if the node is now dead, remove it from the graph. The node
570 // may not be dead if the replacement process recursively simplified to
571 // something else needing this node.
Gabor Greif1c80d112008-08-28 21:40:38 +0000572 if (TLO.Old.getNode()->use_empty()) {
573 removeFromWorkList(TLO.Old.getNode());
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000574
575 // If the operands of this node are only used by the node, they will now
576 // be dead. Make sure to visit them first to delete dead nodes early.
Gabor Greif1c80d112008-08-28 21:40:38 +0000577 for (unsigned i = 0, e = TLO.Old.getNode()->getNumOperands(); i != e; ++i)
578 if (TLO.Old.getNode()->getOperand(i).getNode()->hasOneUse())
579 AddToWorkList(TLO.Old.getNode()->getOperand(i).getNode());
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000580
Gabor Greif1c80d112008-08-28 21:40:38 +0000581 DAG.DeleteNode(TLO.Old.getNode());
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000582 }
Dan Gohman22cefb02009-01-29 01:59:02 +0000583}
584
585/// SimplifyDemandedBits - Check the specified integer node value to see if
586/// it can be simplified or if things it uses can be simplified by bit
587/// propagation. If so, return true.
588bool DAGCombiner::SimplifyDemandedBits(SDValue Op, const APInt &Demanded) {
589 TargetLowering::TargetLoweringOpt TLO(DAG);
590 APInt KnownZero, KnownOne;
591 if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO))
592 return false;
593
594 // Revisit the node.
595 AddToWorkList(Op.getNode());
596
597 // Replace the old value with the new one.
598 ++NodesCombined;
599 DOUT << "\nReplacing.2 "; DEBUG(TLO.Old.getNode()->dump(&DAG));
600 DOUT << "\nWith: "; DEBUG(TLO.New.getNode()->dump(&DAG));
601 DOUT << '\n';
602
603 CommitTargetLoweringOpt(TLO);
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000604 return true;
605}
606
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000607//===----------------------------------------------------------------------===//
608// Main DAG Combiner implementation
609//===----------------------------------------------------------------------===//
610
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000611void DAGCombiner::Run(CombineLevel AtLevel) {
612 // set the instance variables, so that the various visit routines may use it.
613 Level = AtLevel;
614 LegalOperations = Level >= NoIllegalOperations;
615 LegalTypes = Level >= NoIllegalTypes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000616
Evan Cheng56550ae2008-08-29 22:21:44 +0000617 // Add all the dag nodes to the worklist.
618 WorkList.reserve(DAG.allnodes_size());
619 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
620 E = DAG.allnodes_end(); I != E; ++I)
621 WorkList.push_back(I);
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000622
Evan Cheng56550ae2008-08-29 22:21:44 +0000623 // Create a dummy node (which is not added to allnodes), that adds a reference
624 // to the root node, preventing it from being deleted, and tracking any
625 // changes of the root.
626 HandleSDNode Dummy(DAG.getRoot());
627
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000628 // The root of the dag may dangle to deleted nodes until the dag combiner is
629 // done. Set it to null to avoid confusion.
Dan Gohman8181bd12008-07-27 21:46:04 +0000630 DAG.setRoot(SDValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000631
Evan Cheng56550ae2008-08-29 22:21:44 +0000632 // while the worklist isn't empty, inspect the node on the end of it and
633 // try and combine it.
634 while (!WorkList.empty()) {
635 SDNode *N = WorkList.back();
636 WorkList.pop_back();
637
638 // If N has no uses, it is dead. Make sure to revisit all N's operands once
639 // N is deleted from the DAG, since they too may now be dead or may have a
640 // reduced number of uses, allowing other xforms.
641 if (N->use_empty() && N != &Dummy) {
642 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
643 AddToWorkList(N->getOperand(i).getNode());
644
645 DAG.DeleteNode(N);
646 continue;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000647 }
Evan Cheng56550ae2008-08-29 22:21:44 +0000648
649 SDValue RV = combine(N);
650
651 if (RV.getNode() == 0)
652 continue;
653
654 ++NodesCombined;
655
656 // If we get back the same node we passed in, rather than a new node or
657 // zero, we know that the node must have defined multiple values and
658 // CombineTo was used. Since CombineTo takes care of the worklist
659 // mechanics for us, we have no work to do in this case.
660 if (RV.getNode() == N)
661 continue;
662
663 assert(N->getOpcode() != ISD::DELETED_NODE &&
664 RV.getNode()->getOpcode() != ISD::DELETED_NODE &&
665 "Node was deleted but visit returned new node!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000666
Evan Cheng56550ae2008-08-29 22:21:44 +0000667 DOUT << "\nReplacing.3 "; DEBUG(N->dump(&DAG));
668 DOUT << "\nWith: "; DEBUG(RV.getNode()->dump(&DAG));
669 DOUT << '\n';
670 WorkListRemover DeadNodes(*this);
671 if (N->getNumValues() == RV.getNode()->getNumValues())
672 DAG.ReplaceAllUsesWith(N, RV.getNode(), &DeadNodes);
673 else {
674 assert(N->getValueType(0) == RV.getValueType() &&
675 N->getNumValues() == 1 && "Type mismatch");
676 SDValue OpV = RV;
677 DAG.ReplaceAllUsesWith(N, &OpV, &DeadNodes);
678 }
679
680 // Push the new node and any users onto the worklist
681 AddToWorkList(RV.getNode());
682 AddUsersToWorkList(RV.getNode());
683
684 // Add any uses of the old node to the worklist in case this node is the
685 // last one that uses them. They may become dead after this node is
686 // deleted.
687 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
688 AddToWorkList(N->getOperand(i).getNode());
689
Dan Gohman86bf1392009-01-19 21:44:21 +0000690 // Finally, if the node is now dead, remove it from the graph. The node
691 // may not be dead if the replacement process recursively simplified to
692 // something else needing this node.
693 if (N->use_empty()) {
694 // Nodes can be reintroduced into the worklist. Make sure we do not
695 // process a node that has been replaced.
696 removeFromWorkList(N);
Evan Cheng56550ae2008-08-29 22:21:44 +0000697
Dan Gohman86bf1392009-01-19 21:44:21 +0000698 // Finally, since the node is now dead, remove it from the graph.
699 DAG.DeleteNode(N);
700 }
Evan Cheng56550ae2008-08-29 22:21:44 +0000701 }
702
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000703 // If the root changed (e.g. it was a dead load, update the root).
704 DAG.setRoot(Dummy.getValue());
705}
706
Dan Gohman8181bd12008-07-27 21:46:04 +0000707SDValue DAGCombiner::visit(SDNode *N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000708 switch(N->getOpcode()) {
709 default: break;
710 case ISD::TokenFactor: return visitTokenFactor(N);
Chris Lattnerf32fa7f2008-02-13 07:25:05 +0000711 case ISD::MERGE_VALUES: return visitMERGE_VALUES(N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000712 case ISD::ADD: return visitADD(N);
713 case ISD::SUB: return visitSUB(N);
714 case ISD::ADDC: return visitADDC(N);
715 case ISD::ADDE: return visitADDE(N);
716 case ISD::MUL: return visitMUL(N);
717 case ISD::SDIV: return visitSDIV(N);
718 case ISD::UDIV: return visitUDIV(N);
719 case ISD::SREM: return visitSREM(N);
720 case ISD::UREM: return visitUREM(N);
721 case ISD::MULHU: return visitMULHU(N);
722 case ISD::MULHS: return visitMULHS(N);
Dan Gohman6c89ea72007-10-08 17:57:15 +0000723 case ISD::SMUL_LOHI: return visitSMUL_LOHI(N);
724 case ISD::UMUL_LOHI: return visitUMUL_LOHI(N);
725 case ISD::SDIVREM: return visitSDIVREM(N);
726 case ISD::UDIVREM: return visitUDIVREM(N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000727 case ISD::AND: return visitAND(N);
728 case ISD::OR: return visitOR(N);
729 case ISD::XOR: return visitXOR(N);
730 case ISD::SHL: return visitSHL(N);
731 case ISD::SRA: return visitSRA(N);
732 case ISD::SRL: return visitSRL(N);
733 case ISD::CTLZ: return visitCTLZ(N);
734 case ISD::CTTZ: return visitCTTZ(N);
735 case ISD::CTPOP: return visitCTPOP(N);
736 case ISD::SELECT: return visitSELECT(N);
737 case ISD::SELECT_CC: return visitSELECT_CC(N);
738 case ISD::SETCC: return visitSETCC(N);
739 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
740 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
741 case ISD::ANY_EXTEND: return visitANY_EXTEND(N);
742 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
743 case ISD::TRUNCATE: return visitTRUNCATE(N);
744 case ISD::BIT_CONVERT: return visitBIT_CONVERT(N);
Evan Chengb6290462008-05-12 23:04:07 +0000745 case ISD::BUILD_PAIR: return visitBUILD_PAIR(N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000746 case ISD::FADD: return visitFADD(N);
747 case ISD::FSUB: return visitFSUB(N);
748 case ISD::FMUL: return visitFMUL(N);
749 case ISD::FDIV: return visitFDIV(N);
750 case ISD::FREM: return visitFREM(N);
751 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
752 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
753 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
754 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
755 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
756 case ISD::FP_ROUND: return visitFP_ROUND(N);
757 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
758 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
759 case ISD::FNEG: return visitFNEG(N);
760 case ISD::FABS: return visitFABS(N);
761 case ISD::BRCOND: return visitBRCOND(N);
762 case ISD::BR_CC: return visitBR_CC(N);
763 case ISD::LOAD: return visitLOAD(N);
764 case ISD::STORE: return visitSTORE(N);
765 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
Evan Chengd7ba7ed2007-10-06 08:19:55 +0000766 case ISD::EXTRACT_VECTOR_ELT: return visitEXTRACT_VECTOR_ELT(N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000767 case ISD::BUILD_VECTOR: return visitBUILD_VECTOR(N);
768 case ISD::CONCAT_VECTORS: return visitCONCAT_VECTORS(N);
769 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
770 }
Dan Gohman8181bd12008-07-27 21:46:04 +0000771 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000772}
773
Dan Gohman8181bd12008-07-27 21:46:04 +0000774SDValue DAGCombiner::combine(SDNode *N) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000775 SDValue RV = visit(N);
Dan Gohman6c89ea72007-10-08 17:57:15 +0000776
777 // If nothing happened, try a target-specific DAG combine.
Gabor Greif1c80d112008-08-28 21:40:38 +0000778 if (RV.getNode() == 0) {
Dan Gohman6c89ea72007-10-08 17:57:15 +0000779 assert(N->getOpcode() != ISD::DELETED_NODE &&
780 "Node was deleted but visit returned NULL!");
781
782 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
783 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode())) {
784
785 // Expose the DAG combiner to the target combiner impls.
786 TargetLowering::DAGCombinerInfo
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000787 DagCombineInfo(DAG, Level == Unrestricted, false, this);
Dan Gohman6c89ea72007-10-08 17:57:15 +0000788
789 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
790 }
791 }
792
Evan Chengd1113582008-03-22 01:55:50 +0000793 // If N is a commutative binary node, try commuting it to enable more
794 // sdisel CSE.
Gabor Greif1c80d112008-08-28 21:40:38 +0000795 if (RV.getNode() == 0 &&
Evan Chengd1113582008-03-22 01:55:50 +0000796 SelectionDAG::isCommutativeBinOp(N->getOpcode()) &&
797 N->getNumValues() == 1) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000798 SDValue N0 = N->getOperand(0);
799 SDValue N1 = N->getOperand(1);
Bill Wendling131d6e92009-01-30 01:13:16 +0000800
Evan Chengd1113582008-03-22 01:55:50 +0000801 // Constant operands are canonicalized to RHS.
802 if (isa<ConstantSDNode>(N0) || !isa<ConstantSDNode>(N1)) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000803 SDValue Ops[] = { N1, N0 };
Evan Chengd1113582008-03-22 01:55:50 +0000804 SDNode *CSENode = DAG.getNodeIfExists(N->getOpcode(), N->getVTList(),
805 Ops, 2);
Evan Chenge40b51c2008-03-24 23:55:16 +0000806 if (CSENode)
Dan Gohman8181bd12008-07-27 21:46:04 +0000807 return SDValue(CSENode, 0);
Evan Chengd1113582008-03-22 01:55:50 +0000808 }
809 }
810
Dan Gohman6c89ea72007-10-08 17:57:15 +0000811 return RV;
812}
813
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000814/// getInputChainForNode - Given a node, return its input chain if it has one,
815/// otherwise return a null sd operand.
Dan Gohman8181bd12008-07-27 21:46:04 +0000816static SDValue getInputChainForNode(SDNode *N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000817 if (unsigned NumOps = N->getNumOperands()) {
818 if (N->getOperand(0).getValueType() == MVT::Other)
819 return N->getOperand(0);
820 else if (N->getOperand(NumOps-1).getValueType() == MVT::Other)
821 return N->getOperand(NumOps-1);
822 for (unsigned i = 1; i < NumOps-1; ++i)
823 if (N->getOperand(i).getValueType() == MVT::Other)
824 return N->getOperand(i);
825 }
Bill Wendling131d6e92009-01-30 01:13:16 +0000826 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000827}
828
Dan Gohman8181bd12008-07-27 21:46:04 +0000829SDValue DAGCombiner::visitTokenFactor(SDNode *N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000830 // If N has two operands, where one has an input chain equal to the other,
831 // the 'other' chain is redundant.
832 if (N->getNumOperands() == 2) {
Gabor Greif1c80d112008-08-28 21:40:38 +0000833 if (getInputChainForNode(N->getOperand(0).getNode()) == N->getOperand(1))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000834 return N->getOperand(0);
Gabor Greif1c80d112008-08-28 21:40:38 +0000835 if (getInputChainForNode(N->getOperand(1).getNode()) == N->getOperand(0))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000836 return N->getOperand(1);
837 }
838
839 SmallVector<SDNode *, 8> TFs; // List of token factors to visit.
Dan Gohman8181bd12008-07-27 21:46:04 +0000840 SmallVector<SDValue, 8> Ops; // Ops for replacing token factor.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000841 SmallPtrSet<SDNode*, 16> SeenOps;
842 bool Changed = false; // If we should replace this token factor.
843
844 // Start out with this token factor.
845 TFs.push_back(N);
846
847 // Iterate through token factors. The TFs grows when new token factors are
848 // encountered.
849 for (unsigned i = 0; i < TFs.size(); ++i) {
850 SDNode *TF = TFs[i];
851
852 // Check each of the operands.
853 for (unsigned i = 0, ie = TF->getNumOperands(); i != ie; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000854 SDValue Op = TF->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000855
856 switch (Op.getOpcode()) {
857 case ISD::EntryToken:
858 // Entry tokens don't need to be added to the list. They are
859 // rededundant.
860 Changed = true;
861 break;
862
863 case ISD::TokenFactor:
864 if ((CombinerAA || Op.hasOneUse()) &&
Gabor Greif1c80d112008-08-28 21:40:38 +0000865 std::find(TFs.begin(), TFs.end(), Op.getNode()) == TFs.end()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000866 // Queue up for processing.
Gabor Greif1c80d112008-08-28 21:40:38 +0000867 TFs.push_back(Op.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000868 // Clean up in case the token factor is removed.
Gabor Greif1c80d112008-08-28 21:40:38 +0000869 AddToWorkList(Op.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000870 Changed = true;
871 break;
872 }
873 // Fall thru
874
875 default:
876 // Only add if it isn't already in the list.
Gabor Greif1c80d112008-08-28 21:40:38 +0000877 if (SeenOps.insert(Op.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000878 Ops.push_back(Op);
879 else
880 Changed = true;
881 break;
882 }
883 }
884 }
885
Dan Gohman8181bd12008-07-27 21:46:04 +0000886 SDValue Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000887
888 // If we've change things around then replace token factor.
889 if (Changed) {
Dan Gohman301f4052008-01-29 13:02:09 +0000890 if (Ops.empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000891 // The entry token is the only possible outcome.
892 Result = DAG.getEntryNode();
893 } else {
894 // New and improved token factor.
Bill Wendling131d6e92009-01-30 01:13:16 +0000895 Result = DAG.getNode(ISD::TokenFactor, N->getDebugLoc(),
896 MVT::Other, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000897 }
Bill Wendling131d6e92009-01-30 01:13:16 +0000898
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000899 // Don't add users to work list.
900 return CombineTo(N, Result, false);
901 }
902
903 return Result;
904}
905
Chris Lattnerf32fa7f2008-02-13 07:25:05 +0000906/// MERGE_VALUES can always be eliminated.
Dan Gohman8181bd12008-07-27 21:46:04 +0000907SDValue DAGCombiner::visitMERGE_VALUES(SDNode *N) {
Chris Lattnerf32fa7f2008-02-13 07:25:05 +0000908 WorkListRemover DeadNodes(*this);
909 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Dan Gohman8181bd12008-07-27 21:46:04 +0000910 DAG.ReplaceAllUsesOfValueWith(SDValue(N, i), N->getOperand(i),
Chris Lattnerf32fa7f2008-02-13 07:25:05 +0000911 &DeadNodes);
912 removeFromWorkList(N);
913 DAG.DeleteNode(N);
Dan Gohman8181bd12008-07-27 21:46:04 +0000914 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerf32fa7f2008-02-13 07:25:05 +0000915}
916
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000917static
Bill Wendlinge165f5a2009-01-30 02:23:43 +0000918SDValue combineShlAddConstant(DebugLoc DL, SDValue N0, SDValue N1,
919 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +0000920 MVT VT = N0.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +0000921 SDValue N00 = N0.getOperand(0);
922 SDValue N01 = N0.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000923 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N01);
Bill Wendlinge165f5a2009-01-30 02:23:43 +0000924
Gabor Greif1c80d112008-08-28 21:40:38 +0000925 if (N01C && N00.getOpcode() == ISD::ADD && N00.getNode()->hasOneUse() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000926 isa<ConstantSDNode>(N00.getOperand(1))) {
Bill Wendlinge165f5a2009-01-30 02:23:43 +0000927 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
928 N0 = DAG.getNode(ISD::ADD, N0.getDebugLoc(), VT,
929 DAG.getNode(ISD::SHL, N00.getDebugLoc(), VT,
930 N00.getOperand(0), N01),
931 DAG.getNode(ISD::SHL, N01.getDebugLoc(), VT,
932 N00.getOperand(1), N01));
933 return DAG.getNode(ISD::ADD, DL, VT, N0, N1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000934 }
Bill Wendlinge165f5a2009-01-30 02:23:43 +0000935
Dan Gohman8181bd12008-07-27 21:46:04 +0000936 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000937}
938
939static
Dan Gohman8181bd12008-07-27 21:46:04 +0000940SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
Bill Wendling0d810b82008-11-11 08:25:46 +0000941 SelectionDAG &DAG, const TargetLowering &TLI,
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000942 bool LegalOperations) {
Duncan Sands92c43912008-06-06 12:08:01 +0000943 MVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000944 unsigned Opc = N->getOpcode();
945 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
Dan Gohman8181bd12008-07-27 21:46:04 +0000946 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
947 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000948 ISD::CondCode CC = ISD::SETCC_INVALID;
Bill Wendling0d810b82008-11-11 08:25:46 +0000949
950 if (isSlctCC) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000951 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
Bill Wendling0d810b82008-11-11 08:25:46 +0000952 } else {
Dan Gohman8181bd12008-07-27 21:46:04 +0000953 SDValue CCOp = Slct.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000954 if (CCOp.getOpcode() == ISD::SETCC)
955 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
956 }
957
958 bool DoXform = false;
959 bool InvCC = false;
960 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
961 "Bad input!");
Bill Wendling0d810b82008-11-11 08:25:46 +0000962
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000963 if (LHS.getOpcode() == ISD::Constant &&
Bill Wendling0d810b82008-11-11 08:25:46 +0000964 cast<ConstantSDNode>(LHS)->isNullValue()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000965 DoXform = true;
Bill Wendling0d810b82008-11-11 08:25:46 +0000966 } else if (CC != ISD::SETCC_INVALID &&
967 RHS.getOpcode() == ISD::Constant &&
968 cast<ConstantSDNode>(RHS)->isNullValue()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000969 std::swap(LHS, RHS);
Dan Gohman8181bd12008-07-27 21:46:04 +0000970 SDValue Op0 = Slct.getOperand(0);
Bill Wendling0d810b82008-11-11 08:25:46 +0000971 MVT OpVT = isSlctCC ? Op0.getValueType() :
972 Op0.getOperand(0).getValueType();
973 bool isInt = OpVT.isInteger();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000974 CC = ISD::getSetCCInverse(CC, isInt);
Bill Wendling0d810b82008-11-11 08:25:46 +0000975
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000976 if (LegalOperations && !TLI.isCondCodeLegal(CC, OpVT))
Bill Wendling0d810b82008-11-11 08:25:46 +0000977 return SDValue(); // Inverse operator isn't legal.
978
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000979 DoXform = true;
980 InvCC = true;
981 }
982
983 if (DoXform) {
Bill Wendlinge165f5a2009-01-30 02:23:43 +0000984 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000985 if (isSlctCC)
Bill Wendlinge165f5a2009-01-30 02:23:43 +0000986 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000987 Slct.getOperand(0), Slct.getOperand(1), CC);
Dan Gohman8181bd12008-07-27 21:46:04 +0000988 SDValue CCOp = Slct.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000989 if (InvCC)
Bill Wendlinge165f5a2009-01-30 02:23:43 +0000990 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
991 CCOp.getOperand(0), CCOp.getOperand(1), CC);
992 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
993 CCOp, OtherOp, Result);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000994 }
Dan Gohman8181bd12008-07-27 21:46:04 +0000995 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000996}
997
Dan Gohman8181bd12008-07-27 21:46:04 +0000998SDValue DAGCombiner::visitADD(SDNode *N) {
999 SDValue N0 = N->getOperand(0);
1000 SDValue N1 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001001 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1002 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands92c43912008-06-06 12:08:01 +00001003 MVT VT = N0.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001004
1005 // fold vector ops
Duncan Sands92c43912008-06-06 12:08:01 +00001006 if (VT.isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001007 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00001008 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001009 }
Bill Wendlingc43c7ee2008-12-10 22:36:00 +00001010
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001011 // fold (add x, undef) -> undef
1012 if (N0.getOpcode() == ISD::UNDEF)
1013 return N0;
1014 if (N1.getOpcode() == ISD::UNDEF)
1015 return N1;
1016 // fold (add c1, c2) -> c1+c2
1017 if (N0C && N1C)
Bill Wendling0445c4c2008-09-24 10:25:02 +00001018 return DAG.FoldConstantArithmetic(ISD::ADD, VT, N0C, N1C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001019 // canonicalize constant to RHS
1020 if (N0C && !N1C)
Bill Wendlingd850aa52009-01-30 02:31:17 +00001021 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N1, N0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001022 // fold (add x, 0) -> x
1023 if (N1C && N1C->isNullValue())
1024 return N0;
Dan Gohman36322c72008-10-18 02:06:02 +00001025 // fold (add Sym, c) -> Sym+c
1026 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0))
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00001027 if (!LegalOperations && TLI.isOffsetFoldingLegal(GA) && N1C &&
Dan Gohman36322c72008-10-18 02:06:02 +00001028 GA->getOpcode() == ISD::GlobalAddress)
1029 return DAG.getGlobalAddress(GA->getGlobal(), VT,
1030 GA->getOffset() +
1031 (uint64_t)N1C->getSExtValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001032 // fold ((c1-A)+c2) -> (c1+c2)-A
1033 if (N1C && N0.getOpcode() == ISD::SUB)
1034 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
Bill Wendlingd850aa52009-01-30 02:31:17 +00001035 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
Dan Gohman9d24dc72008-03-13 22:13:53 +00001036 DAG.getConstant(N1C->getAPIntValue()+
1037 N0C->getAPIntValue(), VT),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001038 N0.getOperand(1));
1039 // reassociate add
Bill Wendlingabb33a22009-01-30 00:45:56 +00001040 SDValue RADD = ReassociateOps(ISD::ADD, N->getDebugLoc(), N0, N1);
Gabor Greif1c80d112008-08-28 21:40:38 +00001041 if (RADD.getNode() != 0)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001042 return RADD;
1043 // fold ((0-A) + B) -> B-A
1044 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
1045 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Bill Wendlingd850aa52009-01-30 02:31:17 +00001046 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N1, N0.getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001047 // fold (A + (0-B)) -> A-B
1048 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
1049 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Bill Wendlingd850aa52009-01-30 02:31:17 +00001050 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N0, N1.getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001051 // fold (A+(B-A)) -> B
1052 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
1053 return N1.getOperand(0);
Dale Johannesene7e5da32008-11-27 00:43:21 +00001054 // fold ((B-A)+A) -> B
1055 if (N0.getOpcode() == ISD::SUB && N1 == N0.getOperand(1))
1056 return N0.getOperand(0);
Dale Johannesend1feb582008-12-02 01:30:54 +00001057 // fold (A+(B-(A+C))) to (B-C)
1058 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1).getOpcode() == ISD::ADD &&
Bill Wendlingd850aa52009-01-30 02:31:17 +00001059 N0 == N1.getOperand(1).getOperand(0))
1060 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N1.getOperand(0),
Dale Johannesend1feb582008-12-02 01:30:54 +00001061 N1.getOperand(1).getOperand(1));
Dale Johannesend1feb582008-12-02 01:30:54 +00001062 // fold (A+(B-(C+A))) to (B-C)
1063 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1).getOpcode() == ISD::ADD &&
Bill Wendlingd850aa52009-01-30 02:31:17 +00001064 N0 == N1.getOperand(1).getOperand(1))
1065 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N1.getOperand(0),
Dale Johannesend1feb582008-12-02 01:30:54 +00001066 N1.getOperand(1).getOperand(0));
Dale Johannesend32a9602008-12-23 23:47:22 +00001067 // fold (A+((B-A)+or-C)) to (B+or-C)
Dale Johannesend29920f2008-12-02 18:40:40 +00001068 if ((N1.getOpcode() == ISD::SUB || N1.getOpcode() == ISD::ADD) &&
1069 N1.getOperand(0).getOpcode() == ISD::SUB &&
Bill Wendlingd850aa52009-01-30 02:31:17 +00001070 N0 == N1.getOperand(0).getOperand(1))
1071 return DAG.getNode(N1.getOpcode(), N->getDebugLoc(), VT,
1072 N1.getOperand(0).getOperand(0), N1.getOperand(1));
Dale Johannesend29920f2008-12-02 18:40:40 +00001073
Dale Johannesend1feb582008-12-02 01:30:54 +00001074 // fold (A-B)+(C-D) to (A+C)-(B+D) when A or C is constant
1075 if (N0.getOpcode() == ISD::SUB && N1.getOpcode() == ISD::SUB) {
1076 SDValue N00 = N0.getOperand(0);
1077 SDValue N01 = N0.getOperand(1);
1078 SDValue N10 = N1.getOperand(0);
1079 SDValue N11 = N1.getOperand(1);
Bill Wendlingd850aa52009-01-30 02:31:17 +00001080
1081 if (isa<ConstantSDNode>(N00) || isa<ConstantSDNode>(N10))
1082 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1083 DAG.getNode(ISD::ADD, N0.getDebugLoc(), VT, N00, N10),
1084 DAG.getNode(ISD::ADD, N1.getDebugLoc(), VT, N01, N11));
Dale Johannesend1feb582008-12-02 01:30:54 +00001085 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001086
Dan Gohman8181bd12008-07-27 21:46:04 +00001087 if (!VT.isVector() && SimplifyDemandedBits(SDValue(N, 0)))
1088 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001089
1090 // fold (a+b) -> (a|b) iff a and b share no bits.
Duncan Sands92c43912008-06-06 12:08:01 +00001091 if (VT.isInteger() && !VT.isVector()) {
Dan Gohmanbea075f2008-02-20 16:33:30 +00001092 APInt LHSZero, LHSOne;
1093 APInt RHSZero, RHSOne;
Duncan Sands92c43912008-06-06 12:08:01 +00001094 APInt Mask = APInt::getAllOnesValue(VT.getSizeInBits());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001095 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Bill Wendlingd850aa52009-01-30 02:31:17 +00001096
Dan Gohmanbea075f2008-02-20 16:33:30 +00001097 if (LHSZero.getBoolValue()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001098 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
1099
1100 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1101 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
1102 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
1103 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
Bill Wendlingd850aa52009-01-30 02:31:17 +00001104 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N0, N1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001105 }
1106 }
1107
1108 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
Gabor Greif1c80d112008-08-28 21:40:38 +00001109 if (N0.getOpcode() == ISD::SHL && N0.getNode()->hasOneUse()) {
Bill Wendlinge165f5a2009-01-30 02:23:43 +00001110 SDValue Result = combineShlAddConstant(N->getDebugLoc(), N0, N1, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001111 if (Result.getNode()) return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001112 }
Gabor Greif1c80d112008-08-28 21:40:38 +00001113 if (N1.getOpcode() == ISD::SHL && N1.getNode()->hasOneUse()) {
Bill Wendlinge165f5a2009-01-30 02:23:43 +00001114 SDValue Result = combineShlAddConstant(N->getDebugLoc(), N1, N0, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001115 if (Result.getNode()) return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001116 }
1117
1118 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
Gabor Greif1c80d112008-08-28 21:40:38 +00001119 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00001120 SDValue Result = combineSelectAndUse(N, N0, N1, DAG, TLI, LegalOperations);
Gabor Greif1c80d112008-08-28 21:40:38 +00001121 if (Result.getNode()) return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001122 }
Gabor Greif1c80d112008-08-28 21:40:38 +00001123 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00001124 SDValue Result = combineSelectAndUse(N, N1, N0, DAG, TLI, LegalOperations);
Gabor Greif1c80d112008-08-28 21:40:38 +00001125 if (Result.getNode()) return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001126 }
1127
Dan Gohman8181bd12008-07-27 21:46:04 +00001128 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001129}
1130
Dan Gohman8181bd12008-07-27 21:46:04 +00001131SDValue DAGCombiner::visitADDC(SDNode *N) {
1132 SDValue N0 = N->getOperand(0);
1133 SDValue N1 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001134 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1135 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands92c43912008-06-06 12:08:01 +00001136 MVT VT = N0.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001137
1138 // If the flag result is dead, turn this into an ADD.
1139 if (N->hasNUsesOfValue(0, 1))
Bill Wendling4c196ba2009-01-30 02:38:00 +00001140 return CombineTo(N, DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N1, N0),
1141 DAG.getNode(ISD::CARRY_FALSE,
1142 N->getDebugLoc(), MVT::Flag));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001143
1144 // canonicalize constant to RHS.
Dan Gohmanedb43e72008-06-23 15:29:14 +00001145 if (N0C && !N1C)
Bill Wendling4c196ba2009-01-30 02:38:00 +00001146 return DAG.getNode(ISD::ADDC, N->getDebugLoc(), N->getVTList(), N1, N0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001147
1148 // fold (addc x, 0) -> x + no carry out
1149 if (N1C && N1C->isNullValue())
Bill Wendling4c196ba2009-01-30 02:38:00 +00001150 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE,
1151 N->getDebugLoc(), MVT::Flag));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001152
1153 // fold (addc a, b) -> (or a, b), CARRY_FALSE iff a and b share no bits.
Dan Gohmanbea075f2008-02-20 16:33:30 +00001154 APInt LHSZero, LHSOne;
1155 APInt RHSZero, RHSOne;
Duncan Sands92c43912008-06-06 12:08:01 +00001156 APInt Mask = APInt::getAllOnesValue(VT.getSizeInBits());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001157 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Bill Wendling4c196ba2009-01-30 02:38:00 +00001158
Dan Gohmanbea075f2008-02-20 16:33:30 +00001159 if (LHSZero.getBoolValue()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001160 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
1161
1162 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1163 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
1164 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
1165 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
Bill Wendling4c196ba2009-01-30 02:38:00 +00001166 return CombineTo(N, DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N0, N1),
1167 DAG.getNode(ISD::CARRY_FALSE,
1168 N->getDebugLoc(), MVT::Flag));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001169 }
1170
Dan Gohman8181bd12008-07-27 21:46:04 +00001171 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001172}
1173
Dan Gohman8181bd12008-07-27 21:46:04 +00001174SDValue DAGCombiner::visitADDE(SDNode *N) {
1175 SDValue N0 = N->getOperand(0);
1176 SDValue N1 = N->getOperand(1);
1177 SDValue CarryIn = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001178 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1179 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001180
1181 // canonicalize constant to RHS
Dan Gohmanedb43e72008-06-23 15:29:14 +00001182 if (N0C && !N1C)
Bill Wendling4c196ba2009-01-30 02:38:00 +00001183 return DAG.getNode(ISD::ADDE, N->getDebugLoc(), N->getVTList(),
1184 N1, N0, CarryIn);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001185
1186 // fold (adde x, y, false) -> (addc x, y)
Dan Gohmanedb43e72008-06-23 15:29:14 +00001187 if (CarryIn.getOpcode() == ISD::CARRY_FALSE)
Bill Wendling4c196ba2009-01-30 02:38:00 +00001188 return DAG.getNode(ISD::ADDC, N->getDebugLoc(), N->getVTList(), N1, N0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001189
Dan Gohman8181bd12008-07-27 21:46:04 +00001190 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001191}
1192
Dan Gohman8181bd12008-07-27 21:46:04 +00001193SDValue DAGCombiner::visitSUB(SDNode *N) {
1194 SDValue N0 = N->getOperand(0);
1195 SDValue N1 = N->getOperand(1);
Gabor Greif1c80d112008-08-28 21:40:38 +00001196 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1197 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Duncan Sands92c43912008-06-06 12:08:01 +00001198 MVT VT = N0.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001199
1200 // fold vector ops
Duncan Sands92c43912008-06-06 12:08:01 +00001201 if (VT.isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001202 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00001203 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001204 }
Bill Wendlingc43c7ee2008-12-10 22:36:00 +00001205
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001206 // fold (sub x, x) -> 0
Evan Chenga15896e2008-03-12 07:02:50 +00001207 if (N0 == N1)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001208 return DAG.getConstant(0, N->getValueType(0));
1209 // fold (sub c1, c2) -> c1-c2
1210 if (N0C && N1C)
Bill Wendling0445c4c2008-09-24 10:25:02 +00001211 return DAG.FoldConstantArithmetic(ISD::SUB, VT, N0C, N1C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001212 // fold (sub x, c) -> (add x, -c)
1213 if (N1C)
Bill Wendling697795a2009-01-30 02:42:10 +00001214 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0,
Dan Gohman9d24dc72008-03-13 22:13:53 +00001215 DAG.getConstant(-N1C->getAPIntValue(), VT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001216 // fold (A+B)-A -> B
1217 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
1218 return N0.getOperand(1);
1219 // fold (A+B)-B -> A
1220 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Dale Johannesend546e832008-12-23 23:01:27 +00001221 return N0.getOperand(0);
Dale Johannesend32a9602008-12-23 23:47:22 +00001222 // fold ((A+(B+or-C))-B) -> A+or-C
Dale Johannesen0b58ffa2008-12-16 22:13:49 +00001223 if (N0.getOpcode() == ISD::ADD &&
Dale Johannesend546e832008-12-23 23:01:27 +00001224 (N0.getOperand(1).getOpcode() == ISD::SUB ||
1225 N0.getOperand(1).getOpcode() == ISD::ADD) &&
Dale Johannesen0b58ffa2008-12-16 22:13:49 +00001226 N0.getOperand(1).getOperand(0) == N1)
Bill Wendling697795a2009-01-30 02:42:10 +00001227 return DAG.getNode(N0.getOperand(1).getOpcode(), N->getDebugLoc(), VT,
1228 N0.getOperand(0), N0.getOperand(1).getOperand(1));
Dale Johannesend546e832008-12-23 23:01:27 +00001229 // fold ((A+(C+B))-B) -> A+C
1230 if (N0.getOpcode() == ISD::ADD &&
1231 N0.getOperand(1).getOpcode() == ISD::ADD &&
1232 N0.getOperand(1).getOperand(1) == N1)
Bill Wendling697795a2009-01-30 02:42:10 +00001233 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT,
1234 N0.getOperand(0), N0.getOperand(1).getOperand(0));
Dale Johannesenfbc2e4b2008-12-23 01:59:54 +00001235 // fold ((A-(B-C))-C) -> A-B
1236 if (N0.getOpcode() == ISD::SUB &&
1237 N0.getOperand(1).getOpcode() == ISD::SUB &&
1238 N0.getOperand(1).getOperand(1) == N1)
Bill Wendling697795a2009-01-30 02:42:10 +00001239 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1240 N0.getOperand(0), N0.getOperand(1).getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001241 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
Gabor Greif1c80d112008-08-28 21:40:38 +00001242 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00001243 SDValue Result = combineSelectAndUse(N, N1, N0, DAG, TLI, LegalOperations);
Gabor Greif1c80d112008-08-28 21:40:38 +00001244 if (Result.getNode()) return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001245 }
Bill Wendling697795a2009-01-30 02:42:10 +00001246
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001247 // If either operand of a sub is undef, the result is undef
1248 if (N0.getOpcode() == ISD::UNDEF)
1249 return N0;
1250 if (N1.getOpcode() == ISD::UNDEF)
1251 return N1;
1252
Dan Gohman36322c72008-10-18 02:06:02 +00001253 // If the relocation model supports it, consider symbol offsets.
1254 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0))
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00001255 if (!LegalOperations && TLI.isOffsetFoldingLegal(GA)) {
Dan Gohman36322c72008-10-18 02:06:02 +00001256 // fold (sub Sym, c) -> Sym-c
1257 if (N1C && GA->getOpcode() == ISD::GlobalAddress)
1258 return DAG.getGlobalAddress(GA->getGlobal(), VT,
1259 GA->getOffset() -
1260 (uint64_t)N1C->getSExtValue());
1261 // fold (sub Sym+c1, Sym+c2) -> c1-c2
1262 if (GlobalAddressSDNode *GB = dyn_cast<GlobalAddressSDNode>(N1))
1263 if (GA->getGlobal() == GB->getGlobal())
1264 return DAG.getConstant((uint64_t)GA->getOffset() - GB->getOffset(),
1265 VT);
1266 }
1267
Dan Gohman8181bd12008-07-27 21:46:04 +00001268 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001269}
1270
Dan Gohman8181bd12008-07-27 21:46:04 +00001271SDValue DAGCombiner::visitMUL(SDNode *N) {
1272 SDValue N0 = N->getOperand(0);
1273 SDValue N1 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001274 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1275 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands92c43912008-06-06 12:08:01 +00001276 MVT VT = N0.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001277
1278 // fold vector ops
Duncan Sands92c43912008-06-06 12:08:01 +00001279 if (VT.isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001280 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00001281 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001282 }
1283
1284 // fold (mul x, undef) -> 0
1285 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
1286 return DAG.getConstant(0, VT);
1287 // fold (mul c1, c2) -> c1*c2
1288 if (N0C && N1C)
Bill Wendling0445c4c2008-09-24 10:25:02 +00001289 return DAG.FoldConstantArithmetic(ISD::MUL, VT, N0C, N1C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001290 // canonicalize constant to RHS
1291 if (N0C && !N1C)
Bill Wendlingdb4984a2009-01-30 02:45:56 +00001292 return DAG.getNode(ISD::MUL, N->getDebugLoc(), VT, N1, N0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001293 // fold (mul x, 0) -> 0
1294 if (N1C && N1C->isNullValue())
1295 return N1;
1296 // fold (mul x, -1) -> 0-x
1297 if (N1C && N1C->isAllOnesValue())
Bill Wendlingdb4984a2009-01-30 02:45:56 +00001298 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1299 DAG.getConstant(0, VT), N0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001300 // fold (mul x, (1 << c)) -> x << c
Dan Gohman9d24dc72008-03-13 22:13:53 +00001301 if (N1C && N1C->getAPIntValue().isPowerOf2())
Bill Wendlingdb4984a2009-01-30 02:45:56 +00001302 return DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, N0,
Dan Gohman9d24dc72008-03-13 22:13:53 +00001303 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001304 TLI.getShiftAmountTy()));
1305 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
Bill Wendlingdb4984a2009-01-30 02:45:56 +00001306 if (N1C && isPowerOf2_64(-N1C->getSExtValue()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001307 // FIXME: If the input is something that is easily negated (e.g. a
1308 // single-use add), we should put the negate there.
Bill Wendlingdb4984a2009-01-30 02:45:56 +00001309 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1310 DAG.getConstant(0, VT),
Bill Wendlingc80e5c42009-01-30 02:49:26 +00001311 DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, N0,
Dan Gohman40686732008-09-26 21:54:37 +00001312 DAG.getConstant(Log2_64(-N1C->getSExtValue()),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001313 TLI.getShiftAmountTy())));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001314 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
Bill Wendlingc80e5c42009-01-30 02:49:26 +00001315 if (N1C && N0.getOpcode() == ISD::SHL &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001316 isa<ConstantSDNode>(N0.getOperand(1))) {
Bill Wendlingdb4984a2009-01-30 02:45:56 +00001317 SDValue C3 = DAG.getNode(ISD::SHL, N->getDebugLoc(), VT,
1318 N1, N0.getOperand(1));
Gabor Greif1c80d112008-08-28 21:40:38 +00001319 AddToWorkList(C3.getNode());
Bill Wendlingdb4984a2009-01-30 02:45:56 +00001320 return DAG.getNode(ISD::MUL, N->getDebugLoc(), VT,
1321 N0.getOperand(0), C3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001322 }
1323
1324 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
1325 // use.
1326 {
Dan Gohman8181bd12008-07-27 21:46:04 +00001327 SDValue Sh(0,0), Y(0,0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001328 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
1329 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
Gabor Greif1c80d112008-08-28 21:40:38 +00001330 N0.getNode()->hasOneUse()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001331 Sh = N0; Y = N1;
1332 } else if (N1.getOpcode() == ISD::SHL &&
Gabor Greifb420b9d2008-08-30 19:29:20 +00001333 isa<ConstantSDNode>(N1.getOperand(1)) &&
1334 N1.getNode()->hasOneUse()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001335 Sh = N1; Y = N0;
1336 }
Bill Wendlingc80e5c42009-01-30 02:49:26 +00001337
Gabor Greif1c80d112008-08-28 21:40:38 +00001338 if (Sh.getNode()) {
Bill Wendlingdb4984a2009-01-30 02:45:56 +00001339 SDValue Mul = DAG.getNode(ISD::MUL, N->getDebugLoc(), VT,
1340 Sh.getOperand(0), Y);
1341 return DAG.getNode(ISD::SHL, N->getDebugLoc(), VT,
1342 Mul, Sh.getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001343 }
1344 }
Bill Wendlingc80e5c42009-01-30 02:49:26 +00001345
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001346 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
Gabor Greif1c80d112008-08-28 21:40:38 +00001347 if (N1C && N0.getOpcode() == ISD::ADD && N0.getNode()->hasOneUse() &&
Bill Wendlingdb4984a2009-01-30 02:45:56 +00001348 isa<ConstantSDNode>(N0.getOperand(1)))
1349 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT,
1350 DAG.getNode(ISD::MUL, N0.getDebugLoc(), VT,
1351 N0.getOperand(0), N1),
1352 DAG.getNode(ISD::MUL, N1.getDebugLoc(), VT,
1353 N0.getOperand(1), N1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001354
1355 // reassociate mul
Bill Wendlingabb33a22009-01-30 00:45:56 +00001356 SDValue RMUL = ReassociateOps(ISD::MUL, N->getDebugLoc(), N0, N1);
Gabor Greif1c80d112008-08-28 21:40:38 +00001357 if (RMUL.getNode() != 0)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001358 return RMUL;
1359
Dan Gohman8181bd12008-07-27 21:46:04 +00001360 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001361}
1362
Dan Gohman8181bd12008-07-27 21:46:04 +00001363SDValue DAGCombiner::visitSDIV(SDNode *N) {
1364 SDValue N0 = N->getOperand(0);
1365 SDValue N1 = N->getOperand(1);
Gabor Greif1c80d112008-08-28 21:40:38 +00001366 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1367 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Duncan Sands92c43912008-06-06 12:08:01 +00001368 MVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001369
1370 // fold vector ops
Duncan Sands92c43912008-06-06 12:08:01 +00001371 if (VT.isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001372 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00001373 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001374 }
1375
1376 // fold (sdiv c1, c2) -> c1/c2
1377 if (N0C && N1C && !N1C->isNullValue())
Bill Wendling0445c4c2008-09-24 10:25:02 +00001378 return DAG.FoldConstantArithmetic(ISD::SDIV, VT, N0C, N1C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001379 // fold (sdiv X, 1) -> X
Dan Gohman40686732008-09-26 21:54:37 +00001380 if (N1C && N1C->getSExtValue() == 1LL)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001381 return N0;
1382 // fold (sdiv X, -1) -> 0-X
1383 if (N1C && N1C->isAllOnesValue())
Bill Wendling09f5dc82009-01-30 02:52:17 +00001384 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1385 DAG.getConstant(0, VT), N0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001386 // If we know the sign bits of both operands are zero, strength reduce to a
1387 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
Duncan Sands92c43912008-06-06 12:08:01 +00001388 if (!VT.isVector()) {
Dan Gohman07961cd2008-02-25 21:11:39 +00001389 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Bill Wendling09f5dc82009-01-30 02:52:17 +00001390 return DAG.getNode(ISD::UDIV, N->getDebugLoc(), N1.getValueType(),
1391 N0, N1);
Chris Lattner336672f2008-01-27 23:32:17 +00001392 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001393 // fold (sdiv X, pow2) -> simple ops after legalize
Dan Gohman9d24dc72008-03-13 22:13:53 +00001394 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap() &&
Dan Gohman40686732008-09-26 21:54:37 +00001395 (isPowerOf2_64(N1C->getSExtValue()) ||
1396 isPowerOf2_64(-N1C->getSExtValue()))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001397 // If dividing by powers of two is cheap, then don't perform the following
1398 // fold.
1399 if (TLI.isPow2DivCheap())
Dan Gohman8181bd12008-07-27 21:46:04 +00001400 return SDValue();
Bill Wendling09f5dc82009-01-30 02:52:17 +00001401
Dan Gohman40686732008-09-26 21:54:37 +00001402 int64_t pow2 = N1C->getSExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001403 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
1404 unsigned lg2 = Log2_64(abs2);
Bill Wendling09f5dc82009-01-30 02:52:17 +00001405
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001406 // Splat the sign bit into the register
Bill Wendling09f5dc82009-01-30 02:52:17 +00001407 SDValue SGN = DAG.getNode(ISD::SRA, N->getDebugLoc(), VT, N0,
1408 DAG.getConstant(VT.getSizeInBits()-1,
1409 TLI.getShiftAmountTy()));
Gabor Greif1c80d112008-08-28 21:40:38 +00001410 AddToWorkList(SGN.getNode());
Bill Wendling09f5dc82009-01-30 02:52:17 +00001411
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001412 // Add (N0 < 0) ? abs2 - 1 : 0;
Bill Wendling09f5dc82009-01-30 02:52:17 +00001413 SDValue SRL = DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, SGN,
1414 DAG.getConstant(VT.getSizeInBits() - lg2,
1415 TLI.getShiftAmountTy()));
1416 SDValue ADD = DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0, SRL);
Gabor Greif1c80d112008-08-28 21:40:38 +00001417 AddToWorkList(SRL.getNode());
1418 AddToWorkList(ADD.getNode()); // Divide by pow2
Bill Wendling09f5dc82009-01-30 02:52:17 +00001419 SDValue SRA = DAG.getNode(ISD::SRA, N->getDebugLoc(), VT, ADD,
1420 DAG.getConstant(lg2, TLI.getShiftAmountTy()));
1421
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001422 // If we're dividing by a positive value, we're done. Otherwise, we must
1423 // negate the result.
1424 if (pow2 > 0)
1425 return SRA;
Bill Wendling09f5dc82009-01-30 02:52:17 +00001426
Gabor Greif1c80d112008-08-28 21:40:38 +00001427 AddToWorkList(SRA.getNode());
Bill Wendling09f5dc82009-01-30 02:52:17 +00001428 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1429 DAG.getConstant(0, VT), SRA);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001430 }
Bill Wendling09f5dc82009-01-30 02:52:17 +00001431
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001432 // if integer divide is expensive and we satisfy the requirements, emit an
1433 // alternate sequence.
Dan Gohman40686732008-09-26 21:54:37 +00001434 if (N1C && (N1C->getSExtValue() < -1 || N1C->getSExtValue() > 1) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001435 !TLI.isIntDivCheap()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001436 SDValue Op = BuildSDIV(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00001437 if (Op.getNode()) return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001438 }
1439
1440 // undef / X -> 0
1441 if (N0.getOpcode() == ISD::UNDEF)
1442 return DAG.getConstant(0, VT);
1443 // X / undef -> undef
1444 if (N1.getOpcode() == ISD::UNDEF)
1445 return N1;
1446
Dan Gohman8181bd12008-07-27 21:46:04 +00001447 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001448}
1449
Dan Gohman8181bd12008-07-27 21:46:04 +00001450SDValue DAGCombiner::visitUDIV(SDNode *N) {
1451 SDValue N0 = N->getOperand(0);
1452 SDValue N1 = N->getOperand(1);
Gabor Greif1c80d112008-08-28 21:40:38 +00001453 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1454 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Duncan Sands92c43912008-06-06 12:08:01 +00001455 MVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001456
1457 // fold vector ops
Duncan Sands92c43912008-06-06 12:08:01 +00001458 if (VT.isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001459 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00001460 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001461 }
1462
1463 // fold (udiv c1, c2) -> c1/c2
1464 if (N0C && N1C && !N1C->isNullValue())
Bill Wendling0445c4c2008-09-24 10:25:02 +00001465 return DAG.FoldConstantArithmetic(ISD::UDIV, VT, N0C, N1C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001466 // fold (udiv x, (1 << c)) -> x >>u c
Dan Gohman9d24dc72008-03-13 22:13:53 +00001467 if (N1C && N1C->getAPIntValue().isPowerOf2())
Bill Wendlingb3552a52009-01-30 02:55:25 +00001468 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0,
Dan Gohman9d24dc72008-03-13 22:13:53 +00001469 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001470 TLI.getShiftAmountTy()));
1471 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
1472 if (N1.getOpcode() == ISD::SHL) {
1473 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman9d24dc72008-03-13 22:13:53 +00001474 if (SHC->getAPIntValue().isPowerOf2()) {
Duncan Sands92c43912008-06-06 12:08:01 +00001475 MVT ADDVT = N1.getOperand(1).getValueType();
Bill Wendlingb3552a52009-01-30 02:55:25 +00001476 SDValue Add = DAG.getNode(ISD::ADD, N->getDebugLoc(), ADDVT,
1477 N1.getOperand(1),
1478 DAG.getConstant(SHC->getAPIntValue()
1479 .logBase2(),
1480 ADDVT));
Gabor Greif1c80d112008-08-28 21:40:38 +00001481 AddToWorkList(Add.getNode());
Bill Wendlingb3552a52009-01-30 02:55:25 +00001482 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0, Add);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001483 }
1484 }
1485 }
1486 // fold (udiv x, c) -> alternate
Dan Gohman9d24dc72008-03-13 22:13:53 +00001487 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001488 SDValue Op = BuildUDIV(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00001489 if (Op.getNode()) return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001490 }
1491
1492 // undef / X -> 0
1493 if (N0.getOpcode() == ISD::UNDEF)
1494 return DAG.getConstant(0, VT);
1495 // X / undef -> undef
1496 if (N1.getOpcode() == ISD::UNDEF)
1497 return N1;
1498
Dan Gohman8181bd12008-07-27 21:46:04 +00001499 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001500}
1501
Dan Gohman8181bd12008-07-27 21:46:04 +00001502SDValue DAGCombiner::visitSREM(SDNode *N) {
1503 SDValue N0 = N->getOperand(0);
1504 SDValue N1 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001505 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1506 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands92c43912008-06-06 12:08:01 +00001507 MVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001508
1509 // fold (srem c1, c2) -> c1%c2
1510 if (N0C && N1C && !N1C->isNullValue())
Bill Wendling0445c4c2008-09-24 10:25:02 +00001511 return DAG.FoldConstantArithmetic(ISD::SREM, VT, N0C, N1C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001512 // If we know the sign bits of both operands are zero, strength reduce to a
1513 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
Duncan Sands92c43912008-06-06 12:08:01 +00001514 if (!VT.isVector()) {
Dan Gohman07961cd2008-02-25 21:11:39 +00001515 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Bill Wendlingceba88e2009-01-30 02:57:00 +00001516 return DAG.getNode(ISD::UREM, N->getDebugLoc(), VT, N0, N1);
Chris Lattnerce602f52008-01-27 23:21:58 +00001517 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001518
Dan Gohmanfdb31f12007-11-26 23:46:11 +00001519 // If X/C can be simplified by the division-by-constant logic, lower
1520 // X%C to the equivalent of X-X/C*C.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001521 if (N1C && !N1C->isNullValue()) {
Bill Wendlingceba88e2009-01-30 02:57:00 +00001522 SDValue Div = DAG.getNode(ISD::SDIV, N->getDebugLoc(), VT, N0, N1);
Gabor Greif1c80d112008-08-28 21:40:38 +00001523 AddToWorkList(Div.getNode());
1524 SDValue OptimizedDiv = combine(Div.getNode());
1525 if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) {
Bill Wendlingceba88e2009-01-30 02:57:00 +00001526 SDValue Mul = DAG.getNode(ISD::MUL, N->getDebugLoc(), VT,
1527 OptimizedDiv, N1);
1528 SDValue Sub = DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N0, Mul);
Gabor Greif1c80d112008-08-28 21:40:38 +00001529 AddToWorkList(Mul.getNode());
Dan Gohmanfdb31f12007-11-26 23:46:11 +00001530 return Sub;
1531 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001532 }
1533
1534 // undef % X -> 0
1535 if (N0.getOpcode() == ISD::UNDEF)
1536 return DAG.getConstant(0, VT);
1537 // X % undef -> undef
1538 if (N1.getOpcode() == ISD::UNDEF)
1539 return N1;
1540
Dan Gohman8181bd12008-07-27 21:46:04 +00001541 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001542}
1543
Dan Gohman8181bd12008-07-27 21:46:04 +00001544SDValue DAGCombiner::visitUREM(SDNode *N) {
1545 SDValue N0 = N->getOperand(0);
1546 SDValue N1 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001547 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1548 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands92c43912008-06-06 12:08:01 +00001549 MVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001550
1551 // fold (urem c1, c2) -> c1%c2
1552 if (N0C && N1C && !N1C->isNullValue())
Bill Wendling0445c4c2008-09-24 10:25:02 +00001553 return DAG.FoldConstantArithmetic(ISD::UREM, VT, N0C, N1C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001554 // fold (urem x, pow2) -> (and x, pow2-1)
Dan Gohman9d24dc72008-03-13 22:13:53 +00001555 if (N1C && !N1C->isNullValue() && N1C->getAPIntValue().isPowerOf2())
Bill Wendlingceba88e2009-01-30 02:57:00 +00001556 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0,
Dan Gohman9d24dc72008-03-13 22:13:53 +00001557 DAG.getConstant(N1C->getAPIntValue()-1,VT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001558 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
1559 if (N1.getOpcode() == ISD::SHL) {
1560 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman9d24dc72008-03-13 22:13:53 +00001561 if (SHC->getAPIntValue().isPowerOf2()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001562 SDValue Add =
Bill Wendlingceba88e2009-01-30 02:57:00 +00001563 DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N1,
Duncan Sands92c43912008-06-06 12:08:01 +00001564 DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()),
Dan Gohman9d24dc72008-03-13 22:13:53 +00001565 VT));
Gabor Greif1c80d112008-08-28 21:40:38 +00001566 AddToWorkList(Add.getNode());
Bill Wendlingceba88e2009-01-30 02:57:00 +00001567 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0, Add);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001568 }
1569 }
1570 }
1571
Dan Gohmanfdb31f12007-11-26 23:46:11 +00001572 // If X/C can be simplified by the division-by-constant logic, lower
1573 // X%C to the equivalent of X-X/C*C.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001574 if (N1C && !N1C->isNullValue()) {
Bill Wendlingceba88e2009-01-30 02:57:00 +00001575 SDValue Div = DAG.getNode(ISD::UDIV, N->getDebugLoc(), VT, N0, N1);
Dan Gohman2e1517f2008-09-08 16:59:01 +00001576 AddToWorkList(Div.getNode());
Gabor Greif1c80d112008-08-28 21:40:38 +00001577 SDValue OptimizedDiv = combine(Div.getNode());
1578 if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) {
Bill Wendlingceba88e2009-01-30 02:57:00 +00001579 SDValue Mul = DAG.getNode(ISD::MUL, N->getDebugLoc(), VT,
1580 OptimizedDiv, N1);
1581 SDValue Sub = DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N0, Mul);
Gabor Greif1c80d112008-08-28 21:40:38 +00001582 AddToWorkList(Mul.getNode());
Dan Gohmanfdb31f12007-11-26 23:46:11 +00001583 return Sub;
1584 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001585 }
1586
1587 // undef % X -> 0
1588 if (N0.getOpcode() == ISD::UNDEF)
1589 return DAG.getConstant(0, VT);
1590 // X % undef -> undef
1591 if (N1.getOpcode() == ISD::UNDEF)
1592 return N1;
1593
Dan Gohman8181bd12008-07-27 21:46:04 +00001594 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001595}
1596
Dan Gohman8181bd12008-07-27 21:46:04 +00001597SDValue DAGCombiner::visitMULHS(SDNode *N) {
1598 SDValue N0 = N->getOperand(0);
1599 SDValue N1 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001600 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands92c43912008-06-06 12:08:01 +00001601 MVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001602
1603 // fold (mulhs x, 0) -> 0
1604 if (N1C && N1C->isNullValue())
1605 return N1;
1606 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Dan Gohman9d24dc72008-03-13 22:13:53 +00001607 if (N1C && N1C->getAPIntValue() == 1)
Bill Wendlingff9beb92009-01-30 03:00:18 +00001608 return DAG.getNode(ISD::SRA, N->getDebugLoc(), N0.getValueType(), N0,
1609 DAG.getConstant(N0.getValueType().getSizeInBits() - 1,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001610 TLI.getShiftAmountTy()));
1611 // fold (mulhs x, undef) -> 0
1612 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
1613 return DAG.getConstant(0, VT);
1614
Dan Gohman8181bd12008-07-27 21:46:04 +00001615 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001616}
1617
Dan Gohman8181bd12008-07-27 21:46:04 +00001618SDValue DAGCombiner::visitMULHU(SDNode *N) {
1619 SDValue N0 = N->getOperand(0);
1620 SDValue N1 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001621 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands92c43912008-06-06 12:08:01 +00001622 MVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001623
1624 // fold (mulhu x, 0) -> 0
1625 if (N1C && N1C->isNullValue())
1626 return N1;
1627 // fold (mulhu x, 1) -> 0
Dan Gohman9d24dc72008-03-13 22:13:53 +00001628 if (N1C && N1C->getAPIntValue() == 1)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001629 return DAG.getConstant(0, N0.getValueType());
1630 // fold (mulhu x, undef) -> 0
1631 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
1632 return DAG.getConstant(0, VT);
1633
Dan Gohman8181bd12008-07-27 21:46:04 +00001634 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001635}
1636
Dan Gohman6c89ea72007-10-08 17:57:15 +00001637/// SimplifyNodeWithTwoResults - Perform optimizations common to nodes that
1638/// compute two values. LoOp and HiOp give the opcodes for the two computations
1639/// that are being performed. Return true if a simplification was made.
1640///
Dan Gohman8181bd12008-07-27 21:46:04 +00001641SDValue DAGCombiner::SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
1642 unsigned HiOp) {
Dan Gohman6c89ea72007-10-08 17:57:15 +00001643 // If the high half is not needed, just compute the low half.
Evan Chengddfa8c72007-11-08 09:25:29 +00001644 bool HiExists = N->hasAnyUseOfValue(1);
1645 if (!HiExists &&
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00001646 (!LegalOperations ||
Dan Gohman6c89ea72007-10-08 17:57:15 +00001647 TLI.isOperationLegal(LoOp, N->getValueType(0)))) {
Bill Wendling24ecb1b2009-01-30 03:08:40 +00001648 SDValue Res = DAG.getNode(LoOp, N->getDebugLoc(), N->getValueType(0),
1649 N->op_begin(), N->getNumOperands());
Chris Lattner4a7c8452008-01-26 01:09:19 +00001650 return CombineTo(N, Res, Res);
Dan Gohman6c89ea72007-10-08 17:57:15 +00001651 }
1652
1653 // If the low half is not needed, just compute the high half.
Evan Chengddfa8c72007-11-08 09:25:29 +00001654 bool LoExists = N->hasAnyUseOfValue(0);
1655 if (!LoExists &&
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00001656 (!LegalOperations ||
Dan Gohman6c89ea72007-10-08 17:57:15 +00001657 TLI.isOperationLegal(HiOp, N->getValueType(1)))) {
Bill Wendling24ecb1b2009-01-30 03:08:40 +00001658 SDValue Res = DAG.getNode(HiOp, N->getDebugLoc(), N->getValueType(1),
1659 N->op_begin(), N->getNumOperands());
Chris Lattner4a7c8452008-01-26 01:09:19 +00001660 return CombineTo(N, Res, Res);
Dan Gohman6c89ea72007-10-08 17:57:15 +00001661 }
1662
Evan Chengddfa8c72007-11-08 09:25:29 +00001663 // If both halves are used, return as it is.
1664 if (LoExists && HiExists)
Dan Gohman8181bd12008-07-27 21:46:04 +00001665 return SDValue();
Evan Chengddfa8c72007-11-08 09:25:29 +00001666
1667 // If the two computed results can be simplified separately, separate them.
Evan Chengddfa8c72007-11-08 09:25:29 +00001668 if (LoExists) {
Bill Wendling24ecb1b2009-01-30 03:08:40 +00001669 SDValue Lo = DAG.getNode(LoOp, N->getDebugLoc(), N->getValueType(0),
1670 N->op_begin(), N->getNumOperands());
Gabor Greif1c80d112008-08-28 21:40:38 +00001671 AddToWorkList(Lo.getNode());
1672 SDValue LoOpt = combine(Lo.getNode());
1673 if (LoOpt.getNode() && LoOpt.getNode() != Lo.getNode() &&
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00001674 (!LegalOperations ||
Duncan Sands2418bec2008-06-13 19:07:40 +00001675 TLI.isOperationLegal(LoOpt.getOpcode(), LoOpt.getValueType())))
Chris Lattner4a7c8452008-01-26 01:09:19 +00001676 return CombineTo(N, LoOpt, LoOpt);
Dan Gohman6c89ea72007-10-08 17:57:15 +00001677 }
1678
Evan Chengddfa8c72007-11-08 09:25:29 +00001679 if (HiExists) {
Bill Wendling24ecb1b2009-01-30 03:08:40 +00001680 SDValue Hi = DAG.getNode(HiOp, N->getDebugLoc(), N->getValueType(1),
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00001681 N->op_begin(), N->getNumOperands());
Gabor Greif1c80d112008-08-28 21:40:38 +00001682 AddToWorkList(Hi.getNode());
1683 SDValue HiOpt = combine(Hi.getNode());
1684 if (HiOpt.getNode() && HiOpt != Hi &&
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00001685 (!LegalOperations ||
Duncan Sands2418bec2008-06-13 19:07:40 +00001686 TLI.isOperationLegal(HiOpt.getOpcode(), HiOpt.getValueType())))
Chris Lattner4a7c8452008-01-26 01:09:19 +00001687 return CombineTo(N, HiOpt, HiOpt);
Evan Chengddfa8c72007-11-08 09:25:29 +00001688 }
Bill Wendling24ecb1b2009-01-30 03:08:40 +00001689
Dan Gohman8181bd12008-07-27 21:46:04 +00001690 return SDValue();
Dan Gohman6c89ea72007-10-08 17:57:15 +00001691}
1692
Dan Gohman8181bd12008-07-27 21:46:04 +00001693SDValue DAGCombiner::visitSMUL_LOHI(SDNode *N) {
1694 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHS);
Gabor Greif1c80d112008-08-28 21:40:38 +00001695 if (Res.getNode()) return Res;
Dan Gohman6c89ea72007-10-08 17:57:15 +00001696
Dan Gohman8181bd12008-07-27 21:46:04 +00001697 return SDValue();
Dan Gohman6c89ea72007-10-08 17:57:15 +00001698}
1699
Dan Gohman8181bd12008-07-27 21:46:04 +00001700SDValue DAGCombiner::visitUMUL_LOHI(SDNode *N) {
1701 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHU);
Gabor Greif1c80d112008-08-28 21:40:38 +00001702 if (Res.getNode()) return Res;
Dan Gohman6c89ea72007-10-08 17:57:15 +00001703
Dan Gohman8181bd12008-07-27 21:46:04 +00001704 return SDValue();
Dan Gohman6c89ea72007-10-08 17:57:15 +00001705}
1706
Dan Gohman8181bd12008-07-27 21:46:04 +00001707SDValue DAGCombiner::visitSDIVREM(SDNode *N) {
1708 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::SDIV, ISD::SREM);
Gabor Greif1c80d112008-08-28 21:40:38 +00001709 if (Res.getNode()) return Res;
Dan Gohman6c89ea72007-10-08 17:57:15 +00001710
Dan Gohman8181bd12008-07-27 21:46:04 +00001711 return SDValue();
Dan Gohman6c89ea72007-10-08 17:57:15 +00001712}
1713
Dan Gohman8181bd12008-07-27 21:46:04 +00001714SDValue DAGCombiner::visitUDIVREM(SDNode *N) {
1715 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::UDIV, ISD::UREM);
Gabor Greif1c80d112008-08-28 21:40:38 +00001716 if (Res.getNode()) return Res;
Dan Gohman6c89ea72007-10-08 17:57:15 +00001717
Dan Gohman8181bd12008-07-27 21:46:04 +00001718 return SDValue();
Dan Gohman6c89ea72007-10-08 17:57:15 +00001719}
1720
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001721/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
1722/// two operands of the same opcode, try to simplify it.
Dan Gohman8181bd12008-07-27 21:46:04 +00001723SDValue DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
1724 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Duncan Sands92c43912008-06-06 12:08:01 +00001725 MVT VT = N0.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001726 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
1727
1728 // For each of OP in AND/OR/XOR:
1729 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
1730 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
1731 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
1732 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y))
1733 if ((N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND||
1734 N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::TRUNCATE) &&
1735 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
Bill Wendlingc93d72a2009-01-30 19:25:47 +00001736 SDValue ORNode = DAG.getNode(N->getOpcode(), N0.getDebugLoc(),
1737 N0.getOperand(0).getValueType(),
1738 N0.getOperand(0), N1.getOperand(0));
Gabor Greif1c80d112008-08-28 21:40:38 +00001739 AddToWorkList(ORNode.getNode());
Bill Wendlingc93d72a2009-01-30 19:25:47 +00001740 return DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT, ORNode);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001741 }
1742
1743 // For each of OP in SHL/SRL/SRA/AND...
1744 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
1745 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
1746 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
1747 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
1748 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
1749 N0.getOperand(1) == N1.getOperand(1)) {
Bill Wendlingc93d72a2009-01-30 19:25:47 +00001750 SDValue ORNode = DAG.getNode(N->getOpcode(), N0.getDebugLoc(),
1751 N0.getOperand(0).getValueType(),
1752 N0.getOperand(0), N1.getOperand(0));
Gabor Greif1c80d112008-08-28 21:40:38 +00001753 AddToWorkList(ORNode.getNode());
Bill Wendlingc93d72a2009-01-30 19:25:47 +00001754 return DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT,
1755 ORNode, N0.getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001756 }
1757
Dan Gohman8181bd12008-07-27 21:46:04 +00001758 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001759}
1760
Dan Gohman8181bd12008-07-27 21:46:04 +00001761SDValue DAGCombiner::visitAND(SDNode *N) {
1762 SDValue N0 = N->getOperand(0);
1763 SDValue N1 = N->getOperand(1);
1764 SDValue LL, LR, RL, RR, CC0, CC1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001765 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1766 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands92c43912008-06-06 12:08:01 +00001767 MVT VT = N1.getValueType();
1768 unsigned BitWidth = VT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001769
1770 // fold vector ops
Duncan Sands92c43912008-06-06 12:08:01 +00001771 if (VT.isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001772 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00001773 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001774 }
1775
1776 // fold (and x, undef) -> 0
1777 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
1778 return DAG.getConstant(0, VT);
1779 // fold (and c1, c2) -> c1&c2
1780 if (N0C && N1C)
Bill Wendling0445c4c2008-09-24 10:25:02 +00001781 return DAG.FoldConstantArithmetic(ISD::AND, VT, N0C, N1C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001782 // canonicalize constant to RHS
1783 if (N0C && !N1C)
1784 return DAG.getNode(ISD::AND, VT, N1, N0);
1785 // fold (and x, -1) -> x
1786 if (N1C && N1C->isAllOnesValue())
1787 return N0;
1788 // if (and x, c) is known to be zero, return 0
Dan Gohman8181bd12008-07-27 21:46:04 +00001789 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman07961cd2008-02-25 21:11:39 +00001790 APInt::getAllOnesValue(BitWidth)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001791 return DAG.getConstant(0, VT);
1792 // reassociate and
Bill Wendlingabb33a22009-01-30 00:45:56 +00001793 SDValue RAND = ReassociateOps(ISD::AND, N->getDebugLoc(), N0, N1);
Gabor Greif1c80d112008-08-28 21:40:38 +00001794 if (RAND.getNode() != 0)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001795 return RAND;
1796 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
1797 if (N1C && N0.getOpcode() == ISD::OR)
1798 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohman9d24dc72008-03-13 22:13:53 +00001799 if ((ORI->getAPIntValue() & N1C->getAPIntValue()) == N1C->getAPIntValue())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001800 return N1;
1801 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
1802 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001803 SDValue N0Op0 = N0.getOperand(0);
Dan Gohman07961cd2008-02-25 21:11:39 +00001804 APInt Mask = ~N1C->getAPIntValue();
1805 Mask.trunc(N0Op0.getValueSizeInBits());
1806 if (DAG.MaskedValueIsZero(N0Op0, Mask)) {
Bill Wendlingd32f8522009-01-30 20:43:18 +00001807 SDValue Zext = DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(),
1808 N0.getValueType(), N0Op0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001809
1810 // Replace uses of the AND with uses of the Zero extend node.
1811 CombineTo(N, Zext);
1812
1813 // We actually want to replace all uses of the any_extend with the
1814 // zero_extend, to avoid duplicating things. This will later cause this
1815 // AND to be folded.
Gabor Greif1c80d112008-08-28 21:40:38 +00001816 CombineTo(N0.getNode(), Zext);
Dan Gohman8181bd12008-07-27 21:46:04 +00001817 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001818 }
1819 }
1820 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
1821 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1822 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1823 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1824
1825 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands92c43912008-06-06 12:08:01 +00001826 LL.getValueType().isInteger()) {
Bill Wendlingd32f8522009-01-30 20:43:18 +00001827 // fold (and (seteq X, 0), (seteq Y, 0)) -> (seteq (or X, Y), 0)
Dan Gohman9d24dc72008-03-13 22:13:53 +00001828 if (cast<ConstantSDNode>(LR)->isNullValue() && Op1 == ISD::SETEQ) {
Bill Wendlingd32f8522009-01-30 20:43:18 +00001829 SDValue ORNode = DAG.getNode(ISD::OR, N0.getDebugLoc(),
1830 LR.getValueType(), LL, RL);
Gabor Greif1c80d112008-08-28 21:40:38 +00001831 AddToWorkList(ORNode.getNode());
Bill Wendlingd32f8522009-01-30 20:43:18 +00001832 return DAG.getSetCC(N->getDebugLoc(), VT, ORNode, LR, Op1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001833 }
Bill Wendlingd32f8522009-01-30 20:43:18 +00001834 // fold (and (seteq X, -1), (seteq Y, -1)) -> (seteq (and X, Y), -1)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001835 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
Bill Wendlingd32f8522009-01-30 20:43:18 +00001836 SDValue ANDNode = DAG.getNode(ISD::AND, N0.getDebugLoc(),
1837 LR.getValueType(), LL, RL);
Gabor Greif1c80d112008-08-28 21:40:38 +00001838 AddToWorkList(ANDNode.getNode());
Bill Wendlingd32f8522009-01-30 20:43:18 +00001839 return DAG.getSetCC(N->getDebugLoc(), VT, ANDNode, LR, Op1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001840 }
Bill Wendlingd32f8522009-01-30 20:43:18 +00001841 // fold (and (setgt X, -1), (setgt Y, -1)) -> (setgt (or X, Y), -1)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001842 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
Bill Wendlingd32f8522009-01-30 20:43:18 +00001843 SDValue ORNode = DAG.getNode(ISD::OR, N0.getDebugLoc(),
1844 LR.getValueType(), LL, RL);
Gabor Greif1c80d112008-08-28 21:40:38 +00001845 AddToWorkList(ORNode.getNode());
Bill Wendlingd32f8522009-01-30 20:43:18 +00001846 return DAG.getSetCC(N->getDebugLoc(), VT, ORNode, LR, Op1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001847 }
1848 }
1849 // canonicalize equivalent to ll == rl
1850 if (LL == RR && LR == RL) {
1851 Op1 = ISD::getSetCCSwappedOperands(Op1);
1852 std::swap(RL, RR);
1853 }
1854 if (LL == RL && LR == RR) {
Duncan Sands92c43912008-06-06 12:08:01 +00001855 bool isInteger = LL.getValueType().isInteger();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001856 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
Chris Lattner3ab74bd2008-10-28 07:11:07 +00001857 if (Result != ISD::SETCC_INVALID &&
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00001858 (!LegalOperations || TLI.isCondCodeLegal(Result, LL.getValueType())))
Bill Wendlingd32f8522009-01-30 20:43:18 +00001859 return DAG.getSetCC(N->getDebugLoc(), N0.getValueType(),
1860 LL, LR, Result);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001861 }
1862 }
1863
Bill Wendlingd32f8522009-01-30 20:43:18 +00001864 // Simplify: (and (op x...), (op y...)) -> (op (and x, y))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001865 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001866 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00001867 if (Tmp.getNode()) return Tmp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001868 }
1869
1870 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
1871 // fold (and (sra)) -> (and (srl)) when possible.
Duncan Sands92c43912008-06-06 12:08:01 +00001872 if (!VT.isVector() &&
Dan Gohman8181bd12008-07-27 21:46:04 +00001873 SimplifyDemandedBits(SDValue(N, 0)))
1874 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001875 // fold (zext_inreg (extload x)) -> (zextload x)
Gabor Greif1c80d112008-08-28 21:40:38 +00001876 if (ISD::isEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001877 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands92c43912008-06-06 12:08:01 +00001878 MVT EVT = LN0->getMemoryVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001879 // If we zero all the possible extended bits, then we can turn this into
1880 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman07961cd2008-02-25 21:11:39 +00001881 unsigned BitWidth = N1.getValueSizeInBits();
1882 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Duncan Sands92c43912008-06-06 12:08:01 +00001883 BitWidth - EVT.getSizeInBits())) &&
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00001884 ((!LegalOperations && !LN0->isVolatile()) ||
Evan Cheng08c171a2008-10-14 21:26:46 +00001885 TLI.isLoadExtLegal(ISD::ZEXTLOAD, EVT))) {
Bill Wendlingd32f8522009-01-30 20:43:18 +00001886 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, N0.getDebugLoc(), VT,
1887 LN0->getChain(), LN0->getBasePtr(),
1888 LN0->getSrcValue(),
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00001889 LN0->getSrcValueOffset(), EVT,
1890 LN0->isVolatile(), LN0->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001891 AddToWorkList(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00001892 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00001893 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001894 }
1895 }
1896 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Gabor Greif1c80d112008-08-28 21:40:38 +00001897 if (ISD::isSEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001898 N0.hasOneUse()) {
1899 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands92c43912008-06-06 12:08:01 +00001900 MVT EVT = LN0->getMemoryVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001901 // If we zero all the possible extended bits, then we can turn this into
1902 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman07961cd2008-02-25 21:11:39 +00001903 unsigned BitWidth = N1.getValueSizeInBits();
1904 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Duncan Sands92c43912008-06-06 12:08:01 +00001905 BitWidth - EVT.getSizeInBits())) &&
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00001906 ((!LegalOperations && !LN0->isVolatile()) ||
Evan Cheng08c171a2008-10-14 21:26:46 +00001907 TLI.isLoadExtLegal(ISD::ZEXTLOAD, EVT))) {
Bill Wendlingd32f8522009-01-30 20:43:18 +00001908 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, N0.getDebugLoc(), VT,
1909 LN0->getChain(),
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00001910 LN0->getBasePtr(), LN0->getSrcValue(),
1911 LN0->getSrcValueOffset(), EVT,
1912 LN0->isVolatile(), LN0->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001913 AddToWorkList(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00001914 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00001915 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001916 }
1917 }
1918
1919 // fold (and (load x), 255) -> (zextload x, i8)
1920 // fold (and (extload x, i16), 255) -> (zextload x, i8)
1921 if (N1C && N0.getOpcode() == ISD::LOAD) {
1922 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
1923 if (LN0->getExtensionType() != ISD::SEXTLOAD &&
Duncan Sands2418bec2008-06-13 19:07:40 +00001924 LN0->isUnindexed() && N0.hasOneUse() &&
1925 // Do not change the width of a volatile load.
1926 !LN0->isVolatile()) {
Duncan Sands6a437fb2008-06-09 11:32:28 +00001927 MVT EVT = MVT::Other;
1928 uint32_t ActiveBits = N1C->getAPIntValue().getActiveBits();
1929 if (ActiveBits > 0 && APIntOps::isMask(ActiveBits, N1C->getAPIntValue()))
1930 EVT = MVT::getIntegerVT(ActiveBits);
1931
1932 MVT LoadedVT = LN0->getMemoryVT();
Bill Wendlingd32f8522009-01-30 20:43:18 +00001933
Duncan Sands3ea93352008-06-16 08:14:38 +00001934 // Do not generate loads of non-round integer types since these can
1935 // be expensive (and would be wrong if the type is not byte sized).
1936 if (EVT != MVT::Other && LoadedVT.bitsGT(EVT) && EVT.isRound() &&
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00001937 (!LegalOperations || TLI.isLoadExtLegal(ISD::ZEXTLOAD, EVT))) {
Duncan Sands92c43912008-06-06 12:08:01 +00001938 MVT PtrType = N0.getOperand(1).getValueType();
Bill Wendlingd32f8522009-01-30 20:43:18 +00001939
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001940 // For big endian targets, we need to add an offset to the pointer to
1941 // load the correct bytes. For little endian systems, we merely need to
1942 // read fewer bytes from the same pointer.
Duncan Sands92c43912008-06-06 12:08:01 +00001943 unsigned LVTStoreBytes = LoadedVT.getStoreSizeInBits()/8;
1944 unsigned EVTStoreBytes = EVT.getStoreSizeInBits()/8;
Duncan Sands4f18d4f2007-11-09 08:57:19 +00001945 unsigned PtrOff = LVTStoreBytes - EVTStoreBytes;
Duncan Sandsa3691432007-10-28 12:59:45 +00001946 unsigned Alignment = LN0->getAlignment();
Dan Gohman8181bd12008-07-27 21:46:04 +00001947 SDValue NewPtr = LN0->getBasePtr();
Bill Wendlingd32f8522009-01-30 20:43:18 +00001948
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00001949 if (TLI.isBigEndian()) {
Bill Wendlingd32f8522009-01-30 20:43:18 +00001950 NewPtr = DAG.getNode(ISD::ADD, DebugLoc::getUnknownLoc(), PtrType,
1951 NewPtr, DAG.getConstant(PtrOff, PtrType));
Duncan Sandsa3691432007-10-28 12:59:45 +00001952 Alignment = MinAlign(Alignment, PtrOff);
1953 }
Bill Wendlingd32f8522009-01-30 20:43:18 +00001954
Gabor Greif1c80d112008-08-28 21:40:38 +00001955 AddToWorkList(NewPtr.getNode());
Dan Gohman8181bd12008-07-27 21:46:04 +00001956 SDValue Load =
Bill Wendlingd32f8522009-01-30 20:43:18 +00001957 DAG.getExtLoad(ISD::ZEXTLOAD, LN0->getDebugLoc(), VT, LN0->getChain(),
1958 NewPtr, LN0->getSrcValue(), LN0->getSrcValueOffset(),
1959 EVT, LN0->isVolatile(), Alignment);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001960 AddToWorkList(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00001961 CombineTo(N0.getNode(), Load, Load.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00001962 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001963 }
1964 }
1965 }
1966
Dan Gohman8181bd12008-07-27 21:46:04 +00001967 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001968}
1969
Dan Gohman8181bd12008-07-27 21:46:04 +00001970SDValue DAGCombiner::visitOR(SDNode *N) {
1971 SDValue N0 = N->getOperand(0);
1972 SDValue N1 = N->getOperand(1);
1973 SDValue LL, LR, RL, RR, CC0, CC1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001974 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1975 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands92c43912008-06-06 12:08:01 +00001976 MVT VT = N1.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001977
1978 // fold vector ops
Duncan Sands92c43912008-06-06 12:08:01 +00001979 if (VT.isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001980 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00001981 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001982 }
1983
1984 // fold (or x, undef) -> -1
1985 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
1986 return DAG.getConstant(~0ULL, VT);
1987 // fold (or c1, c2) -> c1|c2
1988 if (N0C && N1C)
Bill Wendling0445c4c2008-09-24 10:25:02 +00001989 return DAG.FoldConstantArithmetic(ISD::OR, VT, N0C, N1C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001990 // canonicalize constant to RHS
1991 if (N0C && !N1C)
Bill Wendling43f24b92009-01-30 20:59:34 +00001992 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N1, N0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001993 // fold (or x, 0) -> x
1994 if (N1C && N1C->isNullValue())
1995 return N0;
1996 // fold (or x, -1) -> -1
1997 if (N1C && N1C->isAllOnesValue())
1998 return N1;
1999 // fold (or x, c) -> c iff (x & ~c) == 0
Dan Gohman07961cd2008-02-25 21:11:39 +00002000 if (N1C && DAG.MaskedValueIsZero(N0, ~N1C->getAPIntValue()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002001 return N1;
2002 // reassociate or
Bill Wendlingabb33a22009-01-30 00:45:56 +00002003 SDValue ROR = ReassociateOps(ISD::OR, N->getDebugLoc(), N0, N1);
Gabor Greif1c80d112008-08-28 21:40:38 +00002004 if (ROR.getNode() != 0)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002005 return ROR;
2006 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
Gabor Greif1c80d112008-08-28 21:40:38 +00002007 if (N1C && N0.getOpcode() == ISD::AND && N0.getNode()->hasOneUse() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002008 isa<ConstantSDNode>(N0.getOperand(1))) {
2009 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
Bill Wendling43f24b92009-01-30 20:59:34 +00002010 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
2011 DAG.getNode(ISD::OR, N0.getDebugLoc(), VT,
2012 N0.getOperand(0), N1),
2013 DAG.FoldConstantArithmetic(ISD::OR, VT, N1C, C1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002014 }
2015 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
2016 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
2017 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
2018 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
2019
2020 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands92c43912008-06-06 12:08:01 +00002021 LL.getValueType().isInteger()) {
Bill Wendling43f24b92009-01-30 20:59:34 +00002022 // fold (or (setne X, 0), (setne Y, 0)) -> (setne (or X, Y), 0)
2023 // fold (or (setlt X, 0), (setlt Y, 0)) -> (setne (or X, Y), 0)
Dan Gohman9d24dc72008-03-13 22:13:53 +00002024 if (cast<ConstantSDNode>(LR)->isNullValue() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002025 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
Bill Wendling43f24b92009-01-30 20:59:34 +00002026 SDValue ORNode = DAG.getNode(ISD::OR, LR.getDebugLoc(),
2027 LR.getValueType(), LL, RL);
Gabor Greif1c80d112008-08-28 21:40:38 +00002028 AddToWorkList(ORNode.getNode());
Bill Wendling43f24b92009-01-30 20:59:34 +00002029 return DAG.getSetCC(N->getDebugLoc(), VT, ORNode, LR, Op1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002030 }
Bill Wendling43f24b92009-01-30 20:59:34 +00002031 // fold (or (setne X, -1), (setne Y, -1)) -> (setne (and X, Y), -1)
2032 // fold (or (setgt X, -1), (setgt Y -1)) -> (setgt (and X, Y), -1)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002033 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
2034 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
Bill Wendling43f24b92009-01-30 20:59:34 +00002035 SDValue ANDNode = DAG.getNode(ISD::AND, LR.getDebugLoc(),
2036 LR.getValueType(), LL, RL);
Gabor Greif1c80d112008-08-28 21:40:38 +00002037 AddToWorkList(ANDNode.getNode());
Bill Wendling43f24b92009-01-30 20:59:34 +00002038 return DAG.getSetCC(N->getDebugLoc(), VT, ANDNode, LR, Op1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002039 }
2040 }
2041 // canonicalize equivalent to ll == rl
2042 if (LL == RR && LR == RL) {
2043 Op1 = ISD::getSetCCSwappedOperands(Op1);
2044 std::swap(RL, RR);
2045 }
2046 if (LL == RL && LR == RR) {
Duncan Sands92c43912008-06-06 12:08:01 +00002047 bool isInteger = LL.getValueType().isInteger();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002048 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
Chris Lattner3ab74bd2008-10-28 07:11:07 +00002049 if (Result != ISD::SETCC_INVALID &&
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00002050 (!LegalOperations || TLI.isCondCodeLegal(Result, LL.getValueType())))
Bill Wendling43f24b92009-01-30 20:59:34 +00002051 return DAG.getSetCC(N->getDebugLoc(), N0.getValueType(),
2052 LL, LR, Result);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002053 }
2054 }
2055
Bill Wendling43f24b92009-01-30 20:59:34 +00002056 // Simplify: (or (op x...), (op y...)) -> (op (or x, y))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002057 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002058 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00002059 if (Tmp.getNode()) return Tmp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002060 }
2061
Bill Wendling43f24b92009-01-30 20:59:34 +00002062 // (or (and X, C1), (and Y, C2)) -> (and (or X, Y), C3) if possible.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002063 if (N0.getOpcode() == ISD::AND &&
2064 N1.getOpcode() == ISD::AND &&
2065 N0.getOperand(1).getOpcode() == ISD::Constant &&
2066 N1.getOperand(1).getOpcode() == ISD::Constant &&
2067 // Don't increase # computations.
Gabor Greif1c80d112008-08-28 21:40:38 +00002068 (N0.getNode()->hasOneUse() || N1.getNode()->hasOneUse())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002069 // We can only do this xform if we know that bits from X that are set in C2
2070 // but not in C1 are already zero. Likewise for Y.
Dan Gohman07961cd2008-02-25 21:11:39 +00002071 const APInt &LHSMask =
2072 cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
2073 const APInt &RHSMask =
2074 cast<ConstantSDNode>(N1.getOperand(1))->getAPIntValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002075
2076 if (DAG.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) &&
2077 DAG.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) {
Bill Wendling43f24b92009-01-30 20:59:34 +00002078 SDValue X = DAG.getNode(ISD::OR, N0.getDebugLoc(), VT,
2079 N0.getOperand(0), N1.getOperand(0));
2080 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, X,
2081 DAG.getConstant(LHSMask | RHSMask, VT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002082 }
2083 }
2084
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002085 // See if this is some rotate idiom.
Bill Wendling2e1865c2009-01-30 21:14:50 +00002086 if (SDNode *Rot = MatchRotate(N0, N1, N->getDebugLoc()))
Dan Gohman8181bd12008-07-27 21:46:04 +00002087 return SDValue(Rot, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002088
Dan Gohman8181bd12008-07-27 21:46:04 +00002089 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002090}
2091
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002092/// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present.
Dan Gohman8181bd12008-07-27 21:46:04 +00002093static bool MatchRotateHalf(SDValue Op, SDValue &Shift, SDValue &Mask) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002094 if (Op.getOpcode() == ISD::AND) {
2095 if (isa<ConstantSDNode>(Op.getOperand(1))) {
2096 Mask = Op.getOperand(1);
2097 Op = Op.getOperand(0);
2098 } else {
2099 return false;
2100 }
2101 }
2102
2103 if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) {
2104 Shift = Op;
2105 return true;
2106 }
Bill Wendling43f24b92009-01-30 20:59:34 +00002107
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002108 return false;
2109}
2110
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002111// MatchRotate - Handle an 'or' of two operands. If this is one of the many
2112// idioms for rotate, and if the target supports rotation instructions, generate
2113// a rot[lr].
Bill Wendling2e1865c2009-01-30 21:14:50 +00002114SDNode *DAGCombiner::MatchRotate(SDValue LHS, SDValue RHS, DebugLoc DL) {
Duncan Sands2418bec2008-06-13 19:07:40 +00002115 // Must be a legal type. Expanded 'n promoted things won't work with rotates.
Duncan Sands92c43912008-06-06 12:08:01 +00002116 MVT VT = LHS.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002117 if (!TLI.isTypeLegal(VT)) return 0;
2118
2119 // The target must have at least one rotate flavor.
Dan Gohman52c51aa2009-01-28 17:46:25 +00002120 bool HasROTL = TLI.isOperationLegalOrCustom(ISD::ROTL, VT);
2121 bool HasROTR = TLI.isOperationLegalOrCustom(ISD::ROTR, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002122 if (!HasROTL && !HasROTR) return 0;
Duncan Sands2418bec2008-06-13 19:07:40 +00002123
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002124 // Match "(X shl/srl V1) & V2" where V2 may not be present.
Dan Gohman8181bd12008-07-27 21:46:04 +00002125 SDValue LHSShift; // The shift.
2126 SDValue LHSMask; // AND value if any.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002127 if (!MatchRotateHalf(LHS, LHSShift, LHSMask))
2128 return 0; // Not part of a rotate.
2129
Dan Gohman8181bd12008-07-27 21:46:04 +00002130 SDValue RHSShift; // The shift.
2131 SDValue RHSMask; // AND value if any.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002132 if (!MatchRotateHalf(RHS, RHSShift, RHSMask))
2133 return 0; // Not part of a rotate.
2134
2135 if (LHSShift.getOperand(0) != RHSShift.getOperand(0))
2136 return 0; // Not shifting the same value.
2137
2138 if (LHSShift.getOpcode() == RHSShift.getOpcode())
2139 return 0; // Shifts must disagree.
2140
2141 // Canonicalize shl to left side in a shl/srl pair.
2142 if (RHSShift.getOpcode() == ISD::SHL) {
2143 std::swap(LHS, RHS);
2144 std::swap(LHSShift, RHSShift);
2145 std::swap(LHSMask , RHSMask );
2146 }
2147
Duncan Sands92c43912008-06-06 12:08:01 +00002148 unsigned OpSizeInBits = VT.getSizeInBits();
Dan Gohman8181bd12008-07-27 21:46:04 +00002149 SDValue LHSShiftArg = LHSShift.getOperand(0);
2150 SDValue LHSShiftAmt = LHSShift.getOperand(1);
2151 SDValue RHSShiftAmt = RHSShift.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002152
2153 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
2154 // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2)
2155 if (LHSShiftAmt.getOpcode() == ISD::Constant &&
2156 RHSShiftAmt.getOpcode() == ISD::Constant) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002157 uint64_t LShVal = cast<ConstantSDNode>(LHSShiftAmt)->getZExtValue();
2158 uint64_t RShVal = cast<ConstantSDNode>(RHSShiftAmt)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002159 if ((LShVal + RShVal) != OpSizeInBits)
2160 return 0;
2161
Dan Gohman8181bd12008-07-27 21:46:04 +00002162 SDValue Rot;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002163 if (HasROTL)
Bill Wendling2e1865c2009-01-30 21:14:50 +00002164 Rot = DAG.getNode(ISD::ROTL, DL, VT, LHSShiftArg, LHSShiftAmt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002165 else
Bill Wendling2e1865c2009-01-30 21:14:50 +00002166 Rot = DAG.getNode(ISD::ROTR, DL, VT, LHSShiftArg, RHSShiftAmt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002167
2168 // If there is an AND of either shifted operand, apply it to the result.
Gabor Greif1c80d112008-08-28 21:40:38 +00002169 if (LHSMask.getNode() || RHSMask.getNode()) {
Dan Gohmand047c3e2008-03-03 23:51:38 +00002170 APInt Mask = APInt::getAllOnesValue(OpSizeInBits);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002171
Gabor Greif1c80d112008-08-28 21:40:38 +00002172 if (LHSMask.getNode()) {
Dan Gohmand047c3e2008-03-03 23:51:38 +00002173 APInt RHSBits = APInt::getLowBitsSet(OpSizeInBits, LShVal);
2174 Mask &= cast<ConstantSDNode>(LHSMask)->getAPIntValue() | RHSBits;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002175 }
Gabor Greif1c80d112008-08-28 21:40:38 +00002176 if (RHSMask.getNode()) {
Dan Gohmand047c3e2008-03-03 23:51:38 +00002177 APInt LHSBits = APInt::getHighBitsSet(OpSizeInBits, RShVal);
2178 Mask &= cast<ConstantSDNode>(RHSMask)->getAPIntValue() | LHSBits;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002179 }
2180
Bill Wendling2e1865c2009-01-30 21:14:50 +00002181 Rot = DAG.getNode(ISD::AND, DL, VT, Rot, DAG.getConstant(Mask, VT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002182 }
2183
Gabor Greif1c80d112008-08-28 21:40:38 +00002184 return Rot.getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002185 }
2186
2187 // If there is a mask here, and we have a variable shift, we can't be sure
2188 // that we're masking out the right stuff.
Gabor Greif1c80d112008-08-28 21:40:38 +00002189 if (LHSMask.getNode() || RHSMask.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002190 return 0;
2191
2192 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
2193 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotr x, (sub 32, y))
2194 if (RHSShiftAmt.getOpcode() == ISD::SUB &&
2195 LHSShiftAmt == RHSShiftAmt.getOperand(1)) {
2196 if (ConstantSDNode *SUBC =
2197 dyn_cast<ConstantSDNode>(RHSShiftAmt.getOperand(0))) {
Dan Gohman9d24dc72008-03-13 22:13:53 +00002198 if (SUBC->getAPIntValue() == OpSizeInBits) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002199 if (HasROTL)
Bill Wendling2e1865c2009-01-30 21:14:50 +00002200 return DAG.getNode(ISD::ROTL, DL, VT,
2201 LHSShiftArg, LHSShiftAmt).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002202 else
Bill Wendling2e1865c2009-01-30 21:14:50 +00002203 return DAG.getNode(ISD::ROTR, DL, VT,
2204 LHSShiftArg, RHSShiftAmt).getNode();
Anton Korobeynikov53422f62008-02-20 11:10:28 +00002205 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002206 }
2207 }
2208
2209 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
2210 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotl x, (sub 32, y))
2211 if (LHSShiftAmt.getOpcode() == ISD::SUB &&
2212 RHSShiftAmt == LHSShiftAmt.getOperand(1)) {
2213 if (ConstantSDNode *SUBC =
2214 dyn_cast<ConstantSDNode>(LHSShiftAmt.getOperand(0))) {
Dan Gohman9d24dc72008-03-13 22:13:53 +00002215 if (SUBC->getAPIntValue() == OpSizeInBits) {
Bill Wendling89f05f52008-08-31 01:13:31 +00002216 if (HasROTR)
Bill Wendling2e1865c2009-01-30 21:14:50 +00002217 return DAG.getNode(ISD::ROTR, DL, VT,
2218 LHSShiftArg, RHSShiftAmt).getNode();
Bill Wendling89f05f52008-08-31 01:13:31 +00002219 else
Bill Wendling2e1865c2009-01-30 21:14:50 +00002220 return DAG.getNode(ISD::ROTL, DL, VT,
2221 LHSShiftArg, LHSShiftAmt).getNode();
Anton Korobeynikov53422f62008-02-20 11:10:28 +00002222 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002223 }
2224 }
2225
Dan Gohman921581d2008-10-17 01:23:35 +00002226 // Look for sign/zext/any-extended or truncate cases:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002227 if ((LHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
2228 || LHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
Dan Gohman921581d2008-10-17 01:23:35 +00002229 || LHSShiftAmt.getOpcode() == ISD::ANY_EXTEND
2230 || LHSShiftAmt.getOpcode() == ISD::TRUNCATE) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002231 (RHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
2232 || RHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
Dan Gohman921581d2008-10-17 01:23:35 +00002233 || RHSShiftAmt.getOpcode() == ISD::ANY_EXTEND
2234 || RHSShiftAmt.getOpcode() == ISD::TRUNCATE)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002235 SDValue LExtOp0 = LHSShiftAmt.getOperand(0);
2236 SDValue RExtOp0 = RHSShiftAmt.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002237 if (RExtOp0.getOpcode() == ISD::SUB &&
2238 RExtOp0.getOperand(1) == LExtOp0) {
2239 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
Bill Wendlingf3dd7392008-08-31 00:37:27 +00002240 // (rotl x, y)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002241 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
Bill Wendlingf3dd7392008-08-31 00:37:27 +00002242 // (rotr x, (sub 32, y))
Dan Gohman921581d2008-10-17 01:23:35 +00002243 if (ConstantSDNode *SUBC =
2244 dyn_cast<ConstantSDNode>(RExtOp0.getOperand(0))) {
Dan Gohman9d24dc72008-03-13 22:13:53 +00002245 if (SUBC->getAPIntValue() == OpSizeInBits) {
Bill Wendling2e1865c2009-01-30 21:14:50 +00002246 return DAG.getNode(HasROTL ? ISD::ROTL : ISD::ROTR, DL, VT,
2247 LHSShiftArg,
Gabor Greifb420b9d2008-08-30 19:29:20 +00002248 HasROTL ? LHSShiftAmt : RHSShiftAmt).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002249 }
2250 }
2251 } else if (LExtOp0.getOpcode() == ISD::SUB &&
2252 RExtOp0 == LExtOp0.getOperand(1)) {
Bill Wendlinga70293d2008-08-31 01:04:56 +00002253 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext y))) ->
Bill Wendlingf3dd7392008-08-31 00:37:27 +00002254 // (rotr x, y)
Bill Wendlinga70293d2008-08-31 01:04:56 +00002255 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext y))) ->
Bill Wendlingf3dd7392008-08-31 00:37:27 +00002256 // (rotl x, (sub 32, y))
Dan Gohman921581d2008-10-17 01:23:35 +00002257 if (ConstantSDNode *SUBC =
2258 dyn_cast<ConstantSDNode>(LExtOp0.getOperand(0))) {
Dan Gohman9d24dc72008-03-13 22:13:53 +00002259 if (SUBC->getAPIntValue() == OpSizeInBits) {
Bill Wendling2e1865c2009-01-30 21:14:50 +00002260 return DAG.getNode(HasROTR ? ISD::ROTR : ISD::ROTL, DL, VT,
2261 LHSShiftArg,
Bill Wendlinga70293d2008-08-31 01:04:56 +00002262 HasROTR ? RHSShiftAmt : LHSShiftAmt).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002263 }
2264 }
2265 }
2266 }
2267
2268 return 0;
2269}
2270
Dan Gohman8181bd12008-07-27 21:46:04 +00002271SDValue DAGCombiner::visitXOR(SDNode *N) {
2272 SDValue N0 = N->getOperand(0);
2273 SDValue N1 = N->getOperand(1);
2274 SDValue LHS, RHS, CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002275 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2276 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands92c43912008-06-06 12:08:01 +00002277 MVT VT = N0.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002278
2279 // fold vector ops
Duncan Sands92c43912008-06-06 12:08:01 +00002280 if (VT.isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002281 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00002282 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002283 }
2284
Evan Cheng5d00cb42008-03-25 20:08:07 +00002285 // fold (xor undef, undef) -> 0. This is a common idiom (misuse).
2286 if (N0.getOpcode() == ISD::UNDEF && N1.getOpcode() == ISD::UNDEF)
2287 return DAG.getConstant(0, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002288 // fold (xor x, undef) -> undef
2289 if (N0.getOpcode() == ISD::UNDEF)
2290 return N0;
2291 if (N1.getOpcode() == ISD::UNDEF)
2292 return N1;
2293 // fold (xor c1, c2) -> c1^c2
2294 if (N0C && N1C)
Bill Wendling0445c4c2008-09-24 10:25:02 +00002295 return DAG.FoldConstantArithmetic(ISD::XOR, VT, N0C, N1C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002296 // canonicalize constant to RHS
2297 if (N0C && !N1C)
Bill Wendling2e1865c2009-01-30 21:14:50 +00002298 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT, N1, N0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002299 // fold (xor x, 0) -> x
2300 if (N1C && N1C->isNullValue())
2301 return N0;
2302 // reassociate xor
Bill Wendlingabb33a22009-01-30 00:45:56 +00002303 SDValue RXOR = ReassociateOps(ISD::XOR, N->getDebugLoc(), N0, N1);
Gabor Greif1c80d112008-08-28 21:40:38 +00002304 if (RXOR.getNode() != 0)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002305 return RXOR;
Bill Wendling0d810b82008-11-11 08:25:46 +00002306
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002307 // fold !(x cc y) -> (x !cc y)
Dan Gohman9d24dc72008-03-13 22:13:53 +00002308 if (N1C && N1C->getAPIntValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
Duncan Sands92c43912008-06-06 12:08:01 +00002309 bool isInt = LHS.getValueType().isInteger();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002310 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
2311 isInt);
Bill Wendling0d810b82008-11-11 08:25:46 +00002312
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00002313 if (!LegalOperations || TLI.isCondCodeLegal(NotCC, LHS.getValueType())) {
Bill Wendling0d810b82008-11-11 08:25:46 +00002314 switch (N0.getOpcode()) {
2315 default:
2316 assert(0 && "Unhandled SetCC Equivalent!");
2317 abort();
2318 case ISD::SETCC:
Bill Wendling2e1865c2009-01-30 21:14:50 +00002319 return DAG.getSetCC(N->getDebugLoc(), VT, LHS, RHS, NotCC);
Bill Wendling0d810b82008-11-11 08:25:46 +00002320 case ISD::SELECT_CC:
Bill Wendling2e1865c2009-01-30 21:14:50 +00002321 return DAG.getSelectCC(N->getDebugLoc(), LHS, RHS, N0.getOperand(2),
Bill Wendling0d810b82008-11-11 08:25:46 +00002322 N0.getOperand(3), NotCC);
2323 }
2324 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002325 }
Bill Wendling0d810b82008-11-11 08:25:46 +00002326
Chris Lattnere27cd502007-09-10 21:39:07 +00002327 // fold (not (zext (setcc x, y))) -> (zext (not (setcc x, y)))
Dan Gohman9d24dc72008-03-13 22:13:53 +00002328 if (N1C && N1C->getAPIntValue() == 1 && N0.getOpcode() == ISD::ZERO_EXTEND &&
Gabor Greifb420b9d2008-08-30 19:29:20 +00002329 N0.getNode()->hasOneUse() &&
2330 isSetCCEquivalent(N0.getOperand(0), LHS, RHS, CC)){
Dan Gohman8181bd12008-07-27 21:46:04 +00002331 SDValue V = N0.getOperand(0);
Bill Wendling2e1865c2009-01-30 21:14:50 +00002332 V = DAG.getNode(ISD::XOR, N0.getDebugLoc(), V.getValueType(), V,
Duncan Sandsbed21472007-10-10 09:54:50 +00002333 DAG.getConstant(1, V.getValueType()));
Gabor Greif1c80d112008-08-28 21:40:38 +00002334 AddToWorkList(V.getNode());
Bill Wendling2e1865c2009-01-30 21:14:50 +00002335 return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, V);
Chris Lattnere27cd502007-09-10 21:39:07 +00002336 }
2337
Bill Wendling2e1865c2009-01-30 21:14:50 +00002338 // fold (not (or x, y)) -> (and (not x), (not y)) iff x or y are setcc
Dan Gohman9d24dc72008-03-13 22:13:53 +00002339 if (N1C && N1C->getAPIntValue() == 1 && VT == MVT::i1 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002340 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002341 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002342 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
2343 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Bill Wendling2e1865c2009-01-30 21:14:50 +00002344 LHS = DAG.getNode(ISD::XOR, LHS.getDebugLoc(), VT, LHS, N1); // LHS = ~LHS
2345 RHS = DAG.getNode(ISD::XOR, RHS.getDebugLoc(), VT, RHS, N1); // RHS = ~RHS
Gabor Greif1c80d112008-08-28 21:40:38 +00002346 AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode());
Bill Wendling2e1865c2009-01-30 21:14:50 +00002347 return DAG.getNode(NewOpcode, N->getDebugLoc(), VT, LHS, RHS);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002348 }
2349 }
Bill Wendling2e1865c2009-01-30 21:14:50 +00002350 // fold (not (or x, y)) -> (and (not x), (not y)) iff x or y are constants
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002351 if (N1C && N1C->isAllOnesValue() &&
2352 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002353 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002354 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
2355 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Bill Wendling2e1865c2009-01-30 21:14:50 +00002356 LHS = DAG.getNode(ISD::XOR, LHS.getDebugLoc(), VT, LHS, N1); // LHS = ~LHS
2357 RHS = DAG.getNode(ISD::XOR, RHS.getDebugLoc(), VT, RHS, N1); // RHS = ~RHS
Gabor Greif1c80d112008-08-28 21:40:38 +00002358 AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode());
Bill Wendling2e1865c2009-01-30 21:14:50 +00002359 return DAG.getNode(NewOpcode, N->getDebugLoc(), VT, LHS, RHS);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002360 }
2361 }
Bill Wendling2e1865c2009-01-30 21:14:50 +00002362 // fold (xor (xor x, c1), c2) -> (xor x, (xor c1, c2))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002363 if (N1C && N0.getOpcode() == ISD::XOR) {
2364 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
2365 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2366 if (N00C)
Bill Wendling2e1865c2009-01-30 21:14:50 +00002367 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT, N0.getOperand(1),
2368 DAG.getConstant(N1C->getAPIntValue() ^
Dan Gohman9d24dc72008-03-13 22:13:53 +00002369 N00C->getAPIntValue(), VT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002370 if (N01C)
Bill Wendling2e1865c2009-01-30 21:14:50 +00002371 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT, N0.getOperand(0),
2372 DAG.getConstant(N1C->getAPIntValue() ^
Dan Gohman9d24dc72008-03-13 22:13:53 +00002373 N01C->getAPIntValue(), VT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002374 }
2375 // fold (xor x, x) -> 0
2376 if (N0 == N1) {
Duncan Sands92c43912008-06-06 12:08:01 +00002377 if (!VT.isVector()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002378 return DAG.getConstant(0, VT);
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00002379 } else if (!LegalOperations || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)){
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002380 // Produce a vector of zeros.
Dan Gohman8181bd12008-07-27 21:46:04 +00002381 SDValue El = DAG.getConstant(0, VT.getVectorElementType());
2382 std::vector<SDValue> Ops(VT.getVectorNumElements(), El);
Bill Wendling2e1865c2009-01-30 21:14:50 +00002383 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), VT,
2384 &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002385 }
2386 }
2387
2388 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
2389 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002390 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00002391 if (Tmp.getNode()) return Tmp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002392 }
2393
2394 // Simplify the expression using non-local knowledge.
Duncan Sands92c43912008-06-06 12:08:01 +00002395 if (!VT.isVector() &&
Dan Gohman8181bd12008-07-27 21:46:04 +00002396 SimplifyDemandedBits(SDValue(N, 0)))
2397 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002398
Dan Gohman8181bd12008-07-27 21:46:04 +00002399 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002400}
2401
Chris Lattner91ed3c32007-12-06 07:33:36 +00002402/// visitShiftByConstant - Handle transforms common to the three shifts, when
2403/// the shift amount is a constant.
Dan Gohman8181bd12008-07-27 21:46:04 +00002404SDValue DAGCombiner::visitShiftByConstant(SDNode *N, unsigned Amt) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002405 SDNode *LHS = N->getOperand(0).getNode();
Dan Gohman8181bd12008-07-27 21:46:04 +00002406 if (!LHS->hasOneUse()) return SDValue();
Chris Lattner91ed3c32007-12-06 07:33:36 +00002407
2408 // We want to pull some binops through shifts, so that we have (and (shift))
2409 // instead of (shift (and)), likewise for add, or, xor, etc. This sort of
2410 // thing happens with address calculations, so it's important to canonicalize
2411 // it.
2412 bool HighBitSet = false; // Can we transform this if the high bit is set?
2413
2414 switch (LHS->getOpcode()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002415 default: return SDValue();
Chris Lattner91ed3c32007-12-06 07:33:36 +00002416 case ISD::OR:
2417 case ISD::XOR:
2418 HighBitSet = false; // We can only transform sra if the high bit is clear.
2419 break;
2420 case ISD::AND:
2421 HighBitSet = true; // We can only transform sra if the high bit is set.
2422 break;
2423 case ISD::ADD:
2424 if (N->getOpcode() != ISD::SHL)
Dan Gohman8181bd12008-07-27 21:46:04 +00002425 return SDValue(); // only shl(add) not sr[al](add).
Chris Lattner91ed3c32007-12-06 07:33:36 +00002426 HighBitSet = false; // We can only transform sra if the high bit is clear.
2427 break;
2428 }
2429
2430 // We require the RHS of the binop to be a constant as well.
2431 ConstantSDNode *BinOpCst = dyn_cast<ConstantSDNode>(LHS->getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00002432 if (!BinOpCst) return SDValue();
Chris Lattner91ed3c32007-12-06 07:33:36 +00002433
Chris Lattnerdcd19762007-12-06 07:47:55 +00002434
2435 // FIXME: disable this for unless the input to the binop is a shift by a
2436 // constant. If it is not a shift, it pessimizes some common cases like:
2437 //
2438 //void foo(int *X, int i) { X[i & 1235] = 1; }
2439 //int bar(int *X, int i) { return X[i & 255]; }
Gabor Greif1c80d112008-08-28 21:40:38 +00002440 SDNode *BinOpLHSVal = LHS->getOperand(0).getNode();
Chris Lattnerdcd19762007-12-06 07:47:55 +00002441 if ((BinOpLHSVal->getOpcode() != ISD::SHL &&
2442 BinOpLHSVal->getOpcode() != ISD::SRA &&
2443 BinOpLHSVal->getOpcode() != ISD::SRL) ||
2444 !isa<ConstantSDNode>(BinOpLHSVal->getOperand(1)))
Dan Gohman8181bd12008-07-27 21:46:04 +00002445 return SDValue();
Chris Lattnerdcd19762007-12-06 07:47:55 +00002446
Duncan Sands92c43912008-06-06 12:08:01 +00002447 MVT VT = N->getValueType(0);
Chris Lattner91ed3c32007-12-06 07:33:36 +00002448
2449 // If this is a signed shift right, and the high bit is modified
2450 // by the logical operation, do not perform the transformation.
2451 // The highBitSet boolean indicates the value of the high bit of
2452 // the constant which would cause it to be modified for this
2453 // operation.
2454 if (N->getOpcode() == ISD::SRA) {
Dan Gohmand047c3e2008-03-03 23:51:38 +00002455 bool BinOpRHSSignSet = BinOpCst->getAPIntValue().isNegative();
2456 if (BinOpRHSSignSet != HighBitSet)
Dan Gohman8181bd12008-07-27 21:46:04 +00002457 return SDValue();
Chris Lattner91ed3c32007-12-06 07:33:36 +00002458 }
2459
2460 // Fold the constants, shifting the binop RHS by the shift amount.
Dan Gohman8181bd12008-07-27 21:46:04 +00002461 SDValue NewRHS = DAG.getNode(N->getOpcode(), N->getValueType(0),
Chris Lattner91ed3c32007-12-06 07:33:36 +00002462 LHS->getOperand(1), N->getOperand(1));
2463
2464 // Create the new shift.
Dan Gohman8181bd12008-07-27 21:46:04 +00002465 SDValue NewShift = DAG.getNode(N->getOpcode(), VT, LHS->getOperand(0),
Chris Lattner91ed3c32007-12-06 07:33:36 +00002466 N->getOperand(1));
2467
2468 // Create the new binop.
2469 return DAG.getNode(LHS->getOpcode(), VT, NewShift, NewRHS);
2470}
2471
2472
Dan Gohman8181bd12008-07-27 21:46:04 +00002473SDValue DAGCombiner::visitSHL(SDNode *N) {
2474 SDValue N0 = N->getOperand(0);
2475 SDValue N1 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002476 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2477 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands92c43912008-06-06 12:08:01 +00002478 MVT VT = N0.getValueType();
2479 unsigned OpSizeInBits = VT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002480
2481 // fold (shl c1, c2) -> c1<<c2
2482 if (N0C && N1C)
Bill Wendling0445c4c2008-09-24 10:25:02 +00002483 return DAG.FoldConstantArithmetic(ISD::SHL, VT, N0C, N1C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002484 // fold (shl 0, x) -> 0
2485 if (N0C && N0C->isNullValue())
2486 return N0;
2487 // fold (shl x, c >= size(x)) -> undef
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002488 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002489 return DAG.getNode(ISD::UNDEF, VT);
2490 // fold (shl x, 0) -> x
2491 if (N1C && N1C->isNullValue())
2492 return N0;
2493 // if (shl x, c) is known to be zero, return 0
Dan Gohman8181bd12008-07-27 21:46:04 +00002494 if (DAG.MaskedValueIsZero(SDValue(N, 0),
Duncan Sands92c43912008-06-06 12:08:01 +00002495 APInt::getAllOnesValue(VT.getSizeInBits())))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002496 return DAG.getConstant(0, VT);
Evan Cheng76a64c72008-08-30 02:03:58 +00002497 // fold (shl x, (trunc (and y, c))) -> (shl x, (and (trunc y), c))
2498 // iff (trunc c) == c
2499 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng11c34292008-09-22 18:19:24 +00002500 N1.getOperand(0).getOpcode() == ISD::AND &&
2501 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Cheng76a64c72008-08-30 02:03:58 +00002502 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng11c34292008-09-22 18:19:24 +00002503 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Evan Cheng76a64c72008-08-30 02:03:58 +00002504 MVT TruncVT = N1.getValueType();
Evan Cheng11c34292008-09-22 18:19:24 +00002505 SDValue N100 = N1.getOperand(0).getOperand(0);
djg51bef2e2009-01-27 20:39:34 +00002506 uint64_t TruncC = TruncVT.getIntegerVTBitMask() &
2507 N101C->getZExtValue();
Evan Cheng11c34292008-09-22 18:19:24 +00002508 return DAG.getNode(ISD::SHL, VT, N0,
2509 DAG.getNode(ISD::AND, TruncVT,
2510 DAG.getNode(ISD::TRUNCATE, TruncVT, N100),
djg51bef2e2009-01-27 20:39:34 +00002511 DAG.getConstant(TruncC, TruncVT)));
Evan Cheng76a64c72008-08-30 02:03:58 +00002512 }
2513 }
2514
Dan Gohman8181bd12008-07-27 21:46:04 +00002515 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
2516 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002517 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
2518 if (N1C && N0.getOpcode() == ISD::SHL &&
2519 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002520 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
2521 uint64_t c2 = N1C->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002522 if (c1 + c2 > OpSizeInBits)
2523 return DAG.getConstant(0, VT);
2524 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
2525 DAG.getConstant(c1 + c2, N1.getValueType()));
2526 }
2527 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
2528 // (srl (and x, -1 << c1), c1-c2)
2529 if (N1C && N0.getOpcode() == ISD::SRL &&
2530 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002531 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
2532 uint64_t c2 = N1C->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00002533 SDValue Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002534 DAG.getConstant(~0ULL << c1, VT));
2535 if (c2 > c1)
2536 return DAG.getNode(ISD::SHL, VT, Mask,
2537 DAG.getConstant(c2-c1, N1.getValueType()));
2538 else
2539 return DAG.getNode(ISD::SRL, VT, Mask,
2540 DAG.getConstant(c1-c2, N1.getValueType()));
2541 }
2542 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
2543 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
2544 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002545 DAG.getConstant(~0ULL << N1C->getZExtValue(), VT));
Chris Lattner91ed3c32007-12-06 07:33:36 +00002546
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002547 return N1C ? visitShiftByConstant(N, N1C->getZExtValue()) : SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002548}
2549
Dan Gohman8181bd12008-07-27 21:46:04 +00002550SDValue DAGCombiner::visitSRA(SDNode *N) {
2551 SDValue N0 = N->getOperand(0);
2552 SDValue N1 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002553 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2554 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands92c43912008-06-06 12:08:01 +00002555 MVT VT = N0.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002556
2557 // fold (sra c1, c2) -> c1>>c2
2558 if (N0C && N1C)
Bill Wendling0445c4c2008-09-24 10:25:02 +00002559 return DAG.FoldConstantArithmetic(ISD::SRA, VT, N0C, N1C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002560 // fold (sra 0, x) -> 0
2561 if (N0C && N0C->isNullValue())
2562 return N0;
2563 // fold (sra -1, x) -> -1
2564 if (N0C && N0C->isAllOnesValue())
2565 return N0;
2566 // fold (sra x, c >= size(x)) -> undef
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002567 if (N1C && N1C->getZExtValue() >= VT.getSizeInBits())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002568 return DAG.getNode(ISD::UNDEF, VT);
2569 // fold (sra x, 0) -> x
2570 if (N1C && N1C->isNullValue())
2571 return N0;
2572 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
2573 // sext_inreg.
2574 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002575 unsigned LowBits = VT.getSizeInBits() - (unsigned)N1C->getZExtValue();
Duncan Sands6a437fb2008-06-09 11:32:28 +00002576 MVT EVT = MVT::getIntegerVT(LowBits);
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00002577 if ((!LegalOperations || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, EVT)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002578 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
2579 DAG.getValueType(EVT));
2580 }
Duncan Sands2418bec2008-06-13 19:07:40 +00002581
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002582 // fold (sra (sra x, c1), c2) -> (sra x, c1+c2)
2583 if (N1C && N0.getOpcode() == ISD::SRA) {
2584 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002585 unsigned Sum = N1C->getZExtValue() + C1->getZExtValue();
Duncan Sands92c43912008-06-06 12:08:01 +00002586 if (Sum >= VT.getSizeInBits()) Sum = VT.getSizeInBits()-1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002587 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0),
2588 DAG.getConstant(Sum, N1C->getValueType(0)));
2589 }
2590 }
Christopher Lambfc5c1642008-03-19 08:30:06 +00002591
2592 // fold sra (shl X, m), result_size - n
2593 // -> (sign_extend (trunc (shl X, result_size - n - m))) for
Christopher Lamb21e8a952008-03-20 04:31:39 +00002594 // result_size - n != m.
2595 // If truncate is free for the target sext(shl) is likely to result in better
2596 // code.
Christopher Lambfc5c1642008-03-19 08:30:06 +00002597 if (N0.getOpcode() == ISD::SHL) {
2598 // Get the two constanst of the shifts, CN0 = m, CN = n.
2599 const ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2600 if (N01C && N1C) {
Christopher Lamb21e8a952008-03-20 04:31:39 +00002601 // Determine what the truncate's result bitsize and type would be.
Duncan Sands92c43912008-06-06 12:08:01 +00002602 unsigned VTValSize = VT.getSizeInBits();
2603 MVT TruncVT =
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002604 MVT::getIntegerVT(VTValSize - N1C->getZExtValue());
Christopher Lamb21e8a952008-03-20 04:31:39 +00002605 // Determine the residual right-shift amount.
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002606 unsigned ShiftAmt = N1C->getZExtValue() - N01C->getZExtValue();
Duncan Sands2418bec2008-06-13 19:07:40 +00002607
Christopher Lamb21e8a952008-03-20 04:31:39 +00002608 // If the shift is not a no-op (in which case this should be just a sign
2609 // extend already), the truncated to type is legal, sign_extend is legal
Gabor Greifb420b9d2008-08-30 19:29:20 +00002610 // on that type, and the the truncate to that type is both legal and free,
Christopher Lamb21e8a952008-03-20 04:31:39 +00002611 // perform the transform.
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00002612 if (ShiftAmt &&
Dan Gohman52c51aa2009-01-28 17:46:25 +00002613 TLI.isOperationLegalOrCustom(ISD::SIGN_EXTEND, TruncVT) &&
2614 TLI.isOperationLegalOrCustom(ISD::TRUNCATE, VT) &&
Evan Chengca0e80f2008-03-20 02:18:41 +00002615 TLI.isTruncateFree(VT, TruncVT)) {
Christopher Lamb21e8a952008-03-20 04:31:39 +00002616
Dan Gohman8181bd12008-07-27 21:46:04 +00002617 SDValue Amt = DAG.getConstant(ShiftAmt, TLI.getShiftAmountTy());
2618 SDValue Shift = DAG.getNode(ISD::SRL, VT, N0.getOperand(0), Amt);
2619 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, TruncVT, Shift);
Christopher Lamb21e8a952008-03-20 04:31:39 +00002620 return DAG.getNode(ISD::SIGN_EXTEND, N->getValueType(0), Trunc);
Christopher Lambfc5c1642008-03-19 08:30:06 +00002621 }
2622 }
2623 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002624
Evan Cheng76a64c72008-08-30 02:03:58 +00002625 // fold (sra x, (trunc (and y, c))) -> (sra x, (and (trunc y), c))
2626 // iff (trunc c) == c
2627 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng11c34292008-09-22 18:19:24 +00002628 N1.getOperand(0).getOpcode() == ISD::AND &&
2629 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Cheng76a64c72008-08-30 02:03:58 +00002630 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng11c34292008-09-22 18:19:24 +00002631 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Evan Cheng76a64c72008-08-30 02:03:58 +00002632 MVT TruncVT = N1.getValueType();
Evan Cheng11c34292008-09-22 18:19:24 +00002633 SDValue N100 = N1.getOperand(0).getOperand(0);
djg51bef2e2009-01-27 20:39:34 +00002634 uint64_t TruncC = TruncVT.getIntegerVTBitMask() &
2635 N101C->getZExtValue();
Evan Cheng11c34292008-09-22 18:19:24 +00002636 return DAG.getNode(ISD::SRA, VT, N0,
2637 DAG.getNode(ISD::AND, TruncVT,
2638 DAG.getNode(ISD::TRUNCATE, TruncVT, N100),
djg51bef2e2009-01-27 20:39:34 +00002639 DAG.getConstant(TruncC, TruncVT)));
Evan Cheng76a64c72008-08-30 02:03:58 +00002640 }
2641 }
2642
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002643 // Simplify, based on bits shifted out of the LHS.
Dan Gohman8181bd12008-07-27 21:46:04 +00002644 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
2645 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002646
2647
2648 // If the sign bit is known to be zero, switch this to a SRL.
Dan Gohman07961cd2008-02-25 21:11:39 +00002649 if (DAG.SignBitIsZero(N0))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002650 return DAG.getNode(ISD::SRL, VT, N0, N1);
Chris Lattner91ed3c32007-12-06 07:33:36 +00002651
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002652 return N1C ? visitShiftByConstant(N, N1C->getZExtValue()) : SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002653}
2654
Dan Gohman8181bd12008-07-27 21:46:04 +00002655SDValue DAGCombiner::visitSRL(SDNode *N) {
2656 SDValue N0 = N->getOperand(0);
2657 SDValue N1 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002658 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2659 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands92c43912008-06-06 12:08:01 +00002660 MVT VT = N0.getValueType();
2661 unsigned OpSizeInBits = VT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002662
2663 // fold (srl c1, c2) -> c1 >>u c2
2664 if (N0C && N1C)
Bill Wendling0445c4c2008-09-24 10:25:02 +00002665 return DAG.FoldConstantArithmetic(ISD::SRL, VT, N0C, N1C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002666 // fold (srl 0, x) -> 0
2667 if (N0C && N0C->isNullValue())
2668 return N0;
2669 // fold (srl x, c >= size(x)) -> undef
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002670 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002671 return DAG.getNode(ISD::UNDEF, VT);
2672 // fold (srl x, 0) -> x
2673 if (N1C && N1C->isNullValue())
2674 return N0;
2675 // if (srl x, c) is known to be zero, return 0
Dan Gohman8181bd12008-07-27 21:46:04 +00002676 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman07961cd2008-02-25 21:11:39 +00002677 APInt::getAllOnesValue(OpSizeInBits)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002678 return DAG.getConstant(0, VT);
2679
2680 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
2681 if (N1C && N0.getOpcode() == ISD::SRL &&
2682 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002683 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
2684 uint64_t c2 = N1C->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002685 if (c1 + c2 > OpSizeInBits)
2686 return DAG.getConstant(0, VT);
2687 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
2688 DAG.getConstant(c1 + c2, N1.getValueType()));
2689 }
2690
2691 // fold (srl (anyextend x), c) -> (anyextend (srl x, c))
2692 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
2693 // Shifting in all undef bits?
Duncan Sands92c43912008-06-06 12:08:01 +00002694 MVT SmallVT = N0.getOperand(0).getValueType();
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002695 if (N1C->getZExtValue() >= SmallVT.getSizeInBits())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002696 return DAG.getNode(ISD::UNDEF, VT);
2697
Dan Gohman8181bd12008-07-27 21:46:04 +00002698 SDValue SmallShift = DAG.getNode(ISD::SRL, SmallVT, N0.getOperand(0), N1);
Gabor Greif1c80d112008-08-28 21:40:38 +00002699 AddToWorkList(SmallShift.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002700 return DAG.getNode(ISD::ANY_EXTEND, VT, SmallShift);
2701 }
2702
2703 // fold (srl (sra X, Y), 31) -> (srl X, 31). This srl only looks at the sign
2704 // bit, which is unmodified by sra.
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002705 if (N1C && N1C->getZExtValue()+1 == VT.getSizeInBits()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002706 if (N0.getOpcode() == ISD::SRA)
2707 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0), N1);
2708 }
2709
2710 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
2711 if (N1C && N0.getOpcode() == ISD::CTLZ &&
Duncan Sands92c43912008-06-06 12:08:01 +00002712 N1C->getAPIntValue() == Log2_32(VT.getSizeInBits())) {
Dan Gohmanbea075f2008-02-20 16:33:30 +00002713 APInt KnownZero, KnownOne;
Duncan Sands92c43912008-06-06 12:08:01 +00002714 APInt Mask = APInt::getAllOnesValue(VT.getSizeInBits());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002715 DAG.ComputeMaskedBits(N0.getOperand(0), Mask, KnownZero, KnownOne);
2716
2717 // If any of the input bits are KnownOne, then the input couldn't be all
2718 // zeros, thus the result of the srl will always be zero.
Dan Gohmanbea075f2008-02-20 16:33:30 +00002719 if (KnownOne.getBoolValue()) return DAG.getConstant(0, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002720
2721 // If all of the bits input the to ctlz node are known to be zero, then
2722 // the result of the ctlz is "32" and the result of the shift is one.
Dan Gohmanbea075f2008-02-20 16:33:30 +00002723 APInt UnknownBits = ~KnownZero & Mask;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002724 if (UnknownBits == 0) return DAG.getConstant(1, VT);
2725
2726 // Otherwise, check to see if there is exactly one bit input to the ctlz.
2727 if ((UnknownBits & (UnknownBits-1)) == 0) {
2728 // Okay, we know that only that the single bit specified by UnknownBits
2729 // could be set on input to the CTLZ node. If this bit is set, the SRL
2730 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
2731 // to an SRL,XOR pair, which is likely to simplify more.
Dan Gohmanbea075f2008-02-20 16:33:30 +00002732 unsigned ShAmt = UnknownBits.countTrailingZeros();
Dan Gohman8181bd12008-07-27 21:46:04 +00002733 SDValue Op = N0.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002734 if (ShAmt) {
2735 Op = DAG.getNode(ISD::SRL, VT, Op,
2736 DAG.getConstant(ShAmt, TLI.getShiftAmountTy()));
Gabor Greif1c80d112008-08-28 21:40:38 +00002737 AddToWorkList(Op.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002738 }
2739 return DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(1, VT));
2740 }
2741 }
Evan Cheng76a64c72008-08-30 02:03:58 +00002742
2743 // fold (srl x, (trunc (and y, c))) -> (srl x, (and (trunc y), c))
2744 // iff (trunc c) == c
2745 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng11c34292008-09-22 18:19:24 +00002746 N1.getOperand(0).getOpcode() == ISD::AND &&
2747 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Cheng76a64c72008-08-30 02:03:58 +00002748 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng11c34292008-09-22 18:19:24 +00002749 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Evan Cheng76a64c72008-08-30 02:03:58 +00002750 MVT TruncVT = N1.getValueType();
Evan Cheng11c34292008-09-22 18:19:24 +00002751 SDValue N100 = N1.getOperand(0).getOperand(0);
djg51bef2e2009-01-27 20:39:34 +00002752 uint64_t TruncC = TruncVT.getIntegerVTBitMask() &
2753 N101C->getZExtValue();
Evan Cheng11c34292008-09-22 18:19:24 +00002754 return DAG.getNode(ISD::SRL, VT, N0,
2755 DAG.getNode(ISD::AND, TruncVT,
2756 DAG.getNode(ISD::TRUNCATE, TruncVT, N100),
djg51bef2e2009-01-27 20:39:34 +00002757 DAG.getConstant(TruncC, TruncVT)));
Evan Cheng76a64c72008-08-30 02:03:58 +00002758 }
2759 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002760
2761 // fold operands of srl based on knowledge that the low bits are not
2762 // demanded.
Dan Gohman8181bd12008-07-27 21:46:04 +00002763 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
2764 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002765
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002766 return N1C ? visitShiftByConstant(N, N1C->getZExtValue()) : SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002767}
2768
Dan Gohman8181bd12008-07-27 21:46:04 +00002769SDValue DAGCombiner::visitCTLZ(SDNode *N) {
2770 SDValue N0 = N->getOperand(0);
Duncan Sands92c43912008-06-06 12:08:01 +00002771 MVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002772
2773 // fold (ctlz c1) -> c2
2774 if (isa<ConstantSDNode>(N0))
2775 return DAG.getNode(ISD::CTLZ, VT, N0);
Dan Gohman8181bd12008-07-27 21:46:04 +00002776 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002777}
2778
Dan Gohman8181bd12008-07-27 21:46:04 +00002779SDValue DAGCombiner::visitCTTZ(SDNode *N) {
2780 SDValue N0 = N->getOperand(0);
Duncan Sands92c43912008-06-06 12:08:01 +00002781 MVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002782
2783 // fold (cttz c1) -> c2
2784 if (isa<ConstantSDNode>(N0))
2785 return DAG.getNode(ISD::CTTZ, VT, N0);
Dan Gohman8181bd12008-07-27 21:46:04 +00002786 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002787}
2788
Dan Gohman8181bd12008-07-27 21:46:04 +00002789SDValue DAGCombiner::visitCTPOP(SDNode *N) {
2790 SDValue N0 = N->getOperand(0);
Duncan Sands92c43912008-06-06 12:08:01 +00002791 MVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002792
2793 // fold (ctpop c1) -> c2
2794 if (isa<ConstantSDNode>(N0))
2795 return DAG.getNode(ISD::CTPOP, VT, N0);
Dan Gohman8181bd12008-07-27 21:46:04 +00002796 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002797}
2798
Dan Gohman8181bd12008-07-27 21:46:04 +00002799SDValue DAGCombiner::visitSELECT(SDNode *N) {
2800 SDValue N0 = N->getOperand(0);
2801 SDValue N1 = N->getOperand(1);
2802 SDValue N2 = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002803 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2804 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2805 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
Duncan Sands92c43912008-06-06 12:08:01 +00002806 MVT VT = N->getValueType(0);
2807 MVT VT0 = N0.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002808
2809 // fold select C, X, X -> X
2810 if (N1 == N2)
2811 return N1;
2812 // fold select true, X, Y -> X
2813 if (N0C && !N0C->isNullValue())
2814 return N1;
2815 // fold select false, X, Y -> Y
2816 if (N0C && N0C->isNullValue())
2817 return N2;
2818 // fold select C, 1, X -> C | X
Duncan Sands92c43912008-06-06 12:08:01 +00002819 if (VT == MVT::i1 && N1C && N1C->getAPIntValue() == 1)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002820 return DAG.getNode(ISD::OR, VT, N0, N2);
Bob Wilsonee1fe312009-01-22 22:05:48 +00002821 // fold select C, 0, 1 -> C ^ 1
2822 if (VT.isInteger() &&
2823 (VT0 == MVT::i1 ||
2824 (VT0.isInteger() &&
2825 TLI.getBooleanContents() == TargetLowering::ZeroOrOneBooleanContent)) &&
Dan Gohman9d24dc72008-03-13 22:13:53 +00002826 N1C && N2C && N1C->isNullValue() && N2C->getAPIntValue() == 1) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002827 SDValue XORNode = DAG.getNode(ISD::XOR, VT0, N0, DAG.getConstant(1, VT0));
Evan Chengff601dc2007-08-18 05:57:05 +00002828 if (VT == VT0)
2829 return XORNode;
Gabor Greif1c80d112008-08-28 21:40:38 +00002830 AddToWorkList(XORNode.getNode());
Duncan Sandsec142ee2008-06-08 20:54:56 +00002831 if (VT.bitsGT(VT0))
Evan Chengff601dc2007-08-18 05:57:05 +00002832 return DAG.getNode(ISD::ZERO_EXTEND, VT, XORNode);
2833 return DAG.getNode(ISD::TRUNCATE, VT, XORNode);
2834 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002835 // fold select C, 0, X -> ~C & X
Dale Johannesen53e0ad72007-12-06 17:53:31 +00002836 if (VT == VT0 && VT == MVT::i1 && N1C && N1C->isNullValue()) {
Bob Wilson81a42cf2009-01-22 17:39:32 +00002837 SDValue NOTNode = DAG.getNOT(N0, VT);
2838 AddToWorkList(NOTNode.getNode());
2839 return DAG.getNode(ISD::AND, VT, NOTNode, N2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002840 }
2841 // fold select C, X, 1 -> ~C | X
Dan Gohman9d24dc72008-03-13 22:13:53 +00002842 if (VT == VT0 && VT == MVT::i1 && N2C && N2C->getAPIntValue() == 1) {
Bob Wilson81a42cf2009-01-22 17:39:32 +00002843 SDValue NOTNode = DAG.getNOT(N0, VT);
2844 AddToWorkList(NOTNode.getNode());
2845 return DAG.getNode(ISD::OR, VT, NOTNode, N1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002846 }
2847 // fold select C, X, 0 -> C & X
Duncan Sands92c43912008-06-06 12:08:01 +00002848 if (VT == MVT::i1 && N2C && N2C->isNullValue())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002849 return DAG.getNode(ISD::AND, VT, N0, N1);
2850 // fold X ? X : Y --> X ? 1 : Y --> X | Y
Duncan Sands92c43912008-06-06 12:08:01 +00002851 if (VT == MVT::i1 && N0 == N1)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002852 return DAG.getNode(ISD::OR, VT, N0, N2);
2853 // fold X ? Y : X --> X ? Y : 0 --> X & Y
Duncan Sands92c43912008-06-06 12:08:01 +00002854 if (VT == MVT::i1 && N0 == N2)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002855 return DAG.getNode(ISD::AND, VT, N0, N1);
2856
2857 // If we can fold this based on the true/false value, do so.
2858 if (SimplifySelectOps(N, N1, N2))
Dan Gohman8181bd12008-07-27 21:46:04 +00002859 return SDValue(N, 0); // Don't revisit N.
Duncan Sands2418bec2008-06-13 19:07:40 +00002860
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002861 // fold selects based on a setcc into other things, such as min/max/abs
Anton Korobeynikov53422f62008-02-20 11:10:28 +00002862 if (N0.getOpcode() == ISD::SETCC) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002863 // FIXME:
2864 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
2865 // having to say they don't support SELECT_CC on every type the DAG knows
2866 // about, since there is no way to mark an opcode illegal at all value types
Dan Gohman52c51aa2009-01-28 17:46:25 +00002867 if (TLI.isOperationLegalOrCustom(ISD::SELECT_CC, MVT::Other))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002868 return DAG.getNode(ISD::SELECT_CC, VT, N0.getOperand(0), N0.getOperand(1),
2869 N1, N2, N0.getOperand(2));
2870 else
2871 return SimplifySelect(N0, N1, N2);
Anton Korobeynikov53422f62008-02-20 11:10:28 +00002872 }
Dan Gohman8181bd12008-07-27 21:46:04 +00002873 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002874}
2875
Dan Gohman8181bd12008-07-27 21:46:04 +00002876SDValue DAGCombiner::visitSELECT_CC(SDNode *N) {
2877 SDValue N0 = N->getOperand(0);
2878 SDValue N1 = N->getOperand(1);
2879 SDValue N2 = N->getOperand(2);
2880 SDValue N3 = N->getOperand(3);
2881 SDValue N4 = N->getOperand(4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002882 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
2883
2884 // fold select_cc lhs, rhs, x, x, cc -> x
2885 if (N2 == N3)
2886 return N2;
2887
2888 // Determine if the condition we're dealing with is constant
Duncan Sands4a361272009-01-01 15:52:00 +00002889 SDValue SCC = SimplifySetCC(TLI.getSetCCResultType(N0.getValueType()),
2890 N0, N1, CC, false);
Gabor Greif1c80d112008-08-28 21:40:38 +00002891 if (SCC.getNode()) AddToWorkList(SCC.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002892
Gabor Greif1c80d112008-08-28 21:40:38 +00002893 if (ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.getNode())) {
Dan Gohman9d24dc72008-03-13 22:13:53 +00002894 if (!SCCC->isNullValue())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002895 return N2; // cond always true -> true val
2896 else
2897 return N3; // cond always false -> false val
2898 }
2899
2900 // Fold to a simpler select_cc
Gabor Greif1c80d112008-08-28 21:40:38 +00002901 if (SCC.getNode() && SCC.getOpcode() == ISD::SETCC)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002902 return DAG.getNode(ISD::SELECT_CC, N2.getValueType(),
2903 SCC.getOperand(0), SCC.getOperand(1), N2, N3,
2904 SCC.getOperand(2));
2905
2906 // If we can fold this based on the true/false value, do so.
2907 if (SimplifySelectOps(N, N2, N3))
Dan Gohman8181bd12008-07-27 21:46:04 +00002908 return SDValue(N, 0); // Don't revisit N.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002909
2910 // fold select_cc into other things, such as min/max/abs
2911 return SimplifySelectCC(N0, N1, N2, N3, CC);
2912}
2913
Dan Gohman8181bd12008-07-27 21:46:04 +00002914SDValue DAGCombiner::visitSETCC(SDNode *N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002915 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
2916 cast<CondCodeSDNode>(N->getOperand(2))->get());
2917}
2918
Evan Cheng9decb332007-10-29 19:58:20 +00002919// ExtendUsesToFormExtLoad - Trying to extend uses of a load to enable this:
2920// "fold ({s|z}ext (load x)) -> ({s|z}ext (truncate ({s|z}extload x)))"
2921// transformation. Returns true if extension are possible and the above
2922// mentioned transformation is profitable.
Dan Gohman8181bd12008-07-27 21:46:04 +00002923static bool ExtendUsesToFormExtLoad(SDNode *N, SDValue N0,
Evan Cheng9decb332007-10-29 19:58:20 +00002924 unsigned ExtOpc,
2925 SmallVector<SDNode*, 4> &ExtendNodes,
Dan Gohman96eb47a2009-01-15 19:20:50 +00002926 const TargetLowering &TLI) {
Evan Cheng9decb332007-10-29 19:58:20 +00002927 bool HasCopyToRegUses = false;
2928 bool isTruncFree = TLI.isTruncateFree(N->getValueType(0), N0.getValueType());
Gabor Greifb420b9d2008-08-30 19:29:20 +00002929 for (SDNode::use_iterator UI = N0.getNode()->use_begin(),
2930 UE = N0.getNode()->use_end();
Evan Cheng9decb332007-10-29 19:58:20 +00002931 UI != UE; ++UI) {
Dan Gohman0c97f1d2008-07-27 20:43:25 +00002932 SDNode *User = *UI;
Evan Cheng9decb332007-10-29 19:58:20 +00002933 if (User == N)
2934 continue;
2935 // FIXME: Only extend SETCC N, N and SETCC N, c for now.
2936 if (User->getOpcode() == ISD::SETCC) {
2937 ISD::CondCode CC = cast<CondCodeSDNode>(User->getOperand(2))->get();
2938 if (ExtOpc == ISD::ZERO_EXTEND && ISD::isSignedIntSetCC(CC))
2939 // Sign bits will be lost after a zext.
2940 return false;
2941 bool Add = false;
2942 for (unsigned i = 0; i != 2; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002943 SDValue UseOp = User->getOperand(i);
Evan Cheng9decb332007-10-29 19:58:20 +00002944 if (UseOp == N0)
2945 continue;
2946 if (!isa<ConstantSDNode>(UseOp))
2947 return false;
2948 Add = true;
2949 }
2950 if (Add)
2951 ExtendNodes.push_back(User);
2952 } else {
2953 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002954 SDValue UseOp = User->getOperand(i);
Evan Cheng9decb332007-10-29 19:58:20 +00002955 if (UseOp == N0) {
2956 // If truncate from extended type to original load type is free
2957 // on this target, then it's ok to extend a CopyToReg.
2958 if (isTruncFree && User->getOpcode() == ISD::CopyToReg)
2959 HasCopyToRegUses = true;
2960 else
2961 return false;
2962 }
2963 }
2964 }
2965 }
2966
2967 if (HasCopyToRegUses) {
2968 bool BothLiveOut = false;
2969 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
2970 UI != UE; ++UI) {
Dan Gohman0c97f1d2008-07-27 20:43:25 +00002971 SDNode *User = *UI;
Evan Cheng9decb332007-10-29 19:58:20 +00002972 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002973 SDValue UseOp = User->getOperand(i);
Gabor Greif1c80d112008-08-28 21:40:38 +00002974 if (UseOp.getNode() == N && UseOp.getResNo() == 0) {
Evan Cheng9decb332007-10-29 19:58:20 +00002975 BothLiveOut = true;
2976 break;
2977 }
2978 }
2979 }
2980 if (BothLiveOut)
2981 // Both unextended and extended values are live out. There had better be
2982 // good a reason for the transformation.
2983 return ExtendNodes.size();
2984 }
2985 return true;
2986}
2987
Dan Gohman8181bd12008-07-27 21:46:04 +00002988SDValue DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
2989 SDValue N0 = N->getOperand(0);
Duncan Sands92c43912008-06-06 12:08:01 +00002990 MVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002991
2992 // fold (sext c1) -> c1
2993 if (isa<ConstantSDNode>(N0))
2994 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0);
2995
2996 // fold (sext (sext x)) -> (sext x)
2997 // fold (sext (aext x)) -> (sext x)
2998 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
2999 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
3000
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003001 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman2e0e0cf2008-05-20 20:56:33 +00003002 // fold (sext (truncate (load x))) -> (sext (smaller load x))
3003 // fold (sext (truncate (srl (load x), c))) -> (sext (smaller load (x+c/n)))
Gabor Greif1c80d112008-08-28 21:40:38 +00003004 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
3005 if (NarrowLoad.getNode()) {
3006 if (NarrowLoad.getNode() != N0.getNode())
3007 CombineTo(N0.getNode(), NarrowLoad);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003008 return DAG.getNode(ISD::SIGN_EXTEND, VT, NarrowLoad);
3009 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003010
Dan Gohman2e0e0cf2008-05-20 20:56:33 +00003011 // See if the value being truncated is already sign extended. If so, just
3012 // eliminate the trunc/sext pair.
Dan Gohman8181bd12008-07-27 21:46:04 +00003013 SDValue Op = N0.getOperand(0);
Duncan Sands92c43912008-06-06 12:08:01 +00003014 unsigned OpBits = Op.getValueType().getSizeInBits();
3015 unsigned MidBits = N0.getValueType().getSizeInBits();
3016 unsigned DestBits = VT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003017 unsigned NumSignBits = DAG.ComputeNumSignBits(Op);
3018
3019 if (OpBits == DestBits) {
3020 // Op is i32, Mid is i8, and Dest is i32. If Op has more than 24 sign
3021 // bits, it is already ready.
3022 if (NumSignBits > DestBits-MidBits)
3023 return Op;
3024 } else if (OpBits < DestBits) {
3025 // Op is i32, Mid is i8, and Dest is i64. If Op has more than 24 sign
3026 // bits, just sext from i32.
3027 if (NumSignBits > OpBits-MidBits)
3028 return DAG.getNode(ISD::SIGN_EXTEND, VT, Op);
3029 } else {
3030 // Op is i64, Mid is i8, and Dest is i32. If Op has more than 56 sign
3031 // bits, just truncate to i32.
3032 if (NumSignBits > OpBits-MidBits)
3033 return DAG.getNode(ISD::TRUNCATE, VT, Op);
3034 }
3035
3036 // fold (sext (truncate x)) -> (sextinreg x).
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003037 if (!LegalOperations || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG,
3038 N0.getValueType())) {
Duncan Sandsec142ee2008-06-08 20:54:56 +00003039 if (Op.getValueType().bitsLT(VT))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003040 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
Duncan Sandsec142ee2008-06-08 20:54:56 +00003041 else if (Op.getValueType().bitsGT(VT))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003042 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
3043 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, Op,
3044 DAG.getValueType(N0.getValueType()));
3045 }
3046 }
3047
3048 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Gabor Greif1c80d112008-08-28 21:40:38 +00003049 if (ISD::isNON_EXTLoad(N0.getNode()) &&
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003050 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng08c171a2008-10-14 21:26:46 +00003051 TLI.isLoadExtLegal(ISD::SEXTLOAD, N0.getValueType()))) {
Evan Cheng9decb332007-10-29 19:58:20 +00003052 bool DoXform = true;
3053 SmallVector<SDNode*, 4> SetCCs;
3054 if (!N0.hasOneUse())
3055 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::SIGN_EXTEND, SetCCs, TLI);
3056 if (DoXform) {
3057 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8181bd12008-07-27 21:46:04 +00003058 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003059 LN0->getBasePtr(), LN0->getSrcValue(),
3060 LN0->getSrcValueOffset(),
3061 N0.getValueType(),
3062 LN0->isVolatile(), LN0->getAlignment());
Evan Cheng9decb332007-10-29 19:58:20 +00003063 CombineTo(N, ExtLoad);
Dan Gohman8181bd12008-07-27 21:46:04 +00003064 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad);
Gabor Greif1c80d112008-08-28 21:40:38 +00003065 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Evan Cheng9decb332007-10-29 19:58:20 +00003066 // Extend SetCC uses if necessary.
3067 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
3068 SDNode *SetCC = SetCCs[i];
Dan Gohman8181bd12008-07-27 21:46:04 +00003069 SmallVector<SDValue, 4> Ops;
Evan Cheng9decb332007-10-29 19:58:20 +00003070 for (unsigned j = 0; j != 2; ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003071 SDValue SOp = SetCC->getOperand(j);
Evan Cheng9decb332007-10-29 19:58:20 +00003072 if (SOp == Trunc)
3073 Ops.push_back(ExtLoad);
3074 else
3075 Ops.push_back(DAG.getNode(ISD::SIGN_EXTEND, VT, SOp));
3076 }
3077 Ops.push_back(SetCC->getOperand(2));
3078 CombineTo(SetCC, DAG.getNode(ISD::SETCC, SetCC->getValueType(0),
3079 &Ops[0], Ops.size()));
3080 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003081 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng9decb332007-10-29 19:58:20 +00003082 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003083 }
3084
3085 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
3086 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
Gabor Greif1c80d112008-08-28 21:40:38 +00003087 if ((ISD::isSEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) &&
3088 ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003089 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands92c43912008-06-06 12:08:01 +00003090 MVT EVT = LN0->getMemoryVT();
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003091 if ((!LegalOperations && !LN0->isVolatile()) ||
Evan Cheng08c171a2008-10-14 21:26:46 +00003092 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003093 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003094 LN0->getBasePtr(), LN0->getSrcValue(),
3095 LN0->getSrcValueOffset(), EVT,
3096 LN0->isVolatile(), LN0->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003097 CombineTo(N, ExtLoad);
Gabor Greifb420b9d2008-08-30 19:29:20 +00003098 CombineTo(N0.getNode(),
3099 DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003100 ExtLoad.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00003101 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003102 }
3103 }
3104
3105 // sext(setcc x,y,cc) -> select_cc x, y, -1, 0, cc
3106 if (N0.getOpcode() == ISD::SETCC) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003107 SDValue SCC =
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003108 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
3109 DAG.getConstant(~0ULL, VT), DAG.getConstant(0, VT),
3110 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greif1c80d112008-08-28 21:40:38 +00003111 if (SCC.getNode()) return SCC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003112 }
3113
Dan Gohman415e13a2008-04-28 16:58:24 +00003114 // fold (sext x) -> (zext x) if the sign bit is known zero.
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003115 if ((!LegalOperations || TLI.isOperationLegal(ISD::ZERO_EXTEND, VT)) &&
Dan Gohman5b37f9d2008-04-28 18:47:17 +00003116 DAG.SignBitIsZero(N0))
Dan Gohman415e13a2008-04-28 16:58:24 +00003117 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
3118
Dan Gohman8181bd12008-07-27 21:46:04 +00003119 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003120}
3121
Dan Gohman8181bd12008-07-27 21:46:04 +00003122SDValue DAGCombiner::visitZERO_EXTEND(SDNode *N) {
3123 SDValue N0 = N->getOperand(0);
Duncan Sands92c43912008-06-06 12:08:01 +00003124 MVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003125
3126 // fold (zext c1) -> c1
3127 if (isa<ConstantSDNode>(N0))
3128 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
3129 // fold (zext (zext x)) -> (zext x)
3130 // fold (zext (aext x)) -> (zext x)
3131 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
3132 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
3133
3134 // fold (zext (truncate (load x))) -> (zext (smaller load x))
3135 // fold (zext (truncate (srl (load x), c))) -> (zext (small load (x+c/n)))
3136 if (N0.getOpcode() == ISD::TRUNCATE) {
Gabor Greif1c80d112008-08-28 21:40:38 +00003137 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
3138 if (NarrowLoad.getNode()) {
3139 if (NarrowLoad.getNode() != N0.getNode())
3140 CombineTo(N0.getNode(), NarrowLoad);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003141 return DAG.getNode(ISD::ZERO_EXTEND, VT, NarrowLoad);
3142 }
3143 }
3144
3145 // fold (zext (truncate x)) -> (and x, mask)
3146 if (N0.getOpcode() == ISD::TRUNCATE &&
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003147 (!LegalOperations || TLI.isOperationLegal(ISD::AND, VT))) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003148 SDValue Op = N0.getOperand(0);
Duncan Sandsec142ee2008-06-08 20:54:56 +00003149 if (Op.getValueType().bitsLT(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003150 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
Duncan Sandsec142ee2008-06-08 20:54:56 +00003151 } else if (Op.getValueType().bitsGT(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003152 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
3153 }
3154 return DAG.getZeroExtendInReg(Op, N0.getValueType());
3155 }
3156
3157 // fold (zext (and (trunc x), cst)) -> (and x, cst).
3158 if (N0.getOpcode() == ISD::AND &&
3159 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
3160 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003161 SDValue X = N0.getOperand(0).getOperand(0);
Duncan Sandsec142ee2008-06-08 20:54:56 +00003162 if (X.getValueType().bitsLT(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003163 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
Duncan Sandsec142ee2008-06-08 20:54:56 +00003164 } else if (X.getValueType().bitsGT(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003165 X = DAG.getNode(ISD::TRUNCATE, VT, X);
3166 }
Dan Gohmand047c3e2008-03-03 23:51:38 +00003167 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Duncan Sands92c43912008-06-06 12:08:01 +00003168 Mask.zext(VT.getSizeInBits());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003169 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
3170 }
3171
3172 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Gabor Greif1c80d112008-08-28 21:40:38 +00003173 if (ISD::isNON_EXTLoad(N0.getNode()) &&
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003174 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng08c171a2008-10-14 21:26:46 +00003175 TLI.isLoadExtLegal(ISD::ZEXTLOAD, N0.getValueType()))) {
Evan Cheng9decb332007-10-29 19:58:20 +00003176 bool DoXform = true;
3177 SmallVector<SDNode*, 4> SetCCs;
3178 if (!N0.hasOneUse())
3179 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ZERO_EXTEND, SetCCs, TLI);
3180 if (DoXform) {
3181 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8181bd12008-07-27 21:46:04 +00003182 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003183 LN0->getBasePtr(), LN0->getSrcValue(),
3184 LN0->getSrcValueOffset(),
3185 N0.getValueType(),
3186 LN0->isVolatile(), LN0->getAlignment());
Evan Cheng9decb332007-10-29 19:58:20 +00003187 CombineTo(N, ExtLoad);
Dan Gohman8181bd12008-07-27 21:46:04 +00003188 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad);
Gabor Greif1c80d112008-08-28 21:40:38 +00003189 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Evan Cheng9decb332007-10-29 19:58:20 +00003190 // Extend SetCC uses if necessary.
3191 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
3192 SDNode *SetCC = SetCCs[i];
Dan Gohman8181bd12008-07-27 21:46:04 +00003193 SmallVector<SDValue, 4> Ops;
Evan Cheng9decb332007-10-29 19:58:20 +00003194 for (unsigned j = 0; j != 2; ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003195 SDValue SOp = SetCC->getOperand(j);
Evan Cheng9decb332007-10-29 19:58:20 +00003196 if (SOp == Trunc)
3197 Ops.push_back(ExtLoad);
3198 else
Evan Cheng06aaf4c2007-10-30 20:11:21 +00003199 Ops.push_back(DAG.getNode(ISD::ZERO_EXTEND, VT, SOp));
Evan Cheng9decb332007-10-29 19:58:20 +00003200 }
3201 Ops.push_back(SetCC->getOperand(2));
3202 CombineTo(SetCC, DAG.getNode(ISD::SETCC, SetCC->getValueType(0),
3203 &Ops[0], Ops.size()));
3204 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003205 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng9decb332007-10-29 19:58:20 +00003206 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003207 }
3208
3209 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
3210 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
Gabor Greif1c80d112008-08-28 21:40:38 +00003211 if ((ISD::isZEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) &&
3212 ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003213 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands92c43912008-06-06 12:08:01 +00003214 MVT EVT = LN0->getMemoryVT();
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003215 if ((!LegalOperations && !LN0->isVolatile()) ||
Evan Cheng08c171a2008-10-14 21:26:46 +00003216 TLI.isLoadExtLegal(ISD::ZEXTLOAD, EVT)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003217 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003218 LN0->getBasePtr(), LN0->getSrcValue(),
3219 LN0->getSrcValueOffset(), EVT,
3220 LN0->isVolatile(), LN0->getAlignment());
Duncan Sands2418bec2008-06-13 19:07:40 +00003221 CombineTo(N, ExtLoad);
Gabor Greifb420b9d2008-08-30 19:29:20 +00003222 CombineTo(N0.getNode(),
3223 DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
Duncan Sands2418bec2008-06-13 19:07:40 +00003224 ExtLoad.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00003225 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Duncan Sands2418bec2008-06-13 19:07:40 +00003226 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003227 }
3228
3229 // zext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
3230 if (N0.getOpcode() == ISD::SETCC) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003231 SDValue SCC =
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003232 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
3233 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
3234 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greif1c80d112008-08-28 21:40:38 +00003235 if (SCC.getNode()) return SCC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003236 }
3237
Dan Gohman8181bd12008-07-27 21:46:04 +00003238 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003239}
3240
Dan Gohman8181bd12008-07-27 21:46:04 +00003241SDValue DAGCombiner::visitANY_EXTEND(SDNode *N) {
3242 SDValue N0 = N->getOperand(0);
Duncan Sands92c43912008-06-06 12:08:01 +00003243 MVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003244
3245 // fold (aext c1) -> c1
3246 if (isa<ConstantSDNode>(N0))
3247 return DAG.getNode(ISD::ANY_EXTEND, VT, N0);
3248 // fold (aext (aext x)) -> (aext x)
3249 // fold (aext (zext x)) -> (zext x)
3250 // fold (aext (sext x)) -> (sext x)
3251 if (N0.getOpcode() == ISD::ANY_EXTEND ||
3252 N0.getOpcode() == ISD::ZERO_EXTEND ||
3253 N0.getOpcode() == ISD::SIGN_EXTEND)
3254 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
3255
3256 // fold (aext (truncate (load x))) -> (aext (smaller load x))
3257 // fold (aext (truncate (srl (load x), c))) -> (aext (small load (x+c/n)))
3258 if (N0.getOpcode() == ISD::TRUNCATE) {
Gabor Greif1c80d112008-08-28 21:40:38 +00003259 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
3260 if (NarrowLoad.getNode()) {
3261 if (NarrowLoad.getNode() != N0.getNode())
3262 CombineTo(N0.getNode(), NarrowLoad);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003263 return DAG.getNode(ISD::ANY_EXTEND, VT, NarrowLoad);
3264 }
3265 }
3266
3267 // fold (aext (truncate x))
3268 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003269 SDValue TruncOp = N0.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003270 if (TruncOp.getValueType() == VT)
3271 return TruncOp; // x iff x size == zext size.
Duncan Sandsec142ee2008-06-08 20:54:56 +00003272 if (TruncOp.getValueType().bitsGT(VT))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003273 return DAG.getNode(ISD::TRUNCATE, VT, TruncOp);
3274 return DAG.getNode(ISD::ANY_EXTEND, VT, TruncOp);
3275 }
3276
3277 // fold (aext (and (trunc x), cst)) -> (and x, cst).
3278 if (N0.getOpcode() == ISD::AND &&
3279 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
3280 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003281 SDValue X = N0.getOperand(0).getOperand(0);
Duncan Sandsec142ee2008-06-08 20:54:56 +00003282 if (X.getValueType().bitsLT(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003283 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
Duncan Sandsec142ee2008-06-08 20:54:56 +00003284 } else if (X.getValueType().bitsGT(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003285 X = DAG.getNode(ISD::TRUNCATE, VT, X);
3286 }
Dan Gohmand047c3e2008-03-03 23:51:38 +00003287 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Duncan Sands92c43912008-06-06 12:08:01 +00003288 Mask.zext(VT.getSizeInBits());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003289 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
3290 }
3291
3292 // fold (aext (load x)) -> (aext (truncate (extload x)))
Gabor Greif1c80d112008-08-28 21:40:38 +00003293 if (ISD::isNON_EXTLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003294 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng08c171a2008-10-14 21:26:46 +00003295 TLI.isLoadExtLegal(ISD::EXTLOAD, N0.getValueType()))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003296 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8181bd12008-07-27 21:46:04 +00003297 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003298 LN0->getBasePtr(), LN0->getSrcValue(),
3299 LN0->getSrcValueOffset(),
3300 N0.getValueType(),
3301 LN0->isVolatile(), LN0->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003302 CombineTo(N, ExtLoad);
Dan Gohman759ed292008-07-31 00:50:31 +00003303 // Redirect any chain users to the new load.
Evan Chengc296a302008-08-29 23:20:46 +00003304 DAG.ReplaceAllUsesOfValueWith(SDValue(LN0, 1),
3305 SDValue(ExtLoad.getNode(), 1));
Dan Gohman759ed292008-07-31 00:50:31 +00003306 // If any node needs the original loaded value, recompute it.
3307 if (!LN0->use_empty())
3308 CombineTo(LN0, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
3309 ExtLoad.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00003310 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003311 }
3312
3313 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
3314 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
3315 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
3316 if (N0.getOpcode() == ISD::LOAD &&
Gabor Greif1c80d112008-08-28 21:40:38 +00003317 !ISD::isNON_EXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003318 N0.hasOneUse()) {
3319 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands92c43912008-06-06 12:08:01 +00003320 MVT EVT = LN0->getMemoryVT();
Dan Gohman8181bd12008-07-27 21:46:04 +00003321 SDValue ExtLoad = DAG.getExtLoad(LN0->getExtensionType(), VT,
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003322 LN0->getChain(), LN0->getBasePtr(),
3323 LN0->getSrcValue(),
3324 LN0->getSrcValueOffset(), EVT,
3325 LN0->isVolatile(), LN0->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003326 CombineTo(N, ExtLoad);
Evan Chengc296a302008-08-29 23:20:46 +00003327 CombineTo(N0.getNode(),
3328 DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003329 ExtLoad.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00003330 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003331 }
3332
3333 // aext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
3334 if (N0.getOpcode() == ISD::SETCC) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003335 SDValue SCC =
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003336 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
3337 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
3338 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greif1c80d112008-08-28 21:40:38 +00003339 if (SCC.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003340 return SCC;
3341 }
3342
Dan Gohman8181bd12008-07-27 21:46:04 +00003343 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003344}
3345
Chris Lattnere8671c52007-10-13 06:35:54 +00003346/// GetDemandedBits - See if the specified operand can be simplified with the
3347/// knowledge that only the bits specified by Mask are used. If so, return the
Dan Gohman8181bd12008-07-27 21:46:04 +00003348/// simpler operand, otherwise return a null SDValue.
3349SDValue DAGCombiner::GetDemandedBits(SDValue V, const APInt &Mask) {
Chris Lattnere8671c52007-10-13 06:35:54 +00003350 switch (V.getOpcode()) {
3351 default: break;
3352 case ISD::OR:
3353 case ISD::XOR:
3354 // If the LHS or RHS don't contribute bits to the or, drop them.
3355 if (DAG.MaskedValueIsZero(V.getOperand(0), Mask))
3356 return V.getOperand(1);
3357 if (DAG.MaskedValueIsZero(V.getOperand(1), Mask))
3358 return V.getOperand(0);
3359 break;
Chris Lattnerb77ea552007-10-13 06:58:48 +00003360 case ISD::SRL:
3361 // Only look at single-use SRLs.
Gabor Greif1c80d112008-08-28 21:40:38 +00003362 if (!V.getNode()->hasOneUse())
Chris Lattnerb77ea552007-10-13 06:58:48 +00003363 break;
3364 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
3365 // See if we can recursively simplify the LHS.
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003366 unsigned Amt = RHSC->getZExtValue();
Dan Gohman77d852f2009-01-03 19:22:06 +00003367 // Watch out for shift count overflow though.
3368 if (Amt >= Mask.getBitWidth()) break;
Dan Gohman07961cd2008-02-25 21:11:39 +00003369 APInt NewMask = Mask << Amt;
Dan Gohman8181bd12008-07-27 21:46:04 +00003370 SDValue SimplifyLHS = GetDemandedBits(V.getOperand(0), NewMask);
Gabor Greif1c80d112008-08-28 21:40:38 +00003371 if (SimplifyLHS.getNode()) {
Chris Lattnerb77ea552007-10-13 06:58:48 +00003372 return DAG.getNode(ISD::SRL, V.getValueType(),
3373 SimplifyLHS, V.getOperand(1));
3374 }
3375 }
Chris Lattnere8671c52007-10-13 06:35:54 +00003376 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003377 return SDValue();
Chris Lattnere8671c52007-10-13 06:35:54 +00003378}
3379
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003380/// ReduceLoadWidth - If the result of a wider load is shifted to right of N
3381/// bits and then truncated to a narrower type and where N is a multiple
3382/// of number of bits of the narrower type, transform it to a narrower load
3383/// from address + N / num of bits of new type. If the result is to be
3384/// extended, also fold the extension to form a extending load.
Dan Gohman8181bd12008-07-27 21:46:04 +00003385SDValue DAGCombiner::ReduceLoadWidth(SDNode *N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003386 unsigned Opc = N->getOpcode();
3387 ISD::LoadExtType ExtType = ISD::NON_EXTLOAD;
Dan Gohman8181bd12008-07-27 21:46:04 +00003388 SDValue N0 = N->getOperand(0);
Duncan Sands92c43912008-06-06 12:08:01 +00003389 MVT VT = N->getValueType(0);
Dan Gohman05452202009-01-21 15:17:51 +00003390 MVT EVT = VT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003391
Dan Gohman29c3cef2008-08-14 20:04:46 +00003392 // This transformation isn't valid for vector loads.
3393 if (VT.isVector())
3394 return SDValue();
3395
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003396 // Special case: SIGN_EXTEND_INREG is basically truncating to EVT then
3397 // extended to VT.
3398 if (Opc == ISD::SIGN_EXTEND_INREG) {
3399 ExtType = ISD::SEXTLOAD;
3400 EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003401 if (LegalOperations && !TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))
Dan Gohman8181bd12008-07-27 21:46:04 +00003402 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003403 }
3404
Duncan Sands92c43912008-06-06 12:08:01 +00003405 unsigned EVTBits = EVT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003406 unsigned ShAmt = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003407 if (N0.getOpcode() == ISD::SRL && N0.hasOneUse()) {
3408 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003409 ShAmt = N01->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003410 // Is the shift amount a multiple of size of VT?
3411 if ((ShAmt & (EVTBits-1)) == 0) {
3412 N0 = N0.getOperand(0);
Duncan Sands92c43912008-06-06 12:08:01 +00003413 if (N0.getValueType().getSizeInBits() <= EVTBits)
Dan Gohman8181bd12008-07-27 21:46:04 +00003414 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003415 }
3416 }
3417 }
3418
Duncan Sands3ea93352008-06-16 08:14:38 +00003419 // Do not generate loads of non-round integer types since these can
3420 // be expensive (and would be wrong if the type is not byte sized).
Dan Gohman50187c22009-01-20 01:06:45 +00003421 if (isa<LoadSDNode>(N0) && N0.hasOneUse() && EVT.isRound() &&
Dan Gohman759ed292008-07-31 00:50:31 +00003422 cast<LoadSDNode>(N0)->getMemoryVT().getSizeInBits() > EVTBits &&
Duncan Sands2418bec2008-06-13 19:07:40 +00003423 // Do not change the width of a volatile load.
3424 !cast<LoadSDNode>(N0)->isVolatile()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003425 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands92c43912008-06-06 12:08:01 +00003426 MVT PtrType = N0.getOperand(1).getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003427 // For big endian targets, we need to adjust the offset to the pointer to
3428 // load the correct bytes.
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00003429 if (TLI.isBigEndian()) {
Dan Gohman759ed292008-07-31 00:50:31 +00003430 unsigned LVTStoreBits = LN0->getMemoryVT().getStoreSizeInBits();
Duncan Sands92c43912008-06-06 12:08:01 +00003431 unsigned EVTStoreBits = EVT.getStoreSizeInBits();
Duncan Sands4f18d4f2007-11-09 08:57:19 +00003432 ShAmt = LVTStoreBits - EVTStoreBits - ShAmt;
3433 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003434 uint64_t PtrOff = ShAmt / 8;
Duncan Sandsa3691432007-10-28 12:59:45 +00003435 unsigned NewAlign = MinAlign(LN0->getAlignment(), PtrOff);
Dan Gohman8181bd12008-07-27 21:46:04 +00003436 SDValue NewPtr = DAG.getNode(ISD::ADD, PtrType, LN0->getBasePtr(),
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003437 DAG.getConstant(PtrOff, PtrType));
Gabor Greif1c80d112008-08-28 21:40:38 +00003438 AddToWorkList(NewPtr.getNode());
Dan Gohman8181bd12008-07-27 21:46:04 +00003439 SDValue Load = (ExtType == ISD::NON_EXTLOAD)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003440 ? DAG.getLoad(VT, LN0->getChain(), NewPtr,
Dan Gohman759ed292008-07-31 00:50:31 +00003441 LN0->getSrcValue(), LN0->getSrcValueOffset() + PtrOff,
Duncan Sandsa3691432007-10-28 12:59:45 +00003442 LN0->isVolatile(), NewAlign)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003443 : DAG.getExtLoad(ExtType, VT, LN0->getChain(), NewPtr,
Dan Gohman759ed292008-07-31 00:50:31 +00003444 LN0->getSrcValue(), LN0->getSrcValueOffset() + PtrOff,
3445 EVT, LN0->isVolatile(), NewAlign);
Dan Gohman05452202009-01-21 15:17:51 +00003446 // Replace the old load's chain with the new load's chain.
3447 WorkListRemover DeadNodes(*this);
3448 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), Load.getValue(1),
3449 &DeadNodes);
3450 // Return the new loaded value.
3451 return Load;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003452 }
3453
Dan Gohman8181bd12008-07-27 21:46:04 +00003454 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003455}
3456
3457
Dan Gohman8181bd12008-07-27 21:46:04 +00003458SDValue DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
3459 SDValue N0 = N->getOperand(0);
3460 SDValue N1 = N->getOperand(1);
Duncan Sands92c43912008-06-06 12:08:01 +00003461 MVT VT = N->getValueType(0);
3462 MVT EVT = cast<VTSDNode>(N1)->getVT();
3463 unsigned VTBits = VT.getSizeInBits();
3464 unsigned EVTBits = EVT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003465
3466 // fold (sext_in_reg c1) -> c1
3467 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
3468 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0, N1);
3469
3470 // If the input is already sign extended, just drop the extension.
Duncan Sands92c43912008-06-06 12:08:01 +00003471 if (DAG.ComputeNumSignBits(N0) >= VT.getSizeInBits()-EVTBits+1)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003472 return N0;
3473
3474 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
3475 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Duncan Sandsec142ee2008-06-08 20:54:56 +00003476 EVT.bitsLT(cast<VTSDNode>(N0.getOperand(1))->getVT())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003477 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
3478 }
3479
Dan Gohman759ed292008-07-31 00:50:31 +00003480 // fold (sext_in_reg (sext x)) -> (sext x)
3481 // fold (sext_in_reg (aext x)) -> (sext x)
3482 // if x is small enough.
3483 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND) {
3484 SDValue N00 = N0.getOperand(0);
3485 if (N00.getValueType().getSizeInBits() < EVTBits)
3486 return DAG.getNode(ISD::SIGN_EXTEND, VT, N00, N1);
3487 }
3488
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003489 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is known zero.
Dan Gohman07961cd2008-02-25 21:11:39 +00003490 if (DAG.MaskedValueIsZero(N0, APInt::getBitsSet(VTBits, EVTBits-1, EVTBits)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003491 return DAG.getZeroExtendInReg(N0, EVT);
3492
3493 // fold operands of sext_in_reg based on knowledge that the top bits are not
3494 // demanded.
Dan Gohman8181bd12008-07-27 21:46:04 +00003495 if (SimplifyDemandedBits(SDValue(N, 0)))
3496 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003497
3498 // fold (sext_in_reg (load x)) -> (smaller sextload x)
3499 // fold (sext_in_reg (srl (load x), c)) -> (smaller sextload (x+c/evtbits))
Dan Gohman8181bd12008-07-27 21:46:04 +00003500 SDValue NarrowLoad = ReduceLoadWidth(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00003501 if (NarrowLoad.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003502 return NarrowLoad;
3503
3504 // fold (sext_in_reg (srl X, 24), i8) -> sra X, 24
3505 // fold (sext_in_reg (srl X, 23), i8) -> sra X, 23 iff possible.
3506 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
3507 if (N0.getOpcode() == ISD::SRL) {
3508 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003509 if (ShAmt->getZExtValue()+EVTBits <= VT.getSizeInBits()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003510 // We can turn this into an SRA iff the input to the SRL is already sign
3511 // extended enough.
3512 unsigned InSignBits = DAG.ComputeNumSignBits(N0.getOperand(0));
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003513 if (VT.getSizeInBits()-(ShAmt->getZExtValue()+EVTBits) < InSignBits)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003514 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0), N0.getOperand(1));
3515 }
3516 }
3517
3518 // fold (sext_inreg (extload x)) -> (sextload x)
Gabor Greif1c80d112008-08-28 21:40:38 +00003519 if (ISD::isEXTLoad(N0.getNode()) &&
3520 ISD::isUNINDEXEDLoad(N0.getNode()) &&
Dan Gohman9a4c92c2008-01-30 00:15:11 +00003521 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003522 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng08c171a2008-10-14 21:26:46 +00003523 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003524 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8181bd12008-07-27 21:46:04 +00003525 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003526 LN0->getBasePtr(), LN0->getSrcValue(),
3527 LN0->getSrcValueOffset(), EVT,
3528 LN0->isVolatile(), LN0->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003529 CombineTo(N, ExtLoad);
Gabor Greif1c80d112008-08-28 21:40:38 +00003530 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00003531 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003532 }
3533 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Gabor Greif1c80d112008-08-28 21:40:38 +00003534 if (ISD::isZEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003535 N0.hasOneUse() &&
Dan Gohman9a4c92c2008-01-30 00:15:11 +00003536 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003537 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng08c171a2008-10-14 21:26:46 +00003538 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003539 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8181bd12008-07-27 21:46:04 +00003540 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003541 LN0->getBasePtr(), LN0->getSrcValue(),
3542 LN0->getSrcValueOffset(), EVT,
3543 LN0->isVolatile(), LN0->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003544 CombineTo(N, ExtLoad);
Gabor Greif1c80d112008-08-28 21:40:38 +00003545 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00003546 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003547 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003548 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003549}
3550
Dan Gohman8181bd12008-07-27 21:46:04 +00003551SDValue DAGCombiner::visitTRUNCATE(SDNode *N) {
3552 SDValue N0 = N->getOperand(0);
Duncan Sands92c43912008-06-06 12:08:01 +00003553 MVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003554
3555 // noop truncate
3556 if (N0.getValueType() == N->getValueType(0))
3557 return N0;
3558 // fold (truncate c1) -> c1
3559 if (isa<ConstantSDNode>(N0))
3560 return DAG.getNode(ISD::TRUNCATE, VT, N0);
3561 // fold (truncate (truncate x)) -> (truncate x)
3562 if (N0.getOpcode() == ISD::TRUNCATE)
3563 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
3564 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
3565 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND||
3566 N0.getOpcode() == ISD::ANY_EXTEND) {
Duncan Sandsec142ee2008-06-08 20:54:56 +00003567 if (N0.getOperand(0).getValueType().bitsLT(VT))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003568 // if the source is smaller than the dest, we still need an extend
3569 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Duncan Sandsec142ee2008-06-08 20:54:56 +00003570 else if (N0.getOperand(0).getValueType().bitsGT(VT))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003571 // if the source is larger than the dest, than we just need the truncate
3572 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
3573 else
3574 // if the source and dest are the same type, we can drop both the extend
3575 // and the truncate
3576 return N0.getOperand(0);
3577 }
3578
Chris Lattnere8671c52007-10-13 06:35:54 +00003579 // See if we can simplify the input to this truncate through knowledge that
3580 // only the low bits are being used. For example "trunc (or (shl x, 8), y)"
3581 // -> trunc y
Dan Gohman8181bd12008-07-27 21:46:04 +00003582 SDValue Shorter =
Dan Gohman07961cd2008-02-25 21:11:39 +00003583 GetDemandedBits(N0, APInt::getLowBitsSet(N0.getValueSizeInBits(),
Duncan Sands92c43912008-06-06 12:08:01 +00003584 VT.getSizeInBits()));
Gabor Greif1c80d112008-08-28 21:40:38 +00003585 if (Shorter.getNode())
Chris Lattnere8671c52007-10-13 06:35:54 +00003586 return DAG.getNode(ISD::TRUNCATE, VT, Shorter);
3587
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003588 // fold (truncate (load x)) -> (smaller load x)
3589 // fold (truncate (srl (load x), c)) -> (smaller load (x+c/evtbits))
3590 return ReduceLoadWidth(N);
3591}
3592
Evan Chengb6290462008-05-12 23:04:07 +00003593static SDNode *getBuildPairElt(SDNode *N, unsigned i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003594 SDValue Elt = N->getOperand(i);
Evan Chengb6290462008-05-12 23:04:07 +00003595 if (Elt.getOpcode() != ISD::MERGE_VALUES)
Gabor Greif1c80d112008-08-28 21:40:38 +00003596 return Elt.getNode();
3597 return Elt.getOperand(Elt.getResNo()).getNode();
Evan Chengb6290462008-05-12 23:04:07 +00003598}
3599
3600/// CombineConsecutiveLoads - build_pair (load, load) -> load
3601/// if load locations are consecutive.
Dan Gohman8181bd12008-07-27 21:46:04 +00003602SDValue DAGCombiner::CombineConsecutiveLoads(SDNode *N, MVT VT) {
Evan Chengb6290462008-05-12 23:04:07 +00003603 assert(N->getOpcode() == ISD::BUILD_PAIR);
3604
3605 SDNode *LD1 = getBuildPairElt(N, 0);
3606 if (!ISD::isNON_EXTLoad(LD1) || !LD1->hasOneUse())
Dan Gohman8181bd12008-07-27 21:46:04 +00003607 return SDValue();
Duncan Sands92c43912008-06-06 12:08:01 +00003608 MVT LD1VT = LD1->getValueType(0);
Evan Chengb6290462008-05-12 23:04:07 +00003609 SDNode *LD2 = getBuildPairElt(N, 1);
3610 const MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3611 if (ISD::isNON_EXTLoad(LD2) &&
3612 LD2->hasOneUse() &&
Duncan Sands2418bec2008-06-13 19:07:40 +00003613 // If both are volatile this would reduce the number of volatile loads.
3614 // If one is volatile it might be ok, but play conservative and bail out.
3615 !cast<LoadSDNode>(LD1)->isVolatile() &&
3616 !cast<LoadSDNode>(LD2)->isVolatile() &&
Duncan Sands92c43912008-06-06 12:08:01 +00003617 TLI.isConsecutiveLoad(LD2, LD1, LD1VT.getSizeInBits()/8, 1, MFI)) {
Evan Chengb6290462008-05-12 23:04:07 +00003618 LoadSDNode *LD = cast<LoadSDNode>(LD1);
3619 unsigned Align = LD->getAlignment();
Dan Gohman404e8542008-09-04 15:39:15 +00003620 unsigned NewAlign = TLI.getTargetData()->
Duncan Sands92c43912008-06-06 12:08:01 +00003621 getABITypeAlignment(VT.getTypeForMVT());
Duncan Sands2418bec2008-06-13 19:07:40 +00003622 if (NewAlign <= Align &&
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003623 (!LegalOperations || TLI.isOperationLegal(ISD::LOAD, VT)))
Evan Chengb6290462008-05-12 23:04:07 +00003624 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(),
3625 LD->getSrcValue(), LD->getSrcValueOffset(),
Duncan Sands2418bec2008-06-13 19:07:40 +00003626 false, Align);
Evan Chengb6290462008-05-12 23:04:07 +00003627 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003628 return SDValue();
Evan Chengb6290462008-05-12 23:04:07 +00003629}
3630
Dan Gohman8181bd12008-07-27 21:46:04 +00003631SDValue DAGCombiner::visitBIT_CONVERT(SDNode *N) {
3632 SDValue N0 = N->getOperand(0);
Duncan Sands92c43912008-06-06 12:08:01 +00003633 MVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003634
3635 // If the input is a BUILD_VECTOR with all constant elements, fold this now.
3636 // Only do this before legalize, since afterward the target may be depending
3637 // on the bitconvert.
3638 // First check to see if this is all constant.
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003639 if (!LegalTypes &&
Gabor Greif1c80d112008-08-28 21:40:38 +00003640 N0.getOpcode() == ISD::BUILD_VECTOR && N0.getNode()->hasOneUse() &&
Duncan Sands92c43912008-06-06 12:08:01 +00003641 VT.isVector()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003642 bool isSimple = true;
3643 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i)
3644 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
3645 N0.getOperand(i).getOpcode() != ISD::Constant &&
3646 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
3647 isSimple = false;
3648 break;
3649 }
3650
Duncan Sands92c43912008-06-06 12:08:01 +00003651 MVT DestEltVT = N->getValueType(0).getVectorElementType();
3652 assert(!DestEltVT.isVector() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003653 "Element type of vector ValueType must not be vector!");
3654 if (isSimple) {
Gabor Greif1c80d112008-08-28 21:40:38 +00003655 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(N0.getNode(), DestEltVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003656 }
3657 }
3658
Dan Gohman83c6e8a2008-09-05 01:58:21 +00003659 // If the input is a constant, let getNode fold it.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003660 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003661 SDValue Res = DAG.getNode(ISD::BIT_CONVERT, VT, N0);
Gabor Greif1c80d112008-08-28 21:40:38 +00003662 if (Res.getNode() != N) return Res;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003663 }
3664
3665 if (N0.getOpcode() == ISD::BIT_CONVERT) // conv(conv(x,t1),t2) -> conv(x,t2)
3666 return DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
3667
3668 // fold (conv (load x)) -> (load (conv*)x)
Evan Chengd7ba7ed2007-10-06 08:19:55 +00003669 // If the resultant load doesn't need a higher alignment than the original!
Gabor Greif1c80d112008-08-28 21:40:38 +00003670 if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sands2418bec2008-06-13 19:07:40 +00003671 // Do not change the width of a volatile load.
3672 !cast<LoadSDNode>(N0)->isVolatile() &&
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003673 (!LegalOperations || TLI.isOperationLegal(ISD::LOAD, VT))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003674 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman404e8542008-09-04 15:39:15 +00003675 unsigned Align = TLI.getTargetData()->
Duncan Sands92c43912008-06-06 12:08:01 +00003676 getABITypeAlignment(VT.getTypeForMVT());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003677 unsigned OrigAlign = LN0->getAlignment();
3678 if (Align <= OrigAlign) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003679 SDValue Load = DAG.getLoad(VT, LN0->getChain(), LN0->getBasePtr(),
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003680 LN0->getSrcValue(), LN0->getSrcValueOffset(),
3681 LN0->isVolatile(), OrigAlign);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003682 AddToWorkList(N);
Gabor Greifb420b9d2008-08-30 19:29:20 +00003683 CombineTo(N0.getNode(),
3684 DAG.getNode(ISD::BIT_CONVERT, N0.getValueType(), Load),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003685 Load.getValue(1));
3686 return Load;
3687 }
3688 }
Duncan Sands2418bec2008-06-13 19:07:40 +00003689
Chris Lattneref26cbc2008-01-27 17:42:27 +00003690 // Fold bitconvert(fneg(x)) -> xor(bitconvert(x), signbit)
3691 // Fold bitconvert(fabs(x)) -> and(bitconvert(x), ~signbit)
3692 // This often reduces constant pool loads.
3693 if ((N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FABS) &&
Gabor Greif1c80d112008-08-28 21:40:38 +00003694 N0.getNode()->hasOneUse() && VT.isInteger() && !VT.isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003695 SDValue NewConv = DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
Gabor Greif1c80d112008-08-28 21:40:38 +00003696 AddToWorkList(NewConv.getNode());
Chris Lattneref26cbc2008-01-27 17:42:27 +00003697
Duncan Sands92c43912008-06-06 12:08:01 +00003698 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
Chris Lattneref26cbc2008-01-27 17:42:27 +00003699 if (N0.getOpcode() == ISD::FNEG)
3700 return DAG.getNode(ISD::XOR, VT, NewConv, DAG.getConstant(SignBit, VT));
3701 assert(N0.getOpcode() == ISD::FABS);
3702 return DAG.getNode(ISD::AND, VT, NewConv, DAG.getConstant(~SignBit, VT));
3703 }
3704
3705 // Fold bitconvert(fcopysign(cst, x)) -> bitconvert(x)&sign | cst&~sign'
3706 // Note that we don't handle copysign(x,cst) because this can always be folded
3707 // to an fneg or fabs.
Gabor Greif1c80d112008-08-28 21:40:38 +00003708 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse() &&
Chris Lattner336672f2008-01-27 23:32:17 +00003709 isa<ConstantFPSDNode>(N0.getOperand(0)) &&
Duncan Sands92c43912008-06-06 12:08:01 +00003710 VT.isInteger() && !VT.isVector()) {
3711 unsigned OrigXWidth = N0.getOperand(1).getValueType().getSizeInBits();
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003712 MVT IntXVT = MVT::getIntegerVT(OrigXWidth);
3713 if (TLI.isTypeLegal(IntXVT) || !LegalTypes) {
3714 SDValue X = DAG.getNode(ISD::BIT_CONVERT, IntXVT, N0.getOperand(1));
3715 AddToWorkList(X.getNode());
Chris Lattneref26cbc2008-01-27 17:42:27 +00003716
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003717 // If X has a different width than the result/lhs, sext it or truncate it.
3718 unsigned VTWidth = VT.getSizeInBits();
3719 if (OrigXWidth < VTWidth) {
3720 X = DAG.getNode(ISD::SIGN_EXTEND, VT, X);
3721 AddToWorkList(X.getNode());
3722 } else if (OrigXWidth > VTWidth) {
3723 // To get the sign bit in the right place, we have to shift it right
3724 // before truncating.
3725 X = DAG.getNode(ISD::SRL, X.getValueType(), X,
3726 DAG.getConstant(OrigXWidth-VTWidth, X.getValueType()));
3727 AddToWorkList(X.getNode());
3728 X = DAG.getNode(ISD::TRUNCATE, VT, X);
3729 AddToWorkList(X.getNode());
3730 }
Chris Lattneref26cbc2008-01-27 17:42:27 +00003731
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003732 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
3733 X = DAG.getNode(ISD::AND, VT, X, DAG.getConstant(SignBit, VT));
3734 AddToWorkList(X.getNode());
Chris Lattneref26cbc2008-01-27 17:42:27 +00003735
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003736 SDValue Cst = DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
3737 Cst = DAG.getNode(ISD::AND, VT, Cst, DAG.getConstant(~SignBit, VT));
3738 AddToWorkList(Cst.getNode());
Chris Lattneref26cbc2008-01-27 17:42:27 +00003739
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003740 return DAG.getNode(ISD::OR, VT, X, Cst);
3741 }
Chris Lattneref26cbc2008-01-27 17:42:27 +00003742 }
Evan Chengb6290462008-05-12 23:04:07 +00003743
3744 // bitconvert(build_pair(ld, ld)) -> ld iff load locations are consecutive.
3745 if (N0.getOpcode() == ISD::BUILD_PAIR) {
Gabor Greif1c80d112008-08-28 21:40:38 +00003746 SDValue CombineLD = CombineConsecutiveLoads(N0.getNode(), VT);
3747 if (CombineLD.getNode())
Evan Chengb6290462008-05-12 23:04:07 +00003748 return CombineLD;
3749 }
Chris Lattneref26cbc2008-01-27 17:42:27 +00003750
Dan Gohman8181bd12008-07-27 21:46:04 +00003751 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003752}
3753
Dan Gohman8181bd12008-07-27 21:46:04 +00003754SDValue DAGCombiner::visitBUILD_PAIR(SDNode *N) {
Duncan Sands92c43912008-06-06 12:08:01 +00003755 MVT VT = N->getValueType(0);
Evan Chengb6290462008-05-12 23:04:07 +00003756 return CombineConsecutiveLoads(N, VT);
3757}
3758
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003759/// ConstantFoldBIT_CONVERTofBUILD_VECTOR - We know that BV is a build_vector
3760/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
3761/// destination element value type.
Dan Gohman8181bd12008-07-27 21:46:04 +00003762SDValue DAGCombiner::
Duncan Sands92c43912008-06-06 12:08:01 +00003763ConstantFoldBIT_CONVERTofBUILD_VECTOR(SDNode *BV, MVT DstEltVT) {
3764 MVT SrcEltVT = BV->getOperand(0).getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003765
3766 // If this is already the right type, we're done.
Dan Gohman8181bd12008-07-27 21:46:04 +00003767 if (SrcEltVT == DstEltVT) return SDValue(BV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003768
Duncan Sands92c43912008-06-06 12:08:01 +00003769 unsigned SrcBitSize = SrcEltVT.getSizeInBits();
3770 unsigned DstBitSize = DstEltVT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003771
3772 // If this is a conversion of N elements of one type to N elements of another
3773 // type, convert each element. This handles FP<->INT cases.
3774 if (SrcBitSize == DstBitSize) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003775 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003776 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
3777 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, DstEltVT, BV->getOperand(i)));
Gabor Greif1c80d112008-08-28 21:40:38 +00003778 AddToWorkList(Ops.back().getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003779 }
Duncan Sands92c43912008-06-06 12:08:01 +00003780 MVT VT = MVT::getVectorVT(DstEltVT,
3781 BV->getValueType(0).getVectorNumElements());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003782 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
3783 }
3784
3785 // Otherwise, we're growing or shrinking the elements. To avoid having to
3786 // handle annoying details of growing/shrinking FP values, we convert them to
3787 // int first.
Duncan Sands92c43912008-06-06 12:08:01 +00003788 if (SrcEltVT.isFloatingPoint()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003789 // Convert the input float vector to a int vector where the elements are the
3790 // same sizes.
3791 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
Duncan Sands6a437fb2008-06-09 11:32:28 +00003792 MVT IntVT = MVT::getIntegerVT(SrcEltVT.getSizeInBits());
Gabor Greif1c80d112008-08-28 21:40:38 +00003793 BV = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, IntVT).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003794 SrcEltVT = IntVT;
3795 }
3796
3797 // Now we know the input is an integer vector. If the output is a FP type,
3798 // convert to integer first, then to FP of the right size.
Duncan Sands92c43912008-06-06 12:08:01 +00003799 if (DstEltVT.isFloatingPoint()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003800 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
Duncan Sands6a437fb2008-06-09 11:32:28 +00003801 MVT TmpVT = MVT::getIntegerVT(DstEltVT.getSizeInBits());
Gabor Greif1c80d112008-08-28 21:40:38 +00003802 SDNode *Tmp = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, TmpVT).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003803
3804 // Next, convert to FP elements of the same size.
3805 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(Tmp, DstEltVT);
3806 }
3807
3808 // Okay, we know the src/dst types are both integers of differing types.
3809 // Handling growing first.
Duncan Sands92c43912008-06-06 12:08:01 +00003810 assert(SrcEltVT.isInteger() && DstEltVT.isInteger());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003811 if (SrcBitSize < DstBitSize) {
3812 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
3813
Dan Gohman8181bd12008-07-27 21:46:04 +00003814 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003815 for (unsigned i = 0, e = BV->getNumOperands(); i != e;
3816 i += NumInputsPerOutput) {
3817 bool isLE = TLI.isLittleEndian();
Dan Gohmand047c3e2008-03-03 23:51:38 +00003818 APInt NewBits = APInt(DstBitSize, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003819 bool EltIsUndef = true;
3820 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
3821 // Shift the previously computed bits over.
3822 NewBits <<= SrcBitSize;
Dan Gohman8181bd12008-07-27 21:46:04 +00003823 SDValue Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003824 if (Op.getOpcode() == ISD::UNDEF) continue;
3825 EltIsUndef = false;
3826
Dan Gohmand047c3e2008-03-03 23:51:38 +00003827 NewBits |=
3828 APInt(cast<ConstantSDNode>(Op)->getAPIntValue()).zext(DstBitSize);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003829 }
3830
3831 if (EltIsUndef)
3832 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
3833 else
3834 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
3835 }
3836
Duncan Sands92c43912008-06-06 12:08:01 +00003837 MVT VT = MVT::getVectorVT(DstEltVT, Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003838 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
3839 }
3840
3841 // Finally, this must be the case where we are shrinking elements: each input
3842 // turns into multiple outputs.
Evan Chengd1045a62008-02-18 23:04:32 +00003843 bool isS2V = ISD::isScalarToVector(BV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003844 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
Duncan Sands92c43912008-06-06 12:08:01 +00003845 MVT VT = MVT::getVectorVT(DstEltVT, NumOutputsPerInput*BV->getNumOperands());
Dan Gohman8181bd12008-07-27 21:46:04 +00003846 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003847 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
3848 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
3849 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
3850 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
3851 continue;
3852 }
Dan Gohmand047c3e2008-03-03 23:51:38 +00003853 APInt OpVal = cast<ConstantSDNode>(BV->getOperand(i))->getAPIntValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003854 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
Dan Gohmand047c3e2008-03-03 23:51:38 +00003855 APInt ThisVal = APInt(OpVal).trunc(DstBitSize);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003856 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
Dan Gohmand047c3e2008-03-03 23:51:38 +00003857 if (isS2V && i == 0 && j == 0 && APInt(ThisVal).zext(SrcBitSize) == OpVal)
Evan Chengd1045a62008-02-18 23:04:32 +00003858 // Simply turn this into a SCALAR_TO_VECTOR of the new type.
3859 return DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Ops[0]);
Dan Gohmand047c3e2008-03-03 23:51:38 +00003860 OpVal = OpVal.lshr(DstBitSize);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003861 }
3862
3863 // For big endian targets, swap the order of the pieces of each element.
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00003864 if (TLI.isBigEndian())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003865 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
3866 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003867 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
3868}
3869
3870
3871
Dan Gohman8181bd12008-07-27 21:46:04 +00003872SDValue DAGCombiner::visitFADD(SDNode *N) {
3873 SDValue N0 = N->getOperand(0);
3874 SDValue N1 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003875 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3876 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands92c43912008-06-06 12:08:01 +00003877 MVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003878
3879 // fold vector ops
Duncan Sands92c43912008-06-06 12:08:01 +00003880 if (VT.isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003881 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00003882 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003883 }
3884
3885 // fold (fadd c1, c2) -> c1+c2
Dale Johannesenb89072e2007-10-16 23:38:29 +00003886 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003887 return DAG.getNode(ISD::FADD, VT, N0, N1);
3888 // canonicalize constant to RHS
3889 if (N0CFP && !N1CFP)
3890 return DAG.getNode(ISD::FADD, VT, N1, N0);
Dan Gohman3d015562009-01-22 21:58:43 +00003891 // fold (A + 0) -> A
3892 if (UnsafeFPMath && N1CFP && N1CFP->getValueAPF().isZero())
3893 return N0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003894 // fold (A + (-B)) -> A-B
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003895 if (isNegatibleForFree(N1, LegalOperations) == 2)
Chris Lattnere0992b82008-02-26 07:04:54 +00003896 return DAG.getNode(ISD::FSUB, VT, N0,
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003897 GetNegatedExpression(N1, DAG, LegalOperations));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003898 // fold ((-A) + B) -> B-A
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003899 if (isNegatibleForFree(N0, LegalOperations) == 2)
Chris Lattnere0992b82008-02-26 07:04:54 +00003900 return DAG.getNode(ISD::FSUB, VT, N1,
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003901 GetNegatedExpression(N0, DAG, LegalOperations));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003902
3903 // If allowed, fold (fadd (fadd x, c1), c2) -> (fadd x, (fadd c1, c2))
3904 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FADD &&
Gabor Greif1c80d112008-08-28 21:40:38 +00003905 N0.getNode()->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003906 return DAG.getNode(ISD::FADD, VT, N0.getOperand(0),
3907 DAG.getNode(ISD::FADD, VT, N0.getOperand(1), N1));
3908
Dan Gohman8181bd12008-07-27 21:46:04 +00003909 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003910}
3911
Dan Gohman8181bd12008-07-27 21:46:04 +00003912SDValue DAGCombiner::visitFSUB(SDNode *N) {
3913 SDValue N0 = N->getOperand(0);
3914 SDValue N1 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003915 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3916 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands92c43912008-06-06 12:08:01 +00003917 MVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003918
3919 // fold vector ops
Duncan Sands92c43912008-06-06 12:08:01 +00003920 if (VT.isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003921 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00003922 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003923 }
3924
3925 // fold (fsub c1, c2) -> c1-c2
Dale Johannesenb89072e2007-10-16 23:38:29 +00003926 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003927 return DAG.getNode(ISD::FSUB, VT, N0, N1);
Dan Gohman46ef3eb2009-01-23 19:10:37 +00003928 // fold (A-0) -> A
3929 if (UnsafeFPMath && N1CFP && N1CFP->getValueAPF().isZero())
3930 return N0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003931 // fold (0-B) -> -B
Dale Johannesen7604c1b2007-08-31 23:34:27 +00003932 if (UnsafeFPMath && N0CFP && N0CFP->getValueAPF().isZero()) {
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003933 if (isNegatibleForFree(N1, LegalOperations))
3934 return GetNegatedExpression(N1, DAG, LegalOperations);
Dan Gohman3d015562009-01-22 21:58:43 +00003935 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
3936 return DAG.getNode(ISD::FNEG, VT, N1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003937 }
3938 // fold (A-(-B)) -> A+B
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003939 if (isNegatibleForFree(N1, LegalOperations))
Chris Lattnere0992b82008-02-26 07:04:54 +00003940 return DAG.getNode(ISD::FADD, VT, N0,
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003941 GetNegatedExpression(N1, DAG, LegalOperations));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003942
Dan Gohman8181bd12008-07-27 21:46:04 +00003943 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003944}
3945
Dan Gohman8181bd12008-07-27 21:46:04 +00003946SDValue DAGCombiner::visitFMUL(SDNode *N) {
3947 SDValue N0 = N->getOperand(0);
3948 SDValue N1 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003949 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3950 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands92c43912008-06-06 12:08:01 +00003951 MVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003952
3953 // fold vector ops
Duncan Sands92c43912008-06-06 12:08:01 +00003954 if (VT.isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003955 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00003956 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003957 }
3958
3959 // fold (fmul c1, c2) -> c1*c2
Dale Johannesenb89072e2007-10-16 23:38:29 +00003960 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003961 return DAG.getNode(ISD::FMUL, VT, N0, N1);
3962 // canonicalize constant to RHS
3963 if (N0CFP && !N1CFP)
3964 return DAG.getNode(ISD::FMUL, VT, N1, N0);
Dan Gohman3d015562009-01-22 21:58:43 +00003965 // fold (A * 0) -> 0
3966 if (UnsafeFPMath && N1CFP && N1CFP->getValueAPF().isZero())
3967 return N1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003968 // fold (fmul X, 2.0) -> (fadd X, X)
3969 if (N1CFP && N1CFP->isExactlyValue(+2.0))
3970 return DAG.getNode(ISD::FADD, VT, N0, N0);
3971 // fold (fmul X, -1.0) -> (fneg X)
3972 if (N1CFP && N1CFP->isExactlyValue(-1.0))
Dan Gohman3d015562009-01-22 21:58:43 +00003973 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
3974 return DAG.getNode(ISD::FNEG, VT, N0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003975
3976 // -X * -Y -> X*Y
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003977 if (char LHSNeg = isNegatibleForFree(N0, LegalOperations)) {
3978 if (char RHSNeg = isNegatibleForFree(N1, LegalOperations)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003979 // Both can be negated for free, check to see if at least one is cheaper
3980 // negated.
3981 if (LHSNeg == 2 || RHSNeg == 2)
Chris Lattnere0992b82008-02-26 07:04:54 +00003982 return DAG.getNode(ISD::FMUL, VT,
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003983 GetNegatedExpression(N0, DAG, LegalOperations),
3984 GetNegatedExpression(N1, DAG, LegalOperations));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003985 }
3986 }
3987
3988 // If allowed, fold (fmul (fmul x, c1), c2) -> (fmul x, (fmul c1, c2))
3989 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FMUL &&
Gabor Greif1c80d112008-08-28 21:40:38 +00003990 N0.getNode()->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003991 return DAG.getNode(ISD::FMUL, VT, N0.getOperand(0),
3992 DAG.getNode(ISD::FMUL, VT, N0.getOperand(1), N1));
3993
Dan Gohman8181bd12008-07-27 21:46:04 +00003994 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003995}
3996
Dan Gohman8181bd12008-07-27 21:46:04 +00003997SDValue DAGCombiner::visitFDIV(SDNode *N) {
3998 SDValue N0 = N->getOperand(0);
3999 SDValue N1 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004000 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
4001 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands92c43912008-06-06 12:08:01 +00004002 MVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004003
4004 // fold vector ops
Duncan Sands92c43912008-06-06 12:08:01 +00004005 if (VT.isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004006 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00004007 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004008 }
4009
4010 // fold (fdiv c1, c2) -> c1/c2
Dale Johannesenb89072e2007-10-16 23:38:29 +00004011 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004012 return DAG.getNode(ISD::FDIV, VT, N0, N1);
4013
4014
4015 // -X / -Y -> X*Y
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00004016 if (char LHSNeg = isNegatibleForFree(N0, LegalOperations)) {
4017 if (char RHSNeg = isNegatibleForFree(N1, LegalOperations)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004018 // Both can be negated for free, check to see if at least one is cheaper
4019 // negated.
4020 if (LHSNeg == 2 || RHSNeg == 2)
Chris Lattnere0992b82008-02-26 07:04:54 +00004021 return DAG.getNode(ISD::FDIV, VT,
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00004022 GetNegatedExpression(N0, DAG, LegalOperations),
4023 GetNegatedExpression(N1, DAG, LegalOperations));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004024 }
4025 }
4026
Dan Gohman8181bd12008-07-27 21:46:04 +00004027 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004028}
4029
Dan Gohman8181bd12008-07-27 21:46:04 +00004030SDValue DAGCombiner::visitFREM(SDNode *N) {
4031 SDValue N0 = N->getOperand(0);
4032 SDValue N1 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004033 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
4034 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands92c43912008-06-06 12:08:01 +00004035 MVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004036
4037 // fold (frem c1, c2) -> fmod(c1,c2)
Dale Johannesenb89072e2007-10-16 23:38:29 +00004038 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004039 return DAG.getNode(ISD::FREM, VT, N0, N1);
4040
Dan Gohman8181bd12008-07-27 21:46:04 +00004041 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004042}
4043
Dan Gohman8181bd12008-07-27 21:46:04 +00004044SDValue DAGCombiner::visitFCOPYSIGN(SDNode *N) {
4045 SDValue N0 = N->getOperand(0);
4046 SDValue N1 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004047 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
4048 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands92c43912008-06-06 12:08:01 +00004049 MVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004050
Dale Johannesenb89072e2007-10-16 23:38:29 +00004051 if (N0CFP && N1CFP && VT != MVT::ppcf128) // Constant fold
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004052 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1);
4053
4054 if (N1CFP) {
Dale Johannesenc53301c2007-08-26 01:18:27 +00004055 const APFloat& V = N1CFP->getValueAPF();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004056 // copysign(x, c1) -> fabs(x) iff ispos(c1)
4057 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
Dan Gohman3d015562009-01-22 21:58:43 +00004058 if (!V.isNegative()) {
4059 if (!LegalOperations || TLI.isOperationLegal(ISD::FABS, VT))
4060 return DAG.getNode(ISD::FABS, VT, N0);
4061 } else {
4062 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
4063 return DAG.getNode(ISD::FNEG, VT, DAG.getNode(ISD::FABS, VT, N0));
4064 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004065 }
4066
4067 // copysign(fabs(x), y) -> copysign(x, y)
4068 // copysign(fneg(x), y) -> copysign(x, y)
4069 // copysign(copysign(x,z), y) -> copysign(x, y)
4070 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
4071 N0.getOpcode() == ISD::FCOPYSIGN)
4072 return DAG.getNode(ISD::FCOPYSIGN, VT, N0.getOperand(0), N1);
4073
4074 // copysign(x, abs(y)) -> abs(x)
4075 if (N1.getOpcode() == ISD::FABS)
4076 return DAG.getNode(ISD::FABS, VT, N0);
4077
4078 // copysign(x, copysign(y,z)) -> copysign(x, z)
4079 if (N1.getOpcode() == ISD::FCOPYSIGN)
4080 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(1));
4081
4082 // copysign(x, fp_extend(y)) -> copysign(x, y)
4083 // copysign(x, fp_round(y)) -> copysign(x, y)
4084 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
4085 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(0));
4086
Dan Gohman8181bd12008-07-27 21:46:04 +00004087 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004088}
4089
4090
4091
Dan Gohman8181bd12008-07-27 21:46:04 +00004092SDValue DAGCombiner::visitSINT_TO_FP(SDNode *N) {
4093 SDValue N0 = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004094 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Duncan Sands92c43912008-06-06 12:08:01 +00004095 MVT VT = N->getValueType(0);
Chris Lattner8e0e2bdc2008-06-26 00:16:49 +00004096 MVT OpVT = N0.getValueType();
4097
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004098 // fold (sint_to_fp c1) -> c1fp
Chris Lattner8e0e2bdc2008-06-26 00:16:49 +00004099 if (N0C && OpVT != MVT::ppcf128)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004100 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
Chris Lattner8e0e2bdc2008-06-26 00:16:49 +00004101
4102 // If the input is a legal type, and SINT_TO_FP is not legal on this target,
4103 // but UINT_TO_FP is legal on this target, try to convert.
Dan Gohman52c51aa2009-01-28 17:46:25 +00004104 if (!TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, OpVT) &&
4105 TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, OpVT)) {
Chris Lattner8e0e2bdc2008-06-26 00:16:49 +00004106 // If the sign bit is known to be zero, we can change this to UINT_TO_FP.
4107 if (DAG.SignBitIsZero(N0))
4108 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
4109 }
4110
4111
Dan Gohman8181bd12008-07-27 21:46:04 +00004112 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004113}
4114
Dan Gohman8181bd12008-07-27 21:46:04 +00004115SDValue DAGCombiner::visitUINT_TO_FP(SDNode *N) {
4116 SDValue N0 = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004117 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Duncan Sands92c43912008-06-06 12:08:01 +00004118 MVT VT = N->getValueType(0);
Chris Lattner8e0e2bdc2008-06-26 00:16:49 +00004119 MVT OpVT = N0.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004120
4121 // fold (uint_to_fp c1) -> c1fp
Chris Lattner8e0e2bdc2008-06-26 00:16:49 +00004122 if (N0C && OpVT != MVT::ppcf128)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004123 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
Chris Lattner8e0e2bdc2008-06-26 00:16:49 +00004124
4125 // If the input is a legal type, and UINT_TO_FP is not legal on this target,
4126 // but SINT_TO_FP is legal on this target, try to convert.
Dan Gohman52c51aa2009-01-28 17:46:25 +00004127 if (!TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, OpVT) &&
4128 TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, OpVT)) {
Chris Lattner8e0e2bdc2008-06-26 00:16:49 +00004129 // If the sign bit is known to be zero, we can change this to SINT_TO_FP.
4130 if (DAG.SignBitIsZero(N0))
4131 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
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::visitFP_TO_SINT(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 (fp_to_sint c1fp) -> c1
4143 if (N0CFP)
4144 return DAG.getNode(ISD::FP_TO_SINT, VT, N0);
Dan Gohman8181bd12008-07-27 21:46:04 +00004145 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004146}
4147
Dan Gohman8181bd12008-07-27 21:46:04 +00004148SDValue DAGCombiner::visitFP_TO_UINT(SDNode *N) {
4149 SDValue N0 = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004150 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands92c43912008-06-06 12:08:01 +00004151 MVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004152
4153 // fold (fp_to_uint c1fp) -> c1
Dale Johannesenb89072e2007-10-16 23:38:29 +00004154 if (N0CFP && VT != MVT::ppcf128)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004155 return DAG.getNode(ISD::FP_TO_UINT, VT, N0);
Dan Gohman8181bd12008-07-27 21:46:04 +00004156 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004157}
4158
Dan Gohman8181bd12008-07-27 21:46:04 +00004159SDValue DAGCombiner::visitFP_ROUND(SDNode *N) {
4160 SDValue N0 = N->getOperand(0);
4161 SDValue N1 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004162 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands92c43912008-06-06 12:08:01 +00004163 MVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004164
4165 // fold (fp_round c1fp) -> c1fp
Dale Johannesenb89072e2007-10-16 23:38:29 +00004166 if (N0CFP && N0.getValueType() != MVT::ppcf128)
Chris Lattner5872a362008-01-17 07:00:52 +00004167 return DAG.getNode(ISD::FP_ROUND, VT, N0, N1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004168
4169 // fold (fp_round (fp_extend x)) -> x
4170 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
4171 return N0.getOperand(0);
4172
Chris Lattner7afb8552008-01-24 06:45:35 +00004173 // fold (fp_round (fp_round x)) -> (fp_round x)
4174 if (N0.getOpcode() == ISD::FP_ROUND) {
4175 // This is a value preserving truncation if both round's are.
4176 bool IsTrunc = N->getConstantOperandVal(1) == 1 &&
Gabor Greif1c80d112008-08-28 21:40:38 +00004177 N0.getNode()->getConstantOperandVal(1) == 1;
Chris Lattner7afb8552008-01-24 06:45:35 +00004178 return DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0),
4179 DAG.getIntPtrConstant(IsTrunc));
4180 }
4181
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004182 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
Gabor Greif1c80d112008-08-28 21:40:38 +00004183 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004184 SDValue Tmp = DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0), N1);
Gabor Greif1c80d112008-08-28 21:40:38 +00004185 AddToWorkList(Tmp.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004186 return DAG.getNode(ISD::FCOPYSIGN, VT, Tmp, N0.getOperand(1));
4187 }
4188
Dan Gohman8181bd12008-07-27 21:46:04 +00004189 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004190}
4191
Dan Gohman8181bd12008-07-27 21:46:04 +00004192SDValue DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
4193 SDValue N0 = N->getOperand(0);
Duncan Sands92c43912008-06-06 12:08:01 +00004194 MVT VT = N->getValueType(0);
4195 MVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004196 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
4197
4198 // fold (fp_round_inreg c1fp) -> c1fp
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00004199 if (N0CFP && (TLI.isTypeLegal(EVT) || !LegalTypes)) {
Dan Gohmanc1f3a072008-09-12 18:08:03 +00004200 SDValue Round = DAG.getConstantFP(*N0CFP->getConstantFPValue(), EVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004201 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
4202 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004203 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004204}
4205
Dan Gohman8181bd12008-07-27 21:46:04 +00004206SDValue DAGCombiner::visitFP_EXTEND(SDNode *N) {
4207 SDValue N0 = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004208 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands92c43912008-06-06 12:08:01 +00004209 MVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004210
Chris Lattner6f981fc2007-12-29 06:55:23 +00004211 // If this is fp_round(fpextend), don't fold it, allow ourselves to be folded.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00004212 if (N->hasOneUse() &&
djgc2517d32009-01-26 04:35:06 +00004213 N->use_begin()->getOpcode() == ISD::FP_ROUND)
Dan Gohman8181bd12008-07-27 21:46:04 +00004214 return SDValue();
Chris Lattner5872a362008-01-17 07:00:52 +00004215
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004216 // fold (fp_extend c1fp) -> c1fp
Dale Johannesenb89072e2007-10-16 23:38:29 +00004217 if (N0CFP && VT != MVT::ppcf128)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004218 return DAG.getNode(ISD::FP_EXTEND, VT, N0);
Chris Lattner5872a362008-01-17 07:00:52 +00004219
4220 // Turn fp_extend(fp_round(X, 1)) -> x since the fp_round doesn't affect the
4221 // value of X.
Gabor Greifb420b9d2008-08-30 19:29:20 +00004222 if (N0.getOpcode() == ISD::FP_ROUND
4223 && N0.getNode()->getConstantOperandVal(1) == 1) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004224 SDValue In = N0.getOperand(0);
Chris Lattner5872a362008-01-17 07:00:52 +00004225 if (In.getValueType() == VT) return In;
Duncan Sandsec142ee2008-06-08 20:54:56 +00004226 if (VT.bitsLT(In.getValueType()))
Chris Lattner5872a362008-01-17 07:00:52 +00004227 return DAG.getNode(ISD::FP_ROUND, VT, In, N0.getOperand(1));
4228 return DAG.getNode(ISD::FP_EXTEND, VT, In);
4229 }
4230
4231 // fold (fpext (load x)) -> (fpext (fptrunc (extload x)))
Gabor Greif1c80d112008-08-28 21:40:38 +00004232 if (ISD::isNON_EXTLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00004233 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng08c171a2008-10-14 21:26:46 +00004234 TLI.isLoadExtLegal(ISD::EXTLOAD, N0.getValueType()))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004235 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8181bd12008-07-27 21:46:04 +00004236 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00004237 LN0->getBasePtr(), LN0->getSrcValue(),
4238 LN0->getSrcValueOffset(),
4239 N0.getValueType(),
4240 LN0->isVolatile(), LN0->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004241 CombineTo(N, ExtLoad);
Gabor Greifb420b9d2008-08-30 19:29:20 +00004242 CombineTo(N0.getNode(), DAG.getNode(ISD::FP_ROUND, N0.getValueType(),
4243 ExtLoad, DAG.getIntPtrConstant(1)),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004244 ExtLoad.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004245 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004246 }
Duncan Sands2418bec2008-06-13 19:07:40 +00004247
Dan Gohman8181bd12008-07-27 21:46:04 +00004248 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004249}
4250
Dan Gohman8181bd12008-07-27 21:46:04 +00004251SDValue DAGCombiner::visitFNEG(SDNode *N) {
4252 SDValue N0 = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004253
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00004254 if (isNegatibleForFree(N0, LegalOperations))
4255 return GetNegatedExpression(N0, DAG, LegalOperations);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004256
Chris Lattneref26cbc2008-01-27 17:42:27 +00004257 // Transform fneg(bitconvert(x)) -> bitconvert(x^sign) to avoid loading
4258 // constant pool values.
Gabor Greif1c80d112008-08-28 21:40:38 +00004259 if (N0.getOpcode() == ISD::BIT_CONVERT && N0.getNode()->hasOneUse() &&
Duncan Sands92c43912008-06-06 12:08:01 +00004260 N0.getOperand(0).getValueType().isInteger() &&
4261 !N0.getOperand(0).getValueType().isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004262 SDValue Int = N0.getOperand(0);
Duncan Sands92c43912008-06-06 12:08:01 +00004263 MVT IntVT = Int.getValueType();
4264 if (IntVT.isInteger() && !IntVT.isVector()) {
Chris Lattneref26cbc2008-01-27 17:42:27 +00004265 Int = DAG.getNode(ISD::XOR, IntVT, Int,
Duncan Sands92c43912008-06-06 12:08:01 +00004266 DAG.getConstant(IntVT.getIntegerVTSignBit(), IntVT));
Gabor Greif1c80d112008-08-28 21:40:38 +00004267 AddToWorkList(Int.getNode());
Chris Lattneref26cbc2008-01-27 17:42:27 +00004268 return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Int);
4269 }
4270 }
4271
Dan Gohman8181bd12008-07-27 21:46:04 +00004272 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004273}
4274
Dan Gohman8181bd12008-07-27 21:46:04 +00004275SDValue DAGCombiner::visitFABS(SDNode *N) {
4276 SDValue N0 = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004277 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands92c43912008-06-06 12:08:01 +00004278 MVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004279
4280 // fold (fabs c1) -> fabs(c1)
Dale Johannesenb89072e2007-10-16 23:38:29 +00004281 if (N0CFP && VT != MVT::ppcf128)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004282 return DAG.getNode(ISD::FABS, VT, N0);
4283 // fold (fabs (fabs x)) -> (fabs x)
4284 if (N0.getOpcode() == ISD::FABS)
4285 return N->getOperand(0);
4286 // fold (fabs (fneg x)) -> (fabs x)
4287 // fold (fabs (fcopysign x, y)) -> (fabs x)
4288 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
4289 return DAG.getNode(ISD::FABS, VT, N0.getOperand(0));
4290
Chris Lattneref26cbc2008-01-27 17:42:27 +00004291 // Transform fabs(bitconvert(x)) -> bitconvert(x&~sign) to avoid loading
4292 // constant pool values.
Gabor Greif1c80d112008-08-28 21:40:38 +00004293 if (N0.getOpcode() == ISD::BIT_CONVERT && N0.getNode()->hasOneUse() &&
Duncan Sands92c43912008-06-06 12:08:01 +00004294 N0.getOperand(0).getValueType().isInteger() &&
4295 !N0.getOperand(0).getValueType().isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004296 SDValue Int = N0.getOperand(0);
Duncan Sands92c43912008-06-06 12:08:01 +00004297 MVT IntVT = Int.getValueType();
4298 if (IntVT.isInteger() && !IntVT.isVector()) {
Chris Lattneref26cbc2008-01-27 17:42:27 +00004299 Int = DAG.getNode(ISD::AND, IntVT, Int,
Duncan Sands92c43912008-06-06 12:08:01 +00004300 DAG.getConstant(~IntVT.getIntegerVTSignBit(), IntVT));
Gabor Greif1c80d112008-08-28 21:40:38 +00004301 AddToWorkList(Int.getNode());
Chris Lattneref26cbc2008-01-27 17:42:27 +00004302 return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Int);
4303 }
4304 }
4305
Dan Gohman8181bd12008-07-27 21:46:04 +00004306 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004307}
4308
Dan Gohman8181bd12008-07-27 21:46:04 +00004309SDValue DAGCombiner::visitBRCOND(SDNode *N) {
4310 SDValue Chain = N->getOperand(0);
4311 SDValue N1 = N->getOperand(1);
4312 SDValue N2 = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004313 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
4314
4315 // never taken branch, fold to chain
4316 if (N1C && N1C->isNullValue())
4317 return Chain;
4318 // unconditional branch
Dan Gohman9d24dc72008-03-13 22:13:53 +00004319 if (N1C && N1C->getAPIntValue() == 1)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004320 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
4321 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
4322 // on the target.
4323 if (N1.getOpcode() == ISD::SETCC &&
Dan Gohman52c51aa2009-01-28 17:46:25 +00004324 TLI.isOperationLegalOrCustom(ISD::BR_CC, MVT::Other)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004325 return DAG.getNode(ISD::BR_CC, MVT::Other, Chain, N1.getOperand(2),
4326 N1.getOperand(0), N1.getOperand(1), N2);
4327 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004328 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004329}
4330
4331// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
4332//
Dan Gohman8181bd12008-07-27 21:46:04 +00004333SDValue DAGCombiner::visitBR_CC(SDNode *N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004334 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004335 SDValue CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004336
Duncan Sands6a437fb2008-06-09 11:32:28 +00004337 // Use SimplifySetCC to simplify SETCC's.
Duncan Sands4a361272009-01-01 15:52:00 +00004338 SDValue Simp = SimplifySetCC(TLI.getSetCCResultType(CondLHS.getValueType()),
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00004339 CondLHS, CondRHS, CC->get(), false);
Gabor Greif1c80d112008-08-28 21:40:38 +00004340 if (Simp.getNode()) AddToWorkList(Simp.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004341
Gabor Greif1c80d112008-08-28 21:40:38 +00004342 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004343
4344 // fold br_cc true, dest -> br dest (unconditional branch)
Dan Gohman9d24dc72008-03-13 22:13:53 +00004345 if (SCCC && !SCCC->isNullValue())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004346 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
4347 N->getOperand(4));
4348 // fold br_cc false, dest -> unconditional fall through
4349 if (SCCC && SCCC->isNullValue())
4350 return N->getOperand(0);
4351
4352 // fold to a simpler setcc
Gabor Greif1c80d112008-08-28 21:40:38 +00004353 if (Simp.getNode() && Simp.getOpcode() == ISD::SETCC)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004354 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
4355 Simp.getOperand(2), Simp.getOperand(0),
4356 Simp.getOperand(1), N->getOperand(4));
Dan Gohman8181bd12008-07-27 21:46:04 +00004357 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004358}
4359
4360
Duncan Sandsc218a5a2008-06-15 20:12:31 +00004361/// CombineToPreIndexedLoadStore - Try turning a load / store into a
4362/// pre-indexed load / store when the base pointer is an add or subtract
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004363/// and it has other uses besides the load / store. After the
4364/// transformation, the new indexed load / store has effectively folded
4365/// the add / subtract in and all of its other uses are redirected to the
4366/// new load / store.
4367bool DAGCombiner::CombineToPreIndexedLoadStore(SDNode *N) {
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00004368 if (!LegalOperations)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004369 return false;
4370
4371 bool isLoad = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00004372 SDValue Ptr;
Duncan Sands92c43912008-06-06 12:08:01 +00004373 MVT VT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004374 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattner3bc08502008-01-17 19:59:44 +00004375 if (LD->isIndexed())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004376 return false;
Dan Gohman9a4c92c2008-01-30 00:15:11 +00004377 VT = LD->getMemoryVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004378 if (!TLI.isIndexedLoadLegal(ISD::PRE_INC, VT) &&
4379 !TLI.isIndexedLoadLegal(ISD::PRE_DEC, VT))
4380 return false;
4381 Ptr = LD->getBasePtr();
4382 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattner3bc08502008-01-17 19:59:44 +00004383 if (ST->isIndexed())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004384 return false;
Dan Gohman9a4c92c2008-01-30 00:15:11 +00004385 VT = ST->getMemoryVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004386 if (!TLI.isIndexedStoreLegal(ISD::PRE_INC, VT) &&
4387 !TLI.isIndexedStoreLegal(ISD::PRE_DEC, VT))
4388 return false;
4389 Ptr = ST->getBasePtr();
4390 isLoad = false;
4391 } else
4392 return false;
4393
4394 // If the pointer is not an add/sub, or if it doesn't have multiple uses, bail
4395 // out. There is no reason to make this a preinc/predec.
4396 if ((Ptr.getOpcode() != ISD::ADD && Ptr.getOpcode() != ISD::SUB) ||
Gabor Greif1c80d112008-08-28 21:40:38 +00004397 Ptr.getNode()->hasOneUse())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004398 return false;
4399
4400 // Ask the target to do addressing mode selection.
Dan Gohman8181bd12008-07-27 21:46:04 +00004401 SDValue BasePtr;
4402 SDValue Offset;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004403 ISD::MemIndexedMode AM = ISD::UNINDEXED;
4404 if (!TLI.getPreIndexedAddressParts(N, BasePtr, Offset, AM, DAG))
4405 return false;
4406 // Don't create a indexed load / store with zero offset.
4407 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman9d24dc72008-03-13 22:13:53 +00004408 cast<ConstantSDNode>(Offset)->isNullValue())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004409 return false;
4410
4411 // Try turning it into a pre-indexed load / store except when:
4412 // 1) The new base ptr is a frame index.
4413 // 2) If N is a store and the new base ptr is either the same as or is a
4414 // predecessor of the value being stored.
4415 // 3) Another use of old base ptr is a predecessor of N. If ptr is folded
4416 // that would create a cycle.
4417 // 4) All uses are load / store ops that use it as old base ptr.
4418
4419 // Check #1. Preinc'ing a frame index would require copying the stack pointer
4420 // (plus the implicit offset) to a register to preinc anyway.
4421 if (isa<FrameIndexSDNode>(BasePtr))
4422 return false;
4423
4424 // Check #2.
4425 if (!isLoad) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004426 SDValue Val = cast<StoreSDNode>(N)->getValue();
Gabor Greif1c80d112008-08-28 21:40:38 +00004427 if (Val == BasePtr || BasePtr.getNode()->isPredecessorOf(Val.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004428 return false;
4429 }
4430
4431 // Now check for #3 and #4.
4432 bool RealUse = false;
Gabor Greif1c80d112008-08-28 21:40:38 +00004433 for (SDNode::use_iterator I = Ptr.getNode()->use_begin(),
4434 E = Ptr.getNode()->use_end(); I != E; ++I) {
Dan Gohman0c97f1d2008-07-27 20:43:25 +00004435 SDNode *Use = *I;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004436 if (Use == N)
4437 continue;
Evan Chengd9387682008-03-04 00:41:45 +00004438 if (Use->isPredecessorOf(N))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004439 return false;
4440
4441 if (!((Use->getOpcode() == ISD::LOAD &&
4442 cast<LoadSDNode>(Use)->getBasePtr() == Ptr) ||
Anton Korobeynikov53422f62008-02-20 11:10:28 +00004443 (Use->getOpcode() == ISD::STORE &&
4444 cast<StoreSDNode>(Use)->getBasePtr() == Ptr)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004445 RealUse = true;
4446 }
4447 if (!RealUse)
4448 return false;
4449
Dan Gohman8181bd12008-07-27 21:46:04 +00004450 SDValue Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004451 if (isLoad)
Dan Gohman8181bd12008-07-27 21:46:04 +00004452 Result = DAG.getIndexedLoad(SDValue(N,0), BasePtr, Offset, AM);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004453 else
Dan Gohman8181bd12008-07-27 21:46:04 +00004454 Result = DAG.getIndexedStore(SDValue(N,0), BasePtr, Offset, AM);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004455 ++PreIndexedNodes;
4456 ++NodesCombined;
4457 DOUT << "\nReplacing.4 "; DEBUG(N->dump(&DAG));
Gabor Greif1c80d112008-08-28 21:40:38 +00004458 DOUT << "\nWith: "; DEBUG(Result.getNode()->dump(&DAG));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004459 DOUT << '\n';
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004460 WorkListRemover DeadNodes(*this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004461 if (isLoad) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004462 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0),
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004463 &DeadNodes);
Dan Gohman8181bd12008-07-27 21:46:04 +00004464 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2),
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004465 &DeadNodes);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004466 } else {
Dan Gohman8181bd12008-07-27 21:46:04 +00004467 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1),
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004468 &DeadNodes);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004469 }
4470
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004471 // Finally, since the node is now dead, remove it from the graph.
4472 DAG.DeleteNode(N);
4473
4474 // Replace the uses of Ptr with uses of the updated base value.
4475 DAG.ReplaceAllUsesOfValueWith(Ptr, Result.getValue(isLoad ? 1 : 0),
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004476 &DeadNodes);
Gabor Greif1c80d112008-08-28 21:40:38 +00004477 removeFromWorkList(Ptr.getNode());
4478 DAG.DeleteNode(Ptr.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004479
4480 return true;
4481}
4482
Duncan Sandsc218a5a2008-06-15 20:12:31 +00004483/// CombineToPostIndexedLoadStore - Try to combine a load / store with a
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004484/// add / sub of the base pointer node into a post-indexed load / store.
4485/// The transformation folded the add / subtract into the new indexed
4486/// load / store effectively and all of its uses are redirected to the
4487/// new load / store.
4488bool DAGCombiner::CombineToPostIndexedLoadStore(SDNode *N) {
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00004489 if (!LegalOperations)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004490 return false;
4491
4492 bool isLoad = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00004493 SDValue Ptr;
Duncan Sands92c43912008-06-06 12:08:01 +00004494 MVT VT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004495 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattner3bc08502008-01-17 19:59:44 +00004496 if (LD->isIndexed())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004497 return false;
Dan Gohman9a4c92c2008-01-30 00:15:11 +00004498 VT = LD->getMemoryVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004499 if (!TLI.isIndexedLoadLegal(ISD::POST_INC, VT) &&
4500 !TLI.isIndexedLoadLegal(ISD::POST_DEC, VT))
4501 return false;
4502 Ptr = LD->getBasePtr();
4503 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattner3bc08502008-01-17 19:59:44 +00004504 if (ST->isIndexed())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004505 return false;
Dan Gohman9a4c92c2008-01-30 00:15:11 +00004506 VT = ST->getMemoryVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004507 if (!TLI.isIndexedStoreLegal(ISD::POST_INC, VT) &&
4508 !TLI.isIndexedStoreLegal(ISD::POST_DEC, VT))
4509 return false;
4510 Ptr = ST->getBasePtr();
4511 isLoad = false;
4512 } else
4513 return false;
4514
Gabor Greif1c80d112008-08-28 21:40:38 +00004515 if (Ptr.getNode()->hasOneUse())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004516 return false;
4517
Gabor Greif1c80d112008-08-28 21:40:38 +00004518 for (SDNode::use_iterator I = Ptr.getNode()->use_begin(),
4519 E = Ptr.getNode()->use_end(); I != E; ++I) {
Dan Gohman0c97f1d2008-07-27 20:43:25 +00004520 SDNode *Op = *I;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004521 if (Op == N ||
4522 (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB))
4523 continue;
4524
Dan Gohman8181bd12008-07-27 21:46:04 +00004525 SDValue BasePtr;
4526 SDValue Offset;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004527 ISD::MemIndexedMode AM = ISD::UNINDEXED;
4528 if (TLI.getPostIndexedAddressParts(N, Op, BasePtr, Offset, AM, DAG)) {
4529 if (Ptr == Offset)
4530 std::swap(BasePtr, Offset);
4531 if (Ptr != BasePtr)
4532 continue;
4533 // Don't create a indexed load / store with zero offset.
4534 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman9d24dc72008-03-13 22:13:53 +00004535 cast<ConstantSDNode>(Offset)->isNullValue())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004536 continue;
4537
4538 // Try turning it into a post-indexed load / store except when
4539 // 1) All uses are load / store ops that use it as base ptr.
4540 // 2) Op must be independent of N, i.e. Op is neither a predecessor
4541 // nor a successor of N. Otherwise, if Op is folded that would
4542 // create a cycle.
4543
4544 // Check for #1.
4545 bool TryNext = false;
Gabor Greif1c80d112008-08-28 21:40:38 +00004546 for (SDNode::use_iterator II = BasePtr.getNode()->use_begin(),
4547 EE = BasePtr.getNode()->use_end(); II != EE; ++II) {
Dan Gohman0c97f1d2008-07-27 20:43:25 +00004548 SDNode *Use = *II;
Gabor Greif1c80d112008-08-28 21:40:38 +00004549 if (Use == Ptr.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004550 continue;
4551
4552 // If all the uses are load / store addresses, then don't do the
4553 // transformation.
4554 if (Use->getOpcode() == ISD::ADD || Use->getOpcode() == ISD::SUB){
4555 bool RealUse = false;
4556 for (SDNode::use_iterator III = Use->use_begin(),
4557 EEE = Use->use_end(); III != EEE; ++III) {
Dan Gohman0c97f1d2008-07-27 20:43:25 +00004558 SDNode *UseUse = *III;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004559 if (!((UseUse->getOpcode() == ISD::LOAD &&
Gabor Greif1c80d112008-08-28 21:40:38 +00004560 cast<LoadSDNode>(UseUse)->getBasePtr().getNode() == Use) ||
Anton Korobeynikov53422f62008-02-20 11:10:28 +00004561 (UseUse->getOpcode() == ISD::STORE &&
Gabor Greif1c80d112008-08-28 21:40:38 +00004562 cast<StoreSDNode>(UseUse)->getBasePtr().getNode() == Use)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004563 RealUse = true;
4564 }
4565
4566 if (!RealUse) {
4567 TryNext = true;
4568 break;
4569 }
4570 }
4571 }
4572 if (TryNext)
4573 continue;
4574
4575 // Check for #2
Evan Chengd9387682008-03-04 00:41:45 +00004576 if (!Op->isPredecessorOf(N) && !N->isPredecessorOf(Op)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004577 SDValue Result = isLoad
4578 ? DAG.getIndexedLoad(SDValue(N,0), BasePtr, Offset, AM)
4579 : DAG.getIndexedStore(SDValue(N,0), BasePtr, Offset, AM);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004580 ++PostIndexedNodes;
4581 ++NodesCombined;
4582 DOUT << "\nReplacing.5 "; DEBUG(N->dump(&DAG));
Gabor Greif1c80d112008-08-28 21:40:38 +00004583 DOUT << "\nWith: "; DEBUG(Result.getNode()->dump(&DAG));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004584 DOUT << '\n';
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004585 WorkListRemover DeadNodes(*this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004586 if (isLoad) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004587 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0),
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004588 &DeadNodes);
Dan Gohman8181bd12008-07-27 21:46:04 +00004589 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2),
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004590 &DeadNodes);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004591 } else {
Dan Gohman8181bd12008-07-27 21:46:04 +00004592 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1),
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004593 &DeadNodes);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004594 }
4595
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004596 // Finally, since the node is now dead, remove it from the graph.
4597 DAG.DeleteNode(N);
4598
4599 // Replace the uses of Use with uses of the updated base value.
Dan Gohman8181bd12008-07-27 21:46:04 +00004600 DAG.ReplaceAllUsesOfValueWith(SDValue(Op, 0),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004601 Result.getValue(isLoad ? 1 : 0),
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004602 &DeadNodes);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004603 removeFromWorkList(Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004604 DAG.DeleteNode(Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004605 return true;
4606 }
4607 }
4608 }
4609 return false;
4610}
4611
Chris Lattner4e137af2008-01-25 07:20:16 +00004612/// InferAlignment - If we can infer some alignment information from this
4613/// pointer, return it.
Dan Gohman8181bd12008-07-27 21:46:04 +00004614static unsigned InferAlignment(SDValue Ptr, SelectionDAG &DAG) {
Chris Lattner4e137af2008-01-25 07:20:16 +00004615 // If this is a direct reference to a stack slot, use information about the
4616 // stack slot's alignment.
Chris Lattner1e3362f2008-01-26 19:45:50 +00004617 int FrameIdx = 1 << 31;
4618 int64_t FrameOffset = 0;
Chris Lattner4e137af2008-01-25 07:20:16 +00004619 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
Chris Lattner1e3362f2008-01-26 19:45:50 +00004620 FrameIdx = FI->getIndex();
4621 } else if (Ptr.getOpcode() == ISD::ADD &&
4622 isa<ConstantSDNode>(Ptr.getOperand(1)) &&
4623 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4624 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4625 FrameOffset = Ptr.getConstantOperandVal(1);
Chris Lattner4e137af2008-01-25 07:20:16 +00004626 }
Chris Lattner1e3362f2008-01-26 19:45:50 +00004627
4628 if (FrameIdx != (1 << 31)) {
4629 // FIXME: Handle FI+CST.
4630 const MachineFrameInfo &MFI = *DAG.getMachineFunction().getFrameInfo();
4631 if (MFI.isFixedObjectIndex(FrameIdx)) {
Dan Gohmanb0a2ff92008-08-11 18:27:03 +00004632 int64_t ObjectOffset = MFI.getObjectOffset(FrameIdx) + FrameOffset;
Chris Lattner1e3362f2008-01-26 19:45:50 +00004633
4634 // The alignment of the frame index can be determined from its offset from
4635 // the incoming frame position. If the frame object is at offset 32 and
4636 // the stack is guaranteed to be 16-byte aligned, then we know that the
4637 // object is 16-byte aligned.
4638 unsigned StackAlign = DAG.getTarget().getFrameInfo()->getStackAlignment();
4639 unsigned Align = MinAlign(ObjectOffset, StackAlign);
4640
4641 // Finally, the frame object itself may have a known alignment. Factor
4642 // the alignment + offset into a new alignment. For example, if we know
4643 // the FI is 8 byte aligned, but the pointer is 4 off, we really have a
4644 // 4-byte alignment of the resultant pointer. Likewise align 4 + 4-byte
4645 // offset = 4-byte alignment, align 4 + 1-byte offset = align 1, etc.
4646 unsigned FIInfoAlign = MinAlign(MFI.getObjectAlignment(FrameIdx),
4647 FrameOffset);
4648 return std::max(Align, FIInfoAlign);
4649 }
4650 }
Chris Lattner4e137af2008-01-25 07:20:16 +00004651
4652 return 0;
4653}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004654
Dan Gohman8181bd12008-07-27 21:46:04 +00004655SDValue DAGCombiner::visitLOAD(SDNode *N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004656 LoadSDNode *LD = cast<LoadSDNode>(N);
Dan Gohman8181bd12008-07-27 21:46:04 +00004657 SDValue Chain = LD->getChain();
4658 SDValue Ptr = LD->getBasePtr();
Chris Lattner4e137af2008-01-25 07:20:16 +00004659
4660 // Try to infer better alignment information than the load already has.
Dan Gohmanea12c0c2008-08-20 16:30:28 +00004661 if (!Fast && LD->isUnindexed()) {
Chris Lattner4e137af2008-01-25 07:20:16 +00004662 if (unsigned Align = InferAlignment(Ptr, DAG)) {
4663 if (Align > LD->getAlignment())
4664 return DAG.getExtLoad(LD->getExtensionType(), LD->getValueType(0),
4665 Chain, Ptr, LD->getSrcValue(),
Dan Gohman9a4c92c2008-01-30 00:15:11 +00004666 LD->getSrcValueOffset(), LD->getMemoryVT(),
Chris Lattner4e137af2008-01-25 07:20:16 +00004667 LD->isVolatile(), Align);
4668 }
4669 }
4670
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004671
4672 // If load is not volatile and there are no uses of the loaded value (and
4673 // the updated indexed value in case of indexed loads), change uses of the
4674 // chain value into uses of the chain input (i.e. delete the dead load).
4675 if (!LD->isVolatile()) {
4676 if (N->getValueType(1) == MVT::Other) {
4677 // Unindexed loads.
Evan Chenge8b886a2008-01-16 23:11:54 +00004678 if (N->hasNUsesOfValue(0, 0)) {
4679 // It's not safe to use the two value CombineTo variant here. e.g.
4680 // v1, chain2 = load chain1, loc
4681 // v2, chain3 = load chain2, loc
4682 // v3 = add v2, c
Chris Lattnerbb67c192008-01-24 07:57:06 +00004683 // Now we replace use of chain2 with chain1. This makes the second load
4684 // isomorphic to the one we are deleting, and thus makes this load live.
Evan Chenge8b886a2008-01-16 23:11:54 +00004685 DOUT << "\nReplacing.6 "; DEBUG(N->dump(&DAG));
Gabor Greif1c80d112008-08-28 21:40:38 +00004686 DOUT << "\nWith chain: "; DEBUG(Chain.getNode()->dump(&DAG));
Chris Lattnerbb67c192008-01-24 07:57:06 +00004687 DOUT << "\n";
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004688 WorkListRemover DeadNodes(*this);
Dan Gohman8181bd12008-07-27 21:46:04 +00004689 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Chain, &DeadNodes);
Chris Lattnerbb67c192008-01-24 07:57:06 +00004690 if (N->use_empty()) {
4691 removeFromWorkList(N);
4692 DAG.DeleteNode(N);
4693 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004694 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Chenge8b886a2008-01-16 23:11:54 +00004695 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004696 } else {
4697 // Indexed loads.
4698 assert(N->getValueType(2) == MVT::Other && "Malformed indexed loads?");
4699 if (N->hasNUsesOfValue(0, 0) && N->hasNUsesOfValue(0, 1)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004700 SDValue Undef = DAG.getNode(ISD::UNDEF, N->getValueType(0));
Evan Chenge8b886a2008-01-16 23:11:54 +00004701 DOUT << "\nReplacing.6 "; DEBUG(N->dump(&DAG));
Gabor Greif1c80d112008-08-28 21:40:38 +00004702 DOUT << "\nWith: "; DEBUG(Undef.getNode()->dump(&DAG));
Evan Chenge8b886a2008-01-16 23:11:54 +00004703 DOUT << " and 2 other values\n";
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004704 WorkListRemover DeadNodes(*this);
Dan Gohman8181bd12008-07-27 21:46:04 +00004705 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Undef, &DeadNodes);
4706 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1),
Chris Lattner667f9c12008-01-17 07:20:38 +00004707 DAG.getNode(ISD::UNDEF, N->getValueType(1)),
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004708 &DeadNodes);
Dan Gohman8181bd12008-07-27 21:46:04 +00004709 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 2), Chain, &DeadNodes);
Evan Chenge8b886a2008-01-16 23:11:54 +00004710 removeFromWorkList(N);
Evan Chenge8b886a2008-01-16 23:11:54 +00004711 DAG.DeleteNode(N);
Dan Gohman8181bd12008-07-27 21:46:04 +00004712 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004713 }
4714 }
4715 }
4716
4717 // If this load is directly stored, replace the load value with the stored
4718 // value.
4719 // TODO: Handle store large -> read small portion.
4720 // TODO: Handle TRUNCSTORE/LOADEXT
Dan Gohman729b5ff2008-03-31 20:32:52 +00004721 if (LD->getExtensionType() == ISD::NON_EXTLOAD &&
4722 !LD->isVolatile()) {
Gabor Greif1c80d112008-08-28 21:40:38 +00004723 if (ISD::isNON_TRUNCStore(Chain.getNode())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004724 StoreSDNode *PrevST = cast<StoreSDNode>(Chain);
4725 if (PrevST->getBasePtr() == Ptr &&
4726 PrevST->getValue().getValueType() == N->getValueType(0))
4727 return CombineTo(N, Chain.getOperand(1), Chain);
4728 }
4729 }
4730
4731 if (CombinerAA) {
4732 // Walk up chain skipping non-aliasing memory nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00004733 SDValue BetterChain = FindBetterChain(N, Chain);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004734
4735 // If there is a better chain.
4736 if (Chain != BetterChain) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004737 SDValue ReplLoad;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004738
4739 // Replace the chain to void dependency.
4740 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
4741 ReplLoad = DAG.getLoad(N->getValueType(0), BetterChain, Ptr,
Duncan Sandsa3691432007-10-28 12:59:45 +00004742 LD->getSrcValue(), LD->getSrcValueOffset(),
4743 LD->isVolatile(), LD->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004744 } else {
4745 ReplLoad = DAG.getExtLoad(LD->getExtensionType(),
4746 LD->getValueType(0),
4747 BetterChain, Ptr, LD->getSrcValue(),
4748 LD->getSrcValueOffset(),
Dan Gohman9a4c92c2008-01-30 00:15:11 +00004749 LD->getMemoryVT(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004750 LD->isVolatile(),
4751 LD->getAlignment());
4752 }
4753
4754 // Create token factor to keep old chain connected.
Dan Gohman8181bd12008-07-27 21:46:04 +00004755 SDValue Token = DAG.getNode(ISD::TokenFactor, MVT::Other,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004756 Chain, ReplLoad.getValue(1));
4757
4758 // Replace uses with load result and token factor. Don't add users
4759 // to work list.
4760 return CombineTo(N, ReplLoad.getValue(0), Token, false);
4761 }
4762 }
4763
4764 // Try transforming N to an indexed load.
4765 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Dan Gohman8181bd12008-07-27 21:46:04 +00004766 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004767
Dan Gohman8181bd12008-07-27 21:46:04 +00004768 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004769}
4770
Chris Lattner2e023772008-01-08 23:08:06 +00004771
Dan Gohman8181bd12008-07-27 21:46:04 +00004772SDValue DAGCombiner::visitSTORE(SDNode *N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004773 StoreSDNode *ST = cast<StoreSDNode>(N);
Dan Gohman8181bd12008-07-27 21:46:04 +00004774 SDValue Chain = ST->getChain();
4775 SDValue Value = ST->getValue();
4776 SDValue Ptr = ST->getBasePtr();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004777
Chris Lattner4e137af2008-01-25 07:20:16 +00004778 // Try to infer better alignment information than the store already has.
Dan Gohmanea12c0c2008-08-20 16:30:28 +00004779 if (!Fast && ST->isUnindexed()) {
Chris Lattner4e137af2008-01-25 07:20:16 +00004780 if (unsigned Align = InferAlignment(Ptr, DAG)) {
4781 if (Align > ST->getAlignment())
4782 return DAG.getTruncStore(Chain, Value, Ptr, ST->getSrcValue(),
Dan Gohman9a4c92c2008-01-30 00:15:11 +00004783 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattner4e137af2008-01-25 07:20:16 +00004784 ST->isVolatile(), Align);
4785 }
4786 }
Duncan Sands2418bec2008-06-13 19:07:40 +00004787
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004788 // If this is a store of a bit convert, store the input value if the
4789 // resultant store does not need a higher alignment than the original.
4790 if (Value.getOpcode() == ISD::BIT_CONVERT && !ST->isTruncatingStore() &&
Chris Lattner3bc08502008-01-17 19:59:44 +00004791 ST->isUnindexed()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004792 unsigned Align = ST->getAlignment();
Duncan Sands92c43912008-06-06 12:08:01 +00004793 MVT SVT = Value.getOperand(0).getValueType();
Dan Gohman404e8542008-09-04 15:39:15 +00004794 unsigned OrigAlign = TLI.getTargetData()->
Duncan Sands92c43912008-06-06 12:08:01 +00004795 getABITypeAlignment(SVT.getTypeForMVT());
Duncan Sands2418bec2008-06-13 19:07:40 +00004796 if (Align <= OrigAlign &&
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00004797 ((!LegalOperations && !ST->isVolatile()) ||
Dan Gohman52c51aa2009-01-28 17:46:25 +00004798 TLI.isOperationLegalOrCustom(ISD::STORE, SVT)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004799 return DAG.getStore(Chain, Value.getOperand(0), Ptr, ST->getSrcValue(),
Dan Gohman55a11de2008-06-28 00:45:22 +00004800 ST->getSrcValueOffset(), ST->isVolatile(), OrigAlign);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004801 }
Duncan Sands2418bec2008-06-13 19:07:40 +00004802
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004803 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
4804 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Value)) {
Duncan Sands2418bec2008-06-13 19:07:40 +00004805 // NOTE: If the original store is volatile, this transform must not increase
4806 // the number of stores. For example, on x86-32 an f64 can be stored in one
4807 // processor operation but an i64 (which is not legal) requires two. So the
4808 // transform should not be done in this case.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004809 if (Value.getOpcode() != ISD::TargetConstantFP) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004810 SDValue Tmp;
Duncan Sands92c43912008-06-06 12:08:01 +00004811 switch (CFP->getValueType(0).getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004812 default: assert(0 && "Unknown FP type");
Dale Johannesen1b4181d2007-09-18 18:36:59 +00004813 case MVT::f80: // We don't do this for these yet.
4814 case MVT::f128:
4815 case MVT::ppcf128:
4816 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004817 case MVT::f32:
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00004818 if (((TLI.isTypeLegal(MVT::i32) || !LegalTypes) && !LegalOperations &&
Dan Gohman52c51aa2009-01-28 17:46:25 +00004819 !ST->isVolatile()) ||
4820 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i32)) {
Dale Johannesenfbd9cda2007-09-12 03:30:33 +00004821 Tmp = DAG.getConstant((uint32_t)CFP->getValueAPF().
Dale Johannesen49cc7ce2008-10-09 18:53:47 +00004822 bitcastToAPInt().getZExtValue(), MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004823 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
4824 ST->getSrcValueOffset(), ST->isVolatile(),
4825 ST->getAlignment());
4826 }
4827 break;
4828 case MVT::f64:
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00004829 if (((TLI.isTypeLegal(MVT::i64) || !LegalTypes) && !LegalOperations &&
Dan Gohman52c51aa2009-01-28 17:46:25 +00004830 !ST->isVolatile()) ||
4831 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i64)) {
Dale Johannesen49cc7ce2008-10-09 18:53:47 +00004832 Tmp = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
Dale Johannesenfbd9cda2007-09-12 03:30:33 +00004833 getZExtValue(), MVT::i64);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004834 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
4835 ST->getSrcValueOffset(), ST->isVolatile(),
4836 ST->getAlignment());
Duncan Sands2418bec2008-06-13 19:07:40 +00004837 } else if (!ST->isVolatile() &&
Dan Gohman52c51aa2009-01-28 17:46:25 +00004838 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i32)) {
Duncan Sandsa3691432007-10-28 12:59:45 +00004839 // Many FP stores are not made apparent until after legalize, e.g. for
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004840 // argument passing. Since this is so common, custom legalize the
4841 // 64-bit integer store into two 32-bit stores.
Dale Johannesen49cc7ce2008-10-09 18:53:47 +00004842 uint64_t Val = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00004843 SDValue Lo = DAG.getConstant(Val & 0xFFFFFFFF, MVT::i32);
4844 SDValue Hi = DAG.getConstant(Val >> 32, MVT::i32);
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00004845 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004846
4847 int SVOffset = ST->getSrcValueOffset();
4848 unsigned Alignment = ST->getAlignment();
4849 bool isVolatile = ST->isVolatile();
4850
Dan Gohman8181bd12008-07-27 21:46:04 +00004851 SDValue St0 = DAG.getStore(Chain, Lo, Ptr, ST->getSrcValue(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004852 ST->getSrcValueOffset(),
4853 isVolatile, ST->getAlignment());
4854 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
4855 DAG.getConstant(4, Ptr.getValueType()));
4856 SVOffset += 4;
Duncan Sandsa3691432007-10-28 12:59:45 +00004857 Alignment = MinAlign(Alignment, 4U);
Dan Gohman8181bd12008-07-27 21:46:04 +00004858 SDValue St1 = DAG.getStore(Chain, Hi, Ptr, ST->getSrcValue(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004859 SVOffset, isVolatile, Alignment);
4860 return DAG.getNode(ISD::TokenFactor, MVT::Other, St0, St1);
4861 }
4862 break;
4863 }
4864 }
4865 }
4866
4867 if (CombinerAA) {
4868 // Walk up chain skipping non-aliasing memory nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00004869 SDValue BetterChain = FindBetterChain(N, Chain);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004870
4871 // If there is a better chain.
4872 if (Chain != BetterChain) {
4873 // Replace the chain to avoid dependency.
Dan Gohman8181bd12008-07-27 21:46:04 +00004874 SDValue ReplStore;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004875 if (ST->isTruncatingStore()) {
4876 ReplStore = DAG.getTruncStore(BetterChain, Value, Ptr,
Chris Lattner667f9c12008-01-17 07:20:38 +00004877 ST->getSrcValue(),ST->getSrcValueOffset(),
Dan Gohman9a4c92c2008-01-30 00:15:11 +00004878 ST->getMemoryVT(),
Chris Lattner667f9c12008-01-17 07:20:38 +00004879 ST->isVolatile(), ST->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004880 } else {
4881 ReplStore = DAG.getStore(BetterChain, Value, Ptr,
Chris Lattner667f9c12008-01-17 07:20:38 +00004882 ST->getSrcValue(), ST->getSrcValueOffset(),
4883 ST->isVolatile(), ST->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004884 }
4885
4886 // Create token to keep both nodes around.
Dan Gohman8181bd12008-07-27 21:46:04 +00004887 SDValue Token =
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004888 DAG.getNode(ISD::TokenFactor, MVT::Other, Chain, ReplStore);
4889
4890 // Don't add users to work list.
4891 return CombineTo(N, Token, false);
4892 }
4893 }
4894
4895 // Try transforming N to an indexed store.
4896 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Dan Gohman8181bd12008-07-27 21:46:04 +00004897 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004898
Chris Lattner447d8e82007-12-29 06:26:16 +00004899 // FIXME: is there such a thing as a truncating indexed store?
Chris Lattner3bc08502008-01-17 19:59:44 +00004900 if (ST->isTruncatingStore() && ST->isUnindexed() &&
Duncan Sands92c43912008-06-06 12:08:01 +00004901 Value.getValueType().isInteger()) {
Chris Lattnere8671c52007-10-13 06:35:54 +00004902 // See if we can simplify the input to this truncstore with knowledge that
4903 // only the low bits are being used. For example:
4904 // "truncstore (or (shl x, 8), y), i8" -> "truncstore y, i8"
Dan Gohman8181bd12008-07-27 21:46:04 +00004905 SDValue Shorter =
Dan Gohman07961cd2008-02-25 21:11:39 +00004906 GetDemandedBits(Value,
4907 APInt::getLowBitsSet(Value.getValueSizeInBits(),
Duncan Sands92c43912008-06-06 12:08:01 +00004908 ST->getMemoryVT().getSizeInBits()));
Gabor Greif1c80d112008-08-28 21:40:38 +00004909 AddToWorkList(Value.getNode());
4910 if (Shorter.getNode())
Chris Lattnere8671c52007-10-13 06:35:54 +00004911 return DAG.getTruncStore(Chain, Shorter, Ptr, ST->getSrcValue(),
Dan Gohman9a4c92c2008-01-30 00:15:11 +00004912 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattnere8671c52007-10-13 06:35:54 +00004913 ST->isVolatile(), ST->getAlignment());
Chris Lattnerb77ea552007-10-13 06:58:48 +00004914
4915 // Otherwise, see if we can simplify the operation with
4916 // SimplifyDemandedBits, which only works if the value has a single use.
Dan Gohman11607792008-02-27 00:25:32 +00004917 if (SimplifyDemandedBits(Value,
4918 APInt::getLowBitsSet(
4919 Value.getValueSizeInBits(),
Duncan Sands92c43912008-06-06 12:08:01 +00004920 ST->getMemoryVT().getSizeInBits())))
Dan Gohman8181bd12008-07-27 21:46:04 +00004921 return SDValue(N, 0);
Chris Lattnere8671c52007-10-13 06:35:54 +00004922 }
4923
Chris Lattner447d8e82007-12-29 06:26:16 +00004924 // If this is a load followed by a store to the same location, then the store
4925 // is dead/noop.
4926 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Value)) {
Dan Gohman9a4c92c2008-01-30 00:15:11 +00004927 if (Ld->getBasePtr() == Ptr && ST->getMemoryVT() == Ld->getMemoryVT() &&
Chris Lattner3bc08502008-01-17 19:59:44 +00004928 ST->isUnindexed() && !ST->isVolatile() &&
Chris Lattner2e023772008-01-08 23:08:06 +00004929 // There can't be any side effects between the load and store, such as
4930 // a call or store.
Dan Gohman8181bd12008-07-27 21:46:04 +00004931 Chain.reachesChainWithoutSideEffects(SDValue(Ld, 1))) {
Chris Lattner447d8e82007-12-29 06:26:16 +00004932 // The store is dead, remove it.
4933 return Chain;
4934 }
4935 }
Duncan Sands2418bec2008-06-13 19:07:40 +00004936
Chris Lattner3bc08502008-01-17 19:59:44 +00004937 // If this is an FP_ROUND or TRUNC followed by a store, fold this into a
4938 // truncating store. We can do this even if this is already a truncstore.
4939 if ((Value.getOpcode() == ISD::FP_ROUND || Value.getOpcode() == ISD::TRUNCATE)
Gabor Greif1c80d112008-08-28 21:40:38 +00004940 && Value.getNode()->hasOneUse() && ST->isUnindexed() &&
Chris Lattner3bc08502008-01-17 19:59:44 +00004941 TLI.isTruncStoreLegal(Value.getOperand(0).getValueType(),
Dan Gohman9a4c92c2008-01-30 00:15:11 +00004942 ST->getMemoryVT())) {
Chris Lattner3bc08502008-01-17 19:59:44 +00004943 return DAG.getTruncStore(Chain, Value.getOperand(0), Ptr, ST->getSrcValue(),
Dan Gohman9a4c92c2008-01-30 00:15:11 +00004944 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattner3bc08502008-01-17 19:59:44 +00004945 ST->isVolatile(), ST->getAlignment());
4946 }
Duncan Sands2418bec2008-06-13 19:07:40 +00004947
Dan Gohman8181bd12008-07-27 21:46:04 +00004948 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004949}
4950
Dan Gohman8181bd12008-07-27 21:46:04 +00004951SDValue DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
4952 SDValue InVec = N->getOperand(0);
4953 SDValue InVal = N->getOperand(1);
4954 SDValue EltNo = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004955
4956 // If the invec is a BUILD_VECTOR and if EltNo is a constant, build a new
4957 // vector with the inserted element.
4958 if (InVec.getOpcode() == ISD::BUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004959 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Gabor Greifb420b9d2008-08-30 19:29:20 +00004960 SmallVector<SDValue, 8> Ops(InVec.getNode()->op_begin(),
4961 InVec.getNode()->op_end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004962 if (Elt < Ops.size())
4963 Ops[Elt] = InVal;
4964 return DAG.getNode(ISD::BUILD_VECTOR, InVec.getValueType(),
4965 &Ops[0], Ops.size());
4966 }
4967
Dan Gohman8181bd12008-07-27 21:46:04 +00004968 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004969}
4970
Dan Gohman8181bd12008-07-27 21:46:04 +00004971SDValue DAGCombiner::visitEXTRACT_VECTOR_ELT(SDNode *N) {
Mon P Wang3512a532009-01-17 00:07:25 +00004972 // (vextract (scalar_to_vector val, 0) -> val
4973 SDValue InVec = N->getOperand(0);
Mon P Wang3512a532009-01-17 00:07:25 +00004974
Mon P Wang017cf182009-01-18 06:43:40 +00004975 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR)
4976 return InVec.getOperand(0);
Evan Cheng411fc172008-05-13 08:35:03 +00004977
4978 // Perform only after legalization to ensure build_vector / vector_shuffle
4979 // optimizations have already been done.
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00004980 if (!LegalOperations) return SDValue();
Evan Cheng411fc172008-05-13 08:35:03 +00004981
Mon P Wang3512a532009-01-17 00:07:25 +00004982 // (vextract (v4f32 load $addr), c) -> (f32 load $addr+c*size)
4983 // (vextract (v4f32 s2v (f32 load $addr)), c) -> (f32 load $addr+c*size)
4984 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), 0) -> (f32 load $addr)
Mon P Wang017cf182009-01-18 06:43:40 +00004985 SDValue EltNo = N->getOperand(1);
Evan Chengd7ba7ed2007-10-06 08:19:55 +00004986
Evan Chengd7ba7ed2007-10-06 08:19:55 +00004987 if (isa<ConstantSDNode>(EltNo)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004988 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Evan Chengd7ba7ed2007-10-06 08:19:55 +00004989 bool NewLoad = false;
Mon P Wang3c673102008-12-11 00:26:16 +00004990 bool BCNumEltsChanged = false;
Duncan Sands92c43912008-06-06 12:08:01 +00004991 MVT VT = InVec.getValueType();
4992 MVT EVT = VT.getVectorElementType();
4993 MVT LVT = EVT;
Evan Cheng411fc172008-05-13 08:35:03 +00004994 if (InVec.getOpcode() == ISD::BIT_CONVERT) {
Duncan Sands92c43912008-06-06 12:08:01 +00004995 MVT BCVT = InVec.getOperand(0).getValueType();
Mon P Wang3c673102008-12-11 00:26:16 +00004996 if (!BCVT.isVector() || EVT.bitsGT(BCVT.getVectorElementType()))
Dan Gohman8181bd12008-07-27 21:46:04 +00004997 return SDValue();
Mon P Wang3c673102008-12-11 00:26:16 +00004998 if (VT.getVectorNumElements() != BCVT.getVectorNumElements())
4999 BCNumEltsChanged = true;
Evan Cheng411fc172008-05-13 08:35:03 +00005000 InVec = InVec.getOperand(0);
Duncan Sands92c43912008-06-06 12:08:01 +00005001 EVT = BCVT.getVectorElementType();
Evan Cheng411fc172008-05-13 08:35:03 +00005002 NewLoad = true;
5003 }
Evan Chengd7ba7ed2007-10-06 08:19:55 +00005004
Evan Cheng411fc172008-05-13 08:35:03 +00005005 LoadSDNode *LN0 = NULL;
Gabor Greif1c80d112008-08-28 21:40:38 +00005006 if (ISD::isNormalLoad(InVec.getNode()))
Evan Cheng411fc172008-05-13 08:35:03 +00005007 LN0 = cast<LoadSDNode>(InVec);
5008 else if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR &&
5009 InVec.getOperand(0).getValueType() == EVT &&
Gabor Greif1c80d112008-08-28 21:40:38 +00005010 ISD::isNormalLoad(InVec.getOperand(0).getNode())) {
Evan Cheng411fc172008-05-13 08:35:03 +00005011 LN0 = cast<LoadSDNode>(InVec.getOperand(0));
5012 } else if (InVec.getOpcode() == ISD::VECTOR_SHUFFLE) {
5013 // (vextract (vector_shuffle (load $addr), v2, <1, u, u, u>), 1)
5014 // =>
5015 // (load $addr+1*size)
Mon P Wang3c673102008-12-11 00:26:16 +00005016
5017 // If the bit convert changed the number of elements, it is unsafe
5018 // to examine the mask.
5019 if (BCNumEltsChanged)
5020 return SDValue();
Evan Cheng411fc172008-05-13 08:35:03 +00005021 unsigned Idx = cast<ConstantSDNode>(InVec.getOperand(2).
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005022 getOperand(Elt))->getZExtValue();
Evan Cheng411fc172008-05-13 08:35:03 +00005023 unsigned NumElems = InVec.getOperand(2).getNumOperands();
5024 InVec = (Idx < NumElems) ? InVec.getOperand(0) : InVec.getOperand(1);
5025 if (InVec.getOpcode() == ISD::BIT_CONVERT)
5026 InVec = InVec.getOperand(0);
Gabor Greif1c80d112008-08-28 21:40:38 +00005027 if (ISD::isNormalLoad(InVec.getNode())) {
Evan Cheng411fc172008-05-13 08:35:03 +00005028 LN0 = cast<LoadSDNode>(InVec);
5029 Elt = (Idx < NumElems) ? Idx : Idx - NumElems;
Evan Chengd7ba7ed2007-10-06 08:19:55 +00005030 }
5031 }
Duncan Sandsc218a5a2008-06-15 20:12:31 +00005032 if (!LN0 || !LN0->hasOneUse() || LN0->isVolatile())
Dan Gohman8181bd12008-07-27 21:46:04 +00005033 return SDValue();
Evan Cheng411fc172008-05-13 08:35:03 +00005034
5035 unsigned Align = LN0->getAlignment();
5036 if (NewLoad) {
5037 // Check the resultant load doesn't need a higher alignment than the
5038 // original load.
Dan Gohman404e8542008-09-04 15:39:15 +00005039 unsigned NewAlign = TLI.getTargetData()->
Duncan Sands92c43912008-06-06 12:08:01 +00005040 getABITypeAlignment(LVT.getTypeForMVT());
Dan Gohman52c51aa2009-01-28 17:46:25 +00005041 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, LVT))
Dan Gohman8181bd12008-07-27 21:46:04 +00005042 return SDValue();
Evan Cheng411fc172008-05-13 08:35:03 +00005043 Align = NewAlign;
5044 }
5045
Dan Gohman8181bd12008-07-27 21:46:04 +00005046 SDValue NewPtr = LN0->getBasePtr();
Evan Cheng411fc172008-05-13 08:35:03 +00005047 if (Elt) {
Duncan Sands92c43912008-06-06 12:08:01 +00005048 unsigned PtrOff = LVT.getSizeInBits() * Elt / 8;
5049 MVT PtrType = NewPtr.getValueType();
Evan Cheng411fc172008-05-13 08:35:03 +00005050 if (TLI.isBigEndian())
Duncan Sands92c43912008-06-06 12:08:01 +00005051 PtrOff = VT.getSizeInBits() / 8 - PtrOff;
Evan Cheng411fc172008-05-13 08:35:03 +00005052 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
5053 DAG.getConstant(PtrOff, PtrType));
5054 }
5055 return DAG.getLoad(LVT, LN0->getChain(), NewPtr,
5056 LN0->getSrcValue(), LN0->getSrcValueOffset(),
5057 LN0->isVolatile(), Align);
Evan Chengd7ba7ed2007-10-06 08:19:55 +00005058 }
Dan Gohman8181bd12008-07-27 21:46:04 +00005059 return SDValue();
Evan Chengd7ba7ed2007-10-06 08:19:55 +00005060}
5061
5062
Dan Gohman8181bd12008-07-27 21:46:04 +00005063SDValue DAGCombiner::visitBUILD_VECTOR(SDNode *N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005064 unsigned NumInScalars = N->getNumOperands();
Duncan Sands92c43912008-06-06 12:08:01 +00005065 MVT VT = N->getValueType(0);
5066 unsigned NumElts = VT.getVectorNumElements();
5067 MVT EltType = VT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005068
5069 // Check to see if this is a BUILD_VECTOR of a bunch of EXTRACT_VECTOR_ELT
5070 // operations. If so, and if the EXTRACT_VECTOR_ELT vector inputs come from
5071 // at most two distinct vectors, turn this into a shuffle node.
Dan Gohman8181bd12008-07-27 21:46:04 +00005072 SDValue VecIn1, VecIn2;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005073 for (unsigned i = 0; i != NumInScalars; ++i) {
5074 // Ignore undef inputs.
5075 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
5076
5077 // If this input is something other than a EXTRACT_VECTOR_ELT with a
5078 // constant index, bail out.
5079 if (N->getOperand(i).getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
5080 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005081 VecIn1 = VecIn2 = SDValue(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005082 break;
5083 }
5084
5085 // If the input vector type disagrees with the result of the build_vector,
5086 // we can't make a shuffle.
Dan Gohman8181bd12008-07-27 21:46:04 +00005087 SDValue ExtractedFromVec = N->getOperand(i).getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005088 if (ExtractedFromVec.getValueType() != VT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005089 VecIn1 = VecIn2 = SDValue(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005090 break;
5091 }
5092
5093 // Otherwise, remember this. We allow up to two distinct input vectors.
5094 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
5095 continue;
5096
Gabor Greif1c80d112008-08-28 21:40:38 +00005097 if (VecIn1.getNode() == 0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005098 VecIn1 = ExtractedFromVec;
Gabor Greif1c80d112008-08-28 21:40:38 +00005099 } else if (VecIn2.getNode() == 0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005100 VecIn2 = ExtractedFromVec;
5101 } else {
5102 // Too many inputs.
Dan Gohman8181bd12008-07-27 21:46:04 +00005103 VecIn1 = VecIn2 = SDValue(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005104 break;
5105 }
5106 }
5107
5108 // If everything is good, we can make a shuffle operation.
Gabor Greif1c80d112008-08-28 21:40:38 +00005109 if (VecIn1.getNode()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005110 SmallVector<SDValue, 8> BuildVecIndices;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005111 for (unsigned i = 0; i != NumInScalars; ++i) {
5112 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
5113 BuildVecIndices.push_back(DAG.getNode(ISD::UNDEF, TLI.getPointerTy()));
5114 continue;
5115 }
5116
Dan Gohman8181bd12008-07-27 21:46:04 +00005117 SDValue Extract = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005118
5119 // If extracting from the first vector, just use the index directly.
5120 if (Extract.getOperand(0) == VecIn1) {
5121 BuildVecIndices.push_back(Extract.getOperand(1));
5122 continue;
5123 }
5124
5125 // Otherwise, use InIdx + VecSize
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005126 unsigned Idx =
5127 cast<ConstantSDNode>(Extract.getOperand(1))->getZExtValue();
Chris Lattner5872a362008-01-17 07:00:52 +00005128 BuildVecIndices.push_back(DAG.getIntPtrConstant(Idx+NumInScalars));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005129 }
5130
5131 // Add count and size info.
Duncan Sands92c43912008-06-06 12:08:01 +00005132 MVT BuildVecVT = MVT::getVectorVT(TLI.getPointerTy(), NumElts);
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00005133 if (!TLI.isTypeLegal(BuildVecVT) && LegalTypes)
5134 return SDValue();
5135
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005136 // Return the new VECTOR_SHUFFLE node.
Dan Gohman8181bd12008-07-27 21:46:04 +00005137 SDValue Ops[5];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005138 Ops[0] = VecIn1;
Gabor Greif1c80d112008-08-28 21:40:38 +00005139 if (VecIn2.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005140 Ops[1] = VecIn2;
5141 } else {
5142 // Use an undef build_vector as input for the second operand.
Dan Gohman8181bd12008-07-27 21:46:04 +00005143 std::vector<SDValue> UnOps(NumInScalars,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005144 DAG.getNode(ISD::UNDEF,
5145 EltType));
5146 Ops[1] = DAG.getNode(ISD::BUILD_VECTOR, VT,
5147 &UnOps[0], UnOps.size());
Gabor Greif1c80d112008-08-28 21:40:38 +00005148 AddToWorkList(Ops[1].getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005149 }
5150 Ops[2] = DAG.getNode(ISD::BUILD_VECTOR, BuildVecVT,
5151 &BuildVecIndices[0], BuildVecIndices.size());
5152 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Ops, 3);
5153 }
5154
Dan Gohman8181bd12008-07-27 21:46:04 +00005155 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005156}
5157
Dan Gohman8181bd12008-07-27 21:46:04 +00005158SDValue DAGCombiner::visitCONCAT_VECTORS(SDNode *N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005159 // TODO: Check to see if this is a CONCAT_VECTORS of a bunch of
5160 // EXTRACT_SUBVECTOR operations. If so, and if the EXTRACT_SUBVECTOR vector
5161 // inputs come from at most two distinct vectors, turn this into a shuffle
5162 // node.
5163
5164 // If we only have one input vector, we don't need to do any concatenation.
5165 if (N->getNumOperands() == 1) {
5166 return N->getOperand(0);
5167 }
5168
Dan Gohman8181bd12008-07-27 21:46:04 +00005169 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005170}
5171
Dan Gohman8181bd12008-07-27 21:46:04 +00005172SDValue DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
5173 SDValue ShufMask = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005174 unsigned NumElts = ShufMask.getNumOperands();
5175
Mon P Wangbff5d9c2008-11-10 04:46:22 +00005176 SDValue N0 = N->getOperand(0);
5177 SDValue N1 = N->getOperand(1);
5178
5179 assert(N0.getValueType().getVectorNumElements() == NumElts &&
5180 "Vector shuffle must be normalized in DAG");
5181
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005182 // If the shuffle mask is an identity operation on the LHS, return the LHS.
5183 bool isIdentity = true;
5184 for (unsigned i = 0; i != NumElts; ++i) {
5185 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005186 cast<ConstantSDNode>(ShufMask.getOperand(i))->getZExtValue() != i) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005187 isIdentity = false;
5188 break;
5189 }
5190 }
5191 if (isIdentity) return N->getOperand(0);
5192
5193 // If the shuffle mask is an identity operation on the RHS, return the RHS.
5194 isIdentity = true;
5195 for (unsigned i = 0; i != NumElts; ++i) {
5196 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005197 cast<ConstantSDNode>(ShufMask.getOperand(i))->getZExtValue() !=
5198 i+NumElts) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005199 isIdentity = false;
5200 break;
5201 }
5202 }
5203 if (isIdentity) return N->getOperand(1);
5204
5205 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
5206 // needed at all.
5207 bool isUnary = true;
5208 bool isSplat = true;
5209 int VecNum = -1;
5210 unsigned BaseIdx = 0;
5211 for (unsigned i = 0; i != NumElts; ++i)
5212 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005213 unsigned Idx=cast<ConstantSDNode>(ShufMask.getOperand(i))->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005214 int V = (Idx < NumElts) ? 0 : 1;
5215 if (VecNum == -1) {
5216 VecNum = V;
5217 BaseIdx = Idx;
5218 } else {
5219 if (BaseIdx != Idx)
5220 isSplat = false;
5221 if (VecNum != V) {
5222 isUnary = false;
5223 break;
5224 }
5225 }
5226 }
5227
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005228 // Normalize unary shuffle so the RHS is undef.
5229 if (isUnary && VecNum == 1)
5230 std::swap(N0, N1);
5231
5232 // If it is a splat, check if the argument vector is a build_vector with
5233 // all scalar elements the same.
5234 if (isSplat) {
Gabor Greif1c80d112008-08-28 21:40:38 +00005235 SDNode *V = N0.getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005236
5237 // If this is a bit convert that changes the element type of the vector but
5238 // not the number of vector elements, look through it. Be careful not to
5239 // look though conversions that change things like v4f32 to v2f64.
5240 if (V->getOpcode() == ISD::BIT_CONVERT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005241 SDValue ConvInput = V->getOperand(0);
Evan Cheng76b20d12008-07-22 20:42:56 +00005242 if (ConvInput.getValueType().isVector() &&
5243 ConvInput.getValueType().getVectorNumElements() == NumElts)
Gabor Greif1c80d112008-08-28 21:40:38 +00005244 V = ConvInput.getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005245 }
5246
5247 if (V->getOpcode() == ISD::BUILD_VECTOR) {
5248 unsigned NumElems = V->getNumOperands();
5249 if (NumElems > BaseIdx) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005250 SDValue Base;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005251 bool AllSame = true;
5252 for (unsigned i = 0; i != NumElems; ++i) {
5253 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
5254 Base = V->getOperand(i);
5255 break;
5256 }
5257 }
5258 // Splat of <u, u, u, u>, return <u, u, u, u>
Gabor Greif1c80d112008-08-28 21:40:38 +00005259 if (!Base.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005260 return N0;
5261 for (unsigned i = 0; i != NumElems; ++i) {
Evan Cheng8d68c2b2007-09-18 21:54:37 +00005262 if (V->getOperand(i) != Base) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005263 AllSame = false;
5264 break;
5265 }
5266 }
5267 // Splat of <x, x, x, x>, return <x, x, x, x>
5268 if (AllSame)
5269 return N0;
5270 }
5271 }
5272 }
5273
5274 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
5275 // into an undef.
5276 if (isUnary || N0 == N1) {
5277 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
5278 // first operand.
Dan Gohman8181bd12008-07-27 21:46:04 +00005279 SmallVector<SDValue, 8> MappedOps;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005280 for (unsigned i = 0; i != NumElts; ++i) {
5281 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005282 cast<ConstantSDNode>(ShufMask.getOperand(i))->getZExtValue() <
5283 NumElts) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005284 MappedOps.push_back(ShufMask.getOperand(i));
5285 } else {
5286 unsigned NewIdx =
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005287 cast<ConstantSDNode>(ShufMask.getOperand(i))->getZExtValue() -
5288 NumElts;
Duncan Sandsd3ace282008-07-21 10:20:31 +00005289 MappedOps.push_back(DAG.getConstant(NewIdx,
5290 ShufMask.getOperand(i).getValueType()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005291 }
5292 }
5293 ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMask.getValueType(),
5294 &MappedOps[0], MappedOps.size());
Gabor Greif1c80d112008-08-28 21:40:38 +00005295 AddToWorkList(ShufMask.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005296 return DAG.getNode(ISD::VECTOR_SHUFFLE, N->getValueType(0),
5297 N0,
5298 DAG.getNode(ISD::UNDEF, N->getValueType(0)),
5299 ShufMask);
5300 }
5301
Dan Gohman8181bd12008-07-27 21:46:04 +00005302 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005303}
5304
5305/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
5306/// an AND to a vector_shuffle with the destination vector and a zero vector.
5307/// e.g. AND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
5308/// vector_shuffle V, Zero, <0, 4, 2, 4>
Dan Gohman8181bd12008-07-27 21:46:04 +00005309SDValue DAGCombiner::XformToShuffleWithZero(SDNode *N) {
5310 SDValue LHS = N->getOperand(0);
5311 SDValue RHS = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005312 if (N->getOpcode() == ISD::AND) {
5313 if (RHS.getOpcode() == ISD::BIT_CONVERT)
5314 RHS = RHS.getOperand(0);
5315 if (RHS.getOpcode() == ISD::BUILD_VECTOR) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005316 std::vector<SDValue> IdxOps;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005317 unsigned NumOps = RHS.getNumOperands();
5318 unsigned NumElts = NumOps;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005319 for (unsigned i = 0; i != NumElts; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005320 SDValue Elt = RHS.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005321 if (!isa<ConstantSDNode>(Elt))
Dan Gohman8181bd12008-07-27 21:46:04 +00005322 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005323 else if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
Duncan Sands193c2bd2008-10-19 14:58:05 +00005324 IdxOps.push_back(DAG.getIntPtrConstant(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005325 else if (cast<ConstantSDNode>(Elt)->isNullValue())
Duncan Sands193c2bd2008-10-19 14:58:05 +00005326 IdxOps.push_back(DAG.getIntPtrConstant(NumElts));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005327 else
Dan Gohman8181bd12008-07-27 21:46:04 +00005328 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005329 }
5330
5331 // Let's see if the target supports this vector_shuffle.
Duncan Sands193c2bd2008-10-19 14:58:05 +00005332 if (!TLI.isVectorClearMaskLegal(IdxOps, TLI.getPointerTy(), DAG))
Dan Gohman8181bd12008-07-27 21:46:04 +00005333 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005334
5335 // Return the new VECTOR_SHUFFLE node.
Duncan Sands193c2bd2008-10-19 14:58:05 +00005336 MVT EVT = RHS.getValueType().getVectorElementType();
Duncan Sands92c43912008-06-06 12:08:01 +00005337 MVT VT = MVT::getVectorVT(EVT, NumElts);
Evan Cheng4fdfdac2008-11-05 06:04:18 +00005338 MVT MaskVT = MVT::getVectorVT(TLI.getPointerTy(), NumElts);
Dan Gohman8181bd12008-07-27 21:46:04 +00005339 std::vector<SDValue> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005340 LHS = DAG.getNode(ISD::BIT_CONVERT, VT, LHS);
5341 Ops.push_back(LHS);
Gabor Greif1c80d112008-08-28 21:40:38 +00005342 AddToWorkList(LHS.getNode());
Dan Gohman8181bd12008-07-27 21:46:04 +00005343 std::vector<SDValue> ZeroOps(NumElts, DAG.getConstant(0, EVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005344 Ops.push_back(DAG.getNode(ISD::BUILD_VECTOR, VT,
5345 &ZeroOps[0], ZeroOps.size()));
Evan Cheng4fdfdac2008-11-05 06:04:18 +00005346 Ops.push_back(DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005347 &IdxOps[0], IdxOps.size()));
Dan Gohman8181bd12008-07-27 21:46:04 +00005348 SDValue Result = DAG.getNode(ISD::VECTOR_SHUFFLE, VT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005349 &Ops[0], Ops.size());
Dan Gohman4c219902008-07-16 16:13:58 +00005350 if (VT != N->getValueType(0))
5351 Result = DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Result);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005352 return Result;
5353 }
5354 }
Dan Gohman8181bd12008-07-27 21:46:04 +00005355 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005356}
5357
5358/// SimplifyVBinOp - Visit a binary vector operation, like ADD.
Dan Gohman8181bd12008-07-27 21:46:04 +00005359SDValue DAGCombiner::SimplifyVBinOp(SDNode *N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005360 // After legalize, the target may be depending on adds and other
5361 // binary ops to provide legal ways to construct constants or other
5362 // things. Simplifying them may result in a loss of legality.
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00005363 if (LegalOperations) return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005364
Duncan Sands92c43912008-06-06 12:08:01 +00005365 MVT VT = N->getValueType(0);
5366 assert(VT.isVector() && "SimplifyVBinOp only works on vectors!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005367
Duncan Sands92c43912008-06-06 12:08:01 +00005368 MVT EltType = VT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00005369 SDValue LHS = N->getOperand(0);
5370 SDValue RHS = N->getOperand(1);
5371 SDValue Shuffle = XformToShuffleWithZero(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00005372 if (Shuffle.getNode()) return Shuffle;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005373
5374 // If the LHS and RHS are BUILD_VECTOR nodes, see if we can constant fold
5375 // this operation.
5376 if (LHS.getOpcode() == ISD::BUILD_VECTOR &&
5377 RHS.getOpcode() == ISD::BUILD_VECTOR) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005378 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005379 for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005380 SDValue LHSOp = LHS.getOperand(i);
5381 SDValue RHSOp = RHS.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005382 // If these two elements can't be folded, bail out.
5383 if ((LHSOp.getOpcode() != ISD::UNDEF &&
5384 LHSOp.getOpcode() != ISD::Constant &&
5385 LHSOp.getOpcode() != ISD::ConstantFP) ||
5386 (RHSOp.getOpcode() != ISD::UNDEF &&
5387 RHSOp.getOpcode() != ISD::Constant &&
5388 RHSOp.getOpcode() != ISD::ConstantFP))
5389 break;
5390 // Can't fold divide by zero.
5391 if (N->getOpcode() == ISD::SDIV || N->getOpcode() == ISD::UDIV ||
5392 N->getOpcode() == ISD::FDIV) {
5393 if ((RHSOp.getOpcode() == ISD::Constant &&
Gabor Greif1c80d112008-08-28 21:40:38 +00005394 cast<ConstantSDNode>(RHSOp.getNode())->isNullValue()) ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005395 (RHSOp.getOpcode() == ISD::ConstantFP &&
Gabor Greif1c80d112008-08-28 21:40:38 +00005396 cast<ConstantFPSDNode>(RHSOp.getNode())->getValueAPF().isZero()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005397 break;
5398 }
5399 Ops.push_back(DAG.getNode(N->getOpcode(), EltType, LHSOp, RHSOp));
Gabor Greif1c80d112008-08-28 21:40:38 +00005400 AddToWorkList(Ops.back().getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005401 assert((Ops.back().getOpcode() == ISD::UNDEF ||
5402 Ops.back().getOpcode() == ISD::Constant ||
5403 Ops.back().getOpcode() == ISD::ConstantFP) &&
5404 "Scalar binop didn't fold!");
5405 }
5406
5407 if (Ops.size() == LHS.getNumOperands()) {
Duncan Sands92c43912008-06-06 12:08:01 +00005408 MVT VT = LHS.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005409 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
5410 }
5411 }
5412
Dan Gohman8181bd12008-07-27 21:46:04 +00005413 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005414}
5415
Dan Gohman8181bd12008-07-27 21:46:04 +00005416SDValue DAGCombiner::SimplifySelect(SDValue N0, SDValue N1, SDValue N2){
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005417 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
5418
Dan Gohman8181bd12008-07-27 21:46:04 +00005419 SDValue SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005420 cast<CondCodeSDNode>(N0.getOperand(2))->get());
5421 // If we got a simplified select_cc node back from SimplifySelectCC, then
5422 // break it down into a new SETCC node, and a new SELECT node, and then return
5423 // the SELECT node, since we were called with a SELECT node.
Gabor Greif1c80d112008-08-28 21:40:38 +00005424 if (SCC.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005425 // Check to see if we got a select_cc back (to turn into setcc/select).
5426 // Otherwise, just return whatever node we got back, like fabs.
5427 if (SCC.getOpcode() == ISD::SELECT_CC) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005428 SDValue SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005429 SCC.getOperand(0), SCC.getOperand(1),
5430 SCC.getOperand(4));
Gabor Greif1c80d112008-08-28 21:40:38 +00005431 AddToWorkList(SETCC.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005432 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
5433 SCC.getOperand(3), SETCC);
5434 }
5435 return SCC;
5436 }
Dan Gohman8181bd12008-07-27 21:46:04 +00005437 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005438}
5439
5440/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
5441/// are the two values being selected between, see if we can simplify the
5442/// select. Callers of this should assume that TheSelect is deleted if this
5443/// returns true. As such, they should return the appropriate thing (e.g. the
5444/// node) back to the top-level of the DAG combiner loop to avoid it being
5445/// looked at.
5446///
Dan Gohman8181bd12008-07-27 21:46:04 +00005447bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDValue LHS,
5448 SDValue RHS) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005449
5450 // If this is a select from two identical things, try to pull the operation
5451 // through the select.
5452 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
5453 // If this is a load and the token chain is identical, replace the select
5454 // of two loads with a load through a select of the address to load from.
5455 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
5456 // constants have been dropped into the constant pool.
5457 if (LHS.getOpcode() == ISD::LOAD &&
Duncan Sands2418bec2008-06-13 19:07:40 +00005458 // Do not let this transformation reduce the number of volatile loads.
5459 !cast<LoadSDNode>(LHS)->isVolatile() &&
5460 !cast<LoadSDNode>(RHS)->isVolatile() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005461 // Token chains must be identical.
5462 LHS.getOperand(0) == RHS.getOperand(0)) {
5463 LoadSDNode *LLD = cast<LoadSDNode>(LHS);
5464 LoadSDNode *RLD = cast<LoadSDNode>(RHS);
5465
5466 // If this is an EXTLOAD, the VT's must match.
Dan Gohman9a4c92c2008-01-30 00:15:11 +00005467 if (LLD->getMemoryVT() == RLD->getMemoryVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005468 // FIXME: this conflates two src values, discarding one. This is not
5469 // the right thing to do, but nothing uses srcvalues now. When they do,
5470 // turn SrcValue into a list of locations.
Dan Gohman8181bd12008-07-27 21:46:04 +00005471 SDValue Addr;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005472 if (TheSelect->getOpcode() == ISD::SELECT) {
5473 // Check that the condition doesn't reach either load. If so, folding
5474 // this will induce a cycle into the DAG.
Gabor Greif1c80d112008-08-28 21:40:38 +00005475 if (!LLD->isPredecessorOf(TheSelect->getOperand(0).getNode()) &&
5476 !RLD->isPredecessorOf(TheSelect->getOperand(0).getNode())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005477 Addr = DAG.getNode(ISD::SELECT, LLD->getBasePtr().getValueType(),
5478 TheSelect->getOperand(0), LLD->getBasePtr(),
5479 RLD->getBasePtr());
5480 }
5481 } else {
5482 // Check that the condition doesn't reach either load. If so, folding
5483 // this will induce a cycle into the DAG.
Gabor Greif1c80d112008-08-28 21:40:38 +00005484 if (!LLD->isPredecessorOf(TheSelect->getOperand(0).getNode()) &&
5485 !RLD->isPredecessorOf(TheSelect->getOperand(0).getNode()) &&
5486 !LLD->isPredecessorOf(TheSelect->getOperand(1).getNode()) &&
5487 !RLD->isPredecessorOf(TheSelect->getOperand(1).getNode())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005488 Addr = DAG.getNode(ISD::SELECT_CC, LLD->getBasePtr().getValueType(),
5489 TheSelect->getOperand(0),
5490 TheSelect->getOperand(1),
5491 LLD->getBasePtr(), RLD->getBasePtr(),
5492 TheSelect->getOperand(4));
5493 }
5494 }
5495
Gabor Greif1c80d112008-08-28 21:40:38 +00005496 if (Addr.getNode()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005497 SDValue Load;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005498 if (LLD->getExtensionType() == ISD::NON_EXTLOAD)
5499 Load = DAG.getLoad(TheSelect->getValueType(0), LLD->getChain(),
5500 Addr,LLD->getSrcValue(),
5501 LLD->getSrcValueOffset(),
5502 LLD->isVolatile(),
5503 LLD->getAlignment());
5504 else {
5505 Load = DAG.getExtLoad(LLD->getExtensionType(),
5506 TheSelect->getValueType(0),
5507 LLD->getChain(), Addr, LLD->getSrcValue(),
5508 LLD->getSrcValueOffset(),
Dan Gohman9a4c92c2008-01-30 00:15:11 +00005509 LLD->getMemoryVT(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005510 LLD->isVolatile(),
5511 LLD->getAlignment());
5512 }
5513 // Users of the select now use the result of the load.
5514 CombineTo(TheSelect, Load);
5515
5516 // Users of the old loads now use the new load's chain. We know the
5517 // old-load value is dead now.
Gabor Greif1c80d112008-08-28 21:40:38 +00005518 CombineTo(LHS.getNode(), Load.getValue(0), Load.getValue(1));
5519 CombineTo(RHS.getNode(), Load.getValue(0), Load.getValue(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005520 return true;
5521 }
5522 }
5523 }
5524 }
5525
5526 return false;
5527}
5528
Dan Gohman8181bd12008-07-27 21:46:04 +00005529SDValue DAGCombiner::SimplifySelectCC(SDValue N0, SDValue N1,
5530 SDValue N2, SDValue N3,
5531 ISD::CondCode CC, bool NotExtCompare) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005532
Duncan Sands92c43912008-06-06 12:08:01 +00005533 MVT VT = N2.getValueType();
Gabor Greif1c80d112008-08-28 21:40:38 +00005534 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
5535 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
5536 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005537
5538 // Determine if the condition we're dealing with is constant
Duncan Sands4a361272009-01-01 15:52:00 +00005539 SDValue SCC = SimplifySetCC(TLI.getSetCCResultType(N0.getValueType()),
5540 N0, N1, CC, false);
Gabor Greif1c80d112008-08-28 21:40:38 +00005541 if (SCC.getNode()) AddToWorkList(SCC.getNode());
5542 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005543
5544 // fold select_cc true, x, y -> x
Dan Gohman9d24dc72008-03-13 22:13:53 +00005545 if (SCCC && !SCCC->isNullValue())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005546 return N2;
5547 // fold select_cc false, x, y -> y
Dan Gohman9d24dc72008-03-13 22:13:53 +00005548 if (SCCC && SCCC->isNullValue())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005549 return N3;
5550
5551 // Check to see if we can simplify the select into an fabs node
5552 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
5553 // Allow either -0.0 or 0.0
Dale Johannesen7f2c1d12007-08-25 22:10:57 +00005554 if (CFP->getValueAPF().isZero()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005555 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
5556 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
5557 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
5558 N2 == N3.getOperand(0))
5559 return DAG.getNode(ISD::FABS, VT, N0);
5560
5561 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
5562 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
5563 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
5564 N2.getOperand(0) == N3)
5565 return DAG.getNode(ISD::FABS, VT, N3);
5566 }
5567 }
5568
5569 // Check to see if we can perform the "gzip trick", transforming
5570 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
5571 if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT &&
Duncan Sands92c43912008-06-06 12:08:01 +00005572 N0.getValueType().isInteger() &&
5573 N2.getValueType().isInteger() &&
Dan Gohman9d24dc72008-03-13 22:13:53 +00005574 (N1C->isNullValue() || // (a < 0) ? b : 0
5575 (N1C->getAPIntValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0
Duncan Sands92c43912008-06-06 12:08:01 +00005576 MVT XType = N0.getValueType();
5577 MVT AType = N2.getValueType();
Duncan Sandsec142ee2008-06-08 20:54:56 +00005578 if (XType.bitsGE(AType)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005579 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
5580 // single-bit constant.
Dan Gohman9d24dc72008-03-13 22:13:53 +00005581 if (N2C && ((N2C->getAPIntValue() & (N2C->getAPIntValue()-1)) == 0)) {
5582 unsigned ShCtV = N2C->getAPIntValue().logBase2();
Duncan Sands92c43912008-06-06 12:08:01 +00005583 ShCtV = XType.getSizeInBits()-ShCtV-1;
Dan Gohman8181bd12008-07-27 21:46:04 +00005584 SDValue ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
5585 SDValue Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
Gabor Greif1c80d112008-08-28 21:40:38 +00005586 AddToWorkList(Shift.getNode());
Duncan Sandsec142ee2008-06-08 20:54:56 +00005587 if (XType.bitsGT(AType)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005588 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Gabor Greif1c80d112008-08-28 21:40:38 +00005589 AddToWorkList(Shift.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005590 }
5591 return DAG.getNode(ISD::AND, AType, Shift, N2);
5592 }
Dan Gohman8181bd12008-07-27 21:46:04 +00005593 SDValue Shift = DAG.getNode(ISD::SRA, XType, N0,
Duncan Sands92c43912008-06-06 12:08:01 +00005594 DAG.getConstant(XType.getSizeInBits()-1,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005595 TLI.getShiftAmountTy()));
Gabor Greif1c80d112008-08-28 21:40:38 +00005596 AddToWorkList(Shift.getNode());
Duncan Sandsec142ee2008-06-08 20:54:56 +00005597 if (XType.bitsGT(AType)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005598 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Gabor Greif1c80d112008-08-28 21:40:38 +00005599 AddToWorkList(Shift.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005600 }
5601 return DAG.getNode(ISD::AND, AType, Shift, N2);
5602 }
5603 }
5604
5605 // fold select C, 16, 0 -> shl C, 4
Dan Gohman9d24dc72008-03-13 22:13:53 +00005606 if (N2C && N3C && N3C->isNullValue() && N2C->getAPIntValue().isPowerOf2() &&
Duncan Sands8cf4a822008-11-23 15:47:28 +00005607 TLI.getBooleanContents() == TargetLowering::ZeroOrOneBooleanContent) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005608
5609 // If the caller doesn't want us to simplify this into a zext of a compare,
5610 // don't do it.
Dan Gohman9d24dc72008-03-13 22:13:53 +00005611 if (NotExtCompare && N2C->getAPIntValue() == 1)
Dan Gohman8181bd12008-07-27 21:46:04 +00005612 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005613
5614 // Get a SetCC of the condition
5615 // FIXME: Should probably make sure that setcc is legal if we ever have a
5616 // target where it isn't.
Dan Gohman8181bd12008-07-27 21:46:04 +00005617 SDValue Temp, SCC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005618 // cast from setcc result type to select result type
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00005619 if (LegalTypes) {
Duncan Sands4a361272009-01-01 15:52:00 +00005620 SCC = DAG.getSetCC(TLI.getSetCCResultType(N0.getValueType()),
5621 N0, N1, CC);
Duncan Sandsec142ee2008-06-08 20:54:56 +00005622 if (N2.getValueType().bitsLT(SCC.getValueType()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005623 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
5624 else
5625 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
5626 } else {
5627 SCC = DAG.getSetCC(MVT::i1, N0, N1, CC);
5628 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
5629 }
Gabor Greif1c80d112008-08-28 21:40:38 +00005630 AddToWorkList(SCC.getNode());
5631 AddToWorkList(Temp.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005632
Dan Gohman9d24dc72008-03-13 22:13:53 +00005633 if (N2C->getAPIntValue() == 1)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005634 return Temp;
5635 // shl setcc result by log2 n2c
5636 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
Dan Gohman9d24dc72008-03-13 22:13:53 +00005637 DAG.getConstant(N2C->getAPIntValue().logBase2(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005638 TLI.getShiftAmountTy()));
5639 }
5640
5641 // Check to see if this is the equivalent of setcc
5642 // FIXME: Turn all of these into setcc if setcc if setcc is legal
5643 // otherwise, go ahead with the folds.
Dan Gohman9d24dc72008-03-13 22:13:53 +00005644 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getAPIntValue() == 1ULL)) {
Duncan Sands92c43912008-06-06 12:08:01 +00005645 MVT XType = N0.getValueType();
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00005646 if (!LegalOperations ||
Duncan Sands4a361272009-01-01 15:52:00 +00005647 TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultType(XType))) {
5648 SDValue Res = DAG.getSetCC(TLI.getSetCCResultType(XType), N0, N1, CC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005649 if (Res.getValueType() != VT)
5650 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
5651 return Res;
5652 }
5653
5654 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
5655 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00005656 (!LegalOperations ||
Duncan Sands6ae1a0632008-06-14 17:48:34 +00005657 TLI.isOperationLegal(ISD::CTLZ, XType))) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005658 SDValue Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005659 return DAG.getNode(ISD::SRL, XType, Ctlz,
Duncan Sands92c43912008-06-06 12:08:01 +00005660 DAG.getConstant(Log2_32(XType.getSizeInBits()),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005661 TLI.getShiftAmountTy()));
5662 }
5663 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
5664 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005665 SDValue NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005666 N0);
Bob Wilson81a42cf2009-01-22 17:39:32 +00005667 SDValue NotN0 = DAG.getNOT(N0, XType);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005668 return DAG.getNode(ISD::SRL, XType,
5669 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
Duncan Sands92c43912008-06-06 12:08:01 +00005670 DAG.getConstant(XType.getSizeInBits()-1,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005671 TLI.getShiftAmountTy()));
5672 }
5673 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
5674 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005675 SDValue Sign = DAG.getNode(ISD::SRL, XType, N0,
Duncan Sands92c43912008-06-06 12:08:01 +00005676 DAG.getConstant(XType.getSizeInBits()-1,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005677 TLI.getShiftAmountTy()));
5678 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
5679 }
5680 }
5681
5682 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
5683 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
5684 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
5685 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1) &&
Duncan Sands92c43912008-06-06 12:08:01 +00005686 N2.getOperand(0) == N1 && N0.getValueType().isInteger()) {
5687 MVT XType = N0.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +00005688 SDValue Shift = DAG.getNode(ISD::SRA, XType, N0,
Duncan Sands92c43912008-06-06 12:08:01 +00005689 DAG.getConstant(XType.getSizeInBits()-1,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005690 TLI.getShiftAmountTy()));
Dan Gohman8181bd12008-07-27 21:46:04 +00005691 SDValue Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
Gabor Greif1c80d112008-08-28 21:40:38 +00005692 AddToWorkList(Shift.getNode());
5693 AddToWorkList(Add.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005694 return DAG.getNode(ISD::XOR, XType, Add, Shift);
5695 }
5696 // Check to see if this is an integer abs. select_cc setgt X, -1, X, -X ->
5697 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
5698 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT &&
5699 N0 == N2 && N3.getOpcode() == ISD::SUB && N0 == N3.getOperand(1)) {
5700 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N3.getOperand(0))) {
Duncan Sands92c43912008-06-06 12:08:01 +00005701 MVT XType = N0.getValueType();
5702 if (SubC->isNullValue() && XType.isInteger()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005703 SDValue Shift = DAG.getNode(ISD::SRA, XType, N0,
Duncan Sands92c43912008-06-06 12:08:01 +00005704 DAG.getConstant(XType.getSizeInBits()-1,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005705 TLI.getShiftAmountTy()));
Dan Gohman8181bd12008-07-27 21:46:04 +00005706 SDValue Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
Gabor Greif1c80d112008-08-28 21:40:38 +00005707 AddToWorkList(Shift.getNode());
5708 AddToWorkList(Add.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005709 return DAG.getNode(ISD::XOR, XType, Add, Shift);
5710 }
5711 }
5712 }
5713
Dan Gohman8181bd12008-07-27 21:46:04 +00005714 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005715}
5716
5717/// SimplifySetCC - This is a stub for TargetLowering::SimplifySetCC.
Dan Gohman8181bd12008-07-27 21:46:04 +00005718SDValue DAGCombiner::SimplifySetCC(MVT VT, SDValue N0,
5719 SDValue N1, ISD::CondCode Cond,
5720 bool foldBooleans) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005721 TargetLowering::DAGCombinerInfo
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00005722 DagCombineInfo(DAG, Level == Unrestricted, false, this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005723 return TLI.SimplifySetCC(VT, N0, N1, Cond, foldBooleans, DagCombineInfo);
5724}
5725
5726/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
5727/// return a DAG expression to select that will generate the same value by
5728/// multiplying by a magic number. See:
5729/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Dan Gohman8181bd12008-07-27 21:46:04 +00005730SDValue DAGCombiner::BuildSDIV(SDNode *N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005731 std::vector<SDNode*> Built;
Dan Gohman8181bd12008-07-27 21:46:04 +00005732 SDValue S = TLI.BuildSDIV(N, DAG, &Built);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005733
5734 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
5735 ii != ee; ++ii)
5736 AddToWorkList(*ii);
5737 return S;
5738}
5739
5740/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
5741/// return a DAG expression to select that will generate the same value by
5742/// multiplying by a magic number. See:
5743/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Dan Gohman8181bd12008-07-27 21:46:04 +00005744SDValue DAGCombiner::BuildUDIV(SDNode *N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005745 std::vector<SDNode*> Built;
Dan Gohman8181bd12008-07-27 21:46:04 +00005746 SDValue S = TLI.BuildUDIV(N, DAG, &Built);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005747
5748 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
5749 ii != ee; ++ii)
5750 AddToWorkList(*ii);
5751 return S;
5752}
5753
5754/// FindBaseOffset - Return true if base is known not to alias with anything
5755/// but itself. Provides base object and offset as results.
Dan Gohman8181bd12008-07-27 21:46:04 +00005756static bool FindBaseOffset(SDValue Ptr, SDValue &Base, int64_t &Offset) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005757 // Assume it is a primitive operation.
5758 Base = Ptr; Offset = 0;
5759
5760 // If it's an adding a simple constant then integrate the offset.
5761 if (Base.getOpcode() == ISD::ADD) {
5762 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Base.getOperand(1))) {
5763 Base = Base.getOperand(0);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005764 Offset += C->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005765 }
5766 }
5767
5768 // If it's any of the following then it can't alias with anything but itself.
5769 return isa<FrameIndexSDNode>(Base) ||
5770 isa<ConstantPoolSDNode>(Base) ||
5771 isa<GlobalAddressSDNode>(Base);
5772}
5773
5774/// isAlias - Return true if there is any possibility that the two addresses
5775/// overlap.
Dan Gohman8181bd12008-07-27 21:46:04 +00005776bool DAGCombiner::isAlias(SDValue Ptr1, int64_t Size1,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005777 const Value *SrcValue1, int SrcValueOffset1,
Dan Gohman8181bd12008-07-27 21:46:04 +00005778 SDValue Ptr2, int64_t Size2,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005779 const Value *SrcValue2, int SrcValueOffset2)
5780{
5781 // If they are the same then they must be aliases.
5782 if (Ptr1 == Ptr2) return true;
5783
5784 // Gather base node and offset information.
Dan Gohman8181bd12008-07-27 21:46:04 +00005785 SDValue Base1, Base2;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005786 int64_t Offset1, Offset2;
5787 bool KnownBase1 = FindBaseOffset(Ptr1, Base1, Offset1);
5788 bool KnownBase2 = FindBaseOffset(Ptr2, Base2, Offset2);
5789
5790 // If they have a same base address then...
5791 if (Base1 == Base2) {
5792 // Check to see if the addresses overlap.
5793 return!((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
5794 }
5795
5796 // If we know both bases then they can't alias.
5797 if (KnownBase1 && KnownBase2) return false;
5798
5799 if (CombinerGlobalAA) {
5800 // Use alias analysis information.
Dan Gohmane142c2e2007-08-27 16:32:11 +00005801 int64_t MinOffset = std::min(SrcValueOffset1, SrcValueOffset2);
5802 int64_t Overlap1 = Size1 + SrcValueOffset1 - MinOffset;
5803 int64_t Overlap2 = Size2 + SrcValueOffset2 - MinOffset;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005804 AliasAnalysis::AliasResult AAResult =
5805 AA.alias(SrcValue1, Overlap1, SrcValue2, Overlap2);
5806 if (AAResult == AliasAnalysis::NoAlias)
5807 return false;
5808 }
5809
5810 // Otherwise we have to assume they alias.
5811 return true;
5812}
5813
5814/// FindAliasInfo - Extracts the relevant alias information from the memory
5815/// node. Returns true if the operand was a load.
5816bool DAGCombiner::FindAliasInfo(SDNode *N,
Dan Gohman8181bd12008-07-27 21:46:04 +00005817 SDValue &Ptr, int64_t &Size,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005818 const Value *&SrcValue, int &SrcValueOffset) {
5819 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
5820 Ptr = LD->getBasePtr();
Duncan Sands92c43912008-06-06 12:08:01 +00005821 Size = LD->getMemoryVT().getSizeInBits() >> 3;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005822 SrcValue = LD->getSrcValue();
5823 SrcValueOffset = LD->getSrcValueOffset();
5824 return true;
5825 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
5826 Ptr = ST->getBasePtr();
Duncan Sands92c43912008-06-06 12:08:01 +00005827 Size = ST->getMemoryVT().getSizeInBits() >> 3;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005828 SrcValue = ST->getSrcValue();
5829 SrcValueOffset = ST->getSrcValueOffset();
5830 } else {
5831 assert(0 && "FindAliasInfo expected a memory operand");
5832 }
5833
5834 return false;
5835}
5836
5837/// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
5838/// looking for aliasing nodes and adding them to the Aliases vector.
Dan Gohman8181bd12008-07-27 21:46:04 +00005839void DAGCombiner::GatherAllAliases(SDNode *N, SDValue OriginalChain,
5840 SmallVector<SDValue, 8> &Aliases) {
5841 SmallVector<SDValue, 8> Chains; // List of chains to visit.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005842 std::set<SDNode *> Visited; // Visited node set.
5843
5844 // Get alias information for node.
Dan Gohman8181bd12008-07-27 21:46:04 +00005845 SDValue Ptr;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005846 int64_t Size;
5847 const Value *SrcValue;
5848 int SrcValueOffset;
5849 bool IsLoad = FindAliasInfo(N, Ptr, Size, SrcValue, SrcValueOffset);
5850
5851 // Starting off.
5852 Chains.push_back(OriginalChain);
5853
5854 // Look at each chain and determine if it is an alias. If so, add it to the
5855 // aliases list. If not, then continue up the chain looking for the next
5856 // candidate.
5857 while (!Chains.empty()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005858 SDValue Chain = Chains.back();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005859 Chains.pop_back();
5860
5861 // Don't bother if we've been before.
Gabor Greif1c80d112008-08-28 21:40:38 +00005862 if (Visited.find(Chain.getNode()) != Visited.end()) continue;
5863 Visited.insert(Chain.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005864
5865 switch (Chain.getOpcode()) {
5866 case ISD::EntryToken:
5867 // Entry token is ideal chain operand, but handled in FindBetterChain.
5868 break;
5869
5870 case ISD::LOAD:
5871 case ISD::STORE: {
5872 // Get alias information for Chain.
Dan Gohman8181bd12008-07-27 21:46:04 +00005873 SDValue OpPtr;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005874 int64_t OpSize;
5875 const Value *OpSrcValue;
5876 int OpSrcValueOffset;
Gabor Greif1c80d112008-08-28 21:40:38 +00005877 bool IsOpLoad = FindAliasInfo(Chain.getNode(), OpPtr, OpSize,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005878 OpSrcValue, OpSrcValueOffset);
5879
5880 // If chain is alias then stop here.
5881 if (!(IsLoad && IsOpLoad) &&
5882 isAlias(Ptr, Size, SrcValue, SrcValueOffset,
5883 OpPtr, OpSize, OpSrcValue, OpSrcValueOffset)) {
5884 Aliases.push_back(Chain);
5885 } else {
5886 // Look further up the chain.
5887 Chains.push_back(Chain.getOperand(0));
5888 // Clean up old chain.
Gabor Greif1c80d112008-08-28 21:40:38 +00005889 AddToWorkList(Chain.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005890 }
5891 break;
5892 }
5893
5894 case ISD::TokenFactor:
5895 // We have to check each of the operands of the token factor, so we queue
5896 // then up. Adding the operands to the queue (stack) in reverse order
5897 // maintains the original order and increases the likelihood that getNode
5898 // will find a matching token factor (CSE.)
5899 for (unsigned n = Chain.getNumOperands(); n;)
5900 Chains.push_back(Chain.getOperand(--n));
5901 // Eliminate the token factor if we can.
Gabor Greif1c80d112008-08-28 21:40:38 +00005902 AddToWorkList(Chain.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005903 break;
5904
5905 default:
5906 // For all other instructions we will just have to take what we can get.
5907 Aliases.push_back(Chain);
5908 break;
5909 }
5910 }
5911}
5912
5913/// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, looking
5914/// for a better chain (aliasing node.)
Dan Gohman8181bd12008-07-27 21:46:04 +00005915SDValue DAGCombiner::FindBetterChain(SDNode *N, SDValue OldChain) {
5916 SmallVector<SDValue, 8> Aliases; // Ops for replacing token factor.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005917
5918 // Accumulate all the aliases to this node.
5919 GatherAllAliases(N, OldChain, Aliases);
5920
5921 if (Aliases.size() == 0) {
5922 // If no operands then chain to entry token.
5923 return DAG.getEntryNode();
5924 } else if (Aliases.size() == 1) {
5925 // If a single operand then chain to it. We don't need to revisit it.
5926 return Aliases[0];
5927 }
5928
5929 // Construct a custom tailored token factor.
Dan Gohman8181bd12008-07-27 21:46:04 +00005930 SDValue NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005931 &Aliases[0], Aliases.size());
5932
5933 // Make sure the old chain gets cleaned up.
Gabor Greif1c80d112008-08-28 21:40:38 +00005934 if (NewChain != OldChain) AddToWorkList(OldChain.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005935
5936 return NewChain;
5937}
5938
5939// SelectionDAG::Combine - This is the entry point for the file.
5940//
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00005941void SelectionDAG::Combine(CombineLevel Level, AliasAnalysis &AA, bool Fast) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005942 /// run - This is the main entry point to this class.
5943 ///
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00005944 DAGCombiner(*this, AA, Fast).Run(Level);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005945}