blob: 3935f6da09be3a1c141e1efb646694f556dfa8df [file] [log] [blame]
Nate Begeman4ebd8052005-09-01 23:24:04 +00001//===-- DAGCombiner.cpp - Implement a DAG node combiner -------------------===//
Nate Begeman1d4d4142005-09-01 00:19:25 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Nate Begeman1d4d4142005-09-01 00:19:25 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This pass combines dag nodes to form fewer, simpler DAG nodes. It can be run
11// both before and after the DAG is legalized.
Nate Begeman1d4d4142005-09-01 00:19:25 +000012//
13//===----------------------------------------------------------------------===//
14
15#define DEBUG_TYPE "dagcombine"
Nate Begeman1d4d4142005-09-01 00:19:25 +000016#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattner00161a62008-01-25 07:20:16 +000017#include "llvm/CodeGen/MachineFunction.h"
18#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattnerc76d4412007-05-16 06:37:59 +000019#include "llvm/Analysis/AliasAnalysis.h"
Evan Cheng59d5b682007-05-07 21:27:48 +000020#include "llvm/Target/TargetData.h"
Chris Lattner1329cb82008-01-26 19:45:50 +000021#include "llvm/Target/TargetFrameInfo.h"
Nate Begeman1d4d4142005-09-01 00:19:25 +000022#include "llvm/Target/TargetLowering.h"
Evan Cheng59d5b682007-05-07 21:27:48 +000023#include "llvm/Target/TargetMachine.h"
Chris Lattnerddae4bd2007-01-08 23:04:05 +000024#include "llvm/Target/TargetOptions.h"
Chris Lattnerc76d4412007-05-16 06:37:59 +000025#include "llvm/ADT/SmallPtrSet.h"
26#include "llvm/ADT/Statistic.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000027#include "llvm/Support/Compiler.h"
Jim Laskeyd1aed7a2006-09-21 16:28:59 +000028#include "llvm/Support/CommandLine.h"
Chris Lattnerc76d4412007-05-16 06:37:59 +000029#include "llvm/Support/Debug.h"
30#include "llvm/Support/MathExtras.h"
Chris Lattnera500fc62005-09-09 23:53:39 +000031#include <algorithm>
Dan Gohmanee335e32008-05-23 20:40:06 +000032#include <set>
Nate Begeman1d4d4142005-09-01 00:19:25 +000033using namespace llvm;
34
Chris Lattnercd3245a2006-12-19 22:41:21 +000035STATISTIC(NodesCombined , "Number of dag nodes combined");
36STATISTIC(PreIndexedNodes , "Number of pre-indexed nodes created");
37STATISTIC(PostIndexedNodes, "Number of post-indexed nodes created");
38
Nate Begeman1d4d4142005-09-01 00:19:25 +000039namespace {
Jim Laskey71382342006-10-07 23:37:56 +000040 static cl::opt<bool>
41 CombinerAA("combiner-alias-analysis", cl::Hidden,
Jim Laskey26f7fa72006-10-17 19:33:52 +000042 cl::desc("Turn on alias analysis during testing"));
Jim Laskey3ad175b2006-10-12 15:22:24 +000043
Jim Laskey07a27092006-10-18 19:08:31 +000044 static cl::opt<bool>
45 CombinerGlobalAA("combiner-global-alias-analysis", cl::Hidden,
46 cl::desc("Include global information in alias analysis"));
47
Jim Laskeybc588b82006-10-05 15:07:25 +000048//------------------------------ DAGCombiner ---------------------------------//
49
Jim Laskey71382342006-10-07 23:37:56 +000050 class VISIBILITY_HIDDEN DAGCombiner {
Nate Begeman1d4d4142005-09-01 00:19:25 +000051 SelectionDAG &DAG;
Dan Gohman79ce2762009-01-15 19:20:50 +000052 const TargetLowering &TLI;
Duncan Sands25cf2272008-11-24 14:53:14 +000053 CombineLevel Level;
54 bool LegalOperations;
55 bool LegalTypes;
Dan Gohmana2676512008-08-20 16:30:28 +000056 bool Fast;
Nate Begeman1d4d4142005-09-01 00:19:25 +000057
58 // Worklist of all of the nodes that need to be simplified.
Evan Cheng17a568b2008-08-29 22:21:44 +000059 std::vector<SDNode*> WorkList;
Nate Begeman1d4d4142005-09-01 00:19:25 +000060
Jim Laskeyc7c3f112006-10-16 20:52:31 +000061 // AA - Used for DAG load/store alias analysis.
62 AliasAnalysis &AA;
63
Nate Begeman1d4d4142005-09-01 00:19:25 +000064 /// 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();
Nate Begeman4ebd8052005-09-01 23:24:04 +000070 UI != UE; ++UI)
Dan Gohman89684502008-07-27 20:43:25 +000071 AddToWorkList(*UI);
Nate Begeman1d4d4142005-09-01 00:19:25 +000072 }
73
Dan Gohman389079b2007-10-08 17:57:15 +000074 /// visit - call the node-specific routine that knows how to fold each
75 /// particular type of node.
Dan Gohman475871a2008-07-27 21:46:04 +000076 SDValue visit(SDNode *N);
Dan Gohman389079b2007-10-08 17:57:15 +000077
Chris Lattner24664722006-03-01 04:53:38 +000078 public:
Jim Laskey6ff23e52006-10-04 16:53:27 +000079 /// AddToWorkList - Add to the work list making sure it's instance is at the
80 /// the back (next to be processed.)
Chris Lattner5750df92006-03-01 04:03:14 +000081 void AddToWorkList(SDNode *N) {
Jim Laskey6ff23e52006-10-04 16:53:27 +000082 removeFromWorkList(N);
Chris Lattner5750df92006-03-01 04:03:14 +000083 WorkList.push_back(N);
84 }
Jim Laskey6ff23e52006-10-04 16:53:27 +000085
Chris Lattnerf8dc0612008-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());
Chris Lattner01a22022005-10-10 22:04:48 +000091 }
Nate Begeman368e18d2006-02-16 21:11:51 +000092
Dan Gohman475871a2008-07-27 21:46:04 +000093 SDValue CombineTo(SDNode *N, const SDValue *To, unsigned NumTo,
Chris Lattnerf8dc0612008-02-03 06:49:24 +000094 bool AddTo = true);
95
Dan Gohman475871a2008-07-27 21:46:04 +000096 SDValue CombineTo(SDNode *N, SDValue Res, bool AddTo = true) {
Jim Laskey274062c2006-10-13 23:32:28 +000097 return CombineTo(N, &Res, 1, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +000098 }
99
Dan Gohman475871a2008-07-27 21:46:04 +0000100 SDValue CombineTo(SDNode *N, SDValue Res0, SDValue Res1,
Jim Laskey274062c2006-10-13 23:32:28 +0000101 bool AddTo = true) {
Dan Gohman475871a2008-07-27 21:46:04 +0000102 SDValue To[] = { Res0, Res1 };
Jim Laskey274062c2006-10-13 23:32:28 +0000103 return CombineTo(N, To, 2, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000104 }
Dan Gohmane5af2d32009-01-29 01:59:02 +0000105
106 void CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO);
Chris Lattner0bd48932008-01-17 07:00:52 +0000107
Chris Lattner24664722006-03-01 04:53:38 +0000108 private:
109
Chris Lattner012f2412006-02-17 21:58:01 +0000110 /// SimplifyDemandedBits - Check the specified integer node value to see if
Chris Lattnerb2742f42006-03-01 19:55:35 +0000111 /// it can be simplified or if things it uses can be simplified by bit
Chris Lattner012f2412006-02-17 21:58:01 +0000112 /// propagation. If so, return true.
Dan Gohman475871a2008-07-27 21:46:04 +0000113 bool SimplifyDemandedBits(SDValue Op) {
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000114 APInt Demanded = APInt::getAllOnesValue(Op.getValueSizeInBits());
115 return SimplifyDemandedBits(Op, Demanded);
116 }
117
Dan Gohman475871a2008-07-27 21:46:04 +0000118 bool SimplifyDemandedBits(SDValue Op, const APInt &Demanded);
Chris Lattner87514ca2005-10-10 22:31:19 +0000119
Chris Lattner448f2192006-11-11 00:39:41 +0000120 bool CombineToPreIndexedLoadStore(SDNode *N);
121 bool CombineToPostIndexedLoadStore(SDNode *N);
122
123
Dan Gohman389079b2007-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 Gohman475871a2008-07-27 21:46:04 +0000127 SDValue combine(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000128
129 // Visitation implementation - Implement dag node combining for different
130 // node types. The semantics are as follows:
131 // Return Value:
Evan Cheng17a568b2008-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.
Nate Begeman1d4d4142005-09-01 00:19:25 +0000135 //
Dan Gohman475871a2008-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);
Chris Lattner01a22022005-10-10 22:04:48 +0000197
Dan Gohman475871a2008-07-27 21:46:04 +0000198 SDValue XformToShuffleWithZero(SDNode *N);
Bill Wendling35247c32009-01-30 00:45:56 +0000199 SDValue ReassociateOps(unsigned Opc, DebugLoc DL, SDValue LHS, SDValue RHS);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000200
Dan Gohman475871a2008-07-27 21:46:04 +0000201 SDValue visitShiftByConstant(SDNode *N, unsigned Amt);
Chris Lattnere70da202007-12-06 07:33:36 +0000202
Dan Gohman475871a2008-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,
Chris Lattner1eba01e2007-04-11 06:50:51 +0000208 bool NotExtCompare = false);
Duncan Sands25cf2272008-11-24 14:53:14 +0000209 SDValue SimplifySetCC(MVT VT, SDValue N0, SDValue N1, ISD::CondCode Cond,
210 bool foldBooleans = true);
Dan Gohman475871a2008-07-27 21:46:04 +0000211 SDValue SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
Chris Lattner5eee4272008-01-26 01:09:19 +0000212 unsigned HiOp);
Dan Gohman475871a2008-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 Wendling317bd702009-01-30 21:14:50 +0000217 SDNode *MatchRotate(SDValue LHS, SDValue RHS, DebugLoc DL);
Dan Gohman475871a2008-07-27 21:46:04 +0000218 SDValue ReduceLoadWidth(SDNode *N);
Jim Laskey279f0532006-09-25 16:29:54 +0000219
Dan Gohman475871a2008-07-27 21:46:04 +0000220 SDValue GetDemandedBits(SDValue V, const APInt &Mask);
Chris Lattner2b4c2792007-10-13 06:35:54 +0000221
Jim Laskey6ff23e52006-10-04 16:53:27 +0000222 /// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
223 /// looking for aliasing nodes and adding them to the Aliases vector.
Dan Gohman475871a2008-07-27 21:46:04 +0000224 void GatherAllAliases(SDNode *N, SDValue OriginalChain,
225 SmallVector<SDValue, 8> &Aliases);
Jim Laskey6ff23e52006-10-04 16:53:27 +0000226
Jim Laskey096c22e2006-10-18 12:29:57 +0000227 /// isAlias - Return true if there is any possibility that the two addresses
228 /// overlap.
Dan Gohman475871a2008-07-27 21:46:04 +0000229 bool isAlias(SDValue Ptr1, int64_t Size1,
Jim Laskey096c22e2006-10-18 12:29:57 +0000230 const Value *SrcValue1, int SrcValueOffset1,
Dan Gohman475871a2008-07-27 21:46:04 +0000231 SDValue Ptr2, int64_t Size2,
Jeff Cohend41b30d2006-11-05 19:31:28 +0000232 const Value *SrcValue2, int SrcValueOffset2);
Jim Laskey096c22e2006-10-18 12:29:57 +0000233
Jim Laskey7ca56af2006-10-11 13:47:09 +0000234 /// 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 Gohman475871a2008-07-27 21:46:04 +0000237 SDValue &Ptr, int64_t &Size,
Jim Laskey096c22e2006-10-18 12:29:57 +0000238 const Value *&SrcValue, int &SrcValueOffset);
Jim Laskey7ca56af2006-10-11 13:47:09 +0000239
Jim Laskey279f0532006-09-25 16:29:54 +0000240 /// FindBetterChain - Walk up chain skipping non-aliasing memory nodes,
Jim Laskey6ff23e52006-10-04 16:53:27 +0000241 /// looking for a better chain (aliasing node.)
Dan Gohman475871a2008-07-27 21:46:04 +0000242 SDValue FindBetterChain(SDNode *N, SDValue Chain);
Jim Laskey279f0532006-09-25 16:29:54 +0000243
Nate Begeman1d4d4142005-09-01 00:19:25 +0000244public:
Dan Gohmana2676512008-08-20 16:30:28 +0000245 DAGCombiner(SelectionDAG &D, AliasAnalysis &A, bool fast)
Jim Laskeyc7c3f112006-10-16 20:52:31 +0000246 : DAG(D),
247 TLI(D.getTargetLoweringInfo()),
Duncan Sands25cf2272008-11-24 14:53:14 +0000248 Level(Unrestricted),
249 LegalOperations(false),
250 LegalTypes(false),
Dan Gohmana2676512008-08-20 16:30:28 +0000251 Fast(fast),
Jim Laskeyc7c3f112006-10-16 20:52:31 +0000252 AA(A) {}
Nate Begeman1d4d4142005-09-01 00:19:25 +0000253
254 /// Run - runs the dag combiner on all nodes in the work list
Duncan Sands25cf2272008-11-24 14:53:14 +0000255 void Run(CombineLevel AtLevel);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000256 };
257}
258
Chris Lattnerf8dc0612008-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 Gohmanb5660dc2008-02-20 16:44:09 +0000267 explicit WorkListRemover(DAGCombiner &dc) : DC(dc) {}
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000268
Duncan Sandsedfcf592008-06-11 11:42:12 +0000269 virtual void NodeDeleted(SDNode *N, SDNode *E) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000270 DC.removeFromWorkList(N);
271 }
272
273 virtual void NodeUpdated(SDNode *N) {
274 // Ignore updates.
275 }
276};
277}
278
Chris Lattner24664722006-03-01 04:53:38 +0000279//===----------------------------------------------------------------------===//
280// TargetLowering::DAGCombinerInfo implementation
281//===----------------------------------------------------------------------===//
282
283void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) {
284 ((DAGCombiner*)DC)->AddToWorkList(N);
285}
286
Dan Gohman475871a2008-07-27 21:46:04 +0000287SDValue TargetLowering::DAGCombinerInfo::
288CombineTo(SDNode *N, const std::vector<SDValue> &To) {
Chris Lattner3577e382006-08-11 17:56:38 +0000289 return ((DAGCombiner*)DC)->CombineTo(N, &To[0], To.size());
Chris Lattner24664722006-03-01 04:53:38 +0000290}
291
Dan Gohman475871a2008-07-27 21:46:04 +0000292SDValue TargetLowering::DAGCombinerInfo::
293CombineTo(SDNode *N, SDValue Res) {
Chris Lattner24664722006-03-01 04:53:38 +0000294 return ((DAGCombiner*)DC)->CombineTo(N, Res);
295}
296
297
Dan Gohman475871a2008-07-27 21:46:04 +0000298SDValue TargetLowering::DAGCombinerInfo::
299CombineTo(SDNode *N, SDValue Res0, SDValue Res1) {
Chris Lattner24664722006-03-01 04:53:38 +0000300 return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1);
301}
302
Dan Gohmane5af2d32009-01-29 01:59:02 +0000303void TargetLowering::DAGCombinerInfo::
304CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO) {
305 return ((DAGCombiner*)DC)->CommitTargetLoweringOpt(TLO);
306}
Chris Lattner24664722006-03-01 04:53:38 +0000307
Chris Lattner24664722006-03-01 04:53:38 +0000308//===----------------------------------------------------------------------===//
Chris Lattner29446522007-05-14 22:04:50 +0000309// 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 Sands25cf2272008-11-24 14:53:14 +0000315static char isNegatibleForFree(SDValue Op, bool LegalOperations,
Chris Lattner0254e702008-02-26 07:04:54 +0000316 unsigned Depth = 0) {
Dale Johannesendb44bf82007-10-16 23:38:29 +0000317 // No compile time optimizations on this type.
318 if (Op.getValueType() == MVT::ppcf128)
319 return 0;
320
Chris Lattner29446522007-05-14 22:04:50 +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
Chris Lattner3adf9512007-05-25 02:19:06 +0000327 // Don't recurse exponentially.
328 if (Depth > 6) return 0;
329
Chris Lattner29446522007-05-14 22:04:50 +0000330 switch (Op.getOpcode()) {
331 default: return false;
332 case ISD::ConstantFP:
Chris Lattner0254e702008-02-26 07:04:54 +0000333 // Don't invert constant FP values after legalize. The negated constant
334 // isn't necessarily legal.
Duncan Sands25cf2272008-11-24 14:53:14 +0000335 return LegalOperations ? 0 : 1;
Chris Lattner29446522007-05-14 22:04:50 +0000336 case ISD::FADD:
337 // FIXME: determine better conditions for this xform.
338 if (!UnsafeFPMath) return 0;
339
340 // -(A+B) -> -A - B
Duncan Sands25cf2272008-11-24 14:53:14 +0000341 if (char V = isNegatibleForFree(Op.getOperand(0), LegalOperations, Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000342 return V;
343 // -(A+B) -> -B - A
Duncan Sands25cf2272008-11-24 14:53:14 +0000344 return isNegatibleForFree(Op.getOperand(1), LegalOperations, Depth+1);
Chris Lattner29446522007-05-14 22:04:50 +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 Sands25cf2272008-11-24 14:53:14 +0000357 if (char V = isNegatibleForFree(Op.getOperand(0), LegalOperations, Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000358 return V;
359
Duncan Sands25cf2272008-11-24 14:53:14 +0000360 return isNegatibleForFree(Op.getOperand(1), LegalOperations, Depth+1);
Chris Lattner29446522007-05-14 22:04:50 +0000361
362 case ISD::FP_EXTEND:
363 case ISD::FP_ROUND:
364 case ISD::FSIN:
Duncan Sands25cf2272008-11-24 14:53:14 +0000365 return isNegatibleForFree(Op.getOperand(0), LegalOperations, Depth+1);
Chris Lattner29446522007-05-14 22:04:50 +0000366 }
367}
368
369/// GetNegatedExpression - If isNegatibleForFree returns true, this function
370/// returns the newly negated expression.
Dan Gohman475871a2008-07-27 21:46:04 +0000371static SDValue GetNegatedExpression(SDValue Op, SelectionDAG &DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000372 bool LegalOperations, unsigned Depth = 0) {
Chris Lattner29446522007-05-14 22:04:50 +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
Chris Lattner3adf9512007-05-25 02:19:06 +0000379 assert(Depth <= 6 && "GetNegatedExpression doesn't match isNegatibleForFree");
Chris Lattner29446522007-05-14 22:04:50 +0000380 switch (Op.getOpcode()) {
381 default: assert(0 && "Unknown code");
Dale Johannesenc4dd3c32007-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 }
Chris Lattner29446522007-05-14 22:04:50 +0000387 case ISD::FADD:
388 // FIXME: determine better conditions for this xform.
389 assert(UnsafeFPMath);
390
391 // -(A+B) -> -A - B
Duncan Sands25cf2272008-11-24 14:53:14 +0000392 if (isNegatibleForFree(Op.getOperand(0), LegalOperations, Depth+1))
Bill Wendling35247c32009-01-30 00:45:56 +0000393 return DAG.getNode(ISD::FSUB, Op.getDebugLoc(), Op.getValueType(),
Chris Lattner0254e702008-02-26 07:04:54 +0000394 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000395 LegalOperations, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000396 Op.getOperand(1));
397 // -(A+B) -> -B - A
Bill Wendling35247c32009-01-30 00:45:56 +0000398 return DAG.getNode(ISD::FSUB, Op.getDebugLoc(), Op.getValueType(),
Chris Lattner0254e702008-02-26 07:04:54 +0000399 GetNegatedExpression(Op.getOperand(1), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000400 LegalOperations, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +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);
Dan Gohman23ff1822007-07-02 15:48:56 +0000405
406 // -(0-B) -> B
407 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(Op.getOperand(0)))
Dale Johannesenc4dd3c32007-08-31 23:34:27 +0000408 if (N0CFP->getValueAPF().isZero())
Dan Gohman23ff1822007-07-02 15:48:56 +0000409 return Op.getOperand(1);
Chris Lattner29446522007-05-14 22:04:50 +0000410
411 // -(A-B) -> B-A
Bill Wendling35247c32009-01-30 00:45:56 +0000412 return DAG.getNode(ISD::FSUB, Op.getDebugLoc(), Op.getValueType(),
413 Op.getOperand(1), Op.getOperand(0));
Chris Lattner29446522007-05-14 22:04:50 +0000414
415 case ISD::FMUL:
416 case ISD::FDIV:
417 assert(!HonorSignDependentRoundingFPMath());
418
419 // -(X*Y) -> -X * Y
Duncan Sands25cf2272008-11-24 14:53:14 +0000420 if (isNegatibleForFree(Op.getOperand(0), LegalOperations, Depth+1))
Bill Wendling35247c32009-01-30 00:45:56 +0000421 return DAG.getNode(Op.getOpcode(), Op.getDebugLoc(), Op.getValueType(),
Chris Lattner0254e702008-02-26 07:04:54 +0000422 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000423 LegalOperations, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000424 Op.getOperand(1));
425
426 // -(X*Y) -> X * -Y
Bill Wendling35247c32009-01-30 00:45:56 +0000427 return DAG.getNode(Op.getOpcode(), Op.getDebugLoc(), Op.getValueType(),
Chris Lattner29446522007-05-14 22:04:50 +0000428 Op.getOperand(0),
Chris Lattner0254e702008-02-26 07:04:54 +0000429 GetNegatedExpression(Op.getOperand(1), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000430 LegalOperations, Depth+1));
Chris Lattner29446522007-05-14 22:04:50 +0000431
432 case ISD::FP_EXTEND:
Chris Lattner29446522007-05-14 22:04:50 +0000433 case ISD::FSIN:
Bill Wendling35247c32009-01-30 00:45:56 +0000434 return DAG.getNode(Op.getOpcode(), Op.getDebugLoc(), Op.getValueType(),
Chris Lattner0254e702008-02-26 07:04:54 +0000435 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000436 LegalOperations, Depth+1));
Chris Lattner0bd48932008-01-17 07:00:52 +0000437 case ISD::FP_ROUND:
Bill Wendling35247c32009-01-30 00:45:56 +0000438 return DAG.getNode(ISD::FP_ROUND, Op.getDebugLoc(), Op.getValueType(),
Chris Lattner0254e702008-02-26 07:04:54 +0000439 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000440 LegalOperations, Depth+1),
Chris Lattner0bd48932008-01-17 07:00:52 +0000441 Op.getOperand(1));
Chris Lattner29446522007-05-14 22:04:50 +0000442 }
443}
Chris Lattner24664722006-03-01 04:53:38 +0000444
445
Nate Begeman4ebd8052005-09-01 23:24:04 +0000446// 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.
Nate Begeman646d7e22005-09-02 21:18:40 +0000448// 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 Gohman475871a2008-07-27 21:46:04 +0000451static bool isSetCCEquivalent(SDValue N, SDValue &LHS, SDValue &RHS,
452 SDValue &CC) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000453 if (N.getOpcode() == ISD::SETCC) {
454 LHS = N.getOperand(0);
455 RHS = N.getOperand(1);
456 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000457 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000458 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000459 if (N.getOpcode() == ISD::SELECT_CC &&
460 N.getOperand(2).getOpcode() == ISD::Constant &&
461 N.getOperand(3).getOpcode() == ISD::Constant &&
Dan Gohman002e5d02008-03-13 22:13:53 +0000462 cast<ConstantSDNode>(N.getOperand(2))->getAPIntValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000463 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
464 LHS = N.getOperand(0);
465 RHS = N.getOperand(1);
466 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000467 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000468 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000469 return false;
470}
471
Nate Begeman99801192005-09-07 23:25:52 +0000472// 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 Gohman475871a2008-07-27 21:46:04 +0000475static bool isOneUseSetCC(SDValue N) {
476 SDValue N0, N1, N2;
Gabor Greifba36cb52008-08-28 21:40:38 +0000477 if (isSetCCEquivalent(N, N0, N1, N2) && N.getNode()->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000478 return true;
479 return false;
480}
481
Bill Wendling35247c32009-01-30 00:45:56 +0000482SDValue DAGCombiner::ReassociateOps(unsigned Opc, DebugLoc DL,
483 SDValue N0, SDValue N1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000484 MVT VT = N0.getValueType();
Nate Begemancd4d58c2006-02-03 06:46:56 +0000485 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
486 if (isa<ConstantSDNode>(N1)) {
Bill Wendling35247c32009-01-30 00:45:56 +0000487 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
Bill Wendling6af76182009-01-30 20:50:00 +0000488 SDValue OpNode =
489 DAG.FoldConstantArithmetic(Opc, VT,
490 cast<ConstantSDNode>(N0.getOperand(1)),
491 cast<ConstantSDNode>(N1));
Bill Wendlingd69c3142009-01-30 02:23:43 +0000492 return DAG.getNode(Opc, DL, VT, N0.getOperand(0), OpNode);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000493 } else if (N0.hasOneUse()) {
Bill Wendling35247c32009-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 Greifba36cb52008-08-28 21:40:38 +0000497 AddToWorkList(OpNode.getNode());
Bill Wendling35247c32009-01-30 00:45:56 +0000498 return DAG.getNode(Opc, DL, VT, OpNode, N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000499 }
500 }
Bill Wendling35247c32009-01-30 00:45:56 +0000501
Nate Begemancd4d58c2006-02-03 06:46:56 +0000502 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
503 if (isa<ConstantSDNode>(N0)) {
Bill Wendling35247c32009-01-30 00:45:56 +0000504 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
Bill Wendling6af76182009-01-30 20:50:00 +0000505 SDValue OpNode =
506 DAG.FoldConstantArithmetic(Opc, VT,
507 cast<ConstantSDNode>(N1.getOperand(1)),
508 cast<ConstantSDNode>(N0));
Bill Wendlingd69c3142009-01-30 02:23:43 +0000509 return DAG.getNode(Opc, DL, VT, N1.getOperand(0), OpNode);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000510 } else if (N1.hasOneUse()) {
Bill Wendling35247c32009-01-30 00:45:56 +0000511 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
Bill Wendlingd69c3142009-01-30 02:23:43 +0000512 SDValue OpNode = DAG.getNode(Opc, N0.getDebugLoc(), VT,
Bill Wendling35247c32009-01-30 00:45:56 +0000513 N1.getOperand(0), N0);
Gabor Greifba36cb52008-08-28 21:40:38 +0000514 AddToWorkList(OpNode.getNode());
Bill Wendling35247c32009-01-30 00:45:56 +0000515 return DAG.getNode(Opc, DL, VT, OpNode, N1.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000516 }
517 }
Bill Wendling35247c32009-01-30 00:45:56 +0000518
Dan Gohman475871a2008-07-27 21:46:04 +0000519 return SDValue();
Nate Begemancd4d58c2006-02-03 06:46:56 +0000520}
521
Dan Gohman475871a2008-07-27 21:46:04 +0000522SDValue DAGCombiner::CombineTo(SDNode *N, const SDValue *To, unsigned NumTo,
523 bool AddTo) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000524 assert(N->getNumValues() == NumTo && "Broken CombineTo call!");
525 ++NodesCombined;
526 DOUT << "\nReplacing.1 "; DEBUG(N->dump(&DAG));
Gabor Greifba36cb52008-08-28 21:40:38 +0000527 DOUT << "\nWith: "; DEBUG(To[0].getNode()->dump(&DAG));
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000528 DOUT << " and " << NumTo-1 << " other values\n";
Dan Gohman764fd0c2009-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 Lattnerf8dc0612008-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 Greifba36cb52008-08-28 21:40:38 +0000538 AddToWorkList(To[i].getNode());
539 AddUsersToWorkList(To[i].getNode());
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000540 }
541 }
542
Dan Gohmandbe664a2009-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 Lattnerf8dc0612008-02-03 06:49:24 +0000550
Dan Gohmandbe664a2009-01-19 21:44:21 +0000551 // Finally, since the node is now dead, remove it from the graph.
552 DAG.DeleteNode(N);
553 }
Dan Gohman475871a2008-07-27 21:46:04 +0000554 return SDValue(N, 0);
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000555}
556
Dan Gohmane5af2d32009-01-29 01:59:02 +0000557void
558DAGCombiner::CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &
559 TLO) {
Chris Lattnerf8dc0612008-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 Gohmane5af2d32009-01-29 01:59:02 +0000564
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000565 // Push the new node and any (possibly new) users onto the worklist.
Gabor Greifba36cb52008-08-28 21:40:38 +0000566 AddToWorkList(TLO.New.getNode());
567 AddUsersToWorkList(TLO.New.getNode());
Chris Lattnerf8dc0612008-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 Greifba36cb52008-08-28 21:40:38 +0000572 if (TLO.Old.getNode()->use_empty()) {
573 removeFromWorkList(TLO.Old.getNode());
Chris Lattnerf8dc0612008-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 Greifba36cb52008-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 Lattnerf8dc0612008-02-03 06:49:24 +0000580
Gabor Greifba36cb52008-08-28 21:40:38 +0000581 DAG.DeleteNode(TLO.Old.getNode());
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000582 }
Dan Gohmane5af2d32009-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 Lattnerf8dc0612008-02-03 06:49:24 +0000604 return true;
605}
606
Chris Lattner29446522007-05-14 22:04:50 +0000607//===----------------------------------------------------------------------===//
608// Main DAG Combiner implementation
609//===----------------------------------------------------------------------===//
610
Duncan Sands25cf2272008-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;
Nate Begeman4ebd8052005-09-01 23:24:04 +0000616
Evan Cheng17a568b2008-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 Sands25cf2272008-11-24 14:53:14 +0000622
Evan Cheng17a568b2008-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
Jim Laskey26f7fa72006-10-17 19:33:52 +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 Gohman475871a2008-07-27 21:46:04 +0000630 DAG.setRoot(SDValue());
Chris Lattner24664722006-03-01 04:53:38 +0000631
Evan Cheng17a568b2008-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;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000647 }
Evan Cheng17a568b2008-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!");
Chris Lattner729c6d12006-05-27 00:43:02 +0000666
Evan Cheng17a568b2008-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 Gohmandbe664a2009-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 Cheng17a568b2008-08-29 22:21:44 +0000697
Dan Gohmandbe664a2009-01-19 21:44:21 +0000698 // Finally, since the node is now dead, remove it from the graph.
699 DAG.DeleteNode(N);
700 }
Evan Cheng17a568b2008-08-29 22:21:44 +0000701 }
702
Chris Lattner95038592005-10-05 06:35:28 +0000703 // If the root changed (e.g. it was a dead load, update the root).
704 DAG.setRoot(Dummy.getValue());
Nate Begeman1d4d4142005-09-01 00:19:25 +0000705}
706
Dan Gohman475871a2008-07-27 21:46:04 +0000707SDValue DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000708 switch(N->getOpcode()) {
709 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +0000710 case ISD::TokenFactor: return visitTokenFactor(N);
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000711 case ISD::MERGE_VALUES: return visitMERGE_VALUES(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000712 case ISD::ADD: return visitADD(N);
713 case ISD::SUB: return visitSUB(N);
Chris Lattner91153682007-03-04 20:03:15 +0000714 case ISD::ADDC: return visitADDC(N);
715 case ISD::ADDE: return visitADDE(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000716 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 Gohman389079b2007-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);
Nate Begeman646d7e22005-09-02 21:18:40 +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);
Nate Begeman452d7be2005-09-16 00:54:12 +0000736 case ISD::SELECT: return visitSELECT(N);
737 case ISD::SELECT_CC: return visitSELECT_CC(N);
738 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000739 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
740 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000741 case ISD::ANY_EXTEND: return visitANY_EXTEND(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000742 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
743 case ISD::TRUNCATE: return visitTRUNCATE(N);
Chris Lattner94683772005-12-23 05:30:37 +0000744 case ISD::BIT_CONVERT: return visitBIT_CONVERT(N);
Evan Cheng9bfa03c2008-05-12 23:04:07 +0000745 case ISD::BUILD_PAIR: return visitBUILD_PAIR(N);
Chris Lattner01b3d732005-09-28 22:28:18 +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);
Chris Lattner12d83032006-03-05 05:30:57 +0000751 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000752 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);
Nate Begeman44728a72005-09-19 22:34:01 +0000761 case ISD::BRCOND: return visitBRCOND(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000762 case ISD::BR_CC: return visitBR_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000763 case ISD::LOAD: return visitLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000764 case ISD::STORE: return visitSTORE(N);
Chris Lattnerca242442006-03-19 01:27:56 +0000765 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
Evan Cheng513da432007-10-06 08:19:55 +0000766 case ISD::EXTRACT_VECTOR_ELT: return visitEXTRACT_VECTOR_ELT(N);
Dan Gohman7f321562007-06-25 16:23:39 +0000767 case ISD::BUILD_VECTOR: return visitBUILD_VECTOR(N);
768 case ISD::CONCAT_VECTORS: return visitCONCAT_VECTORS(N);
Chris Lattner66445d32006-03-28 22:11:53 +0000769 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000770 }
Dan Gohman475871a2008-07-27 21:46:04 +0000771 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000772}
773
Dan Gohman475871a2008-07-27 21:46:04 +0000774SDValue DAGCombiner::combine(SDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +0000775 SDValue RV = visit(N);
Dan Gohman389079b2007-10-08 17:57:15 +0000776
777 // If nothing happened, try a target-specific DAG combine.
Gabor Greifba36cb52008-08-28 21:40:38 +0000778 if (RV.getNode() == 0) {
Dan Gohman389079b2007-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 Sands25cf2272008-11-24 14:53:14 +0000787 DagCombineInfo(DAG, Level == Unrestricted, false, this);
Dan Gohman389079b2007-10-08 17:57:15 +0000788
789 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
790 }
791 }
792
Evan Cheng08b11732008-03-22 01:55:50 +0000793 // If N is a commutative binary node, try commuting it to enable more
794 // sdisel CSE.
Gabor Greifba36cb52008-08-28 21:40:38 +0000795 if (RV.getNode() == 0 &&
Evan Cheng08b11732008-03-22 01:55:50 +0000796 SelectionDAG::isCommutativeBinOp(N->getOpcode()) &&
797 N->getNumValues() == 1) {
Dan Gohman475871a2008-07-27 21:46:04 +0000798 SDValue N0 = N->getOperand(0);
799 SDValue N1 = N->getOperand(1);
Bill Wendling5c71acf2009-01-30 01:13:16 +0000800
Evan Cheng08b11732008-03-22 01:55:50 +0000801 // Constant operands are canonicalized to RHS.
802 if (isa<ConstantSDNode>(N0) || !isa<ConstantSDNode>(N1)) {
Dan Gohman475871a2008-07-27 21:46:04 +0000803 SDValue Ops[] = { N1, N0 };
Evan Cheng08b11732008-03-22 01:55:50 +0000804 SDNode *CSENode = DAG.getNodeIfExists(N->getOpcode(), N->getVTList(),
805 Ops, 2);
Evan Chengea100462008-03-24 23:55:16 +0000806 if (CSENode)
Dan Gohman475871a2008-07-27 21:46:04 +0000807 return SDValue(CSENode, 0);
Evan Cheng08b11732008-03-22 01:55:50 +0000808 }
809 }
810
Dan Gohman389079b2007-10-08 17:57:15 +0000811 return RV;
812}
813
Chris Lattner6270f682006-10-08 22:57:01 +0000814/// getInputChainForNode - Given a node, return its input chain if it has one,
815/// otherwise return a null sd operand.
Dan Gohman475871a2008-07-27 21:46:04 +0000816static SDValue getInputChainForNode(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +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 Wendling5c71acf2009-01-30 01:13:16 +0000826 return SDValue();
Chris Lattner6270f682006-10-08 22:57:01 +0000827}
828
Dan Gohman475871a2008-07-27 21:46:04 +0000829SDValue DAGCombiner::visitTokenFactor(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +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 Greifba36cb52008-08-28 21:40:38 +0000833 if (getInputChainForNode(N->getOperand(0).getNode()) == N->getOperand(1))
Chris Lattner6270f682006-10-08 22:57:01 +0000834 return N->getOperand(0);
Gabor Greifba36cb52008-08-28 21:40:38 +0000835 if (getInputChainForNode(N->getOperand(1).getNode()) == N->getOperand(0))
Chris Lattner6270f682006-10-08 22:57:01 +0000836 return N->getOperand(1);
837 }
838
Chris Lattnerc76d4412007-05-16 06:37:59 +0000839 SmallVector<SDNode *, 8> TFs; // List of token factors to visit.
Dan Gohman475871a2008-07-27 21:46:04 +0000840 SmallVector<SDValue, 8> Ops; // Ops for replacing token factor.
Chris Lattnerc76d4412007-05-16 06:37:59 +0000841 SmallPtrSet<SDNode*, 16> SeenOps;
842 bool Changed = false; // If we should replace this token factor.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000843
844 // Start out with this token factor.
Jim Laskey279f0532006-09-25 16:29:54 +0000845 TFs.push_back(N);
Jim Laskey279f0532006-09-25 16:29:54 +0000846
Jim Laskey71382342006-10-07 23:37:56 +0000847 // Iterate through token factors. The TFs grows when new token factors are
Jim Laskeybc588b82006-10-05 15:07:25 +0000848 // encountered.
849 for (unsigned i = 0; i < TFs.size(); ++i) {
850 SDNode *TF = TFs[i];
851
Jim Laskey6ff23e52006-10-04 16:53:27 +0000852 // Check each of the operands.
853 for (unsigned i = 0, ie = TF->getNumOperands(); i != ie; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +0000854 SDValue Op = TF->getOperand(i);
Jim Laskey279f0532006-09-25 16:29:54 +0000855
Jim Laskey6ff23e52006-10-04 16:53:27 +0000856 switch (Op.getOpcode()) {
857 case ISD::EntryToken:
Jim Laskeybc588b82006-10-05 15:07:25 +0000858 // Entry tokens don't need to be added to the list. They are
859 // rededundant.
860 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000861 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000862
Jim Laskey6ff23e52006-10-04 16:53:27 +0000863 case ISD::TokenFactor:
Jim Laskeybc588b82006-10-05 15:07:25 +0000864 if ((CombinerAA || Op.hasOneUse()) &&
Gabor Greifba36cb52008-08-28 21:40:38 +0000865 std::find(TFs.begin(), TFs.end(), Op.getNode()) == TFs.end()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +0000866 // Queue up for processing.
Gabor Greifba36cb52008-08-28 21:40:38 +0000867 TFs.push_back(Op.getNode());
Jim Laskey6ff23e52006-10-04 16:53:27 +0000868 // Clean up in case the token factor is removed.
Gabor Greifba36cb52008-08-28 21:40:38 +0000869 AddToWorkList(Op.getNode());
Jim Laskey6ff23e52006-10-04 16:53:27 +0000870 Changed = true;
871 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000872 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000873 // Fall thru
874
875 default:
Chris Lattnerc76d4412007-05-16 06:37:59 +0000876 // Only add if it isn't already in the list.
Gabor Greifba36cb52008-08-28 21:40:38 +0000877 if (SeenOps.insert(Op.getNode()))
Jim Laskeybc588b82006-10-05 15:07:25 +0000878 Ops.push_back(Op);
Chris Lattnerc76d4412007-05-16 06:37:59 +0000879 else
880 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000881 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000882 }
883 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000884 }
885
Dan Gohman475871a2008-07-27 21:46:04 +0000886 SDValue Result;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000887
888 // If we've change things around then replace token factor.
889 if (Changed) {
Dan Gohman30359592008-01-29 13:02:09 +0000890 if (Ops.empty()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +0000891 // The entry token is the only possible outcome.
892 Result = DAG.getEntryNode();
893 } else {
894 // New and improved token factor.
Bill Wendling5c71acf2009-01-30 01:13:16 +0000895 Result = DAG.getNode(ISD::TokenFactor, N->getDebugLoc(),
896 MVT::Other, &Ops[0], Ops.size());
Nate Begemanded49632005-10-13 03:11:28 +0000897 }
Bill Wendling5c71acf2009-01-30 01:13:16 +0000898
Jim Laskey274062c2006-10-13 23:32:28 +0000899 // Don't add users to work list.
900 return CombineTo(N, Result, false);
Nate Begemanded49632005-10-13 03:11:28 +0000901 }
Jim Laskey279f0532006-09-25 16:29:54 +0000902
Jim Laskey6ff23e52006-10-04 16:53:27 +0000903 return Result;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000904}
905
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000906/// MERGE_VALUES can always be eliminated.
Dan Gohman475871a2008-07-27 21:46:04 +0000907SDValue DAGCombiner::visitMERGE_VALUES(SDNode *N) {
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000908 WorkListRemover DeadNodes(*this);
909 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Dan Gohman475871a2008-07-27 21:46:04 +0000910 DAG.ReplaceAllUsesOfValueWith(SDValue(N, i), N->getOperand(i),
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000911 &DeadNodes);
912 removeFromWorkList(N);
913 DAG.DeleteNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +0000914 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000915}
916
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000917static
Bill Wendlingd69c3142009-01-30 02:23:43 +0000918SDValue combineShlAddConstant(DebugLoc DL, SDValue N0, SDValue N1,
919 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000920 MVT VT = N0.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +0000921 SDValue N00 = N0.getOperand(0);
922 SDValue N01 = N0.getOperand(1);
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000923 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N01);
Bill Wendlingd69c3142009-01-30 02:23:43 +0000924
Gabor Greifba36cb52008-08-28 21:40:38 +0000925 if (N01C && N00.getOpcode() == ISD::ADD && N00.getNode()->hasOneUse() &&
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000926 isa<ConstantSDNode>(N00.getOperand(1))) {
Bill Wendlingd69c3142009-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);
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000934 }
Bill Wendlingd69c3142009-01-30 02:23:43 +0000935
Dan Gohman475871a2008-07-27 21:46:04 +0000936 return SDValue();
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000937}
938
Evan Chengb13cdbd2007-06-21 07:39:16 +0000939static
Dan Gohman475871a2008-07-27 21:46:04 +0000940SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
Bill Wendlingae89bb12008-11-11 08:25:46 +0000941 SelectionDAG &DAG, const TargetLowering &TLI,
Duncan Sands25cf2272008-11-24 14:53:14 +0000942 bool LegalOperations) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000943 MVT VT = N->getValueType(0);
Evan Chengb13cdbd2007-06-21 07:39:16 +0000944 unsigned Opc = N->getOpcode();
945 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
Dan Gohman475871a2008-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);
Evan Chengb13cdbd2007-06-21 07:39:16 +0000948 ISD::CondCode CC = ISD::SETCC_INVALID;
Bill Wendlingae89bb12008-11-11 08:25:46 +0000949
950 if (isSlctCC) {
Evan Chengb13cdbd2007-06-21 07:39:16 +0000951 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
Bill Wendlingae89bb12008-11-11 08:25:46 +0000952 } else {
Dan Gohman475871a2008-07-27 21:46:04 +0000953 SDValue CCOp = Slct.getOperand(0);
Evan Chengb13cdbd2007-06-21 07:39:16 +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 Wendlingae89bb12008-11-11 08:25:46 +0000962
Evan Chengb13cdbd2007-06-21 07:39:16 +0000963 if (LHS.getOpcode() == ISD::Constant &&
Bill Wendlingae89bb12008-11-11 08:25:46 +0000964 cast<ConstantSDNode>(LHS)->isNullValue()) {
Evan Chengb13cdbd2007-06-21 07:39:16 +0000965 DoXform = true;
Bill Wendlingae89bb12008-11-11 08:25:46 +0000966 } else if (CC != ISD::SETCC_INVALID &&
967 RHS.getOpcode() == ISD::Constant &&
968 cast<ConstantSDNode>(RHS)->isNullValue()) {
Evan Chengb13cdbd2007-06-21 07:39:16 +0000969 std::swap(LHS, RHS);
Dan Gohman475871a2008-07-27 21:46:04 +0000970 SDValue Op0 = Slct.getOperand(0);
Bill Wendlingae89bb12008-11-11 08:25:46 +0000971 MVT OpVT = isSlctCC ? Op0.getValueType() :
972 Op0.getOperand(0).getValueType();
973 bool isInt = OpVT.isInteger();
Evan Chengb13cdbd2007-06-21 07:39:16 +0000974 CC = ISD::getSetCCInverse(CC, isInt);
Bill Wendlingae89bb12008-11-11 08:25:46 +0000975
Duncan Sands25cf2272008-11-24 14:53:14 +0000976 if (LegalOperations && !TLI.isCondCodeLegal(CC, OpVT))
Bill Wendlingae89bb12008-11-11 08:25:46 +0000977 return SDValue(); // Inverse operator isn't legal.
978
Evan Chengb13cdbd2007-06-21 07:39:16 +0000979 DoXform = true;
980 InvCC = true;
981 }
982
983 if (DoXform) {
Bill Wendlingd69c3142009-01-30 02:23:43 +0000984 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
Evan Chengb13cdbd2007-06-21 07:39:16 +0000985 if (isSlctCC)
Bill Wendlingd69c3142009-01-30 02:23:43 +0000986 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
Evan Chengb13cdbd2007-06-21 07:39:16 +0000987 Slct.getOperand(0), Slct.getOperand(1), CC);
Dan Gohman475871a2008-07-27 21:46:04 +0000988 SDValue CCOp = Slct.getOperand(0);
Evan Chengb13cdbd2007-06-21 07:39:16 +0000989 if (InvCC)
Bill Wendlingd69c3142009-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);
Evan Chengb13cdbd2007-06-21 07:39:16 +0000994 }
Dan Gohman475871a2008-07-27 21:46:04 +0000995 return SDValue();
Evan Chengb13cdbd2007-06-21 07:39:16 +0000996}
997
Dan Gohman475871a2008-07-27 21:46:04 +0000998SDValue DAGCombiner::visitADD(SDNode *N) {
999 SDValue N0 = N->getOperand(0);
1000 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001001 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1002 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001003 MVT VT = N0.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +00001004
1005 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001006 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001007 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001008 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001009 }
Bill Wendling2476e5d2008-12-10 22:36:00 +00001010
Dan Gohman613e0d82007-07-03 14:03:57 +00001011 // fold (add x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00001012 if (N0.getOpcode() == ISD::UNDEF)
1013 return N0;
1014 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001015 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001016 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001017 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001018 return DAG.FoldConstantArithmetic(ISD::ADD, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00001019 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001020 if (N0C && !N1C)
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001021 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001022 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001023 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001024 return N0;
Dan Gohman6520e202008-10-18 02:06:02 +00001025 // fold (add Sym, c) -> Sym+c
1026 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0))
Duncan Sands25cf2272008-11-24 14:53:14 +00001027 if (!LegalOperations && TLI.isOffsetFoldingLegal(GA) && N1C &&
Dan Gohman6520e202008-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());
Chris Lattner4aafb4f2006-01-12 20:22:43 +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 Wendlingf4eb2262009-01-30 02:31:17 +00001035 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
Dan Gohman002e5d02008-03-13 22:13:53 +00001036 DAG.getConstant(N1C->getAPIntValue()+
1037 N0C->getAPIntValue(), VT),
Chris Lattner4aafb4f2006-01-12 20:22:43 +00001038 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +00001039 // reassociate add
Bill Wendling35247c32009-01-30 00:45:56 +00001040 SDValue RADD = ReassociateOps(ISD::ADD, N->getDebugLoc(), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001041 if (RADD.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001042 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001043 // 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 Wendlingf4eb2262009-01-30 02:31:17 +00001046 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +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 Wendlingf4eb2262009-01-30 02:31:17 +00001050 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +00001051 // fold (A+(B-A)) -> B
1052 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001053 return N1.getOperand(0);
Dale Johannesen56eca912008-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 Johannesen221cd2f2008-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 Wendlingf4eb2262009-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 Johannesen221cd2f2008-12-02 01:30:54 +00001061 N1.getOperand(1).getOperand(1));
Dale Johannesen221cd2f2008-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 Wendlingf4eb2262009-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 Johannesen221cd2f2008-12-02 01:30:54 +00001066 N1.getOperand(1).getOperand(0));
Dale Johannesen7c7bc722008-12-23 23:47:22 +00001067 // fold (A+((B-A)+or-C)) to (B+or-C)
Dale Johannesen34d79852008-12-02 18:40:40 +00001068 if ((N1.getOpcode() == ISD::SUB || N1.getOpcode() == ISD::ADD) &&
1069 N1.getOperand(0).getOpcode() == ISD::SUB &&
Bill Wendlingf4eb2262009-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 Johannesen34d79852008-12-02 18:40:40 +00001073
Dale Johannesen221cd2f2008-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 Wendlingf4eb2262009-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 Johannesen221cd2f2008-12-02 01:30:54 +00001085 }
Chris Lattner947c2892006-03-13 06:51:27 +00001086
Dan Gohman475871a2008-07-27 21:46:04 +00001087 if (!VT.isVector() && SimplifyDemandedBits(SDValue(N, 0)))
1088 return SDValue(N, 0);
Chris Lattner947c2892006-03-13 06:51:27 +00001089
1090 // fold (a+b) -> (a|b) iff a and b share no bits.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001091 if (VT.isInteger() && !VT.isVector()) {
Dan Gohman948d8ea2008-02-20 16:33:30 +00001092 APInt LHSZero, LHSOne;
1093 APInt RHSZero, RHSOne;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001094 APInt Mask = APInt::getAllOnesValue(VT.getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00001095 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001096
Dan Gohman948d8ea2008-02-20 16:33:30 +00001097 if (LHSZero.getBoolValue()) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001098 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
Chris Lattner947c2892006-03-13 06:51:27 +00001099
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 Wendlingf4eb2262009-01-30 02:31:17 +00001104 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N0, N1);
Chris Lattner947c2892006-03-13 06:51:27 +00001105 }
1106 }
Evan Cheng3ef554d2006-11-06 08:14:30 +00001107
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001108 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
Gabor Greifba36cb52008-08-28 21:40:38 +00001109 if (N0.getOpcode() == ISD::SHL && N0.getNode()->hasOneUse()) {
Bill Wendlingd69c3142009-01-30 02:23:43 +00001110 SDValue Result = combineShlAddConstant(N->getDebugLoc(), N0, N1, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001111 if (Result.getNode()) return Result;
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001112 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001113 if (N1.getOpcode() == ISD::SHL && N1.getNode()->hasOneUse()) {
Bill Wendlingd69c3142009-01-30 02:23:43 +00001114 SDValue Result = combineShlAddConstant(N->getDebugLoc(), N1, N0, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001115 if (Result.getNode()) return Result;
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001116 }
1117
Evan Chengb13cdbd2007-06-21 07:39:16 +00001118 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
Gabor Greifba36cb52008-08-28 21:40:38 +00001119 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
Duncan Sands25cf2272008-11-24 14:53:14 +00001120 SDValue Result = combineSelectAndUse(N, N0, N1, DAG, TLI, LegalOperations);
Gabor Greifba36cb52008-08-28 21:40:38 +00001121 if (Result.getNode()) return Result;
Evan Chengb13cdbd2007-06-21 07:39:16 +00001122 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001123 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
Duncan Sands25cf2272008-11-24 14:53:14 +00001124 SDValue Result = combineSelectAndUse(N, N1, N0, DAG, TLI, LegalOperations);
Gabor Greifba36cb52008-08-28 21:40:38 +00001125 if (Result.getNode()) return Result;
Evan Chengb13cdbd2007-06-21 07:39:16 +00001126 }
1127
Dan Gohman475871a2008-07-27 21:46:04 +00001128 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001129}
1130
Dan Gohman475871a2008-07-27 21:46:04 +00001131SDValue DAGCombiner::visitADDC(SDNode *N) {
1132 SDValue N0 = N->getOperand(0);
1133 SDValue N1 = N->getOperand(1);
Chris Lattner91153682007-03-04 20:03:15 +00001134 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1135 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001136 MVT VT = N0.getValueType();
Chris Lattner91153682007-03-04 20:03:15 +00001137
1138 // If the flag result is dead, turn this into an ADD.
1139 if (N->hasNUsesOfValue(0, 1))
Bill Wendling14036c02009-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));
Chris Lattner91153682007-03-04 20:03:15 +00001143
1144 // canonicalize constant to RHS.
Dan Gohman0a4627d2008-06-23 15:29:14 +00001145 if (N0C && !N1C)
Bill Wendling14036c02009-01-30 02:38:00 +00001146 return DAG.getNode(ISD::ADDC, N->getDebugLoc(), N->getVTList(), N1, N0);
Chris Lattner91153682007-03-04 20:03:15 +00001147
Chris Lattnerb6541762007-03-04 20:40:38 +00001148 // fold (addc x, 0) -> x + no carry out
1149 if (N1C && N1C->isNullValue())
Bill Wendling14036c02009-01-30 02:38:00 +00001150 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE,
1151 N->getDebugLoc(), MVT::Flag));
Chris Lattnerb6541762007-03-04 20:40:38 +00001152
1153 // fold (addc a, b) -> (or a, b), CARRY_FALSE iff a and b share no bits.
Dan Gohman948d8ea2008-02-20 16:33:30 +00001154 APInt LHSZero, LHSOne;
1155 APInt RHSZero, RHSOne;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001156 APInt Mask = APInt::getAllOnesValue(VT.getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00001157 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Bill Wendling14036c02009-01-30 02:38:00 +00001158
Dan Gohman948d8ea2008-02-20 16:33:30 +00001159 if (LHSZero.getBoolValue()) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001160 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
Chris Lattnerb6541762007-03-04 20:40:38 +00001161
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 Wendling14036c02009-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));
Chris Lattnerb6541762007-03-04 20:40:38 +00001169 }
Chris Lattner91153682007-03-04 20:03:15 +00001170
Dan Gohman475871a2008-07-27 21:46:04 +00001171 return SDValue();
Chris Lattner91153682007-03-04 20:03:15 +00001172}
1173
Dan Gohman475871a2008-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);
Chris Lattner91153682007-03-04 20:03:15 +00001178 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1179 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Chris Lattner91153682007-03-04 20:03:15 +00001180
1181 // canonicalize constant to RHS
Dan Gohman0a4627d2008-06-23 15:29:14 +00001182 if (N0C && !N1C)
Bill Wendling14036c02009-01-30 02:38:00 +00001183 return DAG.getNode(ISD::ADDE, N->getDebugLoc(), N->getVTList(),
1184 N1, N0, CarryIn);
Chris Lattner91153682007-03-04 20:03:15 +00001185
Chris Lattnerb6541762007-03-04 20:40:38 +00001186 // fold (adde x, y, false) -> (addc x, y)
Dan Gohman0a4627d2008-06-23 15:29:14 +00001187 if (CarryIn.getOpcode() == ISD::CARRY_FALSE)
Bill Wendling14036c02009-01-30 02:38:00 +00001188 return DAG.getNode(ISD::ADDC, N->getDebugLoc(), N->getVTList(), N1, N0);
Chris Lattner91153682007-03-04 20:03:15 +00001189
Dan Gohman475871a2008-07-27 21:46:04 +00001190 return SDValue();
Chris Lattner91153682007-03-04 20:03:15 +00001191}
1192
Dan Gohman475871a2008-07-27 21:46:04 +00001193SDValue DAGCombiner::visitSUB(SDNode *N) {
1194 SDValue N0 = N->getOperand(0);
1195 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001196 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1197 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Duncan Sands83ec4b62008-06-06 12:08:01 +00001198 MVT VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001199
Dan Gohman7f321562007-06-25 16:23:39 +00001200 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001201 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001202 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001203 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001204 }
Bill Wendling2476e5d2008-12-10 22:36:00 +00001205
Chris Lattner854077d2005-10-17 01:07:11 +00001206 // fold (sub x, x) -> 0
Evan Chengc8e3b142008-03-12 07:02:50 +00001207 if (N0 == N1)
Chris Lattner854077d2005-10-17 01:07:11 +00001208 return DAG.getConstant(0, N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001209 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001210 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001211 return DAG.FoldConstantArithmetic(ISD::SUB, VT, N0C, N1C);
Chris Lattner05b57432005-10-11 06:07:15 +00001212 // fold (sub x, c) -> (add x, -c)
1213 if (N1C)
Bill Wendlingb0702e02009-01-30 02:42:10 +00001214 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001215 DAG.getConstant(-N1C->getAPIntValue(), VT));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001216 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +00001217 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001218 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001219 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +00001220 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Dale Johannesenf9cbc1f2008-12-23 23:01:27 +00001221 return N0.getOperand(0);
Dale Johannesen7c7bc722008-12-23 23:47:22 +00001222 // fold ((A+(B+or-C))-B) -> A+or-C
Dale Johannesenfd3b7b72008-12-16 22:13:49 +00001223 if (N0.getOpcode() == ISD::ADD &&
Dale Johannesenf9cbc1f2008-12-23 23:01:27 +00001224 (N0.getOperand(1).getOpcode() == ISD::SUB ||
1225 N0.getOperand(1).getOpcode() == ISD::ADD) &&
Dale Johannesenfd3b7b72008-12-16 22:13:49 +00001226 N0.getOperand(1).getOperand(0) == N1)
Bill Wendlingb0702e02009-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 Johannesenf9cbc1f2008-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 Wendlingb0702e02009-01-30 02:42:10 +00001233 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT,
1234 N0.getOperand(0), N0.getOperand(1).getOperand(0));
Dale Johannesen58e39b02008-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 Wendlingb0702e02009-01-30 02:42:10 +00001239 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1240 N0.getOperand(0), N0.getOperand(1).getOperand(0));
Evan Chengb13cdbd2007-06-21 07:39:16 +00001241 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
Gabor Greifba36cb52008-08-28 21:40:38 +00001242 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
Duncan Sands25cf2272008-11-24 14:53:14 +00001243 SDValue Result = combineSelectAndUse(N, N1, N0, DAG, TLI, LegalOperations);
Gabor Greifba36cb52008-08-28 21:40:38 +00001244 if (Result.getNode()) return Result;
Evan Chengb13cdbd2007-06-21 07:39:16 +00001245 }
Bill Wendlingb0702e02009-01-30 02:42:10 +00001246
Dan Gohman613e0d82007-07-03 14:03:57 +00001247 // If either operand of a sub is undef, the result is undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00001248 if (N0.getOpcode() == ISD::UNDEF)
1249 return N0;
1250 if (N1.getOpcode() == ISD::UNDEF)
1251 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001252
Dan Gohman6520e202008-10-18 02:06:02 +00001253 // If the relocation model supports it, consider symbol offsets.
1254 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0))
Duncan Sands25cf2272008-11-24 14:53:14 +00001255 if (!LegalOperations && TLI.isOffsetFoldingLegal(GA)) {
Dan Gohman6520e202008-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 Gohman475871a2008-07-27 21:46:04 +00001268 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001269}
1270
Dan Gohman475871a2008-07-27 21:46:04 +00001271SDValue DAGCombiner::visitMUL(SDNode *N) {
1272 SDValue N0 = N->getOperand(0);
1273 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001274 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1275 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001276 MVT VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001277
Dan Gohman7f321562007-06-25 16:23:39 +00001278 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001279 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001280 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001281 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001282 }
Dan Gohman7f321562007-06-25 16:23:39 +00001283
Dan Gohman613e0d82007-07-03 14:03:57 +00001284 // fold (mul x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001285 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001286 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001287 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001288 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001289 return DAG.FoldConstantArithmetic(ISD::MUL, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00001290 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001291 if (N0C && !N1C)
Bill Wendling9c8148a2009-01-30 02:45:56 +00001292 return DAG.getNode(ISD::MUL, N->getDebugLoc(), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001293 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001294 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001295 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001296 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +00001297 if (N1C && N1C->isAllOnesValue())
Bill Wendling9c8148a2009-01-30 02:45:56 +00001298 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1299 DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001300 // fold (mul x, (1 << c)) -> x << c
Dan Gohman002e5d02008-03-13 22:13:53 +00001301 if (N1C && N1C->getAPIntValue().isPowerOf2())
Bill Wendling9c8148a2009-01-30 02:45:56 +00001302 return DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001303 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001304 TLI.getShiftAmountTy()));
Chris Lattner3e6099b2005-10-30 06:41:49 +00001305 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
Bill Wendling9c8148a2009-01-30 02:45:56 +00001306 if (N1C && isPowerOf2_64(-N1C->getSExtValue()))
Chris Lattner3e6099b2005-10-30 06:41:49 +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 Wendling9c8148a2009-01-30 02:45:56 +00001309 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1310 DAG.getConstant(0, VT),
Bill Wendling73e16b22009-01-30 02:49:26 +00001311 DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, N0,
Dan Gohman7810bfe2008-09-26 21:54:37 +00001312 DAG.getConstant(Log2_64(-N1C->getSExtValue()),
Chris Lattner3e6099b2005-10-30 06:41:49 +00001313 TLI.getShiftAmountTy())));
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001314 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
Bill Wendling73e16b22009-01-30 02:49:26 +00001315 if (N1C && N0.getOpcode() == ISD::SHL &&
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001316 isa<ConstantSDNode>(N0.getOperand(1))) {
Bill Wendling9c8148a2009-01-30 02:45:56 +00001317 SDValue C3 = DAG.getNode(ISD::SHL, N->getDebugLoc(), VT,
1318 N1, N0.getOperand(1));
Gabor Greifba36cb52008-08-28 21:40:38 +00001319 AddToWorkList(C3.getNode());
Bill Wendling9c8148a2009-01-30 02:45:56 +00001320 return DAG.getNode(ISD::MUL, N->getDebugLoc(), VT,
1321 N0.getOperand(0), C3);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001322 }
1323
1324 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
1325 // use.
1326 {
Dan Gohman475871a2008-07-27 21:46:04 +00001327 SDValue Sh(0,0), Y(0,0);
Chris Lattner0b1a85f2006-03-01 03:44:24 +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 Greifba36cb52008-08-28 21:40:38 +00001330 N0.getNode()->hasOneUse()) {
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001331 Sh = N0; Y = N1;
1332 } else if (N1.getOpcode() == ISD::SHL &&
Gabor Greif12632d22008-08-30 19:29:20 +00001333 isa<ConstantSDNode>(N1.getOperand(1)) &&
1334 N1.getNode()->hasOneUse()) {
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001335 Sh = N1; Y = N0;
1336 }
Bill Wendling73e16b22009-01-30 02:49:26 +00001337
Gabor Greifba36cb52008-08-28 21:40:38 +00001338 if (Sh.getNode()) {
Bill Wendling9c8148a2009-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));
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001343 }
1344 }
Bill Wendling73e16b22009-01-30 02:49:26 +00001345
Chris Lattnera1deca32006-03-04 23:33:26 +00001346 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
Gabor Greifba36cb52008-08-28 21:40:38 +00001347 if (N1C && N0.getOpcode() == ISD::ADD && N0.getNode()->hasOneUse() &&
Bill Wendling9c8148a2009-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));
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001354
Nate Begemancd4d58c2006-02-03 06:46:56 +00001355 // reassociate mul
Bill Wendling35247c32009-01-30 00:45:56 +00001356 SDValue RMUL = ReassociateOps(ISD::MUL, N->getDebugLoc(), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001357 if (RMUL.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001358 return RMUL;
Dan Gohman7f321562007-06-25 16:23:39 +00001359
Dan Gohman475871a2008-07-27 21:46:04 +00001360 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001361}
1362
Dan Gohman475871a2008-07-27 21:46:04 +00001363SDValue DAGCombiner::visitSDIV(SDNode *N) {
1364 SDValue N0 = N->getOperand(0);
1365 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001366 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1367 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Duncan Sands83ec4b62008-06-06 12:08:01 +00001368 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001369
Dan Gohman7f321562007-06-25 16:23:39 +00001370 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001371 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001372 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001373 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001374 }
Dan Gohman7f321562007-06-25 16:23:39 +00001375
Nate Begeman1d4d4142005-09-01 00:19:25 +00001376 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001377 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001378 return DAG.FoldConstantArithmetic(ISD::SDIV, VT, N0C, N1C);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001379 // fold (sdiv X, 1) -> X
Dan Gohman7810bfe2008-09-26 21:54:37 +00001380 if (N1C && N1C->getSExtValue() == 1LL)
Nate Begeman405e3ec2005-10-21 00:02:42 +00001381 return N0;
1382 // fold (sdiv X, -1) -> 0-X
1383 if (N1C && N1C->isAllOnesValue())
Bill Wendling944d34b2009-01-30 02:52:17 +00001384 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1385 DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10: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 Sands83ec4b62008-06-06 12:08:01 +00001388 if (!VT.isVector()) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001389 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Bill Wendling944d34b2009-01-30 02:52:17 +00001390 return DAG.getNode(ISD::UDIV, N->getDebugLoc(), N1.getValueType(),
1391 N0, N1);
Chris Lattnerf32aac32008-01-27 23:32:17 +00001392 }
Nate Begemancd6a6ed2006-02-17 07:26:20 +00001393 // fold (sdiv X, pow2) -> simple ops after legalize
Dan Gohman002e5d02008-03-13 22:13:53 +00001394 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap() &&
Dan Gohman7810bfe2008-09-26 21:54:37 +00001395 (isPowerOf2_64(N1C->getSExtValue()) ||
1396 isPowerOf2_64(-N1C->getSExtValue()))) {
Nate Begeman405e3ec2005-10-21 00:02:42 +00001397 // If dividing by powers of two is cheap, then don't perform the following
1398 // fold.
1399 if (TLI.isPow2DivCheap())
Dan Gohman475871a2008-07-27 21:46:04 +00001400 return SDValue();
Bill Wendling944d34b2009-01-30 02:52:17 +00001401
Dan Gohman7810bfe2008-09-26 21:54:37 +00001402 int64_t pow2 = N1C->getSExtValue();
Nate Begeman405e3ec2005-10-21 00:02:42 +00001403 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
Chris Lattner8f4880b2006-02-16 08:02:36 +00001404 unsigned lg2 = Log2_64(abs2);
Bill Wendling944d34b2009-01-30 02:52:17 +00001405
Chris Lattner8f4880b2006-02-16 08:02:36 +00001406 // Splat the sign bit into the register
Bill Wendling944d34b2009-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 Greifba36cb52008-08-28 21:40:38 +00001410 AddToWorkList(SGN.getNode());
Bill Wendling944d34b2009-01-30 02:52:17 +00001411
Chris Lattner8f4880b2006-02-16 08:02:36 +00001412 // Add (N0 < 0) ? abs2 - 1 : 0;
Bill Wendling944d34b2009-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 Greifba36cb52008-08-28 21:40:38 +00001417 AddToWorkList(SRL.getNode());
1418 AddToWorkList(ADD.getNode()); // Divide by pow2
Bill Wendling944d34b2009-01-30 02:52:17 +00001419 SDValue SRA = DAG.getNode(ISD::SRA, N->getDebugLoc(), VT, ADD,
1420 DAG.getConstant(lg2, TLI.getShiftAmountTy()));
1421
Nate Begeman405e3ec2005-10-21 00:02:42 +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 Wendling944d34b2009-01-30 02:52:17 +00001426
Gabor Greifba36cb52008-08-28 21:40:38 +00001427 AddToWorkList(SRA.getNode());
Bill Wendling944d34b2009-01-30 02:52:17 +00001428 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1429 DAG.getConstant(0, VT), SRA);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001430 }
Bill Wendling944d34b2009-01-30 02:52:17 +00001431
Nate Begeman69575232005-10-20 02:15:44 +00001432 // if integer divide is expensive and we satisfy the requirements, emit an
1433 // alternate sequence.
Dan Gohman7810bfe2008-09-26 21:54:37 +00001434 if (N1C && (N1C->getSExtValue() < -1 || N1C->getSExtValue() > 1) &&
Chris Lattnere9936d12005-10-22 18:50:15 +00001435 !TLI.isIntDivCheap()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001436 SDValue Op = BuildSDIV(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001437 if (Op.getNode()) return Op;
Nate Begeman69575232005-10-20 02:15:44 +00001438 }
Dan Gohman7f321562007-06-25 16:23:39 +00001439
Dan Gohman613e0d82007-07-03 14:03:57 +00001440 // 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;
Dan Gohman7f321562007-06-25 16:23:39 +00001446
Dan Gohman475871a2008-07-27 21:46:04 +00001447 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001448}
1449
Dan Gohman475871a2008-07-27 21:46:04 +00001450SDValue DAGCombiner::visitUDIV(SDNode *N) {
1451 SDValue N0 = N->getOperand(0);
1452 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001453 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1454 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Duncan Sands83ec4b62008-06-06 12:08:01 +00001455 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001456
Dan Gohman7f321562007-06-25 16:23:39 +00001457 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001458 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001459 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001460 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001461 }
Dan Gohman7f321562007-06-25 16:23:39 +00001462
Nate Begeman1d4d4142005-09-01 00:19:25 +00001463 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001464 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001465 return DAG.FoldConstantArithmetic(ISD::UDIV, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001466 // fold (udiv x, (1 << c)) -> x >>u c
Dan Gohman002e5d02008-03-13 22:13:53 +00001467 if (N1C && N1C->getAPIntValue().isPowerOf2())
Bill Wendling07d85142009-01-30 02:55:25 +00001468 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001469 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001470 TLI.getShiftAmountTy()));
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001471 // 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 Gohman002e5d02008-03-13 22:13:53 +00001474 if (SHC->getAPIntValue().isPowerOf2()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001475 MVT ADDVT = N1.getOperand(1).getValueType();
Bill Wendling07d85142009-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 Greifba36cb52008-08-28 21:40:38 +00001481 AddToWorkList(Add.getNode());
Bill Wendling07d85142009-01-30 02:55:25 +00001482 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001483 }
1484 }
1485 }
Nate Begeman69575232005-10-20 02:15:44 +00001486 // fold (udiv x, c) -> alternate
Dan Gohman002e5d02008-03-13 22:13:53 +00001487 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001488 SDValue Op = BuildUDIV(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001489 if (Op.getNode()) return Op;
Chris Lattnere9936d12005-10-22 18:50:15 +00001490 }
Dan Gohman7f321562007-06-25 16:23:39 +00001491
Dan Gohman613e0d82007-07-03 14:03:57 +00001492 // 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;
Dan Gohman7f321562007-06-25 16:23:39 +00001498
Dan Gohman475871a2008-07-27 21:46:04 +00001499 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001500}
1501
Dan Gohman475871a2008-07-27 21:46:04 +00001502SDValue DAGCombiner::visitSREM(SDNode *N) {
1503 SDValue N0 = N->getOperand(0);
1504 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001505 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1506 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001507 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001508
1509 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001510 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001511 return DAG.FoldConstantArithmetic(ISD::SREM, VT, N0C, N1C);
Nate Begeman07ed4172005-10-10 21:26:48 +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 Sands83ec4b62008-06-06 12:08:01 +00001514 if (!VT.isVector()) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001515 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00001516 return DAG.getNode(ISD::UREM, N->getDebugLoc(), VT, N0, N1);
Chris Lattneree339f42008-01-27 23:21:58 +00001517 }
Chris Lattner26d29902006-10-12 20:58:32 +00001518
Dan Gohman77003042007-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.
Chris Lattner26d29902006-10-12 20:58:32 +00001521 if (N1C && !N1C->isNullValue()) {
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00001522 SDValue Div = DAG.getNode(ISD::SDIV, N->getDebugLoc(), VT, N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001523 AddToWorkList(Div.getNode());
1524 SDValue OptimizedDiv = combine(Div.getNode());
1525 if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) {
Bill Wendling6d3bf8c2009-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 Greifba36cb52008-08-28 21:40:38 +00001529 AddToWorkList(Mul.getNode());
Dan Gohman77003042007-11-26 23:46:11 +00001530 return Sub;
1531 }
Chris Lattner26d29902006-10-12 20:58:32 +00001532 }
1533
Dan Gohman613e0d82007-07-03 14:03:57 +00001534 // 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;
Dan Gohman7f321562007-06-25 16:23:39 +00001540
Dan Gohman475871a2008-07-27 21:46:04 +00001541 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001542}
1543
Dan Gohman475871a2008-07-27 21:46:04 +00001544SDValue DAGCombiner::visitUREM(SDNode *N) {
1545 SDValue N0 = N->getOperand(0);
1546 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001547 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1548 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001549 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001550
1551 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001552 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001553 return DAG.FoldConstantArithmetic(ISD::UREM, VT, N0C, N1C);
Nate Begeman07ed4172005-10-10 21:26:48 +00001554 // fold (urem x, pow2) -> (and x, pow2-1)
Dan Gohman002e5d02008-03-13 22:13:53 +00001555 if (N1C && !N1C->isNullValue() && N1C->getAPIntValue().isPowerOf2())
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00001556 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001557 DAG.getConstant(N1C->getAPIntValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +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 Gohman002e5d02008-03-13 22:13:53 +00001561 if (SHC->getAPIntValue().isPowerOf2()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001562 SDValue Add =
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00001563 DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001564 DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()),
Dan Gohman002e5d02008-03-13 22:13:53 +00001565 VT));
Gabor Greifba36cb52008-08-28 21:40:38 +00001566 AddToWorkList(Add.getNode());
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00001567 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0, Add);
Nate Begemanc031e332006-02-05 07:36:48 +00001568 }
1569 }
1570 }
Chris Lattner26d29902006-10-12 20:58:32 +00001571
Dan Gohman77003042007-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.
Chris Lattner26d29902006-10-12 20:58:32 +00001574 if (N1C && !N1C->isNullValue()) {
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00001575 SDValue Div = DAG.getNode(ISD::UDIV, N->getDebugLoc(), VT, N0, N1);
Dan Gohman942ca7f2008-09-08 16:59:01 +00001576 AddToWorkList(Div.getNode());
Gabor Greifba36cb52008-08-28 21:40:38 +00001577 SDValue OptimizedDiv = combine(Div.getNode());
1578 if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) {
Bill Wendling6d3bf8c2009-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 Greifba36cb52008-08-28 21:40:38 +00001582 AddToWorkList(Mul.getNode());
Dan Gohman77003042007-11-26 23:46:11 +00001583 return Sub;
1584 }
Chris Lattner26d29902006-10-12 20:58:32 +00001585 }
1586
Dan Gohman613e0d82007-07-03 14:03:57 +00001587 // 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;
Dan Gohman7f321562007-06-25 16:23:39 +00001593
Dan Gohman475871a2008-07-27 21:46:04 +00001594 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001595}
1596
Dan Gohman475871a2008-07-27 21:46:04 +00001597SDValue DAGCombiner::visitMULHS(SDNode *N) {
1598 SDValue N0 = N->getOperand(0);
1599 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001600 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001601 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001602
1603 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001604 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001605 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001606 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Dan Gohman002e5d02008-03-13 22:13:53 +00001607 if (N1C && N1C->getAPIntValue() == 1)
Bill Wendling326411d2009-01-30 03:00:18 +00001608 return DAG.getNode(ISD::SRA, N->getDebugLoc(), N0.getValueType(), N0,
1609 DAG.getConstant(N0.getValueType().getSizeInBits() - 1,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001610 TLI.getShiftAmountTy()));
Dan Gohman613e0d82007-07-03 14:03:57 +00001611 // fold (mulhs x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001612 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001613 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00001614
Dan Gohman475871a2008-07-27 21:46:04 +00001615 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001616}
1617
Dan Gohman475871a2008-07-27 21:46:04 +00001618SDValue DAGCombiner::visitMULHU(SDNode *N) {
1619 SDValue N0 = N->getOperand(0);
1620 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001621 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001622 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001623
1624 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001625 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001626 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001627 // fold (mulhu x, 1) -> 0
Dan Gohman002e5d02008-03-13 22:13:53 +00001628 if (N1C && N1C->getAPIntValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001629 return DAG.getConstant(0, N0.getValueType());
Dan Gohman613e0d82007-07-03 14:03:57 +00001630 // fold (mulhu x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001631 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001632 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00001633
Dan Gohman475871a2008-07-27 21:46:04 +00001634 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001635}
1636
Dan Gohman389079b2007-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 Gohman475871a2008-07-27 21:46:04 +00001641SDValue DAGCombiner::SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
1642 unsigned HiOp) {
Dan Gohman389079b2007-10-08 17:57:15 +00001643 // If the high half is not needed, just compute the low half.
Evan Cheng44711942007-11-08 09:25:29 +00001644 bool HiExists = N->hasAnyUseOfValue(1);
1645 if (!HiExists &&
Duncan Sands25cf2272008-11-24 14:53:14 +00001646 (!LegalOperations ||
Dan Gohman389079b2007-10-08 17:57:15 +00001647 TLI.isOperationLegal(LoOp, N->getValueType(0)))) {
Bill Wendling826d1142009-01-30 03:08:40 +00001648 SDValue Res = DAG.getNode(LoOp, N->getDebugLoc(), N->getValueType(0),
1649 N->op_begin(), N->getNumOperands());
Chris Lattner5eee4272008-01-26 01:09:19 +00001650 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00001651 }
1652
1653 // If the low half is not needed, just compute the high half.
Evan Cheng44711942007-11-08 09:25:29 +00001654 bool LoExists = N->hasAnyUseOfValue(0);
1655 if (!LoExists &&
Duncan Sands25cf2272008-11-24 14:53:14 +00001656 (!LegalOperations ||
Dan Gohman389079b2007-10-08 17:57:15 +00001657 TLI.isOperationLegal(HiOp, N->getValueType(1)))) {
Bill Wendling826d1142009-01-30 03:08:40 +00001658 SDValue Res = DAG.getNode(HiOp, N->getDebugLoc(), N->getValueType(1),
1659 N->op_begin(), N->getNumOperands());
Chris Lattner5eee4272008-01-26 01:09:19 +00001660 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00001661 }
1662
Evan Cheng44711942007-11-08 09:25:29 +00001663 // If both halves are used, return as it is.
1664 if (LoExists && HiExists)
Dan Gohman475871a2008-07-27 21:46:04 +00001665 return SDValue();
Evan Cheng44711942007-11-08 09:25:29 +00001666
1667 // If the two computed results can be simplified separately, separate them.
Evan Cheng44711942007-11-08 09:25:29 +00001668 if (LoExists) {
Bill Wendling826d1142009-01-30 03:08:40 +00001669 SDValue Lo = DAG.getNode(LoOp, N->getDebugLoc(), N->getValueType(0),
1670 N->op_begin(), N->getNumOperands());
Gabor Greifba36cb52008-08-28 21:40:38 +00001671 AddToWorkList(Lo.getNode());
1672 SDValue LoOpt = combine(Lo.getNode());
1673 if (LoOpt.getNode() && LoOpt.getNode() != Lo.getNode() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00001674 (!LegalOperations ||
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001675 TLI.isOperationLegal(LoOpt.getOpcode(), LoOpt.getValueType())))
Chris Lattner5eee4272008-01-26 01:09:19 +00001676 return CombineTo(N, LoOpt, LoOpt);
Dan Gohman389079b2007-10-08 17:57:15 +00001677 }
1678
Evan Cheng44711942007-11-08 09:25:29 +00001679 if (HiExists) {
Bill Wendling826d1142009-01-30 03:08:40 +00001680 SDValue Hi = DAG.getNode(HiOp, N->getDebugLoc(), N->getValueType(1),
Duncan Sands25cf2272008-11-24 14:53:14 +00001681 N->op_begin(), N->getNumOperands());
Gabor Greifba36cb52008-08-28 21:40:38 +00001682 AddToWorkList(Hi.getNode());
1683 SDValue HiOpt = combine(Hi.getNode());
1684 if (HiOpt.getNode() && HiOpt != Hi &&
Duncan Sands25cf2272008-11-24 14:53:14 +00001685 (!LegalOperations ||
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001686 TLI.isOperationLegal(HiOpt.getOpcode(), HiOpt.getValueType())))
Chris Lattner5eee4272008-01-26 01:09:19 +00001687 return CombineTo(N, HiOpt, HiOpt);
Evan Cheng44711942007-11-08 09:25:29 +00001688 }
Bill Wendling826d1142009-01-30 03:08:40 +00001689
Dan Gohman475871a2008-07-27 21:46:04 +00001690 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001691}
1692
Dan Gohman475871a2008-07-27 21:46:04 +00001693SDValue DAGCombiner::visitSMUL_LOHI(SDNode *N) {
1694 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHS);
Gabor Greifba36cb52008-08-28 21:40:38 +00001695 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001696
Dan Gohman475871a2008-07-27 21:46:04 +00001697 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001698}
1699
Dan Gohman475871a2008-07-27 21:46:04 +00001700SDValue DAGCombiner::visitUMUL_LOHI(SDNode *N) {
1701 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHU);
Gabor Greifba36cb52008-08-28 21:40:38 +00001702 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001703
Dan Gohman475871a2008-07-27 21:46:04 +00001704 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001705}
1706
Dan Gohman475871a2008-07-27 21:46:04 +00001707SDValue DAGCombiner::visitSDIVREM(SDNode *N) {
1708 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::SDIV, ISD::SREM);
Gabor Greifba36cb52008-08-28 21:40:38 +00001709 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001710
Dan Gohman475871a2008-07-27 21:46:04 +00001711 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001712}
1713
Dan Gohman475871a2008-07-27 21:46:04 +00001714SDValue DAGCombiner::visitUDIVREM(SDNode *N) {
1715 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::UDIV, ISD::UREM);
Gabor Greifba36cb52008-08-28 21:40:38 +00001716 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001717
Dan Gohman475871a2008-07-27 21:46:04 +00001718 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001719}
1720
Chris Lattner35e5c142006-05-05 05:51:50 +00001721/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
1722/// two operands of the same opcode, try to simplify it.
Dan Gohman475871a2008-07-27 21:46:04 +00001723SDValue DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
1724 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001725 MVT VT = N0.getValueType();
Chris Lattner35e5c142006-05-05 05:51:50 +00001726 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
1727
Chris Lattner540121f2006-05-05 06:31:05 +00001728 // 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))
Chris Lattner0d8dae72006-05-05 06:32:04 +00001732 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y))
Chris Lattner540121f2006-05-05 06:31:05 +00001733 if ((N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND||
Chris Lattner0d8dae72006-05-05 06:32:04 +00001734 N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::TRUNCATE) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001735 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
Bill Wendlingb74c8672009-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 Greifba36cb52008-08-28 21:40:38 +00001739 AddToWorkList(ORNode.getNode());
Bill Wendlingb74c8672009-01-30 19:25:47 +00001740 return DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT, ORNode);
Chris Lattner35e5c142006-05-05 05:51:50 +00001741 }
1742
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001743 // 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)
Chris Lattner35e5c142006-05-05 05:51:50 +00001747 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001748 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001749 N0.getOperand(1) == N1.getOperand(1)) {
Bill Wendlingb74c8672009-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 Greifba36cb52008-08-28 21:40:38 +00001753 AddToWorkList(ORNode.getNode());
Bill Wendlingb74c8672009-01-30 19:25:47 +00001754 return DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT,
1755 ORNode, N0.getOperand(1));
Chris Lattner35e5c142006-05-05 05:51:50 +00001756 }
1757
Dan Gohman475871a2008-07-27 21:46:04 +00001758 return SDValue();
Chris Lattner35e5c142006-05-05 05:51:50 +00001759}
1760
Dan Gohman475871a2008-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;
Nate Begeman646d7e22005-09-02 21:18:40 +00001765 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1766 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001767 MVT VT = N1.getValueType();
1768 unsigned BitWidth = VT.getSizeInBits();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001769
Dan Gohman7f321562007-06-25 16:23:39 +00001770 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001771 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001772 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001773 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001774 }
Dan Gohman7f321562007-06-25 16:23:39 +00001775
Dan Gohman613e0d82007-07-03 14:03:57 +00001776 // fold (and x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001777 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001778 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001779 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001780 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001781 return DAG.FoldConstantArithmetic(ISD::AND, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00001782 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001783 if (N0C && !N1C)
1784 return DAG.getNode(ISD::AND, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001785 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001786 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001787 return N0;
1788 // if (and x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00001789 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001790 APInt::getAllOnesValue(BitWidth)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001791 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +00001792 // reassociate and
Bill Wendling35247c32009-01-30 00:45:56 +00001793 SDValue RAND = ReassociateOps(ISD::AND, N->getDebugLoc(), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001794 if (RAND.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001795 return RAND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001796 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
Nate Begeman5dc7e862005-11-02 18:42:59 +00001797 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001798 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohman002e5d02008-03-13 22:13:53 +00001799 if ((ORI->getAPIntValue() & N1C->getAPIntValue()) == N1C->getAPIntValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001800 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +00001801 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
1802 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Dan Gohman475871a2008-07-27 21:46:04 +00001803 SDValue N0Op0 = N0.getOperand(0);
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001804 APInt Mask = ~N1C->getAPIntValue();
1805 Mask.trunc(N0Op0.getValueSizeInBits());
1806 if (DAG.MaskedValueIsZero(N0Op0, Mask)) {
Bill Wendling2627a882009-01-30 20:43:18 +00001807 SDValue Zext = DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(),
1808 N0.getValueType(), N0Op0);
Chris Lattner1ec05d12006-03-01 21:47:21 +00001809
1810 // Replace uses of the AND with uses of the Zero extend node.
1811 CombineTo(N, Zext);
1812
Chris Lattner3603cd62006-02-02 07:17:31 +00001813 // 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 Greifba36cb52008-08-28 21:40:38 +00001816 CombineTo(N0.getNode(), Zext);
Dan Gohman475871a2008-07-27 21:46:04 +00001817 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner3603cd62006-02-02 07:17:31 +00001818 }
1819 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001820 // 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 Sands83ec4b62008-06-06 12:08:01 +00001826 LL.getValueType().isInteger()) {
Bill Wendling2627a882009-01-30 20:43:18 +00001827 // fold (and (seteq X, 0), (seteq Y, 0)) -> (seteq (or X, Y), 0)
Dan Gohman002e5d02008-03-13 22:13:53 +00001828 if (cast<ConstantSDNode>(LR)->isNullValue() && Op1 == ISD::SETEQ) {
Bill Wendling2627a882009-01-30 20:43:18 +00001829 SDValue ORNode = DAG.getNode(ISD::OR, N0.getDebugLoc(),
1830 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001831 AddToWorkList(ORNode.getNode());
Bill Wendling2627a882009-01-30 20:43:18 +00001832 return DAG.getSetCC(N->getDebugLoc(), VT, ORNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001833 }
Bill Wendling2627a882009-01-30 20:43:18 +00001834 // fold (and (seteq X, -1), (seteq Y, -1)) -> (seteq (and X, Y), -1)
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001835 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
Bill Wendling2627a882009-01-30 20:43:18 +00001836 SDValue ANDNode = DAG.getNode(ISD::AND, N0.getDebugLoc(),
1837 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001838 AddToWorkList(ANDNode.getNode());
Bill Wendling2627a882009-01-30 20:43:18 +00001839 return DAG.getSetCC(N->getDebugLoc(), VT, ANDNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001840 }
Bill Wendling2627a882009-01-30 20:43:18 +00001841 // fold (and (setgt X, -1), (setgt Y, -1)) -> (setgt (or X, Y), -1)
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001842 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
Bill Wendling2627a882009-01-30 20:43:18 +00001843 SDValue ORNode = DAG.getNode(ISD::OR, N0.getDebugLoc(),
1844 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001845 AddToWorkList(ORNode.getNode());
Bill Wendling2627a882009-01-30 20:43:18 +00001846 return DAG.getSetCC(N->getDebugLoc(), VT, ORNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +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 Sands83ec4b62008-06-06 12:08:01 +00001855 bool isInteger = LL.getValueType().isInteger();
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001856 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
Chris Lattner6e1c6232008-10-28 07:11:07 +00001857 if (Result != ISD::SETCC_INVALID &&
Duncan Sands25cf2272008-11-24 14:53:14 +00001858 (!LegalOperations || TLI.isCondCodeLegal(Result, LL.getValueType())))
Bill Wendling2627a882009-01-30 20:43:18 +00001859 return DAG.getSetCC(N->getDebugLoc(), N0.getValueType(),
1860 LL, LR, Result);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001861 }
1862 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001863
Bill Wendling2627a882009-01-30 20:43:18 +00001864 // Simplify: (and (op x...), (op y...)) -> (op (and x, y))
Chris Lattner35e5c142006-05-05 05:51:50 +00001865 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001866 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001867 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001868 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001869
Nate Begemande996292006-02-03 22:24:05 +00001870 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
1871 // fold (and (sra)) -> (and (srl)) when possible.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001872 if (!VT.isVector() &&
Dan Gohman475871a2008-07-27 21:46:04 +00001873 SimplifyDemandedBits(SDValue(N, 0)))
1874 return SDValue(N, 0);
Nate Begemanded49632005-10-13 03:11:28 +00001875 // fold (zext_inreg (extload x)) -> (zextload x)
Gabor Greifba36cb52008-08-28 21:40:38 +00001876 if (ISD::isEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode())) {
Evan Cheng466685d2006-10-09 20:57:25 +00001877 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001878 MVT EVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +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 Gohman2e68b6f2008-02-25 21:11:39 +00001881 unsigned BitWidth = N1.getValueSizeInBits();
1882 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001883 BitWidth - EVT.getSizeInBits())) &&
Duncan Sands25cf2272008-11-24 14:53:14 +00001884 ((!LegalOperations && !LN0->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00001885 TLI.isLoadExtLegal(ISD::ZEXTLOAD, EVT))) {
Bill Wendling2627a882009-01-30 20:43:18 +00001886 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, N0.getDebugLoc(), VT,
1887 LN0->getChain(), LN0->getBasePtr(),
1888 LN0->getSrcValue(),
Duncan Sands25cf2272008-11-24 14:53:14 +00001889 LN0->getSrcValueOffset(), EVT,
1890 LN0->isVolatile(), LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00001891 AddToWorkList(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001892 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00001893 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001894 }
1895 }
1896 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Gabor Greifba36cb52008-08-28 21:40:38 +00001897 if (ISD::isSEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng83060c52007-03-07 08:07:03 +00001898 N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001899 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001900 MVT EVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +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 Gohman2e68b6f2008-02-25 21:11:39 +00001903 unsigned BitWidth = N1.getValueSizeInBits();
1904 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001905 BitWidth - EVT.getSizeInBits())) &&
Duncan Sands25cf2272008-11-24 14:53:14 +00001906 ((!LegalOperations && !LN0->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00001907 TLI.isLoadExtLegal(ISD::ZEXTLOAD, EVT))) {
Bill Wendling2627a882009-01-30 20:43:18 +00001908 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, N0.getDebugLoc(), VT,
1909 LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00001910 LN0->getBasePtr(), LN0->getSrcValue(),
1911 LN0->getSrcValueOffset(), EVT,
1912 LN0->isVolatile(), LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00001913 AddToWorkList(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001914 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00001915 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001916 }
1917 }
Chris Lattner15045b62006-02-28 06:35:35 +00001918
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001919 // fold (and (load x), 255) -> (zextload x, i8)
1920 // fold (and (extload x, i16), 255) -> (zextload x, i8)
Evan Cheng466685d2006-10-09 20:57:25 +00001921 if (N1C && N0.getOpcode() == ISD::LOAD) {
1922 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
1923 if (LN0->getExtensionType() != ISD::SEXTLOAD &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001924 LN0->isUnindexed() && N0.hasOneUse() &&
1925 // Do not change the width of a volatile load.
1926 !LN0->isVolatile()) {
Duncan Sands8eab8a22008-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 Wendling2627a882009-01-30 20:43:18 +00001933
Duncan Sandsad205a72008-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 Sands25cf2272008-11-24 14:53:14 +00001937 (!LegalOperations || TLI.isLoadExtLegal(ISD::ZEXTLOAD, EVT))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001938 MVT PtrType = N0.getOperand(1).getValueType();
Bill Wendling2627a882009-01-30 20:43:18 +00001939
Evan Cheng466685d2006-10-09 20:57:25 +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 Sands83ec4b62008-06-06 12:08:01 +00001943 unsigned LVTStoreBytes = LoadedVT.getStoreSizeInBits()/8;
1944 unsigned EVTStoreBytes = EVT.getStoreSizeInBits()/8;
Duncan Sandsc6fa1702007-11-09 08:57:19 +00001945 unsigned PtrOff = LVTStoreBytes - EVTStoreBytes;
Duncan Sandsdc846502007-10-28 12:59:45 +00001946 unsigned Alignment = LN0->getAlignment();
Dan Gohman475871a2008-07-27 21:46:04 +00001947 SDValue NewPtr = LN0->getBasePtr();
Bill Wendling2627a882009-01-30 20:43:18 +00001948
Duncan Sands0753fc12008-02-11 10:37:04 +00001949 if (TLI.isBigEndian()) {
Bill Wendling2627a882009-01-30 20:43:18 +00001950 NewPtr = DAG.getNode(ISD::ADD, DebugLoc::getUnknownLoc(), PtrType,
1951 NewPtr, DAG.getConstant(PtrOff, PtrType));
Duncan Sandsdc846502007-10-28 12:59:45 +00001952 Alignment = MinAlign(Alignment, PtrOff);
1953 }
Bill Wendling2627a882009-01-30 20:43:18 +00001954
Gabor Greifba36cb52008-08-28 21:40:38 +00001955 AddToWorkList(NewPtr.getNode());
Dan Gohman475871a2008-07-27 21:46:04 +00001956 SDValue Load =
Bill Wendling2627a882009-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);
Evan Cheng466685d2006-10-09 20:57:25 +00001960 AddToWorkList(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001961 CombineTo(N0.getNode(), Load, Load.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00001962 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng466685d2006-10-09 20:57:25 +00001963 }
Chris Lattner15045b62006-02-28 06:35:35 +00001964 }
1965 }
1966
Dan Gohman475871a2008-07-27 21:46:04 +00001967 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001968}
1969
Dan Gohman475871a2008-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;
Nate Begeman646d7e22005-09-02 21:18:40 +00001974 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1975 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001976 MVT VT = N1.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001977
Dan Gohman7f321562007-06-25 16:23:39 +00001978 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001979 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001980 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001981 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001982 }
Dan Gohman7f321562007-06-25 16:23:39 +00001983
Dan Gohman613e0d82007-07-03 14:03:57 +00001984 // fold (or x, undef) -> -1
Dan Gohmand595b5f2007-07-10 14:20:37 +00001985 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Chris Lattnere094f542007-07-09 16:16:34 +00001986 return DAG.getConstant(~0ULL, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001987 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001988 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001989 return DAG.FoldConstantArithmetic(ISD::OR, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00001990 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001991 if (N0C && !N1C)
Bill Wendling09025642009-01-30 20:59:34 +00001992 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001993 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001994 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001995 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001996 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001997 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001998 return N1;
1999 // fold (or x, c) -> c iff (x & ~c) == 0
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002000 if (N1C && DAG.MaskedValueIsZero(N0, ~N1C->getAPIntValue()))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002001 return N1;
Nate Begemancd4d58c2006-02-03 06:46:56 +00002002 // reassociate or
Bill Wendling35247c32009-01-30 00:45:56 +00002003 SDValue ROR = ReassociateOps(ISD::OR, N->getDebugLoc(), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00002004 if (ROR.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00002005 return ROR;
2006 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
Gabor Greifba36cb52008-08-28 21:40:38 +00002007 if (N1C && N0.getOpcode() == ISD::AND && N0.getNode()->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00002008 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00002009 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
Bill Wendling09025642009-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));
Nate Begeman223df222005-09-08 20:18:10 +00002014 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002015 // 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 Sands83ec4b62008-06-06 12:08:01 +00002021 LL.getValueType().isInteger()) {
Bill Wendling09025642009-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 Gohman002e5d02008-03-13 22:13:53 +00002024 if (cast<ConstantSDNode>(LR)->isNullValue() &&
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002025 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
Bill Wendling09025642009-01-30 20:59:34 +00002026 SDValue ORNode = DAG.getNode(ISD::OR, LR.getDebugLoc(),
2027 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00002028 AddToWorkList(ORNode.getNode());
Bill Wendling09025642009-01-30 20:59:34 +00002029 return DAG.getSetCC(N->getDebugLoc(), VT, ORNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002030 }
Bill Wendling09025642009-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)
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002033 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
2034 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
Bill Wendling09025642009-01-30 20:59:34 +00002035 SDValue ANDNode = DAG.getNode(ISD::AND, LR.getDebugLoc(),
2036 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00002037 AddToWorkList(ANDNode.getNode());
Bill Wendling09025642009-01-30 20:59:34 +00002038 return DAG.getSetCC(N->getDebugLoc(), VT, ANDNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +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 Sands83ec4b62008-06-06 12:08:01 +00002047 bool isInteger = LL.getValueType().isInteger();
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002048 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
Chris Lattner6e1c6232008-10-28 07:11:07 +00002049 if (Result != ISD::SETCC_INVALID &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002050 (!LegalOperations || TLI.isCondCodeLegal(Result, LL.getValueType())))
Bill Wendling09025642009-01-30 20:59:34 +00002051 return DAG.getSetCC(N->getDebugLoc(), N0.getValueType(),
2052 LL, LR, Result);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002053 }
2054 }
Chris Lattner35e5c142006-05-05 05:51:50 +00002055
Bill Wendling09025642009-01-30 20:59:34 +00002056 // Simplify: (or (op x...), (op y...)) -> (op (or x, y))
Chris Lattner35e5c142006-05-05 05:51:50 +00002057 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002058 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002059 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002060 }
Chris Lattner516b9622006-09-14 20:50:57 +00002061
Bill Wendling09025642009-01-30 20:59:34 +00002062 // (or (and X, C1), (and Y, C2)) -> (and (or X, Y), C3) if possible.
Chris Lattner1ec72732006-09-14 21:11:37 +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 Greifba36cb52008-08-28 21:40:38 +00002068 (N0.getNode()->hasOneUse() || N1.getNode()->hasOneUse())) {
Chris Lattner1ec72732006-09-14 21:11:37 +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 Gohman2e68b6f2008-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();
Chris Lattner1ec72732006-09-14 21:11:37 +00002075
Dan Gohmanea859be2007-06-22 14:59:07 +00002076 if (DAG.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) &&
2077 DAG.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) {
Bill Wendling09025642009-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));
Chris Lattner1ec72732006-09-14 21:11:37 +00002082 }
2083 }
2084
Chris Lattner516b9622006-09-14 20:50:57 +00002085 // See if this is some rotate idiom.
Bill Wendling317bd702009-01-30 21:14:50 +00002086 if (SDNode *Rot = MatchRotate(N0, N1, N->getDebugLoc()))
Dan Gohman475871a2008-07-27 21:46:04 +00002087 return SDValue(Rot, 0);
Chris Lattner35e5c142006-05-05 05:51:50 +00002088
Dan Gohman475871a2008-07-27 21:46:04 +00002089 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002090}
2091
Chris Lattner516b9622006-09-14 20:50:57 +00002092/// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present.
Dan Gohman475871a2008-07-27 21:46:04 +00002093static bool MatchRotateHalf(SDValue Op, SDValue &Shift, SDValue &Mask) {
Chris Lattner516b9622006-09-14 20:50:57 +00002094 if (Op.getOpcode() == ISD::AND) {
Reid Spencer3ed469c2006-11-02 20:25:50 +00002095 if (isa<ConstantSDNode>(Op.getOperand(1))) {
Chris Lattner516b9622006-09-14 20:50:57 +00002096 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 Wendling09025642009-01-30 20:59:34 +00002107
Chris Lattner516b9622006-09-14 20:50:57 +00002108 return false;
2109}
2110
Chris Lattner516b9622006-09-14 20:50:57 +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 Wendling317bd702009-01-30 21:14:50 +00002114SDNode *DAGCombiner::MatchRotate(SDValue LHS, SDValue RHS, DebugLoc DL) {
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002115 // Must be a legal type. Expanded 'n promoted things won't work with rotates.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002116 MVT VT = LHS.getValueType();
Chris Lattner516b9622006-09-14 20:50:57 +00002117 if (!TLI.isTypeLegal(VT)) return 0;
2118
2119 // The target must have at least one rotate flavor.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00002120 bool HasROTL = TLI.isOperationLegalOrCustom(ISD::ROTL, VT);
2121 bool HasROTR = TLI.isOperationLegalOrCustom(ISD::ROTR, VT);
Chris Lattner516b9622006-09-14 20:50:57 +00002122 if (!HasROTL && !HasROTR) return 0;
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002123
Chris Lattner516b9622006-09-14 20:50:57 +00002124 // Match "(X shl/srl V1) & V2" where V2 may not be present.
Dan Gohman475871a2008-07-27 21:46:04 +00002125 SDValue LHSShift; // The shift.
2126 SDValue LHSMask; // AND value if any.
Chris Lattner516b9622006-09-14 20:50:57 +00002127 if (!MatchRotateHalf(LHS, LHSShift, LHSMask))
2128 return 0; // Not part of a rotate.
2129
Dan Gohman475871a2008-07-27 21:46:04 +00002130 SDValue RHSShift; // The shift.
2131 SDValue RHSMask; // AND value if any.
Chris Lattner516b9622006-09-14 20:50:57 +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 Sands83ec4b62008-06-06 12:08:01 +00002148 unsigned OpSizeInBits = VT.getSizeInBits();
Dan Gohman475871a2008-07-27 21:46:04 +00002149 SDValue LHSShiftArg = LHSShift.getOperand(0);
2150 SDValue LHSShiftAmt = LHSShift.getOperand(1);
2151 SDValue RHSShiftAmt = RHSShift.getOperand(1);
Chris Lattner516b9622006-09-14 20:50:57 +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)
Scott Michelc9dc1142007-04-02 21:36:32 +00002155 if (LHSShiftAmt.getOpcode() == ISD::Constant &&
2156 RHSShiftAmt.getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002157 uint64_t LShVal = cast<ConstantSDNode>(LHSShiftAmt)->getZExtValue();
2158 uint64_t RShVal = cast<ConstantSDNode>(RHSShiftAmt)->getZExtValue();
Chris Lattner516b9622006-09-14 20:50:57 +00002159 if ((LShVal + RShVal) != OpSizeInBits)
2160 return 0;
2161
Dan Gohman475871a2008-07-27 21:46:04 +00002162 SDValue Rot;
Chris Lattner516b9622006-09-14 20:50:57 +00002163 if (HasROTL)
Bill Wendling317bd702009-01-30 21:14:50 +00002164 Rot = DAG.getNode(ISD::ROTL, DL, VT, LHSShiftArg, LHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00002165 else
Bill Wendling317bd702009-01-30 21:14:50 +00002166 Rot = DAG.getNode(ISD::ROTR, DL, VT, LHSShiftArg, RHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00002167
2168 // If there is an AND of either shifted operand, apply it to the result.
Gabor Greifba36cb52008-08-28 21:40:38 +00002169 if (LHSMask.getNode() || RHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00002170 APInt Mask = APInt::getAllOnesValue(OpSizeInBits);
Chris Lattner516b9622006-09-14 20:50:57 +00002171
Gabor Greifba36cb52008-08-28 21:40:38 +00002172 if (LHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00002173 APInt RHSBits = APInt::getLowBitsSet(OpSizeInBits, LShVal);
2174 Mask &= cast<ConstantSDNode>(LHSMask)->getAPIntValue() | RHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00002175 }
Gabor Greifba36cb52008-08-28 21:40:38 +00002176 if (RHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00002177 APInt LHSBits = APInt::getHighBitsSet(OpSizeInBits, RShVal);
2178 Mask &= cast<ConstantSDNode>(RHSMask)->getAPIntValue() | LHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00002179 }
2180
Bill Wendling317bd702009-01-30 21:14:50 +00002181 Rot = DAG.getNode(ISD::AND, DL, VT, Rot, DAG.getConstant(Mask, VT));
Chris Lattner516b9622006-09-14 20:50:57 +00002182 }
2183
Gabor Greifba36cb52008-08-28 21:40:38 +00002184 return Rot.getNode();
Chris Lattner516b9622006-09-14 20:50:57 +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 Greifba36cb52008-08-28 21:40:38 +00002189 if (LHSMask.getNode() || RHSMask.getNode())
Chris Lattner516b9622006-09-14 20:50:57 +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))
Scott Michelc9dc1142007-04-02 21:36:32 +00002194 if (RHSShiftAmt.getOpcode() == ISD::SUB &&
2195 LHSShiftAmt == RHSShiftAmt.getOperand(1)) {
Chris Lattner516b9622006-09-14 20:50:57 +00002196 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00002197 dyn_cast<ConstantSDNode>(RHSShiftAmt.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002198 if (SUBC->getAPIntValue() == OpSizeInBits) {
Chris Lattner516b9622006-09-14 20:50:57 +00002199 if (HasROTL)
Bill Wendling317bd702009-01-30 21:14:50 +00002200 return DAG.getNode(ISD::ROTL, DL, VT,
2201 LHSShiftArg, LHSShiftAmt).getNode();
Chris Lattner516b9622006-09-14 20:50:57 +00002202 else
Bill Wendling317bd702009-01-30 21:14:50 +00002203 return DAG.getNode(ISD::ROTR, DL, VT,
2204 LHSShiftArg, RHSShiftAmt).getNode();
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002205 }
Chris Lattner516b9622006-09-14 20:50:57 +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))
Scott Michelc9dc1142007-04-02 21:36:32 +00002211 if (LHSShiftAmt.getOpcode() == ISD::SUB &&
2212 RHSShiftAmt == LHSShiftAmt.getOperand(1)) {
Chris Lattner516b9622006-09-14 20:50:57 +00002213 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00002214 dyn_cast<ConstantSDNode>(LHSShiftAmt.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002215 if (SUBC->getAPIntValue() == OpSizeInBits) {
Bill Wendling2692d592008-08-31 01:13:31 +00002216 if (HasROTR)
Bill Wendling317bd702009-01-30 21:14:50 +00002217 return DAG.getNode(ISD::ROTR, DL, VT,
2218 LHSShiftArg, RHSShiftAmt).getNode();
Bill Wendling2692d592008-08-31 01:13:31 +00002219 else
Bill Wendling317bd702009-01-30 21:14:50 +00002220 return DAG.getNode(ISD::ROTL, DL, VT,
2221 LHSShiftArg, LHSShiftAmt).getNode();
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002222 }
Scott Michelc9dc1142007-04-02 21:36:32 +00002223 }
2224 }
2225
Dan Gohman74feef22008-10-17 01:23:35 +00002226 // Look for sign/zext/any-extended or truncate cases:
Scott Michelc9dc1142007-04-02 21:36:32 +00002227 if ((LHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
2228 || LHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
Dan Gohman74feef22008-10-17 01:23:35 +00002229 || LHSShiftAmt.getOpcode() == ISD::ANY_EXTEND
2230 || LHSShiftAmt.getOpcode() == ISD::TRUNCATE) &&
Scott Michelc9dc1142007-04-02 21:36:32 +00002231 (RHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
2232 || RHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
Dan Gohman74feef22008-10-17 01:23:35 +00002233 || RHSShiftAmt.getOpcode() == ISD::ANY_EXTEND
2234 || RHSShiftAmt.getOpcode() == ISD::TRUNCATE)) {
Dan Gohman475871a2008-07-27 21:46:04 +00002235 SDValue LExtOp0 = LHSShiftAmt.getOperand(0);
2236 SDValue RExtOp0 = RHSShiftAmt.getOperand(0);
Scott Michelc9dc1142007-04-02 21:36:32 +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 Wendlingc5cbda12008-08-31 00:37:27 +00002240 // (rotl x, y)
Scott Michelc9dc1142007-04-02 21:36:32 +00002241 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00002242 // (rotr x, (sub 32, y))
Dan Gohman74feef22008-10-17 01:23:35 +00002243 if (ConstantSDNode *SUBC =
2244 dyn_cast<ConstantSDNode>(RExtOp0.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002245 if (SUBC->getAPIntValue() == OpSizeInBits) {
Bill Wendling317bd702009-01-30 21:14:50 +00002246 return DAG.getNode(HasROTL ? ISD::ROTL : ISD::ROTR, DL, VT,
2247 LHSShiftArg,
Gabor Greif12632d22008-08-30 19:29:20 +00002248 HasROTL ? LHSShiftAmt : RHSShiftAmt).getNode();
Scott Michelc9dc1142007-04-02 21:36:32 +00002249 }
2250 }
2251 } else if (LExtOp0.getOpcode() == ISD::SUB &&
2252 RExtOp0 == LExtOp0.getOperand(1)) {
Bill Wendling353dea22008-08-31 01:04:56 +00002253 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext y))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00002254 // (rotr x, y)
Bill Wendling353dea22008-08-31 01:04:56 +00002255 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext y))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00002256 // (rotl x, (sub 32, y))
Dan Gohman74feef22008-10-17 01:23:35 +00002257 if (ConstantSDNode *SUBC =
2258 dyn_cast<ConstantSDNode>(LExtOp0.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002259 if (SUBC->getAPIntValue() == OpSizeInBits) {
Bill Wendling317bd702009-01-30 21:14:50 +00002260 return DAG.getNode(HasROTR ? ISD::ROTR : ISD::ROTL, DL, VT,
2261 LHSShiftArg,
Bill Wendling353dea22008-08-31 01:04:56 +00002262 HasROTR ? RHSShiftAmt : LHSShiftAmt).getNode();
Scott Michelc9dc1142007-04-02 21:36:32 +00002263 }
2264 }
Chris Lattner516b9622006-09-14 20:50:57 +00002265 }
2266 }
2267
2268 return 0;
2269}
2270
Dan Gohman475871a2008-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;
Nate Begeman646d7e22005-09-02 21:18:40 +00002275 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2276 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002277 MVT VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002278
Dan Gohman7f321562007-06-25 16:23:39 +00002279 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00002280 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002281 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002282 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00002283 }
Dan Gohman7f321562007-06-25 16:23:39 +00002284
Evan Cheng26471c42008-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 Gohman613e0d82007-07-03 14:03:57 +00002288 // fold (xor x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00002289 if (N0.getOpcode() == ISD::UNDEF)
2290 return N0;
2291 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00002292 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002293 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002294 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002295 return DAG.FoldConstantArithmetic(ISD::XOR, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00002296 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00002297 if (N0C && !N1C)
Bill Wendling317bd702009-01-30 21:14:50 +00002298 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002299 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002300 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002301 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00002302 // reassociate xor
Bill Wendling35247c32009-01-30 00:45:56 +00002303 SDValue RXOR = ReassociateOps(ISD::XOR, N->getDebugLoc(), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00002304 if (RXOR.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00002305 return RXOR;
Bill Wendlingae89bb12008-11-11 08:25:46 +00002306
Nate Begeman1d4d4142005-09-01 00:19:25 +00002307 // fold !(x cc y) -> (x !cc y)
Dan Gohman002e5d02008-03-13 22:13:53 +00002308 if (N1C && N1C->getAPIntValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002309 bool isInt = LHS.getValueType().isInteger();
Nate Begeman646d7e22005-09-02 21:18:40 +00002310 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
2311 isInt);
Bill Wendlingae89bb12008-11-11 08:25:46 +00002312
Duncan Sands25cf2272008-11-24 14:53:14 +00002313 if (!LegalOperations || TLI.isCondCodeLegal(NotCC, LHS.getValueType())) {
Bill Wendlingae89bb12008-11-11 08:25:46 +00002314 switch (N0.getOpcode()) {
2315 default:
2316 assert(0 && "Unhandled SetCC Equivalent!");
2317 abort();
2318 case ISD::SETCC:
Bill Wendling317bd702009-01-30 21:14:50 +00002319 return DAG.getSetCC(N->getDebugLoc(), VT, LHS, RHS, NotCC);
Bill Wendlingae89bb12008-11-11 08:25:46 +00002320 case ISD::SELECT_CC:
Bill Wendling317bd702009-01-30 21:14:50 +00002321 return DAG.getSelectCC(N->getDebugLoc(), LHS, RHS, N0.getOperand(2),
Bill Wendlingae89bb12008-11-11 08:25:46 +00002322 N0.getOperand(3), NotCC);
2323 }
2324 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00002325 }
Bill Wendlingae89bb12008-11-11 08:25:46 +00002326
Chris Lattner61c5ff42007-09-10 21:39:07 +00002327 // fold (not (zext (setcc x, y))) -> (zext (not (setcc x, y)))
Dan Gohman002e5d02008-03-13 22:13:53 +00002328 if (N1C && N1C->getAPIntValue() == 1 && N0.getOpcode() == ISD::ZERO_EXTEND &&
Gabor Greif12632d22008-08-30 19:29:20 +00002329 N0.getNode()->hasOneUse() &&
2330 isSetCCEquivalent(N0.getOperand(0), LHS, RHS, CC)){
Dan Gohman475871a2008-07-27 21:46:04 +00002331 SDValue V = N0.getOperand(0);
Bill Wendling317bd702009-01-30 21:14:50 +00002332 V = DAG.getNode(ISD::XOR, N0.getDebugLoc(), V.getValueType(), V,
Duncan Sands272dce02007-10-10 09:54:50 +00002333 DAG.getConstant(1, V.getValueType()));
Gabor Greifba36cb52008-08-28 21:40:38 +00002334 AddToWorkList(V.getNode());
Bill Wendling317bd702009-01-30 21:14:50 +00002335 return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, V);
Chris Lattner61c5ff42007-09-10 21:39:07 +00002336 }
2337
Bill Wendling317bd702009-01-30 21:14:50 +00002338 // fold (not (or x, y)) -> (and (not x), (not y)) iff x or y are setcc
Dan Gohman002e5d02008-03-13 22:13:53 +00002339 if (N1C && N1C->getAPIntValue() == 1 && VT == MVT::i1 &&
Nate Begeman99801192005-09-07 23:25:52 +00002340 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Dan Gohman475871a2008-07-27 21:46:04 +00002341 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00002342 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
2343 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Bill Wendling317bd702009-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 Greifba36cb52008-08-28 21:40:38 +00002346 AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode());
Bill Wendling317bd702009-01-30 21:14:50 +00002347 return DAG.getNode(NewOpcode, N->getDebugLoc(), VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002348 }
2349 }
Bill Wendling317bd702009-01-30 21:14:50 +00002350 // fold (not (or x, y)) -> (and (not x), (not y)) iff x or y are constants
Nate Begeman99801192005-09-07 23:25:52 +00002351 if (N1C && N1C->isAllOnesValue() &&
2352 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Dan Gohman475871a2008-07-27 21:46:04 +00002353 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00002354 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
2355 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Bill Wendling317bd702009-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 Greifba36cb52008-08-28 21:40:38 +00002358 AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode());
Bill Wendling317bd702009-01-30 21:14:50 +00002359 return DAG.getNode(NewOpcode, N->getDebugLoc(), VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002360 }
2361 }
Bill Wendling317bd702009-01-30 21:14:50 +00002362 // fold (xor (xor x, c1), c2) -> (xor x, (xor c1, c2))
Nate Begeman223df222005-09-08 20:18:10 +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 Wendling317bd702009-01-30 21:14:50 +00002367 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT, N0.getOperand(1),
2368 DAG.getConstant(N1C->getAPIntValue() ^
Dan Gohman002e5d02008-03-13 22:13:53 +00002369 N00C->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00002370 if (N01C)
Bill Wendling317bd702009-01-30 21:14:50 +00002371 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT, N0.getOperand(0),
2372 DAG.getConstant(N1C->getAPIntValue() ^
Dan Gohman002e5d02008-03-13 22:13:53 +00002373 N01C->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00002374 }
2375 // fold (xor x, x) -> 0
Chris Lattner4fbdd592006-03-28 19:11:05 +00002376 if (N0 == N1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002377 if (!VT.isVector()) {
Chris Lattner4fbdd592006-03-28 19:11:05 +00002378 return DAG.getConstant(0, VT);
Duncan Sands25cf2272008-11-24 14:53:14 +00002379 } else if (!LegalOperations || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)){
Chris Lattner4fbdd592006-03-28 19:11:05 +00002380 // Produce a vector of zeros.
Dan Gohman475871a2008-07-27 21:46:04 +00002381 SDValue El = DAG.getConstant(0, VT.getVectorElementType());
2382 std::vector<SDValue> Ops(VT.getVectorNumElements(), El);
Bill Wendling317bd702009-01-30 21:14:50 +00002383 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), VT,
2384 &Ops[0], Ops.size());
Chris Lattner4fbdd592006-03-28 19:11:05 +00002385 }
2386 }
Chris Lattner35e5c142006-05-05 05:51:50 +00002387
2388 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
2389 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002390 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002391 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002392 }
Chris Lattner35e5c142006-05-05 05:51:50 +00002393
Chris Lattner3e104b12006-04-08 04:15:24 +00002394 // Simplify the expression using non-local knowledge.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002395 if (!VT.isVector() &&
Dan Gohman475871a2008-07-27 21:46:04 +00002396 SimplifyDemandedBits(SDValue(N, 0)))
2397 return SDValue(N, 0);
Chris Lattner3e104b12006-04-08 04:15:24 +00002398
Dan Gohman475871a2008-07-27 21:46:04 +00002399 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002400}
2401
Chris Lattnere70da202007-12-06 07:33:36 +00002402/// visitShiftByConstant - Handle transforms common to the three shifts, when
2403/// the shift amount is a constant.
Dan Gohman475871a2008-07-27 21:46:04 +00002404SDValue DAGCombiner::visitShiftByConstant(SDNode *N, unsigned Amt) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002405 SDNode *LHS = N->getOperand(0).getNode();
Dan Gohman475871a2008-07-27 21:46:04 +00002406 if (!LHS->hasOneUse()) return SDValue();
Chris Lattnere70da202007-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 Gohman475871a2008-07-27 21:46:04 +00002415 default: return SDValue();
Chris Lattnere70da202007-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 Gohman475871a2008-07-27 21:46:04 +00002425 return SDValue(); // only shl(add) not sr[al](add).
Chris Lattnere70da202007-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 Gohman475871a2008-07-27 21:46:04 +00002432 if (!BinOpCst) return SDValue();
Bill Wendling88103372009-01-30 21:37:17 +00002433
2434 // FIXME: disable this unless the input to the binop is a shift by a constant.
2435 // If it is not a shift, it pessimizes some common cases like:
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00002436 //
Bill Wendling88103372009-01-30 21:37:17 +00002437 // void foo(int *X, int i) { X[i & 1235] = 1; }
2438 // int bar(int *X, int i) { return X[i & 255]; }
Gabor Greifba36cb52008-08-28 21:40:38 +00002439 SDNode *BinOpLHSVal = LHS->getOperand(0).getNode();
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00002440 if ((BinOpLHSVal->getOpcode() != ISD::SHL &&
2441 BinOpLHSVal->getOpcode() != ISD::SRA &&
2442 BinOpLHSVal->getOpcode() != ISD::SRL) ||
2443 !isa<ConstantSDNode>(BinOpLHSVal->getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00002444 return SDValue();
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00002445
Duncan Sands83ec4b62008-06-06 12:08:01 +00002446 MVT VT = N->getValueType(0);
Chris Lattnere70da202007-12-06 07:33:36 +00002447
Bill Wendling88103372009-01-30 21:37:17 +00002448 // If this is a signed shift right, and the high bit is modified by the
2449 // logical operation, do not perform the transformation. The highBitSet
2450 // boolean indicates the value of the high bit of the constant which would
2451 // cause it to be modified for this operation.
Chris Lattnere70da202007-12-06 07:33:36 +00002452 if (N->getOpcode() == ISD::SRA) {
Dan Gohman220a8232008-03-03 23:51:38 +00002453 bool BinOpRHSSignSet = BinOpCst->getAPIntValue().isNegative();
2454 if (BinOpRHSSignSet != HighBitSet)
Dan Gohman475871a2008-07-27 21:46:04 +00002455 return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00002456 }
2457
2458 // Fold the constants, shifting the binop RHS by the shift amount.
Bill Wendling88103372009-01-30 21:37:17 +00002459 SDValue NewRHS = DAG.getNode(N->getOpcode(), LHS->getOperand(1).getDebugLoc(),
2460 N->getValueType(0),
2461 LHS->getOperand(1), N->getOperand(1));
Chris Lattnere70da202007-12-06 07:33:36 +00002462
2463 // Create the new shift.
Bill Wendling88103372009-01-30 21:37:17 +00002464 SDValue NewShift = DAG.getNode(N->getOpcode(), LHS->getOperand(0).getDebugLoc(),
2465 VT, LHS->getOperand(0), N->getOperand(1));
Chris Lattnere70da202007-12-06 07:33:36 +00002466
2467 // Create the new binop.
Bill Wendling88103372009-01-30 21:37:17 +00002468 return DAG.getNode(LHS->getOpcode(), N->getDebugLoc(), VT, NewShift, NewRHS);
Chris Lattnere70da202007-12-06 07:33:36 +00002469}
2470
Dan Gohman475871a2008-07-27 21:46:04 +00002471SDValue DAGCombiner::visitSHL(SDNode *N) {
2472 SDValue N0 = N->getOperand(0);
2473 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002474 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2475 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002476 MVT VT = N0.getValueType();
2477 unsigned OpSizeInBits = VT.getSizeInBits();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002478
2479 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002480 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002481 return DAG.FoldConstantArithmetic(ISD::SHL, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002482 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002483 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002484 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002485 // fold (shl x, c >= size(x)) -> undef
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002486 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Bill Wendling88103372009-01-30 21:37:17 +00002487 return DAG.getNode(ISD::UNDEF, N->getDebugLoc(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002488 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002489 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002490 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002491 // if (shl x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00002492 if (DAG.MaskedValueIsZero(SDValue(N, 0),
Duncan Sands83ec4b62008-06-06 12:08:01 +00002493 APInt::getAllOnesValue(VT.getSizeInBits())))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002494 return DAG.getConstant(0, VT);
Evan Chengeb9f8922008-08-30 02:03:58 +00002495 // fold (shl x, (trunc (and y, c))) -> (shl x, (and (trunc y), c))
2496 // iff (trunc c) == c
2497 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00002498 N1.getOperand(0).getOpcode() == ISD::AND &&
2499 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00002500 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00002501 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Evan Chengeb9f8922008-08-30 02:03:58 +00002502 MVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00002503 SDValue N100 = N1.getOperand(0).getOperand(0);
Dan Gohmance9bc122009-01-27 20:39:34 +00002504 uint64_t TruncC = TruncVT.getIntegerVTBitMask() &
2505 N101C->getZExtValue();
Bill Wendling88103372009-01-30 21:37:17 +00002506 return DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, N0,
Evan Cheng242ebd12008-09-22 18:19:24 +00002507 DAG.getNode(ISD::AND, TruncVT,
2508 DAG.getNode(ISD::TRUNCATE, TruncVT, N100),
Dan Gohmance9bc122009-01-27 20:39:34 +00002509 DAG.getConstant(TruncC, TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00002510 }
2511 }
2512
Dan Gohman475871a2008-07-27 21:46:04 +00002513 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
2514 return SDValue(N, 0);
Bill Wendling88103372009-01-30 21:37:17 +00002515
2516 // fold (shl (shl x, c1), c2) -> 0 or (shl x, (add c1, c2))
Nate Begeman646d7e22005-09-02 21:18:40 +00002517 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002518 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002519 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
2520 uint64_t c2 = N1C->getZExtValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002521 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002522 return DAG.getConstant(0, VT);
Bill Wendling88103372009-01-30 21:37:17 +00002523 return DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002524 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002525 }
Bill Wendling88103372009-01-30 21:37:17 +00002526 // fold (shl (srl x, c1), c2) -> (shl (and x, (shl -1, c1)), (sub c2, c1)) or
2527 // (srl (and x, (shl -1, c1)), (sub c1, c2))
Nate Begeman646d7e22005-09-02 21:18:40 +00002528 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002529 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002530 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
2531 uint64_t c2 = N1C->getZExtValue();
Bill Wendling88103372009-01-30 21:37:17 +00002532 SDValue Mask = DAG.getNode(ISD::AND, N0.getDebugLoc(), VT, N0.getOperand(0),
2533 DAG.getConstant(~0ULL << c1, VT));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002534 if (c2 > c1)
Bill Wendling88103372009-01-30 21:37:17 +00002535 return DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +00002536 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002537 else
Bill Wendling88103372009-01-30 21:37:17 +00002538 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +00002539 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002540 }
Bill Wendling88103372009-01-30 21:37:17 +00002541 // fold (shl (sra x, c1), c1) -> (and x, (shl -1, c1))
Nate Begeman646d7e22005-09-02 21:18:40 +00002542 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Bill Wendling88103372009-01-30 21:37:17 +00002543 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0.getOperand(0),
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002544 DAG.getConstant(~0ULL << N1C->getZExtValue(), VT));
Chris Lattnere70da202007-12-06 07:33:36 +00002545
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002546 return N1C ? visitShiftByConstant(N, N1C->getZExtValue()) : SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002547}
2548
Dan Gohman475871a2008-07-27 21:46:04 +00002549SDValue DAGCombiner::visitSRA(SDNode *N) {
2550 SDValue N0 = N->getOperand(0);
2551 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002552 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2553 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002554 MVT VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002555
Bill Wendling88103372009-01-30 21:37:17 +00002556 // fold (sra c1, c2) -> (sra c1, c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002557 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002558 return DAG.FoldConstantArithmetic(ISD::SRA, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002559 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002560 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002561 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002562 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00002563 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002564 return N0;
Bill Wendling88103372009-01-30 21:37:17 +00002565 // fold (sra x, (setge c, size(x))) -> undef
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002566 if (N1C && N1C->getZExtValue() >= VT.getSizeInBits())
Bill Wendling88103372009-01-30 21:37:17 +00002567 return DAG.getNode(ISD::UNDEF, N->getDebugLoc(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002568 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002569 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002570 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00002571 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
2572 // sext_inreg.
2573 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002574 unsigned LowBits = VT.getSizeInBits() - (unsigned)N1C->getZExtValue();
Duncan Sands8eab8a22008-06-09 11:32:28 +00002575 MVT EVT = MVT::getIntegerVT(LowBits);
Duncan Sands25cf2272008-11-24 14:53:14 +00002576 if ((!LegalOperations || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, EVT)))
Bill Wendling88103372009-01-30 21:37:17 +00002577 return DAG.getNode(ISD::SIGN_EXTEND_INREG, N->getDebugLoc(), VT,
2578 N0.getOperand(0), DAG.getValueType(EVT));
Nate Begemanfb7217b2006-02-17 19:54:08 +00002579 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002580
Bill Wendling88103372009-01-30 21:37:17 +00002581 // fold (sra (sra x, c1), c2) -> (sra x, (add c1, c2))
Chris Lattner71d9ebc2006-02-28 06:23:04 +00002582 if (N1C && N0.getOpcode() == ISD::SRA) {
2583 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002584 unsigned Sum = N1C->getZExtValue() + C1->getZExtValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002585 if (Sum >= VT.getSizeInBits()) Sum = VT.getSizeInBits()-1;
Bill Wendling88103372009-01-30 21:37:17 +00002586 return DAG.getNode(ISD::SRA, N->getDebugLoc(), VT, N0.getOperand(0),
Chris Lattner71d9ebc2006-02-28 06:23:04 +00002587 DAG.getConstant(Sum, N1C->getValueType(0)));
2588 }
2589 }
Christopher Lamb15cbde32008-03-19 08:30:06 +00002590
Bill Wendling88103372009-01-30 21:37:17 +00002591 // fold (sra (shl X, m), (sub result_size, n))
2592 // -> (sign_extend (trunc (shl X, (sub (sub result_size, n), m)))) for
Christopher Lambb9b04282008-03-20 04:31:39 +00002593 // result_size - n != m.
2594 // If truncate is free for the target sext(shl) is likely to result in better
2595 // code.
Christopher Lamb15cbde32008-03-19 08:30:06 +00002596 if (N0.getOpcode() == ISD::SHL) {
2597 // Get the two constanst of the shifts, CN0 = m, CN = n.
2598 const ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2599 if (N01C && N1C) {
Christopher Lambb9b04282008-03-20 04:31:39 +00002600 // Determine what the truncate's result bitsize and type would be.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002601 unsigned VTValSize = VT.getSizeInBits();
2602 MVT TruncVT =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002603 MVT::getIntegerVT(VTValSize - N1C->getZExtValue());
Christopher Lambb9b04282008-03-20 04:31:39 +00002604 // Determine the residual right-shift amount.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002605 unsigned ShiftAmt = N1C->getZExtValue() - N01C->getZExtValue();
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002606
Christopher Lambb9b04282008-03-20 04:31:39 +00002607 // If the shift is not a no-op (in which case this should be just a sign
2608 // extend already), the truncated to type is legal, sign_extend is legal
Gabor Greif12632d22008-08-30 19:29:20 +00002609 // on that type, and the the truncate to that type is both legal and free,
Christopher Lambb9b04282008-03-20 04:31:39 +00002610 // perform the transform.
Duncan Sands25cf2272008-11-24 14:53:14 +00002611 if (ShiftAmt &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00002612 TLI.isOperationLegalOrCustom(ISD::SIGN_EXTEND, TruncVT) &&
2613 TLI.isOperationLegalOrCustom(ISD::TRUNCATE, VT) &&
Evan Cheng260e07e2008-03-20 02:18:41 +00002614 TLI.isTruncateFree(VT, TruncVT)) {
Christopher Lambb9b04282008-03-20 04:31:39 +00002615
Dan Gohman475871a2008-07-27 21:46:04 +00002616 SDValue Amt = DAG.getConstant(ShiftAmt, TLI.getShiftAmountTy());
Bill Wendling88103372009-01-30 21:37:17 +00002617 SDValue Shift = DAG.getNode(ISD::SRL, N0.getDebugLoc(), VT,
2618 N0.getOperand(0), Amt);
2619 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(), TruncVT,
2620 Shift);
2621 return DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(),
2622 N->getValueType(0), Trunc);
Christopher Lamb15cbde32008-03-19 08:30:06 +00002623 }
2624 }
2625 }
Chris Lattner71d9ebc2006-02-28 06:23:04 +00002626
Evan Chengeb9f8922008-08-30 02:03:58 +00002627 // fold (sra x, (trunc (and y, c))) -> (sra x, (and (trunc y), c))
2628 // iff (trunc c) == c
2629 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00002630 N1.getOperand(0).getOpcode() == ISD::AND &&
2631 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00002632 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00002633 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Evan Chengeb9f8922008-08-30 02:03:58 +00002634 MVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00002635 SDValue N100 = N1.getOperand(0).getOperand(0);
Dan Gohmance9bc122009-01-27 20:39:34 +00002636 uint64_t TruncC = TruncVT.getIntegerVTBitMask() &
2637 N101C->getZExtValue();
Bill Wendling88103372009-01-30 21:37:17 +00002638 return DAG.getNode(ISD::SRA, N->getDebugLoc(), VT, N0,
2639 DAG.getNode(ISD::AND, DebugLoc::getUnknownLoc(),
2640 TruncVT,
Evan Cheng242ebd12008-09-22 18:19:24 +00002641 DAG.getNode(ISD::TRUNCATE, TruncVT, N100),
Dan Gohmance9bc122009-01-27 20:39:34 +00002642 DAG.getConstant(TruncC, TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00002643 }
2644 }
2645
Chris Lattnera8504462006-05-08 20:51:54 +00002646 // Simplify, based on bits shifted out of the LHS.
Dan Gohman475871a2008-07-27 21:46:04 +00002647 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
2648 return SDValue(N, 0);
Chris Lattnera8504462006-05-08 20:51:54 +00002649
2650
Nate Begeman1d4d4142005-09-01 00:19:25 +00002651 // If the sign bit is known to be zero, switch this to a SRL.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002652 if (DAG.SignBitIsZero(N0))
Bill Wendling88103372009-01-30 21:37:17 +00002653 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0, N1);
Chris Lattnere70da202007-12-06 07:33:36 +00002654
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002655 return N1C ? visitShiftByConstant(N, N1C->getZExtValue()) : SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002656}
2657
Dan Gohman475871a2008-07-27 21:46:04 +00002658SDValue DAGCombiner::visitSRL(SDNode *N) {
2659 SDValue N0 = N->getOperand(0);
2660 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002661 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2662 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002663 MVT VT = N0.getValueType();
2664 unsigned OpSizeInBits = VT.getSizeInBits();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002665
2666 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002667 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002668 return DAG.FoldConstantArithmetic(ISD::SRL, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002669 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002670 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002671 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002672 // fold (srl x, c >= size(x)) -> undef
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002673 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Bill Wendling88103372009-01-30 21:37:17 +00002674 return DAG.getNode(ISD::UNDEF, N->getDebugLoc(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002675 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002676 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002677 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002678 // if (srl x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00002679 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002680 APInt::getAllOnesValue(OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002681 return DAG.getConstant(0, VT);
Chris Lattnerec06e9a2007-04-18 03:05:22 +00002682
Bill Wendling88103372009-01-30 21:37:17 +00002683 // fold (srl (srl x, c1), c2) -> 0 or (srl x, (add c1, c2))
Nate Begeman646d7e22005-09-02 21:18:40 +00002684 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002685 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002686 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
2687 uint64_t c2 = N1C->getZExtValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002688 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002689 return DAG.getConstant(0, VT);
Bill Wendling88103372009-01-30 21:37:17 +00002690 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002691 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002692 }
Chris Lattner350bec02006-04-02 06:11:11 +00002693
Chris Lattner06afe072006-05-05 22:53:17 +00002694 // fold (srl (anyextend x), c) -> (anyextend (srl x, c))
2695 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
2696 // Shifting in all undef bits?
Duncan Sands83ec4b62008-06-06 12:08:01 +00002697 MVT SmallVT = N0.getOperand(0).getValueType();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002698 if (N1C->getZExtValue() >= SmallVT.getSizeInBits())
Bill Wendling88103372009-01-30 21:37:17 +00002699 return DAG.getNode(ISD::UNDEF, N->getDebugLoc(), VT);
Chris Lattner06afe072006-05-05 22:53:17 +00002700
Bill Wendling88103372009-01-30 21:37:17 +00002701 SDValue SmallShift = DAG.getNode(ISD::SRL, N0.getDebugLoc(), SmallVT,
2702 N0.getOperand(0), N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00002703 AddToWorkList(SmallShift.getNode());
Bill Wendling88103372009-01-30 21:37:17 +00002704 return DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, SmallShift);
Chris Lattner06afe072006-05-05 22:53:17 +00002705 }
2706
Chris Lattner3657ffe2006-10-12 20:23:19 +00002707 // fold (srl (sra X, Y), 31) -> (srl X, 31). This srl only looks at the sign
2708 // bit, which is unmodified by sra.
Bill Wendling88103372009-01-30 21:37:17 +00002709 if (N1C && N1C->getZExtValue() + 1 == VT.getSizeInBits()) {
Chris Lattner3657ffe2006-10-12 20:23:19 +00002710 if (N0.getOpcode() == ISD::SRA)
Bill Wendling88103372009-01-30 21:37:17 +00002711 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0.getOperand(0), N1);
Chris Lattner3657ffe2006-10-12 20:23:19 +00002712 }
2713
Chris Lattner350bec02006-04-02 06:11:11 +00002714 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
2715 if (N1C && N0.getOpcode() == ISD::CTLZ &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002716 N1C->getAPIntValue() == Log2_32(VT.getSizeInBits())) {
Dan Gohman948d8ea2008-02-20 16:33:30 +00002717 APInt KnownZero, KnownOne;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002718 APInt Mask = APInt::getAllOnesValue(VT.getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00002719 DAG.ComputeMaskedBits(N0.getOperand(0), Mask, KnownZero, KnownOne);
Chris Lattner350bec02006-04-02 06:11:11 +00002720
2721 // If any of the input bits are KnownOne, then the input couldn't be all
2722 // zeros, thus the result of the srl will always be zero.
Dan Gohman948d8ea2008-02-20 16:33:30 +00002723 if (KnownOne.getBoolValue()) return DAG.getConstant(0, VT);
Chris Lattner350bec02006-04-02 06:11:11 +00002724
2725 // If all of the bits input the to ctlz node are known to be zero, then
2726 // the result of the ctlz is "32" and the result of the shift is one.
Dan Gohman948d8ea2008-02-20 16:33:30 +00002727 APInt UnknownBits = ~KnownZero & Mask;
Chris Lattner350bec02006-04-02 06:11:11 +00002728 if (UnknownBits == 0) return DAG.getConstant(1, VT);
2729
2730 // Otherwise, check to see if there is exactly one bit input to the ctlz.
Bill Wendling88103372009-01-30 21:37:17 +00002731 if ((UnknownBits & (UnknownBits - 1)) == 0) {
Chris Lattner350bec02006-04-02 06:11:11 +00002732 // Okay, we know that only that the single bit specified by UnknownBits
Bill Wendling88103372009-01-30 21:37:17 +00002733 // could be set on input to the CTLZ node. If this bit is set, the SRL
2734 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
2735 // to an SRL/XOR pair, which is likely to simplify more.
Dan Gohman948d8ea2008-02-20 16:33:30 +00002736 unsigned ShAmt = UnknownBits.countTrailingZeros();
Dan Gohman475871a2008-07-27 21:46:04 +00002737 SDValue Op = N0.getOperand(0);
Bill Wendling88103372009-01-30 21:37:17 +00002738
Chris Lattner350bec02006-04-02 06:11:11 +00002739 if (ShAmt) {
Bill Wendling88103372009-01-30 21:37:17 +00002740 Op = DAG.getNode(ISD::SRL, N0.getDebugLoc(), VT, Op,
Chris Lattner350bec02006-04-02 06:11:11 +00002741 DAG.getConstant(ShAmt, TLI.getShiftAmountTy()));
Gabor Greifba36cb52008-08-28 21:40:38 +00002742 AddToWorkList(Op.getNode());
Chris Lattner350bec02006-04-02 06:11:11 +00002743 }
Bill Wendling88103372009-01-30 21:37:17 +00002744
2745 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT,
2746 Op, DAG.getConstant(1, VT));
Chris Lattner350bec02006-04-02 06:11:11 +00002747 }
2748 }
Evan Chengeb9f8922008-08-30 02:03:58 +00002749
2750 // fold (srl x, (trunc (and y, c))) -> (srl x, (and (trunc y), c))
2751 // iff (trunc c) == c
2752 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00002753 N1.getOperand(0).getOpcode() == ISD::AND &&
2754 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00002755 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00002756 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Evan Chengeb9f8922008-08-30 02:03:58 +00002757 MVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00002758 SDValue N100 = N1.getOperand(0).getOperand(0);
Dan Gohmance9bc122009-01-27 20:39:34 +00002759 uint64_t TruncC = TruncVT.getIntegerVTBitMask() &
2760 N101C->getZExtValue();
Bill Wendling88103372009-01-30 21:37:17 +00002761 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0,
2762 DAG.getNode(ISD::AND, DebugLoc::getUnknownLoc(),
2763 TruncVT,
Evan Cheng242ebd12008-09-22 18:19:24 +00002764 DAG.getNode(ISD::TRUNCATE, TruncVT, N100),
Dan Gohmance9bc122009-01-27 20:39:34 +00002765 DAG.getConstant(TruncC, TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00002766 }
2767 }
Chris Lattner61a4c072007-04-18 03:06:49 +00002768
2769 // fold operands of srl based on knowledge that the low bits are not
2770 // demanded.
Dan Gohman475871a2008-07-27 21:46:04 +00002771 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
2772 return SDValue(N, 0);
Chris Lattner61a4c072007-04-18 03:06:49 +00002773
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002774 return N1C ? visitShiftByConstant(N, N1C->getZExtValue()) : SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002775}
2776
Dan Gohman475871a2008-07-27 21:46:04 +00002777SDValue DAGCombiner::visitCTLZ(SDNode *N) {
2778 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002779 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002780
2781 // fold (ctlz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002782 if (isa<ConstantSDNode>(N0))
Bill Wendling34584e62009-01-30 22:02:18 +00002783 return DAG.getNode(ISD::CTLZ, N->getDebugLoc(), VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00002784 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002785}
2786
Dan Gohman475871a2008-07-27 21:46:04 +00002787SDValue DAGCombiner::visitCTTZ(SDNode *N) {
2788 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002789 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002790
2791 // fold (cttz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002792 if (isa<ConstantSDNode>(N0))
Bill Wendling34584e62009-01-30 22:02:18 +00002793 return DAG.getNode(ISD::CTTZ, N->getDebugLoc(), VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00002794 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002795}
2796
Dan Gohman475871a2008-07-27 21:46:04 +00002797SDValue DAGCombiner::visitCTPOP(SDNode *N) {
2798 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002799 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002800
2801 // fold (ctpop c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002802 if (isa<ConstantSDNode>(N0))
Bill Wendling34584e62009-01-30 22:02:18 +00002803 return DAG.getNode(ISD::CTPOP, N->getDebugLoc(), VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00002804 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002805}
2806
Dan Gohman475871a2008-07-27 21:46:04 +00002807SDValue DAGCombiner::visitSELECT(SDNode *N) {
2808 SDValue N0 = N->getOperand(0);
2809 SDValue N1 = N->getOperand(1);
2810 SDValue N2 = N->getOperand(2);
Nate Begeman452d7be2005-09-16 00:54:12 +00002811 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2812 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2813 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002814 MVT VT = N->getValueType(0);
2815 MVT VT0 = N0.getValueType();
Nate Begeman44728a72005-09-19 22:34:01 +00002816
Bill Wendling34584e62009-01-30 22:02:18 +00002817 // fold (select C, X, X) -> X
Nate Begeman452d7be2005-09-16 00:54:12 +00002818 if (N1 == N2)
2819 return N1;
Bill Wendling34584e62009-01-30 22:02:18 +00002820 // fold (select true, X, Y) -> X
Nate Begeman452d7be2005-09-16 00:54:12 +00002821 if (N0C && !N0C->isNullValue())
2822 return N1;
Bill Wendling34584e62009-01-30 22:02:18 +00002823 // fold (select false, X, Y) -> Y
Nate Begeman452d7be2005-09-16 00:54:12 +00002824 if (N0C && N0C->isNullValue())
2825 return N2;
Bill Wendling34584e62009-01-30 22:02:18 +00002826 // fold (select C, 1, X) -> (or C, X)
Duncan Sands83ec4b62008-06-06 12:08:01 +00002827 if (VT == MVT::i1 && N1C && N1C->getAPIntValue() == 1)
Bill Wendling34584e62009-01-30 22:02:18 +00002828 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N0, N2);
2829 // fold (select C, 0, 1) -> (xor C, 1)
Bob Wilson67ba2232009-01-22 22:05:48 +00002830 if (VT.isInteger() &&
2831 (VT0 == MVT::i1 ||
2832 (VT0.isInteger() &&
2833 TLI.getBooleanContents() == TargetLowering::ZeroOrOneBooleanContent)) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00002834 N1C && N2C && N1C->isNullValue() && N2C->getAPIntValue() == 1) {
Bill Wendling34584e62009-01-30 22:02:18 +00002835 SDValue XORNode;
Evan Cheng571c4782007-08-18 05:57:05 +00002836 if (VT == VT0)
Bill Wendling34584e62009-01-30 22:02:18 +00002837 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT0,
2838 N0, DAG.getConstant(1, VT0));
2839 XORNode = DAG.getNode(ISD::XOR, N0.getDebugLoc(), VT0,
2840 N0, DAG.getConstant(1, VT0));
Gabor Greifba36cb52008-08-28 21:40:38 +00002841 AddToWorkList(XORNode.getNode());
Duncan Sands8e4eb092008-06-08 20:54:56 +00002842 if (VT.bitsGT(VT0))
Bill Wendling34584e62009-01-30 22:02:18 +00002843 return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, XORNode);
2844 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, XORNode);
Evan Cheng571c4782007-08-18 05:57:05 +00002845 }
Bill Wendling34584e62009-01-30 22:02:18 +00002846 // fold (select C, 0, X) -> (and (not C), X)
Dale Johannesene0e6fac2007-12-06 17:53:31 +00002847 if (VT == VT0 && VT == MVT::i1 && N1C && N1C->isNullValue()) {
Bob Wilson4c245462009-01-22 17:39:32 +00002848 SDValue NOTNode = DAG.getNOT(N0, VT);
2849 AddToWorkList(NOTNode.getNode());
2850 return DAG.getNode(ISD::AND, VT, NOTNode, N2);
Nate Begeman452d7be2005-09-16 00:54:12 +00002851 }
Bill Wendling34584e62009-01-30 22:02:18 +00002852 // fold (select C, X, 1) -> (or (not C), X)
Dan Gohman002e5d02008-03-13 22:13:53 +00002853 if (VT == VT0 && VT == MVT::i1 && N2C && N2C->getAPIntValue() == 1) {
Bill Wendling34584e62009-01-30 22:02:18 +00002854 SDValue NOTNode = DAG.getNOT(N0.getDebugLoc(), N0, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00002855 AddToWorkList(NOTNode.getNode());
2856 return DAG.getNode(ISD::OR, VT, NOTNode, N1);
Nate Begeman452d7be2005-09-16 00:54:12 +00002857 }
Bill Wendling34584e62009-01-30 22:02:18 +00002858 // fold (select C, X, 0) -> (and C, X)
Duncan Sands83ec4b62008-06-06 12:08:01 +00002859 if (VT == MVT::i1 && N2C && N2C->isNullValue())
Bill Wendling34584e62009-01-30 22:02:18 +00002860 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0, N1);
2861 // fold (select X, X, Y) -> (or X, Y)
2862 // fold (select X, 1, Y) -> (or X, Y)
2863 if (VT == MVT::i1 && (N0 == N1 || (N1C && N1C->getAPIntValue() == 1)))
2864 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N0, N2);
2865 // fold (select X, Y, X) -> (and X, Y)
2866 // fold (select X, Y, 0) -> (and X, Y)
2867 if (VT == MVT::i1 && (N0 == N2 || (N2C && N2C->getAPIntValue() == 0)))
2868 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0, N1);
Chris Lattner729c6d12006-05-27 00:43:02 +00002869
Chris Lattner40c62d52005-10-18 06:04:22 +00002870 // If we can fold this based on the true/false value, do so.
2871 if (SimplifySelectOps(N, N1, N2))
Dan Gohman475871a2008-07-27 21:46:04 +00002872 return SDValue(N, 0); // Don't revisit N.
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002873
Nate Begeman44728a72005-09-19 22:34:01 +00002874 // fold selects based on a setcc into other things, such as min/max/abs
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002875 if (N0.getOpcode() == ISD::SETCC) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00002876 // FIXME:
2877 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
2878 // having to say they don't support SELECT_CC on every type the DAG knows
2879 // about, since there is no way to mark an opcode illegal at all value types
Dan Gohmanf560ffa2009-01-28 17:46:25 +00002880 if (TLI.isOperationLegalOrCustom(ISD::SELECT_CC, MVT::Other))
Bill Wendling34584e62009-01-30 22:02:18 +00002881 return DAG.getNode(ISD::SELECT_CC, N->getDebugLoc(), VT,
2882 N0.getOperand(0), N0.getOperand(1),
Nate Begeman750ac1b2006-02-01 07:19:44 +00002883 N1, N2, N0.getOperand(2));
2884 else
2885 return SimplifySelect(N0, N1, N2);
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002886 }
Bill Wendling34584e62009-01-30 22:02:18 +00002887
Dan Gohman475871a2008-07-27 21:46:04 +00002888 return SDValue();
Nate Begeman452d7be2005-09-16 00:54:12 +00002889}
2890
Dan Gohman475871a2008-07-27 21:46:04 +00002891SDValue DAGCombiner::visitSELECT_CC(SDNode *N) {
2892 SDValue N0 = N->getOperand(0);
2893 SDValue N1 = N->getOperand(1);
2894 SDValue N2 = N->getOperand(2);
2895 SDValue N3 = N->getOperand(3);
2896 SDValue N4 = N->getOperand(4);
Nate Begeman44728a72005-09-19 22:34:01 +00002897 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
2898
Nate Begeman44728a72005-09-19 22:34:01 +00002899 // fold select_cc lhs, rhs, x, x, cc -> x
2900 if (N2 == N3)
2901 return N2;
Chris Lattner40c62d52005-10-18 06:04:22 +00002902
Chris Lattner5f42a242006-09-20 06:19:26 +00002903 // Determine if the condition we're dealing with is constant
Duncan Sands5480c042009-01-01 15:52:00 +00002904 SDValue SCC = SimplifySetCC(TLI.getSetCCResultType(N0.getValueType()),
2905 N0, N1, CC, false);
Gabor Greifba36cb52008-08-28 21:40:38 +00002906 if (SCC.getNode()) AddToWorkList(SCC.getNode());
Chris Lattner5f42a242006-09-20 06:19:26 +00002907
Gabor Greifba36cb52008-08-28 21:40:38 +00002908 if (ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.getNode())) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002909 if (!SCCC->isNullValue())
Chris Lattner5f42a242006-09-20 06:19:26 +00002910 return N2; // cond always true -> true val
2911 else
2912 return N3; // cond always false -> false val
2913 }
2914
2915 // Fold to a simpler select_cc
Gabor Greifba36cb52008-08-28 21:40:38 +00002916 if (SCC.getNode() && SCC.getOpcode() == ISD::SETCC)
Bill Wendling6ce610f2009-01-30 22:23:15 +00002917 return DAG.getNode(ISD::SELECT_CC, N->getDebugLoc(), N2.getValueType(),
Chris Lattner5f42a242006-09-20 06:19:26 +00002918 SCC.getOperand(0), SCC.getOperand(1), N2, N3,
2919 SCC.getOperand(2));
2920
Chris Lattner40c62d52005-10-18 06:04:22 +00002921 // If we can fold this based on the true/false value, do so.
2922 if (SimplifySelectOps(N, N2, N3))
Dan Gohman475871a2008-07-27 21:46:04 +00002923 return SDValue(N, 0); // Don't revisit N.
Chris Lattner40c62d52005-10-18 06:04:22 +00002924
Nate Begeman44728a72005-09-19 22:34:01 +00002925 // fold select_cc into other things, such as min/max/abs
2926 return SimplifySelectCC(N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00002927}
2928
Dan Gohman475871a2008-07-27 21:46:04 +00002929SDValue DAGCombiner::visitSETCC(SDNode *N) {
Nate Begeman452d7be2005-09-16 00:54:12 +00002930 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
2931 cast<CondCodeSDNode>(N->getOperand(2))->get());
2932}
2933
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002934// ExtendUsesToFormExtLoad - Trying to extend uses of a load to enable this:
2935// "fold ({s|z}ext (load x)) -> ({s|z}ext (truncate ({s|z}extload x)))"
2936// transformation. Returns true if extension are possible and the above
2937// mentioned transformation is profitable.
Dan Gohman475871a2008-07-27 21:46:04 +00002938static bool ExtendUsesToFormExtLoad(SDNode *N, SDValue N0,
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002939 unsigned ExtOpc,
2940 SmallVector<SDNode*, 4> &ExtendNodes,
Dan Gohman79ce2762009-01-15 19:20:50 +00002941 const TargetLowering &TLI) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002942 bool HasCopyToRegUses = false;
2943 bool isTruncFree = TLI.isTruncateFree(N->getValueType(0), N0.getValueType());
Gabor Greif12632d22008-08-30 19:29:20 +00002944 for (SDNode::use_iterator UI = N0.getNode()->use_begin(),
2945 UE = N0.getNode()->use_end();
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002946 UI != UE; ++UI) {
Dan Gohman89684502008-07-27 20:43:25 +00002947 SDNode *User = *UI;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002948 if (User == N)
2949 continue;
2950 // FIXME: Only extend SETCC N, N and SETCC N, c for now.
2951 if (User->getOpcode() == ISD::SETCC) {
2952 ISD::CondCode CC = cast<CondCodeSDNode>(User->getOperand(2))->get();
2953 if (ExtOpc == ISD::ZERO_EXTEND && ISD::isSignedIntSetCC(CC))
2954 // Sign bits will be lost after a zext.
2955 return false;
2956 bool Add = false;
2957 for (unsigned i = 0; i != 2; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002958 SDValue UseOp = User->getOperand(i);
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002959 if (UseOp == N0)
2960 continue;
2961 if (!isa<ConstantSDNode>(UseOp))
2962 return false;
2963 Add = true;
2964 }
2965 if (Add)
2966 ExtendNodes.push_back(User);
2967 } else {
2968 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002969 SDValue UseOp = User->getOperand(i);
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002970 if (UseOp == N0) {
2971 // If truncate from extended type to original load type is free
2972 // on this target, then it's ok to extend a CopyToReg.
2973 if (isTruncFree && User->getOpcode() == ISD::CopyToReg)
2974 HasCopyToRegUses = true;
2975 else
2976 return false;
2977 }
2978 }
2979 }
2980 }
2981
2982 if (HasCopyToRegUses) {
2983 bool BothLiveOut = false;
2984 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
2985 UI != UE; ++UI) {
Dan Gohman89684502008-07-27 20:43:25 +00002986 SDNode *User = *UI;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002987 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002988 SDValue UseOp = User->getOperand(i);
Gabor Greifba36cb52008-08-28 21:40:38 +00002989 if (UseOp.getNode() == N && UseOp.getResNo() == 0) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002990 BothLiveOut = true;
2991 break;
2992 }
2993 }
2994 }
2995 if (BothLiveOut)
2996 // Both unextended and extended values are live out. There had better be
2997 // good a reason for the transformation.
2998 return ExtendNodes.size();
2999 }
3000 return true;
3001}
3002
Dan Gohman475871a2008-07-27 21:46:04 +00003003SDValue DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
3004 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003005 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003006
Nate Begeman1d4d4142005-09-01 00:19:25 +00003007 // fold (sext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00003008 if (isa<ConstantSDNode>(N0))
Bill Wendling6ce610f2009-01-30 22:23:15 +00003009 return DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), VT, N0);
Chris Lattner310b5782006-05-06 23:06:26 +00003010
Nate Begeman1d4d4142005-09-01 00:19:25 +00003011 // fold (sext (sext x)) -> (sext x)
Chris Lattner310b5782006-05-06 23:06:26 +00003012 // fold (sext (aext x)) -> (sext x)
3013 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Bill Wendling6ce610f2009-01-30 22:23:15 +00003014 return DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), VT,
3015 N0.getOperand(0));
Chris Lattner310b5782006-05-06 23:06:26 +00003016
Chris Lattner22558872007-02-26 03:13:59 +00003017 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman1fdfa6a2008-05-20 20:56:33 +00003018 // fold (sext (truncate (load x))) -> (sext (smaller load x))
3019 // fold (sext (truncate (srl (load x), c))) -> (sext (smaller load (x+c/n)))
Gabor Greifba36cb52008-08-28 21:40:38 +00003020 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
3021 if (NarrowLoad.getNode()) {
3022 if (NarrowLoad.getNode() != N0.getNode())
3023 CombineTo(N0.getNode(), NarrowLoad);
Bill Wendling6ce610f2009-01-30 22:23:15 +00003024 return DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), VT, NarrowLoad);
Evan Cheng0b063de2007-03-23 02:16:52 +00003025 }
Evan Chengc88138f2007-03-22 01:54:19 +00003026
Dan Gohman1fdfa6a2008-05-20 20:56:33 +00003027 // See if the value being truncated is already sign extended. If so, just
3028 // eliminate the trunc/sext pair.
Dan Gohman475871a2008-07-27 21:46:04 +00003029 SDValue Op = N0.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003030 unsigned OpBits = Op.getValueType().getSizeInBits();
3031 unsigned MidBits = N0.getValueType().getSizeInBits();
3032 unsigned DestBits = VT.getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00003033 unsigned NumSignBits = DAG.ComputeNumSignBits(Op);
Chris Lattner22558872007-02-26 03:13:59 +00003034
3035 if (OpBits == DestBits) {
3036 // Op is i32, Mid is i8, and Dest is i32. If Op has more than 24 sign
3037 // bits, it is already ready.
3038 if (NumSignBits > DestBits-MidBits)
3039 return Op;
3040 } else if (OpBits < DestBits) {
3041 // Op is i32, Mid is i8, and Dest is i64. If Op has more than 24 sign
3042 // bits, just sext from i32.
3043 if (NumSignBits > OpBits-MidBits)
Bill Wendling6ce610f2009-01-30 22:23:15 +00003044 return DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), VT, Op);
Chris Lattner22558872007-02-26 03:13:59 +00003045 } else {
3046 // Op is i64, Mid is i8, and Dest is i32. If Op has more than 56 sign
3047 // bits, just truncate to i32.
3048 if (NumSignBits > OpBits-MidBits)
Bill Wendling6ce610f2009-01-30 22:23:15 +00003049 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, Op);
Chris Lattner6007b842006-09-21 06:00:20 +00003050 }
Chris Lattner22558872007-02-26 03:13:59 +00003051
3052 // fold (sext (truncate x)) -> (sextinreg x).
Duncan Sands25cf2272008-11-24 14:53:14 +00003053 if (!LegalOperations || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG,
3054 N0.getValueType())) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00003055 if (Op.getValueType().bitsLT(VT))
Bill Wendling6ce610f2009-01-30 22:23:15 +00003056 Op = DAG.getNode(ISD::ANY_EXTEND, N0.getDebugLoc(), VT, Op);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003057 else if (Op.getValueType().bitsGT(VT))
Bill Wendling6ce610f2009-01-30 22:23:15 +00003058 Op = DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(), VT, Op);
3059 return DAG.getNode(ISD::SIGN_EXTEND_INREG, N->getDebugLoc(), VT, Op,
Chris Lattner22558872007-02-26 03:13:59 +00003060 DAG.getValueType(N0.getValueType()));
3061 }
Chris Lattner6007b842006-09-21 06:00:20 +00003062 }
Chris Lattner310b5782006-05-06 23:06:26 +00003063
Evan Cheng110dec22005-12-14 02:19:23 +00003064 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00003065 if (ISD::isNON_EXTLoad(N0.getNode()) &&
Duncan Sands25cf2272008-11-24 14:53:14 +00003066 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00003067 TLI.isLoadExtLegal(ISD::SEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003068 bool DoXform = true;
3069 SmallVector<SDNode*, 4> SetCCs;
3070 if (!N0.hasOneUse())
3071 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::SIGN_EXTEND, SetCCs, TLI);
3072 if (DoXform) {
3073 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Bill Wendling6ce610f2009-01-30 22:23:15 +00003074 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, N->getDebugLoc(),
3075 VT, LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003076 LN0->getBasePtr(), LN0->getSrcValue(),
3077 LN0->getSrcValueOffset(),
3078 N0.getValueType(),
3079 LN0->isVolatile(), LN0->getAlignment());
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003080 CombineTo(N, ExtLoad);
Bill Wendling6ce610f2009-01-30 22:23:15 +00003081 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(),
3082 N0.getValueType(), ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00003083 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Bill Wendling6ce610f2009-01-30 22:23:15 +00003084
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003085 // Extend SetCC uses if necessary.
3086 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
3087 SDNode *SetCC = SetCCs[i];
Dan Gohman475871a2008-07-27 21:46:04 +00003088 SmallVector<SDValue, 4> Ops;
Bill Wendling6ce610f2009-01-30 22:23:15 +00003089
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003090 for (unsigned j = 0; j != 2; ++j) {
Dan Gohman475871a2008-07-27 21:46:04 +00003091 SDValue SOp = SetCC->getOperand(j);
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003092 if (SOp == Trunc)
3093 Ops.push_back(ExtLoad);
3094 else
Bill Wendling6ce610f2009-01-30 22:23:15 +00003095 Ops.push_back(DAG.getNode(ISD::SIGN_EXTEND, DebugLoc::getUnknownLoc(),
3096 VT, SOp));
3097 }
3098
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003099 Ops.push_back(SetCC->getOperand(2));
Bill Wendling6ce610f2009-01-30 22:23:15 +00003100 CombineTo(SetCC, DAG.getNode(ISD::SETCC, DebugLoc::getUnknownLoc(),
3101 SetCC->getValueType(0),
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003102 &Ops[0], Ops.size()));
3103 }
Bill Wendling6ce610f2009-01-30 22:23:15 +00003104
Dan Gohman475871a2008-07-27 21:46:04 +00003105 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003106 }
Nate Begeman3df4d522005-10-12 20:40:40 +00003107 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00003108
3109 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
3110 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00003111 if ((ISD::isSEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) &&
3112 ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00003113 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003114 MVT EVT = LN0->getMemoryVT();
Duncan Sands25cf2272008-11-24 14:53:14 +00003115 if ((!LegalOperations && !LN0->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00003116 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT)) {
Bill Wendling6ce610f2009-01-30 22:23:15 +00003117 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, N->getDebugLoc(), VT,
3118 LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003119 LN0->getBasePtr(), LN0->getSrcValue(),
3120 LN0->getSrcValueOffset(), EVT,
3121 LN0->isVolatile(), LN0->getAlignment());
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00003122 CombineTo(N, ExtLoad);
Gabor Greif12632d22008-08-30 19:29:20 +00003123 CombineTo(N0.getNode(),
Bill Wendling6ce610f2009-01-30 22:23:15 +00003124 DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(),
3125 N0.getValueType(), ExtLoad),
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00003126 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003127 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00003128 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00003129 }
3130
Bill Wendling6ce610f2009-01-30 22:23:15 +00003131 // sext(setcc x, y, cc) -> (select_cc x, y, -1, 0, cc)
Chris Lattner20a35c32007-04-11 05:32:27 +00003132 if (N0.getOpcode() == ISD::SETCC) {
Dan Gohman475871a2008-07-27 21:46:04 +00003133 SDValue SCC =
Chris Lattner1eba01e2007-04-11 06:50:51 +00003134 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
3135 DAG.getConstant(~0ULL, VT), DAG.getConstant(0, VT),
3136 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00003137 if (SCC.getNode()) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00003138 }
3139
Dan Gohman8f0ad582008-04-28 16:58:24 +00003140 // fold (sext x) -> (zext x) if the sign bit is known zero.
Duncan Sands25cf2272008-11-24 14:53:14 +00003141 if ((!LegalOperations || TLI.isOperationLegal(ISD::ZERO_EXTEND, VT)) &&
Dan Gohman187db7b2008-04-28 18:47:17 +00003142 DAG.SignBitIsZero(N0))
Bill Wendling6ce610f2009-01-30 22:23:15 +00003143 return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, N0);
Dan Gohman8f0ad582008-04-28 16:58:24 +00003144
Dan Gohman475871a2008-07-27 21:46:04 +00003145 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003146}
3147
Dan Gohman475871a2008-07-27 21:46:04 +00003148SDValue DAGCombiner::visitZERO_EXTEND(SDNode *N) {
3149 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003150 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003151
Nate Begeman1d4d4142005-09-01 00:19:25 +00003152 // fold (zext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00003153 if (isa<ConstantSDNode>(N0))
Bill Wendling6ce610f2009-01-30 22:23:15 +00003154 return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003155 // fold (zext (zext x)) -> (zext x)
Chris Lattner310b5782006-05-06 23:06:26 +00003156 // fold (zext (aext x)) -> (zext x)
3157 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Bill Wendling6ce610f2009-01-30 22:23:15 +00003158 return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT,
3159 N0.getOperand(0));
Chris Lattner6007b842006-09-21 06:00:20 +00003160
Evan Chengc88138f2007-03-22 01:54:19 +00003161 // fold (zext (truncate (load x))) -> (zext (smaller load x))
3162 // fold (zext (truncate (srl (load x), c))) -> (zext (small load (x+c/n)))
Dale Johannesen2041a0e2007-03-30 21:38:07 +00003163 if (N0.getOpcode() == ISD::TRUNCATE) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003164 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
3165 if (NarrowLoad.getNode()) {
3166 if (NarrowLoad.getNode() != N0.getNode())
3167 CombineTo(N0.getNode(), NarrowLoad);
Bill Wendling6ce610f2009-01-30 22:23:15 +00003168 return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, NarrowLoad);
Evan Cheng0b063de2007-03-23 02:16:52 +00003169 }
Evan Chengc88138f2007-03-22 01:54:19 +00003170 }
3171
Chris Lattner6007b842006-09-21 06:00:20 +00003172 // fold (zext (truncate x)) -> (and x, mask)
3173 if (N0.getOpcode() == ISD::TRUNCATE &&
Duncan Sands25cf2272008-11-24 14:53:14 +00003174 (!LegalOperations || TLI.isOperationLegal(ISD::AND, VT))) {
Dan Gohman475871a2008-07-27 21:46:04 +00003175 SDValue Op = N0.getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003176 if (Op.getValueType().bitsLT(VT)) {
Bill Wendling6ce610f2009-01-30 22:23:15 +00003177 Op = DAG.getNode(ISD::ANY_EXTEND, DebugLoc::getUnknownLoc(), VT, Op);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003178 } else if (Op.getValueType().bitsGT(VT)) {
Bill Wendling6ce610f2009-01-30 22:23:15 +00003179 Op = DAG.getNode(ISD::TRUNCATE, DebugLoc::getUnknownLoc(), VT, Op);
Chris Lattner6007b842006-09-21 06:00:20 +00003180 }
Bill Wendling6ce610f2009-01-30 22:23:15 +00003181 return DAG.getZeroExtendInReg(Op, N->getDebugLoc(), N0.getValueType());
Chris Lattner6007b842006-09-21 06:00:20 +00003182 }
3183
Chris Lattner111c2282006-09-21 06:14:31 +00003184 // fold (zext (and (trunc x), cst)) -> (and x, cst).
3185 if (N0.getOpcode() == ISD::AND &&
3186 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
3187 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohman475871a2008-07-27 21:46:04 +00003188 SDValue X = N0.getOperand(0).getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003189 if (X.getValueType().bitsLT(VT)) {
Bill Wendling6ce610f2009-01-30 22:23:15 +00003190 X = DAG.getNode(ISD::ANY_EXTEND, DebugLoc::getUnknownLoc(), VT, X);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003191 } else if (X.getValueType().bitsGT(VT)) {
Bill Wendling6ce610f2009-01-30 22:23:15 +00003192 X = DAG.getNode(ISD::TRUNCATE, DebugLoc::getUnknownLoc(), VT, X);
Chris Lattner111c2282006-09-21 06:14:31 +00003193 }
Dan Gohman220a8232008-03-03 23:51:38 +00003194 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003195 Mask.zext(VT.getSizeInBits());
Bill Wendling6ce610f2009-01-30 22:23:15 +00003196 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
3197 X, DAG.getConstant(Mask, VT));
Chris Lattner111c2282006-09-21 06:14:31 +00003198 }
3199
Evan Cheng110dec22005-12-14 02:19:23 +00003200 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00003201 if (ISD::isNON_EXTLoad(N0.getNode()) &&
Duncan Sands25cf2272008-11-24 14:53:14 +00003202 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00003203 TLI.isLoadExtLegal(ISD::ZEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003204 bool DoXform = true;
3205 SmallVector<SDNode*, 4> SetCCs;
3206 if (!N0.hasOneUse())
3207 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ZERO_EXTEND, SetCCs, TLI);
3208 if (DoXform) {
3209 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Bill Wendling6ce610f2009-01-30 22:23:15 +00003210 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, N->getDebugLoc(), VT,
3211 LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003212 LN0->getBasePtr(), LN0->getSrcValue(),
3213 LN0->getSrcValueOffset(),
3214 N0.getValueType(),
3215 LN0->isVolatile(), LN0->getAlignment());
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003216 CombineTo(N, ExtLoad);
Bill Wendling6ce610f2009-01-30 22:23:15 +00003217 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(),
3218 N0.getValueType(), ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00003219 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Bill Wendling6ce610f2009-01-30 22:23:15 +00003220
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003221 // Extend SetCC uses if necessary.
3222 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
3223 SDNode *SetCC = SetCCs[i];
Dan Gohman475871a2008-07-27 21:46:04 +00003224 SmallVector<SDValue, 4> Ops;
Bill Wendling6ce610f2009-01-30 22:23:15 +00003225
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003226 for (unsigned j = 0; j != 2; ++j) {
Dan Gohman475871a2008-07-27 21:46:04 +00003227 SDValue SOp = SetCC->getOperand(j);
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003228 if (SOp == Trunc)
3229 Ops.push_back(ExtLoad);
3230 else
Evan Chengde1631b2007-10-30 20:11:21 +00003231 Ops.push_back(DAG.getNode(ISD::ZERO_EXTEND, VT, SOp));
Bill Wendling6ce610f2009-01-30 22:23:15 +00003232 }
3233
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003234 Ops.push_back(SetCC->getOperand(2));
Bill Wendling6ce610f2009-01-30 22:23:15 +00003235 CombineTo(SetCC, DAG.getNode(ISD::SETCC, DebugLoc::getUnknownLoc(),
3236 SetCC->getValueType(0),
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003237 &Ops[0], Ops.size()));
3238 }
Bill Wendling6ce610f2009-01-30 22:23:15 +00003239
Dan Gohman475871a2008-07-27 21:46:04 +00003240 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003241 }
Evan Cheng110dec22005-12-14 02:19:23 +00003242 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00003243
3244 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
3245 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00003246 if ((ISD::isZEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) &&
3247 ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00003248 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003249 MVT EVT = LN0->getMemoryVT();
Duncan Sands25cf2272008-11-24 14:53:14 +00003250 if ((!LegalOperations && !LN0->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00003251 TLI.isLoadExtLegal(ISD::ZEXTLOAD, EVT)) {
Bill Wendling6ce610f2009-01-30 22:23:15 +00003252 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, N->getDebugLoc(), VT,
3253 LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003254 LN0->getBasePtr(), LN0->getSrcValue(),
3255 LN0->getSrcValueOffset(), EVT,
3256 LN0->isVolatile(), LN0->getAlignment());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003257 CombineTo(N, ExtLoad);
Gabor Greif12632d22008-08-30 19:29:20 +00003258 CombineTo(N0.getNode(),
Bill Wendling6ce610f2009-01-30 22:23:15 +00003259 DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(), N0.getValueType(),
3260 ExtLoad),
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003261 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003262 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003263 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00003264 }
Chris Lattner20a35c32007-04-11 05:32:27 +00003265
3266 // zext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
3267 if (N0.getOpcode() == ISD::SETCC) {
Dan Gohman475871a2008-07-27 21:46:04 +00003268 SDValue SCC =
Chris Lattner20a35c32007-04-11 05:32:27 +00003269 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
3270 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattner1eba01e2007-04-11 06:50:51 +00003271 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00003272 if (SCC.getNode()) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00003273 }
3274
Dan Gohman475871a2008-07-27 21:46:04 +00003275 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003276}
3277
Dan Gohman475871a2008-07-27 21:46:04 +00003278SDValue DAGCombiner::visitANY_EXTEND(SDNode *N) {
3279 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003280 MVT VT = N->getValueType(0);
Chris Lattner5ffc0662006-05-05 05:58:59 +00003281
3282 // fold (aext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00003283 if (isa<ConstantSDNode>(N0))
Chris Lattner5ffc0662006-05-05 05:58:59 +00003284 return DAG.getNode(ISD::ANY_EXTEND, VT, N0);
3285 // fold (aext (aext x)) -> (aext x)
3286 // fold (aext (zext x)) -> (zext x)
3287 // fold (aext (sext x)) -> (sext x)
3288 if (N0.getOpcode() == ISD::ANY_EXTEND ||
3289 N0.getOpcode() == ISD::ZERO_EXTEND ||
3290 N0.getOpcode() == ISD::SIGN_EXTEND)
Bill Wendling683c9572009-01-30 22:27:33 +00003291 return DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT, N0.getOperand(0));
Chris Lattner5ffc0662006-05-05 05:58:59 +00003292
Evan Chengc88138f2007-03-22 01:54:19 +00003293 // fold (aext (truncate (load x))) -> (aext (smaller load x))
3294 // fold (aext (truncate (srl (load x), c))) -> (aext (small load (x+c/n)))
3295 if (N0.getOpcode() == ISD::TRUNCATE) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003296 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
3297 if (NarrowLoad.getNode()) {
3298 if (NarrowLoad.getNode() != N0.getNode())
3299 CombineTo(N0.getNode(), NarrowLoad);
Bill Wendling683c9572009-01-30 22:27:33 +00003300 return DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, NarrowLoad);
Evan Cheng0b063de2007-03-23 02:16:52 +00003301 }
Evan Chengc88138f2007-03-22 01:54:19 +00003302 }
3303
Chris Lattner84750582006-09-20 06:29:17 +00003304 // fold (aext (truncate x))
3305 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman475871a2008-07-27 21:46:04 +00003306 SDValue TruncOp = N0.getOperand(0);
Chris Lattner84750582006-09-20 06:29:17 +00003307 if (TruncOp.getValueType() == VT)
3308 return TruncOp; // x iff x size == zext size.
Duncan Sands8e4eb092008-06-08 20:54:56 +00003309 if (TruncOp.getValueType().bitsGT(VT))
Bill Wendling683c9572009-01-30 22:27:33 +00003310 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, TruncOp);
3311 return DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, TruncOp);
Chris Lattner84750582006-09-20 06:29:17 +00003312 }
Chris Lattner0e4b9222006-09-21 06:40:43 +00003313
3314 // fold (aext (and (trunc x), cst)) -> (and x, cst).
3315 if (N0.getOpcode() == ISD::AND &&
3316 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
3317 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohman475871a2008-07-27 21:46:04 +00003318 SDValue X = N0.getOperand(0).getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003319 if (X.getValueType().bitsLT(VT)) {
Bill Wendling683c9572009-01-30 22:27:33 +00003320 X = DAG.getNode(ISD::ANY_EXTEND, DebugLoc::getUnknownLoc(), VT, X);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003321 } else if (X.getValueType().bitsGT(VT)) {
Bill Wendling683c9572009-01-30 22:27:33 +00003322 X = DAG.getNode(ISD::TRUNCATE, DebugLoc::getUnknownLoc(), VT, X);
Chris Lattner0e4b9222006-09-21 06:40:43 +00003323 }
Dan Gohman220a8232008-03-03 23:51:38 +00003324 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003325 Mask.zext(VT.getSizeInBits());
Bill Wendling683c9572009-01-30 22:27:33 +00003326 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
3327 X, DAG.getConstant(Mask, VT));
Chris Lattner0e4b9222006-09-21 06:40:43 +00003328 }
3329
Chris Lattner5ffc0662006-05-05 05:58:59 +00003330 // fold (aext (load x)) -> (aext (truncate (extload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00003331 if (ISD::isNON_EXTLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00003332 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00003333 TLI.isLoadExtLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003334 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Bill Wendling683c9572009-01-30 22:27:33 +00003335 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, N->getDebugLoc(), VT,
3336 LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003337 LN0->getBasePtr(), LN0->getSrcValue(),
3338 LN0->getSrcValueOffset(),
3339 N0.getValueType(),
3340 LN0->isVolatile(), LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00003341 CombineTo(N, ExtLoad);
Dan Gohman75dcf082008-07-31 00:50:31 +00003342 // Redirect any chain users to the new load.
Evan Cheng45299662008-08-29 23:20:46 +00003343 DAG.ReplaceAllUsesOfValueWith(SDValue(LN0, 1),
3344 SDValue(ExtLoad.getNode(), 1));
Dan Gohman75dcf082008-07-31 00:50:31 +00003345 // If any node needs the original loaded value, recompute it.
3346 if (!LN0->use_empty())
Bill Wendling683c9572009-01-30 22:27:33 +00003347 CombineTo(LN0, DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(),
3348 N0.getValueType(), ExtLoad),
Dan Gohman75dcf082008-07-31 00:50:31 +00003349 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003350 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner5ffc0662006-05-05 05:58:59 +00003351 }
3352
3353 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
3354 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
3355 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00003356 if (N0.getOpcode() == ISD::LOAD &&
Gabor Greifba36cb52008-08-28 21:40:38 +00003357 !ISD::isNON_EXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng466685d2006-10-09 20:57:25 +00003358 N0.hasOneUse()) {
3359 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003360 MVT EVT = LN0->getMemoryVT();
Bill Wendling683c9572009-01-30 22:27:33 +00003361 SDValue ExtLoad = DAG.getExtLoad(LN0->getExtensionType(), N->getDebugLoc(),
3362 VT, LN0->getChain(), LN0->getBasePtr(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003363 LN0->getSrcValue(),
3364 LN0->getSrcValueOffset(), EVT,
3365 LN0->isVolatile(), LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00003366 CombineTo(N, ExtLoad);
Evan Cheng45299662008-08-29 23:20:46 +00003367 CombineTo(N0.getNode(),
Bill Wendling683c9572009-01-30 22:27:33 +00003368 DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(),
3369 N0.getValueType(), ExtLoad),
Chris Lattner5ffc0662006-05-05 05:58:59 +00003370 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003371 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner5ffc0662006-05-05 05:58:59 +00003372 }
Chris Lattner20a35c32007-04-11 05:32:27 +00003373
3374 // aext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
3375 if (N0.getOpcode() == ISD::SETCC) {
Dan Gohman475871a2008-07-27 21:46:04 +00003376 SDValue SCC =
Chris Lattner1eba01e2007-04-11 06:50:51 +00003377 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
3378 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattnerc24bbad2007-04-11 16:51:53 +00003379 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00003380 if (SCC.getNode())
Chris Lattnerc56a81d2007-04-11 06:43:25 +00003381 return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00003382 }
3383
Dan Gohman475871a2008-07-27 21:46:04 +00003384 return SDValue();
Chris Lattner5ffc0662006-05-05 05:58:59 +00003385}
3386
Chris Lattner2b4c2792007-10-13 06:35:54 +00003387/// GetDemandedBits - See if the specified operand can be simplified with the
3388/// knowledge that only the bits specified by Mask are used. If so, return the
Dan Gohman475871a2008-07-27 21:46:04 +00003389/// simpler operand, otherwise return a null SDValue.
3390SDValue DAGCombiner::GetDemandedBits(SDValue V, const APInt &Mask) {
Chris Lattner2b4c2792007-10-13 06:35:54 +00003391 switch (V.getOpcode()) {
3392 default: break;
3393 case ISD::OR:
3394 case ISD::XOR:
3395 // If the LHS or RHS don't contribute bits to the or, drop them.
3396 if (DAG.MaskedValueIsZero(V.getOperand(0), Mask))
3397 return V.getOperand(1);
3398 if (DAG.MaskedValueIsZero(V.getOperand(1), Mask))
3399 return V.getOperand(0);
3400 break;
Chris Lattnere33544c2007-10-13 06:58:48 +00003401 case ISD::SRL:
3402 // Only look at single-use SRLs.
Gabor Greifba36cb52008-08-28 21:40:38 +00003403 if (!V.getNode()->hasOneUse())
Chris Lattnere33544c2007-10-13 06:58:48 +00003404 break;
3405 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
3406 // See if we can recursively simplify the LHS.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003407 unsigned Amt = RHSC->getZExtValue();
Dan Gohmancc91d632009-01-03 19:22:06 +00003408 // Watch out for shift count overflow though.
3409 if (Amt >= Mask.getBitWidth()) break;
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003410 APInt NewMask = Mask << Amt;
Dan Gohman475871a2008-07-27 21:46:04 +00003411 SDValue SimplifyLHS = GetDemandedBits(V.getOperand(0), NewMask);
Gabor Greifba36cb52008-08-28 21:40:38 +00003412 if (SimplifyLHS.getNode()) {
Chris Lattnere33544c2007-10-13 06:58:48 +00003413 return DAG.getNode(ISD::SRL, V.getValueType(),
3414 SimplifyLHS, V.getOperand(1));
3415 }
3416 }
Chris Lattner2b4c2792007-10-13 06:35:54 +00003417 }
Dan Gohman475871a2008-07-27 21:46:04 +00003418 return SDValue();
Chris Lattner2b4c2792007-10-13 06:35:54 +00003419}
3420
Evan Chengc88138f2007-03-22 01:54:19 +00003421/// ReduceLoadWidth - If the result of a wider load is shifted to right of N
3422/// bits and then truncated to a narrower type and where N is a multiple
3423/// of number of bits of the narrower type, transform it to a narrower load
3424/// from address + N / num of bits of new type. If the result is to be
3425/// extended, also fold the extension to form a extending load.
Dan Gohman475871a2008-07-27 21:46:04 +00003426SDValue DAGCombiner::ReduceLoadWidth(SDNode *N) {
Evan Chengc88138f2007-03-22 01:54:19 +00003427 unsigned Opc = N->getOpcode();
3428 ISD::LoadExtType ExtType = ISD::NON_EXTLOAD;
Dan Gohman475871a2008-07-27 21:46:04 +00003429 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003430 MVT VT = N->getValueType(0);
Dan Gohman764fd0c2009-01-21 15:17:51 +00003431 MVT EVT = VT;
Evan Chengc88138f2007-03-22 01:54:19 +00003432
Dan Gohman7f8613e2008-08-14 20:04:46 +00003433 // This transformation isn't valid for vector loads.
3434 if (VT.isVector())
3435 return SDValue();
3436
Evan Chenge177e302007-03-23 22:13:36 +00003437 // Special case: SIGN_EXTEND_INREG is basically truncating to EVT then
3438 // extended to VT.
Evan Chengc88138f2007-03-22 01:54:19 +00003439 if (Opc == ISD::SIGN_EXTEND_INREG) {
3440 ExtType = ISD::SEXTLOAD;
3441 EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Duncan Sands25cf2272008-11-24 14:53:14 +00003442 if (LegalOperations && !TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))
Dan Gohman475871a2008-07-27 21:46:04 +00003443 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00003444 }
3445
Duncan Sands83ec4b62008-06-06 12:08:01 +00003446 unsigned EVTBits = EVT.getSizeInBits();
Evan Chengc88138f2007-03-22 01:54:19 +00003447 unsigned ShAmt = 0;
3448 if (N0.getOpcode() == ISD::SRL && N0.hasOneUse()) {
3449 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003450 ShAmt = N01->getZExtValue();
Evan Chengc88138f2007-03-22 01:54:19 +00003451 // Is the shift amount a multiple of size of VT?
3452 if ((ShAmt & (EVTBits-1)) == 0) {
3453 N0 = N0.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003454 if (N0.getValueType().getSizeInBits() <= EVTBits)
Dan Gohman475871a2008-07-27 21:46:04 +00003455 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00003456 }
3457 }
3458 }
3459
Duncan Sandsad205a72008-06-16 08:14:38 +00003460 // Do not generate loads of non-round integer types since these can
3461 // be expensive (and would be wrong if the type is not byte sized).
Dan Gohmanf5add582009-01-20 01:06:45 +00003462 if (isa<LoadSDNode>(N0) && N0.hasOneUse() && EVT.isRound() &&
Dan Gohman75dcf082008-07-31 00:50:31 +00003463 cast<LoadSDNode>(N0)->getMemoryVT().getSizeInBits() > EVTBits &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003464 // Do not change the width of a volatile load.
3465 !cast<LoadSDNode>(N0)->isVolatile()) {
Evan Chengc88138f2007-03-22 01:54:19 +00003466 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003467 MVT PtrType = N0.getOperand(1).getValueType();
Evan Chengdae54ce2007-03-24 00:02:43 +00003468 // For big endian targets, we need to adjust the offset to the pointer to
3469 // load the correct bytes.
Duncan Sands0753fc12008-02-11 10:37:04 +00003470 if (TLI.isBigEndian()) {
Dan Gohman75dcf082008-07-31 00:50:31 +00003471 unsigned LVTStoreBits = LN0->getMemoryVT().getStoreSizeInBits();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003472 unsigned EVTStoreBits = EVT.getStoreSizeInBits();
Duncan Sandsc6fa1702007-11-09 08:57:19 +00003473 ShAmt = LVTStoreBits - EVTStoreBits - ShAmt;
3474 }
Evan Chengdae54ce2007-03-24 00:02:43 +00003475 uint64_t PtrOff = ShAmt / 8;
Duncan Sandsdc846502007-10-28 12:59:45 +00003476 unsigned NewAlign = MinAlign(LN0->getAlignment(), PtrOff);
Dan Gohman475871a2008-07-27 21:46:04 +00003477 SDValue NewPtr = DAG.getNode(ISD::ADD, PtrType, LN0->getBasePtr(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003478 DAG.getConstant(PtrOff, PtrType));
Gabor Greifba36cb52008-08-28 21:40:38 +00003479 AddToWorkList(NewPtr.getNode());
Dan Gohman475871a2008-07-27 21:46:04 +00003480 SDValue Load = (ExtType == ISD::NON_EXTLOAD)
Evan Chengc88138f2007-03-22 01:54:19 +00003481 ? DAG.getLoad(VT, LN0->getChain(), NewPtr,
Dan Gohman75dcf082008-07-31 00:50:31 +00003482 LN0->getSrcValue(), LN0->getSrcValueOffset() + PtrOff,
Duncan Sandsdc846502007-10-28 12:59:45 +00003483 LN0->isVolatile(), NewAlign)
Evan Chengc88138f2007-03-22 01:54:19 +00003484 : DAG.getExtLoad(ExtType, VT, LN0->getChain(), NewPtr,
Dan Gohman75dcf082008-07-31 00:50:31 +00003485 LN0->getSrcValue(), LN0->getSrcValueOffset() + PtrOff,
3486 EVT, LN0->isVolatile(), NewAlign);
Dan Gohman764fd0c2009-01-21 15:17:51 +00003487 // Replace the old load's chain with the new load's chain.
3488 WorkListRemover DeadNodes(*this);
3489 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), Load.getValue(1),
3490 &DeadNodes);
3491 // Return the new loaded value.
3492 return Load;
Evan Chengc88138f2007-03-22 01:54:19 +00003493 }
3494
Dan Gohman475871a2008-07-27 21:46:04 +00003495 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00003496}
3497
Chris Lattner5ffc0662006-05-05 05:58:59 +00003498
Dan Gohman475871a2008-07-27 21:46:04 +00003499SDValue DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
3500 SDValue N0 = N->getOperand(0);
3501 SDValue N1 = N->getOperand(1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003502 MVT VT = N->getValueType(0);
3503 MVT EVT = cast<VTSDNode>(N1)->getVT();
3504 unsigned VTBits = VT.getSizeInBits();
3505 unsigned EVTBits = EVT.getSizeInBits();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003506
Nate Begeman1d4d4142005-09-01 00:19:25 +00003507 // fold (sext_in_reg c1) -> c1
Chris Lattnereaeda562006-05-08 20:59:41 +00003508 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
Chris Lattner310b5782006-05-06 23:06:26 +00003509 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0, N1);
Chris Lattneree4ea922006-05-06 09:30:03 +00003510
Chris Lattner541a24f2006-05-06 22:43:44 +00003511 // If the input is already sign extended, just drop the extension.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003512 if (DAG.ComputeNumSignBits(N0) >= VT.getSizeInBits()-EVTBits+1)
Chris Lattneree4ea922006-05-06 09:30:03 +00003513 return N0;
3514
Nate Begeman646d7e22005-09-02 21:18:40 +00003515 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
3516 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Duncan Sands8e4eb092008-06-08 20:54:56 +00003517 EVT.bitsLT(cast<VTSDNode>(N0.getOperand(1))->getVT())) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00003518 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00003519 }
Chris Lattner4b37e872006-05-08 21:18:59 +00003520
Dan Gohman75dcf082008-07-31 00:50:31 +00003521 // fold (sext_in_reg (sext x)) -> (sext x)
3522 // fold (sext_in_reg (aext x)) -> (sext x)
3523 // if x is small enough.
3524 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND) {
3525 SDValue N00 = N0.getOperand(0);
3526 if (N00.getValueType().getSizeInBits() < EVTBits)
3527 return DAG.getNode(ISD::SIGN_EXTEND, VT, N00, N1);
3528 }
3529
Chris Lattner95a5e052007-04-17 19:03:21 +00003530 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is known zero.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003531 if (DAG.MaskedValueIsZero(N0, APInt::getBitsSet(VTBits, EVTBits-1, EVTBits)))
Nate Begemande996292006-02-03 22:24:05 +00003532 return DAG.getZeroExtendInReg(N0, EVT);
Chris Lattner4b37e872006-05-08 21:18:59 +00003533
Chris Lattner95a5e052007-04-17 19:03:21 +00003534 // fold operands of sext_in_reg based on knowledge that the top bits are not
3535 // demanded.
Dan Gohman475871a2008-07-27 21:46:04 +00003536 if (SimplifyDemandedBits(SDValue(N, 0)))
3537 return SDValue(N, 0);
Chris Lattner95a5e052007-04-17 19:03:21 +00003538
Evan Chengc88138f2007-03-22 01:54:19 +00003539 // fold (sext_in_reg (load x)) -> (smaller sextload x)
3540 // fold (sext_in_reg (srl (load x), c)) -> (smaller sextload (x+c/evtbits))
Dan Gohman475871a2008-07-27 21:46:04 +00003541 SDValue NarrowLoad = ReduceLoadWidth(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003542 if (NarrowLoad.getNode())
Evan Chengc88138f2007-03-22 01:54:19 +00003543 return NarrowLoad;
3544
Chris Lattner4b37e872006-05-08 21:18:59 +00003545 // fold (sext_in_reg (srl X, 24), i8) -> sra X, 24
3546 // fold (sext_in_reg (srl X, 23), i8) -> sra X, 23 iff possible.
3547 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
3548 if (N0.getOpcode() == ISD::SRL) {
3549 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003550 if (ShAmt->getZExtValue()+EVTBits <= VT.getSizeInBits()) {
Chris Lattner4b37e872006-05-08 21:18:59 +00003551 // We can turn this into an SRA iff the input to the SRL is already sign
3552 // extended enough.
Dan Gohmanea859be2007-06-22 14:59:07 +00003553 unsigned InSignBits = DAG.ComputeNumSignBits(N0.getOperand(0));
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003554 if (VT.getSizeInBits()-(ShAmt->getZExtValue()+EVTBits) < InSignBits)
Chris Lattner4b37e872006-05-08 21:18:59 +00003555 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0), N0.getOperand(1));
3556 }
3557 }
Evan Chengc88138f2007-03-22 01:54:19 +00003558
Nate Begemanded49632005-10-13 03:11:28 +00003559 // fold (sext_inreg (extload x)) -> (sextload x)
Gabor Greifba36cb52008-08-28 21:40:38 +00003560 if (ISD::isEXTLoad(N0.getNode()) &&
3561 ISD::isUNINDEXEDLoad(N0.getNode()) &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003562 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00003563 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00003564 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003565 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman475871a2008-07-27 21:46:04 +00003566 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003567 LN0->getBasePtr(), LN0->getSrcValue(),
3568 LN0->getSrcValueOffset(), EVT,
3569 LN0->isVolatile(), LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00003570 CombineTo(N, ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00003571 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003572 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00003573 }
3574 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Gabor Greifba36cb52008-08-28 21:40:38 +00003575 if (ISD::isZEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng83060c52007-03-07 08:07:03 +00003576 N0.hasOneUse() &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003577 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00003578 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00003579 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003580 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman475871a2008-07-27 21:46:04 +00003581 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003582 LN0->getBasePtr(), LN0->getSrcValue(),
3583 LN0->getSrcValueOffset(), EVT,
3584 LN0->isVolatile(), LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00003585 CombineTo(N, ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00003586 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003587 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00003588 }
Dan Gohman475871a2008-07-27 21:46:04 +00003589 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003590}
3591
Dan Gohman475871a2008-07-27 21:46:04 +00003592SDValue DAGCombiner::visitTRUNCATE(SDNode *N) {
3593 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003594 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003595
3596 // noop truncate
3597 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00003598 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003599 // fold (truncate c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00003600 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00003601 return DAG.getNode(ISD::TRUNCATE, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003602 // fold (truncate (truncate x)) -> (truncate x)
3603 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00003604 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003605 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
Chris Lattnerb72773b2006-05-05 22:56:26 +00003606 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND||
3607 N0.getOpcode() == ISD::ANY_EXTEND) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00003608 if (N0.getOperand(0).getValueType().bitsLT(VT))
Nate Begeman1d4d4142005-09-01 00:19:25 +00003609 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00003610 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Duncan Sands8e4eb092008-06-08 20:54:56 +00003611 else if (N0.getOperand(0).getValueType().bitsGT(VT))
Nate Begeman1d4d4142005-09-01 00:19:25 +00003612 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00003613 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003614 else
3615 // if the source and dest are the same type, we can drop both the extend
3616 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00003617 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003618 }
Evan Cheng007b69e2007-03-21 20:14:05 +00003619
Chris Lattner2b4c2792007-10-13 06:35:54 +00003620 // See if we can simplify the input to this truncate through knowledge that
3621 // only the low bits are being used. For example "trunc (or (shl x, 8), y)"
3622 // -> trunc y
Dan Gohman475871a2008-07-27 21:46:04 +00003623 SDValue Shorter =
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003624 GetDemandedBits(N0, APInt::getLowBitsSet(N0.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +00003625 VT.getSizeInBits()));
Gabor Greifba36cb52008-08-28 21:40:38 +00003626 if (Shorter.getNode())
Chris Lattner2b4c2792007-10-13 06:35:54 +00003627 return DAG.getNode(ISD::TRUNCATE, VT, Shorter);
3628
Nate Begeman3df4d522005-10-12 20:40:40 +00003629 // fold (truncate (load x)) -> (smaller load x)
Evan Cheng007b69e2007-03-21 20:14:05 +00003630 // fold (truncate (srl (load x), c)) -> (smaller load (x+c/evtbits))
Evan Chengc88138f2007-03-22 01:54:19 +00003631 return ReduceLoadWidth(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003632}
3633
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003634static SDNode *getBuildPairElt(SDNode *N, unsigned i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003635 SDValue Elt = N->getOperand(i);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003636 if (Elt.getOpcode() != ISD::MERGE_VALUES)
Gabor Greifba36cb52008-08-28 21:40:38 +00003637 return Elt.getNode();
3638 return Elt.getOperand(Elt.getResNo()).getNode();
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003639}
3640
3641/// CombineConsecutiveLoads - build_pair (load, load) -> load
3642/// if load locations are consecutive.
Dan Gohman475871a2008-07-27 21:46:04 +00003643SDValue DAGCombiner::CombineConsecutiveLoads(SDNode *N, MVT VT) {
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003644 assert(N->getOpcode() == ISD::BUILD_PAIR);
3645
3646 SDNode *LD1 = getBuildPairElt(N, 0);
3647 if (!ISD::isNON_EXTLoad(LD1) || !LD1->hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00003648 return SDValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003649 MVT LD1VT = LD1->getValueType(0);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003650 SDNode *LD2 = getBuildPairElt(N, 1);
3651 const MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3652 if (ISD::isNON_EXTLoad(LD2) &&
3653 LD2->hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003654 // If both are volatile this would reduce the number of volatile loads.
3655 // If one is volatile it might be ok, but play conservative and bail out.
3656 !cast<LoadSDNode>(LD1)->isVolatile() &&
3657 !cast<LoadSDNode>(LD2)->isVolatile() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003658 TLI.isConsecutiveLoad(LD2, LD1, LD1VT.getSizeInBits()/8, 1, MFI)) {
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003659 LoadSDNode *LD = cast<LoadSDNode>(LD1);
3660 unsigned Align = LD->getAlignment();
Dan Gohman6448d912008-09-04 15:39:15 +00003661 unsigned NewAlign = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00003662 getABITypeAlignment(VT.getTypeForMVT());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003663 if (NewAlign <= Align &&
Duncan Sands25cf2272008-11-24 14:53:14 +00003664 (!LegalOperations || TLI.isOperationLegal(ISD::LOAD, VT)))
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003665 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(),
3666 LD->getSrcValue(), LD->getSrcValueOffset(),
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003667 false, Align);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003668 }
Dan Gohman475871a2008-07-27 21:46:04 +00003669 return SDValue();
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003670}
3671
Dan Gohman475871a2008-07-27 21:46:04 +00003672SDValue DAGCombiner::visitBIT_CONVERT(SDNode *N) {
3673 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003674 MVT VT = N->getValueType(0);
Chris Lattner94683772005-12-23 05:30:37 +00003675
Dan Gohman7f321562007-06-25 16:23:39 +00003676 // If the input is a BUILD_VECTOR with all constant elements, fold this now.
3677 // Only do this before legalize, since afterward the target may be depending
3678 // on the bitconvert.
3679 // First check to see if this is all constant.
Duncan Sands25cf2272008-11-24 14:53:14 +00003680 if (!LegalTypes &&
Gabor Greifba36cb52008-08-28 21:40:38 +00003681 N0.getOpcode() == ISD::BUILD_VECTOR && N0.getNode()->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003682 VT.isVector()) {
Dan Gohman7f321562007-06-25 16:23:39 +00003683 bool isSimple = true;
3684 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i)
3685 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
3686 N0.getOperand(i).getOpcode() != ISD::Constant &&
3687 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
3688 isSimple = false;
3689 break;
3690 }
3691
Duncan Sands83ec4b62008-06-06 12:08:01 +00003692 MVT DestEltVT = N->getValueType(0).getVectorElementType();
3693 assert(!DestEltVT.isVector() &&
Dan Gohman7f321562007-06-25 16:23:39 +00003694 "Element type of vector ValueType must not be vector!");
3695 if (isSimple) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003696 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(N0.getNode(), DestEltVT);
Dan Gohman7f321562007-06-25 16:23:39 +00003697 }
3698 }
3699
Dan Gohman3dd168d2008-09-05 01:58:21 +00003700 // If the input is a constant, let getNode fold it.
Chris Lattner94683772005-12-23 05:30:37 +00003701 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
Dan Gohman475871a2008-07-27 21:46:04 +00003702 SDValue Res = DAG.getNode(ISD::BIT_CONVERT, VT, N0);
Gabor Greifba36cb52008-08-28 21:40:38 +00003703 if (Res.getNode() != N) return Res;
Chris Lattner94683772005-12-23 05:30:37 +00003704 }
3705
Chris Lattnerc8547d82005-12-23 05:37:50 +00003706 if (N0.getOpcode() == ISD::BIT_CONVERT) // conv(conv(x,t1),t2) -> conv(x,t2)
3707 return DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
Chris Lattner6258fb22006-04-02 02:53:43 +00003708
Chris Lattner57104102005-12-23 05:44:41 +00003709 // fold (conv (load x)) -> (load (conv*)x)
Evan Cheng513da432007-10-06 08:19:55 +00003710 // If the resultant load doesn't need a higher alignment than the original!
Gabor Greifba36cb52008-08-28 21:40:38 +00003711 if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003712 // Do not change the width of a volatile load.
3713 !cast<LoadSDNode>(N0)->isVolatile() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00003714 (!LegalOperations || TLI.isOperationLegal(ISD::LOAD, VT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003715 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman6448d912008-09-04 15:39:15 +00003716 unsigned Align = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00003717 getABITypeAlignment(VT.getTypeForMVT());
Evan Cheng59d5b682007-05-07 21:27:48 +00003718 unsigned OrigAlign = LN0->getAlignment();
3719 if (Align <= OrigAlign) {
Dan Gohman475871a2008-07-27 21:46:04 +00003720 SDValue Load = DAG.getLoad(VT, LN0->getChain(), LN0->getBasePtr(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003721 LN0->getSrcValue(), LN0->getSrcValueOffset(),
3722 LN0->isVolatile(), OrigAlign);
Evan Cheng59d5b682007-05-07 21:27:48 +00003723 AddToWorkList(N);
Gabor Greif12632d22008-08-30 19:29:20 +00003724 CombineTo(N0.getNode(),
3725 DAG.getNode(ISD::BIT_CONVERT, N0.getValueType(), Load),
Evan Cheng59d5b682007-05-07 21:27:48 +00003726 Load.getValue(1));
3727 return Load;
3728 }
Chris Lattner57104102005-12-23 05:44:41 +00003729 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003730
Chris Lattner3bd39d42008-01-27 17:42:27 +00003731 // Fold bitconvert(fneg(x)) -> xor(bitconvert(x), signbit)
3732 // Fold bitconvert(fabs(x)) -> and(bitconvert(x), ~signbit)
3733 // This often reduces constant pool loads.
3734 if ((N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FABS) &&
Gabor Greifba36cb52008-08-28 21:40:38 +00003735 N0.getNode()->hasOneUse() && VT.isInteger() && !VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003736 SDValue NewConv = DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00003737 AddToWorkList(NewConv.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003738
Duncan Sands83ec4b62008-06-06 12:08:01 +00003739 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003740 if (N0.getOpcode() == ISD::FNEG)
3741 return DAG.getNode(ISD::XOR, VT, NewConv, DAG.getConstant(SignBit, VT));
3742 assert(N0.getOpcode() == ISD::FABS);
3743 return DAG.getNode(ISD::AND, VT, NewConv, DAG.getConstant(~SignBit, VT));
3744 }
3745
3746 // Fold bitconvert(fcopysign(cst, x)) -> bitconvert(x)&sign | cst&~sign'
3747 // Note that we don't handle copysign(x,cst) because this can always be folded
3748 // to an fneg or fabs.
Gabor Greifba36cb52008-08-28 21:40:38 +00003749 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse() &&
Chris Lattnerf32aac32008-01-27 23:32:17 +00003750 isa<ConstantFPSDNode>(N0.getOperand(0)) &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003751 VT.isInteger() && !VT.isVector()) {
3752 unsigned OrigXWidth = N0.getOperand(1).getValueType().getSizeInBits();
Duncan Sands25cf2272008-11-24 14:53:14 +00003753 MVT IntXVT = MVT::getIntegerVT(OrigXWidth);
3754 if (TLI.isTypeLegal(IntXVT) || !LegalTypes) {
3755 SDValue X = DAG.getNode(ISD::BIT_CONVERT, IntXVT, N0.getOperand(1));
3756 AddToWorkList(X.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003757
Duncan Sands25cf2272008-11-24 14:53:14 +00003758 // If X has a different width than the result/lhs, sext it or truncate it.
3759 unsigned VTWidth = VT.getSizeInBits();
3760 if (OrigXWidth < VTWidth) {
3761 X = DAG.getNode(ISD::SIGN_EXTEND, VT, X);
3762 AddToWorkList(X.getNode());
3763 } else if (OrigXWidth > VTWidth) {
3764 // To get the sign bit in the right place, we have to shift it right
3765 // before truncating.
3766 X = DAG.getNode(ISD::SRL, X.getValueType(), X,
3767 DAG.getConstant(OrigXWidth-VTWidth, X.getValueType()));
3768 AddToWorkList(X.getNode());
3769 X = DAG.getNode(ISD::TRUNCATE, VT, X);
3770 AddToWorkList(X.getNode());
3771 }
Chris Lattner3bd39d42008-01-27 17:42:27 +00003772
Duncan Sands25cf2272008-11-24 14:53:14 +00003773 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
3774 X = DAG.getNode(ISD::AND, VT, X, DAG.getConstant(SignBit, VT));
3775 AddToWorkList(X.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003776
Duncan Sands25cf2272008-11-24 14:53:14 +00003777 SDValue Cst = DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
3778 Cst = DAG.getNode(ISD::AND, VT, Cst, DAG.getConstant(~SignBit, VT));
3779 AddToWorkList(Cst.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003780
Duncan Sands25cf2272008-11-24 14:53:14 +00003781 return DAG.getNode(ISD::OR, VT, X, Cst);
3782 }
Chris Lattner3bd39d42008-01-27 17:42:27 +00003783 }
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003784
3785 // bitconvert(build_pair(ld, ld)) -> ld iff load locations are consecutive.
3786 if (N0.getOpcode() == ISD::BUILD_PAIR) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003787 SDValue CombineLD = CombineConsecutiveLoads(N0.getNode(), VT);
3788 if (CombineLD.getNode())
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003789 return CombineLD;
3790 }
Chris Lattner3bd39d42008-01-27 17:42:27 +00003791
Dan Gohman475871a2008-07-27 21:46:04 +00003792 return SDValue();
Chris Lattner94683772005-12-23 05:30:37 +00003793}
3794
Dan Gohman475871a2008-07-27 21:46:04 +00003795SDValue DAGCombiner::visitBUILD_PAIR(SDNode *N) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003796 MVT VT = N->getValueType(0);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003797 return CombineConsecutiveLoads(N, VT);
3798}
3799
Dan Gohman7f321562007-06-25 16:23:39 +00003800/// ConstantFoldBIT_CONVERTofBUILD_VECTOR - We know that BV is a build_vector
Chris Lattner6258fb22006-04-02 02:53:43 +00003801/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
3802/// destination element value type.
Dan Gohman475871a2008-07-27 21:46:04 +00003803SDValue DAGCombiner::
Duncan Sands83ec4b62008-06-06 12:08:01 +00003804ConstantFoldBIT_CONVERTofBUILD_VECTOR(SDNode *BV, MVT DstEltVT) {
3805 MVT SrcEltVT = BV->getOperand(0).getValueType();
Chris Lattner6258fb22006-04-02 02:53:43 +00003806
3807 // If this is already the right type, we're done.
Dan Gohman475871a2008-07-27 21:46:04 +00003808 if (SrcEltVT == DstEltVT) return SDValue(BV, 0);
Chris Lattner6258fb22006-04-02 02:53:43 +00003809
Duncan Sands83ec4b62008-06-06 12:08:01 +00003810 unsigned SrcBitSize = SrcEltVT.getSizeInBits();
3811 unsigned DstBitSize = DstEltVT.getSizeInBits();
Chris Lattner6258fb22006-04-02 02:53:43 +00003812
3813 // If this is a conversion of N elements of one type to N elements of another
3814 // type, convert each element. This handles FP<->INT cases.
3815 if (SrcBitSize == DstBitSize) {
Dan Gohman475871a2008-07-27 21:46:04 +00003816 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003817 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003818 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, DstEltVT, BV->getOperand(i)));
Gabor Greifba36cb52008-08-28 21:40:38 +00003819 AddToWorkList(Ops.back().getNode());
Chris Lattner3e104b12006-04-08 04:15:24 +00003820 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00003821 MVT VT = MVT::getVectorVT(DstEltVT,
3822 BV->getValueType(0).getVectorNumElements());
Dan Gohman7f321562007-06-25 16:23:39 +00003823 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003824 }
3825
3826 // Otherwise, we're growing or shrinking the elements. To avoid having to
3827 // handle annoying details of growing/shrinking FP values, we convert them to
3828 // int first.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003829 if (SrcEltVT.isFloatingPoint()) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003830 // Convert the input float vector to a int vector where the elements are the
3831 // same sizes.
3832 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
Duncan Sands8eab8a22008-06-09 11:32:28 +00003833 MVT IntVT = MVT::getIntegerVT(SrcEltVT.getSizeInBits());
Gabor Greifba36cb52008-08-28 21:40:38 +00003834 BV = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, IntVT).getNode();
Chris Lattner6258fb22006-04-02 02:53:43 +00003835 SrcEltVT = IntVT;
3836 }
3837
3838 // Now we know the input is an integer vector. If the output is a FP type,
3839 // convert to integer first, then to FP of the right size.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003840 if (DstEltVT.isFloatingPoint()) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003841 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
Duncan Sands8eab8a22008-06-09 11:32:28 +00003842 MVT TmpVT = MVT::getIntegerVT(DstEltVT.getSizeInBits());
Gabor Greifba36cb52008-08-28 21:40:38 +00003843 SDNode *Tmp = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, TmpVT).getNode();
Chris Lattner6258fb22006-04-02 02:53:43 +00003844
3845 // Next, convert to FP elements of the same size.
Dan Gohman7f321562007-06-25 16:23:39 +00003846 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(Tmp, DstEltVT);
Chris Lattner6258fb22006-04-02 02:53:43 +00003847 }
3848
3849 // Okay, we know the src/dst types are both integers of differing types.
3850 // Handling growing first.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003851 assert(SrcEltVT.isInteger() && DstEltVT.isInteger());
Chris Lattner6258fb22006-04-02 02:53:43 +00003852 if (SrcBitSize < DstBitSize) {
3853 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
3854
Dan Gohman475871a2008-07-27 21:46:04 +00003855 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003856 for (unsigned i = 0, e = BV->getNumOperands(); i != e;
Chris Lattner6258fb22006-04-02 02:53:43 +00003857 i += NumInputsPerOutput) {
3858 bool isLE = TLI.isLittleEndian();
Dan Gohman220a8232008-03-03 23:51:38 +00003859 APInt NewBits = APInt(DstBitSize, 0);
Chris Lattner6258fb22006-04-02 02:53:43 +00003860 bool EltIsUndef = true;
3861 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
3862 // Shift the previously computed bits over.
3863 NewBits <<= SrcBitSize;
Dan Gohman475871a2008-07-27 21:46:04 +00003864 SDValue Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
Chris Lattner6258fb22006-04-02 02:53:43 +00003865 if (Op.getOpcode() == ISD::UNDEF) continue;
3866 EltIsUndef = false;
3867
Dan Gohman220a8232008-03-03 23:51:38 +00003868 NewBits |=
3869 APInt(cast<ConstantSDNode>(Op)->getAPIntValue()).zext(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00003870 }
3871
3872 if (EltIsUndef)
3873 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
3874 else
3875 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
3876 }
3877
Duncan Sands83ec4b62008-06-06 12:08:01 +00003878 MVT VT = MVT::getVectorVT(DstEltVT, Ops.size());
Dan Gohman7f321562007-06-25 16:23:39 +00003879 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003880 }
3881
3882 // Finally, this must be the case where we are shrinking elements: each input
3883 // turns into multiple outputs.
Evan Chengefec7512008-02-18 23:04:32 +00003884 bool isS2V = ISD::isScalarToVector(BV);
Chris Lattner6258fb22006-04-02 02:53:43 +00003885 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003886 MVT VT = MVT::getVectorVT(DstEltVT, NumOutputsPerInput*BV->getNumOperands());
Dan Gohman475871a2008-07-27 21:46:04 +00003887 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003888 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003889 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
3890 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
3891 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
3892 continue;
3893 }
Dan Gohman220a8232008-03-03 23:51:38 +00003894 APInt OpVal = cast<ConstantSDNode>(BV->getOperand(i))->getAPIntValue();
Chris Lattner6258fb22006-04-02 02:53:43 +00003895 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
Dan Gohman220a8232008-03-03 23:51:38 +00003896 APInt ThisVal = APInt(OpVal).trunc(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00003897 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
Dan Gohman220a8232008-03-03 23:51:38 +00003898 if (isS2V && i == 0 && j == 0 && APInt(ThisVal).zext(SrcBitSize) == OpVal)
Evan Chengefec7512008-02-18 23:04:32 +00003899 // Simply turn this into a SCALAR_TO_VECTOR of the new type.
3900 return DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Ops[0]);
Dan Gohman220a8232008-03-03 23:51:38 +00003901 OpVal = OpVal.lshr(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00003902 }
3903
3904 // For big endian targets, swap the order of the pieces of each element.
Duncan Sands0753fc12008-02-11 10:37:04 +00003905 if (TLI.isBigEndian())
Chris Lattner6258fb22006-04-02 02:53:43 +00003906 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
3907 }
Dan Gohman7f321562007-06-25 16:23:39 +00003908 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003909}
3910
3911
3912
Dan Gohman475871a2008-07-27 21:46:04 +00003913SDValue DAGCombiner::visitFADD(SDNode *N) {
3914 SDValue N0 = N->getOperand(0);
3915 SDValue N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003916 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3917 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003918 MVT VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00003919
Dan Gohman7f321562007-06-25 16:23:39 +00003920 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003921 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003922 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003923 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00003924 }
Dan Gohman7f321562007-06-25 16:23:39 +00003925
Nate Begemana0e221d2005-10-18 00:28:13 +00003926 // fold (fadd c1, c2) -> c1+c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003927 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003928 return DAG.getNode(ISD::FADD, VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003929 // canonicalize constant to RHS
3930 if (N0CFP && !N1CFP)
3931 return DAG.getNode(ISD::FADD, VT, N1, N0);
Dan Gohman760f86f2009-01-22 21:58:43 +00003932 // fold (A + 0) -> A
3933 if (UnsafeFPMath && N1CFP && N1CFP->getValueAPF().isZero())
3934 return N0;
Chris Lattner01b3d732005-09-28 22:28:18 +00003935 // fold (A + (-B)) -> A-B
Duncan Sands25cf2272008-11-24 14:53:14 +00003936 if (isNegatibleForFree(N1, LegalOperations) == 2)
Chris Lattner0254e702008-02-26 07:04:54 +00003937 return DAG.getNode(ISD::FSUB, VT, N0,
Duncan Sands25cf2272008-11-24 14:53:14 +00003938 GetNegatedExpression(N1, DAG, LegalOperations));
Chris Lattner01b3d732005-09-28 22:28:18 +00003939 // fold ((-A) + B) -> B-A
Duncan Sands25cf2272008-11-24 14:53:14 +00003940 if (isNegatibleForFree(N0, LegalOperations) == 2)
Chris Lattner0254e702008-02-26 07:04:54 +00003941 return DAG.getNode(ISD::FSUB, VT, N1,
Duncan Sands25cf2272008-11-24 14:53:14 +00003942 GetNegatedExpression(N0, DAG, LegalOperations));
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003943
3944 // If allowed, fold (fadd (fadd x, c1), c2) -> (fadd x, (fadd c1, c2))
3945 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FADD &&
Gabor Greifba36cb52008-08-28 21:40:38 +00003946 N0.getNode()->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003947 return DAG.getNode(ISD::FADD, VT, N0.getOperand(0),
3948 DAG.getNode(ISD::FADD, VT, N0.getOperand(1), N1));
3949
Dan Gohman475871a2008-07-27 21:46:04 +00003950 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00003951}
3952
Dan Gohman475871a2008-07-27 21:46:04 +00003953SDValue DAGCombiner::visitFSUB(SDNode *N) {
3954 SDValue N0 = N->getOperand(0);
3955 SDValue N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003956 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3957 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003958 MVT VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00003959
Dan Gohman7f321562007-06-25 16:23:39 +00003960 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003961 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003962 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003963 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00003964 }
Dan Gohman7f321562007-06-25 16:23:39 +00003965
Nate Begemana0e221d2005-10-18 00:28:13 +00003966 // fold (fsub c1, c2) -> c1-c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003967 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003968 return DAG.getNode(ISD::FSUB, VT, N0, N1);
Dan Gohmana90c8e62009-01-23 19:10:37 +00003969 // fold (A-0) -> A
3970 if (UnsafeFPMath && N1CFP && N1CFP->getValueAPF().isZero())
3971 return N0;
Dan Gohman23ff1822007-07-02 15:48:56 +00003972 // fold (0-B) -> -B
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003973 if (UnsafeFPMath && N0CFP && N0CFP->getValueAPF().isZero()) {
Duncan Sands25cf2272008-11-24 14:53:14 +00003974 if (isNegatibleForFree(N1, LegalOperations))
3975 return GetNegatedExpression(N1, DAG, LegalOperations);
Dan Gohman760f86f2009-01-22 21:58:43 +00003976 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
3977 return DAG.getNode(ISD::FNEG, VT, N1);
Dan Gohman23ff1822007-07-02 15:48:56 +00003978 }
Chris Lattner01b3d732005-09-28 22:28:18 +00003979 // fold (A-(-B)) -> A+B
Duncan Sands25cf2272008-11-24 14:53:14 +00003980 if (isNegatibleForFree(N1, LegalOperations))
Chris Lattner0254e702008-02-26 07:04:54 +00003981 return DAG.getNode(ISD::FADD, VT, N0,
Duncan Sands25cf2272008-11-24 14:53:14 +00003982 GetNegatedExpression(N1, DAG, LegalOperations));
Chris Lattner29446522007-05-14 22:04:50 +00003983
Dan Gohman475871a2008-07-27 21:46:04 +00003984 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00003985}
3986
Dan Gohman475871a2008-07-27 21:46:04 +00003987SDValue DAGCombiner::visitFMUL(SDNode *N) {
3988 SDValue N0 = N->getOperand(0);
3989 SDValue N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003990 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3991 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003992 MVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00003993
Dan Gohman7f321562007-06-25 16:23:39 +00003994 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003995 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003996 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003997 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00003998 }
Dan Gohman7f321562007-06-25 16:23:39 +00003999
Nate Begeman11af4ea2005-10-17 20:40:11 +00004000 // fold (fmul c1, c2) -> c1*c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00004001 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00004002 return DAG.getNode(ISD::FMUL, VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00004003 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00004004 if (N0CFP && !N1CFP)
4005 return DAG.getNode(ISD::FMUL, VT, N1, N0);
Dan Gohman760f86f2009-01-22 21:58:43 +00004006 // fold (A * 0) -> 0
4007 if (UnsafeFPMath && N1CFP && N1CFP->getValueAPF().isZero())
4008 return N1;
Nate Begeman11af4ea2005-10-17 20:40:11 +00004009 // fold (fmul X, 2.0) -> (fadd X, X)
4010 if (N1CFP && N1CFP->isExactlyValue(+2.0))
4011 return DAG.getNode(ISD::FADD, VT, N0, N0);
Chris Lattner29446522007-05-14 22:04:50 +00004012 // fold (fmul X, -1.0) -> (fneg X)
4013 if (N1CFP && N1CFP->isExactlyValue(-1.0))
Dan Gohman760f86f2009-01-22 21:58:43 +00004014 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
4015 return DAG.getNode(ISD::FNEG, VT, N0);
Chris Lattner29446522007-05-14 22:04:50 +00004016
4017 // -X * -Y -> X*Y
Duncan Sands25cf2272008-11-24 14:53:14 +00004018 if (char LHSNeg = isNegatibleForFree(N0, LegalOperations)) {
4019 if (char RHSNeg = isNegatibleForFree(N1, LegalOperations)) {
Chris Lattner29446522007-05-14 22:04:50 +00004020 // Both can be negated for free, check to see if at least one is cheaper
4021 // negated.
4022 if (LHSNeg == 2 || RHSNeg == 2)
Chris Lattner0254e702008-02-26 07:04:54 +00004023 return DAG.getNode(ISD::FMUL, VT,
Duncan Sands25cf2272008-11-24 14:53:14 +00004024 GetNegatedExpression(N0, DAG, LegalOperations),
4025 GetNegatedExpression(N1, DAG, LegalOperations));
Chris Lattner29446522007-05-14 22:04:50 +00004026 }
4027 }
Chris Lattnerddae4bd2007-01-08 23:04:05 +00004028
4029 // If allowed, fold (fmul (fmul x, c1), c2) -> (fmul x, (fmul c1, c2))
4030 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FMUL &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004031 N0.getNode()->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
Chris Lattnerddae4bd2007-01-08 23:04:05 +00004032 return DAG.getNode(ISD::FMUL, VT, N0.getOperand(0),
4033 DAG.getNode(ISD::FMUL, VT, N0.getOperand(1), N1));
4034
Dan Gohman475871a2008-07-27 21:46:04 +00004035 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00004036}
4037
Dan Gohman475871a2008-07-27 21:46:04 +00004038SDValue DAGCombiner::visitFDIV(SDNode *N) {
4039 SDValue N0 = N->getOperand(0);
4040 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00004041 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
4042 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004043 MVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00004044
Dan Gohman7f321562007-06-25 16:23:39 +00004045 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00004046 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004047 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00004048 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00004049 }
Dan Gohman7f321562007-06-25 16:23:39 +00004050
Nate Begemana148d982006-01-18 22:35:16 +00004051 // fold (fdiv c1, c2) -> c1/c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00004052 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00004053 return DAG.getNode(ISD::FDIV, VT, N0, N1);
Chris Lattner29446522007-05-14 22:04:50 +00004054
4055
4056 // -X / -Y -> X*Y
Duncan Sands25cf2272008-11-24 14:53:14 +00004057 if (char LHSNeg = isNegatibleForFree(N0, LegalOperations)) {
4058 if (char RHSNeg = isNegatibleForFree(N1, LegalOperations)) {
Chris Lattner29446522007-05-14 22:04:50 +00004059 // Both can be negated for free, check to see if at least one is cheaper
4060 // negated.
4061 if (LHSNeg == 2 || RHSNeg == 2)
Chris Lattner0254e702008-02-26 07:04:54 +00004062 return DAG.getNode(ISD::FDIV, VT,
Duncan Sands25cf2272008-11-24 14:53:14 +00004063 GetNegatedExpression(N0, DAG, LegalOperations),
4064 GetNegatedExpression(N1, DAG, LegalOperations));
Chris Lattner29446522007-05-14 22:04:50 +00004065 }
4066 }
4067
Dan Gohman475871a2008-07-27 21:46:04 +00004068 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00004069}
4070
Dan Gohman475871a2008-07-27 21:46:04 +00004071SDValue DAGCombiner::visitFREM(SDNode *N) {
4072 SDValue N0 = N->getOperand(0);
4073 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00004074 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
4075 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004076 MVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00004077
Nate Begemana148d982006-01-18 22:35:16 +00004078 // fold (frem c1, c2) -> fmod(c1,c2)
Dale Johannesendb44bf82007-10-16 23:38:29 +00004079 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00004080 return DAG.getNode(ISD::FREM, VT, N0, N1);
Dan Gohman7f321562007-06-25 16:23:39 +00004081
Dan Gohman475871a2008-07-27 21:46:04 +00004082 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00004083}
4084
Dan Gohman475871a2008-07-27 21:46:04 +00004085SDValue DAGCombiner::visitFCOPYSIGN(SDNode *N) {
4086 SDValue N0 = N->getOperand(0);
4087 SDValue N1 = N->getOperand(1);
Chris Lattner12d83032006-03-05 05:30:57 +00004088 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
4089 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004090 MVT VT = N->getValueType(0);
Chris Lattner12d83032006-03-05 05:30:57 +00004091
Dale Johannesendb44bf82007-10-16 23:38:29 +00004092 if (N0CFP && N1CFP && VT != MVT::ppcf128) // Constant fold
Chris Lattner12d83032006-03-05 05:30:57 +00004093 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1);
4094
4095 if (N1CFP) {
Dale Johannesene6c17422007-08-26 01:18:27 +00004096 const APFloat& V = N1CFP->getValueAPF();
Chris Lattner12d83032006-03-05 05:30:57 +00004097 // copysign(x, c1) -> fabs(x) iff ispos(c1)
4098 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
Dan Gohman760f86f2009-01-22 21:58:43 +00004099 if (!V.isNegative()) {
4100 if (!LegalOperations || TLI.isOperationLegal(ISD::FABS, VT))
4101 return DAG.getNode(ISD::FABS, VT, N0);
4102 } else {
4103 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
4104 return DAG.getNode(ISD::FNEG, VT, DAG.getNode(ISD::FABS, VT, N0));
4105 }
Chris Lattner12d83032006-03-05 05:30:57 +00004106 }
4107
4108 // copysign(fabs(x), y) -> copysign(x, y)
4109 // copysign(fneg(x), y) -> copysign(x, y)
4110 // copysign(copysign(x,z), y) -> copysign(x, y)
4111 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
4112 N0.getOpcode() == ISD::FCOPYSIGN)
4113 return DAG.getNode(ISD::FCOPYSIGN, VT, N0.getOperand(0), N1);
4114
4115 // copysign(x, abs(y)) -> abs(x)
4116 if (N1.getOpcode() == ISD::FABS)
4117 return DAG.getNode(ISD::FABS, VT, N0);
4118
4119 // copysign(x, copysign(y,z)) -> copysign(x, z)
4120 if (N1.getOpcode() == ISD::FCOPYSIGN)
4121 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(1));
4122
4123 // copysign(x, fp_extend(y)) -> copysign(x, y)
4124 // copysign(x, fp_round(y)) -> copysign(x, y)
4125 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
4126 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(0));
4127
Dan Gohman475871a2008-07-27 21:46:04 +00004128 return SDValue();
Chris Lattner12d83032006-03-05 05:30:57 +00004129}
4130
4131
Chris Lattner01b3d732005-09-28 22:28:18 +00004132
Dan Gohman475871a2008-07-27 21:46:04 +00004133SDValue DAGCombiner::visitSINT_TO_FP(SDNode *N) {
4134 SDValue N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00004135 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004136 MVT VT = N->getValueType(0);
Chris Lattnercda88752008-06-26 00:16:49 +00004137 MVT OpVT = N0.getValueType();
4138
Nate Begeman1d4d4142005-09-01 00:19:25 +00004139 // fold (sint_to_fp c1) -> c1fp
Chris Lattnercda88752008-06-26 00:16:49 +00004140 if (N0C && OpVT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00004141 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
Chris Lattnercda88752008-06-26 00:16:49 +00004142
4143 // If the input is a legal type, and SINT_TO_FP is not legal on this target,
4144 // but UINT_TO_FP is legal on this target, try to convert.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00004145 if (!TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, OpVT) &&
4146 TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, OpVT)) {
Chris Lattnercda88752008-06-26 00:16:49 +00004147 // If the sign bit is known to be zero, we can change this to UINT_TO_FP.
4148 if (DAG.SignBitIsZero(N0))
4149 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
4150 }
4151
4152
Dan Gohman475871a2008-07-27 21:46:04 +00004153 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004154}
4155
Dan Gohman475871a2008-07-27 21:46:04 +00004156SDValue DAGCombiner::visitUINT_TO_FP(SDNode *N) {
4157 SDValue N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00004158 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004159 MVT VT = N->getValueType(0);
Chris Lattnercda88752008-06-26 00:16:49 +00004160 MVT OpVT = N0.getValueType();
Nate Begemana148d982006-01-18 22:35:16 +00004161
Nate Begeman1d4d4142005-09-01 00:19:25 +00004162 // fold (uint_to_fp c1) -> c1fp
Chris Lattnercda88752008-06-26 00:16:49 +00004163 if (N0C && OpVT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00004164 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
Chris Lattnercda88752008-06-26 00:16:49 +00004165
4166 // If the input is a legal type, and UINT_TO_FP is not legal on this target,
4167 // but SINT_TO_FP is legal on this target, try to convert.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00004168 if (!TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, OpVT) &&
4169 TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, OpVT)) {
Chris Lattnercda88752008-06-26 00:16:49 +00004170 // If the sign bit is known to be zero, we can change this to SINT_TO_FP.
4171 if (DAG.SignBitIsZero(N0))
4172 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
4173 }
4174
Dan Gohman475871a2008-07-27 21:46:04 +00004175 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004176}
4177
Dan Gohman475871a2008-07-27 21:46:04 +00004178SDValue DAGCombiner::visitFP_TO_SINT(SDNode *N) {
4179 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00004180 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004181 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004182
4183 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00004184 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00004185 return DAG.getNode(ISD::FP_TO_SINT, VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00004186 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004187}
4188
Dan Gohman475871a2008-07-27 21:46:04 +00004189SDValue DAGCombiner::visitFP_TO_UINT(SDNode *N) {
4190 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00004191 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004192 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004193
4194 // fold (fp_to_uint c1fp) -> c1
Dale Johannesendb44bf82007-10-16 23:38:29 +00004195 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00004196 return DAG.getNode(ISD::FP_TO_UINT, VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00004197 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004198}
4199
Dan Gohman475871a2008-07-27 21:46:04 +00004200SDValue DAGCombiner::visitFP_ROUND(SDNode *N) {
4201 SDValue N0 = N->getOperand(0);
4202 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00004203 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004204 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004205
4206 // fold (fp_round c1fp) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00004207 if (N0CFP && N0.getValueType() != MVT::ppcf128)
Chris Lattner0bd48932008-01-17 07:00:52 +00004208 return DAG.getNode(ISD::FP_ROUND, VT, N0, N1);
Chris Lattner79dbea52006-03-13 06:26:26 +00004209
4210 // fold (fp_round (fp_extend x)) -> x
4211 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
4212 return N0.getOperand(0);
4213
Chris Lattner0aa5e6f2008-01-24 06:45:35 +00004214 // fold (fp_round (fp_round x)) -> (fp_round x)
4215 if (N0.getOpcode() == ISD::FP_ROUND) {
4216 // This is a value preserving truncation if both round's are.
4217 bool IsTrunc = N->getConstantOperandVal(1) == 1 &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004218 N0.getNode()->getConstantOperandVal(1) == 1;
Chris Lattner0aa5e6f2008-01-24 06:45:35 +00004219 return DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0),
4220 DAG.getIntPtrConstant(IsTrunc));
4221 }
4222
Chris Lattner79dbea52006-03-13 06:26:26 +00004223 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
Gabor Greifba36cb52008-08-28 21:40:38 +00004224 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004225 SDValue Tmp = DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0), N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00004226 AddToWorkList(Tmp.getNode());
Chris Lattner79dbea52006-03-13 06:26:26 +00004227 return DAG.getNode(ISD::FCOPYSIGN, VT, Tmp, N0.getOperand(1));
4228 }
4229
Dan Gohman475871a2008-07-27 21:46:04 +00004230 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004231}
4232
Dan Gohman475871a2008-07-27 21:46:04 +00004233SDValue DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
4234 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004235 MVT VT = N->getValueType(0);
4236 MVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00004237 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004238
Nate Begeman1d4d4142005-09-01 00:19:25 +00004239 // fold (fp_round_inreg c1fp) -> c1fp
Duncan Sands25cf2272008-11-24 14:53:14 +00004240 if (N0CFP && (TLI.isTypeLegal(EVT) || !LegalTypes)) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00004241 SDValue Round = DAG.getConstantFP(*N0CFP->getConstantFPValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00004242 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004243 }
Dan Gohman475871a2008-07-27 21:46:04 +00004244 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004245}
4246
Dan Gohman475871a2008-07-27 21:46:04 +00004247SDValue DAGCombiner::visitFP_EXTEND(SDNode *N) {
4248 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00004249 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004250 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004251
Chris Lattner5938bef2007-12-29 06:55:23 +00004252 // If this is fp_round(fpextend), don't fold it, allow ourselves to be folded.
Roman Levenstein9cac5252008-04-16 16:15:27 +00004253 if (N->hasOneUse() &&
Dan Gohmane7852d02009-01-26 04:35:06 +00004254 N->use_begin()->getOpcode() == ISD::FP_ROUND)
Dan Gohman475871a2008-07-27 21:46:04 +00004255 return SDValue();
Chris Lattner0bd48932008-01-17 07:00:52 +00004256
Nate Begeman1d4d4142005-09-01 00:19:25 +00004257 // fold (fp_extend c1fp) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00004258 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00004259 return DAG.getNode(ISD::FP_EXTEND, VT, N0);
Chris Lattner0bd48932008-01-17 07:00:52 +00004260
4261 // Turn fp_extend(fp_round(X, 1)) -> x since the fp_round doesn't affect the
4262 // value of X.
Gabor Greif12632d22008-08-30 19:29:20 +00004263 if (N0.getOpcode() == ISD::FP_ROUND
4264 && N0.getNode()->getConstantOperandVal(1) == 1) {
Dan Gohman475871a2008-07-27 21:46:04 +00004265 SDValue In = N0.getOperand(0);
Chris Lattner0bd48932008-01-17 07:00:52 +00004266 if (In.getValueType() == VT) return In;
Duncan Sands8e4eb092008-06-08 20:54:56 +00004267 if (VT.bitsLT(In.getValueType()))
Chris Lattner0bd48932008-01-17 07:00:52 +00004268 return DAG.getNode(ISD::FP_ROUND, VT, In, N0.getOperand(1));
4269 return DAG.getNode(ISD::FP_EXTEND, VT, In);
4270 }
4271
4272 // fold (fpext (load x)) -> (fpext (fptrunc (extload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00004273 if (ISD::isNON_EXTLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00004274 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00004275 TLI.isLoadExtLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00004276 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman475871a2008-07-27 21:46:04 +00004277 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00004278 LN0->getBasePtr(), LN0->getSrcValue(),
4279 LN0->getSrcValueOffset(),
4280 N0.getValueType(),
4281 LN0->isVolatile(), LN0->getAlignment());
Chris Lattnere564dbb2006-05-05 21:34:35 +00004282 CombineTo(N, ExtLoad);
Gabor Greif12632d22008-08-30 19:29:20 +00004283 CombineTo(N0.getNode(), DAG.getNode(ISD::FP_ROUND, N0.getValueType(),
4284 ExtLoad, DAG.getIntPtrConstant(1)),
Chris Lattnere564dbb2006-05-05 21:34:35 +00004285 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004286 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnere564dbb2006-05-05 21:34:35 +00004287 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004288
Dan Gohman475871a2008-07-27 21:46:04 +00004289 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004290}
4291
Dan Gohman475871a2008-07-27 21:46:04 +00004292SDValue DAGCombiner::visitFNEG(SDNode *N) {
4293 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00004294
Duncan Sands25cf2272008-11-24 14:53:14 +00004295 if (isNegatibleForFree(N0, LegalOperations))
4296 return GetNegatedExpression(N0, DAG, LegalOperations);
Dan Gohman23ff1822007-07-02 15:48:56 +00004297
Chris Lattner3bd39d42008-01-27 17:42:27 +00004298 // Transform fneg(bitconvert(x)) -> bitconvert(x^sign) to avoid loading
4299 // constant pool values.
Gabor Greifba36cb52008-08-28 21:40:38 +00004300 if (N0.getOpcode() == ISD::BIT_CONVERT && N0.getNode()->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00004301 N0.getOperand(0).getValueType().isInteger() &&
4302 !N0.getOperand(0).getValueType().isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004303 SDValue Int = N0.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004304 MVT IntVT = Int.getValueType();
4305 if (IntVT.isInteger() && !IntVT.isVector()) {
Chris Lattner3bd39d42008-01-27 17:42:27 +00004306 Int = DAG.getNode(ISD::XOR, IntVT, Int,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004307 DAG.getConstant(IntVT.getIntegerVTSignBit(), IntVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00004308 AddToWorkList(Int.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00004309 return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Int);
4310 }
4311 }
4312
Dan Gohman475871a2008-07-27 21:46:04 +00004313 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004314}
4315
Dan Gohman475871a2008-07-27 21:46:04 +00004316SDValue DAGCombiner::visitFABS(SDNode *N) {
4317 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00004318 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004319 MVT VT = N->getValueType(0);
Nate Begemana148d982006-01-18 22:35:16 +00004320
Nate Begeman1d4d4142005-09-01 00:19:25 +00004321 // fold (fabs c1) -> fabs(c1)
Dale Johannesendb44bf82007-10-16 23:38:29 +00004322 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00004323 return DAG.getNode(ISD::FABS, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004324 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00004325 if (N0.getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00004326 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004327 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00004328 // fold (fabs (fcopysign x, y)) -> (fabs x)
4329 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
4330 return DAG.getNode(ISD::FABS, VT, N0.getOperand(0));
4331
Chris Lattner3bd39d42008-01-27 17:42:27 +00004332 // Transform fabs(bitconvert(x)) -> bitconvert(x&~sign) to avoid loading
4333 // constant pool values.
Gabor Greifba36cb52008-08-28 21:40:38 +00004334 if (N0.getOpcode() == ISD::BIT_CONVERT && N0.getNode()->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00004335 N0.getOperand(0).getValueType().isInteger() &&
4336 !N0.getOperand(0).getValueType().isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004337 SDValue Int = N0.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004338 MVT IntVT = Int.getValueType();
4339 if (IntVT.isInteger() && !IntVT.isVector()) {
Chris Lattner3bd39d42008-01-27 17:42:27 +00004340 Int = DAG.getNode(ISD::AND, IntVT, Int,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004341 DAG.getConstant(~IntVT.getIntegerVTSignBit(), IntVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00004342 AddToWorkList(Int.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00004343 return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Int);
4344 }
4345 }
4346
Dan Gohman475871a2008-07-27 21:46:04 +00004347 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004348}
4349
Dan Gohman475871a2008-07-27 21:46:04 +00004350SDValue DAGCombiner::visitBRCOND(SDNode *N) {
4351 SDValue Chain = N->getOperand(0);
4352 SDValue N1 = N->getOperand(1);
4353 SDValue N2 = N->getOperand(2);
Nate Begeman44728a72005-09-19 22:34:01 +00004354 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
4355
4356 // never taken branch, fold to chain
4357 if (N1C && N1C->isNullValue())
4358 return Chain;
4359 // unconditional branch
Dan Gohman002e5d02008-03-13 22:13:53 +00004360 if (N1C && N1C->getAPIntValue() == 1)
Nate Begeman44728a72005-09-19 22:34:01 +00004361 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
Nate Begeman750ac1b2006-02-01 07:19:44 +00004362 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
4363 // on the target.
4364 if (N1.getOpcode() == ISD::SETCC &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00004365 TLI.isOperationLegalOrCustom(ISD::BR_CC, MVT::Other)) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00004366 return DAG.getNode(ISD::BR_CC, MVT::Other, Chain, N1.getOperand(2),
4367 N1.getOperand(0), N1.getOperand(1), N2);
4368 }
Dan Gohman475871a2008-07-27 21:46:04 +00004369 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00004370}
4371
Chris Lattner3ea0b472005-10-05 06:47:48 +00004372// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
4373//
Dan Gohman475871a2008-07-27 21:46:04 +00004374SDValue DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00004375 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004376 SDValue CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
Chris Lattner3ea0b472005-10-05 06:47:48 +00004377
Duncan Sands8eab8a22008-06-09 11:32:28 +00004378 // Use SimplifySetCC to simplify SETCC's.
Duncan Sands5480c042009-01-01 15:52:00 +00004379 SDValue Simp = SimplifySetCC(TLI.getSetCCResultType(CondLHS.getValueType()),
Duncan Sands25cf2272008-11-24 14:53:14 +00004380 CondLHS, CondRHS, CC->get(), false);
Gabor Greifba36cb52008-08-28 21:40:38 +00004381 if (Simp.getNode()) AddToWorkList(Simp.getNode());
Chris Lattner30f73e72006-10-14 03:52:46 +00004382
Gabor Greifba36cb52008-08-28 21:40:38 +00004383 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.getNode());
Nate Begemane17daeb2005-10-05 21:43:42 +00004384
4385 // fold br_cc true, dest -> br dest (unconditional branch)
Dan Gohman002e5d02008-03-13 22:13:53 +00004386 if (SCCC && !SCCC->isNullValue())
Nate Begemane17daeb2005-10-05 21:43:42 +00004387 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
4388 N->getOperand(4));
4389 // fold br_cc false, dest -> unconditional fall through
4390 if (SCCC && SCCC->isNullValue())
4391 return N->getOperand(0);
Chris Lattner30f73e72006-10-14 03:52:46 +00004392
Nate Begemane17daeb2005-10-05 21:43:42 +00004393 // fold to a simpler setcc
Gabor Greifba36cb52008-08-28 21:40:38 +00004394 if (Simp.getNode() && Simp.getOpcode() == ISD::SETCC)
Nate Begemane17daeb2005-10-05 21:43:42 +00004395 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
4396 Simp.getOperand(2), Simp.getOperand(0),
4397 Simp.getOperand(1), N->getOperand(4));
Dan Gohman475871a2008-07-27 21:46:04 +00004398 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00004399}
4400
Chris Lattner448f2192006-11-11 00:39:41 +00004401
Duncan Sandsec87aa82008-06-15 20:12:31 +00004402/// CombineToPreIndexedLoadStore - Try turning a load / store into a
4403/// pre-indexed load / store when the base pointer is an add or subtract
Chris Lattner448f2192006-11-11 00:39:41 +00004404/// and it has other uses besides the load / store. After the
4405/// transformation, the new indexed load / store has effectively folded
4406/// the add / subtract in and all of its other uses are redirected to the
4407/// new load / store.
4408bool DAGCombiner::CombineToPreIndexedLoadStore(SDNode *N) {
Duncan Sands25cf2272008-11-24 14:53:14 +00004409 if (!LegalOperations)
Chris Lattner448f2192006-11-11 00:39:41 +00004410 return false;
4411
4412 bool isLoad = true;
Dan Gohman475871a2008-07-27 21:46:04 +00004413 SDValue Ptr;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004414 MVT VT;
Chris Lattner448f2192006-11-11 00:39:41 +00004415 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004416 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004417 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004418 VT = LD->getMemoryVT();
Evan Cheng83060c52007-03-07 08:07:03 +00004419 if (!TLI.isIndexedLoadLegal(ISD::PRE_INC, VT) &&
Chris Lattner448f2192006-11-11 00:39:41 +00004420 !TLI.isIndexedLoadLegal(ISD::PRE_DEC, VT))
4421 return false;
4422 Ptr = LD->getBasePtr();
4423 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004424 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004425 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004426 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00004427 if (!TLI.isIndexedStoreLegal(ISD::PRE_INC, VT) &&
4428 !TLI.isIndexedStoreLegal(ISD::PRE_DEC, VT))
4429 return false;
4430 Ptr = ST->getBasePtr();
4431 isLoad = false;
4432 } else
4433 return false;
4434
Chris Lattner9f1794e2006-11-11 00:56:29 +00004435 // If the pointer is not an add/sub, or if it doesn't have multiple uses, bail
4436 // out. There is no reason to make this a preinc/predec.
4437 if ((Ptr.getOpcode() != ISD::ADD && Ptr.getOpcode() != ISD::SUB) ||
Gabor Greifba36cb52008-08-28 21:40:38 +00004438 Ptr.getNode()->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00004439 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00004440
Chris Lattner9f1794e2006-11-11 00:56:29 +00004441 // Ask the target to do addressing mode selection.
Dan Gohman475871a2008-07-27 21:46:04 +00004442 SDValue BasePtr;
4443 SDValue Offset;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004444 ISD::MemIndexedMode AM = ISD::UNINDEXED;
4445 if (!TLI.getPreIndexedAddressParts(N, BasePtr, Offset, AM, DAG))
4446 return false;
Evan Chenga7d4a042007-05-03 23:52:19 +00004447 // Don't create a indexed load / store with zero offset.
4448 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00004449 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Chenga7d4a042007-05-03 23:52:19 +00004450 return false;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004451
Chris Lattner41e53fd2006-11-11 01:00:15 +00004452 // Try turning it into a pre-indexed load / store except when:
Evan Chengc843abe2007-05-24 02:35:39 +00004453 // 1) The new base ptr is a frame index.
4454 // 2) If N is a store and the new base ptr is either the same as or is a
Chris Lattner9f1794e2006-11-11 00:56:29 +00004455 // predecessor of the value being stored.
Evan Chengc843abe2007-05-24 02:35:39 +00004456 // 3) Another use of old base ptr is a predecessor of N. If ptr is folded
Chris Lattner9f1794e2006-11-11 00:56:29 +00004457 // that would create a cycle.
Evan Chengc843abe2007-05-24 02:35:39 +00004458 // 4) All uses are load / store ops that use it as old base ptr.
Chris Lattner448f2192006-11-11 00:39:41 +00004459
Chris Lattner41e53fd2006-11-11 01:00:15 +00004460 // Check #1. Preinc'ing a frame index would require copying the stack pointer
4461 // (plus the implicit offset) to a register to preinc anyway.
4462 if (isa<FrameIndexSDNode>(BasePtr))
4463 return false;
4464
4465 // Check #2.
Chris Lattner9f1794e2006-11-11 00:56:29 +00004466 if (!isLoad) {
Dan Gohman475871a2008-07-27 21:46:04 +00004467 SDValue Val = cast<StoreSDNode>(N)->getValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00004468 if (Val == BasePtr || BasePtr.getNode()->isPredecessorOf(Val.getNode()))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004469 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00004470 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004471
Evan Chengc843abe2007-05-24 02:35:39 +00004472 // Now check for #3 and #4.
Chris Lattner9f1794e2006-11-11 00:56:29 +00004473 bool RealUse = false;
Gabor Greifba36cb52008-08-28 21:40:38 +00004474 for (SDNode::use_iterator I = Ptr.getNode()->use_begin(),
4475 E = Ptr.getNode()->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00004476 SDNode *Use = *I;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004477 if (Use == N)
4478 continue;
Evan Cheng917be682008-03-04 00:41:45 +00004479 if (Use->isPredecessorOf(N))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004480 return false;
4481
4482 if (!((Use->getOpcode() == ISD::LOAD &&
4483 cast<LoadSDNode>(Use)->getBasePtr() == Ptr) ||
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00004484 (Use->getOpcode() == ISD::STORE &&
4485 cast<StoreSDNode>(Use)->getBasePtr() == Ptr)))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004486 RealUse = true;
4487 }
4488 if (!RealUse)
4489 return false;
4490
Dan Gohman475871a2008-07-27 21:46:04 +00004491 SDValue Result;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004492 if (isLoad)
Dan Gohman475871a2008-07-27 21:46:04 +00004493 Result = DAG.getIndexedLoad(SDValue(N,0), BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004494 else
Dan Gohman475871a2008-07-27 21:46:04 +00004495 Result = DAG.getIndexedStore(SDValue(N,0), BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004496 ++PreIndexedNodes;
4497 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +00004498 DOUT << "\nReplacing.4 "; DEBUG(N->dump(&DAG));
Gabor Greifba36cb52008-08-28 21:40:38 +00004499 DOUT << "\nWith: "; DEBUG(Result.getNode()->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +00004500 DOUT << '\n';
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004501 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004502 if (isLoad) {
Dan Gohman475871a2008-07-27 21:46:04 +00004503 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004504 &DeadNodes);
Dan Gohman475871a2008-07-27 21:46:04 +00004505 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004506 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004507 } else {
Dan Gohman475871a2008-07-27 21:46:04 +00004508 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004509 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004510 }
4511
Chris Lattner9f1794e2006-11-11 00:56:29 +00004512 // Finally, since the node is now dead, remove it from the graph.
4513 DAG.DeleteNode(N);
4514
4515 // Replace the uses of Ptr with uses of the updated base value.
4516 DAG.ReplaceAllUsesOfValueWith(Ptr, Result.getValue(isLoad ? 1 : 0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004517 &DeadNodes);
Gabor Greifba36cb52008-08-28 21:40:38 +00004518 removeFromWorkList(Ptr.getNode());
4519 DAG.DeleteNode(Ptr.getNode());
Chris Lattner9f1794e2006-11-11 00:56:29 +00004520
4521 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00004522}
4523
Duncan Sandsec87aa82008-06-15 20:12:31 +00004524/// CombineToPostIndexedLoadStore - Try to combine a load / store with a
Chris Lattner448f2192006-11-11 00:39:41 +00004525/// add / sub of the base pointer node into a post-indexed load / store.
4526/// The transformation folded the add / subtract into the new indexed
4527/// load / store effectively and all of its uses are redirected to the
4528/// new load / store.
4529bool DAGCombiner::CombineToPostIndexedLoadStore(SDNode *N) {
Duncan Sands25cf2272008-11-24 14:53:14 +00004530 if (!LegalOperations)
Chris Lattner448f2192006-11-11 00:39:41 +00004531 return false;
4532
4533 bool isLoad = true;
Dan Gohman475871a2008-07-27 21:46:04 +00004534 SDValue Ptr;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004535 MVT VT;
Chris Lattner448f2192006-11-11 00:39:41 +00004536 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004537 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004538 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004539 VT = LD->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00004540 if (!TLI.isIndexedLoadLegal(ISD::POST_INC, VT) &&
4541 !TLI.isIndexedLoadLegal(ISD::POST_DEC, VT))
4542 return false;
4543 Ptr = LD->getBasePtr();
4544 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004545 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004546 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004547 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00004548 if (!TLI.isIndexedStoreLegal(ISD::POST_INC, VT) &&
4549 !TLI.isIndexedStoreLegal(ISD::POST_DEC, VT))
4550 return false;
4551 Ptr = ST->getBasePtr();
4552 isLoad = false;
4553 } else
4554 return false;
4555
Gabor Greifba36cb52008-08-28 21:40:38 +00004556 if (Ptr.getNode()->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00004557 return false;
4558
Gabor Greifba36cb52008-08-28 21:40:38 +00004559 for (SDNode::use_iterator I = Ptr.getNode()->use_begin(),
4560 E = Ptr.getNode()->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00004561 SDNode *Op = *I;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004562 if (Op == N ||
4563 (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB))
4564 continue;
4565
Dan Gohman475871a2008-07-27 21:46:04 +00004566 SDValue BasePtr;
4567 SDValue Offset;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004568 ISD::MemIndexedMode AM = ISD::UNINDEXED;
4569 if (TLI.getPostIndexedAddressParts(N, Op, BasePtr, Offset, AM, DAG)) {
4570 if (Ptr == Offset)
4571 std::swap(BasePtr, Offset);
4572 if (Ptr != BasePtr)
Chris Lattner448f2192006-11-11 00:39:41 +00004573 continue;
Evan Chenga7d4a042007-05-03 23:52:19 +00004574 // Don't create a indexed load / store with zero offset.
4575 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00004576 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Chenga7d4a042007-05-03 23:52:19 +00004577 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00004578
Chris Lattner9f1794e2006-11-11 00:56:29 +00004579 // Try turning it into a post-indexed load / store except when
4580 // 1) All uses are load / store ops that use it as base ptr.
4581 // 2) Op must be independent of N, i.e. Op is neither a predecessor
4582 // nor a successor of N. Otherwise, if Op is folded that would
4583 // create a cycle.
4584
4585 // Check for #1.
4586 bool TryNext = false;
Gabor Greifba36cb52008-08-28 21:40:38 +00004587 for (SDNode::use_iterator II = BasePtr.getNode()->use_begin(),
4588 EE = BasePtr.getNode()->use_end(); II != EE; ++II) {
Dan Gohman89684502008-07-27 20:43:25 +00004589 SDNode *Use = *II;
Gabor Greifba36cb52008-08-28 21:40:38 +00004590 if (Use == Ptr.getNode())
Chris Lattner448f2192006-11-11 00:39:41 +00004591 continue;
4592
Chris Lattner9f1794e2006-11-11 00:56:29 +00004593 // If all the uses are load / store addresses, then don't do the
4594 // transformation.
4595 if (Use->getOpcode() == ISD::ADD || Use->getOpcode() == ISD::SUB){
4596 bool RealUse = false;
4597 for (SDNode::use_iterator III = Use->use_begin(),
4598 EEE = Use->use_end(); III != EEE; ++III) {
Dan Gohman89684502008-07-27 20:43:25 +00004599 SDNode *UseUse = *III;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004600 if (!((UseUse->getOpcode() == ISD::LOAD &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004601 cast<LoadSDNode>(UseUse)->getBasePtr().getNode() == Use) ||
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00004602 (UseUse->getOpcode() == ISD::STORE &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004603 cast<StoreSDNode>(UseUse)->getBasePtr().getNode() == Use)))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004604 RealUse = true;
4605 }
Chris Lattner448f2192006-11-11 00:39:41 +00004606
Chris Lattner9f1794e2006-11-11 00:56:29 +00004607 if (!RealUse) {
4608 TryNext = true;
4609 break;
Chris Lattner448f2192006-11-11 00:39:41 +00004610 }
4611 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004612 }
4613 if (TryNext)
4614 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00004615
Chris Lattner9f1794e2006-11-11 00:56:29 +00004616 // Check for #2
Evan Cheng917be682008-03-04 00:41:45 +00004617 if (!Op->isPredecessorOf(N) && !N->isPredecessorOf(Op)) {
Dan Gohman475871a2008-07-27 21:46:04 +00004618 SDValue Result = isLoad
4619 ? DAG.getIndexedLoad(SDValue(N,0), BasePtr, Offset, AM)
4620 : DAG.getIndexedStore(SDValue(N,0), BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004621 ++PostIndexedNodes;
4622 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +00004623 DOUT << "\nReplacing.5 "; DEBUG(N->dump(&DAG));
Gabor Greifba36cb52008-08-28 21:40:38 +00004624 DOUT << "\nWith: "; DEBUG(Result.getNode()->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +00004625 DOUT << '\n';
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004626 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004627 if (isLoad) {
Dan Gohman475871a2008-07-27 21:46:04 +00004628 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004629 &DeadNodes);
Dan Gohman475871a2008-07-27 21:46:04 +00004630 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004631 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004632 } else {
Dan Gohman475871a2008-07-27 21:46:04 +00004633 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004634 &DeadNodes);
Chris Lattner448f2192006-11-11 00:39:41 +00004635 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004636
Chris Lattner9f1794e2006-11-11 00:56:29 +00004637 // Finally, since the node is now dead, remove it from the graph.
4638 DAG.DeleteNode(N);
4639
4640 // Replace the uses of Use with uses of the updated base value.
Dan Gohman475871a2008-07-27 21:46:04 +00004641 DAG.ReplaceAllUsesOfValueWith(SDValue(Op, 0),
Chris Lattner9f1794e2006-11-11 00:56:29 +00004642 Result.getValue(isLoad ? 1 : 0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004643 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004644 removeFromWorkList(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004645 DAG.DeleteNode(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004646 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00004647 }
4648 }
4649 }
4650 return false;
4651}
4652
Chris Lattner00161a62008-01-25 07:20:16 +00004653/// InferAlignment - If we can infer some alignment information from this
4654/// pointer, return it.
Dan Gohman475871a2008-07-27 21:46:04 +00004655static unsigned InferAlignment(SDValue Ptr, SelectionDAG &DAG) {
Chris Lattner00161a62008-01-25 07:20:16 +00004656 // If this is a direct reference to a stack slot, use information about the
4657 // stack slot's alignment.
Chris Lattner1329cb82008-01-26 19:45:50 +00004658 int FrameIdx = 1 << 31;
4659 int64_t FrameOffset = 0;
Chris Lattner00161a62008-01-25 07:20:16 +00004660 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
Chris Lattner1329cb82008-01-26 19:45:50 +00004661 FrameIdx = FI->getIndex();
4662 } else if (Ptr.getOpcode() == ISD::ADD &&
4663 isa<ConstantSDNode>(Ptr.getOperand(1)) &&
4664 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4665 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4666 FrameOffset = Ptr.getConstantOperandVal(1);
Chris Lattner00161a62008-01-25 07:20:16 +00004667 }
Chris Lattner1329cb82008-01-26 19:45:50 +00004668
4669 if (FrameIdx != (1 << 31)) {
4670 // FIXME: Handle FI+CST.
4671 const MachineFrameInfo &MFI = *DAG.getMachineFunction().getFrameInfo();
4672 if (MFI.isFixedObjectIndex(FrameIdx)) {
Dan Gohman8cea8ff2008-08-11 18:27:03 +00004673 int64_t ObjectOffset = MFI.getObjectOffset(FrameIdx) + FrameOffset;
Chris Lattner1329cb82008-01-26 19:45:50 +00004674
4675 // The alignment of the frame index can be determined from its offset from
4676 // the incoming frame position. If the frame object is at offset 32 and
4677 // the stack is guaranteed to be 16-byte aligned, then we know that the
4678 // object is 16-byte aligned.
4679 unsigned StackAlign = DAG.getTarget().getFrameInfo()->getStackAlignment();
4680 unsigned Align = MinAlign(ObjectOffset, StackAlign);
4681
4682 // Finally, the frame object itself may have a known alignment. Factor
4683 // the alignment + offset into a new alignment. For example, if we know
4684 // the FI is 8 byte aligned, but the pointer is 4 off, we really have a
4685 // 4-byte alignment of the resultant pointer. Likewise align 4 + 4-byte
4686 // offset = 4-byte alignment, align 4 + 1-byte offset = align 1, etc.
4687 unsigned FIInfoAlign = MinAlign(MFI.getObjectAlignment(FrameIdx),
4688 FrameOffset);
4689 return std::max(Align, FIInfoAlign);
4690 }
4691 }
Chris Lattner00161a62008-01-25 07:20:16 +00004692
4693 return 0;
4694}
Chris Lattner448f2192006-11-11 00:39:41 +00004695
Dan Gohman475871a2008-07-27 21:46:04 +00004696SDValue DAGCombiner::visitLOAD(SDNode *N) {
Evan Cheng466685d2006-10-09 20:57:25 +00004697 LoadSDNode *LD = cast<LoadSDNode>(N);
Dan Gohman475871a2008-07-27 21:46:04 +00004698 SDValue Chain = LD->getChain();
4699 SDValue Ptr = LD->getBasePtr();
Chris Lattner00161a62008-01-25 07:20:16 +00004700
4701 // Try to infer better alignment information than the load already has.
Dan Gohmana2676512008-08-20 16:30:28 +00004702 if (!Fast && LD->isUnindexed()) {
Chris Lattner00161a62008-01-25 07:20:16 +00004703 if (unsigned Align = InferAlignment(Ptr, DAG)) {
4704 if (Align > LD->getAlignment())
4705 return DAG.getExtLoad(LD->getExtensionType(), LD->getValueType(0),
4706 Chain, Ptr, LD->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004707 LD->getSrcValueOffset(), LD->getMemoryVT(),
Chris Lattner00161a62008-01-25 07:20:16 +00004708 LD->isVolatile(), Align);
4709 }
4710 }
4711
Evan Cheng45a7ca92007-05-01 00:38:21 +00004712
4713 // If load is not volatile and there are no uses of the loaded value (and
4714 // the updated indexed value in case of indexed loads), change uses of the
4715 // chain value into uses of the chain input (i.e. delete the dead load).
4716 if (!LD->isVolatile()) {
Evan Cheng498f5592007-05-01 08:53:39 +00004717 if (N->getValueType(1) == MVT::Other) {
4718 // Unindexed loads.
Evan Cheng02c42852008-01-16 23:11:54 +00004719 if (N->hasNUsesOfValue(0, 0)) {
4720 // It's not safe to use the two value CombineTo variant here. e.g.
4721 // v1, chain2 = load chain1, loc
4722 // v2, chain3 = load chain2, loc
4723 // v3 = add v2, c
Chris Lattner125991a2008-01-24 07:57:06 +00004724 // Now we replace use of chain2 with chain1. This makes the second load
4725 // isomorphic to the one we are deleting, and thus makes this load live.
Evan Cheng02c42852008-01-16 23:11:54 +00004726 DOUT << "\nReplacing.6 "; DEBUG(N->dump(&DAG));
Gabor Greifba36cb52008-08-28 21:40:38 +00004727 DOUT << "\nWith chain: "; DEBUG(Chain.getNode()->dump(&DAG));
Chris Lattner125991a2008-01-24 07:57:06 +00004728 DOUT << "\n";
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004729 WorkListRemover DeadNodes(*this);
Dan Gohman475871a2008-07-27 21:46:04 +00004730 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Chain, &DeadNodes);
Chris Lattner125991a2008-01-24 07:57:06 +00004731 if (N->use_empty()) {
4732 removeFromWorkList(N);
4733 DAG.DeleteNode(N);
4734 }
Dan Gohman475871a2008-07-27 21:46:04 +00004735 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng02c42852008-01-16 23:11:54 +00004736 }
Evan Cheng498f5592007-05-01 08:53:39 +00004737 } else {
4738 // Indexed loads.
4739 assert(N->getValueType(2) == MVT::Other && "Malformed indexed loads?");
4740 if (N->hasNUsesOfValue(0, 0) && N->hasNUsesOfValue(0, 1)) {
Dan Gohman475871a2008-07-27 21:46:04 +00004741 SDValue Undef = DAG.getNode(ISD::UNDEF, N->getValueType(0));
Evan Cheng02c42852008-01-16 23:11:54 +00004742 DOUT << "\nReplacing.6 "; DEBUG(N->dump(&DAG));
Gabor Greifba36cb52008-08-28 21:40:38 +00004743 DOUT << "\nWith: "; DEBUG(Undef.getNode()->dump(&DAG));
Evan Cheng02c42852008-01-16 23:11:54 +00004744 DOUT << " and 2 other values\n";
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004745 WorkListRemover DeadNodes(*this);
Dan Gohman475871a2008-07-27 21:46:04 +00004746 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Undef, &DeadNodes);
4747 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1),
Chris Lattner4626b252008-01-17 07:20:38 +00004748 DAG.getNode(ISD::UNDEF, N->getValueType(1)),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004749 &DeadNodes);
Dan Gohman475871a2008-07-27 21:46:04 +00004750 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 2), Chain, &DeadNodes);
Evan Cheng02c42852008-01-16 23:11:54 +00004751 removeFromWorkList(N);
Evan Cheng02c42852008-01-16 23:11:54 +00004752 DAG.DeleteNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +00004753 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng45a7ca92007-05-01 00:38:21 +00004754 }
Evan Cheng45a7ca92007-05-01 00:38:21 +00004755 }
4756 }
Chris Lattner01a22022005-10-10 22:04:48 +00004757
4758 // If this load is directly stored, replace the load value with the stored
4759 // value.
4760 // TODO: Handle store large -> read small portion.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004761 // TODO: Handle TRUNCSTORE/LOADEXT
Dan Gohmanb061c4b2008-03-31 20:32:52 +00004762 if (LD->getExtensionType() == ISD::NON_EXTLOAD &&
4763 !LD->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004764 if (ISD::isNON_TRUNCStore(Chain.getNode())) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00004765 StoreSDNode *PrevST = cast<StoreSDNode>(Chain);
4766 if (PrevST->getBasePtr() == Ptr &&
4767 PrevST->getValue().getValueType() == N->getValueType(0))
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004768 return CombineTo(N, Chain.getOperand(1), Chain);
Evan Cheng8b2794a2006-10-13 21:14:26 +00004769 }
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004770 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00004771
Jim Laskey7ca56af2006-10-11 13:47:09 +00004772 if (CombinerAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00004773 // Walk up chain skipping non-aliasing memory nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00004774 SDValue BetterChain = FindBetterChain(N, Chain);
Jim Laskey279f0532006-09-25 16:29:54 +00004775
Jim Laskey6ff23e52006-10-04 16:53:27 +00004776 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00004777 if (Chain != BetterChain) {
Dan Gohman475871a2008-07-27 21:46:04 +00004778 SDValue ReplLoad;
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004779
Jim Laskey279f0532006-09-25 16:29:54 +00004780 // Replace the chain to void dependency.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004781 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
4782 ReplLoad = DAG.getLoad(N->getValueType(0), BetterChain, Ptr,
Duncan Sandsdc846502007-10-28 12:59:45 +00004783 LD->getSrcValue(), LD->getSrcValueOffset(),
4784 LD->isVolatile(), LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004785 } else {
4786 ReplLoad = DAG.getExtLoad(LD->getExtensionType(),
4787 LD->getValueType(0),
4788 BetterChain, Ptr, LD->getSrcValue(),
4789 LD->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004790 LD->getMemoryVT(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00004791 LD->isVolatile(),
4792 LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004793 }
Jim Laskey279f0532006-09-25 16:29:54 +00004794
Jim Laskey6ff23e52006-10-04 16:53:27 +00004795 // Create token factor to keep old chain connected.
Dan Gohman475871a2008-07-27 21:46:04 +00004796 SDValue Token = DAG.getNode(ISD::TokenFactor, MVT::Other,
Jim Laskey288af5e2006-09-25 19:32:58 +00004797 Chain, ReplLoad.getValue(1));
Jim Laskey6ff23e52006-10-04 16:53:27 +00004798
Jim Laskey274062c2006-10-13 23:32:28 +00004799 // Replace uses with load result and token factor. Don't add users
4800 // to work list.
4801 return CombineTo(N, ReplLoad.getValue(0), Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00004802 }
4803 }
4804
Evan Cheng7fc033a2006-11-03 03:06:21 +00004805 // Try transforming N to an indexed load.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00004806 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Dan Gohman475871a2008-07-27 21:46:04 +00004807 return SDValue(N, 0);
Evan Cheng7fc033a2006-11-03 03:06:21 +00004808
Dan Gohman475871a2008-07-27 21:46:04 +00004809 return SDValue();
Chris Lattner01a22022005-10-10 22:04:48 +00004810}
4811
Chris Lattner07649d92008-01-08 23:08:06 +00004812
Dan Gohman475871a2008-07-27 21:46:04 +00004813SDValue DAGCombiner::visitSTORE(SDNode *N) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00004814 StoreSDNode *ST = cast<StoreSDNode>(N);
Dan Gohman475871a2008-07-27 21:46:04 +00004815 SDValue Chain = ST->getChain();
4816 SDValue Value = ST->getValue();
4817 SDValue Ptr = ST->getBasePtr();
Jim Laskey7aed46c2006-10-11 18:55:16 +00004818
Chris Lattner00161a62008-01-25 07:20:16 +00004819 // Try to infer better alignment information than the store already has.
Dan Gohmana2676512008-08-20 16:30:28 +00004820 if (!Fast && ST->isUnindexed()) {
Chris Lattner00161a62008-01-25 07:20:16 +00004821 if (unsigned Align = InferAlignment(Ptr, DAG)) {
4822 if (Align > ST->getAlignment())
4823 return DAG.getTruncStore(Chain, Value, Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004824 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattner00161a62008-01-25 07:20:16 +00004825 ST->isVolatile(), Align);
4826 }
4827 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004828
Evan Cheng59d5b682007-05-07 21:27:48 +00004829 // If this is a store of a bit convert, store the input value if the
Evan Cheng2c4f9432007-05-09 21:49:47 +00004830 // resultant store does not need a higher alignment than the original.
Dale Johannesen98a6c622007-05-16 22:45:30 +00004831 if (Value.getOpcode() == ISD::BIT_CONVERT && !ST->isTruncatingStore() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00004832 ST->isUnindexed()) {
Evan Cheng59d5b682007-05-07 21:27:48 +00004833 unsigned Align = ST->getAlignment();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004834 MVT SVT = Value.getOperand(0).getValueType();
Dan Gohman6448d912008-09-04 15:39:15 +00004835 unsigned OrigAlign = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00004836 getABITypeAlignment(SVT.getTypeForMVT());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004837 if (Align <= OrigAlign &&
Duncan Sands25cf2272008-11-24 14:53:14 +00004838 ((!LegalOperations && !ST->isVolatile()) ||
Dan Gohmanf560ffa2009-01-28 17:46:25 +00004839 TLI.isOperationLegalOrCustom(ISD::STORE, SVT)))
Evan Cheng59d5b682007-05-07 21:27:48 +00004840 return DAG.getStore(Chain, Value.getOperand(0), Ptr, ST->getSrcValue(),
Dan Gohman77455612008-06-28 00:45:22 +00004841 ST->getSrcValueOffset(), ST->isVolatile(), OrigAlign);
Jim Laskey279f0532006-09-25 16:29:54 +00004842 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004843
Nate Begeman2cbba892006-12-11 02:23:46 +00004844 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Nate Begeman2cbba892006-12-11 02:23:46 +00004845 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Value)) {
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004846 // NOTE: If the original store is volatile, this transform must not increase
4847 // the number of stores. For example, on x86-32 an f64 can be stored in one
4848 // processor operation but an i64 (which is not legal) requires two. So the
4849 // transform should not be done in this case.
Evan Cheng25ece662006-12-11 17:25:19 +00004850 if (Value.getOpcode() != ISD::TargetConstantFP) {
Dan Gohman475871a2008-07-27 21:46:04 +00004851 SDValue Tmp;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004852 switch (CFP->getValueType(0).getSimpleVT()) {
Chris Lattner62be1a72006-12-12 04:16:14 +00004853 default: assert(0 && "Unknown FP type");
Dale Johannesenc7b21d52007-09-18 18:36:59 +00004854 case MVT::f80: // We don't do this for these yet.
4855 case MVT::f128:
4856 case MVT::ppcf128:
4857 break;
Chris Lattner62be1a72006-12-12 04:16:14 +00004858 case MVT::f32:
Duncan Sands25cf2272008-11-24 14:53:14 +00004859 if (((TLI.isTypeLegal(MVT::i32) || !LegalTypes) && !LegalOperations &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00004860 !ST->isVolatile()) ||
4861 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i32)) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00004862 Tmp = DAG.getConstant((uint32_t)CFP->getValueAPF().
Dale Johannesen7111b022008-10-09 18:53:47 +00004863 bitcastToAPInt().getZExtValue(), MVT::i32);
Chris Lattner62be1a72006-12-12 04:16:14 +00004864 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004865 ST->getSrcValueOffset(), ST->isVolatile(),
4866 ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00004867 }
4868 break;
4869 case MVT::f64:
Duncan Sands25cf2272008-11-24 14:53:14 +00004870 if (((TLI.isTypeLegal(MVT::i64) || !LegalTypes) && !LegalOperations &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00004871 !ST->isVolatile()) ||
4872 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i64)) {
Dale Johannesen7111b022008-10-09 18:53:47 +00004873 Tmp = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
Dale Johannesen9d5f4562007-09-12 03:30:33 +00004874 getZExtValue(), MVT::i64);
Chris Lattner62be1a72006-12-12 04:16:14 +00004875 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004876 ST->getSrcValueOffset(), ST->isVolatile(),
4877 ST->getAlignment());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004878 } else if (!ST->isVolatile() &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00004879 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i32)) {
Duncan Sandsdc846502007-10-28 12:59:45 +00004880 // Many FP stores are not made apparent until after legalize, e.g. for
Chris Lattner62be1a72006-12-12 04:16:14 +00004881 // argument passing. Since this is so common, custom legalize the
4882 // 64-bit integer store into two 32-bit stores.
Dale Johannesen7111b022008-10-09 18:53:47 +00004883 uint64_t Val = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00004884 SDValue Lo = DAG.getConstant(Val & 0xFFFFFFFF, MVT::i32);
4885 SDValue Hi = DAG.getConstant(Val >> 32, MVT::i32);
Duncan Sands0753fc12008-02-11 10:37:04 +00004886 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner62be1a72006-12-12 04:16:14 +00004887
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004888 int SVOffset = ST->getSrcValueOffset();
4889 unsigned Alignment = ST->getAlignment();
4890 bool isVolatile = ST->isVolatile();
4891
Dan Gohman475871a2008-07-27 21:46:04 +00004892 SDValue St0 = DAG.getStore(Chain, Lo, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004893 ST->getSrcValueOffset(),
4894 isVolatile, ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00004895 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
4896 DAG.getConstant(4, Ptr.getValueType()));
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004897 SVOffset += 4;
Duncan Sandsdc846502007-10-28 12:59:45 +00004898 Alignment = MinAlign(Alignment, 4U);
Dan Gohman475871a2008-07-27 21:46:04 +00004899 SDValue St1 = DAG.getStore(Chain, Hi, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004900 SVOffset, isVolatile, Alignment);
Chris Lattner62be1a72006-12-12 04:16:14 +00004901 return DAG.getNode(ISD::TokenFactor, MVT::Other, St0, St1);
4902 }
4903 break;
Evan Cheng25ece662006-12-11 17:25:19 +00004904 }
Nate Begeman2cbba892006-12-11 02:23:46 +00004905 }
Nate Begeman2cbba892006-12-11 02:23:46 +00004906 }
4907
Jim Laskey279f0532006-09-25 16:29:54 +00004908 if (CombinerAA) {
4909 // Walk up chain skipping non-aliasing memory nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00004910 SDValue BetterChain = FindBetterChain(N, Chain);
Jim Laskey279f0532006-09-25 16:29:54 +00004911
Jim Laskey6ff23e52006-10-04 16:53:27 +00004912 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00004913 if (Chain != BetterChain) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00004914 // Replace the chain to avoid dependency.
Dan Gohman475871a2008-07-27 21:46:04 +00004915 SDValue ReplStore;
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004916 if (ST->isTruncatingStore()) {
4917 ReplStore = DAG.getTruncStore(BetterChain, Value, Ptr,
Chris Lattner4626b252008-01-17 07:20:38 +00004918 ST->getSrcValue(),ST->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004919 ST->getMemoryVT(),
Chris Lattner4626b252008-01-17 07:20:38 +00004920 ST->isVolatile(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004921 } else {
4922 ReplStore = DAG.getStore(BetterChain, Value, Ptr,
Chris Lattner4626b252008-01-17 07:20:38 +00004923 ST->getSrcValue(), ST->getSrcValueOffset(),
4924 ST->isVolatile(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004925 }
4926
Jim Laskey279f0532006-09-25 16:29:54 +00004927 // Create token to keep both nodes around.
Dan Gohman475871a2008-07-27 21:46:04 +00004928 SDValue Token =
Jim Laskey274062c2006-10-13 23:32:28 +00004929 DAG.getNode(ISD::TokenFactor, MVT::Other, Chain, ReplStore);
4930
4931 // Don't add users to work list.
4932 return CombineTo(N, Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00004933 }
Jim Laskeyd1aed7a2006-09-21 16:28:59 +00004934 }
Chris Lattnerc33baaa2005-12-23 05:48:07 +00004935
Evan Cheng33dbedc2006-11-05 09:31:14 +00004936 // Try transforming N to an indexed store.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00004937 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Dan Gohman475871a2008-07-27 21:46:04 +00004938 return SDValue(N, 0);
Evan Cheng33dbedc2006-11-05 09:31:14 +00004939
Chris Lattner3c872852007-12-29 06:26:16 +00004940 // FIXME: is there such a thing as a truncating indexed store?
Chris Lattnerddf89562008-01-17 19:59:44 +00004941 if (ST->isTruncatingStore() && ST->isUnindexed() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00004942 Value.getValueType().isInteger()) {
Chris Lattner2b4c2792007-10-13 06:35:54 +00004943 // See if we can simplify the input to this truncstore with knowledge that
4944 // only the low bits are being used. For example:
4945 // "truncstore (or (shl x, 8), y), i8" -> "truncstore y, i8"
Dan Gohman475871a2008-07-27 21:46:04 +00004946 SDValue Shorter =
Dan Gohman2e68b6f2008-02-25 21:11:39 +00004947 GetDemandedBits(Value,
4948 APInt::getLowBitsSet(Value.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +00004949 ST->getMemoryVT().getSizeInBits()));
Gabor Greifba36cb52008-08-28 21:40:38 +00004950 AddToWorkList(Value.getNode());
4951 if (Shorter.getNode())
Chris Lattner2b4c2792007-10-13 06:35:54 +00004952 return DAG.getTruncStore(Chain, Shorter, Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004953 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattner2b4c2792007-10-13 06:35:54 +00004954 ST->isVolatile(), ST->getAlignment());
Chris Lattnere33544c2007-10-13 06:58:48 +00004955
4956 // Otherwise, see if we can simplify the operation with
4957 // SimplifyDemandedBits, which only works if the value has a single use.
Dan Gohman7b8d4a92008-02-27 00:25:32 +00004958 if (SimplifyDemandedBits(Value,
4959 APInt::getLowBitsSet(
4960 Value.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +00004961 ST->getMemoryVT().getSizeInBits())))
Dan Gohman475871a2008-07-27 21:46:04 +00004962 return SDValue(N, 0);
Chris Lattner2b4c2792007-10-13 06:35:54 +00004963 }
4964
Chris Lattner3c872852007-12-29 06:26:16 +00004965 // If this is a load followed by a store to the same location, then the store
4966 // is dead/noop.
4967 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Value)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004968 if (Ld->getBasePtr() == Ptr && ST->getMemoryVT() == Ld->getMemoryVT() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00004969 ST->isUnindexed() && !ST->isVolatile() &&
Chris Lattner07649d92008-01-08 23:08:06 +00004970 // There can't be any side effects between the load and store, such as
4971 // a call or store.
Dan Gohman475871a2008-07-27 21:46:04 +00004972 Chain.reachesChainWithoutSideEffects(SDValue(Ld, 1))) {
Chris Lattner3c872852007-12-29 06:26:16 +00004973 // The store is dead, remove it.
4974 return Chain;
4975 }
4976 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004977
Chris Lattnerddf89562008-01-17 19:59:44 +00004978 // If this is an FP_ROUND or TRUNC followed by a store, fold this into a
4979 // truncating store. We can do this even if this is already a truncstore.
4980 if ((Value.getOpcode() == ISD::FP_ROUND || Value.getOpcode() == ISD::TRUNCATE)
Gabor Greifba36cb52008-08-28 21:40:38 +00004981 && Value.getNode()->hasOneUse() && ST->isUnindexed() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00004982 TLI.isTruncStoreLegal(Value.getOperand(0).getValueType(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004983 ST->getMemoryVT())) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004984 return DAG.getTruncStore(Chain, Value.getOperand(0), Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004985 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattnerddf89562008-01-17 19:59:44 +00004986 ST->isVolatile(), ST->getAlignment());
4987 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004988
Dan Gohman475871a2008-07-27 21:46:04 +00004989 return SDValue();
Chris Lattner87514ca2005-10-10 22:31:19 +00004990}
4991
Dan Gohman475871a2008-07-27 21:46:04 +00004992SDValue DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
4993 SDValue InVec = N->getOperand(0);
4994 SDValue InVal = N->getOperand(1);
4995 SDValue EltNo = N->getOperand(2);
Chris Lattnerca242442006-03-19 01:27:56 +00004996
4997 // If the invec is a BUILD_VECTOR and if EltNo is a constant, build a new
4998 // vector with the inserted element.
4999 if (InVec.getOpcode() == ISD::BUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005000 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Gabor Greif12632d22008-08-30 19:29:20 +00005001 SmallVector<SDValue, 8> Ops(InVec.getNode()->op_begin(),
5002 InVec.getNode()->op_end());
Chris Lattnerca242442006-03-19 01:27:56 +00005003 if (Elt < Ops.size())
5004 Ops[Elt] = InVal;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005005 return DAG.getNode(ISD::BUILD_VECTOR, InVec.getValueType(),
5006 &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00005007 }
5008
Dan Gohman475871a2008-07-27 21:46:04 +00005009 return SDValue();
Chris Lattnerca242442006-03-19 01:27:56 +00005010}
5011
Dan Gohman475871a2008-07-27 21:46:04 +00005012SDValue DAGCombiner::visitEXTRACT_VECTOR_ELT(SDNode *N) {
Mon P Wang7ac9cdf2009-01-17 00:07:25 +00005013 // (vextract (scalar_to_vector val, 0) -> val
5014 SDValue InVec = N->getOperand(0);
Mon P Wang7ac9cdf2009-01-17 00:07:25 +00005015
Mon P Wange3bc6ae2009-01-18 06:43:40 +00005016 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR)
5017 return InVec.getOperand(0);
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005018
5019 // Perform only after legalization to ensure build_vector / vector_shuffle
5020 // optimizations have already been done.
Duncan Sands25cf2272008-11-24 14:53:14 +00005021 if (!LegalOperations) return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005022
Mon P Wang7ac9cdf2009-01-17 00:07:25 +00005023 // (vextract (v4f32 load $addr), c) -> (f32 load $addr+c*size)
5024 // (vextract (v4f32 s2v (f32 load $addr)), c) -> (f32 load $addr+c*size)
5025 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), 0) -> (f32 load $addr)
Mon P Wange3bc6ae2009-01-18 06:43:40 +00005026 SDValue EltNo = N->getOperand(1);
Evan Cheng513da432007-10-06 08:19:55 +00005027
Evan Cheng513da432007-10-06 08:19:55 +00005028 if (isa<ConstantSDNode>(EltNo)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005029 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Evan Cheng513da432007-10-06 08:19:55 +00005030 bool NewLoad = false;
Mon P Wanga60b5232008-12-11 00:26:16 +00005031 bool BCNumEltsChanged = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005032 MVT VT = InVec.getValueType();
5033 MVT EVT = VT.getVectorElementType();
5034 MVT LVT = EVT;
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005035 if (InVec.getOpcode() == ISD::BIT_CONVERT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005036 MVT BCVT = InVec.getOperand(0).getValueType();
Mon P Wanga60b5232008-12-11 00:26:16 +00005037 if (!BCVT.isVector() || EVT.bitsGT(BCVT.getVectorElementType()))
Dan Gohman475871a2008-07-27 21:46:04 +00005038 return SDValue();
Mon P Wanga60b5232008-12-11 00:26:16 +00005039 if (VT.getVectorNumElements() != BCVT.getVectorNumElements())
5040 BCNumEltsChanged = true;
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005041 InVec = InVec.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005042 EVT = BCVT.getVectorElementType();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005043 NewLoad = true;
5044 }
Evan Cheng513da432007-10-06 08:19:55 +00005045
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005046 LoadSDNode *LN0 = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00005047 if (ISD::isNormalLoad(InVec.getNode()))
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005048 LN0 = cast<LoadSDNode>(InVec);
5049 else if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR &&
5050 InVec.getOperand(0).getValueType() == EVT &&
Gabor Greifba36cb52008-08-28 21:40:38 +00005051 ISD::isNormalLoad(InVec.getOperand(0).getNode())) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005052 LN0 = cast<LoadSDNode>(InVec.getOperand(0));
5053 } else if (InVec.getOpcode() == ISD::VECTOR_SHUFFLE) {
5054 // (vextract (vector_shuffle (load $addr), v2, <1, u, u, u>), 1)
5055 // =>
5056 // (load $addr+1*size)
Mon P Wanga60b5232008-12-11 00:26:16 +00005057
5058 // If the bit convert changed the number of elements, it is unsafe
5059 // to examine the mask.
5060 if (BCNumEltsChanged)
5061 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005062 unsigned Idx = cast<ConstantSDNode>(InVec.getOperand(2).
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005063 getOperand(Elt))->getZExtValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005064 unsigned NumElems = InVec.getOperand(2).getNumOperands();
5065 InVec = (Idx < NumElems) ? InVec.getOperand(0) : InVec.getOperand(1);
5066 if (InVec.getOpcode() == ISD::BIT_CONVERT)
5067 InVec = InVec.getOperand(0);
Gabor Greifba36cb52008-08-28 21:40:38 +00005068 if (ISD::isNormalLoad(InVec.getNode())) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005069 LN0 = cast<LoadSDNode>(InVec);
5070 Elt = (Idx < NumElems) ? Idx : Idx - NumElems;
Evan Cheng513da432007-10-06 08:19:55 +00005071 }
5072 }
Duncan Sandsec87aa82008-06-15 20:12:31 +00005073 if (!LN0 || !LN0->hasOneUse() || LN0->isVolatile())
Dan Gohman475871a2008-07-27 21:46:04 +00005074 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005075
5076 unsigned Align = LN0->getAlignment();
5077 if (NewLoad) {
5078 // Check the resultant load doesn't need a higher alignment than the
5079 // original load.
Dan Gohman6448d912008-09-04 15:39:15 +00005080 unsigned NewAlign = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00005081 getABITypeAlignment(LVT.getTypeForMVT());
Dan Gohmanf560ffa2009-01-28 17:46:25 +00005082 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, LVT))
Dan Gohman475871a2008-07-27 21:46:04 +00005083 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005084 Align = NewAlign;
5085 }
5086
Dan Gohman475871a2008-07-27 21:46:04 +00005087 SDValue NewPtr = LN0->getBasePtr();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005088 if (Elt) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005089 unsigned PtrOff = LVT.getSizeInBits() * Elt / 8;
5090 MVT PtrType = NewPtr.getValueType();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005091 if (TLI.isBigEndian())
Duncan Sands83ec4b62008-06-06 12:08:01 +00005092 PtrOff = VT.getSizeInBits() / 8 - PtrOff;
Evan Cheng77f0b7a2008-05-13 08:35:03 +00005093 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
5094 DAG.getConstant(PtrOff, PtrType));
5095 }
5096 return DAG.getLoad(LVT, LN0->getChain(), NewPtr,
5097 LN0->getSrcValue(), LN0->getSrcValueOffset(),
5098 LN0->isVolatile(), Align);
Evan Cheng513da432007-10-06 08:19:55 +00005099 }
Dan Gohman475871a2008-07-27 21:46:04 +00005100 return SDValue();
Evan Cheng513da432007-10-06 08:19:55 +00005101}
5102
5103
Dan Gohman475871a2008-07-27 21:46:04 +00005104SDValue DAGCombiner::visitBUILD_VECTOR(SDNode *N) {
Dan Gohman7f321562007-06-25 16:23:39 +00005105 unsigned NumInScalars = N->getNumOperands();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005106 MVT VT = N->getValueType(0);
5107 unsigned NumElts = VT.getVectorNumElements();
5108 MVT EltType = VT.getVectorElementType();
Chris Lattnerca242442006-03-19 01:27:56 +00005109
Dan Gohman7f321562007-06-25 16:23:39 +00005110 // Check to see if this is a BUILD_VECTOR of a bunch of EXTRACT_VECTOR_ELT
5111 // operations. If so, and if the EXTRACT_VECTOR_ELT vector inputs come from
5112 // at most two distinct vectors, turn this into a shuffle node.
Dan Gohman475871a2008-07-27 21:46:04 +00005113 SDValue VecIn1, VecIn2;
Chris Lattnerd7648c82006-03-28 20:28:38 +00005114 for (unsigned i = 0; i != NumInScalars; ++i) {
5115 // Ignore undef inputs.
5116 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
5117
Dan Gohman7f321562007-06-25 16:23:39 +00005118 // If this input is something other than a EXTRACT_VECTOR_ELT with a
Chris Lattnerd7648c82006-03-28 20:28:38 +00005119 // constant index, bail out.
Dan Gohman7f321562007-06-25 16:23:39 +00005120 if (N->getOperand(i).getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
Chris Lattnerd7648c82006-03-28 20:28:38 +00005121 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
Dan Gohman475871a2008-07-27 21:46:04 +00005122 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00005123 break;
5124 }
5125
Dan Gohman7f321562007-06-25 16:23:39 +00005126 // If the input vector type disagrees with the result of the build_vector,
Chris Lattnerd7648c82006-03-28 20:28:38 +00005127 // we can't make a shuffle.
Dan Gohman475871a2008-07-27 21:46:04 +00005128 SDValue ExtractedFromVec = N->getOperand(i).getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00005129 if (ExtractedFromVec.getValueType() != VT) {
Dan Gohman475871a2008-07-27 21:46:04 +00005130 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00005131 break;
5132 }
5133
5134 // Otherwise, remember this. We allow up to two distinct input vectors.
5135 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
5136 continue;
5137
Gabor Greifba36cb52008-08-28 21:40:38 +00005138 if (VecIn1.getNode() == 0) {
Chris Lattnerd7648c82006-03-28 20:28:38 +00005139 VecIn1 = ExtractedFromVec;
Gabor Greifba36cb52008-08-28 21:40:38 +00005140 } else if (VecIn2.getNode() == 0) {
Chris Lattnerd7648c82006-03-28 20:28:38 +00005141 VecIn2 = ExtractedFromVec;
5142 } else {
5143 // Too many inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00005144 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00005145 break;
5146 }
5147 }
5148
5149 // If everything is good, we can make a shuffle operation.
Gabor Greifba36cb52008-08-28 21:40:38 +00005150 if (VecIn1.getNode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005151 SmallVector<SDValue, 8> BuildVecIndices;
Chris Lattnerd7648c82006-03-28 20:28:38 +00005152 for (unsigned i = 0; i != NumInScalars; ++i) {
5153 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
Evan Cheng597a3bd2007-01-20 10:10:26 +00005154 BuildVecIndices.push_back(DAG.getNode(ISD::UNDEF, TLI.getPointerTy()));
Chris Lattnerd7648c82006-03-28 20:28:38 +00005155 continue;
5156 }
5157
Dan Gohman475871a2008-07-27 21:46:04 +00005158 SDValue Extract = N->getOperand(i);
Chris Lattnerd7648c82006-03-28 20:28:38 +00005159
5160 // If extracting from the first vector, just use the index directly.
5161 if (Extract.getOperand(0) == VecIn1) {
5162 BuildVecIndices.push_back(Extract.getOperand(1));
5163 continue;
5164 }
5165
5166 // Otherwise, use InIdx + VecSize
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005167 unsigned Idx =
5168 cast<ConstantSDNode>(Extract.getOperand(1))->getZExtValue();
Chris Lattner0bd48932008-01-17 07:00:52 +00005169 BuildVecIndices.push_back(DAG.getIntPtrConstant(Idx+NumInScalars));
Chris Lattnerd7648c82006-03-28 20:28:38 +00005170 }
5171
5172 // Add count and size info.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005173 MVT BuildVecVT = MVT::getVectorVT(TLI.getPointerTy(), NumElts);
Duncan Sands25cf2272008-11-24 14:53:14 +00005174 if (!TLI.isTypeLegal(BuildVecVT) && LegalTypes)
5175 return SDValue();
5176
Dan Gohman7f321562007-06-25 16:23:39 +00005177 // Return the new VECTOR_SHUFFLE node.
Dan Gohman475871a2008-07-27 21:46:04 +00005178 SDValue Ops[5];
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005179 Ops[0] = VecIn1;
Gabor Greifba36cb52008-08-28 21:40:38 +00005180 if (VecIn2.getNode()) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005181 Ops[1] = VecIn2;
Chris Lattnercef896e2006-03-28 22:19:47 +00005182 } else {
Dan Gohman7f321562007-06-25 16:23:39 +00005183 // Use an undef build_vector as input for the second operand.
Dan Gohman475871a2008-07-27 21:46:04 +00005184 std::vector<SDValue> UnOps(NumInScalars,
Chris Lattnercef896e2006-03-28 22:19:47 +00005185 DAG.getNode(ISD::UNDEF,
Dan Gohman7f321562007-06-25 16:23:39 +00005186 EltType));
5187 Ops[1] = DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005188 &UnOps[0], UnOps.size());
Gabor Greifba36cb52008-08-28 21:40:38 +00005189 AddToWorkList(Ops[1].getNode());
Chris Lattnercef896e2006-03-28 22:19:47 +00005190 }
Dan Gohman7f321562007-06-25 16:23:39 +00005191 Ops[2] = DAG.getNode(ISD::BUILD_VECTOR, BuildVecVT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005192 &BuildVecIndices[0], BuildVecIndices.size());
Dan Gohman7f321562007-06-25 16:23:39 +00005193 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Ops, 3);
Chris Lattnerd7648c82006-03-28 20:28:38 +00005194 }
5195
Dan Gohman475871a2008-07-27 21:46:04 +00005196 return SDValue();
Chris Lattnerd7648c82006-03-28 20:28:38 +00005197}
5198
Dan Gohman475871a2008-07-27 21:46:04 +00005199SDValue DAGCombiner::visitCONCAT_VECTORS(SDNode *N) {
Dan Gohman7f321562007-06-25 16:23:39 +00005200 // TODO: Check to see if this is a CONCAT_VECTORS of a bunch of
5201 // EXTRACT_SUBVECTOR operations. If so, and if the EXTRACT_SUBVECTOR vector
5202 // inputs come from at most two distinct vectors, turn this into a shuffle
5203 // node.
5204
5205 // If we only have one input vector, we don't need to do any concatenation.
5206 if (N->getNumOperands() == 1) {
5207 return N->getOperand(0);
5208 }
5209
Dan Gohman475871a2008-07-27 21:46:04 +00005210 return SDValue();
Dan Gohman7f321562007-06-25 16:23:39 +00005211}
5212
Dan Gohman475871a2008-07-27 21:46:04 +00005213SDValue DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
5214 SDValue ShufMask = N->getOperand(2);
Chris Lattnerf1d0c622006-03-31 22:16:43 +00005215 unsigned NumElts = ShufMask.getNumOperands();
5216
Mon P Wangaeb06d22008-11-10 04:46:22 +00005217 SDValue N0 = N->getOperand(0);
5218 SDValue N1 = N->getOperand(1);
5219
5220 assert(N0.getValueType().getVectorNumElements() == NumElts &&
5221 "Vector shuffle must be normalized in DAG");
5222
Chris Lattnerf1d0c622006-03-31 22:16:43 +00005223 // If the shuffle mask is an identity operation on the LHS, return the LHS.
5224 bool isIdentity = true;
5225 for (unsigned i = 0; i != NumElts; ++i) {
5226 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005227 cast<ConstantSDNode>(ShufMask.getOperand(i))->getZExtValue() != i) {
Chris Lattnerf1d0c622006-03-31 22:16:43 +00005228 isIdentity = false;
5229 break;
5230 }
5231 }
5232 if (isIdentity) return N->getOperand(0);
5233
5234 // If the shuffle mask is an identity operation on the RHS, return the RHS.
5235 isIdentity = true;
5236 for (unsigned i = 0; i != NumElts; ++i) {
5237 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005238 cast<ConstantSDNode>(ShufMask.getOperand(i))->getZExtValue() !=
5239 i+NumElts) {
Chris Lattnerf1d0c622006-03-31 22:16:43 +00005240 isIdentity = false;
5241 break;
5242 }
5243 }
5244 if (isIdentity) return N->getOperand(1);
Evan Chenge7bec0d2006-07-20 22:44:41 +00005245
5246 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
5247 // needed at all.
5248 bool isUnary = true;
Evan Cheng917ec982006-07-21 08:25:53 +00005249 bool isSplat = true;
Evan Chenge7bec0d2006-07-20 22:44:41 +00005250 int VecNum = -1;
Reid Spencer9160a6a2006-07-25 20:44:41 +00005251 unsigned BaseIdx = 0;
Evan Chenge7bec0d2006-07-20 22:44:41 +00005252 for (unsigned i = 0; i != NumElts; ++i)
5253 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005254 unsigned Idx=cast<ConstantSDNode>(ShufMask.getOperand(i))->getZExtValue();
Evan Chenge7bec0d2006-07-20 22:44:41 +00005255 int V = (Idx < NumElts) ? 0 : 1;
Evan Cheng917ec982006-07-21 08:25:53 +00005256 if (VecNum == -1) {
Evan Chenge7bec0d2006-07-20 22:44:41 +00005257 VecNum = V;
Evan Cheng917ec982006-07-21 08:25:53 +00005258 BaseIdx = Idx;
5259 } else {
5260 if (BaseIdx != Idx)
5261 isSplat = false;
5262 if (VecNum != V) {
5263 isUnary = false;
5264 break;
5265 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00005266 }
5267 }
5268
Evan Chenge7bec0d2006-07-20 22:44:41 +00005269 // Normalize unary shuffle so the RHS is undef.
5270 if (isUnary && VecNum == 1)
5271 std::swap(N0, N1);
5272
Evan Cheng917ec982006-07-21 08:25:53 +00005273 // If it is a splat, check if the argument vector is a build_vector with
5274 // all scalar elements the same.
5275 if (isSplat) {
Gabor Greifba36cb52008-08-28 21:40:38 +00005276 SDNode *V = N0.getNode();
Evan Cheng917ec982006-07-21 08:25:53 +00005277
Dan Gohman7f321562007-06-25 16:23:39 +00005278 // If this is a bit convert that changes the element type of the vector but
Evan Cheng59569222006-10-16 22:49:37 +00005279 // not the number of vector elements, look through it. Be careful not to
5280 // look though conversions that change things like v4f32 to v2f64.
Dan Gohman7f321562007-06-25 16:23:39 +00005281 if (V->getOpcode() == ISD::BIT_CONVERT) {
Dan Gohman475871a2008-07-27 21:46:04 +00005282 SDValue ConvInput = V->getOperand(0);
Evan Cheng29257862008-07-22 20:42:56 +00005283 if (ConvInput.getValueType().isVector() &&
5284 ConvInput.getValueType().getVectorNumElements() == NumElts)
Gabor Greifba36cb52008-08-28 21:40:38 +00005285 V = ConvInput.getNode();
Evan Cheng59569222006-10-16 22:49:37 +00005286 }
5287
Dan Gohman7f321562007-06-25 16:23:39 +00005288 if (V->getOpcode() == ISD::BUILD_VECTOR) {
5289 unsigned NumElems = V->getNumOperands();
Evan Cheng917ec982006-07-21 08:25:53 +00005290 if (NumElems > BaseIdx) {
Dan Gohman475871a2008-07-27 21:46:04 +00005291 SDValue Base;
Evan Cheng917ec982006-07-21 08:25:53 +00005292 bool AllSame = true;
5293 for (unsigned i = 0; i != NumElems; ++i) {
5294 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
5295 Base = V->getOperand(i);
5296 break;
5297 }
5298 }
5299 // Splat of <u, u, u, u>, return <u, u, u, u>
Gabor Greifba36cb52008-08-28 21:40:38 +00005300 if (!Base.getNode())
Evan Cheng917ec982006-07-21 08:25:53 +00005301 return N0;
5302 for (unsigned i = 0; i != NumElems; ++i) {
Evan Chenge0480d22007-09-18 21:54:37 +00005303 if (V->getOperand(i) != Base) {
Evan Cheng917ec982006-07-21 08:25:53 +00005304 AllSame = false;
5305 break;
5306 }
5307 }
5308 // Splat of <x, x, x, x>, return <x, x, x, x>
5309 if (AllSame)
5310 return N0;
5311 }
5312 }
5313 }
5314
Evan Chenge7bec0d2006-07-20 22:44:41 +00005315 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
5316 // into an undef.
5317 if (isUnary || N0 == N1) {
Chris Lattner17614ea2006-04-08 05:34:25 +00005318 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
5319 // first operand.
Dan Gohman475871a2008-07-27 21:46:04 +00005320 SmallVector<SDValue, 8> MappedOps;
Chris Lattner17614ea2006-04-08 05:34:25 +00005321 for (unsigned i = 0; i != NumElts; ++i) {
5322 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005323 cast<ConstantSDNode>(ShufMask.getOperand(i))->getZExtValue() <
5324 NumElts) {
Chris Lattner17614ea2006-04-08 05:34:25 +00005325 MappedOps.push_back(ShufMask.getOperand(i));
5326 } else {
5327 unsigned NewIdx =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005328 cast<ConstantSDNode>(ShufMask.getOperand(i))->getZExtValue() -
5329 NumElts;
Duncan Sandsd038e042008-07-21 10:20:31 +00005330 MappedOps.push_back(DAG.getConstant(NewIdx,
5331 ShufMask.getOperand(i).getValueType()));
Chris Lattner17614ea2006-04-08 05:34:25 +00005332 }
5333 }
Dan Gohman7f321562007-06-25 16:23:39 +00005334 ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMask.getValueType(),
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005335 &MappedOps[0], MappedOps.size());
Gabor Greifba36cb52008-08-28 21:40:38 +00005336 AddToWorkList(ShufMask.getNode());
Dan Gohman7f321562007-06-25 16:23:39 +00005337 return DAG.getNode(ISD::VECTOR_SHUFFLE, N->getValueType(0),
5338 N0,
5339 DAG.getNode(ISD::UNDEF, N->getValueType(0)),
5340 ShufMask);
Chris Lattner17614ea2006-04-08 05:34:25 +00005341 }
Dan Gohman7f321562007-06-25 16:23:39 +00005342
Dan Gohman475871a2008-07-27 21:46:04 +00005343 return SDValue();
Chris Lattnerf1d0c622006-03-31 22:16:43 +00005344}
5345
Evan Cheng44f1f092006-04-20 08:56:16 +00005346/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
Dan Gohman7f321562007-06-25 16:23:39 +00005347/// an AND to a vector_shuffle with the destination vector and a zero vector.
5348/// e.g. AND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
Evan Cheng44f1f092006-04-20 08:56:16 +00005349/// vector_shuffle V, Zero, <0, 4, 2, 4>
Dan Gohman475871a2008-07-27 21:46:04 +00005350SDValue DAGCombiner::XformToShuffleWithZero(SDNode *N) {
5351 SDValue LHS = N->getOperand(0);
5352 SDValue RHS = N->getOperand(1);
Dan Gohman7f321562007-06-25 16:23:39 +00005353 if (N->getOpcode() == ISD::AND) {
5354 if (RHS.getOpcode() == ISD::BIT_CONVERT)
Evan Cheng44f1f092006-04-20 08:56:16 +00005355 RHS = RHS.getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00005356 if (RHS.getOpcode() == ISD::BUILD_VECTOR) {
Dan Gohman475871a2008-07-27 21:46:04 +00005357 std::vector<SDValue> IdxOps;
Evan Cheng44f1f092006-04-20 08:56:16 +00005358 unsigned NumOps = RHS.getNumOperands();
Dan Gohman7f321562007-06-25 16:23:39 +00005359 unsigned NumElts = NumOps;
Evan Cheng44f1f092006-04-20 08:56:16 +00005360 for (unsigned i = 0; i != NumElts; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005361 SDValue Elt = RHS.getOperand(i);
Evan Cheng44f1f092006-04-20 08:56:16 +00005362 if (!isa<ConstantSDNode>(Elt))
Dan Gohman475871a2008-07-27 21:46:04 +00005363 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00005364 else if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
Duncan Sands94989ac2008-10-19 14:58:05 +00005365 IdxOps.push_back(DAG.getIntPtrConstant(i));
Evan Cheng44f1f092006-04-20 08:56:16 +00005366 else if (cast<ConstantSDNode>(Elt)->isNullValue())
Duncan Sands94989ac2008-10-19 14:58:05 +00005367 IdxOps.push_back(DAG.getIntPtrConstant(NumElts));
Evan Cheng44f1f092006-04-20 08:56:16 +00005368 else
Dan Gohman475871a2008-07-27 21:46:04 +00005369 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00005370 }
5371
5372 // Let's see if the target supports this vector_shuffle.
Duncan Sands94989ac2008-10-19 14:58:05 +00005373 if (!TLI.isVectorClearMaskLegal(IdxOps, TLI.getPointerTy(), DAG))
Dan Gohman475871a2008-07-27 21:46:04 +00005374 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00005375
Dan Gohman7f321562007-06-25 16:23:39 +00005376 // Return the new VECTOR_SHUFFLE node.
Duncan Sands94989ac2008-10-19 14:58:05 +00005377 MVT EVT = RHS.getValueType().getVectorElementType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005378 MVT VT = MVT::getVectorVT(EVT, NumElts);
Evan Cheng3eb57d52008-11-05 06:04:18 +00005379 MVT MaskVT = MVT::getVectorVT(TLI.getPointerTy(), NumElts);
Dan Gohman475871a2008-07-27 21:46:04 +00005380 std::vector<SDValue> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00005381 LHS = DAG.getNode(ISD::BIT_CONVERT, VT, LHS);
Evan Cheng44f1f092006-04-20 08:56:16 +00005382 Ops.push_back(LHS);
Gabor Greifba36cb52008-08-28 21:40:38 +00005383 AddToWorkList(LHS.getNode());
Dan Gohman475871a2008-07-27 21:46:04 +00005384 std::vector<SDValue> ZeroOps(NumElts, DAG.getConstant(0, EVT));
Dan Gohman7f321562007-06-25 16:23:39 +00005385 Ops.push_back(DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005386 &ZeroOps[0], ZeroOps.size()));
Evan Cheng3eb57d52008-11-05 06:04:18 +00005387 Ops.push_back(DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005388 &IdxOps[0], IdxOps.size()));
Dan Gohman475871a2008-07-27 21:46:04 +00005389 SDValue Result = DAG.getNode(ISD::VECTOR_SHUFFLE, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005390 &Ops[0], Ops.size());
Dan Gohman7a9a5af2008-07-16 16:13:58 +00005391 if (VT != N->getValueType(0))
5392 Result = DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Result);
Evan Cheng44f1f092006-04-20 08:56:16 +00005393 return Result;
5394 }
5395 }
Dan Gohman475871a2008-07-27 21:46:04 +00005396 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00005397}
5398
Dan Gohman7f321562007-06-25 16:23:39 +00005399/// SimplifyVBinOp - Visit a binary vector operation, like ADD.
Dan Gohman475871a2008-07-27 21:46:04 +00005400SDValue DAGCombiner::SimplifyVBinOp(SDNode *N) {
Dan Gohman7f321562007-06-25 16:23:39 +00005401 // After legalize, the target may be depending on adds and other
5402 // binary ops to provide legal ways to construct constants or other
5403 // things. Simplifying them may result in a loss of legality.
Duncan Sands25cf2272008-11-24 14:53:14 +00005404 if (LegalOperations) return SDValue();
Dan Gohman7f321562007-06-25 16:23:39 +00005405
Duncan Sands83ec4b62008-06-06 12:08:01 +00005406 MVT VT = N->getValueType(0);
5407 assert(VT.isVector() && "SimplifyVBinOp only works on vectors!");
Dan Gohman7f321562007-06-25 16:23:39 +00005408
Duncan Sands83ec4b62008-06-06 12:08:01 +00005409 MVT EltType = VT.getVectorElementType();
Dan Gohman475871a2008-07-27 21:46:04 +00005410 SDValue LHS = N->getOperand(0);
5411 SDValue RHS = N->getOperand(1);
5412 SDValue Shuffle = XformToShuffleWithZero(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00005413 if (Shuffle.getNode()) return Shuffle;
Evan Cheng44f1f092006-04-20 08:56:16 +00005414
Dan Gohman7f321562007-06-25 16:23:39 +00005415 // If the LHS and RHS are BUILD_VECTOR nodes, see if we can constant fold
Chris Lattneredab1b92006-04-02 03:25:57 +00005416 // this operation.
Dan Gohman7f321562007-06-25 16:23:39 +00005417 if (LHS.getOpcode() == ISD::BUILD_VECTOR &&
5418 RHS.getOpcode() == ISD::BUILD_VECTOR) {
Dan Gohman475871a2008-07-27 21:46:04 +00005419 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00005420 for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005421 SDValue LHSOp = LHS.getOperand(i);
5422 SDValue RHSOp = RHS.getOperand(i);
Chris Lattneredab1b92006-04-02 03:25:57 +00005423 // If these two elements can't be folded, bail out.
5424 if ((LHSOp.getOpcode() != ISD::UNDEF &&
5425 LHSOp.getOpcode() != ISD::Constant &&
5426 LHSOp.getOpcode() != ISD::ConstantFP) ||
5427 (RHSOp.getOpcode() != ISD::UNDEF &&
5428 RHSOp.getOpcode() != ISD::Constant &&
5429 RHSOp.getOpcode() != ISD::ConstantFP))
5430 break;
Evan Cheng7b336a82006-05-31 06:08:35 +00005431 // Can't fold divide by zero.
Dan Gohman7f321562007-06-25 16:23:39 +00005432 if (N->getOpcode() == ISD::SDIV || N->getOpcode() == ISD::UDIV ||
5433 N->getOpcode() == ISD::FDIV) {
Evan Cheng7b336a82006-05-31 06:08:35 +00005434 if ((RHSOp.getOpcode() == ISD::Constant &&
Gabor Greifba36cb52008-08-28 21:40:38 +00005435 cast<ConstantSDNode>(RHSOp.getNode())->isNullValue()) ||
Evan Cheng7b336a82006-05-31 06:08:35 +00005436 (RHSOp.getOpcode() == ISD::ConstantFP &&
Gabor Greifba36cb52008-08-28 21:40:38 +00005437 cast<ConstantFPSDNode>(RHSOp.getNode())->getValueAPF().isZero()))
Evan Cheng7b336a82006-05-31 06:08:35 +00005438 break;
5439 }
Dan Gohman7f321562007-06-25 16:23:39 +00005440 Ops.push_back(DAG.getNode(N->getOpcode(), EltType, LHSOp, RHSOp));
Gabor Greifba36cb52008-08-28 21:40:38 +00005441 AddToWorkList(Ops.back().getNode());
Chris Lattneredab1b92006-04-02 03:25:57 +00005442 assert((Ops.back().getOpcode() == ISD::UNDEF ||
5443 Ops.back().getOpcode() == ISD::Constant ||
5444 Ops.back().getOpcode() == ISD::ConstantFP) &&
5445 "Scalar binop didn't fold!");
5446 }
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00005447
Dan Gohman7f321562007-06-25 16:23:39 +00005448 if (Ops.size() == LHS.getNumOperands()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005449 MVT VT = LHS.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +00005450 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00005451 }
Chris Lattneredab1b92006-04-02 03:25:57 +00005452 }
5453
Dan Gohman475871a2008-07-27 21:46:04 +00005454 return SDValue();
Chris Lattneredab1b92006-04-02 03:25:57 +00005455}
5456
Dan Gohman475871a2008-07-27 21:46:04 +00005457SDValue DAGCombiner::SimplifySelect(SDValue N0, SDValue N1, SDValue N2){
Nate Begemanf845b452005-10-08 00:29:44 +00005458 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
5459
Dan Gohman475871a2008-07-27 21:46:04 +00005460 SDValue SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
Nate Begemanf845b452005-10-08 00:29:44 +00005461 cast<CondCodeSDNode>(N0.getOperand(2))->get());
5462 // If we got a simplified select_cc node back from SimplifySelectCC, then
5463 // break it down into a new SETCC node, and a new SELECT node, and then return
5464 // the SELECT node, since we were called with a SELECT node.
Gabor Greifba36cb52008-08-28 21:40:38 +00005465 if (SCC.getNode()) {
Nate Begemanf845b452005-10-08 00:29:44 +00005466 // Check to see if we got a select_cc back (to turn into setcc/select).
5467 // Otherwise, just return whatever node we got back, like fabs.
5468 if (SCC.getOpcode() == ISD::SELECT_CC) {
Dan Gohman475871a2008-07-27 21:46:04 +00005469 SDValue SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
Nate Begemanf845b452005-10-08 00:29:44 +00005470 SCC.getOperand(0), SCC.getOperand(1),
5471 SCC.getOperand(4));
Gabor Greifba36cb52008-08-28 21:40:38 +00005472 AddToWorkList(SETCC.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00005473 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
5474 SCC.getOperand(3), SETCC);
5475 }
5476 return SCC;
5477 }
Dan Gohman475871a2008-07-27 21:46:04 +00005478 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00005479}
5480
Chris Lattner40c62d52005-10-18 06:04:22 +00005481/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
5482/// are the two values being selected between, see if we can simplify the
Chris Lattner729c6d12006-05-27 00:43:02 +00005483/// select. Callers of this should assume that TheSelect is deleted if this
5484/// returns true. As such, they should return the appropriate thing (e.g. the
5485/// node) back to the top-level of the DAG combiner loop to avoid it being
5486/// looked at.
Chris Lattner40c62d52005-10-18 06:04:22 +00005487///
Dan Gohman475871a2008-07-27 21:46:04 +00005488bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDValue LHS,
5489 SDValue RHS) {
Chris Lattner40c62d52005-10-18 06:04:22 +00005490
5491 // If this is a select from two identical things, try to pull the operation
5492 // through the select.
5493 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
Chris Lattner40c62d52005-10-18 06:04:22 +00005494 // If this is a load and the token chain is identical, replace the select
5495 // of two loads with a load through a select of the address to load from.
5496 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
5497 // constants have been dropped into the constant pool.
Evan Cheng466685d2006-10-09 20:57:25 +00005498 if (LHS.getOpcode() == ISD::LOAD &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005499 // Do not let this transformation reduce the number of volatile loads.
5500 !cast<LoadSDNode>(LHS)->isVolatile() &&
5501 !cast<LoadSDNode>(RHS)->isVolatile() &&
Chris Lattner40c62d52005-10-18 06:04:22 +00005502 // Token chains must be identical.
Evan Cheng466685d2006-10-09 20:57:25 +00005503 LHS.getOperand(0) == RHS.getOperand(0)) {
5504 LoadSDNode *LLD = cast<LoadSDNode>(LHS);
5505 LoadSDNode *RLD = cast<LoadSDNode>(RHS);
5506
5507 // If this is an EXTLOAD, the VT's must match.
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005508 if (LLD->getMemoryVT() == RLD->getMemoryVT()) {
Evan Cheng466685d2006-10-09 20:57:25 +00005509 // FIXME: this conflates two src values, discarding one. This is not
5510 // the right thing to do, but nothing uses srcvalues now. When they do,
5511 // turn SrcValue into a list of locations.
Dan Gohman475871a2008-07-27 21:46:04 +00005512 SDValue Addr;
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005513 if (TheSelect->getOpcode() == ISD::SELECT) {
5514 // Check that the condition doesn't reach either load. If so, folding
5515 // this will induce a cycle into the DAG.
Gabor Greifba36cb52008-08-28 21:40:38 +00005516 if (!LLD->isPredecessorOf(TheSelect->getOperand(0).getNode()) &&
5517 !RLD->isPredecessorOf(TheSelect->getOperand(0).getNode())) {
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005518 Addr = DAG.getNode(ISD::SELECT, LLD->getBasePtr().getValueType(),
5519 TheSelect->getOperand(0), LLD->getBasePtr(),
5520 RLD->getBasePtr());
5521 }
5522 } else {
5523 // Check that the condition doesn't reach either load. If so, folding
5524 // this will induce a cycle into the DAG.
Gabor Greifba36cb52008-08-28 21:40:38 +00005525 if (!LLD->isPredecessorOf(TheSelect->getOperand(0).getNode()) &&
5526 !RLD->isPredecessorOf(TheSelect->getOperand(0).getNode()) &&
5527 !LLD->isPredecessorOf(TheSelect->getOperand(1).getNode()) &&
5528 !RLD->isPredecessorOf(TheSelect->getOperand(1).getNode())) {
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005529 Addr = DAG.getNode(ISD::SELECT_CC, LLD->getBasePtr().getValueType(),
Evan Cheng466685d2006-10-09 20:57:25 +00005530 TheSelect->getOperand(0),
5531 TheSelect->getOperand(1),
5532 LLD->getBasePtr(), RLD->getBasePtr(),
5533 TheSelect->getOperand(4));
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005534 }
Evan Cheng466685d2006-10-09 20:57:25 +00005535 }
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005536
Gabor Greifba36cb52008-08-28 21:40:38 +00005537 if (Addr.getNode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005538 SDValue Load;
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005539 if (LLD->getExtensionType() == ISD::NON_EXTLOAD)
5540 Load = DAG.getLoad(TheSelect->getValueType(0), LLD->getChain(),
5541 Addr,LLD->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00005542 LLD->getSrcValueOffset(),
5543 LLD->isVolatile(),
5544 LLD->getAlignment());
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005545 else {
5546 Load = DAG.getExtLoad(LLD->getExtensionType(),
5547 TheSelect->getValueType(0),
5548 LLD->getChain(), Addr, LLD->getSrcValue(),
5549 LLD->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005550 LLD->getMemoryVT(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00005551 LLD->isVolatile(),
5552 LLD->getAlignment());
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005553 }
5554 // Users of the select now use the result of the load.
5555 CombineTo(TheSelect, Load);
5556
5557 // Users of the old loads now use the new load's chain. We know the
5558 // old-load value is dead now.
Gabor Greifba36cb52008-08-28 21:40:38 +00005559 CombineTo(LHS.getNode(), Load.getValue(0), Load.getValue(1));
5560 CombineTo(RHS.getNode(), Load.getValue(0), Load.getValue(1));
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005561 return true;
5562 }
Evan Chengc5484282006-10-04 00:56:09 +00005563 }
Chris Lattner40c62d52005-10-18 06:04:22 +00005564 }
5565 }
5566
5567 return false;
5568}
5569
Dan Gohman475871a2008-07-27 21:46:04 +00005570SDValue DAGCombiner::SimplifySelectCC(SDValue N0, SDValue N1,
5571 SDValue N2, SDValue N3,
5572 ISD::CondCode CC, bool NotExtCompare) {
Nate Begemanf845b452005-10-08 00:29:44 +00005573
Duncan Sands83ec4b62008-06-06 12:08:01 +00005574 MVT VT = N2.getValueType();
Gabor Greifba36cb52008-08-28 21:40:38 +00005575 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
5576 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
5577 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00005578
5579 // Determine if the condition we're dealing with is constant
Duncan Sands5480c042009-01-01 15:52:00 +00005580 SDValue SCC = SimplifySetCC(TLI.getSetCCResultType(N0.getValueType()),
5581 N0, N1, CC, false);
Gabor Greifba36cb52008-08-28 21:40:38 +00005582 if (SCC.getNode()) AddToWorkList(SCC.getNode());
5583 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00005584
5585 // fold select_cc true, x, y -> x
Dan Gohman002e5d02008-03-13 22:13:53 +00005586 if (SCCC && !SCCC->isNullValue())
Nate Begemanf845b452005-10-08 00:29:44 +00005587 return N2;
5588 // fold select_cc false, x, y -> y
Dan Gohman002e5d02008-03-13 22:13:53 +00005589 if (SCCC && SCCC->isNullValue())
Nate Begemanf845b452005-10-08 00:29:44 +00005590 return N3;
5591
5592 // Check to see if we can simplify the select into an fabs node
5593 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
5594 // Allow either -0.0 or 0.0
Dale Johannesen87503a62007-08-25 22:10:57 +00005595 if (CFP->getValueAPF().isZero()) {
Nate Begemanf845b452005-10-08 00:29:44 +00005596 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
5597 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
5598 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
5599 N2 == N3.getOperand(0))
5600 return DAG.getNode(ISD::FABS, VT, N0);
5601
5602 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
5603 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
5604 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
5605 N2.getOperand(0) == N3)
5606 return DAG.getNode(ISD::FABS, VT, N3);
5607 }
5608 }
5609
5610 // Check to see if we can perform the "gzip trick", transforming
5611 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
Chris Lattnere3152e52006-09-20 06:41:35 +00005612 if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00005613 N0.getValueType().isInteger() &&
5614 N2.getValueType().isInteger() &&
Dan Gohman002e5d02008-03-13 22:13:53 +00005615 (N1C->isNullValue() || // (a < 0) ? b : 0
5616 (N1C->getAPIntValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0
Duncan Sands83ec4b62008-06-06 12:08:01 +00005617 MVT XType = N0.getValueType();
5618 MVT AType = N2.getValueType();
Duncan Sands8e4eb092008-06-08 20:54:56 +00005619 if (XType.bitsGE(AType)) {
Nate Begemanf845b452005-10-08 00:29:44 +00005620 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00005621 // single-bit constant.
Dan Gohman002e5d02008-03-13 22:13:53 +00005622 if (N2C && ((N2C->getAPIntValue() & (N2C->getAPIntValue()-1)) == 0)) {
5623 unsigned ShCtV = N2C->getAPIntValue().logBase2();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005624 ShCtV = XType.getSizeInBits()-ShCtV-1;
Dan Gohman475871a2008-07-27 21:46:04 +00005625 SDValue ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
5626 SDValue Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
Gabor Greifba36cb52008-08-28 21:40:38 +00005627 AddToWorkList(Shift.getNode());
Duncan Sands8e4eb092008-06-08 20:54:56 +00005628 if (XType.bitsGT(AType)) {
Nate Begemanf845b452005-10-08 00:29:44 +00005629 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00005630 AddToWorkList(Shift.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00005631 }
5632 return DAG.getNode(ISD::AND, AType, Shift, N2);
5633 }
Dan Gohman475871a2008-07-27 21:46:04 +00005634 SDValue Shift = DAG.getNode(ISD::SRA, XType, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005635 DAG.getConstant(XType.getSizeInBits()-1,
Nate Begemanf845b452005-10-08 00:29:44 +00005636 TLI.getShiftAmountTy()));
Gabor Greifba36cb52008-08-28 21:40:38 +00005637 AddToWorkList(Shift.getNode());
Duncan Sands8e4eb092008-06-08 20:54:56 +00005638 if (XType.bitsGT(AType)) {
Nate Begemanf845b452005-10-08 00:29:44 +00005639 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00005640 AddToWorkList(Shift.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00005641 }
5642 return DAG.getNode(ISD::AND, AType, Shift, N2);
5643 }
5644 }
Nate Begeman07ed4172005-10-10 21:26:48 +00005645
5646 // fold select C, 16, 0 -> shl C, 4
Dan Gohman002e5d02008-03-13 22:13:53 +00005647 if (N2C && N3C && N3C->isNullValue() && N2C->getAPIntValue().isPowerOf2() &&
Duncan Sands03228082008-11-23 15:47:28 +00005648 TLI.getBooleanContents() == TargetLowering::ZeroOrOneBooleanContent) {
Chris Lattner1eba01e2007-04-11 06:50:51 +00005649
5650 // If the caller doesn't want us to simplify this into a zext of a compare,
5651 // don't do it.
Dan Gohman002e5d02008-03-13 22:13:53 +00005652 if (NotExtCompare && N2C->getAPIntValue() == 1)
Dan Gohman475871a2008-07-27 21:46:04 +00005653 return SDValue();
Chris Lattner1eba01e2007-04-11 06:50:51 +00005654
Nate Begeman07ed4172005-10-10 21:26:48 +00005655 // Get a SetCC of the condition
5656 // FIXME: Should probably make sure that setcc is legal if we ever have a
5657 // target where it isn't.
Dan Gohman475871a2008-07-27 21:46:04 +00005658 SDValue Temp, SCC;
Nate Begeman07ed4172005-10-10 21:26:48 +00005659 // cast from setcc result type to select result type
Duncan Sands25cf2272008-11-24 14:53:14 +00005660 if (LegalTypes) {
Duncan Sands5480c042009-01-01 15:52:00 +00005661 SCC = DAG.getSetCC(TLI.getSetCCResultType(N0.getValueType()),
5662 N0, N1, CC);
Duncan Sands8e4eb092008-06-08 20:54:56 +00005663 if (N2.getValueType().bitsLT(SCC.getValueType()))
Chris Lattner555d8d62006-12-07 22:36:47 +00005664 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
5665 else
5666 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00005667 } else {
5668 SCC = DAG.getSetCC(MVT::i1, N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00005669 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00005670 }
Gabor Greifba36cb52008-08-28 21:40:38 +00005671 AddToWorkList(SCC.getNode());
5672 AddToWorkList(Temp.getNode());
Chris Lattnerc56a81d2007-04-11 06:43:25 +00005673
Dan Gohman002e5d02008-03-13 22:13:53 +00005674 if (N2C->getAPIntValue() == 1)
Chris Lattnerc56a81d2007-04-11 06:43:25 +00005675 return Temp;
Nate Begeman07ed4172005-10-10 21:26:48 +00005676 // shl setcc result by log2 n2c
5677 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
Dan Gohman002e5d02008-03-13 22:13:53 +00005678 DAG.getConstant(N2C->getAPIntValue().logBase2(),
Nate Begeman07ed4172005-10-10 21:26:48 +00005679 TLI.getShiftAmountTy()));
5680 }
5681
Nate Begemanf845b452005-10-08 00:29:44 +00005682 // Check to see if this is the equivalent of setcc
5683 // FIXME: Turn all of these into setcc if setcc if setcc is legal
5684 // otherwise, go ahead with the folds.
Dan Gohman002e5d02008-03-13 22:13:53 +00005685 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getAPIntValue() == 1ULL)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005686 MVT XType = N0.getValueType();
Duncan Sands25cf2272008-11-24 14:53:14 +00005687 if (!LegalOperations ||
Duncan Sands5480c042009-01-01 15:52:00 +00005688 TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultType(XType))) {
5689 SDValue Res = DAG.getSetCC(TLI.getSetCCResultType(XType), N0, N1, CC);
Nate Begemanf845b452005-10-08 00:29:44 +00005690 if (Res.getValueType() != VT)
5691 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
5692 return Res;
5693 }
5694
5695 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
5696 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
Duncan Sands25cf2272008-11-24 14:53:14 +00005697 (!LegalOperations ||
Duncan Sands184a8762008-06-14 17:48:34 +00005698 TLI.isOperationLegal(ISD::CTLZ, XType))) {
Dan Gohman475871a2008-07-27 21:46:04 +00005699 SDValue Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
Nate Begemanf845b452005-10-08 00:29:44 +00005700 return DAG.getNode(ISD::SRL, XType, Ctlz,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005701 DAG.getConstant(Log2_32(XType.getSizeInBits()),
Nate Begemanf845b452005-10-08 00:29:44 +00005702 TLI.getShiftAmountTy()));
5703 }
5704 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
5705 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
Dan Gohman475871a2008-07-27 21:46:04 +00005706 SDValue NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
Nate Begemanf845b452005-10-08 00:29:44 +00005707 N0);
Bob Wilson4c245462009-01-22 17:39:32 +00005708 SDValue NotN0 = DAG.getNOT(N0, XType);
Nate Begemanf845b452005-10-08 00:29:44 +00005709 return DAG.getNode(ISD::SRL, XType,
5710 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
Duncan Sands83ec4b62008-06-06 12:08:01 +00005711 DAG.getConstant(XType.getSizeInBits()-1,
Nate Begemanf845b452005-10-08 00:29:44 +00005712 TLI.getShiftAmountTy()));
5713 }
5714 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
5715 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
Dan Gohman475871a2008-07-27 21:46:04 +00005716 SDValue Sign = DAG.getNode(ISD::SRL, XType, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005717 DAG.getConstant(XType.getSizeInBits()-1,
Nate Begemanf845b452005-10-08 00:29:44 +00005718 TLI.getShiftAmountTy()));
5719 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
5720 }
5721 }
5722
5723 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
5724 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
5725 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
Chris Lattner1982ef22007-04-11 05:11:38 +00005726 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1) &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00005727 N2.getOperand(0) == N1 && N0.getValueType().isInteger()) {
5728 MVT XType = N0.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00005729 SDValue Shift = DAG.getNode(ISD::SRA, XType, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005730 DAG.getConstant(XType.getSizeInBits()-1,
Chris Lattner1982ef22007-04-11 05:11:38 +00005731 TLI.getShiftAmountTy()));
Dan Gohman475871a2008-07-27 21:46:04 +00005732 SDValue Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00005733 AddToWorkList(Shift.getNode());
5734 AddToWorkList(Add.getNode());
Chris Lattner1982ef22007-04-11 05:11:38 +00005735 return DAG.getNode(ISD::XOR, XType, Add, Shift);
5736 }
5737 // Check to see if this is an integer abs. select_cc setgt X, -1, X, -X ->
5738 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
5739 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT &&
5740 N0 == N2 && N3.getOpcode() == ISD::SUB && N0 == N3.getOperand(1)) {
5741 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N3.getOperand(0))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005742 MVT XType = N0.getValueType();
5743 if (SubC->isNullValue() && XType.isInteger()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005744 SDValue Shift = DAG.getNode(ISD::SRA, XType, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005745 DAG.getConstant(XType.getSizeInBits()-1,
Chris Lattner1982ef22007-04-11 05:11:38 +00005746 TLI.getShiftAmountTy()));
Dan Gohman475871a2008-07-27 21:46:04 +00005747 SDValue Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00005748 AddToWorkList(Shift.getNode());
5749 AddToWorkList(Add.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00005750 return DAG.getNode(ISD::XOR, XType, Add, Shift);
5751 }
5752 }
5753 }
Chris Lattner1982ef22007-04-11 05:11:38 +00005754
Dan Gohman475871a2008-07-27 21:46:04 +00005755 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00005756}
5757
Evan Chengfa1eb272007-02-08 22:13:59 +00005758/// SimplifySetCC - This is a stub for TargetLowering::SimplifySetCC.
Dan Gohman475871a2008-07-27 21:46:04 +00005759SDValue DAGCombiner::SimplifySetCC(MVT VT, SDValue N0,
5760 SDValue N1, ISD::CondCode Cond,
5761 bool foldBooleans) {
Evan Chengfa1eb272007-02-08 22:13:59 +00005762 TargetLowering::DAGCombinerInfo
Duncan Sands25cf2272008-11-24 14:53:14 +00005763 DagCombineInfo(DAG, Level == Unrestricted, false, this);
Evan Chengfa1eb272007-02-08 22:13:59 +00005764 return TLI.SimplifySetCC(VT, N0, N1, Cond, foldBooleans, DagCombineInfo);
Nate Begeman452d7be2005-09-16 00:54:12 +00005765}
5766
Nate Begeman69575232005-10-20 02:15:44 +00005767/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
5768/// return a DAG expression to select that will generate the same value by
5769/// multiplying by a magic number. See:
5770/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Dan Gohman475871a2008-07-27 21:46:04 +00005771SDValue DAGCombiner::BuildSDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00005772 std::vector<SDNode*> Built;
Dan Gohman475871a2008-07-27 21:46:04 +00005773 SDValue S = TLI.BuildSDIV(N, DAG, &Built);
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005774
Andrew Lenharth232c9102006-06-12 16:07:18 +00005775 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005776 ii != ee; ++ii)
5777 AddToWorkList(*ii);
5778 return S;
Nate Begeman69575232005-10-20 02:15:44 +00005779}
5780
5781/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
5782/// return a DAG expression to select that will generate the same value by
5783/// multiplying by a magic number. See:
5784/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Dan Gohman475871a2008-07-27 21:46:04 +00005785SDValue DAGCombiner::BuildUDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00005786 std::vector<SDNode*> Built;
Dan Gohman475871a2008-07-27 21:46:04 +00005787 SDValue S = TLI.BuildUDIV(N, DAG, &Built);
Nate Begeman69575232005-10-20 02:15:44 +00005788
Andrew Lenharth232c9102006-06-12 16:07:18 +00005789 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005790 ii != ee; ++ii)
5791 AddToWorkList(*ii);
5792 return S;
Nate Begeman69575232005-10-20 02:15:44 +00005793}
5794
Jim Laskey71382342006-10-07 23:37:56 +00005795/// FindBaseOffset - Return true if base is known not to alias with anything
5796/// but itself. Provides base object and offset as results.
Dan Gohman475871a2008-07-27 21:46:04 +00005797static bool FindBaseOffset(SDValue Ptr, SDValue &Base, int64_t &Offset) {
Jim Laskey71382342006-10-07 23:37:56 +00005798 // Assume it is a primitive operation.
5799 Base = Ptr; Offset = 0;
5800
5801 // If it's an adding a simple constant then integrate the offset.
5802 if (Base.getOpcode() == ISD::ADD) {
5803 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Base.getOperand(1))) {
5804 Base = Base.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005805 Offset += C->getZExtValue();
Jim Laskey71382342006-10-07 23:37:56 +00005806 }
5807 }
5808
5809 // If it's any of the following then it can't alias with anything but itself.
5810 return isa<FrameIndexSDNode>(Base) ||
5811 isa<ConstantPoolSDNode>(Base) ||
5812 isa<GlobalAddressSDNode>(Base);
5813}
5814
5815/// isAlias - Return true if there is any possibility that the two addresses
5816/// overlap.
Dan Gohman475871a2008-07-27 21:46:04 +00005817bool DAGCombiner::isAlias(SDValue Ptr1, int64_t Size1,
Jim Laskey096c22e2006-10-18 12:29:57 +00005818 const Value *SrcValue1, int SrcValueOffset1,
Dan Gohman475871a2008-07-27 21:46:04 +00005819 SDValue Ptr2, int64_t Size2,
Jim Laskey096c22e2006-10-18 12:29:57 +00005820 const Value *SrcValue2, int SrcValueOffset2)
5821{
Jim Laskey71382342006-10-07 23:37:56 +00005822 // If they are the same then they must be aliases.
5823 if (Ptr1 == Ptr2) return true;
5824
5825 // Gather base node and offset information.
Dan Gohman475871a2008-07-27 21:46:04 +00005826 SDValue Base1, Base2;
Jim Laskey71382342006-10-07 23:37:56 +00005827 int64_t Offset1, Offset2;
5828 bool KnownBase1 = FindBaseOffset(Ptr1, Base1, Offset1);
5829 bool KnownBase2 = FindBaseOffset(Ptr2, Base2, Offset2);
5830
5831 // If they have a same base address then...
5832 if (Base1 == Base2) {
5833 // Check to see if the addresses overlap.
5834 return!((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
5835 }
5836
Jim Laskey096c22e2006-10-18 12:29:57 +00005837 // If we know both bases then they can't alias.
5838 if (KnownBase1 && KnownBase2) return false;
5839
Jim Laskey07a27092006-10-18 19:08:31 +00005840 if (CombinerGlobalAA) {
5841 // Use alias analysis information.
Dan Gohmane9c8fa02007-08-27 16:32:11 +00005842 int64_t MinOffset = std::min(SrcValueOffset1, SrcValueOffset2);
5843 int64_t Overlap1 = Size1 + SrcValueOffset1 - MinOffset;
5844 int64_t Overlap2 = Size2 + SrcValueOffset2 - MinOffset;
Jim Laskey07a27092006-10-18 19:08:31 +00005845 AliasAnalysis::AliasResult AAResult =
Jim Laskey096c22e2006-10-18 12:29:57 +00005846 AA.alias(SrcValue1, Overlap1, SrcValue2, Overlap2);
Jim Laskey07a27092006-10-18 19:08:31 +00005847 if (AAResult == AliasAnalysis::NoAlias)
5848 return false;
5849 }
Jim Laskey096c22e2006-10-18 12:29:57 +00005850
5851 // Otherwise we have to assume they alias.
5852 return true;
Jim Laskey71382342006-10-07 23:37:56 +00005853}
5854
5855/// FindAliasInfo - Extracts the relevant alias information from the memory
5856/// node. Returns true if the operand was a load.
Jim Laskey7ca56af2006-10-11 13:47:09 +00005857bool DAGCombiner::FindAliasInfo(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +00005858 SDValue &Ptr, int64_t &Size,
Jim Laskey096c22e2006-10-18 12:29:57 +00005859 const Value *&SrcValue, int &SrcValueOffset) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00005860 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
5861 Ptr = LD->getBasePtr();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005862 Size = LD->getMemoryVT().getSizeInBits() >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005863 SrcValue = LD->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00005864 SrcValueOffset = LD->getSrcValueOffset();
Jim Laskey71382342006-10-07 23:37:56 +00005865 return true;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005866 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00005867 Ptr = ST->getBasePtr();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005868 Size = ST->getMemoryVT().getSizeInBits() >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005869 SrcValue = ST->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00005870 SrcValueOffset = ST->getSrcValueOffset();
Jim Laskey7ca56af2006-10-11 13:47:09 +00005871 } else {
Jim Laskey71382342006-10-07 23:37:56 +00005872 assert(0 && "FindAliasInfo expected a memory operand");
Jim Laskey71382342006-10-07 23:37:56 +00005873 }
5874
5875 return false;
5876}
5877
Jim Laskey6ff23e52006-10-04 16:53:27 +00005878/// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
5879/// looking for aliasing nodes and adding them to the Aliases vector.
Dan Gohman475871a2008-07-27 21:46:04 +00005880void DAGCombiner::GatherAllAliases(SDNode *N, SDValue OriginalChain,
5881 SmallVector<SDValue, 8> &Aliases) {
5882 SmallVector<SDValue, 8> Chains; // List of chains to visit.
Jim Laskey6ff23e52006-10-04 16:53:27 +00005883 std::set<SDNode *> Visited; // Visited node set.
5884
Jim Laskey279f0532006-09-25 16:29:54 +00005885 // Get alias information for node.
Dan Gohman475871a2008-07-27 21:46:04 +00005886 SDValue Ptr;
Jim Laskey279f0532006-09-25 16:29:54 +00005887 int64_t Size;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005888 const Value *SrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00005889 int SrcValueOffset;
5890 bool IsLoad = FindAliasInfo(N, Ptr, Size, SrcValue, SrcValueOffset);
Jim Laskey279f0532006-09-25 16:29:54 +00005891
Jim Laskey6ff23e52006-10-04 16:53:27 +00005892 // Starting off.
Jim Laskeybc588b82006-10-05 15:07:25 +00005893 Chains.push_back(OriginalChain);
Jim Laskey6ff23e52006-10-04 16:53:27 +00005894
Jim Laskeybc588b82006-10-05 15:07:25 +00005895 // Look at each chain and determine if it is an alias. If so, add it to the
5896 // aliases list. If not, then continue up the chain looking for the next
5897 // candidate.
5898 while (!Chains.empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005899 SDValue Chain = Chains.back();
Jim Laskeybc588b82006-10-05 15:07:25 +00005900 Chains.pop_back();
Jim Laskey6ff23e52006-10-04 16:53:27 +00005901
Jim Laskeybc588b82006-10-05 15:07:25 +00005902 // Don't bother if we've been before.
Gabor Greifba36cb52008-08-28 21:40:38 +00005903 if (Visited.find(Chain.getNode()) != Visited.end()) continue;
5904 Visited.insert(Chain.getNode());
Jim Laskeybc588b82006-10-05 15:07:25 +00005905
5906 switch (Chain.getOpcode()) {
5907 case ISD::EntryToken:
5908 // Entry token is ideal chain operand, but handled in FindBetterChain.
5909 break;
Jim Laskey6ff23e52006-10-04 16:53:27 +00005910
Jim Laskeybc588b82006-10-05 15:07:25 +00005911 case ISD::LOAD:
5912 case ISD::STORE: {
5913 // Get alias information for Chain.
Dan Gohman475871a2008-07-27 21:46:04 +00005914 SDValue OpPtr;
Jim Laskeybc588b82006-10-05 15:07:25 +00005915 int64_t OpSize;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005916 const Value *OpSrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00005917 int OpSrcValueOffset;
Gabor Greifba36cb52008-08-28 21:40:38 +00005918 bool IsOpLoad = FindAliasInfo(Chain.getNode(), OpPtr, OpSize,
Jim Laskey096c22e2006-10-18 12:29:57 +00005919 OpSrcValue, OpSrcValueOffset);
Jim Laskeybc588b82006-10-05 15:07:25 +00005920
5921 // If chain is alias then stop here.
5922 if (!(IsLoad && IsOpLoad) &&
Jim Laskey096c22e2006-10-18 12:29:57 +00005923 isAlias(Ptr, Size, SrcValue, SrcValueOffset,
5924 OpPtr, OpSize, OpSrcValue, OpSrcValueOffset)) {
Jim Laskeybc588b82006-10-05 15:07:25 +00005925 Aliases.push_back(Chain);
5926 } else {
5927 // Look further up the chain.
5928 Chains.push_back(Chain.getOperand(0));
5929 // Clean up old chain.
Gabor Greifba36cb52008-08-28 21:40:38 +00005930 AddToWorkList(Chain.getNode());
Jim Laskey279f0532006-09-25 16:29:54 +00005931 }
Jim Laskeybc588b82006-10-05 15:07:25 +00005932 break;
5933 }
5934
5935 case ISD::TokenFactor:
5936 // We have to check each of the operands of the token factor, so we queue
5937 // then up. Adding the operands to the queue (stack) in reverse order
5938 // maintains the original order and increases the likelihood that getNode
5939 // will find a matching token factor (CSE.)
5940 for (unsigned n = Chain.getNumOperands(); n;)
5941 Chains.push_back(Chain.getOperand(--n));
5942 // Eliminate the token factor if we can.
Gabor Greifba36cb52008-08-28 21:40:38 +00005943 AddToWorkList(Chain.getNode());
Jim Laskeybc588b82006-10-05 15:07:25 +00005944 break;
5945
5946 default:
5947 // For all other instructions we will just have to take what we can get.
5948 Aliases.push_back(Chain);
5949 break;
Jim Laskey279f0532006-09-25 16:29:54 +00005950 }
5951 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00005952}
5953
5954/// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, looking
5955/// for a better chain (aliasing node.)
Dan Gohman475871a2008-07-27 21:46:04 +00005956SDValue DAGCombiner::FindBetterChain(SDNode *N, SDValue OldChain) {
5957 SmallVector<SDValue, 8> Aliases; // Ops for replacing token factor.
Jim Laskey279f0532006-09-25 16:29:54 +00005958
Jim Laskey6ff23e52006-10-04 16:53:27 +00005959 // Accumulate all the aliases to this node.
5960 GatherAllAliases(N, OldChain, Aliases);
5961
5962 if (Aliases.size() == 0) {
5963 // If no operands then chain to entry token.
5964 return DAG.getEntryNode();
5965 } else if (Aliases.size() == 1) {
5966 // If a single operand then chain to it. We don't need to revisit it.
5967 return Aliases[0];
5968 }
5969
5970 // Construct a custom tailored token factor.
Dan Gohman475871a2008-07-27 21:46:04 +00005971 SDValue NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
Jim Laskey6ff23e52006-10-04 16:53:27 +00005972 &Aliases[0], Aliases.size());
5973
5974 // Make sure the old chain gets cleaned up.
Gabor Greifba36cb52008-08-28 21:40:38 +00005975 if (NewChain != OldChain) AddToWorkList(OldChain.getNode());
Jim Laskey6ff23e52006-10-04 16:53:27 +00005976
5977 return NewChain;
Jim Laskey279f0532006-09-25 16:29:54 +00005978}
5979
Nate Begeman1d4d4142005-09-01 00:19:25 +00005980// SelectionDAG::Combine - This is the entry point for the file.
5981//
Duncan Sands25cf2272008-11-24 14:53:14 +00005982void SelectionDAG::Combine(CombineLevel Level, AliasAnalysis &AA, bool Fast) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00005983 /// run - This is the main entry point to this class.
5984 ///
Duncan Sands25cf2272008-11-24 14:53:14 +00005985 DAGCombiner(*this, AA, Fast).Run(Level);
Nate Begeman1d4d4142005-09-01 00:19:25 +00005986}